sap.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /*
  3. * BlueZ - Bluetooth protocol stack for Linux
  4. *
  5. * Copyright (C) 2010 Instituto Nokia de Tecnologia - INdT
  6. * Copyright (C) 2010 ST-Ericsson SA
  7. *
  8. * Author: Marek Skowron <marek.skowron@tieto.com> for ST-Ericsson.
  9. * Author: Waldemar Rymarkiewicz <waldemar.rymarkiewicz@tieto.com>
  10. * for ST-Ericsson.
  11. *
  12. */
  13. #include <stdint.h>
  14. #include <glib.h>
  15. #ifdef SAP_DEBUG
  16. #define SAP_VDBG(fmt, arg...) DBG(fmt, arg)
  17. #else
  18. #define SAP_VDBG(fmt...)
  19. #endif
  20. #define SAP_VERSION 0x0101
  21. /* Connection Status - SAP v1.1 section 5.2.2 */
  22. enum sap_status {
  23. SAP_STATUS_OK = 0x00,
  24. SAP_STATUS_CONNECTION_FAILED = 0x01,
  25. SAP_STATUS_MAX_MSG_SIZE_NOT_SUPPORTED = 0x02,
  26. SAP_STATUS_MAX_MSG_SIZE_TOO_SMALL = 0x03,
  27. SAP_STATUS_OK_ONGOING_CALL = 0x04
  28. };
  29. /* Disconnection Type - SAP v1.1 section 5.2.3 */
  30. enum sap_disconnection_type {
  31. SAP_DISCONNECTION_TYPE_GRACEFUL = 0x00,
  32. SAP_DISCONNECTION_TYPE_IMMEDIATE = 0x01
  33. };
  34. /* Result codes - SAP v1.1 section 5.2.4 */
  35. enum sap_result {
  36. SAP_RESULT_OK = 0x00,
  37. SAP_RESULT_ERROR_NO_REASON = 0x01,
  38. SAP_RESULT_ERROR_NOT_ACCESSIBLE = 0x02,
  39. SAP_RESULT_ERROR_POWERED_OFF = 0x03,
  40. SAP_RESULT_ERROR_CARD_REMOVED = 0x04,
  41. SAP_RESULT_ERROR_POWERED_ON = 0x05,
  42. SAP_RESULT_ERROR_NO_DATA = 0x06,
  43. SAP_RESULT_NOT_SUPPORTED = 0x07
  44. };
  45. /* Status Change - SAP v1.1 section 5.2.8 */
  46. enum sap_status_change {
  47. SAP_STATUS_CHANGE_UNKNOWN_ERROR = 0x00,
  48. SAP_STATUS_CHANGE_CARD_RESET = 0x01,
  49. SAP_STATUS_CHANGE_CARD_NOT_ACCESSIBLE = 0x02,
  50. SAP_STATUS_CHANGE_CARD_REMOVED = 0x03,
  51. SAP_STATUS_CHANGE_CARD_INSERTED = 0x04,
  52. SAP_STATUS_CHANGE_CARD_RECOVERED = 0x05
  53. };
  54. /* Message format - SAP v1.1 section 5.1 */
  55. struct sap_parameter {
  56. uint8_t id;
  57. uint8_t reserved;
  58. uint16_t len;
  59. uint8_t val[];
  60. /*
  61. * Padding bytes 0-3 bytes
  62. */
  63. } __attribute__((packed));
  64. struct sap_message {
  65. uint8_t id;
  66. uint8_t nparam;
  67. uint16_t reserved;
  68. struct sap_parameter param[];
  69. } __attribute__((packed));
  70. #define SAP_BUF_SIZE 512
  71. #define SAP_MSG_HEADER_SIZE 4
  72. enum sap_protocol {
  73. SAP_CONNECT_REQ = 0x00,
  74. SAP_CONNECT_RESP = 0x01,
  75. SAP_DISCONNECT_REQ = 0x02,
  76. SAP_DISCONNECT_RESP = 0x03,
  77. SAP_DISCONNECT_IND = 0x04,
  78. SAP_TRANSFER_APDU_REQ = 0x05,
  79. SAP_TRANSFER_APDU_RESP = 0x06,
  80. SAP_TRANSFER_ATR_REQ = 0x07,
  81. SAP_TRANSFER_ATR_RESP = 0x08,
  82. SAP_POWER_SIM_OFF_REQ = 0x09,
  83. SAP_POWER_SIM_OFF_RESP = 0x0A,
  84. SAP_POWER_SIM_ON_REQ = 0x0B,
  85. SAP_POWER_SIM_ON_RESP = 0x0C,
  86. SAP_RESET_SIM_REQ = 0x0D,
  87. SAP_RESET_SIM_RESP = 0x0E,
  88. SAP_TRANSFER_CARD_READER_STATUS_REQ = 0x0F,
  89. SAP_TRANSFER_CARD_READER_STATUS_RESP = 0x10,
  90. SAP_STATUS_IND = 0x11,
  91. SAP_ERROR_RESP = 0x12,
  92. SAP_SET_TRANSPORT_PROTOCOL_REQ = 0x13,
  93. SAP_SET_TRANSPORT_PROTOCOL_RESP = 0x14
  94. };
  95. /* Parameters Ids - SAP 1.1 section 5.2 */
  96. enum sap_param_id {
  97. SAP_PARAM_ID_MAX_MSG_SIZE = 0x00,
  98. SAP_PARAM_ID_CONN_STATUS = 0x01,
  99. SAP_PARAM_ID_RESULT_CODE = 0x02,
  100. SAP_PARAM_ID_DISCONNECT_IND = 0x03,
  101. SAP_PARAM_ID_COMMAND_APDU = 0x04,
  102. SAP_PARAM_ID_COMMAND_APDU7816 = 0x10,
  103. SAP_PARAM_ID_RESPONSE_APDU = 0x05,
  104. SAP_PARAM_ID_ATR = 0x06,
  105. SAP_PARAM_ID_CARD_READER_STATUS = 0x07,
  106. SAP_PARAM_ID_STATUS_CHANGE = 0x08,
  107. SAP_PARAM_ID_TRANSPORT_PROTOCOL = 0x09
  108. };
  109. #define SAP_PARAM_ID_MAX_MSG_SIZE_LEN 0x02
  110. #define SAP_PARAM_ID_CONN_STATUS_LEN 0x01
  111. #define SAP_PARAM_ID_RESULT_CODE_LEN 0x01
  112. #define SAP_PARAM_ID_DISCONNECT_IND_LEN 0x01
  113. #define SAP_PARAM_ID_CARD_READER_STATUS_LEN 0x01
  114. #define SAP_PARAM_ID_STATUS_CHANGE_LEN 0x01
  115. #define SAP_PARAM_ID_TRANSPORT_PROTO_LEN 0x01
  116. /* Transport Protocol - SAP v1.1 section 5.2.9 */
  117. enum sap_transport_protocol {
  118. SAP_TRANSPORT_PROTOCOL_T0 = 0x00,
  119. SAP_TRANSPORT_PROTOCOL_T1 = 0x01
  120. };
  121. /*SAP driver init and exit routines. Implemented by sap-*.c */
  122. int sap_init(void);
  123. void sap_exit(void);
  124. /* SAP requests implemented by sap-*.c */
  125. void sap_connect_req(void *sap_device, uint16_t maxmsgsize);
  126. void sap_disconnect_req(void *sap_device, uint8_t linkloss);
  127. void sap_transfer_apdu_req(void *sap_device, struct sap_parameter *param);
  128. void sap_transfer_atr_req(void *sap_device);
  129. void sap_power_sim_off_req(void *sap_device);
  130. void sap_power_sim_on_req(void *sap_device);
  131. void sap_reset_sim_req(void *sap_device);
  132. void sap_transfer_card_reader_status_req(void *sap_device);
  133. void sap_set_transport_protocol_req(void *sap_device,
  134. struct sap_parameter *param);
  135. /*SAP responses to SAP requests. Implemented by server.c */
  136. int sap_connect_rsp(void *sap_device, uint8_t status);
  137. int sap_disconnect_rsp(void *sap_device);
  138. int sap_transfer_apdu_rsp(void *sap_device, uint8_t result,
  139. uint8_t *sap_apdu_resp, uint16_t length);
  140. int sap_transfer_atr_rsp(void *sap_device, uint8_t result,
  141. uint8_t *sap_atr, uint16_t length);
  142. int sap_power_sim_off_rsp(void *sap_device, uint8_t result);
  143. int sap_power_sim_on_rsp(void *sap_device, uint8_t result);
  144. int sap_reset_sim_rsp(void *sap_device, uint8_t result);
  145. int sap_transfer_card_reader_status_rsp(void *sap_device, uint8_t result,
  146. uint8_t status);
  147. int sap_transport_protocol_rsp(void *sap_device, uint8_t result);
  148. /* Event indication. Implemented by server.c*/
  149. int sap_status_ind(void *sap_device, uint8_t status_change);
  150. int sap_disconnect_ind(void *sap_device, uint8_t disc_type);