MythTV  master
dvbci.h
Go to the documentation of this file.
1 /*
2  * ci.hh: 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 #ifndef DVBCI_H
28 #define DVBCI_H
29 
30 #if HAVE_STDINT_H
31 #include <cstdint>
32 #endif
33 
34 #include <cstdio>
35 
36 #include <pthread.h>
37 #include <sys/types.h>
38 #include <sys/socket.h>
39 #include <netinet/in.h>
40 #include <sys/un.h>
41 #include <sys/stat.h>
42 #include <sys/uio.h>
43 
44 #define MAXCASYSTEMIDS 64
45 
46 class cMutex {
47  friend class cCondVar;
48 private:
49  pthread_mutex_t m_mutex {};
50  pid_t m_lockingPid {0};
51  int m_locked {0};
52 public:
53  cMutex(void) { pthread_mutex_init(&m_mutex, nullptr); }
54  ~cMutex() { pthread_mutex_destroy(&m_mutex); }
55  void Lock(void);
56  void Unlock(void);
57  };
58 
59 class cMutexLock {
60 private:
61  cMutex *m_mutex {nullptr};
62  bool m_locked {false};
63 public:
64  explicit cMutexLock(cMutex *Mutex = nullptr) { Lock(Mutex); }
65  ~cMutexLock();
66  bool Lock(cMutex *Mutex);
67  };
68 
69 
70 class cCiMMI;
71 
72 class cCiMenu {
73  friend class cCiMMI;
74 private:
75  enum { MAX_CIMENU_ENTRIES = 64 };
76  cCiMMI *m_mmi {nullptr};
78  char *m_titleText {nullptr};
79  char *m_subTitleText {nullptr};
80  char *m_bottomText {nullptr};
82  int m_numEntries {0};
83  bool AddEntry(char *s);
84  cCiMenu(cCiMMI *MMI, bool Selectable);
85 public:
86  ~cCiMenu();
87  const char *TitleText(void) { return m_titleText; }
88  const char *SubTitleText(void) { return m_subTitleText; }
89  const char *BottomText(void) { return m_bottomText; }
90  const char *Entry(int n) { return n < m_numEntries ? m_entries[n] : nullptr; }
91  int NumEntries(void) const { return m_numEntries; }
92  bool Selectable(void) const { return m_selectable; }
93  bool Select(int Index);
94  bool Cancel(void);
95  };
96 
97 class cCiEnquiry {
98  friend class cCiMMI;
99 private:
100  cCiMMI *m_mmi {nullptr};
101  char *m_text {nullptr};
102  bool m_blind {false};
104  explicit cCiEnquiry(cCiMMI *MMI) : m_mmi(MMI) {}
105 public:
106  ~cCiEnquiry();
107  const char *Text(void) { return m_text; }
108  bool Blind(void) const { return m_blind; }
109  int ExpectedLength(void) const { return m_expectedLength; }
110  bool Reply(const char *s);
111  bool Cancel(void);
112  };
113 
114 // Ca Pmt List Management:
115 
116 #define CPLM_MORE 0x00
117 #define CPLM_FIRST 0x01
118 #define CPLM_LAST 0x02
119 #define CPLM_ONLY 0x03
120 #define CPLM_ADD 0x04
121 #define CPLM_UPDATE 0x05
122 
123 class cCiCaPmt {
125  friend class cHlCiHandler;
126 private:
127  int m_length {0};
129  uint8_t m_capmt[2048] {0};
130 public:
131  explicit cCiCaPmt(int ProgramNumber, uint8_t cplm = CPLM_ONLY);
132  void AddElementaryStream(int type, int pid);
133  void AddCaDescriptor(int ca_system_id, int ca_pid, int data_len,
134  const uint8_t *data);
135  };
136 
137 #define MAX_CI_SESSION 16 //XXX
138 
139 class cCiSession;
140 class cCiTransportLayer;
142 
143 class cCiHandler {
144 public:
145  virtual ~cCiHandler() = default;
146  static cCiHandler *CreateCiHandler(const char *FileName);
147  virtual int NumSlots(void) = 0;
148  virtual bool Process(void) = 0;
149  virtual bool HasUserIO(void) = 0;
150  virtual bool NeedCaPmt(void) = 0;
151  virtual bool EnterMenu(int Slot) = 0;
152  virtual cCiMenu *GetMenu(void) = 0;
153  virtual cCiEnquiry *GetEnquiry(void) = 0;
154  virtual const unsigned short *GetCaSystemIds(int Slot) = 0;
155  virtual bool SetCaPmt(cCiCaPmt &CaPmt, int Slot) = 0;
156  virtual void SetTimeOffset(double /*offset_in_seconds*/) { }
157  };
158 
159 class cLlCiHandler : public cCiHandler {
160  friend class cCiHandler;
161 private:
163  int m_fdCa;
165  bool m_newCaSupport {false};
166  bool m_hasUserIO {false};
167  bool m_needCaPmt {false};
171  static int ResourceIdToInt(const uint8_t *Data);
172  bool Send(uint8_t Tag, int SessionId, int ResourceId = 0, int Status = -1);
173  cCiSession *GetSessionBySessionId(int SessionId);
174  cCiSession *GetSessionByResourceId(int ResourceId, int Slot);
175  cCiSession *CreateSession(int ResourceId);
176  bool OpenSession(int Length, const uint8_t *Data);
177  bool CloseSession(int SessionId);
178  int CloseAllSessions(int Slot);
179  cLlCiHandler(int Fd, int NumSlots);
180 public:
181  ~cLlCiHandler() override;
182  cLlCiHandler(const cLlCiHandler &) = delete; // not copyable
183  cLlCiHandler &operator=(const cLlCiHandler &) = delete; // not copyable
184  int NumSlots(void) override // cCiHandler
185  { return m_numSlots; }
186  bool Process(void) override; // cCiHandler
187  bool HasUserIO(void) override // cCiHandler
188  { return m_hasUserIO; }
189  bool NeedCaPmt(void) override // cCiHandler
190  { return m_needCaPmt; }
191  bool EnterMenu(int Slot) override; // cCiHandler
192  cCiMenu *GetMenu(void) override; // cCiHandler
193  cCiEnquiry *GetEnquiry(void) override; // cCiHandler
194  bool SetCaPmt(cCiCaPmt &CaPmt);
195  const unsigned short *GetCaSystemIds(int Slot) override; // cCiHandler
196  bool SetCaPmt(cCiCaPmt &CaPmt, int Slot) override; // cCiHandler
197  void SetTimeOffset(double offset_in_seconds) override; // cCiHandler
198  bool Reset(int Slot);
199  static bool connected();
200  };
201 
202 class cHlCiHandler : public cCiHandler {
203  friend class cCiHandler;
204  private:
206  int m_fdCa;
208  int m_state {0};
210  unsigned short m_caSystemIds[MAXCASYSTEMIDS + 1] {0}; // list is zero terminated!
211  cHlCiHandler(int Fd, int NumSlots);
212  int CommHL(unsigned tag, unsigned function, struct ca_msg *msg) const;
213  int GetData(unsigned tag, struct ca_msg *msg);
214  int SendData(unsigned tag, struct ca_msg *msg);
215  public:
216  ~cHlCiHandler() override;
217  int NumSlots(void) override // cCiHandler
218  { return m_numSlots; }
219  bool Process(void) override; // cCiHandler
220  bool HasUserIO(void) override { return false; } // cCiHandler
221  bool NeedCaPmt(void) override; // cCiHandler
222  bool EnterMenu(int Slot) override; // cCiHandler
223  cCiMenu *GetMenu(void) override; // cCiHandler
224  cCiEnquiry *GetEnquiry(void) override; // cCiHandler
225  bool SetCaPmt(cCiCaPmt &CaPmt);
226  const unsigned short *GetCaSystemIds(int Slot) override; // cCiHandler
227  bool SetCaPmt(cCiCaPmt &CaPmt, int Slot) override; // cCiHandler
228  bool Reset(int Slot) const;
229  bool connected() const;
230 };
231 
232 int tcp_listen(struct sockaddr_in *name,int sckt,unsigned long address=INADDR_ANY);
233 int accept_tcp(int ip_sock,struct sockaddr_in *ip_name);
234 int udp_listen(struct sockaddr_un *name,char const * filename);
235 int accept_udp(int ip_sock,struct sockaddr_un *ip_name);
236 
237 #endif // DVBCI_H
cCiConditionalAccessSupport
Definition: dvbci.cpp:988
cLlCiHandler::NeedCaPmt
bool NeedCaPmt(void) override
Definition: dvbci.h:189
cLlCiHandler::SetTimeOffset
void SetTimeOffset(double offset_in_seconds) override
Definition: dvbci.cpp:1833
accept_tcp
int accept_tcp(int ip_sock, struct sockaddr_in *ip_name)
cLlCiHandler::connected
static bool connected()
Definition: dvbci.cpp:1853
cLlCiHandler::HasUserIO
bool HasUserIO(void) override
Definition: dvbci.h:187
cCiMenu::NumEntries
int NumEntries(void) const
Definition: dvbci.h:91
cCiCaPmt::AddCaDescriptor
void AddCaDescriptor(int ca_system_id, int ca_pid, int data_len, const uint8_t *data)
Definition: dvbci.cpp:1498
cLlCiHandler::operator=
cLlCiHandler & operator=(const cLlCiHandler &)=delete
cCiHandler::EnterMenu
virtual bool EnterMenu(int Slot)=0
cLlCiHandler::GetSessionBySessionId
cCiSession * GetSessionBySessionId(int SessionId)
Definition: dvbci.cpp:1606
cLlCiHandler::CloseAllSessions
int CloseAllSessions(int Slot)
Definition: dvbci.cpp:1690
cCiHandler::SetCaPmt
virtual bool SetCaPmt(cCiCaPmt &CaPmt, int Slot)=0
cCiMenu::TitleText
const char * TitleText(void)
Definition: dvbci.h:87
cHlCiHandler::cHlCiHandler
cHlCiHandler(int Fd, int NumSlots)
Definition: dvbci.cpp:1860
cCiMMI
Definition: dvbci.cpp:1203
cMutex::m_mutex
pthread_mutex_t m_mutex
Definition: dvbci.h:49
cHlCiHandler::SetCaPmt
bool SetCaPmt(cCiCaPmt &CaPmt)
cLlCiHandler::NumSlots
int NumSlots(void) override
Definition: dvbci.h:184
cHlCiHandler::GetData
int GetData(unsigned tag, struct ca_msg *msg)
Definition: dvbci.cpp:1886
cCiHandler::GetEnquiry
virtual cCiEnquiry * GetEnquiry(void)=0
cCiMenu::cCiMenu
cCiMenu(cCiMMI *MMI, bool Selectable)
Definition: dvbci.cpp:1388
cHlCiHandler::connected
bool connected() const
cCiCaPmt::m_length
int m_length
Definition: dvbci.h:127
cCiEnquiry::Cancel
bool Cancel(void)
Definition: dvbci.cpp:1436
cCiEnquiry::Text
const char * Text(void)
Definition: dvbci.h:107
cLlCiHandler::m_tc
cCiTransportConnection * m_tc
Definition: dvbci.h:170
cHlCiHandler::m_caSystemIds
unsigned short m_caSystemIds[MAXCASYSTEMIDS+1]
Definition: dvbci.h:210
cCiEnquiry::Blind
bool Blind(void) const
Definition: dvbci.h:108
cLlCiHandler::GetEnquiry
cCiEnquiry * GetEnquiry(void) override
Definition: dvbci.cpp:1808
cCiCaPmt
Definition: dvbci.h:123
cHlCiHandler::SendData
int SendData(unsigned tag, struct ca_msg *msg)
Definition: dvbci.cpp:1891
cLlCiHandler::Process
bool Process(void) override
Definition: dvbci.cpp:1702
cMutexLock::~cMutexLock
~cMutexLock()
Definition: dvbci.cpp:202
cCiMenu::m_mmi
cCiMMI * m_mmi
Definition: dvbci.h:76
cMutex::cCondVar
friend class cCondVar
Definition: dvbci.h:47
cCiHandler::Process
virtual bool Process(void)=0
cCiEnquiry::Reply
bool Reply(const char *s)
Definition: dvbci.cpp:1431
cMutexLock
Definition: dvbci.h:59
cMutexLock::cMutexLock
cMutexLock(cMutex *Mutex=nullptr)
Definition: dvbci.h:64
cHlCiHandler::~cHlCiHandler
~cHlCiHandler() override
Definition: dvbci.cpp:1867
cLlCiHandler::m_mutex
cMutex m_mutex
Definition: dvbci.h:162
cCiCaPmt::m_capmt
uint8_t m_capmt[2048]
XXX is there a specified maximum?
Definition: dvbci.h:129
cHlCiHandler::Process
bool Process(void) override
Definition: dvbci.cpp:1896
cCiMenu::Selectable
bool Selectable(void) const
Definition: dvbci.h:92
cLlCiHandler::m_numSlots
int m_numSlots
Definition: dvbci.h:164
cLlCiHandler::~cLlCiHandler
~cLlCiHandler() override
Definition: dvbci.cpp:1546
cLlCiHandler::cLlCiHandler
cLlCiHandler(const cLlCiHandler &)=delete
cCiMenu::m_subTitleText
char * m_subTitleText
Definition: dvbci.h:79
cCiHandler::SetTimeOffset
virtual void SetTimeOffset(double)
Definition: dvbci.h:156
cLlCiHandler::m_sessions
cCiSession * m_sessions[MAX_CI_SESSION]
Definition: dvbci.h:168
cHlCiHandler::m_state
int m_state
Definition: dvbci.h:208
cMutex
Definition: dvbci.h:46
cHlCiHandler::EnterMenu
bool EnterMenu(int Slot) override
Definition: dvbci.cpp:1945
cMutex::m_locked
int m_locked
Definition: dvbci.h:51
cCiTransportConnection
Definition: dvbci.cpp:370
cCiMenu::BottomText
const char * BottomText(void)
Definition: dvbci.h:89
cHlCiHandler::HasUserIO
bool HasUserIO(void) override
Definition: dvbci.h:220
cMutexLock::m_locked
bool m_locked
Definition: dvbci.h:62
cCiEnquiry::m_blind
bool m_blind
Definition: dvbci.h:102
cCiHandler::NumSlots
virtual int NumSlots(void)=0
cLlCiHandler::SetCaPmt
bool SetCaPmt(cCiCaPmt &CaPmt)
cCiEnquiry::~cCiEnquiry
~cCiEnquiry()
Definition: dvbci.cpp:1426
cCiHandler::NeedCaPmt
virtual bool NeedCaPmt(void)=0
cLlCiHandler::m_fdCa
int m_fdCa
Definition: dvbci.h:163
cCiMenu::Select
bool Select(int Index)
Definition: dvbci.cpp:1412
MAX_CI_SESSION
#define MAX_CI_SESSION
Definition: dvbci.h:137
cHlCiHandler
Definition: dvbci.h:202
cCiMenu::m_bottomText
char * m_bottomText
Definition: dvbci.h:80
cCiMenu::m_titleText
char * m_titleText
Definition: dvbci.h:78
cLlCiHandler::Send
bool Send(uint8_t Tag, int SessionId, int ResourceId=0, int Status=-1)
Definition: dvbci.cpp:1588
cMutex::Unlock
void Unlock(void)
Definition: dvbci.cpp:189
cMutexLock::m_mutex
cMutex * m_mutex
Definition: dvbci.h:61
filename
QString filename
Definition: mythplugins/mytharchive/mytharchivehelper/main.cpp:634
cLlCiHandler::m_hasUserIO
bool m_hasUserIO
Definition: dvbci.h:166
MAXCASYSTEMIDS
#define MAXCASYSTEMIDS
Definition: dvbci.h:44
cLlCiHandler::CloseSession
bool CloseSession(int SessionId)
Definition: dvbci.cpp:1674
cLlCiHandler
Definition: dvbci.h:159
cMutexLock::Lock
bool Lock(cMutex *Mutex)
Definition: dvbci.cpp:208
cMutex::Lock
void Lock(void)
Definition: dvbci.cpp:180
cLlCiHandler::CreateSession
cCiSession * CreateSession(int ResourceId)
Definition: dvbci.cpp:1624
cCiHandler::HasUserIO
virtual bool HasUserIO(void)=0
cCiTransportLayer
Definition: dvbci.cpp:573
cCiMenu::~cCiMenu
~cCiMenu()
Definition: dvbci.cpp:1394
cCiMenu::m_entries
char * m_entries[MAX_CIMENU_ENTRIES]
Definition: dvbci.h:81
cHlCiHandler::Reset
bool Reset(int Slot) const
Definition: dvbci.cpp:1991
accept_udp
int accept_udp(int ip_sock, struct sockaddr_un *ip_name)
cHlCiHandler::CommHL
int CommHL(unsigned tag, unsigned function, struct ca_msg *msg) const
Definition: dvbci.cpp:1873
cHlCiHandler::NumSlots
int NumSlots(void) override
Definition: dvbci.h:217
tcp_listen
int tcp_listen(struct sockaddr_in *name, int sckt, unsigned long address=INADDR_ANY)
cCiHandler::~cCiHandler
virtual ~cCiHandler()=default
cMutex::~cMutex
~cMutex()
Definition: dvbci.h:54
cCiMenu::m_selectable
bool m_selectable
Definition: dvbci.h:77
cHlCiHandler::m_fdCa
int m_fdCa
Definition: dvbci.h:206
cHlCiHandler::m_numSlots
int m_numSlots
Definition: dvbci.h:207
cHlCiHandler::GetCaSystemIds
const unsigned short * GetCaSystemIds(int Slot) override
Definition: dvbci.cpp:1960
cCiSession
Definition: dvbci.cpp:746
cLlCiHandler::m_tpl
cCiTransportLayer * m_tpl
Definition: dvbci.h:169
cCiHandler::GetCaSystemIds
virtual const unsigned short * GetCaSystemIds(int Slot)=0
cMutex::m_lockingPid
pid_t m_lockingPid
Definition: dvbci.h:50
udp_listen
int udp_listen(struct sockaddr_un *name, char const *filename)
cCiEnquiry::m_mmi
cCiMMI * m_mmi
Definition: dvbci.h:100
cCiMenu::MAX_CIMENU_ENTRIES
@ MAX_CIMENU_ENTRIES
Definition: dvbci.h:75
cLlCiHandler::cLlCiHandler
cLlCiHandler(int Fd, int NumSlots)
Definition: dvbci.cpp:1539
cMutex::cMutex
cMutex(void)
Definition: dvbci.h:53
cHlCiHandler::NeedCaPmt
bool NeedCaPmt(void) override
Definition: dvbci.cpp:2000
cCiEnquiry::cCiEnquiry
cCiEnquiry(cCiMMI *MMI)
Definition: dvbci.h:104
cCiEnquiry::m_text
char * m_text
Definition: dvbci.h:101
cLlCiHandler::Reset
bool Reset(int Slot)
Definition: dvbci.cpp:1846
cLlCiHandler::m_needCaPmt
bool m_needCaPmt
Definition: dvbci.h:167
cLlCiHandler::GetCaSystemIds
const unsigned short * GetCaSystemIds(int Slot) override
Definition: dvbci.cpp:1819
cLlCiHandler::EnterMenu
bool EnterMenu(int Slot) override
Definition: dvbci.cpp:1790
cLlCiHandler::ResourceIdToInt
static int ResourceIdToInt(const uint8_t *Data)
Definition: dvbci.cpp:1583
cCiHandler::CreateCiHandler
static cCiHandler * CreateCiHandler(const char *FileName)
Definition: dvbci.cpp:1555
CPLM_ONLY
#define CPLM_ONLY
Definition: dvbci.h:119
cCiMenu
Definition: dvbci.h:72
cCiEnquiry::ExpectedLength
int ExpectedLength(void) const
Definition: dvbci.h:109
cCiHandler::GetMenu
virtual cCiMenu * GetMenu(void)=0
cCiEnquiry
Definition: dvbci.h:97
cHlCiHandler::m_mutex
cMutex m_mutex
Definition: dvbci.h:205
cCiCaPmt::m_infoLengthPos
int m_infoLengthPos
Definition: dvbci.h:128
cLlCiHandler::GetSessionByResourceId
cCiSession * GetSessionByResourceId(int ResourceId, int Slot)
Definition: dvbci.cpp:1615
cCiCaPmt::cCiCaPmt
cCiCaPmt(int ProgramNumber, uint8_t cplm=CPLM_ONLY)
Definition: dvbci.cpp:1450
cLlCiHandler::GetMenu
cCiMenu * GetMenu(void) override
Definition: dvbci.cpp:1797
cLlCiHandler::m_newCaSupport
bool m_newCaSupport
Definition: dvbci.h:165
cCiEnquiry::m_expectedLength
int m_expectedLength
Definition: dvbci.h:103
cCiHandler
Definition: dvbci.h:143
cCiMenu::AddEntry
bool AddEntry(char *s)
Definition: dvbci.cpp:1403
cCiMenu::SubTitleText
const char * SubTitleText(void)
Definition: dvbci.h:88
cCiMenu::m_numEntries
int m_numEntries
Definition: dvbci.h:82
cLlCiHandler::OpenSession
bool OpenSession(int Length, const uint8_t *Data)
Definition: dvbci.cpp:1644
cCiMenu::Cancel
bool Cancel(void)
Definition: dvbci.cpp:1419
cHlCiHandler::GetMenu
cCiMenu * GetMenu(void) override
Definition: dvbci.cpp:1950
cHlCiHandler::GetEnquiry
cCiEnquiry * GetEnquiry(void) override
Definition: dvbci.cpp:1955
cHlCiHandler::m_numCaSystemIds
int m_numCaSystemIds
Definition: dvbci.h:209
cCiMenu::Entry
const char * Entry(int n)
Definition: dvbci.h:90
cCiCaPmt::AddElementaryStream
void AddElementaryStream(int type, int pid)
Definition: dvbci.cpp:1463