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