ccid.h 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #pragma once
  2. #include <stdlib.h> // malloc
  3. #include <stdint.h> // uint32_t
  4. #include <stdarg.h> // __VA_ARGS__
  5. #include <string.h>
  6. #include <stdio.h>
  7. #include "seader_bridge.h"
  8. #include "seader_worker_i.h"
  9. #define SYNC (0x03)
  10. #define CTRL (0x06)
  11. #define NAK (0x15)
  12. #define BMICCSTATUS_MASK 0x03
  13. #define CARD_OUT 0x02
  14. #define CARD_IN_1 0x03
  15. #define CARD_IN_2 0x06
  16. #define CARD_IN_BOTH 0x07
  17. /*
  18. * * BULK_OUT messages from PC to Reader
  19. * * Defined in CCID Rev 1.1 6.1 (page 26)
  20. * */
  21. #define CCID_MESSAGE_TYPE_PC_to_RDR_IccPowerOn 0x62
  22. #define CCID_MESSAGE_TYPE_PC_to_RDR_IccPowerOff 0x63
  23. #define CCID_MESSAGE_TYPE_PC_to_RDR_GetSlotStatus 0x65
  24. #define CCID_MESSAGE_TYPE_PC_to_RDR_XfrBlock 0x6f
  25. #define CCID_MESSAGE_TYPE_PC_to_RDR_GetParameters 0x6c
  26. #define CCID_MESSAGE_TYPE_PC_to_RDR_ResetParameters 0x6d
  27. #define CCID_MESSAGE_TYPE_PC_to_RDR_SetParameters 0x61
  28. #define CCID_MESSAGE_TYPE_PC_to_RDR_Escape 0x6b
  29. #define CCID_MESSAGE_TYPE_PC_to_RDR_IccClock 0x6e
  30. #define CCID_MESSAGE_TYPE_PC_to_RDR_T0APDU 0x6a
  31. #define CCID_MESSAGE_TYPE_PC_to_RDR_Secure 0x69
  32. #define CCID_MESSAGE_TYPE_PC_to_RDR_Mechanical 0x71
  33. #define CCID_MESSAGE_TYPE_PC_to_RDR_Abort 0x72
  34. #define CCID_MESSAGE_TYPE_PC_to_RDR_SetDataRateAndClockFrequency 0x73
  35. /*
  36. * * BULK_IN messages from Reader to PC
  37. * * Defined in CCID Rev 1.1 6.2 (page 48)
  38. * */
  39. #define CCID_MESSAGE_TYPE_RDR_to_PC_DataBlock 0x80
  40. #define CCID_MESSAGE_TYPE_RDR_to_PC_SlotStatus 0x81
  41. #define CCID_MESSAGE_TYPE_RDR_to_PC_Parameters 0x82
  42. #define CCID_MESSAGE_TYPE_RDR_to_PC_Escape 0x83
  43. #define CCID_MESSAGE_TYPE_RDR_to_PC_DataRateAndClockFrequency 0x84
  44. /*
  45. * * INTERRUPT_IN messages from Reader to PC
  46. * * Defined in CCID Rev 1.1 6.3 (page 56)
  47. * */
  48. #define CCID_MESSAGE_TYPE_RDR_to_PC_NotifySlotChange 0x50
  49. #define CCID_MESSAGE_TYPE_RDR_to_PC_HardwareError 0x51
  50. /* Status codes that go in bStatus (see 6.2.6) */
  51. enum { ICC_STATUS_PRESENT_ACTIVE = 0, ICC_STATUS_PRESENT_INACTIVE, ICC_STATUS_NOT_PRESENT };
  52. enum {
  53. COMMAND_STATUS_NO_ERROR = 0,
  54. COMMAND_STATUS_FAILED,
  55. COMMAND_STATUS_TIME_EXTENSION_REQUIRED
  56. };
  57. /* Error codes that go in bError (see 6.2.6) */
  58. enum {
  59. ERROR_CMD_NOT_SUPPORTED = 0,
  60. ERROR_CMD_ABORTED = -1,
  61. ERROR_ICC_MUTE = -2,
  62. ERROR_XFR_PARITY_ERROR = -3,
  63. ERROR_XFR_OVERRUN = -4,
  64. ERROR_HW_ERROR = -5,
  65. };
  66. struct CCID_Message {
  67. uint8_t bMessageType;
  68. uint32_t dwLength;
  69. uint8_t bSlot;
  70. uint8_t bSeq;
  71. uint8_t bStatus;
  72. uint8_t bError;
  73. uint8_t* payload;
  74. size_t consumed;
  75. };
  76. void check_for_sam(SeaderUartBridge* seader_uart);
  77. void PC_to_RDR_IccPowerOn(SeaderUartBridge* seader_uart);
  78. void PC_to_RDR_GetSlotStatus(SeaderUartBridge* seader_uart);
  79. void PC_to_RDR_SetParameters(SeaderUartBridge* seader_uart);
  80. void PC_to_RDR_GetParameters(SeaderUartBridge* seader_uart);
  81. void PC_to_RDR_XfrBlock(SeaderUartBridge* seader_uart, uint8_t* data, size_t len);
  82. size_t processCCID(SeaderWorker* seader_worker, uint8_t* cmd, size_t cmd_len);