35#include <linux/dvb/ca.h>
36#include <netinet/in.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))
69static constexpr int OK { 0 };
71static constexpr int ERROR { -2 };
83static 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);
95static const uint8_t *
GetLength(
const uint8_t *Data,
int &Length)
109 int l = Length & ~SIZE_INDICATOR;
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;
145static 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);
178static 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;
279 void Dump(
bool Outgoing);
282cTPDU::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);
1520 esyslog(
"ERROR: buffer overflow in CA_PMT");
1552 const uint8_t *data)
1556 esyslog(
"ERROR: adding CA descriptor without program/stream!");
1562 esyslog(
"ERROR: buffer overflow in CA_PMT");
1594 m_numSlots(NumSlots),
1610 int fd_ca = open(FileName, O_RDWR);
1614 if (ioctl(fd_ca, CA_GET_CAP, &Caps) == 0)
1619 if (Caps.slot_type & CA_CI_LINK)
1621 if (Caps.slot_type & CA_CI)
1623 isyslog(
"CAM doesn't support either high or low level CI,"
1624 " Caps.slot_type=%i", Caps.slot_type);
1628 esyslog(
"ERROR: no CAM slots found");
1642 return (ntohl(*(
int *)Data));
1647 std::vector<uint8_t> buffer {Tag, 0x00} ;
1649 buffer.push_back(Status);
1651 buffer.push_back((ResourceId >> 24) & 0xFF);
1652 buffer.push_back((ResourceId >> 16) & 0xFF);
1653 buffer.push_back((ResourceId >> 8) & 0xFF);
1654 buffer.push_back( ResourceId & 0xFF);
1656 buffer.push_back((SessionId >> 8) & 0xFF);
1657 buffer.push_back( SessionId & 0xFF);
1658 buffer[1] = buffer.size() - 2;
1665 if (session && session->SessionId() == SessionId)
1674 if (session && session->Tc()->Slot() == Slot && session->ResourceId() == ResourceId)
1685 switch (ResourceId) {
1702 if (Length == 6 && *(Data + 1) == 0x04) {
1705 switch (ResourceId) {
1720 esyslog(
"ERROR: can't create session for resource identifier: %08X",
1724 default:
esyslog(
"ERROR: unknown resource identifier: %08X", ResourceId);
1734 if (Session &&
m_sessions[SessionId - 1] == Session) {
1741 esyslog(
"ERROR: unknown session id: %d", SessionId);
1750 if (session && session->Tc()->Slot() == Slot) {
1763 for (
int Slot = 0; Slot <
m_numSlots; Slot++)
1769 const uint8_t *Data =
m_tc->
Data(Length);
1770 if (Data && Length > 1)
1777 int SessionId = ntohs(*(
short *)&Data[2]);
1781 Session->
Process(Length - 4, Data + 4);
1792 esyslog(
"ERROR: unknown session id: %d", SessionId);
1809 esyslog(
"ERROR: unknown session tag: %02X", *Data);
1825 bool UserIO =
false;
1829 if (session && session->Process())
1831 UserIO |= session->HasUserIO();
1858 for (
int Slot = 0; Slot <
m_numSlots; Slot++) {
1869 for (
int Slot = 0; Slot <
m_numSlots; Slot++) {
1889 return cas && cas->
SendPMT(CaPmt);
1921 m_numSlots(NumSlots)
1923 esyslog(
"New High level CI handler");
1935 msg->msg[2] = tag & 0xff;
1936 msg->msg[1] = (tag & 0xff00) >> 8;
1937 msg->msg[0] = (tag & 0xff0000) >> 16;
1938 esyslog(
"Sending message=[%02x %02x %02x ]",
1939 msg->msg[0], msg->msg[1], msg->msg[2]);
1942 return ioctl(
m_fdCa, function, msg);
1947 return CommHL(tag, CA_GET_MSG, msg);
1952 return CommHL(tag, CA_SEND_MSG, msg);
1959 struct ca_msg msg {};
1965 esyslog(
"HLCI communication failed");
1970 esyslog(
"HLCI communication failed");
1972 QString message(
"Debug: ");
1973 for(
int i = 0; i < 20; i++) {
1974 message += QString(
"%1 ").arg(msg.msg[i]);
1976 LOG(VB_GENERAL, LOG_DEBUG, message);
1979 const uint8_t *
d = &msg.msg[4];
1981 unsigned short id = ((
unsigned short)(*
d) << 8) | *(
d + 1);
2024 struct ca_msg msg {};
2033 esyslog(
"CA message too long");
2040 esyslog(
"HLCI communication failed");
2049 if ((ioctl(
m_fdCa, CA_RESET)) < 0) {
2050 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)