- fix Building without Nagra not possible at Nagra_Merlin https://trac.streamboard...
[oscam.git] / module-gbox-sms.c
blob0a0fbee6ba68de0656d53aa465247aa4901138e3
1 #define MODULE_LOG_PREFIX "gbox/sms"
3 #include "globals.h"
5 #ifdef MODULE_GBOX
6 #include "module-gbox.h"
7 #include "module-gbox-sms.h"
8 #include "oscam-string.h"
9 #include "oscam-files.h"
10 #include "oscam-string.h"
11 #include "oscam-client.h"
12 #include "oscam-time.h"
13 #include "oscam-lock.h"
15 static int32_t poll_gsms_data(uint16_t *boxid, uint8_t *num, char *text)
17 char *fext= FILE_GSMS_TXT;
18 char *fname = get_gbox_tmp_fname(fext);
20 FILE *fhandle = fopen(fname, "r");
21 if(!fhandle)
23 //cs_log("Couldn't open %s: %s", fname, strerror(errno));
24 return -2;
27 uint32_t length1;
28 uint8_t length;
29 char buffer[140];
30 char *tail;
32 memset(buffer, 0, sizeof(buffer));
33 fseek(fhandle, 0, SEEK_END);
34 length1 = ftell(fhandle);
35 fseek(fhandle, 0, SEEK_SET);
37 if(length1 < 13)
39 cs_log("GSMS failed. Min msg char in %s = 6, actual = %d", fname, length1 - 7);
40 fclose(fhandle);
41 unlink(fname);
42 return -1;
45 if(fgets(buffer, 140, fhandle) != NULL)
47 *boxid = strtol(buffer, &tail, 16);
48 *num = atoi(tail);
51 fclose(fhandle);
52 unlink(fname);
54 if(length1 > 127 + 7)
56 length = 127 + 7;
58 else
60 length = length1;
63 cs_log_dbg(D_READER, "total msg length taken from %s = %d, limited to %d", fname, length1, length);
64 cs_strncpy(text, buffer + 7, sizeof(buffer));
66 return 0;
69 static void write_gsms_to_osd_file(struct s_client *cli, uint8_t *gsms)
71 char *fext= FILE_OSD_MSG;
72 char *fname = get_gbox_tmp_fname(fext);
74 if(file_exists(fname))
76 char gsms_buf[150];
77 uint8_t i;
79 // allow only alphanumerical characters in osd gsms due to safety reasons
80 for(i = 0; i < cs_strlen((char *)gsms); i++)
82 if(!isalnum(gsms[i]) && gsms[i] != ' ')
84 gsms[i] = '_';
88 memset(gsms_buf, 0, sizeof(gsms_buf));
89 snprintf(gsms_buf, sizeof(gsms_buf), "%s %s:%s %s", fname, username(cli), cli->reader->device, gsms);
90 cs_log_dbg(D_READER, "found OSD 'driver' %s - write gsms to OSD", fname);
92 char *cmd = gsms_buf;
93 FILE *p;
94 if((p = popen(cmd, "w")) == NULL)
96 cs_log("Error %s", fname);
97 return;
100 pclose(p);
103 return;
106 void write_gsms_ack(struct s_client *cli)
108 char tsbuf[28];
109 time_t walltime = cs_time();
110 cs_ctime_r(&walltime, tsbuf);
111 struct gbox_peer *peer = cli->gbox;
112 char *fext = FILE_GSMS_ACK;
113 char *fname = get_gbox_tmp_fname(fext);
115 FILE *fhandle = fopen(fname, "a+");
116 if(!fhandle)
118 cs_log("Couldn't open %s: %s", fname, strerror(errno));
119 return;
122 fprintf(fhandle, "Peer %04X (%s) confirmed receipt of GSMS on %s", peer->gbox.id, cli->reader->device, tsbuf);
123 fclose(fhandle);
124 return;
127 static void write_gsms_nack(struct s_client *cl, uint8_t inf)
129 char tsbuf[28];
130 time_t walltime = cs_time();
131 cs_ctime_r(&walltime, tsbuf);
132 struct gbox_peer *peer = cl->gbox;
133 char *fext = FILE_GSMS_NACK;
134 char *fname = get_gbox_tmp_fname(fext);
136 FILE *fhandle = fopen(fname, "a+");
137 if(!fhandle)
139 cs_log("Couldn't open %s: %s", fname, strerror(errno));
140 return;
143 if(inf)
145 fprintf(fhandle, "INFO: GSMS to all: Peer %04X (%s) was OFFLINE %s",
146 peer->gbox.id, cl->reader->device,tsbuf);
148 else
150 fprintf(fhandle, "WARNING: Private GSMS to Peer %04X (%s) failed - was OFFLINE %s",
151 peer->gbox.id, cl->reader->device,tsbuf);
154 fclose(fhandle);
155 return;
158 void write_gsms_msg(struct s_client *cli, uint8_t *gsms, uint16_t type, uint16_t UNUSED(msglen))
160 char tsbuf[28];
161 time_t walltime = cs_time();
162 cs_ctime_r(&walltime, tsbuf);
163 struct gbox_peer *peer = cli->gbox;
164 struct s_reader *rdr = cli->reader;
165 char *fext = FILE_GSMS_MSG;
166 char *fname = get_gbox_tmp_fname(fext);
168 FILE *fhandle = fopen(fname, "a+");
169 if(!fhandle)
171 cs_log("Couldn't open %s: %s", fname, strerror(errno));
172 return;
175 if(type == 0x30)
177 fprintf(fhandle, "Normal message received from %04X %s on %s%s\n\n", peer->gbox.id, cli->reader->device, tsbuf, gsms);
178 rdr->gbox_gsms_peer = peer->gbox.id;
179 snprintf(rdr->last_gsms, sizeof(rdr->last_gsms), "%s %s", gsms, tsbuf); // for easy handling of gsms by webif
181 else if(type == 0x31)
183 fprintf(fhandle, "OSD message received from %04X %s on %s%s\n\n", peer->gbox.id, cli->reader->device, tsbuf, gsms);
184 write_gsms_to_osd_file(cli, gsms);
185 rdr->gbox_gsms_peer = peer->gbox.id;
186 snprintf(rdr->last_gsms, sizeof(rdr->last_gsms), "%s %s", gsms, tsbuf); // for easy handling of gsms by webif
188 else
190 fprintf(fhandle, "Corrupted message received from %04X %s on %s%s\n\n", peer->gbox.id, cli->reader->device, tsbuf, gsms);
193 fclose(fhandle);
194 return;
197 void gsms_unavail(void)
199 cs_log("INFO: GSMS feature disabled by conf");
202 static void gbox_send_gsms2peer(struct s_client *cl, char *gsms, uint8_t msg_type, int8_t gsms_len)
204 uint8_t outbuf[150];
205 struct gbox_peer *peer = cl->gbox;
206 uint16_t local_gbox_id = gbox_get_local_gbox_id();
207 uint32_t local_gbox_pw = gbox_get_local_gbox_password();
208 struct s_reader *rdr = cl->reader;
210 gbox_message_header(outbuf, MSG_GSMS, peer->gbox.password, local_gbox_pw);
212 outbuf[10] = (peer->gbox.id >> 8) & 0xff;
213 outbuf[11] = peer->gbox.id & 0xff;
214 outbuf[12] = (local_gbox_id >> 8) & 0xff;
215 outbuf[13] = local_gbox_id & 0xff;
216 outbuf[14] = msg_type;
217 outbuf[15] = gsms_len;
218 memcpy(outbuf + 16, gsms, gsms_len);
219 outbuf[16 + gsms_len] = 0;
221 cs_log("<-[gbx] send GSMS to %s:%d id: %04X", rdr->device, rdr->r_port, peer->gbox.id);
222 gbox_send(cl, outbuf, gsms_len + 17);
223 return;
225 int gbox_direct_send_gsms(uint16_t boxid, uint8_t num, char *gsms)
227 uint8_t msg_type = 0, gsms_len = 0;
228 int peer_found = 0;
229 char text[GBOX_MAX_MSG_TXT + 1];
231 memset(text, 0, sizeof(text));
233 if(cfg.gsms_dis)
235 gsms_unavail();
236 return 0;
239 gsms_len = cs_strlen(gsms);
240 if(gsms_len < 6)
242 cs_log("GBOX: message to send to peer is too short 6 chars expected and %d received text[%s]", gsms_len, gsms);
244 else if(gsms_len > GBOX_MAX_MSG_TXT)
246 gsms_len = GBOX_MAX_MSG_TXT;
247 cs_log("GBOX message is too long so it will be truncated to max. [%d]", GBOX_MAX_MSG_TXT);
250 cs_strncpy(text, gsms, sizeof(text));
252 switch(num)
254 case 0:
255 msg_type = 0x30;
256 break;
258 case 1:
259 msg_type = 0x31;
260 break;
262 //case 2:
263 // gsms_prot = 2;
264 // msg_type = 0x30;
265 // break;
267 //case 3:
268 // gsms_prot = 2;
269 // msg_type = 0x31;
270 // break;
272 default:
273 cs_log("ERROR unknown gsms protocol");
274 return 0;
277 cs_log_dbg(D_READER,"init gsms_length=%d msg_type=%02X ", gsms_len, msg_type);
278 struct s_client *cl;
279 cs_readlock(__func__, &clientlist_lock);
281 for(cl = first_client; cl; cl = cl->next)
283 peer_found=0;
284 if(cl->gbox && cl->typ == 'p')
286 struct gbox_peer *peer = cl->gbox;
287 if(peer->online && boxid == 0xFFFF) // send gsms to all peers online
289 gbox_send_gsms2peer(cl, text, msg_type, gsms_len);
290 peer_found=1;
293 if(!peer->online && boxid == 0xFFFF)
295 cs_log("GBOX Info: peer %04X is OFFLINE", peer->gbox.id);
296 write_gsms_nack( cl, 1);
299 if(peer->online && boxid == peer->gbox.id)
301 gbox_send_gsms2peer(cl, text, msg_type, gsms_len);
302 peer_found=1;
305 if(!peer->online && boxid == peer->gbox.id)
307 cs_log("GBOX WARNING: send GSMS failed - peer %04X is OFFLINE", peer->gbox.id);
308 write_gsms_nack( cl, 0);
313 cs_readunlock(__func__, &clientlist_lock);
314 return peer_found;
317 void gbox_get_online_peers(void)
319 int n = 0, i;
320 struct s_client *cl;
322 for(i = 0; i < GBOX_MAX_DEST_PEERS; i++)
324 cfg.gbox_dest_peers[i] = '\0';
327 cfg.gbox_dest_peers_num = 0;
328 cs_readlock(__func__, &clientlist_lock);
330 for(cl = first_client; cl; cl = cl->next)
332 if(cl->gbox && cl->typ == 'p' && n < GBOX_MAX_DEST_PEERS)
334 struct gbox_peer *peer = cl->gbox;
335 if(peer->online) // peer is online
337 cfg.gbox_dest_peers[n++] = peer->gbox.id;
342 cs_readunlock(__func__, &clientlist_lock);
343 cfg.gbox_dest_peers_num = n;
344 return;
347 void gbox_init_send_gsms(void)
349 uint16_t boxid = 0;
350 uint8_t num = 0;
351 uint8_t msg_type = 0;
352 int32_t poll_result = 0;
353 char text[150];
354 memset(text, 0, sizeof(text));
355 char *fext = FILE_GSMS_TXT;
356 char *fname = get_gbox_tmp_fname(fext);
358 if(cfg.gsms_dis)
360 unlink(fname);
361 gsms_unavail();
362 return;
365 poll_result = poll_gsms_data( &boxid, &num, text);
366 if(poll_result)
368 if(poll_result != -2)
370 cs_log("ERROR polling file %s", fname);
373 return;
376 int8_t gsms_len = cs_strlen(text);
377 cs_log_dbg(D_READER,"got from %s: box_ID = %04X num = %d gsms_length = %d txt = %s", fname, boxid, num, gsms_len, text);
379 switch(num)
381 case 0:
382 msg_type = 0x30;
383 break;
385 case 1:
386 msg_type = 0x31;
387 break;
389 //case 2:
390 // gsms_prot = 2;
391 // msg_type = 0x30;
392 // break;
394 //case 3:
395 // gsms_prot = 2;
396 // msg_type = 0x31;
397 // break;
399 default:
400 cs_log("ERROR unknown gsms protocol");
401 return;
404 cs_log_dbg(D_READER,"init gsms to boxid= %04X length= %d msg_type= %02X ", boxid, gsms_len, msg_type);
406 uint8_t id_valid = 0;
407 struct s_client *cl;
408 cs_readlock(__func__, &clientlist_lock);
410 for(cl = first_client; cl; cl = cl->next)
412 if(cl->gbox && cl->typ == 'p')
414 struct gbox_peer *peer = cl->gbox;
416 if(peer->online && boxid == 0xFFFF) // send gsms to all peers online
418 gbox_send_gsms2peer(cl, text, msg_type, gsms_len);
419 id_valid = 1;
422 if(!peer->online && boxid == 0xFFFF)
424 cs_log("Info: peer %04X is OFFLINE", peer->gbox.id);
425 write_gsms_nack( cl, 1);
426 id_valid = 1;
429 if(peer->online && boxid == peer->gbox.id)
431 gbox_send_gsms2peer(cl, text, msg_type, gsms_len);
432 id_valid = 1;
435 if(!peer->online && boxid == peer->gbox.id)
437 cs_log("WARNING: send GSMS failed - peer %04X is OFFLINE", peer->gbox.id);
438 write_gsms_nack( cl, 0);
439 id_valid = 1;
444 cs_readunlock(__func__, &clientlist_lock);
446 if(!id_valid)
448 cs_log("WARNING: send GSMS failed - peer_id unknown");
451 return;
454 void gbox_send_gsms_ack(struct s_client *cli)
456 uint8_t outbuf[20];
457 struct gbox_peer *peer = cli->gbox;
458 uint16_t local_gbox_id = gbox_get_local_gbox_id();
459 uint32_t local_gbox_pw = gbox_get_local_gbox_password();
460 struct s_reader *rdr = cli->reader;
462 if(peer->online)
464 gbox_message_header(outbuf, MSG_GSMS_ACK, peer->gbox.password, local_gbox_pw);
466 outbuf[10] = 0;
467 outbuf[11] = 0;
468 outbuf[12] = (local_gbox_id >> 8) & 0xff;
469 outbuf[13] = local_gbox_id & 0xff;
470 outbuf[14] = 0x1;
471 outbuf[15] = 0;
473 cs_log_dbg(D_READER,"<-[gbx] send GSMS_ACK to %s:%d id: %04X", rdr->device, rdr->r_port, peer->gbox.id);
474 gbox_send(cli, outbuf, 16);
478 #endif