Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
descrambler/htsp server: add descramble info message, fixes #2616
The sources are capmt (oscam's DVBAPI) ECM INFO and cwc (newcamd) client.
The latest DVBAPI network protocol should be used to get this info.
  • Loading branch information
perexg committed Oct 6, 2015
1 parent f7eb779 commit 1aa7ddd
Show file tree
Hide file tree
Showing 15 changed files with 156 additions and 5 deletions.
5 changes: 5 additions & 0 deletions src/descrambler.h
Expand Up @@ -150,6 +150,11 @@ void descrambler_caid_changed ( struct service *t );
int descrambler_resolved ( struct service *t, th_descrambler_t *ignore );
void descrambler_keys ( th_descrambler_t *t, int type,
const uint8_t *even, const uint8_t *odd );
void descrambler_notify ( th_descrambler_t *t,
uint16_t caid, uint32_t provid,
const char *cardsystem, uint16_t pid, uint32_t ecmtime,
uint16_t hops, const char *reader, const char *from,
const char *protocol );
int descrambler_descramble ( struct service *t,
struct elementary_stream *st,
const uint8_t *tsb, int len );
Expand Down
30 changes: 30 additions & 0 deletions src/descrambler/capmt.c
Expand Up @@ -1063,6 +1063,32 @@ capmt_process_key(capmt_t *capmt, uint8_t adapter, uint16_t seq,
pthread_mutex_unlock(&capmt->capmt_mutex);
}

static void
capmt_process_notify(capmt_t *capmt, uint8_t adapter,
uint16_t sid, uint16_t caid, uint32_t provid,
const char *cardsystem, uint16_t pid, uint32_t ecmtime,
uint16_t hops, const char *reader, const char *from,
const char *protocol )
{
mpegts_service_t *t;
capmt_service_t *ct;

pthread_mutex_lock(&capmt->capmt_mutex);
LIST_FOREACH(ct, &capmt->capmt_services, ct_link) {
t = (mpegts_service_t *)ct->td_service;

if (sid != t->s_dvb_service_id)
continue;
if (adapter != ct->ct_adapter)
continue;

descrambler_notify((th_descrambler_t *)ct, caid, provid,
cardsystem, pid, ecmtime, hops, reader, from,
protocol);
}
pthread_mutex_unlock(&capmt->capmt_mutex);
}

static int
capmt_msg_size(capmt_t *capmt, sbuf_t *sb, int offset)
{
Expand Down Expand Up @@ -1245,6 +1271,10 @@ capmt_analyze_cmd(capmt_t *capmt, int adapter, sbuf_t *sb, int offset)
char *protocol = capmt_peek_str(sb, &offset2);
uint8_t hops = sbuf_peek_u8(sb, offset2);

capmt_process_notify(capmt, adapter, sid, caid, provid,
cardsystem, pid, ecmtime, hops, reader,
from, protocol);

tvhlog(LOG_DEBUG, "capmt", "%s: ECM_INFO: adapter=%d sid=%d caid=%04X(%s) pid=%04X provid=%06X ecmtime=%d hops=%d reader=%s from=%s protocol=%s",
capmt_name(capmt), adapter, sid, caid, cardsystem, pid, provid, ecmtime, hops, reader, from, protocol);

Expand Down
19 changes: 15 additions & 4 deletions src/descrambler/cwc.c
Expand Up @@ -102,6 +102,8 @@ typedef struct ecm_section {

int es_section;
int es_channel;
uint16_t es_caid;
uint16_t es_provid;

uint16_t es_seq;
char es_nok;
Expand Down Expand Up @@ -654,7 +656,7 @@ handle_ecm_reply(cwc_service_t *ct, ecm_section_t *es, uint8_t *msg,
cwc_t *cwc = ct->cs_cwc;
ecm_pid_t *ep;
ecm_section_t *es2;
char chaninfo[32];
char chaninfo[128];
int i;
int64_t delay = (getmonoclock() - es->es_time) / 1000LL; // in ms

Expand Down Expand Up @@ -794,6 +796,13 @@ handle_ecm_reply(cwc_service_t *ct, ecm_section_t *es, uint8_t *msg,
descrambler_keys((th_descrambler_t *)ct, DESCRAMBLER_AES, msg + 3, msg + 3 + 16);
pthread_mutex_lock(&cwc->cwc_mutex);
}

snprintf(chaninfo, sizeof(chaninfo), "%s:%i", cwc->cwc_hostname, cwc->cwc_port);
descrambler_notify((th_descrambler_t *)ct,
es->es_caid, es->es_provid,
caid2name(es->es_caid),
es->es_channel, delay,
1, "", chaninfo, "newcamd");
}
}

Expand Down Expand Up @@ -1265,7 +1274,7 @@ cwc_table_input(void *opaque, int pid, const uint8_t *data, int len, int emm)
struct cs_card_data *pcard = NULL;
caid_t *c;
uint16_t caid;
uint32_t providerid;
uint32_t provid;

if (data == NULL)
return;
Expand Down Expand Up @@ -1337,7 +1346,7 @@ cwc_table_input(void *opaque, int pid, const uint8_t *data, int len, int emm)

found:
caid = c->caid;
providerid = c->providerid;
provid = c->providerid;

switch(data[0]) {
case 0x80:
Expand Down Expand Up @@ -1374,6 +1383,8 @@ cwc_table_input(void *opaque, int pid, const uint8_t *data, int len, int emm)
if (es->es_keystate == ES_FORBIDDEN || es->es_keystate == ES_IDLE)
break;

es->es_caid = caid;
es->es_provid = provid;
es->es_channel = channel;
es->es_pending = 1;
es->es_resolved = 0;
Expand All @@ -1384,7 +1395,7 @@ cwc_table_input(void *opaque, int pid, const uint8_t *data, int len, int emm)
goto end;
}

es->es_seq = cwc_send_msg(cwc, data, len, sid, 1, caid, providerid);
es->es_seq = cwc_send_msg(cwc, data, len, sid, 1, caid, provid);

tvhlog(LOG_DEBUG, "cwc",
"Sending ECM%s section=%d/%d, for service \"%s\" (seqno: %d)",
Expand Down
36 changes: 36 additions & 0 deletions src/descrambler/descrambler.c
Expand Up @@ -28,6 +28,7 @@
#include "input.h"
#include "input/mpegts/tsdemux.h"
#include "dvbcam.h"
#include "streaming.h"

#define MAX_QUICK_ECM_ENTRIES 100

Expand Down Expand Up @@ -168,6 +169,41 @@ descrambler_caid_changed ( service_t *t )
}
}

void
descrambler_notify( th_descrambler_t *td,
uint16_t caid, uint32_t provid,
const char *cardsystem, uint16_t pid, uint32_t ecmtime,
uint16_t hops, const char *reader, const char *from,
const char *protocol )
{
mpegts_service_t *t = (mpegts_service_t *)td->td_service;
streaming_message_t *sm;
descramble_info_t *di;

tvhlog(LOG_DEBUG, "descrambler", "info - service='%s' caid=%04X(%s) "
"provid=%06X ecmtime=%d hops=%d "
"reader=%s from=%s protocol=%s",
t->s_dvb_svcname, caid, cardsystem, provid,
ecmtime, hops, reader, from, protocol);

sm = streaming_msg_create(SMT_DESCRAMBLE_INFO);
sm->sm_data = di = calloc(1, sizeof(*di));

di->pid = pid;
di->caid = caid;
di->provid = provid;
di->ecmtime = ecmtime;
di->hops = hops;
strncpy(di->cardsystem, cardsystem, sizeof(di->cardsystem)-1);
strncpy(di->reader, reader, sizeof(di->reader)-1);
strncpy(di->from, from, sizeof(di->protocol)-1);
strncpy(di->protocol, protocol, sizeof(di->protocol)-1);

pthread_mutex_lock(&t->s_stream_mutex);
streaming_pad_deliver(&t->s_streaming_pad, sm);
pthread_mutex_unlock(&t->s_stream_mutex);
}

int
descrambler_resolved( service_t *t, th_descrambler_t *ignore )
{
Expand Down
1 change: 1 addition & 0 deletions src/dvr/dvr_rec.c
Expand Up @@ -1190,6 +1190,7 @@ dvr_thread(void *aux)
case SMT_SKIP:
case SMT_SIGNAL_STATUS:
case SMT_TIMESHIFT_STATUS:
case SMT_DESCRAMBLE_INFO:
break;

case SMT_EXIT:
Expand Down
34 changes: 33 additions & 1 deletion src/htsp_server.c
Expand Up @@ -65,7 +65,7 @@

static void *htsp_server, *htsp_server_2;

#define HTSP_PROTO_VERSION 23
#define HTSP_PROTO_VERSION 24

#define HTSP_ASYNC_OFF 0x00
#define HTSP_ASYNC_ON 0x01
Expand Down Expand Up @@ -3720,6 +3720,34 @@ htsp_subscription_signal_status(htsp_subscription_t *hs, signal_status_t *sig)
htsp_send_message(hs->hs_htsp, m, &hs->hs_htsp->htsp_hmq_qstatus);
}

/**
*
*/
static void
htsp_subscription_descramble_info(htsp_subscription_t *hs, descramble_info_t *di)
{
/* don't bother old clients */
if (hs->hs_htsp->htsp_version < 24)
return;

htsmsg_t *m = htsmsg_create_map();
htsmsg_add_str(m, "method", "descrambleInfo");
htsmsg_add_u32(m, "pid", di->pid);
htsmsg_add_u32(m, "caid", di->caid);
htsmsg_add_u32(m, "provid", di->provid);
htsmsg_add_u32(m, "ecmtime", di->ecmtime);
htsmsg_add_u32(m, "hops", di->hops);
if (di->cardsystem[0])
htsmsg_add_str(m, "cardsystem", di->cardsystem);
if (di->reader[0])
htsmsg_add_str(m, "reader", di->reader);
if (di->from[0])
htsmsg_add_str(m, "from", di->from);
if (di->protocol[0])
htsmsg_add_str(m, "protocol", di->protocol);
htsp_send_message(hs->hs_htsp, m, &hs->hs_htsp->htsp_hmq_qstatus);
}

/**
*
*/
Expand Down Expand Up @@ -3821,6 +3849,10 @@ htsp_streaming_input(void *opaque, streaming_message_t *sm)
htsp_subscription_signal_status(hs, sm->sm_data);
break;

case SMT_DESCRAMBLE_INFO:
htsp_subscription_descramble_info(hs, sm->sm_data);
break;

case SMT_NOSTART:
case SMT_NOSTART_WARN:
htsp_subscription_status(hs, streaming_code2txt(sm->sm_code),
Expand Down
2 changes: 2 additions & 0 deletions src/plumbing/globalheaders.c
Expand Up @@ -331,6 +331,7 @@ gh_hold(globalheaders_t *gh, streaming_message_t *sm)
case SMT_EXIT:
case SMT_SERVICE_STATUS:
case SMT_SIGNAL_STATUS:
case SMT_DESCRAMBLE_INFO:
case SMT_NOSTART:
case SMT_NOSTART_WARN:
case SMT_MPEGTS:
Expand Down Expand Up @@ -367,6 +368,7 @@ gh_pass(globalheaders_t *gh, streaming_message_t *sm)
case SMT_EXIT:
case SMT_SERVICE_STATUS:
case SMT_SIGNAL_STATUS:
case SMT_DESCRAMBLE_INFO:
case SMT_NOSTART:
case SMT_NOSTART_WARN:
case SMT_MPEGTS:
Expand Down
1 change: 1 addition & 0 deletions src/plumbing/transcoding.c
Expand Up @@ -1879,6 +1879,7 @@ transcoder_input(void *opaque, streaming_message_t *sm)
case SMT_EXIT:
case SMT_SERVICE_STATUS:
case SMT_SIGNAL_STATUS:
case SMT_DESCRAMBLE_INFO:
case SMT_NOSTART:
case SMT_NOSTART_WARN:
case SMT_MPEGTS:
Expand Down
1 change: 1 addition & 0 deletions src/plumbing/tsfix.c
Expand Up @@ -548,6 +548,7 @@ tsfix_input(void *opaque, streaming_message_t *sm)
case SMT_EXIT:
case SMT_SERVICE_STATUS:
case SMT_SIGNAL_STATUS:
case SMT_DESCRAMBLE_INFO:
case SMT_NOSTART:
case SMT_NOSTART_WARN:
case SMT_MPEGTS:
Expand Down
1 change: 1 addition & 0 deletions src/satip/rtp.c
Expand Up @@ -316,6 +316,7 @@ satip_rtp_thread(void *aux)
case SMT_SPEED:
case SMT_SERVICE_STATUS:
case SMT_TIMESHIFT_STATUS:
case SMT_DESCRAMBLE_INFO:
break;
}

Expand Down
6 changes: 6 additions & 0 deletions src/streaming.c
Expand Up @@ -260,6 +260,11 @@ streaming_msg_clone(streaming_message_t *src)
memcpy(dst->sm_data, src->sm_data, sizeof(signal_status_t));
break;

case SMT_DESCRAMBLE_INFO:
dst->sm_data = malloc(sizeof(descramble_info_t));
memcpy(dst->sm_data, src->sm_data, sizeof(descramble_info_t));
break;

case SMT_TIMESHIFT_STATUS:
dst->sm_data = malloc(sizeof(timeshift_status_t));
memcpy(dst->sm_data, src->sm_data, sizeof(timeshift_status_t));
Expand Down Expand Up @@ -338,6 +343,7 @@ streaming_msg_free(streaming_message_t *sm)

case SMT_SKIP:
case SMT_SIGNAL_STATUS:
case SMT_DESCRAMBLE_INFO:
#if ENABLE_TIMESHIFT
case SMT_TIMESHIFT_STATUS:
#endif
Expand Down
1 change: 1 addition & 0 deletions src/timeshift/timeshift_reader.c
Expand Up @@ -137,6 +137,7 @@ static ssize_t _read_msg ( timeshift_file_t *tsf, int fd, streaming_message_t **
case SMT_NOSTART:
case SMT_NOSTART_WARN:
case SMT_SERVICE_STATUS:
case SMT_DESCRAMBLE_INFO:
return -1;
break;

Expand Down
1 change: 1 addition & 0 deletions src/timeshift/timeshift_writer.c
Expand Up @@ -311,6 +311,7 @@ static void _process_msg
case SMT_NOSTART_WARN:
case SMT_SERVICE_STATUS:
case SMT_TIMESHIFT_STATUS:
case SMT_DESCRAMBLE_INFO:
break;

/* Store */
Expand Down
22 changes: 22 additions & 0 deletions src/tvheadend.h
Expand Up @@ -270,6 +270,21 @@ typedef struct signal_status {
int tc_block; /* total block count */
} signal_status_t;

/**
* Descramble info
*/
typedef struct descramble_info {
uint16_t pid;
uint16_t caid;
uint32_t provid;
uint32_t ecmtime;
uint16_t hops;
char cardsystem[128];
char reader [128];
char from [128];
char protocol [128];
} descramble_info_t;

/**
* Streaming skip
*/
Expand Down Expand Up @@ -355,6 +370,13 @@ typedef enum {
*/
SMT_SIGNAL_STATUS,

/**
* Descrambler info message
*
* Notification about descrambler
*/
SMT_DESCRAMBLE_INFO,

/**
* Streaming stop.
*
Expand Down
1 change: 1 addition & 0 deletions src/webui/webui.c
Expand Up @@ -432,6 +432,7 @@ http_stream_run(http_connection_t *hc, profile_chain_t *prch,
case SMT_SKIP:
case SMT_SPEED:
case SMT_SIGNAL_STATUS:
case SMT_DESCRAMBLE_INFO:
case SMT_TIMESHIFT_STATUS:
break;

Expand Down

0 comments on commit 1aa7ddd

Please sign in to comment.