1 #ifndef MODULE_DVBAPI_H_
2 #define MODULE_DVBAPI_H_
22 #define STANDBY_FILE "./.pauseoscam"
23 #define ECMINFO_FILE "./ecm.info"
25 #define TMPDIR "/tmp/"
26 #define STANDBY_FILE "/tmp/.pauseoscam"
27 #define ECMINFO_FILE "/tmp/ecm.info"
32 #define BOXTYPE_DREAMBOX 1
33 #define BOXTYPE_DUCKBOX 2
34 #define BOXTYPE_UFS910 3
35 #define BOXTYPE_DBOX2 4
36 #define BOXTYPE_IPBOX 5
37 #define BOXTYPE_IPBOX_PMT 6
38 #define BOXTYPE_DM7000 7
39 #define BOXTYPE_QBOXHD 8
40 #define BOXTYPE_COOLSTREAM 9
41 #define BOXTYPE_NEUMO 10
43 #define BOXTYPE_PC_NODMX 12
44 #define BOXTYPE_SAMYGO 13
46 #define DMXMD5HASHSIZE 16 // use MD5()
48 // we store the results of remove_streampid_from_list()
49 // and update_streampid_list() in one variable, so make sure
50 // the return values do not collide
52 // remove_streampid_from_list()
53 #define NO_STREAMPID_LISTED 0x00
54 #define REMOVED_STREAMPID_INDEX 0x01
55 #define REMOVED_STREAMPID_LASTINDEX 0x02
56 #define REMOVED_DECODING_STREAMPID_INDEX 0x03
58 // update_streampid_list():
59 #define FOUND_STREAMPID_INDEX 0x10
60 #define ADDED_STREAMPID_INDEX 0x11
61 #define FIRST_STREAMPID_INDEX 0x12
63 // remove_streampid_from_list() and update_streampid_list()
64 #define INVALID_STREAMPID_INDEX 0x20
66 #define INDEX_DISABLE_ALL 0xEFFFFFFD // used for remove_streampid_from_list(), dvbapi_set_pid()
67 #define INDEX_INVALID 0xEFFFFFFF
69 #define DUMMY_FD 0xFFFF
71 //-----------------------------------------------------------------------------
72 // constants used in socket communication
73 //-----------------------------------------------------------------------------
75 #define DVBAPI_PROTOCOL_VERSION 3
76 #define DVBAPI_MAX_PACKET_SIZE 262 // maximum possible packet size
78 #define DVBAPI_CA_GET_DESCR_INFO 0x80086F83
79 #define DVBAPI_CA_SET_DESCR 0x40106F86
80 #define DVBAPI_CA_SET_PID 0x40086F87
81 #define DVBAPI_CA_SET_DESCR_MODE 0x400C6F88
82 #define DVBAPI_CA_SET_DESCR_DATA 0x40186F89
83 //#define DVBAPI_DMX_START 0x00006F29 // in case we ever need this
84 #define DVBAPI_DMX_STOP 0x00006F2A
85 #define DVBAPI_DMX_SET_FILTER 0x403C6F2B
87 #define DVBAPI_AOT_CA 0x9F803000
88 #define DVBAPI_AOT_CA_PMT 0x9F803200 // least significant byte is length (ignored)
89 #define DVBAPI_AOT_CA_STOP 0x9F803F04
90 #define DVBAPI_FILTER_DATA 0xFFFF0000
91 #define DVBAPI_CLIENT_INFO 0xFFFF0001
92 #define DVBAPI_SERVER_INFO 0xFFFF0002
93 #define DVBAPI_ECM_INFO 0xFFFF0003
95 #define DVBAPI_INDEX_DISABLE 0xFFFFFFFF // only used for ca_pid_t
97 //-----------------------------------------------------------------------------
98 // CA PMT defined values according to EN 50221
99 // https://www.dvb.org/resources/public/standards/En50221.V1.pdf
100 // https://www.dvb.org/resources/public/standards/R206-001.V1.pdf
101 //-----------------------------------------------------------------------------
103 // ca_pmt_list_management: This parameter is used to indicate whether the user has selected a single program or several
104 // programs. The following values can be used:
106 #define CA_PMT_LIST_MORE 0x00 // The CA PMT object is neither the first one, nor the last one of the list.
108 #define CA_PMT_LIST_FIRST 0x01 // The CA PMT object is the first one of a new list of more than one CA PMT object.
109 // All previously selected programs are being replaced by the programs of the new list.
111 #define CA_PMT_LIST_LAST 0x02 // The CA PMT object is the last of the list.
113 #define CA_PMT_LIST_ONLY 0x03 // The list is made of a single CA PMT object.
115 #define CA_PMT_LIST_ADD 0x04 // The CA PMT has to be added to an existing list, that is, a new program has been seleced
116 // by the user, but all previously selected programs remain selected.
118 #define CA_PMT_LIST_UPDATE 0x05 // The CA PMT of a program already in the list is sent again because the version_number or
119 // the current_next_indicator has changed.
121 // ca_pmt_cmd_id: This parameter indicates what response is required from the application to a CA PMT object. It can
122 // take the following values:
124 #define CA_PMT_CMD_OK_DESCRAMBLING 0x01 // The host does not expect answer to the CA PMT and the application can start
125 // descrambling the program or start an MMI dialogue immediately.
127 #define CA_PMT_CMD_OK_MMI 0x02 // The application can start an MMI dialogue, but shall not start descrambling
128 // before reception of a new CA PMT object with "ca_pmt_cmd_id" set to
129 // "ok_descrambling". In this case the host shall quarantee that an MMI session
130 // can be opened by the CA application.
132 #define CA_PMT_CMD_QUERY 0x03 // The host expects to receive a CA PMT reply. In this case, the applicaiton is
133 // not allowed to start descrambling or MMI dialogue before reception of a new
134 // CA PMT object with "ca_pmt_cmd_id" set to "ok_descrambling or "ok_mmi".
136 #define CA_PMT_CMD_NOT_SELECTED 0x04 // It indicates to the CA application that the host no longer requires that CA
137 // application to attempt to descramble the service. The CA application shall
138 // close any MMI dialogue it has opened.
144 #define ENIGMA_NAMESPACE 0x81
145 #define DEMUX_CA_MASK_ADAPTER 0x82 // deprecated - applications should use descriptors ADAPTER_DEVICE, DEMUX_DEVICE and CA_DEVICE instead
146 #define ADAPTER_DEVICE 0x83
148 #define SERVICE_TYPE_MASK 0x85 // not used by OSCam
149 #define DEMUX_DEVICE 0x86
150 #define CA_DEVICE 0x87
152 //-----------------------------------------------------------------------------
153 // api used for internal device communication
154 //-----------------------------------------------------------------------------
156 #define DMX_FILTER_SIZE 16
158 // The following is part of the linux dvb api (v1),
159 // but modifed to overcome some bugs in specific devices
161 typedef struct dmxFilter
163 uint8_t filter
[DMX_FILTER_SIZE
];
164 uint8_t mask
[DMX_FILTER_SIZE
];
167 struct dmxSctFilterParams
174 #define DMX_CHECK_CRC 1
175 #define DMX_ONESHOT 2
176 #define DMX_IMMEDIATE_START 4
179 #define DMX_START1 _IOW('o', 41, int)
180 #define DMX_STOP1 _IOW('o', 42, int)
181 #define DMX_SET_FILTER1 _IOW('o', 43, struct dmxSctFilterParams *)
183 // The following is part of the linux dvb api
184 // https://www.kernel.org/doc/html/latest/media/uapi/dvb/demux.html
185 // https://github.com/torvalds/linux/blob/master/include/uapi/linux/dvb/dmx.h
187 typedef struct dmx_filter
189 uint8_t filter
[DMX_FILTER_SIZE
];
190 uint8_t mask
[DMX_FILTER_SIZE
];
191 uint8_t mode
[DMX_FILTER_SIZE
];
194 struct dmx_sct_filter_params
201 #define DMX_CHECK_CRC 1
202 #define DMX_ONESHOT 2
203 #define DMX_IMMEDIATE_START 4
206 #define DMX_START _IO('o', 41)
207 #define DMX_STOP _IO('o', 42)
208 #define DMX_SET_FILTER _IOW('o', 43, struct dmx_sct_filter_params)
210 // The following is part of the linux dvb api
211 // https://www.kernel.org/doc/html/latest/media/uapi/dvb/ca.html
212 // https://github.com/torvalds/linux/blob/master/include/uapi/linux/dvb/ca.h
214 typedef struct ca_descr_info
217 uint32_t type
; /* bitmask: 1 == ECD, 2 == NDS, 4 == DDS */
220 typedef struct ca_descr
223 uint32_t parity
; /* 0 == even, 1 == odd */
227 // ca_pid has been removed from the api, but we still use it
228 typedef struct ca_pid
231 int32_t index
; /* -1 == disable */
241 enum ca_descr_cipher_mode
247 // Structs "ca_descr_mode" and "ca_descr_data" and respective ioctl
248 // commands are part of a custom api
251 * struct ca_descr_mode - Used to select a crypto algorithm and mode
254 * @index: Key slot allocated for a PID or service.
255 * See CA_SET_PID and struct ca_pid.
256 * @algo: Algorithm to select for @index.
257 * @cipher_mode: Cipher mode to use with @algo.
260 typedef struct ca_descr_mode
263 enum ca_descr_algo algo
;
264 enum ca_descr_cipher_mode cipher_mode
;
268 * struct ca_descr_data - Used to write Keys and IVs to a descrambler.
270 * @index: Key slot allocated for a PID or service.
271 * See CA_SET_PID and struct ca_pid.
272 * @parity: Indicates even or odd parity for control words.
273 * @data_type: Key or IV.
274 * @length: Size of @data array; depends on selected algorithm and
276 * @data: Pointer to variable @length key or initialization vector data.
279 enum ca_descr_data_type
291 typedef struct ca_descr_data
294 enum ca_descr_parity parity
;
295 enum ca_descr_data_type data_type
;
300 #define CA_GET_DESCR_INFO _IOR('o', 131, ca_descr_info_t)
301 #define CA_SET_DESCR _IOW('o', 134, ca_descr_t)
302 #define CA_SET_PID _IOW('o', 135, ca_pid_t)
303 #define CA_SET_DESCR_MODE _IOW('o', 136, ca_descr_mode_t)
304 #define CA_SET_DESCR_DATA _IOW('o', 137, ca_descr_data_t)
306 //-----------------------------------------------------------------------------
307 // OSCam defined structures
308 //-----------------------------------------------------------------------------
315 char *cam_socket_path
;
319 typedef struct filter_s
321 uint32_t fd
; // filter handle
330 uint8_t lastecmd5
[CS_ECMSTORESIZE
]; // last requested ecm md5
332 uint8_t prevecmd5
[CS_ECMSTORESIZE
]; // previous requested ecm md5
334 #if defined(WITH_STAPI) || defined(WITH_STAPI5)
336 uint32_t SlotHandle
[10];
337 uint32_t BufferHandle
[10];
344 #ifdef WITH_EXTENDED_CW
345 #define MAX_STREAM_INDICES 32 // In practice, 5 is the maximum ever used
347 #define MAX_STREAM_INDICES 1
350 #define CA_MAX 32 // Max ca devices supported by oscam - limited by sizeof(ca_mask) of struct demux_s (32 bits)
351 #define INDEX_MAX 64 // Max descramblers per ca device - limited by sizeof(activeindexers) of struct s_streampid (64 bits)
352 #define INDEX_MAX_LOCAL 16 // Max total descramblers to use for enigma2 and other STBs when dvbapi_get_descrambler_info() fails
353 #define INDEX_MAX_NET 64 // Max total descramblers to use for PC (VDR, Tvheadend, etc)
355 typedef struct s_ecmpid
358 uint32_t PROVID
; // provider
362 uint32_t VPID
; // video pid
363 uint8_t irdeto_maxindex
; // max irdeto indices always fresh fetched from current ecm
364 uint8_t irdeto_curindex
; // current irdeto index we want to handle
365 uint8_t irdeto_cycle
; // temp var that holds the irdeto index we started with to detect if we cycled trough all indices
370 int8_t useMultipleIndices
; // whether or not to use multiple indices for this ecm pid
371 uint32_t index
[MAX_STREAM_INDICES
]; // ca indices used for this ecm pid (index[0] holds ca index for STREAMmpids[0] and so on)
372 uint32_t streams
; // bit mask of STREAMpids enabled for this ECMpid
379 typedef struct s_emmpid
396 #define MAX_DEMUX 32 // Max number of demuxes supported by OSCam - each channel/service occupies one demux
397 #define MAX_ECM_PIDS 24 // Max number of ECM pids per demux
398 #define MAX_EMM_PIDS 24 // Max number of EMM pids per demux
399 #define MAX_STREAM_PIDS 32 // Max number of pids other than ECM and EMM (e.g. audio, video, subtitle, etc) per demux (hardware descramblers might have a capacity of 30 pids)
400 #define MAX_FILTER 64
405 typedef struct demux_s
407 int8_t demux_index
; // ID of the (hardware) demux device carrying this program - we get this via CA PMT
408 int8_t adapter_index
; // ID of the adapter device carrying this program - we get this via CA PMT
409 uint32_t ca_mask
; // Bit mask of ca devices used for descrambling this program - we get this via CA PMT
410 int32_t socket_fd
; // Connection identifier through which we received the CA PMT object
411 uint16_t client_proto_version
;
412 FILTERTYPE demux_fd
[MAX_FILTER
];
413 int8_t ECMpidcount
; // Count of ECM pids in this program
414 ECMPIDTYPE ECMpids
[MAX_ECM_PIDS
];
415 int8_t EMMpidcount
; // Count of EMM pids in this program
416 EMMPIDTYPE EMMpids
[MAX_EMM_PIDS
];
417 struct timeb emmstart
; // last time emm cat was started
418 uint16_t max_emm_filter
;
419 int8_t STREAMpidcount
;
420 uint16_t STREAMpids
[MAX_STREAM_PIDS
];
421 enum stream_type STREAMpidsType
[MAX_STREAM_PIDS
]; // type (audio, video, subtitle, etc) of the corresponding stream pid
422 int16_t pidindex
; // ECMpid used for descrambling - holds index of the ECMpids[] array
425 uint16_t program_number
; // also called service id (srvid)
426 uint16_t onid
; // original network id
427 uint16_t tsid
; // transport stream id
428 uint16_t pmtpid
; // PMT pid for the program_number
429 uint32_t ens
; // enigma namespace
430 uint8_t last_cw
[MAX_STREAM_INDICES
][2][16]; // even/odd pairs of 16 byte CWs used for descrambling on the last crypto period
433 uint8_t hexserial
[8];
434 struct s_reader
*rdr
;
437 bool stop_descrambling
; // Program is marked to stop descrambling (not selected in the new CA PMT list)
438 bool running
; // Descrambling is currently running for this program
439 uint8_t old_ecmfiltercount
; // previous ecm filter count
440 uint8_t old_emmfiltercount
; // previous emm filter count
441 pthread_mutex_t answerlock
; // request mode 1 avoid race
443 uint32_t DescramblerHandle
[PTINUM
];
444 int32_t desc_pidcount
;
445 uint32_t slot_assc
[PTINUM
][SLOTNUM
];
450 int8_t decodingtries
; // -1 = first run
451 struct timeb decstart
;
455 typedef struct s_streampid
457 uint16_t streampid
; // pid of this stream
458 uint8_t cadevice
; // CA device used for descramlbing
459 uint32_t caindex
; // index (slot) of the CA device used
460 uint64_t activeindexers
; // bitmask indexers if streampid enabled for index, bit is set
461 bool use_des
; // whether to use DES for descrambling this streampid
464 struct s_dvbapi_priority
466 char type
; // can be 'p', 'i', 'm', 'd', 's', 'l', 'j', 'a' or 'x'
479 #if defined(WITH_STAPI) || defined(WITH_STAPI5)
482 int8_t disablefilter
;
484 struct s_dvbapi_priority
*next
;
487 //-----------------------------------------------------------------------------
488 // function declarations
489 //-----------------------------------------------------------------------------
491 void dvbapi_stop_descrambling(int32_t demux_id
, uint32_t msgid
);
492 void dvbapi_stop_all_descrambling(uint32_t msgid
);
493 void dvbapi_process_input(int32_t demux_id
, int32_t filter_num
, uint8_t *buffer
, int32_t len
, uint32_t msgid
);
494 int32_t dvbapi_open_device(int32_t, int32_t, int);
495 int32_t dvbapi_stop_filternum(int32_t demux_id
, int32_t num
, uint32_t msgid
);
496 int32_t dvbapi_stop_filter(int32_t demux_id
, int32_t type
, uint32_t msgid
);
497 struct s_dvbapi_priority
*dvbapi_check_prio_match(int32_t demux_id
, int32_t pidindex
, char type
);
498 void dvbapi_send_dcw(struct s_client
*client
, ECM_REQUEST
*er
);
499 void dvbapi_write_cw(int32_t demux_id
, int32_t pid
, int32_t stream_id
, uint8_t *cw
, uint8_t cw_length
, uint8_t *iv
, uint8_t iv_length
, enum ca_descr_algo algo
, enum ca_descr_cipher_mode cipher_mode
, uint32_t msgid
);
500 int32_t dvbapi_parse_capmt(const uint8_t *buffer
, uint32_t length
, int32_t connfd
, char *pmtfile
, uint16_t client_proto_version
, uint32_t msgid
);
501 void request_cw(struct s_client
*client
, ECM_REQUEST
*er
, int32_t demux_id
, uint8_t delayed_ecm_check
);
502 void dvbapi_try_next_caid(int32_t demux_id
, int8_t checked
, uint32_t msgid
);
503 void dvbapi_read_priority(void);
504 int32_t dvbapi_set_section_filter(int32_t demux_id
, ECM_REQUEST
*er
, int32_t n
);
505 int32_t dvbapi_activate_section_filter(int32_t demux_id
, int32_t num
, int32_t fd
, int32_t pid
, uint8_t *filter
, uint8_t *mask
, uint32_t msgid
);
506 int32_t dvbapi_check_ecm_delayed_delivery(int32_t demux_id
, ECM_REQUEST
*er
);
507 int32_t dvbapi_get_filternum(int32_t demux_id
, ECM_REQUEST
*er
, int32_t type
);
508 uint32_t dvbapi_ca_set_pid(int32_t demux_id
, int32_t pid
, int32_t stream_id
, bool use_des
, uint32_t msgid
);
509 void dvbapi_set_pid(int32_t demux_id
, int32_t num
, uint32_t idx
, bool enable
, bool use_des
, uint32_t msgid
);
510 int8_t update_streampid_list(uint8_t cadevice
, uint16_t pid
, uint32_t idx
, bool use_des
);
511 int8_t remove_streampid_from_list(uint8_t cadevice
, uint16_t pid
, uint32_t idx
);
512 void disable_unused_streampids(int16_t demux_id
);
513 uint32_t is_ca_used(uint8_t cadevice
, int32_t pid
);
514 uint32_t count_active_indexers(void);
515 uint16_t dvbapi_get_client_proto_version(void);
516 const char *dvbapi_get_client_name(void);
517 void rotate_emmfilter(int32_t demux_id
);
518 int32_t filtermatch(uint8_t *buffer
, int32_t filter_num
, int32_t demux_id
, int32_t len
);
519 void delayer(ECM_REQUEST
*er
, uint32_t delay
);
520 void check_add_emmpid(int32_t demux_id
, uint8_t *filter
, int32_t l
, int32_t emmtype
);
521 void *dvbapi_start_handler(struct s_client
*cl
, uint8_t *mbuf
, int32_t module_idx
, void *(*_main_func
)(void *));
522 uint32_t dvbapi_get_desc_index(int32_t demux_id
, int32_t pid
, int32_t stream_id
);
523 void dvbapi_write_ecminfo_file(struct s_client
*client
, ECM_REQUEST
*er
, uint8_t *lastcw0
, uint8_t *lastcw1
, uint8_t cw_length
);
525 #if defined(WITH_AZBOX) || defined(WITH_MCA)
526 #define USE_OPENXCAS 1
527 extern int32_t openxcas_provid
;
528 extern uint16_t openxcas_sid
, openxcas_caid
, openxcas_ecm_pid
;
529 static inline void openxcas_set_caid(uint16_t _caid
) { openxcas_caid
= _caid
; }
530 static inline void openxcas_set_ecm_pid(uint16_t _pid
) { openxcas_ecm_pid
= _pid
; }
531 static inline void openxcas_set_sid(uint16_t _sid
) { openxcas_sid
= _sid
; }
532 static inline void openxcas_set_provid(uint32_t _provid
) { openxcas_provid
= _provid
; }
534 #define USE_OPENXCAS 0
535 static inline void openxcas_set_caid(uint16_t UNUSED(_caid
)) { }
536 static inline void openxcas_set_ecm_pid(uint16_t UNUSED(_pid
)) { }
537 static inline void openxcas_set_sid(uint16_t UNUSED(_sid
)) { }
538 static inline void openxcas_set_provid(uint32_t UNUSED(_provid
)) { }
541 bool is_dvbapi_usr(char *usr
);
542 static inline bool module_dvbapi_enabled(void) { return cfg
.dvbapi_enabled
; }
544 static inline void dvbapi_stop_all_descrambling(uint32_t UNUSED(msgid
)) { }
545 static inline void dvbapi_read_priority(void) { }
546 static inline bool is_dvbapi_usr(char *UNUSED(usr
)) { return 0; }
547 static inline bool module_dvbapi_enabled(void) { return 0; }
548 #endif // WITH_DVBAPI
550 #endif // MODULE_DVBAPI_H_