Tizen 2.0 release
[framework/connectivity/bluez.git] / lib / mgmt.h
1 /*
2  *  BlueZ - Bluetooth protocol stack for Linux
3  *
4  *  Copyright (C) 2010  Nokia Corporation
5  *  Copyright (C) 2010  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 #ifndef __packed
25 #define __packed __attribute__((packed))
26 #endif
27
28 #define MGMT_INDEX_NONE                 0xFFFF
29
30 #define MGMT_STATUS_SUCCESS             0x00
31 #define MGMT_STATUS_UNKNOWN_COMMAND     0x01
32 #define MGMT_STATUS_NOT_CONNECTED       0x02
33 #define MGMT_STATUS_FAILED              0x03
34 #define MGMT_STATUS_CONNECT_FAILED      0x04
35 #define MGMT_STATUS_AUTH_FAILED         0x05
36 #define MGMT_STATUS_NOT_PAIRED          0x06
37 #define MGMT_STATUS_NO_RESOURCES        0x07
38 #define MGMT_STATUS_TIMEOUT             0x08
39 #define MGMT_STATUS_ALREADY_CONNECTED   0x09
40 #define MGMT_STATUS_BUSY                0x0a
41 #define MGMT_STATUS_REJECTED            0x0b
42 #define MGMT_STATUS_NOT_SUPPORTED       0x0c
43 #define MGMT_STATUS_INVALID_PARAMS      0x0d
44 #define MGMT_STATUS_DISCONNECTED        0x0e
45 #define MGMT_STATUS_NOT_POWERED         0x0f
46 #define MGMT_STATUS_CANCELLED           0x10
47 #define MGMT_STATUS_INVALID_INDEX       0x11
48
49 struct mgmt_hdr {
50         uint16_t opcode;
51         uint16_t index;
52         uint16_t len;
53 } __packed;
54 #define MGMT_HDR_SIZE   6
55
56 struct mgmt_addr_info {
57         bdaddr_t bdaddr;
58         uint8_t type;
59 } __packed;
60
61 #define MGMT_OP_READ_VERSION            0x0001
62 struct mgmt_rp_read_version {
63         uint8_t version;
64         uint16_t revision;
65 } __packed;
66
67 #define MGMT_OP_READ_COMMANDS           0x0002
68 struct mgmt_rp_read_commands {
69         uint16_t num_commands;
70         uint16_t num_events;
71         uint16_t opcodes[0];
72 } __packed;
73
74 #define MGMT_OP_READ_INDEX_LIST         0x0003
75 struct mgmt_rp_read_index_list {
76         uint16_t num_controllers;
77         uint16_t index[0];
78 } __packed;
79
80 /* Reserve one extra byte for names in management messages so that they
81  * are always guaranteed to be nul-terminated */
82 #define MGMT_MAX_NAME_LENGTH            (HCI_MAX_NAME_LENGTH + 1)
83 #define MGMT_MAX_SHORT_NAME_LENGTH      (10 + 1)
84
85 #define MGMT_SETTING_POWERED            0x00000001
86 #define MGMT_SETTING_CONNECTABLE        0x00000002
87 #define MGMT_SETTING_FAST_CONNECTABLE   0x00000004
88 #define MGMT_SETTING_DISCOVERABLE       0x00000008
89 #define MGMT_SETTING_PAIRABLE           0x00000010
90 #define MGMT_SETTING_LINK_SECURITY      0x00000020
91 #define MGMT_SETTING_SSP                0x00000040
92 #define MGMT_SETTING_BREDR              0x00000080
93 #define MGMT_SETTING_HS                 0x00000100
94 #define MGMT_SETTING_LE                 0x00000200
95
96 #define MGMT_OP_READ_INFO               0x0004
97 struct mgmt_rp_read_info {
98         bdaddr_t bdaddr;
99         uint8_t version;
100         uint16_t manufacturer;
101         uint32_t supported_settings;
102         uint32_t current_settings;
103         uint8_t dev_class[3];
104         uint8_t name[MGMT_MAX_NAME_LENGTH];
105         uint8_t short_name[MGMT_MAX_SHORT_NAME_LENGTH];
106 } __packed;
107
108 struct mgmt_mode {
109         uint8_t val;
110 } __packed;
111
112 #define MGMT_OP_SET_POWERED             0x0005
113
114 #define MGMT_OP_SET_DISCOVERABLE        0x0006
115 struct mgmt_cp_set_discoverable {
116         uint8_t val;
117         uint16_t timeout;
118 } __packed;
119
120 #define MGMT_OP_SET_CONNECTABLE         0x0007
121
122 #define MGMT_OP_SET_FAST_CONNECTABLE    0x0008
123
124 #define MGMT_OP_SET_PAIRABLE            0x0009
125
126 #define MGMT_OP_SET_LINK_SECURITY       0x000A
127
128 #define MGMT_OP_SET_SSP                 0x000B
129
130 #define MGMT_OP_SET_HS                  0x000C
131
132 #define MGMT_OP_SET_LE                  0x000D
133
134 #define MGMT_OP_SET_DEV_CLASS           0x000E
135 struct mgmt_cp_set_dev_class {
136         uint8_t major;
137         uint8_t minor;
138 } __packed;
139
140 #define MGMT_OP_SET_LOCAL_NAME          0x000F
141 struct mgmt_cp_set_local_name {
142         uint8_t name[MGMT_MAX_NAME_LENGTH];
143         uint8_t short_name[MGMT_MAX_SHORT_NAME_LENGTH];
144 } __packed;
145
146 #define MGMT_OP_ADD_UUID                0x0010
147 struct mgmt_cp_add_uuid {
148         uint8_t uuid[16];
149         uint8_t svc_hint;
150 } __packed;
151
152 #define MGMT_OP_REMOVE_UUID             0x0011
153 struct mgmt_cp_remove_uuid {
154         uint8_t uuid[16];
155 } __packed;
156
157 struct mgmt_link_key_info {
158         struct mgmt_addr_info addr;
159         uint8_t type;
160         uint8_t val[16];
161         uint8_t pin_len;
162 } __packed;
163
164 #define MGMT_OP_LOAD_LINK_KEYS          0x0012
165 struct mgmt_cp_load_link_keys {
166         uint8_t debug_keys;
167         uint16_t key_count;
168         struct mgmt_link_key_info keys[0];
169 } __packed;
170
171 struct mgmt_ltk_info {
172         struct mgmt_addr_info addr;
173         uint8_t authenticated;
174         uint8_t master;
175         uint8_t enc_size;
176         uint16_t ediv;
177         uint8_t rand[8];
178         uint8_t val[16];
179 } __packed;
180
181 #define MGMT_OP_LOAD_LONG_TERM_KEYS     0x0013
182 struct mgmt_cp_load_long_term_keys {
183         uint16_t key_count;
184         struct mgmt_ltk_info keys[0];
185 } __packed;
186
187 #define MGMT_OP_DISCONNECT              0x0014
188 struct mgmt_cp_disconnect {
189         struct mgmt_addr_info addr;
190 } __packed;
191 struct mgmt_rp_disconnect {
192         struct mgmt_addr_info addr;
193 } __packed;
194
195 #define MGMT_OP_GET_CONNECTIONS         0x0015
196 struct mgmt_rp_get_connections {
197         uint16_t conn_count;
198         struct mgmt_addr_info addr[0];
199 } __packed;
200
201 #define MGMT_OP_PIN_CODE_REPLY          0x0016
202 struct mgmt_cp_pin_code_reply {
203         struct mgmt_addr_info addr;
204         uint8_t pin_len;
205         uint8_t pin_code[16];
206 } __packed;
207
208 #define MGMT_OP_PIN_CODE_NEG_REPLY      0x0017
209 struct mgmt_cp_pin_code_neg_reply {
210         struct mgmt_addr_info addr;
211 } __packed;
212
213 #define MGMT_OP_SET_IO_CAPABILITY       0x0018
214 struct mgmt_cp_set_io_capability {
215         uint8_t io_capability;
216 } __packed;
217
218 #define MGMT_OP_PAIR_DEVICE             0x0019
219 struct mgmt_cp_pair_device {
220         struct mgmt_addr_info addr;
221         uint8_t io_cap;
222 } __packed;
223 struct mgmt_rp_pair_device {
224         struct mgmt_addr_info addr;
225 } __packed;
226
227 #define MGMT_OP_CANCEL_PAIR_DEVICE      0x001A
228
229 #define MGMT_OP_UNPAIR_DEVICE           0x001B
230 struct mgmt_cp_unpair_device {
231         struct mgmt_addr_info addr;
232         uint8_t disconnect;
233 } __packed;
234 struct mgmt_rp_unpair_device {
235         struct mgmt_addr_info addr;
236 } __packed;
237
238 #define MGMT_OP_USER_CONFIRM_REPLY      0x001C
239 struct mgmt_cp_user_confirm_reply {
240         struct mgmt_addr_info addr;
241 } __packed;
242 struct mgmt_rp_user_confirm_reply {
243         struct mgmt_addr_info addr;
244 } __packed;
245
246 #define MGMT_OP_USER_CONFIRM_NEG_REPLY  0x001D
247
248 #define MGMT_OP_USER_PASSKEY_REPLY      0x001E
249 struct mgmt_cp_user_passkey_reply {
250         struct mgmt_addr_info addr;
251         uint32_t passkey;
252 } __packed;
253 struct mgmt_rp_user_passkey_reply {
254         struct mgmt_addr_info addr;
255 } __packed;
256
257 #define MGMT_OP_USER_PASSKEY_NEG_REPLY  0x001F
258 struct mgmt_cp_user_passkey_neg_reply {
259         struct mgmt_addr_info addr;
260 } __packed;
261
262 #define MGMT_OP_READ_LOCAL_OOB_DATA     0x0020
263 struct mgmt_rp_read_local_oob_data {
264         uint8_t hash[16];
265         uint8_t randomizer[16];
266 } __packed;
267
268 #define MGMT_OP_ADD_REMOTE_OOB_DATA     0x0021
269 struct mgmt_cp_add_remote_oob_data {
270         struct mgmt_addr_info addr;
271         uint8_t hash[16];
272         uint8_t randomizer[16];
273 } __packed;
274
275 #define MGMT_OP_REMOVE_REMOTE_OOB_DATA  0x0022
276 struct mgmt_cp_remove_remote_oob_data {
277         struct mgmt_addr_info addr;
278 } __packed;
279
280 #define MGMT_OP_START_DISCOVERY         0x0023
281 struct mgmt_cp_start_discovery {
282         uint8_t type;
283 } __packed;
284
285 #define MGMT_OP_STOP_DISCOVERY          0x0024
286 struct mgmt_cp_stop_discovery {
287         uint8_t type;
288 } __packed;
289
290 #define MGMT_OP_CONFIRM_NAME            0x0025
291 struct mgmt_cp_confirm_name {
292         struct mgmt_addr_info addr;
293         uint8_t name_known;
294 } __packed;
295 struct mgmt_rp_confirm_name {
296         struct mgmt_addr_info addr;
297 } __packed;
298
299 #define MGMT_OP_BLOCK_DEVICE            0x0026
300 struct mgmt_cp_block_device {
301         struct mgmt_addr_info addr;
302 } __packed;
303
304 #define MGMT_OP_UNBLOCK_DEVICE          0x0027
305 struct mgmt_cp_unblock_device {
306         struct mgmt_addr_info addr;
307 } __packed;
308
309 #define MGMT_OP_SET_DEVICE_ID           0x0028
310 struct mgmt_cp_set_device_id {
311         uint16_t source;
312         uint16_t vendor;
313         uint16_t product;
314         uint16_t version;
315 } __packed;
316
317 #define MGMT_EV_CMD_COMPLETE            0x0001
318 struct mgmt_ev_cmd_complete {
319         uint16_t opcode;
320         uint8_t status;
321         uint8_t data[0];
322 } __packed;
323
324 #define MGMT_EV_CMD_STATUS              0x0002
325 struct mgmt_ev_cmd_status {
326         uint16_t opcode;
327         uint8_t status;
328 } __packed;
329
330 #define MGMT_EV_CONTROLLER_ERROR        0x0003
331 struct mgmt_ev_controller_error {
332         uint8_t error_code;
333 } __packed;
334
335 #define MGMT_EV_INDEX_ADDED             0x0004
336
337 #define MGMT_EV_INDEX_REMOVED           0x0005
338
339 #define MGMT_EV_NEW_SETTINGS            0x0006
340
341 #define MGMT_EV_CLASS_OF_DEV_CHANGED    0x0007
342 struct mgmt_ev_class_of_dev_changed {
343         uint8_t class_of_dev[3];
344 } __packed;
345
346 #define MGMT_EV_LOCAL_NAME_CHANGED      0x0008
347 struct mgmt_ev_local_name_changed {
348         uint8_t name[MGMT_MAX_NAME_LENGTH];
349         uint8_t short_name[MGMT_MAX_SHORT_NAME_LENGTH];
350 } __packed;
351
352 #define MGMT_EV_NEW_LINK_KEY            0x0009
353 struct mgmt_ev_new_link_key {
354         uint8_t store_hint;
355         struct mgmt_link_key_info key;
356 } __packed;
357
358 #define MGMT_EV_NEW_LONG_TERM_KEY       0x000A
359 struct mgmt_ev_new_long_term_key {
360         uint8_t store_hint;
361         struct mgmt_ltk_info key;
362 } __packed;
363
364 #define MGMT_EV_DEVICE_CONNECTED        0x000B
365 struct mgmt_ev_device_connected {
366         struct mgmt_addr_info addr;
367         uint32_t flags;
368         uint16_t eir_len;
369         uint8_t eir[0];
370 } __packed;
371
372 #define MGMT_EV_DEVICE_DISCONNECTED     0x000C
373 struct mgmt_ev_device_disconnected {
374         struct mgmt_addr_info addr;
375 } __packed;
376
377 #define MGMT_EV_CONNECT_FAILED          0x000D
378 struct mgmt_ev_connect_failed {
379         struct mgmt_addr_info addr;
380         uint8_t status;
381 } __packed;
382
383 #define MGMT_EV_PIN_CODE_REQUEST        0x000E
384 struct mgmt_ev_pin_code_request {
385         struct mgmt_addr_info addr;
386         uint8_t secure;
387 } __packed;
388
389 #define MGMT_EV_USER_CONFIRM_REQUEST    0x000F
390 struct mgmt_ev_user_confirm_request {
391         struct mgmt_addr_info addr;
392         uint8_t confirm_hint;
393         uint32_t value;
394 } __packed;
395
396 #define MGMT_EV_USER_PASSKEY_REQUEST    0x0010
397 struct mgmt_ev_user_passkey_request {
398         struct mgmt_addr_info addr;
399 } __packed;
400
401 #define MGMT_EV_AUTH_FAILED             0x0011
402 struct mgmt_ev_auth_failed {
403         struct mgmt_addr_info addr;
404         uint8_t status;
405 } __packed;
406
407 #define MGMT_DEV_FOUND_CONFIRM_NAME     0x01
408 #define MGMT_DEV_FOUND_LEGACY_PAIRING   0x02
409
410 #define MGMT_EV_DEVICE_FOUND            0x0012
411 struct mgmt_ev_device_found {
412         struct mgmt_addr_info addr;
413         int8_t rssi;
414         uint32_t flags;
415         uint16_t eir_len;
416         uint8_t eir[0];
417 } __packed;
418
419 #define MGMT_EV_DISCOVERING             0x0013
420 struct mgmt_ev_discovering {
421         uint8_t type;
422         uint8_t discovering;
423 } __packed;
424
425 #define MGMT_EV_DEVICE_BLOCKED          0x0014
426 struct mgmt_ev_device_blocked {
427         struct mgmt_addr_info addr;
428 } __packed;
429
430 #define MGMT_EV_DEVICE_UNBLOCKED        0x0015
431 struct mgmt_ev_device_unblocked {
432         struct mgmt_addr_info addr;
433 } __packed;
434
435 #define MGMT_EV_DEVICE_UNPAIRED         0x0016
436 struct mgmt_ev_device_unpaired {
437         struct mgmt_addr_info addr;
438 } __packed;
439
440 static const char *mgmt_op[] = {
441         "<0x0000>",
442         "Read Version",
443         "Read Commands",
444         "Read Index List",
445         "Read Controller Info",
446         "Set Powered",
447         "Set Discoverable",
448         "Set Connectable",
449         "Set Fast Connectable",         /* 0x0008 */
450         "Set Pairable",
451         "Set Link Security",
452         "Set Secure Simple Pairing",
453         "Set High Speed",
454         "Set Low Energy",
455         "Set Dev Class",
456         "Set Local Name",
457         "Add UUID",                     /* 0x0010 */
458         "Remove UUID",
459         "Load Link Keys",
460         "Load Long Term Keys",
461         "Disconnect",
462         "Get Connections",
463         "PIN Code Reply",
464         "PIN Code Neg Reply",
465         "Set IO Capability",            /* 0x0018 */
466         "Pair Device",
467         "Cancel Pair Device",
468         "Unpair Device",
469         "User Confirm Reply",
470         "User Confirm Neg Reply",
471         "User Passkey Reply",
472         "User Passkey Neg Reply",
473         "Read Local OOB Data",          /* 0x0020 */
474         "Add Remote OOB Data",
475         "Remove Remove OOB Data",
476         "Start Discovery",
477         "Stop Discovery",
478         "Confirm Name",
479         "Block Device",
480         "Unblock Device",
481         "Set Device ID",
482 };
483
484 static const char *mgmt_ev[] = {
485         "<0x0000>",
486         "Command Complete",
487         "Command Status",
488         "Controller Error",
489         "Index Added",
490         "Index Removed",
491         "New Settings",
492         "Class of Device Changed",
493         "Local Name Changed",           /* 0x0008 */
494         "New Link Key",
495         "New Long Term Key",
496         "Device Connected",
497         "Device Disconnected",
498         "Connect Failed",
499         "PIN Code Request",
500         "User Confirm Request",
501         "User Passkey Request",         /* 0x0010 */
502         "Authentication Failed",
503         "Device Found",
504         "Discovering",
505         "Device Blocked",
506         "Device Unblocked",
507         "Device Unpaired",
508 };
509
510 static const char *mgmt_status[] = {
511         "Success",
512         "Unknown Command",
513         "Not Connected",
514         "Failed",
515         "Connect Failed",
516         "Authentication Failed",
517         "Not Paired",
518         "No Resources",
519         "Timeout",
520         "Already Connected",
521         "Busy",
522         "Rejected",
523         "Not Supported",
524         "Invalid Parameters",
525         "Disconnected",
526         "Not Powered",
527         "Cancelled",
528         "Invalid Index",
529 };
530
531 #ifndef NELEM
532 #define NELEM(x) (sizeof(x) / sizeof((x)[0]))
533 #endif
534
535 static inline const char *mgmt_opstr(uint16_t op)
536 {
537         if (op >= NELEM(mgmt_op))
538                 return "<unknown opcode>";
539         return mgmt_op[op];
540 }
541
542 static inline const char *mgmt_evstr(uint16_t ev)
543 {
544         if (ev >= NELEM(mgmt_ev))
545                 return "<unknown event>";
546         return mgmt_ev[ev];
547 }
548
549 static inline const char *mgmt_errstr(uint8_t status)
550 {
551         if (status >= NELEM(mgmt_status))
552                 return "<unknown status>";
553         return mgmt_status[status];
554 }