📄 ds8007.lst
字号:
246 1 EDC = generateEDC(type,PCB,EDC);
247 1 writeByte(length);
248 1 EDC = generateEDC(type,length,EDC);
249 1 for (i=0;i<length;i++)
250 1 {
251 2 writeByte(buffer[i]);
252 2 EDC = generateEDC(type,buffer[i],EDC);
253 2 }
254 1
255 1 if (type == EDC_TYPE_LRC)
256 1 {
257 2 writeLastByte(EDC);
258 2 }
259 1 else // type = CRC
260 1 {
261 2 writeByte(EDC);
262 2 writeLastByte(EDC>>8);
263 2 }
264 1 }
265
266 /*
267 Receive a T=1 formatted block.
268 This does a raw receive, it does not check sequence numbers.
269 */
270 int16_t receiveBlock(uint8_t *rNAD,uint8_t *rPCB,uint8_t *rLEN,uint8_t *buffer,uint8_t type)
271 {
272 1 int16_t retval;
273 1 int16_t index = 0;
274 1 uint16_t EDC;
275 1 int16_t i;
276 1 uint8_t expectedLength;
277 1
278 1 // Get NAD, PCB, and Length
279 1 retval = readByte();
280 1 if (retval < 0) return retval;
281 1 *rNAD = retval;
282 1 retval = readByte();
283 1 if (retval < 0) return retval;
284 1 *rPCB = retval;
285 1 retval = readByte();
286 1 if (retval < 0) return retval;
287 1 *rLEN = retval;
288 1
289 1
290 1 // Add one to length for LRC
291 1 expectedLength = *rLEN+1;
292 1 // Add additional byte if using CRC
293 1 if (type == EDC_TYPE_CRC)
294 1 expectedLength++;
295 1
296 1 // Get all data bytes plus EDC (1 or 2 bytes at end)
297 1 for (i = 0;i < expectedLength;i++)
298 1 {
299 2 retval = readByte();
300 2 if (retval < 0)
301 2 {
302 3 return retval;
C51 COMPILER V8.08 DS8007 10/26/2007 09:32:25 PAGE 6
303 3 }
304 2 buffer[index++] = retval;
305 2 }
306 1
307 1 // Check the LRC or CRC
308 1 if (type == EDC_TYPE_LRC)
309 1 {
310 2 EDC = 0;
311 2 EDC = generateEDC(EDC_TYPE_LRC,*rNAD,EDC);
312 2 EDC = generateEDC(EDC_TYPE_LRC,*rPCB,EDC);
313 2 EDC = generateEDC(EDC_TYPE_LRC,*rLEN,EDC);
314 2 for (i = 0;i < index;i++)
315 2 {
316 3 EDC = generateEDC(EDC_TYPE_LRC,buffer[i],EDC);
317 3 }
318 2 if (EDC != 0)
319 2 {
320 3 return ERR_RECEIVE_LRC;
321 3 }
322 2 }
323 1 else // EDC is CRC
324 1 {
325 2 EDC = 0xFFFF;
326 2 EDC = generateEDC(EDC_TYPE_LRC,*rNAD,EDC);
327 2 EDC = generateEDC(EDC_TYPE_LRC,*rPCB,EDC);
328 2 EDC = generateEDC(EDC_TYPE_LRC,*rLEN,EDC);
329 2 for (i = 0;i < (index-2);i++)
330 2 {
331 3 EDC = generateEDC(EDC_TYPE_CRC,buffer[i],EDC);
332 3 }
333 2
334 2 if (((EDC >> 8) != buffer[index-2]) ||
335 2 ((EDC & 0xFF) != buffer[index-1]))
336 2 {
337 3 return ERR_RECEIVE_CRC;
338 3 }
339 2 }
340 1 return *rLEN;
341 1 }
342
343 /*
344 Send an S block for setting IFSD (card terminal buffer size)
345 */
346 int16_t dssc_sendsblockIFSD(uint8_t IFSD)
347 {
348 1 uint8_t buffer[1];
349 1 uint8_t rNAD,rPCB,rLEN;
350 1 uint8_t rbuffer[100];
351 1
352 1 buffer[0] = IFSD;
353 1 sendBlock(0,0xC1,1,buffer,EDCtype[currentSlot]);
354 1 receiveBlock(&rNAD,&rPCB,&rLEN,rbuffer,EDCtype[currentSlot]);
355 1
356 1 // If we do not get a confirmation response, fail.
357 1 if ((rPCB == 0xE1) && (rLEN == 1) && (rbuffer[0] == IFSD))
358 1 return 0;
359 1 else
360 1 return ERR_SETIFSD_FAILURE;
361 1 }
362
363 /*
364 Set Node address info for this card
C51 COMPILER V8.08 DS8007 10/26/2007 09:32:25 PAGE 7
365 */
366 void dssc_setNAD(uint8_t value)
367 {
368 1 NAD[currentSlot] = value;
369 1 }
370
371 /*
372 Send a message using T=1 protocol
373 */
374 int16_t dssc_sendAPDUT1(uint8_t *buffer,int16_t length,uint8_t *rbuffer)
375 {
376 1 int16_t maxSegLength = IFSC[currentSlot]; // Set by ATR (TA3), or default to 0x20
377 1 int16_t retval;
378 1 uint8_t sequenceNumber = 0;
379 1 uint8_t rNAD,rPCB,rLEN;
380 1 uint8_t tempbuffer[512];
381 1
382 1 while (length > maxSegLength)
383 1 {
384 2 // Send block with chaining mode, current sequence number, and maximum length.
385 2 sendBlock(NAD[currentSlot],sequenceNumber|0x20,maxSegLength,buffer,EDCtype[currentSlot]);
386 2 retval = receiveBlock(&rNAD,&rPCB,&rLEN,tempbuffer,EDCtype[currentSlot]);
387 2 if (retval < 0) return retval;
388 2 // Check for bad sequence number return
389 2 if ((rPCB ^ ((sequenceNumber>>2) ^ 0x10)) != 0x80)
390 2 {
391 3 return ERR_RECEIVE_SEQUENCENUM;
392 3 }
393 2 sequenceNumber ^= 0x40;
394 2 buffer += maxSegLength;
395 2 length -= maxSegLength;
396 2 }
397 1
398 1 // Send last (or only) block. Then, we can wait for the receive side.
399 1 sendBlock(NAD[currentSlot],sequenceNumber,length,buffer,EDCtype[currentSlot]);
400 1
401 1 retval = receiveBlock(&rNAD,&rPCB,&rLEN,tempbuffer,EDCtype[currentSlot]);
402 1 if (retval < 0) return retval;
403 1
404 1 memcpy(rbuffer,tempbuffer,rLEN);
405 1
406 1 return retval;
407 1 }
408
409 /*
410 Send a message using T=0 protocol
411 */
412 int16_t dssc_sendAPDUT0(uint8_t *buffer,int16_t length,uint8_t *rbuffer)
413 {
414 1 int index;
415 1 int16_t rindex = 0;
416 1 uint8_t val;
417 1 uint8_t INS = buffer[1];
418 1 int retval;
419 1
420 1 // Write 5 byte command header
421 1 for (index = 0;index < 4;)
422 1 writeByte(buffer[index++]);
423 1 writeLastByte(buffer[index++]);
424 1
425 1 while (1)
426 1 {
C51 COMPILER V8.08 DS8007 10/26/2007 09:32:25 PAGE 8
427 2 // Get procedure byte
428 2 retval = readByte();
429 2 if (retval < 0)
430 2 {
431 3 return retval;
432 3 }
433 2 val = retval;
434 2
435 2 if ((val & 0xFE) == INS)
436 2 {
437 3 // ACK, send/receive all remaining bytes
438 3 if (index < length)
439 3 {
440 4 for (;index < (length-1);index++)
441 4 writeByte(buffer[index]);
442 4 if (index < length)
443 4 writeLastByte(buffer[index++]);
444 4 // NOTE: Does not support VPP state change
445 4 }
446 3 else
447 3 {
448 4 // Read bytes up to Lc/P3 + 2
449 4 rindex = 0;
450 4 while ( (rindex < (buffer[4] + 2)) && ((retval = readByte()) >= 0) )
451 4 {
452 5 rbuffer[rindex++] = retval;
453 5 }
454 4 // return any error.
455 4 if (retval < 0)
456 4 return retval;
457 4 break;
458 4 }
459 3 }
460 2 else if ((val & 0xFE) == ~INS)
461 2 {
462 3 if (index < length)
463 3 {
464 4 // ACK, send/receive one remaining byte
465 4 if (index < length)
466 4 writeLastByte(buffer[index++]);
467 4 // NOTE: Does not support VPP state change
468 4 }
469 3 else
470 3 {
471 4 // Read one byte or timeout????
472 4 retval = readByte();
473 4 if (retval < 0)
474 4 {
475 5 // If we get anything other than a timeout, return the error.
476 5 if (retval != ERR_RECEIVE_TIMEOUT)
477 5 return retval;
478 5 break;
479 5 }
480 4 else
481 4 rbuffer[rindex++] = retval;
482 4 }
483 3 }
484 2 else if (val == 0x60)
485 2 {
486 3 // NULL
487 3 }
488 2 else if (((val & 0xF0) == 0x60) || ((val & 0xF0) == 0x90))
C51 COMPILER V8.08 DS8007 10/26/2007 09:32:25 PAGE 9
489 2 {
490 3 // SW1, get SW2
491 3 rbuffer[rindex++]=val;
492 3 val = readByte();
493 3 if (retval < 0) return retval;
494 3 rbuffer[rindex++]=val;
495 3 break;
496 3 }
497 2
498 2 }
499 1
500 1 return rindex;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -