• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  BlueZ - Bluetooth protocol stack for Linux
3  *
4  *  Copyright (C) 2010 Instituto Nokia de Tecnologia - INdT
5  *  Copyright (C) 2010 ST-Ericsson SA
6  *
7  *  Author: Marek Skowron <marek.skowron@tieto.com> for ST-Ericsson.
8  *  Author: Waldemar Rymarkiewicz <waldemar.rymarkiewicz@tieto.com>
9  *          for ST-Ericsson.
10  *
11  *  This program is free software; you can redistribute it and/or modify
12  *  it under the terms of the GNU General Public License as published by
13  *  the Free Software Foundation; either version 2 of the License, or
14  *  (at your option) any later version.
15  *
16  *  This program is distributed in the hope that it will be useful,
17  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  *  GNU General Public License for more details.
20  *
21  *  You should have received a copy of the GNU General Public License
22  *  along with this program; if not, write to the Free Software
23  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
24  */
25 
26 #include <stdint.h>
27 #include <glib.h>
28 
29 #define SAP_VERSION 0x0101
30 
31 /* Connection Status - SAP v1.1 section 5.2.2 */
32 enum sap_status {
33 	SAP_STATUS_OK				= 0x00,
34 	SAP_STATUS_CONNECTION_FAILED		= 0x01,
35 	SAP_STATUS_MAX_MSG_SIZE_NOT_SUPPORTED	= 0x02,
36 	SAP_STATUS_MAX_MSG_SIZE_TOO_SMALL	= 0x03,
37 	SAP_STATUS_OK_ONGOING_CALL		= 0x04
38 };
39 
40 /* Disconnection Type - SAP v1.1 section 5.2.3 */
41 enum sap_disconnection_type {
42 	SAP_DISCONNECTION_TYPE_GRACEFUL		= 0x00,
43 	SAP_DISCONNECTION_TYPE_IMMEDIATE	= 0x01,
44 	SAP_DISCONNECTION_TYPE_CLIENT		= 0xFF
45 };
46 
47 /* Result codes - SAP v1.1 section 5.2.4 */
48 enum sap_result {
49 	SAP_RESULT_OK			= 0x00,
50 	SAP_RESULT_ERROR_NO_REASON	= 0x01,
51 	SAP_RESULT_ERROR_NOT_ACCESSIBLE	= 0x02,
52 	SAP_RESULT_ERROR_POWERED_OFF	= 0x03,
53 	SAP_RESULT_ERROR_CARD_REMOVED	= 0x04,
54 	SAP_RESULT_ERROR_POWERED_ON	= 0x05,
55 	SAP_RESULT_ERROR_NO_DATA	= 0x06,
56 	SAP_RESULT_NOT_SUPPORTED	= 0x07
57 };
58 
59 /* Status Change - SAP v1.1 section 5.2.8 */
60 enum sap_status_change {
61 	SAP_STATUS_CHANGE_UNKNOWN_ERROR		= 0x00,
62 	SAP_STATUS_CHANGE_CARD_RESET		= 0x01,
63 	SAP_STATUS_CHANGE_CARD_NOT_ACCESSIBLE	= 0x02,
64 	SAP_STATUS_CHANGE_CARD_REMOVED		= 0x03,
65 	SAP_STATUS_CHANGE_CARD_INSERTED		= 0x04,
66 	SAP_STATUS_CHANGE_CARD_RECOVERED	= 0x05
67 };
68 
69 /* Message format - SAP v1.1 section 5.1 */
70 struct sap_parameter {
71 	uint8_t id;
72 	uint8_t reserved;
73 	uint16_t len;
74 	uint8_t val[0];
75 	/*
76 	 * Padding bytes 0-3 bytes
77 	 */
78 } __attribute__((packed));
79 
80 struct sap_message {
81 	uint8_t id;
82 	uint8_t nparam;
83 	uint16_t reserved;
84 	struct sap_parameter param[0];
85 } __attribute__((packed));
86 
87 enum {
88 	ICC_READER_UNSPECIFIED_ERROR, /* No further information available */
89 	ICC_READER_NOT_PRESENT,       /* Card Reader removed or not present */
90 	ICC_READER_BUSY,              /* Card Reader in use */
91 	ICC_READER_CARD_POWERED_ON,   /* Card in reader and is powered on */
92 	ICC_READER_DEACTIVATED,       /* Card Reader deactivated */
93 	ICC_READER_CARD_POWERED_OFF,  /* Card in reader, but powered off */
94 	ICC_READER_NO_CARD,           /* No card in reader */
95 	ICC_READER_LAST
96 };
97 
98 #define SAP_BUF_SIZE		512
99 #define SAP_MSG_HEADER_SIZE	4
100 
101 enum sap_protocol {
102 	SAP_CONNECT_REQ		= 0x00,
103 	SAP_CONNECT_RESP	= 0x01,
104 	SAP_DISCONNECT_REQ	= 0x02,
105 	SAP_DISCONNECT_RESP	= 0x03,
106 	SAP_DISCONNECT_IND	= 0x04,
107 	SAP_TRANSFER_APDU_REQ	= 0x05,
108 	SAP_TRANSFER_APDU_RESP	= 0x06,
109 	SAP_TRANSFER_ATR_REQ	= 0x07,
110 	SAP_TRANSFER_ATR_RESP	= 0x08,
111 	SAP_POWER_SIM_OFF_REQ	= 0x09,
112 	SAP_POWER_SIM_OFF_RESP	= 0x0A,
113 	SAP_POWER_SIM_ON_REQ	= 0x0B,
114 	SAP_POWER_SIM_ON_RESP	= 0x0C,
115 	SAP_RESET_SIM_REQ	= 0x0D,
116 	SAP_RESET_SIM_RESP	= 0x0E,
117 	SAP_TRANSFER_CARD_READER_STATUS_REQ	= 0x0F,
118 	SAP_TRANSFER_CARD_READER_STATUS_RESP	= 0x10,
119 	SAP_STATUS_IND	= 0x11,
120 	SAP_ERROR_RESP	= 0x12,
121 	SAP_SET_TRANSPORT_PROTOCOL_REQ	= 0x13,
122 	SAP_SET_TRANSPORT_PROTOCOL_RESP	= 0x14
123 };
124 
125 /* Parameters Ids - SAP 1.1 section 5.2 */
126 enum sap_param_id {
127 	SAP_PARAM_ID_MAX_MSG_SIZE	= 0x00,
128 	SAP_PARAM_ID_CONN_STATUS	= 0x01,
129 	SAP_PARAM_ID_RESULT_CODE	= 0x02,
130 	SAP_PARAM_ID_DISCONNECT_IND	= 0x03,
131 	SAP_PARAM_ID_COMMAND_APDU	= 0x04,
132 	SAP_PARAM_ID_COMMAND_APDU7816	= 0x10,
133 	SAP_PARAM_ID_RESPONSE_APDU	= 0x05,
134 	SAP_PARAM_ID_ATR		= 0x06,
135 	SAP_PARAM_ID_CARD_READER_STATUS	= 0x07,
136 	SAP_PARAM_ID_STATUS_CHANGE	= 0x08,
137 	SAP_PARAM_ID_TRANSPORT_PROTOCOL	= 0x09
138 };
139 
140 #define SAP_PARAM_ID_MAX_MSG_SIZE_LEN		0x02
141 #define SAP_PARAM_ID_CONN_STATUS_LEN		0x01
142 #define SAP_PARAM_ID_RESULT_CODE_LEN		0x01
143 #define SAP_PARAM_ID_DISCONNECT_IND_LEN		0x01
144 #define SAP_PARAM_ID_CARD_READER_STATUS_LEN	0x01
145 #define SAP_PARAM_ID_STATUS_CHANGE_LEN		0x01
146 #define SAP_PARAM_ID_TRANSPORT_PROTO_LEN	0x01
147 
148 /* Transport Protocol - SAP v1.1 section 5.2.9 */
149 enum sap_transport_protocol {
150 	SAP_TRANSPORT_PROTOCOL_T0 = 0x00,
151 	SAP_TRANSPORT_PROTOCOL_T1 = 0x01
152 };
153 
154 /*SAP driver init and exit routines. Implemented by sap-*.c */
155 int sap_init(void);
156 void sap_exit(void);
157 
158 /* SAP requests implemented by sap-*.c */
159 void sap_connect_req(void *sap_device, uint16_t maxmsgsize);
160 void sap_disconnect_req(void *sap_device, uint8_t linkloss);
161 void sap_transfer_apdu_req(void *sap_device, struct sap_parameter *param);
162 void sap_transfer_atr_req(void *sap_device);
163 void sap_power_sim_off_req(void *sap_device);
164 void sap_power_sim_on_req(void *sap_device);
165 void sap_reset_sim_req(void *sap_device);
166 void sap_transfer_card_reader_status_req(void *sap_device);
167 void sap_set_transport_protocol_req(void *sap_device,
168 					struct sap_parameter *param);
169 
170 /*SAP responses to SAP requests. Implemented by server.c */
171 int sap_connect_rsp(void *sap_device, uint8_t status, uint16_t maxmsgsize);
172 int sap_disconnect_rsp(void *sap_device);
173 int sap_transfer_apdu_rsp(void *sap_device, uint8_t result,
174 				uint8_t *sap_apdu_resp, uint16_t length);
175 int sap_transfer_atr_rsp(void *sap_device, uint8_t result,
176 				uint8_t *sap_atr, uint16_t length);
177 int sap_power_sim_off_rsp(void *sap_device, uint8_t result);
178 int sap_power_sim_on_rsp(void *sap_device, uint8_t result);
179 int sap_reset_sim_rsp(void *sap_device, uint8_t result);
180 int sap_transfer_card_reader_status_rsp(void *sap_device, uint8_t result,
181 						uint8_t status);
182 int sap_error_rsp(void *sap_device);
183 int sap_transport_protocol_rsp(void *sap_device, uint8_t result);
184 
185 /* Event indication. Implemented by server.c*/
186 int sap_status_ind(void *sap_device, uint8_t status_change);
187 int sap_disconnect_ind(void *sap_device, uint8_t disc_type);
188