SASC
DVBSoftCAM

cam.h

Go to the documentation of this file.
00001 /*
00002  * Softcam plugin to VDR (C++)
00003  *
00004  * This code is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version.
00008  *
00009  * This code is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017  * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
00018  */
00019 
00020 #ifndef ___CAM_H
00021 #define ___CAM_H
00022 
00023 #include <linux/dvb/ca.h>
00024 #include <vdr/ci.h>
00025 #include <vdr/thread.h>
00026 #include "data.h"
00027 #include "misc.h"
00028 
00029 class cChannel;
00030 class cRingBufferLinear;
00031 class cDevice;
00032 
00033 class cEcmHandler;
00034 class cEcmData;
00035 class cLogger;
00036 class cHookManager;
00037 class cLogHook;
00038 class cScCamSlot;
00039 class cDeCSA;
00040 class cPrg;
00041 class cScDevicePlugin;
00042 
00043 // ----------------------------------------------------------------
00044 
00045 class cEcmCache : public cStructListPlain<cEcmData> {
00046 private:
00047   cEcmData *Exists(cEcmInfo *e);
00048 protected:
00049   virtual bool ParseLinePlain(const char *line);
00050 public:
00051   cEcmCache(void);
00052   void New(cEcmInfo *e);
00053   int GetCached(cSimpleList<cEcmInfo> *list, int sid, int Source, int Transponder);
00054   void Delete(cEcmInfo *e);
00055   void Flush(void);
00056   };
00057 
00058 extern cEcmCache ecmcache;
00059 
00060 // ----------------------------------------------------------------
00061 
00062 #ifndef SASC
00063 class cCiFrame {
00064 private:
00065   cRingBufferLinear *rb;
00066   unsigned char *mem;
00067   int len, alen, glen;
00068 public:
00069   cCiFrame(void);
00070   ~cCiFrame();
00071   void SetRb(cRingBufferLinear *Rb) { rb=Rb; }
00072   unsigned char *GetBuff(int l);
00073   void Put(void);
00074   unsigned char *Get(int &l);
00075   void Del(void);
00076   int Avail(void);
00077   };
00078 #endif //!SASC
00079 
00080 // ----------------------------------------------------------------
00081 
00082 class cCaDescr {
00083 private:
00084   unsigned char *descr;
00085   int len;
00086 public:
00087   cCaDescr(void);
00088   cCaDescr(const cCaDescr &arg);
00089   ~cCaDescr();
00090   const unsigned char *Get(int &l) const;
00091   void Set(const cCaDescr *d);
00092   void Set(const unsigned char *de, int l);
00093   void Clear(void);
00094   bool operator== (const cCaDescr &arg) const;
00095   void Join(const cCaDescr *cd, bool rev=false);
00096   cString ToString(void);
00097   };
00098 
00099 // ----------------------------------------------------------------
00100 
00101 class cPrgPid : public cSimpleItem {
00102 private:
00103   bool proc;
00104 public:
00105   int type, pid;
00106   cCaDescr caDescr;
00107   //
00108   cPrgPid(int Type, int Pid) { type=Type; pid=Pid; proc=false; }
00109   bool Proc(void) const { return proc; }
00110   void Proc(bool is) { proc=is; };
00111   };
00112 
00113 // ----------------------------------------------------------------
00114 
00115 class cPrg : public cSimpleItem {
00116 private:
00117   bool isUpdate, pidCaDescr;
00118   //
00119   void Setup(void);
00120 public:
00121   int sid, source, transponder;
00122   cSimpleList<cPrgPid> pids;
00123   cCaDescr caDescr;
00124   //
00125   cPrg(void);
00126   cPrg(int Sid, bool IsUpdate);
00127   bool IsUpdate(void) const { return isUpdate; }
00128   bool HasPidCaDescr(void) const { return pidCaDescr; }
00129   void SetPidCaDescr(bool val) { pidCaDescr=val; }
00130   bool SimplifyCaDescr(void);
00131   void DumpCaDescr(int c);
00132   };
00133 
00134 // ----------------------------------------------------------------
00135 
00136 typedef int caid_t;
00137 
00138 #define MAX_CI_SLOTS      8
00139 #ifdef VDR_MAXCAID
00140 #define MAX_CI_SLOT_CAIDS VDR_MAXCAID
00141 #else
00142 #define MAX_CI_SLOT_CAIDS 16
00143 #endif
00144 
00145 #define MAX_CW_IDX        16
00146 #define MAX_SPLIT_SID     16
00147 
00148 #ifndef SASC
00149 class cCam : public cCiAdapter, public cSimpleItem {
00150 #else
00151 class cCam : public cSimpleItem {
00152 #endif
00153 private:
00154   cDevice *device;
00155   const char *devId;
00156   int adapter, frontend;
00157 #ifndef SASC
00158   cMutex ciMutex;
00159   cRingBufferLinear *rb;
00160   cScCamSlot *slots[MAX_CI_SLOTS];
00161   cCiFrame frame;
00162   //
00163   cDeCSA *decsa;
00164 #endif
00165   cScDevicePlugin *devplugin;
00166   bool softcsa, fullts;
00167   //
00168   cTimeMs caidTimer, triggerTimer;
00169   int version[MAX_CI_SLOTS];
00170   caid_t caids[MAX_CI_SLOTS][MAX_CI_SLOT_CAIDS+1];
00171   int tcid;
00172   bool rebuildcaids;
00173   //
00174   cTimeMs readTimer, writeTimer;
00175   //
00176   cMutex camMutex;
00177   cSimpleList<cEcmHandler> handlerList;
00178   cLogger *logger;
00179   cHookManager *hookman;
00180   int source, transponder, liveVpid, liveApid;
00181   int splitSid[MAX_SPLIT_SID+1];
00182   unsigned char indexMap[MAX_CW_IDX], lastCW[MAX_CW_IDX][2*8];
00183   //
00184   void BuildCaids(bool force);
00185   cEcmHandler *GetHandler(int sid, bool needZero, bool noshift);
00186   void RemHandler(cEcmHandler *handler);
00187   int GetFreeIndex(void);
00188   void LogStartup(void);
00189 protected:
00190 #ifndef SASC
00191   virtual int Read(unsigned char *Buffer, int MaxLength);
00192   virtual void Write(const unsigned char *Buffer, int Length);
00193   virtual bool Reset(int Slot);
00194   virtual eModuleStatus ModuleStatus(int Slot);
00195   virtual bool Assign(cDevice *Device, bool Query=false);
00196 #endif
00197 public:
00198   cCam(cDevice *Device, int Adapter, int Frontend, const char *DevId, cScDevicePlugin *DevPlugin, bool SoftCSA, bool FullTS);
00199   virtual ~cCam();
00200   // CI adapter API
00201   int GetCaids(int slot, unsigned short *Caids, int max);
00202   void CaidsChanged(void);
00203   virtual bool SetCaDescr(ca_descr_t *ca_descr, bool initial);
00204   virtual bool SetCaPid(ca_pid_t *ca_pid);
00205   void Stop(void);
00206   void AddPrg(cPrg *prg);
00207   bool HasPrg(int prg);
00208   // EcmHandler API
00209   void WriteCW(int index, unsigned char *cw, bool force);
00210   void SetCWIndex(int pid, int index);
00211   void DumpAV7110(void);
00212   bool IsSoftCSA(bool live);
00213   int Adapter(void) { return adapter; }
00214   int Frontend(void) { return frontend; }
00215   // System API
00216   void LogEcmStatus(const cEcmInfo *ecm, bool on);
00217   void AddHook(cLogHook *hook);
00218   bool TriggerHook(int id);
00219   // Plugin API
00220   bool Active(bool log);
00221   void HouseKeeping(void);
00222   void Tune(const cChannel *channel);
00223   void PostTune(void);
00224   void SetPid(int type, int pid, bool on);
00225   char *CurrentKeyStr(int num, const char **id);
00226 #ifndef SASC
00227   bool OwnSlot(const cCamSlot *slot) const;
00228   cDeCSA *DeCSA(void) const { return decsa; }
00229 #endif
00230   };
00231 
00232 void LogStatsDown(void);
00233 
00234 // ----------------------------------------------------------------
00235 
00236 #ifndef SASC
00237 
00238 #define MAX_CSA_PIDS 8192
00239 #define MAX_CSA_IDX  16
00240 
00241 class cDeCSA {
00242 private:
00243   int cs;
00244   unsigned char **range, *lastData;
00245   unsigned char pidmap[MAX_CSA_PIDS];
00246   void *keys[MAX_CSA_IDX];
00247   unsigned int even_odd[MAX_CSA_IDX], flags[MAX_CSA_IDX];
00248   cMutex mutex;
00249   cCondVar wait;
00250   cTimeMs stall;
00251   bool active;
00252   const char *devId;
00253   //
00254   bool GetKeyStruct(int idx);
00255   void ResetState(void);
00256 public:
00257   cDeCSA(const char *DevId);
00258   ~cDeCSA();
00259   bool Decrypt(unsigned char *data, int len, bool force);
00260   bool SetDescr(ca_descr_t *ca_descr, bool initial);
00261   bool SetCaPid(ca_pid_t *ca_pid);
00262   void SetActive(bool on);
00263   };
00264 #endif //!SASC
00265 
00266 #endif // ___CAM_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines