forked from aiolos/sasc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmartcard.h
188 lines (163 loc) · 5.28 KB
/
smartcard.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
* Softcam plugin to VDR (C++)
*
* This code is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This code is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
* Or, point your browser to http://www.gnu.org/copyleft/gpl.html
*/
#ifndef ___SMARTCARD_H
#define ___SMARTCARD_H
#include <vdr/thread.h>
#include "data.h"
#include "misc.h"
// ----------------------------------------------------------------
#define MAX_LEN 256 // max. response length
#define CMD_LEN 5 // command length
#define INS_IDX 1 // INS index
#define LEN_IDX 4 // LEN index
#define SB_LEN 2 // status byte (SB) len
#define MAX_ATR_LEN 33 // max. ATR length
#define MAX_HIST 15 // max. number of historical characters
#define IDSTR_LEN 25 // lenght of card identify string
#define MAKE_SC_ID(a,b,c,d) (((a)<<24)+((b)<<16)+((c)<<8)+(d))
// ----------------------------------------------------------------
class cSmartCards;
class cSmartCardSlot;
// ----------------------------------------------------------------
class cInfoStr : public cLineBuff {
private:
cMutex mutex;
char *current;
public:
cInfoStr(void);
~cInfoStr();
// Query API
bool Get(char *buff, int len);
// Construct API
void Begin(void);
void Finish();
};
// ----------------------------------------------------------------
class cSmartCardData : public cStructItem {
protected:
int ident;
public:
cSmartCardData(int Ident);
virtual ~cSmartCardData() {}
virtual bool Parse(const char *line)=0;
virtual bool Matches(cSmartCardData *cmp)=0;
int Ident(void) const { return ident; }
};
// ----------------------------------------------------------------
#define SM_NONE 0
#define SM_8E2 1
#define SM_8O2 2
#define SM_8N2 3
#define SM_MAX 4
#define SM_MASK 0x1F
#define SM_1SB 0x80
#define SM_DIRECT 0
#define SM_INDIRECT 1
#define SM_INDIRECT_INV 2
struct CardConfig {
int SerMode;
int workTO, serTO, serDL; // values in ms
};
struct StatusMsg {
unsigned char sb[SB_LEN];
const char *message;
bool retval;
};
struct Atr {
int T, F, fs, N, WI, BWI, CWI, TA1, Tspec;
float D;
int wwt, bwt;
int atrLen, histLen;
int convention;
unsigned char atr[MAX_ATR_LEN];
unsigned char hist[MAX_HIST];
};
class cSmartCard : public cMutex {
private:
cSmartCardSlot *slot;
int slotnum;
const struct CardConfig *cfg;
const struct StatusMsg *msg;
bool cardUp;
protected:
const struct Atr *atr;
unsigned char *sb;
char idStr[IDSTR_LEN];
cInfoStr infoStr;
//
int SerRead(unsigned char *data, int len, int to=0);
int SerWrite(const unsigned char *data, int len);
bool IsoRead(const unsigned char *cmd, unsigned char *data);
bool IsoWrite(const unsigned char *cmd, const unsigned char *data);
bool Status(void);
void NewCardConfig(const struct CardConfig *Cfg);
int CheckSctLen(const unsigned char *data, int off);
void CaidsChanged(void);
public:
cSmartCard(const struct CardConfig *Cfg, const struct StatusMsg *Msg);
virtual ~cSmartCard() {};
virtual bool Init(void)=0;
virtual bool Decode(const cEcmInfo *ecm, const unsigned char *data, unsigned char *cw)=0;
virtual bool Update(int pid, int caid, const unsigned char *data) { return false; }
virtual bool CanHandle(unsigned short CaId) { return true; }
//
bool Setup(cSmartCardSlot *Slot, int sermode, const struct Atr *Atr, unsigned char *Sb);
bool CardUp(void) { return cardUp; }
bool GetCardIdStr(char *str, int len);
bool GetCardInfoStr(char *str, int len);
};
// ----------------------------------------------------------------
class cSmartCardLink {
friend class cSmartCards;
private:
cSmartCardLink *next;
const char *name;
int id;
public:
cSmartCardLink(const char *Name, int Id);
const char *Name(void) { return name; }
int Id(void) { return id; }
virtual ~cSmartCardLink() {}
virtual cSmartCard *Create(void)=0;
virtual cSmartCardData *CreateData(void) { return 0; }
};
// ----------------------------------------------------------------
class cSmartCards {
friend class cSmartCardLink;
private:
static cSmartCardLink *first;
//
static void Register(cSmartCardLink *scl);
public:
void Shutdown(void);
void Disable(void);
static cSmartCardLink *First(void) { return first; }
static cSmartCardLink *Next(cSmartCardLink *scl) { return scl->next; }
// to be called ONLY from a system class!
cSmartCardData *FindCardData(cSmartCardData *param);
bool HaveCard(int id);
cSmartCard *LockCard(int id);
void ReleaseCard(cSmartCard *sc);
// to be called ONLY from frontend thread!
bool ListCard(int num, char *str, int len);
bool CardInfo(int num, char *str, int len);
void CardReset(int num);
};
extern cSmartCards smartcards;
#endif //___SMARTCARD_H