📄 mifare1.lst
字号:
C51 COMPILER V8.00 MIFARE1 04/23/2009 15:56:16 PAGE 1
C51 COMPILER V8.00, COMPILATION OF MODULE MIFARE1
OBJECT MODULE PLACED IN .\out_sst89e564\mifare1.obj
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE source\mifare1.c LARGE BROWSE ORDER NOAREGS DEBUG OBJECTEXTEND PRINT(.\mifa
-re1.lst) OBJECT(.\out_sst89e564\mifare1.obj)
line level source
1 /****************************************
2 * mifare1.c *
3 * Designe by 刘俐训 *
4 * excute command for m1 card *
5 * check if a card is actived *
6 * last update: 05-01-2007 *
7 ****************************************/
8 #include "includes.h"
9
10 /* About auto search rf card: the default value is read from
11 * the configration table and it is configrable, the current
12 * value can be enable or disable by a command.
13 */
14
15 static unsigned char snr[4]; // serial number of m1 card
16 static bit fWaitCmd = 0; // flag of waiting for command
17
18
19 unsigned char snr[4];
20 unsigned char oldsnr[4];
21 unsigned char m1keys[16][6]; // save 16 keys max
22 unsigned char m1keystatus[16]; // sector id and a/b flag
23 unsigned char m1keycnt = 0;
24 unsigned char code m1keycntmax = 16;
25
26
27 // 按照8086的格式(低位在前,高位在后)进行转换
28 long buftolong86(char *buf);
29 void longtobuf86(long val, char *buf);
30
31 // 按照8051的格式(高位在前,低位在后)进行转换
32 long buftolong51(char *buf);
33 void longtobuf51(long val, char *buf);
34 unsigned char M1Auth(unsigned char keyab, unsigned char *key, unsigned char sec);
35
36
37 void M1Init(void)
38 {
39 1 int ch;
40 1
41 1 // rf card
42 1 M500PcdInit(); // remember to set if we should set auto search rf card function
43 1 M500PcdMfOutSelect(3);
44 1
45 1 ch = ParaRead(PARA_RFRXGAIN);
46 1 if((ch >= 0) && (ch <= 3))
47 1 {
48 2 M500SetRcvGain((unsigned char)ch);
49 2 }
50 1 else
51 1 {
52 2 ParaWrite(PARA_RFRXGAIN, 2);
53 2 M500SetRcvGain(2);
54 2 }
C51 COMPILER V8.00 MIFARE1 04/23/2009 15:56:16 PAGE 2
55 1 }
56
57 // check if a rf card is arrived and if I have sent a notice to host
58 // but the host give no response, I will send the notice once every
59 // second untill I get a response or a command for rf card.
60 void M1Card(void)
61 {
62 1 static unsigned int waitcmdtime = 0;
63 1 unsigned char buf[4];
64 1 static bit fCardIn = 0; // flag of card in
65 1 static unsigned char cnt = 0;
66 1
67 1 if(fAutoSearchCard)
68 1 {
69 2 if(M1Search(snr))
70 2 {
71 3 if(!fCardIn)
72 3 {
73 4 fCardIn = 1;
74 4
75 4 // beep
76 4 if(fRFCBeep)
77 4 {
78 5 beep(2, 10);
79 5 }
80 4
81 4 // light
82 4 CardLed(1, 1);
83 4
84 4 if(memcmp(snr, oldsnr, 4)) // 不相等
85 4 {
86 5 memcpy(oldsnr, snr, 4);
87 5 m1keycnt = 0;
88 5 }
89 4
90 4 // 通知主机有卡
91 4 if(fNotice)
92 4 {
93 5 buf[0] = RFCARD;
94 5 buf[1] = 1;
95 5
96 5 if(!fStopOnACK)
97 5 {
98 6 CommSendNotWaitAck(buf, 2);
99 6 waitcmdtime = GetTickCount();
100 6 fWaitCmd = 1;
101 6 }
102 5 else if(!CommSend(buf, 2))
103 5 {
104 6 fCardIn = 0;
105 6 CardLed(1, 0);
106 6
107 6 cnt ++;
108 6 // if 3 times host no response, beep to notice customer
109 6 if(cnt > 3)
110 6 {
111 7 cnt = 0;
112 7 beep(3, 25);
113 7 }
114 6 }
115 5 }
116 4 }
C51 COMPILER V8.00 MIFARE1 04/23/2009 15:56:16 PAGE 3
117 3 }
118 2 else
119 2 {
120 3 cnt = 0;
121 3 if(fCardIn)
122 3 {
123 4 fCardIn = 0;
124 4 fWaitCmd = 0;
125 4
126 4 if(fRFCBeep)
127 4 {
128 5 beep(1, 40);
129 5 }
130 4
131 4 CardLed(1, 0);
132 4
133 4 if(fNotice)
134 4 {
135 5 buf[0] = RFCARD;
136 5 buf[1] = 0;
137 5
138 5 #if 1
139 5 CommSendNotWaitAck(buf, 2); /////////////////////////////////////
140 5 #else
CommSend(buf, 2);
#endif
143 5 }
144 4 }
145 3 }
146 2 }
147 1
148 1 if(fWaitCmd)
149 1 {
150 2 if(GetTickCount() - waitcmdtime >= 1000)
151 2 {
152 3 fCardIn = 0;
153 3 fWaitCmd = 0;
154 3 beep(3, 25);
155 3 }
156 2 }
157 1 }
158
159 // excute the command for m1 card from host
160 unsigned int M1Command(unsigned char *cmd, unsigned int len)
161 {
162 1 unsigned char m1sector; // m1卡命令针对的扇区
163 1 unsigned char m1block; // m1卡命令针对的块号
164 1 long epvalue;
165 1 long oldepvalue;
166 1 unsigned char epstatus = 0x03;
167 1 unsigned char m1keyidx;
168 1 bit fkeypassed;
169 1 unsigned char *m1data;
170 1 int i;
171 1
172 1 // clear waiting cmd flag
173 1 fWaitCmd = 0;
174 1
175 1 m1sector = cmd[2];
176 1 m1block = cmd[3];
177 1 m1data = cmd + 4;
178 1
C51 COMPILER V8.00 MIFARE1 04/23/2009 15:56:16 PAGE 4
179 1 ///////////////////////////////////////
180 1 // request card!
181 1 if(!M1Search(snr))
182 1 {
183 2 // error
184 2 cmd[0] = FAIL;
185 2 cmd[1] = MI_NOTAGERR;
186 2 return 2;
187 2 }
188 1
189 1 if(cmd[1] == CMD_GETSNR) // get card snr
190 1 {
191 2 if(len != 2)
192 2 {
193 3 cmd[0] = ERRORCMDLEN;
194 3 return 1;
195 3 }
196 2
197 2 // copy snr to cmd
198 2 cmd[0] = SUCCESS;
199 2 memcpy(cmd+1, snr, 4);
200 2 return 5;
201 2 }
202 1
203 1 if(cmd[1] == CMD_AUTH) // auth card with specified key
204 1 {
205 2 if(len != 10)
206 2 {
207 3 cmd[0] = ERRORCMDLEN;
208 3 return 1;
209 3 }
210 2
211 2 m1sector &= 0x7f; // mask the invalid msb
212 2 i = M1Auth(m1block, m1data, m1sector);
213 2 if(MI_OK != i)
214 2 {
215 3 cmd[0] = FAIL;
216 3 cmd[1] = i;
217 3 return 2;
218 3 }
219 2 else
220 2 {
221 3 cmd[0] = SUCCESS;
222 3 return 1;
223 3 }
224 2 }
225 1 else if(cmd[1] == CMD_LDKEY)
226 1 {
227 2 if((len < 10) && (len != 2))
228 2 {
229 3 cmd[0] = ERRORCMDLEN;
230 3 return 1;
231 3 }
232 2
233 2 len -= 2;
234 2 if((len & 0x07) != 0) // len % 8
235 2 {
236 3 cmd[0] = ERRORCMDLEN;
237 3 return 1;
238 3 }
239 2
240 2 len >>= 3; // len = len / 8;
C51 COMPILER V8.00 MIFARE1 04/23/2009 15:56:16 PAGE 5
241 2 if(len > m1keycntmax) // too much keys
242 2 {
243 3 cmd[0] = ERRORCMDLEN;
244 3 return 1;
245 3 }
246 2
247 2 // check if there are same key sector in the data
248 2 for(i=0; i<len-1; i++)
249 2 {
250 3 unsigned char i1;
251 3 bit fsame = 0;
252 3
253 3 for(i1=i+1; i1<len; i1++)
254 3 {
255 4 if(cmd[2+(i<<3)] > 0x7f)
256 4 {
257 5 fsame = 1;
258 5 break;
259 5 }
260 4 if(cmd[2+(i<<3)] == cmd[2+(i1<<3)])
261 4 {
262 5 fsame = 1;
263 5 break;
264 5 }
265 4 }
266 3 if(fsame)
267 3 break;
268 3 }
269 2 if(i < len-1) // found same sector in buffer
270 2 {
271 3 cmd[0] = ERRORCMD;
272 3 return 1;
273 3 }
274 2
275 2 // cmd --> m1sector
276 2 cmd += 2;
277 2
278 2 // copy to key buffer
279 2 for(i=0; i<len; i++)
280 2 {
281 3 m1sector = cmd[0] & 0x7f;
282 3 m1block = cmd[1];
283 3 m1data = cmd + 2;
284 3
285 3 // store key
286 3 memcpy(m1keys[i], m1data, 6);
287 3 if(!m1block)
288 3 m1keystatus[i] = m1sector & 0x7f;
289 3 else
290 3 m1keystatus[i] = m1sector | 0x80;
291 3
292 3 cmd += 8;
293 3 }
294 2 m1keycnt = i;
295 2
296 2 cmd[0] = SUCCESS;
297 2 cmd[1] = m1keycnt;
298 2 return 2;
299 2 }
300 1
301 1 // 其他命令,首先作认证
302 1 // search key for current sector
C51 COMPILER V8.00 MIFARE1 04/23/2009 15:56:16 PAGE 6
303 1 m1keyidx = 0xff;
304 1 fkeypassed = 0;
305 1 for(i=0; i<m1keycnt; i++)
306 1 {
307 2 if(m1sector == (m1keystatus[i] & 0x7f))
308 2 {
309 3 m1keyidx = i;
310 3 break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -