-
Notifications
You must be signed in to change notification settings - Fork 0
/
module-gbox-sms.c
302 lines (289 loc) · 8.58 KB
/
module-gbox-sms.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#include "globals.h"
#ifdef MODULE_GBOX
#include "module-gbox.h"
#include "module-gbox-sms.h"
#include "oscam-string.h"
#include "oscam-files.h"
#include "oscam-string.h"
#include "oscam-client.h"
#include "oscam-time.h"
static uint32_t poll_gsms_data (uint16_t *boxid, uint8_t *num, char *text)
{
char *fext= FILE_GSMS_TXT;
char *fname = get_gbox_tmp_fname(fext);
FILE *fhandle = fopen(fname, "r");
if(!fhandle)
{
cs_log("Couldn't open %s: %s", fname, strerror(errno));
return -1;
}
uint32_t length1;
uint8_t length;
char buffer[140];
char *tail;
memset(buffer, 0, sizeof(buffer));
fseek (fhandle,0L,SEEK_END);
length1 = ftell(fhandle);
fseek (fhandle,0L,SEEK_SET);
if (length1 < 13)
{
cs_log("GSMS: min msg char in %s = 6, actual = %d",fname, length1-7);
fclose(fhandle);
unlink(fname);
return -1;
}
if(fgets(buffer,140,fhandle) != NULL)
{
*boxid = strtol (buffer, &tail, 16);
*num = atoi (tail);
}
fclose(fhandle);
unlink(fname);
if (length1 > (127+7))
{
length = 127+7;
}
else
{
length = length1;
}
cs_debug_mask(D_READER, "GSMS: total msg length taken from %s = %d, limitted to %d",fname, length1, length);
strncpy(text, &(buffer[7]),length-7);
return 0;
}
static void write_gsms_to_osd_file(struct s_client *cli, unsigned char *gsms)
{
char *fext= FILE_OSD_MSG;
char *fname = get_gbox_tmp_fname(fext);
if (file_exists(fname))
{
char gsms_buf[150];
memset(gsms_buf, 0, sizeof(gsms_buf));
snprintf(gsms_buf, sizeof(gsms_buf), "%s %s:%s %s", fname, username(cli), cli->reader->device, gsms);
cs_debug_mask(D_READER, "GSMS: found OSD 'driver' %s - write gsms to OSD", fname);
char *cmd = gsms_buf;
FILE *p;
if ((p = popen(cmd, "w")) == NULL)
{
cs_log("Error %s",fname);
return;
}
pclose(p);
}
return;
}
void write_gsms_ack (struct s_client *cli, uint8_t gsms_prot)
{
char tsbuf[28];
time_t walltime = cs_time();
cs_ctime_r(&walltime, tsbuf);
struct gbox_peer *peer = cli->gbox;
char *fext= FILE_GSMS_ACK;
char *fname = get_gbox_tmp_fname(fext);
FILE *fhandle = fopen(fname, "a+");
if(!fhandle)
{
cs_log("Couldn't open %s: %s", fname, strerror(errno));
return;
}
fprintf(fhandle, "Peer %04X (%s) confirmed receipt of GSMS_%d on %s",peer->gbox.id, cli->reader->device, gsms_prot, tsbuf);
fclose(fhandle);
return;
}
static void write_gsms_nack (struct s_client *cl, uint8_t gsms_prot, uint8_t inf)
{
char tsbuf[28];
time_t walltime = cs_time();
cs_ctime_r(&walltime, tsbuf);
struct gbox_peer *peer = cl->gbox;
char *fext= FILE_GSMS_NACK;
char *fname = get_gbox_tmp_fname(fext);
FILE *fhandle = fopen(fname, "a+");
if(!fhandle)
{
cs_log("Couldn't open %s: %s", fname, strerror(errno));
return;
}
if(inf)
{
fprintf(fhandle, "INFO: GSMS_%d to all: Peer %04X (%s) was OFFLINE %s",gsms_prot,peer->gbox.id, cl->reader->device,tsbuf);
}
else
{
fprintf(fhandle, "WARNING: GSMS_%d private to Peer %04X (%s) failed - was OFFLINE %s",gsms_prot,peer->gbox.id, cl->reader->device,tsbuf);
}
fclose(fhandle);
return;
}
void write_gsms_msg (struct s_client *cli, uchar *gsms, uint16_t type, uint16_t UNUSED(msglen))
{
char tsbuf[28];
time_t walltime = cs_time();
cs_ctime_r(&walltime, tsbuf);
struct gbox_peer *peer = cli->gbox;
struct s_reader *rdr = cli->reader;
char *fext= FILE_GSMS_MSG;
char *fname = get_gbox_tmp_fname(fext);
FILE *fhandle = fopen(fname, "a+");
if(!fhandle)
{
cs_log("Couldn't open %s: %s", fname, strerror(errno));
return;
}
if(type == 0x30)
{
fprintf(fhandle, "Normal message received from %04X %s on %s%s\n\n",peer->gbox.id, cli->reader->device, tsbuf, gsms);
snprintf(rdr->last_gsms, sizeof(rdr->last_gsms), "%s %s", gsms, tsbuf); //added for easy handling of gsms by webif
}
else if(type == 0x31)
{
fprintf(fhandle, "OSD message received from %04X %s on %s%s\n\n",peer->gbox.id, cli->reader->device, tsbuf, gsms);
write_gsms_to_osd_file(cli, gsms);
snprintf(rdr->last_gsms, sizeof(rdr->last_gsms), "%s %s", gsms, tsbuf); //added for easy handling of gsms by webif
}
else
{fprintf(fhandle, "Corrupted message received from %04X %s on %s%s\n\n",peer->gbox.id, cli->reader->device, tsbuf, gsms);}
fclose(fhandle);
return;
}
void gsms_unavail(void)
{
cs_log("INFO: GSMS feature disabled by conf");
}
static void gbox_send_gsms2peer(struct s_client *cl, char *gsms, uint8_t msg_type, uint8_t gsms_prot, int8_t gsms_len)
{
uchar outbuf[150];
struct gbox_peer *peer = cl->gbox;
uint16_t local_gbox_id = gbox_get_local_gbox_id();
uint32_t local_gbox_pw = gbox_get_local_gbox_password();
struct s_reader *rdr = cl->reader;
if (gsms_prot == 1)
{
gbox_code_cmd(outbuf, MSG_GSMS_1);
outbuf[2] = gsms_len; // gsms len
outbuf[3] = msg_type; //msg type
memcpy(&outbuf[4], gsms,gsms_len);
cs_log("<-[gbx] send GSMS_1 to %s:%d id: %04X", rdr->device, rdr->r_port, peer->gbox.id);
gbox_send(cl, outbuf, gsms_len + 4);
}
if (gsms_prot == 2)
{
gbox_code_cmd(outbuf, MSG_GSMS_2);
memcpy(outbuf + 2, peer->gbox.password, 4);
outbuf[6] = (local_gbox_pw >> 24) & 0xff;
outbuf[7] = (local_gbox_pw >> 16) & 0xff;
outbuf[8] = (local_gbox_pw >> 8) & 0xff;
outbuf[9] = local_gbox_pw & 0xff;
outbuf[10] = (peer->gbox.id >> 8) & 0xff;
outbuf[11] = peer->gbox.id & 0xff;
outbuf[12] = (local_gbox_id >> 8) & 0xff;
outbuf[13] = local_gbox_id & 0xff;
outbuf[14] = msg_type; //msg type
outbuf[15] = gsms_len; // gsms length
memcpy(&outbuf[16], gsms,gsms_len);
outbuf[16 + gsms_len] = 0; //last byte 0x00
cs_log("<-[gbx] send GSMS_2 to %s:%d id: %04X", rdr->device, rdr->r_port, peer->gbox.id);
gbox_send(cl, outbuf, gsms_len + 17);
}
return;
}
void gbox_init_send_gsms(void)
{
uint16_t boxid = 0;
uint8_t num = 0;
uint8_t gsms_prot = 0;
uint8_t msg_type = 0;
char text[150];
memset(text, 0, sizeof(text));
char *fext= FILE_GSMS_TXT;
char *fname = get_gbox_tmp_fname(fext);
if(cfg.gsms_dis)
{
unlink(fname);
gsms_unavail();
return;
}
if (poll_gsms_data( &boxid, &num, text))
{
cs_log("GSMS: ERROR polling file %s", fname);
return;
}
int8_t gsms_len = strlen(text);
cs_debug_mask(D_READER,"GSMS: got from %s: box_ID = %04X num = %d gsms_length = %d txt = %s",fname, boxid, num, gsms_len, text);
switch(num)
{
case 0: {gsms_prot = 1; msg_type = 0x30; break;}
case 1: {gsms_prot = 1; msg_type = 0x31; break;}
case 2: {gsms_prot = 2; msg_type = 0x30; break;}
case 3: {gsms_prot = 2; msg_type = 0x31; break;}
default:{cs_log("GSMS: ERROR unknown gsms protocol"); return;}
}
cs_debug_mask(D_READER,"init GSMS: gsms_length=%d msg_type=%02X msg_prot=%d",gsms_len, msg_type, gsms_prot);
struct s_client *cl;
for (cl = first_client; cl; cl = cl->next)
{
if(cl->gbox && cl->typ == 'p')
{
struct gbox_peer *peer = cl->gbox;
if (peer->online && boxid == 0xFFFF) //send gsms to all peers online
{
gbox_send_gsms2peer(cl, text, msg_type, gsms_prot, gsms_len);
}
if (!peer->online && boxid == 0xFFFF)
{
cs_log("Info: peer %04X is OFFLINE",peer->gbox.id);
write_gsms_nack( cl, gsms_prot, 1);
}
if (peer->online && boxid == peer->gbox.id)
{
gbox_send_gsms2peer(cl, text, msg_type, gsms_prot, gsms_len);
}
if (!peer->online && boxid == peer->gbox.id)
{
cs_log("WARNING: send GSMS failed - peer %04X is OFFLINE",peer->gbox.id);
write_gsms_nack( cl, gsms_prot, 0);
}
}
}
return;
}
void gbox_send_gsms_ack(struct s_client *cli, uint8_t gsms_prot)
{
uchar outbuf[20];
struct gbox_peer *peer = cli->gbox;
uint16_t local_gbox_id = gbox_get_local_gbox_id();
uint32_t local_gbox_pw = gbox_get_local_gbox_password();
struct s_reader *rdr = cli->reader;
if (peer->online && gsms_prot == 1)
{
gbox_code_cmd(outbuf, MSG_GSMS_ACK_1);
outbuf[2] = 0x90;
outbuf[3] = 0x98;
outbuf[4] = 0x90;
outbuf[5] = 0x98;
outbuf[6] = 0x90;
outbuf[7] = 0x98;
outbuf[8] = 0x90;
outbuf[9] = 0x98;
gbox_send(cli, outbuf, 10);
cs_debug_mask(D_READER,"<-[gbx] send GSMS_ACK_1 to %s:%d id: %04X",rdr->device, rdr->r_port, peer->gbox.id);
}
if (peer->online && gsms_prot == 2)
{
gbox_code_cmd(outbuf, MSG_GSMS_ACK_2);
memcpy(outbuf + 2, peer->gbox.password, 4);
outbuf[6] = (local_gbox_pw >> 24) & 0xff;
outbuf[7] = (local_gbox_pw >> 16) & 0xff;
outbuf[8] = (local_gbox_pw >> 8) & 0xff;
outbuf[9] = local_gbox_pw & 0xff;
outbuf[10] = 0;
outbuf[11] = 0;
outbuf[12] = (local_gbox_id >> 8) & 0xff;
outbuf[13] = local_gbox_id & 0xff;
outbuf[14] = 0x1;
outbuf[15] = 0;
cs_debug_mask(D_READER,"<-[gbx] send GSMS_ACK_2 to %s:%d id: %04X",rdr->device, rdr->r_port, peer->gbox.id);
gbox_send(cli, outbuf, 16);
}
}
#endif