1 #define MODULE_LOG_PREFIX "gbox/sms"
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");
23 //cs_log("Couldn't open %s: %s", fname, strerror(errno));
32 memset(buffer
, 0, sizeof(buffer
));
33 fseek(fhandle
, 0, SEEK_END
);
34 length1
= ftell(fhandle
);
35 fseek(fhandle
, 0, SEEK_SET
);
39 cs_log("GSMS failed. Min msg char in %s = 6, actual = %d", fname
, length1
- 7);
45 if(fgets(buffer
, 140, fhandle
) != NULL
)
47 *boxid
= strtol(buffer
, &tail
, 16);
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
));
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
))
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
] != ' ')
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
);
94 if((p
= popen(cmd
, "w")) == NULL
)
96 cs_log("Error %s", fname
);
106 void write_gsms_ack(struct s_client
*cli
)
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+");
118 cs_log("Couldn't open %s: %s", fname
, strerror(errno
));
122 fprintf(fhandle
, "Peer %04X (%s) confirmed receipt of GSMS on %s", peer
->gbox
.id
, cli
->reader
->device
, tsbuf
);
127 static void write_gsms_nack(struct s_client
*cl
, uint8_t inf
)
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+");
139 cs_log("Couldn't open %s: %s", fname
, strerror(errno
));
145 fprintf(fhandle
, "INFO: GSMS to all: Peer %04X (%s) was OFFLINE %s",
146 peer
->gbox
.id
, cl
->reader
->device
,tsbuf
);
150 fprintf(fhandle
, "WARNING: Private GSMS to Peer %04X (%s) failed - was OFFLINE %s",
151 peer
->gbox
.id
, cl
->reader
->device
,tsbuf
);
158 void write_gsms_msg(struct s_client
*cli
, uint8_t *gsms
, uint16_t type
, uint16_t UNUSED(msglen
))
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+");
171 cs_log("Couldn't open %s: %s", fname
, strerror(errno
));
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
190 fprintf(fhandle
, "Corrupted message received from %04X %s on %s%s\n\n", peer
->gbox
.id
, cli
->reader
->device
, tsbuf
, gsms
);
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
)
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);
225 int gbox_direct_send_gsms(uint16_t boxid
, uint8_t num
, char *gsms
)
227 uint8_t msg_type
= 0, gsms_len
= 0;
229 char text
[GBOX_MAX_MSG_TXT
+ 1];
231 memset(text
, 0, sizeof(text
));
239 gsms_len
= cs_strlen(gsms
);
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
));
273 cs_log("ERROR unknown gsms protocol");
277 cs_log_dbg(D_READER
,"init gsms_length=%d msg_type=%02X ", gsms_len
, msg_type
);
279 cs_readlock(__func__
, &clientlist_lock
);
281 for(cl
= first_client
; cl
; cl
= cl
->next
)
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
);
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
);
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
);
317 void gbox_get_online_peers(void)
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
;
347 void gbox_init_send_gsms(void)
351 uint8_t msg_type
= 0;
352 int32_t poll_result
= 0;
354 memset(text
, 0, sizeof(text
));
355 char *fext
= FILE_GSMS_TXT
;
356 char *fname
= get_gbox_tmp_fname(fext
);
365 poll_result
= poll_gsms_data( &boxid
, &num
, text
);
368 if(poll_result
!= -2)
370 cs_log("ERROR polling file %s", fname
);
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
);
400 cs_log("ERROR unknown gsms protocol");
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;
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
);
422 if(!peer
->online
&& boxid
== 0xFFFF)
424 cs_log("Info: peer %04X is OFFLINE", peer
->gbox
.id
);
425 write_gsms_nack( cl
, 1);
429 if(peer
->online
&& boxid
== peer
->gbox
.id
)
431 gbox_send_gsms2peer(cl
, text
, msg_type
, gsms_len
);
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);
444 cs_readunlock(__func__
, &clientlist_lock
);
448 cs_log("WARNING: send GSMS failed - peer_id unknown");
454 void gbox_send_gsms_ack(struct s_client
*cli
)
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
;
464 gbox_message_header(outbuf
, MSG_GSMS_ACK
, peer
->gbox
.password
, local_gbox_pw
);
468 outbuf
[12] = (local_gbox_id
>> 8) & 0xff;
469 outbuf
[13] = local_gbox_id
& 0xff;
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);