interactive.c 20 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2011 Nokia Corporation
  7. *
  8. *
  9. */
  10. #ifdef HAVE_CONFIG_H
  11. #include <config.h>
  12. #endif
  13. #define _GNU_SOURCE
  14. #include <string.h>
  15. #include <stdlib.h>
  16. #include <stdarg.h>
  17. #include <errno.h>
  18. #include <stdio.h>
  19. #include <unistd.h>
  20. #include <signal.h>
  21. #include <sys/signalfd.h>
  22. #include <glib.h>
  23. #include <readline/readline.h>
  24. #include <readline/history.h>
  25. #include "lib/bluetooth.h"
  26. #include "lib/sdp.h"
  27. #include "lib/uuid.h"
  28. #include "src/shared/util.h"
  29. #include "btio/btio.h"
  30. #include "att.h"
  31. #include "gattrib.h"
  32. #include "gatt.h"
  33. #include "gatttool.h"
  34. #include "client/display.h"
  35. static GIOChannel *iochannel = NULL;
  36. static GAttrib *attrib = NULL;
  37. static GMainLoop *event_loop;
  38. static GString *prompt;
  39. static char *opt_src = NULL;
  40. static char *opt_dst = NULL;
  41. static char *opt_dst_type = NULL;
  42. static char *opt_sec_level = NULL;
  43. static int opt_psm = 0;
  44. static int opt_mtu = 0;
  45. static int start;
  46. static int end;
  47. static void cmd_help(int argcp, char **argvp);
  48. static enum state {
  49. STATE_DISCONNECTED,
  50. STATE_CONNECTING,
  51. STATE_CONNECTED
  52. } conn_state;
  53. #define error(fmt, arg...) \
  54. rl_printf(COLOR_RED "Error: " COLOR_OFF fmt, ## arg)
  55. #define failed(fmt, arg...) \
  56. rl_printf(COLOR_RED "Command Failed: " COLOR_OFF fmt, ## arg)
  57. static char *get_prompt(void)
  58. {
  59. if (conn_state == STATE_CONNECTED)
  60. g_string_assign(prompt, COLOR_BLUE);
  61. else
  62. g_string_assign(prompt, "");
  63. if (opt_dst)
  64. g_string_append_printf(prompt, "[%17s]", opt_dst);
  65. else
  66. g_string_append_printf(prompt, "[%17s]", "");
  67. if (conn_state == STATE_CONNECTED)
  68. g_string_append(prompt, COLOR_OFF);
  69. if (opt_psm)
  70. g_string_append(prompt, "[BR]");
  71. else
  72. g_string_append(prompt, "[LE]");
  73. g_string_append(prompt, "> ");
  74. return prompt->str;
  75. }
  76. static void set_state(enum state st)
  77. {
  78. conn_state = st;
  79. rl_set_prompt(get_prompt());
  80. }
  81. static void events_handler(const uint8_t *pdu, uint16_t len, gpointer user_data)
  82. {
  83. uint8_t *opdu;
  84. uint16_t handle, i, olen;
  85. size_t plen;
  86. GString *s;
  87. handle = get_le16(&pdu[1]);
  88. switch (pdu[0]) {
  89. case ATT_OP_HANDLE_NOTIFY:
  90. s = g_string_new(NULL);
  91. g_string_printf(s, "Notification handle = 0x%04x value: ",
  92. handle);
  93. break;
  94. case ATT_OP_HANDLE_IND:
  95. s = g_string_new(NULL);
  96. g_string_printf(s, "Indication handle = 0x%04x value: ",
  97. handle);
  98. break;
  99. default:
  100. error("Invalid opcode\n");
  101. return;
  102. }
  103. for (i = 3; i < len; i++)
  104. g_string_append_printf(s, "%02x ", pdu[i]);
  105. rl_printf("%s\n", s->str);
  106. g_string_free(s, TRUE);
  107. if (pdu[0] == ATT_OP_HANDLE_NOTIFY)
  108. return;
  109. opdu = g_attrib_get_buffer(attrib, &plen);
  110. olen = enc_confirmation(opdu, plen);
  111. if (olen > 0)
  112. g_attrib_send(attrib, 0, opdu, olen, NULL, NULL, NULL);
  113. }
  114. static void connect_cb(GIOChannel *io, GError *err, gpointer user_data)
  115. {
  116. uint16_t mtu;
  117. uint16_t cid;
  118. if (err) {
  119. set_state(STATE_DISCONNECTED);
  120. error("%s\n", err->message);
  121. return;
  122. }
  123. bt_io_get(io, &err, BT_IO_OPT_IMTU, &mtu,
  124. BT_IO_OPT_CID, &cid, BT_IO_OPT_INVALID);
  125. if (err) {
  126. g_printerr("Can't detect MTU, using default: %s", err->message);
  127. g_error_free(err);
  128. mtu = ATT_DEFAULT_LE_MTU;
  129. }
  130. if (cid == ATT_CID)
  131. mtu = ATT_DEFAULT_LE_MTU;
  132. attrib = g_attrib_new(iochannel, mtu, false);
  133. g_attrib_register(attrib, ATT_OP_HANDLE_NOTIFY, GATTRIB_ALL_HANDLES,
  134. events_handler, attrib, NULL);
  135. g_attrib_register(attrib, ATT_OP_HANDLE_IND, GATTRIB_ALL_HANDLES,
  136. events_handler, attrib, NULL);
  137. set_state(STATE_CONNECTED);
  138. rl_printf("Connection successful\n");
  139. }
  140. static void disconnect_io()
  141. {
  142. if (conn_state == STATE_DISCONNECTED)
  143. return;
  144. g_attrib_unref(attrib);
  145. attrib = NULL;
  146. opt_mtu = 0;
  147. g_io_channel_shutdown(iochannel, FALSE, NULL);
  148. g_io_channel_unref(iochannel);
  149. iochannel = NULL;
  150. set_state(STATE_DISCONNECTED);
  151. }
  152. static void primary_all_cb(uint8_t status, GSList *services, void *user_data)
  153. {
  154. GSList *l;
  155. if (status) {
  156. error("Discover all primary services failed: %s\n",
  157. att_ecode2str(status));
  158. return;
  159. }
  160. if (services == NULL) {
  161. error("No primary service found\n");
  162. return;
  163. }
  164. for (l = services; l; l = l->next) {
  165. struct gatt_primary *prim = l->data;
  166. rl_printf("attr handle: 0x%04x, end grp handle: 0x%04x uuid: %s\n",
  167. prim->range.start, prim->range.end, prim->uuid);
  168. }
  169. }
  170. static void primary_by_uuid_cb(uint8_t status, GSList *ranges, void *user_data)
  171. {
  172. GSList *l;
  173. if (status) {
  174. error("Discover primary services by UUID failed: %s\n",
  175. att_ecode2str(status));
  176. return;
  177. }
  178. if (ranges == NULL) {
  179. error("No service UUID found\n");
  180. return;
  181. }
  182. for (l = ranges; l; l = l->next) {
  183. struct att_range *range = l->data;
  184. rl_printf("Starting handle: 0x%04x Ending handle: 0x%04x\n",
  185. range->start, range->end);
  186. }
  187. }
  188. static void included_cb(uint8_t status, GSList *includes, void *user_data)
  189. {
  190. GSList *l;
  191. if (status) {
  192. error("Find included services failed: %s\n",
  193. att_ecode2str(status));
  194. return;
  195. }
  196. if (includes == NULL) {
  197. rl_printf("No included services found for this range\n");
  198. return;
  199. }
  200. for (l = includes; l; l = l->next) {
  201. struct gatt_included *incl = l->data;
  202. rl_printf("handle: 0x%04x, start handle: 0x%04x, "
  203. "end handle: 0x%04x uuid: %s\n",
  204. incl->handle, incl->range.start,
  205. incl->range.end, incl->uuid);
  206. }
  207. }
  208. static void char_cb(uint8_t status, GSList *characteristics, void *user_data)
  209. {
  210. GSList *l;
  211. if (status) {
  212. error("Discover all characteristics failed: %s\n",
  213. att_ecode2str(status));
  214. return;
  215. }
  216. for (l = characteristics; l; l = l->next) {
  217. struct gatt_char *chars = l->data;
  218. rl_printf("handle: 0x%04x, char properties: 0x%02x, char value "
  219. "handle: 0x%04x, uuid: %s\n", chars->handle,
  220. chars->properties, chars->value_handle,
  221. chars->uuid);
  222. }
  223. }
  224. static void char_desc_cb(uint8_t status, GSList *descriptors, void *user_data)
  225. {
  226. GSList *l;
  227. if (status) {
  228. error("Discover descriptors failed: %s\n",
  229. att_ecode2str(status));
  230. return;
  231. }
  232. for (l = descriptors; l; l = l->next) {
  233. struct gatt_desc *desc = l->data;
  234. rl_printf("handle: 0x%04x, uuid: %s\n", desc->handle,
  235. desc->uuid);
  236. }
  237. }
  238. static void char_read_cb(guint8 status, const guint8 *pdu, guint16 plen,
  239. gpointer user_data)
  240. {
  241. uint8_t value[plen];
  242. ssize_t vlen;
  243. int i;
  244. GString *s;
  245. if (status != 0) {
  246. error("Characteristic value/descriptor read failed: %s\n",
  247. att_ecode2str(status));
  248. return;
  249. }
  250. vlen = dec_read_resp(pdu, plen, value, sizeof(value));
  251. if (vlen < 0) {
  252. error("Protocol error\n");
  253. return;
  254. }
  255. s = g_string_new("Characteristic value/descriptor: ");
  256. for (i = 0; i < vlen; i++)
  257. g_string_append_printf(s, "%02x ", value[i]);
  258. rl_printf("%s\n", s->str);
  259. g_string_free(s, TRUE);
  260. }
  261. static void char_read_by_uuid_cb(guint8 status, const guint8 *pdu,
  262. guint16 plen, gpointer user_data)
  263. {
  264. struct att_data_list *list;
  265. int i;
  266. GString *s;
  267. if (status != 0) {
  268. error("Read characteristics by UUID failed: %s\n",
  269. att_ecode2str(status));
  270. return;
  271. }
  272. list = dec_read_by_type_resp(pdu, plen);
  273. if (list == NULL)
  274. return;
  275. s = g_string_new(NULL);
  276. for (i = 0; i < list->num; i++) {
  277. uint8_t *value = list->data[i];
  278. int j;
  279. g_string_printf(s, "handle: 0x%04x \t value: ",
  280. get_le16(value));
  281. value += 2;
  282. for (j = 0; j < list->len - 2; j++, value++)
  283. g_string_append_printf(s, "%02x ", *value);
  284. rl_printf("%s\n", s->str);
  285. }
  286. att_data_list_free(list);
  287. g_string_free(s, TRUE);
  288. }
  289. static void cmd_exit(int argcp, char **argvp)
  290. {
  291. rl_callback_handler_remove();
  292. g_main_loop_quit(event_loop);
  293. }
  294. static gboolean channel_watcher(GIOChannel *chan, GIOCondition cond,
  295. gpointer user_data)
  296. {
  297. disconnect_io();
  298. return FALSE;
  299. }
  300. static void cmd_connect(int argcp, char **argvp)
  301. {
  302. GError *gerr = NULL;
  303. if (conn_state != STATE_DISCONNECTED)
  304. return;
  305. if (argcp > 1) {
  306. g_free(opt_dst);
  307. opt_dst = g_strdup(argvp[1]);
  308. g_free(opt_dst_type);
  309. if (argcp > 2)
  310. opt_dst_type = g_strdup(argvp[2]);
  311. else
  312. opt_dst_type = g_strdup("public");
  313. }
  314. if (opt_dst == NULL) {
  315. error("Remote Bluetooth address required\n");
  316. return;
  317. }
  318. rl_printf("Attempting to connect to %s\n", opt_dst);
  319. set_state(STATE_CONNECTING);
  320. iochannel = gatt_connect(opt_src, opt_dst, opt_dst_type, opt_sec_level,
  321. opt_psm, opt_mtu, connect_cb, &gerr);
  322. if (iochannel == NULL) {
  323. set_state(STATE_DISCONNECTED);
  324. error("%s\n", gerr->message);
  325. g_error_free(gerr);
  326. } else
  327. g_io_add_watch(iochannel, G_IO_HUP, channel_watcher, NULL);
  328. }
  329. static void cmd_disconnect(int argcp, char **argvp)
  330. {
  331. disconnect_io();
  332. }
  333. static void cmd_primary(int argcp, char **argvp)
  334. {
  335. bt_uuid_t uuid;
  336. if (conn_state != STATE_CONNECTED) {
  337. failed("Disconnected\n");
  338. return;
  339. }
  340. if (argcp == 1) {
  341. gatt_discover_primary(attrib, NULL, primary_all_cb, NULL);
  342. return;
  343. }
  344. if (bt_string_to_uuid(&uuid, argvp[1]) < 0) {
  345. error("Invalid UUID\n");
  346. return;
  347. }
  348. gatt_discover_primary(attrib, &uuid, primary_by_uuid_cb, NULL);
  349. }
  350. static int strtohandle(const char *src)
  351. {
  352. char *e;
  353. int dst;
  354. errno = 0;
  355. dst = strtoll(src, &e, 16);
  356. if (errno != 0 || *e != '\0')
  357. return -EINVAL;
  358. return dst;
  359. }
  360. static void cmd_included(int argcp, char **argvp)
  361. {
  362. int start = 0x0001;
  363. int end = 0xffff;
  364. if (conn_state != STATE_CONNECTED) {
  365. failed("Disconnected\n");
  366. return;
  367. }
  368. if (argcp > 1) {
  369. start = strtohandle(argvp[1]);
  370. if (start < 0) {
  371. error("Invalid start handle: %s\n", argvp[1]);
  372. return;
  373. }
  374. end = start;
  375. }
  376. if (argcp > 2) {
  377. end = strtohandle(argvp[2]);
  378. if (end < 0) {
  379. error("Invalid end handle: %s\n", argvp[2]);
  380. return;
  381. }
  382. }
  383. gatt_find_included(attrib, start, end, included_cb, NULL);
  384. }
  385. static void cmd_char(int argcp, char **argvp)
  386. {
  387. int start = 0x0001;
  388. int end = 0xffff;
  389. if (conn_state != STATE_CONNECTED) {
  390. failed("Disconnected\n");
  391. return;
  392. }
  393. if (argcp > 1) {
  394. start = strtohandle(argvp[1]);
  395. if (start < 0) {
  396. error("Invalid start handle: %s\n", argvp[1]);
  397. return;
  398. }
  399. }
  400. if (argcp > 2) {
  401. end = strtohandle(argvp[2]);
  402. if (end < 0) {
  403. error("Invalid end handle: %s\n", argvp[2]);
  404. return;
  405. }
  406. }
  407. if (argcp > 3) {
  408. bt_uuid_t uuid;
  409. if (bt_string_to_uuid(&uuid, argvp[3]) < 0) {
  410. error("Invalid UUID\n");
  411. return;
  412. }
  413. gatt_discover_char(attrib, start, end, &uuid, char_cb, NULL);
  414. return;
  415. }
  416. gatt_discover_char(attrib, start, end, NULL, char_cb, NULL);
  417. }
  418. static void cmd_char_desc(int argcp, char **argvp)
  419. {
  420. if (conn_state != STATE_CONNECTED) {
  421. failed("Disconnected\n");
  422. return;
  423. }
  424. if (argcp > 1) {
  425. start = strtohandle(argvp[1]);
  426. if (start < 0) {
  427. error("Invalid start handle: %s\n", argvp[1]);
  428. return;
  429. }
  430. } else
  431. start = 0x0001;
  432. if (argcp > 2) {
  433. end = strtohandle(argvp[2]);
  434. if (end < 0) {
  435. error("Invalid end handle: %s\n", argvp[2]);
  436. return;
  437. }
  438. } else
  439. end = 0xffff;
  440. gatt_discover_desc(attrib, start, end, NULL, char_desc_cb, NULL);
  441. }
  442. static void cmd_read_hnd(int argcp, char **argvp)
  443. {
  444. int handle;
  445. if (conn_state != STATE_CONNECTED) {
  446. failed("Disconnected\n");
  447. return;
  448. }
  449. if (argcp < 2) {
  450. error("Missing argument: handle\n");
  451. return;
  452. }
  453. handle = strtohandle(argvp[1]);
  454. if (handle < 0) {
  455. error("Invalid handle: %s\n", argvp[1]);
  456. return;
  457. }
  458. gatt_read_char(attrib, handle, char_read_cb, attrib);
  459. }
  460. static void cmd_read_uuid(int argcp, char **argvp)
  461. {
  462. int start = 0x0001;
  463. int end = 0xffff;
  464. bt_uuid_t uuid;
  465. if (conn_state != STATE_CONNECTED) {
  466. failed("Disconnected\n");
  467. return;
  468. }
  469. if (argcp < 2) {
  470. error("Missing argument: UUID\n");
  471. return;
  472. }
  473. if (bt_string_to_uuid(&uuid, argvp[1]) < 0) {
  474. error("Invalid UUID\n");
  475. return;
  476. }
  477. if (argcp > 2) {
  478. start = strtohandle(argvp[2]);
  479. if (start < 0) {
  480. error("Invalid start handle: %s\n", argvp[1]);
  481. return;
  482. }
  483. }
  484. if (argcp > 3) {
  485. end = strtohandle(argvp[3]);
  486. if (end < 0) {
  487. error("Invalid end handle: %s\n", argvp[2]);
  488. return;
  489. }
  490. }
  491. gatt_read_char_by_uuid(attrib, start, end, &uuid, char_read_by_uuid_cb,
  492. NULL);
  493. }
  494. static void char_write_req_cb(guint8 status, const guint8 *pdu, guint16 plen,
  495. gpointer user_data)
  496. {
  497. if (status != 0) {
  498. error("Characteristic Write Request failed: "
  499. "%s\n", att_ecode2str(status));
  500. return;
  501. }
  502. if (!dec_write_resp(pdu, plen) && !dec_exec_write_resp(pdu, plen)) {
  503. error("Protocol error\n");
  504. return;
  505. }
  506. rl_printf("Characteristic value was written successfully\n");
  507. }
  508. static void cmd_char_write(int argcp, char **argvp)
  509. {
  510. uint8_t *value;
  511. size_t plen;
  512. int handle;
  513. if (conn_state != STATE_CONNECTED) {
  514. failed("Disconnected\n");
  515. return;
  516. }
  517. if (argcp < 3) {
  518. rl_printf("Usage: %s <handle> <new value>\n", argvp[0]);
  519. return;
  520. }
  521. handle = strtohandle(argvp[1]);
  522. if (handle <= 0) {
  523. error("A valid handle is required\n");
  524. return;
  525. }
  526. plen = gatt_attr_data_from_string(argvp[2], &value);
  527. if (plen == 0) {
  528. error("Invalid value\n");
  529. return;
  530. }
  531. if (g_strcmp0("char-write-req", argvp[0]) == 0)
  532. gatt_write_char(attrib, handle, value, plen,
  533. char_write_req_cb, NULL);
  534. else
  535. gatt_write_cmd(attrib, handle, value, plen, NULL, NULL);
  536. g_free(value);
  537. }
  538. static void cmd_sec_level(int argcp, char **argvp)
  539. {
  540. GError *gerr = NULL;
  541. BtIOSecLevel sec_level;
  542. if (argcp < 2) {
  543. rl_printf("sec-level: %s\n", opt_sec_level);
  544. return;
  545. }
  546. if (strcasecmp(argvp[1], "medium") == 0)
  547. sec_level = BT_IO_SEC_MEDIUM;
  548. else if (strcasecmp(argvp[1], "high") == 0)
  549. sec_level = BT_IO_SEC_HIGH;
  550. else if (strcasecmp(argvp[1], "low") == 0)
  551. sec_level = BT_IO_SEC_LOW;
  552. else {
  553. rl_printf("Allowed values: low | medium | high\n");
  554. return;
  555. }
  556. g_free(opt_sec_level);
  557. opt_sec_level = g_strdup(argvp[1]);
  558. if (conn_state != STATE_CONNECTED)
  559. return;
  560. if (opt_psm) {
  561. rl_printf("Change will take effect on reconnection\n");
  562. return;
  563. }
  564. bt_io_set(iochannel, &gerr,
  565. BT_IO_OPT_SEC_LEVEL, sec_level,
  566. BT_IO_OPT_INVALID);
  567. if (gerr) {
  568. error("%s\n", gerr->message);
  569. g_error_free(gerr);
  570. }
  571. }
  572. static void exchange_mtu_cb(guint8 status, const guint8 *pdu, guint16 plen,
  573. gpointer user_data)
  574. {
  575. uint16_t mtu;
  576. if (status != 0) {
  577. error("Exchange MTU Request failed: %s\n",
  578. att_ecode2str(status));
  579. return;
  580. }
  581. if (!dec_mtu_resp(pdu, plen, &mtu)) {
  582. error("Protocol error\n");
  583. return;
  584. }
  585. mtu = MIN(mtu, opt_mtu);
  586. /* Set new value for MTU in client */
  587. if (g_attrib_set_mtu(attrib, mtu))
  588. rl_printf("MTU was exchanged successfully: %d\n", mtu);
  589. else
  590. error("Error exchanging MTU\n");
  591. }
  592. static void cmd_mtu(int argcp, char **argvp)
  593. {
  594. if (conn_state != STATE_CONNECTED) {
  595. failed("Disconnected\n");
  596. return;
  597. }
  598. if (opt_psm) {
  599. failed("Operation is only available for LE transport.\n");
  600. return;
  601. }
  602. if (argcp < 2) {
  603. rl_printf("Usage: mtu <value>\n");
  604. return;
  605. }
  606. if (opt_mtu) {
  607. failed("MTU exchange can only occur once per connection.\n");
  608. return;
  609. }
  610. errno = 0;
  611. opt_mtu = strtoll(argvp[1], NULL, 0);
  612. if (errno != 0 || opt_mtu < ATT_DEFAULT_LE_MTU) {
  613. error("Invalid value. Minimum MTU size is %d\n",
  614. ATT_DEFAULT_LE_MTU);
  615. return;
  616. }
  617. gatt_exchange_mtu(attrib, opt_mtu, exchange_mtu_cb, NULL);
  618. }
  619. static struct {
  620. const char *cmd;
  621. void (*func)(int argcp, char **argvp);
  622. const char *params;
  623. const char *desc;
  624. } commands[] = {
  625. { "help", cmd_help, "",
  626. "Show this help"},
  627. { "exit", cmd_exit, "",
  628. "Exit interactive mode" },
  629. { "quit", cmd_exit, "",
  630. "Exit interactive mode" },
  631. { "connect", cmd_connect, "[address [address type]]",
  632. "Connect to a remote device" },
  633. { "disconnect", cmd_disconnect, "",
  634. "Disconnect from a remote device" },
  635. { "primary", cmd_primary, "[UUID]",
  636. "Primary Service Discovery" },
  637. { "included", cmd_included, "[start hnd [end hnd]]",
  638. "Find Included Services" },
  639. { "characteristics", cmd_char, "[start hnd [end hnd [UUID]]]",
  640. "Characteristics Discovery" },
  641. { "char-desc", cmd_char_desc, "[start hnd] [end hnd]",
  642. "Characteristics Descriptor Discovery" },
  643. { "char-read-hnd", cmd_read_hnd, "<handle>",
  644. "Characteristics Value/Descriptor Read by handle" },
  645. { "char-read-uuid", cmd_read_uuid, "<UUID> [start hnd] [end hnd]",
  646. "Characteristics Value/Descriptor Read by UUID" },
  647. { "char-write-req", cmd_char_write, "<handle> <new value>",
  648. "Characteristic Value Write (Write Request)" },
  649. { "char-write-cmd", cmd_char_write, "<handle> <new value>",
  650. "Characteristic Value Write (No response)" },
  651. { "sec-level", cmd_sec_level, "[low | medium | high]",
  652. "Set security level. Default: low" },
  653. { "mtu", cmd_mtu, "<value>",
  654. "Exchange MTU for GATT/ATT" },
  655. { NULL, NULL, NULL}
  656. };
  657. static void cmd_help(int argcp, char **argvp)
  658. {
  659. int i;
  660. for (i = 0; commands[i].cmd; i++)
  661. rl_printf("%-15s %-30s %s\n", commands[i].cmd,
  662. commands[i].params, commands[i].desc);
  663. }
  664. static void parse_line(char *line_read)
  665. {
  666. char **argvp;
  667. int argcp;
  668. int i;
  669. if (line_read == NULL) {
  670. rl_printf("\n");
  671. cmd_exit(0, NULL);
  672. return;
  673. }
  674. line_read = g_strstrip(line_read);
  675. if (*line_read == '\0')
  676. goto done;
  677. add_history(line_read);
  678. if (g_shell_parse_argv(line_read, &argcp, &argvp, NULL) == FALSE)
  679. goto done;
  680. for (i = 0; commands[i].cmd; i++)
  681. if (strcasecmp(commands[i].cmd, argvp[0]) == 0)
  682. break;
  683. if (commands[i].cmd)
  684. commands[i].func(argcp, argvp);
  685. else
  686. error("%s: command not found\n", argvp[0]);
  687. g_strfreev(argvp);
  688. done:
  689. free(line_read);
  690. }
  691. static gboolean prompt_read(GIOChannel *chan, GIOCondition cond,
  692. gpointer user_data)
  693. {
  694. if (cond & (G_IO_HUP | G_IO_ERR | G_IO_NVAL)) {
  695. g_io_channel_unref(chan);
  696. return FALSE;
  697. }
  698. rl_callback_read_char();
  699. return TRUE;
  700. }
  701. static char *completion_generator(const char *text, int state)
  702. {
  703. static int index = 0, len = 0;
  704. const char *cmd = NULL;
  705. if (state == 0) {
  706. index = 0;
  707. len = strlen(text);
  708. }
  709. while ((cmd = commands[index].cmd) != NULL) {
  710. index++;
  711. if (strncmp(cmd, text, len) == 0)
  712. return strdup(cmd);
  713. }
  714. return NULL;
  715. }
  716. static char **commands_completion(const char *text, int start, int end)
  717. {
  718. if (start == 0)
  719. return rl_completion_matches(text, &completion_generator);
  720. else
  721. return NULL;
  722. }
  723. static guint setup_standard_input(void)
  724. {
  725. GIOChannel *channel;
  726. guint source;
  727. channel = g_io_channel_unix_new(fileno(stdin));
  728. source = g_io_add_watch(channel,
  729. G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
  730. prompt_read, NULL);
  731. g_io_channel_unref(channel);
  732. return source;
  733. }
  734. static gboolean signal_handler(GIOChannel *channel, GIOCondition condition,
  735. gpointer user_data)
  736. {
  737. static unsigned int __terminated = 0;
  738. struct signalfd_siginfo si;
  739. ssize_t result;
  740. int fd;
  741. if (condition & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
  742. g_main_loop_quit(event_loop);
  743. return FALSE;
  744. }
  745. fd = g_io_channel_unix_get_fd(channel);
  746. result = read(fd, &si, sizeof(si));
  747. if (result != sizeof(si))
  748. return FALSE;
  749. switch (si.ssi_signo) {
  750. case SIGINT:
  751. rl_replace_line("", 0);
  752. rl_crlf();
  753. rl_on_new_line();
  754. rl_redisplay();
  755. break;
  756. case SIGTERM:
  757. if (__terminated == 0) {
  758. rl_replace_line("", 0);
  759. rl_crlf();
  760. g_main_loop_quit(event_loop);
  761. }
  762. __terminated = 1;
  763. break;
  764. }
  765. return TRUE;
  766. }
  767. static guint setup_signalfd(void)
  768. {
  769. GIOChannel *channel;
  770. guint source;
  771. sigset_t mask;
  772. int fd;
  773. sigemptyset(&mask);
  774. sigaddset(&mask, SIGINT);
  775. sigaddset(&mask, SIGTERM);
  776. if (sigprocmask(SIG_BLOCK, &mask, NULL) < 0) {
  777. perror("Failed to set signal mask");
  778. return 0;
  779. }
  780. fd = signalfd(-1, &mask, 0);
  781. if (fd < 0) {
  782. perror("Failed to create signal descriptor");
  783. return 0;
  784. }
  785. channel = g_io_channel_unix_new(fd);
  786. g_io_channel_set_close_on_unref(channel, TRUE);
  787. g_io_channel_set_encoding(channel, NULL, NULL);
  788. g_io_channel_set_buffered(channel, FALSE);
  789. source = g_io_add_watch(channel,
  790. G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
  791. signal_handler, NULL);
  792. g_io_channel_unref(channel);
  793. return source;
  794. }
  795. int interactive(const char *src, const char *dst,
  796. const char *dst_type, int psm)
  797. {
  798. guint input;
  799. guint signal;
  800. opt_sec_level = g_strdup("low");
  801. opt_src = g_strdup(src);
  802. opt_dst = g_strdup(dst);
  803. opt_dst_type = g_strdup(dst_type);
  804. opt_psm = psm;
  805. prompt = g_string_new(NULL);
  806. event_loop = g_main_loop_new(NULL, FALSE);
  807. input = setup_standard_input();
  808. signal = setup_signalfd();
  809. rl_attempted_completion_function = commands_completion;
  810. rl_erase_empty_line = 1;
  811. rl_callback_handler_install(get_prompt(), parse_line);
  812. g_main_loop_run(event_loop);
  813. rl_callback_handler_remove();
  814. cmd_disconnect(0, NULL);
  815. g_source_remove(input);
  816. g_source_remove(signal);
  817. g_main_loop_unref(event_loop);
  818. g_string_free(prompt, TRUE);
  819. g_free(opt_src);
  820. g_free(opt_dst);
  821. g_free(opt_sec_level);
  822. return 0;
  823. }