3 * BlueZ - Bluetooth protocol stack for Linux
5 * Copyright (C) 2012 Tieto Poland
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
36 #define PADDING4(x) ((4 - ((x) & 0x03)) & 0x03)
38 #define SAP_CONNECT_REQ 0x00
39 #define SAP_CONNECT_RESP 0x01
40 #define SAP_DISCONNECT_REQ 0x02
41 #define SAP_DISCONNECT_RESP 0x03
42 #define SAP_DISCONNECT_IND 0x04
43 #define SAP_TRANSFER_APDU_REQ 0x05
44 #define SAP_TRANSFER_APDU_RESP 0x06
45 #define SAP_TRANSFER_ATR_REQ 0x07
46 #define SAP_TRANSFER_ATR_RESP 0x08
47 #define SAP_POWER_SIM_OFF_REQ 0x09
48 #define SAP_POWER_SIM_OFF_RESP 0x0A
49 #define SAP_POWER_SIM_ON_REQ 0x0B
50 #define SAP_POWER_SIM_ON_RESP 0x0C
51 #define SAP_RESET_SIM_REQ 0x0D
52 #define SAP_RESET_SIM_RESP 0x0E
53 #define SAP_TRANSFER_CARD_READER_STATUS_REQ 0x0F
54 #define SAP_TRANSFER_CARD_READER_STATUS_RESP 0x10
55 #define SAP_STATUS_IND 0x11
56 #define SAP_ERROR_RESP 0x12
57 #define SAP_SET_TRANSPORT_PROTOCOL_REQ 0x13
58 #define SAP_SET_TRANSPORT_PROTOCOL_RESP 0x14
60 #define SAP_PARAM_ID_MAX_MSG_SIZE 0x00
61 #define SAP_PARAM_ID_CONN_STATUS 0x01
62 #define SAP_PARAM_ID_RESULT_CODE 0x02
63 #define SAP_PARAM_ID_DISCONNECT_IND 0x03
64 #define SAP_PARAM_ID_COMMAND_APDU 0x04
65 #define SAP_PARAM_ID_COMMAND_APDU7816 0x10
66 #define SAP_PARAM_ID_RESPONSE_APDU 0x05
67 #define SAP_PARAM_ID_ATR 0x06
68 #define SAP_PARAM_ID_CARD_READER_STATUS 0x07
69 #define SAP_PARAM_ID_STATUS_CHANGE 0x08
70 #define SAP_PARAM_ID_TRANSPORT_PROTOCOL 0x09
72 #define SAP_STATUS_OK 0x00
73 #define SAP_STATUS_CONNECTION_FAILED 0x01
74 #define SAP_STATUS_MAX_MSG_SIZE_NOT_SUPPORTED 0x02
75 #define SAP_STATUS_MAX_MSG_SIZE_TOO_SMALL 0x03
76 #define SAP_STATUS_OK_ONGOING_CALL 0x04
78 #define SAP_DISCONNECTION_TYPE_GRACEFUL 0x00
79 #define SAP_DISCONNECTION_TYPE_IMMEDIATE 0x01
80 #define SAP_DISCONNECTION_TYPE_CLIENT 0xFF
82 #define SAP_RESULT_OK 0x00
83 #define SAP_RESULT_ERROR_NO_REASON 0x01
84 #define SAP_RESULT_ERROR_NOT_ACCESSIBLE 0x02
85 #define SAP_RESULT_ERROR_POWERED_OFF 0x03
86 #define SAP_RESULT_ERROR_CARD_REMOVED 0x04
87 #define SAP_RESULT_ERROR_POWERED_ON 0x05
88 #define SAP_RESULT_ERROR_NO_DATA 0x06
89 #define SAP_RESULT_NOT_SUPPORTED 0x07
91 #define SAP_STATUS_CHANGE_UNKNOWN_ERROR 0x00
92 #define SAP_STATUS_CHANGE_CARD_RESET 0x01
93 #define SAP_STATUS_CHANGE_CARD_NOT_ACCESSIBLE 0x02
94 #define SAP_STATUS_CHANGE_CARD_REMOVED 0x03
95 #define SAP_STATUS_CHANGE_CARD_INSERTED 0x04
96 #define SAP_STATUS_CHANGE_CARD_RECOVERED 0x05
98 #define SAP_TRANSPORT_PROTOCOL_T0 0x00
99 #define SAP_TRANSPORT_PROTOCOL_T1 0x01
101 static const char *msg2str(uint8_t msg)
104 case SAP_CONNECT_REQ:
105 return "Connect Req";
106 case SAP_CONNECT_RESP:
107 return "Connect Resp";
108 case SAP_DISCONNECT_REQ:
109 return "Disconnect Req";
110 case SAP_DISCONNECT_RESP:
111 return "Disconnect Resp";
112 case SAP_DISCONNECT_IND:
113 return "Disconnect Ind";
114 case SAP_TRANSFER_APDU_REQ:
115 return "Transfer APDU Req";
116 case SAP_TRANSFER_APDU_RESP:
117 return "Transfer APDU Resp";
118 case SAP_TRANSFER_ATR_REQ:
119 return "Transfer ATR Req";
120 case SAP_TRANSFER_ATR_RESP:
121 return "Transfer ATR Resp";
122 case SAP_POWER_SIM_OFF_REQ:
123 return "Power SIM Off Req";
124 case SAP_POWER_SIM_OFF_RESP:
125 return "Power SIM Off Resp";
126 case SAP_POWER_SIM_ON_REQ:
127 return "Power SIM On Req";
128 case SAP_POWER_SIM_ON_RESP:
129 return "Power SIM On Resp";
130 case SAP_RESET_SIM_REQ:
131 return "Reset SIM Req";
132 case SAP_RESET_SIM_RESP:
133 return "Reset SIM Resp";
134 case SAP_TRANSFER_CARD_READER_STATUS_REQ:
135 return "Transfer Card Reader Status Req";
136 case SAP_TRANSFER_CARD_READER_STATUS_RESP:
137 return "Transfer Card Reader Status Resp";
142 case SAP_SET_TRANSPORT_PROTOCOL_REQ:
143 return "Set Transport Protocol Req";
144 case SAP_SET_TRANSPORT_PROTOCOL_RESP:
145 return "Set Transport Protocol Resp";
151 static const char *param2str(uint8_t param)
154 case SAP_PARAM_ID_MAX_MSG_SIZE:
156 case SAP_PARAM_ID_CONN_STATUS:
157 return "ConnectionStatus";
158 case SAP_PARAM_ID_RESULT_CODE:
160 case SAP_PARAM_ID_DISCONNECT_IND:
161 return "DisconnectionType";
162 case SAP_PARAM_ID_COMMAND_APDU:
163 return "CommandAPDU";
164 case SAP_PARAM_ID_COMMAND_APDU7816:
165 return "CommandAPDU7816";
166 case SAP_PARAM_ID_RESPONSE_APDU:
167 return "ResponseAPDU";
168 case SAP_PARAM_ID_ATR:
170 case SAP_PARAM_ID_CARD_READER_STATUS:
171 return "CardReaderStatus";
172 case SAP_PARAM_ID_STATUS_CHANGE:
173 return "StatusChange";
174 case SAP_PARAM_ID_TRANSPORT_PROTOCOL:
175 return "TransportProtocol";
181 static const char *status2str(uint8_t status)
185 return "OK, Server can fulfill requirements";
186 case SAP_STATUS_CONNECTION_FAILED:
187 return "Error, Server unable to establish connection";
188 case SAP_STATUS_MAX_MSG_SIZE_NOT_SUPPORTED:
189 return "Error, Server does not support maximum message size";
190 case SAP_STATUS_MAX_MSG_SIZE_TOO_SMALL:
191 return "Error, maximum message size by Client is too small";
192 case SAP_STATUS_OK_ONGOING_CALL:
193 return "OK, ongoing call";
199 static const char *disctype2str(uint8_t disctype)
202 case SAP_DISCONNECTION_TYPE_GRACEFUL:
204 case SAP_DISCONNECTION_TYPE_IMMEDIATE:
211 static const char *result2str(uint8_t result)
215 return "OK, request processed correctly";
216 case SAP_RESULT_ERROR_NO_REASON:
217 return "Error, no reason defined";
218 case SAP_RESULT_ERROR_NOT_ACCESSIBLE:
219 return "Error, card not accessible";
220 case SAP_RESULT_ERROR_POWERED_OFF:
221 return "Error, card (already) powered off";
222 case SAP_RESULT_ERROR_CARD_REMOVED:
223 return "Error, card removed";
224 case SAP_RESULT_ERROR_POWERED_ON:
225 return "Error, card already powered on";
226 case SAP_RESULT_ERROR_NO_DATA:
227 return "Error, data not available";
228 case SAP_RESULT_NOT_SUPPORTED:
229 return "Error, not supported";
235 static const char *statuschg2str(uint8_t statuschg)
238 case SAP_STATUS_CHANGE_UNKNOWN_ERROR:
239 return "Unknown Error";
240 case SAP_STATUS_CHANGE_CARD_RESET:
242 case SAP_STATUS_CHANGE_CARD_NOT_ACCESSIBLE:
243 return "Card not accessible";
244 case SAP_STATUS_CHANGE_CARD_REMOVED:
245 return "Card removed";
246 case SAP_STATUS_CHANGE_CARD_INSERTED:
247 return "Card inserted";
248 case SAP_STATUS_CHANGE_CARD_RECOVERED:
249 return "Card recovered";
255 static const char *prot2str(uint8_t prot)
258 case SAP_TRANSPORT_PROTOCOL_T0:
260 case SAP_TRANSPORT_PROTOCOL_T1:
267 static void parse_parameters(int level, struct frame *frm)
273 while (frm->len > 3) {
274 p_indent(level, frm);
276 param = p_get_u8(frm);
278 len = p_get_u16(frm);
280 printf("%s (0x%02x) len %d = ", param2str(param), param, len);
283 case SAP_PARAM_ID_MAX_MSG_SIZE:
284 printf("%d\n", p_get_u16(frm));
286 case SAP_PARAM_ID_CONN_STATUS:
288 printf("0x%02x (%s)\n", pv8, status2str(pv8));
290 case SAP_PARAM_ID_RESULT_CODE:
291 case SAP_PARAM_ID_CARD_READER_STATUS:
293 printf("0x%02x (%s)\n", pv8, result2str(pv8));
295 case SAP_PARAM_ID_DISCONNECT_IND:
297 printf("0x%02x (%s)\n", pv8, disctype2str(pv8));
299 case SAP_PARAM_ID_STATUS_CHANGE:
301 printf("0x%02x (%s)\n", pv8, statuschg2str(pv8));
303 case SAP_PARAM_ID_TRANSPORT_PROTOCOL:
305 printf("0x%02x (%s)\n", pv8, prot2str(pv8));
309 raw_ndump(level + 1, frm, len);
315 frm->ptr += PADDING4(len);
316 frm->len -= PADDING4(len);
320 void sap_dump(int level, struct frame *frm)
325 params = p_get_u8(frm);
327 /* Skip reserved field */
330 p_indent(level, frm);
332 printf("SAP: %s: params %d\n", msg2str(msg), params);
334 parse_parameters(level, frm);