gatttool.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2010 Nokia Corporation
  7. * Copyright (C) 2010 Marcel Holtmann <marcel@holtmann.org>
  8. *
  9. *
  10. */
  11. #ifdef HAVE_CONFIG_H
  12. #include <config.h>
  13. #endif
  14. #include <errno.h>
  15. #include <stdlib.h>
  16. #include <unistd.h>
  17. #include <glib.h>
  18. #include "lib/bluetooth.h"
  19. #include "lib/hci.h"
  20. #include "lib/hci_lib.h"
  21. #include "lib/sdp.h"
  22. #include "lib/uuid.h"
  23. #include "src/shared/util.h"
  24. #include "att.h"
  25. #include "btio/btio.h"
  26. #include "gattrib.h"
  27. #include "gatt.h"
  28. #include "gatttool.h"
  29. static char *opt_src = NULL;
  30. static char *opt_dst = NULL;
  31. static char *opt_dst_type = NULL;
  32. static char *opt_value = NULL;
  33. static char *opt_sec_level = NULL;
  34. static bt_uuid_t *opt_uuid = NULL;
  35. static int opt_start = 0x0001;
  36. static int opt_end = 0xffff;
  37. static int opt_handle = -1;
  38. static int opt_mtu = 0;
  39. static int opt_psm = 0;
  40. static gboolean opt_primary = FALSE;
  41. static gboolean opt_characteristics = FALSE;
  42. static gboolean opt_char_read = FALSE;
  43. static gboolean opt_listen = FALSE;
  44. static gboolean opt_char_desc = FALSE;
  45. static gboolean opt_char_write = FALSE;
  46. static gboolean opt_char_write_req = FALSE;
  47. static gboolean opt_interactive = FALSE;
  48. static GMainLoop *event_loop;
  49. static gboolean got_error = FALSE;
  50. static GSourceFunc operation;
  51. struct characteristic_data {
  52. GAttrib *attrib;
  53. uint16_t start;
  54. uint16_t end;
  55. };
  56. static void events_handler(const uint8_t *pdu, uint16_t len, gpointer user_data)
  57. {
  58. GAttrib *attrib = user_data;
  59. uint8_t *opdu;
  60. uint16_t handle, i, olen = 0;
  61. size_t plen;
  62. handle = get_le16(&pdu[1]);
  63. switch (pdu[0]) {
  64. case ATT_OP_HANDLE_NOTIFY:
  65. g_print("Notification handle = 0x%04x value: ", handle);
  66. break;
  67. case ATT_OP_HANDLE_IND:
  68. g_print("Indication handle = 0x%04x value: ", handle);
  69. break;
  70. default:
  71. g_print("Invalid opcode\n");
  72. return;
  73. }
  74. for (i = 3; i < len; i++)
  75. g_print("%02x ", pdu[i]);
  76. g_print("\n");
  77. if (pdu[0] == ATT_OP_HANDLE_NOTIFY)
  78. return;
  79. opdu = g_attrib_get_buffer(attrib, &plen);
  80. olen = enc_confirmation(opdu, plen);
  81. if (olen > 0)
  82. g_attrib_send(attrib, 0, opdu, olen, NULL, NULL, NULL);
  83. }
  84. static gboolean listen_start(gpointer user_data)
  85. {
  86. GAttrib *attrib = user_data;
  87. g_attrib_register(attrib, ATT_OP_HANDLE_NOTIFY, GATTRIB_ALL_HANDLES,
  88. events_handler, attrib, NULL);
  89. g_attrib_register(attrib, ATT_OP_HANDLE_IND, GATTRIB_ALL_HANDLES,
  90. events_handler, attrib, NULL);
  91. return FALSE;
  92. }
  93. static void connect_cb(GIOChannel *io, GError *err, gpointer user_data)
  94. {
  95. GAttrib *attrib;
  96. uint16_t mtu;
  97. uint16_t cid;
  98. GError *gerr = NULL;
  99. if (err) {
  100. g_printerr("%s\n", err->message);
  101. got_error = TRUE;
  102. g_main_loop_quit(event_loop);
  103. }
  104. bt_io_get(io, &gerr, BT_IO_OPT_IMTU, &mtu,
  105. BT_IO_OPT_CID, &cid, BT_IO_OPT_INVALID);
  106. if (gerr) {
  107. g_printerr("Can't detect MTU, using default: %s",
  108. gerr->message);
  109. g_error_free(gerr);
  110. mtu = ATT_DEFAULT_LE_MTU;
  111. }
  112. if (cid == ATT_CID)
  113. mtu = ATT_DEFAULT_LE_MTU;
  114. attrib = g_attrib_new(io, mtu, false);
  115. if (opt_listen)
  116. g_idle_add(listen_start, attrib);
  117. operation(attrib);
  118. }
  119. static void primary_all_cb(uint8_t status, GSList *services, void *user_data)
  120. {
  121. GSList *l;
  122. if (status) {
  123. g_printerr("Discover all primary services failed: %s\n",
  124. att_ecode2str(status));
  125. goto done;
  126. }
  127. for (l = services; l; l = l->next) {
  128. struct gatt_primary *prim = l->data;
  129. g_print("attr handle = 0x%04x, end grp handle = 0x%04x "
  130. "uuid: %s\n", prim->range.start, prim->range.end, prim->uuid);
  131. }
  132. done:
  133. g_main_loop_quit(event_loop);
  134. }
  135. static void primary_by_uuid_cb(uint8_t status, GSList *ranges, void *user_data)
  136. {
  137. GSList *l;
  138. if (status != 0) {
  139. g_printerr("Discover primary services by UUID failed: %s\n",
  140. att_ecode2str(status));
  141. goto done;
  142. }
  143. for (l = ranges; l; l = l->next) {
  144. struct att_range *range = l->data;
  145. g_print("Starting handle: %04x Ending handle: %04x\n",
  146. range->start, range->end);
  147. }
  148. done:
  149. g_main_loop_quit(event_loop);
  150. }
  151. static gboolean primary(gpointer user_data)
  152. {
  153. GAttrib *attrib = user_data;
  154. if (opt_uuid)
  155. gatt_discover_primary(attrib, opt_uuid, primary_by_uuid_cb,
  156. NULL);
  157. else
  158. gatt_discover_primary(attrib, NULL, primary_all_cb, NULL);
  159. return FALSE;
  160. }
  161. static void char_discovered_cb(uint8_t status, GSList *characteristics,
  162. void *user_data)
  163. {
  164. GSList *l;
  165. if (status) {
  166. g_printerr("Discover all characteristics failed: %s\n",
  167. att_ecode2str(status));
  168. goto done;
  169. }
  170. for (l = characteristics; l; l = l->next) {
  171. struct gatt_char *chars = l->data;
  172. g_print("handle = 0x%04x, char properties = 0x%02x, char value "
  173. "handle = 0x%04x, uuid = %s\n", chars->handle,
  174. chars->properties, chars->value_handle, chars->uuid);
  175. }
  176. done:
  177. g_main_loop_quit(event_loop);
  178. }
  179. static gboolean characteristics(gpointer user_data)
  180. {
  181. GAttrib *attrib = user_data;
  182. gatt_discover_char(attrib, opt_start, opt_end, opt_uuid,
  183. char_discovered_cb, NULL);
  184. return FALSE;
  185. }
  186. static void char_read_cb(guint8 status, const guint8 *pdu, guint16 plen,
  187. gpointer user_data)
  188. {
  189. uint8_t value[plen];
  190. ssize_t vlen;
  191. int i;
  192. if (status != 0) {
  193. g_printerr("Characteristic value/descriptor read failed: %s\n",
  194. att_ecode2str(status));
  195. goto done;
  196. }
  197. vlen = dec_read_resp(pdu, plen, value, sizeof(value));
  198. if (vlen < 0) {
  199. g_printerr("Protocol error\n");
  200. goto done;
  201. }
  202. g_print("Characteristic value/descriptor: ");
  203. for (i = 0; i < vlen; i++)
  204. g_print("%02x ", value[i]);
  205. g_print("\n");
  206. done:
  207. if (!opt_listen)
  208. g_main_loop_quit(event_loop);
  209. }
  210. static void char_read_by_uuid_cb(guint8 status, const guint8 *pdu,
  211. guint16 plen, gpointer user_data)
  212. {
  213. struct att_data_list *list;
  214. int i;
  215. if (status != 0) {
  216. g_printerr("Read characteristics by UUID failed: %s\n",
  217. att_ecode2str(status));
  218. goto done;
  219. }
  220. list = dec_read_by_type_resp(pdu, plen);
  221. if (list == NULL)
  222. goto done;
  223. for (i = 0; i < list->num; i++) {
  224. uint8_t *value = list->data[i];
  225. int j;
  226. g_print("handle: 0x%04x \t value: ", get_le16(value));
  227. value += 2;
  228. for (j = 0; j < list->len - 2; j++, value++)
  229. g_print("%02x ", *value);
  230. g_print("\n");
  231. }
  232. att_data_list_free(list);
  233. done:
  234. g_main_loop_quit(event_loop);
  235. }
  236. static gboolean characteristics_read(gpointer user_data)
  237. {
  238. GAttrib *attrib = user_data;
  239. if (opt_uuid != NULL) {
  240. gatt_read_char_by_uuid(attrib, opt_start, opt_end, opt_uuid,
  241. char_read_by_uuid_cb, NULL);
  242. return FALSE;
  243. }
  244. if (opt_handle <= 0) {
  245. g_printerr("A valid handle is required\n");
  246. g_main_loop_quit(event_loop);
  247. return FALSE;
  248. }
  249. gatt_read_char(attrib, opt_handle, char_read_cb, attrib);
  250. return FALSE;
  251. }
  252. static void mainloop_quit(gpointer user_data)
  253. {
  254. uint8_t *value = user_data;
  255. g_free(value);
  256. g_main_loop_quit(event_loop);
  257. }
  258. static gboolean characteristics_write(gpointer user_data)
  259. {
  260. GAttrib *attrib = user_data;
  261. uint8_t *value;
  262. size_t len;
  263. if (opt_handle <= 0) {
  264. g_printerr("A valid handle is required\n");
  265. goto error;
  266. }
  267. if (opt_value == NULL || opt_value[0] == '\0') {
  268. g_printerr("A value is required\n");
  269. goto error;
  270. }
  271. len = gatt_attr_data_from_string(opt_value, &value);
  272. if (len == 0) {
  273. g_printerr("Invalid value\n");
  274. goto error;
  275. }
  276. gatt_write_cmd(attrib, opt_handle, value, len, mainloop_quit, value);
  277. g_free(value);
  278. return FALSE;
  279. error:
  280. g_main_loop_quit(event_loop);
  281. return FALSE;
  282. }
  283. static void char_write_req_cb(guint8 status, const guint8 *pdu, guint16 plen,
  284. gpointer user_data)
  285. {
  286. if (status != 0) {
  287. g_printerr("Characteristic Write Request failed: "
  288. "%s\n", att_ecode2str(status));
  289. goto done;
  290. }
  291. if (!dec_write_resp(pdu, plen) && !dec_exec_write_resp(pdu, plen)) {
  292. g_printerr("Protocol error\n");
  293. goto done;
  294. }
  295. g_print("Characteristic value was written successfully\n");
  296. done:
  297. if (!opt_listen)
  298. g_main_loop_quit(event_loop);
  299. }
  300. static gboolean characteristics_write_req(gpointer user_data)
  301. {
  302. GAttrib *attrib = user_data;
  303. uint8_t *value;
  304. size_t len;
  305. if (opt_handle <= 0) {
  306. g_printerr("A valid handle is required\n");
  307. goto error;
  308. }
  309. if (opt_value == NULL || opt_value[0] == '\0') {
  310. g_printerr("A value is required\n");
  311. goto error;
  312. }
  313. len = gatt_attr_data_from_string(opt_value, &value);
  314. if (len == 0) {
  315. g_printerr("Invalid value\n");
  316. goto error;
  317. }
  318. gatt_write_char(attrib, opt_handle, value, len, char_write_req_cb,
  319. NULL);
  320. g_free(value);
  321. return FALSE;
  322. error:
  323. g_main_loop_quit(event_loop);
  324. return FALSE;
  325. }
  326. static void char_desc_cb(uint8_t status, GSList *descriptors, void *user_data)
  327. {
  328. GSList *l;
  329. if (status) {
  330. g_printerr("Discover descriptors failed: %s\n",
  331. att_ecode2str(status));
  332. return;
  333. }
  334. for (l = descriptors; l; l = l->next) {
  335. struct gatt_desc *desc = l->data;
  336. g_print("handle = 0x%04x, uuid = %s\n", desc->handle,
  337. desc->uuid);
  338. }
  339. if (!opt_listen)
  340. g_main_loop_quit(event_loop);
  341. }
  342. static gboolean characteristics_desc(gpointer user_data)
  343. {
  344. GAttrib *attrib = user_data;
  345. gatt_discover_desc(attrib, opt_start, opt_end, NULL, char_desc_cb,
  346. NULL);
  347. return FALSE;
  348. }
  349. static gboolean parse_uuid(const char *key, const char *value,
  350. gpointer user_data, GError **error)
  351. {
  352. if (!value)
  353. return FALSE;
  354. opt_uuid = g_try_malloc(sizeof(bt_uuid_t));
  355. if (opt_uuid == NULL)
  356. return FALSE;
  357. if (bt_string_to_uuid(opt_uuid, value) < 0)
  358. return FALSE;
  359. return TRUE;
  360. }
  361. static GOptionEntry primary_char_options[] = {
  362. { "start", 's' , 0, G_OPTION_ARG_INT, &opt_start,
  363. "Starting handle(optional)", "0x0001" },
  364. { "end", 'e' , 0, G_OPTION_ARG_INT, &opt_end,
  365. "Ending handle(optional)", "0xffff" },
  366. { "uuid", 'u', G_OPTION_FLAG_OPTIONAL_ARG, G_OPTION_ARG_CALLBACK,
  367. parse_uuid, "UUID16 or UUID128(optional)", "0x1801"},
  368. { NULL },
  369. };
  370. static GOptionEntry char_rw_options[] = {
  371. { "handle", 'a' , 0, G_OPTION_ARG_INT, &opt_handle,
  372. "Read/Write characteristic by handle(required)", "0x0001" },
  373. { "value", 'n' , 0, G_OPTION_ARG_STRING, &opt_value,
  374. "Write characteristic value (required for write operation)",
  375. "0x0001" },
  376. {NULL},
  377. };
  378. static GOptionEntry gatt_options[] = {
  379. { "primary", 0, 0, G_OPTION_ARG_NONE, &opt_primary,
  380. "Primary Service Discovery", NULL },
  381. { "characteristics", 0, 0, G_OPTION_ARG_NONE, &opt_characteristics,
  382. "Characteristics Discovery", NULL },
  383. { "char-read", 0, 0, G_OPTION_ARG_NONE, &opt_char_read,
  384. "Characteristics Value/Descriptor Read", NULL },
  385. { "char-write", 0, 0, G_OPTION_ARG_NONE, &opt_char_write,
  386. "Characteristics Value Write Without Response (Write Command)",
  387. NULL },
  388. { "char-write-req", 0, 0, G_OPTION_ARG_NONE, &opt_char_write_req,
  389. "Characteristics Value Write (Write Request)", NULL },
  390. { "char-desc", 0, 0, G_OPTION_ARG_NONE, &opt_char_desc,
  391. "Characteristics Descriptor Discovery", NULL },
  392. { "listen", 0, 0, G_OPTION_ARG_NONE, &opt_listen,
  393. "Listen for notifications and indications", NULL },
  394. { "interactive", 'I', G_OPTION_FLAG_IN_MAIN, G_OPTION_ARG_NONE,
  395. &opt_interactive, "Use interactive mode", NULL },
  396. { NULL },
  397. };
  398. static GOptionEntry options[] = {
  399. { "adapter", 'i', 0, G_OPTION_ARG_STRING, &opt_src,
  400. "Specify local adapter interface", "hciX" },
  401. { "device", 'b', 0, G_OPTION_ARG_STRING, &opt_dst,
  402. "Specify remote Bluetooth address", "MAC" },
  403. { "addr-type", 't', 0, G_OPTION_ARG_STRING, &opt_dst_type,
  404. "Set LE address type. Default: public", "[public | random]"},
  405. { "mtu", 'm', 0, G_OPTION_ARG_INT, &opt_mtu,
  406. "Specify the MTU size", "MTU" },
  407. { "psm", 'p', 0, G_OPTION_ARG_INT, &opt_psm,
  408. "Specify the PSM for GATT/ATT over BR/EDR", "PSM" },
  409. { "sec-level", 'l', 0, G_OPTION_ARG_STRING, &opt_sec_level,
  410. "Set security level. Default: low", "[low | medium | high]"},
  411. { NULL },
  412. };
  413. int main(int argc, char *argv[])
  414. {
  415. GOptionContext *context;
  416. GOptionGroup *gatt_group, *params_group, *char_rw_group;
  417. GError *gerr = NULL;
  418. GIOChannel *chan;
  419. opt_dst_type = g_strdup("public");
  420. opt_sec_level = g_strdup("low");
  421. context = g_option_context_new(NULL);
  422. g_option_context_add_main_entries(context, options, NULL);
  423. /* GATT commands */
  424. gatt_group = g_option_group_new("gatt", "GATT commands",
  425. "Show all GATT commands", NULL, NULL);
  426. g_option_context_add_group(context, gatt_group);
  427. g_option_group_add_entries(gatt_group, gatt_options);
  428. /* Primary Services and Characteristics arguments */
  429. params_group = g_option_group_new("params",
  430. "Primary Services/Characteristics arguments",
  431. "Show all Primary Services/Characteristics arguments",
  432. NULL, NULL);
  433. g_option_context_add_group(context, params_group);
  434. g_option_group_add_entries(params_group, primary_char_options);
  435. /* Characteristics value/descriptor read/write arguments */
  436. char_rw_group = g_option_group_new("char-read-write",
  437. "Characteristics Value/Descriptor Read/Write arguments",
  438. "Show all Characteristics Value/Descriptor Read/Write "
  439. "arguments",
  440. NULL, NULL);
  441. g_option_context_add_group(context, char_rw_group);
  442. g_option_group_add_entries(char_rw_group, char_rw_options);
  443. if (!g_option_context_parse(context, &argc, &argv, &gerr)) {
  444. g_printerr("%s\n", gerr->message);
  445. g_clear_error(&gerr);
  446. }
  447. if (opt_interactive) {
  448. interactive(opt_src, opt_dst, opt_dst_type, opt_psm);
  449. goto done;
  450. }
  451. if (opt_primary)
  452. operation = primary;
  453. else if (opt_characteristics)
  454. operation = characteristics;
  455. else if (opt_char_read)
  456. operation = characteristics_read;
  457. else if (opt_char_write)
  458. operation = characteristics_write;
  459. else if (opt_char_write_req)
  460. operation = characteristics_write_req;
  461. else if (opt_char_desc)
  462. operation = characteristics_desc;
  463. else {
  464. char *help = g_option_context_get_help(context, TRUE, NULL);
  465. g_print("%s\n", help);
  466. g_free(help);
  467. got_error = TRUE;
  468. goto done;
  469. }
  470. if (opt_dst == NULL) {
  471. g_print("Remote Bluetooth address required\n");
  472. got_error = TRUE;
  473. goto done;
  474. }
  475. chan = gatt_connect(opt_src, opt_dst, opt_dst_type, opt_sec_level,
  476. opt_psm, opt_mtu, connect_cb, &gerr);
  477. if (chan == NULL) {
  478. g_printerr("%s\n", gerr->message);
  479. g_clear_error(&gerr);
  480. got_error = TRUE;
  481. goto done;
  482. }
  483. event_loop = g_main_loop_new(NULL, FALSE);
  484. g_main_loop_run(event_loop);
  485. g_main_loop_unref(event_loop);
  486. done:
  487. g_option_context_free(context);
  488. g_free(opt_src);
  489. g_free(opt_dst);
  490. g_free(opt_uuid);
  491. g_free(opt_sec_level);
  492. if (got_error)
  493. exit(EXIT_FAILURE);
  494. else
  495. exit(EXIT_SUCCESS);
  496. }