37#include <linux/dvb/ca.h>
38#include <netinet/in.h>
54#define esyslog(a...) LOG(VB_GENERAL, LOG_ERR, QString::asprintf(a))
55#define isyslog(a...) LOG(VB_DVBCAM, LOG_INFO, QString::asprintf(a))
56#define dsyslog(a...) LOG(VB_DVBCAM, LOG_DEBUG, QString::asprintf(a))
58#define LOG_ERROR esyslog("ERROR (%s,%d): %m", __FILE__, __LINE__)
59#define LOG_ERROR_STR(s) esyslog("ERROR: %s: %m", s)
69#define dbgprotocol(a...) if (sDebugProtocol) LOG(VB_DVBCAM, LOG_DEBUG, QString::asprintf(a))
71static constexpr int OK { 0 };
73static constexpr int ERROR { -2 };
85static ssize_t
safe_read(
int filedes,
void *buffer,
size_t size)
88 ssize_t
p =
read(filedes, buffer, size);
89 if (
p < 0 && (errno == EINTR || errno == EAGAIN)) {
90 dsyslog(
"EINTR while reading from file handle %d - retrying", filedes);
97static const uint8_t *
GetLength(
const uint8_t *Data,
int &Length)
111 int l = Length & ~SIZE_INDICATOR;
113 for (
int i = 0; i < l; i++)
114 Length = (Length << 8) | *Data++;
134 int n =
sizeof(Length);
135 for (
int i = n - 1; i >= 0; i--) {
136 int b = (Length >> (8 * i)) & 0xFF;
147static void SetLength(std::vector<uint8_t> &Data,
int Length)
151 Data.push_back(Length);
156 size_t len_offset = Data.size();
159 int n =
sizeof(Length);
160 for (
int i = n - 1; i >= 0; i--) {
161 int b = (Length >> (8 * i)) & 0xFF;
162 if ((len_offset != Data.size()) || b)
174 char *s = (
char *)malloc(Length + 1);
175 strncpy(s, (
char *)Data, Length);
180static char *
GetString(
int &Length,
const uint8_t **Data)
189 if (Length > 0 && Data && *Data) {
193 Length -=
d - *Data + l;
217 esyslog(
"cMutex Lock inbalance detected");
221 pthread_mutex_unlock(&
m_mutex);
269 std::array<uint8_t,MAX_TPDU_SIZE>
m_data {0};
270 const uint8_t *
GetData(
const uint8_t *
Data,
int &Length)
const;
281 void Dump(
bool Outgoing);
284cTPDU::cTPDU(uint8_t Slot, uint8_t Tcid, uint8_t Tag,
int Length,
const uint8_t *Data)
309 esyslog(
"ERROR: illegal data length for TPDU tag 0x%02X: %d",
Tag, Length);
315 uint8_t *
p =
m_data.data() + 3;
319 memcpy(
p,
Data, Length);
323 esyslog(
"ERROR: illegal data length for TPDU tag 0x%02X: %d",
Tag, Length);
327 esyslog(
"ERROR: unknown TPDU tag: 0x%02X",
Tag);
336 esyslog(
"ERROR: attemp to write TPDU with zero size");
355 static constexpr ssize_t MAX_DUMP { 256 };
356 QString msg = QString(
"%1 ").arg(Outgoing ?
"-->" :
"<--");
357 for (
int i = 0; i <
m_size && i < MAX_DUMP; i++)
358 msg += QString(
"%1 ").arg((
short int)
m_data[i], 2, 16, QChar(
'0'));
361 LOG(VB_DVBCAM, LOG_INFO, msg);
364 for (
int i = 0; i <
m_size && i < MAX_DUMP; i++)
365 msg += QString(
"%1 ").arg(isprint(
m_data[i]) ?
m_data[i] :
'.', 2);
368 LOG(VB_DVBCAM, LOG_INFO, msg);
409 void Init(
int Fd, uint8_t
Slot, uint8_t Tcid);
410 int SendTPDU(uint8_t Tag,
int Length = 0,
const uint8_t *
Data =
nullptr)
const;
425 const uint8_t *
Data(
int &Length);
462 std::array<struct pollfd,1> pfd {};
464 pfd[0].events = POLLIN;
469 if (ret == -1 && (errno == EAGAIN || errno == EINTR))
475 (pfd[0].revents & POLLIN) &&
497 default:
return ERROR;
561 dsyslog(
"CAM: retrying to establish connection");
563 dsyslog(
"CAM: connection established");
582 auto curr_time = nowAsDuration<std::chrono::milliseconds>();
583 std::chrono::milliseconds msdiff = curr_time -
m_lastPoll;
604 std::array<cCiTransportConnection,MAX_CI_CONNECT>
m_tc;
625 dbgprotocol(
"Creating connection: slot %d, tcid %zd\n", Slot, i + 1);
627 if (
m_tc[i].CreateConnection() ==
OK)
638 if (ioctl(
m_fd, CA_RESET, 1 << Slot) != -1) {
642 esyslog(
"ERROR: can't reset CAM slot %d: %m", Slot);
649 ca_slot_info_t sinfo;
651 if (ioctl(
m_fd, CA_GET_SLOT_INFO, &sinfo) != -1)
652 return (sinfo.flags & CA_CI_MODULE_READY) != 0U;
653 esyslog(
"ERROR: can't get info on CAM slot %d: %m", Slot);
659 for (
auto & conn :
m_tc) {
661 if (Tc->
Slot() == Slot) {
662 switch (Tc->
State()) {
787 static int GetTag(
int &Length,
const uint8_t **Data);
788 static const uint8_t *
GetData(
const uint8_t *Data,
int &Length);
789 int SendData(
int Tag,
int Length = 0,
const uint8_t *Data =
nullptr);
791 {
return SendData(Tag, Data.size(), Data.data()); };
799 virtual bool Process(
int Length = 0,
const uint8_t *Data =
nullptr);
803 : m_sessionId(SessionId),
804 m_resourceId(ResourceId),
818 if (Length >= 3 && Data && *Data) {
820 for (
int i = 0; i < 3; i++)
821 t = (
t << 8) | *(*Data)++;
831 return Length ? Data :
nullptr;
838 esyslog(
"ERROR: CAM: data length (%d) is negative", Length);
842 if ((Length > 0) && !Data)
844 esyslog(
"ERROR: CAM: Data pointer null");
848 std::vector<uint8_t> buffer {
852 static_cast<uint8_t
>((Tag >> 16) & 0xFF),
853 static_cast<uint8_t
>((Tag >> 8) & 0xFF),
854 static_cast<uint8_t
>((Tag ) & 0xFF)} ;
855 buffer.reserve(2048);
858 if (buffer.size() + Length >= buffer.capacity())
860 esyslog(
"ERROR: CAM: data length (%d) exceeds buffer size", Length);
866 buffer.insert(buffer.end(), Data, Data + Length);
872 [[maybe_unused]]
const uint8_t *Data)
884 bool Process(
int Length = 0,
const uint8_t *Data =
nullptr)
override;
897 int Tag =
GetTag(Length, &Data);
901 const std::array<const uint32_t,5> resources
911 reinterpret_cast<const uint8_t*
>(resources.data()));
919 const uint8_t *
d =
GetData(Data, l);
921 esyslog(
"CI resource manager: unexpected data");
927 esyslog(
"ERROR: CI resource manager: unexpected tag %06X in state %d", Tag,
m_state);
931 default:
esyslog(
"ERROR: CI resource manager: unknown tag %06X", Tag);
956 bool Process(
int Length = 0,
const uint8_t *Data =
nullptr)
override;
983 int Tag =
GetTag(Length, &Data);
988 const uint8_t *
d =
GetData(Data, l);
1007 default:
esyslog(
"ERROR: CI application information: unknown tag %06X", Tag);
1038 bool Process(
int Length = 0,
const uint8_t *Data =
nullptr)
override;
1054 int Tag =
GetTag(Length, &Data);
1059 const uint8_t *
d =
GetData(Data, l);
1061 unsigned short id = ((
unsigned short)(*
d) << 8) | *(
d + 1);
1080 default:
esyslog(
"ERROR: CI conditional access support: unknown tag %06X", Tag);
1112 bool Process(
int Length = 0,
const uint8_t *Data =
nullptr)
override;
1129 {
return ((
d / 10) << 4) + (
d % 10); }
1131 {
return static_cast<uint8_t
>(a & 0xFF); }
1133 {
return static_cast<uint8_t
>((a >> 8) & 0xFF); }
1137 time_t
t = time(
nullptr);
1138 struct tm tm_gmt {};
1139 struct tm tm_loc {};
1147 if (gmtime_r(&
t, &tm_gmt) && localtime_r(&
t, &tm_loc)) {
1148 int Y = tm_gmt.tm_year;
1149 int M = tm_gmt.tm_mon + 1;
1150 int D = tm_gmt.tm_mday;
1151 int L = (M == 1 || M == 2) ? 1 : 0;
1152 int MJD = 14956 +
D + int((Y - L) * 365.25) + int((M + 1 + L * 12) * 30.6001);
1154 int16_t local_offset = htons(tm_loc.tm_gmtoff / 60);
1155 std::vector<uint8_t> T {
1161 BYTE0(local_offset),
1176 int Tag =
GetTag(Length, &Data);
1181 const uint8_t *
d =
GetData(Data, l);
1189 default:
esyslog(
"ERROR: CI date time: unknown tag %06X", Tag);
1253 char *
GetText(
int &Length,
const uint8_t **Data);
1259 bool Process(
int Length = 0,
const uint8_t *Data =
nullptr)
override;
1290 int Tag =
GetTag(Length, Data);
1296 esyslog(
"CI MMI: unexpected text tag: %06X", Tag);
1303 int Tag =
GetTag(Length, &Data);
1308 const uint8_t *
d =
GetData(Data, l);
1313 struct tDisplayReply { uint8_t m_id; uint8_t m_mode; };
1314 tDisplayReply dr {};
1321 default:
esyslog(
"CI MMI: unsupported display control command %02X", *
d);
1333 const uint8_t *
d =
GetData(Data, l);
1359 const uint8_t *
d =
GetData(Data, l);
1361 uint8_t blind = *
d++;
1374 const uint8_t *
d =
GetData(Data, l);
1384 default:
esyslog(
"ERROR: CI MMI: unknown close_mmi_cmd_id %02X", *
d);
1391 default:
esyslog(
"ERROR: CI MMI: unknown tag %06X", Tag);
1431 strncpy(answer.m_text, Text,
sizeof(answer.m_text) - 1);
1432 answer.m_text[255] =
'\0';
1443 m_selectable(Selectable)
1491 return Reply(
nullptr);
1522 esyslog(
"ERROR: buffer overflow in CA_PMT");
1554 const uint8_t *data)
1558 esyslog(
"ERROR: adding CA descriptor without program/stream!");
1564 esyslog(
"ERROR: buffer overflow in CA_PMT");
1596 m_numSlots(NumSlots),
1612 int fd_ca = open(FileName, O_RDWR);
1616 if (ioctl(fd_ca, CA_GET_CAP, &Caps) == 0)
1621 if (Caps.slot_type & CA_CI_LINK)
1623 if (Caps.slot_type & CA_CI)
1625 isyslog(
"CAM doesn't support either high or low level CI,"
1626 " Caps.slot_type=%i", Caps.slot_type);
1630 esyslog(
"ERROR: no CAM slots found");
1644 return (ntohl(*(
int *)Data));
1649 std::vector<uint8_t> buffer {Tag, 0x00} ;
1651 buffer.push_back(Status);
1653 buffer.push_back((ResourceId >> 24) & 0xFF);
1654 buffer.push_back((ResourceId >> 16) & 0xFF);
1655 buffer.push_back((ResourceId >> 8) & 0xFF);
1656 buffer.push_back( ResourceId & 0xFF);
1658 buffer.push_back((SessionId >> 8) & 0xFF);
1659 buffer.push_back( SessionId & 0xFF);
1660 buffer[1] = buffer.size() - 2;
1667 if (session && session->SessionId() == SessionId)
1676 if (session && session->Tc()->Slot() == Slot && session->ResourceId() == ResourceId)
1687 switch (ResourceId) {
1704 if (Length == 6 && *(Data + 1) == 0x04) {
1707 switch (ResourceId) {
1722 esyslog(
"ERROR: can't create session for resource identifier: %08X",
1726 default:
esyslog(
"ERROR: unknown resource identifier: %08X", ResourceId);
1736 if (Session &&
m_sessions[SessionId - 1] == Session) {
1743 esyslog(
"ERROR: unknown session id: %d", SessionId);
1752 if (session && session->Tc()->Slot() == Slot) {
1765 for (
int Slot = 0; Slot <
m_numSlots; Slot++)
1771 const uint8_t *Data =
m_tc->
Data(Length);
1772 if (Data && Length > 1)
1779 int SessionId = ntohs(*(
short *)&Data[2]);
1783 Session->
Process(Length - 4, Data + 4);
1794 esyslog(
"ERROR: unknown session id: %d", SessionId);
1811 esyslog(
"ERROR: unknown session tag: %02X", *Data);
1827 bool UserIO =
false;
1831 if (session && session->Process())
1833 UserIO |= session->HasUserIO();
1860 for (
int Slot = 0; Slot <
m_numSlots; Slot++) {
1871 for (
int Slot = 0; Slot <
m_numSlots; Slot++) {
1891 return cas && cas->
SendPMT(CaPmt);
1923 m_numSlots(NumSlots)
1925 esyslog(
"New High level CI handler");
1937 msg->msg[2] = tag & 0xff;
1938 msg->msg[1] = (tag & 0xff00) >> 8;
1939 msg->msg[0] = (tag & 0xff0000) >> 16;
1940 esyslog(
"Sending message=[%02x %02x %02x ]",
1941 msg->msg[0], msg->msg[1], msg->msg[2]);
1944 return ioctl(
m_fdCa, function, msg);
1949 return CommHL(tag, CA_GET_MSG, msg);
1954 return CommHL(tag, CA_SEND_MSG, msg);
1961 struct ca_msg msg {};
1967 esyslog(
"HLCI communication failed");
1972 esyslog(
"HLCI communication failed");
1974 QString message(
"Debug: ");
1975 for(
int i = 0; i < 20; i++) {
1976 message += QString(
"%1 ").arg(msg.msg[i]);
1978 LOG(VB_GENERAL, LOG_DEBUG, message);
1981 const uint8_t *
d = &msg.msg[4];
1983 unsigned short id = ((
unsigned short)(*
d) << 8) | *(
d + 1);
2026 struct ca_msg msg {};
2035 esyslog(
"CA message too long");
2042 esyslog(
"HLCI communication failed");
2051 if ((ioctl(
m_fdCa, CA_RESET)) < 0) {
2052 esyslog(
"ioctl CA_RESET failed.");
void AddCaDescriptor(int ca_system_id, int ca_pid, int data_len, const uint8_t *data)
uint8_t m_capmt[2048]
XXX is there a specified maximum?
void AddElementaryStream(int type, int pid)
cCiCaPmt(int ProgramNumber, uint8_t cplm=CPLM_ONLY)
bool SendPMT(const cCiCaPmt &CaPmt)
cCiConditionalAccessSupport(int SessionId, cCiTransportConnection *Tc)
dvbca_vector m_caSystemIds
dvbca_vector GetCaSystemIds(void)
bool Process(int Length=0, const uint8_t *Data=nullptr) override
bool NeedCaPmt(void) const
bool Process(int Length=0, const uint8_t *Data=nullptr) override
cCiDateTime(int SessionId, cCiTransportConnection *Tc)
void SetTimeOffset(double offset)
bool Reply(const char *s)
virtual int NumSlots(void)=0
static cCiHandler * CreateCiHandler(const char *FileName)
cCiMMI(int SessionId, cCiTransportConnection *Tc)
bool HasUserIO(void) override
bool Process(int Length=0, const uint8_t *Data=nullptr) override
bool SendMenuAnswer(uint8_t Selection)
bool SendAnswer(const char *Text)
char * GetText(int &Length, const uint8_t **Data)
cCiEnquiry * Enquiry(void)
bool Process(int Length=0, const uint8_t *Data=nullptr) override
cCiResourceManager(int SessionId, cCiTransportConnection *Tc)
static int GetTag(int &Length, const uint8_t **Data)
const cCiTransportConnection * Tc(void)
cCiTransportConnection * m_tc
virtual bool Process(int Length=0, const uint8_t *Data=nullptr)
int ResourceId(void) const
int SendData(int Tag, std::vector< uint8_t > &Data)
int SendData(int Tag, int Length=0, const uint8_t *Data=nullptr)
static const uint8_t * GetData(const uint8_t *Data, int &Length)
virtual bool HasUserIO(void)
virtual ~cCiSession()=default
int SessionId(void) const
cCiSession(int SessionId, int ResourceId, cCiTransportConnection *Tc)
~cCiTransportConnection()
int CreateConnection(void)
void Init(int Fd, uint8_t Slot, uint8_t Tcid)
int SendData(std::vector< uint8_t > &Data)
bool DataAvailable(void) const
std::chrono::milliseconds m_lastPoll
int LastResponse(void) const
int SendTPDU(uint8_t Tag, int Length=0, const uint8_t *Data=nullptr) const
cCiTransportConnection(void)
int SendData(int Length, const uint8_t *Data)
const uint8_t * Data(int &Length)
std::array< cCiTransportConnection, MAX_CI_CONNECT > m_tc
bool ResetSlot(int Slot) const
cCiTransportConnection * NewConnection(int Slot)
cCiTransportLayer(int Fd, int NumSlots)
cCiTransportConnection * Process(int Slot)
bool ModuleReady(int Slot) const
bool NeedCaPmt(void) override
int SendData(unsigned tag, struct ca_msg *msg)
bool EnterMenu(int Slot) override
cHlCiHandler(int Fd, int NumSlots)
cCiMenu * GetMenu(void) override
cCiEnquiry * GetEnquiry(void) override
dvbca_vector GetCaSystemIds(int Slot) override
bool SetCaPmt(cCiCaPmt &CaPmt)
dvbca_vector m_caSystemIds
bool Process(void) override
int GetData(unsigned tag, struct ca_msg *msg)
int CommHL(unsigned tag, unsigned function, struct ca_msg *msg) const
bool Reset(int Slot) const
bool CloseSession(int SessionId)
bool OpenSession(int Length, const uint8_t *Data)
int CloseAllSessions(int Slot)
bool Send(uint8_t Tag, int SessionId, int ResourceId=0, int Status=-1)
cCiMenu * GetMenu(void) override
cCiSession * m_sessions[MAX_CI_SESSION]
cCiEnquiry * GetEnquiry(void) override
bool Process(void) override
int NumSlots(void) override
cCiTransportLayer * m_tpl
void SetTimeOffset(double offset_in_seconds) override
cLlCiHandler(int Fd, int NumSlots)
cCiSession * CreateSession(int ResourceId)
bool EnterMenu(int Slot) override
static int ResourceIdToInt(const uint8_t *Data)
cCiSession * GetSessionByResourceId(int ResourceId, int Slot)
dvbca_vector GetCaSystemIds(int Slot) override
cCiSession * GetSessionBySessionId(int SessionId)
bool SetCaPmt(cCiCaPmt &CaPmt)
cCiTransportConnection * m_tc
const uint8_t * Data(int &Length)
std::array< uint8_t, MAX_TPDU_SIZE > m_data
const uint8_t * GetData(const uint8_t *Data, int &Length) const
static constexpr size_t MAX_TPDU_SIZE
@ AOT_SUBTITLE_DOWNLOAD_LAST
@ AOT_APPLICATION_INFO_ENQ
@ AOT_CONNECTION_DESCRIPTOR
@ AOT_SUBTITLE_SEGMENT_MORE
@ AOT_SUBTITLE_DOWNLOAD_MORE
@ AOT_SUBTITLE_SEGMENT_LAST
static char * GetString(int &Length, const uint8_t **Data)
static constexpr uint8_t DATA_INDICATOR
static constexpr int SIZE_INDICATOR
static constexpr uint8_t BYTE1(uint16_t a)
static uint8_t * SetLength(uint8_t *Data, int Length)
static constexpr size_t MAX_CI_CONNECT
@ DCC_DISPLAY_CHARACTER_TABLE_LIST
@ DCC_OVERLAY_GRAPHICS_CHARACTERISTICS
@ DCC_FULL_SCREEN_GRAPHICS_CHARACTERISTICS
@ DCC_INPUT_CHARACTER_TABLE_LIST
@ RI_APPLICATION_INFORMATION
@ RI_CONDITIONAL_ACCESS_SUPPORT
static constexpr uint8_t EF_BLIND
static constexpr int MAX_TPDU_DATA
@ DRI_LIST_FULL_SCREEN_GRAPHIC_CHARACTERISTICS
@ DRI_UNKNOWN_DISPLAY_CONTROL_CMD
@ DRI_LIST_GRAPHIC_OVERLAY_CHARACTERISTICS
@ DRI_UNKNOWN_CHARACTER_TABLE
@ DRI_LIST_DISPLAY_CHARACTER_TABLES
@ DRI_LIST_INPUT_CHARACTER_TABLES
static ssize_t safe_read(int filedes, void *buffer, size_t size)
static constexpr int TIMEOUT
static bool sDebugProtocol
static constexpr int CAM_READ_TIMEOUT
@ MM_LOW_LEVEL_OVERLAY_GRAPHICS
@ MM_LOW_LEVEL_FULL_SCREEN_GRAPHICS
static char * CopyString(int Length, const uint8_t *Data)
static constexpr uint8_t DEC2BCD(uint8_t d)
#define dbgprotocol(a...)
static constexpr std::chrono::milliseconds POLL_INTERVAL
static constexpr int ERROR
static constexpr int8_t MAX_CONNECT_RETRIES
@ ST_CLOSE_SESSION_RESPONSE
@ ST_CLOSE_SESSION_REQUEST
@ ST_OPEN_SESSION_RESPONSE
@ ST_OPEN_SESSION_REQUEST
@ ST_CREATE_SESSION_RESPONSE
static const uint8_t * GetLength(const uint8_t *Data, int &Length)
static bool sDumpTPDUDataTransfer
static constexpr uint8_t BYTE0(uint16_t a)
static constexpr time_t WRKRND_TIME_BEFORE_ENTER_MENU
std::vector< uint16_t > dvbca_vector
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 const iso6937table * d
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
def read(device=None, features=[])
def write(text, progress=True)