MythTV  0.26-pre
dvbci.h
Go to the documentation of this file.
00001 /*
00002  * ci.hh: Common Interface
00003  *
00004  * Copyright (C) 2000 Klaus Schmidinger
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00019  * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
00020  *
00021  * The author can be reached at kls@cadsoft.de
00022  *
00023  * The project's page is at http://www.cadsoft.de/people/kls/vdr
00024  *
00025  */
00026 
00027 #ifndef __CI_H
00028 #define __CI_H
00029 
00030 #if HAVE_STDINT_H
00031 #include <stdint.h>
00032 #endif 
00033 
00034 #include <stdio.h>
00035 
00036 #include <pthread.h>
00037 #include <sys/types.h>
00038 #include <sys/socket.h>
00039 #include <netinet/in.h>
00040 #include <sys/un.h>
00041 #include <sys/stat.h>
00042 #include <sys/uio.h>
00043 
00044 #define MAXCASYSTEMIDS 64
00045 
00046 class cMutex {
00047   friend class cCondVar;
00048 private:
00049   pthread_mutex_t mutex;
00050   pid_t lockingPid;
00051   int locked;
00052 public:
00053   cMutex(void);
00054   ~cMutex();
00055   void Lock(void);
00056   void Unlock(void);
00057   };
00058 
00059 class cMutexLock {
00060 private:
00061   cMutex *mutex;
00062   bool locked;
00063 public:
00064   cMutexLock(cMutex *Mutex = NULL);
00065   ~cMutexLock();
00066   bool Lock(cMutex *Mutex);
00067   };
00068 
00069 
00070 class cCiMMI;
00071 
00072 class cCiMenu {
00073   friend class cCiMMI;
00074 private:
00075   enum { MAX_CIMENU_ENTRIES = 64 }; 
00076   cCiMMI *mmi;
00077   bool selectable;
00078   char *titleText;
00079   char *subTitleText;
00080   char *bottomText;
00081   char *entries[MAX_CIMENU_ENTRIES];
00082   int numEntries;
00083   bool AddEntry(char *s);
00084   cCiMenu(cCiMMI *MMI, bool Selectable);
00085 public:
00086   ~cCiMenu();
00087   const char *TitleText(void) { return titleText; }
00088   const char *SubTitleText(void) { return subTitleText; }
00089   const char *BottomText(void) { return bottomText; }
00090   const char *Entry(int n) { return n < numEntries ? entries[n] : NULL; }
00091   int NumEntries(void) { return numEntries; }
00092   bool Selectable(void) { return selectable; }
00093   bool Select(int Index);
00094   bool Cancel(void);
00095   };
00096 
00097 class cCiEnquiry {
00098   friend class cCiMMI;
00099 private:
00100   cCiMMI *mmi;
00101   char *text;
00102   bool blind;
00103   int expectedLength;
00104   cCiEnquiry(cCiMMI *MMI);
00105 public:
00106   ~cCiEnquiry();
00107   const char *Text(void) { return text; }
00108   bool Blind(void) { return blind; }
00109   int ExpectedLength(void) { return expectedLength; }
00110   bool Reply(const char *s);
00111   bool Cancel(void);
00112   };
00113 
00114 // Ca Pmt List Management:
00115 
00116 #define CPLM_MORE    0x00
00117 #define CPLM_FIRST   0x01
00118 #define CPLM_LAST    0x02
00119 #define CPLM_ONLY    0x03
00120 #define CPLM_ADD     0x04
00121 #define CPLM_UPDATE  0x05
00122 
00123 class cCiCaPmt {
00124   friend class cCiConditionalAccessSupport;
00125   friend class cHlCiHandler;
00126 private:
00127   int length;
00128   int infoLengthPos;
00129   uint8_t capmt[2048]; 
00130 public:
00131   cCiCaPmt(int ProgramNumber, uint8_t cplm = CPLM_ONLY);
00132   void AddElementaryStream(int type, int pid);
00133   void AddCaDescriptor(int ca_system_id, int ca_pid, int data_len,
00134                        const uint8_t *data);
00135   };
00136 
00137 #define MAX_CI_SESSION  16 //XXX
00138 
00139 class cCiSession;
00140 class cCiTransportLayer;
00141 class cCiTransportConnection;
00142 
00143 class cCiHandler {
00144 public:
00145   virtual ~cCiHandler() {};
00146   static cCiHandler *CreateCiHandler(const char *FileName);
00147   virtual int NumSlots(void) = 0;
00148   virtual bool Process(void) = 0;
00149   virtual bool HasUserIO(void) = 0;
00150   virtual bool NeedCaPmt(void) = 0;
00151   virtual bool EnterMenu(int Slot) = 0;
00152   virtual cCiMenu *GetMenu(void) = 0;
00153   virtual cCiEnquiry *GetEnquiry(void) = 0;
00154   virtual const unsigned short *GetCaSystemIds(int Slot) = 0;
00155   virtual bool SetCaPmt(cCiCaPmt &CaPmt, int Slot) = 0;
00156   virtual void SetTimeOffset(double /*offset_in_seconds*/) { }
00157   };
00158 
00159 class cLlCiHandler : public cCiHandler {
00160   friend class cCiHandler;
00161 private:
00162   cMutex mutex;
00163   int fdCa;
00164   int numSlots;
00165   bool newCaSupport;
00166   bool hasUserIO;
00167   bool needCaPmt;
00168   cCiSession *sessions[MAX_CI_SESSION];
00169   cCiTransportLayer *tpl;
00170   cCiTransportConnection *tc;
00171   int ResourceIdToInt(const uint8_t *Data);
00172   bool Send(uint8_t Tag, int SessionId, int ResourceId = 0, int Status = -1);
00173   cCiSession *GetSessionBySessionId(int SessionId);
00174   cCiSession *GetSessionByResourceId(int ResourceId, int Slot);
00175   cCiSession *CreateSession(int ResourceId);
00176   bool OpenSession(int Length, const uint8_t *Data);
00177   bool CloseSession(int SessionId);
00178   int CloseAllSessions(int Slot);
00179   cLlCiHandler(int Fd, int NumSlots);
00180 public:
00181   virtual ~cLlCiHandler();
00182   int NumSlots(void) { return numSlots; }
00183   bool Process(void);
00184   bool HasUserIO(void) { return hasUserIO; }
00185   bool NeedCaPmt(void) { return needCaPmt; }
00186   bool EnterMenu(int Slot);
00187   cCiMenu *GetMenu(void);
00188   cCiEnquiry *GetEnquiry(void);
00189   bool SetCaPmt(cCiCaPmt &CaPmt);
00190   const unsigned short *GetCaSystemIds(int Slot);
00191   bool SetCaPmt(cCiCaPmt &CaPmt, int Slot);
00192   void SetTimeOffset(double offset_in_seconds);
00193   bool Reset(int Slot);
00194   bool connected() const;
00195   };
00196 
00197 class cHlCiHandler : public cCiHandler {
00198     friend class cCiHandler;
00199   private:
00200     cMutex mutex;
00201     int fdCa;
00202     int numSlots;
00203     int state;
00204     int numCaSystemIds;
00205     unsigned short caSystemIds[MAXCASYSTEMIDS + 1]; // list is zero terminated!
00206     cHlCiHandler(int Fd, int NumSlots);
00207     int CommHL(unsigned tag, unsigned function, struct ca_msg *msg);
00208     int GetData(unsigned tag, struct ca_msg *msg);
00209     int SendData(unsigned tag, struct ca_msg *msg);
00210   public:
00211     virtual ~cHlCiHandler();
00212     int NumSlots(void) { return numSlots; }
00213     bool Process(void);
00214     bool HasUserIO(void) { return false; }//hasUserIO; }
00215     bool NeedCaPmt(void);
00216     bool EnterMenu(int Slot);
00217     cCiMenu *GetMenu(void);
00218     cCiEnquiry *GetEnquiry(void);
00219     bool SetCaPmt(cCiCaPmt &CaPmt);
00220     const unsigned short *GetCaSystemIds(int Slot);
00221     bool SetCaPmt(cCiCaPmt &CaPmt, int Slot);
00222     bool Reset(int Slot);
00223     bool connected() const;
00224 };
00225 
00226 int tcp_listen(struct sockaddr_in *name,int sckt,unsigned long address=INADDR_ANY);
00227 int accept_tcp(int ip_sock,struct sockaddr_in *ip_name);
00228 int udp_listen(struct sockaddr_un *name,char const * const filename);
00229 int accept_udp(int ip_sock,struct sockaddr_un *ip_name);
00230 
00231 #endif //__CI_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends