gap.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. // SPDX-License-Identifier: LGPL-2.1-or-later
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2015 Intel Corporation. All rights reserved.
  7. *
  8. *
  9. */
  10. #ifdef HAVE_CONFIG_H
  11. #include <config.h>
  12. #endif
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include "lib/bluetooth.h"
  16. #include "lib/mgmt.h"
  17. #include "src/shared/util.h"
  18. #include "src/shared/mgmt.h"
  19. #include "peripheral/gatt.h"
  20. #include "peripheral/gap.h"
  21. static struct mgmt *mgmt = NULL;
  22. static uint16_t mgmt_index = MGMT_INDEX_NONE;
  23. static bool adv_features = false;
  24. static bool adv_instances = false;
  25. static bool require_connectable = true;
  26. static uint8_t static_addr[6] = { 0x90, 0x78, 0x56, 0x34, 0x12, 0xc0 };
  27. static uint8_t dev_name[260] = { 0x00, };
  28. static uint8_t dev_name_len = 0;
  29. void gap_set_static_address(uint8_t addr[6])
  30. {
  31. memcpy(static_addr, addr, sizeof(static_addr));
  32. printf("Using static address %02x:%02x:%02x:%02x:%02x:%02x\n",
  33. static_addr[5], static_addr[4], static_addr[3],
  34. static_addr[2], static_addr[1], static_addr[0]);
  35. }
  36. static void clear_long_term_keys(uint16_t index)
  37. {
  38. struct mgmt_cp_load_long_term_keys cp;
  39. memset(&cp, 0, sizeof(cp));
  40. cp.key_count = cpu_to_le16(0);
  41. mgmt_send(mgmt, MGMT_OP_LOAD_LONG_TERM_KEYS, index,
  42. sizeof(cp), &cp, NULL, NULL, NULL);
  43. }
  44. static void clear_identity_resolving_keys(uint16_t index)
  45. {
  46. struct mgmt_cp_load_irks cp;
  47. memset(&cp, 0, sizeof(cp));
  48. cp.irk_count = cpu_to_le16(0);
  49. mgmt_send(mgmt, MGMT_OP_LOAD_IRKS, index,
  50. sizeof(cp), &cp, NULL, NULL, NULL);
  51. }
  52. static void add_advertising(uint16_t index)
  53. {
  54. const char ad[] = { 0x11, 0x15,
  55. 0xd0, 0x00, 0x2d, 0x12, 0x1e, 0x4b, 0x0f, 0xa4,
  56. 0x99, 0x4e, 0xce, 0xb5, 0x31, 0xf4, 0x05, 0x79 };
  57. struct mgmt_cp_add_advertising *cp;
  58. void *buf;
  59. buf = malloc(sizeof(*cp) + sizeof(ad));
  60. if (!buf)
  61. return;
  62. memset(buf, 0, sizeof(*cp) + sizeof(ad));
  63. cp = buf;
  64. cp->instance = 0x01;
  65. cp->flags = cpu_to_le32((1 << 0) | (1 << 1) | (1 << 4));
  66. cp->duration = cpu_to_le16(0);
  67. cp->timeout = cpu_to_le16(0);
  68. cp->adv_data_len = sizeof(ad);
  69. cp->scan_rsp_len = 0;
  70. memcpy(cp->data, ad, sizeof(ad));
  71. mgmt_send(mgmt, MGMT_OP_ADD_ADVERTISING, index,
  72. sizeof(*cp) + sizeof(ad), buf, NULL, NULL, NULL);
  73. free(buf);
  74. }
  75. static void enable_advertising(uint16_t index)
  76. {
  77. uint8_t val;
  78. val = require_connectable ? 0x01 : 0x00;
  79. mgmt_send(mgmt, MGMT_OP_SET_CONNECTABLE, index, 1, &val,
  80. NULL, NULL, NULL);
  81. val = 0x01;
  82. mgmt_send(mgmt, MGMT_OP_SET_POWERED, index, 1, &val,
  83. NULL, NULL, NULL);
  84. if (adv_instances) {
  85. add_advertising(index);
  86. return;
  87. }
  88. val = require_connectable ? 0x01 : 0x02;
  89. mgmt_send(mgmt, MGMT_OP_SET_ADVERTISING, index, 1, &val,
  90. NULL, NULL, NULL);
  91. }
  92. static void new_settings_event(uint16_t index, uint16_t length,
  93. const void *param, void *user_data)
  94. {
  95. printf("New settings\n");
  96. }
  97. static void local_name_changed_event(uint16_t index, uint16_t length,
  98. const void *param, void *user_data)
  99. {
  100. printf("Local name changed\n");
  101. }
  102. static void new_long_term_key_event(uint16_t index, uint16_t length,
  103. const void *param, void *user_data)
  104. {
  105. printf("New long term key\n");
  106. }
  107. static void device_connected_event(uint16_t index, uint16_t length,
  108. const void *param, void *user_data)
  109. {
  110. printf("Device connected\n");
  111. }
  112. static void device_disconnected_event(uint16_t index, uint16_t length,
  113. const void *param, void *user_data)
  114. {
  115. printf("Device disconnected\n");
  116. }
  117. static void user_confirm_request_event(uint16_t index, uint16_t length,
  118. const void *param, void *user_data)
  119. {
  120. printf("User confirm request\n");
  121. }
  122. static void user_passkey_request_event(uint16_t index, uint16_t length,
  123. const void *param, void *user_data)
  124. {
  125. printf("User passkey request\n");
  126. }
  127. static void auth_failed_event(uint16_t index, uint16_t length,
  128. const void *param, void *user_data)
  129. {
  130. printf("Authentication failed\n");
  131. }
  132. static void device_unpaired_event(uint16_t index, uint16_t length,
  133. const void *param, void *user_data)
  134. {
  135. printf("Device unpaired\n");
  136. }
  137. static void passkey_notify_event(uint16_t index, uint16_t length,
  138. const void *param, void *user_data)
  139. {
  140. printf("Passkey notification\n");
  141. }
  142. static void new_irk_event(uint16_t index, uint16_t length,
  143. const void *param, void *user_data)
  144. {
  145. printf("New identify resolving key\n");
  146. }
  147. static void new_csrk_event(uint16_t index, uint16_t length,
  148. const void *param, void *user_data)
  149. {
  150. printf("New connection signature resolving key\n");
  151. }
  152. static void new_conn_param_event(uint16_t index, uint16_t length,
  153. const void *param, void *user_data)
  154. {
  155. printf("New connection parameter\n");
  156. }
  157. static void advertising_added_event(uint16_t index, uint16_t length,
  158. const void *param, void *user_data)
  159. {
  160. printf("Advertising added\n");
  161. }
  162. static void advertising_removed_event(uint16_t index, uint16_t length,
  163. const void *param, void *user_data)
  164. {
  165. printf("Advertising removed\n");
  166. }
  167. static void read_adv_features_complete(uint8_t status, uint16_t len,
  168. const void *param, void *user_data)
  169. {
  170. const struct mgmt_rp_read_adv_features *rp = param;
  171. uint16_t index = PTR_TO_UINT(user_data);
  172. uint32_t flags;
  173. flags = le32_to_cpu(rp->supported_flags);
  174. if (rp->max_instances > 0) {
  175. adv_instances = true;
  176. if (flags & (1 << 0))
  177. require_connectable = false;
  178. } else
  179. require_connectable = false;
  180. enable_advertising(index);
  181. }
  182. static void read_info_complete(uint8_t status, uint16_t len,
  183. const void *param, void *user_data)
  184. {
  185. const struct mgmt_rp_read_info *rp = param;
  186. uint16_t index = PTR_TO_UINT(user_data);
  187. uint32_t required_settings = MGMT_SETTING_LE |
  188. MGMT_SETTING_STATIC_ADDRESS;
  189. uint32_t supported_settings, current_settings;
  190. uint8_t val;
  191. required_settings = MGMT_SETTING_LE;
  192. if (status) {
  193. fprintf(stderr, "Reading info for index %u failed: %s\n",
  194. index, mgmt_errstr(status));
  195. return;
  196. }
  197. if (mgmt_index != MGMT_INDEX_NONE)
  198. return;
  199. supported_settings = le32_to_cpu(rp->supported_settings);
  200. current_settings = le32_to_cpu(rp->current_settings);
  201. if ((supported_settings & required_settings) != required_settings)
  202. return;
  203. printf("Selecting index %u\n", index);
  204. mgmt_index = index;
  205. mgmt_register(mgmt, MGMT_EV_NEW_SETTINGS, index,
  206. new_settings_event, NULL, NULL);
  207. mgmt_register(mgmt, MGMT_EV_LOCAL_NAME_CHANGED, index,
  208. local_name_changed_event, NULL, NULL);
  209. mgmt_register(mgmt, MGMT_EV_NEW_LONG_TERM_KEY, index,
  210. new_long_term_key_event, NULL, NULL);
  211. mgmt_register(mgmt, MGMT_EV_DEVICE_CONNECTED, index,
  212. device_connected_event, NULL, NULL);
  213. mgmt_register(mgmt, MGMT_EV_DEVICE_DISCONNECTED, index,
  214. device_disconnected_event, NULL, NULL);
  215. mgmt_register(mgmt, MGMT_EV_USER_CONFIRM_REQUEST, index,
  216. user_confirm_request_event, NULL, NULL);
  217. mgmt_register(mgmt, MGMT_EV_USER_PASSKEY_REQUEST, index,
  218. user_passkey_request_event, NULL, NULL);
  219. mgmt_register(mgmt, MGMT_EV_AUTH_FAILED, index,
  220. auth_failed_event, NULL, NULL);
  221. mgmt_register(mgmt, MGMT_EV_DEVICE_UNPAIRED, index,
  222. device_unpaired_event, NULL, NULL);
  223. mgmt_register(mgmt, MGMT_EV_PASSKEY_NOTIFY, index,
  224. passkey_notify_event, NULL, NULL);
  225. mgmt_register(mgmt, MGMT_EV_NEW_IRK, index,
  226. new_irk_event, NULL, NULL);
  227. mgmt_register(mgmt, MGMT_EV_NEW_CSRK, index,
  228. new_csrk_event, NULL, NULL);
  229. mgmt_register(mgmt, MGMT_EV_NEW_CONN_PARAM, index,
  230. new_conn_param_event, NULL, NULL);
  231. mgmt_register(mgmt, MGMT_EV_ADVERTISING_ADDED, index,
  232. advertising_added_event, NULL, NULL);
  233. mgmt_register(mgmt, MGMT_EV_ADVERTISING_REMOVED, index,
  234. advertising_removed_event, NULL, NULL);
  235. dev_name_len = snprintf((char *) dev_name, 26, "BlueZ Peripheral");
  236. if (current_settings & MGMT_SETTING_POWERED) {
  237. val = 0x00;
  238. mgmt_send(mgmt, MGMT_OP_SET_POWERED, index, 1, &val,
  239. NULL, NULL, NULL);
  240. }
  241. if (!(current_settings & MGMT_SETTING_LE)) {
  242. val = 0x01;
  243. mgmt_send(mgmt, MGMT_OP_SET_LE, index, 1, &val,
  244. NULL, NULL, NULL);
  245. }
  246. if (current_settings & MGMT_SETTING_BREDR) {
  247. val = 0x00;
  248. mgmt_send(mgmt, MGMT_OP_SET_BREDR, index, 1, &val,
  249. NULL, NULL, NULL);
  250. }
  251. if ((supported_settings & MGMT_SETTING_SECURE_CONN) &&
  252. !(current_settings & MGMT_SETTING_SECURE_CONN)) {
  253. val = 0x01;
  254. mgmt_send(mgmt, MGMT_OP_SET_SECURE_CONN, index, 1, &val,
  255. NULL, NULL, NULL);
  256. }
  257. if (current_settings & MGMT_SETTING_DEBUG_KEYS) {
  258. val = 0x00;
  259. mgmt_send(mgmt, MGMT_OP_SET_DEBUG_KEYS, index, 1, &val,
  260. NULL, NULL, NULL);
  261. }
  262. if (!(current_settings & MGMT_SETTING_BONDABLE)) {
  263. val = 0x01;
  264. mgmt_send(mgmt, MGMT_OP_SET_BONDABLE, index, 1, &val,
  265. NULL, NULL, NULL);
  266. }
  267. clear_long_term_keys(mgmt_index);
  268. clear_identity_resolving_keys(mgmt_index);
  269. mgmt_send(mgmt, MGMT_OP_SET_STATIC_ADDRESS, index,
  270. 6, static_addr, NULL, NULL, NULL);
  271. mgmt_send(mgmt, MGMT_OP_SET_LOCAL_NAME, index,
  272. 260, dev_name, NULL, NULL, NULL);
  273. gatt_set_static_address(static_addr);
  274. gatt_set_device_name(dev_name, dev_name_len);
  275. gatt_server_start();
  276. if (adv_features)
  277. mgmt_send(mgmt, MGMT_OP_READ_ADV_FEATURES, index, 0, NULL,
  278. read_adv_features_complete,
  279. UINT_TO_PTR(index), NULL);
  280. else
  281. enable_advertising(index);
  282. }
  283. static void read_index_list_complete(uint8_t status, uint16_t len,
  284. const void *param, void *user_data)
  285. {
  286. const struct mgmt_rp_read_index_list *rp = param;
  287. uint16_t count;
  288. int i;
  289. if (status) {
  290. fprintf(stderr, "Reading index list failed: %s\n",
  291. mgmt_errstr(status));
  292. return;
  293. }
  294. count = le16_to_cpu(rp->num_controllers);
  295. printf("Index list: %u\n", count);
  296. for (i = 0; i < count; i++) {
  297. uint16_t index = cpu_to_le16(rp->index[i]);
  298. mgmt_send(mgmt, MGMT_OP_READ_INFO, index, 0, NULL,
  299. read_info_complete, UINT_TO_PTR(index), NULL);
  300. }
  301. }
  302. static void index_added_event(uint16_t index, uint16_t length,
  303. const void *param, void *user_data)
  304. {
  305. printf("Index added\n");
  306. if (mgmt_index != MGMT_INDEX_NONE)
  307. return;
  308. mgmt_send(mgmt, MGMT_OP_READ_INFO, index, 0, NULL,
  309. read_info_complete, UINT_TO_PTR(index), NULL);
  310. }
  311. static void index_removed_event(uint16_t index, uint16_t length,
  312. const void *param, void *user_data)
  313. {
  314. printf("Index removed\n");
  315. if (mgmt_index != index)
  316. return;
  317. mgmt_index = MGMT_INDEX_NONE;
  318. }
  319. static void read_ext_index_list_complete(uint8_t status, uint16_t len,
  320. const void *param, void *user_data)
  321. {
  322. const struct mgmt_rp_read_ext_index_list *rp = param;
  323. uint16_t count;
  324. int i;
  325. if (status) {
  326. fprintf(stderr, "Reading extended index list failed: %s\n",
  327. mgmt_errstr(status));
  328. return;
  329. }
  330. count = le16_to_cpu(rp->num_controllers);
  331. printf("Extended index list: %u\n", count);
  332. for (i = 0; i < count; i++) {
  333. uint16_t index = cpu_to_le16(rp->entry[i].index);
  334. if (rp->entry[i].type != 0x00)
  335. continue;
  336. mgmt_send(mgmt, MGMT_OP_READ_INFO, index, 0, NULL,
  337. read_info_complete, UINT_TO_PTR(index), NULL);
  338. }
  339. }
  340. static void ext_index_added_event(uint16_t index, uint16_t length,
  341. const void *param, void *user_data)
  342. {
  343. const struct mgmt_ev_ext_index_added *ev = param;
  344. printf("Extended index added: %u\n", ev->type);
  345. if (mgmt_index != MGMT_INDEX_NONE)
  346. return;
  347. if (ev->type != 0x00)
  348. return;
  349. mgmt_send(mgmt, MGMT_OP_READ_INFO, index, 0, NULL,
  350. read_info_complete, UINT_TO_PTR(index), NULL);
  351. }
  352. static void ext_index_removed_event(uint16_t index, uint16_t length,
  353. const void *param, void *user_data)
  354. {
  355. const struct mgmt_ev_ext_index_added *ev = param;
  356. printf("Extended index removed: %u\n", ev->type);
  357. if (mgmt_index != index)
  358. return;
  359. if (ev->type != 0x00)
  360. return;
  361. mgmt_index = MGMT_INDEX_NONE;
  362. }
  363. static void read_commands_complete(uint8_t status, uint16_t len,
  364. const void *param, void *user_data)
  365. {
  366. const struct mgmt_rp_read_commands *rp = param;
  367. uint16_t num_commands;
  368. bool ext_index_list = false;
  369. int i;
  370. if (status) {
  371. fprintf(stderr, "Reading index list failed: %s\n",
  372. mgmt_errstr(status));
  373. return;
  374. }
  375. num_commands = le16_to_cpu(rp->num_commands);
  376. for (i = 0; i < num_commands; i++) {
  377. uint16_t op = get_le16(rp->opcodes + 1);
  378. if (op == MGMT_OP_READ_EXT_INDEX_LIST)
  379. ext_index_list = true;
  380. else if (op == MGMT_OP_READ_ADV_FEATURES)
  381. adv_features = true;
  382. }
  383. if (ext_index_list) {
  384. mgmt_register(mgmt, MGMT_EV_EXT_INDEX_ADDED, MGMT_INDEX_NONE,
  385. ext_index_added_event, NULL, NULL);
  386. mgmt_register(mgmt, MGMT_EV_EXT_INDEX_REMOVED, MGMT_INDEX_NONE,
  387. ext_index_removed_event, NULL, NULL);
  388. if (!mgmt_send(mgmt, MGMT_OP_READ_EXT_INDEX_LIST,
  389. MGMT_INDEX_NONE, 0, NULL,
  390. read_ext_index_list_complete, NULL, NULL)) {
  391. fprintf(stderr, "Failed to read extended index list\n");
  392. return;
  393. }
  394. } else {
  395. mgmt_register(mgmt, MGMT_EV_INDEX_ADDED, MGMT_INDEX_NONE,
  396. index_added_event, NULL, NULL);
  397. mgmt_register(mgmt, MGMT_EV_INDEX_REMOVED, MGMT_INDEX_NONE,
  398. index_removed_event, NULL, NULL);
  399. if (!mgmt_send(mgmt, MGMT_OP_READ_INDEX_LIST,
  400. MGMT_INDEX_NONE, 0, NULL,
  401. read_index_list_complete, NULL, NULL)) {
  402. fprintf(stderr, "Failed to read index list\n");
  403. return;
  404. }
  405. }
  406. }
  407. void gap_start(void)
  408. {
  409. mgmt = mgmt_new_default();
  410. if (!mgmt) {
  411. fprintf(stderr, "Failed to open management socket\n");
  412. return;
  413. }
  414. if (!mgmt_send(mgmt, MGMT_OP_READ_COMMANDS,
  415. MGMT_INDEX_NONE, 0, NULL,
  416. read_commands_complete, NULL, NULL)) {
  417. fprintf(stderr, "Failed to read supported commands\n");
  418. return;
  419. }
  420. }
  421. void gap_stop(void)
  422. {
  423. if (!mgmt)
  424. return;
  425. gatt_server_stop();
  426. mgmt_unref(mgmt);
  427. mgmt = NULL;
  428. mgmt_index = MGMT_INDEX_NONE;
  429. }