Go to the documentation of this file.
35 #include <linux/dvb/ca.h>
36 #include <netinet/in.h>
38 #include <sys/ioctl.h>
52 #define esyslog(a...) LOG(VB_GENERAL, LOG_ERR, QString::asprintf(a))
53 #define isyslog(a...) LOG(VB_DVBCAM, LOG_INFO, QString::asprintf(a))
54 #define dsyslog(a...) LOG(VB_DVBCAM, LOG_DEBUG, QString::asprintf(a))
56 #define LOG_ERROR esyslog("ERROR (%s,%d): %m", __FILE__, __LINE__)
57 #define LOG_ERROR_STR(s) esyslog("ERROR: %s: %m", s)
67 #define dbgprotocol(a...) if (sDebugProtocol) LOG(VB_DVBCAM, LOG_DEBUG, QString::asprintf(a))
69 static constexpr
int OK { 0 };
71 static constexpr
int ERROR { -2 };
83 static ssize_t
safe_read(
int filedes,
void *buffer,
size_t size)
86 ssize_t
p =
read(filedes, buffer, size);
87 if (
p < 0 && (errno == EINTR || errno == EAGAIN)) {
88 dsyslog(
"EINTR while reading from file handle %d - retrying", filedes);
95 static const uint8_t *
GetLength(
const uint8_t *Data,
int &Length)
111 for (
int i = 0; i < l; i++)
112 Length = (Length << 8) | *Data++;
132 int n =
sizeof(Length);
133 for (
int i = n - 1; i >= 0; i--) {
134 int b = (Length >> (8 * i)) & 0xFF;
145 static void SetLength(std::vector<uint8_t> &Data,
int Length)
149 Data.push_back(Length);
154 size_t len_offset = Data.size();
157 int n =
sizeof(Length);
158 for (
int i = n - 1; i >= 0; i--) {
159 int b = (Length >> (8 * i)) & 0xFF;
160 if ((len_offset != Data.size()) || b)
172 char *s = (
char *)malloc(Length + 1);
173 strncpy(s, (
char *)Data, Length);
178 static char *
GetString(
int &Length,
const uint8_t **Data)
187 if (Length > 0 && Data && *Data) {
191 Length -=
d - *Data + l;
215 esyslog(
"cMutex Lock inbalance detected");
219 pthread_mutex_unlock(&
m_mutex);
267 std::array<uint8_t,MAX_TPDU_SIZE>
m_data {0};
268 const uint8_t *
GetData(
const uint8_t *
Data,
int &Length)
const;
270 cTPDU(
void) =
default;
279 void Dump(
bool Outgoing);
282 cTPDU::cTPDU(uint8_t Slot, uint8_t Tcid, uint8_t Tag,
int Length,
const uint8_t *Data)
307 esyslog(
"ERROR: illegal data length for TPDU tag 0x%02X: %d",
Tag, Length);
312 uint8_t *
p =
m_data.data() + 3;
316 memcpy(
p,
Data, Length);
320 esyslog(
"ERROR: illegal data length for TPDU tag 0x%02X: %d",
Tag, Length);
323 esyslog(
"ERROR: unknown TPDU tag: 0x%02X",
Tag);
332 esyslog(
"ERROR: attemp to write TPDU with zero size");
351 static constexpr ssize_t MAX_DUMP { 256 };
352 QString msg = QString(
"%1 ").arg(Outgoing ?
"-->" :
"<--");
353 for (
int i = 0; i <
m_size && i < MAX_DUMP; i++)
354 msg += QString(
"%1 ").arg((
short int)
m_data[i], 2, 16, QChar(
'0'));
357 LOG(VB_DVBCAM, LOG_INFO, msg);
360 for (
int i = 0; i <
m_size && i < MAX_DUMP; i++)
361 msg += QString(
"%1 ").arg(isprint(
m_data[i]) ?
m_data[i] :
'.', 2);
364 LOG(VB_DVBCAM, LOG_INFO, msg);
405 void Init(
int Fd, uint8_t
Slot, uint8_t Tcid);
406 int SendTPDU(uint8_t Tag,
int Length = 0,
const uint8_t *
Data =
nullptr)
const;
421 const uint8_t *
Data(
int &Length);
458 std::array<struct pollfd,1> pfd {};
460 pfd[0].events = POLLIN;
465 if (ret == -1 && (errno == EAGAIN || errno == EINTR))
471 (pfd[0].revents & POLLIN) &&
493 default:
return ERROR;
557 dsyslog(
"CAM: retrying to establish connection");
559 dsyslog(
"CAM: connection established");
578 auto curr_time = nowAsDuration<std::chrono::milliseconds>();
579 std::chrono::milliseconds msdiff = curr_time -
m_lastPoll;
600 std::array<cCiTransportConnection,MAX_CI_CONNECT>
m_tc;
621 dbgprotocol(
"Creating connection: slot %d, tcid %zd\n", Slot, i + 1);
623 if (
m_tc[i].CreateConnection() ==
OK)
634 if (ioctl(
m_fd, CA_RESET, 1 << Slot) != -1) {
638 esyslog(
"ERROR: can't reset CAM slot %d: %m", Slot);
645 ca_slot_info_t sinfo;
647 if (ioctl(
m_fd, CA_GET_SLOT_INFO, &sinfo) != -1)
648 return (sinfo.flags & CA_CI_MODULE_READY) != 0U;
649 esyslog(
"ERROR: can't get info on CAM slot %d: %m", Slot);
655 for (
auto & conn :
m_tc) {
657 if (Tc->
Slot() == Slot) {
658 switch (Tc->
State()) {
783 static int GetTag(
int &Length,
const uint8_t **Data);
784 static const uint8_t *
GetData(
const uint8_t *Data,
int &Length);
785 int SendData(
int Tag,
int Length = 0,
const uint8_t *Data =
nullptr);
787 {
return SendData(Tag, Data.size(), Data.data()); };
795 virtual bool Process(
int Length = 0,
const uint8_t *Data =
nullptr);
814 if (Length >= 3 && Data && *Data) {
816 for (
int i = 0; i < 3; i++)
817 t = (
t << 8) | *(*Data)++;
827 return Length ? Data :
nullptr;
834 esyslog(
"ERROR: CAM: data length (%d) is negative", Length);
838 if ((Length > 0) && !Data)
840 esyslog(
"ERROR: CAM: Data pointer null");
844 std::vector<uint8_t> buffer {
848 static_cast<uint8_t
>((Tag >> 16) & 0xFF),
849 static_cast<uint8_t
>((Tag >> 8) & 0xFF),
850 static_cast<uint8_t
>((Tag ) & 0xFF)} ;
851 buffer.reserve(2048);
854 if (buffer.size() + Length >= buffer.capacity())
856 esyslog(
"ERROR: CAM: data length (%d) exceeds buffer size", Length);
862 buffer.insert(buffer.end(), Data, Data + Length);
881 bool Process(
int Length = 0,
const uint8_t *Data =
nullptr)
override;
894 int Tag =
GetTag(Length, &Data);
898 const std::array<const uint32_t,5> resources
908 reinterpret_cast<const uint8_t*
>(resources.data()));
916 const uint8_t *
d =
GetData(Data, l);
918 esyslog(
"CI resource manager: unexpected data");
924 esyslog(
"ERROR: CI resource manager: unexpected tag %06X in state %d", Tag,
m_state);
928 default:
esyslog(
"ERROR: CI resource manager: unknown tag %06X", Tag);
953 bool Process(
int Length = 0,
const uint8_t *Data =
nullptr)
override;
980 int Tag =
GetTag(Length, &Data);
985 const uint8_t *
d =
GetData(Data, l);
986 if ((l -= 1) < 0)
break;
988 if ((l -= 2) < 0)
break;
991 if ((l -= 2) < 0)
break;
1001 default:
esyslog(
"ERROR: CI application information: unknown tag %06X", Tag);
1032 bool Process(
int Length = 0,
const uint8_t *Data =
nullptr)
override;
1048 int Tag =
GetTag(Length, &Data);
1053 const uint8_t *
d =
GetData(Data, l);
1055 unsigned short id = ((
unsigned short)(*
d) << 8) | *(
d + 1);
1074 default:
esyslog(
"ERROR: CI conditional access support: unknown tag %06X", Tag);
1106 bool Process(
int Length = 0,
const uint8_t *Data =
nullptr)
override;
1123 {
return ((
d / 10) << 4) + (
d % 10); }
1125 {
return static_cast<uint8_t
>(a & 0xFF); }
1127 {
return static_cast<uint8_t
>((a >> 8) & 0xFF); }
1131 time_t
t = time(
nullptr);
1132 struct tm tm_gmt {};
1133 struct tm tm_loc {};
1141 if (gmtime_r(&
t, &tm_gmt) && localtime_r(&
t, &tm_loc)) {
1142 int Y = tm_gmt.tm_year;
1143 int M = tm_gmt.tm_mon + 1;
1144 int D = tm_gmt.tm_mday;
1145 int L = (M == 1 || M == 2) ? 1 : 0;
1146 int MJD = 14956 +
D + int((Y - L) * 365.25) + int((M + 1 + L * 12) * 30.6001);
1148 int16_t local_offset = htons(tm_loc.tm_gmtoff / 60);
1149 std::vector<uint8_t> T {
1155 BYTE0(local_offset),
1170 int Tag =
GetTag(Length, &Data);
1175 const uint8_t *
d =
GetData(Data, l);
1183 default:
esyslog(
"ERROR: CI date time: unknown tag %06X", Tag);
1247 char *
GetText(
int &Length,
const uint8_t **Data);
1253 bool Process(
int Length = 0,
const uint8_t *Data =
nullptr)
override;
1284 int Tag =
GetTag(Length, Data);
1290 esyslog(
"CI MMI: unexpected text tag: %06X", Tag);
1297 int Tag =
GetTag(Length, &Data);
1302 const uint8_t *
d =
GetData(Data, l);
1307 struct tDisplayReply { uint8_t m_id; uint8_t m_mode; };
1308 tDisplayReply dr {};
1315 default:
esyslog(
"CI MMI: unsupported display control command %02X", *
d);
1327 const uint8_t *
d =
GetData(Data, l);
1352 const uint8_t *
d =
GetData(Data, l);
1354 uint8_t blind = *
d++;
1367 const uint8_t *
d =
GetData(Data, l);
1377 default:
esyslog(
"ERROR: CI MMI: unknown close_mmi_cmd_id %02X", *
d);
1384 default:
esyslog(
"ERROR: CI MMI: unknown tag %06X", Tag);
1424 strncpy(answer.m_text, Text,
sizeof(answer.m_text) - 1);
1425 answer.m_text[255] =
'\0';
1484 return Reply(
nullptr);
1515 esyslog(
"ERROR: buffer overflow in CA_PMT");
1547 const uint8_t *data)
1551 esyslog(
"ERROR: adding CA descriptor without program/stream!");
1557 esyslog(
"ERROR: buffer overflow in CA_PMT");
1605 int fd_ca = open(FileName, O_RDWR);
1609 if (ioctl(fd_ca, CA_GET_CAP, &Caps) == 0)
1614 if (Caps.slot_type & CA_CI_LINK)
1616 if (Caps.slot_type & CA_CI)
1618 isyslog(
"CAM doesn't support either high or low level CI,"
1619 " Caps.slot_type=%i", Caps.slot_type);
1622 esyslog(
"ERROR: no CAM slots found");
1633 return (ntohl(*(
int *)Data));
1638 std::vector<uint8_t> buffer {Tag, 0x00} ;
1640 buffer.push_back(Status);
1642 buffer.push_back((ResourceId >> 24) & 0xFF);
1643 buffer.push_back((ResourceId >> 16) & 0xFF);
1644 buffer.push_back((ResourceId >> 8) & 0xFF);
1645 buffer.push_back( ResourceId & 0xFF);
1647 buffer.push_back((SessionId >> 8) & 0xFF);
1648 buffer.push_back( SessionId & 0xFF);
1649 buffer[1] = buffer.size() - 2;
1656 if (session && session->SessionId() == SessionId)
1665 if (session && session->Tc()->Slot() == Slot && session->ResourceId() == ResourceId)
1676 switch (ResourceId) {
1693 if (Length == 6 && *(Data + 1) == 0x04) {
1696 switch (ResourceId) {
1711 esyslog(
"ERROR: can't create session for resource identifier: %08X",
1715 default:
esyslog(
"ERROR: unknown resource identifier: %08X", ResourceId);
1725 if (Session &&
m_sessions[SessionId - 1] == Session) {
1732 esyslog(
"ERROR: unknown session id: %d", SessionId);
1741 if (session && session->Tc()->Slot() == Slot) {
1754 for (
int Slot = 0; Slot <
m_numSlots; Slot++)
1760 const uint8_t *Data =
m_tc->
Data(Length);
1761 if (Data && Length > 1)
1768 int SessionId = ntohs(*(
short *)&Data[2]);
1772 Session->
Process(Length - 4, Data + 4);
1782 esyslog(
"ERROR: unknown session id: %d", SessionId);
1798 esyslog(
"ERROR: unknown session tag: %02X", *Data);
1814 bool UserIO =
false;
1818 if (session && session->Process())
1820 UserIO |= session->HasUserIO();
1847 for (
int Slot = 0; Slot <
m_numSlots; Slot++) {
1858 for (
int Slot = 0; Slot <
m_numSlots; Slot++) {
1878 return cas && cas->
SendPMT(CaPmt);
1912 esyslog(
"New High level CI handler");
1924 msg->msg[2] = tag & 0xff;
1925 msg->msg[1] = (tag & 0xff00) >> 8;
1926 msg->msg[0] = (tag & 0xff0000) >> 16;
1927 esyslog(
"Sending message=[%02x %02x %02x ]",
1928 msg->msg[0], msg->msg[1], msg->msg[2]);
1931 return ioctl(
m_fdCa,
function, msg);
1936 return CommHL(tag, CA_GET_MSG, msg);
1941 return CommHL(tag, CA_SEND_MSG, msg);
1948 struct ca_msg msg {};
1954 esyslog(
"HLCI communication failed");
1959 esyslog(
"HLCI communication failed");
1961 QString message(
"Debug: ");
1962 for(
int i = 0; i < 20; i++) {
1963 message += QString(
"%1 ").arg(msg.msg[i]);
1965 LOG(VB_GENERAL, LOG_DEBUG, message);
1968 const uint8_t *
d = &msg.msg[4];
1970 unsigned short id = ((
unsigned short)(*
d) << 8) | *(
d + 1);
2013 struct ca_msg msg {};
2022 esyslog(
"CA message too long");
2029 esyslog(
"HLCI communication failed");
2038 if ((ioctl(
m_fdCa, CA_RESET)) < 0) {
2039 esyslog(
"ioctl CA_RESET failed.");
void SetTimeOffset(double offset_in_seconds) override
@ RI_CONDITIONAL_ACCESS_SUPPORT
bool DataAvailable(void) const
void AddCaDescriptor(int ca_system_id, int ca_pid, int data_len, const uint8_t *data)
static constexpr uint8_t DATA_INDICATOR
@ AOT_CONNECTION_DESCRIPTOR
cCiSession * GetSessionBySessionId(int SessionId)
~cCiTransportConnection()
int CloseAllSessions(int Slot)
static int GetTag(int &Length, const uint8_t **Data)
std::vector< uint16_t > dvbca_vector
dvbca_vector m_caSystemIds
@ AOT_APPLICATION_INFO_ENQ
cHlCiHandler(int Fd, int NumSlots)
def read(device=None, features=[])
bool SetCaPmt(cCiCaPmt &CaPmt)
static char * GetString(int &Length, const uint8_t **Data)
@ ST_CLOSE_SESSION_REQUEST
@ AOT_SUBTITLE_DOWNLOAD_MORE
@ AOT_SUBTITLE_SEGMENT_MORE
int NumSlots(void) override
int SendData(int Length, const uint8_t *Data)
int GetData(unsigned tag, struct ca_msg *msg)
@ ST_OPEN_SESSION_REQUEST
def write(text, progress=True)
void Init(int Fd, uint8_t Slot, uint8_t Tcid)
std::array< uint8_t, MAX_TPDU_SIZE > m_data
cCiTransportConnection * m_tc
cCiEnquiry * GetEnquiry(void) override
static constexpr int TIMEOUT
int SendData(unsigned tag, struct ca_msg *msg)
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
bool Process(void) override
@ ST_OPEN_SESSION_RESPONSE
static bool sDebugProtocol
@ DRI_UNKNOWN_CHARACTER_TABLE
std::chrono::milliseconds m_lastPoll
@ DCC_OVERLAY_GRAPHICS_CHARACTERISTICS
std::array< cCiTransportConnection, MAX_CI_CONNECT > m_tc
bool Reply(const char *s)
@ MM_LOW_LEVEL_OVERLAY_GRAPHICS
cCiTransportConnection * Process(int Slot)
cCiMMI(int SessionId, cCiTransportConnection *Tc)
@ DCC_INPUT_CHARACTER_TABLE_LIST
uint8_t m_capmt[2048]
XXX is there a specified maximum?
static constexpr uint8_t EF_BLIND
bool Process(int Length=0, const uint8_t *Data=nullptr) override
bool Process(void) override
@ DRI_LIST_GRAPHIC_OVERLAY_CHARACTERISTICS
@ DRI_LIST_INPUT_CHARACTER_TABLES
cCiSession * m_sessions[MAX_CI_SESSION]
bool EnterMenu(int Slot) override
static ssize_t safe_read(int filedes, void *buffer, size_t size)
bool ModuleReady(int Slot) const
static uint8_t * SetLength(uint8_t *Data, int Length)
virtual ~cCiSession()=default
bool SendAnswer(const char *Text)
@ ST_CREATE_SESSION_RESPONSE
virtual int NumSlots(void)=0
@ MM_LOW_LEVEL_FULL_SCREEN_GRAPHICS
static constexpr int MAX_TPDU_DATA
bool SetCaPmt(cCiCaPmt &CaPmt)
int SessionId(void) const
static constexpr int SIZE_INDICATOR
dvbca_vector GetCaSystemIds(int Slot) override
@ DCC_DISPLAY_CHARACTER_TABLE_LIST
@ AOT_SUBTITLE_DOWNLOAD_LAST
static const uint8_t * GetData(const uint8_t *Data, int &Length)
@ ST_CLOSE_SESSION_RESPONSE
int SendData(std::vector< uint8_t > &Data)
@ DRI_UNKNOWN_DISPLAY_CONTROL_CMD
bool Send(uint8_t Tag, int SessionId, int ResourceId=0, int Status=-1)
dvbca_vector GetCaSystemIds(void)
bool CloseSession(int SessionId)
void SetTimeOffset(double offset)
cCiSession * CreateSession(int ResourceId)
cCiSession(int SessionId, int ResourceId, cCiTransportConnection *Tc)
const uint8_t * Data(int &Length)
static constexpr size_t MAX_CI_CONNECT
dvbca_vector m_caSystemIds
virtual bool Process(int Length=0, const uint8_t *Data=nullptr)
cCiResourceManager(int SessionId, cCiTransportConnection *Tc)
const cCiTransportConnection * Tc(void)
static constexpr uint8_t DEC2BCD(uint8_t d)
bool Reset(int Slot) const
@ DRI_LIST_DISPLAY_CHARACTER_TABLES
bool SendPMT(const cCiCaPmt &CaPmt)
int CommHL(unsigned tag, unsigned function, struct ca_msg *msg) const
int NumSlots(void) override
bool ResetSlot(int Slot) const
static constexpr time_t WRKRND_TIME_BEFORE_ENTER_MENU
static constexpr size_t MAX_TPDU_SIZE
bool Process(int Length=0, const uint8_t *Data=nullptr) override
bool HasUserIO(void) override
int SendData(int Tag, std::vector< uint8_t > &Data)
@ RI_APPLICATION_INFORMATION
bool SendMenuAnswer(uint8_t Selection)
cCiTransportLayer * m_tpl
dvbca_vector GetCaSystemIds(int Slot) override
@ DRI_LIST_FULL_SCREEN_GRAPHIC_CHARACTERISTICS
bool Process(int Length=0, const uint8_t *Data=nullptr) override
#define dbgprotocol(a...)
cCiTransportConnection * NewConnection(int Slot)
cCiTransportConnection(void)
const uint8_t * GetData(const uint8_t *Data, int &Length) const
int LastResponse(void) const
cLlCiHandler(int Fd, int NumSlots)
cCiTransportLayer(int Fd, int NumSlots)
static constexpr std::chrono::milliseconds POLL_INTERVAL
char * GetText(int &Length, const uint8_t **Data)
const uint8_t * Data(int &Length)
bool NeedCaPmt(void) override
static constexpr uint8_t BYTE1(uint16_t a)
cCiTransportConnection * m_tc
bool EnterMenu(int Slot) override
static int ResourceIdToInt(const uint8_t *Data)
static bool sDumpTPDUDataTransfer
static char * CopyString(int Length, const uint8_t *Data)
static cCiHandler * CreateCiHandler(const char *FileName)
@ DCC_FULL_SCREEN_GRAPHICS_CHARACTERISTICS
static const uint8_t * GetLength(const uint8_t *Data, int &Length)
virtual bool HasUserIO(void)
static const iso6937table * d
static constexpr int8_t MAX_CONNECT_RETRIES
int CreateConnection(void)
cCiSession * GetSessionByResourceId(int ResourceId, int Slot)
cCiConditionalAccessSupport(int SessionId, cCiTransportConnection *Tc)
cCiCaPmt(int ProgramNumber, uint8_t cplm=CPLM_ONLY)
@ AOT_SUBTITLE_SEGMENT_LAST
bool NeedCaPmt(void) const
cCiMenu * GetMenu(void) override
int ResourceId(void) const
static constexpr uint8_t BYTE0(uint16_t a)
int SendTPDU(uint8_t Tag, int Length=0, const uint8_t *Data=nullptr) const
bool Process(int Length=0, const uint8_t *Data=nullptr) override
int SendData(int Tag, int Length=0, const uint8_t *Data=nullptr)
static pid_list_t::iterator find(const PIDInfoMap &map, pid_list_t &list, pid_list_t::iterator begin, pid_list_t::iterator end, bool find_open)
static constexpr int CAM_READ_TIMEOUT
bool OpenSession(int Length, const uint8_t *Data)
cCiEnquiry * Enquiry(void)
cCiMenu * GetMenu(void) override
cCiEnquiry * GetEnquiry(void) override
static constexpr int ERROR
void AddElementaryStream(int type, int pid)
cCiDateTime(int SessionId, cCiTransportConnection *Tc)