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);
313 uint8_t *
p =
m_data.data() + 3;
317 memcpy(
p,
Data, Length);
321 esyslog(
"ERROR: illegal data length for TPDU tag 0x%02X: %d",
Tag, Length);
325 esyslog(
"ERROR: unknown TPDU tag: 0x%02X",
Tag);
334 esyslog(
"ERROR: attemp to write TPDU with zero size");
353 static constexpr ssize_t MAX_DUMP { 256 };
354 QString msg = QString(
"%1 ").arg(Outgoing ?
"-->" :
"<--");
355 for (
int i = 0; i <
m_size && i < MAX_DUMP; i++)
356 msg += QString(
"%1 ").arg((
short int)
m_data[i], 2, 16, QChar(
'0'));
359 LOG(VB_DVBCAM, LOG_INFO, msg);
362 for (
int i = 0; i <
m_size && i < MAX_DUMP; i++)
363 msg += QString(
"%1 ").arg(isprint(
m_data[i]) ?
m_data[i] :
'.', 2);
366 LOG(VB_DVBCAM, LOG_INFO, msg);
407 void Init(
int Fd, uint8_t
Slot, uint8_t Tcid);
408 int SendTPDU(uint8_t Tag,
int Length = 0,
const uint8_t *
Data =
nullptr)
const;
423 const uint8_t *
Data(
int &Length);
460 std::array<struct pollfd,1> pfd {};
462 pfd[0].events = POLLIN;
467 if (ret == -1 && (errno == EAGAIN || errno == EINTR))
473 (pfd[0].revents & POLLIN) &&
495 default:
return ERROR;
559 dsyslog(
"CAM: retrying to establish connection");
561 dsyslog(
"CAM: connection established");
580 auto curr_time = nowAsDuration<std::chrono::milliseconds>();
581 std::chrono::milliseconds msdiff = curr_time -
m_lastPoll;
602 std::array<cCiTransportConnection,MAX_CI_CONNECT>
m_tc;
623 dbgprotocol(
"Creating connection: slot %d, tcid %zd\n", Slot, i + 1);
625 if (
m_tc[i].CreateConnection() ==
OK)
636 if (ioctl(
m_fd, CA_RESET, 1 << Slot) != -1) {
640 esyslog(
"ERROR: can't reset CAM slot %d: %m", Slot);
647 ca_slot_info_t sinfo;
649 if (ioctl(
m_fd, CA_GET_SLOT_INFO, &sinfo) != -1)
650 return (sinfo.flags & CA_CI_MODULE_READY) != 0U;
651 esyslog(
"ERROR: can't get info on CAM slot %d: %m", Slot);
657 for (
auto & conn :
m_tc) {
659 if (Tc->
Slot() == Slot) {
660 switch (Tc->
State()) {
785 static int GetTag(
int &Length,
const uint8_t **Data);
786 static const uint8_t *
GetData(
const uint8_t *Data,
int &Length);
787 int SendData(
int Tag,
int Length = 0,
const uint8_t *Data =
nullptr);
789 {
return SendData(Tag, Data.size(), Data.data()); };
797 virtual bool Process(
int Length = 0,
const uint8_t *Data =
nullptr);
801 : m_sessionId(SessionId),
802 m_resourceId(ResourceId),
816 if (Length >= 3 && Data && *Data) {
818 for (
int i = 0; i < 3; i++)
819 t = (
t << 8) | *(*Data)++;
829 return Length ? Data :
nullptr;
836 esyslog(
"ERROR: CAM: data length (%d) is negative", Length);
840 if ((Length > 0) && !Data)
842 esyslog(
"ERROR: CAM: Data pointer null");
846 std::vector<uint8_t> buffer {
850 static_cast<uint8_t
>((Tag >> 16) & 0xFF),
851 static_cast<uint8_t
>((Tag >> 8) & 0xFF),
852 static_cast<uint8_t
>((Tag ) & 0xFF)} ;
853 buffer.reserve(2048);
856 if (buffer.size() + Length >= buffer.capacity())
858 esyslog(
"ERROR: CAM: data length (%d) exceeds buffer size", Length);
864 buffer.insert(buffer.end(), Data, Data + Length);
870 [[maybe_unused]]
const uint8_t *Data)
882 bool Process(
int Length = 0,
const uint8_t *Data =
nullptr)
override;
895 int Tag =
GetTag(Length, &Data);
899 const std::array<const uint32_t,5> resources
909 reinterpret_cast<const uint8_t*
>(resources.data()));
917 const uint8_t *
d =
GetData(Data, l);
919 esyslog(
"CI resource manager: unexpected data");
925 esyslog(
"ERROR: CI resource manager: unexpected tag %06X in state %d", Tag,
m_state);
929 default:
esyslog(
"ERROR: CI resource manager: unknown tag %06X", Tag);
954 bool Process(
int Length = 0,
const uint8_t *Data =
nullptr)
override;
981 int Tag =
GetTag(Length, &Data);
986 const uint8_t *
d =
GetData(Data, l);
1005 default:
esyslog(
"ERROR: CI application information: unknown tag %06X", Tag);
1036 bool Process(
int Length = 0,
const uint8_t *Data =
nullptr)
override;
1052 int Tag =
GetTag(Length, &Data);
1057 const uint8_t *
d =
GetData(Data, l);
1059 unsigned short id = ((
unsigned short)(*
d) << 8) | *(
d + 1);
1078 default:
esyslog(
"ERROR: CI conditional access support: unknown tag %06X", Tag);
1110 bool Process(
int Length = 0,
const uint8_t *Data =
nullptr)
override;
1127 {
return ((
d / 10) << 4) + (
d % 10); }
1129 {
return static_cast<uint8_t
>(a & 0xFF); }
1131 {
return static_cast<uint8_t
>((a >> 8) & 0xFF); }
1135 time_t
t = time(
nullptr);
1136 struct tm tm_gmt {};
1137 struct tm tm_loc {};
1145 if (gmtime_r(&
t, &tm_gmt) && localtime_r(&
t, &tm_loc)) {
1146 int Y = tm_gmt.tm_year;
1147 int M = tm_gmt.tm_mon + 1;
1148 int D = tm_gmt.tm_mday;
1149 int L = (M == 1 || M == 2) ? 1 : 0;
1150 int MJD = 14956 +
D + int((Y - L) * 365.25) + int((M + 1 + L * 12) * 30.6001);
1152 int16_t local_offset = htons(tm_loc.tm_gmtoff / 60);
1153 std::vector<uint8_t> T {
1159 BYTE0(local_offset),
1174 int Tag =
GetTag(Length, &Data);
1179 const uint8_t *
d =
GetData(Data, l);
1187 default:
esyslog(
"ERROR: CI date time: unknown tag %06X", Tag);
1251 char *
GetText(
int &Length,
const uint8_t **Data);
1257 bool Process(
int Length = 0,
const uint8_t *Data =
nullptr)
override;
1288 int Tag =
GetTag(Length, Data);
1294 esyslog(
"CI MMI: unexpected text tag: %06X", Tag);
1301 int Tag =
GetTag(Length, &Data);
1306 const uint8_t *
d =
GetData(Data, l);
1311 struct tDisplayReply { uint8_t m_id; uint8_t m_mode; };
1312 tDisplayReply dr {};
1319 default:
esyslog(
"CI MMI: unsupported display control command %02X", *
d);
1331 const uint8_t *
d =
GetData(Data, l);
1357 const uint8_t *
d =
GetData(Data, l);
1359 uint8_t blind = *
d++;
1372 const uint8_t *
d =
GetData(Data, l);
1382 default:
esyslog(
"ERROR: CI MMI: unknown close_mmi_cmd_id %02X", *
d);
1389 default:
esyslog(
"ERROR: CI MMI: unknown tag %06X", Tag);
1429 strncpy(answer.m_text, Text,
sizeof(answer.m_text) - 1);
1430 answer.m_text[255] =
'\0';
1441 m_selectable(Selectable)
1489 return Reply(
nullptr);
1504 : m_infoLengthPos(m_length)
1519 esyslog(
"ERROR: buffer overflow in CA_PMT");
1551 const uint8_t *data)
1555 esyslog(
"ERROR: adding CA descriptor without program/stream!");
1561 esyslog(
"ERROR: buffer overflow in CA_PMT");
1593 m_numSlots(NumSlots),
1609 int fd_ca = open(FileName, O_RDWR);
1613 if (ioctl(fd_ca, CA_GET_CAP, &Caps) == 0)
1618 if (Caps.slot_type & CA_CI_LINK)
1620 if (Caps.slot_type & CA_CI)
1622 isyslog(
"CAM doesn't support either high or low level CI,"
1623 " Caps.slot_type=%i", Caps.slot_type);
1627 esyslog(
"ERROR: no CAM slots found");
1641 return (ntohl(*(
int *)Data));
1646 std::vector<uint8_t> buffer {Tag, 0x00} ;
1648 buffer.push_back(Status);
1650 buffer.push_back((ResourceId >> 24) & 0xFF);
1651 buffer.push_back((ResourceId >> 16) & 0xFF);
1652 buffer.push_back((ResourceId >> 8) & 0xFF);
1653 buffer.push_back( ResourceId & 0xFF);
1655 buffer.push_back((SessionId >> 8) & 0xFF);
1656 buffer.push_back( SessionId & 0xFF);
1657 buffer[1] = buffer.size() - 2;
1664 if (session && session->SessionId() == SessionId)
1673 if (session && session->Tc()->Slot() == Slot && session->ResourceId() == ResourceId)
1684 switch (ResourceId) {
1701 if (Length == 6 && *(Data + 1) == 0x04) {
1704 switch (ResourceId) {
1719 esyslog(
"ERROR: can't create session for resource identifier: %08X",
1723 default:
esyslog(
"ERROR: unknown resource identifier: %08X", ResourceId);
1733 if (Session &&
m_sessions[SessionId - 1] == Session) {
1740 esyslog(
"ERROR: unknown session id: %d", SessionId);
1749 if (session && session->Tc()->Slot() == Slot) {
1762 for (
int Slot = 0; Slot <
m_numSlots; Slot++)
1768 const uint8_t *Data =
m_tc->
Data(Length);
1769 if (Data && Length > 1)
1776 int SessionId = ntohs(*(
short *)&Data[2]);
1780 Session->
Process(Length - 4, Data + 4);
1791 esyslog(
"ERROR: unknown session id: %d", SessionId);
1808 esyslog(
"ERROR: unknown session tag: %02X", *Data);
1824 bool UserIO =
false;
1828 if (session && session->Process())
1830 UserIO |= session->HasUserIO();
1857 for (
int Slot = 0; Slot <
m_numSlots; Slot++) {
1868 for (
int Slot = 0; Slot <
m_numSlots; Slot++) {
1888 return cas && cas->
SendPMT(CaPmt);
1920 m_numSlots(NumSlots)
1922 esyslog(
"New High level CI handler");
1934 msg->msg[2] = tag & 0xff;
1935 msg->msg[1] = (tag & 0xff00) >> 8;
1936 msg->msg[0] = (tag & 0xff0000) >> 16;
1937 esyslog(
"Sending message=[%02x %02x %02x ]",
1938 msg->msg[0], msg->msg[1], msg->msg[2]);
1941 return ioctl(
m_fdCa,
function, msg);
1946 return CommHL(tag, CA_GET_MSG, msg);
1951 return CommHL(tag, CA_SEND_MSG, msg);
1958 struct ca_msg msg {};
1964 esyslog(
"HLCI communication failed");
1969 esyslog(
"HLCI communication failed");
1971 QString message(
"Debug: ");
1972 for(
int i = 0; i < 20; i++) {
1973 message += QString(
"%1 ").arg(msg.msg[i]);
1975 LOG(VB_GENERAL, LOG_DEBUG, message);
1978 const uint8_t *
d = &msg.msg[4];
1980 unsigned short id = ((
unsigned short)(*
d) << 8) | *(
d + 1);
2023 struct ca_msg msg {};
2032 esyslog(
"CA message too long");
2039 esyslog(
"HLCI communication failed");
2048 if ((ioctl(
m_fdCa, CA_RESET)) < 0) {
2049 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
@ DCC_FULL_SCREEN_GRAPHICS_CHARACTERISTICS
cHlCiHandler(int Fd, int NumSlots)
def read(device=None, features=[])
bool SetCaPmt(cCiCaPmt &CaPmt)
static char * GetString(int &Length, const uint8_t **Data)
@ 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)
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
static bool sDebugProtocol
@ DRI_LIST_INPUT_CHARACTER_TABLES
@ ST_OPEN_SESSION_RESPONSE
std::chrono::milliseconds m_lastPoll
std::array< cCiTransportConnection, MAX_CI_CONNECT > m_tc
bool Reply(const char *s)
@ DRI_LIST_DISPLAY_CHARACTER_TABLES
cCiTransportConnection * Process(int Slot)
cCiMMI(int SessionId, cCiTransportConnection *Tc)
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
@ DCC_OVERLAY_GRAPHICS_CHARACTERISTICS
@ DRI_UNKNOWN_DISPLAY_CONTROL_CMD
cCiSession * m_sessions[MAX_CI_SESSION]
@ MM_LOW_LEVEL_FULL_SCREEN_GRAPHICS
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)
virtual int NumSlots(void)=0
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
@ AOT_SUBTITLE_DOWNLOAD_LAST
static const uint8_t * GetData(const uint8_t *Data, int &Length)
int SendData(std::vector< uint8_t > &Data)
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)
@ DCC_INPUT_CHARACTER_TABLE_LIST
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)
@ ST_CREATE_SESSION_RESPONSE
const cCiTransportConnection * Tc(void)
static constexpr uint8_t DEC2BCD(uint8_t d)
bool Reset(int Slot) const
bool SendPMT(const cCiCaPmt &CaPmt)
int CommHL(unsigned tag, unsigned function, struct ca_msg *msg) const
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
bool Process(int Length=0, const uint8_t *Data=nullptr) override
@ DCC_DISPLAY_CHARACTER_TABLE_LIST
#define dbgprotocol(a...)
@ ST_OPEN_SESSION_REQUEST
cCiTransportConnection * NewConnection(int Slot)
@ DRI_LIST_FULL_SCREEN_GRAPHIC_CHARACTERISTICS
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)
@ DRI_LIST_GRAPHIC_OVERLAY_CHARACTERISTICS
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)
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)
@ MM_LOW_LEVEL_OVERLAY_GRAPHICS
cCiCaPmt(int ProgramNumber, uint8_t cplm=CPLM_ONLY)
@ AOT_SUBTITLE_SEGMENT_LAST
bool NeedCaPmt(void) const
@ ST_CLOSE_SESSION_RESPONSE
@ ST_CLOSE_SESSION_REQUEST
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
@ DRI_UNKNOWN_CHARACTER_TABLE
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)