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 using dvbca_vector = std::vector<uint16_t>;
46 
47 class cMutex {
48  friend class cCondVar;
49 private:
50  pthread_mutex_t m_mutex {};
51  pid_t m_lockingPid {0};
52  int m_locked {0};
53 public:
54  cMutex(void) { pthread_mutex_init(&m_mutex, nullptr); }
55  ~cMutex() { pthread_mutex_destroy(&m_mutex); }
56  void Lock(void);
57  void Unlock(void);
58  };
59 
60 class cMutexLock {
61 private:
62  cMutex *m_mutex {nullptr};
63  bool m_locked {false};
64 public:
65  explicit cMutexLock(cMutex *Mutex = nullptr) { Lock(Mutex); }
66  ~cMutexLock();
67  bool Lock(cMutex *Mutex);
68  };
69 
70 
71 class cCiMMI;
72 
73 class cCiMenu {
74  friend class cCiMMI;
75 private:
76  enum { MAX_CIMENU_ENTRIES = 64 };
77  cCiMMI *m_mmi {nullptr};
79  char *m_titleText {nullptr};
80  char *m_subTitleText {nullptr};
81  char *m_bottomText {nullptr};
83  int m_numEntries {0};
84  bool AddEntry(char *s);
85  cCiMenu(cCiMMI *MMI, bool Selectable);
86 public:
87  ~cCiMenu();
88  const char *TitleText(void) { return m_titleText; }
89  const char *SubTitleText(void) { return m_subTitleText; }
90  const char *BottomText(void) { return m_bottomText; }
91  const char *Entry(int n) { return n < m_numEntries ? m_entries[n] : nullptr; }
92  int NumEntries(void) const { return m_numEntries; }
93  bool Selectable(void) const { return m_selectable; }
94  bool Select(int Index);
95  bool Cancel(void);
96  };
97 
98 class cCiEnquiry {
99  friend class cCiMMI;
100 private:
101  cCiMMI *m_mmi {nullptr};
102  char *m_text {nullptr};
103  bool m_blind {false};
105  explicit cCiEnquiry(cCiMMI *MMI) : m_mmi(MMI) {}
106 public:
107  ~cCiEnquiry();
108  const char *Text(void) { return m_text; }
109  bool Blind(void) const { return m_blind; }
110  int ExpectedLength(void) const { return m_expectedLength; }
111  bool Reply(const char *s);
112  bool Cancel(void);
113  };
114 
115 // Ca Pmt List Management:
116 
117 #define CPLM_MORE 0x00
118 #define CPLM_FIRST 0x01
119 #define CPLM_LAST 0x02
120 #define CPLM_ONLY 0x03
121 #define CPLM_ADD 0x04
122 #define CPLM_UPDATE 0x05
123 
124 class cCiCaPmt {
126  friend class cHlCiHandler;
127 private:
128  int m_length {0};
130  uint8_t m_capmt[2048] {0};
131 public:
132  explicit cCiCaPmt(int ProgramNumber, uint8_t cplm = CPLM_ONLY);
133  void AddElementaryStream(int type, int pid);
134  void AddCaDescriptor(int ca_system_id, int ca_pid, int data_len,
135  const uint8_t *data);
136  };
137 
138 #define MAX_CI_SESSION 16 //XXX
139 
140 class cCiSession;
141 class cCiTransportLayer;
143 
144 class cCiHandler {
145 public:
146  virtual ~cCiHandler() = default;
147  static cCiHandler *CreateCiHandler(const char *FileName);
148  virtual int NumSlots(void) = 0;
149  virtual bool Process(void) = 0;
150  virtual bool HasUserIO(void) = 0;
151  virtual bool NeedCaPmt(void) = 0;
152  virtual bool EnterMenu(int Slot) = 0;
153  virtual cCiMenu *GetMenu(void) = 0;
154  virtual cCiEnquiry *GetEnquiry(void) = 0;
155  virtual dvbca_vector GetCaSystemIds(int Slot) = 0;
156  virtual bool SetCaPmt(cCiCaPmt &CaPmt, int Slot) = 0;
157  virtual void SetTimeOffset(double /*offset_in_seconds*/) { }
158  };
159 
160 class cLlCiHandler : public cCiHandler {
161  friend class cCiHandler;
162 private:
164  int m_fdCa;
166  bool m_newCaSupport {false};
167  bool m_hasUserIO {false};
168  bool m_needCaPmt {false};
172  static int ResourceIdToInt(const uint8_t *Data);
173  bool Send(uint8_t Tag, int SessionId, int ResourceId = 0, int Status = -1);
174  cCiSession *GetSessionBySessionId(int SessionId);
175  cCiSession *GetSessionByResourceId(int ResourceId, int Slot);
176  cCiSession *CreateSession(int ResourceId);
177  bool OpenSession(int Length, const uint8_t *Data);
178  bool CloseSession(int SessionId);
179  int CloseAllSessions(int Slot);
180  cLlCiHandler(int Fd, int NumSlots);
181 public:
182  ~cLlCiHandler() override;
183  cLlCiHandler(const cLlCiHandler &) = delete; // not copyable
184  cLlCiHandler &operator=(const cLlCiHandler &) = delete; // not copyable
185  int NumSlots(void) override // cCiHandler
186  { return m_numSlots; }
187  bool Process(void) override; // cCiHandler
188  bool HasUserIO(void) override // cCiHandler
189  { return m_hasUserIO; }
190  bool NeedCaPmt(void) override // cCiHandler
191  { return m_needCaPmt; }
192  bool EnterMenu(int Slot) override; // cCiHandler
193  cCiMenu *GetMenu(void) override; // cCiHandler
194  cCiEnquiry *GetEnquiry(void) override; // cCiHandler
195  bool SetCaPmt(cCiCaPmt &CaPmt);
196  dvbca_vector GetCaSystemIds(int Slot) override; // cCiHandler
197  bool SetCaPmt(cCiCaPmt &CaPmt, int Slot) override; // cCiHandler
198  void SetTimeOffset(double offset_in_seconds) override; // cCiHandler
199  bool Reset(int Slot);
200  static bool connected();
201  };
202 
203 class cHlCiHandler : public cCiHandler {
204  friend class cCiHandler;
205  private:
207  int m_fdCa;
209  int m_state {0};
212  cHlCiHandler(int Fd, int NumSlots);
213  int CommHL(unsigned tag, unsigned function, struct ca_msg *msg) const;
214  int GetData(unsigned tag, struct ca_msg *msg);
215  int SendData(unsigned tag, struct ca_msg *msg);
216  public:
217  ~cHlCiHandler() override;
218  int NumSlots(void) override // cCiHandler
219  { return m_numSlots; }
220  bool Process(void) override; // cCiHandler
221  bool HasUserIO(void) override { return false; } // cCiHandler
222  bool NeedCaPmt(void) override; // cCiHandler
223  bool EnterMenu(int Slot) override; // cCiHandler
224  cCiMenu *GetMenu(void) override; // cCiHandler
225  cCiEnquiry *GetEnquiry(void) override; // cCiHandler
226  bool SetCaPmt(cCiCaPmt &CaPmt);
227  dvbca_vector GetCaSystemIds(int Slot) override; // cCiHandler
228  bool SetCaPmt(cCiCaPmt &CaPmt, int Slot) override; // cCiHandler
229  bool Reset(int Slot) const;
230  bool connected() const;
231 };
232 
233 int tcp_listen(struct sockaddr_in *name,int sckt,unsigned long address=INADDR_ANY);
234 int accept_tcp(int ip_sock,struct sockaddr_in *ip_name);
235 int udp_listen(struct sockaddr_un *name,char const * filename);
236 int accept_udp(int ip_sock,struct sockaddr_un *ip_name);
237 
238 #endif // DVBCI_H
cCiConditionalAccessSupport
Definition: dvbci.cpp:1018
cLlCiHandler::NeedCaPmt
bool NeedCaPmt(void) override
Definition: dvbci.h:190
cLlCiHandler::SetTimeOffset
void SetTimeOffset(double offset_in_seconds) override
Definition: dvbci.cpp:1861
accept_tcp
int accept_tcp(int ip_sock, struct sockaddr_in *ip_name)
cLlCiHandler::connected
static bool connected()
Definition: dvbci.cpp:1881
cLlCiHandler::HasUserIO
bool HasUserIO(void) override
Definition: dvbci.h:188
cCiMenu::NumEntries
int NumEntries(void) const
Definition: dvbci.h:92
cCiMenu::MAX_CIMENU_ENTRIES
@ MAX_CIMENU_ENTRIES
Definition: dvbci.h:76
cCiCaPmt::AddCaDescriptor
void AddCaDescriptor(int ca_system_id, int ca_pid, int data_len, const uint8_t *data)
Definition: dvbci.cpp:1526
cLlCiHandler::operator=
cLlCiHandler & operator=(const cLlCiHandler &)=delete
cCiHandler::EnterMenu
virtual bool EnterMenu(int Slot)=0
cLlCiHandler::GetSessionBySessionId
cCiSession * GetSessionBySessionId(int SessionId)
Definition: dvbci.cpp:1633
cLlCiHandler::CloseAllSessions
int CloseAllSessions(int Slot)
Definition: dvbci.cpp:1717
cCiHandler::SetCaPmt
virtual bool SetCaPmt(cCiCaPmt &CaPmt, int Slot)=0
dvbca_vector
std::vector< uint16_t > dvbca_vector
Definition: dvbci.h:45
cCiMenu::TitleText
const char * TitleText(void)
Definition: dvbci.h:88
cHlCiHandler::cHlCiHandler
cHlCiHandler(int Fd, int NumSlots)
Definition: dvbci.cpp:1888
cCiMMI
Definition: dvbci.cpp:1227
cMutex::m_mutex
pthread_mutex_t m_mutex
Definition: dvbci.h:50
cHlCiHandler::SetCaPmt
bool SetCaPmt(cCiCaPmt &CaPmt)
cLlCiHandler::NumSlots
int NumSlots(void) override
Definition: dvbci.h:185
cHlCiHandler::GetData
int GetData(unsigned tag, struct ca_msg *msg)
Definition: dvbci.cpp:1914
cCiHandler::GetEnquiry
virtual cCiEnquiry * GetEnquiry(void)=0
cCiMenu::cCiMenu
cCiMenu(cCiMMI *MMI, bool Selectable)
Definition: dvbci.cpp:1416
cHlCiHandler::connected
bool connected() const
cCiCaPmt::m_length
int m_length
Definition: dvbci.h:128
cCiEnquiry::Cancel
bool Cancel(void)
Definition: dvbci.cpp:1464
cCiEnquiry::Text
const char * Text(void)
Definition: dvbci.h:108
cLlCiHandler::m_tc
cCiTransportConnection * m_tc
Definition: dvbci.h:171
cCiEnquiry::Blind
bool Blind(void) const
Definition: dvbci.h:109
cLlCiHandler::GetEnquiry
cCiEnquiry * GetEnquiry(void) override
Definition: dvbci.cpp:1835
cCiCaPmt
Definition: dvbci.h:124
cHlCiHandler::SendData
int SendData(unsigned tag, struct ca_msg *msg)
Definition: dvbci.cpp:1919
cLlCiHandler::Process
bool Process(void) override
Definition: dvbci.cpp:1729
cMutexLock::~cMutexLock
~cMutexLock()
Definition: dvbci.cpp:224
cCiMenu::m_mmi
cCiMMI * m_mmi
Definition: dvbci.h:77
cMutex::cCondVar
friend class cCondVar
Definition: dvbci.h:48
cCiHandler::Process
virtual bool Process(void)=0
cCiEnquiry::Reply
bool Reply(const char *s)
Definition: dvbci.cpp:1459
cMutexLock
Definition: dvbci.h:60
cMutexLock::cMutexLock
cMutexLock(cMutex *Mutex=nullptr)
Definition: dvbci.h:65
cHlCiHandler::~cHlCiHandler
~cHlCiHandler() override
Definition: dvbci.cpp:1895
cLlCiHandler::m_mutex
cMutex m_mutex
Definition: dvbci.h:163
cCiCaPmt::m_capmt
uint8_t m_capmt[2048]
XXX is there a specified maximum?
Definition: dvbci.h:130
cHlCiHandler::Process
bool Process(void) override
Definition: dvbci.cpp:1924
cCiMenu::Selectable
bool Selectable(void) const
Definition: dvbci.h:93
cLlCiHandler::m_numSlots
int m_numSlots
Definition: dvbci.h:165
cLlCiHandler::~cLlCiHandler
~cLlCiHandler() override
Definition: dvbci.cpp:1574
cCiMenu::m_subTitleText
char * m_subTitleText
Definition: dvbci.h:80
cCiHandler::SetTimeOffset
virtual void SetTimeOffset(double)
Definition: dvbci.h:157
cLlCiHandler::m_sessions
cCiSession * m_sessions[MAX_CI_SESSION]
Definition: dvbci.h:169
cHlCiHandler::m_state
int m_state
Definition: dvbci.h:209
cMutex
Definition: dvbci.h:47
cHlCiHandler::EnterMenu
bool EnterMenu(int Slot) override
Definition: dvbci.cpp:1970
cMutex::m_locked
int m_locked
Definition: dvbci.h:52
cCiTransportConnection
Definition: dvbci.cpp:392
cCiMenu::BottomText
const char * BottomText(void)
Definition: dvbci.h:90
cHlCiHandler::HasUserIO
bool HasUserIO(void) override
Definition: dvbci.h:221
cMutexLock::m_locked
bool m_locked
Definition: dvbci.h:63
cCiEnquiry::m_blind
bool m_blind
Definition: dvbci.h:103
cCiHandler::NumSlots
virtual int NumSlots(void)=0
cLlCiHandler::SetCaPmt
bool SetCaPmt(cCiCaPmt &CaPmt)
cCiEnquiry::~cCiEnquiry
~cCiEnquiry()
Definition: dvbci.cpp:1454
cLlCiHandler::GetCaSystemIds
dvbca_vector GetCaSystemIds(int Slot) override
Definition: dvbci.cpp:1846
cCiHandler::NeedCaPmt
virtual bool NeedCaPmt(void)=0
cLlCiHandler::m_fdCa
int m_fdCa
Definition: dvbci.h:164
cCiMenu::Select
bool Select(int Index)
Definition: dvbci.cpp:1440
MAX_CI_SESSION
#define MAX_CI_SESSION
Definition: dvbci.h:138
cHlCiHandler
Definition: dvbci.h:203
cCiMenu::m_bottomText
char * m_bottomText
Definition: dvbci.h:81
cCiMenu::m_titleText
char * m_titleText
Definition: dvbci.h:79
cLlCiHandler::Send
bool Send(uint8_t Tag, int SessionId, int ResourceId=0, int Status=-1)
Definition: dvbci.cpp:1616
cMutex::Unlock
void Unlock(void)
Definition: dvbci.cpp:211
cMutexLock::m_mutex
cMutex * m_mutex
Definition: dvbci.h:62
filename
QString filename
Definition: mythplugins/mytharchive/mytharchivehelper/main.cpp:637
cLlCiHandler::m_hasUserIO
bool m_hasUserIO
Definition: dvbci.h:167
cLlCiHandler::CloseSession
bool CloseSession(int SessionId)
Definition: dvbci.cpp:1701
cLlCiHandler
Definition: dvbci.h:160
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:1651
cCiHandler::HasUserIO
virtual bool HasUserIO(void)=0
cCiTransportLayer
Definition: dvbci.cpp:597
cCiMenu::~cCiMenu
~cCiMenu()
Definition: dvbci.cpp:1422
cHlCiHandler::m_caSystemIds
dvbca_vector m_caSystemIds
Definition: dvbci.h:211
cCiMenu::m_entries
char * m_entries[MAX_CIMENU_ENTRIES]
Definition: dvbci.h:82
cHlCiHandler::Reset
bool Reset(int Slot) const
Definition: dvbci.cpp:2016
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:1901
cHlCiHandler::NumSlots
int NumSlots(void) override
Definition: dvbci.h:218
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:55
cCiMenu::m_selectable
bool m_selectable
Definition: dvbci.h:78
cHlCiHandler::m_fdCa
int m_fdCa
Definition: dvbci.h:207
cHlCiHandler::m_numSlots
int m_numSlots
Definition: dvbci.h:208
cCiSession
Definition: dvbci.cpp:770
cLlCiHandler::m_tpl
cCiTransportLayer * m_tpl
Definition: dvbci.h:170
cHlCiHandler::GetCaSystemIds
dvbca_vector GetCaSystemIds(int Slot) override
Definition: dvbci.cpp:1985
cMutex::m_lockingPid
pid_t m_lockingPid
Definition: dvbci.h:51
udp_listen
int udp_listen(struct sockaddr_un *name, char const *filename)
cCiEnquiry::m_mmi
cCiMMI * m_mmi
Definition: dvbci.h:101
cLlCiHandler::cLlCiHandler
cLlCiHandler(int Fd, int NumSlots)
Definition: dvbci.cpp:1567
cMutex::cMutex
cMutex(void)
Definition: dvbci.h:54
cHlCiHandler::NeedCaPmt
bool NeedCaPmt(void) override
Definition: dvbci.cpp:2025
cCiEnquiry::cCiEnquiry
cCiEnquiry(cCiMMI *MMI)
Definition: dvbci.h:105
cCiEnquiry::m_text
char * m_text
Definition: dvbci.h:102
cLlCiHandler::Reset
bool Reset(int Slot)
Definition: dvbci.cpp:1874
cLlCiHandler::m_needCaPmt
bool m_needCaPmt
Definition: dvbci.h:168
cLlCiHandler::EnterMenu
bool EnterMenu(int Slot) override
Definition: dvbci.cpp:1817
cLlCiHandler::ResourceIdToInt
static int ResourceIdToInt(const uint8_t *Data)
Definition: dvbci.cpp:1611
cCiHandler::CreateCiHandler
static cCiHandler * CreateCiHandler(const char *FileName)
Definition: dvbci.cpp:1583
CPLM_ONLY
#define CPLM_ONLY
Definition: dvbci.h:120
cCiMenu
Definition: dvbci.h:73
cCiEnquiry::ExpectedLength
int ExpectedLength(void) const
Definition: dvbci.h:110
cCiHandler::GetMenu
virtual cCiMenu * GetMenu(void)=0
cCiEnquiry
Definition: dvbci.h:98
cHlCiHandler::m_mutex
cMutex m_mutex
Definition: dvbci.h:206
cCiCaPmt::m_infoLengthPos
int m_infoLengthPos
Definition: dvbci.h:129
cCiHandler::GetCaSystemIds
virtual dvbca_vector GetCaSystemIds(int Slot)=0
cLlCiHandler::GetSessionByResourceId
cCiSession * GetSessionByResourceId(int ResourceId, int Slot)
Definition: dvbci.cpp:1642
cCiCaPmt::cCiCaPmt
cCiCaPmt(int ProgramNumber, uint8_t cplm=CPLM_ONLY)
Definition: dvbci.cpp:1478
cLlCiHandler::GetMenu
cCiMenu * GetMenu(void) override
Definition: dvbci.cpp:1824
cLlCiHandler::m_newCaSupport
bool m_newCaSupport
Definition: dvbci.h:166
cCiEnquiry::m_expectedLength
int m_expectedLength
Definition: dvbci.h:104
cCiHandler
Definition: dvbci.h:144
cCiMenu::AddEntry
bool AddEntry(char *s)
Definition: dvbci.cpp:1431
cCiMenu::SubTitleText
const char * SubTitleText(void)
Definition: dvbci.h:89
cCiMenu::m_numEntries
int m_numEntries
Definition: dvbci.h:83
cLlCiHandler::OpenSession
bool OpenSession(int Length, const uint8_t *Data)
Definition: dvbci.cpp:1671
cCiMenu::Cancel
bool Cancel(void)
Definition: dvbci.cpp:1447
cHlCiHandler::GetMenu
cCiMenu * GetMenu(void) override
Definition: dvbci.cpp:1975
cHlCiHandler::GetEnquiry
cCiEnquiry * GetEnquiry(void) override
Definition: dvbci.cpp:1980
cHlCiHandler::m_numCaSystemIds
int m_numCaSystemIds
Definition: dvbci.h:210
cCiMenu::Entry
const char * Entry(int n)
Definition: dvbci.h:91
cCiCaPmt::AddElementaryStream
void AddElementaryStream(int type, int pid)
Definition: dvbci.cpp:1491