1 | #include "globals.h"
|
---|
2 | #ifdef MODULE_SERIAL
|
---|
3 | #include "oscam-client.h"
|
---|
4 | #include "oscam-ecm.h"
|
---|
5 | #include "oscam-net.h"
|
---|
6 | #include "oscam-string.h"
|
---|
7 | #include "oscam-time.h"
|
---|
8 |
|
---|
9 | #define HSIC_CRC 0xA5
|
---|
10 | #define SSSP_MAX_PID 8
|
---|
11 |
|
---|
12 | #define P_HSIC 1 // Humax Sharing Interface Client
|
---|
13 | #define P_SSSP 2 // Simple Serial Sharing Protocol
|
---|
14 | #define P_BOMBA 3 // This is not really a Protocol
|
---|
15 | #define P_DSR95 4 // DSR9500 with SID
|
---|
16 | #define P_GS 5 // GS7001
|
---|
17 | #define P_ALPHA 6 // AlphaStar Receivers
|
---|
18 | #define P_DSR95_OLD 7 // DSR9500 without SID
|
---|
19 | #define P_GBOX 8 // Arion with gbox
|
---|
20 | #define P_MAX P_GBOX
|
---|
21 | #define P_AUTO 0xFF
|
---|
22 |
|
---|
23 | #define P_DSR_AUTO 0
|
---|
24 | #define P_DSR_GNUSMAS 1
|
---|
25 | #define P_DSR_OPEN 2
|
---|
26 | #define P_DSR_PIONEER 3
|
---|
27 | #define P_DSR_WITHSID 4
|
---|
28 | #define P_DSR_UNKNOWN 5
|
---|
29 |
|
---|
30 | #define IS_ECM 0 // incoming data is ECM
|
---|
31 | #define IS_DCW 1 // incoming data is DCW
|
---|
32 | #define IS_PMT 2 // incoming data is PMT
|
---|
33 | #define IS_LGO 3 // incoming data is client logon
|
---|
34 | #define IS_ECHO 4 // incoming data is DCW echo from Samsung
|
---|
35 | #define IS_CAT 5 // incoming data is CAT
|
---|
36 | #define IS_BAD 0xFF // incoming data is unknown
|
---|
37 |
|
---|
38 | static const char * const proto_txt[]={"unknown", "hsic", "sssp", "bomba", "dsr9500", "gs",
|
---|
39 | "alpha", "dsr9500old", "gbox"};
|
---|
40 | static const char * const dsrproto_txt[]={"unknown", "samsung", "openbox", "pioneer",
|
---|
41 | "extended", "unknown"};
|
---|
42 | static const char * const incomplete="incomplete request (%d bytes)";
|
---|
43 |
|
---|
44 | typedef struct s_gbox
|
---|
45 | {
|
---|
46 | int32_t cat_len;
|
---|
47 | int32_t pmt_len;
|
---|
48 | int32_t ecm_len;
|
---|
49 | } GBOX_LENS;
|
---|
50 |
|
---|
51 | typedef struct s_sssp
|
---|
52 | {
|
---|
53 | uint16_t caid;
|
---|
54 | uint16_t pid;
|
---|
55 | uint32_t prid;
|
---|
56 | } SSSP_TAB;
|
---|
57 |
|
---|
58 | //added to support multiple instances with thread
|
---|
59 | struct s_serial_client
|
---|
60 | {
|
---|
61 | int32_t connected;
|
---|
62 | struct timeb tps;
|
---|
63 | struct timeb tpe;
|
---|
64 | char oscam_ser_usr[32];
|
---|
65 | char oscam_ser_device[64];
|
---|
66 | speed_t oscam_ser_baud;
|
---|
67 | int32_t oscam_ser_delay;
|
---|
68 | int32_t oscam_ser_timeout;
|
---|
69 | int32_t oscam_ser_proto;
|
---|
70 | int32_t serial_errors;
|
---|
71 | int32_t dsr9500type;
|
---|
72 | int32_t samsung_0a; // number of 0A in ALL dcw sent into samsung
|
---|
73 | int32_t samsung_dcw; // number of dcw sent into samsung before echo or ecm is received
|
---|
74 |
|
---|
75 | GBOX_LENS gbox_lens;
|
---|
76 | SSSP_TAB sssp_tab[SSSP_MAX_PID];
|
---|
77 | uint16_t sssp_srvid;
|
---|
78 | int32_t sssp_num;
|
---|
79 | int32_t sssp_fix;
|
---|
80 | };
|
---|
81 |
|
---|
82 | pthread_mutex_t mutex;
|
---|
83 | pthread_cond_t cond;
|
---|
84 | int32_t bcopy_end = -1;
|
---|
85 |
|
---|
86 | struct s_thread_param
|
---|
87 | {
|
---|
88 | uint8_t module_idx;
|
---|
89 | struct s_serial_client serialdata;
|
---|
90 | };
|
---|
91 |
|
---|
92 | static int32_t chk_ser_srvid_match(uint16_t caid, uint16_t sid, uint32_t provid, SIDTAB *sidtab)
|
---|
93 | {
|
---|
94 | int32_t i, rc=0;
|
---|
95 |
|
---|
96 | if (!sidtab->num_caid)
|
---|
97 | rc|=1;
|
---|
98 | else
|
---|
99 | for (i=0; (i<sidtab->num_caid) && (!(rc&1)); i++)
|
---|
100 | if (caid==sidtab->caid[i]) rc|=1;
|
---|
101 |
|
---|
102 | if (!sidtab->num_provid)
|
---|
103 | rc|=2;
|
---|
104 | else
|
---|
105 | for (i=0; (i<sidtab->num_provid) && (!(rc&2)); i++)
|
---|
106 | if (provid==sidtab->provid[i]) rc|=2;
|
---|
107 |
|
---|
108 | if (!sidtab->num_srvid)
|
---|
109 | rc|=4;
|
---|
110 | else
|
---|
111 | for (i=0; (i<sidtab->num_srvid) && (!(rc&4)); i++)
|
---|
112 | if (sid==sidtab->srvid[i]) rc|=4;
|
---|
113 |
|
---|
114 | return(rc==7);
|
---|
115 | }
|
---|
116 |
|
---|
117 | static int32_t chk_ser_srvid(struct s_client *cl, uint16_t caid, uint16_t sid, uint32_t provid)
|
---|
118 | {
|
---|
119 | int32_t nr, rc=0;
|
---|
120 | SIDTAB *sidtab;
|
---|
121 |
|
---|
122 | if (!cl->sidtabs.ok)
|
---|
123 | {
|
---|
124 | if (!cl->sidtabs.no) return(1);
|
---|
125 | rc=1;
|
---|
126 | }
|
---|
127 | for (nr=0, sidtab=cfg.sidtab; sidtab; sidtab=sidtab->next, nr++)
|
---|
128 | if (sidtab->num_caid | sidtab->num_provid | sidtab->num_srvid)
|
---|
129 | {
|
---|
130 | if ((cl->sidtabs.no&((SIDTABBITS)1<<nr)) &&
|
---|
131 | (chk_ser_srvid_match(caid, sid, provid, sidtab)))
|
---|
132 | return(0);
|
---|
133 | if ((cl->sidtabs.ok&((SIDTABBITS)1<<nr)) &&
|
---|
134 | (chk_ser_srvid_match(caid, sid, provid, sidtab)))
|
---|
135 | rc=1;
|
---|
136 | }
|
---|
137 | return(rc);
|
---|
138 | }
|
---|
139 |
|
---|
140 | static void oscam_wait_ser_fork(void)
|
---|
141 | {
|
---|
142 | pthread_mutex_lock(&mutex);
|
---|
143 | do {
|
---|
144 | if (bcopy_end) {
|
---|
145 | bcopy_end = 0;
|
---|
146 | break;
|
---|
147 | }
|
---|
148 | else
|
---|
149 | pthread_cond_wait(&cond, &mutex);
|
---|
150 | } while (1);
|
---|
151 | pthread_mutex_unlock(&mutex);
|
---|
152 | }
|
---|
153 |
|
---|
154 | static int32_t oscam_ser_alpha_convert(uchar *buf, int32_t l)
|
---|
155 | {
|
---|
156 | int32_t i;
|
---|
157 | if (buf[0]==0x7E) // normalize
|
---|
158 | {
|
---|
159 | l-=2;
|
---|
160 | memmove(buf, buf+1, l); // remove BOT/EOT
|
---|
161 | for (i=0; i<l; i++)
|
---|
162 | if (buf[i]==0x20)
|
---|
163 | {
|
---|
164 | memmove(buf+i, buf+i+1, --l);
|
---|
165 | buf[i]^=0x20;
|
---|
166 | }
|
---|
167 | }
|
---|
168 | else // to alphastar
|
---|
169 | {
|
---|
170 | memmove(buf+1, buf, l++); // insert BOT
|
---|
171 | buf[0]=0x7E;
|
---|
172 | for (i=1; i<l; i++)
|
---|
173 | if ((buf[i]==0x20) || (buf[i]==0x7E) || (buf[i]==0x7F))
|
---|
174 | {
|
---|
175 | buf[i]^=0x20;
|
---|
176 | memmove(buf+i+1, buf+i, l++);
|
---|
177 | buf[i++]=0x20;
|
---|
178 | }
|
---|
179 | buf[l++]=0x7F; // insert EOT
|
---|
180 | }
|
---|
181 | return(l);
|
---|
182 | }
|
---|
183 |
|
---|
184 | static void oscam_ser_disconnect(void);
|
---|
185 |
|
---|
186 | static int32_t oscam_ser_parse_url(char *url, struct s_serial_client *serialdata, char *pcltype)
|
---|
187 | {
|
---|
188 | char *service, *usr, *dev, *baud=NULL, *dummy, *para;
|
---|
189 | char cltype;
|
---|
190 |
|
---|
191 | cltype = pcltype?(*pcltype):cur_client()->typ;
|
---|
192 |
|
---|
193 | serialdata->oscam_ser_proto=P_AUTO;
|
---|
194 | if( (dummy=strstr(url, "://")) )
|
---|
195 | {
|
---|
196 | int32_t i;
|
---|
197 | service=url;
|
---|
198 | url=dummy+3;
|
---|
199 | *dummy=0;
|
---|
200 | for (i=1; i<=P_MAX; i++)
|
---|
201 | if (!strcmp(service, proto_txt[i]))
|
---|
202 | serialdata->oscam_ser_proto=i;
|
---|
203 | }
|
---|
204 | if (!(cltype == 'c') && (serialdata->oscam_ser_proto==P_AUTO)) return(0);
|
---|
205 | switch(serialdata->oscam_ser_proto) // set the defaults
|
---|
206 | {
|
---|
207 | case P_GS:
|
---|
208 | serialdata->oscam_ser_timeout=500;
|
---|
209 | serialdata->oscam_ser_baud=B19200;
|
---|
210 | break;
|
---|
211 | default:
|
---|
212 | serialdata->oscam_ser_timeout=50;
|
---|
213 | #ifdef B115200
|
---|
214 | serialdata->oscam_ser_baud=B115200;
|
---|
215 | #else
|
---|
216 | serialdata->oscam_ser_baud=B9600;
|
---|
217 | #endif
|
---|
218 | }
|
---|
219 |
|
---|
220 | switch( serialdata->oscam_ser_proto )
|
---|
221 | {
|
---|
222 | case P_DSR95:
|
---|
223 | serialdata->dsr9500type=(cltype == 'c')?P_DSR_AUTO:P_DSR_WITHSID;
|
---|
224 | break;
|
---|
225 | case P_DSR95_OLD:
|
---|
226 | serialdata->dsr9500type=P_DSR_AUTO;
|
---|
227 | serialdata->oscam_ser_proto=P_DSR95;
|
---|
228 | }
|
---|
229 |
|
---|
230 | usr=url;
|
---|
231 | if( (dev=strchr(usr, '@')) )
|
---|
232 | {
|
---|
233 | *dev++='\0';
|
---|
234 | if( (dummy=strchr(usr, ':')) ) // fake pwd
|
---|
235 | *dummy++='\0';
|
---|
236 | if ((cltype == 'c') && (!usr[0])) return(0);
|
---|
237 | }
|
---|
238 | else
|
---|
239 | {
|
---|
240 | if (cltype == 'c') return(0); // user needed in server-mode
|
---|
241 | dev=usr;
|
---|
242 | }
|
---|
243 | if( (baud=strchr(dev, ':')) )// port = baud
|
---|
244 | *baud++='\0';
|
---|
245 | dummy=baud ? baud : dev;
|
---|
246 | if( (para=strchr(dummy, '?')) )
|
---|
247 | {
|
---|
248 | char *ptr1, *ptr2, *saveptr1 = NULL;
|
---|
249 | *para++='\0';
|
---|
250 | for (ptr1=strtok_r(para, "&", &saveptr1); ptr1; ptr1=strtok_r(NULL, "&", &saveptr1))
|
---|
251 | {
|
---|
252 | if (!(ptr2=strchr(ptr1, '='))) continue;
|
---|
253 | *ptr2++='\0';
|
---|
254 | strtolower(ptr1);
|
---|
255 | if (!strcmp("delay" , ptr1)) serialdata->oscam_ser_delay =atoi(ptr2);
|
---|
256 | if (!strcmp("timeout", ptr1)) serialdata->oscam_ser_timeout=atoi(ptr2);
|
---|
257 | }
|
---|
258 | }
|
---|
259 | if (baud)
|
---|
260 | {
|
---|
261 | trim(baud);
|
---|
262 | #ifdef B115200
|
---|
263 | if (!strcmp(baud, "115200"))
|
---|
264 | serialdata->oscam_ser_baud=B115200;
|
---|
265 | else
|
---|
266 | #endif
|
---|
267 | #ifdef B57600
|
---|
268 | if (!strcmp(baud, "57600"))
|
---|
269 | serialdata->oscam_ser_baud=B57600;
|
---|
270 | else
|
---|
271 | #endif
|
---|
272 | if (!strcmp(baud, "38400"))
|
---|
273 | serialdata->oscam_ser_baud=B38400;
|
---|
274 | else if (!strcmp(baud, "19200"))
|
---|
275 | serialdata->oscam_ser_baud=B19200;
|
---|
276 | else if (!strcmp(baud, "9600"))
|
---|
277 | serialdata->oscam_ser_baud=B9600;
|
---|
278 | }
|
---|
279 | cs_strncpy(serialdata->oscam_ser_usr, usr, sizeof(serialdata->oscam_ser_usr));
|
---|
280 | cs_strncpy(serialdata->oscam_ser_device, dev, sizeof(serialdata->oscam_ser_device));
|
---|
281 | return(serialdata->oscam_ser_baud);
|
---|
282 | }
|
---|
283 |
|
---|
284 | static void oscam_ser_set_baud(struct termios *tio, speed_t baud)
|
---|
285 | {
|
---|
286 | cfsetospeed(tio, baud);
|
---|
287 | cfsetispeed(tio, baud);
|
---|
288 | }
|
---|
289 |
|
---|
290 | static int32_t oscam_ser_set_serial_device(int32_t fd, speed_t baud)
|
---|
291 | {
|
---|
292 | struct termios tio;
|
---|
293 |
|
---|
294 | memset(&tio, 0, sizeof(tio));
|
---|
295 | // tio.c_cflag = (CS8 | CREAD | HUPCL | CLOCAL);
|
---|
296 | tio.c_cflag = (CS8 | CREAD | CLOCAL);
|
---|
297 | tio.c_iflag = IGNPAR;
|
---|
298 | tio.c_cc[VMIN] = 1;
|
---|
299 | tio.c_cc[VTIME] = 0;
|
---|
300 | //#if !defined(__CYGWIN__)
|
---|
301 | oscam_ser_set_baud(&tio, B1200);
|
---|
302 | tcsetattr(fd, TCSANOW, &tio);
|
---|
303 | cs_sleepms(500);
|
---|
304 | //#endif
|
---|
305 | oscam_ser_set_baud(&tio, baud);
|
---|
306 | return(tcsetattr(fd, TCSANOW, &tio));
|
---|
307 | }
|
---|
308 |
|
---|
309 | static int32_t oscam_ser_poll(int32_t event, struct s_client *client)
|
---|
310 | {
|
---|
311 | int32_t msec;
|
---|
312 | struct pollfd pfds;
|
---|
313 | struct timeb tpc;
|
---|
314 | cs_ftime(&tpc);
|
---|
315 | msec=1000*(client->serialdata->tpe.time-tpc.time)+client->serialdata->tpe.millitm-tpc.millitm;
|
---|
316 | if (msec<0)
|
---|
317 | return(0);
|
---|
318 | pfds.fd=cur_client()->pfd;
|
---|
319 | pfds.events=event;
|
---|
320 | pfds.revents=0;
|
---|
321 | if (poll(&pfds, 1, msec)!=1)
|
---|
322 | return(0);
|
---|
323 | else
|
---|
324 | return(((pfds.revents)&event)==event);
|
---|
325 | }
|
---|
326 |
|
---|
327 | static int32_t oscam_ser_write(struct s_client *client, const uchar * const buf, int32_t n)
|
---|
328 | {
|
---|
329 | int32_t i;
|
---|
330 | for (i=0; (i<n) && (oscam_ser_poll(POLLOUT, client)); i++)
|
---|
331 | {
|
---|
332 | if (client->serialdata->oscam_ser_delay)
|
---|
333 | cs_sleepms(client->serialdata->oscam_ser_delay);
|
---|
334 | if (write(client->pfd, buf+i, 1)<1)
|
---|
335 | break;
|
---|
336 | }
|
---|
337 | return(i);
|
---|
338 | }
|
---|
339 |
|
---|
340 | static int32_t oscam_ser_send(struct s_client *client, const uchar * const buf, int32_t l)
|
---|
341 | {
|
---|
342 | int32_t n;
|
---|
343 | struct s_serial_client *serialdata=client->serialdata ;
|
---|
344 | if (!client->pfd) return(0);
|
---|
345 | cs_ftime(&serialdata->tps);
|
---|
346 | serialdata->tpe=client->serialdata->tps;
|
---|
347 | serialdata->tpe.millitm+=serialdata->oscam_ser_timeout+(l*(serialdata->oscam_ser_delay+1));
|
---|
348 | serialdata->tpe.time+=(serialdata->tpe.millitm/1000);
|
---|
349 | serialdata->tpe.millitm%=1000;
|
---|
350 | n=oscam_ser_write(client, buf, l);
|
---|
351 | cs_ftime(&serialdata->tpe);
|
---|
352 | cs_ddump_mask(D_CLIENT, buf, l, "send %d of %d bytes to %s in %ld msec", n, l, remote_txt(),
|
---|
353 | 1000*(serialdata->tpe.time-serialdata->tps.time)+serialdata->tpe.millitm-serialdata->tps.millitm);
|
---|
354 | if (n!=l)
|
---|
355 | cs_log("transmit error. send %d of %d bytes only !", n, l);
|
---|
356 | return(n);
|
---|
357 | }
|
---|
358 |
|
---|
359 | static int32_t oscam_ser_selrec(uchar *buf, int32_t n, int32_t l, int32_t *c)
|
---|
360 | {
|
---|
361 | int32_t i;
|
---|
362 | if (*c+n>l)
|
---|
363 | n=l-*c;
|
---|
364 | if (n<=0) return(0);
|
---|
365 | for (i=0; (i<n) && (oscam_ser_poll(POLLIN, cur_client())); i++)
|
---|
366 | if (read(cur_client()->pfd, buf+*c, 1)<1 )
|
---|
367 | return(0);
|
---|
368 | else
|
---|
369 | (*c)++;
|
---|
370 | return(i==n);
|
---|
371 | }
|
---|
372 |
|
---|
373 | static int32_t oscam_ser_recv(struct s_client *client, uchar *xbuf, int32_t l)
|
---|
374 | {
|
---|
375 | int32_t s, p, n, r;
|
---|
376 | uchar job=IS_BAD;
|
---|
377 | static uchar lb;
|
---|
378 | static int32_t have_lb=0;
|
---|
379 | uchar *buf=xbuf+1;
|
---|
380 | struct s_serial_client *serialdata=client->serialdata;
|
---|
381 |
|
---|
382 | if (!client->pfd) return(-1);
|
---|
383 | cs_ftime(&serialdata->tps);
|
---|
384 | serialdata->tpe=serialdata->tps;
|
---|
385 | serialdata->tpe.millitm+=serialdata->oscam_ser_timeout;
|
---|
386 | serialdata->tpe.time+=(serialdata->tpe.millitm/1000);
|
---|
387 | serialdata->tpe.millitm%=1000;
|
---|
388 | buf[0]=lb;
|
---|
389 | for (s=p=r=0, n=have_lb; (s<4) && (p>=0); s++)
|
---|
390 | {
|
---|
391 | switch(s)
|
---|
392 | {
|
---|
393 | case 0: // STAGE 0: skip known garbage from DSR9500
|
---|
394 | if (oscam_ser_selrec(buf, 2-n, l, &n))
|
---|
395 | {
|
---|
396 | if ((buf[0]==0x0A) && (buf[1]==0x0D))
|
---|
397 | p=(-4);
|
---|
398 | if ((buf[0]==0x0D) && (buf[1]==0x0A))
|
---|
399 | p=(-4);
|
---|
400 | }
|
---|
401 | else
|
---|
402 | p=(-3);
|
---|
403 | have_lb=0;
|
---|
404 | break;
|
---|
405 | case 1: // STAGE 1: identify protocol
|
---|
406 | p=(-3);
|
---|
407 | if (oscam_ser_selrec(buf, 1, l, &n)) // now we have 3 bytes in buf
|
---|
408 | {
|
---|
409 | if((buf[0] == 0x04) && (buf[1] == 0x00) && (buf[2] == 0x02)) { //skip unsupported Advanced Serial Sharing Protocol HF 8900
|
---|
410 | oscam_ser_selrec(buf, 2, l, &n); // get rest 2 bytes to buffor
|
---|
411 | p=(-4);
|
---|
412 | have_lb=0;
|
---|
413 | break;
|
---|
414 | }
|
---|
415 | else {
|
---|
416 |
|
---|
417 | p=(-2);
|
---|
418 | if (client->typ == 'c') // HERE IS SERVER
|
---|
419 | {
|
---|
420 | job=IS_ECM; // assume ECM
|
---|
421 | switch(buf[0])
|
---|
422 | {
|
---|
423 | case 0x00: if( (buf[1]==0x01)&&(buf[2]==0x00) )
|
---|
424 | { p=P_GS; job=IS_LGO; serialdata->tpe.time++; } break;
|
---|
425 | case 0x01: if( (buf[1]&0xf0)==0xb0 ) p=P_GBOX;
|
---|
426 | else {p=P_SSSP; job=IS_PMT;}
|
---|
427 | break; // pmt-request
|
---|
428 | case 0x02: p=P_HSIC; break;
|
---|
429 | case 0x03: switch(serialdata->oscam_ser_proto)
|
---|
430 | {
|
---|
431 | case P_SSSP :
|
---|
432 | case P_GS :
|
---|
433 | case P_DSR95 : p=serialdata->oscam_ser_proto; break;
|
---|
434 | case P_AUTO : p=(buf[1]<0x30) ? P_SSSP : P_DSR95;
|
---|
435 | break; // auto for GS is useless !!
|
---|
436 | } break;
|
---|
437 | case 0x04: p=P_DSR95; job=IS_ECHO; serialdata->dsr9500type=P_DSR_GNUSMAS; break;
|
---|
438 | case 0x7E: p=P_ALPHA; if (buf[1]!=0x80) job=IS_BAD; break;
|
---|
439 | case 0x80:
|
---|
440 | case 0x81: p=P_BOMBA; break;
|
---|
441 | }
|
---|
442 | }
|
---|
443 |
|
---|
444 | else // HERE IS CLIENT
|
---|
445 | {
|
---|
446 | job=IS_DCW; // assume DCW
|
---|
447 | switch(serialdata->oscam_ser_proto)
|
---|
448 | {
|
---|
449 | case P_HSIC : if ((buf[0]==4) && (buf[1]==4)) p=P_HSIC; break;
|
---|
450 | case P_BOMBA: p=P_BOMBA; break;
|
---|
451 | case P_DSR95: if (buf[0]==4) p=P_DSR95; break;
|
---|
452 | case P_ALPHA: if (buf[0]==0x88) p=P_ALPHA; break;
|
---|
453 | }
|
---|
454 | }
|
---|
455 | if ((serialdata->oscam_ser_proto!=p) && (serialdata->oscam_ser_proto!=P_AUTO))
|
---|
456 | p=(-2);
|
---|
457 | }
|
---|
458 | }
|
---|
459 | break;
|
---|
460 | case 2: // STAGE 2: examine length
|
---|
461 | if (client->typ == 'c') switch(p)
|
---|
462 | {
|
---|
463 | case P_SSSP : r=(buf[1]<<8)|buf[2]; break;
|
---|
464 | case P_BOMBA : r=buf[2]; break;
|
---|
465 | case P_HSIC : if (oscam_ser_selrec(buf, 12, l, &n)) r=buf[14];
|
---|
466 | else p=(-1);
|
---|
467 | break;
|
---|
468 | case P_DSR95 : if( job==IS_ECHO )
|
---|
469 | {
|
---|
470 | r=17*serialdata->samsung_dcw-3+serialdata->samsung_0a;
|
---|
471 | serialdata->samsung_dcw=serialdata->samsung_0a=0;
|
---|
472 | }
|
---|
473 | else
|
---|
474 | {
|
---|
475 | if (oscam_ser_selrec(buf, 16, l, &n))
|
---|
476 | {
|
---|
477 | uchar b;
|
---|
478 | if (cs_atob(&b, (char *)buf+17, 1)<0)
|
---|
479 | p=(-2);
|
---|
480 | else {
|
---|
481 | r=(b<<1);
|
---|
482 | r+=(serialdata->dsr9500type==P_DSR_WITHSID)?4:0;
|
---|
483 | }
|
---|
484 | }
|
---|
485 | else p=(-1);
|
---|
486 | }
|
---|
487 | break;
|
---|
488 | case P_GS : if (job==IS_LGO)
|
---|
489 | r=5;
|
---|
490 | else
|
---|
491 | {
|
---|
492 | if (oscam_ser_selrec(buf, 1, l, &n))
|
---|
493 | r=(buf[3]<<8)|buf[2];
|
---|
494 | else p=(-1);
|
---|
495 | }
|
---|
496 | break;
|
---|
497 | case P_ALPHA : r=-0x7F; // char specifying EOT
|
---|
498 | break;
|
---|
499 | case P_GBOX : r=((buf[1]&0xf)<<8) | buf[2];
|
---|
500 | serialdata->gbox_lens.cat_len = r;
|
---|
501 | break;
|
---|
502 | default : serialdata->dsr9500type=P_DSR_AUTO;
|
---|
503 | }
|
---|
504 | else switch(p)
|
---|
505 | {
|
---|
506 | case P_HSIC : r=(buf[2]==0x3A) ? 20 : 0; break; // 3A=DCW / FC=ECM was wrong
|
---|
507 | case P_BOMBA : r=13; break;
|
---|
508 | case P_DSR95 : r=14; break;
|
---|
509 | case P_ALPHA : r=(buf[1]<<8)|buf[2]; break; // should be 16 always
|
---|
510 | }
|
---|
511 | break;
|
---|
512 | case 3: // STAGE 3: get the rest ...
|
---|
513 | if (r>0) // read r additional bytes
|
---|
514 | {
|
---|
515 | int32_t all = n+r;
|
---|
516 | if( !oscam_ser_selrec(buf, r, l, &n) )
|
---|
517 | {
|
---|
518 | cs_debug_mask(D_CLIENT, "not all data received, waiting another 50 ms");
|
---|
519 | serialdata->tpe.millitm+=50;
|
---|
520 | if( !oscam_ser_selrec(buf, all-n, l, &n) )
|
---|
521 | p=(-1);
|
---|
522 | }
|
---|
523 | // auto detect DSR9500 protocol
|
---|
524 | if( client->typ == 'c' && p==P_DSR95 && serialdata->dsr9500type==P_DSR_AUTO )
|
---|
525 | {
|
---|
526 | serialdata->tpe.millitm+=20;
|
---|
527 | if( oscam_ser_selrec(buf, 2, l, &n) )
|
---|
528 | {
|
---|
529 | if( cs_atoi((char *)buf+n-2, 1, 1)==0xFFFFFFFF )
|
---|
530 | {
|
---|
531 | switch( (buf[n-2]<<8)|buf[n-1] )
|
---|
532 | {
|
---|
533 | case 0x0A0D : serialdata->dsr9500type=P_DSR_OPEN; break;
|
---|
534 | case 0x0D0A : serialdata->dsr9500type=P_DSR_PIONEER; break;
|
---|
535 | default : serialdata->dsr9500type=P_DSR_UNKNOWN; break;
|
---|
536 | }
|
---|
537 | }else{
|
---|
538 | if( oscam_ser_selrec(buf, 2, l, &n) )
|
---|
539 | if( cs_atoi((char *)buf+n-2, 1, 1)==0xFFFFFFFF )
|
---|
540 | serialdata->dsr9500type=P_DSR_UNKNOWN;
|
---|
541 | else
|
---|
542 | serialdata->dsr9500type=P_DSR_WITHSID;
|
---|
543 | else {
|
---|
544 | serialdata->dsr9500type=P_DSR_UNKNOWN;
|
---|
545 | p=(-1);
|
---|
546 | }
|
---|
547 | }
|
---|
548 | }
|
---|
549 | else
|
---|
550 | serialdata->dsr9500type=P_DSR_GNUSMAS;
|
---|
551 | if( p )
|
---|
552 | cs_log("detected dsr9500-%s type receiver",
|
---|
553 | dsrproto_txt[serialdata->dsr9500type]);
|
---|
554 | }
|
---|
555 | // gbox
|
---|
556 | if( client->typ == 'c' && p==P_GBOX )
|
---|
557 | {
|
---|
558 | int32_t j;
|
---|
559 | for( j=0; (j<3) && (p>0); j++)
|
---|
560 | switch( j )
|
---|
561 | {
|
---|
562 | case 0: // PMT head
|
---|
563 | if( !oscam_ser_selrec(buf, 3, l, &n) )
|
---|
564 | p=(-1);
|
---|
565 | else if( !(buf[n-3]==0x02 && (buf[n-2]&0xf0)==0xb0) )
|
---|
566 | p=(-2);
|
---|
567 | break;
|
---|
568 | case 1: // PMT + ECM header
|
---|
569 | serialdata->gbox_lens.pmt_len=((buf[n-2]&0xf)<<8)|buf[n-1];
|
---|
570 | if( !oscam_ser_selrec(buf, serialdata->gbox_lens.pmt_len+3, l, &n) )
|
---|
571 | p=(-1);
|
---|
572 | break;
|
---|
573 | case 2: // ECM + ECM PID
|
---|
574 | serialdata->gbox_lens.ecm_len=((buf[n-2]&0xf)<<8)|buf[n-1];
|
---|
575 | if( !oscam_ser_selrec(buf, serialdata->gbox_lens.ecm_len+4, l, &n) )
|
---|
576 | p=(-1);
|
---|
577 | }
|
---|
578 | } // gbox
|
---|
579 | }
|
---|
580 | else if (r<0) // read until specified char (-r)
|
---|
581 | {
|
---|
582 | while((buf[n-1]!=(-r)) && (p>0))
|
---|
583 | if (!oscam_ser_selrec(buf, 1, l, &n))
|
---|
584 | p=(-1);
|
---|
585 | }
|
---|
586 | break;
|
---|
587 | }
|
---|
588 | }
|
---|
589 | if (p==(-2) || p==(-1)) {
|
---|
590 | oscam_ser_selrec(buf, l-n, l, &n); // flush buffer
|
---|
591 | serialdata->serial_errors++;
|
---|
592 | }
|
---|
593 | cs_ftime(&serialdata->tpe);
|
---|
594 | cs_ddump_mask(D_CLIENT, buf, n, "received %d bytes from %s in %ld msec", n, remote_txt(),
|
---|
595 | 1000*(serialdata->tpe.time-serialdata->tps.time)+serialdata->tpe.millitm-serialdata->tps.millitm);
|
---|
596 | client->last=serialdata->tpe.time;
|
---|
597 | switch(p)
|
---|
598 | {
|
---|
599 | case (-1): if (client->typ == 'c'&&(n>2)&&(buf[0]==2)&&(buf[1]==2)&&(buf[2]==2))
|
---|
600 | {
|
---|
601 | oscam_ser_disconnect();
|
---|
602 | cs_log("humax powered on"); // this is nice ;)
|
---|
603 | }
|
---|
604 | else {
|
---|
605 | if(client->typ == 'c' && buf[0] == 0x1 && buf[1] == 0x08 && buf[2] == 0x20 && buf[3] == 0x08) {
|
---|
606 | oscam_ser_disconnect();
|
---|
607 | cs_log("ferguson powered on"); // this is nice to ;)
|
---|
608 | }
|
---|
609 | else
|
---|
610 | cs_log(incomplete, n);
|
---|
611 | }
|
---|
612 | break;
|
---|
613 | case (-2): cs_debug_mask(D_CLIENT, "unknown request or garbage");
|
---|
614 | break;
|
---|
615 | }
|
---|
616 | xbuf[0]=(uchar) ((job<<4) | p);
|
---|
617 | return((p<0)?0:n+1);
|
---|
618 | }
|
---|
619 |
|
---|
620 | /*
|
---|
621 | * server functions
|
---|
622 | */
|
---|
623 |
|
---|
624 | static void oscam_ser_disconnect_client(void)
|
---|
625 | {
|
---|
626 | uchar mbuf[1024];
|
---|
627 | struct s_serial_client *serialdata=cur_client()->serialdata;
|
---|
628 | switch(serialdata->connected ? serialdata->connected : serialdata->oscam_ser_proto)
|
---|
629 | {
|
---|
630 | case P_GS:
|
---|
631 | mbuf[0] = 0x01;
|
---|
632 | mbuf[1] = 0x00;
|
---|
633 | mbuf[2] = 0x00;
|
---|
634 | mbuf[3] = 0x00;
|
---|
635 | oscam_ser_send(cur_client(), mbuf, 4);
|
---|
636 | break;
|
---|
637 | }
|
---|
638 | serialdata->dsr9500type=P_DSR_AUTO;
|
---|
639 | serialdata->serial_errors=0;
|
---|
640 | }
|
---|
641 |
|
---|
642 | static void oscam_ser_init_client(void)
|
---|
643 | {
|
---|
644 | uchar mbuf[4];
|
---|
645 | switch(cur_client()->serialdata->oscam_ser_proto) // sure, does not work in auto-mode
|
---|
646 | {
|
---|
647 | case P_GS:
|
---|
648 | oscam_ser_disconnect_client(); // send disconnect first
|
---|
649 | cs_sleepms(300); // wait a little bit
|
---|
650 | mbuf[0] = 0x00;
|
---|
651 | mbuf[1] = 0x00;
|
---|
652 | mbuf[2] = 0x00;
|
---|
653 | mbuf[3] = 0x00;
|
---|
654 | oscam_ser_send(cur_client(), mbuf, 4); // send connect
|
---|
655 | break;
|
---|
656 | }
|
---|
657 | }
|
---|
658 |
|
---|
659 | static void oscam_ser_disconnect(void)
|
---|
660 | {
|
---|
661 | oscam_ser_disconnect_client();
|
---|
662 | if (cur_client()->serialdata->connected)
|
---|
663 | cs_log("%s disconnected (%s)", username(cur_client()), proto_txt[cur_client()->serialdata->connected]);
|
---|
664 | cur_client()->serialdata->connected=0;
|
---|
665 | }
|
---|
666 |
|
---|
667 | static void oscam_ser_auth_client(int32_t proto)
|
---|
668 | {
|
---|
669 | int32_t ok = 0;
|
---|
670 | struct s_serial_client *serialdata=cur_client()->serialdata;
|
---|
671 | // After reload base account ptrs may be placed in other address,
|
---|
672 | // and we may can't find it in this process.
|
---|
673 | // Simply save valid account.
|
---|
674 | struct s_auth *account=0;
|
---|
675 |
|
---|
676 | if (serialdata->connected==proto)
|
---|
677 | return;
|
---|
678 | if (serialdata->connected)
|
---|
679 | oscam_ser_disconnect();
|
---|
680 | serialdata->connected=proto;
|
---|
681 |
|
---|
682 | for (ok=0, account=cfg.account; (account) && (!ok); account=account->next)
|
---|
683 | if( (ok=!strcmp(serialdata->oscam_ser_usr, account->usr)) )
|
---|
684 | break;
|
---|
685 | cs_auth_client(cur_client(), ok ? account : (struct s_auth *)(-1), proto_txt[serialdata->connected]);
|
---|
686 | }
|
---|
687 |
|
---|
688 | static void oscam_ser_send_dcw(struct s_client *client, ECM_REQUEST *er)
|
---|
689 | {
|
---|
690 | uchar mbuf[23];
|
---|
691 | int32_t i;
|
---|
692 | uchar crc;
|
---|
693 | struct s_serial_client *serialdata=cur_client()->serialdata;
|
---|
694 | if (er->rc<E_NOTFOUND) // found
|
---|
695 | switch(serialdata->connected)
|
---|
696 | {
|
---|
697 | case P_HSIC:
|
---|
698 | for (i=0, crc=HSIC_CRC; i<16; i++)
|
---|
699 | crc^=er->cw[i];
|
---|
700 | memset(mbuf , 0x04 , 2);
|
---|
701 | memset(mbuf+2 , 0x3a , 2);
|
---|
702 | memcpy(mbuf+4 , er->cw, 16);
|
---|
703 | memcpy(mbuf+20, &crc , 1);
|
---|
704 | memset(mbuf+21, 0x1b , 2);
|
---|
705 | oscam_ser_send(client, mbuf, 23);
|
---|
706 | break;
|
---|
707 | case P_SSSP:
|
---|
708 | mbuf[0]=0xF2;
|
---|
709 | mbuf[1]=0;
|
---|
710 | mbuf[2]=16;
|
---|
711 | memcpy(mbuf+3, er->cw, 16);
|
---|
712 | oscam_ser_send(client, mbuf, 19);
|
---|
713 | if (!serialdata->sssp_fix)
|
---|
714 | {
|
---|
715 | mbuf[0]=0xF1;
|
---|
716 | mbuf[1]=0;
|
---|
717 | mbuf[2]=2;
|
---|
718 | i2b_buf(2, er->pid, mbuf + 3);
|
---|
719 | oscam_ser_send(client, mbuf, 5);
|
---|
720 | serialdata->sssp_fix=1;
|
---|
721 | }
|
---|
722 | break;
|
---|
723 | case P_GBOX:
|
---|
724 | case P_BOMBA:
|
---|
725 | oscam_ser_send(client, er->cw, 16);
|
---|
726 | break;
|
---|
727 | case P_DSR95:
|
---|
728 | mbuf[0]=4;
|
---|
729 | memcpy(mbuf+1, er->cw, 16);
|
---|
730 | oscam_ser_send(client, mbuf, 17);
|
---|
731 | if( serialdata->dsr9500type==P_DSR_GNUSMAS )
|
---|
732 | {
|
---|
733 | serialdata->samsung_0a=0;
|
---|
734 | for( i=1; i<17; i++ )
|
---|
735 | if( mbuf[i]==0x0A )
|
---|
736 | serialdata->samsung_0a++;
|
---|
737 | serialdata->samsung_dcw++;
|
---|
738 | }
|
---|
739 | break;
|
---|
740 | case P_GS:
|
---|
741 | mbuf[0]=0x03;
|
---|
742 | mbuf[1]=0x08;
|
---|
743 | mbuf[2]=0x10;
|
---|
744 | mbuf[3]=0x00;
|
---|
745 | memcpy(mbuf+4, er->cw, 16);
|
---|
746 | oscam_ser_send(client, mbuf, 20);
|
---|
747 | break;
|
---|
748 | case P_ALPHA:
|
---|
749 | mbuf[0]=0x88;
|
---|
750 | mbuf[1]=0x00;
|
---|
751 | mbuf[2]=0x10;
|
---|
752 | memcpy(mbuf+3, er->cw, 16);
|
---|
753 | oscam_ser_send(client, mbuf, 19);
|
---|
754 | break;
|
---|
755 | }
|
---|
756 | else // not found
|
---|
757 | switch(serialdata->connected)
|
---|
758 | {
|
---|
759 | case P_GS:
|
---|
760 | mbuf[0]=0x03;
|
---|
761 | mbuf[1]=0x09;
|
---|
762 | mbuf[2]=0x00;
|
---|
763 | mbuf[3]=0x00;
|
---|
764 | oscam_ser_send(client, mbuf, 4);
|
---|
765 | break;
|
---|
766 | }
|
---|
767 | serialdata->serial_errors=0; // clear error counter
|
---|
768 | }
|
---|
769 |
|
---|
770 | static void oscam_ser_process_pmt(uchar *buf, int32_t l)
|
---|
771 | {
|
---|
772 | int32_t i;
|
---|
773 | uchar sbuf[32];
|
---|
774 | struct s_serial_client *serialdata=cur_client()->serialdata;
|
---|
775 | switch(serialdata->connected)
|
---|
776 | {
|
---|
777 | case P_SSSP:
|
---|
778 | serialdata->sssp_fix=0;
|
---|
779 | memset(serialdata->sssp_tab, 0, sizeof(serialdata->sssp_tab));
|
---|
780 | serialdata->sssp_srvid=b2i(2, buf+3);
|
---|
781 | serialdata->sssp_num=0;
|
---|
782 |
|
---|
783 |
|
---|
784 | for (i=9; (i<l) && (serialdata->sssp_num<SSSP_MAX_PID); i+=7)
|
---|
785 | {
|
---|
786 | if(chk_ser_srvid(cur_client(), b2i(2, buf+i), b2i(2, buf+3), b2i(3, buf+i+4))) { // check support for pid (caid, sid and provid in oscam.services)
|
---|
787 | memcpy(sbuf+3+(serialdata->sssp_num<<1), buf+i+2, 2);
|
---|
788 | serialdata->sssp_tab[serialdata->sssp_num].caid=b2i(2, buf+i );
|
---|
789 | serialdata->sssp_tab[serialdata->sssp_num].pid =b2i(2, buf+i+2);
|
---|
790 | serialdata->sssp_tab[serialdata->sssp_num].prid=b2i(3, buf+i+4);
|
---|
791 | serialdata->sssp_num++;
|
---|
792 | }
|
---|
793 | }
|
---|
794 | sbuf[0]=0xF1;
|
---|
795 | sbuf[1]=0;
|
---|
796 | sbuf[2]=(serialdata->sssp_num<<1);
|
---|
797 | oscam_ser_send(cur_client(), sbuf, sbuf[2]+3);
|
---|
798 | break;
|
---|
799 | }
|
---|
800 | }
|
---|
801 |
|
---|
802 | static void oscam_ser_client_logon(uchar *buf, int32_t l)
|
---|
803 | {
|
---|
804 | uchar gs_logon[]={0, 1, 0, 0, 2, 1, 0, 0};
|
---|
805 | switch(cur_client()->serialdata->connected)
|
---|
806 | {
|
---|
807 | case P_GS:
|
---|
808 | if ((l>=8) && (!memcmp(buf, gs_logon, 8)))
|
---|
809 | {
|
---|
810 | buf[0] = 0x02;
|
---|
811 | buf[1] = 0x04;
|
---|
812 | buf[2] = 0x00;
|
---|
813 | buf[3] = 0x00;
|
---|
814 | oscam_ser_send(cur_client(), buf, 4);
|
---|
815 | }
|
---|
816 | break;
|
---|
817 | }
|
---|
818 | }
|
---|
819 |
|
---|
820 | static int32_t oscam_ser_check_ecm(ECM_REQUEST *er, uchar *buf, int32_t l)
|
---|
821 | {
|
---|
822 | int32_t i;
|
---|
823 | struct s_serial_client *serialdata = cur_client()->serialdata;
|
---|
824 |
|
---|
825 | if (l<16)
|
---|
826 | {
|
---|
827 | cs_log(incomplete, l);
|
---|
828 | return(1);
|
---|
829 | }
|
---|
830 |
|
---|
831 | switch(serialdata->connected)
|
---|
832 | {
|
---|
833 | case P_HSIC:
|
---|
834 | er->ecmlen = l-12;
|
---|
835 | er->caid = b2i(2, buf+1 );
|
---|
836 | er->prid = b2i(3, buf+3 );
|
---|
837 | er->pid = b2i(2, buf+6 );
|
---|
838 | er->srvid= b2i(2, buf+10);
|
---|
839 | memcpy(er->ecm, buf+12, er->ecmlen);
|
---|
840 | break;
|
---|
841 | case P_SSSP:
|
---|
842 | er->pid=b2i(2, buf+3);
|
---|
843 | for (i=0; (i<8) && (serialdata->sssp_tab[i].pid!=er->pid); i++);
|
---|
844 | if (i>=serialdata->sssp_num)
|
---|
845 | {
|
---|
846 | cs_debug_mask(D_CLIENT, "illegal request, unknown pid=%04X", er->pid);
|
---|
847 | return(2);
|
---|
848 | }
|
---|
849 | er->ecmlen = l-5;
|
---|
850 | er->srvid= serialdata->sssp_srvid;
|
---|
851 | er->caid = serialdata->sssp_tab[i].caid;
|
---|
852 | er->prid = serialdata->sssp_tab[i].prid;
|
---|
853 | memcpy(er->ecm, buf+5, er->ecmlen);
|
---|
854 | break;
|
---|
855 | case P_BOMBA:
|
---|
856 | er->ecmlen = l;
|
---|
857 | memcpy(er->ecm, buf, er->ecmlen);
|
---|
858 | break;
|
---|
859 | case P_DSR95:
|
---|
860 | buf[l]='\0'; // prepare for trim
|
---|
861 | trim((char *)buf+13); // strip spc, nl, cr ...
|
---|
862 | er->ecmlen = strlen((char *)buf+13)>>1;
|
---|
863 | er->prid=cs_atoi((char *)buf+3, 3, 0); // ignore errors
|
---|
864 | er->caid=cs_atoi((char *)buf+9, 2, 0); // ignore errors
|
---|
865 | if (cs_atob(er->ecm, (char *)buf+13, er->ecmlen)<0)
|
---|
866 | {
|
---|
867 | cs_log("illegal characters in ecm-request");
|
---|
868 | return(1);
|
---|
869 | }
|
---|
870 | if( serialdata->dsr9500type==P_DSR_WITHSID )
|
---|
871 | {
|
---|
872 | er->ecmlen -= 2;
|
---|
873 | er->srvid=cs_atoi((char *)buf+13+(er->ecmlen << 1), 2, 0);
|
---|
874 | }
|
---|
875 | break;
|
---|
876 | case P_GS:
|
---|
877 | er->ecmlen = ((buf[3]<<8)|buf[2]) - 6;
|
---|
878 | er->srvid = (buf[5]<<8)|buf[4]; // sid
|
---|
879 | er->caid = (buf[7]<<8)|buf[6];
|
---|
880 | er->prid = 0;
|
---|
881 | if (er->ecmlen > 256) er->ecmlen = 256;
|
---|
882 | memcpy(er->ecm, buf+10, er->ecmlen);
|
---|
883 | break;
|
---|
884 | case P_ALPHA:
|
---|
885 | l=oscam_ser_alpha_convert(buf, l);
|
---|
886 | er->ecmlen= b2i(2, buf+1 )-2;
|
---|
887 | er->caid = b2i(2, buf+3 );
|
---|
888 | if ((er->ecmlen!=l-5) || (er->ecmlen>257))
|
---|
889 | {
|
---|
890 | cs_log(incomplete, l);
|
---|
891 | return(1);
|
---|
892 | }
|
---|
893 | memcpy(er->ecm, buf+5, er->ecmlen);
|
---|
894 | break;
|
---|
895 | case P_GBOX:
|
---|
896 | er->srvid = b2i(2, buf+serialdata->gbox_lens.cat_len+3+3);
|
---|
897 | er->ecmlen = serialdata->gbox_lens.ecm_len+3;
|
---|
898 | memcpy(er->ecm, buf+serialdata->gbox_lens.cat_len+3+serialdata->gbox_lens.pmt_len+3, er->ecmlen);
|
---|
899 | break;
|
---|
900 | }
|
---|
901 | return(0);
|
---|
902 | }
|
---|
903 |
|
---|
904 | static void oscam_ser_process_ecm(uchar *buf, int32_t l)
|
---|
905 | {
|
---|
906 | ECM_REQUEST *er;
|
---|
907 |
|
---|
908 | if (!(er=get_ecmtask()))
|
---|
909 | return;
|
---|
910 |
|
---|
911 | switch(oscam_ser_check_ecm(er, buf, l))
|
---|
912 | {
|
---|
913 | case 2: er->rc=E_CORRUPT; return; // error without log
|
---|
914 | case 1: er->rc=E_CORRUPT; // error with log
|
---|
915 | }
|
---|
916 | get_cw(cur_client(), er);
|
---|
917 | }
|
---|
918 |
|
---|
919 |
|
---|
920 | static void oscam_ser_server(void)
|
---|
921 | {
|
---|
922 | int32_t n;
|
---|
923 | uchar mbuf[1024];
|
---|
924 |
|
---|
925 | int32_t * pserial_errors = &cur_client()->serialdata->serial_errors;
|
---|
926 |
|
---|
927 | cur_client()->serialdata->connected=0;
|
---|
928 | oscam_ser_init_client();
|
---|
929 |
|
---|
930 | while ((n=process_input(mbuf, sizeof(mbuf), cfg.cmaxidle))>=0)
|
---|
931 | {
|
---|
932 | if ((*pserial_errors) > 3)
|
---|
933 | {
|
---|
934 | cs_log("too many errors, reiniting...");
|
---|
935 | break;
|
---|
936 | }
|
---|
937 | if (n>0)
|
---|
938 | {
|
---|
939 | oscam_ser_auth_client(mbuf[0] & 0xF);
|
---|
940 | switch (mbuf[0]>>4)
|
---|
941 | {
|
---|
942 | case IS_ECM:
|
---|
943 | oscam_ser_process_ecm(mbuf+1, n-1);
|
---|
944 | break;
|
---|
945 | case IS_PMT:
|
---|
946 | oscam_ser_process_pmt(mbuf+1, n-1);
|
---|
947 | break;
|
---|
948 | case IS_LGO:
|
---|
949 | oscam_ser_client_logon(mbuf+1, n-1);
|
---|
950 | break;
|
---|
951 | }
|
---|
952 | }
|
---|
953 | }
|
---|
954 | oscam_ser_disconnect();
|
---|
955 | }
|
---|
956 |
|
---|
957 | static int32_t init_oscam_ser_device(char *device, speed_t baud)
|
---|
958 | {
|
---|
959 | int32_t fd;
|
---|
960 |
|
---|
961 | fd=open(device, O_RDWR | O_NOCTTY | O_SYNC | O_NONBLOCK);
|
---|
962 | if (fd>0)
|
---|
963 | {
|
---|
964 | fcntl(fd, F_SETFL, 0);
|
---|
965 | if (oscam_ser_set_serial_device(fd, baud)<0) cs_log("ERROR ioctl");
|
---|
966 | if (tcflush(fd, TCIOFLUSH)<0) cs_log("ERROR flush");
|
---|
967 | }
|
---|
968 | else
|
---|
969 | {
|
---|
970 | fd=0;
|
---|
971 | cs_log("ERROR opening %s (errno=%d %s)", device, errno, strerror(errno));
|
---|
972 | }
|
---|
973 | return(fd);
|
---|
974 | }
|
---|
975 |
|
---|
976 | static void oscam_copy_serialdata(struct s_serial_client *dest, struct s_serial_client *src)
|
---|
977 | {
|
---|
978 | if( dest && src )
|
---|
979 | {
|
---|
980 | dest->connected = src->connected;
|
---|
981 | memcpy(&dest->tps, &src->tps, sizeof(dest->tps));
|
---|
982 | memcpy(&dest->tpe, &src->tpe, sizeof(dest->tpe));
|
---|
983 | memcpy(&dest->oscam_ser_usr, &src->oscam_ser_usr, sizeof(dest->oscam_ser_usr));
|
---|
984 | memcpy(&dest->oscam_ser_device, &src->oscam_ser_device, sizeof(dest->oscam_ser_device));
|
---|
985 | dest->oscam_ser_baud = src->oscam_ser_baud;
|
---|
986 | dest->oscam_ser_delay = src->oscam_ser_delay;
|
---|
987 | dest->oscam_ser_timeout = src->oscam_ser_timeout;
|
---|
988 | dest->oscam_ser_proto = src->oscam_ser_proto;
|
---|
989 | dest->serial_errors = src->serial_errors;
|
---|
990 | dest->dsr9500type = src->dsr9500type;
|
---|
991 | dest->samsung_0a = src->samsung_0a; // number of 0A in ALL dcw sent into samsung
|
---|
992 | dest->samsung_dcw = src->samsung_dcw; // number of dcw sent into samsung before echo or ecm is received
|
---|
993 |
|
---|
994 | dest->gbox_lens = src->gbox_lens;
|
---|
995 | memcpy(&dest->sssp_tab, &src->sssp_tab, sizeof(dest->sssp_tab));
|
---|
996 | dest->sssp_srvid = src->sssp_srvid;
|
---|
997 | dest->sssp_num = src->sssp_num;
|
---|
998 | dest->sssp_fix = src->sssp_fix;
|
---|
999 | }
|
---|
1000 | }
|
---|
1001 |
|
---|
1002 | static void oscam_init_serialdata(struct s_serial_client *dest)
|
---|
1003 | {
|
---|
1004 | if(dest)
|
---|
1005 | {
|
---|
1006 | memset(dest, 0, sizeof(struct s_serial_client));
|
---|
1007 | dest->oscam_ser_timeout = 50;
|
---|
1008 | dest->dsr9500type=P_DSR_AUTO;
|
---|
1009 | }
|
---|
1010 | }
|
---|
1011 |
|
---|
1012 | static void * oscam_ser_fork(void *pthreadparam)
|
---|
1013 | {
|
---|
1014 | struct s_thread_param *pparam = (struct s_thread_param *) pthreadparam;
|
---|
1015 | struct s_client *cl=create_client(get_null_ip());
|
---|
1016 | pthread_setspecific(getclient, cl);
|
---|
1017 | cl->thread=pthread_self();
|
---|
1018 | cl->typ='c';
|
---|
1019 | cl->module_idx = pparam->module_idx;
|
---|
1020 | cl->account=first_client->account;
|
---|
1021 |
|
---|
1022 | if (!cl->serialdata && !cs_malloc(&cl->serialdata, sizeof(struct s_serial_client)))
|
---|
1023 | return NULL;
|
---|
1024 |
|
---|
1025 | set_thread_name(__func__);
|
---|
1026 | oscam_init_serialdata(cl->serialdata);
|
---|
1027 | oscam_copy_serialdata(cl->serialdata, &pparam->serialdata);
|
---|
1028 | cs_log("serial: initialized (%s@%s)", cl->serialdata->oscam_ser_proto>P_MAX ?
|
---|
1029 | "auto" : proto_txt[cl->serialdata->oscam_ser_proto], cl->serialdata->oscam_ser_device);
|
---|
1030 |
|
---|
1031 | pthread_mutex_lock(&mutex);
|
---|
1032 | bcopy_end = 1;
|
---|
1033 | pthread_mutex_unlock(&mutex);
|
---|
1034 | pthread_cond_signal(&cond);
|
---|
1035 |
|
---|
1036 | while(1)
|
---|
1037 | {
|
---|
1038 | cl->login=time((time_t *)0);
|
---|
1039 | cl->pfd=init_oscam_ser_device(cl->serialdata->oscam_ser_device, cl->serialdata->oscam_ser_baud);
|
---|
1040 | if (cl->pfd)
|
---|
1041 | oscam_ser_server();
|
---|
1042 | else
|
---|
1043 | cs_sleepms(60000); // retry in 1 min. (USB-Device ?)
|
---|
1044 | if (cl->pfd) close(cl->pfd);
|
---|
1045 | }
|
---|
1046 | NULLFREE(cl->serialdata);
|
---|
1047 | return NULL;
|
---|
1048 | }
|
---|
1049 |
|
---|
1050 | void * init_oscam_ser(struct s_client *UNUSED(cl), uchar *UNUSED(mbuf), int32_t module_idx)
|
---|
1051 | {
|
---|
1052 | char sdevice[512];
|
---|
1053 | int32_t ret;
|
---|
1054 | struct s_thread_param param;
|
---|
1055 | pthread_attr_t attr;
|
---|
1056 | pthread_attr_init(&attr);
|
---|
1057 | pthread_attr_setstacksize(&attr, PTHREAD_STACK_SIZE);
|
---|
1058 | oscam_init_serialdata(¶m.serialdata);
|
---|
1059 | if (cfg.ser_device)
|
---|
1060 | cs_strncpy(sdevice, cfg.ser_device, sizeof(sdevice));
|
---|
1061 | else
|
---|
1062 | memset(sdevice, 0, sizeof(sdevice));
|
---|
1063 | param.module_idx = module_idx;
|
---|
1064 | char *p;
|
---|
1065 | pthread_t temp;
|
---|
1066 | char cltype = 'c'; //now auto should work
|
---|
1067 | if(bcopy_end == -1){ //mutex should be initialized only once
|
---|
1068 | pthread_mutex_init(&mutex,NULL);
|
---|
1069 | pthread_cond_init(&cond,NULL);
|
---|
1070 | bcopy_end = 0;
|
---|
1071 | }
|
---|
1072 | while( (p=strrchr(sdevice, ';')) )
|
---|
1073 | {
|
---|
1074 | *p = 0;
|
---|
1075 | if (!(p + 1) || (!(p + 1)[0])) return NULL;
|
---|
1076 | if (!oscam_ser_parse_url(p + 1, ¶m.serialdata, &cltype)) return NULL;
|
---|
1077 | ret = pthread_create(&temp, &attr, oscam_ser_fork, (void *) ¶m);
|
---|
1078 | if(ret){
|
---|
1079 | cs_log("ERROR: can't create serial reader thread (errno=%d %s)", ret, strerror(ret));
|
---|
1080 | pthread_attr_destroy(&attr);
|
---|
1081 | return NULL;
|
---|
1082 | } else {
|
---|
1083 | oscam_wait_ser_fork();
|
---|
1084 | pthread_detach(temp);
|
---|
1085 | }
|
---|
1086 | }
|
---|
1087 |
|
---|
1088 | if (!sdevice[0]) return NULL;
|
---|
1089 | if (!oscam_ser_parse_url(sdevice, ¶m.serialdata, &cltype)) return NULL;
|
---|
1090 | ret = pthread_create(&temp, &attr, oscam_ser_fork, (void *) ¶m);
|
---|
1091 | if(ret){
|
---|
1092 | cs_log("ERROR: can't create serial reader thread (errno=%d %s)", ret, strerror(ret));
|
---|
1093 | pthread_attr_destroy(&attr);
|
---|
1094 | return NULL;
|
---|
1095 | } else {
|
---|
1096 | oscam_wait_ser_fork();
|
---|
1097 | pthread_detach(temp);
|
---|
1098 | }
|
---|
1099 | pthread_attr_destroy(&attr);
|
---|
1100 | return NULL;
|
---|
1101 | }
|
---|
1102 |
|
---|
1103 | /*
|
---|
1104 | * client functions
|
---|
1105 | */
|
---|
1106 |
|
---|
1107 | static int32_t oscam_ser_client_init(struct s_client *client)
|
---|
1108 | {
|
---|
1109 | if (!client->serialdata && !cs_malloc(&client->serialdata, sizeof(struct s_serial_client)))
|
---|
1110 | return 1;
|
---|
1111 |
|
---|
1112 | oscam_init_serialdata(client->serialdata);
|
---|
1113 |
|
---|
1114 | if ((!client->reader->device[0])) cs_disconnect_client(client);
|
---|
1115 | if (!oscam_ser_parse_url(client->reader->device, client->serialdata, NULL)) cs_disconnect_client(client);
|
---|
1116 | client->pfd=init_oscam_ser_device(client->serialdata->oscam_ser_device, client->serialdata->oscam_ser_baud);
|
---|
1117 | return((client->pfd>0) ? 0 : 1);
|
---|
1118 | }
|
---|
1119 |
|
---|
1120 | static int32_t oscam_ser_send_ecm(struct s_client *client, ECM_REQUEST *er, uchar *buf)
|
---|
1121 | {
|
---|
1122 | char *tmp;
|
---|
1123 | switch(client->serialdata->oscam_ser_proto)
|
---|
1124 | {
|
---|
1125 | case P_HSIC:
|
---|
1126 | memset(buf, 0, 12);
|
---|
1127 | buf[0]=2;
|
---|
1128 | i2b_buf(2, er->caid, buf + 1);
|
---|
1129 | i2b_buf(3, er->prid, buf + 3);
|
---|
1130 | i2b_buf(2, er->pid, buf + 6);
|
---|
1131 | i2b_buf(2, er->srvid, buf + 10);
|
---|
1132 | memcpy(buf+12, er->ecm, er->ecmlen);
|
---|
1133 | oscam_ser_send(client, buf, 12+er->ecmlen);
|
---|
1134 | break;
|
---|
1135 | case P_BOMBA:
|
---|
1136 | oscam_ser_send(client, er->ecm, er->ecmlen);
|
---|
1137 | break;
|
---|
1138 | case P_DSR95:
|
---|
1139 | if (cs_malloc(&tmp, er->ecmlen * 2 + 1)) {
|
---|
1140 | if( client->serialdata->dsr9500type==P_DSR_WITHSID )
|
---|
1141 | {
|
---|
1142 | snprintf((char *)buf, 512, "%c%08X%04X%s%04X\n\r",
|
---|
1143 | 3, er->prid, er->caid, cs_hexdump(0, er->ecm, er->ecmlen, tmp, er->ecmlen * 2 + 1), er->srvid);
|
---|
1144 | oscam_ser_send(client, buf, (er->ecmlen<<1)+19); // 1 + 8 + 4 + l*2 + 4 + 2
|
---|
1145 | }
|
---|
1146 | else
|
---|
1147 | {
|
---|
1148 | snprintf((char *)buf, 512, "%c%08X%04X%s\n\r",
|
---|
1149 | 3, er->prid, er->caid, cs_hexdump(0, er->ecm, er->ecmlen, tmp, er->ecmlen * 2 + 1));
|
---|
1150 | oscam_ser_send(client, buf, (er->ecmlen<<1)+15); // 1 + 8 + 4 + l*2 + 2
|
---|
1151 | }
|
---|
1152 | free(tmp);
|
---|
1153 | }
|
---|
1154 | break;
|
---|
1155 | case P_ALPHA:
|
---|
1156 | buf[0]=0x80;
|
---|
1157 | i2b_buf(2, 2+er->ecmlen, buf + 1);
|
---|
1158 | i2b_buf(2, er->caid, buf + 3);
|
---|
1159 | memcpy(buf+5, er->ecm, er->ecmlen);
|
---|
1160 | oscam_ser_send(client, buf, oscam_ser_alpha_convert(buf, 5+er->ecmlen));
|
---|
1161 | break;
|
---|
1162 | }
|
---|
1163 | return(0);
|
---|
1164 | }
|
---|
1165 |
|
---|
1166 | static void oscam_ser_process_dcw(uchar *dcw, int32_t *rc, uchar *buf, int32_t l, struct s_client *client)
|
---|
1167 | {
|
---|
1168 | switch(client->serialdata->oscam_ser_proto)
|
---|
1169 | {
|
---|
1170 | case P_HSIC:
|
---|
1171 | if ((l>=23) && (buf[2]==0x3A) && (buf[3]==0x3A))
|
---|
1172 | {
|
---|
1173 | int32_t i;
|
---|
1174 | uchar crc;
|
---|
1175 | for (i=4, crc=HSIC_CRC; i<20; i++)
|
---|
1176 | crc^=buf[i];
|
---|
1177 | if (crc==buf[20])
|
---|
1178 | {
|
---|
1179 | memcpy(dcw, buf+4, 16);
|
---|
1180 | *rc=1;
|
---|
1181 | }
|
---|
1182 | }
|
---|
1183 | break;
|
---|
1184 | case P_BOMBA:
|
---|
1185 | if (l>=16)
|
---|
1186 | {
|
---|
1187 | memcpy(dcw, buf, 16);
|
---|
1188 | *rc=1;
|
---|
1189 | }
|
---|
1190 | break;
|
---|
1191 | case P_DSR95:
|
---|
1192 | if ((l>=17) && (buf[0]==4))
|
---|
1193 | {
|
---|
1194 | memcpy(dcw, buf+1, 16);
|
---|
1195 | *rc=1;
|
---|
1196 | }
|
---|
1197 | break;
|
---|
1198 | case P_ALPHA:
|
---|
1199 | if ((l>=19) && (buf[0]==0x88))
|
---|
1200 | {
|
---|
1201 | memcpy(dcw, buf+3, 16);
|
---|
1202 | *rc=1;
|
---|
1203 | }
|
---|
1204 | break;
|
---|
1205 | }
|
---|
1206 | }
|
---|
1207 |
|
---|
1208 | static int32_t oscam_ser_recv_chk(struct s_client *client, uchar *dcw, int32_t *rc, uchar *buf, int32_t n)
|
---|
1209 | {
|
---|
1210 | *rc=(-1);
|
---|
1211 | switch (buf[0]>>4)
|
---|
1212 | {
|
---|
1213 | case IS_DCW:
|
---|
1214 | oscam_ser_process_dcw(dcw, rc, buf+1, n-1, client);
|
---|
1215 | break;
|
---|
1216 | }
|
---|
1217 | return((*rc<0) ? (-1) : 0); // idx not supported in serial module
|
---|
1218 | }
|
---|
1219 |
|
---|
1220 | /*
|
---|
1221 | * protocol structure
|
---|
1222 | */
|
---|
1223 |
|
---|
1224 | void module_serial(struct s_module *ph)
|
---|
1225 | {
|
---|
1226 | ph->desc="serial";
|
---|
1227 | ph->type=MOD_CONN_SERIAL;
|
---|
1228 | ph->large_ecm_support = 1;
|
---|
1229 | ph->listenertype = LIS_SERIAL;
|
---|
1230 | ph->s_handler=init_oscam_ser;
|
---|
1231 | ph->recv=oscam_ser_recv;
|
---|
1232 | ph->send_dcw=oscam_ser_send_dcw;
|
---|
1233 | ph->c_init=oscam_ser_client_init;
|
---|
1234 | ph->c_recv_chk=oscam_ser_recv_chk;
|
---|
1235 | ph->c_send_ecm=oscam_ser_send_ecm;
|
---|
1236 | ph->num=R_SERIAL;
|
---|
1237 | }
|
---|
1238 | #endif
|
---|