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 __CI_H
28 #define __CI_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) { return m_numEntries; }
92  bool Selectable(void) { 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) { return m_blind; }
109  int ExpectedLength(void) { 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];
130 public:
131  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  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  cLlCiHandler(const cLlCiHandler &) = delete; // not copyable
181  cLlCiHandler &operator=(const cLlCiHandler &) = delete; // not copyable
182 public:
183  virtual ~cLlCiHandler();
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  bool connected() const;
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]; // list is zero terminated!
211  cHlCiHandler(int Fd, int NumSlots);
212  int CommHL(unsigned tag, unsigned function, struct ca_msg *msg);
213  int GetData(unsigned tag, struct ca_msg *msg);
214  int SendData(unsigned tag, struct ca_msg *msg);
215  public:
216  virtual ~cHlCiHandler();
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);
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 * const filename);
235 int accept_udp(int ip_sock,struct sockaddr_un *ip_name);
236 
237 #endif //__CI_H
bool m_hasUserIO
Definition: dvbci.h:166
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
int accept_tcp(int ip_sock, struct sockaddr_in *ip_name)
bool m_blind
Definition: dvbci.h:102
virtual int NumSlots(void)=0
bool Process(void) override
Definition: dvbci.cpp:1711
cMutex m_mutex
Definition: dvbci.h:162
static cCiHandler * CreateCiHandler(const char *FileName)
Definition: dvbci.cpp:1564
int m_infoLengthPos
Definition: dvbci.h:128
int m_numEntries
Definition: dvbci.h:82
int GetData(unsigned tag, struct ca_msg *msg)
Definition: dvbci.cpp:1894
int SendData(unsigned tag, struct ca_msg *msg)
Definition: dvbci.cpp:1899
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
int udp_listen(struct sockaddr_un *name, char const *const filename)
uint8_t m_capmt[2048]
XXX is there a specified maximum?
Definition: dvbci.h:129
char * m_text
Definition: dvbci.h:101
char * m_titleText
Definition: dvbci.h:78
bool Lock(cMutex *Mutex)
Definition: dvbci.cpp:208
virtual bool NeedCaPmt(void)=0
cCiSession * m_sessions[MAX_CI_SESSION]
Definition: dvbci.h:168
bool NeedCaPmt(void) override
Definition: dvbci.h:189
#define MAXCASYSTEMIDS
Definition: dvbci.h:44
cCiSession * GetSessionBySessionId(int SessionId)
Definition: dvbci.cpp:1615
bool Reset(int Slot)
Definition: dvbci.cpp:1853
int CommHL(unsigned tag, unsigned function, struct ca_msg *msg)
Definition: dvbci.cpp:1881
bool Selectable(void)
Definition: dvbci.h:92
virtual ~cCiHandler()=default
virtual bool HasUserIO(void)=0
bool EnterMenu(int Slot) override
Definition: dvbci.cpp:1797
pthread_mutex_t m_mutex
Definition: dvbci.h:49
bool Cancel(void)
Definition: dvbci.cpp:1425
char * m_subTitleText
Definition: dvbci.h:79
bool NeedCaPmt(void) override
Definition: dvbci.cpp:2008
pid_t m_lockingPid
Definition: dvbci.h:50
cLlCiHandler(int Fd, int NumSlots)
Definition: dvbci.cpp:1545
#define MAX_CI_SESSION
Definition: dvbci.h:137
void AddCaDescriptor(int ca_system_id, int ca_pid, int data_len, const uint8_t *data)
Definition: dvbci.cpp:1504
const unsigned short * GetCaSystemIds(int Slot) override
Definition: dvbci.cpp:1826
cCiMMI * m_mmi
Definition: dvbci.h:100
~cMutex()
Definition: dvbci.h:54
cCiCaPmt(int ProgramNumber, uint8_t cplm=CPLM_ONLY)
Definition: dvbci.cpp:1456
bool connected() const
Definition: dvbci.cpp:1860
virtual bool EnterMenu(int Slot)=0
~cCiMenu()
Definition: dvbci.cpp:1400
virtual cCiEnquiry * GetEnquiry(void)=0
char * m_bottomText
Definition: dvbci.h:80
bool HasUserIO(void) override
Definition: dvbci.h:220
cCiEnquiry(cCiMMI *MMI)
Definition: dvbci.h:104
virtual bool SetCaPmt(cCiCaPmt &CaPmt, int Slot)=0
virtual const unsigned short * GetCaSystemIds(int Slot)=0
bool Cancel(void)
Definition: dvbci.cpp:1442
const char * Text(void)
Definition: dvbci.h:107
cCiSession * GetSessionByResourceId(int ResourceId, int Slot)
Definition: dvbci.cpp:1624
cCiEnquiry * GetEnquiry(void) override
Definition: dvbci.cpp:1963
bool Process(void) override
Definition: dvbci.cpp:1904
virtual cCiMenu * GetMenu(void)=0
cCiMenu * GetMenu(void) override
Definition: dvbci.cpp:1804
bool HasUserIO(void) override
Definition: dvbci.h:187
friend class cCondVar
Definition: dvbci.h:47
unsigned short m_caSystemIds[MAXCASYSTEMIDS+1]
Definition: dvbci.h:210
bool Reply(const char *s)
Definition: dvbci.cpp:1437
int m_expectedLength
Definition: dvbci.h:103
bool SetCaPmt(cCiCaPmt &CaPmt)
cLlCiHandler & operator=(const cLlCiHandler &)=delete
const char * name
Definition: ParseText.cpp:328
int tcp_listen(struct sockaddr_in *name, int sckt, unsigned long address=INADDR_ANY)
Definition: dvbci.h:46
bool m_needCaPmt
Definition: dvbci.h:167
bool m_selectable
Definition: dvbci.h:77
int CloseAllSessions(int Slot)
Definition: dvbci.cpp:1699
virtual void SetTimeOffset(double)
Definition: dvbci.h:156
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
cCiEnquiry * GetEnquiry(void) override
Definition: dvbci.cpp:1815
bool connected() const
int ExpectedLength(void)
Definition: dvbci.h:109
int NumSlots(void) override
Definition: dvbci.h:184
#define CPLM_ONLY
Definition: dvbci.h:119
bool CloseSession(int SessionId)
Definition: dvbci.cpp:1683
~cMutexLock()
Definition: dvbci.cpp:202
~cCiEnquiry()
Definition: dvbci.cpp:1432
cHlCiHandler(int Fd, int NumSlots)
Definition: dvbci.cpp:1867
Definition: dvbci.h:72
const char * Entry(int n)
Definition: dvbci.h:90
int m_length
Definition: dvbci.h:127
cCiTransportConnection * m_tc
Definition: dvbci.h:170
int NumEntries(void)
Definition: dvbci.h:91
void AddElementaryStream(int type, int pid)
Definition: dvbci.cpp:1469
const unsigned short * GetCaSystemIds(int Slot) override
Definition: dvbci.cpp:1968
const char * SubTitleText(void)
Definition: dvbci.h:88
int m_fdCa
Definition: dvbci.h:206
int m_numSlots
Definition: dvbci.h:164
void SetTimeOffset(double offset_in_seconds) override
Definition: dvbci.cpp:1840
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
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)
const char * BottomText(void)
Definition: dvbci.h:89
int accept_udp(int ip_sock, struct sockaddr_un *ip_name)
int m_locked
Definition: dvbci.h:51
virtual bool Process(void)=0
cCiTransportLayer * m_tpl
Definition: dvbci.h:169
cCiMMI * m_mmi
Definition: dvbci.h:76
cMutexLock(cMutex *Mutex=nullptr)
Definition: dvbci.h:64
bool EnterMenu(int Slot) override
Definition: dvbci.cpp:1953
int ResourceIdToInt(const uint8_t *Data)
Definition: dvbci.cpp:1592
const char * TitleText(void)
Definition: dvbci.h:87
bool AddEntry(char *s)
Definition: dvbci.cpp:1409
int m_fdCa
Definition: dvbci.h:163
bool Blind(void)
Definition: dvbci.h:108
cMutex(void)
Definition: dvbci.h:53
void Unlock(void)
Definition: dvbci.cpp:189