Upgrade bluez5_37 :Merge the code from private
[platform/upstream/bluez.git] / tools / parser / sap.c
1 /*
2  *
3  *  BlueZ - Bluetooth protocol stack for Linux
4  *
5  *  Copyright (C) 2012  Tieto Poland
6  *
7  *
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.
12  *
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.
17  *
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
21  *
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdio.h>
29 #include <errno.h>
30 #include <unistd.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34 #include "parser.h"
35
36 #define PADDING4(x) ((4 - ((x) & 0x03)) & 0x03)
37
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
59
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
71
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
77
78 #define SAP_DISCONNECTION_TYPE_GRACEFUL         0x00
79 #define SAP_DISCONNECTION_TYPE_IMMEDIATE        0x01
80 #define SAP_DISCONNECTION_TYPE_CLIENT           0xFF
81
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
90
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
97
98 #define SAP_TRANSPORT_PROTOCOL_T0       0x00
99 #define SAP_TRANSPORT_PROTOCOL_T1       0x01
100
101 static const char *msg2str(uint8_t msg)
102 {
103         switch (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";
138         case SAP_STATUS_IND:
139                 return "Status Ind";
140         case SAP_ERROR_RESP:
141                 return "Error 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";
146         default:
147                 return "Reserved";
148         }
149 }
150
151 static const char *param2str(uint8_t param)
152 {
153         switch (param) {
154         case SAP_PARAM_ID_MAX_MSG_SIZE:
155                 return "MaxMsgSize";
156         case SAP_PARAM_ID_CONN_STATUS:
157                 return "ConnectionStatus";
158         case SAP_PARAM_ID_RESULT_CODE:
159                 return "ResultCode";
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:
169                 return "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";
176         default:
177                 return "Reserved";
178         }
179 }
180
181 static const char *status2str(uint8_t status)
182 {
183         switch (status) {
184         case  SAP_STATUS_OK:
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";
194         default:
195                 return "Reserved";
196         }
197 }
198
199 static const char *disctype2str(uint8_t disctype)
200 {
201         switch (disctype) {
202         case  SAP_DISCONNECTION_TYPE_GRACEFUL:
203                 return "Graceful";
204         case  SAP_DISCONNECTION_TYPE_IMMEDIATE:
205                 return "Immediate";
206         default:
207                 return "Reserved";
208         }
209 }
210
211 static const char *result2str(uint8_t result)
212 {
213         switch (result) {
214         case  SAP_RESULT_OK:
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";
230         default:
231                 return "Reserved";
232         }
233 }
234
235 static const char *statuschg2str(uint8_t statuschg)
236 {
237         switch (statuschg) {
238         case  SAP_STATUS_CHANGE_UNKNOWN_ERROR:
239                 return "Unknown Error";
240         case  SAP_STATUS_CHANGE_CARD_RESET:
241                 return "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";
250         default:
251                 return "Reserved";
252         }
253 }
254
255 static const char *prot2str(uint8_t prot)
256 {
257         switch (prot) {
258         case SAP_TRANSPORT_PROTOCOL_T0:
259                 return "T=0";
260         case SAP_TRANSPORT_PROTOCOL_T1:
261                 return "T=1";
262         default:
263                 return "Reserved";
264         }
265 }
266
267 static void parse_parameters(int level, struct frame *frm)
268 {
269         uint8_t param;
270         uint16_t len;
271         uint8_t pv8;
272
273         while (frm->len > 3) {
274                 p_indent(level, frm);
275
276                 param = p_get_u8(frm);
277                 p_get_u8(frm);
278                 len = p_get_u16(frm);
279
280                 printf("%s (0x%02x) len %d = ", param2str(param), param, len);
281
282                 switch (param) {
283                 case SAP_PARAM_ID_MAX_MSG_SIZE:
284                         printf("%d\n", p_get_u16(frm));
285                         break;
286                 case SAP_PARAM_ID_CONN_STATUS:
287                         pv8 = p_get_u8(frm);
288                         printf("0x%02x (%s)\n", pv8, status2str(pv8));
289                         break;
290                 case SAP_PARAM_ID_RESULT_CODE:
291                 case SAP_PARAM_ID_CARD_READER_STATUS:
292                         pv8 = p_get_u8(frm);
293                         printf("0x%02x (%s)\n", pv8, result2str(pv8));
294                         break;
295                 case SAP_PARAM_ID_DISCONNECT_IND:
296                         pv8 = p_get_u8(frm);
297                         printf("0x%02x (%s)\n", pv8, disctype2str(pv8));
298                         break;
299                 case SAP_PARAM_ID_STATUS_CHANGE:
300                         pv8 = p_get_u8(frm);
301                         printf("0x%02x (%s)\n", pv8, statuschg2str(pv8));
302                         break;
303                 case SAP_PARAM_ID_TRANSPORT_PROTOCOL:
304                         pv8 = p_get_u8(frm);
305                         printf("0x%02x (%s)\n", pv8, prot2str(pv8));
306                         break;
307                 default:
308                         printf("\n");
309                         raw_ndump(level + 1, frm, len);
310                         frm->ptr += len;
311                         frm->len -= len;
312                 }
313
314                 /* Skip padding */
315                 frm->ptr += PADDING4(len);
316                 frm->len -= PADDING4(len);
317         }
318 }
319
320 void sap_dump(int level, struct frame *frm)
321 {
322         uint8_t msg, params;
323
324         msg = p_get_u8(frm);
325         params = p_get_u8(frm);
326
327         /* Skip reserved field */
328         p_get_u16(frm);
329
330         p_indent(level, frm);
331
332         printf("SAP: %s: params %d\n", msg2str(msg), params);
333
334         parse_parameters(level, frm);
335 }