SASC
DVBSoftCAM

smartcard.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 ___SMARTCARD_H
00021 #define ___SMARTCARD_H
00022 
00023 #include <vdr/thread.h>
00024 #include "data.h"
00025 #include "misc.h"
00026 
00027 // ----------------------------------------------------------------
00028 
00029 #define MAX_LEN     256        // max. response length
00030 #define CMD_LEN     5          // command length
00031 #define INS_IDX     1          // INS index
00032 #define LEN_IDX     4          // LEN index
00033 
00034 #define SB_LEN      2          // status byte (SB) len
00035 #define MAX_ATR_LEN 33         // max. ATR length
00036 #define MAX_HIST    15         // max. number of historical characters
00037 #define IDSTR_LEN   25         // lenght of card identify string
00038 
00039 #define MAKE_SC_ID(a,b,c,d) (((a)<<24)+((b)<<16)+((c)<<8)+(d))
00040 
00041 // ----------------------------------------------------------------
00042 
00043 class cSmartCards;
00044 class cSmartCardSlot;
00045 
00046 // ----------------------------------------------------------------
00047 
00048 class cInfoStr : public cLineBuff {
00049 private:
00050   cMutex mutex;
00051   char *current;
00052 public:
00053   cInfoStr(void);
00054   ~cInfoStr();
00055   // Query API
00056   bool Get(char *buff, int len);
00057   // Construct API
00058   void Begin(void);
00059   void Finish();
00060   };
00061 
00062 // ----------------------------------------------------------------
00063 
00064 class cSmartCardData : public cStructItem {
00065 protected:
00066   int ident;
00067 public:
00068   cSmartCardData(int Ident);
00069   virtual ~cSmartCardData() {}
00070   virtual bool Parse(const char *line)=0;
00071   virtual bool Matches(cSmartCardData *cmp)=0;
00072   int Ident(void) const { return ident; }
00073   };
00074 
00075 // ----------------------------------------------------------------
00076 
00077 #define SM_NONE 0
00078 #define SM_8E2  1
00079 #define SM_8O2  2
00080 #define SM_8N2  3
00081 #define SM_MAX  4
00082 #define SM_MASK 0x1F
00083 #define SM_1SB  0x80
00084 
00085 #define SM_DIRECT       0
00086 #define SM_INDIRECT     1
00087 #define SM_INDIRECT_INV 2
00088 
00089 struct CardConfig {
00090   int SerMode;
00091   int workTO, serTO, serDL; // values in ms
00092   };
00093 
00094 struct StatusMsg {
00095   unsigned char sb[SB_LEN];
00096   const char *message; 
00097   bool retval;
00098   };
00099 
00100 struct Atr {
00101   int T, F, fs, N, WI, BWI, CWI, TA1, Tspec;
00102   float D;
00103   int wwt, bwt;
00104   int atrLen, histLen;
00105   int convention;
00106   unsigned char atr[MAX_ATR_LEN];
00107   unsigned char hist[MAX_HIST];
00108   };
00109 
00110 class cSmartCard : public cMutex {
00111 private:
00112   cSmartCardSlot *slot;
00113   int slotnum;
00114   const struct CardConfig *cfg;
00115   const struct StatusMsg *msg;
00116   bool cardUp;
00117 protected:
00118   const struct Atr *atr;
00119   unsigned char *sb;
00120   char idStr[IDSTR_LEN];
00121   cInfoStr infoStr;
00122   //
00123   int SerRead(unsigned char *data, int len, int to=0);
00124   int SerWrite(const unsigned char *data, int len);
00125   bool IsoRead(const unsigned char *cmd, unsigned char *data);
00126   bool IsoWrite(const unsigned char *cmd, const unsigned char *data);
00127   bool Status(void);
00128   void NewCardConfig(const struct CardConfig *Cfg);
00129   int CheckSctLen(const unsigned char *data, int off);
00130   void CaidsChanged(void);
00131 public:
00132   cSmartCard(const struct CardConfig *Cfg, const struct StatusMsg *Msg);
00133   virtual ~cSmartCard() {};
00134   virtual bool Init(void)=0;
00135   virtual bool Decode(const cEcmInfo *ecm, const unsigned char *data, unsigned char *cw)=0;
00136   virtual bool Update(int pid, int caid, const unsigned char *data) { return false; }
00137   virtual bool CanHandle(unsigned short CaId) { return true; }
00138   //
00139   bool Setup(cSmartCardSlot *Slot, int sermode, const struct Atr *Atr, unsigned char *Sb);
00140   bool CardUp(void) { return cardUp; }
00141   bool GetCardIdStr(char *str, int len);
00142   bool GetCardInfoStr(char *str, int len);
00143   };
00144 
00145 // ----------------------------------------------------------------
00146 
00147 class cSmartCardLink {
00148 friend class cSmartCards;
00149 private:
00150   cSmartCardLink *next;
00151   const char *name;
00152   int id;
00153 public:
00154   cSmartCardLink(const char *Name, int Id);
00155   const char *Name(void) { return name; }
00156   int Id(void) { return id; }
00157   virtual ~cSmartCardLink() {}
00158   virtual cSmartCard *Create(void)=0;
00159   virtual cSmartCardData *CreateData(void) { return 0; }
00160   };
00161 
00162 // ----------------------------------------------------------------
00163 
00164 class cSmartCards {
00165 friend class cSmartCardLink;
00166 private:
00167   static cSmartCardLink *first;
00168   //
00169   static void Register(cSmartCardLink *scl);
00170 public:
00171   void Shutdown(void);
00172   void Disable(void);
00173   static cSmartCardLink *First(void) { return first; }
00174   static cSmartCardLink *Next(cSmartCardLink *scl) { return scl->next; }
00175   // to be called ONLY from a system class!
00176   cSmartCardData *FindCardData(cSmartCardData *param);
00177   bool HaveCard(int id);
00178   cSmartCard *LockCard(int id);
00179   void ReleaseCard(cSmartCard *sc);
00180   // to be called ONLY from frontend thread!
00181   bool ListCard(int num, char *str, int len);
00182   bool CardInfo(int num, char *str, int len);
00183   void CardReset(int num);
00184   };
00185 
00186 extern cSmartCards smartcards;
00187 
00188 #endif //___SMARTCARD_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines