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)
168static std::string
CopyString(
int Length,
const uint8_t *Data)
174 return {
reinterpret_cast<const char*
>(Data),
static_cast<size_t>(Length) };
177static std::string
GetString(
int &Length,
const uint8_t **Data)
186 if (Length > 0 && Data && *Data) {
190 Length -=
d - *Data + l;
214 esyslog(
"cMutex Lock inbalance detected");
218 pthread_mutex_unlock(&
m_mutex);
266 std::array<uint8_t,MAX_TPDU_SIZE>
m_data {0};
267 const uint8_t *
GetData(
const uint8_t *
Data,
int &Length)
const;
278 void Dump(
bool Outgoing);
281cTPDU::cTPDU(uint8_t Slot, uint8_t Tcid, uint8_t Tag,
int Length,
const uint8_t *Data)
306 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);
324 esyslog(
"ERROR: unknown TPDU tag: 0x%02X",
Tag);
333 esyslog(
"ERROR: attemp to write TPDU with zero size");
352 static constexpr ssize_t MAX_DUMP { 256 };
353 QString msg = QString(
"%1 ").arg(Outgoing ?
"-->" :
"<--");
354 for (
int i = 0; i <
m_size && i < MAX_DUMP; i++)
355 msg += QString(
"%1 ").arg((
short int)
m_data[i], 2, 16, QChar(
'0'));
358 LOG(VB_DVBCAM, LOG_INFO, msg);
361 for (
int i = 0; i <
m_size && i < MAX_DUMP; i++)
362 msg += QString(
"%1 ").arg(isprint(
m_data[i]) ?
m_data[i] :
'.', 2);
365 LOG(VB_DVBCAM, LOG_INFO, msg);
406 void Init(
int Fd, uint8_t
Slot, uint8_t Tcid);
407 int SendTPDU(uint8_t Tag,
int Length = 0,
const uint8_t *
Data =
nullptr)
const;
422 const uint8_t *
Data(
int &Length);
459 std::array<struct pollfd,1> pfd {};
461 pfd[0].events = POLLIN;
466 if (ret == -1 && (errno == EAGAIN || errno == EINTR))
472 (pfd[0].revents & POLLIN) &&
494 default:
return ERROR;
558 dsyslog(
"CAM: retrying to establish connection");
560 dsyslog(
"CAM: connection established");
579 auto curr_time = nowAsDuration<std::chrono::milliseconds>();
580 std::chrono::milliseconds msdiff = curr_time -
m_lastPoll;
601 std::array<cCiTransportConnection,MAX_CI_CONNECT>
m_tc;
622 dbgprotocol(
"Creating connection: slot %d, tcid %zd\n", Slot, i + 1);
624 if (
m_tc[i].CreateConnection() ==
OK)
635 if (ioctl(
m_fd, CA_RESET, 1 << Slot) != -1) {
639 esyslog(
"ERROR: can't reset CAM slot %d: %m", Slot);
646 ca_slot_info_t sinfo;
648 if (ioctl(
m_fd, CA_GET_SLOT_INFO, &sinfo) != -1)
649 return (sinfo.flags & CA_CI_MODULE_READY) != 0U;
650 esyslog(
"ERROR: can't get info on CAM slot %d: %m", Slot);
656 for (
auto & conn :
m_tc) {
658 if (Tc->
Slot() == Slot) {
659 switch (Tc->
State()) {
784 static int GetTag(
int &Length,
const uint8_t **Data);
785 static const uint8_t *
GetData(
const uint8_t *Data,
int &Length);
786 int SendData(
int Tag,
int Length = 0,
const uint8_t *Data =
nullptr);
788 {
return SendData(Tag, Data.size(), Data.data()); };
796 virtual bool Process(
int Length = 0,
const uint8_t *Data =
nullptr);
800 : m_sessionId(SessionId),
801 m_resourceId(ResourceId),
815 if (Length >= 3 && Data && *Data) {
817 for (
int i = 0; i < 3; i++)
818 t = (
t << 8) | *(*Data)++;
828 return Length ? Data :
nullptr;
835 esyslog(
"ERROR: CAM: data length (%d) is negative", Length);
839 if ((Length > 0) && !Data)
841 esyslog(
"ERROR: CAM: Data pointer null");
845 std::vector<uint8_t> buffer {
849 static_cast<uint8_t
>((Tag >> 16) & 0xFF),
850 static_cast<uint8_t
>((Tag >> 8) & 0xFF),
851 static_cast<uint8_t
>((Tag ) & 0xFF)} ;
852 buffer.reserve(2048);
855 if (buffer.size() + Length >= buffer.capacity())
857 esyslog(
"ERROR: CAM: data length (%d) exceeds buffer size", Length);
863 buffer.insert(buffer.end(), Data, Data + Length);
869 [[maybe_unused]]
const uint8_t *Data)
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);
952 bool Process(
int Length = 0,
const uint8_t *Data =
nullptr)
override;
973 int Tag =
GetTag(Length, &Data);
978 const uint8_t *
d =
GetData(Data, l);
996 default:
esyslog(
"ERROR: CI application information: unknown tag %06X", Tag);
1027 bool Process(
int Length = 0,
const uint8_t *Data =
nullptr)
override;
1043 int Tag =
GetTag(Length, &Data);
1048 const uint8_t *
d =
GetData(Data, l);
1050 unsigned short id = ((
unsigned short)(*
d) << 8) | *(
d + 1);
1069 default:
esyslog(
"ERROR: CI conditional access support: unknown tag %06X", Tag);
1101 bool Process(
int Length = 0,
const uint8_t *Data =
nullptr)
override;
1118 {
return ((
d / 10) << 4) + (
d % 10); }
1120 {
return static_cast<uint8_t
>(a & 0xFF); }
1122 {
return static_cast<uint8_t
>((a >> 8) & 0xFF); }
1126 time_t
t = time(
nullptr);
1127 struct tm tm_gmt {};
1128 struct tm tm_loc {};
1136 if (gmtime_r(&
t, &tm_gmt) && localtime_r(&
t, &tm_loc)) {
1137 int Y = tm_gmt.tm_year;
1138 int M = tm_gmt.tm_mon + 1;
1139 int D = tm_gmt.tm_mday;
1140 int L = (M == 1 || M == 2) ? 1 : 0;
1141 int MJD = 14956 +
D + int((Y - L) * 365.25) + int((M + 1 + L * 12) * 30.6001);
1143 int16_t local_offset = htons(tm_loc.tm_gmtoff / 60);
1144 std::vector<uint8_t> T {
1150 BYTE0(local_offset),
1165 int Tag =
GetTag(Length, &Data);
1170 const uint8_t *
d =
GetData(Data, l);
1178 default:
esyslog(
"ERROR: CI date time: unknown tag %06X", Tag);
1242 std::string
GetText(
int &Length,
const uint8_t **Data);
1248 bool Process(
int Length = 0,
const uint8_t *Data =
nullptr)
override;
1279 int Tag =
GetTag(Length, Data);
1281 std::string s =
GetString(Length, Data);
1285 esyslog(
"CI MMI: unexpected text tag: %06X", Tag);
1292 int Tag =
GetTag(Length, &Data);
1297 const uint8_t *
d =
GetData(Data, l);
1302 struct tDisplayReply { uint8_t m_id; uint8_t m_mode; };
1303 tDisplayReply dr {};
1310 default:
esyslog(
"CI MMI: unsupported display control command %02X", *
d);
1322 const uint8_t *
d =
GetData(Data, l);
1347 const uint8_t *
d =
GetData(Data, l);
1349 uint8_t blind = *
d++;
1362 const uint8_t *
d =
GetData(Data, l);
1372 default:
esyslog(
"ERROR: CI MMI: unknown close_mmi_cmd_id %02X", *
d);
1379 default:
esyslog(
"ERROR: CI MMI: unknown tag %06X", Tag);
1419 strncpy(answer.m_text, Text,
sizeof(answer.m_text) - 1);
1420 answer.m_text[255] =
'\0';
1431 m_selectable(Selectable)
1438 if (
m_mmi && -1 <= Index && Index <
static_cast<int>(
m_entries.size()))
1457 return Reply(
nullptr);
1488 esyslog(
"ERROR: buffer overflow in CA_PMT");
1520 const uint8_t *data)
1524 esyslog(
"ERROR: adding CA descriptor without program/stream!");
1530 esyslog(
"ERROR: buffer overflow in CA_PMT");
1562 m_numSlots(NumSlots),
1578 int fd_ca = open(FileName, O_RDWR);
1582 if (ioctl(fd_ca, CA_GET_CAP, &Caps) == 0)
1587 if (Caps.slot_type & CA_CI_LINK)
1589 if (Caps.slot_type & CA_CI)
1591 isyslog(
"CAM doesn't support either high or low level CI,"
1592 " Caps.slot_type=%i", Caps.slot_type);
1596 esyslog(
"ERROR: no CAM slots found");
1610 return (ntohl(*(
int *)Data));
1615 std::vector<uint8_t> buffer {Tag, 0x00} ;
1617 buffer.push_back(Status);
1619 buffer.push_back((ResourceId >> 24) & 0xFF);
1620 buffer.push_back((ResourceId >> 16) & 0xFF);
1621 buffer.push_back((ResourceId >> 8) & 0xFF);
1622 buffer.push_back( ResourceId & 0xFF);
1624 buffer.push_back((SessionId >> 8) & 0xFF);
1625 buffer.push_back( SessionId & 0xFF);
1626 buffer[1] = buffer.size() - 2;
1633 if (session && session->SessionId() == SessionId)
1642 if (session && session->Tc()->Slot() == Slot && session->ResourceId() == ResourceId)
1653 switch (ResourceId) {
1670 if (Length == 6 && *(Data + 1) == 0x04) {
1673 switch (ResourceId) {
1688 esyslog(
"ERROR: can't create session for resource identifier: %08X",
1692 default:
esyslog(
"ERROR: unknown resource identifier: %08X", ResourceId);
1702 if (Session &&
m_sessions[SessionId - 1] == Session) {
1709 esyslog(
"ERROR: unknown session id: %d", SessionId);
1718 if (session && session->Tc()->Slot() == Slot) {
1731 for (
int Slot = 0; Slot <
m_numSlots; Slot++)
1737 const uint8_t *Data =
m_tc->
Data(Length);
1738 if (Data && Length > 1)
1745 int SessionId = ntohs(*(
short *)&Data[2]);
1749 Session->
Process(Length - 4, Data + 4);
1760 esyslog(
"ERROR: unknown session id: %d", SessionId);
1777 esyslog(
"ERROR: unknown session tag: %02X", *Data);
1793 bool UserIO =
false;
1797 if (session && session->Process())
1799 UserIO |= session->HasUserIO();
1826 for (
int Slot = 0; Slot <
m_numSlots; Slot++) {
1837 for (
int Slot = 0; Slot <
m_numSlots; Slot++) {
1857 return cas && cas->
SendPMT(CaPmt);
1889 m_numSlots(NumSlots)
1891 esyslog(
"New High level CI handler");
1903 msg->msg[2] = tag & 0xff;
1904 msg->msg[1] = (tag & 0xff00) >> 8;
1905 msg->msg[0] = (tag & 0xff0000) >> 16;
1906 esyslog(
"Sending message=[%02x %02x %02x ]",
1907 msg->msg[0], msg->msg[1], msg->msg[2]);
1910 return ioctl(
m_fdCa, function, msg);
1915 return CommHL(tag, CA_GET_MSG, msg);
1920 return CommHL(tag, CA_SEND_MSG, msg);
1927 struct ca_msg msg {};
1933 esyslog(
"HLCI communication failed");
1938 esyslog(
"HLCI communication failed");
1940 QString message(
"Debug: ");
1941 for(
int i = 0; i < 20; i++) {
1942 message += QString(
"%1 ").arg(msg.msg[i]);
1944 LOG(VB_GENERAL, LOG_DEBUG, message);
1947 const uint8_t *
d = &msg.msg[4];
1949 unsigned short id = ((
unsigned short)(*
d) << 8) | *(
d + 1);
1992 struct ca_msg msg {};
2001 esyslog(
"CA message too long");
2008 esyslog(
"HLCI communication failed");
2017 if ((ioctl(
m_fdCa, CA_RESET)) < 0) {
2018 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)
cCiEnquiry * Enquiry(void)
std::string GetText(int &Length, const uint8_t **Data)
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 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 std::string GetString(int &Length, const uint8_t **Data)
static bool sDebugProtocol
static constexpr int CAM_READ_TIMEOUT
static std::string CopyString(int Length, const uint8_t *Data)
@ MM_LOW_LEVEL_OVERLAY_GRAPHICS
@ MM_LOW_LEVEL_FULL_SCREEN_GRAPHICS
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)