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  Init(-1, m_slot, m_tcid);
507  }
508  return m_lastResponse;
509 }
510 
511 int cCiTransportConnection::SendData(int Length, const uint8_t *Data)
512 {
513  while (m_state == stACTIVE && Length > 0) {
514  uint8_t Tag = T_DATA_LAST;
515  int l = Length;
516  if (l > MAX_TPDU_DATA) {
517  Tag = T_DATA_MORE;
518  l = MAX_TPDU_DATA;
519  }
520  if (SendTPDU(Tag, l, Data) != OK || RecvTPDU() != T_SB)
521  break;
522  Length -= l;
523  Data += l;
524  }
525  return Length ? ERROR : OK;
526 }
527 
529 {
530  if (SendTPDU(T_RCV) == OK)
531  return RecvTPDU();
532  return ERROR;
533 }
534 
535 const uint8_t *cCiTransportConnection::Data(int &Length)
536 {
537  return m_tpdu->Data(Length);
538 }
539 
540 #define MAX_CONNECT_RETRIES 25
541 
543 {
544  if (m_state == stIDLE) {
545  if (SendTPDU(T_CREATE_TC) == OK) {
547  if (RecvTPDU() == T_CTC_REPLY) {
548  sConnected=true;
549  return OK;
550  // the following is a workaround for CAMs that don't quite follow the specs...
551  }
552 
553  for (int i = 0; i < MAX_CONNECT_RETRIES; i++) {
554  dsyslog("CAM: retrying to establish connection");
555  if (RecvTPDU() == T_CTC_REPLY) {
556  dsyslog("CAM: connection established");
557  sConnected=true;
558  return OK;
559  }
560  }
561  return ERROR;
562  }
563  }
564  return ERROR;
565 }
566 
567 // Polls can be done with a 100ms interval (EN50221 - A.4.1.12)
568 static constexpr std::chrono::milliseconds POLL_INTERVAL { 100ms };
569 
571 {
572  if (m_state != stACTIVE)
573  return ERROR;
574 
575  auto curr_time = nowAsDuration<std::chrono::milliseconds>();
576  std::chrono::milliseconds msdiff = curr_time - m_lastPoll;
577 
578  if (msdiff < POLL_INTERVAL)
579  return OK;
580 
581  m_lastPoll = curr_time;
582 
583  if (SendTPDU(T_DATA_LAST) != OK)
584  return ERROR;
585 
586  return RecvTPDU();
587 }
588 
589 // --- cCiTransportLayer -----------------------------------------------------
590 
591 #define MAX_CI_CONNECT 16 // maximum possible value is 254
592 
594 private:
595  int m_fd;
597  std::array<cCiTransportConnection,MAX_CI_CONNECT> m_tc;
598 public:
599  cCiTransportLayer(int Fd, int NumSlots);
601  bool ResetSlot(int Slot) const;
602  bool ModuleReady(int Slot) const;
603  cCiTransportConnection *Process(int Slot);
604  };
605 
607 {
608  m_fd = Fd;
609  m_numSlots = NumSlots;
610  for (int s = 0; s < m_numSlots; s++)
611  ResetSlot(s);
612 }
613 
615 {
616  for (int i = 0; i < MAX_CI_CONNECT; i++) {
617  if (m_tc[i].State() == stIDLE) {
618  dbgprotocol("Creating connection: slot %d, tcid %d\n", Slot, i + 1);
619  m_tc[i].Init(m_fd, Slot, i + 1);
620  if (m_tc[i].CreateConnection() == OK)
621  return &m_tc[i];
622  break;
623  }
624  }
625  return nullptr;
626 }
627 
628 bool cCiTransportLayer::ResetSlot(int Slot) const
629 {
630  dbgprotocol("Resetting slot %d...", Slot);
631  if (ioctl(m_fd, CA_RESET, 1 << Slot) != -1) {
632  dbgprotocol("ok.\n");
633  return true;
634  }
635  esyslog("ERROR: can't reset CAM slot %d: %m", Slot);
636  dbgprotocol("failed!\n");
637  return false;
638 }
639 
640 bool cCiTransportLayer::ModuleReady(int Slot) const
641 {
642  ca_slot_info_t sinfo;
643  sinfo.num = Slot;
644  if (ioctl(m_fd, CA_GET_SLOT_INFO, &sinfo) != -1)
645  return (sinfo.flags & CA_CI_MODULE_READY) != 0U;
646  esyslog("ERROR: can't get info on CAM slot %d: %m", Slot);
647  return false;
648 }
649 
651 {
652  for (auto & conn : m_tc) {
653  cCiTransportConnection *Tc = &conn;
654  if (Tc->Slot() == Slot) {
655  switch (Tc->State()) {
656  case stCREATION:
657  case stACTIVE:
658  if (!Tc->DataAvailable()) {
659  Tc->Poll();
660  }
661  switch (Tc->LastResponse()) {
662  case T_REQUEST_TC:
663  //XXX
664  break;
665  case T_DATA_MORE:
666  case T_DATA_LAST:
667  case T_CTC_REPLY:
668  case T_SB:
669  if (Tc->DataAvailable())
670  Tc->RecvData();
671  break;
672  case TIMEOUT:
673  case ERROR:
674  default:
675  //XXX Tc->state = stIDLE;//XXX Init()???
676  return nullptr;
677  break;
678  }
679  //XXX this will only work with _one_ transport connection per slot!
680  return Tc;
681  break;
682  default: ;
683  }
684  }
685  }
686  return nullptr;
687 }
688 
689 // -- cCiSession -------------------------------------------------------------
690 
691 // Session Tags:
692 
693 #define ST_SESSION_NUMBER 0x90
694 #define ST_OPEN_SESSION_REQUEST 0x91
695 #define ST_OPEN_SESSION_RESPONSE 0x92
696 #define ST_CREATE_SESSION 0x93
697 #define ST_CREATE_SESSION_RESPONSE 0x94
698 #define ST_CLOSE_SESSION_REQUEST 0x95
699 #define ST_CLOSE_SESSION_RESPONSE 0x96
700 
701 // Session Status:
702 
703 #define SS_OK 0x00
704 #define SS_NOT_ALLOCATED 0xF0
705 
706 // Resource Identifiers:
707 
708 #define RI_RESOURCE_MANAGER 0x00010041
709 #define RI_APPLICATION_INFORMATION 0x00020041
710 #define RI_CONDITIONAL_ACCESS_SUPPORT 0x00030041
711 #define RI_HOST_CONTROL 0x00200041
712 #define RI_DATE_TIME 0x00240041
713 #define RI_MMI 0x00400041
714 
715 // Application Object Tags:
716 
717 #define AOT_NONE 0x000000
718 #define AOT_PROFILE_ENQ 0x9F8010
719 #define AOT_PROFILE 0x9F8011
720 #define AOT_PROFILE_CHANGE 0x9F8012
721 #define AOT_APPLICATION_INFO_ENQ 0x9F8020
722 #define AOT_APPLICATION_INFO 0x9F8021
723 #define AOT_ENTER_MENU 0x9F8022
724 #define AOT_CA_INFO_ENQ 0x9F8030
725 #define AOT_CA_INFO 0x9F8031
726 #define AOT_CA_PMT 0x9F8032
727 #define AOT_CA_PMT_REPLY 0x9F8033
728 #define AOT_TUNE 0x9F8400
729 #define AOT_REPLACE 0x9F8401
730 #define AOT_CLEAR_REPLACE 0x9F8402
731 #define AOT_ASK_RELEASE 0x9F8403
732 #define AOT_DATE_TIME_ENQ 0x9F8440
733 #define AOT_DATE_TIME 0x9F8441
734 #define AOT_CLOSE_MMI 0x9F8800
735 #define AOT_DISPLAY_CONTROL 0x9F8801
736 #define AOT_DISPLAY_REPLY 0x9F8802
737 #define AOT_TEXT_LAST 0x9F8803
738 #define AOT_TEXT_MORE 0x9F8804
739 #define AOT_KEYPAD_CONTROL 0x9F8805
740 #define AOT_KEYPRESS 0x9F8806
741 #define AOT_ENQ 0x9F8807
742 #define AOT_ANSW 0x9F8808
743 #define AOT_MENU_LAST 0x9F8809
744 #define AOT_MENU_MORE 0x9F880A
745 #define AOT_MENU_ANSW 0x9F880B
746 #define AOT_LIST_LAST 0x9F880C
747 #define AOT_LIST_MORE 0x9F880D
748 #define AOT_SUBTITLE_SEGMENT_LAST 0x9F880E
749 #define AOT_SUBTITLE_SEGMENT_MORE 0x9F880F
750 #define AOT_DISPLAY_MESSAGE 0x9F8810
751 #define AOT_SCENE_END_MARK 0x9F8811
752 #define AOT_SCENE_DONE 0x9F8812
753 #define AOT_SCENE_CONTROL 0x9F8813
754 #define AOT_SUBTITLE_DOWNLOAD_LAST 0x9F8814
755 #define AOT_SUBTITLE_DOWNLOAD_MORE 0x9F8815
756 #define AOT_FLUSH_DOWNLOAD 0x9F8816
757 #define AOT_DOWNLOAD_REPLY 0x9F8817
758 #define AOT_COMMS_CMD 0x9F8C00
759 #define AOT_CONNECTION_DESCRIPTOR 0x9F8C01
760 #define AOT_COMMS_REPLY 0x9F8C02
761 #define AOT_COMMS_SEND_LAST 0x9F8C03
762 #define AOT_COMMS_SEND_MORE 0x9F8C04
763 #define AOT_COMMS_RCV_LAST 0x9F8C05
764 #define AOT_COMMS_RCV_MORE 0x9F8C06
765 
766 class cCiSession {
767 private:
771 protected:
772  static int GetTag(int &Length, const uint8_t **Data);
773  static const uint8_t *GetData(const uint8_t *Data, int &Length);
774  int SendData(int Tag, int Length = 0, const uint8_t *Data = nullptr);
775  int SendData(int Tag, std::vector<uint8_t> &Data)
776  { return SendData(Tag, Data.size(), Data.data()); };
777 public:
779  virtual ~cCiSession() = default;
780  const cCiTransportConnection *Tc(void) { return m_tc; }
781  int SessionId(void) const { return m_sessionId; }
782  int ResourceId(void) const { return m_resourceId; }
783  virtual bool HasUserIO(void) { return false; }
784  virtual bool Process(int Length = 0, const uint8_t *Data = nullptr);
785  };
786 
787 cCiSession::cCiSession(int SessionId, int ResourceId, cCiTransportConnection *Tc)
788 {
791  m_tc = Tc;
792 }
793 
794 int cCiSession::GetTag(int &Length, const uint8_t **Data)
802 {
803  if (Length >= 3 && Data && *Data) {
804  int t = 0;
805  for (int i = 0; i < 3; i++)
806  t = (t << 8) | *(*Data)++;
807  Length -= 3;
808  return t;
809  }
810  return AOT_NONE;
811 }
812 
813 const uint8_t *cCiSession::GetData(const uint8_t *Data, int &Length)
814 {
815  Data = GetLength(Data, Length);
816  return Length ? Data : nullptr;
817 }
818 
819 int cCiSession::SendData(int Tag, int Length, const uint8_t *Data)
820 {
821  if (Length < 0)
822  {
823  esyslog("ERROR: CAM: data length (%d) is negative", Length);
824  return ERROR;
825  }
826 
827  if ((Length > 0) && !Data)
828  {
829  esyslog("ERROR: CAM: Data pointer null");
830  return ERROR;
831  }
832 
833  std::vector<uint8_t> buffer {
834  ST_SESSION_NUMBER, 0x02,
835  static_cast<uint8_t>((m_sessionId >> 8) & 0xFF),
836  static_cast<uint8_t>((m_sessionId ) & 0xFF),
837  static_cast<uint8_t>((Tag >> 16) & 0xFF),
838  static_cast<uint8_t>((Tag >> 8) & 0xFF),
839  static_cast<uint8_t>((Tag ) & 0xFF)} ;
840  buffer.reserve(2048);
841 
842  SetLength(buffer, Length);
843  if (buffer.size() + Length >= buffer.capacity())
844  {
845  esyslog("ERROR: CAM: data length (%d) exceeds buffer size", Length);
846  return ERROR;
847  }
848 
849  if (Length != 0)
850  {
851  buffer.insert(buffer.end(), Data, Data + Length);
852  }
853  return m_tc->SendData(buffer);
854 }
855 
856 bool cCiSession::Process(int Length, const uint8_t *Data)
857 {
858  (void)Length;
859  (void)Data;
860  return true;
861 }
862 
863 // -- cCiResourceManager -----------------------------------------------------
864 
866 private:
867  int m_state;
868 public:
870  bool Process(int Length = 0, const uint8_t *Data = nullptr) override; // cCiSession
871  };
872 
874 :cCiSession(SessionId, RI_RESOURCE_MANAGER, Tc)
875 {
876  dbgprotocol("New Resource Manager (session id %d)\n", SessionId);
877  m_state = 0;
878 }
879 
880 bool cCiResourceManager::Process(int Length, const uint8_t *Data)
881 {
882  if (Data) {
883  int Tag = GetTag(Length, &Data);
884  switch (Tag) {
885  case AOT_PROFILE_ENQ: {
886  dbgprotocol("%d: <== Profile Enquiry\n", SessionId());
887  const std::array<const uint32_t,5> resources
888  {
889  htonl(RI_RESOURCE_MANAGER),
892  htonl(RI_DATE_TIME),
893  htonl(RI_MMI)
894  };
895  dbgprotocol("%d: ==> Profile\n", SessionId());
896  SendData(AOT_PROFILE, resources.size() * sizeof(uint32_t),
897  reinterpret_cast<const uint8_t*>(resources.data()));
898  m_state = 3;
899  }
900  break;
901  case AOT_PROFILE: {
902  dbgprotocol("%d: <== Profile\n", SessionId());
903  if (m_state == 1) {
904  int l = 0;
905  const uint8_t *d = GetData(Data, l);
906  if (l > 0 && d)
907  esyslog("CI resource manager: unexpected data");
908  dbgprotocol("%d: ==> Profile Change\n", SessionId());
910  m_state = 2;
911  }
912  else {
913  esyslog("ERROR: CI resource manager: unexpected tag %06X in state %d", Tag, m_state);
914  }
915  }
916  break;
917  default: esyslog("ERROR: CI resource manager: unknown tag %06X", Tag);
918  return false;
919  }
920  }
921  else if (m_state == 0) {
922  dbgprotocol("%d: ==> Profile Enq\n", SessionId());
924  m_state = 1;
925  }
926  return true;
927 }
928 
929 // --- cCiApplicationInformation ---------------------------------------------
930 
932 private:
933  int m_state;
939 public:
941  ~cCiApplicationInformation() override;
942  bool Process(int Length = 0, const uint8_t *Data = nullptr) override; // cCiSession
943  bool EnterMenu(void);
944  char *GetApplicationString() { return strdup(m_menuString); };
947  };
948 
950 :cCiSession(SessionId, RI_APPLICATION_INFORMATION, Tc)
951 {
952  dbgprotocol("New Application Information (session id %d)\n", SessionId);
953  m_state = 0;
954  m_creationTime = time(nullptr);
955  m_applicationType = 0;
957  m_manufacturerCode = 0;
958  m_menuString = nullptr;
959 }
960 
962 {
963  free(m_menuString);
964 }
965 
966 bool cCiApplicationInformation::Process(int Length, const uint8_t *Data)
967 {
968  if (Data) {
969  int Tag = GetTag(Length, &Data);
970  switch (Tag) {
971  case AOT_APPLICATION_INFO: {
972  dbgprotocol("%d: <== Application Info\n", SessionId());
973  int l = 0;
974  const uint8_t *d = GetData(Data, l);
975  if ((l -= 1) < 0) break;
976  m_applicationType = *d++;
977  if ((l -= 2) < 0) break;
978  m_applicationManufacturer = ntohs(*(uint16_t *)d);
979  d += 2;
980  if ((l -= 2) < 0) break;
981  m_manufacturerCode = ntohs(*(uint16_t *)d);
982  d += 2;
983  free(m_menuString);
984  m_menuString = GetString(l, &d);
985  isyslog("CAM: %s, %02X, %04X, %04X", m_menuString, m_applicationType,
987  }
988  m_state = 2;
989  break;
990  default: esyslog("ERROR: CI application information: unknown tag %06X", Tag);
991  return false;
992  }
993  }
994  else if (m_state == 0) {
995  dbgprotocol("%d: ==> Application Info Enq\n", SessionId());
997  m_state = 1;
998  }
999  return true;
1000 }
1001 
1003 {
1004  if (m_state == 2 && time(nullptr) - m_creationTime > WRKRND_TIME_BEFORE_ENTER_MENU) {
1005  dbgprotocol("%d: ==> Enter Menu\n", SessionId());
1007  return true;//XXX
1008  }
1009  return false;
1010 }
1011 
1012 // --- cCiConditionalAccessSupport -------------------------------------------
1013 
1015 private:
1016  int m_state {0};
1018  bool m_needCaPmt {false};
1019 public:
1021  bool Process(int Length = 0, const uint8_t *Data = nullptr) override; // cCiSession
1023  bool SendPMT(const cCiCaPmt &CaPmt);
1024  bool NeedCaPmt(void) const { return m_needCaPmt; }
1025  };
1026 
1028  int SessionId, cCiTransportConnection *Tc) :
1030 {
1031  dbgprotocol("New Conditional Access Support (session id %d)\n", SessionId);
1032 }
1033 
1034 bool cCiConditionalAccessSupport::Process(int Length, const uint8_t *Data)
1035 {
1036  if (Data) {
1037  int Tag = GetTag(Length, &Data);
1038  switch (Tag) {
1039  case AOT_CA_INFO: {
1040  dbgprotocol("%d: <== Ca Info", SessionId());
1041  int l = 0;
1042  const uint8_t *d = GetData(Data, l);
1043  while (l > 1) {
1044  unsigned short id = ((unsigned short)(*d) << 8) | *(d + 1);
1045  dbgprotocol(" %04X", id);
1046  d += 2;
1047  l -= 2;
1048 
1049  // Make sure the id is not already present
1050  if (std::find(m_caSystemIds.cbegin(), m_caSystemIds.cend(), id)
1051  != m_caSystemIds.end())
1052  continue;
1053 
1054  // Insert before the last element.
1055  m_caSystemIds.emplace_back(id);
1056  }
1057 
1058  dbgprotocol("\n");
1059  }
1060  m_state = 2;
1061  m_needCaPmt = true;
1062  break;
1063  default: esyslog("ERROR: CI conditional access support: unknown tag %06X", Tag);
1064  return false;
1065  }
1066  }
1067  else if (m_state == 0) {
1068  dbgprotocol("%d: ==> Ca Info Enq\n", SessionId());
1070  m_state = 1;
1071  }
1072  return true;
1073 }
1074 
1076 {
1077  if (m_state == 2) {
1078  SendData(AOT_CA_PMT, CaPmt.m_length, CaPmt.m_capmt);
1079  m_needCaPmt = false;
1080  return true;
1081  }
1082  return false;
1083 }
1084 
1085 // --- cCiDateTime -----------------------------------------------------------
1086 
1087 class cCiDateTime : public cCiSession {
1088 private:
1090  time_t m_lastTime;
1092  bool SendDateTime(void);
1093 public:
1095  bool Process(int Length = 0, const uint8_t *Data = nullptr) override; // cCiSession
1096  void SetTimeOffset(double offset);
1097  };
1098 
1100 :cCiSession(SessionId, RI_DATE_TIME, Tc)
1101 {
1102  m_interval = 0;
1103  m_lastTime = 0;
1104  m_timeOffset = 0;
1105  dbgprotocol("New Date Time (session id %d)\n", SessionId);
1106 }
1107 
1108 void cCiDateTime::SetTimeOffset(double offset)
1109 {
1110  m_timeOffset = (int) offset;
1111  dbgprotocol("New Time Offset: %i secs\n", m_timeOffset);
1112 }
1113 
1115 {
1116  time_t t = time(nullptr);
1117  struct tm tm_gmt {};
1118  struct tm tm_loc {};
1119 
1120  // Avoid using signed time_t types
1121  if (m_timeOffset < 0)
1122  t -= (time_t)(-m_timeOffset);
1123  else
1124  t += (time_t)(m_timeOffset);
1125 
1126  if (gmtime_r(&t, &tm_gmt) && localtime_r(&t, &tm_loc)) {
1127  int Y = tm_gmt.tm_year;
1128  int M = tm_gmt.tm_mon + 1;
1129  int D = tm_gmt.tm_mday;
1130  int L = (M == 1 || M == 2) ? 1 : 0;
1131  int MJD = 14956 + D + int((Y - L) * 365.25) + int((M + 1 + L * 12) * 30.6001);
1132 #define DEC2BCD(d) (uint8_t((((d) / 10) << 4) + ((d) % 10)))
1133 #define BYTE0(a) static_cast<uint8_t>((a) & 0xFF)
1134 #define BYTE1(a) static_cast<uint8_t>(((a) >> 8) & 0xFF)
1135  uint16_t mjd = htons(MJD);
1136  int16_t local_offset = htons(tm_loc.tm_gmtoff / 60);
1137  std::vector<uint8_t> T {
1138  BYTE0(mjd),
1139  BYTE1(mjd),
1140  DEC2BCD(tm_gmt.tm_hour),
1141  DEC2BCD(tm_gmt.tm_min),
1142  DEC2BCD(tm_gmt.tm_sec),
1143  BYTE0(local_offset),
1144  BYTE1(local_offset)
1145  };
1146 
1147  dbgprotocol("%d: ==> Date Time\n", SessionId());
1148  SendData(AOT_DATE_TIME, T);
1149  //XXX return value of all SendData() calls???
1150  return true;
1151  }
1152  return false;
1153 }
1154 
1155 bool cCiDateTime::Process(int Length, const uint8_t *Data)
1156 {
1157  if (Data) {
1158  int Tag = GetTag(Length, &Data);
1159  switch (Tag) {
1160  case AOT_DATE_TIME_ENQ: {
1161  m_interval = 0;
1162  int l = 0;
1163  const uint8_t *d = GetData(Data, l);
1164  if (l > 0)
1165  m_interval = *d;
1166  dbgprotocol("%d: <== Date Time Enq, interval = %d\n", SessionId(), m_interval);
1167  m_lastTime = time(nullptr);
1168  return SendDateTime();
1169  }
1170  break;
1171  default: esyslog("ERROR: CI date time: unknown tag %06X", Tag);
1172  return false;
1173  }
1174  }
1175  else if (m_interval && time(nullptr) - m_lastTime > m_interval) {
1176  m_lastTime = time(nullptr);
1177  return SendDateTime();
1178  }
1179  return true;
1180 }
1181 
1182 // --- cCiMMI ----------------------------------------------------------------
1183 
1184 // Close MMI Commands:
1185 
1186 #define CLOSE_MMI_IMMEDIATE 0x00
1187 #define CLOSE_MMI_DELAY 0x01
1188 
1189 // Display Control Commands:
1190 
1191 #define DCC_SET_MMI_MODE 0x01
1192 #define DCC_DISPLAY_CHARACTER_TABLE_LIST 0x02
1193 #define DCC_INPUT_CHARACTER_TABLE_LIST 0x03
1194 #define DCC_OVERLAY_GRAPHICS_CHARACTERISTICS 0x04
1195 #define DCC_FULL_SCREEN_GRAPHICS_CHARACTERISTICS 0x05
1196 
1197 // MMI Modes:
1198 
1199 #define MM_HIGH_LEVEL 0x01
1200 #define MM_LOW_LEVEL_OVERLAY_GRAPHICS 0x02
1201 #define MM_LOW_LEVEL_FULL_SCREEN_GRAPHICS 0x03
1202 
1203 // Display Reply IDs:
1204 
1205 #define DRI_MMI_MODE_ACK 0x01
1206 #define DRI_LIST_DISPLAY_CHARACTER_TABLES 0x02
1207 #define DRI_LIST_INPUT_CHARACTER_TABLES 0x03
1208 #define DRI_LIST_GRAPHIC_OVERLAY_CHARACTERISTICS 0x04
1209 #define DRI_LIST_FULL_SCREEN_GRAPHIC_CHARACTERISTICS 0x05
1210 #define DRI_UNKNOWN_DISPLAY_CONTROL_CMD 0xF0
1211 #define DRI_UNKNOWN_MMI_MODE 0xF1
1212 #define DRI_UNKNOWN_CHARACTER_TABLE 0xF2
1213 
1214 // Enquiry Flags:
1215 
1216 #define EF_BLIND 0x01
1217 
1218 // Answer IDs:
1219 
1220 #define AI_CANCEL 0x00
1221 #define AI_ANSWER 0x01
1222 
1223 class cCiMMI : public cCiSession {
1224 private:
1225  char *GetText(int &Length, const uint8_t **Data);
1228 public:
1230  ~cCiMMI() override;
1231  bool Process(int Length = 0, const uint8_t *Data = nullptr) override; // cCiSession
1232  bool HasUserIO(void) override { return m_menu || m_enquiry; } // cCiSession
1233  cCiMenu *Menu(void);
1234  cCiEnquiry *Enquiry(void);
1235  bool SendMenuAnswer(uint8_t Selection);
1236  bool SendAnswer(const char *Text);
1237  };
1238 
1240 :cCiSession(SessionId, RI_MMI, Tc)
1241 {
1242  dbgprotocol("New MMI (session id %d)\n", SessionId);
1243  m_menu = nullptr;
1244  m_enquiry = nullptr;
1245 }
1246 
1248 {
1249  delete m_menu;
1250  delete m_enquiry;
1251 }
1252 
1253 char *cCiMMI::GetText(int &Length, const uint8_t **Data)
1261 {
1262  int Tag = GetTag(Length, Data);
1263  if (Tag == AOT_TEXT_LAST) {
1264  char *s = GetString(Length, Data);
1265  dbgprotocol("%d: <== Text Last '%s'\n", SessionId(), s);
1266  return s;
1267  }
1268  esyslog("CI MMI: unexpected text tag: %06X", Tag);
1269  return nullptr;
1270 }
1271 
1272 bool cCiMMI::Process(int Length, const uint8_t *Data)
1273 {
1274  if (Data) {
1275  int Tag = GetTag(Length, &Data);
1276  switch (Tag) {
1277  case AOT_DISPLAY_CONTROL: {
1278  dbgprotocol("%d: <== Display Control\n", SessionId());
1279  int l = 0;
1280  const uint8_t *d = GetData(Data, l);
1281  if (l > 0) {
1282  switch (*d) {
1283  case DCC_SET_MMI_MODE:
1284  if (l == 2 && *++d == MM_HIGH_LEVEL) {
1285  struct tDisplayReply { uint8_t m_id; uint8_t m_mode; };
1286  tDisplayReply dr {};
1287  dr.m_id = DRI_MMI_MODE_ACK;
1288  dr.m_mode = MM_HIGH_LEVEL;
1289  dbgprotocol("%d: ==> Display Reply\n", SessionId());
1290  SendData(AOT_DISPLAY_REPLY, 2, (uint8_t *)&dr);
1291  }
1292  break;
1293  default: esyslog("CI MMI: unsupported display control command %02X", *d);
1294  return false;
1295  }
1296  }
1297  }
1298  break;
1299  case AOT_LIST_LAST:
1300  case AOT_MENU_LAST: {
1301  dbgprotocol("%d: <== Menu Last\n", SessionId());
1302  delete m_menu;
1303  m_menu = new cCiMenu(this, Tag == AOT_MENU_LAST);
1304  int l = 0;
1305  const uint8_t *d = GetData(Data, l);
1306  if (l > 0) {
1307  // since the specification allows choiceNb to be undefined it is useless, so let's just skip it:
1308  d++;
1309  l--;
1310  if (l > 0) m_menu->m_titleText = GetText(l, &d);
1311  if (l > 0) m_menu->m_subTitleText = GetText(l, &d);
1312  if (l > 0) m_menu->m_bottomText = GetText(l, &d);
1313  while (l > 0) {
1314  char *s = GetText(l, &d);
1315  if (s) {
1316  if (!m_menu->AddEntry(s))
1317  free(s);
1318  }
1319  else
1320  break;
1321  }
1322  }
1323  }
1324  break;
1325  case AOT_ENQ: {
1326  dbgprotocol("%d: <== Enq\n", SessionId());
1327  delete m_enquiry;
1328  m_enquiry = new cCiEnquiry(this);
1329  int l = 0;
1330  const uint8_t *d = GetData(Data, l);
1331  if (l > 0) {
1332  uint8_t blind = *d++;
1333  //XXX GetByte()???
1334  l--;
1335  m_enquiry->m_blind = ((blind & EF_BLIND) != 0);
1337  l--;
1338  // I really wonder why there is no text length field here...
1339  m_enquiry->m_text = CopyString(l, d);
1340  }
1341  }
1342  break;
1343  case AOT_CLOSE_MMI: {
1344  int l = 0;
1345  const uint8_t *d = GetData(Data, l);
1346 
1347  if(l > 0){
1348  switch(*d){
1349  case CLOSE_MMI_IMMEDIATE:
1350  dbgprotocol("%d <== Menu Close: immediate\n", SessionId());
1351  break;
1352  case CLOSE_MMI_DELAY:
1353  dbgprotocol("%d <== Menu Close: delay\n", SessionId());
1354  break;
1355  default: esyslog("ERROR: CI MMI: unknown close_mmi_cmd_id %02X", *d);
1356  return false;
1357  }
1358  }
1359 
1360  break;
1361  }
1362  default: esyslog("ERROR: CI MMI: unknown tag %06X", Tag);
1363  return false;
1364  }
1365  }
1366  return true;
1367 }
1368 
1370 {
1371  cCiMenu *m = m_menu;
1372  m_menu = nullptr;
1373  return m;
1374 }
1375 
1377 {
1378  cCiEnquiry *e = m_enquiry;
1379  m_enquiry = nullptr;
1380  return e;
1381 }
1382 
1383 bool cCiMMI::SendMenuAnswer(uint8_t Selection)
1384 {
1385  dbgprotocol("%d: ==> Menu Answ\n", SessionId());
1386  SendData(AOT_MENU_ANSW, 1, &Selection);
1387  //XXX return value of all SendData() calls???
1388  return true;
1389 }
1390 
1391 // Define protocol structure
1392 extern "C" {
1393  struct tAnswer { uint8_t m_id; char m_text[256]; };
1394 }
1395 
1396 bool cCiMMI::SendAnswer(const char *Text)
1397 {
1398  dbgprotocol("%d: ==> Answ\n", SessionId());
1399  tAnswer answer {};
1400  answer.m_id = Text ? AI_ANSWER : AI_CANCEL;
1401  if (Text) {
1402  strncpy(answer.m_text, Text, sizeof(answer.m_text) - 1);
1403  answer.m_text[255] = '\0';
1404  }
1405  SendData(AOT_ANSW, Text ? strlen(Text) + 1 : 1, (uint8_t *)&answer);
1406  //XXX return value of all SendData() calls???
1407  return true;
1408 }
1409 
1410 // --- cCiMenu ---------------------------------------------------------------
1411 
1412 cCiMenu::cCiMenu(cCiMMI *MMI, bool Selectable)
1413 {
1414  m_mmi = MMI;
1416 }
1417 
1419 {
1420  free(m_titleText);
1421  free(m_subTitleText);
1422  free(m_bottomText);
1423  for (int i = 0; i < m_numEntries; i++)
1424  free(m_entries[i]);
1425 }
1426 
1427 bool cCiMenu::AddEntry(char *s)
1428 {
1430  m_entries[m_numEntries++] = s;
1431  return true;
1432  }
1433  return false;
1434 }
1435 
1436 bool cCiMenu::Select(int Index)
1437 {
1438  if (m_mmi && -1 <= Index && Index < m_numEntries)
1439  return m_mmi->SendMenuAnswer(Index + 1);
1440  return false;
1441 }
1442 
1444 {
1445  return Select(-1);
1446 }
1447 
1448 // --- cCiEnquiry ------------------------------------------------------------
1449 
1451 {
1452  free(m_text);
1453 }
1454 
1455 bool cCiEnquiry::Reply(const char *s)
1456 {
1457  return m_mmi ? m_mmi->SendAnswer(s) : false;
1458 }
1459 
1461 {
1462  return Reply(nullptr);
1463 }
1464 
1465 // --- cCiCaPmt --------------------------------------------------------------
1466 
1467 // Ca Pmt Cmd Ids:
1468 
1469 #define CPCI_OK_DESCRAMBLING 0x01
1470 #define CPCI_OK_MMI 0x02
1471 #define CPCI_QUERY 0x03
1472 #define CPCI_NOT_SELECTED 0x04
1473 
1474 cCiCaPmt::cCiCaPmt(int ProgramNumber, uint8_t cplm)
1475 {
1476  m_capmt[m_length++] = cplm; // ca_pmt_list_management
1477  m_capmt[m_length++] = (ProgramNumber >> 8) & 0xFF;
1478  m_capmt[m_length++] = ProgramNumber & 0xFF;
1479  m_capmt[m_length++] = 0x01; // version_number, current_next_indicator - apparently vn doesn't matter, but cni must be 1
1480 
1481  // program_info_length
1483  m_capmt[m_length++] = 0x00;
1484  m_capmt[m_length++] = 0x00;
1485 }
1486 
1488 {
1489  if (m_length + 5 > int(sizeof(m_capmt)))
1490  {
1491  esyslog("ERROR: buffer overflow in CA_PMT");
1492  return;
1493  }
1494 
1495  m_capmt[m_length++] = type & 0xFF;
1496  m_capmt[m_length++] = (pid >> 8) & 0xFF;
1497  m_capmt[m_length++] = pid & 0xFF;
1498 
1499  // ES_info_length
1501  m_capmt[m_length++] = 0x00;
1502  m_capmt[m_length++] = 0x00;
1503 }
1504 
1522 void cCiCaPmt::AddCaDescriptor(int ca_system_id, int ca_pid, int data_len,
1523  const uint8_t *data)
1524 {
1525  if (!m_infoLengthPos)
1526  {
1527  esyslog("ERROR: adding CA descriptor without program/stream!");
1528  return;
1529  }
1530 
1531  if (m_length + data_len + 7 > int(sizeof(m_capmt)))
1532  {
1533  esyslog("ERROR: buffer overflow in CA_PMT");
1534  return;
1535  }
1536 
1537  // We are either at start of program descriptors or stream descriptors.
1538  if (m_infoLengthPos + 2 == m_length)
1539  m_capmt[m_length++] = CPCI_OK_DESCRAMBLING; // ca_pmt_cmd_id
1540 
1541  m_capmt[m_length++] = 0x09; // CA descriptor tag
1542  m_capmt[m_length++] = 4 + data_len; // descriptor length
1543 
1544  m_capmt[m_length++] = (ca_system_id >> 8) & 0xFF;
1545  m_capmt[m_length++] = ca_system_id & 0xFF;
1546  m_capmt[m_length++] = (ca_pid >> 8) & 0xFF;
1547  m_capmt[m_length++] = ca_pid & 0xFF;
1548 
1549  if (data_len > 0)
1550  {
1551  memcpy(&m_capmt[m_length], data, data_len);
1552  m_length += data_len;
1553  }
1554 
1555  // update program_info_length/ES_info_length
1556  int l = m_length - m_infoLengthPos - 2;
1557  m_capmt[m_infoLengthPos] = (l >> 8) & 0xFF;
1558  m_capmt[m_infoLengthPos + 1] = l & 0xFF;
1559 }
1560 
1561 // -- cLlCiHandler -------------------------------------------------------------
1562 
1563 cLlCiHandler::cLlCiHandler(int Fd, int NumSlots)
1564 {
1565  m_numSlots = NumSlots;
1566  m_tpl = new cCiTransportLayer(Fd, m_numSlots);
1567  m_fdCa = Fd;
1568 }
1569 
1571 {
1572  cMutexLock MutexLock(&m_mutex);
1573  for (auto & session : m_sessions)
1574  delete session;
1575  delete m_tpl;
1576  close(m_fdCa);
1577 }
1578 
1580 {
1581  int fd_ca = open(FileName, O_RDWR);
1582  if (fd_ca >= 0)
1583  {
1584  ca_caps_t Caps;
1585  if (ioctl(fd_ca, CA_GET_CAP, &Caps) == 0)
1586  {
1587  int NumSlots = Caps.slot_num;
1588  if (NumSlots > 0)
1589  {
1590  if (Caps.slot_type & CA_CI_LINK)
1591  return new cLlCiHandler(fd_ca, NumSlots);
1592  if (Caps.slot_type & CA_CI)
1593  return new cHlCiHandler(fd_ca, NumSlots);
1594  isyslog("CAM doesn't support either high or low level CI,"
1595  " Caps.slot_type=%i", Caps.slot_type);
1596  }
1597  else
1598  esyslog("ERROR: no CAM slots found");
1599  }
1600  else
1601  LOG_ERROR_STR(FileName);
1602  close(fd_ca);
1603  }
1604  return nullptr;
1605 }
1606 
1607 int cLlCiHandler::ResourceIdToInt(const uint8_t *Data)
1608 {
1609  return (ntohl(*(int *)Data));
1610 }
1611 
1612 bool cLlCiHandler::Send(uint8_t Tag, int SessionId, int ResourceId, int Status)
1613 {
1614  std::vector<uint8_t> buffer {Tag, 0x00} ; // 0x00 will be replaced with length
1615  if (Status >= 0)
1616  buffer.push_back(Status);
1617  if (ResourceId) {
1618  buffer.push_back((ResourceId >> 24) & 0xFF);
1619  buffer.push_back((ResourceId >> 16) & 0xFF);
1620  buffer.push_back((ResourceId >> 8) & 0xFF);
1621  buffer.push_back( ResourceId & 0xFF);
1622  }
1623  buffer.push_back((SessionId >> 8) & 0xFF);
1624  buffer.push_back( SessionId & 0xFF);
1625  buffer[1] = buffer.size() - 2; // length
1626  return m_tc && m_tc->SendData(buffer) == OK;
1627 }
1628 
1630 {
1631  for (auto & session : m_sessions) {
1632  if (session && session->SessionId() == SessionId)
1633  return session;
1634  }
1635  return nullptr;
1636 }
1637 
1639 {
1640  for (auto & session : m_sessions) {
1641  if (session && session->Tc()->Slot() == Slot && session->ResourceId() == ResourceId)
1642  return session;
1643  }
1644  return nullptr;
1645 }
1646 
1648 {
1649  if (!GetSessionByResourceId(ResourceId, m_tc->Slot())) {
1650  for (int i = 0; i < MAX_CI_SESSION; i++) {
1651  if (!m_sessions[i]) {
1652  switch (ResourceId) {
1653  case RI_RESOURCE_MANAGER: return m_sessions[i] = new cCiResourceManager(i + 1, m_tc);
1656  return m_sessions[i] = new cCiConditionalAccessSupport(i + 1, m_tc);
1657  case RI_HOST_CONTROL: break; //XXX
1658  case RI_DATE_TIME: return m_sessions[i] = new cCiDateTime(i + 1, m_tc);
1659  case RI_MMI: return m_sessions[i] = new cCiMMI(i + 1, m_tc);
1660  }
1661  }
1662  }
1663  }
1664  return nullptr;
1665 }
1666 
1667 bool cLlCiHandler::OpenSession(int Length, const uint8_t *Data)
1668 {
1669  if (Length == 6 && *(Data + 1) == 0x04) {
1670  int ResourceId = ResourceIdToInt(Data + 2);
1671  dbgprotocol("OpenSession %08X\n", ResourceId);
1672  switch (ResourceId) {
1673  case RI_RESOURCE_MANAGER:
1676  case RI_HOST_CONTROL:
1677  case RI_DATE_TIME:
1678  case RI_MMI:
1679  {
1680  cCiSession *Session = CreateSession(ResourceId);
1681  if (Session)
1682  {
1684  Session->ResourceId(), SS_OK);
1685  return true;
1686  }
1687  esyslog("ERROR: can't create session for resource identifier: %08X",
1688  ResourceId);
1689  break;
1690  }
1691  default: esyslog("ERROR: unknown resource identifier: %08X", ResourceId);
1692  }
1693  }
1694  return false;
1695 }
1696 
1697 bool cLlCiHandler::CloseSession(int SessionId)
1698 {
1699  dbgprotocol("CloseSession %08X\n", SessionId);
1700  cCiSession *Session = GetSessionBySessionId(SessionId);
1701  if (Session && m_sessions[SessionId - 1] == Session) {
1702  delete Session;
1703  m_sessions[SessionId - 1] = nullptr;
1704  Send(ST_CLOSE_SESSION_RESPONSE, SessionId, 0, SS_OK);
1705  return true;
1706  }
1707 
1708  esyslog("ERROR: unknown session id: %d", SessionId);
1710  return false;
1711 }
1712 
1714 {
1715  int result = 0;
1716  for (auto & session : m_sessions) {
1717  if (session && session->Tc()->Slot() == Slot) {
1718  CloseSession(session->SessionId());
1719  result++;
1720  }
1721  }
1722  return result;
1723 }
1724 
1726 {
1727  bool result = true;
1728  cMutexLock MutexLock(&m_mutex);
1729 
1730  for (int Slot = 0; Slot < m_numSlots; Slot++)
1731  {
1732  m_tc = m_tpl->Process(Slot);
1733  if (m_tc)
1734  {
1735  int Length = 0;
1736  const uint8_t *Data = m_tc->Data(Length);
1737  if (Data && Length > 1)
1738  {
1739  switch (*Data)
1740  {
1741  case ST_SESSION_NUMBER:
1742  if (Length > 4)
1743  {
1744  int SessionId = ntohs(*(short *)&Data[2]);
1745  cCiSession *Session = GetSessionBySessionId(SessionId);
1746  if (Session)
1747  {
1748  Session->Process(Length - 4, Data + 4);
1749  if (Session->ResourceId() == RI_APPLICATION_INFORMATION)
1750  {
1751 #if 0
1752  esyslog("Test: %x",
1753  ((cCiApplicationInformation*)Session)->GetApplicationManufacturer());
1754 #endif
1755  }
1756  }
1757  else
1758  esyslog("ERROR: unknown session id: %d", SessionId);
1759  }
1760  break;
1761 
1763  OpenSession(Length, Data);
1764  break;
1765 
1767  if (Length == 4)
1768  CloseSession(ntohs(*(short *)&Data[2]));
1769  break;
1770 
1771  case ST_CREATE_SESSION_RESPONSE: //XXX fall through to default
1772  case ST_CLOSE_SESSION_RESPONSE: //XXX fall through to default
1773  default:
1774  esyslog("ERROR: unknown session tag: %02X", *Data);
1775  }
1776  }
1777  }
1778  else if (CloseAllSessions(Slot))
1779  {
1780  m_tpl->ResetSlot(Slot);
1781  result = false;
1782  }
1783  else if (m_tpl->ModuleReady(Slot))
1784  {
1785  dbgprotocol("Module ready in slot %d\n", Slot);
1786  m_tpl->NewConnection(Slot);
1787  }
1788  }
1789 
1790  bool UserIO = false;
1791  m_needCaPmt = false;
1792  for (auto & session : m_sessions)
1793  {
1794  if (session && session->Process())
1795  {
1796  UserIO |= session->HasUserIO();
1797  if (session->ResourceId() == RI_CONDITIONAL_ACCESS_SUPPORT)
1798  {
1799  auto *cas = dynamic_cast<cCiConditionalAccessSupport *>(session);
1800  if (cas == nullptr)
1801  continue;
1802  m_needCaPmt |= cas->NeedCaPmt();
1803  }
1804  }
1805  }
1806  m_hasUserIO = UserIO;
1807 
1808  if (m_newCaSupport)
1809  m_newCaSupport = result = false; // triggers new SetCaPmt at caller!
1810  return result;
1811 }
1812 
1814 {
1815  cMutexLock MutexLock(&m_mutex);
1817  return api ? api->EnterMenu() : false;
1818 }
1819 
1821 {
1822  cMutexLock MutexLock(&m_mutex);
1823  for (int Slot = 0; Slot < m_numSlots; Slot++) {
1824  auto *mmi = dynamic_cast<cCiMMI *>(GetSessionByResourceId(RI_MMI, Slot));
1825  if (mmi)
1826  return mmi->Menu();
1827  }
1828  return nullptr;
1829 }
1830 
1832 {
1833  cMutexLock MutexLock(&m_mutex);
1834  for (int Slot = 0; Slot < m_numSlots; Slot++) {
1835  auto *mmi = dynamic_cast<cCiMMI *>(GetSessionByResourceId(RI_MMI, Slot));
1836  if (mmi)
1837  return mmi->Enquiry();
1838  }
1839  return nullptr;
1840 }
1841 
1843  {
1844  static dvbca_vector empty {};
1845  cMutexLock MutexLock(&m_mutex);
1847  return cas ? cas->GetCaSystemIds() : empty;
1848 }
1849 
1850 bool cLlCiHandler::SetCaPmt(cCiCaPmt &CaPmt, int Slot)
1851 {
1852  cMutexLock MutexLock(&m_mutex);
1854  return cas && cas->SendPMT(CaPmt);
1855 }
1856 
1857 void cLlCiHandler::SetTimeOffset(double offset_in_seconds)
1858 {
1859  cMutexLock MutexLock(&m_mutex);
1860  cCiDateTime *dt = nullptr;
1861 
1862  for (uint i = 0; i < (uint) NumSlots(); i++)
1863  {
1864  dt = dynamic_cast<cCiDateTime*>(GetSessionByResourceId(RI_DATE_TIME, i));
1865  if (dt)
1866  dt->SetTimeOffset(offset_in_seconds);
1867  }
1868 }
1869 
1870 bool cLlCiHandler::Reset(int Slot)
1871 {
1872  cMutexLock MutexLock(&m_mutex);
1873  CloseAllSessions(Slot);
1874  return m_tpl->ResetSlot(Slot);
1875 }
1876 
1878 {
1879  return sConnected;
1880 }
1881 
1882 // -- cHlCiHandler -------------------------------------------------------------
1883 
1884 cHlCiHandler::cHlCiHandler(int Fd, int NumSlots)
1885 {
1886  m_numSlots = NumSlots;
1887  m_fdCa = Fd;
1888  esyslog("New High level CI handler");
1889 }
1890 
1892 {
1893  cMutexLock MutexLock(&m_mutex);
1894  close(m_fdCa);
1895 }
1896 
1897 int cHlCiHandler::CommHL(unsigned tag, unsigned function, struct ca_msg *msg) const
1898 {
1899  if (tag) {
1900  msg->msg[2] = tag & 0xff;
1901  msg->msg[1] = (tag & 0xff00) >> 8;
1902  msg->msg[0] = (tag & 0xff0000) >> 16;
1903  esyslog("Sending message=[%02x %02x %02x ]",
1904  msg->msg[0], msg->msg[1], msg->msg[2]);
1905  }
1906 
1907  return ioctl(m_fdCa, function, msg);
1908 }
1909 
1910 int cHlCiHandler::GetData(unsigned tag, struct ca_msg *msg)
1911 {
1912  return CommHL(tag, CA_GET_MSG, msg);
1913 }
1914 
1915 int cHlCiHandler::SendData(unsigned tag, struct ca_msg *msg)
1916 {
1917  return CommHL(tag, CA_SEND_MSG, msg);
1918 }
1919 
1921 {
1922  cMutexLock MutexLock(&m_mutex);
1923 
1924  struct ca_msg msg {};
1925  switch(m_state) {
1926  case 0:
1927  // Get CA_system_ids
1928  /* Enquire */
1929  if ((SendData(AOT_CA_INFO_ENQ, &msg)) < 0) {
1930  esyslog("HLCI communication failed");
1931  } else {
1932  dbgprotocol("==> Ca Info Enquiry");
1933  /* Receive */
1934  if ((GetData(AOT_CA_INFO, &msg)) < 0) {
1935  esyslog("HLCI communication failed");
1936  } else {
1937  QString message("Debug: ");
1938  for(int i = 0; i < 20; i++) {
1939  message += QString("%1 ").arg(msg.msg[i]);
1940  }
1941  LOG(VB_GENERAL, LOG_DEBUG, message);
1942  dbgprotocol("<== Ca Info");
1943  int l = msg.msg[3];
1944  const uint8_t *d = &msg.msg[4];
1945  while (l > 1) {
1946  unsigned short id = ((unsigned short)(*d) << 8) | *(d + 1);
1947  dbgprotocol(" %04X", id);
1948  d += 2;
1949  l -= 2;
1950 
1951  // Insert before the last element.
1952  m_caSystemIds.emplace_back(id);
1953  }
1954  dbgprotocol("\n");
1955  }
1956  m_state = 1;
1957  break;
1958  }
1959  }
1960 
1961  bool result = true;
1962 
1963  return result;
1964 }
1965 
1966 bool cHlCiHandler::EnterMenu(int /*Slot*/)
1967 {
1968  return false;
1969 }
1970 
1972 {
1973  return nullptr;
1974 }
1975 
1977 {
1978  return nullptr;
1979 }
1980 
1982 {
1983  return m_caSystemIds;
1984 }
1985 
1986 bool cHlCiHandler::SetCaPmt(cCiCaPmt &CaPmt, int /*Slot*/)
1987 {
1988  cMutexLock MutexLock(&m_mutex);
1989  struct ca_msg msg {};
1990 
1991  esyslog("Setting CA PMT.");
1992  m_state = 2;
1993 
1994  msg.msg[3] = CaPmt.m_length;
1995 
1996  if (CaPmt.m_length > (256 - 4))
1997  {
1998  esyslog("CA message too long");
1999  return false;
2000  }
2001 
2002  memcpy(&msg.msg[4], CaPmt.m_capmt, CaPmt.m_length);
2003 
2004  if ((SendData(AOT_CA_PMT, &msg)) < 0) {
2005  esyslog("HLCI communication failed");
2006  return false;
2007  }
2008 
2009  return true;
2010 }
2011 
2012 bool cHlCiHandler::Reset(int /*Slot*/) const
2013 {
2014  if ((ioctl(m_fdCa, CA_RESET)) < 0) {
2015  esyslog("ioctl CA_RESET failed.");
2016  return false;
2017  }
2018  return true;
2019 }
2020 
2022 {
2023  return m_state == 1;
2024 }
cCiDateTime::m_lastTime
time_t m_lastTime
Definition: dvbci.cpp:1090
cCiConditionalAccessSupport::m_state
int m_state
Definition: dvbci.cpp:1016
cCiConditionalAccessSupport
Definition: dvbci.cpp:1014
cCiApplicationInformation::~cCiApplicationInformation
~cCiApplicationInformation() override
Definition: dvbci.cpp:961
T_RCV
#define T_RCV
Definition: dvbci.cpp:251
cCiApplicationInformation::GetManufacturerCode
uint16_t GetManufacturerCode() const
Definition: dvbci.cpp:946
cLlCiHandler::SetTimeOffset
void SetTimeOffset(double offset_in_seconds) override
Definition: dvbci.cpp:1857
dvbci.h
cTPDU::cTPDU
cTPDU(void)=default
cLlCiHandler::connected
static bool connected()
Definition: dvbci.cpp:1877
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:1522
T_TC_ERROR
#define T_TC_ERROR
Definition: dvbci.cpp:258
ST_CREATE_SESSION_RESPONSE
#define ST_CREATE_SESSION_RESPONSE
Definition: dvbci.cpp:697
tAnswer::m_text
char m_text[256]
Definition: dvbci.cpp:1393
BYTE0
#define BYTE0(a)
cLlCiHandler::GetSessionBySessionId
cCiSession * GetSessionBySessionId(int SessionId)
Definition: dvbci.cpp:1629
cCiTransportConnection::~cCiTransportConnection
~cCiTransportConnection()
Definition: dvbci.cpp:428
AOT_LIST_LAST
#define AOT_LIST_LAST
Definition: dvbci.cpp:746
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:737
cLlCiHandler::CloseAllSessions
int CloseAllSessions(int Slot)
Definition: dvbci.cpp:1713
cCiSession::GetTag
static int GetTag(int &Length, const uint8_t **Data)
Definition: dvbci.cpp:794
cCiConditionalAccessSupport::m_needCaPmt
bool m_needCaPmt
Definition: dvbci.cpp:1018
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:949
T_NEW_TC
#define T_NEW_TC
Definition: dvbci.cpp:257
RI_MMI
#define RI_MMI
Definition: dvbci.cpp:713
cCiTransportConnection::m_slot
uint8_t m_slot
Definition: dvbci.cpp:396
AOT_NONE
#define AOT_NONE
Definition: dvbci.cpp:717
DRI_MMI_MODE_ACK
#define DRI_MMI_MODE_ACK
Definition: dvbci.cpp:1205
cCiConditionalAccessSupport::m_caSystemIds
dvbca_vector m_caSystemIds
Definition: dvbci.cpp:1017
cHlCiHandler::cHlCiHandler
cHlCiHandler(int Fd, int NumSlots)
Definition: dvbci.cpp:1884
cCiMMI
Definition: dvbci.cpp:1223
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:720
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:596
cLlCiHandler::NumSlots
int NumSlots(void) override
Definition: dvbci.h:187
cCiTransportConnection::SendData
int SendData(int Length, const uint8_t *Data)
Definition: dvbci.cpp:511
cHlCiHandler::GetData
int GetData(unsigned tag, struct ca_msg *msg)
Definition: dvbci.cpp:1910
cCiMenu::cCiMenu
cCiMenu(cCiMMI *MMI, bool Selectable)
Definition: dvbci.cpp:1412
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:1460
ST_OPEN_SESSION_REQUEST
#define ST_OPEN_SESSION_REQUEST
Definition: dvbci.cpp:694
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:1220
cLlCiHandler::m_tc
cCiTransportConnection * m_tc
Definition: dvbci.h:173
cLlCiHandler::GetEnquiry
cCiEnquiry * GetEnquiry(void) override
Definition: dvbci.cpp:1831
cCiCaPmt
Definition: dvbci.h:126
cHlCiHandler::SendData
int SendData(unsigned tag, struct ca_msg *msg)
Definition: dvbci.cpp:1915
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:23
cLlCiHandler::Process
bool Process(void) override
Definition: dvbci.cpp:1725
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:734
cCiMenu::m_mmi
cCiMMI * m_mmi
Definition: dvbci.h:79
RI_HOST_CONTROL
#define RI_HOST_CONTROL
Definition: dvbci.cpp:711
AOT_CA_PMT
#define AOT_CA_PMT
Definition: dvbci.cpp:726
AOT_ENQ
#define AOT_ENQ
Definition: dvbci.cpp:741
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:597
cCiEnquiry::Reply
bool Reply(const char *s)
Definition: dvbci.cpp:1455
cMutexLock
Definition: dvbci.h:62
cCiTransportLayer::Process
cCiTransportConnection * Process(int Slot)
Definition: dvbci.cpp:650
AOT_PROFILE
#define AOT_PROFILE
Definition: dvbci.cpp:719
cHlCiHandler::~cHlCiHandler
~cHlCiHandler() override
Definition: dvbci.cpp:1891
cCiMMI::cCiMMI
cCiMMI(int SessionId, cCiTransportConnection *Tc)
Definition: dvbci.cpp:1239
cCiApplicationInformation::m_applicationManufacturer
uint16_t m_applicationManufacturer
Definition: dvbci.cpp:936
cLlCiHandler::m_mutex
cMutex m_mutex
Definition: dvbci.h:165
AOT_DATE_TIME_ENQ
#define AOT_DATE_TIME_ENQ
Definition: dvbci.cpp:732
cCiTransportConnection::m_tcid
uint8_t m_tcid
Definition: dvbci.cpp:397
AOT_APPLICATION_INFO
#define AOT_APPLICATION_INFO
Definition: dvbci.cpp:722
cCiDateTime::m_timeOffset
int m_timeOffset
Definition: dvbci.cpp:1091
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:880
cHlCiHandler::Process
bool Process(void) override
Definition: dvbci.cpp:1920
CPCI_OK_DESCRAMBLING
#define CPCI_OK_DESCRAMBLING
Definition: dvbci.cpp:1469
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:1570
CLOSE_MMI_DELAY
#define CLOSE_MMI_DELAY
Definition: dvbci.cpp:1187
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:708
cMutex
Definition: dvbci.h:49
cHlCiHandler::EnterMenu
bool EnterMenu(int Slot) override
Definition: dvbci.cpp:1966
cCiApplicationInformation::m_menuString
char * m_menuString
Definition: dvbci.cpp:938
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:591
cCiTransportLayer::ModuleReady
bool ModuleReady(int Slot) const
Definition: dvbci.cpp:640
mythlogging.h
RI_CONDITIONAL_ACCESS_SUPPORT
#define RI_CONDITIONAL_ACCESS_SUPPORT
Definition: dvbci.cpp:710
RI_APPLICATION_INFORMATION
#define RI_APPLICATION_INFORMATION
Definition: dvbci.cpp:709
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:1396
AOT_APPLICATION_INFO_ENQ
#define AOT_APPLICATION_INFO_ENQ
Definition: dvbci.cpp:721
AI_ANSWER
#define AI_ANSWER
Definition: dvbci.cpp:1221
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:695
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:781
cTPDU::Tcid
uint8_t Tcid(void)
Definition: dvbci.cpp:271
cCiEnquiry::~cCiEnquiry
~cCiEnquiry()
Definition: dvbci.cpp:1450
cCiResourceManager
Definition: dvbci.cpp:865
cLlCiHandler::GetCaSystemIds
dvbca_vector GetCaSystemIds(int Slot) override
Definition: dvbci.cpp:1842
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:813
cCiMenu::Select
bool Select(int Index)
Definition: dvbci.cpp:1436
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:698
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:1393
cCiTransportConnection::SendData
int SendData(std::vector< uint8_t > &Data)
Definition: dvbci.cpp:416
AOT_ANSW
#define AOT_ANSW
Definition: dvbci.cpp:742
cCiTransportConnection::Poll
int Poll(void)
Definition: dvbci.cpp:570
cLlCiHandler::Send
bool Send(uint8_t Tag, int SessionId, int ResourceId=0, int Status=-1)
Definition: dvbci.cpp:1612
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:1226
cLlCiHandler::m_hasUserIO
bool m_hasUserIO
Definition: dvbci.h:169
CLOSE_MMI_IMMEDIATE
#define CLOSE_MMI_IMMEDIATE
Definition: dvbci.cpp:1186
cCiTransportConnection::m_dataAvailable
bool m_dataAvailable
Definition: dvbci.cpp:402
cCiConditionalAccessSupport::GetCaSystemIds
dvbca_vector GetCaSystemIds(void)
Definition: dvbci.cpp:1022
cLlCiHandler::CloseSession
bool CloseSession(int SessionId)
Definition: dvbci.cpp:1697
cLlCiHandler
Definition: dvbci.h:162
AOT_DISPLAY_REPLY
#define AOT_DISPLAY_REPLY
Definition: dvbci.cpp:736
cMutexLock::Lock
bool Lock(cMutex *Mutex)
Definition: dvbci.cpp:230
cCiDateTime::SetTimeOffset
void SetTimeOffset(double offset)
Definition: dvbci.cpp:1108
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:1647
cCiApplicationInformation::GetApplicationManufacturer
uint16_t GetApplicationManufacturer() const
Definition: dvbci.cpp:945
cCiTransportLayer
Definition: dvbci.cpp:593
cCiSession::cCiSession
cCiSession(int SessionId, int ResourceId, cCiTransportConnection *Tc)
Definition: dvbci.cpp:787
cCiTransportConnection::Data
const uint8_t * Data(int &Length)
Definition: dvbci.cpp:535
cCiMenu::~cCiMenu
~cCiMenu()
Definition: dvbci.cpp:1418
AOT_ENTER_MENU
#define AOT_ENTER_MENU
Definition: dvbci.cpp:723
cCiApplicationInformation::GetApplicationString
char * GetApplicationString()
Definition: dvbci.cpp:944
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:856
uint
unsigned int uint
Definition: compat.h:140
cCiResourceManager::cCiResourceManager
cCiResourceManager(int SessionId, cCiTransportConnection *Tc)
Definition: dvbci.cpp:873
cCiSession::Tc
const cCiTransportConnection * Tc(void)
Definition: dvbci.cpp:780
cCiMenu::m_entries
char * m_entries[MAX_CIMENU_ENTRIES]
Definition: dvbci.h:84
cCiApplicationInformation::m_manufacturerCode
uint16_t m_manufacturerCode
Definition: dvbci.cpp:937
cTPDU::Tag
uint8_t Tag(void)
Definition: dvbci.cpp:272
cHlCiHandler::Reset
bool Reset(int Slot) const
Definition: dvbci.cpp:2012
cCiConditionalAccessSupport::SendPMT
bool SendPMT(const cCiCaPmt &CaPmt)
Definition: dvbci.cpp:1075
cHlCiHandler::CommHL
int CommHL(unsigned tag, unsigned function, struct ca_msg *msg) const
Definition: dvbci.cpp:1897
cHlCiHandler::NumSlots
int NumSlots(void) override
Definition: dvbci.h:220
cCiTransportLayer::ResetSlot
bool ResetSlot(int Slot) const
Definition: dvbci.cpp:628
ST_SESSION_NUMBER
#define ST_SESSION_NUMBER
Definition: dvbci.cpp:693
cCiConditionalAccessSupport::Process
bool Process(int Length=0, const uint8_t *Data=nullptr) override
Definition: dvbci.cpp:1034
cCiMMI::HasUserIO
bool HasUserIO(void) override
Definition: dvbci.cpp:1232
cTPDU::m_size
int m_size
Definition: dvbci.cpp:264
AOT_PROFILE_ENQ
#define AOT_PROFILE_ENQ
Definition: dvbci.cpp:718
cCiApplicationInformation::Process
bool Process(int Length=0, const uint8_t *Data=nullptr) override
Definition: dvbci.cpp:966
dsyslog
#define dsyslog(a...)
Definition: dvbci.cpp:56
cCiApplicationInformation::EnterMenu
bool EnterMenu(void)
Definition: dvbci.cpp:1002
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:775
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:1383
AOT_DISPLAY_CONTROL
#define AOT_DISPLAY_CONTROL
Definition: dvbci.cpp:735
cCiApplicationInformation
Definition: dvbci.cpp:931
cCiSession
Definition: dvbci.cpp:766
DCC_SET_MMI_MODE
#define DCC_SET_MMI_MODE
Definition: dvbci.cpp:1191
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:1247
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:1981
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:1155
cMutex::m_lockingPid
pid_t m_lockingPid
Definition: dvbci.h:53
cCiMMI::m_enquiry
cCiEnquiry * m_enquiry
Definition: dvbci.cpp:1227
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:614
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:1087
cCiTransportConnection::LastResponse
int LastResponse(void) const
Definition: dvbci.cpp:409
cCiTransportLayer::m_fd
int m_fd
Definition: dvbci.cpp:595
cLlCiHandler::cLlCiHandler
cLlCiHandler(int Fd, int NumSlots)
Definition: dvbci.cpp:1563
cCiTransportLayer::cCiTransportLayer
cCiTransportLayer(int Fd, int NumSlots)
Definition: dvbci.cpp:606
POLL_INTERVAL
static constexpr std::chrono::milliseconds POLL_INTERVAL
Definition: dvbci.cpp:568
ST_CLOSE_SESSION_RESPONSE
#define ST_CLOSE_SESSION_RESPONSE
Definition: dvbci.cpp:699
cCiMMI::GetText
char * GetText(int &Length, const uint8_t **Data)
Definition: dvbci.cpp:1253
AOT_MENU_LAST
#define AOT_MENU_LAST
Definition: dvbci.cpp:743
AOT_CA_INFO
#define AOT_CA_INFO
Definition: dvbci.cpp:725
cTPDU::Data
const uint8_t * Data(int &Length)
Definition: dvbci.cpp:273
gmtime_r
static __inline struct tm * gmtime_r(const time_t *timep, struct tm *result)
Definition: compat.h:270
cHlCiHandler::NeedCaPmt
bool NeedCaPmt(void) override
Definition: dvbci.cpp:2021
cCiSession::m_tc
cCiTransportConnection * m_tc
Definition: dvbci.cpp:770
AOT_CA_INFO_ENQ
#define AOT_CA_INFO_ENQ
Definition: dvbci.cpp:724
cCiEnquiry::m_text
char * m_text
Definition: dvbci.h:104
cLlCiHandler::Reset
bool Reset(int Slot)
Definition: dvbci.cpp:1870
cLlCiHandler::m_needCaPmt
bool m_needCaPmt
Definition: dvbci.h:170
cLlCiHandler::EnterMenu
bool EnterMenu(int Slot) override
Definition: dvbci.cpp:1813
cLlCiHandler::ResourceIdToInt
static int ResourceIdToInt(const uint8_t *Data)
Definition: dvbci.cpp:1607
sDumpTPDUDataTransfer
static bool sDumpTPDUDataTransfer
Definition: dvbci.cpp:63
EF_BLIND
#define EF_BLIND
Definition: dvbci.cpp:1216
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:1579
AOT_DATE_TIME
#define AOT_DATE_TIME
Definition: dvbci.cpp:733
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:933
cCiMenu
Definition: dvbci.h:75
cCiSession::m_resourceId
int m_resourceId
Definition: dvbci.cpp:769
cTPDU::Read
int Read(int fd)
Definition: dvbci.cpp:334
tAnswer
Definition: dvbci.cpp:1393
cCiEnquiry
Definition: dvbci.h:100
cCiSession::HasUserIO
virtual bool HasUserIO(void)
Definition: dvbci.cpp:783
cCiDateTime::SendDateTime
bool SendDateTime(void)
Definition: dvbci.cpp:1114
cCiSession::m_sessionId
int m_sessionId
Definition: dvbci.cpp:768
cHlCiHandler::m_mutex
cMutex m_mutex
Definition: dvbci.h:208
cCiResourceManager::m_state
int m_state
Definition: dvbci.cpp:867
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:745
cCiTransportConnection::State
eState State(void)
Definition: dvbci.cpp:408
TIMEOUT
#define TIMEOUT
Definition: dvbci.cpp:70
cCiTransportConnection::CreateConnection
int CreateConnection(void)
Definition: dvbci.cpp:542
cCiApplicationInformation::m_applicationType
uint8_t m_applicationType
Definition: dvbci.cpp:935
cLlCiHandler::GetSessionByResourceId
cCiSession * GetSessionByResourceId(int ResourceId, int Slot)
Definition: dvbci.cpp:1638
cCiConditionalAccessSupport::cCiConditionalAccessSupport
cCiConditionalAccessSupport(int SessionId, cCiTransportConnection *Tc)
Definition: dvbci.cpp:1027
cCiTransportConnection::m_fd
int m_fd
Definition: dvbci.cpp:395
cCiCaPmt::cCiCaPmt
cCiCaPmt(int ProgramNumber, uint8_t cplm=CPLM_ONLY)
Definition: dvbci.cpp:1474
cCiMMI::Menu
cCiMenu * Menu(void)
Definition: dvbci.cpp:1369
MAX_CONNECT_RETRIES
#define MAX_CONNECT_RETRIES
Definition: dvbci.cpp:540
cCiConditionalAccessSupport::NeedCaPmt
bool NeedCaPmt(void) const
Definition: dvbci.cpp:1024
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:1820
cLlCiHandler::m_newCaSupport
bool m_newCaSupport
Definition: dvbci.h:168
cCiTransportConnection::RecvData
int RecvData(void)
Definition: dvbci.cpp:528
cCiSession::ResourceId
int ResourceId(void) const
Definition: dvbci.cpp:782
cCiEnquiry::m_expectedLength
int m_expectedLength
Definition: dvbci.h:106
cCiDateTime::m_interval
int m_interval
Definition: dvbci.cpp:1089
cCiHandler
Definition: dvbci.h:146
cCiMenu::AddEntry
bool AddEntry(char *s)
Definition: dvbci.cpp:1427
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:712
cCiApplicationInformation::m_creationTime
time_t m_creationTime
Definition: dvbci.cpp:934
cCiMMI::Process
bool Process(int Length=0, const uint8_t *Data=nullptr) override
Definition: dvbci.cpp:1272
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:819
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:1199
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:1667
SS_OK
#define SS_OK
Definition: dvbci.cpp:703
cCiMenu::Cancel
bool Cancel(void)
Definition: dvbci.cpp:1443
cCiMMI::Enquiry
cCiEnquiry * Enquiry(void)
Definition: dvbci.cpp:1376
cHlCiHandler::GetMenu
cCiMenu * GetMenu(void) override
Definition: dvbci.cpp:1971
localtime_r
static __inline struct tm * localtime_r(const time_t *timep, struct tm *result)
Definition: compat.h:287
SS_NOT_ALLOCATED
#define SS_NOT_ALLOCATED
Definition: dvbci.cpp:704
cHlCiHandler::GetEnquiry
cCiEnquiry * GetEnquiry(void) override
Definition: dvbci.cpp:1976
cCiCaPmt::AddElementaryStream
void AddElementaryStream(int type, int pid)
Definition: dvbci.cpp:1487
cCiDateTime::cCiDateTime
cCiDateTime(int SessionId, cCiTransportConnection *Tc)
Definition: dvbci.cpp:1099
T_DATA_MORE
#define T_DATA_MORE
Definition: dvbci.cpp:260