2003-01-06 14:44:27 +01:00
|
|
|
/*
|
|
|
|
* ci.c: Common Interface
|
|
|
|
*
|
|
|
|
* See the main source file 'vdr.c' for copyright information and
|
|
|
|
* how to reach the author.
|
|
|
|
*
|
2020-08-17 10:26:18 +02:00
|
|
|
* $Id: ci.c 4.32 2020/08/17 10:26:18 kls Exp $
|
2003-01-06 14:44:27 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ci.h"
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <linux/dvb/ca.h>
|
|
|
|
#include <malloc.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <poll.h>
|
2017-01-09 13:42:41 +01:00
|
|
|
#include <stdio.h>
|
2003-01-06 14:44:27 +01:00
|
|
|
#include <string.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <unistd.h>
|
2007-01-07 14:46:14 +01:00
|
|
|
#include "device.h"
|
2017-03-18 16:33:59 +01:00
|
|
|
#include "mtd.h"
|
2003-12-24 10:30:35 +01:00
|
|
|
#include "pat.h"
|
2014-01-01 12:37:22 +01:00
|
|
|
#include "receiver.h"
|
2014-04-06 10:56:50 +02:00
|
|
|
#include "remux.h"
|
|
|
|
#include "libsi/si.h"
|
2015-01-30 13:38:44 +01:00
|
|
|
#include "skins.h"
|
2003-01-06 14:44:27 +01:00
|
|
|
#include "tools.h"
|
|
|
|
|
|
|
|
// Set these to 'true' for debug output:
|
|
|
|
static bool DumpTPDUDataTransfer = false;
|
|
|
|
static bool DebugProtocol = false;
|
2007-01-07 14:46:14 +01:00
|
|
|
static bool DumpPolls = false;
|
|
|
|
static bool DumpDateTime = false;
|
2003-01-06 14:44:27 +01:00
|
|
|
|
2003-02-02 15:49:52 +01:00
|
|
|
#define dbgprotocol(a...) if (DebugProtocol) fprintf(stderr, a)
|
2003-01-06 14:44:27 +01:00
|
|
|
|
|
|
|
// --- Helper functions ------------------------------------------------------
|
|
|
|
|
|
|
|
#define SIZE_INDICATOR 0x80
|
|
|
|
|
|
|
|
static const uint8_t *GetLength(const uint8_t *Data, int &Length)
|
|
|
|
///< Gets the length field from the beginning of Data.
|
2013-02-16 15:26:45 +01:00
|
|
|
///< Returns a pointer to the first byte after the length and
|
2003-01-06 14:44:27 +01:00
|
|
|
///< stores the length value in Length.
|
|
|
|
{
|
|
|
|
Length = *Data++;
|
|
|
|
if ((Length & SIZE_INDICATOR) != 0) {
|
|
|
|
int l = Length & ~SIZE_INDICATOR;
|
|
|
|
Length = 0;
|
|
|
|
for (int i = 0; i < l; i++)
|
|
|
|
Length = (Length << 8) | *Data++;
|
|
|
|
}
|
|
|
|
return Data;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint8_t *SetLength(uint8_t *Data, int Length)
|
|
|
|
///< Sets the length field at the beginning of Data.
|
2013-02-16 15:26:45 +01:00
|
|
|
///< Returns a pointer to the first byte after the length.
|
2003-01-06 14:44:27 +01:00
|
|
|
{
|
|
|
|
uint8_t *p = Data;
|
|
|
|
if (Length < 128)
|
|
|
|
*p++ = Length;
|
|
|
|
else {
|
|
|
|
int n = sizeof(Length);
|
|
|
|
for (int i = n - 1; i >= 0; i--) {
|
|
|
|
int b = (Length >> (8 * i)) & 0xFF;
|
|
|
|
if (p != Data || b)
|
|
|
|
*++p = b;
|
|
|
|
}
|
|
|
|
*Data = (p - Data) | SIZE_INDICATOR;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *CopyString(int Length, const uint8_t *Data)
|
|
|
|
///< Copies the string at Data.
|
2013-02-16 15:26:45 +01:00
|
|
|
///< Returns a pointer to a newly allocated string.
|
2003-01-06 14:44:27 +01:00
|
|
|
{
|
2017-06-10 11:53:39 +02:00
|
|
|
char *s = MALLOC(char, Length + 1);
|
|
|
|
char *p = s;
|
|
|
|
while (Length > 0) {
|
2020-08-17 10:26:18 +02:00
|
|
|
int c = *Data;
|
2017-06-10 11:53:39 +02:00
|
|
|
if (isprint(c)) // some CAMs send funny characters in their strings, let's just skip them
|
|
|
|
*p++ = c;
|
|
|
|
else if (c == 0x8A) // the character 0x8A is used as newline, so let's put a real '\n' in there
|
|
|
|
*p++ = '\n';
|
2005-10-02 13:13:31 +02:00
|
|
|
Length--;
|
|
|
|
Data++;
|
|
|
|
}
|
2017-06-10 11:53:39 +02:00
|
|
|
*p = 0;
|
2003-01-06 14:44:27 +01:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *GetString(int &Length, const uint8_t **Data)
|
|
|
|
///< Gets the string at Data.
|
2013-02-16 15:26:45 +01:00
|
|
|
///< Returns a pointer to a newly allocated string, or NULL in case of error.
|
2003-01-06 14:44:27 +01:00
|
|
|
///< Upon return Length and Data represent the remaining data after the string has been skipped.
|
|
|
|
{
|
|
|
|
if (Length > 0 && Data && *Data) {
|
|
|
|
int l = 0;
|
|
|
|
const uint8_t *d = GetLength(*Data, l);
|
|
|
|
char *s = CopyString(l, d);
|
|
|
|
Length -= d - *Data + l;
|
|
|
|
*Data = d + l;
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2014-01-01 12:37:22 +01:00
|
|
|
// --- cCaPidReceiver --------------------------------------------------------
|
|
|
|
|
2014-04-06 10:56:50 +02:00
|
|
|
// A receiver that is used to make the device receive the ECM pids, as well as the
|
|
|
|
// CAT and the EMM pids.
|
2014-01-01 12:37:22 +01:00
|
|
|
|
|
|
|
class cCaPidReceiver : public cReceiver {
|
2014-04-06 10:56:50 +02:00
|
|
|
private:
|
|
|
|
int catVersion;
|
|
|
|
cVector<int> emmPids;
|
2020-07-10 09:06:21 +02:00
|
|
|
uchar buffer[1024]; // CAT table length: 10 bit -> max. 1021 + 3 bytes
|
2015-01-13 14:45:12 +01:00
|
|
|
uchar *bufp;
|
2020-07-10 09:06:21 +02:00
|
|
|
#define CAT_MAXPACKETS 6 // 6 * 184 = 1104 bytes for CAT table
|
|
|
|
uchar mtdCatBuffer[CAT_MAXPACKETS][TS_SIZE]; // TODO: handle multi table CATs!
|
|
|
|
int mtdNumCatPackets;
|
2015-01-13 14:45:12 +01:00
|
|
|
int length;
|
2018-01-28 11:21:28 +01:00
|
|
|
cMutex mutex;
|
|
|
|
bool handlingPid;
|
2014-04-06 10:56:50 +02:00
|
|
|
void AddEmmPid(int Pid);
|
|
|
|
void DelEmmPids(void);
|
2014-01-01 12:37:22 +01:00
|
|
|
public:
|
2014-04-06 10:56:50 +02:00
|
|
|
cCaPidReceiver(void);
|
2014-01-01 12:37:22 +01:00
|
|
|
virtual ~cCaPidReceiver() { Detach(); }
|
2015-09-05 11:49:56 +02:00
|
|
|
virtual void Receive(const uchar *Data, int Length);
|
|
|
|
bool HasCaPids(void) const { return NumPids() - emmPids.Size() - 1 > 0; }
|
2015-01-12 10:44:58 +01:00
|
|
|
void Reset(void) { DelEmmPids(); catVersion = -1; }
|
2018-01-28 11:21:28 +01:00
|
|
|
bool HandlingPid(void);
|
|
|
|
///< The cCaPidReceiver adds/deletes PIDs to/from the base class cReceiver,
|
|
|
|
///< which in turn does the same on the cDevice it is attached to. The cDevice
|
|
|
|
///< then sets the PIDs on the assigned cCamSlot, which can cause a deadlock on the
|
|
|
|
///< cCamSlot's mutex if a cReceiver is detached from the device at the same time.
|
|
|
|
///< Since these PIDs, however, are none that have to be decrypted,
|
|
|
|
///< it is not necessary to set them in the CAM. Therefore this function is
|
|
|
|
///< used in cCamSlot::SetPid() to detect this situation, and thus avoid the
|
|
|
|
///< deadlock.
|
2014-01-01 12:37:22 +01:00
|
|
|
};
|
|
|
|
|
2014-04-06 10:56:50 +02:00
|
|
|
cCaPidReceiver::cCaPidReceiver(void)
|
|
|
|
{
|
|
|
|
catVersion = -1;
|
2015-01-13 14:45:12 +01:00
|
|
|
bufp = NULL;
|
2020-07-10 09:06:21 +02:00
|
|
|
mtdNumCatPackets = 0;
|
2015-01-13 14:45:12 +01:00
|
|
|
length = 0;
|
2018-01-28 11:21:28 +01:00
|
|
|
handlingPid = false;
|
|
|
|
cMutexLock MutexLock(&mutex);
|
|
|
|
handlingPid = true;
|
2014-04-06 10:56:50 +02:00
|
|
|
AddPid(CATPID);
|
2018-01-28 11:21:28 +01:00
|
|
|
handlingPid = false;
|
2014-04-06 10:56:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void cCaPidReceiver::AddEmmPid(int Pid)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < emmPids.Size(); i++) {
|
|
|
|
if (emmPids[i] == Pid)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
emmPids.Append(Pid);
|
2018-01-28 11:21:28 +01:00
|
|
|
cMutexLock MutexLock(&mutex);
|
|
|
|
handlingPid = true;
|
2014-04-06 10:56:50 +02:00
|
|
|
AddPid(Pid);
|
2018-01-28 11:21:28 +01:00
|
|
|
handlingPid = false;
|
2014-04-06 10:56:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void cCaPidReceiver::DelEmmPids(void)
|
|
|
|
{
|
2018-01-28 11:21:28 +01:00
|
|
|
cMutexLock MutexLock(&mutex);
|
|
|
|
handlingPid = true;
|
2014-04-06 10:56:50 +02:00
|
|
|
for (int i = 0; i < emmPids.Size(); i++)
|
|
|
|
DelPid(emmPids[i]);
|
|
|
|
emmPids.Clear();
|
2018-01-28 11:21:28 +01:00
|
|
|
handlingPid = false;
|
2014-04-06 10:56:50 +02:00
|
|
|
}
|
|
|
|
|
2015-09-05 11:49:56 +02:00
|
|
|
void cCaPidReceiver::Receive(const uchar *Data, int Length)
|
2014-04-06 10:56:50 +02:00
|
|
|
{
|
2015-01-13 14:45:12 +01:00
|
|
|
if (TsPid(Data) == CATPID) {
|
2017-03-18 16:33:59 +01:00
|
|
|
cMtdCamSlot *MtdCamSlot = dynamic_cast<cMtdCamSlot *>(Device()->CamSlot());
|
2015-09-05 11:49:56 +02:00
|
|
|
const uchar *p = NULL;
|
2015-01-13 14:45:12 +01:00
|
|
|
if (TsPayloadStart(Data)) {
|
|
|
|
if (Data[5] == SI::TableIdCAT) {
|
2020-07-10 09:06:21 +02:00
|
|
|
length = (int(Data[6] & 0x0F) << 8) | Data[7]; // section length (12 bit field)
|
2015-01-13 14:45:12 +01:00
|
|
|
if (length > 5) {
|
|
|
|
int v = (Data[10] & 0x3E) >> 1; // version number
|
|
|
|
if (v != catVersion) {
|
|
|
|
if (Data[11] == 0 && Data[12] == 0) { // section number, last section number
|
2020-07-10 09:06:21 +02:00
|
|
|
length += 3; // with TableIdCAT -> Data[5]
|
|
|
|
if (length > TS_SIZE - 5) {
|
|
|
|
int n = TS_SIZE - 5;
|
|
|
|
memcpy(buffer, Data + 5, n);
|
2015-01-13 14:45:12 +01:00
|
|
|
bufp = buffer + n;
|
2020-07-10 09:06:21 +02:00
|
|
|
length -= n;
|
2015-01-13 14:45:12 +01:00
|
|
|
}
|
|
|
|
else {
|
2020-07-10 09:06:21 +02:00
|
|
|
p = Data + 5; // no need to copy the data
|
|
|
|
}
|
|
|
|
if (MtdCamSlot) {
|
|
|
|
mtdNumCatPackets = 0;
|
|
|
|
memcpy(mtdCatBuffer[mtdNumCatPackets++], Data, TS_SIZE);
|
2015-01-13 14:45:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
dsyslog("multi table CAT section - unhandled!");
|
|
|
|
catVersion = v;
|
2014-04-06 10:56:50 +02:00
|
|
|
}
|
2020-07-10 09:06:21 +02:00
|
|
|
else if (MtdCamSlot) {
|
|
|
|
for (int i = 0; i < mtdNumCatPackets; i++)
|
|
|
|
MtdCamSlot->PutCat(mtdCatBuffer[i], TS_SIZE);
|
|
|
|
}
|
2014-04-06 10:56:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-01-13 14:45:12 +01:00
|
|
|
else if (bufp && length > 0) {
|
|
|
|
int n = min(length, TS_SIZE - 4);
|
|
|
|
if (bufp + n - buffer <= int(sizeof(buffer))) {
|
|
|
|
memcpy(bufp, Data + 4, n);
|
|
|
|
bufp += n;
|
|
|
|
length -= n;
|
|
|
|
if (length <= 0) {
|
|
|
|
p = buffer;
|
|
|
|
length = bufp - buffer;
|
|
|
|
}
|
2020-07-10 09:06:21 +02:00
|
|
|
if (MtdCamSlot)
|
|
|
|
memcpy(mtdCatBuffer[mtdNumCatPackets++], Data, TS_SIZE);
|
2015-01-13 14:45:12 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
esyslog("ERROR: buffer overflow in cCaPidReceiver::Receive()");
|
2019-05-05 18:35:15 +02:00
|
|
|
bufp = NULL;
|
2015-01-13 14:45:12 +01:00
|
|
|
length = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (p) {
|
2020-07-10 09:06:21 +02:00
|
|
|
if (!SI::CRC32::crc32((const char *)p, length, 0xFFFFFFFF)) { // <TableIdCAT,....,crc32>
|
2020-06-27 10:05:56 +02:00
|
|
|
DelEmmPids();
|
2020-07-10 09:06:21 +02:00
|
|
|
for (int i = 8; i < length - 4; i++) { // -4 = checksum
|
2020-06-27 10:05:56 +02:00
|
|
|
if (p[i] == 0x09) {
|
|
|
|
int CaId = int(p[i + 2] << 8) | p[i + 3];
|
|
|
|
int EmmPid = Peek13(p + i + 4);
|
|
|
|
AddEmmPid(EmmPid);
|
|
|
|
if (MtdCamSlot)
|
|
|
|
MtdMapPid(const_cast<uchar *>(p + i + 4), MtdCamSlot->MtdMapper());
|
|
|
|
switch (CaId >> 8) {
|
2020-07-10 09:06:21 +02:00
|
|
|
case 0x01: for (int j = i + 7; j < i + p[i + 1] + 2; j += 4) {
|
2020-06-27 10:05:56 +02:00
|
|
|
EmmPid = Peek13(p + j);
|
|
|
|
AddEmmPid(EmmPid);
|
|
|
|
if (MtdCamSlot)
|
|
|
|
MtdMapPid(const_cast<uchar *>(p + j), MtdCamSlot->MtdMapper());
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
i += p[i + 1] + 2 - 1; // -1 to compensate for the loop increment
|
|
|
|
}
|
2015-01-13 14:45:12 +01:00
|
|
|
}
|
2020-06-27 10:05:56 +02:00
|
|
|
if (MtdCamSlot) {
|
2020-07-10 09:06:21 +02:00
|
|
|
// update crc32
|
|
|
|
uint32_t crc = SI::CRC32::crc32((const char *)p, length - 4, 0xFFFFFFFF); // <TableIdCAT....>[crc32]
|
|
|
|
uchar *c = const_cast<uchar *>(p + length - 4);
|
|
|
|
*c++ = crc >> 24;
|
|
|
|
*c++ = crc >> 16;
|
|
|
|
*c++ = crc >> 8;
|
|
|
|
*c++ = crc;
|
|
|
|
// modify CAT packets
|
|
|
|
const uchar *t = p;
|
|
|
|
for (int i = 0, j = 5; i < mtdNumCatPackets; i++, j = 4) {
|
|
|
|
int n = min(length, TS_SIZE - j);
|
|
|
|
memcpy(mtdCatBuffer[i] + j, t, n);
|
|
|
|
t += n;
|
|
|
|
length -= n;
|
|
|
|
MtdCamSlot->PutCat(mtdCatBuffer[i], TS_SIZE);
|
|
|
|
}
|
2019-05-05 18:35:15 +02:00
|
|
|
}
|
2020-06-27 10:05:56 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
esyslog("ERROR: wrong checksum in CAT");
|
|
|
|
catVersion = -1;
|
2019-05-05 18:35:15 +02:00
|
|
|
}
|
2015-01-13 14:45:12 +01:00
|
|
|
p = NULL;
|
2019-05-05 18:35:15 +02:00
|
|
|
bufp = NULL;
|
2015-01-13 14:45:12 +01:00
|
|
|
length = 0;
|
|
|
|
}
|
2014-04-06 10:56:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-28 11:21:28 +01:00
|
|
|
bool cCaPidReceiver::HandlingPid(void)
|
|
|
|
{
|
|
|
|
cMutexLock MutexLock(&mutex);
|
|
|
|
return handlingPid;
|
|
|
|
}
|
|
|
|
|
2015-01-30 13:38:44 +01:00
|
|
|
// --- cCaActivationReceiver -------------------------------------------------
|
|
|
|
|
|
|
|
// A receiver that is used to make the device stay on a given channel and
|
|
|
|
// keep the CAM slot assigned.
|
|
|
|
|
|
|
|
#define UNSCRAMBLE_TIME 5 // seconds of receiving purely unscrambled data before considering the smart card "activated"
|
|
|
|
#define TS_PACKET_FACTOR 1024 // only process every TS_PACKET_FACTORth packet to keep the load down
|
|
|
|
|
|
|
|
class cCaActivationReceiver : public cReceiver {
|
|
|
|
private:
|
|
|
|
cCamSlot *camSlot;
|
|
|
|
time_t lastScrambledTime;
|
|
|
|
int numTsPackets;
|
|
|
|
protected:
|
2015-09-05 11:49:56 +02:00
|
|
|
virtual void Receive(const uchar *Data, int Length);
|
2015-01-30 13:38:44 +01:00
|
|
|
public:
|
|
|
|
cCaActivationReceiver(const cChannel *Channel, cCamSlot *CamSlot);
|
|
|
|
virtual ~cCaActivationReceiver();
|
|
|
|
};
|
|
|
|
|
|
|
|
cCaActivationReceiver::cCaActivationReceiver(const cChannel *Channel, cCamSlot *CamSlot)
|
|
|
|
:cReceiver(Channel, MINPRIORITY + 1)
|
|
|
|
{
|
|
|
|
camSlot = CamSlot;
|
|
|
|
lastScrambledTime = time(NULL);
|
|
|
|
numTsPackets = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
cCaActivationReceiver::~cCaActivationReceiver()
|
|
|
|
{
|
|
|
|
Detach();
|
|
|
|
}
|
|
|
|
|
2015-09-05 11:49:56 +02:00
|
|
|
void cCaActivationReceiver::Receive(const uchar *Data, int Length)
|
2015-01-30 13:38:44 +01:00
|
|
|
{
|
|
|
|
if (numTsPackets++ % TS_PACKET_FACTOR == 0) {
|
|
|
|
time_t Now = time(NULL);
|
|
|
|
if (TsIsScrambled(Data))
|
|
|
|
lastScrambledTime = Now;
|
|
|
|
else if (Now - lastScrambledTime > UNSCRAMBLE_TIME) {
|
2018-02-03 12:59:56 +01:00
|
|
|
dsyslog("CAM %d: activated!", camSlot->MasterSlotNumber());
|
2015-01-30 13:38:44 +01:00
|
|
|
Skins.QueueMessage(mtInfo, tr("CAM activated!"));
|
2017-03-18 16:33:59 +01:00
|
|
|
cDevice *d = Device();
|
2015-01-30 13:38:44 +01:00
|
|
|
Detach();
|
2017-03-18 16:33:59 +01:00
|
|
|
if (d) {
|
|
|
|
if (cCamSlot *s = d->CamSlot())
|
|
|
|
s->CancelActivation(); // this will delete *this* object, so no more code referencing *this* after this call!
|
|
|
|
}
|
2015-01-30 13:38:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-10 11:53:39 +02:00
|
|
|
// --- cCamResponse ----------------------------------------------------------
|
|
|
|
|
|
|
|
// CAM Response Actions:
|
|
|
|
|
|
|
|
#define CRA_NONE 0
|
|
|
|
#define CRA_DISCARD -1
|
|
|
|
#define CRA_CONFIRM -2
|
|
|
|
#define CRA_SELECT -3
|
|
|
|
|
|
|
|
class cCamResponse : public cListObject {
|
|
|
|
private:
|
|
|
|
int camNumber;
|
|
|
|
char *text;
|
|
|
|
int action;
|
|
|
|
public:
|
|
|
|
cCamResponse(void);
|
|
|
|
~cCamResponse();
|
|
|
|
bool Parse(const char *s);
|
|
|
|
int Matches(int CamNumber, const char *Text) const;
|
|
|
|
};
|
|
|
|
|
|
|
|
cCamResponse::cCamResponse(void)
|
|
|
|
{
|
|
|
|
camNumber = -1;
|
|
|
|
text = NULL;
|
|
|
|
action = CRA_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
cCamResponse::~cCamResponse()
|
|
|
|
{
|
|
|
|
free(text);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cCamResponse::Parse(const char *s)
|
|
|
|
{
|
|
|
|
// Number:
|
|
|
|
s = skipspace(s);
|
|
|
|
if (*s == '*') {
|
|
|
|
camNumber = 0; // all CAMs
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
char *e;
|
|
|
|
camNumber = strtol(s, &e, 10);
|
|
|
|
if (e == s || camNumber <= 0)
|
|
|
|
return false;
|
|
|
|
s = e;
|
|
|
|
}
|
|
|
|
// Text:
|
|
|
|
s = skipspace(s);
|
|
|
|
char *t = const_cast<char *>(s); // might have to modify it
|
|
|
|
char *q = NULL; // holds a copy in case of backslashes
|
|
|
|
bool InQuotes = false;
|
|
|
|
while (*t) {
|
|
|
|
if (*t == '"') {
|
|
|
|
if (t == s) { // opening quotes
|
|
|
|
InQuotes = true;
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
else if (InQuotes) // closing quotes
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (*t == '\\') {
|
|
|
|
if (!q) { // need to make a copy in order to strip backslashes
|
|
|
|
q = strdup(s);
|
|
|
|
t = q + (t - s);
|
|
|
|
s = q;
|
|
|
|
}
|
|
|
|
memmove(t, t + 1, strlen(t));
|
|
|
|
}
|
|
|
|
else if (*t == ' ') {
|
|
|
|
if (!InQuotes)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
t++;
|
|
|
|
}
|
|
|
|
free(text); // just for safety
|
|
|
|
text = NULL;
|
|
|
|
if (t != s) {
|
|
|
|
text = strndup(s, t - s);
|
|
|
|
s = t + 1;
|
|
|
|
}
|
|
|
|
free(q);
|
|
|
|
if (!text)
|
|
|
|
return false;
|
|
|
|
// Action:
|
|
|
|
s = skipspace(s);
|
|
|
|
if (strcasecmp(s, "DISCARD") == 0) action = CRA_DISCARD;
|
|
|
|
else if (strcasecmp(s, "CONFIRM") == 0) action = CRA_CONFIRM;
|
|
|
|
else if (strcasecmp(s, "SELECT") == 0) action = CRA_SELECT;
|
|
|
|
else if (isnumber(s)) action = atoi(s);
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int cCamResponse::Matches(int CamNumber, const char *Text) const
|
|
|
|
{
|
|
|
|
if (!camNumber || camNumber == CamNumber) {
|
|
|
|
if (strcmp(text, Text) == 0)
|
|
|
|
return action;
|
|
|
|
}
|
|
|
|
return CRA_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// --- cCamResponses --------------------------------------------------------
|
|
|
|
|
|
|
|
class cCamResponses : public cConfig<cCamResponse> {
|
|
|
|
public:
|
|
|
|
int GetMatch(int CamNumber, const char *Text) const;
|
|
|
|
};
|
|
|
|
|
|
|
|
int cCamResponses::GetMatch(int CamNumber, const char *Text) const
|
|
|
|
{
|
|
|
|
for (const cCamResponse *cr = First(); cr; cr = Next(cr)) {
|
|
|
|
int Action = cr->Matches(CamNumber, Text);
|
|
|
|
if (Action != CRA_NONE) {
|
|
|
|
dsyslog("CAM %d: auto response %4d to '%s'\n", CamNumber, Action, Text);
|
|
|
|
return Action;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return CRA_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
cCamResponses CamResponses;
|
|
|
|
|
|
|
|
bool CamResponsesLoad(const char *FileName, bool AllowComments, bool MustExist)
|
|
|
|
{
|
|
|
|
return CamResponses.Load(FileName, AllowComments, MustExist);
|
|
|
|
}
|
|
|
|
|
2003-01-06 14:44:27 +01:00
|
|
|
// --- cTPDU -----------------------------------------------------------------
|
|
|
|
|
2017-05-18 09:05:46 +02:00
|
|
|
#define MAX_TPDU_SIZE 4096
|
2003-01-06 14:44:27 +01:00
|
|
|
#define MAX_TPDU_DATA (MAX_TPDU_SIZE - 4)
|
|
|
|
|
|
|
|
#define DATA_INDICATOR 0x80
|
|
|
|
|
|
|
|
#define T_SB 0x80
|
|
|
|
#define T_RCV 0x81
|
|
|
|
#define T_CREATE_TC 0x82
|
|
|
|
#define T_CTC_REPLY 0x83
|
|
|
|
#define T_DELETE_TC 0x84
|
|
|
|
#define T_DTC_REPLY 0x85
|
|
|
|
#define T_REQUEST_TC 0x86
|
|
|
|
#define T_NEW_TC 0x87
|
|
|
|
#define T_TC_ERROR 0x88
|
|
|
|
#define T_DATA_LAST 0xA0
|
|
|
|
#define T_DATA_MORE 0xA1
|
|
|
|
|
|
|
|
class cTPDU {
|
|
|
|
private:
|
|
|
|
int size;
|
2007-01-07 14:46:14 +01:00
|
|
|
uint8_t buffer[MAX_TPDU_SIZE];
|
2003-01-06 14:44:27 +01:00
|
|
|
const uint8_t *GetData(const uint8_t *Data, int &Length);
|
|
|
|
public:
|
|
|
|
cTPDU(void) { size = 0; }
|
|
|
|
cTPDU(uint8_t Slot, uint8_t Tcid, uint8_t Tag, int Length = 0, const uint8_t *Data = NULL);
|
2007-01-07 14:46:14 +01:00
|
|
|
uint8_t Slot(void) { return buffer[0]; }
|
|
|
|
uint8_t Tcid(void) { return buffer[1]; }
|
|
|
|
uint8_t Tag(void) { return buffer[2]; }
|
|
|
|
const uint8_t *Data(int &Length) { return GetData(buffer + 3, Length); }
|
2003-01-06 14:44:27 +01:00
|
|
|
uint8_t Status(void);
|
2007-01-07 14:46:14 +01:00
|
|
|
uint8_t *Buffer(void) { return buffer; }
|
|
|
|
int Size(void) { return size; }
|
|
|
|
void SetSize(int Size) { size = Size; }
|
|
|
|
int MaxSize(void) { return sizeof(buffer); }
|
|
|
|
void Dump(int SlotNumber, bool Outgoing);
|
2003-01-06 14:44:27 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
cTPDU::cTPDU(uint8_t Slot, uint8_t Tcid, uint8_t Tag, int Length, const uint8_t *Data)
|
|
|
|
{
|
|
|
|
size = 0;
|
2007-01-07 14:46:14 +01:00
|
|
|
buffer[0] = Slot;
|
|
|
|
buffer[1] = Tcid;
|
|
|
|
buffer[2] = Tag;
|
2003-01-06 14:44:27 +01:00
|
|
|
switch (Tag) {
|
|
|
|
case T_RCV:
|
|
|
|
case T_CREATE_TC:
|
|
|
|
case T_CTC_REPLY:
|
|
|
|
case T_DELETE_TC:
|
|
|
|
case T_DTC_REPLY:
|
|
|
|
case T_REQUEST_TC:
|
2007-01-07 14:46:14 +01:00
|
|
|
buffer[3] = 1; // length
|
|
|
|
buffer[4] = Tcid;
|
2003-01-06 14:44:27 +01:00
|
|
|
size = 5;
|
|
|
|
break;
|
|
|
|
case T_NEW_TC:
|
|
|
|
case T_TC_ERROR:
|
|
|
|
if (Length == 1) {
|
2007-01-07 14:46:14 +01:00
|
|
|
buffer[3] = 2; // length
|
|
|
|
buffer[4] = Tcid;
|
|
|
|
buffer[5] = Data[0];
|
2003-01-06 14:44:27 +01:00
|
|
|
size = 6;
|
|
|
|
}
|
|
|
|
else
|
2007-01-07 14:46:14 +01:00
|
|
|
esyslog("ERROR: invalid data length for TPDU tag 0x%02X: %d (%d/%d)", Tag, Length, Slot, Tcid);
|
2003-01-06 14:44:27 +01:00
|
|
|
break;
|
|
|
|
case T_DATA_LAST:
|
|
|
|
case T_DATA_MORE:
|
|
|
|
if (Length <= MAX_TPDU_DATA) {
|
2007-01-07 14:46:14 +01:00
|
|
|
uint8_t *p = buffer + 3;
|
2003-01-06 14:44:27 +01:00
|
|
|
p = SetLength(p, Length + 1);
|
|
|
|
*p++ = Tcid;
|
|
|
|
if (Length)
|
|
|
|
memcpy(p, Data, Length);
|
2007-01-07 14:46:14 +01:00
|
|
|
size = Length + (p - buffer);
|
2003-01-06 14:44:27 +01:00
|
|
|
}
|
|
|
|
else
|
2007-01-07 14:46:14 +01:00
|
|
|
esyslog("ERROR: invalid data length for TPDU tag 0x%02X: %d (%d/%d)", Tag, Length, Slot, Tcid);
|
2003-01-06 14:44:27 +01:00
|
|
|
break;
|
|
|
|
default:
|
2007-01-07 14:46:14 +01:00
|
|
|
esyslog("ERROR: unknown TPDU tag: 0x%02X (%d/%d)", Tag, Slot, Tcid);
|
2003-01-06 14:44:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-07 14:46:14 +01:00
|
|
|
void cTPDU::Dump(int SlotNumber, bool Outgoing)
|
2003-01-06 14:44:27 +01:00
|
|
|
{
|
2007-01-07 14:46:14 +01:00
|
|
|
if (DumpTPDUDataTransfer && (DumpPolls || Tag() != T_SB)) {
|
2003-01-06 14:44:27 +01:00
|
|
|
#define MAX_DUMP 256
|
2007-01-07 14:46:14 +01:00
|
|
|
fprintf(stderr, " %d: %s ", SlotNumber, Outgoing ? "-->" : "<--");
|
2003-01-06 14:44:27 +01:00
|
|
|
for (int i = 0; i < size && i < MAX_DUMP; i++)
|
2007-01-07 14:46:14 +01:00
|
|
|
fprintf(stderr, "%02X ", buffer[i]);
|
2003-02-09 11:54:22 +01:00
|
|
|
fprintf(stderr, "%s\n", size >= MAX_DUMP ? "..." : "");
|
2003-01-06 14:44:27 +01:00
|
|
|
if (!Outgoing) {
|
2007-01-07 14:46:14 +01:00
|
|
|
fprintf(stderr, " ");
|
2003-01-06 14:44:27 +01:00
|
|
|
for (int i = 0; i < size && i < MAX_DUMP; i++)
|
2007-01-07 14:46:14 +01:00
|
|
|
fprintf(stderr, "%2c ", isprint(buffer[i]) ? buffer[i] : '.');
|
2003-02-09 11:54:22 +01:00
|
|
|
fprintf(stderr, "%s\n", size >= MAX_DUMP ? "..." : "");
|
2003-01-06 14:44:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint8_t *cTPDU::GetData(const uint8_t *Data, int &Length)
|
|
|
|
{
|
|
|
|
if (size) {
|
|
|
|
Data = GetLength(Data, Length);
|
|
|
|
if (Length) {
|
|
|
|
Length--; // the first byte is always the tcid
|
|
|
|
return Data + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t cTPDU::Status(void)
|
|
|
|
{
|
2007-01-07 14:46:14 +01:00
|
|
|
if (size >= 4 && buffer[size - 4] == T_SB && buffer[size - 3] == 2)
|
|
|
|
return buffer[size - 1];
|
2003-01-06 14:44:27 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// --- cCiTransportConnection ------------------------------------------------
|
|
|
|
|
2007-01-07 14:46:14 +01:00
|
|
|
#define MAX_SESSIONS_PER_TC 16
|
2003-01-06 14:44:27 +01:00
|
|
|
|
|
|
|
class cCiTransportConnection {
|
|
|
|
private:
|
2007-01-07 14:46:14 +01:00
|
|
|
enum eState { stIDLE, stCREATION, stACTIVE, stDELETION };
|
2020-06-16 14:25:43 +02:00
|
|
|
cMutex mutex;
|
2007-01-07 14:46:14 +01:00
|
|
|
cCamSlot *camSlot;
|
2003-01-06 14:44:27 +01:00
|
|
|
uint8_t tcid;
|
|
|
|
eState state;
|
2007-01-07 14:46:14 +01:00
|
|
|
bool createConnectionRequested;
|
|
|
|
bool deleteConnectionRequested;
|
|
|
|
bool hasUserIO;
|
|
|
|
cTimeMs alive;
|
|
|
|
cTimeMs timer;
|
|
|
|
cCiSession *sessions[MAX_SESSIONS_PER_TC + 1]; // session numbering starts with 1
|
2017-05-18 09:05:46 +02:00
|
|
|
cCiSession *tsPostProcessor;
|
2007-01-07 14:46:14 +01:00
|
|
|
void SendTPDU(uint8_t Tag, int Length = 0, const uint8_t *Data = NULL);
|
|
|
|
void SendTag(uint8_t Tag, uint16_t SessionId, uint32_t ResourceId = 0, int Status = -1);
|
|
|
|
void Poll(void);
|
|
|
|
uint32_t ResourceIdToInt(const uint8_t *Data);
|
|
|
|
cCiSession *GetSessionBySessionId(uint16_t SessionId);
|
|
|
|
void OpenSession(int Length, const uint8_t *Data);
|
|
|
|
void CloseSession(uint16_t SessionId);
|
|
|
|
void HandleSessions(cTPDU *TPDU);
|
2003-01-06 14:44:27 +01:00
|
|
|
public:
|
2007-01-07 14:46:14 +01:00
|
|
|
cCiTransportConnection(cCamSlot *CamSlot, uint8_t Tcid);
|
|
|
|
virtual ~cCiTransportConnection();
|
2017-05-18 09:05:46 +02:00
|
|
|
void SetTsPostProcessor(cCiSession *CiSession);
|
|
|
|
bool TsPostProcess(uint8_t *TsPacket);
|
2007-01-07 14:46:14 +01:00
|
|
|
cCamSlot *CamSlot(void) { return camSlot; }
|
|
|
|
uint8_t Tcid(void) const { return tcid; }
|
|
|
|
void CreateConnection(void) { createConnectionRequested = true; }
|
|
|
|
void DeleteConnection(void) { deleteConnectionRequested = true; }
|
|
|
|
const char *GetCamName(void);
|
|
|
|
bool Ready(void);
|
|
|
|
bool HasUserIO(void) { return hasUserIO; }
|
|
|
|
void SendData(int Length, const uint8_t *Data);
|
|
|
|
bool Process(cTPDU *TPDU = NULL);
|
|
|
|
cCiSession *GetSessionByResourceId(uint32_t ResourceId);
|
2003-01-06 14:44:27 +01:00
|
|
|
};
|
|
|
|
|
2007-01-07 14:46:14 +01:00
|
|
|
// --- cCiSession ------------------------------------------------------------
|
2003-01-06 14:44:27 +01:00
|
|
|
|
|
|
|
// Session Tags:
|
|
|
|
|
|
|
|
#define ST_SESSION_NUMBER 0x90
|
|
|
|
#define ST_OPEN_SESSION_REQUEST 0x91
|
|
|
|
#define ST_OPEN_SESSION_RESPONSE 0x92
|
|
|
|
#define ST_CREATE_SESSION 0x93
|
|
|
|
#define ST_CREATE_SESSION_RESPONSE 0x94
|
|
|
|
#define ST_CLOSE_SESSION_REQUEST 0x95
|
|
|
|
#define ST_CLOSE_SESSION_RESPONSE 0x96
|
|
|
|
|
|
|
|
// Session Status:
|
|
|
|
|
|
|
|
#define SS_OK 0x00
|
|
|
|
#define SS_NOT_ALLOCATED 0xF0
|
|
|
|
|
|
|
|
// Resource Identifiers:
|
|
|
|
|
|
|
|
#define RI_RESOURCE_MANAGER 0x00010041
|
|
|
|
#define RI_APPLICATION_INFORMATION 0x00020041
|
|
|
|
#define RI_CONDITIONAL_ACCESS_SUPPORT 0x00030041
|
|
|
|
#define RI_HOST_CONTROL 0x00200041
|
|
|
|
#define RI_DATE_TIME 0x00240041
|
|
|
|
#define RI_MMI 0x00400041
|
|
|
|
|
|
|
|
// Application Object Tags:
|
|
|
|
|
|
|
|
#define AOT_NONE 0x000000
|
|
|
|
#define AOT_PROFILE_ENQ 0x9F8010
|
|
|
|
#define AOT_PROFILE 0x9F8011
|
|
|
|
#define AOT_PROFILE_CHANGE 0x9F8012
|
|
|
|
#define AOT_APPLICATION_INFO_ENQ 0x9F8020
|
|
|
|
#define AOT_APPLICATION_INFO 0x9F8021
|
|
|
|
#define AOT_ENTER_MENU 0x9F8022
|
|
|
|
#define AOT_CA_INFO_ENQ 0x9F8030
|
|
|
|
#define AOT_CA_INFO 0x9F8031
|
|
|
|
#define AOT_CA_PMT 0x9F8032
|
|
|
|
#define AOT_CA_PMT_REPLY 0x9F8033
|
|
|
|
#define AOT_TUNE 0x9F8400
|
|
|
|
#define AOT_REPLACE 0x9F8401
|
|
|
|
#define AOT_CLEAR_REPLACE 0x9F8402
|
|
|
|
#define AOT_ASK_RELEASE 0x9F8403
|
|
|
|
#define AOT_DATE_TIME_ENQ 0x9F8440
|
|
|
|
#define AOT_DATE_TIME 0x9F8441
|
|
|
|
#define AOT_CLOSE_MMI 0x9F8800
|
|
|
|
#define AOT_DISPLAY_CONTROL 0x9F8801
|
|
|
|
#define AOT_DISPLAY_REPLY 0x9F8802
|
|
|
|
#define AOT_TEXT_LAST 0x9F8803
|
|
|
|
#define AOT_TEXT_MORE 0x9F8804
|
|
|
|
#define AOT_KEYPAD_CONTROL 0x9F8805
|
|
|
|
#define AOT_KEYPRESS 0x9F8806
|
|
|
|
#define AOT_ENQ 0x9F8807
|
|
|
|
#define AOT_ANSW 0x9F8808
|
|
|
|
#define AOT_MENU_LAST 0x9F8809
|
|
|
|
#define AOT_MENU_MORE 0x9F880A
|
|
|
|
#define AOT_MENU_ANSW 0x9F880B
|
|
|
|
#define AOT_LIST_LAST 0x9F880C
|
|
|
|
#define AOT_LIST_MORE 0x9F880D
|
|
|
|
#define AOT_SUBTITLE_SEGMENT_LAST 0x9F880E
|
|
|
|
#define AOT_SUBTITLE_SEGMENT_MORE 0x9F880F
|
|
|
|
#define AOT_DISPLAY_MESSAGE 0x9F8810
|
|
|
|
#define AOT_SCENE_END_MARK 0x9F8811
|
|
|
|
#define AOT_SCENE_DONE 0x9F8812
|
|
|
|
#define AOT_SCENE_CONTROL 0x9F8813
|
|
|
|
#define AOT_SUBTITLE_DOWNLOAD_LAST 0x9F8814
|
|
|
|
#define AOT_SUBTITLE_DOWNLOAD_MORE 0x9F8815
|
|
|
|
#define AOT_FLUSH_DOWNLOAD 0x9F8816
|
|
|
|
#define AOT_DOWNLOAD_REPLY 0x9F8817
|
|
|
|
#define AOT_COMMS_CMD 0x9F8C00
|
|
|
|
#define AOT_CONNECTION_DESCRIPTOR 0x9F8C01
|
|
|
|
#define AOT_COMMS_REPLY 0x9F8C02
|
|
|
|
#define AOT_COMMS_SEND_LAST 0x9F8C03
|
|
|
|
#define AOT_COMMS_SEND_MORE 0x9F8C04
|
|
|
|
#define AOT_COMMS_RCV_LAST 0x9F8C05
|
|
|
|
#define AOT_COMMS_RCV_MORE 0x9F8C06
|
|
|
|
|
2017-05-18 09:05:46 +02:00
|
|
|
#define RESOURCE_CLASS_MASK 0xFFFF0000
|
2003-01-06 14:44:27 +01:00
|
|
|
|
2006-08-12 10:26:43 +02:00
|
|
|
cCiSession::cCiSession(uint16_t SessionId, uint32_t ResourceId, cCiTransportConnection *Tc)
|
2003-01-06 14:44:27 +01:00
|
|
|
{
|
|
|
|
sessionId = SessionId;
|
|
|
|
resourceId = ResourceId;
|
|
|
|
tc = Tc;
|
|
|
|
}
|
|
|
|
|
|
|
|
cCiSession::~cCiSession()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-05-18 09:05:46 +02:00
|
|
|
void cCiSession::SetResourceId(uint32_t Id)
|
|
|
|
{
|
|
|
|
resourceId = Id;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cCiSession::SetTsPostProcessor(void)
|
|
|
|
{
|
|
|
|
tc->SetTsPostProcessor(this);
|
|
|
|
}
|
|
|
|
|
2003-01-06 14:44:27 +01:00
|
|
|
int cCiSession::GetTag(int &Length, const uint8_t **Data)
|
|
|
|
///< Gets the tag at Data.
|
2013-02-16 15:26:45 +01:00
|
|
|
///< Returns the actual tag, or AOT_NONE in case of error.
|
2003-01-06 14:44:27 +01:00
|
|
|
///< Upon return Length and Data represent the remaining data after the tag has been skipped.
|
|
|
|
{
|
|
|
|
if (Length >= 3 && Data && *Data) {
|
|
|
|
int t = 0;
|
|
|
|
for (int i = 0; i < 3; i++)
|
|
|
|
t = (t << 8) | *(*Data)++;
|
|
|
|
Length -= 3;
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
return AOT_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint8_t *cCiSession::GetData(const uint8_t *Data, int &Length)
|
|
|
|
{
|
|
|
|
Data = GetLength(Data, Length);
|
|
|
|
return Length ? Data : NULL;
|
|
|
|
}
|
|
|
|
|
2007-01-07 14:46:14 +01:00
|
|
|
void cCiSession::SendData(int Tag, int Length, const uint8_t *Data)
|
2003-01-06 14:44:27 +01:00
|
|
|
{
|
2017-05-18 09:05:46 +02:00
|
|
|
uint8_t buffer[MAX_TPDU_SIZE];
|
2003-01-06 14:44:27 +01:00
|
|
|
uint8_t *p = buffer;
|
|
|
|
*p++ = ST_SESSION_NUMBER;
|
|
|
|
*p++ = 0x02;
|
|
|
|
*p++ = (sessionId >> 8) & 0xFF;
|
|
|
|
*p++ = sessionId & 0xFF;
|
|
|
|
*p++ = (Tag >> 16) & 0xFF;
|
|
|
|
*p++ = (Tag >> 8) & 0xFF;
|
|
|
|
*p++ = Tag & 0xFF;
|
|
|
|
p = SetLength(p, Length);
|
2003-01-11 11:15:19 +01:00
|
|
|
if (p - buffer + Length < int(sizeof(buffer))) {
|
2013-12-29 17:20:22 +01:00
|
|
|
if (Data)
|
|
|
|
memcpy(p, Data, Length);
|
2003-01-06 14:44:27 +01:00
|
|
|
p += Length;
|
2007-01-07 14:46:14 +01:00
|
|
|
tc->SendData(p - buffer, buffer);
|
2003-01-06 14:44:27 +01:00
|
|
|
}
|
2007-01-07 14:46:14 +01:00
|
|
|
else
|
2017-05-18 09:05:46 +02:00
|
|
|
esyslog("ERROR: CAM %d: data length (%d) exceeds buffer size", CamSlot()->SlotNumber(), Length);
|
|
|
|
}
|
|
|
|
|
|
|
|
cCamSlot *cCiSession::CamSlot(void)
|
|
|
|
{
|
|
|
|
return Tc()->CamSlot();
|
2003-01-06 14:44:27 +01:00
|
|
|
}
|
|
|
|
|
2007-01-07 14:46:14 +01:00
|
|
|
void cCiSession::Process(int Length, const uint8_t *Data)
|
2003-01-06 14:44:27 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2007-01-07 14:46:14 +01:00
|
|
|
// --- cCiResourceManager ----------------------------------------------------
|
2003-01-06 14:44:27 +01:00
|
|
|
|
|
|
|
class cCiResourceManager : public cCiSession {
|
|
|
|
private:
|
|
|
|
int state;
|
|
|
|
public:
|
2006-08-12 10:26:43 +02:00
|
|
|
cCiResourceManager(uint16_t SessionId, cCiTransportConnection *Tc);
|
2007-01-07 14:46:14 +01:00
|
|
|
virtual void Process(int Length = 0, const uint8_t *Data = NULL);
|
2003-01-06 14:44:27 +01:00
|
|
|
};
|
|
|
|
|
2006-08-12 10:26:43 +02:00
|
|
|
cCiResourceManager::cCiResourceManager(uint16_t SessionId, cCiTransportConnection *Tc)
|
2003-01-06 14:44:27 +01:00
|
|
|
:cCiSession(SessionId, RI_RESOURCE_MANAGER, Tc)
|
|
|
|
{
|
2017-05-18 09:05:46 +02:00
|
|
|
dbgprotocol("Slot %d: new Resource Manager (session id %d)\n", CamSlot()->SlotNumber(), SessionId);
|
2003-01-06 14:44:27 +01:00
|
|
|
state = 0;
|
|
|
|
}
|
|
|
|
|
2007-01-07 14:46:14 +01:00
|
|
|
void cCiResourceManager::Process(int Length, const uint8_t *Data)
|
2003-01-06 14:44:27 +01:00
|
|
|
{
|
|
|
|
if (Data) {
|
|
|
|
int Tag = GetTag(Length, &Data);
|
|
|
|
switch (Tag) {
|
|
|
|
case AOT_PROFILE_ENQ: {
|
2017-05-18 09:05:46 +02:00
|
|
|
dbgprotocol("Slot %d: <== Profile Enquiry (%d)\n", CamSlot()->SlotNumber(), SessionId());
|
|
|
|
dbgprotocol("Slot %d: ==> Profile (%d)\n", CamSlot()->SlotNumber(), SessionId());
|
|
|
|
SendData(AOT_PROFILE, CiResourceHandlers.NumIds() * sizeof(uint32_t), (uint8_t*)CiResourceHandlers.Ids());
|
2003-01-06 14:44:27 +01:00
|
|
|
state = 3;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case AOT_PROFILE: {
|
2017-05-18 09:05:46 +02:00
|
|
|
dbgprotocol("Slot %d: <== Profile (%d)\n", CamSlot()->SlotNumber(), SessionId());
|
2003-01-06 14:44:27 +01:00
|
|
|
if (state == 1) {
|
|
|
|
int l = 0;
|
|
|
|
const uint8_t *d = GetData(Data, l);
|
|
|
|
if (l > 0 && d)
|
2017-05-18 09:05:46 +02:00
|
|
|
esyslog("ERROR: CAM %d: resource manager: unexpected data", CamSlot()->SlotNumber());
|
|
|
|
dbgprotocol("Slot %d: ==> Profile Change (%d)\n", CamSlot()->SlotNumber(), SessionId());
|
2003-01-06 14:44:27 +01:00
|
|
|
SendData(AOT_PROFILE_CHANGE);
|
|
|
|
state = 2;
|
|
|
|
}
|
|
|
|
else {
|
2017-05-18 09:05:46 +02:00
|
|
|
esyslog("ERROR: CAM %d: resource manager: unexpected tag %06X in state %d", CamSlot()->SlotNumber(), Tag, state);
|
2003-01-06 14:44:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2017-05-18 09:05:46 +02:00
|
|
|
default: esyslog("ERROR: CAM %d: resource manager: unknown tag %06X", CamSlot()->SlotNumber(), Tag);
|
2003-01-06 14:44:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (state == 0) {
|
2017-05-18 09:05:46 +02:00
|
|
|
dbgprotocol("Slot %d: ==> Profile Enq (%d)\n", CamSlot()->SlotNumber(), SessionId());
|
2003-01-06 14:44:27 +01:00
|
|
|
SendData(AOT_PROFILE_ENQ);
|
|
|
|
state = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// --- cCiApplicationInformation ---------------------------------------------
|
|
|
|
|
2006-08-12 10:26:43 +02:00
|
|
|
cCiApplicationInformation::cCiApplicationInformation(uint16_t SessionId, cCiTransportConnection *Tc)
|
2003-01-06 14:44:27 +01:00
|
|
|
:cCiSession(SessionId, RI_APPLICATION_INFORMATION, Tc)
|
|
|
|
{
|
2017-05-18 09:05:46 +02:00
|
|
|
dbgprotocol("Slot %d: new Application Information (session id %d)\n", CamSlot()->SlotNumber(), SessionId);
|
2003-01-06 14:44:27 +01:00
|
|
|
state = 0;
|
|
|
|
menuString = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
cCiApplicationInformation::~cCiApplicationInformation()
|
|
|
|
{
|
|
|
|
free(menuString);
|
|
|
|
}
|
|
|
|
|
2007-01-07 14:46:14 +01:00
|
|
|
void cCiApplicationInformation::Process(int Length, const uint8_t *Data)
|
2003-01-06 14:44:27 +01:00
|
|
|
{
|
|
|
|
if (Data) {
|
|
|
|
int Tag = GetTag(Length, &Data);
|
|
|
|
switch (Tag) {
|
|
|
|
case AOT_APPLICATION_INFO: {
|
2017-05-18 09:05:46 +02:00
|
|
|
dbgprotocol("Slot %d: <== Application Info (%d)\n", CamSlot()->SlotNumber(), SessionId());
|
2003-01-06 14:44:27 +01:00
|
|
|
int l = 0;
|
|
|
|
const uint8_t *d = GetData(Data, l);
|
|
|
|
if ((l -= 1) < 0) break;
|
|
|
|
applicationType = *d++;
|
|
|
|
if ((l -= 2) < 0) break;
|
2003-12-22 15:59:18 +01:00
|
|
|
applicationManufacturer = ntohs(get_unaligned((uint16_t *)d));
|
2003-01-06 14:44:27 +01:00
|
|
|
d += 2;
|
|
|
|
if ((l -= 2) < 0) break;
|
2003-12-22 15:59:18 +01:00
|
|
|
manufacturerCode = ntohs(get_unaligned((uint16_t *)d));
|
2003-01-06 14:44:27 +01:00
|
|
|
d += 2;
|
|
|
|
free(menuString);
|
|
|
|
menuString = GetString(l, &d);
|
2017-05-18 09:05:46 +02:00
|
|
|
isyslog("CAM %d: %s, %02X, %04X, %04X", CamSlot()->SlotNumber(), menuString, applicationType, applicationManufacturer, manufacturerCode);
|
2003-01-06 14:44:27 +01:00
|
|
|
state = 2;
|
2007-01-07 14:46:14 +01:00
|
|
|
}
|
2003-01-06 14:44:27 +01:00
|
|
|
break;
|
2017-05-18 09:05:46 +02:00
|
|
|
default: esyslog("ERROR: CAM %d: application information: unknown tag %06X", CamSlot()->SlotNumber(), Tag);
|
2003-01-06 14:44:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (state == 0) {
|
2017-05-18 09:05:46 +02:00
|
|
|
dbgprotocol("Slot %d: ==> Application Info Enq (%d)\n", CamSlot()->SlotNumber(), SessionId());
|
2003-01-06 14:44:27 +01:00
|
|
|
SendData(AOT_APPLICATION_INFO_ENQ);
|
|
|
|
state = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cCiApplicationInformation::EnterMenu(void)
|
|
|
|
{
|
2007-01-07 14:46:14 +01:00
|
|
|
if (state == 2) {
|
2017-05-18 09:05:46 +02:00
|
|
|
dbgprotocol("Slot %d: ==> Enter Menu (%d)\n", CamSlot()->SlotNumber(), SessionId());
|
2003-01-06 14:44:27 +01:00
|
|
|
SendData(AOT_ENTER_MENU);
|
2007-01-07 14:46:14 +01:00
|
|
|
return true;
|
2003-01-06 14:44:27 +01:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-11-26 13:39:47 +01:00
|
|
|
// --- cCiCaPmt --------------------------------------------------------------
|
|
|
|
|
2009-08-15 22:19:42 +02:00
|
|
|
#define MAXCASYSTEMIDS 64
|
|
|
|
|
2005-11-26 13:39:47 +01:00
|
|
|
// Ca Pmt List Management:
|
|
|
|
|
|
|
|
#define CPLM_MORE 0x00
|
|
|
|
#define CPLM_FIRST 0x01
|
|
|
|
#define CPLM_LAST 0x02
|
|
|
|
#define CPLM_ONLY 0x03
|
|
|
|
#define CPLM_ADD 0x04
|
|
|
|
#define CPLM_UPDATE 0x05
|
|
|
|
|
|
|
|
// Ca Pmt Cmd Ids:
|
|
|
|
|
|
|
|
#define CPCI_OK_DESCRAMBLING 0x01
|
|
|
|
#define CPCI_OK_MMI 0x02
|
|
|
|
#define CPCI_QUERY 0x03
|
|
|
|
#define CPCI_NOT_SELECTED 0x04
|
|
|
|
|
2014-01-01 12:37:22 +01:00
|
|
|
class cCiCaPmt {
|
2005-11-26 13:39:47 +01:00
|
|
|
friend class cCiConditionalAccessSupport;
|
|
|
|
private:
|
|
|
|
uint8_t cmdId;
|
|
|
|
int esInfoLengthPos;
|
2016-12-23 14:08:14 +01:00
|
|
|
cDynamicBuffer caDescriptors;
|
|
|
|
cDynamicBuffer capmt;
|
2009-08-15 22:19:42 +02:00
|
|
|
int source;
|
|
|
|
int transponder;
|
|
|
|
int programNumber;
|
|
|
|
int caSystemIds[MAXCASYSTEMIDS + 1]; // list is zero terminated!
|
2005-11-26 13:39:47 +01:00
|
|
|
void AddCaDescriptors(int Length, const uint8_t *Data);
|
|
|
|
public:
|
2007-01-07 14:46:14 +01:00
|
|
|
cCiCaPmt(uint8_t CmdId, int Source, int Transponder, int ProgramNumber, const int *CaSystemIds);
|
|
|
|
uint8_t CmdId(void) { return cmdId; }
|
2005-11-26 13:39:47 +01:00
|
|
|
void SetListManagement(uint8_t ListManagement);
|
2016-12-23 14:08:14 +01:00
|
|
|
uint8_t ListManagement(void) { return capmt.Get(0); }
|
2005-11-26 13:39:47 +01:00
|
|
|
void AddPid(int Pid, uint8_t StreamType);
|
2017-03-18 16:33:59 +01:00
|
|
|
void MtdMapPids(cMtdMapper *MtdMapper);
|
2005-11-26 13:39:47 +01:00
|
|
|
};
|
|
|
|
|
2007-01-07 14:46:14 +01:00
|
|
|
cCiCaPmt::cCiCaPmt(uint8_t CmdId, int Source, int Transponder, int ProgramNumber, const int *CaSystemIds)
|
2005-11-26 13:39:47 +01:00
|
|
|
{
|
|
|
|
cmdId = CmdId;
|
2009-08-15 22:19:42 +02:00
|
|
|
source = Source;
|
|
|
|
transponder = Transponder;
|
|
|
|
programNumber = ProgramNumber;
|
|
|
|
int i = 0;
|
|
|
|
if (CaSystemIds) {
|
|
|
|
for (; CaSystemIds[i]; i++)
|
|
|
|
caSystemIds[i] = CaSystemIds[i];
|
|
|
|
}
|
|
|
|
caSystemIds[i] = 0;
|
2016-12-23 14:08:14 +01:00
|
|
|
GetCaDescriptors(source, transponder, programNumber, caSystemIds, caDescriptors, 0);
|
|
|
|
capmt.Append(CPLM_ONLY);
|
|
|
|
capmt.Append((ProgramNumber >> 8) & 0xFF);
|
|
|
|
capmt.Append( ProgramNumber & 0xFF);
|
|
|
|
capmt.Append(0x01); // version_number, current_next_indicator - apparently vn doesn't matter, but cni must be 1
|
|
|
|
esInfoLengthPos = capmt.Length();
|
|
|
|
capmt.Append(0x00); // program_info_length H (at program level)
|
|
|
|
capmt.Append(0x00); // program_info_length L
|
|
|
|
AddCaDescriptors(caDescriptors.Length(), caDescriptors.Data());
|
2005-11-26 13:39:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void cCiCaPmt::SetListManagement(uint8_t ListManagement)
|
|
|
|
{
|
2016-12-23 14:08:14 +01:00
|
|
|
capmt.Set(0, ListManagement);
|
2005-11-26 13:39:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void cCiCaPmt::AddPid(int Pid, uint8_t StreamType)
|
|
|
|
{
|
|
|
|
if (Pid) {
|
2016-12-23 14:08:14 +01:00
|
|
|
GetCaDescriptors(source, transponder, programNumber, caSystemIds, caDescriptors, Pid);
|
|
|
|
capmt.Append(StreamType);
|
|
|
|
capmt.Append((Pid >> 8) & 0xFF);
|
|
|
|
capmt.Append( Pid & 0xFF);
|
|
|
|
esInfoLengthPos = capmt.Length();
|
|
|
|
capmt.Append(0x00); // ES_info_length H (at ES level)
|
|
|
|
capmt.Append(0x00); // ES_info_length L
|
|
|
|
AddCaDescriptors(caDescriptors.Length(), caDescriptors.Data());
|
2005-11-26 13:39:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void cCiCaPmt::AddCaDescriptors(int Length, const uint8_t *Data)
|
|
|
|
{
|
|
|
|
if (esInfoLengthPos) {
|
2016-12-23 14:08:14 +01:00
|
|
|
if (Length || cmdId == CPCI_QUERY) {
|
|
|
|
capmt.Append(cmdId);
|
|
|
|
capmt.Append(Data, Length);
|
|
|
|
int l = capmt.Length() - esInfoLengthPos - 2;
|
|
|
|
capmt.Set(esInfoLengthPos, (l >> 8) & 0xFF);
|
|
|
|
capmt.Set(esInfoLengthPos + 1, l & 0xFF);
|
2005-11-26 13:39:47 +01:00
|
|
|
}
|
|
|
|
esInfoLengthPos = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
esyslog("ERROR: adding CA descriptor without Pid!");
|
|
|
|
}
|
|
|
|
|
2017-03-18 16:33:59 +01:00
|
|
|
static int MtdMapCaDescriptor(uchar *p, cMtdMapper *MtdMapper)
|
|
|
|
{
|
|
|
|
// See pat.c: cCaDescriptor::cCaDescriptor() for the layout of the data!
|
|
|
|
if (*p == SI::CaDescriptorTag) {
|
|
|
|
int l = *++p;
|
|
|
|
if (l >= 4) {
|
|
|
|
MtdMapPid(p + 3, MtdMapper);
|
|
|
|
return l + 2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
esyslog("ERROR: wrong length (%d) in MtdMapCaDescriptor()", l);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
esyslog("ERROR: wrong tag (%d) in MtdMapCaDescriptor()", *p);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int MtdMapCaDescriptors(uchar *p, cMtdMapper *MtdMapper)
|
|
|
|
{
|
|
|
|
int Length = p[0] * 256 + p[1];
|
|
|
|
if (Length >= 3) {
|
|
|
|
p += 3;
|
|
|
|
int m = Length - 1;
|
|
|
|
while (m > 0) {
|
|
|
|
int l = MtdMapCaDescriptor(p, MtdMapper);
|
|
|
|
if (l > 0) {
|
|
|
|
p += l;
|
|
|
|
m -= l;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Length + 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int MtdMapStream(uchar *p, cMtdMapper *MtdMapper)
|
|
|
|
{
|
|
|
|
// See ci.c: cCiCaPmt::AddPid() for the layout of the data!
|
|
|
|
MtdMapPid(p + 1, MtdMapper);
|
|
|
|
int l = MtdMapCaDescriptors(p + 3, MtdMapper);
|
|
|
|
if (l > 0)
|
|
|
|
return l + 3;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int MtdMapStreams(uchar *p, cMtdMapper *MtdMapper, int Length)
|
|
|
|
{
|
|
|
|
int m = Length;
|
|
|
|
while (m >= 5) {
|
|
|
|
int l = MtdMapStream(p, MtdMapper);
|
|
|
|
if (l > 0) {
|
|
|
|
p += l;
|
|
|
|
m -= l;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return Length;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cCiCaPmt::MtdMapPids(cMtdMapper *MtdMapper)
|
|
|
|
{
|
|
|
|
uchar *p = capmt.Data();
|
|
|
|
int m = capmt.Length();
|
|
|
|
if (m >= 3) {
|
|
|
|
MtdMapSid(p + 1, MtdMapper);
|
|
|
|
p += 4;
|
|
|
|
m -= 4;
|
|
|
|
if (m >= 2) {
|
|
|
|
int l = MtdMapCaDescriptors(p, MtdMapper);
|
|
|
|
if (l >= 0) {
|
|
|
|
p += l;
|
|
|
|
m -= l;
|
|
|
|
MtdMapStreams(p, MtdMapper, m);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-01-06 14:44:27 +01:00
|
|
|
// --- cCiConditionalAccessSupport -------------------------------------------
|
|
|
|
|
2005-11-26 13:39:47 +01:00
|
|
|
// CA Enable Ids:
|
|
|
|
|
|
|
|
#define CAEI_POSSIBLE 0x01
|
|
|
|
#define CAEI_POSSIBLE_COND_PURCHASE 0x02
|
|
|
|
#define CAEI_POSSIBLE_COND_TECHNICAL 0x03
|
|
|
|
#define CAEI_NOT_POSSIBLE_ENTITLEMENT 0x71
|
|
|
|
#define CAEI_NOT_POSSIBLE_TECHNICAL 0x73
|
|
|
|
|
|
|
|
#define CA_ENABLE_FLAG 0x80
|
|
|
|
|
|
|
|
#define CA_ENABLE(x) (((x) & CA_ENABLE_FLAG) ? (x) & ~CA_ENABLE_FLAG : 0)
|
|
|
|
|
2017-06-19 12:16:40 +02:00
|
|
|
#define QUERY_WAIT_TIME 500 // ms to wait before sending a query
|
2007-01-07 14:46:14 +01:00
|
|
|
#define QUERY_REPLY_TIMEOUT 2000 // ms to wait for a reply to a query
|
2017-06-19 12:16:40 +02:00
|
|
|
#define QUERY_RETRIES 6 // max. number of retries to check if there is a reply to a query
|
2007-01-07 14:46:14 +01:00
|
|
|
|
2003-01-06 14:44:27 +01:00
|
|
|
class cCiConditionalAccessSupport : public cCiSession {
|
|
|
|
private:
|
|
|
|
int state;
|
2003-04-18 12:48:49 +02:00
|
|
|
int numCaSystemIds;
|
2007-01-07 14:46:14 +01:00
|
|
|
int caSystemIds[MAXCASYSTEMIDS + 1]; // list is zero terminated!
|
|
|
|
bool repliesToQuery;
|
|
|
|
cTimeMs timer;
|
2017-06-19 12:16:40 +02:00
|
|
|
int numRetries;
|
2003-01-06 14:44:27 +01:00
|
|
|
public:
|
2006-08-12 10:26:43 +02:00
|
|
|
cCiConditionalAccessSupport(uint16_t SessionId, cCiTransportConnection *Tc);
|
2007-01-07 14:46:14 +01:00
|
|
|
virtual void Process(int Length = 0, const uint8_t *Data = NULL);
|
|
|
|
const int *GetCaSystemIds(void) { return caSystemIds; }
|
|
|
|
void SendPMT(cCiCaPmt *CaPmt);
|
|
|
|
bool RepliesToQuery(void) { return repliesToQuery; }
|
|
|
|
bool Ready(void) { return state >= 4; }
|
|
|
|
bool ReceivedReply(void) { return state >= 5; }
|
|
|
|
bool CanDecrypt(void) { return state == 6; }
|
2003-01-06 14:44:27 +01:00
|
|
|
};
|
|
|
|
|
2006-08-12 10:26:43 +02:00
|
|
|
cCiConditionalAccessSupport::cCiConditionalAccessSupport(uint16_t SessionId, cCiTransportConnection *Tc)
|
2003-01-06 14:44:27 +01:00
|
|
|
:cCiSession(SessionId, RI_CONDITIONAL_ACCESS_SUPPORT, Tc)
|
|
|
|
{
|
2017-05-18 09:05:46 +02:00
|
|
|
dbgprotocol("Slot %d: new Conditional Access Support (session id %d)\n", CamSlot()->SlotNumber(), SessionId);
|
2005-11-26 13:39:47 +01:00
|
|
|
state = 0; // inactive
|
2003-04-18 12:48:49 +02:00
|
|
|
caSystemIds[numCaSystemIds = 0] = 0;
|
2007-01-07 14:46:14 +01:00
|
|
|
repliesToQuery = false;
|
2017-06-19 12:16:40 +02:00
|
|
|
numRetries = 0;
|
2003-01-06 14:44:27 +01:00
|
|
|
}
|
|
|
|
|
2007-01-07 14:46:14 +01:00
|
|
|
void cCiConditionalAccessSupport::Process(int Length, const uint8_t *Data)
|
2003-01-06 14:44:27 +01:00
|
|
|
{
|
2003-02-02 15:49:52 +01:00
|
|
|
if (Data) {
|
|
|
|
int Tag = GetTag(Length, &Data);
|
|
|
|
switch (Tag) {
|
|
|
|
case AOT_CA_INFO: {
|
2017-05-18 09:05:46 +02:00
|
|
|
dbgprotocol("Slot %d: <== Ca Info (%d)", CamSlot()->SlotNumber(), SessionId());
|
2014-01-15 10:21:48 +01:00
|
|
|
cString Ids;
|
2006-08-20 11:54:56 +02:00
|
|
|
numCaSystemIds = 0;
|
2003-02-02 15:49:52 +01:00
|
|
|
int l = 0;
|
|
|
|
const uint8_t *d = GetData(Data, l);
|
|
|
|
while (l > 1) {
|
2007-01-07 14:46:14 +01:00
|
|
|
uint16_t id = ((uint16_t)(*d) << 8) | *(d + 1);
|
2014-01-15 10:21:48 +01:00
|
|
|
Ids = cString::sprintf("%s %04X", *Ids ? *Ids : "", id);
|
2003-04-18 12:48:49 +02:00
|
|
|
dbgprotocol(" %04X", id);
|
2003-02-02 15:49:52 +01:00
|
|
|
d += 2;
|
|
|
|
l -= 2;
|
2006-08-20 11:54:56 +02:00
|
|
|
if (numCaSystemIds < MAXCASYSTEMIDS)
|
2003-04-18 12:48:49 +02:00
|
|
|
caSystemIds[numCaSystemIds++] = id;
|
2006-08-20 11:54:56 +02:00
|
|
|
else {
|
2017-05-18 09:05:46 +02:00
|
|
|
esyslog("ERROR: CAM %d: too many CA system IDs!", CamSlot()->SlotNumber());
|
2006-08-20 11:54:56 +02:00
|
|
|
break;
|
|
|
|
}
|
2003-02-02 15:49:52 +01:00
|
|
|
}
|
2006-08-20 11:54:56 +02:00
|
|
|
caSystemIds[numCaSystemIds] = 0;
|
2003-02-02 15:49:52 +01:00
|
|
|
dbgprotocol("\n");
|
2007-01-07 14:46:14 +01:00
|
|
|
if (state == 1) {
|
2017-06-19 12:16:40 +02:00
|
|
|
timer.Set(0);
|
|
|
|
numRetries = QUERY_RETRIES;
|
2007-01-07 14:46:14 +01:00
|
|
|
state = 2; // got ca info
|
|
|
|
}
|
2017-05-18 09:05:46 +02:00
|
|
|
dsyslog("CAM %d: system ids:%s", CamSlot()->SlotNumber(), *Ids ? *Ids : " none");
|
2003-02-02 15:49:52 +01:00
|
|
|
}
|
2005-11-26 13:39:47 +01:00
|
|
|
break;
|
|
|
|
case AOT_CA_PMT_REPLY: {
|
2017-05-18 09:05:46 +02:00
|
|
|
dbgprotocol("Slot %d: <== Ca Pmt Reply (%d)", CamSlot()->SlotNumber(), SessionId());
|
2007-01-07 14:46:14 +01:00
|
|
|
if (!repliesToQuery) {
|
2018-02-03 12:59:56 +01:00
|
|
|
if (CamSlot()->IsMasterSlot())
|
|
|
|
dsyslog("CAM %d: replies to QUERY - multi channel decryption (MCD) possible", CamSlot()->SlotNumber());
|
2007-01-07 14:46:14 +01:00
|
|
|
repliesToQuery = true;
|
2017-05-18 09:05:46 +02:00
|
|
|
if (CamSlot()->MtdAvailable()) {
|
2018-02-03 12:59:56 +01:00
|
|
|
if (CamSlot()->IsMasterSlot())
|
|
|
|
dsyslog("CAM %d: supports multi transponder decryption (MTD)", CamSlot()->SlotNumber());
|
2017-05-18 09:05:46 +02:00
|
|
|
CamSlot()->MtdActivate(true);
|
2017-03-18 16:33:59 +01:00
|
|
|
}
|
2007-01-07 14:46:14 +01:00
|
|
|
}
|
|
|
|
state = 5; // got ca pmt reply
|
2005-11-26 13:39:47 +01:00
|
|
|
int l = 0;
|
|
|
|
const uint8_t *d = GetData(Data, l);
|
|
|
|
if (l > 1) {
|
2007-01-07 14:46:14 +01:00
|
|
|
uint16_t pnr = ((uint16_t)(*d) << 8) | *(d + 1);
|
2005-11-26 13:39:47 +01:00
|
|
|
dbgprotocol(" %d", pnr);
|
|
|
|
d += 2;
|
|
|
|
l -= 2;
|
|
|
|
if (l > 0) {
|
|
|
|
dbgprotocol(" %02X", *d);
|
|
|
|
d += 1;
|
|
|
|
l -= 1;
|
|
|
|
if (l > 0) {
|
|
|
|
if (l % 3 == 0 && l > 1) {
|
|
|
|
// The EN50221 standard defines that the next byte is supposed
|
|
|
|
// to be the CA_enable value at programme level. However, there are
|
|
|
|
// CAMs (for instance the AlphaCrypt with firmware <= 3.05) that
|
|
|
|
// insert a two byte length field here.
|
|
|
|
// This is a workaround to skip this length field:
|
2007-01-07 14:46:14 +01:00
|
|
|
uint16_t len = ((uint16_t)(*d) << 8) | *(d + 1);
|
2005-11-26 13:39:47 +01:00
|
|
|
if (len == l - 2) {
|
|
|
|
d += 2;
|
|
|
|
l -= 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
unsigned char caepl = *d;
|
|
|
|
dbgprotocol(" %02X", caepl);
|
|
|
|
d += 1;
|
|
|
|
l -= 1;
|
|
|
|
bool ok = true;
|
|
|
|
if (l <= 2)
|
|
|
|
ok = CA_ENABLE(caepl) == CAEI_POSSIBLE;
|
|
|
|
while (l > 2) {
|
2007-01-07 14:46:14 +01:00
|
|
|
uint16_t pid = ((uint16_t)(*d) << 8) | *(d + 1);
|
2005-11-26 13:39:47 +01:00
|
|
|
unsigned char caees = *(d + 2);
|
|
|
|
dbgprotocol(" %d=%02X", pid, caees);
|
|
|
|
d += 3;
|
|
|
|
l -= 3;
|
|
|
|
if (CA_ENABLE(caees) != CAEI_POSSIBLE)
|
|
|
|
ok = false;
|
|
|
|
}
|
|
|
|
if (ok)
|
2007-01-07 14:46:14 +01:00
|
|
|
state = 6; // descrambling possible
|
2005-11-26 13:39:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dbgprotocol("\n");
|
|
|
|
}
|
2003-02-02 15:49:52 +01:00
|
|
|
break;
|
2017-05-18 09:05:46 +02:00
|
|
|
default: esyslog("ERROR: CAM %d: conditional access support: unknown tag %06X", CamSlot()->SlotNumber(), Tag);
|
2003-02-02 15:49:52 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (state == 0) {
|
2017-05-18 09:05:46 +02:00
|
|
|
dbgprotocol("Slot %d: ==> Ca Info Enq (%d)\n", CamSlot()->SlotNumber(), SessionId());
|
2003-01-06 14:44:27 +01:00
|
|
|
SendData(AOT_CA_INFO_ENQ);
|
2005-11-26 13:39:47 +01:00
|
|
|
state = 1; // enquired ca info
|
2003-01-06 14:44:27 +01:00
|
|
|
}
|
2017-06-19 12:16:40 +02:00
|
|
|
else if ((state == 2 || state == 3) && timer.TimedOut()) {
|
|
|
|
if (numRetries-- > 0) {
|
|
|
|
cCiCaPmt CaPmt(CPCI_QUERY, 0, 0, 0, NULL);
|
|
|
|
SendPMT(&CaPmt);
|
|
|
|
timer.Set(QUERY_WAIT_TIME);
|
|
|
|
state = 3; // waiting for reply
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
dsyslog("CAM %d: doesn't reply to QUERY - only a single channel can be decrypted", CamSlot()->SlotNumber());
|
2018-09-23 10:21:12 +02:00
|
|
|
CamSlot()->MtdActivate(false);
|
2017-06-19 12:16:40 +02:00
|
|
|
state = 4; // normal operation
|
|
|
|
}
|
2007-01-07 14:46:14 +01:00
|
|
|
}
|
2003-01-06 14:44:27 +01:00
|
|
|
}
|
|
|
|
|
2007-01-07 14:46:14 +01:00
|
|
|
void cCiConditionalAccessSupport::SendPMT(cCiCaPmt *CaPmt)
|
2003-01-06 14:44:27 +01:00
|
|
|
{
|
2005-11-26 13:39:47 +01:00
|
|
|
if (CaPmt && state >= 2) {
|
2017-05-18 09:05:46 +02:00
|
|
|
dbgprotocol("Slot %d: ==> Ca Pmt (%d) %d %d\n", CamSlot()->SlotNumber(), SessionId(), CaPmt->ListManagement(), CaPmt->CmdId());
|
2016-12-23 14:08:14 +01:00
|
|
|
SendData(AOT_CA_PMT, CaPmt->capmt.Length(), CaPmt->capmt.Data());
|
2007-01-07 14:46:14 +01:00
|
|
|
state = 4; // sent ca pmt
|
2003-01-06 14:44:27 +01:00
|
|
|
}
|
2005-11-26 13:39:47 +01:00
|
|
|
}
|
|
|
|
|
2017-05-12 09:17:39 +02:00
|
|
|
// --- cCiHostControl --------------------------------------------------------
|
|
|
|
|
|
|
|
class cCiHostControl : public cCiSession {
|
|
|
|
public:
|
|
|
|
cCiHostControl(uint16_t SessionId, cCiTransportConnection *Tc);
|
|
|
|
virtual void Process(int Length = 0, const uint8_t *Data = NULL);
|
|
|
|
};
|
|
|
|
|
|
|
|
cCiHostControl::cCiHostControl(uint16_t SessionId, cCiTransportConnection* Tc)
|
|
|
|
:cCiSession(SessionId, RI_HOST_CONTROL, Tc)
|
|
|
|
{
|
2017-05-18 09:05:46 +02:00
|
|
|
dbgprotocol("Slot %d: new Host Control (session id %d)\n", CamSlot()->SlotNumber(), SessionId);
|
2017-05-12 09:17:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void cCiHostControl::Process(int Length, const uint8_t* Data)
|
|
|
|
{
|
|
|
|
if (Data) {
|
|
|
|
int Tag = GetTag(Length, &Data);
|
|
|
|
switch (Tag) {
|
|
|
|
case AOT_TUNE:
|
2017-05-18 09:05:46 +02:00
|
|
|
dbgprotocol("Slot %d: <== Host Control Tune (%d)\n", CamSlot()->SlotNumber(), SessionId());
|
2017-05-12 09:17:39 +02:00
|
|
|
break;
|
|
|
|
case AOT_REPLACE:
|
2017-05-18 09:05:46 +02:00
|
|
|
dbgprotocol("Slot %d: <== Host Control Replace (%d)\n", CamSlot()->SlotNumber(), SessionId());
|
2017-05-12 09:17:39 +02:00
|
|
|
break;
|
|
|
|
case AOT_CLEAR_REPLACE:
|
2017-05-18 09:05:46 +02:00
|
|
|
dbgprotocol("Slot %d: <== Host Control Clear Replace (%d)\n", CamSlot()->SlotNumber(), SessionId());
|
2017-05-12 09:17:39 +02:00
|
|
|
break;
|
2017-05-18 09:05:46 +02:00
|
|
|
default: esyslog("ERROR: CAM %d: Host Control: unknown tag %06X", CamSlot()->SlotNumber(), Tag);
|
2017-05-12 09:17:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-01-06 14:44:27 +01:00
|
|
|
// --- cCiDateTime -----------------------------------------------------------
|
|
|
|
|
|
|
|
class cCiDateTime : public cCiSession {
|
|
|
|
private:
|
|
|
|
int interval;
|
|
|
|
time_t lastTime;
|
2007-01-07 14:46:14 +01:00
|
|
|
void SendDateTime(void);
|
2003-01-06 14:44:27 +01:00
|
|
|
public:
|
2006-08-12 10:26:43 +02:00
|
|
|
cCiDateTime(uint16_t SessionId, cCiTransportConnection *Tc);
|
2007-01-07 14:46:14 +01:00
|
|
|
virtual void Process(int Length = 0, const uint8_t *Data = NULL);
|
2003-01-06 14:44:27 +01:00
|
|
|
};
|
|
|
|
|
2006-08-12 10:26:43 +02:00
|
|
|
cCiDateTime::cCiDateTime(uint16_t SessionId, cCiTransportConnection *Tc)
|
2003-01-06 14:44:27 +01:00
|
|
|
:cCiSession(SessionId, RI_DATE_TIME, Tc)
|
|
|
|
{
|
|
|
|
interval = 0;
|
|
|
|
lastTime = 0;
|
2017-05-18 09:05:46 +02:00
|
|
|
dbgprotocol("Slot %d: new Date Time (session id %d)\n", CamSlot()->SlotNumber(), SessionId);
|
2003-01-06 14:44:27 +01:00
|
|
|
}
|
|
|
|
|
2007-01-07 14:46:14 +01:00
|
|
|
void cCiDateTime::SendDateTime(void)
|
2003-01-06 14:44:27 +01:00
|
|
|
{
|
|
|
|
time_t t = time(NULL);
|
|
|
|
struct tm tm_gmt;
|
|
|
|
struct tm tm_loc;
|
|
|
|
if (gmtime_r(&t, &tm_gmt) && localtime_r(&t, &tm_loc)) {
|
|
|
|
int Y = tm_gmt.tm_year;
|
|
|
|
int M = tm_gmt.tm_mon + 1;
|
|
|
|
int D = tm_gmt.tm_mday;
|
|
|
|
int L = (M == 1 || M == 2) ? 1 : 0;
|
|
|
|
int MJD = 14956 + D + int((Y - L) * 365.25) + int((M + 1 + L * 12) * 30.6001);
|
2012-10-07 11:13:18 +02:00
|
|
|
#define DEC2BCD(d) uint8_t(((d / 10) << 4) + (d % 10))
|
2014-01-22 09:41:08 +01:00
|
|
|
#pragma pack(1)
|
2007-01-07 14:46:14 +01:00
|
|
|
struct tTime { uint16_t mjd; uint8_t h, m, s; short offset; };
|
2014-01-22 09:41:08 +01:00
|
|
|
#pragma pack()
|
2014-02-25 10:04:07 +01:00
|
|
|
tTime T = { mjd : htons(MJD), h : DEC2BCD(tm_gmt.tm_hour), m : DEC2BCD(tm_gmt.tm_min), s : DEC2BCD(tm_gmt.tm_sec), offset : short(htons(tm_loc.tm_gmtoff / 60)) };
|
2007-01-07 14:46:14 +01:00
|
|
|
bool OldDumpTPDUDataTransfer = DumpTPDUDataTransfer;
|
|
|
|
DumpTPDUDataTransfer &= DumpDateTime;
|
|
|
|
if (DumpDateTime)
|
2017-05-18 09:05:46 +02:00
|
|
|
dbgprotocol("Slot %d: ==> Date Time (%d)\n", CamSlot()->SlotNumber(), SessionId());
|
2003-01-06 14:44:27 +01:00
|
|
|
SendData(AOT_DATE_TIME, 7, (uint8_t*)&T);
|
2007-01-07 14:46:14 +01:00
|
|
|
DumpTPDUDataTransfer = OldDumpTPDUDataTransfer;
|
2003-01-06 14:44:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-07 14:46:14 +01:00
|
|
|
void cCiDateTime::Process(int Length, const uint8_t *Data)
|
2003-01-06 14:44:27 +01:00
|
|
|
{
|
|
|
|
if (Data) {
|
|
|
|
int Tag = GetTag(Length, &Data);
|
|
|
|
switch (Tag) {
|
|
|
|
case AOT_DATE_TIME_ENQ: {
|
|
|
|
interval = 0;
|
|
|
|
int l = 0;
|
|
|
|
const uint8_t *d = GetData(Data, l);
|
|
|
|
if (l > 0)
|
|
|
|
interval = *d;
|
2017-05-18 09:05:46 +02:00
|
|
|
dbgprotocol("Slot %d: <== Date Time Enq (%d), interval = %d\n", CamSlot()->SlotNumber(), SessionId(), interval);
|
2003-01-06 14:44:27 +01:00
|
|
|
lastTime = time(NULL);
|
2007-01-07 14:46:14 +01:00
|
|
|
SendDateTime();
|
2003-01-06 14:44:27 +01:00
|
|
|
}
|
|
|
|
break;
|
2017-05-18 09:05:46 +02:00
|
|
|
default: esyslog("ERROR: CAM %d: date time: unknown tag %06X", CamSlot()->SlotNumber(), Tag);
|
2003-01-06 14:44:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (interval && time(NULL) - lastTime > interval) {
|
|
|
|
lastTime = time(NULL);
|
2007-01-07 14:46:14 +01:00
|
|
|
SendDateTime();
|
2003-01-06 14:44:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// --- cCiMMI ----------------------------------------------------------------
|
|
|
|
|
|
|
|
// Display Control Commands:
|
|
|
|
|
|
|
|
#define DCC_SET_MMI_MODE 0x01
|
|
|
|
#define DCC_DISPLAY_CHARACTER_TABLE_LIST 0x02
|
|
|
|
#define DCC_INPUT_CHARACTER_TABLE_LIST 0x03
|
|
|
|
#define DCC_OVERLAY_GRAPHICS_CHARACTERISTICS 0x04
|
|
|
|
#define DCC_FULL_SCREEN_GRAPHICS_CHARACTERISTICS 0x05
|
|
|
|
|
|
|
|
// MMI Modes:
|
|
|
|
|
|
|
|
#define MM_HIGH_LEVEL 0x01
|
|
|
|
#define MM_LOW_LEVEL_OVERLAY_GRAPHICS 0x02
|
|
|
|
#define MM_LOW_LEVEL_FULL_SCREEN_GRAPHICS 0x03
|
|
|
|
|
|
|
|
// Display Reply IDs:
|
|
|
|
|
|
|
|
#define DRI_MMI_MODE_ACK 0x01
|
|
|
|
#define DRI_LIST_DISPLAY_CHARACTER_TABLES 0x02
|
|
|
|
#define DRI_LIST_INPUT_CHARACTER_TABLES 0x03
|
|
|
|
#define DRI_LIST_GRAPHIC_OVERLAY_CHARACTERISTICS 0x04
|
|
|
|
#define DRI_LIST_FULL_SCREEN_GRAPHIC_CHARACTERISTICS 0x05
|
|
|
|
#define DRI_UNKNOWN_DISPLAY_CONTROL_CMD 0xF0
|
|
|
|
#define DRI_UNKNOWN_MMI_MODE 0xF1
|
|
|
|
#define DRI_UNKNOWN_CHARACTER_TABLE 0xF2
|
|
|
|
|
|
|
|
// Enquiry Flags:
|
|
|
|
|
|
|
|
#define EF_BLIND 0x01
|
|
|
|
|
|
|
|
// Answer IDs:
|
|
|
|
|
|
|
|
#define AI_CANCEL 0x00
|
|
|
|
#define AI_ANSWER 0x01
|
|
|
|
|
|
|
|
class cCiMMI : public cCiSession {
|
|
|
|
private:
|
|
|
|
char *GetText(int &Length, const uint8_t **Data);
|
2003-10-26 13:19:22 +01:00
|
|
|
cCiMenu *menu, *fetchedMenu;
|
|
|
|
cCiEnquiry *enquiry, *fetchedEnquiry;
|
2003-01-06 14:44:27 +01:00
|
|
|
public:
|
2006-08-12 10:26:43 +02:00
|
|
|
cCiMMI(uint16_t SessionId, cCiTransportConnection *Tc);
|
2003-01-06 14:44:27 +01:00
|
|
|
virtual ~cCiMMI();
|
2007-01-07 14:46:14 +01:00
|
|
|
virtual void Process(int Length = 0, const uint8_t *Data = NULL);
|
2003-04-20 09:53:08 +02:00
|
|
|
virtual bool HasUserIO(void) { return menu || enquiry; }
|
2003-10-26 13:19:22 +01:00
|
|
|
cCiMenu *Menu(bool Clear = false);
|
|
|
|
cCiEnquiry *Enquiry(bool Clear = false);
|
2007-01-07 14:46:14 +01:00
|
|
|
void SendMenuAnswer(uint8_t Selection);
|
2003-01-06 14:44:27 +01:00
|
|
|
bool SendAnswer(const char *Text);
|
2005-10-02 13:54:34 +02:00
|
|
|
bool SendCloseMMI(void);
|
2003-01-06 14:44:27 +01:00
|
|
|
};
|
|
|
|
|
2006-08-12 10:26:43 +02:00
|
|
|
cCiMMI::cCiMMI(uint16_t SessionId, cCiTransportConnection *Tc)
|
2003-01-06 14:44:27 +01:00
|
|
|
:cCiSession(SessionId, RI_MMI, Tc)
|
|
|
|
{
|
2017-05-18 09:05:46 +02:00
|
|
|
dbgprotocol("Slot %d: new MMI (session id %d)\n", CamSlot()->SlotNumber(), SessionId);
|
2003-10-26 13:19:22 +01:00
|
|
|
menu = fetchedMenu = NULL;
|
|
|
|
enquiry = fetchedEnquiry = NULL;
|
2003-01-06 14:44:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
cCiMMI::~cCiMMI()
|
|
|
|
{
|
2003-10-26 13:19:22 +01:00
|
|
|
if (fetchedMenu) {
|
2005-10-30 13:21:23 +01:00
|
|
|
cMutexLock MutexLock(fetchedMenu->mutex);
|
2003-10-26 13:19:22 +01:00
|
|
|
fetchedMenu->mmi = NULL;
|
|
|
|
}
|
2003-01-06 14:44:27 +01:00
|
|
|
delete menu;
|
2003-10-26 13:19:22 +01:00
|
|
|
if (fetchedEnquiry) {
|
2005-10-30 13:21:23 +01:00
|
|
|
cMutexLock MutexLock(fetchedEnquiry->mutex);
|
2003-10-26 13:19:22 +01:00
|
|
|
fetchedEnquiry->mmi = NULL;
|
|
|
|
}
|
2003-01-06 14:44:27 +01:00
|
|
|
delete enquiry;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *cCiMMI::GetText(int &Length, const uint8_t **Data)
|
|
|
|
///< Gets the text at Data.
|
2013-02-16 15:26:45 +01:00
|
|
|
///< Returns a pointer to a newly allocated string, or NULL in case of error.
|
2003-01-06 14:44:27 +01:00
|
|
|
///< Upon return Length and Data represent the remaining data after the text has been skipped.
|
|
|
|
{
|
|
|
|
int Tag = GetTag(Length, Data);
|
|
|
|
if (Tag == AOT_TEXT_LAST) {
|
|
|
|
char *s = GetString(Length, Data);
|
2017-05-18 09:05:46 +02:00
|
|
|
dbgprotocol("Slot %d: <== Text Last (%d) '%s'\n", CamSlot()->SlotNumber(), SessionId(), s);
|
2003-01-06 14:44:27 +01:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
else
|
2017-05-18 09:05:46 +02:00
|
|
|
esyslog("ERROR: CAM %d: MMI: unexpected text tag: %06X", CamSlot()->SlotNumber(), Tag);
|
2003-01-06 14:44:27 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-01-07 14:46:14 +01:00
|
|
|
void cCiMMI::Process(int Length, const uint8_t *Data)
|
2003-01-06 14:44:27 +01:00
|
|
|
{
|
|
|
|
if (Data) {
|
|
|
|
int Tag = GetTag(Length, &Data);
|
|
|
|
switch (Tag) {
|
|
|
|
case AOT_DISPLAY_CONTROL: {
|
2017-05-18 09:05:46 +02:00
|
|
|
dbgprotocol("Slot %d: <== Display Control (%d)\n", CamSlot()->SlotNumber(), SessionId());
|
2003-01-06 14:44:27 +01:00
|
|
|
int l = 0;
|
|
|
|
const uint8_t *d = GetData(Data, l);
|
|
|
|
if (l > 0) {
|
|
|
|
switch (*d) {
|
|
|
|
case DCC_SET_MMI_MODE:
|
|
|
|
if (l == 2 && *++d == MM_HIGH_LEVEL) {
|
|
|
|
struct tDisplayReply { uint8_t id; uint8_t mode; };
|
2014-02-25 10:04:07 +01:00
|
|
|
tDisplayReply dr = { id : DRI_MMI_MODE_ACK, mode : MM_HIGH_LEVEL };
|
2017-05-18 09:05:46 +02:00
|
|
|
dbgprotocol("Slot %d: ==> Display Reply (%d)\n", CamSlot()->SlotNumber(), SessionId());
|
2003-01-06 14:44:27 +01:00
|
|
|
SendData(AOT_DISPLAY_REPLY, 2, (uint8_t *)&dr);
|
|
|
|
}
|
|
|
|
break;
|
2017-05-18 09:05:46 +02:00
|
|
|
default: esyslog("ERROR: CAM %d: MMI: unsupported display control command %02X", CamSlot()->SlotNumber(), *d);
|
2003-01-06 14:44:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case AOT_LIST_LAST:
|
|
|
|
case AOT_MENU_LAST: {
|
2017-05-18 09:05:46 +02:00
|
|
|
dbgprotocol("Slot %d: <== Menu Last (%d)\n", CamSlot()->SlotNumber(), SessionId());
|
2003-01-06 14:44:27 +01:00
|
|
|
delete menu;
|
|
|
|
menu = new cCiMenu(this, Tag == AOT_MENU_LAST);
|
|
|
|
int l = 0;
|
|
|
|
const uint8_t *d = GetData(Data, l);
|
|
|
|
if (l > 0) {
|
|
|
|
// since the specification allows choiceNb to be undefined it is useless, so let's just skip it:
|
|
|
|
d++;
|
|
|
|
l--;
|
|
|
|
if (l > 0) menu->titleText = GetText(l, &d);
|
|
|
|
if (l > 0) menu->subTitleText = GetText(l, &d);
|
|
|
|
if (l > 0) menu->bottomText = GetText(l, &d);
|
2017-06-10 11:53:39 +02:00
|
|
|
int Action = CRA_NONE;
|
|
|
|
int Select = -1;
|
|
|
|
int Item = 0;
|
2003-01-06 14:44:27 +01:00
|
|
|
while (l > 0) {
|
|
|
|
char *s = GetText(l, &d);
|
|
|
|
if (s) {
|
|
|
|
if (!menu->AddEntry(s))
|
|
|
|
free(s);
|
2017-06-10 11:53:39 +02:00
|
|
|
else if (Action == CRA_NONE) {
|
|
|
|
Action = CamResponses.GetMatch(CamSlot()->SlotNumber(), s);
|
|
|
|
if (Action == CRA_SELECT)
|
|
|
|
Select = Item;
|
|
|
|
}
|
2003-01-06 14:44:27 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
2017-06-10 11:53:39 +02:00
|
|
|
Item++;
|
2003-01-06 14:44:27 +01:00
|
|
|
}
|
2017-06-10 11:53:39 +02:00
|
|
|
if (Action != CRA_NONE) {
|
|
|
|
delete menu;
|
|
|
|
menu = NULL;
|
|
|
|
cCondWait::SleepMs(100);
|
|
|
|
if (Action == CRA_DISCARD) {
|
|
|
|
SendCloseMMI();
|
|
|
|
dsyslog("CAM %d: DISCARD", CamSlot()->SlotNumber());
|
|
|
|
}
|
|
|
|
else if (Action == CRA_CONFIRM) {
|
|
|
|
SendMenuAnswer(1);
|
|
|
|
dsyslog("CAM %d: CONFIRM", CamSlot()->SlotNumber());
|
|
|
|
}
|
|
|
|
else if (Action == CRA_SELECT) {
|
|
|
|
SendMenuAnswer(Select + 1);
|
|
|
|
dsyslog("CAM %d: SELECT %d", CamSlot()->SlotNumber(), Select + 1);
|
|
|
|
}
|
|
|
|
}
|
2003-01-06 14:44:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case AOT_ENQ: {
|
2017-05-18 09:05:46 +02:00
|
|
|
dbgprotocol("Slot %d: <== Enq (%d)\n", CamSlot()->SlotNumber(), SessionId());
|
2003-01-06 14:44:27 +01:00
|
|
|
delete enquiry;
|
|
|
|
enquiry = new cCiEnquiry(this);
|
|
|
|
int l = 0;
|
|
|
|
const uint8_t *d = GetData(Data, l);
|
|
|
|
if (l > 0) {
|
|
|
|
uint8_t blind = *d++;
|
|
|
|
//XXX GetByte()???
|
|
|
|
l--;
|
|
|
|
enquiry->blind = blind & EF_BLIND;
|
|
|
|
enquiry->expectedLength = *d++;
|
|
|
|
l--;
|
|
|
|
// I really wonder why there is no text length field here...
|
|
|
|
enquiry->text = CopyString(l, d);
|
2017-06-10 11:53:39 +02:00
|
|
|
int Action = CamResponses.GetMatch(CamSlot()->SlotNumber(), enquiry->text);
|
|
|
|
if (Action > CRA_NONE) {
|
|
|
|
char s[enquiry->expectedLength * 2];
|
|
|
|
snprintf(s, sizeof(s), "%d", Action);
|
|
|
|
if (int(strlen(s)) == enquiry->expectedLength) {
|
|
|
|
delete enquiry;
|
|
|
|
enquiry = NULL;
|
|
|
|
SendAnswer(s);
|
|
|
|
dsyslog("CAM %d: PIN", CamSlot()->SlotNumber());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
esyslog("CAM %d: ERROR: unexpected PIN length %d, expected %d", CamSlot()->SlotNumber(), int(strlen(s)), enquiry->expectedLength);
|
|
|
|
}
|
2003-01-06 14:44:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2005-08-20 12:16:23 +02:00
|
|
|
case AOT_CLOSE_MMI: {
|
|
|
|
int id = -1;
|
|
|
|
int delay = -1;
|
|
|
|
int l = 0;
|
|
|
|
const uint8_t *d = GetData(Data, l);
|
|
|
|
if (l > 0) {
|
|
|
|
id = *d++;
|
|
|
|
if (l > 1)
|
|
|
|
delay = *d;
|
|
|
|
}
|
2017-05-18 09:05:46 +02:00
|
|
|
dbgprotocol("Slot %d: <== Close MMI (%d) id = %02X delay = %d\n", CamSlot()->SlotNumber(), SessionId(), id, delay);
|
2005-08-20 12:16:23 +02:00
|
|
|
}
|
|
|
|
break;
|
2017-05-18 09:05:46 +02:00
|
|
|
default: esyslog("ERROR: CAM %d: MMI: unknown tag %06X", CamSlot()->SlotNumber(), Tag);
|
2003-01-06 14:44:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-10-26 13:19:22 +01:00
|
|
|
cCiMenu *cCiMMI::Menu(bool Clear)
|
2003-01-06 14:44:27 +01:00
|
|
|
{
|
2003-10-26 13:19:22 +01:00
|
|
|
if (Clear)
|
|
|
|
fetchedMenu = NULL;
|
|
|
|
else if (menu) {
|
|
|
|
fetchedMenu = menu;
|
|
|
|
menu = NULL;
|
|
|
|
}
|
|
|
|
return fetchedMenu;
|
2003-01-06 14:44:27 +01:00
|
|
|
}
|
|
|
|
|
2003-10-26 13:19:22 +01:00
|
|
|
cCiEnquiry *cCiMMI::Enquiry(bool Clear)
|
2003-01-06 14:44:27 +01:00
|
|
|
{
|
2003-10-26 13:19:22 +01:00
|
|
|
if (Clear)
|
|
|
|
fetchedEnquiry = NULL;
|
|
|
|
else if (enquiry) {
|
|
|
|
fetchedEnquiry = enquiry;
|
|
|
|
enquiry = NULL;
|
|
|
|
}
|
|
|
|
return fetchedEnquiry;
|
2003-01-06 14:44:27 +01:00
|
|
|
}
|
|
|
|
|
2007-01-07 14:46:14 +01:00
|
|
|
void cCiMMI::SendMenuAnswer(uint8_t Selection)
|
2003-01-06 14:44:27 +01:00
|
|
|
{
|
2017-05-18 09:05:46 +02:00
|
|
|
dbgprotocol("Slot %d: ==> Menu Answ (%d)\n", CamSlot()->SlotNumber(), SessionId());
|
2003-01-06 14:44:27 +01:00
|
|
|
SendData(AOT_MENU_ANSW, 1, &Selection);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cCiMMI::SendAnswer(const char *Text)
|
|
|
|
{
|
2017-05-18 09:05:46 +02:00
|
|
|
dbgprotocol("Slot %d: ==> Answ (%d)\n", CamSlot()->SlotNumber(), SessionId());
|
2003-01-06 14:44:27 +01:00
|
|
|
struct tAnswer { uint8_t id; char text[256]; };//XXX
|
|
|
|
tAnswer answer;
|
|
|
|
answer.id = Text ? AI_ANSWER : AI_CANCEL;
|
2019-05-06 12:03:59 +02:00
|
|
|
int len = 0;
|
|
|
|
if (Text) {
|
|
|
|
len = min(sizeof(answer.text), strlen(Text));
|
|
|
|
memcpy(answer.text, Text, len);
|
|
|
|
}
|
|
|
|
SendData(AOT_ANSW, len + 1, (uint8_t *)&answer);
|
2003-01-06 14:44:27 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2005-10-02 13:54:34 +02:00
|
|
|
bool cCiMMI::SendCloseMMI(void)
|
|
|
|
{
|
2017-05-18 09:05:46 +02:00
|
|
|
dbgprotocol("Slot %d: ==> Close MMI (%d)\n", CamSlot()->SlotNumber(), SessionId());
|
2005-10-02 13:54:34 +02:00
|
|
|
SendData(AOT_CLOSE_MMI, 0);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2003-01-06 14:44:27 +01:00
|
|
|
// --- cCiMenu ---------------------------------------------------------------
|
|
|
|
|
|
|
|
cCiMenu::cCiMenu(cCiMMI *MMI, bool Selectable)
|
|
|
|
{
|
|
|
|
mmi = MMI;
|
2006-07-22 13:43:01 +02:00
|
|
|
mutex = NULL;
|
2003-01-06 14:44:27 +01:00
|
|
|
selectable = Selectable;
|
|
|
|
titleText = subTitleText = bottomText = NULL;
|
|
|
|
numEntries = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
cCiMenu::~cCiMenu()
|
|
|
|
{
|
2005-10-30 13:21:23 +01:00
|
|
|
cMutexLock MutexLock(mutex);
|
2003-10-26 13:19:22 +01:00
|
|
|
if (mmi)
|
|
|
|
mmi->Menu(true);
|
2003-01-06 14:44:27 +01:00
|
|
|
free(titleText);
|
|
|
|
free(subTitleText);
|
|
|
|
free(bottomText);
|
|
|
|
for (int i = 0; i < numEntries; i++)
|
|
|
|
free(entries[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cCiMenu::AddEntry(char *s)
|
|
|
|
{
|
|
|
|
if (numEntries < MAX_CIMENU_ENTRIES) {
|
|
|
|
entries[numEntries++] = s;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2005-10-02 12:59:53 +02:00
|
|
|
bool cCiMenu::HasUpdate(void)
|
|
|
|
{
|
2005-10-03 12:48:13 +02:00
|
|
|
// If the mmi is gone, the menu shall be closed, which also qualifies as 'update'.
|
|
|
|
return !mmi || mmi->HasUserIO();
|
2005-10-02 12:59:53 +02:00
|
|
|
}
|
|
|
|
|
2007-01-07 14:46:14 +01:00
|
|
|
void cCiMenu::Select(int Index)
|
2003-01-06 14:44:27 +01:00
|
|
|
{
|
2005-10-30 13:21:23 +01:00
|
|
|
cMutexLock MutexLock(mutex);
|
2003-01-06 14:44:27 +01:00
|
|
|
if (mmi && -1 <= Index && Index < numEntries)
|
2007-01-07 14:46:14 +01:00
|
|
|
mmi->SendMenuAnswer(Index + 1);
|
2003-01-06 14:44:27 +01:00
|
|
|
}
|
|
|
|
|
2007-01-07 14:46:14 +01:00
|
|
|
void cCiMenu::Cancel(void)
|
2003-01-06 14:44:27 +01:00
|
|
|
{
|
2007-01-07 14:46:14 +01:00
|
|
|
Select(-1);
|
2003-01-06 14:44:27 +01:00
|
|
|
}
|
|
|
|
|
2007-01-07 14:46:14 +01:00
|
|
|
void cCiMenu::Abort(void)
|
2005-10-02 13:54:34 +02:00
|
|
|
{
|
2005-10-30 13:21:23 +01:00
|
|
|
cMutexLock MutexLock(mutex);
|
2007-01-07 14:46:14 +01:00
|
|
|
if (mmi)
|
|
|
|
mmi->SendCloseMMI();
|
2005-10-02 13:54:34 +02:00
|
|
|
}
|
|
|
|
|
2003-01-06 14:44:27 +01:00
|
|
|
// --- cCiEnquiry ------------------------------------------------------------
|
|
|
|
|
|
|
|
cCiEnquiry::cCiEnquiry(cCiMMI *MMI)
|
|
|
|
{
|
|
|
|
mmi = MMI;
|
2017-06-10 11:53:39 +02:00
|
|
|
mutex = NULL;
|
2003-01-06 14:44:27 +01:00
|
|
|
text = NULL;
|
2006-07-22 13:43:01 +02:00
|
|
|
blind = false;
|
|
|
|
expectedLength = 0;
|
2003-01-06 14:44:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
cCiEnquiry::~cCiEnquiry()
|
|
|
|
{
|
2005-10-30 13:21:23 +01:00
|
|
|
cMutexLock MutexLock(mutex);
|
2003-10-26 13:19:22 +01:00
|
|
|
if (mmi)
|
|
|
|
mmi->Enquiry(true);
|
2003-01-06 14:44:27 +01:00
|
|
|
free(text);
|
|
|
|
}
|
|
|
|
|
2007-01-07 14:46:14 +01:00
|
|
|
void cCiEnquiry::Reply(const char *s)
|
2003-01-06 14:44:27 +01:00
|
|
|
{
|
2005-10-30 13:21:23 +01:00
|
|
|
cMutexLock MutexLock(mutex);
|
2007-01-07 14:46:14 +01:00
|
|
|
if (mmi)
|
|
|
|
mmi->SendAnswer(s);
|
2003-01-06 14:44:27 +01:00
|
|
|
}
|
|
|
|
|
2007-01-07 14:46:14 +01:00
|
|
|
void cCiEnquiry::Cancel(void)
|
2003-01-06 14:44:27 +01:00
|
|
|
{
|
2007-01-07 14:46:14 +01:00
|
|
|
Reply(NULL);
|
2003-01-06 14:44:27 +01:00
|
|
|
}
|
|
|
|
|
2007-01-07 14:46:14 +01:00
|
|
|
void cCiEnquiry::Abort(void)
|
2005-10-03 12:58:22 +02:00
|
|
|
{
|
2005-10-30 13:21:23 +01:00
|
|
|
cMutexLock MutexLock(mutex);
|
2007-01-07 14:46:14 +01:00
|
|
|
if (mmi)
|
|
|
|
mmi->SendCloseMMI();
|
2005-10-03 12:58:22 +02:00
|
|
|
}
|
|
|
|
|
2017-05-18 09:05:46 +02:00
|
|
|
// --- cCiResourceHandler ----------------------------------------------------
|
|
|
|
|
|
|
|
cCiResourceHandler::cCiResourceHandler(void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
cCiResourceHandler::~cCiResourceHandler()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// --- cCiDefaultResourceHandler ---------------------------------------------
|
|
|
|
|
|
|
|
class cCiDefaultResourceHandler : public cCiResourceHandler {
|
|
|
|
public:
|
|
|
|
virtual const uint32_t *ResourceIds(void) const;
|
|
|
|
virtual cCiSession *GetNewCiSession(uint32_t ResourceId, uint16_t SessionId, cCiTransportConnection *Tc);
|
|
|
|
};
|
|
|
|
|
|
|
|
const uint32_t *cCiDefaultResourceHandler::ResourceIds(void) const
|
|
|
|
{
|
|
|
|
static uint32_t Ids[] = {
|
|
|
|
RI_RESOURCE_MANAGER,
|
|
|
|
RI_APPLICATION_INFORMATION,
|
|
|
|
RI_CONDITIONAL_ACCESS_SUPPORT,
|
|
|
|
RI_HOST_CONTROL,
|
|
|
|
RI_DATE_TIME,
|
|
|
|
RI_MMI,
|
|
|
|
0
|
|
|
|
};
|
|
|
|
return Ids;
|
|
|
|
}
|
|
|
|
|
|
|
|
cCiSession *cCiDefaultResourceHandler::GetNewCiSession(uint32_t ResourceId, uint16_t SessionId, cCiTransportConnection *Tc)
|
|
|
|
{
|
|
|
|
switch (ResourceId) {
|
|
|
|
case RI_RESOURCE_MANAGER: return new cCiResourceManager(SessionId, Tc); break;
|
|
|
|
case RI_APPLICATION_INFORMATION: return new cCiApplicationInformation(SessionId, Tc); break;
|
|
|
|
case RI_CONDITIONAL_ACCESS_SUPPORT: return new cCiConditionalAccessSupport(SessionId, Tc); break;
|
|
|
|
case RI_HOST_CONTROL: return new cCiHostControl(SessionId, Tc); break;
|
|
|
|
case RI_DATE_TIME: return new cCiDateTime(SessionId, Tc); break;
|
|
|
|
case RI_MMI: return new cCiMMI(SessionId, Tc); break;
|
|
|
|
default: return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// --- cCiResourceHandlers ---------------------------------------------------
|
|
|
|
|
|
|
|
cCiResourceHandlers CiResourceHandlers;
|
|
|
|
|
|
|
|
cCiResourceHandlers::cCiResourceHandlers(void)
|
|
|
|
{
|
|
|
|
Register(new cCiDefaultResourceHandler);
|
|
|
|
}
|
|
|
|
|
|
|
|
void cCiResourceHandlers::Register(cCiResourceHandler *ResourceHandler)
|
|
|
|
{
|
|
|
|
if (ResourceHandler) {
|
|
|
|
Add(ResourceHandler);
|
|
|
|
if (const uint32_t *r = ResourceHandler->ResourceIds()) {
|
|
|
|
while (*r) {
|
|
|
|
resourceIds.Append(htonl(*r));
|
|
|
|
r++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cCiSession *cCiResourceHandlers::GetNewCiSession(uint32_t ResourceId, uint16_t SessionId, cCiTransportConnection *Tc)
|
|
|
|
{
|
|
|
|
for (cCiResourceHandler *r = Last(); r; r = Prev(r)) {
|
|
|
|
if (cCiSession *CiSession = r->GetNewCiSession(ResourceId, SessionId, Tc))
|
|
|
|
return CiSession;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-01-07 14:46:14 +01:00
|
|
|
// --- cCiTransportConnection (cont'd) ---------------------------------------
|
2003-01-06 14:44:27 +01:00
|
|
|
|
2007-01-07 14:46:14 +01:00
|
|
|
#define TC_POLL_TIMEOUT 300 // ms WORKAROUND: TC_POLL_TIMEOUT < 300ms doesn't work with DragonCAM
|
|
|
|
#define TC_ALIVE_TIMEOUT 2000 // ms after which a transport connection is assumed dead
|
|
|
|
|
|
|
|
cCiTransportConnection::cCiTransportConnection(cCamSlot *CamSlot, uint8_t Tcid)
|
2003-01-06 14:44:27 +01:00
|
|
|
{
|
2007-01-07 14:46:14 +01:00
|
|
|
dbgprotocol("Slot %d: creating connection %d/%d\n", CamSlot->SlotNumber(), CamSlot->SlotIndex(), Tcid);
|
|
|
|
camSlot = CamSlot;
|
|
|
|
tcid = Tcid;
|
|
|
|
state = stIDLE;
|
|
|
|
createConnectionRequested = false;
|
|
|
|
deleteConnectionRequested = false;
|
2003-04-20 09:53:08 +02:00
|
|
|
hasUserIO = false;
|
2007-01-07 14:46:14 +01:00
|
|
|
alive.Set(TC_ALIVE_TIMEOUT);
|
|
|
|
for (int i = 0; i <= MAX_SESSIONS_PER_TC; i++) // sessions[0] is not used, but initialized anyway
|
2003-01-06 14:44:27 +01:00
|
|
|
sessions[i] = NULL;
|
2017-05-18 09:05:46 +02:00
|
|
|
tsPostProcessor = NULL;
|
2003-01-06 14:44:27 +01:00
|
|
|
}
|
|
|
|
|
2007-01-07 14:46:14 +01:00
|
|
|
cCiTransportConnection::~cCiTransportConnection()
|
2003-01-06 14:44:27 +01:00
|
|
|
{
|
2007-01-07 14:46:14 +01:00
|
|
|
for (int i = 1; i <= MAX_SESSIONS_PER_TC; i++)
|
2003-01-06 14:44:27 +01:00
|
|
|
delete sessions[i];
|
|
|
|
}
|
|
|
|
|
2017-05-18 09:05:46 +02:00
|
|
|
void cCiTransportConnection::SetTsPostProcessor(cCiSession *CiSession)
|
|
|
|
{
|
|
|
|
tsPostProcessor = CiSession;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cCiTransportConnection::TsPostProcess(uint8_t *TsPacket)
|
|
|
|
{
|
2020-06-16 14:25:43 +02:00
|
|
|
cMutexLock MutexLock(&mutex);
|
2017-05-18 09:05:46 +02:00
|
|
|
if (tsPostProcessor)
|
|
|
|
return tsPostProcessor->TsPostProcess(TsPacket);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-01-07 14:46:14 +01:00
|
|
|
bool cCiTransportConnection::Ready(void)
|
2003-01-06 14:44:27 +01:00
|
|
|
{
|
2007-01-07 14:46:14 +01:00
|
|
|
cCiConditionalAccessSupport *cas = (cCiConditionalAccessSupport *)GetSessionByResourceId(RI_CONDITIONAL_ACCESS_SUPPORT);
|
|
|
|
return cas && cas->Ready();
|
2003-01-06 14:44:27 +01:00
|
|
|
}
|
|
|
|
|
2007-01-07 14:46:14 +01:00
|
|
|
const char *cCiTransportConnection::GetCamName(void)
|
2003-01-06 14:44:27 +01:00
|
|
|
{
|
2007-01-07 14:46:14 +01:00
|
|
|
cCiApplicationInformation *ai = (cCiApplicationInformation *)GetSessionByResourceId(RI_APPLICATION_INFORMATION);
|
|
|
|
return ai ? ai->GetMenuString() : NULL;
|
2003-01-06 14:44:27 +01:00
|
|
|
}
|
|
|
|
|
2007-01-07 14:46:14 +01:00
|
|
|
void cCiTransportConnection::SendTPDU(uint8_t Tag, int Length, const uint8_t *Data)
|
|
|
|
{
|
|
|
|
cTPDU TPDU(camSlot->SlotIndex(), tcid, Tag, Length, Data);
|
|
|
|
camSlot->Write(&TPDU);
|
|
|
|
timer.Set(TC_POLL_TIMEOUT);
|
|
|
|
}
|
|
|
|
|
|
|
|
void cCiTransportConnection::SendData(int Length, const uint8_t *Data)
|
|
|
|
{
|
|
|
|
// if Length ever exceeds MAX_TPDU_DATA this needs to be handled differently
|
|
|
|
if (state == stACTIVE && Length > 0)
|
|
|
|
SendTPDU(T_DATA_LAST, Length, Data);
|
|
|
|
}
|
|
|
|
|
|
|
|
void cCiTransportConnection::SendTag(uint8_t Tag, uint16_t SessionId, uint32_t ResourceId, int Status)
|
2003-01-06 14:44:27 +01:00
|
|
|
{
|
|
|
|
uint8_t buffer[16];
|
|
|
|
uint8_t *p = buffer;
|
|
|
|
*p++ = Tag;
|
|
|
|
*p++ = 0x00; // will contain length
|
|
|
|
if (Status >= 0)
|
|
|
|
*p++ = Status;
|
|
|
|
if (ResourceId) {
|
2006-08-12 10:26:43 +02:00
|
|
|
put_unaligned(htonl(ResourceId), (uint32_t *)p);
|
2003-01-06 14:44:27 +01:00
|
|
|
p += 4;
|
|
|
|
}
|
2003-12-22 15:59:18 +01:00
|
|
|
put_unaligned(htons(SessionId), (uint16_t *)p);
|
2003-01-06 14:44:27 +01:00
|
|
|
p += 2;
|
|
|
|
buffer[1] = p - buffer - 2; // length
|
2007-01-07 14:46:14 +01:00
|
|
|
SendData(p - buffer, buffer);
|
2003-01-06 14:44:27 +01:00
|
|
|
}
|
|
|
|
|
2007-01-07 14:46:14 +01:00
|
|
|
void cCiTransportConnection::Poll(void)
|
2003-01-06 14:44:27 +01:00
|
|
|
{
|
2007-01-07 14:46:14 +01:00
|
|
|
bool OldDumpTPDUDataTransfer = DumpTPDUDataTransfer;
|
|
|
|
DumpTPDUDataTransfer &= DumpPolls;
|
|
|
|
if (DumpPolls)
|
|
|
|
dbgprotocol("Slot %d: ==> Poll\n", camSlot->SlotNumber());
|
|
|
|
SendTPDU(T_DATA_LAST);
|
|
|
|
DumpTPDUDataTransfer = OldDumpTPDUDataTransfer;
|
2003-01-06 14:44:27 +01:00
|
|
|
}
|
|
|
|
|
2007-01-07 14:46:14 +01:00
|
|
|
uint32_t cCiTransportConnection::ResourceIdToInt(const uint8_t *Data)
|
2003-01-06 14:44:27 +01:00
|
|
|
{
|
2007-01-07 14:46:14 +01:00
|
|
|
return (ntohl(get_unaligned((uint32_t *)Data)));
|
2003-01-06 14:44:27 +01:00
|
|
|
}
|
|
|
|
|
2007-01-07 14:46:14 +01:00
|
|
|
cCiSession *cCiTransportConnection::GetSessionBySessionId(uint16_t SessionId)
|
2003-01-06 14:44:27 +01:00
|
|
|
{
|
2007-01-07 14:46:14 +01:00
|
|
|
return (SessionId <= MAX_SESSIONS_PER_TC) ? sessions[SessionId] : NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
cCiSession *cCiTransportConnection::GetSessionByResourceId(uint32_t ResourceId)
|
|
|
|
{
|
2017-05-18 09:05:46 +02:00
|
|
|
cCiSession *CiSession = NULL;
|
2007-01-07 14:46:14 +01:00
|
|
|
for (int i = 1; i <= MAX_SESSIONS_PER_TC; i++) {
|
2017-05-18 09:05:46 +02:00
|
|
|
if (cCiSession *s = sessions[i]) {
|
|
|
|
if (s->ResourceId() == ResourceId)
|
|
|
|
return s; // prefer exact match
|
|
|
|
if ((s->ResourceId() & RESOURCE_CLASS_MASK) == (ResourceId & RESOURCE_CLASS_MASK))
|
|
|
|
CiSession = s;
|
|
|
|
}
|
2007-01-07 14:46:14 +01:00
|
|
|
}
|
2017-05-18 09:05:46 +02:00
|
|
|
return CiSession;
|
2003-01-06 14:44:27 +01:00
|
|
|
}
|
|
|
|
|
2007-01-07 14:46:14 +01:00
|
|
|
void cCiTransportConnection::OpenSession(int Length, const uint8_t *Data)
|
2003-01-06 14:44:27 +01:00
|
|
|
{
|
|
|
|
if (Length == 6 && *(Data + 1) == 0x04) {
|
2006-08-12 10:26:43 +02:00
|
|
|
uint32_t ResourceId = ResourceIdToInt(Data + 2);
|
2007-01-07 14:46:14 +01:00
|
|
|
dbgprotocol("Slot %d: open session %08X\n", camSlot->SlotNumber(), ResourceId);
|
|
|
|
if (!GetSessionByResourceId(ResourceId)) {
|
|
|
|
for (int i = 1; i <= MAX_SESSIONS_PER_TC; i++) {
|
|
|
|
if (!sessions[i]) {
|
2017-05-18 09:05:46 +02:00
|
|
|
sessions[i] = CiResourceHandlers.GetNewCiSession(ResourceId, i, this);
|
2007-01-07 14:46:14 +01:00
|
|
|
if (sessions[i])
|
|
|
|
SendTag(ST_OPEN_SESSION_RESPONSE, sessions[i]->SessionId(), sessions[i]->ResourceId(), SS_OK);
|
2017-05-18 09:05:46 +02:00
|
|
|
else
|
|
|
|
esyslog("ERROR: CAM %d: unknown resource identifier: %08X (%d/%d)", camSlot->SlotNumber(), ResourceId, camSlot->SlotIndex(), tcid);
|
2007-01-07 14:46:14 +01:00
|
|
|
return;
|
|
|
|
}
|
2003-01-06 14:44:27 +01:00
|
|
|
}
|
2007-01-07 14:46:14 +01:00
|
|
|
esyslog("ERROR: CAM %d: no free session slot for resource identifier %08X (%d/%d)", camSlot->SlotNumber(), ResourceId, camSlot->SlotIndex(), tcid);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
esyslog("ERROR: CAM %d: session for resource identifier %08X already exists (%d/%d)", camSlot->SlotNumber(), ResourceId, camSlot->SlotIndex(), tcid);
|
2003-01-06 14:44:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-07 14:46:14 +01:00
|
|
|
void cCiTransportConnection::CloseSession(uint16_t SessionId)
|
2003-01-06 14:44:27 +01:00
|
|
|
{
|
2007-01-07 14:46:14 +01:00
|
|
|
dbgprotocol("Slot %d: close session %d\n", camSlot->SlotNumber(), SessionId);
|
2003-01-06 14:44:27 +01:00
|
|
|
cCiSession *Session = GetSessionBySessionId(SessionId);
|
2007-01-07 14:46:14 +01:00
|
|
|
if (Session && sessions[SessionId] == Session) {
|
2003-01-06 14:44:27 +01:00
|
|
|
delete Session;
|
2007-01-07 14:46:14 +01:00
|
|
|
sessions[SessionId] = NULL;
|
|
|
|
SendTag(ST_CLOSE_SESSION_RESPONSE, SessionId, 0, SS_OK);
|
2003-01-06 14:44:27 +01:00
|
|
|
}
|
|
|
|
else {
|
2007-01-07 14:46:14 +01:00
|
|
|
esyslog("ERROR: CAM %d: unknown session id: %d (%d/%d)", camSlot->SlotNumber(), SessionId, camSlot->SlotIndex(), tcid);
|
|
|
|
SendTag(ST_CLOSE_SESSION_RESPONSE, SessionId, 0, SS_NOT_ALLOCATED);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void cCiTransportConnection::HandleSessions(cTPDU *TPDU)
|
|
|
|
{
|
|
|
|
int Length;
|
|
|
|
const uint8_t *Data = TPDU->Data(Length);
|
|
|
|
if (Data && Length > 1) {
|
|
|
|
switch (*Data) {
|
|
|
|
case ST_SESSION_NUMBER: if (Length > 4) {
|
|
|
|
uint16_t SessionId = ntohs(get_unaligned((uint16_t *)&Data[2]));
|
|
|
|
cCiSession *Session = GetSessionBySessionId(SessionId);
|
|
|
|
if (Session)
|
|
|
|
Session->Process(Length - 4, Data + 4);
|
|
|
|
else
|
|
|
|
esyslog("ERROR: CAM %d: unknown session id: %d (%d/%d)", camSlot->SlotNumber(), SessionId, camSlot->SlotIndex(), tcid);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case ST_OPEN_SESSION_REQUEST: OpenSession(Length, Data);
|
|
|
|
break;
|
|
|
|
case ST_CLOSE_SESSION_REQUEST: if (Length == 4)
|
|
|
|
CloseSession(ntohs(get_unaligned((uint16_t *)&Data[2])));
|
|
|
|
break;
|
|
|
|
case ST_CREATE_SESSION_RESPONSE: // not implemented
|
|
|
|
case ST_CLOSE_SESSION_RESPONSE: // not implemented
|
|
|
|
default: esyslog("ERROR: CAM %d: unknown session tag: %02X (%d/%d)", camSlot->SlotNumber(), *Data, camSlot->SlotIndex(), tcid);
|
|
|
|
}
|
2003-01-06 14:44:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-07 14:46:14 +01:00
|
|
|
bool cCiTransportConnection::Process(cTPDU *TPDU)
|
2003-02-09 11:54:22 +01:00
|
|
|
{
|
2007-01-07 14:46:14 +01:00
|
|
|
if (TPDU)
|
|
|
|
alive.Set(TC_ALIVE_TIMEOUT);
|
|
|
|
else if (alive.TimedOut())
|
|
|
|
return false;
|
|
|
|
switch (state) {
|
|
|
|
case stIDLE:
|
|
|
|
if (createConnectionRequested) {
|
|
|
|
dbgprotocol("Slot %d: create connection %d/%d\n", camSlot->SlotNumber(), camSlot->SlotIndex(), tcid);
|
|
|
|
createConnectionRequested = false;
|
|
|
|
SendTPDU(T_CREATE_TC);
|
|
|
|
state = stCREATION;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
case stCREATION:
|
|
|
|
if (TPDU && TPDU->Tag() == T_CTC_REPLY) {
|
|
|
|
dbgprotocol("Slot %d: connection created %d/%d\n", camSlot->SlotNumber(), camSlot->SlotIndex(), tcid);
|
|
|
|
Poll();
|
|
|
|
state = stACTIVE;
|
|
|
|
}
|
|
|
|
else if (timer.TimedOut()) {
|
|
|
|
dbgprotocol("Slot %d: timeout while creating connection %d/%d\n", camSlot->SlotNumber(), camSlot->SlotIndex(), tcid);
|
|
|
|
state = stIDLE;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
case stACTIVE:
|
|
|
|
if (deleteConnectionRequested) {
|
|
|
|
dbgprotocol("Slot %d: delete connection requested %d/%d\n", camSlot->SlotNumber(), camSlot->SlotIndex(), tcid);
|
|
|
|
deleteConnectionRequested = false;
|
|
|
|
SendTPDU(T_DELETE_TC);
|
|
|
|
state = stDELETION;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (TPDU) {
|
|
|
|
switch (TPDU->Tag()) {
|
|
|
|
case T_REQUEST_TC:
|
|
|
|
esyslog("ERROR: CAM %d: T_REQUEST_TC not implemented (%d/%d)", camSlot->SlotNumber(), camSlot->SlotIndex(), tcid);
|
|
|
|
break;
|
|
|
|
case T_DATA_MORE:
|
|
|
|
case T_DATA_LAST:
|
|
|
|
HandleSessions(TPDU);
|
|
|
|
// continue with T_SB
|
|
|
|
case T_SB:
|
|
|
|
if ((TPDU->Status() & DATA_INDICATOR) != 0) {
|
|
|
|
dbgprotocol("Slot %d: receive data %d/%d\n", camSlot->SlotNumber(), camSlot->SlotIndex(), tcid);
|
|
|
|
SendTPDU(T_RCV);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case T_DELETE_TC:
|
|
|
|
dbgprotocol("Slot %d: delete connection %d/%d\n", camSlot->SlotNumber(), camSlot->SlotIndex(), tcid);
|
|
|
|
SendTPDU(T_DTC_REPLY);
|
|
|
|
state = stIDLE;
|
|
|
|
return true;
|
2009-12-06 12:57:45 +01:00
|
|
|
case T_RCV:
|
|
|
|
case T_CREATE_TC:
|
|
|
|
case T_CTC_REPLY:
|
|
|
|
case T_DTC_REPLY:
|
|
|
|
case T_NEW_TC:
|
|
|
|
case T_TC_ERROR:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
esyslog("ERROR: unknown TPDU tag: 0x%02X (%s)", TPDU->Tag(), __FUNCTION__);
|
2007-01-07 14:46:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (timer.TimedOut())
|
|
|
|
Poll();
|
|
|
|
hasUserIO = false;
|
|
|
|
for (int i = 1; i <= MAX_SESSIONS_PER_TC; i++) {
|
|
|
|
if (sessions[i]) {
|
|
|
|
sessions[i]->Process();
|
|
|
|
if (sessions[i]->HasUserIO())
|
|
|
|
hasUserIO = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case stDELETION:
|
|
|
|
if (TPDU && TPDU->Tag() == T_DTC_REPLY || timer.TimedOut()) {
|
|
|
|
dbgprotocol("Slot %d: connection deleted %d/%d\n", camSlot->SlotNumber(), camSlot->SlotIndex(), tcid);
|
|
|
|
state = stIDLE;
|
|
|
|
}
|
|
|
|
return true;
|
2009-12-06 12:57:45 +01:00
|
|
|
default:
|
|
|
|
esyslog("ERROR: unknown state: %d (%s)", state, __FUNCTION__);
|
2007-01-07 14:46:14 +01:00
|
|
|
}
|
|
|
|
return true;
|
2003-02-09 11:54:22 +01:00
|
|
|
}
|
|
|
|
|
2007-01-07 14:46:14 +01:00
|
|
|
// --- cCiCaPidData ----------------------------------------------------------
|
|
|
|
|
|
|
|
class cCiCaPidData : public cListObject {
|
|
|
|
public:
|
|
|
|
bool active;
|
|
|
|
int pid;
|
|
|
|
int streamType;
|
|
|
|
cCiCaPidData(int Pid, int StreamType)
|
|
|
|
{
|
|
|
|
active = false;
|
|
|
|
pid = Pid;
|
|
|
|
streamType = StreamType;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// --- cCiCaProgramData ------------------------------------------------------
|
|
|
|
|
|
|
|
class cCiCaProgramData : public cListObject {
|
|
|
|
public:
|
|
|
|
int programNumber;
|
|
|
|
bool modified;
|
|
|
|
cList<cCiCaPidData> pidList;
|
|
|
|
cCiCaProgramData(int ProgramNumber)
|
|
|
|
{
|
|
|
|
programNumber = ProgramNumber;
|
|
|
|
modified = false;
|
|
|
|
}
|
2017-03-18 16:33:59 +01:00
|
|
|
bool Active(void)
|
|
|
|
{
|
|
|
|
for (cCiCaPidData *p = pidList.First(); p; p = pidList.Next(p)) {
|
|
|
|
if (p->active)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2007-01-07 14:46:14 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
// --- cCiAdapter ------------------------------------------------------------
|
|
|
|
|
|
|
|
cCiAdapter::cCiAdapter(void)
|
|
|
|
:cThread("CI adapter")
|
2006-01-07 15:16:09 +01:00
|
|
|
{
|
2007-01-07 14:46:14 +01:00
|
|
|
for (int i = 0; i < MAX_CAM_SLOTS_PER_ADAPTER; i++)
|
|
|
|
camSlots[i] = NULL;
|
2006-01-07 15:16:09 +01:00
|
|
|
}
|
|
|
|
|
2007-01-07 14:46:14 +01:00
|
|
|
cCiAdapter::~cCiAdapter()
|
2005-08-21 08:56:49 +02:00
|
|
|
{
|
2007-01-07 14:46:14 +01:00
|
|
|
Cancel(3);
|
|
|
|
for (int i = 0; i < MAX_CAM_SLOTS_PER_ADAPTER; i++)
|
|
|
|
delete camSlots[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
void cCiAdapter::AddCamSlot(cCamSlot *CamSlot)
|
|
|
|
{
|
|
|
|
if (CamSlot) {
|
|
|
|
for (int i = 0; i < MAX_CAM_SLOTS_PER_ADAPTER; i++) {
|
|
|
|
if (!camSlots[i]) {
|
|
|
|
CamSlot->slotIndex = i;
|
|
|
|
camSlots[i] = CamSlot;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
esyslog("ERROR: no free CAM slot in CI adapter");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-15 09:28:37 +01:00
|
|
|
cCamSlot *cCiAdapter::ItCamSlot(int &Iter)
|
|
|
|
{
|
|
|
|
if (Iter >= 0) {
|
|
|
|
for (; Iter < MAX_CAM_SLOTS_PER_ADAPTER; ) {
|
|
|
|
if (cCamSlot *Found = camSlots[Iter++])
|
|
|
|
return Found;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-01-07 14:46:14 +01:00
|
|
|
void cCiAdapter::Action(void)
|
|
|
|
{
|
|
|
|
cTPDU TPDU;
|
|
|
|
while (Running()) {
|
|
|
|
int n = Read(TPDU.Buffer(), TPDU.MaxSize());
|
|
|
|
if (n > 0 && TPDU.Slot() < MAX_CAM_SLOTS_PER_ADAPTER) {
|
|
|
|
TPDU.SetSize(n);
|
|
|
|
cCamSlot *cs = camSlots[TPDU.Slot()];
|
|
|
|
TPDU.Dump(cs ? cs->SlotNumber() : 0, false);
|
|
|
|
if (cs)
|
|
|
|
cs->Process(&TPDU);
|
|
|
|
}
|
|
|
|
for (int i = 0; i < MAX_CAM_SLOTS_PER_ADAPTER; i++) {
|
|
|
|
if (camSlots[i])
|
|
|
|
camSlots[i]->Process();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// --- cCamSlot --------------------------------------------------------------
|
|
|
|
|
2008-04-13 13:30:55 +02:00
|
|
|
#define MODULE_CHECK_INTERVAL 500 // ms
|
2007-01-07 14:46:14 +01:00
|
|
|
#define MODULE_RESET_TIMEOUT 2 // s
|
|
|
|
|
2017-01-23 12:01:48 +01:00
|
|
|
cCamSlot::cCamSlot(cCiAdapter *CiAdapter, bool WantsTsData, cCamSlot *MasterSlot)
|
2007-01-07 14:46:14 +01:00
|
|
|
{
|
|
|
|
ciAdapter = CiAdapter;
|
2017-01-23 12:01:48 +01:00
|
|
|
masterSlot = MasterSlot;
|
2015-01-15 09:28:37 +01:00
|
|
|
assignedDevice = NULL;
|
2017-01-23 12:01:48 +01:00
|
|
|
caPidReceiver = WantsTsData ? new cCaPidReceiver : NULL;
|
2015-01-30 13:38:44 +01:00
|
|
|
caActivationReceiver = NULL;
|
2007-01-07 14:46:14 +01:00
|
|
|
slotIndex = -1;
|
2017-03-18 16:33:59 +01:00
|
|
|
mtdAvailable = false;
|
|
|
|
mtdHandler = NULL;
|
2007-01-07 14:46:14 +01:00
|
|
|
lastModuleStatus = msReset; // avoids initial reset log message
|
|
|
|
resetTime = 0;
|
|
|
|
resendPmt = false;
|
|
|
|
for (int i = 0; i <= MAX_CONNECTIONS_PER_CAM_SLOT; i++) // tc[0] is not used, but initialized anyway
|
|
|
|
tc[i] = NULL;
|
2017-03-18 16:33:59 +01:00
|
|
|
if (MasterSlot)
|
|
|
|
slotNumber = MasterSlot->SlotNumber();
|
2017-02-21 14:17:07 +01:00
|
|
|
if (ciAdapter) {
|
2017-03-18 16:33:59 +01:00
|
|
|
CamSlots.Add(this);
|
|
|
|
slotNumber = Index() + 1;
|
2007-01-07 14:46:14 +01:00
|
|
|
ciAdapter->AddCamSlot(this);
|
2017-02-21 14:17:07 +01:00
|
|
|
Reset();
|
|
|
|
}
|
2007-01-07 14:46:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
cCamSlot::~cCamSlot()
|
|
|
|
{
|
2017-03-25 14:09:31 +01:00
|
|
|
Assign(NULL);
|
2014-01-01 12:37:22 +01:00
|
|
|
delete caPidReceiver;
|
2015-01-30 13:38:44 +01:00
|
|
|
delete caActivationReceiver;
|
2007-01-07 14:46:14 +01:00
|
|
|
CamSlots.Del(this, false);
|
|
|
|
DeleteAllConnections();
|
2017-03-18 16:33:59 +01:00
|
|
|
delete mtdHandler;
|
|
|
|
}
|
|
|
|
|
|
|
|
cCamSlot *cCamSlot::MtdSpawn(void)
|
|
|
|
{
|
|
|
|
cMutexLock MutexLock(&mutex);
|
|
|
|
if (mtdHandler)
|
|
|
|
return mtdHandler->GetMtdCamSlot(this);
|
|
|
|
return this;
|
2007-01-07 14:46:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool cCamSlot::Assign(cDevice *Device, bool Query)
|
2003-02-09 11:54:22 +01:00
|
|
|
{
|
|
|
|
cMutexLock MutexLock(&mutex);
|
2017-03-18 16:33:59 +01:00
|
|
|
if (Device == assignedDevice)
|
|
|
|
return true;
|
2007-01-07 14:46:14 +01:00
|
|
|
if (ciAdapter) {
|
2017-03-18 16:33:59 +01:00
|
|
|
int OldDeviceNumber = 0;
|
|
|
|
if (assignedDevice && !Query) {
|
|
|
|
OldDeviceNumber = assignedDevice->DeviceNumber() + 1;
|
|
|
|
if (caPidReceiver)
|
|
|
|
assignedDevice->Detach(caPidReceiver);
|
|
|
|
assignedDevice->SetCamSlot(NULL);
|
|
|
|
assignedDevice = NULL;
|
|
|
|
}
|
2007-01-07 14:46:14 +01:00
|
|
|
if (ciAdapter->Assign(Device, true)) {
|
2017-03-18 16:33:59 +01:00
|
|
|
if (!Query) {
|
2007-01-07 14:46:14 +01:00
|
|
|
StopDecrypting();
|
|
|
|
if (ciAdapter->Assign(Device)) {
|
|
|
|
if (Device) {
|
|
|
|
Device->SetCamSlot(this);
|
2017-03-18 16:33:59 +01:00
|
|
|
assignedDevice = Device;
|
2017-02-21 14:17:07 +01:00
|
|
|
if (caPidReceiver) {
|
|
|
|
caPidReceiver->Reset();
|
|
|
|
Device->AttachReceiver(caPidReceiver);
|
|
|
|
}
|
|
|
|
dsyslog("CAM %d: assigned to device %d", MasterSlotNumber(), Device->DeviceNumber() + 1);
|
2005-11-26 13:39:47 +01:00
|
|
|
}
|
2015-01-30 13:38:44 +01:00
|
|
|
else {
|
|
|
|
CancelActivation();
|
2017-02-21 14:17:07 +01:00
|
|
|
dsyslog("CAM %d: unassigned from device %d", MasterSlotNumber(), OldDeviceNumber);
|
2015-01-30 13:38:44 +01:00
|
|
|
}
|
2007-01-07 14:46:14 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-05-28 15:54:22 +02:00
|
|
|
bool cCamSlot::Devices(cVector<int> &DeviceNumbers)
|
2017-03-18 16:33:59 +01:00
|
|
|
{
|
|
|
|
cMutexLock MutexLock(&mutex);
|
|
|
|
if (mtdHandler)
|
2019-05-28 15:54:22 +02:00
|
|
|
return mtdHandler->Devices(DeviceNumbers);
|
2017-03-18 16:33:59 +01:00
|
|
|
if (assignedDevice)
|
2019-05-28 15:54:22 +02:00
|
|
|
DeviceNumbers.Append(assignedDevice->DeviceNumber());
|
|
|
|
return DeviceNumbers.Size() > 0;
|
2017-03-18 16:33:59 +01:00
|
|
|
}
|
|
|
|
|
2007-01-07 14:46:14 +01:00
|
|
|
void cCamSlot::NewConnection(void)
|
|
|
|
{
|
|
|
|
cMutexLock MutexLock(&mutex);
|
|
|
|
for (int i = 1; i <= MAX_CONNECTIONS_PER_CAM_SLOT; i++) {
|
|
|
|
if (!tc[i]) {
|
|
|
|
tc[i] = new cCiTransportConnection(this, i);
|
|
|
|
tc[i]->CreateConnection();
|
|
|
|
return;
|
2003-02-09 11:54:22 +01:00
|
|
|
}
|
|
|
|
}
|
2007-01-07 14:46:14 +01:00
|
|
|
esyslog("ERROR: CAM %d: can't create new transport connection!", slotNumber);
|
|
|
|
}
|
|
|
|
|
|
|
|
void cCamSlot::DeleteAllConnections(void)
|
|
|
|
{
|
|
|
|
cMutexLock MutexLock(&mutex);
|
|
|
|
for (int i = 1; i <= MAX_CONNECTIONS_PER_CAM_SLOT; i++) {
|
|
|
|
delete tc[i];
|
|
|
|
tc[i] = NULL;
|
2003-02-09 11:54:22 +01:00
|
|
|
}
|
2003-01-06 14:44:27 +01:00
|
|
|
}
|
|
|
|
|
2007-01-07 14:46:14 +01:00
|
|
|
void cCamSlot::Process(cTPDU *TPDU)
|
2005-11-26 13:39:47 +01:00
|
|
|
{
|
|
|
|
cMutexLock MutexLock(&mutex);
|
2007-01-07 14:46:14 +01:00
|
|
|
if (TPDU) {
|
|
|
|
int n = TPDU->Tcid();
|
|
|
|
if (1 <= n && n <= MAX_CONNECTIONS_PER_CAM_SLOT) {
|
|
|
|
if (tc[n])
|
|
|
|
tc[n]->Process(TPDU);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (int i = 1; i <= MAX_CONNECTIONS_PER_CAM_SLOT; i++) {
|
|
|
|
if (tc[i]) {
|
|
|
|
if (!tc[i]->Process()) {
|
2017-03-18 16:33:59 +01:00
|
|
|
Reset();
|
|
|
|
return;
|
|
|
|
}
|
2005-11-26 13:39:47 +01:00
|
|
|
}
|
2007-01-07 14:46:14 +01:00
|
|
|
}
|
|
|
|
if (moduleCheckTimer.TimedOut()) {
|
|
|
|
eModuleStatus ms = ModuleStatus();
|
|
|
|
if (ms != lastModuleStatus) {
|
|
|
|
switch (ms) {
|
|
|
|
case msNone:
|
|
|
|
dbgprotocol("Slot %d: no module present\n", slotNumber);
|
|
|
|
isyslog("CAM %d: no module present", slotNumber);
|
2017-04-24 09:25:29 +02:00
|
|
|
StopDecrypting();
|
2007-01-07 14:46:14 +01:00
|
|
|
DeleteAllConnections();
|
2015-01-30 13:38:44 +01:00
|
|
|
CancelActivation();
|
2017-04-26 09:18:26 +02:00
|
|
|
if (mtdHandler)
|
|
|
|
mtdHandler->UnAssignAll();
|
|
|
|
else
|
|
|
|
Assign(NULL);
|
2007-01-07 14:46:14 +01:00
|
|
|
break;
|
|
|
|
case msReset:
|
|
|
|
dbgprotocol("Slot %d: module reset\n", slotNumber);
|
|
|
|
isyslog("CAM %d: module reset", slotNumber);
|
|
|
|
DeleteAllConnections();
|
|
|
|
break;
|
|
|
|
case msPresent:
|
|
|
|
dbgprotocol("Slot %d: module present\n", slotNumber);
|
|
|
|
isyslog("CAM %d: module present", slotNumber);
|
|
|
|
break;
|
|
|
|
case msReady:
|
|
|
|
dbgprotocol("Slot %d: module ready\n", slotNumber);
|
|
|
|
isyslog("CAM %d: module ready", slotNumber);
|
|
|
|
NewConnection();
|
2017-03-18 16:33:59 +01:00
|
|
|
resendPmt = true;
|
2007-01-07 14:46:14 +01:00
|
|
|
break;
|
2009-12-06 12:57:45 +01:00
|
|
|
default:
|
|
|
|
esyslog("ERROR: unknown module status %d (%s)", ms, __FUNCTION__);
|
2007-01-07 14:46:14 +01:00
|
|
|
}
|
|
|
|
lastModuleStatus = ms;
|
|
|
|
}
|
|
|
|
moduleCheckTimer.Set(MODULE_CHECK_INTERVAL);
|
|
|
|
}
|
2017-03-18 16:33:59 +01:00
|
|
|
if (resendPmt && Ready()) {
|
|
|
|
if (mtdHandler) {
|
|
|
|
mtdHandler->StartDecrypting();
|
|
|
|
resendPmt = false;
|
|
|
|
}
|
|
|
|
else if (caProgramList.Count())
|
|
|
|
StartDecrypting();
|
|
|
|
}
|
2007-01-07 14:46:14 +01:00
|
|
|
processed.Broadcast();
|
|
|
|
}
|
|
|
|
|
|
|
|
cCiSession *cCamSlot::GetSessionByResourceId(uint32_t ResourceId)
|
|
|
|
{
|
|
|
|
cMutexLock MutexLock(&mutex);
|
|
|
|
return tc[1] ? tc[1]->GetSessionByResourceId(ResourceId) : NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cCamSlot::Write(cTPDU *TPDU)
|
|
|
|
{
|
|
|
|
cMutexLock MutexLock(&mutex);
|
|
|
|
if (ciAdapter && TPDU->Size()) {
|
|
|
|
TPDU->Dump(SlotNumber(), true);
|
|
|
|
ciAdapter->Write(TPDU->Buffer(), TPDU->Size());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cCamSlot::Reset(void)
|
|
|
|
{
|
|
|
|
cMutexLock MutexLock(&mutex);
|
|
|
|
ChannelCamRelations.Reset(slotNumber);
|
|
|
|
DeleteAllConnections();
|
|
|
|
if (ciAdapter) {
|
|
|
|
dbgprotocol("Slot %d: reset...", slotNumber);
|
|
|
|
if (ciAdapter->Reset(slotIndex)) {
|
|
|
|
resetTime = time(NULL);
|
|
|
|
dbgprotocol("ok.\n");
|
2012-05-29 11:19:56 +02:00
|
|
|
lastModuleStatus = msReset;
|
2007-01-07 14:46:14 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
dbgprotocol("failed!\n");
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-01-30 13:38:44 +01:00
|
|
|
bool cCamSlot::CanActivate(void)
|
|
|
|
{
|
|
|
|
return ModuleStatus() == msReady;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cCamSlot::StartActivation(void)
|
|
|
|
{
|
|
|
|
cMutexLock MutexLock(&mutex);
|
|
|
|
if (!caActivationReceiver) {
|
|
|
|
if (cDevice *d = Device()) {
|
2015-09-01 11:14:27 +02:00
|
|
|
LOCK_CHANNELS_READ;
|
|
|
|
if (const cChannel *Channel = Channels->GetByNumber(cDevice::CurrentChannel())) {
|
2015-01-30 13:38:44 +01:00
|
|
|
caActivationReceiver = new cCaActivationReceiver(Channel, this);
|
|
|
|
d->AttachReceiver(caActivationReceiver);
|
|
|
|
dsyslog("CAM %d: activating on device %d with channel %d (%s)", SlotNumber(), d->DeviceNumber() + 1, Channel->Number(), Channel->Name());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void cCamSlot::CancelActivation(void)
|
|
|
|
{
|
|
|
|
cMutexLock MutexLock(&mutex);
|
2017-03-18 16:33:59 +01:00
|
|
|
if (mtdHandler)
|
|
|
|
mtdHandler->CancelActivation();
|
|
|
|
else {
|
|
|
|
delete caActivationReceiver;
|
|
|
|
caActivationReceiver = NULL;
|
|
|
|
}
|
2015-01-30 13:38:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool cCamSlot::IsActivating(void)
|
|
|
|
{
|
2017-03-18 16:33:59 +01:00
|
|
|
if (mtdHandler)
|
|
|
|
return mtdHandler->IsActivating();
|
2015-01-30 13:38:44 +01:00
|
|
|
return caActivationReceiver;
|
|
|
|
}
|
|
|
|
|
2007-01-07 14:46:14 +01:00
|
|
|
eModuleStatus cCamSlot::ModuleStatus(void)
|
|
|
|
{
|
|
|
|
cMutexLock MutexLock(&mutex);
|
|
|
|
eModuleStatus ms = ciAdapter ? ciAdapter->ModuleStatus(slotIndex) : msNone;
|
|
|
|
if (resetTime) {
|
|
|
|
if (ms <= msReset) {
|
|
|
|
if (time(NULL) - resetTime < MODULE_RESET_TIMEOUT)
|
|
|
|
return msReset;
|
|
|
|
}
|
|
|
|
resetTime = 0;
|
2005-11-26 13:39:47 +01:00
|
|
|
}
|
2007-01-07 14:46:14 +01:00
|
|
|
return ms;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *cCamSlot::GetCamName(void)
|
|
|
|
{
|
|
|
|
cMutexLock MutexLock(&mutex);
|
|
|
|
return tc[1] ? tc[1]->GetCamName() : NULL;
|
2005-11-26 13:39:47 +01:00
|
|
|
}
|
|
|
|
|
2007-01-07 14:46:14 +01:00
|
|
|
bool cCamSlot::Ready(void)
|
2003-01-06 14:44:27 +01:00
|
|
|
{
|
|
|
|
cMutexLock MutexLock(&mutex);
|
2007-01-07 14:46:14 +01:00
|
|
|
return ModuleStatus() == msNone || tc[1] && tc[1]->Ready();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cCamSlot::HasMMI(void)
|
|
|
|
{
|
|
|
|
return GetSessionByResourceId(RI_MMI);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cCamSlot::HasUserIO(void)
|
|
|
|
{
|
|
|
|
cMutexLock MutexLock(&mutex);
|
|
|
|
return tc[1] && tc[1]->HasUserIO();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cCamSlot::EnterMenu(void)
|
|
|
|
{
|
|
|
|
cMutexLock MutexLock(&mutex);
|
|
|
|
cCiApplicationInformation *api = (cCiApplicationInformation *)GetSessionByResourceId(RI_APPLICATION_INFORMATION);
|
2003-01-06 14:44:27 +01:00
|
|
|
return api ? api->EnterMenu() : false;
|
|
|
|
}
|
|
|
|
|
2007-01-07 14:46:14 +01:00
|
|
|
cCiMenu *cCamSlot::GetMenu(void)
|
2003-01-06 14:44:27 +01:00
|
|
|
{
|
|
|
|
cMutexLock MutexLock(&mutex);
|
2007-01-07 14:46:14 +01:00
|
|
|
cCiMMI *mmi = (cCiMMI *)GetSessionByResourceId(RI_MMI);
|
|
|
|
if (mmi) {
|
|
|
|
cCiMenu *Menu = mmi->Menu();
|
|
|
|
if (Menu)
|
|
|
|
Menu->mutex = &mutex;
|
|
|
|
return Menu;
|
|
|
|
}
|
2003-02-09 11:54:22 +01:00
|
|
|
return NULL;
|
2003-01-06 14:44:27 +01:00
|
|
|
}
|
|
|
|
|
2007-01-07 14:46:14 +01:00
|
|
|
cCiEnquiry *cCamSlot::GetEnquiry(void)
|
2003-01-06 14:44:27 +01:00
|
|
|
{
|
|
|
|
cMutexLock MutexLock(&mutex);
|
2007-01-07 14:46:14 +01:00
|
|
|
cCiMMI *mmi = (cCiMMI *)GetSessionByResourceId(RI_MMI);
|
|
|
|
if (mmi) {
|
|
|
|
cCiEnquiry *Enquiry = mmi->Enquiry();
|
|
|
|
if (Enquiry)
|
|
|
|
Enquiry->mutex = &mutex;
|
|
|
|
return Enquiry;
|
|
|
|
}
|
2003-02-09 11:54:22 +01:00
|
|
|
return NULL;
|
2003-01-06 14:44:27 +01:00
|
|
|
}
|
|
|
|
|
2017-03-18 16:33:59 +01:00
|
|
|
cCiCaPmtList::~cCiCaPmtList()
|
|
|
|
{
|
|
|
|
for (int i = 0; i < caPmts.Size(); i++)
|
|
|
|
delete caPmts[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
cCiCaPmt *cCiCaPmtList::Add(uint8_t CmdId, int Source, int Transponder, int ProgramNumber, const int *CaSystemIds)
|
|
|
|
{
|
|
|
|
cCiCaPmt *p = new cCiCaPmt(CmdId, Source, Transponder, ProgramNumber, CaSystemIds);
|
|
|
|
caPmts.Append(p);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cCiCaPmtList::Del(cCiCaPmt *CaPmt)
|
|
|
|
{
|
|
|
|
if (caPmts.RemoveElement(CaPmt))
|
|
|
|
delete CaPmt;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cCamSlot::RepliesToQuery(void)
|
2004-02-08 15:11:07 +01:00
|
|
|
{
|
|
|
|
cMutexLock MutexLock(&mutex);
|
2007-01-07 14:46:14 +01:00
|
|
|
cCiConditionalAccessSupport *cas = (cCiConditionalAccessSupport *)GetSessionByResourceId(RI_CONDITIONAL_ACCESS_SUPPORT);
|
2017-03-18 16:33:59 +01:00
|
|
|
return cas && cas->RepliesToQuery();
|
|
|
|
}
|
|
|
|
|
|
|
|
void cCamSlot::BuildCaPmts(uint8_t CmdId, cCiCaPmtList &CaPmtList, cMtdMapper *MtdMapper)
|
|
|
|
{
|
|
|
|
cMutexLock MutexLock(&mutex);
|
|
|
|
CaPmtList.caPmts.Clear();
|
|
|
|
const int *CaSystemIds = GetCaSystemIds();
|
|
|
|
if (CaSystemIds && *CaSystemIds) {
|
|
|
|
if (caProgramList.Count()) {
|
|
|
|
for (cCiCaProgramData *p = caProgramList.First(); p; p = caProgramList.Next(p)) {
|
|
|
|
if (p->modified || resendPmt) {
|
|
|
|
bool Active = p->Active();
|
|
|
|
cCiCaPmt *CaPmt = CaPmtList.Add(Active ? CmdId : CPCI_NOT_SELECTED, source, transponder, p->programNumber, CaSystemIds);
|
|
|
|
for (cCiCaPidData *q = p->pidList.First(); q; q = p->pidList.Next(q)) {
|
|
|
|
if (q->active)
|
|
|
|
CaPmt->AddPid(q->pid, q->streamType);
|
2007-01-07 14:46:14 +01:00
|
|
|
}
|
2017-03-18 16:33:59 +01:00
|
|
|
if (caPidReceiver) {
|
|
|
|
int CaPids[MAXRECEIVEPIDS + 1];
|
|
|
|
if (GetCaPids(source, transponder, p->programNumber, CaSystemIds, MAXRECEIVEPIDS + 1, CaPids) > 0) {
|
|
|
|
if (Active)
|
|
|
|
caPidReceiver->AddPids(CaPids);
|
2019-03-19 14:58:06 +01:00
|
|
|
else {
|
|
|
|
KeepSharedCaPids(p->programNumber, CaSystemIds, CaPids);
|
2017-03-18 16:33:59 +01:00
|
|
|
caPidReceiver->DelPids(CaPids);
|
2019-03-19 14:58:06 +01:00
|
|
|
}
|
2017-03-18 16:33:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (RepliesToQuery())
|
|
|
|
CaPmt->SetListManagement(Active ? CPLM_ADD : CPLM_UPDATE);
|
|
|
|
if (MtdMapper)
|
|
|
|
CaPmt->MtdMapPids(MtdMapper);
|
|
|
|
p->modified = false;
|
2007-01-07 14:46:14 +01:00
|
|
|
}
|
2017-03-18 16:33:59 +01:00
|
|
|
}
|
|
|
|
}
|
2017-03-23 14:34:53 +01:00
|
|
|
else if (CmdId == CPCI_NOT_SELECTED)
|
|
|
|
CaPmtList.Add(CmdId, 0, 0, 0, NULL);
|
2017-03-18 16:33:59 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-19 14:58:06 +01:00
|
|
|
void cCamSlot::KeepSharedCaPids(int ProgramNumber, const int *CaSystemIds, int *CaPids)
|
|
|
|
{
|
|
|
|
int numPids = 0;
|
|
|
|
int *pCaPids = CaPids;
|
|
|
|
while (*pCaPids) {
|
|
|
|
numPids++;
|
|
|
|
pCaPids++;
|
|
|
|
}
|
|
|
|
if (numPids <= 0)
|
|
|
|
return;
|
|
|
|
int CaPids2[MAXRECEIVEPIDS + 1];
|
|
|
|
for (cCiCaProgramData *p = caProgramList.First(); p; p = caProgramList.Next(p)) {
|
2020-07-01 15:16:21 +02:00
|
|
|
if (p->Active()) {
|
2019-03-19 14:58:06 +01:00
|
|
|
if (GetCaPids(source, transponder, p->programNumber, CaSystemIds, MAXRECEIVEPIDS + 1, CaPids2) > 0) {
|
|
|
|
int *pCaPids2 = CaPids2;
|
|
|
|
while (*pCaPids2) {
|
|
|
|
pCaPids = CaPids;
|
|
|
|
while (*pCaPids) {
|
|
|
|
if (*pCaPids == *pCaPids2) {
|
|
|
|
dsyslog("CAM %d: keeping shared CA pid %d", SlotNumber(), *pCaPids);
|
|
|
|
// To remove *pCaPids from CaPids we overwrite it with the last valie in the list, and then strip the last value:
|
|
|
|
*pCaPids = CaPids[numPids - 1];
|
|
|
|
numPids--;
|
|
|
|
CaPids[numPids] = 0;
|
|
|
|
if (numPids <= 0)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
pCaPids++;
|
|
|
|
}
|
|
|
|
pCaPids2++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-18 16:33:59 +01:00
|
|
|
void cCamSlot::SendCaPmts(cCiCaPmtList &CaPmtList)
|
|
|
|
{
|
|
|
|
cMutexLock MutexLock(&mutex);
|
|
|
|
cCiConditionalAccessSupport *cas = (cCiConditionalAccessSupport *)GetSessionByResourceId(RI_CONDITIONAL_ACCESS_SUPPORT);
|
|
|
|
if (cas) {
|
|
|
|
for (int i = 0; i < CaPmtList.caPmts.Size(); i++)
|
|
|
|
cas->SendPMT(CaPmtList.caPmts[i]);
|
|
|
|
}
|
|
|
|
resendPmt = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cCamSlot::SendCaPmt(uint8_t CmdId)
|
|
|
|
{
|
|
|
|
cMutexLock MutexLock(&mutex);
|
|
|
|
cCiCaPmtList CaPmtList;
|
|
|
|
BuildCaPmts(CmdId, CaPmtList);
|
|
|
|
SendCaPmts(CaPmtList);
|
|
|
|
}
|
|
|
|
|
|
|
|
void cCamSlot::MtdEnable(void)
|
|
|
|
{
|
|
|
|
mtdAvailable = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cCamSlot::MtdActivate(bool On)
|
|
|
|
{
|
|
|
|
if (McdAvailable() && MtdAvailable()) {
|
|
|
|
if (On) {
|
|
|
|
if (!mtdHandler) {
|
|
|
|
dsyslog("CAM %d: activating MTD support", SlotNumber());
|
|
|
|
mtdHandler = new cMtdHandler;
|
2007-01-07 14:46:14 +01:00
|
|
|
}
|
|
|
|
}
|
2017-03-18 16:33:59 +01:00
|
|
|
else if (mtdHandler) {
|
|
|
|
dsyslog("CAM %d: deactivating MTD support", SlotNumber());
|
|
|
|
delete mtdHandler;
|
|
|
|
mtdHandler = NULL;
|
|
|
|
}
|
2007-01-07 14:46:14 +01:00
|
|
|
}
|
2004-02-08 15:11:07 +01:00
|
|
|
}
|
|
|
|
|
2017-03-18 16:33:59 +01:00
|
|
|
int cCamSlot::MtdPutData(uchar *Data, int Count)
|
|
|
|
{
|
|
|
|
return mtdHandler->Put(Data, Count);
|
|
|
|
}
|
|
|
|
|
2007-01-07 14:46:14 +01:00
|
|
|
const int *cCamSlot::GetCaSystemIds(void)
|
2003-01-06 14:44:27 +01:00
|
|
|
{
|
|
|
|
cMutexLock MutexLock(&mutex);
|
2007-01-07 14:46:14 +01:00
|
|
|
cCiConditionalAccessSupport *cas = (cCiConditionalAccessSupport *)GetSessionByResourceId(RI_CONDITIONAL_ACCESS_SUPPORT);
|
2003-04-18 12:48:49 +02:00
|
|
|
return cas ? cas->GetCaSystemIds() : NULL;
|
|
|
|
}
|
|
|
|
|
2007-01-07 14:46:14 +01:00
|
|
|
int cCamSlot::Priority(void)
|
2004-01-04 12:30:00 +01:00
|
|
|
{
|
2017-03-18 16:33:59 +01:00
|
|
|
if (mtdHandler)
|
|
|
|
return mtdHandler->Priority();
|
2007-01-07 14:46:14 +01:00
|
|
|
cDevice *d = Device();
|
2012-03-02 10:19:00 +01:00
|
|
|
return d ? d->Priority() : IDLEPRIORITY;
|
2004-01-04 12:30:00 +01:00
|
|
|
}
|
|
|
|
|
2007-01-07 14:46:14 +01:00
|
|
|
bool cCamSlot::ProvidesCa(const int *CaSystemIds)
|
2003-04-18 12:48:49 +02:00
|
|
|
{
|
|
|
|
cMutexLock MutexLock(&mutex);
|
2007-01-07 14:46:14 +01:00
|
|
|
cCiConditionalAccessSupport *cas = (cCiConditionalAccessSupport *)GetSessionByResourceId(RI_CONDITIONAL_ACCESS_SUPPORT);
|
|
|
|
if (cas) {
|
|
|
|
for (const int *ids = cas->GetCaSystemIds(); ids && *ids; ids++) {
|
|
|
|
for (const int *id = CaSystemIds; *id; id++) {
|
|
|
|
if (*id == *ids)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2005-11-26 13:39:47 +01:00
|
|
|
}
|
2007-01-07 14:46:14 +01:00
|
|
|
return false;
|
2005-11-26 13:39:47 +01:00
|
|
|
}
|
|
|
|
|
2007-01-07 14:46:14 +01:00
|
|
|
void cCamSlot::AddPid(int ProgramNumber, int Pid, int StreamType)
|
2005-11-26 13:39:47 +01:00
|
|
|
{
|
|
|
|
cMutexLock MutexLock(&mutex);
|
|
|
|
cCiCaProgramData *ProgramData = NULL;
|
|
|
|
for (cCiCaProgramData *p = caProgramList.First(); p; p = caProgramList.Next(p)) {
|
|
|
|
if (p->programNumber == ProgramNumber) {
|
|
|
|
ProgramData = p;
|
|
|
|
for (cCiCaPidData *q = p->pidList.First(); q; q = p->pidList.Next(q)) {
|
|
|
|
if (q->pid == Pid)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!ProgramData)
|
|
|
|
caProgramList.Add(ProgramData = new cCiCaProgramData(ProgramNumber));
|
|
|
|
ProgramData->pidList.Add(new cCiCaPidData(Pid, StreamType));
|
|
|
|
}
|
|
|
|
|
2007-01-07 14:46:14 +01:00
|
|
|
void cCamSlot::SetPid(int Pid, bool Active)
|
2005-11-26 13:39:47 +01:00
|
|
|
{
|
2018-01-28 11:21:28 +01:00
|
|
|
if (caPidReceiver && caPidReceiver->HandlingPid())
|
|
|
|
return;
|
2005-11-26 13:39:47 +01:00
|
|
|
cMutexLock MutexLock(&mutex);
|
|
|
|
for (cCiCaProgramData *p = caProgramList.First(); p; p = caProgramList.Next(p)) {
|
|
|
|
for (cCiCaPidData *q = p->pidList.First(); q; q = p->pidList.Next(q)) {
|
|
|
|
if (q->pid == Pid) {
|
2007-01-07 14:46:14 +01:00
|
|
|
if (q->active != Active) {
|
|
|
|
q->active = Active;
|
|
|
|
p->modified = true;
|
|
|
|
}
|
2005-11-26 13:39:47 +01:00
|
|
|
return;
|
|
|
|
}
|
2017-03-18 16:33:59 +01:00
|
|
|
}
|
2005-11-26 13:39:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-07 14:46:14 +01:00
|
|
|
// see ISO/IEC 13818-1
|
2011-05-21 15:24:10 +02:00
|
|
|
#define STREAM_TYPE_VIDEO 0x02
|
|
|
|
#define STREAM_TYPE_AUDIO 0x04
|
|
|
|
#define STREAM_TYPE_PRIVATE 0x06
|
2007-01-07 14:46:14 +01:00
|
|
|
|
|
|
|
void cCamSlot::AddChannel(const cChannel *Channel)
|
2005-11-26 13:39:47 +01:00
|
|
|
{
|
|
|
|
cMutexLock MutexLock(&mutex);
|
2007-01-07 14:46:14 +01:00
|
|
|
if (source != Channel->Source() || transponder != Channel->Transponder())
|
|
|
|
StopDecrypting();
|
|
|
|
source = Channel->Source();
|
|
|
|
transponder = Channel->Transponder();
|
|
|
|
if (Channel->Ca() >= CA_ENCRYPTED_MIN) {
|
|
|
|
AddPid(Channel->Sid(), Channel->Vpid(), STREAM_TYPE_VIDEO);
|
|
|
|
for (const int *Apid = Channel->Apids(); *Apid; Apid++)
|
|
|
|
AddPid(Channel->Sid(), *Apid, STREAM_TYPE_AUDIO);
|
|
|
|
for (const int *Dpid = Channel->Dpids(); *Dpid; Dpid++)
|
2011-05-21 15:24:10 +02:00
|
|
|
AddPid(Channel->Sid(), *Dpid, STREAM_TYPE_PRIVATE);
|
|
|
|
for (const int *Spid = Channel->Spids(); *Spid; Spid++)
|
|
|
|
AddPid(Channel->Sid(), *Spid, STREAM_TYPE_PRIVATE);
|
2007-01-07 14:46:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#define QUERY_REPLY_WAIT 100 // ms to wait between checks for a reply
|
|
|
|
|
2017-03-23 14:34:53 +01:00
|
|
|
bool cCamSlot::CanDecrypt(const cChannel *Channel, cMtdMapper *MtdMapper)
|
2007-01-07 14:46:14 +01:00
|
|
|
{
|
|
|
|
if (Channel->Ca() < CA_ENCRYPTED_MIN)
|
|
|
|
return true; // channel not encrypted
|
|
|
|
if (!IsDecrypting())
|
|
|
|
return true; // any CAM can decrypt at least one channel
|
|
|
|
cMutexLock MutexLock(&mutex);
|
|
|
|
cCiConditionalAccessSupport *cas = (cCiConditionalAccessSupport *)GetSessionByResourceId(RI_CONDITIONAL_ACCESS_SUPPORT);
|
|
|
|
if (cas && cas->RepliesToQuery()) {
|
|
|
|
cCiCaPmt CaPmt(CPCI_QUERY, Channel->Source(), Channel->Transponder(), Channel->Sid(), GetCaSystemIds());
|
|
|
|
CaPmt.SetListManagement(CPLM_ADD); // WORKAROUND: CPLM_ONLY doesn't work with Alphacrypt 3.09 (deletes existing CA_PMTs)
|
|
|
|
CaPmt.AddPid(Channel->Vpid(), STREAM_TYPE_VIDEO);
|
|
|
|
for (const int *Apid = Channel->Apids(); *Apid; Apid++)
|
|
|
|
CaPmt.AddPid(*Apid, STREAM_TYPE_AUDIO);
|
|
|
|
for (const int *Dpid = Channel->Dpids(); *Dpid; Dpid++)
|
2011-05-21 15:24:10 +02:00
|
|
|
CaPmt.AddPid(*Dpid, STREAM_TYPE_PRIVATE);
|
|
|
|
for (const int *Spid = Channel->Spids(); *Spid; Spid++)
|
2013-02-17 13:19:36 +01:00
|
|
|
CaPmt.AddPid(*Spid, STREAM_TYPE_PRIVATE);
|
2017-03-23 14:34:53 +01:00
|
|
|
if (MtdMapper)
|
|
|
|
CaPmt.MtdMapPids(MtdMapper);
|
2007-01-07 14:46:14 +01:00
|
|
|
cas->SendPMT(&CaPmt);
|
|
|
|
cTimeMs Timeout(QUERY_REPLY_TIMEOUT);
|
|
|
|
do {
|
|
|
|
processed.TimedWait(mutex, QUERY_REPLY_WAIT);
|
|
|
|
if ((cas = (cCiConditionalAccessSupport *)GetSessionByResourceId(RI_CONDITIONAL_ACCESS_SUPPORT)) != NULL) { // must re-fetch it, there might have been a reset
|
|
|
|
if (cas->ReceivedReply())
|
|
|
|
return cas->CanDecrypt();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
} while (!Timeout.TimedOut());
|
|
|
|
dsyslog("CAM %d: didn't reply to QUERY", SlotNumber());
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cCamSlot::StartDecrypting(void)
|
|
|
|
{
|
|
|
|
SendCaPmt(CPCI_OK_DESCRAMBLING);
|
|
|
|
}
|
|
|
|
|
|
|
|
void cCamSlot::StopDecrypting(void)
|
|
|
|
{
|
|
|
|
cMutexLock MutexLock(&mutex);
|
2020-06-16 14:33:32 +02:00
|
|
|
if (mtdHandler) {
|
|
|
|
mtdHandler->StopDecrypting();
|
|
|
|
return;
|
|
|
|
}
|
2017-03-23 14:34:53 +01:00
|
|
|
if (caProgramList.Count()) {
|
|
|
|
caProgramList.Clear();
|
2020-06-16 14:33:32 +02:00
|
|
|
if (!dynamic_cast<cMtdCamSlot *>(this) || !MasterSlot()->IsDecrypting())
|
2017-03-23 14:34:53 +01:00
|
|
|
SendCaPmt(CPCI_NOT_SELECTED);
|
|
|
|
}
|
2007-01-07 14:46:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool cCamSlot::IsDecrypting(void)
|
|
|
|
{
|
|
|
|
cMutexLock MutexLock(&mutex);
|
2017-03-18 16:33:59 +01:00
|
|
|
if (mtdHandler)
|
|
|
|
return mtdHandler->IsDecrypting();
|
2007-01-07 14:46:14 +01:00
|
|
|
if (caProgramList.Count()) {
|
|
|
|
for (cCiCaProgramData *p = caProgramList.First(); p; p = caProgramList.Next(p)) {
|
|
|
|
if (p->modified)
|
|
|
|
return true; // any modifications need to be processed before we can assume it's no longer decrypting
|
|
|
|
for (cCiCaPidData *q = p->pidList.First(); q; q = p->pidList.Next(q)) {
|
|
|
|
if (q->active)
|
|
|
|
return true;
|
2005-11-26 13:39:47 +01:00
|
|
|
}
|
|
|
|
}
|
2007-01-07 14:46:14 +01:00
|
|
|
}
|
2005-11-26 13:39:47 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-01-02 10:47:08 +01:00
|
|
|
uchar *cCamSlot::Decrypt(uchar *Data, int &Count)
|
2013-12-28 13:29:54 +01:00
|
|
|
{
|
2017-04-15 09:39:55 +02:00
|
|
|
if (Data)
|
|
|
|
Count = TS_SIZE;
|
2014-01-02 10:47:08 +01:00
|
|
|
return Data;
|
2013-12-28 13:29:54 +01:00
|
|
|
}
|
|
|
|
|
2017-05-18 09:05:46 +02:00
|
|
|
bool cCamSlot::TsPostProcess(uchar *Data)
|
|
|
|
{
|
|
|
|
return tc[1] ? tc[1]->TsPostProcess(Data) : false;
|
|
|
|
}
|
|
|
|
|
2017-05-01 09:32:32 +02:00
|
|
|
bool cCamSlot::Inject(uchar *Data, int Count)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cCamSlot::InjectEit(int Sid)
|
|
|
|
{
|
|
|
|
cEitGenerator Eit(Sid);
|
|
|
|
Inject(Eit.Data(), Eit.Length());
|
|
|
|
}
|
|
|
|
|
2014-01-16 11:57:54 +01:00
|
|
|
// --- cCamSlots -------------------------------------------------------------
|
|
|
|
|
|
|
|
cCamSlots CamSlots;
|
|
|
|
|
2017-01-23 12:01:48 +01:00
|
|
|
int cCamSlots::NumReadyMasterSlots(void)
|
|
|
|
{
|
|
|
|
int n = 0;
|
|
|
|
for (cCamSlot *CamSlot = CamSlots.First(); CamSlot; CamSlot = CamSlots.Next(CamSlot)) {
|
|
|
|
if (CamSlot->IsMasterSlot() && CamSlot->ModuleStatus() == msReady)
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2014-01-16 11:57:54 +01:00
|
|
|
bool cCamSlots::WaitForAllCamSlotsReady(int Timeout)
|
|
|
|
{
|
2017-01-23 12:01:48 +01:00
|
|
|
bool ready = true;
|
2014-01-16 11:57:54 +01:00
|
|
|
for (time_t t0 = time(NULL); time(NULL) - t0 < Timeout; ) {
|
2017-01-23 12:01:48 +01:00
|
|
|
ready = true;
|
2014-01-16 11:57:54 +01:00
|
|
|
for (cCamSlot *CamSlot = CamSlots.First(); CamSlot; CamSlot = CamSlots.Next(CamSlot)) {
|
|
|
|
if (!CamSlot->Ready()) {
|
|
|
|
ready = false;
|
|
|
|
cCondWait::SleepMs(100);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ready)
|
2017-01-23 12:01:48 +01:00
|
|
|
break;
|
2014-01-16 11:57:54 +01:00
|
|
|
}
|
2017-01-23 12:01:48 +01:00
|
|
|
for (cCamSlot *CamSlot = CamSlots.First(); CamSlot; CamSlot = CamSlots.Next(CamSlot))
|
|
|
|
dsyslog("CAM %d: %sready, %s", CamSlot->SlotNumber(), CamSlot->Ready() ? "" : "not ", CamSlot->IsMasterSlot() ? *cString::sprintf("master (%s)", CamSlot->GetCamName() ? CamSlot->GetCamName() : "empty") : *cString::sprintf("slave of CAM %d", CamSlot->MasterSlotNumber()));
|
|
|
|
return ready;
|
2014-01-16 11:57:54 +01:00
|
|
|
}
|
|
|
|
|
2007-01-07 14:46:14 +01:00
|
|
|
// --- cChannelCamRelation ---------------------------------------------------
|
|
|
|
|
|
|
|
#define CAM_CHECKED_TIMEOUT 15 // seconds before a CAM that has been checked for a particular channel will be checked again
|
|
|
|
|
|
|
|
class cChannelCamRelation : public cListObject {
|
|
|
|
private:
|
|
|
|
tChannelID channelID;
|
|
|
|
uint32_t camSlotsChecked;
|
|
|
|
uint32_t camSlotsDecrypt;
|
|
|
|
time_t lastChecked;
|
|
|
|
public:
|
|
|
|
cChannelCamRelation(tChannelID ChannelID);
|
|
|
|
bool TimedOut(void);
|
|
|
|
tChannelID ChannelID(void) { return channelID; }
|
|
|
|
bool CamChecked(int CamSlotNumber);
|
|
|
|
bool CamDecrypt(int CamSlotNumber);
|
|
|
|
void SetChecked(int CamSlotNumber);
|
|
|
|
void SetDecrypt(int CamSlotNumber);
|
|
|
|
void ClrChecked(int CamSlotNumber);
|
|
|
|
void ClrDecrypt(int CamSlotNumber);
|
|
|
|
};
|
|
|
|
|
|
|
|
cChannelCamRelation::cChannelCamRelation(tChannelID ChannelID)
|
|
|
|
{
|
|
|
|
channelID = ChannelID;
|
|
|
|
camSlotsChecked = 0;
|
|
|
|
camSlotsDecrypt = 0;
|
|
|
|
lastChecked = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cChannelCamRelation::TimedOut(void)
|
|
|
|
{
|
|
|
|
return !camSlotsDecrypt && time(NULL) - lastChecked > CAM_CHECKED_TIMEOUT;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cChannelCamRelation::CamChecked(int CamSlotNumber)
|
|
|
|
{
|
|
|
|
if (lastChecked && time(NULL) - lastChecked > CAM_CHECKED_TIMEOUT) {
|
|
|
|
lastChecked = 0;
|
|
|
|
camSlotsChecked = 0;
|
|
|
|
}
|
|
|
|
return camSlotsChecked & (1 << (CamSlotNumber - 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cChannelCamRelation::CamDecrypt(int CamSlotNumber)
|
|
|
|
{
|
|
|
|
return camSlotsDecrypt & (1 << (CamSlotNumber - 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
void cChannelCamRelation::SetChecked(int CamSlotNumber)
|
|
|
|
{
|
|
|
|
camSlotsChecked |= (1 << (CamSlotNumber - 1));
|
|
|
|
lastChecked = time(NULL);
|
|
|
|
ClrDecrypt(CamSlotNumber);
|
|
|
|
}
|
|
|
|
|
|
|
|
void cChannelCamRelation::SetDecrypt(int CamSlotNumber)
|
|
|
|
{
|
|
|
|
camSlotsDecrypt |= (1 << (CamSlotNumber - 1));
|
|
|
|
ClrChecked(CamSlotNumber);
|
|
|
|
}
|
|
|
|
|
|
|
|
void cChannelCamRelation::ClrChecked(int CamSlotNumber)
|
|
|
|
{
|
|
|
|
camSlotsChecked &= ~(1 << (CamSlotNumber - 1));
|
|
|
|
lastChecked = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cChannelCamRelation::ClrDecrypt(int CamSlotNumber)
|
|
|
|
{
|
|
|
|
camSlotsDecrypt &= ~(1 << (CamSlotNumber - 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
// --- cChannelCamRelations --------------------------------------------------
|
|
|
|
|
2017-01-09 13:42:41 +01:00
|
|
|
#define MAX_CAM_NUMBER 32
|
2007-01-07 14:46:14 +01:00
|
|
|
#define CHANNEL_CAM_RELATIONS_CLEANUP_INTERVAL 3600 // seconds between cleanups
|
|
|
|
|
|
|
|
cChannelCamRelations ChannelCamRelations;
|
|
|
|
|
|
|
|
cChannelCamRelations::cChannelCamRelations(void)
|
|
|
|
{
|
|
|
|
lastCleanup = time(NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void cChannelCamRelations::Cleanup(void)
|
|
|
|
{
|
|
|
|
cMutexLock MutexLock(&mutex);
|
|
|
|
if (time(NULL) - lastCleanup > CHANNEL_CAM_RELATIONS_CLEANUP_INTERVAL) {
|
|
|
|
for (cChannelCamRelation *ccr = First(); ccr; ) {
|
|
|
|
cChannelCamRelation *c = ccr;
|
|
|
|
ccr = Next(ccr);
|
|
|
|
if (c->TimedOut())
|
|
|
|
Del(c);
|
|
|
|
}
|
|
|
|
lastCleanup = time(NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cChannelCamRelation *cChannelCamRelations::GetEntry(tChannelID ChannelID)
|
|
|
|
{
|
|
|
|
cMutexLock MutexLock(&mutex);
|
|
|
|
Cleanup();
|
|
|
|
for (cChannelCamRelation *ccr = First(); ccr; ccr = Next(ccr)) {
|
|
|
|
if (ccr->ChannelID() == ChannelID)
|
|
|
|
return ccr;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
cChannelCamRelation *cChannelCamRelations::AddEntry(tChannelID ChannelID)
|
|
|
|
{
|
|
|
|
cMutexLock MutexLock(&mutex);
|
|
|
|
cChannelCamRelation *ccr = GetEntry(ChannelID);
|
|
|
|
if (!ccr)
|
|
|
|
Add(ccr = new cChannelCamRelation(ChannelID));
|
|
|
|
return ccr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cChannelCamRelations::Reset(int CamSlotNumber)
|
|
|
|
{
|
|
|
|
cMutexLock MutexLock(&mutex);
|
|
|
|
for (cChannelCamRelation *ccr = First(); ccr; ccr = Next(ccr)) {
|
|
|
|
ccr->ClrChecked(CamSlotNumber);
|
|
|
|
ccr->ClrDecrypt(CamSlotNumber);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cChannelCamRelations::CamChecked(tChannelID ChannelID, int CamSlotNumber)
|
|
|
|
{
|
|
|
|
cMutexLock MutexLock(&mutex);
|
|
|
|
cChannelCamRelation *ccr = GetEntry(ChannelID);
|
|
|
|
return ccr ? ccr->CamChecked(CamSlotNumber) : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cChannelCamRelations::CamDecrypt(tChannelID ChannelID, int CamSlotNumber)
|
|
|
|
{
|
|
|
|
cMutexLock MutexLock(&mutex);
|
|
|
|
cChannelCamRelation *ccr = GetEntry(ChannelID);
|
|
|
|
return ccr ? ccr->CamDecrypt(CamSlotNumber) : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cChannelCamRelations::SetChecked(tChannelID ChannelID, int CamSlotNumber)
|
|
|
|
{
|
|
|
|
cMutexLock MutexLock(&mutex);
|
|
|
|
cChannelCamRelation *ccr = AddEntry(ChannelID);
|
|
|
|
if (ccr)
|
|
|
|
ccr->SetChecked(CamSlotNumber);
|
|
|
|
}
|
|
|
|
|
|
|
|
void cChannelCamRelations::SetDecrypt(tChannelID ChannelID, int CamSlotNumber)
|
|
|
|
{
|
|
|
|
cMutexLock MutexLock(&mutex);
|
|
|
|
cChannelCamRelation *ccr = AddEntry(ChannelID);
|
|
|
|
if (ccr)
|
|
|
|
ccr->SetDecrypt(CamSlotNumber);
|
|
|
|
}
|
|
|
|
|
|
|
|
void cChannelCamRelations::ClrChecked(tChannelID ChannelID, int CamSlotNumber)
|
2005-11-26 13:39:47 +01:00
|
|
|
{
|
|
|
|
cMutexLock MutexLock(&mutex);
|
2007-01-07 14:46:14 +01:00
|
|
|
cChannelCamRelation *ccr = GetEntry(ChannelID);
|
|
|
|
if (ccr)
|
|
|
|
ccr->ClrChecked(CamSlotNumber);
|
2003-01-06 14:44:27 +01:00
|
|
|
}
|
|
|
|
|
2007-01-07 14:46:14 +01:00
|
|
|
void cChannelCamRelations::ClrDecrypt(tChannelID ChannelID, int CamSlotNumber)
|
2003-01-06 14:44:27 +01:00
|
|
|
{
|
|
|
|
cMutexLock MutexLock(&mutex);
|
2007-01-07 14:46:14 +01:00
|
|
|
cChannelCamRelation *ccr = GetEntry(ChannelID);
|
|
|
|
if (ccr)
|
|
|
|
ccr->ClrDecrypt(CamSlotNumber);
|
2003-01-06 14:44:27 +01:00
|
|
|
}
|
2017-01-09 13:42:41 +01:00
|
|
|
|
|
|
|
void cChannelCamRelations::Load(const char *FileName)
|
|
|
|
{
|
|
|
|
cMutexLock MutexLock(&mutex);
|
|
|
|
fileName = FileName;
|
|
|
|
if (access(fileName, R_OK) == 0) {
|
|
|
|
dsyslog("loading %s", *fileName);
|
|
|
|
if (FILE *f = fopen(fileName, "r")) {
|
|
|
|
cReadLine ReadLine;
|
|
|
|
char *s;
|
|
|
|
while ((s = ReadLine.Read(f)) != NULL) {
|
|
|
|
if (char *p = strchr(s, ' ')) {
|
|
|
|
*p = 0;
|
|
|
|
if (*++p) {
|
|
|
|
tChannelID ChannelID = tChannelID::FromString(s);
|
|
|
|
if (ChannelID.Valid()) {
|
|
|
|
char *q;
|
|
|
|
char *strtok_next;
|
|
|
|
while ((q = strtok_r(p, " ", &strtok_next)) != NULL) {
|
|
|
|
int CamSlotNumber = atoi(q);
|
|
|
|
if (CamSlotNumber >= 1 && CamSlotNumber <= MAX_CAM_NUMBER)
|
|
|
|
SetDecrypt(ChannelID, CamSlotNumber);
|
|
|
|
p = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
LOG_ERROR_STR(*fileName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void cChannelCamRelations::Save(void)
|
|
|
|
{
|
2018-03-19 16:39:58 +01:00
|
|
|
if (!*fileName)
|
|
|
|
return;
|
2017-01-09 13:42:41 +01:00
|
|
|
cMutexLock MutexLock(&mutex);
|
2017-04-02 10:06:31 +02:00
|
|
|
struct stat st;
|
|
|
|
if (stat(fileName, &st) == 0) {
|
|
|
|
if ((st.st_mode & S_IWUSR) == 0) {
|
|
|
|
dsyslog("not saving %s (file is read-only)", *fileName);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2017-01-09 13:42:41 +01:00
|
|
|
dsyslog("saving %s", *fileName);
|
|
|
|
cSafeFile f(fileName);
|
|
|
|
if (f.Open()) {
|
|
|
|
for (cChannelCamRelation *ccr = First(); ccr; ccr = Next(ccr)) {
|
|
|
|
if (ccr->ChannelID().Valid()) {
|
|
|
|
cString s;
|
|
|
|
for (int i = 1; i <= MAX_CAM_NUMBER; i++) {
|
|
|
|
if (ccr->CamDecrypt(i))
|
|
|
|
s = cString::sprintf("%s%s%d", *s ? *s : "", *s ? " " : "", i);
|
|
|
|
}
|
|
|
|
if (*s)
|
|
|
|
fprintf(f, "%s %s\n", *ccr->ChannelID().ToString(), *s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f.Close();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
LOG_ERROR_STR(*fileName);
|
|
|
|
}
|