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);
868 [[maybe_unused]]
const uint8_t *Data)
880 bool Process(
int Length = 0,
const uint8_t *Data =
nullptr)
override;
893 int Tag =
GetTag(Length, &Data);
897 const std::array<const uint32_t,5> resources
907 reinterpret_cast<const uint8_t*
>(resources.data()));
915 const uint8_t *
d =
GetData(Data, l);
917 esyslog(
"CI resource manager: unexpected data");
923 esyslog(
"ERROR: CI resource manager: unexpected tag %06X in state %d", Tag,
m_state);
927 default:
esyslog(
"ERROR: CI resource manager: unknown tag %06X", Tag);
952 bool Process(
int Length = 0,
const uint8_t *Data =
nullptr)
override;
979 int Tag =
GetTag(Length, &Data);
984 const uint8_t *
d =
GetData(Data, l);
985 if ((l -= 1) < 0)
break;
987 if ((l -= 2) < 0)
break;
990 if ((l -= 2) < 0)
break;
1000 default:
esyslog(
"ERROR: CI application information: unknown tag %06X", Tag);
1031 bool Process(
int Length = 0,
const uint8_t *Data =
nullptr)
override;
1047 int Tag =
GetTag(Length, &Data);
1052 const uint8_t *
d =
GetData(Data, l);
1054 unsigned short id = ((
unsigned short)(*
d) << 8) | *(
d + 1);
1073 default:
esyslog(
"ERROR: CI conditional access support: unknown tag %06X", Tag);
1105 bool Process(
int Length = 0,
const uint8_t *Data =
nullptr)
override;
1122 {
return ((
d / 10) << 4) + (
d % 10); }
1124 {
return static_cast<uint8_t
>(a & 0xFF); }
1126 {
return static_cast<uint8_t
>((a >> 8) & 0xFF); }
1130 time_t
t = time(
nullptr);
1131 struct tm tm_gmt {};
1132 struct tm tm_loc {};
1140 if (gmtime_r(&
t, &tm_gmt) && localtime_r(&
t, &tm_loc)) {
1141 int Y = tm_gmt.tm_year;
1142 int M = tm_gmt.tm_mon + 1;
1143 int D = tm_gmt.tm_mday;
1144 int L = (M == 1 || M == 2) ? 1 : 0;
1145 int MJD = 14956 +
D + int((Y - L) * 365.25) + int((M + 1 + L * 12) * 30.6001);
1147 int16_t local_offset = htons(tm_loc.tm_gmtoff / 60);
1148 std::vector<uint8_t> T {
1154 BYTE0(local_offset),
1169 int Tag =
GetTag(Length, &Data);
1174 const uint8_t *
d =
GetData(Data, l);
1182 default:
esyslog(
"ERROR: CI date time: unknown tag %06X", Tag);
1246 char *
GetText(
int &Length,
const uint8_t **Data);
1252 bool Process(
int Length = 0,
const uint8_t *Data =
nullptr)
override;
1283 int Tag =
GetTag(Length, Data);
1289 esyslog(
"CI MMI: unexpected text tag: %06X", Tag);
1296 int Tag =
GetTag(Length, &Data);
1301 const uint8_t *
d =
GetData(Data, l);
1306 struct tDisplayReply { uint8_t m_id; uint8_t m_mode; };
1307 tDisplayReply dr {};
1314 default:
esyslog(
"CI MMI: unsupported display control command %02X", *
d);
1326 const uint8_t *
d =
GetData(Data, l);
1351 const uint8_t *
d =
GetData(Data, l);
1353 uint8_t blind = *
d++;
1366 const uint8_t *
d =
GetData(Data, l);
1376 default:
esyslog(
"ERROR: CI MMI: unknown close_mmi_cmd_id %02X", *
d);
1383 default:
esyslog(
"ERROR: CI MMI: unknown tag %06X", Tag);
1423 strncpy(answer.m_text, Text,
sizeof(answer.m_text) - 1);
1424 answer.m_text[255] =
'\0';
1483 return Reply(
nullptr);
1514 esyslog(
"ERROR: buffer overflow in CA_PMT");
1546 const uint8_t *data)
1550 esyslog(
"ERROR: adding CA descriptor without program/stream!");
1556 esyslog(
"ERROR: buffer overflow in CA_PMT");
1604 int fd_ca = open(FileName, O_RDWR);
1608 if (ioctl(fd_ca, CA_GET_CAP, &Caps) == 0)
1613 if (Caps.slot_type & CA_CI_LINK)
1615 if (Caps.slot_type & CA_CI)
1617 isyslog(
"CAM doesn't support either high or low level CI,"
1618 " Caps.slot_type=%i", Caps.slot_type);
1621 esyslog(
"ERROR: no CAM slots found");
1632 return (ntohl(*(
int *)Data));
1637 std::vector<uint8_t> buffer {Tag, 0x00} ;
1639 buffer.push_back(Status);
1641 buffer.push_back((ResourceId >> 24) & 0xFF);
1642 buffer.push_back((ResourceId >> 16) & 0xFF);
1643 buffer.push_back((ResourceId >> 8) & 0xFF);
1644 buffer.push_back( ResourceId & 0xFF);
1646 buffer.push_back((SessionId >> 8) & 0xFF);
1647 buffer.push_back( SessionId & 0xFF);
1648 buffer[1] = buffer.size() - 2;
1655 if (session && session->SessionId() == SessionId)
1664 if (session && session->Tc()->Slot() == Slot && session->ResourceId() == ResourceId)
1675 switch (ResourceId) {
1692 if (Length == 6 && *(Data + 1) == 0x04) {
1695 switch (ResourceId) {
1710 esyslog(
"ERROR: can't create session for resource identifier: %08X",
1714 default:
esyslog(
"ERROR: unknown resource identifier: %08X", ResourceId);
1724 if (Session &&
m_sessions[SessionId - 1] == Session) {
1731 esyslog(
"ERROR: unknown session id: %d", SessionId);
1740 if (session && session->Tc()->Slot() == Slot) {
1753 for (
int Slot = 0; Slot <
m_numSlots; Slot++)
1759 const uint8_t *Data =
m_tc->
Data(Length);
1760 if (Data && Length > 1)
1767 int SessionId = ntohs(*(
short *)&Data[2]);
1771 Session->
Process(Length - 4, Data + 4);
1781 esyslog(
"ERROR: unknown session id: %d", SessionId);
1797 esyslog(
"ERROR: unknown session tag: %02X", *Data);
1813 bool UserIO =
false;
1817 if (session && session->Process())
1819 UserIO |= session->HasUserIO();
1846 for (
int Slot = 0; Slot <
m_numSlots; Slot++) {
1857 for (
int Slot = 0; Slot <
m_numSlots; Slot++) {
1877 return cas && cas->
SendPMT(CaPmt);
1911 esyslog(
"New High level CI handler");
1923 msg->msg[2] = tag & 0xff;
1924 msg->msg[1] = (tag & 0xff00) >> 8;
1925 msg->msg[0] = (tag & 0xff0000) >> 16;
1926 esyslog(
"Sending message=[%02x %02x %02x ]",
1927 msg->msg[0], msg->msg[1], msg->msg[2]);
1930 return ioctl(
m_fdCa,
function, msg);
1935 return CommHL(tag, CA_GET_MSG, msg);
1940 return CommHL(tag, CA_SEND_MSG, msg);
1947 struct ca_msg msg {};
1953 esyslog(
"HLCI communication failed");
1958 esyslog(
"HLCI communication failed");
1960 QString message(
"Debug: ");
1961 for(
int i = 0; i < 20; i++) {
1962 message += QString(
"%1 ").arg(msg.msg[i]);
1964 LOG(VB_GENERAL, LOG_DEBUG, message);
1967 const uint8_t *
d = &msg.msg[4];
1969 unsigned short id = ((
unsigned short)(*
d) << 8) | *(
d + 1);
2012 struct ca_msg msg {};
2021 esyslog(
"CA message too long");
2028 esyslog(
"HLCI communication failed");
2037 if ((ioctl(
m_fdCa, CA_RESET)) < 0) {
2038 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)