ccid.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. /*
  14. * Bit 0 = Slot 0 current state
  15. * Bit 1 = Slot 0 changed status
  16. * Bit 2 = Slot 1 current state
  17. * Bit 3 = Slot 1 changed status
  18. */
  19. // TODO: rename/renumber
  20. #define SLOT_0_MASK 0x03
  21. #define CARD_OUT_1 0x02
  22. #define CARD_IN_1 0x03
  23. #define SLOT_1_MASK 0x0C
  24. #define CARD_IN_2 0x04
  25. #define CARD_OUT_2 0x0C
  26. /*
  27. * * BULK_OUT messages from PC to Reader
  28. * * Defined in CCID Rev 1.1 6.1 (page 26)
  29. * */
  30. #define CCID_MESSAGE_TYPE_PC_to_RDR_IccPowerOn 0x62
  31. #define CCID_MESSAGE_TYPE_PC_to_RDR_IccPowerOff 0x63
  32. #define CCID_MESSAGE_TYPE_PC_to_RDR_GetSlotStatus 0x65
  33. #define CCID_MESSAGE_TYPE_PC_to_RDR_XfrBlock 0x6f
  34. #define CCID_MESSAGE_TYPE_PC_to_RDR_GetParameters 0x6c
  35. #define CCID_MESSAGE_TYPE_PC_to_RDR_ResetParameters 0x6d
  36. #define CCID_MESSAGE_TYPE_PC_to_RDR_SetParameters 0x61
  37. #define CCID_MESSAGE_TYPE_PC_to_RDR_Escape 0x6b
  38. #define CCID_MESSAGE_TYPE_PC_to_RDR_IccClock 0x6e
  39. #define CCID_MESSAGE_TYPE_PC_to_RDR_T0APDU 0x6a
  40. #define CCID_MESSAGE_TYPE_PC_to_RDR_Secure 0x69
  41. #define CCID_MESSAGE_TYPE_PC_to_RDR_Mechanical 0x71
  42. #define CCID_MESSAGE_TYPE_PC_to_RDR_Abort 0x72
  43. #define CCID_MESSAGE_TYPE_PC_to_RDR_SetDataRateAndClockFrequency 0x73
  44. /*
  45. * * BULK_IN messages from Reader to PC
  46. * * Defined in CCID Rev 1.1 6.2 (page 48)
  47. * */
  48. #define CCID_MESSAGE_TYPE_RDR_to_PC_DataBlock 0x80
  49. #define CCID_MESSAGE_TYPE_RDR_to_PC_SlotStatus 0x81
  50. #define CCID_MESSAGE_TYPE_RDR_to_PC_Parameters 0x82
  51. #define CCID_MESSAGE_TYPE_RDR_to_PC_Escape 0x83
  52. #define CCID_MESSAGE_TYPE_RDR_to_PC_DataRateAndClockFrequency 0x84
  53. /*
  54. * * INTERRUPT_IN messages from Reader to PC
  55. * * Defined in CCID Rev 1.1 6.3 (page 56)
  56. * */
  57. #define CCID_MESSAGE_TYPE_RDR_to_PC_NotifySlotChange 0x50
  58. #define CCID_MESSAGE_TYPE_RDR_to_PC_HardwareError 0x51
  59. /* Status codes that go in bStatus (see 6.2.6) */
  60. enum { ICC_STATUS_PRESENT_ACTIVE = 0, ICC_STATUS_PRESENT_INACTIVE, ICC_STATUS_NOT_PRESENT };
  61. enum {
  62. COMMAND_STATUS_NO_ERROR = 0,
  63. COMMAND_STATUS_FAILED,
  64. COMMAND_STATUS_TIME_EXTENSION_REQUIRED
  65. };
  66. /* Error codes that go in bError (see 6.2.6) */
  67. enum {
  68. ERROR_CMD_NOT_SUPPORTED = 0,
  69. ERROR_CMD_ABORTED = -1,
  70. ERROR_ICC_MUTE = -2,
  71. ERROR_XFR_PARITY_ERROR = -3,
  72. ERROR_XFR_OVERRUN = -4,
  73. ERROR_HW_ERROR = -5,
  74. };
  75. struct CCID_Message {
  76. uint8_t bMessageType;
  77. uint32_t dwLength;
  78. uint8_t bSlot;
  79. uint8_t bSeq;
  80. uint8_t bStatus;
  81. uint8_t bError;
  82. uint8_t* payload;
  83. size_t consumed;
  84. };
  85. void seader_ccid_check_for_sam(SeaderUartBridge* seader_uart);
  86. void seader_ccid_IccPowerOn(SeaderUartBridge* seader_uart, uint8_t slot);
  87. void seader_ccid_GetSlotStatus(SeaderUartBridge* seader_uart, uint8_t slot);
  88. void seader_ccid_SetParameters(SeaderUartBridge* seader_uart);
  89. void seader_ccid_GetParameters(SeaderUartBridge* seader_uart);
  90. void seader_ccid_XfrBlock(SeaderUartBridge* seader_uart, uint8_t* data, size_t len);
  91. void seader_ccid_XfrBlockToSlot(
  92. SeaderUartBridge* seader_uart,
  93. uint8_t slot,
  94. uint8_t* data,
  95. size_t len);
  96. size_t seader_ccid_process(Seader* seader, uint8_t* cmd, size_t cmd_len);