diff --git a/Makefile b/Makefile index ff598ad3..6d112641 100644 --- a/Makefile +++ b/Makefile @@ -337,6 +337,11 @@ SRCS-${CONFIG_CAPMT} += \ SRCS-${CONFIG_CONSTCW} += \ src/descrambler/constcw.c +# DVB CAM +SRCS-${CONFIG_LINUXDVB_CA} += \ + src/input/mpegts/linuxdvb/linuxdvb_ca.c \ + src/descrambler/dvbcam.c + # TSDEBUGCW SRCS-${CONFIG_TSDEBUG} += \ src/descrambler/tsdebugcw.c diff --git a/configure b/configure index 37a783cc..0b84fec0 100755 --- a/configure +++ b/configure @@ -43,6 +43,7 @@ OPTIONS=( "tvhcsa:auto" "bundle:no" "dvbcsa:no" + "libdvben50221:auto" "kqueue:no" "dbus_1:auto" "android:no" @@ -196,6 +197,16 @@ int test(void) } ' -liconv +check_cc_snippet libdvben50221 ' +#include +#define TEST test +int test(void) +{ + struct en50221_transport_layer *tl = en50221_tl_create(5, 32); + return 0; +} +' '-ldvben50221 -ldvbapi -lucsi' + # # Python # @@ -432,6 +443,14 @@ if enabled cwc || enabled capmt || enabled constcw; then fi fi +# +# libdvben50221 +# +if enabled_or_auto libdvben50221; then + LDFLAGS="$LDFLAGS -ldvben50221 -ldvbapi -lucsi" + enable linuxdvb_ca +fi + # # Icon caching # diff --git a/src/descrambler/descrambler.c b/src/descrambler/descrambler.c index 3c466197..f27fc53a 100644 --- a/src/descrambler/descrambler.c +++ b/src/descrambler/descrambler.c @@ -22,6 +22,7 @@ #include "ffdecsa/FFdecsa.h" #include "input.h" #include "input/mpegts/tsdemux.h" +#include "dvbcam.h" struct caid_tab { const char *name; @@ -84,6 +85,9 @@ descrambler_init ( void ) ffdecsa_init(); #endif caclient_init(); +#if ENABLE_LINUXDVB_CA + dvbcam_init(); +#endif } void @@ -123,6 +127,10 @@ descrambler_service_start ( service_t *t ) tvhcsa_init(&dr->dr_csa); } caclient_start(t); + +#if ENABLE_LINUXDVB_CA + dvbcam_service_start(t); +#endif } void @@ -131,6 +139,10 @@ descrambler_service_stop ( service_t *t ) th_descrambler_t *td; th_descrambler_runtime_t *dr = t->s_descramble; +#if ENABLE_LINUXDVB_CA + dvbcam_service_stop(t); +#endif + while ((td = LIST_FIRST(&t->s_descramblers)) != NULL) td->td_stop(td); t->s_descramble = NULL; diff --git a/src/descrambler/dvbcam.c b/src/descrambler/dvbcam.c new file mode 100644 index 00000000..6990ec29 --- /dev/null +++ b/src/descrambler/dvbcam.c @@ -0,0 +1,222 @@ +/* + * tvheadend, DVB CAM interface + * Copyright (C) 2014 Damjan Marion + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include "tvheadend.h" +#include "caclient.h" +#include "service.h" +#include "input.h" + +#include "dvbcam.h" +#include "input/mpegts/linuxdvb/linuxdvb_private.h" + +#if ENABLE_LINUXDVB_CA + +#define CAIDS_PER_CA_SLOT 16 + +typedef struct dvbcam_active_service { + TAILQ_ENTRY(dvbcam_active_service) link; + service_t *t; + uint8_t *last_pmt; + int last_pmt_len; + linuxdvb_ca_t *ca; + uint8_t slot; +} dvbcam_active_service_t; + +typedef struct dvbcam_active_caid { + TAILQ_ENTRY(dvbcam_active_caid) link; + uint16_t caids[CAIDS_PER_CA_SLOT]; + int num_caids; + linuxdvb_ca_t *ca; + uint8_t slot; +} dvbcam_active_caid_t; + +TAILQ_HEAD(,dvbcam_active_service) dvbcam_active_services; +TAILQ_HEAD(,dvbcam_active_caid) dvbcam_active_caids; + +pthread_mutex_t dvbcam_mutex; + +void +dvbcam_register_cam(linuxdvb_ca_t * lca, uint8_t slot, uint16_t * caids, + int num_caids) +{ + dvbcam_active_caid_t *ac; + + num_caids = MIN(CAIDS_PER_CA_SLOT, num_caids); + + if ((ac = malloc(sizeof(*ac))) == NULL) + return; + + ac->ca = lca; + ac->slot = slot; + memcpy(ac->caids, caids, num_caids * sizeof(uint16_t)); + ac->num_caids = num_caids; + + pthread_mutex_lock(&dvbcam_mutex); + + TAILQ_INSERT_TAIL(&dvbcam_active_caids, ac, link); + + pthread_mutex_unlock(&dvbcam_mutex); +} + +void +dvbcam_unregister_cam(linuxdvb_ca_t * lca, uint8_t slot) +{ + dvbcam_active_caid_t *ac, *ac_tmp; + dvbcam_active_service_t *as; + + pthread_mutex_lock(&dvbcam_mutex); + + /* remove pointer to this CAM in all active services */ + TAILQ_FOREACH(as, &dvbcam_active_services, link) + if (as->ca == lca && as->slot == slot) + as->ca = NULL; + + /* delete entry */ + for (ac = TAILQ_FIRST(&dvbcam_active_caids); ac != NULL; ac = ac_tmp) { + ac_tmp = TAILQ_NEXT(ac, link); + if(ac && ac->ca == lca && ac->slot == slot) { + TAILQ_REMOVE(&dvbcam_active_caids, ac, link); + free(ac); + } + } + + pthread_mutex_unlock(&dvbcam_mutex); +} + +void +dvbcam_pmt_data(mpegts_service_t *s, const uint8_t *ptr, int len) +{ + linuxdvb_frontend_t *lfe; + dvbcam_active_caid_t *ac; + dvbcam_active_service_t *as = NULL, *as2; + int i, l; + uint8_t *p; + + lfe = (linuxdvb_frontend_t*) s->s_dvb_active_input; + + if (!lfe) + return; + + pthread_mutex_lock(&dvbcam_mutex); + + TAILQ_FOREACH(as2, &dvbcam_active_services, link) + if (as2->t == (service_t *) s) { + as = as2; + break; + } + + pthread_mutex_unlock(&dvbcam_mutex); + + if (!as) + return; + + if(as->last_pmt) + free(as->last_pmt); + + as->last_pmt = malloc(len + 3); + memcpy(as->last_pmt, ptr-3, len + 3); + as->last_pmt_len = len + 3; + as->ca = NULL; + + l = (ptr[7] & 0x03 )| ptr[8]; + p = (uint8_t *) ptr + 9; + + while (l > 0 ) { + uint8_t desc_tag = p[0]; + uint8_t desc_len = p[1]; + + if (desc_tag == DVB_DESC_CA) { + uint16_t caid = (p[2] << 8) | p[3]; + + pthread_mutex_lock(&dvbcam_mutex); + + TAILQ_FOREACH(ac, &dvbcam_active_caids, link) { + for(i=0;inum_caids;i++) { + if(ac->ca && ac->ca->lca_adapter == lfe->lfe_adapter && + ac->caids[i] == caid) + { + as->ca = ac->ca; + as->slot = ac->slot; + break; + } + } + } + pthread_mutex_unlock(&dvbcam_mutex); + } + p += desc_len + 2; + l -= desc_len + 2; + } + + /* this service doesn't have assigned CAM */ + if (!as->ca) + return; + + linuxdvb_ca_send_capmt(as->ca, as->slot, as->last_pmt, as->last_pmt_len); +} + +void +dvbcam_service_start(service_t *t) +{ + dvbcam_active_service_t *as; + + TAILQ_FOREACH(as, &dvbcam_active_services, link) + if (as->t == t) + return; + + if ((as = malloc(sizeof(*as))) == NULL) + return; + + as->t = t; + as->last_pmt = NULL; + as->last_pmt_len = 0; + + pthread_mutex_lock(&dvbcam_mutex); + TAILQ_INSERT_TAIL(&dvbcam_active_services, as, link); + pthread_mutex_unlock(&dvbcam_mutex); +} + +void +dvbcam_service_stop(service_t *t) +{ + dvbcam_active_service_t *as, *as_tmp; + + pthread_mutex_lock(&dvbcam_mutex); + + for (as = TAILQ_FIRST(&dvbcam_active_services); as != NULL; as = as_tmp) { + as_tmp = TAILQ_NEXT(as, link); + if(as && as->t == t) { + TAILQ_REMOVE(&dvbcam_active_services, as, link); + if(as->last_pmt) + free(as->last_pmt); + free(as); + } + } + + pthread_mutex_unlock(&dvbcam_mutex); +} + +void +dvbcam_init(void) +{ + pthread_mutex_init(&dvbcam_mutex, NULL); + TAILQ_INIT(&dvbcam_active_services); + TAILQ_INIT(&dvbcam_active_caids); +} + +#endif /* ENABLE_LINUXDVB_CA */ diff --git a/src/descrambler/dvbcam.h b/src/descrambler/dvbcam.h new file mode 100644 index 00000000..f0078d95 --- /dev/null +++ b/src/descrambler/dvbcam.h @@ -0,0 +1,37 @@ +/* + * tvheadend - CSA wrapper + * Copyright (C) 2013 Adam Sutton + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef __DVBCAM_H__ +#define __DVBCAM_H__ + +struct mpegts_service; +struct elementary_stream; + +#if ENABLE_LINUXDVB_CA + +typedef struct linuxdvb_ca linuxdvb_ca_t; +void dvbcam_init(void); +void dvbcam_service_start(struct service *t); +void dvbcam_service_stop(struct service *t); +void dvbcam_register_cam(linuxdvb_ca_t * lca, uint8_t slot, uint16_t * caids, int num_caids); +void dvbcam_unregister_cam(linuxdvb_ca_t * lca, uint8_t slot); +void dvbcam_pmt_data(mpegts_service_t *s, const uint8_t *ptr, int len); + +#endif + +#endif /* __DVBCAM_H__ */ diff --git a/src/input/mpegts/dvb_psi.c b/src/input/mpegts/dvb_psi.c index 0e9ff0a6..7f487b83 100644 --- a/src/input/mpegts/dvb_psi.c +++ b/src/input/mpegts/dvb_psi.c @@ -26,6 +26,7 @@ #include "dvb_charset.h" #include "bouquet.h" #include "fastscan.h" +#include "descrambler/dvbcam.h" #include #include @@ -985,6 +986,10 @@ dvb_pmt_callback if (s->s_dvb_service_id == sid) break; if (!s) return -1; +#if ENABLE_LINUXDVB_CA + dvbcam_pmt_data(s, ptr, len); +#endif + /* Process */ tvhdebug("pmt", "sid %04X (%d)", sid, sid); pthread_mutex_lock(&s->s_stream_mutex); diff --git a/src/input/mpegts/linuxdvb/linuxdvb_adapter.c b/src/input/mpegts/linuxdvb/linuxdvb_adapter.c index f9b173c1..99602d20 100644 --- a/src/input/mpegts/linuxdvb/linuxdvb_adapter.c +++ b/src/input/mpegts/linuxdvb/linuxdvb_adapter.c @@ -34,8 +34,10 @@ #include #include +#include #define FE_PATH "%s/frontend%d" +#define CA_PATH "%s/ca%d" #define DVR_PATH "%s/dvr%d" #define DMX_PATH "%s/demux%d" @@ -225,6 +227,9 @@ linuxdvb_adapter_add ( const char *path ) extern int linuxdvb_adapter_mask; int a, i, j, r, fd; char fe_path[512], dmx_path[512], dvr_path[512]; +#if ENABLE_LINUXDVB_CA + char ca_path[512]; +#endif tvh_uuid_t uuid; linuxdvb_adapter_t *la = NULL; struct dvb_frontend_info dfi; @@ -357,6 +362,36 @@ linuxdvb_adapter_add ( const char *path ) pthread_mutex_unlock(&global_lock); } + /* Process each CA device */ +#if ENABLE_LINUXDVB_CA + for (i = 0; i < 32; i++) { + snprintf(ca_path, sizeof(ca_path), CA_PATH, path, i); + if (access(ca_path, R_OK | W_OK)) continue; + + /* Get ca info */ + for (j = 0; j < MAX_DEV_OPEN_ATTEMPTS; j++) { + if ((fd = tvh_open(ca_path, O_RDWR, 0)) >= 0) break; + usleep(100000); + } + if (fd < 0) { + tvhlog(LOG_ERR, "linuxdvb", "unable to open %s", ca_path); + continue; + } + r = ioctl(fd, CA_RESET, NULL); + close(fd); + if(r) { + tvhlog(LOG_ERR, "linuxdvb", "unable to query %s", ca_path); + continue; + } + if (!la) + continue; + + pthread_mutex_lock(&global_lock); + linuxdvb_ca_create(la, i, ca_path); + pthread_mutex_unlock(&global_lock); + } +#endif + /* Cleanup */ if (conf) htsmsg_destroy(conf); diff --git a/src/input/mpegts/linuxdvb/linuxdvb_ca.c b/src/input/mpegts/linuxdvb/linuxdvb_ca.c new file mode 100644 index 00000000..7d7004da --- /dev/null +++ b/src/input/mpegts/linuxdvb/linuxdvb_ca.c @@ -0,0 +1,429 @@ +/* + * Tvheadend - Linux DVB CA + * + * Copyright (C) 2015 Damjan Marion + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "tvheadend.h" +#include "linuxdvb_private.h" +#include "notify.h" +#include "atomic.h" +#include "tvhpoll.h" +#include "streaming.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "descrambler/dvbcam.h" + +typedef enum { + CA_SLOT_STATE_EMPTY = 0, + CA_SLOT_STATE_MODULE_PRESENT, + CA_SLOT_STATE_MODULE_READY +} ca_slot_state_t; + +const static char * +ca_slot_state2str(ca_slot_state_t v) +{ + switch(v) { + case CA_SLOT_STATE_EMPTY: return "EMPTY"; + case CA_SLOT_STATE_MODULE_PRESENT: return "MODULE_PRESENT"; + case CA_SLOT_STATE_MODULE_READY: return "MODULE_READY"; + }; + return "UNKNOWN"; +} + +static uint32_t +resource_ids[] = { EN50221_APP_RM_RESOURCEID, + EN50221_APP_MMI_RESOURCEID, + EN50221_APP_DVB_RESOURCEID, + EN50221_APP_CA_RESOURCEID, + EN50221_APP_DATETIME_RESOURCEID, + EN50221_APP_AI_RESOURCEID}; + +static int +linuxdvb_ca_lookup_cb (void * arg, uint8_t slot_id, uint32_t requested_rid, + en50221_sl_resource_callback *callback_out, + void **arg_out, uint32_t *connected_rid) +{ + linuxdvb_ca_t * lca = arg; + + switch (requested_rid) { + case EN50221_APP_RM_RESOURCEID: + *callback_out = (en50221_sl_resource_callback) en50221_app_rm_message; + *arg_out = lca->lca_rm_resource; + *connected_rid = EN50221_APP_RM_RESOURCEID; + break; + case EN50221_APP_AI_RESOURCEID: + *callback_out = (en50221_sl_resource_callback) en50221_app_ai_message; + *arg_out = lca->lca_ai_resource; + *connected_rid = EN50221_APP_AI_RESOURCEID; + break; + case EN50221_APP_CA_RESOURCEID: + *callback_out = (en50221_sl_resource_callback) en50221_app_ca_message; + *arg_out = lca->lca_ca_resource; + *connected_rid = EN50221_APP_CA_RESOURCEID; + break; + case EN50221_APP_DATETIME_RESOURCEID: + *callback_out = (en50221_sl_resource_callback) en50221_app_datetime_message; + *arg_out = lca->lca_dt_resource; + *connected_rid = EN50221_APP_DATETIME_RESOURCEID; + break; + default: + tvhtrace("en50221", "lookup cb for unknown resource id %u on slot %u", + requested_rid, slot_id); + return -1; + } + return 0; +} + +static int +linuxdvb_ca_session_cb (void *arg, int reason, uint8_t slot_id, + uint16_t session_num, uint32_t rid) +{ + linuxdvb_ca_t * lca = arg; + + switch(reason) { + case S_SCALLBACK_REASON_CAMCONNECTING: + break; + case S_SCALLBACK_REASON_CAMCONNECTED: + if (rid == EN50221_APP_RM_RESOURCEID) { + en50221_app_rm_enq(lca->lca_rm_resource, session_num); + } else if (rid == EN50221_APP_AI_RESOURCEID) { + en50221_app_ai_enquiry(lca->lca_ai_resource, session_num); + } else if (rid == EN50221_APP_CA_RESOURCEID) { + en50221_app_ca_info_enq(lca->lca_ca_resource, session_num); + lca->lca_ca_session_number = session_num; + } + break; + case S_SCALLBACK_REASON_CLOSE: + if (rid == EN50221_APP_CA_RESOURCEID) + dvbcam_unregister_cam(lca, 0); + break; + case S_SCALLBACK_REASON_TC_CONNECT: + break; + default: + tvhtrace("en50221", "unhandled sessopn callback - " + "reason %d slot_id %u session_num %u resource_id %x", + reason, slot_id, session_num, rid); + } + return 0; +} + +static int +linuxdvb_ca_rm_enq_cb(void *arg, uint8_t slot_id, uint16_t session_num) +{ + linuxdvb_ca_t * lca = arg; + + tvhtrace("en50221", "rm enq callback received for slot %d", slot_id); + + if (en50221_app_rm_reply(lca->lca_rm_resource, session_num, + sizeof(resource_ids)/4, resource_ids)) { + tvherror("en50221", "failed to send rm reply to slot %u session %u", + slot_id, session_num); + } + + return 0; +} + +static int +linuxdvb_ca_rm_reply_cb(void *arg, uint8_t slot_id, uint16_t session_num, + uint32_t resource_id_count, uint32_t *_resource_ids) +{ + linuxdvb_ca_t * lca = arg; + + tvhtrace("en50221", "rm reply cb received for slot %d", slot_id); + + uint32_t i; + for(i=0; i< resource_id_count; i++) { + tvhtrace("en50221", "CAM provided resource id: %08x", _resource_ids[i]); + } + + if (en50221_app_rm_changed(lca->lca_rm_resource, session_num)) { + tvherror("en50221", "failed to send RM reply on slot %u", slot_id); + } + + return 0; +} + +static int +linuxdvb_ca_rm_changed_cb(void *arg, uint8_t slot_id, + uint16_t session_num) +{ + linuxdvb_ca_t * lca = arg; + tvhtrace("en50221", "rm changed cb received for slot %d", slot_id); + + if (en50221_app_rm_enq(lca->lca_rm_resource, session_num)) { + tvherror("en50221", "failed to send ENQ to slot %d", slot_id); + } + + return 0; +} + +static int +linuxdvb_ca_dt_enquiry_cb(void *arg, uint8_t slot_id, uint16_t session_num, + uint8_t response_int) +{ + linuxdvb_ca_t * lca = arg; + tvhtrace("en50221", "datetime enquiry cb received for slot %d", slot_id); + + if (en50221_app_datetime_send(lca->lca_dt_resource, session_num, time(NULL), -1)) { + tvherror("en50221", "Failed to send datetime to slot %d", slot_id); + } + + return 0; +} + +static int +linuxdvb_ca_ai_callback(void *arg, uint8_t slot_id, uint16_t session_num, + uint8_t app_type, uint16_t app_manufacturer, + uint16_t manufacturer_code, uint8_t menu_string_len, + uint8_t *menu_string) +{ + tvhinfo("en50221", "slot %u: Application type: %02x", slot_id, app_type); + tvhinfo("en50221", "slot %u: Application manufacturer: %04x",slot_id, app_manufacturer); + tvhinfo("en50221", "slot %u: Manufacturer code: %04x", slot_id, manufacturer_code); + tvhinfo("en50221", "slot %u: Menu string: %.*s", slot_id, menu_string_len, menu_string); + return 0; +} + +static int +linuxdvb_ca_ca_info_callback(void *arg, uint8_t slot_id, uint16_t session_num, uint32_t ca_id_count, uint16_t *ca_ids) +{ + linuxdvb_ca_t * lca = arg; + uint32_t i; + + + dvbcam_unregister_cam(lca, 0); + dvbcam_register_cam(lca, 0, ca_ids, ca_id_count); + + + for(i=0; i< ca_id_count; i++) { + tvhinfo("en50221", "slot %d supported CAID: %04x (%s)", slot_id, + ca_ids[i], descrambler_caid2name(ca_ids[i])); + } + + //ca_connected = 1; + return 0; +} + +static int +linuxdvb_ca_ca_pmt_reply_cb(void *arg, uint8_t slot_id, uint16_t session_num, + struct en50221_app_pmt_reply *reply, + uint32_t reply_size) +{ + tvhtrace("en50221", "ca pmt reply callback received for slot %d", slot_id); + + return 0; +} + +static void * +linuxdvb_ca_en50221_thread ( void *aux ) +{ + linuxdvb_ca_t *lca = aux; + int slot_id, lasterror = 0; + + lca->lca_tl = en50221_tl_create(5, 32); + if (!lca->lca_tl) { + tvherror("en50221", "failed to create transport layer"); + return NULL; + } + + slot_id = en50221_tl_register_slot(lca->lca_tl, lca->lca_ca_fd, 0, 1000, 100); + if (slot_id < 0) { + tvherror("en50221", "slot registration failed"); + return NULL; + } + + lca->lca_sl = en50221_sl_create(lca->lca_tl, 256); + if (lca->lca_sl == NULL) { + tvherror("en50221", "failed to create session layer"); + return NULL; + } + + // create the sendfuncs + lca->lca_sf.arg = lca->lca_sl; + lca->lca_sf.send_data = (en50221_send_data) en50221_sl_send_data; + lca->lca_sf.send_datav = (en50221_send_datav) en50221_sl_send_datav; + + /* create app resources and assign callbacks */ + lca->lca_rm_resource = en50221_app_rm_create(&lca->lca_sf); + en50221_app_rm_register_enq_callback(lca->lca_rm_resource, linuxdvb_ca_rm_enq_cb, lca); + en50221_app_rm_register_reply_callback(lca->lca_rm_resource, linuxdvb_ca_rm_reply_cb, lca); + en50221_app_rm_register_changed_callback(lca->lca_rm_resource, linuxdvb_ca_rm_changed_cb, lca); + + lca->lca_dt_resource = en50221_app_datetime_create(&lca->lca_sf); + en50221_app_datetime_register_enquiry_callback(lca->lca_dt_resource, linuxdvb_ca_dt_enquiry_cb, lca); + + lca->lca_ai_resource = en50221_app_ai_create(&lca->lca_sf); + en50221_app_ai_register_callback(lca->lca_ai_resource, linuxdvb_ca_ai_callback, lca); + + lca->lca_ca_resource = en50221_app_ca_create(&lca->lca_sf); + en50221_app_ca_register_info_callback(lca->lca_ca_resource, linuxdvb_ca_ca_info_callback, lca); + en50221_app_ca_register_pmt_reply_callback(lca->lca_ca_resource, linuxdvb_ca_ca_pmt_reply_cb, lca); + + en50221_sl_register_lookup_callback(lca->lca_sl, linuxdvb_ca_lookup_cb, lca); + en50221_sl_register_session_callback(lca->lca_sl, linuxdvb_ca_session_cb, lca); + + lca->lca_tc = en50221_tl_new_tc(lca->lca_tl, slot_id); + + while (tvheadend_running & lca->lca_en50221_thread_running) { + int error; + if ((error = en50221_tl_poll(lca->lca_tl)) != 0) { + if (error != lasterror) { + tvherror("en50221", "poll error on slot %d [error:%i]", + en50221_tl_get_error_slot(lca->lca_tl), + en50221_tl_get_error(lca->lca_tl)); + } + lasterror = error; + } + } + + dvbcam_unregister_cam(lca, 0); + + en50221_tl_destroy_slot(lca->lca_tl, slot_id); + en50221_sl_destroy(lca->lca_sl); + en50221_tl_destroy(lca->lca_tl); + return 0; +} + +static void +linuxdvb_ca_monitor ( void *aux ) +{ + linuxdvb_ca_t *lca = aux; + ca_slot_info_t csi; + int state; + + csi.num = 0; + + if (lca->lca_ca_fd > 0) { + + if ((ioctl(lca->lca_ca_fd, CA_GET_SLOT_INFO, &csi)) != 0) { + tvherror("linuxdvb", "failed to get ca%u slot info [e=%s]", + lca->lca_number, strerror(errno)); + } + if (csi.flags & CA_CI_MODULE_READY) + state = CA_SLOT_STATE_MODULE_READY; + else if (csi.flags & CA_CI_MODULE_PRESENT) + state = CA_SLOT_STATE_MODULE_PRESENT; + else + state = CA_SLOT_STATE_EMPTY; + + if (lca->lca_state != state) { + tvhlog(LOG_INFO, "linuxdvb", "ca%u slot %u status changed to %s", + lca->lca_number, csi.num, ca_slot_state2str(state)); + + if (state == CA_SLOT_STATE_MODULE_READY) { + lca->lca_en50221_thread_running = 1; + tvhthread_create(&lca->lca_en50221_thread, NULL, + linuxdvb_ca_en50221_thread, lca); + + } else { + lca->lca_en50221_thread_running = 0; + pthread_join(lca->lca_en50221_thread, NULL); + } + + lca->lca_state = state; + } + } + + gtimer_arm_ms(&lca->lca_monitor_timer, linuxdvb_ca_monitor, lca, 250); +} + +linuxdvb_ca_t * +linuxdvb_ca_create + ( linuxdvb_adapter_t *la, int number, const char *ca_path) +{ + linuxdvb_ca_t *lca; + char id[6]; + + /* Internal config ID */ + snprintf(id, sizeof(id), "CA #%d", number); + + lca = calloc(1, sizeof(linuxdvb_frontend_t)); + lca->lca_number = number; + lca->lca_ca_path = strdup(ca_path); + + /* Adapter link */ + lca->lca_adapter = la; + LIST_INSERT_HEAD(&la->la_cas, lca, lca_link); + + lca->lca_ca_fd = tvh_open(lca->lca_ca_path, O_RDWR | O_NONBLOCK, 0); + tvhtrace("linuxdvb", "opening CA%u %s (fd %d)", + lca->lca_number, lca->lca_ca_path, lca->lca_ca_fd); + + gtimer_arm_ms(&lca->lca_monitor_timer, linuxdvb_ca_monitor, lca, 250); + + return lca; +} + +void +linuxdvb_ca_send_capmt(linuxdvb_ca_t *lca, uint8_t slot, const uint8_t *ptr, int len) +{ + struct section *section; + struct section_ext *result; + struct mpeg_pmt_section *pmt; + uint8_t *buffer; + uint8_t capmt[4096]; + int size; + + buffer = malloc(len); + if (!buffer) + return; + + memcpy(buffer, ptr, len); + + section = section_codec(buffer, len); + if (!section){ + tvherror("en50221", "failed to decode PMT section"); + goto fail; + } + + result = section_ext_decode(section, 0); + if (!result){ + tvherror("en50221", "failed to decode PMT ext_section"); + goto fail; + } + + pmt = mpeg_pmt_section_codec(result); + if (!pmt){ + tvherror("en50221", "failed to decode PMT"); + goto fail; + } + + size = en50221_ca_format_pmt(pmt, capmt, sizeof(capmt), + CA_LIST_MANAGEMENT_ONLY, slot, + CA_PMT_CMD_ID_OK_DESCRAMBLING); + if (size < 0) { + tvherror("en50221", "Failed to format CAPMT"); + } + if (en50221_app_ca_pmt(lca->lca_ca_resource, lca->lca_ca_session_number, + capmt, size)) { + tvherror("en50221", "Failed to send CAPMT"); + } + + tvhtrace("en50221", "CAPMT sent"); + +fail: + free(buffer); +} diff --git a/src/input/mpegts/linuxdvb/linuxdvb_private.h b/src/input/mpegts/linuxdvb/linuxdvb_private.h index 116e79a5..b8039e4b 100644 --- a/src/input/mpegts/linuxdvb/linuxdvb_private.h +++ b/src/input/mpegts/linuxdvb/linuxdvb_private.h @@ -24,6 +24,23 @@ #if ENABLE_LINUXDVB #include + +#if ENABLE_LIBDVBEN50221 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#endif #endif #define DVB_VER_INT(maj,min) (((maj) << 16) + (min)) @@ -34,6 +51,9 @@ typedef struct linuxdvb_hardware linuxdvb_hardware_t; typedef struct linuxdvb_adapter linuxdvb_adapter_t; typedef struct linuxdvb_frontend linuxdvb_frontend_t; +#if ENABLE_LINUXDVB_CA +typedef struct linuxdvb_ca linuxdvb_ca_t; +#endif typedef struct linuxdvb_satconf linuxdvb_satconf_t; typedef struct linuxdvb_satconf_ele linuxdvb_satconf_ele_t; typedef struct linuxdvb_diseqc linuxdvb_diseqc_t; @@ -60,6 +80,12 @@ struct linuxdvb_adapter */ LIST_HEAD(,linuxdvb_frontend) la_frontends; + /* + * CA devices + */ +#if ENABLE_LINUXDVB_CA + LIST_HEAD(,linuxdvb_ca) la_cas; +#endif /* * Functions */ @@ -126,6 +152,47 @@ struct linuxdvb_frontend linuxdvb_satconf_t *lfe_satconf; }; +#if ENABLE_LINUXDVB_CA +struct linuxdvb_ca +{ + /* + * Adapter + */ + linuxdvb_adapter_t *lca_adapter; + LIST_ENTRY(linuxdvb_ca) lca_link; + + /* + * CA handling + */ + int lca_ca_fd; + gtimer_t lca_monitor_timer; + pthread_t lca_en50221_thread; + int lca_en50221_thread_running; + + /* + * EN50221 + */ + struct en50221_transport_layer *lca_tl; + struct en50221_session_layer *lca_sl; + struct en50221_app_send_functions lca_sf; + struct en50221_app_rm *lca_rm_resource; + struct en50221_app_ai *lca_ai_resource; + struct en50221_app_ca *lca_ca_resource; + struct en50221_app_datetime *lca_dt_resource; + int lca_tc; + uint16_t lca_ai_session_number; + uint16_t lca_ca_session_number; + + /* + * CA info + */ + int lca_number; + char lca_name[128]; + char *lca_ca_path; + int lca_state; +}; +#endif + struct linuxdvb_satconf { idnode_t ls_id; @@ -283,6 +350,17 @@ int linuxdvb_frontend_tune1 int linuxdvb2tvh_delsys ( int delsys ); +#if ENABLE_LINUXDVB_CA + +linuxdvb_ca_t * +linuxdvb_ca_create + ( linuxdvb_adapter_t *la, int number, const char *ca_path); + +void +linuxdvb_ca_send_capmt(linuxdvb_ca_t *lca, uint8_t slot, const uint8_t *ptr, int len); + +#endif + /* * Diseqc gear */