1 | #define MODULE_LOG_PREFIX "dvbmca"
|
---|
2 |
|
---|
3 | /**
|
---|
4 | * dvbapi-support for Matrix Cam Air
|
---|
5 | *
|
---|
6 | * The code is based partially on module-dvbapi-azbox.
|
---|
7 | * the (closed source) oscam that comes with the MCA is
|
---|
8 | * apparently based on svn-revision 5124-5242.
|
---|
9 | * DEMUXMATRIX is essentially the old DEMUXTYPE which
|
---|
10 | * has changed since then.
|
---|
11 | *
|
---|
12 | * We may be able to implement add/remove-filter
|
---|
13 | * by adding/removing them from DEMUXMATRIX
|
---|
14 | * and reexecute mca_write_flt
|
---|
15 | * In some cases the MCA will send ECMs for multiple PIDS
|
---|
16 | * So it is apparently able to handle multiple filters
|
---|
17 | *
|
---|
18 | * @author dirtyharry123
|
---|
19 | */
|
---|
20 |
|
---|
21 | #include "globals.h"
|
---|
22 |
|
---|
23 | #if defined(HAVE_DVBAPI) && defined(WITH_MCA)
|
---|
24 |
|
---|
25 | #include "extapi/openxcas/openxcas_message.h"
|
---|
26 |
|
---|
27 | #include "module-dvbapi.h"
|
---|
28 | #include "module-dvbapi-mca.h"
|
---|
29 | #include "oscam-client.h"
|
---|
30 | #include "oscam-ecm.h"
|
---|
31 | #include "oscam-reader.h"
|
---|
32 | #include "oscam-string.h"
|
---|
33 | #include "oscam-time.h"
|
---|
34 |
|
---|
35 | int8_t dummy(void)
|
---|
36 | {
|
---|
37 | return 0;
|
---|
38 | }
|
---|
39 | #define openxcas_start_filter_ex(...) dummy()
|
---|
40 | #define openxcas_stop_filter_ex(...) dummy()
|
---|
41 | #define openxcas_get_message mca_get_message
|
---|
42 | #define azbox_openxcas_ex_callback mca_ex_callback
|
---|
43 | #define openxcas_stop_filter(...) do { } while(0)
|
---|
44 | #define openxcas_remove_filter(...) do { } while(0)
|
---|
45 | #define openxcas_destory_cipher_ex(...) do { } while(0)
|
---|
46 |
|
---|
47 | // These variables are declared in module-dvbapi.c
|
---|
48 | extern void *dvbapi_client;
|
---|
49 | extern DEMUXTYPE demux[MAX_DEMUX];
|
---|
50 |
|
---|
51 | // These are used in module-dvbapi.c
|
---|
52 | int32_t openxcas_provid;
|
---|
53 | uint16_t openxcas_sid, openxcas_caid, openxcas_ecm_pid;
|
---|
54 |
|
---|
55 | static uint8_t openxcas_cw[16];
|
---|
56 | static int32_t openxcas_seq, openxcas_filter_idx, openxcas_stream_id, openxcas_cipher_idx, openxcas_busy = 0;
|
---|
57 | static uint16_t openxcas_video_pid, openxcas_audio_pid, openxcas_data_pid;
|
---|
58 | static uint8_t found[MAX_DEMUX];
|
---|
59 |
|
---|
60 | static int fd_mdvbi = -1;
|
---|
61 | static int fd_mdesc = -1;
|
---|
62 | static int fd_mflt = -1;
|
---|
63 |
|
---|
64 | #define MCA_DVBI "/tmp/mdvbi"
|
---|
65 | #define MCA_DESC "/tmp/mdesc"
|
---|
66 | #define MCA_FLT "/tmp/mflt"
|
---|
67 |
|
---|
68 | enum eOPENXCAS_FILTER_TYPE
|
---|
69 | {
|
---|
70 | OPENXCAS_FILTER_UNKNOWN = 0,
|
---|
71 | OPENXCAS_FILTER_ECM,
|
---|
72 | OPENXCAS_FILTER_EMM,
|
---|
73 | };
|
---|
74 |
|
---|
75 | #define ECM_PIDS_MATRIX 20
|
---|
76 | #define MAX_FILTER_MATRIX 10
|
---|
77 |
|
---|
78 | struct s_ecmpids_matrix
|
---|
79 | {
|
---|
80 | uint16_t CAID;
|
---|
81 | uint32_t PROVID;
|
---|
82 | uint16_t ECM_PID;
|
---|
83 | uint16_t EMM_PID;
|
---|
84 | int32_t irdeto_maxindex;
|
---|
85 | int32_t irdeto_curindex;
|
---|
86 | int32_t irdeto_cycle;
|
---|
87 | int32_t checked;
|
---|
88 | int32_t status;
|
---|
89 | uint8_t table;
|
---|
90 | int32_t index;
|
---|
91 | uint32_t streams;
|
---|
92 | };
|
---|
93 |
|
---|
94 | typedef struct filter_s_matrix
|
---|
95 | {
|
---|
96 | uint32_t fd; //FilterHandle
|
---|
97 | int32_t pidindex;
|
---|
98 | int32_t pid;
|
---|
99 | uint16_t type;
|
---|
100 | int32_t count;
|
---|
101 | } FILTERTYPE_MATRIX;
|
---|
102 |
|
---|
103 | struct s_emmpids_matrix
|
---|
104 | {
|
---|
105 | uint16_t CAID;
|
---|
106 | uint32_t PROVID;
|
---|
107 | uint16_t PID;
|
---|
108 | uint8_t type;
|
---|
109 | };
|
---|
110 |
|
---|
111 | typedef struct demux_s_matrix
|
---|
112 | {
|
---|
113 | int32_t demux_index;
|
---|
114 | FILTERTYPE_MATRIX demux_fd[MAX_FILTER_MATRIX];
|
---|
115 | int32_t ca_mask;
|
---|
116 | int32_t adapter_index;
|
---|
117 | int32_t socket_fd;
|
---|
118 | int32_t ECMpidcount;
|
---|
119 | struct s_ecmpids_matrix ECMpids[ECM_PIDS_MATRIX];
|
---|
120 | int32_t EMMpidcount;
|
---|
121 | struct s_emmpids_matrix EMMpids[ECM_PIDS_MATRIX];
|
---|
122 | int32_t STREAMpidcount;
|
---|
123 | uint16_t STREAMpids[ECM_PIDS_MATRIX];
|
---|
124 | int32_t pidindex;
|
---|
125 | int32_t curindex;
|
---|
126 | int32_t tries;
|
---|
127 | int32_t max_status;
|
---|
128 | uint16_t program_number;
|
---|
129 | uint8_t lastcw[2][8];
|
---|
130 | int32_t emm_filter;
|
---|
131 | uint8_t hexserial[8];
|
---|
132 | struct s_reader *rdr;
|
---|
133 | char pmt_file[30];
|
---|
134 | int32_t pmt_time;
|
---|
135 | } DEMUXMATRIX;
|
---|
136 |
|
---|
137 | static int mca_open(void)
|
---|
138 | {
|
---|
139 | if((fd_mdvbi = open(MCA_DVBI, O_RDONLY)) < 0)
|
---|
140 | {
|
---|
141 | cs_log("can't open \"%s\" (err=%d %s)", MCA_DVBI, errno, strerror(errno));
|
---|
142 | return -1;
|
---|
143 | }
|
---|
144 | if((fd_mdesc = open(MCA_DESC, O_WRONLY)) < 0)
|
---|
145 | {
|
---|
146 | cs_log("can't open \"%s\" (err=%d %s)", MCA_DESC, errno, strerror(errno));
|
---|
147 | return -1;
|
---|
148 | }
|
---|
149 | if((fd_mflt = open(MCA_FLT, O_WRONLY)) < 0)
|
---|
150 | {
|
---|
151 | cs_log("can't open \"%s\" (err=%d %s)", MCA_FLT, errno, strerror(errno));
|
---|
152 | return -1;
|
---|
153 | }
|
---|
154 | return 0;
|
---|
155 | }
|
---|
156 |
|
---|
157 | static int mca_exit(void)
|
---|
158 | {
|
---|
159 | if((fd_mdvbi = close(fd_mdvbi)) < 0)
|
---|
160 | {
|
---|
161 | cs_log("can't close \"%s\" (err=%d %s)", MCA_DVBI, errno, strerror(errno));
|
---|
162 | return -1;
|
---|
163 | }
|
---|
164 | if((fd_mdvbi = close(fd_mdesc)) < 0)
|
---|
165 | {
|
---|
166 | cs_log("can't close \"%s\" (err=%d %s)", MCA_DESC, errno, strerror(errno));
|
---|
167 | return -1;
|
---|
168 | }
|
---|
169 | if((fd_mdvbi = close(fd_mflt)) < 0)
|
---|
170 | {
|
---|
171 | cs_log("can't close \"%s\" (err=%d %s)", MCA_FLT, errno, strerror(errno));
|
---|
172 | return -1;
|
---|
173 | }
|
---|
174 | return 0;
|
---|
175 | }
|
---|
176 |
|
---|
177 | void mca_init(void)
|
---|
178 | {
|
---|
179 | if(mca_open() < 0)
|
---|
180 | {
|
---|
181 | cs_log("could not init");
|
---|
182 | }
|
---|
183 | }
|
---|
184 |
|
---|
185 | void mca_close(void)
|
---|
186 | {
|
---|
187 | if(mca_exit() < 0)
|
---|
188 | {
|
---|
189 | cs_log("could not close");
|
---|
190 | }
|
---|
191 | }
|
---|
192 |
|
---|
193 | static int mca_get_message(openxcas_msg_t *message, int timeout)
|
---|
194 | {
|
---|
195 | int rval = -1;
|
---|
196 | struct pollfd mdvbi_poll_fd;
|
---|
197 | mdvbi_poll_fd.fd = fd_mdvbi;
|
---|
198 | mdvbi_poll_fd.events = POLLIN | POLLPRI;
|
---|
199 | rval = poll(&mdvbi_poll_fd, 1, timeout == 0 ? -1 : timeout);
|
---|
200 | if((rval >= 1) && (mdvbi_poll_fd.revents & (POLLIN | POLLPRI)))
|
---|
201 | {
|
---|
202 | rval = read(fd_mdvbi, message, 568);
|
---|
203 | }
|
---|
204 | else { rval = -1; }
|
---|
205 | return rval;
|
---|
206 | }
|
---|
207 |
|
---|
208 | static int mca_write_flt(DEMUXMATRIX *demux_matrix, int timeout)
|
---|
209 | {
|
---|
210 | int rval = -1;
|
---|
211 | struct pollfd mflt_poll_fd;
|
---|
212 | mflt_poll_fd.fd = fd_mflt;
|
---|
213 | mflt_poll_fd.events = POLLOUT;
|
---|
214 | rval = poll(&mflt_poll_fd, 1, timeout);
|
---|
215 | if((rval >= 1) && (mflt_poll_fd.revents & POLLOUT))
|
---|
216 | {
|
---|
217 | rval = write(fd_mflt, demux_matrix, sizeof(DEMUXMATRIX));
|
---|
218 | }
|
---|
219 | else { rval = -1; }
|
---|
220 | return rval;
|
---|
221 | }
|
---|
222 |
|
---|
223 | static int mca_set_key(uint8_t *mca_cw)
|
---|
224 | {
|
---|
225 | int rval = -1;
|
---|
226 | struct pollfd mdesc_poll_fd;
|
---|
227 | mdesc_poll_fd.fd = fd_mdesc;
|
---|
228 | mdesc_poll_fd.events = POLLOUT;
|
---|
229 | rval = poll(&mdesc_poll_fd, 1, 0);
|
---|
230 | if((rval >= 1) && (mdesc_poll_fd.revents & POLLOUT))
|
---|
231 | {
|
---|
232 | rval = write(fd_mdesc, mca_cw, 16);
|
---|
233 | }
|
---|
234 | else { rval = -1; }
|
---|
235 | return rval;
|
---|
236 | }
|
---|
237 |
|
---|
238 | static int mca_capmt_remove_duplicates(uint8_t *capmt, int len)
|
---|
239 | {
|
---|
240 | int i, newlen = len;
|
---|
241 | uint16_t descriptor_length = 0;
|
---|
242 | uint32_t program_info_length = ((capmt[4] & 0x0F) << 8) | capmt[5];
|
---|
243 | for(i = 7; i < len; i += descriptor_length + 2)
|
---|
244 | {
|
---|
245 | descriptor_length = capmt[i + 1];
|
---|
246 | if(capmt[i] != 0x09) { continue; }
|
---|
247 | if(!memcmp(&(capmt[i]), &(capmt[i + descriptor_length + 2]), descriptor_length + 2))
|
---|
248 | {
|
---|
249 | memmove(&(capmt[i + descriptor_length + 2]), &(capmt[i + (2 * (descriptor_length + 2))]), newlen - (descriptor_length + 2));
|
---|
250 | newlen -= descriptor_length + 2;
|
---|
251 | }
|
---|
252 | }
|
---|
253 | program_info_length -= (len - newlen);
|
---|
254 | capmt[4] = (uint8_t)((capmt[4] & 0xF0) | ((program_info_length & 0xF00) >> 8));
|
---|
255 | capmt[5] = (uint8_t)(program_info_length & 0x0FF);
|
---|
256 | return newlen;
|
---|
257 | }
|
---|
258 |
|
---|
259 | static void mca_demux_convert(DEMUXTYPE *demux_orig, DEMUXMATRIX *demux_matrix)
|
---|
260 | {
|
---|
261 | int i = 0;
|
---|
262 | memset(demux_matrix, 0, sizeof(DEMUXMATRIX));
|
---|
263 | demux_matrix->demux_index = (int32_t)demux_orig->demux_index;
|
---|
264 | for(i = 0; i < MAX_FILTER_MATRIX; ++i)
|
---|
265 | {
|
---|
266 | demux_matrix->demux_fd[i].fd = (uint32_t) demux_orig->demux_fd[i].fd;
|
---|
267 | demux_matrix->demux_fd[i].pidindex = (int32_t) demux_orig->demux_fd[i].pidindex;
|
---|
268 | demux_matrix->demux_fd[i].pid = (int32_t) demux_orig->demux_fd[i].pid;
|
---|
269 | demux_matrix->demux_fd[i].type = (uint16_t) demux_orig->demux_fd[i].type;
|
---|
270 | demux_matrix->demux_fd[i].count = (int32_t) demux_orig->demux_fd[i].count;
|
---|
271 | }
|
---|
272 | demux_matrix->ca_mask = (int32_t)demux_orig->ca_mask;
|
---|
273 | demux_matrix->adapter_index = (int32_t)demux_orig->adapter_index;
|
---|
274 | demux_matrix->socket_fd = (int32_t)demux_orig->socket_fd;
|
---|
275 | demux_matrix->ECMpidcount = (int32_t)demux_orig->ECMpidcount;
|
---|
276 | for(i = 0; i < demux_matrix->ECMpidcount; ++i)
|
---|
277 | {
|
---|
278 | demux_matrix->ECMpids[i].CAID = (uint16_t)demux_orig->ECMpids[i].CAID;
|
---|
279 | demux_matrix->ECMpids[i].PROVID = (uint32_t)demux_orig->ECMpids[i].PROVID;
|
---|
280 | demux_matrix->ECMpids[i].ECM_PID = (uint16_t)demux_orig->ECMpids[i].ECM_PID;
|
---|
281 | demux_matrix->ECMpids[i].EMM_PID = (uint16_t)demux_orig->ECMpids[i].EMM_PID;
|
---|
282 | demux_matrix->ECMpids[i].irdeto_maxindex = (int32_t)demux_orig->ECMpids[i].irdeto_maxindex;
|
---|
283 | demux_matrix->ECMpids[i].irdeto_curindex = (int32_t)demux_orig->ECMpids[i].irdeto_curindex;
|
---|
284 | demux_matrix->ECMpids[i].irdeto_cycle = (int32_t)demux_orig->ECMpids[i].irdeto_cycle;
|
---|
285 | demux_matrix->ECMpids[i].checked = (int32_t)demux_orig->ECMpids[i].checked;
|
---|
286 | demux_matrix->ECMpids[i].status = (int32_t)demux_orig->ECMpids[i].status;
|
---|
287 | demux_matrix->ECMpids[i].table = (uint8_t)demux_orig->ECMpids[i].table;
|
---|
288 | demux_matrix->ECMpids[i].streams = (uint32_t)demux_orig->ECMpids[i].streams;
|
---|
289 | }
|
---|
290 | demux_matrix->STREAMpidcount = (int32_t)demux->STREAMpidcount;
|
---|
291 | memcpy(&demux_matrix->STREAMpids, &demux_orig->STREAMpids, demux_matrix->STREAMpidcount * sizeof(uint16_t));
|
---|
292 | demux_matrix->pidindex = (int32_t)demux_orig->pidindex;
|
---|
293 | demux_matrix->curindex = (int32_t)demux_orig->curindex;
|
---|
294 | demux_matrix->max_status = (int32_t)demux_orig->max_status;
|
---|
295 | demux_matrix->program_number = (uint16_t)demux_orig->program_number;
|
---|
296 | memcpy(&demux_matrix->lastcw[0], &demux_orig->last_cw[0][0], 8 * sizeof(uint8_t));
|
---|
297 | memcpy(&demux_matrix->lastcw[1], &demux_orig->last_cw[0][1], 8 * sizeof(uint8_t));
|
---|
298 | demux_matrix->emm_filter = (int32_t)demux_orig->emm_filter;
|
---|
299 | memcpy(&demux_matrix->hexserial, &demux_orig->hexserial, 8 * sizeof(uint8_t));
|
---|
300 | demux_matrix->rdr = (struct s_reader *)demux_orig->rdr;
|
---|
301 | memcpy(&demux_matrix->pmt_file, &demux_orig->pmt_file, 30);
|
---|
302 | demux_matrix->pmt_time = (int32_t)demux_orig->pmt_time;
|
---|
303 | }
|
---|
304 |
|
---|
305 | static void mca_ecm_callback(int32_t stream_id, uint32_t UNUSED(seq), int32_t cipher_index, uint32_t caid, uint8_t *ecm_data, int32_t l, uint16_t pid)
|
---|
306 | {
|
---|
307 | cs_log_dbg(D_DVBAPI, "ecm callback received");
|
---|
308 |
|
---|
309 | openxcas_stream_id = stream_id;
|
---|
310 | //openxcas_seq = seq;
|
---|
311 | //openxcas_caid = caid;
|
---|
312 | openxcas_ecm_pid = pid;
|
---|
313 | openxcas_busy = 1;
|
---|
314 | //char tmp[1024];
|
---|
315 |
|
---|
316 | //As soon as we have received a valid CW we lock onto that CAID, otherwise we will have freezers.
|
---|
317 | if(openxcas_caid && caid && openxcas_caid != caid)
|
---|
318 | {
|
---|
319 | cs_log("ignoring caid: %04X, waiting for %04X", caid, openxcas_caid);
|
---|
320 | openxcas_busy = 0;
|
---|
321 | return;
|
---|
322 | }
|
---|
323 |
|
---|
324 | if(l < 0 || l > MAX_ECM_SIZE)
|
---|
325 | { return; }
|
---|
326 |
|
---|
327 | ECM_REQUEST *er;
|
---|
328 | if(!(er = get_ecmtask()))
|
---|
329 | { return; }
|
---|
330 |
|
---|
331 | er->srvid = openxcas_sid;
|
---|
332 | er->caid = openxcas_caid;
|
---|
333 | er->pid = openxcas_ecm_pid;
|
---|
334 | er->prid = openxcas_provid;
|
---|
335 |
|
---|
336 | er->ecmlen = l;
|
---|
337 | memcpy(er->ecm, ecm_data, er->ecmlen);
|
---|
338 |
|
---|
339 | request_cw(dvbapi_client, er, 0, 0);
|
---|
340 |
|
---|
341 | openxcas_stop_filter(openxcas_stream_id, OPENXCAS_FILTER_ECM);
|
---|
342 | openxcas_remove_filter(openxcas_stream_id, OPENXCAS_FILTER_ECM);
|
---|
343 |
|
---|
344 | openxcas_cipher_idx = cipher_index;
|
---|
345 |
|
---|
346 | struct timeb tp;
|
---|
347 | cs_ftime(&tp);
|
---|
348 | tp.time += 500;
|
---|
349 | }
|
---|
350 |
|
---|
351 |
|
---|
352 | static void mca_ex_callback(int32_t stream_id, uint32_t UNUSED(seq), int32_t idx, uint32_t pid, uint8_t *ecm_data, int32_t l)
|
---|
353 | {
|
---|
354 | cs_log_dbg(D_DVBAPI, "ex callback received");
|
---|
355 |
|
---|
356 | openxcas_stream_id = stream_id;
|
---|
357 | openxcas_ecm_pid = pid;
|
---|
358 | openxcas_cipher_idx = idx; // is this really cipher_idx?
|
---|
359 |
|
---|
360 | if(l < 0 || l > MAX_ECM_SIZE)
|
---|
361 | { return; }
|
---|
362 |
|
---|
363 | ECM_REQUEST *er;
|
---|
364 | if(!(er = get_ecmtask()))
|
---|
365 | { return; }
|
---|
366 |
|
---|
367 | er->srvid = openxcas_sid;
|
---|
368 | er->caid = openxcas_caid;
|
---|
369 | er->pid = openxcas_ecm_pid;
|
---|
370 | er->prid = openxcas_provid;
|
---|
371 |
|
---|
372 | er->ecmlen = l;
|
---|
373 | memcpy(er->ecm, ecm_data, er->ecmlen);
|
---|
374 |
|
---|
375 | request_cw(dvbapi_client, er, 0, 0);
|
---|
376 |
|
---|
377 | if(openxcas_stop_filter_ex(stream_id, seq, openxcas_filter_idx) < 0)
|
---|
378 | { cs_log("unable to stop ex filter"); }
|
---|
379 | else
|
---|
380 | { cs_log_dbg(D_DVBAPI, "ex filter stopped"); }
|
---|
381 |
|
---|
382 | uint8_t mask[12];
|
---|
383 | uint8_t comp[12];
|
---|
384 | memset(&mask, 0x00, sizeof(mask));
|
---|
385 | memset(&comp, 0x00, sizeof(comp));
|
---|
386 |
|
---|
387 | mask[0] = 0xff;
|
---|
388 | comp[0] = ecm_data[0] ^ 1;
|
---|
389 |
|
---|
390 | if((openxcas_filter_idx = openxcas_start_filter_ex(stream_id, seq, openxcas_ecm_pid, mask, comp, (void *)azbox_openxcas_ex_callback)) < 0)
|
---|
391 | { cs_log("unable to start ex filter"); }
|
---|
392 | else
|
---|
393 | { cs_log_dbg(D_DVBAPI, "ex filter started, pid = %x", openxcas_ecm_pid); }
|
---|
394 | }
|
---|
395 |
|
---|
396 | static void *mca_main_thread(void *cli)
|
---|
397 | {
|
---|
398 | struct s_client *client = (struct s_client *) cli;
|
---|
399 | client->thread = pthread_self();
|
---|
400 | SAFE_SETSPECIFIC(getclient, cli);
|
---|
401 | dvbapi_client = cli;
|
---|
402 |
|
---|
403 | struct s_auth *account;
|
---|
404 | int32_t ok = 0;
|
---|
405 | for(account = cfg.account; account; account = account->next)
|
---|
406 | {
|
---|
407 | if((ok = is_dvbapi_usr(account->usr)))
|
---|
408 | { break; }
|
---|
409 | }
|
---|
410 | cs_auth_client(client, ok ? account : (struct s_auth *)(-1), "dvbapi");
|
---|
411 |
|
---|
412 | dvbapi_read_priority();
|
---|
413 |
|
---|
414 | openxcas_msg_t msg;
|
---|
415 | int32_t ret;
|
---|
416 | while((ret = openxcas_get_message(&msg, 0)) >= 0)
|
---|
417 | {
|
---|
418 | cs_sleepms(10);
|
---|
419 |
|
---|
420 | if(ret)
|
---|
421 | {
|
---|
422 | openxcas_stream_id = msg.stream_id;
|
---|
423 | openxcas_seq = msg.sequence;
|
---|
424 | struct stOpenXCAS_Data data;
|
---|
425 |
|
---|
426 | switch(msg.cmd)
|
---|
427 | {
|
---|
428 | case OPENXCAS_SELECT_CHANNEL:
|
---|
429 | cs_log_dbg(D_DVBAPI, "OPENXCAS_SELECT_CHANNEL");
|
---|
430 |
|
---|
431 | // parse channel info
|
---|
432 | struct stOpenXCASChannel chan;
|
---|
433 | memcpy(&chan, msg.buf, msg.buf_len);
|
---|
434 |
|
---|
435 | cs_log("channel change: sid = %x, vpid = %x. apid = %x", chan.service_id, chan.v_pid, chan.a_pid);
|
---|
436 |
|
---|
437 | openxcas_video_pid = chan.v_pid;
|
---|
438 | openxcas_audio_pid = chan.a_pid;
|
---|
439 | openxcas_data_pid = chan.d_pid;
|
---|
440 | break;
|
---|
441 | case OPENXCAS_START_PMT_ECM:
|
---|
442 | //FIXME: Apparently this is what the original MCA-oscam does
|
---|
443 | cs_log_dbg(D_DVBAPI, "OPENXCAS_STOP_PMT_ECM");
|
---|
444 | memset(&demux, 0, sizeof(demux));
|
---|
445 | memset(&found, 0, sizeof(found));
|
---|
446 |
|
---|
447 | cs_log_dbg(D_DVBAPI, "OPENXCAS_START_PMT_ECM");
|
---|
448 |
|
---|
449 | // parse pmt
|
---|
450 | cs_log_dump_dbg(D_DVBAPI, msg.buf + 2, msg.buf_len - 2, "capmt:");
|
---|
451 | // For some reason the mca sometimes sends duplicate ECMpids,
|
---|
452 | // we remove them here so dvbapi will not try them twice.
|
---|
453 | int new_len = mca_capmt_remove_duplicates(msg.buf + 2, msg.buf_len - 2);
|
---|
454 | if(new_len < msg.buf_len - 2)
|
---|
455 | { cs_log_dump_dbg(D_DVBAPI, msg.buf + 2, new_len, "capmt (duplicates removed):"); }
|
---|
456 | int demux_id = dvbapi_parse_capmt(msg.buf + 2, new_len, -1, NULL, 0, 0);
|
---|
457 |
|
---|
458 | uint8_t mask[12];
|
---|
459 | uint8_t comp[12];
|
---|
460 | memset(&mask, 0x00, sizeof(mask));
|
---|
461 | memset(&comp, 0x00, sizeof(comp));
|
---|
462 |
|
---|
463 | mask[0] = 0xfe;
|
---|
464 | comp[0] = 0x80;
|
---|
465 |
|
---|
466 | if(demux_id < 0)
|
---|
467 | {
|
---|
468 | cs_log("could not parse pmt");
|
---|
469 | break;
|
---|
470 | }
|
---|
471 |
|
---|
472 | //if ((ret = openxcas_add_filter(msg.stream_id, OPENXCAS_FILTER_ECM, 0, 0xffff, openxcas_ecm_pid, mask, comp, (void *)mca_ecm_callback)) < 0)
|
---|
473 | DEMUXMATRIX demux_matrix;
|
---|
474 | mca_demux_convert(&demux[demux_id], &demux_matrix);
|
---|
475 | if((ret = mca_write_flt(&demux_matrix, 0)) < 0)
|
---|
476 | { cs_log("unable to add ecm filter"); }
|
---|
477 | else
|
---|
478 | {
|
---|
479 | cs_log_dbg(D_DVBAPI, "ecm filter added, pid = %x, caid = %x", openxcas_ecm_pid, 0);
|
---|
480 | cs_log_dbg(D_DVBAPI, "ecm filter started");
|
---|
481 | }
|
---|
482 |
|
---|
483 | //if (!openxcas_create_cipher_ex(msg.stream_id, openxcas_seq, 0, openxcas_ecm_pid, openxcas_video_pid, 0xffff, openxcas_audio_pid, 0xffff, 0xffff, 0xffff))
|
---|
484 | // cs_log("failed to create cipher ex");
|
---|
485 | //else
|
---|
486 | cs_log_dbg(D_DVBAPI, "cipher created");
|
---|
487 | break;
|
---|
488 | case OPENXCAS_STOP_PMT_ECM:
|
---|
489 | cs_log_dbg(D_DVBAPI, "OPENXCAS_STOP_PMT_ECM");
|
---|
490 | openxcas_stop_filter(msg.stream_id, OPENXCAS_FILTER_ECM);
|
---|
491 | openxcas_remove_filter(msg.stream_id, OPENXCAS_FILTER_ECM);
|
---|
492 | openxcas_stop_filter_ex(msg.stream_id, msg.sequence, openxcas_filter_idx);
|
---|
493 | openxcas_destory_cipher_ex(msg.stream_id, msg.sequence);
|
---|
494 | memset(&demux, 0, sizeof(demux));
|
---|
495 | memset(&found, 0, sizeof(found));
|
---|
496 | break;
|
---|
497 | case OPENXCAS_ECM_CALLBACK:
|
---|
498 | cs_log_dbg(D_DVBAPI, "OPENXCAS_ECM_CALLBACK");
|
---|
499 | memcpy(&data, msg.buf, msg.buf_len);
|
---|
500 | if(!openxcas_busy)
|
---|
501 | //openxcas_filter_callback(msg.stream_id, msg.sequence, OPENXCAS_FILTER_ECM, &data);
|
---|
502 | { mca_ecm_callback(msg.stream_id, msg.sequence, data.cipher_index, data.ca_system_id, (uint8_t *)&data.buf, data.len, data.pid); }
|
---|
503 | break;
|
---|
504 | case OPENXCAS_PID_FILTER_CALLBACK:
|
---|
505 | cs_log_dbg(D_DVBAPI, "OPENXCAS_PID_FILTER_CALLBACK");
|
---|
506 | memcpy(&data, msg.buf, msg.buf_len);
|
---|
507 | //openxcas_filter_callback_ex(msg.stream_id, msg.sequence, (struct stOpenXCAS_Data *)msg.buf);
|
---|
508 | mca_ex_callback(msg.stream_id, msg.sequence, data.cipher_index, data.pid, (uint8_t *)&data.buf, data.len);
|
---|
509 | break;
|
---|
510 | case OPENXCAS_QUIT:
|
---|
511 | cs_log_dbg(D_DVBAPI, "OPENXCAS_QUIT");
|
---|
512 | mca_exit();
|
---|
513 | cs_log("exited");
|
---|
514 | return NULL;
|
---|
515 | break;
|
---|
516 | case OPENXCAS_UKNOWN_MSG:
|
---|
517 | default:
|
---|
518 | cs_log_dbg(D_DVBAPI, "OPENXCAS_UKNOWN_MSG (%d)", msg.cmd);
|
---|
519 | //cs_log_dump_dbg(D_DVBAPI, &msg, sizeof(msg), "msg dump:");
|
---|
520 | break;
|
---|
521 | }
|
---|
522 | }
|
---|
523 | }
|
---|
524 | cs_log("invalid message");
|
---|
525 | return NULL;
|
---|
526 | }
|
---|
527 |
|
---|
528 | void mca_send_dcw(struct s_client *client, ECM_REQUEST *er)
|
---|
529 | {
|
---|
530 | struct s_dvbapi_priority *delayentry = dvbapi_check_prio_match(0, demux[0].pidindex, 'd');
|
---|
531 | uint32_t delay = 0;
|
---|
532 |
|
---|
533 | cs_log_dbg(D_DVBAPI, "send_dcw");
|
---|
534 |
|
---|
535 | if(delayentry)
|
---|
536 | {
|
---|
537 | if(delayentry->delay < 1000)
|
---|
538 | {
|
---|
539 | delay = delayentry->delay;
|
---|
540 | cs_log_dbg(D_DVBAPI, "specific delay: write cw %d ms after ecmrequest", delay);
|
---|
541 | }
|
---|
542 | }
|
---|
543 | else if (cfg.dvbapi_delayer > 0)
|
---|
544 | {
|
---|
545 | delay = cfg.dvbapi_delayer;
|
---|
546 | cs_log_dbg(D_DVBAPI, "generic delay: write cw %d ms after ecmrequest", delay);
|
---|
547 | }
|
---|
548 |
|
---|
549 | delayer(er, delay);
|
---|
550 |
|
---|
551 | if(cfg.dvbapi_ecminfo_file != 0)
|
---|
552 | {
|
---|
553 | dvbapi_write_ecminfo_file(client, er, demux[0].last_cw[0][0], demux[0].last_cw[0][1], 8);
|
---|
554 | }
|
---|
555 |
|
---|
556 | openxcas_busy = 0;
|
---|
557 |
|
---|
558 | int32_t i;
|
---|
559 | for(i = 0; i < MAX_DEMUX; i++)
|
---|
560 | {
|
---|
561 |
|
---|
562 | if(er->rc >= E_NOTFOUND && !found[i])
|
---|
563 | {
|
---|
564 | cs_log_dbg(D_DVBAPI, "cw not found");
|
---|
565 |
|
---|
566 | if(demux[i].pidindex == -1)
|
---|
567 | { dvbapi_try_next_caid(i, 0, 0); }
|
---|
568 |
|
---|
569 | openxcas_stop_filter(openxcas_stream_id, OPENXCAS_FILTER_ECM);
|
---|
570 | openxcas_remove_filter(openxcas_stream_id, OPENXCAS_FILTER_ECM);
|
---|
571 |
|
---|
572 | uint8_t mask[12];
|
---|
573 | uint8_t comp[12];
|
---|
574 | memset(&mask, 0x00, sizeof(mask));
|
---|
575 | memset(&comp, 0x00, sizeof(comp));
|
---|
576 |
|
---|
577 | mask[0] = 0xfe;
|
---|
578 | comp[0] = 0x80;
|
---|
579 |
|
---|
580 | DEMUXMATRIX demux_matrix;
|
---|
581 | mca_demux_convert(&demux[0], &demux_matrix);
|
---|
582 | if(mca_write_flt(&demux_matrix, 0) < 0)
|
---|
583 | { cs_log("unable to add ecm filter (0)"); }
|
---|
584 | else
|
---|
585 | {
|
---|
586 | cs_log_dbg(D_DVBAPI, "ecm filter added, pid = %x, caid = %x", openxcas_ecm_pid, 0);
|
---|
587 | cs_log_dbg(D_DVBAPI, "ecm filter started");
|
---|
588 | }
|
---|
589 |
|
---|
590 | return;
|
---|
591 | }
|
---|
592 | else
|
---|
593 | {
|
---|
594 | found[i] = 1;
|
---|
595 | }
|
---|
596 | }
|
---|
597 |
|
---|
598 | uint8_t nullcw[8];
|
---|
599 | memset(nullcw, 0, 8);
|
---|
600 |
|
---|
601 | int32_t n;
|
---|
602 | for(n = 0; n < 2; n++)
|
---|
603 | {
|
---|
604 | // Skip check for BISS1 - cw could be indeed zero
|
---|
605 | // Skip check for BISS2 - we use the extended cw, so the "simple" cw is always zero
|
---|
606 | if((memcmp(er->cw + (n * 8), demux[0].last_cw[0][0], 8) && memcmp(er->cw + (n * 8), demux[0].last_cw[0][1], 8))
|
---|
607 | && (memcmp(er->cw + (n * 8), nullcw, 8) != 0 || caid_is_biss(er->caid)))
|
---|
608 | {
|
---|
609 | memcpy(demux[0].last_cw[0][n], er->cw + (n * 8), 8);
|
---|
610 | memcpy(openxcas_cw + (n * 8), er->cw + (n * 8), 8);
|
---|
611 | if(mca_set_key(openxcas_cw) < 0)
|
---|
612 | { cs_log("set cw failed"); }
|
---|
613 | else
|
---|
614 | { cs_log_dump_dbg(D_DVBAPI, openxcas_cw, 16, "write cws to descrambler"); }
|
---|
615 | }
|
---|
616 | }
|
---|
617 | }
|
---|
618 |
|
---|
619 | void *mca_handler(struct s_client *cl, uint8_t *mbuf, int32_t module_idx)
|
---|
620 | {
|
---|
621 | return dvbapi_start_handler(cl, mbuf, module_idx, mca_main_thread);
|
---|
622 | }
|
---|
623 |
|
---|
624 | #endif
|
---|