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 #include <vector>
36 
37 #include <pthread.h>
38 #include <sys/types.h>
39 #include <sys/socket.h>
40 #include <netinet/in.h>
41 #include <sys/un.h>
42 #include <sys/stat.h>
43 #include <sys/uio.h>
44 
45 #include "mythchrono.h"
46 
47 using dvbca_vector = std::vector<uint16_t>;
48 
49 class cMutex {
50  friend class cCondVar;
51 private:
52  pthread_mutex_t m_mutex {};
53  pid_t m_lockingPid {0};
54  int m_locked {0};
55 public:
56  cMutex(void) { pthread_mutex_init(&m_mutex, nullptr); }
57  ~cMutex() { pthread_mutex_destroy(&m_mutex); }
58  void Lock(void);
59  void Unlock(void);
60  };
61 
62 class cMutexLock {
63 private:
64  cMutex *m_mutex {nullptr};
65  bool m_locked {false};
66 public:
67  explicit cMutexLock(cMutex *Mutex = nullptr) { Lock(Mutex); }
68  ~cMutexLock();
69  bool Lock(cMutex *Mutex);
70  };
71 
72 
73 class cCiMMI;
74 
75 class cCiMenu {
76  friend class cCiMMI;
77 private:
78  enum { MAX_CIMENU_ENTRIES = 64 };
79  cCiMMI *m_mmi {nullptr};
81  char *m_titleText {nullptr};
82  char *m_subTitleText {nullptr};
83  char *m_bottomText {nullptr};
85  int m_numEntries {0};
86  bool AddEntry(char *s);
87  cCiMenu(cCiMMI *MMI, bool Selectable);
88 public:
89  ~cCiMenu();
90  const char *TitleText(void) { return m_titleText; }
91  const char *SubTitleText(void) { return m_subTitleText; }
92  const char *BottomText(void) { return m_bottomText; }
93  const char *Entry(int n) { return n < m_numEntries ? m_entries[n] : nullptr; }
94  int NumEntries(void) const { return m_numEntries; }
95  bool Selectable(void) const { return m_selectable; }
96  bool Select(int Index);
97  bool Cancel(void);
98  };
99 
100 class cCiEnquiry {
101  friend class cCiMMI;
102 private:
103  cCiMMI *m_mmi {nullptr};
104  char *m_text {nullptr};
105  bool m_blind {false};
107  explicit cCiEnquiry(cCiMMI *MMI) : m_mmi(MMI) {}
108 public:
109  ~cCiEnquiry();
110  const char *Text(void) { return m_text; }
111  bool Blind(void) const { return m_blind; }
112  int ExpectedLength(void) const { return m_expectedLength; }
113  bool Reply(const char *s);
114  bool Cancel(void);
115  };
116 
117 // Ca Pmt List Management:
118 
119 #define CPLM_MORE 0x00
120 #define CPLM_FIRST 0x01
121 #define CPLM_LAST 0x02
122 #define CPLM_ONLY 0x03
123 #define CPLM_ADD 0x04
124 #define CPLM_UPDATE 0x05
125 
126 class cCiCaPmt {
128  friend class cHlCiHandler;
129 private:
130  int m_length {0};
132  uint8_t m_capmt[2048] {0};
133 public:
134  explicit cCiCaPmt(int ProgramNumber, uint8_t cplm = CPLM_ONLY);
135  void AddElementaryStream(int type, int pid);
136  void AddCaDescriptor(int ca_system_id, int ca_pid, int data_len,
137  const uint8_t *data);
138  };
139 
140 #define MAX_CI_SESSION 16 //XXX
141 
142 class cCiSession;
143 class cCiTransportLayer;
145 
146 class cCiHandler {
147 public:
148  virtual ~cCiHandler() = default;
149  static cCiHandler *CreateCiHandler(const char *FileName);
150  virtual int NumSlots(void) = 0;
151  virtual bool Process(void) = 0;
152  virtual bool HasUserIO(void) = 0;
153  virtual bool NeedCaPmt(void) = 0;
154  virtual bool EnterMenu(int Slot) = 0;
155  virtual cCiMenu *GetMenu(void) = 0;
156  virtual cCiEnquiry *GetEnquiry(void) = 0;
157  virtual dvbca_vector GetCaSystemIds(int Slot) = 0;
158  virtual bool SetCaPmt(cCiCaPmt &CaPmt, int Slot) = 0;
159  virtual void SetTimeOffset(double /*offset_in_seconds*/) { }
160  };
161 
162 class cLlCiHandler : public cCiHandler {
163  friend class cCiHandler;
164 private:
166  int m_fdCa;
168  bool m_newCaSupport {false};
169  bool m_hasUserIO {false};
170  bool m_needCaPmt {false};
174  static int ResourceIdToInt(const uint8_t *Data);
175  bool Send(uint8_t Tag, int SessionId, int ResourceId = 0, int Status = -1);
176  cCiSession *GetSessionBySessionId(int SessionId);
177  cCiSession *GetSessionByResourceId(int ResourceId, int Slot);
178  cCiSession *CreateSession(int ResourceId);
179  bool OpenSession(int Length, const uint8_t *Data);
180  bool CloseSession(int SessionId);
181  int CloseAllSessions(int Slot);
182  cLlCiHandler(int Fd, int NumSlots);
183 public:
184  ~cLlCiHandler() override;
185  cLlCiHandler(const cLlCiHandler &) = delete; // not copyable
186  cLlCiHandler &operator=(const cLlCiHandler &) = delete; // not copyable
187  int NumSlots(void) override // cCiHandler
188  { return m_numSlots; }
189  bool Process(void) override; // cCiHandler
190  bool HasUserIO(void) override // cCiHandler
191  { return m_hasUserIO; }
192  bool NeedCaPmt(void) override // cCiHandler
193  { return m_needCaPmt; }
194  bool EnterMenu(int Slot) override; // cCiHandler
195  cCiMenu *GetMenu(void) override; // cCiHandler
196  cCiEnquiry *GetEnquiry(void) override; // cCiHandler
197  bool SetCaPmt(cCiCaPmt &CaPmt);
198  dvbca_vector GetCaSystemIds(int Slot) override; // cCiHandler
199  bool SetCaPmt(cCiCaPmt &CaPmt, int Slot) override; // cCiHandler
200  void SetTimeOffset(double offset_in_seconds) override; // cCiHandler
201  bool Reset(int Slot);
202  static bool connected();
203  };
204 
205 class cHlCiHandler : public cCiHandler {
206  friend class cCiHandler;
207  private:
209  int m_fdCa;
211  int m_state {0};
214  cHlCiHandler(int Fd, int NumSlots);
215  int CommHL(unsigned tag, unsigned function, struct ca_msg *msg) const;
216  int GetData(unsigned tag, struct ca_msg *msg);
217  int SendData(unsigned tag, struct ca_msg *msg);
218  public:
219  ~cHlCiHandler() override;
220  int NumSlots(void) override // cCiHandler
221  { return m_numSlots; }
222  bool Process(void) override; // cCiHandler
223  bool HasUserIO(void) override { return false; } // cCiHandler
224  bool NeedCaPmt(void) override; // cCiHandler
225  bool EnterMenu(int Slot) override; // cCiHandler
226  cCiMenu *GetMenu(void) override; // cCiHandler
227  cCiEnquiry *GetEnquiry(void) override; // cCiHandler
228  bool SetCaPmt(cCiCaPmt &CaPmt);
229  dvbca_vector GetCaSystemIds(int Slot) override; // cCiHandler
230  bool SetCaPmt(cCiCaPmt &CaPmt, int Slot) override; // cCiHandler
231  bool Reset(int Slot) const;
232  bool connected() const;
233 };
234 
235 int tcp_listen(struct sockaddr_in *name,int sckt,unsigned long address=INADDR_ANY);
236 int accept_tcp(int ip_sock,struct sockaddr_in *ip_name);
237 int udp_listen(struct sockaddr_un *name,char const * filename);
238 int accept_udp(int ip_sock,struct sockaddr_un *ip_name);
239 
240 #endif // DVBCI_H
cCiConditionalAccessSupport
Definition: dvbci.cpp:1014
cLlCiHandler::NeedCaPmt
bool NeedCaPmt(void) override
Definition: dvbci.h:192
cLlCiHandler::SetTimeOffset
void SetTimeOffset(double offset_in_seconds) override
Definition: dvbci.cpp:1857
accept_tcp
int accept_tcp(int ip_sock, struct sockaddr_in *ip_name)
cLlCiHandler::connected
static bool connected()
Definition: dvbci.cpp:1877
cLlCiHandler::HasUserIO
bool HasUserIO(void) override
Definition: dvbci.h:190
cCiMenu::NumEntries
int NumEntries(void) const
Definition: dvbci.h:94
cCiCaPmt::AddCaDescriptor
void AddCaDescriptor(int ca_system_id, int ca_pid, int data_len, const uint8_t *data)
Definition: dvbci.cpp:1522
cLlCiHandler::operator=
cLlCiHandler & operator=(const cLlCiHandler &)=delete
cCiHandler::EnterMenu
virtual bool EnterMenu(int Slot)=0
cLlCiHandler::GetSessionBySessionId
cCiSession * GetSessionBySessionId(int SessionId)
Definition: dvbci.cpp:1629
cLlCiHandler::CloseAllSessions
int CloseAllSessions(int Slot)
Definition: dvbci.cpp:1713
cCiHandler::SetCaPmt
virtual bool SetCaPmt(cCiCaPmt &CaPmt, int Slot)=0
dvbca_vector
std::vector< uint16_t > dvbca_vector
Definition: dvbci.h:47
cCiMenu::TitleText
const char * TitleText(void)
Definition: dvbci.h:90
cHlCiHandler::cHlCiHandler
cHlCiHandler(int Fd, int NumSlots)
Definition: dvbci.cpp:1884
cCiMMI
Definition: dvbci.cpp:1223
cMutex::m_mutex
pthread_mutex_t m_mutex
Definition: dvbci.h:52
cHlCiHandler::SetCaPmt
bool SetCaPmt(cCiCaPmt &CaPmt)
cLlCiHandler::NumSlots
int NumSlots(void) override
Definition: dvbci.h:187
cHlCiHandler::GetData
int GetData(unsigned tag, struct ca_msg *msg)
Definition: dvbci.cpp:1910
cCiHandler::GetEnquiry
virtual cCiEnquiry * GetEnquiry(void)=0
cCiMenu::cCiMenu
cCiMenu(cCiMMI *MMI, bool Selectable)
Definition: dvbci.cpp:1412
cHlCiHandler::connected
bool connected() const
cCiCaPmt::m_length
int m_length
Definition: dvbci.h:130
cCiEnquiry::Cancel
bool Cancel(void)
Definition: dvbci.cpp:1460
cCiEnquiry::Text
const char * Text(void)
Definition: dvbci.h:110
cLlCiHandler::m_tc
cCiTransportConnection * m_tc
Definition: dvbci.h:173
cCiEnquiry::Blind
bool Blind(void) const
Definition: dvbci.h:111
cLlCiHandler::GetEnquiry
cCiEnquiry * GetEnquiry(void) override
Definition: dvbci.cpp:1831
cCiCaPmt
Definition: dvbci.h:126
cHlCiHandler::SendData
int SendData(unsigned tag, struct ca_msg *msg)
Definition: dvbci.cpp:1915
cLlCiHandler::Process
bool Process(void) override
Definition: dvbci.cpp:1725
cMutexLock::~cMutexLock
~cMutexLock()
Definition: dvbci.cpp:224
cCiMenu::m_mmi
cCiMMI * m_mmi
Definition: dvbci.h:79
cMutex::cCondVar
friend class cCondVar
Definition: dvbci.h:50
cCiHandler::Process
virtual bool Process(void)=0
cCiEnquiry::Reply
bool Reply(const char *s)
Definition: dvbci.cpp:1455
cMutexLock
Definition: dvbci.h:62
cMutexLock::cMutexLock
cMutexLock(cMutex *Mutex=nullptr)
Definition: dvbci.h:67
cHlCiHandler::~cHlCiHandler
~cHlCiHandler() override
Definition: dvbci.cpp:1891
cLlCiHandler::m_mutex
cMutex m_mutex
Definition: dvbci.h:165
cCiCaPmt::m_capmt
uint8_t m_capmt[2048]
XXX is there a specified maximum?
Definition: dvbci.h:132
cHlCiHandler::Process
bool Process(void) override
Definition: dvbci.cpp:1920
cCiMenu::Selectable
bool Selectable(void) const
Definition: dvbci.h:95
cLlCiHandler::m_numSlots
int m_numSlots
Definition: dvbci.h:167
cLlCiHandler::~cLlCiHandler
~cLlCiHandler() override
Definition: dvbci.cpp:1570
cCiMenu::m_subTitleText
char * m_subTitleText
Definition: dvbci.h:82
cCiHandler::SetTimeOffset
virtual void SetTimeOffset(double)
Definition: dvbci.h:159
cLlCiHandler::m_sessions
cCiSession * m_sessions[MAX_CI_SESSION]
Definition: dvbci.h:171
cHlCiHandler::m_state
int m_state
Definition: dvbci.h:211
cMutex
Definition: dvbci.h:49
cHlCiHandler::EnterMenu
bool EnterMenu(int Slot) override
Definition: dvbci.cpp:1966
cMutex::m_locked
int m_locked
Definition: dvbci.h:54
cCiTransportConnection
Definition: dvbci.cpp:392
cCiMenu::BottomText
const char * BottomText(void)
Definition: dvbci.h:92
cHlCiHandler::HasUserIO
bool HasUserIO(void) override
Definition: dvbci.h:223
cMutexLock::m_locked
bool m_locked
Definition: dvbci.h:65
cCiEnquiry::m_blind
bool m_blind
Definition: dvbci.h:105
cCiHandler::NumSlots
virtual int NumSlots(void)=0
cLlCiHandler::SetCaPmt
bool SetCaPmt(cCiCaPmt &CaPmt)
cCiEnquiry::~cCiEnquiry
~cCiEnquiry()
Definition: dvbci.cpp:1450
cLlCiHandler::GetCaSystemIds
dvbca_vector GetCaSystemIds(int Slot) override
Definition: dvbci.cpp:1842
cCiHandler::NeedCaPmt
virtual bool NeedCaPmt(void)=0
cLlCiHandler::m_fdCa
int m_fdCa
Definition: dvbci.h:166
cCiMenu::Select
bool Select(int Index)
Definition: dvbci.cpp:1436
MAX_CI_SESSION
#define MAX_CI_SESSION
Definition: dvbci.h:140
cHlCiHandler
Definition: dvbci.h:205
cCiMenu::m_bottomText
char * m_bottomText
Definition: dvbci.h:83
cCiMenu::m_titleText
char * m_titleText
Definition: dvbci.h:81
cLlCiHandler::Send
bool Send(uint8_t Tag, int SessionId, int ResourceId=0, int Status=-1)
Definition: dvbci.cpp:1612
cMutex::Unlock
void Unlock(void)
Definition: dvbci.cpp:211
cMutexLock::m_mutex
cMutex * m_mutex
Definition: dvbci.h:64
cLlCiHandler::m_hasUserIO
bool m_hasUserIO
Definition: dvbci.h:169
cLlCiHandler::CloseSession
bool CloseSession(int SessionId)
Definition: dvbci.cpp:1697
cLlCiHandler
Definition: dvbci.h:162
cMutexLock::Lock
bool Lock(cMutex *Mutex)
Definition: dvbci.cpp:230
cMutex::Lock
void Lock(void)
Definition: dvbci.cpp:202
cLlCiHandler::CreateSession
cCiSession * CreateSession(int ResourceId)
Definition: dvbci.cpp:1647
cCiHandler::HasUserIO
virtual bool HasUserIO(void)=0
cCiTransportLayer
Definition: dvbci.cpp:593
cCiMenu::~cCiMenu
~cCiMenu()
Definition: dvbci.cpp:1418
cHlCiHandler::m_caSystemIds
dvbca_vector m_caSystemIds
Definition: dvbci.h:213
cCiMenu::m_entries
char * m_entries[MAX_CIMENU_ENTRIES]
Definition: dvbci.h:84
cHlCiHandler::Reset
bool Reset(int Slot) const
Definition: dvbci.cpp:2012
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:1897
cHlCiHandler::NumSlots
int NumSlots(void) override
Definition: dvbci.h:220
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:57
cCiMenu::m_selectable
bool m_selectable
Definition: dvbci.h:80
cHlCiHandler::m_fdCa
int m_fdCa
Definition: dvbci.h:209
cHlCiHandler::m_numSlots
int m_numSlots
Definition: dvbci.h:210
cCiSession
Definition: dvbci.cpp:766
cLlCiHandler::m_tpl
cCiTransportLayer * m_tpl
Definition: dvbci.h:172
cHlCiHandler::GetCaSystemIds
dvbca_vector GetCaSystemIds(int Slot) override
Definition: dvbci.cpp:1981
cMutex::m_lockingPid
pid_t m_lockingPid
Definition: dvbci.h:53
udp_listen
int udp_listen(struct sockaddr_un *name, char const *filename)
cCiEnquiry::m_mmi
cCiMMI * m_mmi
Definition: dvbci.h:103
cCiMenu::MAX_CIMENU_ENTRIES
@ MAX_CIMENU_ENTRIES
Definition: dvbci.h:78
cLlCiHandler::cLlCiHandler
cLlCiHandler(int Fd, int NumSlots)
Definition: dvbci.cpp:1563
cMutex::cMutex
cMutex(void)
Definition: dvbci.h:56
mythchrono.h
cHlCiHandler::NeedCaPmt
bool NeedCaPmt(void) override
Definition: dvbci.cpp:2021
cCiEnquiry::cCiEnquiry
cCiEnquiry(cCiMMI *MMI)
Definition: dvbci.h:107
cCiEnquiry::m_text
char * m_text
Definition: dvbci.h:104
cLlCiHandler::Reset
bool Reset(int Slot)
Definition: dvbci.cpp:1870
cLlCiHandler::m_needCaPmt
bool m_needCaPmt
Definition: dvbci.h:170
cLlCiHandler::EnterMenu
bool EnterMenu(int Slot) override
Definition: dvbci.cpp:1813
cLlCiHandler::ResourceIdToInt
static int ResourceIdToInt(const uint8_t *Data)
Definition: dvbci.cpp:1607
cCiHandler::CreateCiHandler
static cCiHandler * CreateCiHandler(const char *FileName)
Definition: dvbci.cpp:1579
CPLM_ONLY
#define CPLM_ONLY
Definition: dvbci.h:122
cCiMenu
Definition: dvbci.h:75
cCiEnquiry::ExpectedLength
int ExpectedLength(void) const
Definition: dvbci.h:112
cCiHandler::GetMenu
virtual cCiMenu * GetMenu(void)=0
cCiEnquiry
Definition: dvbci.h:100
cHlCiHandler::m_mutex
cMutex m_mutex
Definition: dvbci.h:208
cCiCaPmt::m_infoLengthPos
int m_infoLengthPos
Definition: dvbci.h:131
cCiHandler::GetCaSystemIds
virtual dvbca_vector GetCaSystemIds(int Slot)=0
cLlCiHandler::GetSessionByResourceId
cCiSession * GetSessionByResourceId(int ResourceId, int Slot)
Definition: dvbci.cpp:1638
cCiCaPmt::cCiCaPmt
cCiCaPmt(int ProgramNumber, uint8_t cplm=CPLM_ONLY)
Definition: dvbci.cpp:1474
build_compdb.filename
filename
Definition: build_compdb.py:21
cLlCiHandler::GetMenu
cCiMenu * GetMenu(void) override
Definition: dvbci.cpp:1820
cLlCiHandler::m_newCaSupport
bool m_newCaSupport
Definition: dvbci.h:168
cCiEnquiry::m_expectedLength
int m_expectedLength
Definition: dvbci.h:106
cCiHandler
Definition: dvbci.h:146
cCiMenu::AddEntry
bool AddEntry(char *s)
Definition: dvbci.cpp:1427
cCiMenu::SubTitleText
const char * SubTitleText(void)
Definition: dvbci.h:91
cCiMenu::m_numEntries
int m_numEntries
Definition: dvbci.h:85
cLlCiHandler::OpenSession
bool OpenSession(int Length, const uint8_t *Data)
Definition: dvbci.cpp:1667
cCiMenu::Cancel
bool Cancel(void)
Definition: dvbci.cpp:1443
cHlCiHandler::GetMenu
cCiMenu * GetMenu(void) override
Definition: dvbci.cpp:1971
cHlCiHandler::GetEnquiry
cCiEnquiry * GetEnquiry(void) override
Definition: dvbci.cpp:1976
cHlCiHandler::m_numCaSystemIds
int m_numCaSystemIds
Definition: dvbci.h:212
cCiMenu::Entry
const char * Entry(int n)
Definition: dvbci.h:93
cCiCaPmt::AddElementaryStream
void AddElementaryStream(int type, int pid)
Definition: dvbci.cpp:1487