MythTV  master
dvbci.cpp
Go to the documentation of this file.
1 /*
2  * ci.cc: Common Interface
3  *
4  * Copyright (C) 2000 Klaus Schmidinger
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19  * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
20  *
21  * The author can be reached at kls@cadsoft.de
22  *
23  * The project's page is at http://www.cadsoft.de/people/kls/vdr
24  *
25  */
26 
27 #include "dvbci.h"
28 
29 #include <cctype>
30 #include <cerrno>
31 #include <cstring>
32 #include <ctime>
33 #include <fcntl.h>
34 #include <linux/dvb/ca.h>
35 #include <netinet/in.h>
36 #include <poll.h>
37 #include <sys/ioctl.h>
38 #include <sys/time.h>
39 #include <unistd.h>
40 #ifdef __FreeBSD__
41 # include <stdlib.h>
42 #else
43 # include <malloc.h>
44 #endif
45 
46 #include <QString>
47 
48 #include "mythlogging.h"
49 
50 #ifndef MALLOC
51 #define MALLOC(type, size) (type *)malloc(sizeof(type) * (size))
52 #endif
53 
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))
57 
58 #define LOG_ERROR esyslog("ERROR (%s,%d): %m", __FILE__, __LINE__)
59 #define LOG_ERROR_STR(s) esyslog("ERROR: %s: %m", s)
60 
61 
62 // Set these to 'true' for debug output:
63 static bool sDumpTPDUDataTransfer = false;
64 static bool sDebugProtocol = false;
65 static bool sConnected = false;
66 
67 #define dbgprotocol(a...) if (sDebugProtocol) LOG(VB_DVBCAM, LOG_DEBUG, QString::asprintf(a))
68 
69 #define OK 0
70 #define TIMEOUT (-1)
71 #define ERROR (-2)
72 
73 // --- Workarounds -----------------------------------------------------------
74 
75 // The Irdeto AllCAM 4.7 (and maybe others, too) does not react on AOT_ENTER_MENU
76 // during the first few seconds of a newly established connection
77 #define WRKRND_TIME_BEFORE_ENTER_MENU 15 // seconds
78 
79 // --- Helper functions ------------------------------------------------------
80 
81 #define SIZE_INDICATOR 0x80
82 
83 static ssize_t safe_read(int filedes, void *buffer, size_t size)
84 {
85  for (;;) {
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);
89  continue;
90  }
91  return p;
92  }
93 }
94 
95 static const uint8_t *GetLength(const uint8_t *Data, int &Length)
106 {
107  Length = *Data++;
108  if ((Length & SIZE_INDICATOR) != 0) {
109  int l = Length & ~SIZE_INDICATOR;
110  Length = 0;
111  for (int i = 0; i < l; i++)
112  Length = (Length << 8) | *Data++;
113  }
114  return Data;
115 }
116 
117 static uint8_t *SetLength(uint8_t *Data, int Length)
127 {
128  uint8_t *p = Data;
129  if (Length < 128)
130  *p++ = Length;
131  else {
132  int n = sizeof(Length);
133  for (int i = n - 1; i >= 0; i--) {
134  int b = (Length >> (8 * i)) & 0xFF;
135  if (p != Data || b)
136  *++p = b;
137  }
138  *Data = (p - Data) | SIZE_INDICATOR;
139  p++;
140  }
141  return p;
142 }
143 
145 static void SetLength(std::vector<uint8_t> &Data, int Length)
146 {
147  if (Length < 128)
148  {
149  Data.push_back(Length);
150  return;
151  }
152 
153  // This will be replaced with the number of bytes in the length
154  size_t len_offset = Data.size();
155  Data.push_back(0);
156 
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)
161  Data.push_back(b);
162  }
163  Data[len_offset] = (Data.size() - len_offset) | SIZE_INDICATOR;
164 }
165 
166 static char *CopyString(int Length, const uint8_t *Data)
171 {
172  char *s = MALLOC(char, Length + 1);
173  strncpy(s, (char *)Data, Length);
174  s[Length] = 0;
175  return s;
176 }
177 
178 static char *GetString(int &Length, const uint8_t **Data)
186 {
187  if (Length > 0 && Data && *Data) {
188  int l = 0;
189  const uint8_t *d = GetLength(*Data, l);
190  char *s = CopyString(l, d);
191  Length -= d - *Data + l;
192  *Data = d + l;
193  return s;
194  }
195  return nullptr;
196 }
197 
198 
199 
200 // --- cMutex ----------------------------------------------------------------
201 
202 void cMutex::Lock(void)
203 {
204  if (getpid() != m_lockingPid || !m_locked) {
205  pthread_mutex_lock(&m_mutex);
206  m_lockingPid = getpid();
207  }
208  m_locked++;
209 }
210 
211 void cMutex::Unlock(void)
212 {
213  if (--m_locked <= 0) {
214  if (m_locked < 0) {
215  esyslog("cMutex Lock inbalance detected");
216  m_locked = 0;
217  }
218  m_lockingPid = 0;
219  pthread_mutex_unlock(&m_mutex);
220  }
221 }
222 // --- cMutexLock ------------------------------------------------------------
223 
225 {
226  if (m_mutex && m_locked)
227  m_mutex->Unlock();
228 }
229 
231 {
232  if (Mutex && !m_mutex) {
233  m_mutex = Mutex;
234  Mutex->Lock();
235  m_locked = true;
236  return true;
237  }
238  return false;
239 }
240 
241 
242 
243 // --- cTPDU -----------------------------------------------------------------
244 
245 #define MAX_TPDU_SIZE 2048
246 #define MAX_TPDU_DATA (MAX_TPDU_SIZE - 4)
247 
248 #define DATA_INDICATOR 0x80
249 
250 #define T_SB 0x80
251 #define T_RCV 0x81
252 #define T_CREATE_TC 0x82
253 #define T_CTC_REPLY 0x83
254 #define T_DELETE_TC 0x84
255 #define T_DTC_REPLY 0x85
256 #define T_REQUEST_TC 0x86
257 #define T_NEW_TC 0x87
258 #define T_TC_ERROR 0x88
259 #define T_DATA_LAST 0xA0
260 #define T_DATA_MORE 0xA1
261 
262 class cTPDU {
263 private:
264  int m_size {0};
265  std::array<uint8_t,MAX_TPDU_SIZE> m_data {0};
266  const uint8_t *GetData(const uint8_t *Data, int &Length) const;
267 public:
268  cTPDU(void) = default;
269  cTPDU(uint8_t Slot, uint8_t Tcid, uint8_t Tag, int Length = 0, const uint8_t *Data = nullptr);
270  uint8_t Slot(void) { return m_data[0]; }
271  uint8_t Tcid(void) { return m_data[1]; }
272  uint8_t Tag(void) { return m_data[2]; }
273  const uint8_t *Data(int &Length) { return GetData(m_data.data() + 3, Length); }
274  uint8_t Status(void);
275  int Write(int fd);
276  int Read(int fd);
277  void Dump(bool Outgoing);
278  };
279 
280 cTPDU::cTPDU(uint8_t Slot, uint8_t Tcid, uint8_t Tag, int Length, const uint8_t *Data)
281 {
282  m_data[0] = Slot;
283  m_data[1] = Tcid;
284  m_data[2] = Tag;
285  switch (Tag) {
286  case T_RCV:
287  case T_CREATE_TC:
288  case T_CTC_REPLY:
289  case T_DELETE_TC:
290  case T_DTC_REPLY:
291  case T_REQUEST_TC:
292  m_data[3] = 1; // length
293  m_data[4] = Tcid;
294  m_size = 5;
295  break;
296  case T_NEW_TC:
297  case T_TC_ERROR:
298  if (Length == 1) {
299  m_data[3] = 2; // length
300  m_data[4] = Tcid;
301  m_data[5] = Data[0];
302  m_size = 6;
303  }
304  else
305  esyslog("ERROR: illegal data length for TPDU tag 0x%02X: %d", Tag, Length);
306  break;
307  case T_DATA_LAST:
308  case T_DATA_MORE:
309  if (Length <= MAX_TPDU_DATA) {
310  uint8_t *p = m_data.data() + 3;
311  p = SetLength(p, Length + 1);
312  *p++ = Tcid;
313  if (Length)
314  memcpy(p, Data, Length);
315  m_size = Length + (p - m_data.data());
316  }
317  else
318  esyslog("ERROR: illegal data length for TPDU tag 0x%02X: %d", Tag, Length);
319  break;
320  default:
321  esyslog("ERROR: unknown TPDU tag: 0x%02X", Tag);
322  }
323  }
324 
325 int cTPDU::Write(int fd)
326 {
327  Dump(true);
328  if (m_size)
329  return write(fd, m_data.data(), m_size) == m_size ? OK : ERROR;
330  esyslog("ERROR: attemp to write TPDU with zero size");
331  return ERROR;
332 }
333 
334 int cTPDU::Read(int fd)
335 {
336  m_size = safe_read(fd, m_data.data(), m_data.size());
337  if (m_size < 0) {
338  esyslog("ERROR: %m");
339  m_size = 0;
340  return ERROR;
341  }
342  Dump(false);
343  return OK;
344 }
345 
346 void cTPDU::Dump(bool Outgoing)
347 {
348  if (sDumpTPDUDataTransfer) {
349 #define MAX_DUMP 256
350  QString msg = QString("%1 ").arg(Outgoing ? "-->" : "<--");
351  for (int i = 0; i < m_size && i < MAX_DUMP; i++)
352  msg += QString("%1 ").arg((short int)m_data[i], 2, 16, QChar('0'));
353  if (m_size >= MAX_DUMP)
354  msg += "...";
355  LOG(VB_DVBCAM, LOG_INFO, msg);
356  if (!Outgoing) {
357  msg = QString(" ");
358  for (int i = 0; i < m_size && i < MAX_DUMP; i++)
359  msg += QString("%1 ").arg(isprint(m_data[i]) ? m_data[i] : '.', 2);
360  if (m_size >= MAX_DUMP)
361  msg += "...";
362  LOG(VB_DVBCAM, LOG_INFO, msg);
363  }
364  }
365 }
366 
367 const uint8_t *cTPDU::GetData(const uint8_t *Data, int &Length) const
368 {
369  if (m_size) {
370  Data = GetLength(Data, Length);
371  if (Length) {
372  Length--; // the first byte is always the tcid
373  return Data + 1;
374  }
375  }
376  return nullptr;
377 }
378 
379 uint8_t cTPDU::Status(void)
380 {
381  if (m_size >= 4 && m_data[m_size - 4] == T_SB && m_data[m_size - 3] == 2) {
382  //XXX test tcid???
383  return m_data[m_size - 1];
384  }
385  return 0;
386 }
387 
388 // --- cCiTransportConnection ------------------------------------------------
389 
391 
393  friend class cCiTransportLayer;
394 private:
395  int m_fd {-1};
396  uint8_t m_slot {0};
397  uint8_t m_tcid {0};
399  cTPDU *m_tpdu {nullptr};
400  std::chrono::milliseconds m_lastPoll {0ms};
402  bool m_dataAvailable {false};
403  void Init(int Fd, uint8_t Slot, uint8_t Tcid);
404  int SendTPDU(uint8_t Tag, int Length = 0, const uint8_t *Data = nullptr) const;
405  int RecvTPDU(void);
406  int CreateConnection(void);
407  int Poll(void);
408  eState State(void) { return m_state; }
409  int LastResponse(void) const { return m_lastResponse; }
410  bool DataAvailable(void) const { return m_dataAvailable; }
411 public:
414  int Slot(void) const { return m_slot; }
415  int SendData(int Length, const uint8_t *Data);
416  int SendData(std::vector<uint8_t> &Data)
417  { return SendData(Data.size(), Data.data()); }
418  int RecvData(void);
419  const uint8_t *Data(int &Length);
420  //XXX Close()
421  };
422 
424 {
425  Init(-1, 0, 0);
426 }
427 
429 {
430  delete m_tpdu;
431 }
432 
433 void cCiTransportConnection::Init(int Fd, uint8_t Slot, uint8_t Tcid)
434 {
435  m_fd = Fd;
436  m_slot = Slot;
437  m_tcid = Tcid;
438  m_state = stIDLE;
439  if (m_fd >= 0 && !m_tpdu)
440  m_tpdu = new cTPDU;
442  m_dataAvailable = false;
443 //XXX Clear()???
444 }
445 
446 int cCiTransportConnection::SendTPDU(uint8_t Tag, int Length, const uint8_t *Data) const
447 {
448  cTPDU TPDU(m_slot, m_tcid, Tag, Length, Data);
449  return TPDU.Write(m_fd);
450 }
451 
452 #define CAM_READ_TIMEOUT 5000 // ms
453 
455 {
456  std::array<struct pollfd,1> pfd {};
457  pfd[0].fd = m_fd;
458  pfd[0].events = POLLIN;
460 
461  for (;;) {
462  int ret = poll(pfd.data(), 1, CAM_READ_TIMEOUT);
463  if (ret == -1 && (errno == EAGAIN || errno == EINTR))
464  continue;
465  break;
466  }
467 
468  if (
469  (pfd[0].revents & POLLIN) &&
470  m_tpdu->Read(m_fd) == OK &&
471  m_tpdu->Tcid() == m_tcid
472  )
473  {
474  switch (m_state) {
475  case stIDLE: break;
476  case stCREATION: if (m_tpdu->Tag() == T_CTC_REPLY) {
478  m_state = stACTIVE;
480  }
481  break;
482  case stACTIVE: switch (m_tpdu->Tag()) {
483  case T_SB:
484  case T_DATA_LAST:
485  case T_DATA_MORE:
486  case T_REQUEST_TC: break;
487  case T_DELETE_TC: if (SendTPDU(T_DTC_REPLY) != OK)
488  return ERROR;
489  Init(m_fd, m_slot, m_tcid);
490  break;
491  default: return ERROR;
492  }
495  break;
496  case stDELETION: if (m_tpdu->Tag() == T_DTC_REPLY) {
497  Init(m_fd, m_slot, m_tcid);
498  //XXX Status()???
500  }
501  break;
502  }
503  }
504  else {
505  esyslog("ERROR: CAM: Read failed: slot %d, tcid %d\n", m_slot, m_tcid);
506  if (m_tpdu->Tcid() == m_tcid)
507  Init(-1, m_slot, m_tcid);
508  }
509  return m_lastResponse;
510 }
511 
512 int cCiTransportConnection::SendData(int Length, const uint8_t *Data)
513 {
514  while (m_state == stACTIVE && Length > 0) {
515  uint8_t Tag = T_DATA_LAST;
516  int l = Length;
517  if (l > MAX_TPDU_DATA) {
518  Tag = T_DATA_MORE;
519  l = MAX_TPDU_DATA;
520  }
521  if (SendTPDU(Tag, l, Data) != OK || RecvTPDU() != T_SB)
522  break;
523  Length -= l;
524  Data += l;
525  }
526  return Length ? ERROR : OK;
527 }
528 
530 {
531  if (SendTPDU(T_RCV) == OK)
532  return RecvTPDU();
533  return ERROR;
534 }
535 
536 const uint8_t *cCiTransportConnection::Data(int &Length)
537 {
538  return m_tpdu->Data(Length);
539 }
540 
541 #define MAX_CONNECT_RETRIES 25
542 
544 {
545  if (m_state == stIDLE) {
546  if (SendTPDU(T_CREATE_TC) == OK) {
548  if (RecvTPDU() == T_CTC_REPLY) {
549  sConnected=true;
550  return OK;
551  // the following is a workaround for CAMs that don't quite follow the specs...
552  }
553 
554  for (int i = 0; i < MAX_CONNECT_RETRIES; i++) {
555  dsyslog("CAM: retrying to establish connection");
556  if (RecvTPDU() == T_CTC_REPLY) {
557  dsyslog("CAM: connection established");
558  sConnected=true;
559  return OK;
560  }
561  }
562  return ERROR;
563  }
564  }
565  return ERROR;
566 }
567 
568 // Polls can be done with a 100ms interval (EN50221 - A.4.1.12)
569 static constexpr std::chrono::milliseconds POLL_INTERVAL { 100ms };
570 
572 {
573  if (m_state != stACTIVE)
574  return ERROR;
575 
576  auto curr_time = nowAsDuration<std::chrono::milliseconds>();
577  std::chrono::milliseconds msdiff = curr_time - m_lastPoll;
578 
579  if (msdiff < POLL_INTERVAL)
580  return OK;
581 
582  m_lastPoll = curr_time;
583 
584  if (SendTPDU(T_DATA_LAST) != OK)
585  return ERROR;
586 
587  return RecvTPDU();
588 }
589 
590 // --- cCiTransportLayer -----------------------------------------------------
591 
592 #define MAX_CI_CONNECT 16 // maximum possible value is 254
593 
595 private:
596  int m_fd;
598  std::array<cCiTransportConnection,MAX_CI_CONNECT> m_tc;
599 public:
600  cCiTransportLayer(int Fd, int NumSlots);
602  bool ResetSlot(int Slot) const;
603  bool ModuleReady(int Slot) const;
604  cCiTransportConnection *Process(int Slot);
605  };
606 
608 {
609  m_fd = Fd;
610  m_numSlots = NumSlots;
611  for (int s = 0; s < m_numSlots; s++)
612  ResetSlot(s);
613 }
614 
616 {
617  for (int i = 0; i < MAX_CI_CONNECT; i++) {
618  if (m_tc[i].State() == stIDLE) {
619  dbgprotocol("Creating connection: slot %d, tcid %d\n", Slot, i + 1);
620  m_tc[i].Init(m_fd, Slot, i + 1);
621  if (m_tc[i].CreateConnection() == OK)
622  return &m_tc[i];
623  break;
624  }
625  }
626  return nullptr;
627 }
628 
629 bool cCiTransportLayer::ResetSlot(int Slot) const
630 {
631  dbgprotocol("Resetting slot %d...", Slot);
632  if (ioctl(m_fd, CA_RESET, 1 << Slot) != -1) {
633  dbgprotocol("ok.\n");
634  return true;
635  }
636  esyslog("ERROR: can't reset CAM slot %d: %m", Slot);
637  dbgprotocol("failed!\n");
638  return false;
639 }
640 
641 bool cCiTransportLayer::ModuleReady(int Slot) const
642 {
643  ca_slot_info_t sinfo;
644  sinfo.num = Slot;
645  if (ioctl(m_fd, CA_GET_SLOT_INFO, &sinfo) != -1)
646  return (sinfo.flags & CA_CI_MODULE_READY) != 0U;
647  esyslog("ERROR: can't get info on CAM slot %d: %m", Slot);
648  return false;
649 }
650 
652 {
653  for (auto & conn : m_tc) {
654  cCiTransportConnection *Tc = &conn;
655  if (Tc->Slot() == Slot) {
656  switch (Tc->State()) {
657  case stCREATION:
658  case stACTIVE:
659  if (!Tc->DataAvailable()) {
660  Tc->Poll();
661  }
662  switch (Tc->LastResponse()) {
663  case T_REQUEST_TC:
664  //XXX
665  break;
666  case T_DATA_MORE:
667  case T_DATA_LAST:
668  case T_CTC_REPLY:
669  case T_SB:
670  if (Tc->DataAvailable())
671  Tc->RecvData();
672  break;
673  case TIMEOUT:
674  case ERROR:
675  default:
676  //XXX Tc->state = stIDLE;//XXX Init()???
677  return nullptr;
678  break;
679  }
680  //XXX this will only work with _one_ transport connection per slot!
681  return Tc;
682  break;
683  default: ;
684  }
685  }
686  }
687  return nullptr;
688 }
689 
690 // -- cCiSession -------------------------------------------------------------
691 
692 // Session Tags:
693 
694 #define ST_SESSION_NUMBER 0x90
695 #define ST_OPEN_SESSION_REQUEST 0x91
696 #define ST_OPEN_SESSION_RESPONSE 0x92
697 #define ST_CREATE_SESSION 0x93
698 #define ST_CREATE_SESSION_RESPONSE 0x94
699 #define ST_CLOSE_SESSION_REQUEST 0x95
700 #define ST_CLOSE_SESSION_RESPONSE 0x96
701 
702 // Session Status:
703 
704 #define SS_OK 0x00
705 #define SS_NOT_ALLOCATED 0xF0
706 
707 // Resource Identifiers:
708 
709 #define RI_RESOURCE_MANAGER 0x00010041
710 #define RI_APPLICATION_INFORMATION 0x00020041
711 #define RI_CONDITIONAL_ACCESS_SUPPORT 0x00030041
712 #define RI_HOST_CONTROL 0x00200041
713 #define RI_DATE_TIME 0x00240041
714 #define RI_MMI 0x00400041
715 
716 // Application Object Tags:
717 
718 #define AOT_NONE 0x000000
719 #define AOT_PROFILE_ENQ 0x9F8010
720 #define AOT_PROFILE 0x9F8011
721 #define AOT_PROFILE_CHANGE 0x9F8012
722 #define AOT_APPLICATION_INFO_ENQ 0x9F8020
723 #define AOT_APPLICATION_INFO 0x9F8021
724 #define AOT_ENTER_MENU 0x9F8022
725 #define AOT_CA_INFO_ENQ 0x9F8030
726 #define AOT_CA_INFO 0x9F8031
727 #define AOT_CA_PMT 0x9F8032
728 #define AOT_CA_PMT_REPLY 0x9F8033
729 #define AOT_TUNE 0x9F8400
730 #define AOT_REPLACE 0x9F8401
731 #define AOT_CLEAR_REPLACE 0x9F8402
732 #define AOT_ASK_RELEASE 0x9F8403
733 #define AOT_DATE_TIME_ENQ 0x9F8440
734 #define AOT_DATE_TIME 0x9F8441
735 #define AOT_CLOSE_MMI 0x9F8800
736 #define AOT_DISPLAY_CONTROL 0x9F8801
737 #define AOT_DISPLAY_REPLY 0x9F8802
738 #define AOT_TEXT_LAST 0x9F8803
739 #define AOT_TEXT_MORE 0x9F8804
740 #define AOT_KEYPAD_CONTROL 0x9F8805
741 #define AOT_KEYPRESS 0x9F8806
742 #define AOT_ENQ 0x9F8807
743 #define AOT_ANSW 0x9F8808
744 #define AOT_MENU_LAST 0x9F8809
745 #define AOT_MENU_MORE 0x9F880A
746 #define AOT_MENU_ANSW 0x9F880B
747 #define AOT_LIST_LAST 0x9F880C
748 #define AOT_LIST_MORE 0x9F880D
749 #define AOT_SUBTITLE_SEGMENT_LAST 0x9F880E
750 #define AOT_SUBTITLE_SEGMENT_MORE 0x9F880F
751 #define AOT_DISPLAY_MESSAGE 0x9F8810
752 #define AOT_SCENE_END_MARK 0x9F8811
753 #define AOT_SCENE_DONE 0x9F8812
754 #define AOT_SCENE_CONTROL 0x9F8813
755 #define AOT_SUBTITLE_DOWNLOAD_LAST 0x9F8814
756 #define AOT_SUBTITLE_DOWNLOAD_MORE 0x9F8815
757 #define AOT_FLUSH_DOWNLOAD 0x9F8816
758 #define AOT_DOWNLOAD_REPLY 0x9F8817
759 #define AOT_COMMS_CMD 0x9F8C00
760 #define AOT_CONNECTION_DESCRIPTOR 0x9F8C01
761 #define AOT_COMMS_REPLY 0x9F8C02
762 #define AOT_COMMS_SEND_LAST 0x9F8C03
763 #define AOT_COMMS_SEND_MORE 0x9F8C04
764 #define AOT_COMMS_RCV_LAST 0x9F8C05
765 #define AOT_COMMS_RCV_MORE 0x9F8C06
766 
767 class cCiSession {
768 private:
772 protected:
773  static int GetTag(int &Length, const uint8_t **Data);
774  static const uint8_t *GetData(const uint8_t *Data, int &Length);
775  int SendData(int Tag, int Length = 0, const uint8_t *Data = nullptr);
776  int SendData(int Tag, std::vector<uint8_t> &Data)
777  { return SendData(Tag, Data.size(), Data.data()); };
778 public:
780  virtual ~cCiSession() = default;
781  const cCiTransportConnection *Tc(void) { return m_tc; }
782  int SessionId(void) const { return m_sessionId; }
783  int ResourceId(void) const { return m_resourceId; }
784  virtual bool HasUserIO(void) { return false; }
785  virtual bool Process(int Length = 0, const uint8_t *Data = nullptr);
786  };
787 
788 cCiSession::cCiSession(int SessionId, int ResourceId, cCiTransportConnection *Tc)
789 {
792  m_tc = Tc;
793 }
794 
795 int cCiSession::GetTag(int &Length, const uint8_t **Data)
803 {
804  if (Length >= 3 && Data && *Data) {
805  int t = 0;
806  for (int i = 0; i < 3; i++)
807  t = (t << 8) | *(*Data)++;
808  Length -= 3;
809  return t;
810  }
811  return AOT_NONE;
812 }
813 
814 const uint8_t *cCiSession::GetData(const uint8_t *Data, int &Length)
815 {
816  Data = GetLength(Data, Length);
817  return Length ? Data : nullptr;
818 }
819 
820 int cCiSession::SendData(int Tag, int Length, const uint8_t *Data)
821 {
822  if (Length < 0)
823  {
824  esyslog("ERROR: CAM: data length (%d) is negative", Length);
825  return ERROR;
826  }
827 
828  if ((Length > 0) && !Data)
829  {
830  esyslog("ERROR: CAM: Data pointer null");
831  return ERROR;
832  }
833 
834  std::vector<uint8_t> buffer {
835  ST_SESSION_NUMBER, 0x02,
836  static_cast<uint8_t>((m_sessionId >> 8) & 0xFF),
837  static_cast<uint8_t>((m_sessionId ) & 0xFF),
838  static_cast<uint8_t>((Tag >> 16) & 0xFF),
839  static_cast<uint8_t>((Tag >> 8) & 0xFF),
840  static_cast<uint8_t>((Tag ) & 0xFF)} ;
841  buffer.reserve(2048);
842 
843  SetLength(buffer, Length);
844  if (buffer.size() + Length >= buffer.capacity())
845  {
846  esyslog("ERROR: CAM: data length (%d) exceeds buffer size", Length);
847  return ERROR;
848  }
849 
850  if (Length != 0)
851  {
852  buffer.insert(buffer.end(), Data, Data + Length);
853  }
854  return m_tc->SendData(buffer);
855 }
856 
857 bool cCiSession::Process(int Length, const uint8_t *Data)
858 {
859  (void)Length;
860  (void)Data;
861  return true;
862 }
863 
864 // -- cCiResourceManager -----------------------------------------------------
865 
867 private:
868  int m_state;
869 public:
871  bool Process(int Length = 0, const uint8_t *Data = nullptr) override; // cCiSession
872  };
873 
875 :cCiSession(SessionId, RI_RESOURCE_MANAGER, Tc)
876 {
877  dbgprotocol("New Resource Manager (session id %d)\n", SessionId);
878  m_state = 0;
879 }
880 
881 bool cCiResourceManager::Process(int Length, const uint8_t *Data)
882 {
883  if (Data) {
884  int Tag = GetTag(Length, &Data);
885  switch (Tag) {
886  case AOT_PROFILE_ENQ: {
887  dbgprotocol("%d: <== Profile Enquiry\n", SessionId());
888  const std::array<const uint32_t,5> resources
889  {
890  htonl(RI_RESOURCE_MANAGER),
893  htonl(RI_DATE_TIME),
894  htonl(RI_MMI)
895  };
896  dbgprotocol("%d: ==> Profile\n", SessionId());
897  SendData(AOT_PROFILE, resources.size() * sizeof(uint32_t),
898  reinterpret_cast<const uint8_t*>(resources.data()));
899  m_state = 3;
900  }
901  break;
902  case AOT_PROFILE: {
903  dbgprotocol("%d: <== Profile\n", SessionId());
904  if (m_state == 1) {
905  int l = 0;
906  const uint8_t *d = GetData(Data, l);
907  if (l > 0 && d)
908  esyslog("CI resource manager: unexpected data");
909  dbgprotocol("%d: ==> Profile Change\n", SessionId());
911  m_state = 2;
912  }
913  else {
914  esyslog("ERROR: CI resource manager: unexpected tag %06X in state %d", Tag, m_state);
915  }
916  }
917  break;
918  default: esyslog("ERROR: CI resource manager: unknown tag %06X", Tag);
919  return false;
920  }
921  }
922  else if (m_state == 0) {
923  dbgprotocol("%d: ==> Profile Enq\n", SessionId());
925  m_state = 1;
926  }
927  return true;
928 }
929 
930 // --- cCiApplicationInformation ---------------------------------------------
931 
933 private:
934  int m_state;
940 public:
942  ~cCiApplicationInformation() override;
943  bool Process(int Length = 0, const uint8_t *Data = nullptr) override; // cCiSession
944  bool EnterMenu(void);
945  char *GetApplicationString() { return strdup(m_menuString); };
948  };
949 
951 :cCiSession(SessionId, RI_APPLICATION_INFORMATION, Tc)
952 {
953  dbgprotocol("New Application Information (session id %d)\n", SessionId);
954  m_state = 0;
955  m_creationTime = time(nullptr);
956  m_applicationType = 0;
958  m_manufacturerCode = 0;
959  m_menuString = nullptr;
960 }
961 
963 {
964  free(m_menuString);
965 }
966 
967 bool cCiApplicationInformation::Process(int Length, const uint8_t *Data)
968 {
969  if (Data) {
970  int Tag = GetTag(Length, &Data);
971  switch (Tag) {
972  case AOT_APPLICATION_INFO: {
973  dbgprotocol("%d: <== Application Info\n", SessionId());
974  int l = 0;
975  const uint8_t *d = GetData(Data, l);
976  if ((l -= 1) < 0) break;
977  m_applicationType = *d++;
978  if ((l -= 2) < 0) break;
979  m_applicationManufacturer = ntohs(*(uint16_t *)d);
980  d += 2;
981  if ((l -= 2) < 0) break;
982  m_manufacturerCode = ntohs(*(uint16_t *)d);
983  d += 2;
984  free(m_menuString);
985  m_menuString = GetString(l, &d);
986  isyslog("CAM: %s, %02X, %04X, %04X", m_menuString, m_applicationType,
988  }
989  m_state = 2;
990  break;
991  default: esyslog("ERROR: CI application information: unknown tag %06X", Tag);
992  return false;
993  }
994  }
995  else if (m_state == 0) {
996  dbgprotocol("%d: ==> Application Info Enq\n", SessionId());
998  m_state = 1;
999  }
1000  return true;
1001 }
1002 
1004 {
1005  if (m_state == 2 && time(nullptr) - m_creationTime > WRKRND_TIME_BEFORE_ENTER_MENU) {
1006  dbgprotocol("%d: ==> Enter Menu\n", SessionId());
1008  return true;//XXX
1009  }
1010  return false;
1011 }
1012 
1013 // --- cCiConditionalAccessSupport -------------------------------------------
1014 
1016 private:
1017  int m_state {0};
1019  bool m_needCaPmt {false};
1020 public:
1022  bool Process(int Length = 0, const uint8_t *Data = nullptr) override; // cCiSession
1024  bool SendPMT(const cCiCaPmt &CaPmt);
1025  bool NeedCaPmt(void) const { return m_needCaPmt; }
1026  };
1027 
1029  int SessionId, cCiTransportConnection *Tc) :
1031 {
1032  dbgprotocol("New Conditional Access Support (session id %d)\n", SessionId);
1033 }
1034 
1035 bool cCiConditionalAccessSupport::Process(int Length, const uint8_t *Data)
1036 {
1037  if (Data) {
1038  int Tag = GetTag(Length, &Data);
1039  switch (Tag) {
1040  case AOT_CA_INFO: {
1041  dbgprotocol("%d: <== Ca Info", SessionId());
1042  int l = 0;
1043  const uint8_t *d = GetData(Data, l);
1044  while (l > 1) {
1045  unsigned short id = ((unsigned short)(*d) << 8) | *(d + 1);
1046  dbgprotocol(" %04X", id);
1047  d += 2;
1048  l -= 2;
1049 
1050  // Make sure the id is not already present
1051  if (std::find(m_caSystemIds.cbegin(), m_caSystemIds.cend(), id)
1052  != m_caSystemIds.end())
1053  continue;
1054 
1055  // Insert before the last element.
1056  m_caSystemIds.emplace_back(id);
1057  }
1058 
1059  dbgprotocol("\n");
1060  }
1061  m_state = 2;
1062  m_needCaPmt = true;
1063  break;
1064  default: esyslog("ERROR: CI conditional access support: unknown tag %06X", Tag);
1065  return false;
1066  }
1067  }
1068  else if (m_state == 0) {
1069  dbgprotocol("%d: ==> Ca Info Enq\n", SessionId());
1071  m_state = 1;
1072  }
1073  return true;
1074 }
1075 
1077 {
1078  if (m_state == 2) {
1079  SendData(AOT_CA_PMT, CaPmt.m_length, CaPmt.m_capmt);
1080  m_needCaPmt = false;
1081  return true;
1082  }
1083  return false;
1084 }
1085 
1086 // --- cCiDateTime -----------------------------------------------------------
1087 
1088 class cCiDateTime : public cCiSession {
1089 private:
1091  time_t m_lastTime;
1093  bool SendDateTime(void);
1094 public:
1096  bool Process(int Length = 0, const uint8_t *Data = nullptr) override; // cCiSession
1097  void SetTimeOffset(double offset);
1098  };
1099 
1101 :cCiSession(SessionId, RI_DATE_TIME, Tc)
1102 {
1103  m_interval = 0;
1104  m_lastTime = 0;
1105  m_timeOffset = 0;
1106  dbgprotocol("New Date Time (session id %d)\n", SessionId);
1107 }
1108 
1109 void cCiDateTime::SetTimeOffset(double offset)
1110 {
1111  m_timeOffset = (int) offset;
1112  dbgprotocol("New Time Offset: %i secs\n", m_timeOffset);
1113 }
1114 
1116 {
1117  time_t t = time(nullptr);
1118  struct tm tm_gmt {};
1119  struct tm tm_loc {};
1120 
1121  // Avoid using signed time_t types
1122  if (m_timeOffset < 0)
1123  t -= (time_t)(-m_timeOffset);
1124  else
1125  t += (time_t)(m_timeOffset);
1126 
1127  if (gmtime_r(&t, &tm_gmt) && localtime_r(&t, &tm_loc)) {
1128  int Y = tm_gmt.tm_year;
1129  int M = tm_gmt.tm_mon + 1;
1130  int D = tm_gmt.tm_mday;
1131  int L = (M == 1 || M == 2) ? 1 : 0;
1132  int MJD = 14956 + D + int((Y - L) * 365.25) + int((M + 1 + L * 12) * 30.6001);
1133 #define DEC2BCD(d) (uint8_t((((d) / 10) << 4) + ((d) % 10)))
1134 #define BYTE0(a) static_cast<uint8_t>((a) & 0xFF)
1135 #define BYTE1(a) static_cast<uint8_t>(((a) >> 8) & 0xFF)
1136  uint16_t mjd = htons(MJD);
1137  int16_t local_offset = htons(tm_loc.tm_gmtoff / 60);
1138  std::vector<uint8_t> T {
1139  BYTE0(mjd),
1140  BYTE1(mjd),
1141  DEC2BCD(tm_gmt.tm_hour),
1142  DEC2BCD(tm_gmt.tm_min),
1143  DEC2BCD(tm_gmt.tm_sec),
1144  BYTE0(local_offset),
1145  BYTE1(local_offset)
1146  };
1147 
1148  dbgprotocol("%d: ==> Date Time\n", SessionId());
1149  SendData(AOT_DATE_TIME, T);
1150  //XXX return value of all SendData() calls???
1151  return true;
1152  }
1153  return false;
1154 }
1155 
1156 bool cCiDateTime::Process(int Length, const uint8_t *Data)
1157 {
1158  if (Data) {
1159  int Tag = GetTag(Length, &Data);
1160  switch (Tag) {
1161  case AOT_DATE_TIME_ENQ: {
1162  m_interval = 0;
1163  int l = 0;
1164  const uint8_t *d = GetData(Data, l);
1165  if (l > 0)
1166  m_interval = *d;
1167  dbgprotocol("%d: <== Date Time Enq, interval = %d\n", SessionId(), m_interval);
1168  m_lastTime = time(nullptr);
1169  return SendDateTime();
1170  }
1171  break;
1172  default: esyslog("ERROR: CI date time: unknown tag %06X", Tag);
1173  return false;
1174  }
1175  }
1176  else if (m_interval && time(nullptr) - m_lastTime > m_interval) {
1177  m_lastTime = time(nullptr);
1178  return SendDateTime();
1179  }
1180  return true;
1181 }
1182 
1183 // --- cCiMMI ----------------------------------------------------------------
1184 
1185 // Close MMI Commands:
1186 
1187 #define CLOSE_MMI_IMMEDIATE 0x00
1188 #define CLOSE_MMI_DELAY 0x01
1189 
1190 // Display Control Commands:
1191 
1192 #define DCC_SET_MMI_MODE 0x01
1193 #define DCC_DISPLAY_CHARACTER_TABLE_LIST 0x02
1194 #define DCC_INPUT_CHARACTER_TABLE_LIST 0x03
1195 #define DCC_OVERLAY_GRAPHICS_CHARACTERISTICS 0x04
1196 #define DCC_FULL_SCREEN_GRAPHICS_CHARACTERISTICS 0x05
1197 
1198 // MMI Modes:
1199 
1200 #define MM_HIGH_LEVEL 0x01
1201 #define MM_LOW_LEVEL_OVERLAY_GRAPHICS 0x02
1202 #define MM_LOW_LEVEL_FULL_SCREEN_GRAPHICS 0x03
1203 
1204 // Display Reply IDs:
1205 
1206 #define DRI_MMI_MODE_ACK 0x01
1207 #define DRI_LIST_DISPLAY_CHARACTER_TABLES 0x02
1208 #define DRI_LIST_INPUT_CHARACTER_TABLES 0x03
1209 #define DRI_LIST_GRAPHIC_OVERLAY_CHARACTERISTICS 0x04
1210 #define DRI_LIST_FULL_SCREEN_GRAPHIC_CHARACTERISTICS 0x05
1211 #define DRI_UNKNOWN_DISPLAY_CONTROL_CMD 0xF0
1212 #define DRI_UNKNOWN_MMI_MODE 0xF1
1213 #define DRI_UNKNOWN_CHARACTER_TABLE 0xF2
1214 
1215 // Enquiry Flags:
1216 
1217 #define EF_BLIND 0x01
1218 
1219 // Answer IDs:
1220 
1221 #define AI_CANCEL 0x00
1222 #define AI_ANSWER 0x01
1223 
1224 class cCiMMI : public cCiSession {
1225 private:
1226  char *GetText(int &Length, const uint8_t **Data);
1229 public:
1231  ~cCiMMI() override;
1232  bool Process(int Length = 0, const uint8_t *Data = nullptr) override; // cCiSession
1233  bool HasUserIO(void) override { return m_menu || m_enquiry; } // cCiSession
1234  cCiMenu *Menu(void);
1235  cCiEnquiry *Enquiry(void);
1236  bool SendMenuAnswer(uint8_t Selection);
1237  bool SendAnswer(const char *Text);
1238  };
1239 
1241 :cCiSession(SessionId, RI_MMI, Tc)
1242 {
1243  dbgprotocol("New MMI (session id %d)\n", SessionId);
1244  m_menu = nullptr;
1245  m_enquiry = nullptr;
1246 }
1247 
1249 {
1250  delete m_menu;
1251  delete m_enquiry;
1252 }
1253 
1254 char *cCiMMI::GetText(int &Length, const uint8_t **Data)
1262 {
1263  int Tag = GetTag(Length, Data);
1264  if (Tag == AOT_TEXT_LAST) {
1265  char *s = GetString(Length, Data);
1266  dbgprotocol("%d: <== Text Last '%s'\n", SessionId(), s);
1267  return s;
1268  }
1269  esyslog("CI MMI: unexpected text tag: %06X", Tag);
1270  return nullptr;
1271 }
1272 
1273 bool cCiMMI::Process(int Length, const uint8_t *Data)
1274 {
1275  if (Data) {
1276  int Tag = GetTag(Length, &Data);
1277  switch (Tag) {
1278  case AOT_DISPLAY_CONTROL: {
1279  dbgprotocol("%d: <== Display Control\n", SessionId());
1280  int l = 0;
1281  const uint8_t *d = GetData(Data, l);
1282  if (l > 0) {
1283  switch (*d) {
1284  case DCC_SET_MMI_MODE:
1285  if (l == 2 && *++d == MM_HIGH_LEVEL) {
1286  struct tDisplayReply { uint8_t m_id; uint8_t m_mode; };
1287  tDisplayReply dr {};
1288  dr.m_id = DRI_MMI_MODE_ACK;
1289  dr.m_mode = MM_HIGH_LEVEL;
1290  dbgprotocol("%d: ==> Display Reply\n", SessionId());
1291  SendData(AOT_DISPLAY_REPLY, 2, (uint8_t *)&dr);
1292  }
1293  break;
1294  default: esyslog("CI MMI: unsupported display control command %02X", *d);
1295  return false;
1296  }
1297  }
1298  }
1299  break;
1300  case AOT_LIST_LAST:
1301  case AOT_MENU_LAST: {
1302  dbgprotocol("%d: <== Menu Last\n", SessionId());
1303  delete m_menu;
1304  m_menu = new cCiMenu(this, Tag == AOT_MENU_LAST);
1305  int l = 0;
1306  const uint8_t *d = GetData(Data, l);
1307  if (l > 0) {
1308  // since the specification allows choiceNb to be undefined it is useless, so let's just skip it:
1309  d++;
1310  l--;
1311  if (l > 0) m_menu->m_titleText = GetText(l, &d);
1312  if (l > 0) m_menu->m_subTitleText = GetText(l, &d);
1313  if (l > 0) m_menu->m_bottomText = GetText(l, &d);
1314  while (l > 0) {
1315  char *s = GetText(l, &d);
1316  if (s) {
1317  if (!m_menu->AddEntry(s))
1318  free(s);
1319  }
1320  else
1321  break;
1322  }
1323  }
1324  }
1325  break;
1326  case AOT_ENQ: {
1327  dbgprotocol("%d: <== Enq\n", SessionId());
1328  delete m_enquiry;
1329  m_enquiry = new cCiEnquiry(this);
1330  int l = 0;
1331  const uint8_t *d = GetData(Data, l);
1332  if (l > 0) {
1333  uint8_t blind = *d++;
1334  //XXX GetByte()???
1335  l--;
1336  m_enquiry->m_blind = ((blind & EF_BLIND) != 0);
1338  l--;
1339  // I really wonder why there is no text length field here...
1340  m_enquiry->m_text = CopyString(l, d);
1341  }
1342  }
1343  break;
1344  case AOT_CLOSE_MMI: {
1345  int l = 0;
1346  const uint8_t *d = GetData(Data, l);
1347 
1348  if(l > 0){
1349  switch(*d){
1350  case CLOSE_MMI_IMMEDIATE:
1351  dbgprotocol("%d <== Menu Close: immediate\n", SessionId());
1352  break;
1353  case CLOSE_MMI_DELAY:
1354  dbgprotocol("%d <== Menu Close: delay\n", SessionId());
1355  break;
1356  default: esyslog("ERROR: CI MMI: unknown close_mmi_cmd_id %02X", *d);
1357  return false;
1358  }
1359  }
1360 
1361  break;
1362  }
1363  default: esyslog("ERROR: CI MMI: unknown tag %06X", Tag);
1364  return false;
1365  }
1366  }
1367  return true;
1368 }
1369 
1371 {
1372  cCiMenu *m = m_menu;
1373  m_menu = nullptr;
1374  return m;
1375 }
1376 
1378 {
1379  cCiEnquiry *e = m_enquiry;
1380  m_enquiry = nullptr;
1381  return e;
1382 }
1383 
1384 bool cCiMMI::SendMenuAnswer(uint8_t Selection)
1385 {
1386  dbgprotocol("%d: ==> Menu Answ\n", SessionId());
1387  SendData(AOT_MENU_ANSW, 1, &Selection);
1388  //XXX return value of all SendData() calls???
1389  return true;
1390 }
1391 
1392 // Define protocol structure
1393 extern "C" {
1394  struct tAnswer { uint8_t m_id; char m_text[256]; };
1395 }
1396 
1397 bool cCiMMI::SendAnswer(const char *Text)
1398 {
1399  dbgprotocol("%d: ==> Answ\n", SessionId());
1400  tAnswer answer {};
1401  answer.m_id = Text ? AI_ANSWER : AI_CANCEL;
1402  if (Text) {
1403  strncpy(answer.m_text, Text, sizeof(answer.m_text) - 1);
1404  answer.m_text[255] = '\0';
1405  }
1406  SendData(AOT_ANSW, Text ? strlen(Text) + 1 : 1, (uint8_t *)&answer);
1407  //XXX return value of all SendData() calls???
1408  return true;
1409 }
1410 
1411 // --- cCiMenu ---------------------------------------------------------------
1412 
1413 cCiMenu::cCiMenu(cCiMMI *MMI, bool Selectable)
1414 {
1415  m_mmi = MMI;
1417 }
1418 
1420 {
1421  free(m_titleText);
1422  free(m_subTitleText);
1423  free(m_bottomText);
1424  for (int i = 0; i < m_numEntries; i++)
1425  free(m_entries[i]);
1426 }
1427 
1428 bool cCiMenu::AddEntry(char *s)
1429 {
1431  m_entries[m_numEntries++] = s;
1432  return true;
1433  }
1434  return false;
1435 }
1436 
1437 bool cCiMenu::Select(int Index)
1438 {
1439  if (m_mmi && -1 <= Index && Index < m_numEntries)
1440  return m_mmi->SendMenuAnswer(Index + 1);
1441  return false;
1442 }
1443 
1445 {
1446  return Select(-1);
1447 }
1448 
1449 // --- cCiEnquiry ------------------------------------------------------------
1450 
1452 {
1453  free(m_text);
1454 }
1455 
1456 bool cCiEnquiry::Reply(const char *s)
1457 {
1458  return m_mmi ? m_mmi->SendAnswer(s) : false;
1459 }
1460 
1462 {
1463  return Reply(nullptr);
1464 }
1465 
1466 // --- cCiCaPmt --------------------------------------------------------------
1467 
1468 // Ca Pmt Cmd Ids:
1469 
1470 #define CPCI_OK_DESCRAMBLING 0x01
1471 #define CPCI_OK_MMI 0x02
1472 #define CPCI_QUERY 0x03
1473 #define CPCI_NOT_SELECTED 0x04
1474 
1475 cCiCaPmt::cCiCaPmt(int ProgramNumber, uint8_t cplm)
1476 {
1477  m_capmt[m_length++] = cplm; // ca_pmt_list_management
1478  m_capmt[m_length++] = (ProgramNumber >> 8) & 0xFF;
1479  m_capmt[m_length++] = ProgramNumber & 0xFF;
1480  m_capmt[m_length++] = 0x01; // version_number, current_next_indicator - apparently vn doesn't matter, but cni must be 1
1481 
1482  // program_info_length
1484  m_capmt[m_length++] = 0x00;
1485  m_capmt[m_length++] = 0x00;
1486 }
1487 
1489 {
1490  if (m_length + 5 > int(sizeof(m_capmt)))
1491  {
1492  esyslog("ERROR: buffer overflow in CA_PMT");
1493  return;
1494  }
1495 
1496  m_capmt[m_length++] = type & 0xFF;
1497  m_capmt[m_length++] = (pid >> 8) & 0xFF;
1498  m_capmt[m_length++] = pid & 0xFF;
1499 
1500  // ES_info_length
1502  m_capmt[m_length++] = 0x00;
1503  m_capmt[m_length++] = 0x00;
1504 }
1505 
1523 void cCiCaPmt::AddCaDescriptor(int ca_system_id, int ca_pid, int data_len,
1524  const uint8_t *data)
1525 {
1526  if (!m_infoLengthPos)
1527  {
1528  esyslog("ERROR: adding CA descriptor without program/stream!");
1529  return;
1530  }
1531 
1532  if (m_length + data_len + 7 > int(sizeof(m_capmt)))
1533  {
1534  esyslog("ERROR: buffer overflow in CA_PMT");
1535  return;
1536  }
1537 
1538  // We are either at start of program descriptors or stream descriptors.
1539  if (m_infoLengthPos + 2 == m_length)
1540  m_capmt[m_length++] = CPCI_OK_DESCRAMBLING; // ca_pmt_cmd_id
1541 
1542  m_capmt[m_length++] = 0x09; // CA descriptor tag
1543  m_capmt[m_length++] = 4 + data_len; // descriptor length
1544 
1545  m_capmt[m_length++] = (ca_system_id >> 8) & 0xFF;
1546  m_capmt[m_length++] = ca_system_id & 0xFF;
1547  m_capmt[m_length++] = (ca_pid >> 8) & 0xFF;
1548  m_capmt[m_length++] = ca_pid & 0xFF;
1549 
1550  if (data_len > 0)
1551  {
1552  memcpy(&m_capmt[m_length], data, data_len);
1553  m_length += data_len;
1554  }
1555 
1556  // update program_info_length/ES_info_length
1557  int l = m_length - m_infoLengthPos - 2;
1558  m_capmt[m_infoLengthPos] = (l >> 8) & 0xFF;
1559  m_capmt[m_infoLengthPos + 1] = l & 0xFF;
1560 }
1561 
1562 // -- cLlCiHandler -------------------------------------------------------------
1563 
1564 cLlCiHandler::cLlCiHandler(int Fd, int NumSlots)
1565 {
1566  m_numSlots = NumSlots;
1567  m_tpl = new cCiTransportLayer(Fd, m_numSlots);
1568  m_fdCa = Fd;
1569 }
1570 
1572 {
1573  cMutexLock MutexLock(&m_mutex);
1574  for (auto & session : m_sessions)
1575  delete session;
1576  delete m_tpl;
1577  close(m_fdCa);
1578 }
1579 
1581 {
1582  int fd_ca = open(FileName, O_RDWR);
1583  if (fd_ca >= 0)
1584  {
1585  ca_caps_t Caps;
1586  if (ioctl(fd_ca, CA_GET_CAP, &Caps) == 0)
1587  {
1588  int NumSlots = Caps.slot_num;
1589  if (NumSlots > 0)
1590  {
1591  if (Caps.slot_type & CA_CI_LINK)
1592  return new cLlCiHandler(fd_ca, NumSlots);
1593  if (Caps.slot_type & CA_CI)
1594  return new cHlCiHandler(fd_ca, NumSlots);
1595  isyslog("CAM doesn't support either high or low level CI,"
1596  " Caps.slot_type=%i", Caps.slot_type);
1597  }
1598  else
1599  esyslog("ERROR: no CAM slots found");
1600  }
1601  else
1602  LOG_ERROR_STR(FileName);
1603  close(fd_ca);
1604  }
1605  return nullptr;
1606 }
1607 
1608 int cLlCiHandler::ResourceIdToInt(const uint8_t *Data)
1609 {
1610  return (ntohl(*(int *)Data));
1611 }
1612 
1613 bool cLlCiHandler::Send(uint8_t Tag, int SessionId, int ResourceId, int Status)
1614 {
1615  std::vector<uint8_t> buffer {Tag, 0x00} ; // 0x00 will be replaced with length
1616  if (Status >= 0)
1617  buffer.push_back(Status);
1618  if (ResourceId) {
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);
1623  }
1624  buffer.push_back((SessionId >> 8) & 0xFF);
1625  buffer.push_back( SessionId & 0xFF);
1626  buffer[1] = buffer.size() - 2; // length
1627  return m_tc && m_tc->SendData(buffer) == OK;
1628 }
1629 
1631 {
1632  for (auto & session : m_sessions) {
1633  if (session && session->SessionId() == SessionId)
1634  return session;
1635  }
1636  return nullptr;
1637 }
1638 
1640 {
1641  for (auto & session : m_sessions) {
1642  if (session && session->Tc()->Slot() == Slot && session->ResourceId() == ResourceId)
1643  return session;
1644  }
1645  return nullptr;
1646 }
1647 
1649 {
1650  if (!GetSessionByResourceId(ResourceId, m_tc->Slot())) {
1651  for (int i = 0; i < MAX_CI_SESSION; i++) {
1652  if (!m_sessions[i]) {
1653  switch (ResourceId) {
1654  case RI_RESOURCE_MANAGER: return m_sessions[i] = new cCiResourceManager(i + 1, m_tc);
1657  return m_sessions[i] = new cCiConditionalAccessSupport(i + 1, m_tc);
1658  case RI_HOST_CONTROL: break; //XXX
1659  case RI_DATE_TIME: return m_sessions[i] = new cCiDateTime(i + 1, m_tc);
1660  case RI_MMI: return m_sessions[i] = new cCiMMI(i + 1, m_tc);
1661  }
1662  }
1663  }
1664  }
1665  return nullptr;
1666 }
1667 
1668 bool cLlCiHandler::OpenSession(int Length, const uint8_t *Data)
1669 {
1670  if (Length == 6 && *(Data + 1) == 0x04) {
1671  int ResourceId = ResourceIdToInt(Data + 2);
1672  dbgprotocol("OpenSession %08X\n", ResourceId);
1673  switch (ResourceId) {
1674  case RI_RESOURCE_MANAGER:
1677  case RI_HOST_CONTROL:
1678  case RI_DATE_TIME:
1679  case RI_MMI:
1680  {
1681  cCiSession *Session = CreateSession(ResourceId);
1682  if (Session)
1683  {
1685  Session->ResourceId(), SS_OK);
1686  return true;
1687  }
1688  esyslog("ERROR: can't create session for resource identifier: %08X",
1689  ResourceId);
1690  break;
1691  }
1692  default: esyslog("ERROR: unknown resource identifier: %08X", ResourceId);
1693  }
1694  }
1695  return false;
1696 }
1697 
1698 bool cLlCiHandler::CloseSession(int SessionId)
1699 {
1700  dbgprotocol("CloseSession %08X\n", SessionId);
1701  cCiSession *Session = GetSessionBySessionId(SessionId);
1702  if (Session && m_sessions[SessionId - 1] == Session) {
1703  delete Session;
1704  m_sessions[SessionId - 1] = nullptr;
1705  Send(ST_CLOSE_SESSION_RESPONSE, SessionId, 0, SS_OK);
1706  return true;
1707  }
1708 
1709  esyslog("ERROR: unknown session id: %d", SessionId);
1711  return false;
1712 }
1713 
1715 {
1716  int result = 0;
1717  for (auto & session : m_sessions) {
1718  if (session && session->Tc()->Slot() == Slot) {
1719  CloseSession(session->SessionId());
1720  result++;
1721  }
1722  }
1723  return result;
1724 }
1725 
1727 {
1728  bool result = true;
1729  cMutexLock MutexLock(&m_mutex);
1730 
1731  for (int Slot = 0; Slot < m_numSlots; Slot++)
1732  {
1733  m_tc = m_tpl->Process(Slot);
1734  if (m_tc)
1735  {
1736  int Length = 0;
1737  const uint8_t *Data = m_tc->Data(Length);
1738  if (Data && Length > 1)
1739  {
1740  switch (*Data)
1741  {
1742  case ST_SESSION_NUMBER:
1743  if (Length > 4)
1744  {
1745  int SessionId = ntohs(*(short *)&Data[2]);
1746  cCiSession *Session = GetSessionBySessionId(SessionId);
1747  if (Session)
1748  {
1749  Session->Process(Length - 4, Data + 4);
1750  if (Session->ResourceId() == RI_APPLICATION_INFORMATION)
1751  {
1752 #if 0
1753  esyslog("Test: %x",
1754  ((cCiApplicationInformation*)Session)->GetApplicationManufacturer());
1755 #endif
1756  }
1757  }
1758  else
1759  esyslog("ERROR: unknown session id: %d", SessionId);
1760  }
1761  break;
1762 
1764  OpenSession(Length, Data);
1765  break;
1766 
1768  if (Length == 4)
1769  CloseSession(ntohs(*(short *)&Data[2]));
1770  break;
1771 
1772  case ST_CREATE_SESSION_RESPONSE: //XXX fall through to default
1773  case ST_CLOSE_SESSION_RESPONSE: //XXX fall through to default
1774  default:
1775  esyslog("ERROR: unknown session tag: %02X", *Data);
1776  }
1777  }
1778  }
1779  else if (CloseAllSessions(Slot))
1780  {
1781  m_tpl->ResetSlot(Slot);
1782  result = false;
1783  }
1784  else if (m_tpl->ModuleReady(Slot))
1785  {
1786  dbgprotocol("Module ready in slot %d\n", Slot);
1787  m_tpl->NewConnection(Slot);
1788  }
1789  }
1790 
1791  bool UserIO = false;
1792  m_needCaPmt = false;
1793  for (auto & session : m_sessions)
1794  {
1795  if (session && session->Process())
1796  {
1797  UserIO |= session->HasUserIO();
1798  if (session->ResourceId() == RI_CONDITIONAL_ACCESS_SUPPORT)
1799  {
1800  auto *cas = dynamic_cast<cCiConditionalAccessSupport *>(session);
1801  if (cas == nullptr)
1802  continue;
1803  m_needCaPmt |= cas->NeedCaPmt();
1804  }
1805  }
1806  }
1807  m_hasUserIO = UserIO;
1808 
1809  if (m_newCaSupport)
1810  m_newCaSupport = result = false; // triggers new SetCaPmt at caller!
1811  return result;
1812 }
1813 
1815 {
1816  cMutexLock MutexLock(&m_mutex);
1818  return api ? api->EnterMenu() : false;
1819 }
1820 
1822 {
1823  cMutexLock MutexLock(&m_mutex);
1824  for (int Slot = 0; Slot < m_numSlots; Slot++) {
1825  auto *mmi = dynamic_cast<cCiMMI *>(GetSessionByResourceId(RI_MMI, Slot));
1826  if (mmi)
1827  return mmi->Menu();
1828  }
1829  return nullptr;
1830 }
1831 
1833 {
1834  cMutexLock MutexLock(&m_mutex);
1835  for (int Slot = 0; Slot < m_numSlots; Slot++) {
1836  auto *mmi = dynamic_cast<cCiMMI *>(GetSessionByResourceId(RI_MMI, Slot));
1837  if (mmi)
1838  return mmi->Enquiry();
1839  }
1840  return nullptr;
1841 }
1842 
1844  {
1845  static dvbca_vector empty {};
1846  cMutexLock MutexLock(&m_mutex);
1848  return cas ? cas->GetCaSystemIds() : empty;
1849 }
1850 
1851 bool cLlCiHandler::SetCaPmt(cCiCaPmt &CaPmt, int Slot)
1852 {
1853  cMutexLock MutexLock(&m_mutex);
1855  return cas && cas->SendPMT(CaPmt);
1856 }
1857 
1858 void cLlCiHandler::SetTimeOffset(double offset_in_seconds)
1859 {
1860  cMutexLock MutexLock(&m_mutex);
1861  cCiDateTime *dt = nullptr;
1862 
1863  for (uint i = 0; i < (uint) NumSlots(); i++)
1864  {
1865  dt = dynamic_cast<cCiDateTime*>(GetSessionByResourceId(RI_DATE_TIME, i));
1866  if (dt)
1867  dt->SetTimeOffset(offset_in_seconds);
1868  }
1869 }
1870 
1871 bool cLlCiHandler::Reset(int Slot)
1872 {
1873  cMutexLock MutexLock(&m_mutex);
1874  CloseAllSessions(Slot);
1875  return m_tpl->ResetSlot(Slot);
1876 }
1877 
1879 {
1880  return sConnected;
1881 }
1882 
1883 // -- cHlCiHandler -------------------------------------------------------------
1884 
1885 cHlCiHandler::cHlCiHandler(int Fd, int NumSlots)
1886 {
1887  m_numSlots = NumSlots;
1888  m_fdCa = Fd;
1889  esyslog("New High level CI handler");
1890 }
1891 
1893 {
1894  cMutexLock MutexLock(&m_mutex);
1895  close(m_fdCa);
1896 }
1897 
1898 int cHlCiHandler::CommHL(unsigned tag, unsigned function, struct ca_msg *msg) const
1899 {
1900  if (tag) {
1901  msg->msg[2] = tag & 0xff;
1902  msg->msg[1] = (tag & 0xff00) >> 8;
1903  msg->msg[0] = (tag & 0xff0000) >> 16;
1904  esyslog("Sending message=[%02x %02x %02x ]",
1905  msg->msg[0], msg->msg[1], msg->msg[2]);
1906  }
1907 
1908  return ioctl(m_fdCa, function, msg);
1909 }
1910 
1911 int cHlCiHandler::GetData(unsigned tag, struct ca_msg *msg)
1912 {
1913  return CommHL(tag, CA_GET_MSG, msg);
1914 }
1915 
1916 int cHlCiHandler::SendData(unsigned tag, struct ca_msg *msg)
1917 {
1918  return CommHL(tag, CA_SEND_MSG, msg);
1919 }
1920 
1922 {
1923  cMutexLock MutexLock(&m_mutex);
1924 
1925  struct ca_msg msg {};
1926  switch(m_state) {
1927  case 0:
1928  // Get CA_system_ids
1929  /* Enquire */
1930  if ((SendData(AOT_CA_INFO_ENQ, &msg)) < 0) {
1931  esyslog("HLCI communication failed");
1932  } else {
1933  dbgprotocol("==> Ca Info Enquiry");
1934  /* Receive */
1935  if ((GetData(AOT_CA_INFO, &msg)) < 0) {
1936  esyslog("HLCI communication failed");
1937  } else {
1938  QString message("Debug: ");
1939  for(int i = 0; i < 20; i++) {
1940  message += QString("%1 ").arg(msg.msg[i]);
1941  }
1942  LOG(VB_GENERAL, LOG_DEBUG, message);
1943  dbgprotocol("<== Ca Info");
1944  int l = msg.msg[3];
1945  const uint8_t *d = &msg.msg[4];
1946  while (l > 1) {
1947  unsigned short id = ((unsigned short)(*d) << 8) | *(d + 1);
1948  dbgprotocol(" %04X", id);
1949  d += 2;
1950  l -= 2;
1951 
1952  // Insert before the last element.
1953  m_caSystemIds.emplace_back(id);
1954  }
1955  dbgprotocol("\n");
1956  }
1957  m_state = 1;
1958  break;
1959  }
1960  }
1961 
1962  bool result = true;
1963 
1964  return result;
1965 }
1966 
1967 bool cHlCiHandler::EnterMenu(int /*Slot*/)
1968 {
1969  return false;
1970 }
1971 
1973 {
1974  return nullptr;
1975 }
1976 
1978 {
1979  return nullptr;
1980 }
1981 
1983 {
1984  return m_caSystemIds;
1985 }
1986 
1987 bool cHlCiHandler::SetCaPmt(cCiCaPmt &CaPmt, int /*Slot*/)
1988 {
1989  cMutexLock MutexLock(&m_mutex);
1990  struct ca_msg msg {};
1991 
1992  esyslog("Setting CA PMT.");
1993  m_state = 2;
1994 
1995  msg.msg[3] = CaPmt.m_length;
1996 
1997  if (CaPmt.m_length > (256 - 4))
1998  {
1999  esyslog("CA message too long");
2000  return false;
2001  }
2002 
2003  memcpy(&msg.msg[4], CaPmt.m_capmt, CaPmt.m_length);
2004 
2005  if ((SendData(AOT_CA_PMT, &msg)) < 0) {
2006  esyslog("HLCI communication failed");
2007  return false;
2008  }
2009 
2010  return true;
2011 }
2012 
2013 bool cHlCiHandler::Reset(int /*Slot*/) const
2014 {
2015  if ((ioctl(m_fdCa, CA_RESET)) < 0) {
2016  esyslog("ioctl CA_RESET failed.");
2017  return false;
2018  }
2019  return true;
2020 }
2021 
2023 {
2024  return m_state == 1;
2025 }
cCiDateTime::m_lastTime
time_t m_lastTime
Definition: dvbci.cpp:1091
cCiConditionalAccessSupport::m_state
int m_state
Definition: dvbci.cpp:1017
cCiConditionalAccessSupport
Definition: dvbci.cpp:1015
cCiApplicationInformation::~cCiApplicationInformation
~cCiApplicationInformation() override
Definition: dvbci.cpp:962
T_RCV
#define T_RCV
Definition: dvbci.cpp:251
cCiApplicationInformation::GetManufacturerCode
uint16_t GetManufacturerCode() const
Definition: dvbci.cpp:947
cLlCiHandler::SetTimeOffset
void SetTimeOffset(double offset_in_seconds) override
Definition: dvbci.cpp:1858
dvbci.h
cTPDU::cTPDU
cTPDU(void)=default
cLlCiHandler::connected
static bool connected()
Definition: dvbci.cpp:1878
cCiTransportConnection::RecvTPDU
int RecvTPDU(void)
Definition: dvbci.cpp:454
cCiTransportConnection::DataAvailable
bool DataAvailable(void) const
Definition: dvbci.cpp:410
cCiCaPmt::AddCaDescriptor
void AddCaDescriptor(int ca_system_id, int ca_pid, int data_len, const uint8_t *data)
Definition: dvbci.cpp:1523
T_TC_ERROR
#define T_TC_ERROR
Definition: dvbci.cpp:258
ST_CREATE_SESSION_RESPONSE
#define ST_CREATE_SESSION_RESPONSE
Definition: dvbci.cpp:698
tAnswer::m_text
char m_text[256]
Definition: dvbci.cpp:1394
BYTE0
#define BYTE0(a)
cLlCiHandler::GetSessionBySessionId
cCiSession * GetSessionBySessionId(int SessionId)
Definition: dvbci.cpp:1630
cCiTransportConnection::~cCiTransportConnection
~cCiTransportConnection()
Definition: dvbci.cpp:428
AOT_LIST_LAST
#define AOT_LIST_LAST
Definition: dvbci.cpp:747
cCiTransportConnection::m_lastResponse
int m_lastResponse
Definition: dvbci.cpp:401
cTPDU
Definition: dvbci.cpp:262
AOT_TEXT_LAST
#define AOT_TEXT_LAST
Definition: dvbci.cpp:738
cLlCiHandler::CloseAllSessions
int CloseAllSessions(int Slot)
Definition: dvbci.cpp:1714
cCiSession::GetTag
static int GetTag(int &Length, const uint8_t **Data)
Definition: dvbci.cpp:795
cCiConditionalAccessSupport::m_needCaPmt
bool m_needCaPmt
Definition: dvbci.cpp:1019
dvbca_vector
std::vector< uint16_t > dvbca_vector
Definition: dvbci.h:47
stDELETION
@ stDELETION
Definition: dvbci.cpp:390
stIDLE
@ stIDLE
Definition: dvbci.cpp:390
cCiApplicationInformation::cCiApplicationInformation
cCiApplicationInformation(int SessionId, cCiTransportConnection *Tc)
Definition: dvbci.cpp:950
T_NEW_TC
#define T_NEW_TC
Definition: dvbci.cpp:257
RI_MMI
#define RI_MMI
Definition: dvbci.cpp:714
cCiTransportConnection::m_slot
uint8_t m_slot
Definition: dvbci.cpp:396
AOT_NONE
#define AOT_NONE
Definition: dvbci.cpp:718
DRI_MMI_MODE_ACK
#define DRI_MMI_MODE_ACK
Definition: dvbci.cpp:1206
cCiConditionalAccessSupport::m_caSystemIds
dvbca_vector m_caSystemIds
Definition: dvbci.cpp:1018
cHlCiHandler::cHlCiHandler
cHlCiHandler(int Fd, int NumSlots)
Definition: dvbci.cpp:1885
cCiMMI
Definition: dvbci.cpp:1224
discid.disc.read
def read(device=None, features=[])
Definition: disc.py:35
cMutex::m_mutex
pthread_mutex_t m_mutex
Definition: dvbci.h:52
AOT_PROFILE_CHANGE
#define AOT_PROFILE_CHANGE
Definition: dvbci.cpp:721
cHlCiHandler::SetCaPmt
bool SetCaPmt(cCiCaPmt &CaPmt)
GetString
static char * GetString(int &Length, const uint8_t **Data)
Definition: dvbci.cpp:178
cCiTransportLayer::m_numSlots
int m_numSlots
Definition: dvbci.cpp:597
cLlCiHandler::NumSlots
int NumSlots(void) override
Definition: dvbci.h:187
cCiTransportConnection::SendData
int SendData(int Length, const uint8_t *Data)
Definition: dvbci.cpp:512
cHlCiHandler::GetData
int GetData(unsigned tag, struct ca_msg *msg)
Definition: dvbci.cpp:1911
cCiMenu::cCiMenu
cCiMenu(cCiMMI *MMI, bool Selectable)
Definition: dvbci.cpp:1413
mythburn.write
def write(text, progress=True)
Definition: mythburn.py:308
cCiCaPmt::m_length
int m_length
Definition: dvbci.h:130
cCiEnquiry::Cancel
bool Cancel(void)
Definition: dvbci.cpp:1461
ST_OPEN_SESSION_REQUEST
#define ST_OPEN_SESSION_REQUEST
Definition: dvbci.cpp:695
cTPDU::Write
int Write(int fd)
Definition: dvbci.cpp:325
cCiTransportConnection::Init
void Init(int Fd, uint8_t Slot, uint8_t Tcid)
Definition: dvbci.cpp:433
LOG_ERROR_STR
#define LOG_ERROR_STR(s)
Definition: dvbci.cpp:59
stACTIVE
@ stACTIVE
Definition: dvbci.cpp:390
cTPDU::m_data
std::array< uint8_t, MAX_TPDU_SIZE > m_data
Definition: dvbci.cpp:265
WRKRND_TIME_BEFORE_ENTER_MENU
#define WRKRND_TIME_BEFORE_ENTER_MENU
Definition: dvbci.cpp:77
AI_CANCEL
#define AI_CANCEL
Definition: dvbci.cpp:1221
cLlCiHandler::m_tc
cCiTransportConnection * m_tc
Definition: dvbci.h:173
cLlCiHandler::GetEnquiry
cCiEnquiry * GetEnquiry(void) override
Definition: dvbci.cpp:1832
cCiCaPmt
Definition: dvbci.h:126
cHlCiHandler::SendData
int SendData(unsigned tag, struct ca_msg *msg)
Definition: dvbci.cpp:1916
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:23
cLlCiHandler::Process
bool Process(void) override
Definition: dvbci.cpp:1726
cMutexLock::~cMutexLock
~cMutexLock()
Definition: dvbci.cpp:224
sDebugProtocol
static bool sDebugProtocol
Definition: dvbci.cpp:64
AOT_CLOSE_MMI
#define AOT_CLOSE_MMI
Definition: dvbci.cpp:735
cCiMenu::m_mmi
cCiMMI * m_mmi
Definition: dvbci.h:79
RI_HOST_CONTROL
#define RI_HOST_CONTROL
Definition: dvbci.cpp:712
AOT_CA_PMT
#define AOT_CA_PMT
Definition: dvbci.cpp:727
AOT_ENQ
#define AOT_ENQ
Definition: dvbci.cpp:742
cCiTransportConnection::m_lastPoll
std::chrono::milliseconds m_lastPoll
Definition: dvbci.cpp:400
cCiTransportLayer::m_tc
std::array< cCiTransportConnection, MAX_CI_CONNECT > m_tc
Definition: dvbci.cpp:598
cCiEnquiry::Reply
bool Reply(const char *s)
Definition: dvbci.cpp:1456
cMutexLock
Definition: dvbci.h:62
cCiTransportLayer::Process
cCiTransportConnection * Process(int Slot)
Definition: dvbci.cpp:651
AOT_PROFILE
#define AOT_PROFILE
Definition: dvbci.cpp:720
cHlCiHandler::~cHlCiHandler
~cHlCiHandler() override
Definition: dvbci.cpp:1892
cCiMMI::cCiMMI
cCiMMI(int SessionId, cCiTransportConnection *Tc)
Definition: dvbci.cpp:1240
cCiApplicationInformation::m_applicationManufacturer
uint16_t m_applicationManufacturer
Definition: dvbci.cpp:937
cLlCiHandler::m_mutex
cMutex m_mutex
Definition: dvbci.h:165
AOT_DATE_TIME_ENQ
#define AOT_DATE_TIME_ENQ
Definition: dvbci.cpp:733
cCiTransportConnection::m_tcid
uint8_t m_tcid
Definition: dvbci.cpp:397
AOT_APPLICATION_INFO
#define AOT_APPLICATION_INFO
Definition: dvbci.cpp:723
cCiDateTime::m_timeOffset
int m_timeOffset
Definition: dvbci.cpp:1092
cCiCaPmt::m_capmt
uint8_t m_capmt[2048]
XXX is there a specified maximum?
Definition: dvbci.h:132
cCiResourceManager::Process
bool Process(int Length=0, const uint8_t *Data=nullptr) override
Definition: dvbci.cpp:881
cHlCiHandler::Process
bool Process(void) override
Definition: dvbci.cpp:1921
CPCI_OK_DESCRAMBLING
#define CPCI_OK_DESCRAMBLING
Definition: dvbci.cpp:1470
close
#define close
Definition: compat.h:17
cCiMenu::Selectable
bool Selectable(void) const
Definition: dvbci.h:95
cLlCiHandler::m_numSlots
int m_numSlots
Definition: dvbci.h:167
cLlCiHandler::~cLlCiHandler
~cLlCiHandler() override
Definition: dvbci.cpp:1571
CLOSE_MMI_DELAY
#define CLOSE_MMI_DELAY
Definition: dvbci.cpp:1188
T_CREATE_TC
#define T_CREATE_TC
Definition: dvbci.cpp:252
CAM_READ_TIMEOUT
#define CAM_READ_TIMEOUT
Definition: dvbci.cpp:452
cCiMenu::m_subTitleText
char * m_subTitleText
Definition: dvbci.h:82
cLlCiHandler::m_sessions
cCiSession * m_sessions[MAX_CI_SESSION]
Definition: dvbci.h:171
cHlCiHandler::m_state
int m_state
Definition: dvbci.h:211
RI_RESOURCE_MANAGER
#define RI_RESOURCE_MANAGER
Definition: dvbci.cpp:709
cMutex
Definition: dvbci.h:49
cHlCiHandler::EnterMenu
bool EnterMenu(int Slot) override
Definition: dvbci.cpp:1967
cCiApplicationInformation::m_menuString
char * m_menuString
Definition: dvbci.cpp:939
cMutex::m_locked
int m_locked
Definition: dvbci.h:54
cCiTransportConnection
Definition: dvbci.cpp:392
cTPDU::Dump
void Dump(bool Outgoing)
Definition: dvbci.cpp:346
safe_read
static ssize_t safe_read(int filedes, void *buffer, size_t size)
Definition: dvbci.cpp:83
MAX_CI_CONNECT
#define MAX_CI_CONNECT
Definition: dvbci.cpp:592
cCiTransportLayer::ModuleReady
bool ModuleReady(int Slot) const
Definition: dvbci.cpp:641
mythlogging.h
RI_CONDITIONAL_ACCESS_SUPPORT
#define RI_CONDITIONAL_ACCESS_SUPPORT
Definition: dvbci.cpp:711
RI_APPLICATION_INFORMATION
#define RI_APPLICATION_INFORMATION
Definition: dvbci.cpp:710
BYTE1
#define BYTE1(a)
SetLength
static uint8_t * SetLength(uint8_t *Data, int Length)
Definition: dvbci.cpp:117
cCiSession::~cCiSession
virtual ~cCiSession()=default
cMutexLock::m_locked
bool m_locked
Definition: dvbci.h:65
cCiMMI::SendAnswer
bool SendAnswer(const char *Text)
Definition: dvbci.cpp:1397
AOT_APPLICATION_INFO_ENQ
#define AOT_APPLICATION_INFO_ENQ
Definition: dvbci.cpp:722
AI_ANSWER
#define AI_ANSWER
Definition: dvbci.cpp:1222
cCiEnquiry::m_blind
bool m_blind
Definition: dvbci.h:105
cCiHandler::NumSlots
virtual int NumSlots(void)=0
cLlCiHandler::SetCaPmt
bool SetCaPmt(cCiCaPmt &CaPmt)
stCREATION
@ stCREATION
Definition: dvbci.cpp:390
hardwareprofile.config.p
p
Definition: config.py:33
hardwareprofile.i18n.t
t
Definition: i18n.py:36
ST_OPEN_SESSION_RESPONSE
#define ST_OPEN_SESSION_RESPONSE
Definition: dvbci.cpp:696
T_DTC_REPLY
#define T_DTC_REPLY
Definition: dvbci.cpp:255
T_SB
#define T_SB
Definition: dvbci.cpp:250
cCiSession::SessionId
int SessionId(void) const
Definition: dvbci.cpp:782
cTPDU::Tcid
uint8_t Tcid(void)
Definition: dvbci.cpp:271
cCiEnquiry::~cCiEnquiry
~cCiEnquiry()
Definition: dvbci.cpp:1451
cCiResourceManager
Definition: dvbci.cpp:866
cLlCiHandler::GetCaSystemIds
dvbca_vector GetCaSystemIds(int Slot) override
Definition: dvbci.cpp:1843
cLlCiHandler::m_fdCa
int m_fdCa
Definition: dvbci.h:166
cCiSession::GetData
static const uint8_t * GetData(const uint8_t *Data, int &Length)
Definition: dvbci.cpp:814
cCiMenu::Select
bool Select(int Index)
Definition: dvbci.cpp:1437
MAX_CI_SESSION
#define MAX_CI_SESSION
Definition: dvbci.h:140
cHlCiHandler
Definition: dvbci.h:205
State
State
Definition: zmserver.h:68
ST_CLOSE_SESSION_REQUEST
#define ST_CLOSE_SESSION_REQUEST
Definition: dvbci.cpp:699
MAX_TPDU_DATA
#define MAX_TPDU_DATA
Definition: dvbci.cpp:246
cCiMenu::m_bottomText
char * m_bottomText
Definition: dvbci.h:83
cCiMenu::m_titleText
char * m_titleText
Definition: dvbci.h:81
tAnswer::m_id
uint8_t m_id
Definition: dvbci.cpp:1394
cCiTransportConnection::SendData
int SendData(std::vector< uint8_t > &Data)
Definition: dvbci.cpp:416
AOT_ANSW
#define AOT_ANSW
Definition: dvbci.cpp:743
cCiTransportConnection::Poll
int Poll(void)
Definition: dvbci.cpp:571
cLlCiHandler::Send
bool Send(uint8_t Tag, int SessionId, int ResourceId=0, int Status=-1)
Definition: dvbci.cpp:1613
cMutex::Unlock
void Unlock(void)
Definition: dvbci.cpp:211
cMutexLock::m_mutex
cMutex * m_mutex
Definition: dvbci.h:64
cCiMMI::m_menu
cCiMenu * m_menu
Definition: dvbci.cpp:1227
cLlCiHandler::m_hasUserIO
bool m_hasUserIO
Definition: dvbci.h:169
CLOSE_MMI_IMMEDIATE
#define CLOSE_MMI_IMMEDIATE
Definition: dvbci.cpp:1187
cCiTransportConnection::m_dataAvailable
bool m_dataAvailable
Definition: dvbci.cpp:402
cCiConditionalAccessSupport::GetCaSystemIds
dvbca_vector GetCaSystemIds(void)
Definition: dvbci.cpp:1023
cLlCiHandler::CloseSession
bool CloseSession(int SessionId)
Definition: dvbci.cpp:1698
cLlCiHandler
Definition: dvbci.h:162
AOT_DISPLAY_REPLY
#define AOT_DISPLAY_REPLY
Definition: dvbci.cpp:737
cMutexLock::Lock
bool Lock(cMutex *Mutex)
Definition: dvbci.cpp:230
cCiDateTime::SetTimeOffset
void SetTimeOffset(double offset)
Definition: dvbci.cpp:1109
MAX_DUMP
#define MAX_DUMP
cMutex::Lock
void Lock(void)
Definition: dvbci.cpp:202
cTPDU::Status
uint8_t Status(void)
Definition: dvbci.cpp:379
cLlCiHandler::CreateSession
cCiSession * CreateSession(int ResourceId)
Definition: dvbci.cpp:1648
cCiApplicationInformation::GetApplicationManufacturer
uint16_t GetApplicationManufacturer() const
Definition: dvbci.cpp:946
cCiTransportLayer
Definition: dvbci.cpp:594
cCiSession::cCiSession
cCiSession(int SessionId, int ResourceId, cCiTransportConnection *Tc)
Definition: dvbci.cpp:788
cCiTransportConnection::Data
const uint8_t * Data(int &Length)
Definition: dvbci.cpp:536
cCiMenu::~cCiMenu
~cCiMenu()
Definition: dvbci.cpp:1419
AOT_ENTER_MENU
#define AOT_ENTER_MENU
Definition: dvbci.cpp:724
cCiApplicationInformation::GetApplicationString
char * GetApplicationString()
Definition: dvbci.cpp:945
cHlCiHandler::m_caSystemIds
dvbca_vector m_caSystemIds
Definition: dvbci.h:213
cCiSession::Process
virtual bool Process(int Length=0, const uint8_t *Data=nullptr)
Definition: dvbci.cpp:857
uint
unsigned int uint
Definition: compat.h:144
cCiResourceManager::cCiResourceManager
cCiResourceManager(int SessionId, cCiTransportConnection *Tc)
Definition: dvbci.cpp:874
cCiSession::Tc
const cCiTransportConnection * Tc(void)
Definition: dvbci.cpp:781
cCiMenu::m_entries
char * m_entries[MAX_CIMENU_ENTRIES]
Definition: dvbci.h:84
cCiApplicationInformation::m_manufacturerCode
uint16_t m_manufacturerCode
Definition: dvbci.cpp:938
cTPDU::Tag
uint8_t Tag(void)
Definition: dvbci.cpp:272
cHlCiHandler::Reset
bool Reset(int Slot) const
Definition: dvbci.cpp:2013
cCiConditionalAccessSupport::SendPMT
bool SendPMT(const cCiCaPmt &CaPmt)
Definition: dvbci.cpp:1076
cHlCiHandler::CommHL
int CommHL(unsigned tag, unsigned function, struct ca_msg *msg) const
Definition: dvbci.cpp:1898
cHlCiHandler::NumSlots
int NumSlots(void) override
Definition: dvbci.h:220
cCiTransportLayer::ResetSlot
bool ResetSlot(int Slot) const
Definition: dvbci.cpp:629
ST_SESSION_NUMBER
#define ST_SESSION_NUMBER
Definition: dvbci.cpp:694
cCiConditionalAccessSupport::Process
bool Process(int Length=0, const uint8_t *Data=nullptr) override
Definition: dvbci.cpp:1035
cCiMMI::HasUserIO
bool HasUserIO(void) override
Definition: dvbci.cpp:1233
cTPDU::m_size
int m_size
Definition: dvbci.cpp:264
AOT_PROFILE_ENQ
#define AOT_PROFILE_ENQ
Definition: dvbci.cpp:719
cCiApplicationInformation::Process
bool Process(int Length=0, const uint8_t *Data=nullptr) override
Definition: dvbci.cpp:967
dsyslog
#define dsyslog(a...)
Definition: dvbci.cpp:56
cCiApplicationInformation::EnterMenu
bool EnterMenu(void)
Definition: dvbci.cpp:1003
cCiMenu::m_selectable
bool m_selectable
Definition: dvbci.h:80
cHlCiHandler::m_fdCa
int m_fdCa
Definition: dvbci.h:209
cHlCiHandler::m_numSlots
int m_numSlots
Definition: dvbci.h:210
esyslog
#define esyslog(a...)
Definition: dvbci.cpp:54
cTPDU::Slot
uint8_t Slot(void)
Definition: dvbci.cpp:270
cCiSession::SendData
int SendData(int Tag, std::vector< uint8_t > &Data)
Definition: dvbci.cpp:776
ERROR
#define ERROR
Definition: dvbci.cpp:71
sConnected
static bool sConnected
Definition: dvbci.cpp:65
cCiMMI::SendMenuAnswer
bool SendMenuAnswer(uint8_t Selection)
Definition: dvbci.cpp:1384
AOT_DISPLAY_CONTROL
#define AOT_DISPLAY_CONTROL
Definition: dvbci.cpp:736
cCiApplicationInformation
Definition: dvbci.cpp:932
cCiSession
Definition: dvbci.cpp:767
DCC_SET_MMI_MODE
#define DCC_SET_MMI_MODE
Definition: dvbci.cpp:1192
cLlCiHandler::m_tpl
cCiTransportLayer * m_tpl
Definition: dvbci.h:172
cCiTransportConnection::m_state
eState m_state
Definition: dvbci.cpp:398
cCiMMI::~cCiMMI
~cCiMMI() override
Definition: dvbci.cpp:1248
T_DATA_LAST
#define T_DATA_LAST
Definition: dvbci.cpp:259
SIZE_INDICATOR
#define SIZE_INDICATOR
Definition: dvbci.cpp:81
cHlCiHandler::GetCaSystemIds
dvbca_vector GetCaSystemIds(int Slot) override
Definition: dvbci.cpp:1982
cCiTransportConnection::Slot
int Slot(void) const
Definition: dvbci.cpp:414
cCiDateTime::Process
bool Process(int Length=0, const uint8_t *Data=nullptr) override
Definition: dvbci.cpp:1156
cMutex::m_lockingPid
pid_t m_lockingPid
Definition: dvbci.h:53
cCiMMI::m_enquiry
cCiEnquiry * m_enquiry
Definition: dvbci.cpp:1228
dbgprotocol
#define dbgprotocol(a...)
Definition: dvbci.cpp:67
eState
eState
Definition: dvbci.cpp:390
cCiEnquiry::m_mmi
cCiMMI * m_mmi
Definition: dvbci.h:103
cCiTransportLayer::NewConnection
cCiTransportConnection * NewConnection(int Slot)
Definition: dvbci.cpp:615
cCiMenu::MAX_CIMENU_ENTRIES
@ MAX_CIMENU_ENTRIES
Definition: dvbci.h:78
cCiTransportConnection::cCiTransportConnection
cCiTransportConnection(void)
Definition: dvbci.cpp:423
cTPDU::GetData
const uint8_t * GetData(const uint8_t *Data, int &Length) const
Definition: dvbci.cpp:367
MALLOC
#define MALLOC(type, size)
Definition: dvbci.cpp:51
T_CTC_REPLY
#define T_CTC_REPLY
Definition: dvbci.cpp:253
DATA_INDICATOR
#define DATA_INDICATOR
Definition: dvbci.cpp:248
cCiDateTime
Definition: dvbci.cpp:1088
cCiTransportConnection::LastResponse
int LastResponse(void) const
Definition: dvbci.cpp:409
cCiTransportLayer::m_fd
int m_fd
Definition: dvbci.cpp:596
cLlCiHandler::cLlCiHandler
cLlCiHandler(int Fd, int NumSlots)
Definition: dvbci.cpp:1564
cCiTransportLayer::cCiTransportLayer
cCiTransportLayer(int Fd, int NumSlots)
Definition: dvbci.cpp:607
POLL_INTERVAL
static constexpr std::chrono::milliseconds POLL_INTERVAL
Definition: dvbci.cpp:569
ST_CLOSE_SESSION_RESPONSE
#define ST_CLOSE_SESSION_RESPONSE
Definition: dvbci.cpp:700
cCiMMI::GetText
char * GetText(int &Length, const uint8_t **Data)
Definition: dvbci.cpp:1254
AOT_MENU_LAST
#define AOT_MENU_LAST
Definition: dvbci.cpp:744
AOT_CA_INFO
#define AOT_CA_INFO
Definition: dvbci.cpp:726
cTPDU::Data
const uint8_t * Data(int &Length)
Definition: dvbci.cpp:273
cHlCiHandler::NeedCaPmt
bool NeedCaPmt(void) override
Definition: dvbci.cpp:2022
cCiSession::m_tc
cCiTransportConnection * m_tc
Definition: dvbci.cpp:771
AOT_CA_INFO_ENQ
#define AOT_CA_INFO_ENQ
Definition: dvbci.cpp:725
cCiEnquiry::m_text
char * m_text
Definition: dvbci.h:104
cLlCiHandler::Reset
bool Reset(int Slot)
Definition: dvbci.cpp:1871
cLlCiHandler::m_needCaPmt
bool m_needCaPmt
Definition: dvbci.h:170
cLlCiHandler::EnterMenu
bool EnterMenu(int Slot) override
Definition: dvbci.cpp:1814
cLlCiHandler::ResourceIdToInt
static int ResourceIdToInt(const uint8_t *Data)
Definition: dvbci.cpp:1608
sDumpTPDUDataTransfer
static bool sDumpTPDUDataTransfer
Definition: dvbci.cpp:63
EF_BLIND
#define EF_BLIND
Definition: dvbci.cpp:1217
CopyString
static char * CopyString(int Length, const uint8_t *Data)
Definition: dvbci.cpp:166
cCiHandler::CreateCiHandler
static cCiHandler * CreateCiHandler(const char *FileName)
Definition: dvbci.cpp:1580
AOT_DATE_TIME
#define AOT_DATE_TIME
Definition: dvbci.cpp:734
DEC2BCD
#define DEC2BCD(d)
GetLength
static const uint8_t * GetLength(const uint8_t *Data, int &Length)
Definition: dvbci.cpp:95
uint16_t
unsigned short uint16_t
Definition: iso6937tables.h:3
cCiApplicationInformation::m_state
int m_state
Definition: dvbci.cpp:934
cCiMenu
Definition: dvbci.h:75
cCiSession::m_resourceId
int m_resourceId
Definition: dvbci.cpp:770
cTPDU::Read
int Read(int fd)
Definition: dvbci.cpp:334
tAnswer
Definition: dvbci.cpp:1394
cCiEnquiry
Definition: dvbci.h:100
cCiSession::HasUserIO
virtual bool HasUserIO(void)
Definition: dvbci.cpp:784
cCiDateTime::SendDateTime
bool SendDateTime(void)
Definition: dvbci.cpp:1115
cCiSession::m_sessionId
int m_sessionId
Definition: dvbci.cpp:769
cHlCiHandler::m_mutex
cMutex m_mutex
Definition: dvbci.h:208
cCiResourceManager::m_state
int m_state
Definition: dvbci.cpp:868
d
static const iso6937table * d
Definition: iso6937tables.cpp:1025
cCiCaPmt::m_infoLengthPos
int m_infoLengthPos
Definition: dvbci.h:131
AOT_MENU_ANSW
#define AOT_MENU_ANSW
Definition: dvbci.cpp:746
cCiTransportConnection::State
eState State(void)
Definition: dvbci.cpp:408
TIMEOUT
#define TIMEOUT
Definition: dvbci.cpp:70
cCiTransportConnection::CreateConnection
int CreateConnection(void)
Definition: dvbci.cpp:543
cCiApplicationInformation::m_applicationType
uint8_t m_applicationType
Definition: dvbci.cpp:936
cLlCiHandler::GetSessionByResourceId
cCiSession * GetSessionByResourceId(int ResourceId, int Slot)
Definition: dvbci.cpp:1639
cCiConditionalAccessSupport::cCiConditionalAccessSupport
cCiConditionalAccessSupport(int SessionId, cCiTransportConnection *Tc)
Definition: dvbci.cpp:1028
cCiTransportConnection::m_fd
int m_fd
Definition: dvbci.cpp:395
cCiCaPmt::cCiCaPmt
cCiCaPmt(int ProgramNumber, uint8_t cplm=CPLM_ONLY)
Definition: dvbci.cpp:1475
cCiMMI::Menu
cCiMenu * Menu(void)
Definition: dvbci.cpp:1370
MAX_CONNECT_RETRIES
#define MAX_CONNECT_RETRIES
Definition: dvbci.cpp:541
cCiConditionalAccessSupport::NeedCaPmt
bool NeedCaPmt(void) const
Definition: dvbci.cpp:1025
D
#define D(i, j)
cCiTransportConnection::m_tpdu
cTPDU * m_tpdu
Definition: dvbci.cpp:399
OK
#define OK
Definition: dvbci.cpp:69
cLlCiHandler::GetMenu
cCiMenu * GetMenu(void) override
Definition: dvbci.cpp:1821
cLlCiHandler::m_newCaSupport
bool m_newCaSupport
Definition: dvbci.h:168
cCiTransportConnection::RecvData
int RecvData(void)
Definition: dvbci.cpp:529
cCiSession::ResourceId
int ResourceId(void) const
Definition: dvbci.cpp:783
cCiEnquiry::m_expectedLength
int m_expectedLength
Definition: dvbci.h:106
cCiDateTime::m_interval
int m_interval
Definition: dvbci.cpp:1090
cCiHandler
Definition: dvbci.h:146
cCiMenu::AddEntry
bool AddEntry(char *s)
Definition: dvbci.cpp:1428
cCiTransportConnection::SendTPDU
int SendTPDU(uint8_t Tag, int Length=0, const uint8_t *Data=nullptr) const
Definition: dvbci.cpp:446
RI_DATE_TIME
#define RI_DATE_TIME
Definition: dvbci.cpp:713
cCiApplicationInformation::m_creationTime
time_t m_creationTime
Definition: dvbci.cpp:935
cCiMMI::Process
bool Process(int Length=0, const uint8_t *Data=nullptr) override
Definition: dvbci.cpp:1273
T_REQUEST_TC
#define T_REQUEST_TC
Definition: dvbci.cpp:256
isyslog
#define isyslog(a...)
Definition: dvbci.cpp:55
cCiSession::SendData
int SendData(int Tag, int Length=0, const uint8_t *Data=nullptr)
Definition: dvbci.cpp:820
find
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)
Definition: dvbstreamhandler.cpp:356
MM_HIGH_LEVEL
#define MM_HIGH_LEVEL
Definition: dvbci.cpp:1200
cCiMenu::m_numEntries
int m_numEntries
Definition: dvbci.h:85
T_DELETE_TC
#define T_DELETE_TC
Definition: dvbci.cpp:254
cLlCiHandler::OpenSession
bool OpenSession(int Length, const uint8_t *Data)
Definition: dvbci.cpp:1668
SS_OK
#define SS_OK
Definition: dvbci.cpp:704
cCiMenu::Cancel
bool Cancel(void)
Definition: dvbci.cpp:1444
cCiMMI::Enquiry
cCiEnquiry * Enquiry(void)
Definition: dvbci.cpp:1377
cHlCiHandler::GetMenu
cCiMenu * GetMenu(void) override
Definition: dvbci.cpp:1972
SS_NOT_ALLOCATED
#define SS_NOT_ALLOCATED
Definition: dvbci.cpp:705
cHlCiHandler::GetEnquiry
cCiEnquiry * GetEnquiry(void) override
Definition: dvbci.cpp:1977
cCiCaPmt::AddElementaryStream
void AddElementaryStream(int type, int pid)
Definition: dvbci.cpp:1488
cCiDateTime::cCiDateTime
cCiDateTime(int SessionId, cCiTransportConnection *Tc)
Definition: dvbci.cpp:1100
T_DATA_MORE
#define T_DATA_MORE
Definition: dvbci.cpp:260