Upgrade bluez5_37 :Merge the code from private
[platform/upstream/bluez.git] / tools / parser / hidp.c
1 /*
2  *
3  *  BlueZ - Bluetooth protocol stack for Linux
4  *
5  *  Copyright (C) 2003-2011  Marcel Holtmann <marcel@holtmann.org>
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 static char *type2str(uint8_t head)
37 {
38         switch (head & 0xf0) {
39         case 0x00:
40                 return "Handshake";
41         case 0x10:
42                 return "Control";
43         case 0x40:
44                 return "Get report";
45         case 0x50:
46                 return "Set report";
47         case 0x60:
48                 return "Get protocol";
49         case 0x70:
50                 return "Set protocol";
51         case 0x80:
52                 return "Get idle";
53         case 0x90:
54                 return "Set idle";
55         case 0xa0:
56                 return "Data";
57         case 0xb0:
58                 return "Data continuation";
59         default:
60                 return "Reserved";
61         }
62 }
63
64 static char *result2str(uint8_t head)
65 {
66         switch (head & 0x0f) {
67         case 0x00:
68                 return "Successful";
69         case 0x01:
70                 return "Not ready";
71         case 0x02:
72                 return "Invalid report ID";
73         case 0x03:
74                 return "Unsupported request";
75         case 0x04:
76                 return "Invalid parameter";
77         case 0x0e:
78                 return "Unknown";
79         case 0x0f:
80                 return "Fatal";
81         default:
82                 return "Reserved";
83         }
84 }
85
86 static char *operation2str(uint8_t head)
87 {
88         switch (head & 0x0f) {
89         case 0x00:
90                 return "No operation";
91         case 0x01:
92                 return "Hard reset";
93         case 0x02:
94                 return "Soft reset";
95         case 0x03:
96                 return "Suspend";
97         case 0x04:
98                 return "Exit suspend";
99         case 0x05:
100                 return "Virtual cable unplug";
101         default:
102                 return "Reserved";
103         }
104 }
105
106 static char *report2str(uint8_t head)
107 {
108         switch (head & 0x03) {
109         case 0x00:
110                 return "Other report";
111         case 0x01:
112                 return "Input report";
113         case 0x02:
114                 return "Output report";
115         case 0x03:
116                 return "Feature report";
117         default:
118                 return "Reserved";
119         }
120 }
121
122 static char *protocol2str(uint8_t head)
123 {
124         switch (head & 0x01) {
125         case 0x00:
126                 return "Boot protocol";
127         case 0x01:
128                 return "Report protocol";
129         default:
130                 return "Reserved";
131         }
132 }
133
134 void hidp_dump(int level, struct frame *frm)
135 {
136         uint8_t hdr;
137         char *param;
138
139         hdr = p_get_u8(frm);
140
141         switch (hdr & 0xf0) {
142         case 0x00:
143                 param = result2str(hdr);
144                 break;
145         case 0x10:
146                 param = operation2str(hdr);
147                 break;
148         case 0x60:
149         case 0x70:
150                 param = protocol2str(hdr);
151                 break;
152         case 0x40:
153         case 0x50:
154         case 0xa0:
155         case 0xb0:
156                 param = report2str(hdr);
157                 break;
158         default:
159                 param = "";
160                 break;
161         }
162
163         p_indent(level, frm);
164
165         printf("HIDP: %s: %s\n", type2str(hdr), param);
166
167         raw_dump(level, frm);
168 }