meshctl.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021
  1. // SPDX-License-Identifier: LGPL-2.1-or-later
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2017 Intel Corporation. All rights reserved.
  7. *
  8. *
  9. */
  10. #ifdef HAVE_CONFIG_H
  11. #include <config.h>
  12. #endif
  13. #define _GNU_SOURCE
  14. #include <stdio.h>
  15. #include <errno.h>
  16. #include <unistd.h>
  17. #include <stdlib.h>
  18. #include <stdbool.h>
  19. #include <wordexp.h>
  20. #include <inttypes.h>
  21. #include <ctype.h>
  22. #include <sys/file.h>
  23. #include <sys/ioctl.h>
  24. #include <sys/stat.h>
  25. #include "bluetooth/bluetooth.h"
  26. #include <glib.h>
  27. #include "lib/bluetooth.h"
  28. #include "lib/uuid.h"
  29. #include "src/shared/shell.h"
  30. #include "src/shared/util.h"
  31. #include "gdbus/gdbus.h"
  32. #include "mesh/agent.h"
  33. #include "mesh/config-model.h"
  34. #include "mesh-gatt/mesh-net.h"
  35. #include "mesh-gatt/gatt.h"
  36. #include "mesh-gatt/crypto.h"
  37. #include "mesh-gatt/node.h"
  38. #include "mesh-gatt/net.h"
  39. #include "mesh-gatt/keys.h"
  40. #include "mesh-gatt/prov.h"
  41. #include "mesh-gatt/util.h"
  42. #include "mesh-gatt/prov-db.h"
  43. #include "mesh-gatt/onoff-model.h"
  44. /* String display constants */
  45. #define COLORED_NEW COLOR_GREEN "NEW" COLOR_OFF
  46. #define COLORED_CHG COLOR_YELLOW "CHG" COLOR_OFF
  47. #define COLORED_DEL COLOR_RED "DEL" COLOR_OFF
  48. #define PROMPT_ON COLOR_BLUE "[meshctl]" COLOR_OFF "# "
  49. #define PROMPT_OFF "Waiting to connect to bluetoothd..."
  50. #define MESH_PROV_DATA_IN_UUID_STR "00002adb-0000-1000-8000-00805f9b34fb"
  51. #define MESH_PROV_DATA_OUT_UUID_STR "00002adc-0000-1000-8000-00805f9b34fb"
  52. #define MESH_PROXY_DATA_IN_UUID_STR "00002add-0000-1000-8000-00805f9b34fb"
  53. #define MESH_PROXY_DATA_OUT_UUID_STR "00002ade-0000-1000-8000-00805f9b34fb"
  54. static DBusConnection *dbus_conn;
  55. struct adapter {
  56. GDBusProxy *proxy;
  57. GList *mesh_devices;
  58. };
  59. struct mesh_device {
  60. GDBusProxy *proxy;
  61. uint8_t dev_uuid[16];
  62. gboolean hide;
  63. };
  64. GList *service_list;
  65. GList *char_list;
  66. static GList *ctrl_list;
  67. static struct adapter *default_ctrl;
  68. static char *mesh_prov_db_filename;
  69. static char *mesh_local_config_filename;
  70. static bool discovering = false;
  71. static bool discover_mesh;
  72. static uint16_t prov_net_key_index = NET_IDX_PRIMARY;
  73. static const struct bt_shell_menu main_menu;
  74. #define CONN_TYPE_NETWORK 0x00
  75. #define CONN_TYPE_IDENTITY 0x01
  76. #define CONN_TYPE_PROVISION 0x02
  77. #define CONN_TYPE_INVALID 0xff
  78. #define NET_IDX_INVALID 0xffff
  79. struct {
  80. GDBusProxy *device;
  81. GDBusProxy *service;
  82. GDBusProxy *data_in;
  83. GDBusProxy *data_out;
  84. bool session_open;
  85. uint16_t unicast;
  86. uint16_t net_idx;
  87. uint8_t dev_uuid[16];
  88. uint8_t type;
  89. } connection;
  90. static bool service_is_mesh(GDBusProxy *proxy, const char *target_uuid)
  91. {
  92. DBusMessageIter iter;
  93. const char *uuid;
  94. if (g_dbus_proxy_get_property(proxy, "UUID", &iter) == FALSE)
  95. return false;
  96. dbus_message_iter_get_basic(&iter, &uuid);
  97. if (target_uuid)
  98. return (!bt_uuid_strcmp(uuid, target_uuid));
  99. else if (bt_uuid_strcmp(uuid, MESH_PROV_SVC_UUID) ||
  100. bt_uuid_strcmp(uuid, MESH_PROXY_SVC_UUID))
  101. return true;
  102. else
  103. return false;
  104. }
  105. static bool char_is_mesh(GDBusProxy *proxy, const char *target_uuid)
  106. {
  107. DBusMessageIter iter;
  108. const char *uuid;
  109. if (g_dbus_proxy_get_property(proxy, "UUID", &iter) == FALSE)
  110. return false;
  111. dbus_message_iter_get_basic(&iter, &uuid);
  112. if (target_uuid)
  113. return (!bt_uuid_strcmp(uuid, target_uuid));
  114. if (!bt_uuid_strcmp(uuid, MESH_PROV_DATA_IN_UUID_STR))
  115. return true;
  116. if (!bt_uuid_strcmp(uuid, MESH_PROV_DATA_OUT_UUID_STR))
  117. return true;
  118. if (!bt_uuid_strcmp(uuid, MESH_PROXY_DATA_IN_UUID_STR))
  119. return true;
  120. if (!bt_uuid_strcmp(uuid, MESH_PROXY_DATA_OUT_UUID_STR))
  121. return true;
  122. return false;
  123. }
  124. static gboolean check_default_ctrl(void)
  125. {
  126. if (!default_ctrl) {
  127. bt_shell_printf("No default controller available\n");
  128. return FALSE;
  129. }
  130. return TRUE;
  131. }
  132. static void proxy_leak(gpointer data)
  133. {
  134. bt_shell_printf("Leaking proxy %p\n", data);
  135. }
  136. static void connect_handler(DBusConnection *connection, void *user_data)
  137. {
  138. bt_shell_set_prompt(PROMPT_ON);
  139. }
  140. static void disconnect_handler(DBusConnection *connection, void *user_data)
  141. {
  142. bt_shell_detach();
  143. bt_shell_set_prompt(PROMPT_OFF);
  144. g_list_free_full(ctrl_list, proxy_leak);
  145. ctrl_list = NULL;
  146. default_ctrl = NULL;
  147. }
  148. static void print_adapter(GDBusProxy *proxy, const char *description)
  149. {
  150. DBusMessageIter iter;
  151. const char *address, *name;
  152. if (g_dbus_proxy_get_property(proxy, "Address", &iter) == FALSE)
  153. return;
  154. dbus_message_iter_get_basic(&iter, &address);
  155. if (g_dbus_proxy_get_property(proxy, "Alias", &iter) == TRUE)
  156. dbus_message_iter_get_basic(&iter, &name);
  157. else
  158. name = "<unknown>";
  159. bt_shell_printf("%s%s%sController %s %s %s\n",
  160. description ? "[" : "",
  161. description ? : "",
  162. description ? "] " : "",
  163. address, name,
  164. default_ctrl &&
  165. default_ctrl->proxy == proxy ?
  166. "[default]" : "");
  167. }
  168. static void print_device(GDBusProxy *proxy, const char *description)
  169. {
  170. DBusMessageIter iter;
  171. const char *address, *name;
  172. if (g_dbus_proxy_get_property(proxy, "Address", &iter) == FALSE)
  173. return;
  174. dbus_message_iter_get_basic(&iter, &address);
  175. if (g_dbus_proxy_get_property(proxy, "Alias", &iter) == TRUE)
  176. dbus_message_iter_get_basic(&iter, &name);
  177. else
  178. name = "<unknown>";
  179. bt_shell_printf("%s%s%sDevice %s %s\n",
  180. description ? "[" : "",
  181. description ? : "",
  182. description ? "] " : "",
  183. address, name);
  184. }
  185. static void print_iter(const char *label, const char *name,
  186. DBusMessageIter *iter)
  187. {
  188. dbus_bool_t valbool;
  189. dbus_uint32_t valu32;
  190. dbus_uint16_t valu16;
  191. dbus_int16_t vals16;
  192. unsigned char byte;
  193. const char *valstr;
  194. DBusMessageIter subiter;
  195. char *entry;
  196. if (iter == NULL) {
  197. bt_shell_printf("%s%s is nil\n", label, name);
  198. return;
  199. }
  200. switch (dbus_message_iter_get_arg_type(iter)) {
  201. case DBUS_TYPE_INVALID:
  202. bt_shell_printf("%s%s is invalid\n", label, name);
  203. break;
  204. case DBUS_TYPE_STRING:
  205. case DBUS_TYPE_OBJECT_PATH:
  206. dbus_message_iter_get_basic(iter, &valstr);
  207. bt_shell_printf("%s%s: %s\n", label, name, valstr);
  208. break;
  209. case DBUS_TYPE_BOOLEAN:
  210. dbus_message_iter_get_basic(iter, &valbool);
  211. bt_shell_printf("%s%s: %s\n", label, name,
  212. valbool == TRUE ? "yes" : "no");
  213. break;
  214. case DBUS_TYPE_UINT32:
  215. dbus_message_iter_get_basic(iter, &valu32);
  216. bt_shell_printf("%s%s: 0x%06x\n", label, name, valu32);
  217. break;
  218. case DBUS_TYPE_UINT16:
  219. dbus_message_iter_get_basic(iter, &valu16);
  220. bt_shell_printf("%s%s: 0x%04x\n", label, name, valu16);
  221. break;
  222. case DBUS_TYPE_INT16:
  223. dbus_message_iter_get_basic(iter, &vals16);
  224. bt_shell_printf("%s%s: %d\n", label, name, vals16);
  225. break;
  226. case DBUS_TYPE_BYTE:
  227. dbus_message_iter_get_basic(iter, &byte);
  228. bt_shell_printf("%s%s: 0x%02x\n", label, name, byte);
  229. break;
  230. case DBUS_TYPE_VARIANT:
  231. dbus_message_iter_recurse(iter, &subiter);
  232. print_iter(label, name, &subiter);
  233. break;
  234. case DBUS_TYPE_ARRAY:
  235. dbus_message_iter_recurse(iter, &subiter);
  236. while (dbus_message_iter_get_arg_type(&subiter) !=
  237. DBUS_TYPE_INVALID) {
  238. print_iter(label, name, &subiter);
  239. dbus_message_iter_next(&subiter);
  240. }
  241. break;
  242. case DBUS_TYPE_DICT_ENTRY:
  243. dbus_message_iter_recurse(iter, &subiter);
  244. entry = g_strconcat(name, "Key", NULL);
  245. print_iter(label, entry, &subiter);
  246. g_free(entry);
  247. entry = g_strconcat(name, " Value", NULL);
  248. dbus_message_iter_next(&subiter);
  249. print_iter(label, entry, &subiter);
  250. g_free(entry);
  251. break;
  252. default:
  253. bt_shell_printf("%s%s has unsupported type\n", label, name);
  254. break;
  255. }
  256. }
  257. static void print_property(GDBusProxy *proxy, const char *name)
  258. {
  259. DBusMessageIter iter;
  260. if (g_dbus_proxy_get_property(proxy, name, &iter) == FALSE)
  261. return;
  262. print_iter("\t", name, &iter);
  263. }
  264. static void forget_mesh_devices()
  265. {
  266. g_list_free_full(default_ctrl->mesh_devices, g_free);
  267. default_ctrl->mesh_devices = NULL;
  268. }
  269. static struct mesh_device *find_device_by_uuid(GList *source, uint8_t uuid[16])
  270. {
  271. GList *list;
  272. for (list = g_list_first(source); list; list = g_list_next(list)) {
  273. struct mesh_device *dev = list->data;
  274. if (!memcmp(dev->dev_uuid, uuid, 16))
  275. return dev;
  276. }
  277. return NULL;
  278. }
  279. static void print_prov_service(struct prov_svc_data *prov_data)
  280. {
  281. const char *prefix = "\t\t";
  282. char txt_uuid[16 * 2 + 1];
  283. int i;
  284. bt_shell_printf("%sMesh Provisioning Service (%s)\n", prefix,
  285. MESH_PROV_SVC_UUID);
  286. for (i = 0; i < 16; ++i) {
  287. sprintf(txt_uuid + (i * 2), "%2.2x", prov_data->dev_uuid[i]);
  288. }
  289. bt_shell_printf("%s\tDevice UUID: %s\n", prefix, txt_uuid);
  290. bt_shell_printf("%s\tOOB: %4.4x\n", prefix, prov_data->oob);
  291. }
  292. static bool parse_prov_service_data(const char *uuid, uint8_t *data, int len,
  293. void *data_out)
  294. {
  295. struct prov_svc_data *prov_data = data_out;
  296. int i;
  297. if (len < 18)
  298. return false;
  299. for (i = 0; i < 16; ++i) {
  300. prov_data->dev_uuid[i] = data[i];
  301. }
  302. prov_data->oob = get_be16(&data[16]);
  303. return true;
  304. }
  305. static bool parse_mesh_service_data(const char *uuid, uint8_t *data, int len,
  306. void *data_out)
  307. {
  308. const char *prefix = "\t\t";
  309. if (!(len == 9 && data[0] == 0x00) && !(len == 17 && data[0] == 0x01)) {
  310. bt_shell_printf("Unexpected mesh proxy service data length %d\n",
  311. len);
  312. return false;
  313. }
  314. if (data[0] != connection.type)
  315. return false;
  316. if (data[0] == CONN_TYPE_IDENTITY) {
  317. uint8_t *key;
  318. if (IS_UNASSIGNED(connection.unicast)) {
  319. /* This would be a bug */
  320. bt_shell_printf("Error: Searching identity with "
  321. "unicast 0000\n");
  322. return false;
  323. }
  324. key = keys_net_key_get(prov_net_key_index, true);
  325. if (!key)
  326. return false;
  327. if (!mesh_crypto_identity_check(key, connection.unicast,
  328. &data[1]))
  329. return false;
  330. if (discovering) {
  331. bt_shell_printf("\n%sMesh Proxy Service (%s)\n", prefix,
  332. uuid);
  333. bt_shell_printf("%sIdentity for node %4.4x\n", prefix,
  334. connection.unicast);
  335. }
  336. } else if (data[0] == CONN_TYPE_NETWORK) {
  337. uint16_t net_idx = net_validate_proxy_beacon(data + 1);
  338. if (net_idx == NET_IDX_INVALID || net_idx != connection.net_idx)
  339. return false;
  340. if (discovering) {
  341. bt_shell_printf("\n%sMesh Proxy Service (%s)\n", prefix,
  342. uuid);
  343. bt_shell_printf("%sNetwork Beacon for net index %4.4x\n",
  344. prefix, net_idx);
  345. }
  346. }
  347. return true;
  348. }
  349. static bool parse_service_data(GDBusProxy *proxy, const char *target_uuid,
  350. void *data_out)
  351. {
  352. DBusMessageIter iter, entries;
  353. bool mesh_prov = false;
  354. bool mesh_proxy = false;
  355. if (target_uuid) {
  356. mesh_prov = !strcmp(target_uuid, MESH_PROV_SVC_UUID);
  357. mesh_proxy = !strcmp(target_uuid, MESH_PROXY_SVC_UUID);
  358. }
  359. if (!g_dbus_proxy_get_property(proxy, "ServiceData", &iter))
  360. return false;
  361. if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY)
  362. return false;
  363. dbus_message_iter_recurse(&iter, &entries);
  364. while (dbus_message_iter_get_arg_type(&entries)
  365. == DBUS_TYPE_DICT_ENTRY) {
  366. DBusMessageIter value, entry, array;
  367. const char *uuid_str;
  368. bt_uuid_t uuid;
  369. uint8_t *service_data;
  370. int len;
  371. dbus_message_iter_recurse(&entries, &entry);
  372. dbus_message_iter_get_basic(&entry, &uuid_str);
  373. if (bt_string_to_uuid(&uuid, uuid_str) < 0)
  374. goto fail;
  375. dbus_message_iter_next(&entry);
  376. if (dbus_message_iter_get_arg_type(&entry) != DBUS_TYPE_VARIANT)
  377. goto fail;
  378. dbus_message_iter_recurse(&entry, &value);
  379. if (dbus_message_iter_get_arg_type(&value) != DBUS_TYPE_ARRAY)
  380. goto fail;
  381. dbus_message_iter_recurse(&value, &array);
  382. if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_BYTE)
  383. goto fail;
  384. dbus_message_iter_get_fixed_array(&array, &service_data, &len);
  385. if (mesh_prov && !strcmp(uuid_str, MESH_PROV_SVC_UUID)) {
  386. return parse_prov_service_data(uuid_str, service_data,
  387. len, data_out);
  388. } else if (mesh_proxy &&
  389. !strcmp(uuid_str, MESH_PROXY_SVC_UUID)) {
  390. return parse_mesh_service_data(uuid_str, service_data,
  391. len, data_out);
  392. }
  393. dbus_message_iter_next(&entries);
  394. }
  395. if (!target_uuid)
  396. return true;
  397. fail:
  398. return false;
  399. }
  400. static void print_uuids(GDBusProxy *proxy)
  401. {
  402. DBusMessageIter iter, value;
  403. if (g_dbus_proxy_get_property(proxy, "UUIDs", &iter) == FALSE)
  404. return;
  405. dbus_message_iter_recurse(&iter, &value);
  406. while (dbus_message_iter_get_arg_type(&value) == DBUS_TYPE_STRING) {
  407. const char *uuid, *text;
  408. dbus_message_iter_get_basic(&value, &uuid);
  409. text = bt_uuidstr_to_str(uuid);
  410. if (text) {
  411. char str[26];
  412. unsigned int n;
  413. str[sizeof(str) - 1] = '\0';
  414. n = snprintf(str, sizeof(str), "%s", text);
  415. if (n > sizeof(str) - 1) {
  416. str[sizeof(str) - 2] = '.';
  417. str[sizeof(str) - 3] = '.';
  418. if (str[sizeof(str) - 4] == ' ')
  419. str[sizeof(str) - 4] = '.';
  420. n = sizeof(str) - 1;
  421. }
  422. bt_shell_printf("\tUUID: %s%*c(%s)\n",
  423. str, 26 - n, ' ', uuid);
  424. } else
  425. bt_shell_printf("\tUUID: %*c(%s)\n", 26, ' ', uuid);
  426. dbus_message_iter_next(&value);
  427. }
  428. }
  429. static gboolean device_is_child(GDBusProxy *device, GDBusProxy *parent)
  430. {
  431. DBusMessageIter iter;
  432. const char *adapter, *path;
  433. if (!parent)
  434. return FALSE;
  435. if (g_dbus_proxy_get_property(device, "Adapter", &iter) == FALSE)
  436. return FALSE;
  437. dbus_message_iter_get_basic(&iter, &adapter);
  438. path = g_dbus_proxy_get_path(parent);
  439. if (!strcmp(path, adapter))
  440. return TRUE;
  441. return FALSE;
  442. }
  443. static struct adapter *find_parent(GDBusProxy *device)
  444. {
  445. GList *list;
  446. for (list = g_list_first(ctrl_list); list; list = g_list_next(list)) {
  447. struct adapter *adapter = list->data;
  448. if (device_is_child(device, adapter->proxy) == TRUE)
  449. return adapter;
  450. }
  451. return NULL;
  452. }
  453. static void set_connected_device(GDBusProxy *proxy)
  454. {
  455. char *desc = NULL;
  456. DBusMessageIter iter;
  457. char buf[10];
  458. bool mesh;
  459. connection.device = proxy;
  460. if (proxy == NULL) {
  461. memset(&connection, 0, sizeof(connection));
  462. connection.type = CONN_TYPE_INVALID;
  463. goto done;
  464. }
  465. if (connection.type == CONN_TYPE_IDENTITY) {
  466. mesh = true;
  467. snprintf(buf, 10, "Node-%4.4x", connection.unicast);
  468. } else if (connection.type == CONN_TYPE_NETWORK) {
  469. mesh = true;
  470. snprintf(buf, 9, "Net-%4.4x", connection.net_idx);
  471. } else {
  472. mesh = false;
  473. }
  474. if (!g_dbus_proxy_get_property(proxy, "Alias", &iter) && !mesh)
  475. goto done;
  476. dbus_message_iter_get_basic(&iter, &desc);
  477. desc = g_strdup_printf(COLOR_BLUE "[%s%s%s]" COLOR_OFF "# ", desc,
  478. (desc && mesh) ? "-" : "",
  479. mesh ? buf : "");
  480. done:
  481. bt_shell_set_prompt(desc ? desc : PROMPT_ON);
  482. g_free(desc);
  483. /* If disconnected, return to main menu */
  484. if (proxy == NULL)
  485. bt_shell_set_menu(&main_menu);
  486. }
  487. static void connect_reply(DBusMessage *message, void *user_data)
  488. {
  489. GDBusProxy *proxy = user_data;
  490. DBusError error;
  491. dbus_error_init(&error);
  492. if (dbus_set_error_from_message(&error, message) == TRUE) {
  493. bt_shell_printf("Failed to connect: %s\n", error.name);
  494. dbus_error_free(&error);
  495. set_connected_device(NULL);
  496. return bt_shell_noninteractive_quit(EXIT_FAILURE);
  497. }
  498. bt_shell_printf("Connection successful\n");
  499. set_connected_device(proxy);
  500. return bt_shell_noninteractive_quit(EXIT_SUCCESS);
  501. }
  502. static void update_device_info(GDBusProxy *proxy)
  503. {
  504. struct adapter *adapter = find_parent(proxy);
  505. DBusMessageIter iter;
  506. struct prov_svc_data prov_data;
  507. if (!adapter) {
  508. /* TODO: Error */
  509. return;
  510. }
  511. if (adapter != default_ctrl)
  512. return;
  513. if (!g_dbus_proxy_get_property(proxy, "Address", &iter))
  514. return;
  515. if (parse_service_data(proxy, MESH_PROV_SVC_UUID, &prov_data)) {
  516. struct mesh_device *dev;
  517. dev = find_device_by_uuid(adapter->mesh_devices,
  518. prov_data.dev_uuid);
  519. /* Display provisioning service once per discovery session */
  520. if (discovering && (!dev || !dev->hide))
  521. print_prov_service(&prov_data);
  522. if (dev) {
  523. dev->proxy = proxy;
  524. dev->hide = discovering;
  525. return;
  526. }
  527. dev = g_malloc0(sizeof(struct mesh_device));
  528. if (!dev)
  529. return;
  530. dev->proxy = proxy;
  531. dev->hide = discovering;
  532. memcpy(dev->dev_uuid, prov_data.dev_uuid, 16);
  533. adapter->mesh_devices = g_list_append(adapter->mesh_devices,
  534. dev);
  535. print_device(proxy, COLORED_NEW);
  536. node_create_new(&prov_data);
  537. } else if (parse_service_data(proxy, MESH_PROXY_SVC_UUID, NULL) &&
  538. discover_mesh) {
  539. bool res;
  540. g_dbus_proxy_method_call(default_ctrl->proxy, "StopDiscovery",
  541. NULL, NULL, NULL, NULL);
  542. discover_mesh = false;
  543. forget_mesh_devices();
  544. res = g_dbus_proxy_method_call(proxy, "Connect", NULL,
  545. connect_reply, proxy, NULL);
  546. if (!res)
  547. bt_shell_printf("Failed to connect to mesh\n");
  548. else
  549. bt_shell_printf("Trying to connect to mesh\n");
  550. }
  551. }
  552. static void adapter_added(GDBusProxy *proxy)
  553. {
  554. struct adapter *adapter = g_malloc0(sizeof(struct adapter));
  555. adapter->proxy = proxy;
  556. ctrl_list = g_list_append(ctrl_list, adapter);
  557. if (!default_ctrl)
  558. default_ctrl = adapter;
  559. print_adapter(proxy, COLORED_NEW);
  560. }
  561. static void data_out_notify(GDBusProxy *proxy, bool enable,
  562. GDBusReturnFunction cb)
  563. {
  564. struct mesh_node *node;
  565. node = node_find_by_uuid(connection.dev_uuid);
  566. if (!mesh_gatt_notify(proxy, enable, cb, node))
  567. bt_shell_printf("Failed to %s notification on %s\n", enable ?
  568. "start" : "stop", g_dbus_proxy_get_path(proxy));
  569. else
  570. bt_shell_printf("%s notification on %s\n", enable ?
  571. "Start" : "Stop", g_dbus_proxy_get_path(proxy));
  572. }
  573. struct disconnect_data {
  574. GDBusReturnFunction cb;
  575. void *data;
  576. };
  577. static void disconnect(GDBusReturnFunction cb, void *user_data)
  578. {
  579. GDBusProxy *proxy;
  580. DBusMessageIter iter;
  581. const char *addr;
  582. proxy = connection.device;
  583. if (!proxy)
  584. return;
  585. if (g_dbus_proxy_method_call(proxy, "Disconnect", NULL, cb, user_data,
  586. NULL) == FALSE) {
  587. bt_shell_printf("Failed to disconnect\n");
  588. return;
  589. }
  590. if (g_dbus_proxy_get_property(proxy, "Address", &iter) == TRUE)
  591. dbus_message_iter_get_basic(&iter, &addr);
  592. bt_shell_printf("Attempting to disconnect from %s\n", addr);
  593. }
  594. static void disc_notify_cb(DBusMessage *message, void *user_data)
  595. {
  596. struct disconnect_data *disc_data = user_data;
  597. disconnect(disc_data->cb, disc_data->data);
  598. g_free(user_data);
  599. }
  600. static void disconnect_device(GDBusReturnFunction cb, void *user_data)
  601. {
  602. DBusMessageIter iter;
  603. net_session_close(connection.data_in);
  604. /* Stop notificiation on prov_out or proxy out characteristics */
  605. if (connection.data_out) {
  606. if (g_dbus_proxy_get_property(connection.data_out, "Notifying",
  607. &iter) == TRUE) {
  608. struct disconnect_data *disc_data;
  609. disc_data = g_malloc(sizeof(struct disconnect_data));
  610. disc_data->cb = cb;
  611. disc_data->data = user_data;
  612. if (mesh_gatt_notify(connection.data_out, false,
  613. disc_notify_cb, disc_data))
  614. return;
  615. }
  616. }
  617. disconnect(cb, user_data);
  618. }
  619. static void mesh_prov_done(void *user_data, int status);
  620. static void notify_prov_out_cb(DBusMessage *message, void *user_data)
  621. {
  622. struct mesh_node *node = user_data;
  623. DBusError error;
  624. dbus_error_init(&error);
  625. if (dbus_set_error_from_message(&error, message) == TRUE) {
  626. bt_shell_printf("Failed to start notify: %s\n", error.name);
  627. dbus_error_free(&error);
  628. return;
  629. }
  630. bt_shell_printf("Notify for Mesh Provisioning Out Data started\n");
  631. if (connection.type != CONN_TYPE_PROVISION) {
  632. bt_shell_printf("Error: wrong connection type %d (expected %d)\n",
  633. connection.type, CONN_TYPE_PROVISION);
  634. return;
  635. }
  636. if (!connection.data_in) {
  637. bt_shell_printf("Error: don't have mesh provisioning data in\n");
  638. return;
  639. }
  640. if (!node) {
  641. bt_shell_printf("Error: provisioning node not present\n");
  642. return;
  643. }
  644. if(!prov_open(node, connection.data_in, prov_net_key_index,
  645. mesh_prov_done, node))
  646. {
  647. bt_shell_printf("Failed to start provisioning\n");
  648. node_free(node);
  649. disconnect_device(NULL, NULL);
  650. } else
  651. bt_shell_printf("Initiated provisioning\n");
  652. }
  653. static void session_open_cb (int status)
  654. {
  655. if (status) {
  656. bt_shell_printf("Failed to open Mesh session\n");
  657. disconnect_device(NULL, NULL);
  658. return;
  659. }
  660. bt_shell_printf("Mesh session is open\n");
  661. /* Get composition data for a newly provisioned node */
  662. if (connection.type == CONN_TYPE_IDENTITY)
  663. config_client_get_composition(connection.unicast);
  664. }
  665. static void notify_proxy_out_cb(DBusMessage *message, void *user_data)
  666. {
  667. DBusError error;
  668. dbus_error_init(&error);
  669. if (dbus_set_error_from_message(&error, message) == TRUE) {
  670. bt_shell_printf("Failed to start notify: %s\n", error.name);
  671. dbus_error_free(&error);
  672. return;
  673. }
  674. bt_shell_printf("Notify for Mesh Proxy Out Data started\n");
  675. if (connection.type != CONN_TYPE_IDENTITY &&
  676. connection.type != CONN_TYPE_NETWORK) {
  677. bt_shell_printf("Error: wrong connection type %d "
  678. "(expected %d or %d)\n", connection.type,
  679. CONN_TYPE_IDENTITY, CONN_TYPE_NETWORK);
  680. return;
  681. }
  682. if (!connection.data_in) {
  683. bt_shell_printf("Error: don't have mesh proxy data in\n");
  684. return;
  685. }
  686. bt_shell_printf("Trying to open mesh session\n");
  687. net_session_open(connection.data_in, true, session_open_cb);
  688. connection.session_open = true;
  689. }
  690. static GDBusProxy *get_characteristic(GDBusProxy *device, const char *char_uuid)
  691. {
  692. GList *l;
  693. GDBusProxy *service;
  694. const char *svc_uuid;
  695. if (connection.type == CONN_TYPE_PROVISION) {
  696. svc_uuid = MESH_PROV_SVC_UUID;
  697. } else {
  698. svc_uuid = MESH_PROXY_SVC_UUID;
  699. }
  700. for (l = service_list; l; l = l->next) {
  701. if (mesh_gatt_is_child(l->data, device, "Device") &&
  702. service_is_mesh(l->data, svc_uuid))
  703. break;
  704. }
  705. if (l)
  706. service = l->data;
  707. else {
  708. bt_shell_printf("Mesh service not found\n");
  709. return NULL;
  710. }
  711. for (l = char_list; l; l = l->next) {
  712. if (mesh_gatt_is_child(l->data, service, "Service") &&
  713. char_is_mesh(l->data, char_uuid)) {
  714. bt_shell_printf("Found matching char: path %s,"
  715. " uuid %s\n",
  716. g_dbus_proxy_get_path(l->data),
  717. char_uuid);
  718. return l->data;
  719. }
  720. }
  721. return NULL;
  722. }
  723. static void mesh_session_setup(GDBusProxy *proxy)
  724. {
  725. if (connection.type == CONN_TYPE_PROVISION) {
  726. connection.data_in = get_characteristic(proxy,
  727. MESH_PROV_DATA_IN_UUID_STR);
  728. if (!connection.data_in)
  729. goto fail;
  730. connection.data_out = get_characteristic(proxy,
  731. MESH_PROV_DATA_OUT_UUID_STR);
  732. if (!connection.data_out)
  733. goto fail;
  734. data_out_notify(connection.data_out, true, notify_prov_out_cb);
  735. } else if (connection.type != CONN_TYPE_INVALID){
  736. connection.data_in = get_characteristic(proxy,
  737. MESH_PROXY_DATA_IN_UUID_STR);
  738. if (!connection.data_in)
  739. goto fail;
  740. connection.data_out = get_characteristic(proxy,
  741. MESH_PROXY_DATA_OUT_UUID_STR);
  742. if (!connection.data_out)
  743. goto fail;
  744. data_out_notify(connection.data_out, true, notify_proxy_out_cb);
  745. }
  746. return;
  747. fail:
  748. bt_shell_printf("Services resolved, mesh characteristics not found\n");
  749. }
  750. static void proxy_added(GDBusProxy *proxy, void *user_data)
  751. {
  752. const char *interface;
  753. interface = g_dbus_proxy_get_interface(proxy);
  754. if (!strcmp(interface, "org.bluez.Device1")) {
  755. update_device_info(proxy);
  756. } else if (!strcmp(interface, "org.bluez.Adapter1")) {
  757. adapter_added(proxy);
  758. } else if (!strcmp(interface, "org.bluez.GattService1") &&
  759. service_is_mesh(proxy, NULL)) {
  760. bt_shell_printf("Service added %s\n",
  761. g_dbus_proxy_get_path(proxy));
  762. service_list = g_list_append(service_list, proxy);
  763. } else if (!strcmp(interface, "org.bluez.GattCharacteristic1") &&
  764. char_is_mesh(proxy, NULL)) {
  765. bt_shell_printf("Char added %s:\n",
  766. g_dbus_proxy_get_path(proxy));
  767. char_list = g_list_append(char_list, proxy);
  768. }
  769. }
  770. static void start_discovery_reply(DBusMessage *message, void *user_data)
  771. {
  772. dbus_bool_t enable = GPOINTER_TO_UINT(user_data);
  773. DBusError error;
  774. dbus_error_init(&error);
  775. if (dbus_set_error_from_message(&error, message) == TRUE) {
  776. bt_shell_printf("Failed to %s discovery: %s\n",
  777. enable == TRUE ? "start" : "stop", error.name);
  778. dbus_error_free(&error);
  779. return bt_shell_noninteractive_quit(EXIT_FAILURE);
  780. }
  781. bt_shell_printf("Discovery %s\n",
  782. enable == TRUE ? "started" : "stopped");
  783. return bt_shell_noninteractive_quit(EXIT_SUCCESS);
  784. }
  785. static struct mesh_device *find_device_by_proxy(GList *source,
  786. GDBusProxy *proxy)
  787. {
  788. GList *list;
  789. for (list = g_list_first(source); list; list = g_list_next(list)) {
  790. struct mesh_device *dev = list->data;
  791. GDBusProxy *proxy = dev->proxy;
  792. if (dev->proxy == proxy)
  793. return dev;
  794. }
  795. return NULL;
  796. }
  797. static void device_removed(GDBusProxy *proxy)
  798. {
  799. struct adapter *adapter = find_parent(proxy);
  800. struct mesh_device *dev;
  801. if (!adapter) {
  802. /* TODO: Error */
  803. return;
  804. }
  805. dev = find_device_by_proxy(adapter->mesh_devices, proxy);
  806. if (dev)
  807. adapter->mesh_devices = g_list_remove(adapter->mesh_devices,
  808. dev);
  809. print_device(proxy, COLORED_DEL);
  810. if (connection.device == proxy)
  811. set_connected_device(NULL);
  812. }
  813. static void adapter_removed(GDBusProxy *proxy)
  814. {
  815. GList *ll;
  816. for (ll = g_list_first(ctrl_list); ll; ll = g_list_next(ll)) {
  817. struct adapter *adapter = ll->data;
  818. if (adapter->proxy == proxy) {
  819. print_adapter(proxy, COLORED_DEL);
  820. if (default_ctrl && default_ctrl->proxy == proxy) {
  821. default_ctrl = NULL;
  822. set_connected_device(NULL);
  823. }
  824. ctrl_list = g_list_remove_link(ctrl_list, ll);
  825. g_list_free_full(adapter->mesh_devices, g_free);
  826. g_free(adapter);
  827. g_list_free(ll);
  828. return;
  829. }
  830. }
  831. }
  832. static void proxy_removed(GDBusProxy *proxy, void *user_data)
  833. {
  834. const char *interface;
  835. interface = g_dbus_proxy_get_interface(proxy);
  836. if (!strcmp(interface, "org.bluez.Device1")) {
  837. device_removed(proxy);
  838. } else if (!strcmp(interface, "org.bluez.Adapter1")) {
  839. adapter_removed(proxy);
  840. } else if (!strcmp(interface, "org.bluez.GattService1")) {
  841. if (proxy == connection.service) {
  842. if (service_is_mesh(proxy, MESH_PROXY_SVC_UUID)) {
  843. data_out_notify(connection.data_out,
  844. false, NULL);
  845. net_session_close(connection.data_in);
  846. }
  847. connection.service = NULL;
  848. connection.data_in = NULL;
  849. connection.data_out = NULL;
  850. }
  851. service_list = g_list_remove(service_list, proxy);
  852. } else if (!strcmp(interface, "org.bluez.GattCharacteristic1")) {
  853. char_list = g_list_remove(char_list, proxy);
  854. }
  855. }
  856. static int get_characteristic_value(DBusMessageIter *value, uint8_t *buf)
  857. {
  858. DBusMessageIter array;
  859. uint8_t *data;
  860. int len;
  861. if (dbus_message_iter_get_arg_type(value) != DBUS_TYPE_ARRAY)
  862. return 0;
  863. dbus_message_iter_recurse(value, &array);
  864. if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_BYTE)
  865. return 0;
  866. dbus_message_iter_get_fixed_array(&array, &data, &len);
  867. memcpy(buf, data, len);
  868. return len;
  869. }
  870. static bool process_mesh_characteristic(GDBusProxy *proxy)
  871. {
  872. DBusMessageIter iter;
  873. const char *uuid;
  874. uint8_t *res;
  875. uint8_t buf[256];
  876. bool is_prov;
  877. if (g_dbus_proxy_get_property(proxy, "UUID", &iter) == FALSE)
  878. return false;
  879. dbus_message_iter_get_basic(&iter, &uuid);
  880. if (g_dbus_proxy_get_property(proxy, "Value", &iter) == FALSE)
  881. return false;
  882. is_prov = !bt_uuid_strcmp(uuid, MESH_PROV_DATA_OUT_UUID_STR);
  883. if (is_prov || !bt_uuid_strcmp(uuid, MESH_PROXY_DATA_OUT_UUID_STR))
  884. {
  885. struct mesh_node *node;
  886. uint16_t len;
  887. len = get_characteristic_value(&iter, buf);
  888. if (!len || len > 69)
  889. return false;
  890. res = buf;
  891. len = mesh_gatt_sar(&res, len);
  892. if (!len)
  893. return false;
  894. if (is_prov) {
  895. node = node_find_by_uuid(connection.dev_uuid);
  896. if (!node) {
  897. bt_shell_printf("Node not found?\n");
  898. return false;
  899. }
  900. return prov_data_ready(node, res, len);
  901. }
  902. return net_data_ready(res, len);
  903. }
  904. return false;
  905. }
  906. static void property_changed(GDBusProxy *proxy, const char *name,
  907. DBusMessageIter *iter, void *user_data)
  908. {
  909. const char *interface;
  910. interface = g_dbus_proxy_get_interface(proxy);
  911. if (!strcmp(interface, "org.bluez.Device1")) {
  912. if (default_ctrl && device_is_child(proxy,
  913. default_ctrl->proxy) == TRUE) {
  914. if (strcmp(name, "Connected") == 0) {
  915. dbus_bool_t connected;
  916. dbus_message_iter_get_basic(iter, &connected);
  917. if (connected && connection.device == NULL)
  918. set_connected_device(proxy);
  919. else if (!connected &&
  920. connection.device == proxy) {
  921. net_session_close(connection.data_in);
  922. set_connected_device(NULL);
  923. }
  924. } else if ((strcmp(name, "Alias") == 0) &&
  925. connection.device == proxy) {
  926. /* Re-generate prompt */
  927. set_connected_device(proxy);
  928. } else if (!strcmp(name, "ServiceData")) {
  929. update_device_info(proxy);
  930. } else if (!strcmp(name, "ServicesResolved")) {
  931. gboolean resolved;
  932. dbus_message_iter_get_basic(iter, &resolved);
  933. bt_shell_printf("Services resolved %s\n",
  934. resolved ? "yes" : "no");
  935. if (resolved)
  936. mesh_session_setup(connection.device);
  937. }
  938. }
  939. } else if (!strcmp(interface, "org.bluez.Adapter1")) {
  940. DBusMessageIter addr_iter;
  941. char *str;
  942. bt_shell_printf("Adapter property changed \n");
  943. if (g_dbus_proxy_get_property(proxy, "Address",
  944. &addr_iter) == TRUE) {
  945. const char *address;
  946. dbus_message_iter_get_basic(&addr_iter, &address);
  947. str = g_strdup_printf("[" COLORED_CHG
  948. "] Controller %s ", address);
  949. } else
  950. str = g_strdup("");
  951. if (strcmp(name, "Discovering") == 0) {
  952. int temp;
  953. dbus_message_iter_get_basic(iter, &temp);
  954. discovering = !!temp;
  955. }
  956. print_iter(str, name, iter);
  957. g_free(str);
  958. } else if (!strcmp(interface, "org.bluez.GattService1")) {
  959. bt_shell_printf("Service property changed %s\n",
  960. g_dbus_proxy_get_path(proxy));
  961. } else if (!strcmp(interface, "org.bluez.GattCharacteristic1")) {
  962. bt_shell_printf("Characteristic property changed %s\n",
  963. g_dbus_proxy_get_path(proxy));
  964. if ((strcmp(name, "Value") == 0) &&
  965. ((connection.type == CONN_TYPE_PROVISION) ||
  966. connection.session_open))
  967. process_mesh_characteristic(proxy);
  968. }
  969. }
  970. static void message_handler(DBusConnection *connection,
  971. DBusMessage *message, void *user_data)
  972. {
  973. bt_shell_printf("[SIGNAL] %s.%s\n", dbus_message_get_interface(message),
  974. dbus_message_get_member(message));
  975. }
  976. static struct adapter *find_ctrl_by_address(GList *source, const char *address)
  977. {
  978. GList *list;
  979. for (list = g_list_first(source); list; list = g_list_next(list)) {
  980. struct adapter *adapter = list->data;
  981. DBusMessageIter iter;
  982. const char *str;
  983. if (g_dbus_proxy_get_property(adapter->proxy,
  984. "Address", &iter) == FALSE)
  985. continue;
  986. dbus_message_iter_get_basic(&iter, &str);
  987. if (!strcmp(str, address))
  988. return adapter;
  989. }
  990. return NULL;
  991. }
  992. static gboolean parse_argument_on_off(int argc, char *argv[],
  993. dbus_bool_t *value)
  994. {
  995. if (!strcmp(argv[1], "on") || !strcmp(argv[1], "yes")) {
  996. *value = TRUE;
  997. return TRUE;
  998. }
  999. if (!strcmp(argv[1], "off") || !strcmp(argv[1], "no")) {
  1000. *value = FALSE;
  1001. return TRUE;
  1002. }
  1003. bt_shell_printf("Invalid argument %s\n", argv[1]);
  1004. return FALSE;
  1005. }
  1006. static void cmd_list(int argc, char *argv[])
  1007. {
  1008. GList *list;
  1009. for (list = g_list_first(ctrl_list); list; list = g_list_next(list)) {
  1010. struct adapter *adapter = list->data;
  1011. print_adapter(adapter->proxy, NULL);
  1012. }
  1013. return bt_shell_noninteractive_quit(EXIT_SUCCESS);
  1014. }
  1015. static void cmd_show(int argc, char *argv[])
  1016. {
  1017. struct adapter *adapter;
  1018. GDBusProxy *proxy;
  1019. DBusMessageIter iter;
  1020. const char *address;
  1021. if (argc < 2 || !strlen(argv[1])) {
  1022. if (check_default_ctrl() == FALSE)
  1023. return bt_shell_noninteractive_quit(EXIT_FAILURE);
  1024. proxy = default_ctrl->proxy;
  1025. } else {
  1026. adapter = find_ctrl_by_address(ctrl_list, argv[1]);
  1027. if (!adapter) {
  1028. bt_shell_printf("Controller %s not available\n",
  1029. argv[1]);
  1030. return bt_shell_noninteractive_quit(EXIT_FAILURE);
  1031. }
  1032. proxy = adapter->proxy;
  1033. }
  1034. if (g_dbus_proxy_get_property(proxy, "Address", &iter) == FALSE)
  1035. return bt_shell_noninteractive_quit(EXIT_FAILURE);
  1036. dbus_message_iter_get_basic(&iter, &address);
  1037. bt_shell_printf("Controller %s\n", address);
  1038. print_property(proxy, "Name");
  1039. print_property(proxy, "Alias");
  1040. print_property(proxy, "Class");
  1041. print_property(proxy, "Powered");
  1042. print_property(proxy, "Discoverable");
  1043. print_uuids(proxy);
  1044. print_property(proxy, "Modalias");
  1045. print_property(proxy, "Discovering");
  1046. return bt_shell_noninteractive_quit(EXIT_SUCCESS);
  1047. }
  1048. static void cmd_select(int argc, char *argv[])
  1049. {
  1050. struct adapter *adapter;
  1051. adapter = find_ctrl_by_address(ctrl_list, argv[1]);
  1052. if (!adapter) {
  1053. bt_shell_printf("Controller %s not available\n", argv[1]);
  1054. return bt_shell_noninteractive_quit(EXIT_FAILURE);
  1055. }
  1056. if (default_ctrl && default_ctrl->proxy == adapter->proxy)
  1057. return bt_shell_noninteractive_quit(EXIT_SUCCESS);
  1058. forget_mesh_devices();
  1059. default_ctrl = adapter;
  1060. print_adapter(adapter->proxy, NULL);
  1061. return bt_shell_noninteractive_quit(EXIT_SUCCESS);
  1062. }
  1063. static void generic_callback(const DBusError *error, void *user_data)
  1064. {
  1065. char *str = user_data;
  1066. if (dbus_error_is_set(error)) {
  1067. bt_shell_printf("Failed to set %s: %s\n", str, error->name);
  1068. return bt_shell_noninteractive_quit(EXIT_FAILURE);
  1069. } else {
  1070. bt_shell_printf("Changing %s succeeded\n", str);
  1071. return bt_shell_noninteractive_quit(EXIT_SUCCESS);
  1072. }
  1073. }
  1074. static void cmd_power(int argc, char *argv[])
  1075. {
  1076. dbus_bool_t powered;
  1077. char *str;
  1078. if (parse_argument_on_off(argc, argv, &powered) == FALSE)
  1079. return bt_shell_noninteractive_quit(EXIT_FAILURE);
  1080. if (check_default_ctrl() == FALSE)
  1081. return bt_shell_noninteractive_quit(EXIT_FAILURE);
  1082. str = g_strdup_printf("power %s", powered == TRUE ? "on" : "off");
  1083. if (g_dbus_proxy_set_property_basic(default_ctrl->proxy, "Powered",
  1084. DBUS_TYPE_BOOLEAN, &powered,
  1085. generic_callback, str, g_free) == TRUE)
  1086. return;
  1087. g_free(str);
  1088. return bt_shell_noninteractive_quit(EXIT_FAILURE);
  1089. }
  1090. #define DISTANCE_VAL_INVALID 0x7FFF
  1091. struct set_discovery_filter_args {
  1092. char *transport;
  1093. dbus_uint16_t rssi;
  1094. dbus_int16_t pathloss;
  1095. char **uuids;
  1096. size_t uuids_len;
  1097. dbus_bool_t duplicate;
  1098. };
  1099. static void set_discovery_filter_setup(DBusMessageIter *iter, void *user_data)
  1100. {
  1101. struct set_discovery_filter_args *args = user_data;
  1102. DBusMessageIter dict;
  1103. dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
  1104. DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
  1105. DBUS_TYPE_STRING_AS_STRING
  1106. DBUS_TYPE_VARIANT_AS_STRING
  1107. DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
  1108. g_dbus_dict_append_array(&dict, "UUIDs", DBUS_TYPE_STRING,
  1109. &args->uuids,
  1110. args->uuids_len);
  1111. if (args->pathloss != DISTANCE_VAL_INVALID)
  1112. g_dbus_dict_append_entry(&dict, "Pathloss", DBUS_TYPE_UINT16,
  1113. &args->pathloss);
  1114. if (args->rssi != DISTANCE_VAL_INVALID)
  1115. g_dbus_dict_append_entry(&dict, "RSSI", DBUS_TYPE_INT16,
  1116. &args->rssi);
  1117. if (args->transport != NULL)
  1118. g_dbus_dict_append_entry(&dict, "Transport", DBUS_TYPE_STRING,
  1119. &args->transport);
  1120. if (args->duplicate)
  1121. g_dbus_dict_append_entry(&dict, "DuplicateData",
  1122. DBUS_TYPE_BOOLEAN,
  1123. &args->duplicate);
  1124. dbus_message_iter_close_container(iter, &dict);
  1125. }
  1126. static void set_discovery_filter_reply(DBusMessage *message, void *user_data)
  1127. {
  1128. DBusError error;
  1129. dbus_error_init(&error);
  1130. if (dbus_set_error_from_message(&error, message) == TRUE) {
  1131. bt_shell_printf("SetDiscoveryFilter failed: %s\n", error.name);
  1132. dbus_error_free(&error);
  1133. return bt_shell_noninteractive_quit(EXIT_FAILURE);
  1134. }
  1135. bt_shell_printf("SetDiscoveryFilter success\n");
  1136. return bt_shell_noninteractive_quit(EXIT_SUCCESS);
  1137. }
  1138. static gint filtered_scan_rssi = DISTANCE_VAL_INVALID;
  1139. static gint filtered_scan_pathloss = DISTANCE_VAL_INVALID;
  1140. static char **filtered_scan_uuids;
  1141. static size_t filtered_scan_uuids_len;
  1142. static char *filtered_scan_transport = "le";
  1143. static void set_scan_filter_commit(void)
  1144. {
  1145. struct set_discovery_filter_args args;
  1146. args.pathloss = filtered_scan_pathloss;
  1147. args.rssi = filtered_scan_rssi;
  1148. args.transport = filtered_scan_transport;
  1149. args.uuids = filtered_scan_uuids;
  1150. args.uuids_len = filtered_scan_uuids_len;
  1151. args.duplicate = TRUE;
  1152. if (check_default_ctrl() == FALSE)
  1153. return bt_shell_noninteractive_quit(EXIT_FAILURE);
  1154. if (g_dbus_proxy_method_call(default_ctrl->proxy, "SetDiscoveryFilter",
  1155. set_discovery_filter_setup, set_discovery_filter_reply,
  1156. &args, NULL) == FALSE) {
  1157. bt_shell_printf("Failed to set discovery filter\n");
  1158. return bt_shell_noninteractive_quit(EXIT_FAILURE);
  1159. }
  1160. }
  1161. static void set_scan_filter_uuids(char *filters[])
  1162. {
  1163. g_strfreev(filtered_scan_uuids);
  1164. filtered_scan_uuids = NULL;
  1165. filtered_scan_uuids_len = 0;
  1166. if (!filters)
  1167. goto commit;
  1168. filtered_scan_uuids = g_strdupv(filters);
  1169. if (!filtered_scan_uuids) {
  1170. bt_shell_printf("Failed to parse input\n");
  1171. return bt_shell_noninteractive_quit(EXIT_FAILURE);
  1172. }
  1173. filtered_scan_uuids_len = g_strv_length(filtered_scan_uuids);
  1174. commit:
  1175. set_scan_filter_commit();
  1176. }
  1177. static void cmd_scan_unprovisioned(int argc, char *argv[])
  1178. {
  1179. dbus_bool_t enable;
  1180. char *filters[] = { MESH_PROV_SVC_UUID, NULL };
  1181. const char *method;
  1182. if (parse_argument_on_off(argc, argv, &enable) == FALSE)
  1183. return bt_shell_noninteractive_quit(EXIT_FAILURE);
  1184. if (check_default_ctrl() == FALSE)
  1185. return bt_shell_noninteractive_quit(EXIT_FAILURE);
  1186. if (enable == TRUE) {
  1187. discover_mesh = false;
  1188. set_scan_filter_uuids(filters);
  1189. method = "StartDiscovery";
  1190. } else {
  1191. method = "StopDiscovery";
  1192. }
  1193. if (g_dbus_proxy_method_call(default_ctrl->proxy, method,
  1194. NULL, start_discovery_reply,
  1195. GUINT_TO_POINTER(enable), NULL) == FALSE) {
  1196. bt_shell_printf("Failed to %s discovery\n",
  1197. enable == TRUE ? "start" : "stop");
  1198. return bt_shell_noninteractive_quit(EXIT_FAILURE);
  1199. }
  1200. }
  1201. static void cmd_info(int argc, char *argv[])
  1202. {
  1203. GDBusProxy *proxy;
  1204. DBusMessageIter iter;
  1205. const char *address;
  1206. proxy = connection.device;
  1207. if (!proxy)
  1208. return bt_shell_noninteractive_quit(EXIT_FAILURE);
  1209. if (g_dbus_proxy_get_property(proxy, "Address", &iter) == FALSE)
  1210. return bt_shell_noninteractive_quit(EXIT_FAILURE);
  1211. dbus_message_iter_get_basic(&iter, &address);
  1212. bt_shell_printf("Device %s\n", address);
  1213. print_property(proxy, "Name");
  1214. print_property(proxy, "Alias");
  1215. print_property(proxy, "Class");
  1216. print_property(proxy, "Appearance");
  1217. print_property(proxy, "Icon");
  1218. print_property(proxy, "Trusted");
  1219. print_property(proxy, "Blocked");
  1220. print_property(proxy, "Connected");
  1221. print_uuids(proxy);
  1222. print_property(proxy, "Modalias");
  1223. print_property(proxy, "ManufacturerData");
  1224. print_property(proxy, "ServiceData");
  1225. print_property(proxy, "RSSI");
  1226. print_property(proxy, "TxPower");
  1227. return bt_shell_noninteractive_quit(EXIT_SUCCESS);
  1228. }
  1229. static const char *security2str(uint8_t level)
  1230. {
  1231. switch (level) {
  1232. case 0:
  1233. return "low";
  1234. case 1:
  1235. return "medium";
  1236. case 2:
  1237. return "high";
  1238. default:
  1239. return "invalid";
  1240. }
  1241. }
  1242. static void cmd_security(int argc, char *argv[])
  1243. {
  1244. uint8_t level;
  1245. char *end;
  1246. if (argc == 1)
  1247. goto done;
  1248. level = strtol(argv[1], &end, 10);
  1249. if (end == argv[1] || !prov_set_sec_level(level)) {
  1250. bt_shell_printf("Invalid security level %s\n", argv[1]);
  1251. return bt_shell_noninteractive_quit(EXIT_FAILURE);
  1252. }
  1253. done:
  1254. bt_shell_printf("Provision Security Level set to %u (%s)\n",
  1255. prov_get_sec_level(),
  1256. security2str(prov_get_sec_level()));
  1257. return bt_shell_noninteractive_quit(EXIT_SUCCESS);
  1258. }
  1259. static void cmd_connect(int argc, char *argv[])
  1260. {
  1261. char *filters[] = { MESH_PROXY_SVC_UUID, NULL };
  1262. if (check_default_ctrl() == FALSE)
  1263. return bt_shell_noninteractive_quit(EXIT_FAILURE);
  1264. memset(&connection, 0, sizeof(connection));
  1265. if (argc < 2 || !strlen(argv[1])) {
  1266. connection.net_idx = NET_IDX_PRIMARY;
  1267. } else {
  1268. char *end;
  1269. connection.net_idx = strtol(argv[1], &end, 16);
  1270. if (end == argv[1]) {
  1271. connection.net_idx = NET_IDX_INVALID;
  1272. bt_shell_printf("Invalid network index %s\n", argv[1]);
  1273. return bt_shell_noninteractive_quit(EXIT_FAILURE);
  1274. }
  1275. if (argc > 2)
  1276. connection.unicast = strtol(argv[2], NULL, 16);
  1277. }
  1278. if (discovering)
  1279. g_dbus_proxy_method_call(default_ctrl->proxy, "StopDiscovery",
  1280. NULL, NULL, NULL, NULL);
  1281. set_scan_filter_uuids(filters);
  1282. discover_mesh = true;
  1283. if (connection.unicast == UNASSIGNED_ADDRESS) {
  1284. connection.type = CONN_TYPE_NETWORK;
  1285. bt_shell_printf("Looking for mesh network with net index "
  1286. "%4.4x\n", connection.net_idx);
  1287. } else {
  1288. connection.type = CONN_TYPE_IDENTITY;
  1289. bt_shell_printf("Looking for node id %4.4x"
  1290. " on network with net index %4.4x\n",
  1291. connection.unicast, connection.net_idx);
  1292. }
  1293. if (g_dbus_proxy_method_call(default_ctrl->proxy,
  1294. "StartDiscovery", NULL, start_discovery_reply,
  1295. GUINT_TO_POINTER(TRUE), NULL) == FALSE) {
  1296. bt_shell_printf("Failed to start mesh proxy discovery\n");
  1297. return bt_shell_noninteractive_quit(EXIT_FAILURE);
  1298. }
  1299. g_dbus_proxy_method_call(default_ctrl->proxy, "StartDiscovery",
  1300. NULL, NULL, NULL, NULL);
  1301. }
  1302. static void prov_disconn_reply(DBusMessage *message, void *user_data)
  1303. {
  1304. struct mesh_node *node = user_data;
  1305. char *filters[] = { MESH_PROXY_SVC_UUID, NULL };
  1306. DBusError error;
  1307. dbus_error_init(&error);
  1308. if (dbus_set_error_from_message(&error, message) == TRUE) {
  1309. bt_shell_printf("Failed to disconnect: %s\n", error.name);
  1310. dbus_error_free(&error);
  1311. return bt_shell_noninteractive_quit(EXIT_FAILURE);
  1312. }
  1313. set_connected_device(NULL);
  1314. set_scan_filter_uuids(filters);
  1315. discover_mesh = true;
  1316. connection.type = CONN_TYPE_IDENTITY;
  1317. connection.data_in = NULL;
  1318. connection.data_out = NULL;
  1319. connection.unicast = node_get_primary(node);
  1320. if (g_dbus_proxy_method_call(default_ctrl->proxy,
  1321. "StartDiscovery", NULL, start_discovery_reply,
  1322. GUINT_TO_POINTER(TRUE), NULL) == FALSE) {
  1323. bt_shell_printf("Failed to start mesh proxy discovery\n");
  1324. return bt_shell_noninteractive_quit(EXIT_FAILURE);
  1325. }
  1326. }
  1327. static void disconn_reply(DBusMessage *message, void *user_data)
  1328. {
  1329. GDBusProxy *proxy = user_data;
  1330. DBusError error;
  1331. dbus_error_init(&error);
  1332. if (dbus_set_error_from_message(&error, message) == TRUE) {
  1333. bt_shell_printf("Failed to disconnect: %s\n", error.name);
  1334. dbus_error_free(&error);
  1335. return bt_shell_noninteractive_quit(EXIT_FAILURE);
  1336. }
  1337. bt_shell_printf("Successfully disconnected\n");
  1338. if (proxy != connection.device)
  1339. return bt_shell_noninteractive_quit(EXIT_SUCCESS);
  1340. set_connected_device(NULL);
  1341. return bt_shell_noninteractive_quit(EXIT_SUCCESS);
  1342. }
  1343. static void cmd_disconn(int argc, char *argv[])
  1344. {
  1345. if (connection.type == CONN_TYPE_PROVISION) {
  1346. struct mesh_node *node = node_find_by_uuid(connection.dev_uuid);
  1347. if (node)
  1348. node_free(node);
  1349. }
  1350. disconnect_device(disconn_reply, connection.device);
  1351. return bt_shell_noninteractive_quit(EXIT_SUCCESS);
  1352. }
  1353. static void mesh_prov_done(void *user_data, int status)
  1354. {
  1355. struct mesh_node *node = user_data;
  1356. if (status){
  1357. bt_shell_printf("Provisioning failed\n");
  1358. node_free(node);
  1359. disconnect_device(NULL, NULL);
  1360. return;
  1361. }
  1362. bt_shell_printf("Provision success. Assigned Primary Unicast %4.4x\n",
  1363. node_get_primary(node));
  1364. if (!prov_db_add_new_node(node))
  1365. bt_shell_printf("Failed to add node to provisioning DB\n");
  1366. disconnect_device(prov_disconn_reply, node);
  1367. }
  1368. static void cmd_start_prov(int argc, char *argv[])
  1369. {
  1370. GDBusProxy *proxy;
  1371. struct mesh_device *dev;
  1372. struct mesh_node *node;
  1373. int len;
  1374. len = strlen(argv[1]);
  1375. if ( len > 32 || len % 2) {
  1376. bt_shell_printf("Incorrect UUID size %d\n", len);
  1377. }
  1378. disconnect_device(NULL, NULL);
  1379. memset(connection.dev_uuid, 0, 16);
  1380. str2hex(argv[1], len, connection.dev_uuid, len/2);
  1381. node = node_find_by_uuid(connection.dev_uuid);
  1382. if (!node) {
  1383. bt_shell_printf("Device with UUID %s not found.\n", argv[1]);
  1384. bt_shell_printf("Stale services? Remove device and "
  1385. "re-discover\n");
  1386. return bt_shell_noninteractive_quit(EXIT_FAILURE);
  1387. }
  1388. /* TODO: add command to remove a node from mesh, i.e., "unprovision" */
  1389. if (node_is_provisioned(node)) {
  1390. bt_shell_printf("Already provisioned with unicast %4.4x\n",
  1391. node_get_primary(node));
  1392. return bt_shell_noninteractive_quit(EXIT_FAILURE);
  1393. }
  1394. dev = find_device_by_uuid(default_ctrl->mesh_devices,
  1395. connection.dev_uuid);
  1396. if (!dev || !dev->proxy) {
  1397. bt_shell_printf("Could not find device proxy\n");
  1398. memset(connection.dev_uuid, 0, 16);
  1399. return bt_shell_noninteractive_quit(EXIT_FAILURE);
  1400. }
  1401. proxy = dev->proxy;
  1402. if (discovering)
  1403. g_dbus_proxy_method_call(default_ctrl->proxy, "StopDiscovery",
  1404. NULL, NULL, NULL, NULL);
  1405. forget_mesh_devices();
  1406. connection.type = CONN_TYPE_PROVISION;
  1407. if (g_dbus_proxy_method_call(proxy, "Connect", NULL, connect_reply,
  1408. proxy, NULL) == FALSE) {
  1409. bt_shell_printf("Failed to connect ");
  1410. print_device(proxy, NULL);
  1411. return bt_shell_noninteractive_quit(EXIT_FAILURE);
  1412. } else {
  1413. bt_shell_printf("Trying to connect ");
  1414. print_device(proxy, NULL);
  1415. }
  1416. }
  1417. static void cmd_print_mesh(int argc, char *argv[])
  1418. {
  1419. if (!prov_db_show(mesh_prov_db_filename)) {
  1420. bt_shell_printf("Unavailable\n");
  1421. return bt_shell_noninteractive_quit(EXIT_FAILURE);
  1422. }
  1423. return bt_shell_noninteractive_quit(EXIT_SUCCESS);
  1424. }
  1425. static void cmd_print_local(int argc, char *argv[])
  1426. {
  1427. if (!prov_db_show(mesh_local_config_filename)) {
  1428. bt_shell_printf("Unavailable\n");
  1429. return bt_shell_noninteractive_quit(EXIT_FAILURE);
  1430. }
  1431. return bt_shell_noninteractive_quit(EXIT_SUCCESS);
  1432. }
  1433. static const struct bt_shell_menu main_menu = {
  1434. .name = "main",
  1435. .entries = {
  1436. { "list", NULL, cmd_list, "List available controllers"},
  1437. { "show", "[ctrl]", cmd_show, "Controller information"},
  1438. { "select", "<ctrl>", cmd_select, "Select default controller"},
  1439. { "security", "[0(low)/1(medium)/2(high)]", cmd_security,
  1440. "Display or change provision security level"},
  1441. { "info", "[dev]", cmd_info, "Device information"},
  1442. { "connect", "[net_idx] [dst]", cmd_connect,
  1443. "Connect to mesh network or node on network"},
  1444. { "discover-unprovisioned", "<on/off>", cmd_scan_unprovisioned,
  1445. "Look for devices to provision" },
  1446. { "provision", "<uuid>", cmd_start_prov, "Initiate provisioning"},
  1447. { "power", "<on/off>", cmd_power, "Set controller power" },
  1448. { "disconnect", "[dev]", cmd_disconn, "Disconnect device"},
  1449. { "mesh-info", NULL, cmd_print_mesh,
  1450. "Mesh networkinfo (provisioner)" },
  1451. { "local-info", NULL, cmd_print_local, "Local mesh node info" },
  1452. { } },
  1453. };
  1454. static const char *config_dir;
  1455. static const struct option options[] = {
  1456. { "config", required_argument, 0, 'c' },
  1457. { 0, 0, 0, 0 }
  1458. };
  1459. static const char **optargs[] = {
  1460. &config_dir
  1461. };
  1462. static const char *help[] = {
  1463. "Read local mesh config JSON files from <directory>"
  1464. };
  1465. static const struct bt_shell_opt opt = {
  1466. .options = options,
  1467. .optno = sizeof(options) / sizeof(struct option),
  1468. .optstr = "c:",
  1469. .optarg = optargs,
  1470. .help = help,
  1471. };
  1472. static void client_ready(GDBusClient *client, void *user_data)
  1473. {
  1474. bt_shell_attach(fileno(stdin));
  1475. }
  1476. int main(int argc, char *argv[])
  1477. {
  1478. GDBusClient *client;
  1479. int status;
  1480. int len;
  1481. int extra;
  1482. char *mesh_dir = NULL;
  1483. bt_shell_init(argc, argv, &opt);
  1484. bt_shell_set_menu(&main_menu);
  1485. bt_shell_set_prompt(PROMPT_OFF);
  1486. if (!config_dir) {
  1487. char *home;
  1488. home = getenv("XDG_CONFIG_HOME");
  1489. if (home) {
  1490. mesh_dir = g_strdup_printf("%s/meshctl", home);
  1491. }
  1492. if (!mesh_dir) {
  1493. home = getenv("HOME");
  1494. if (home)
  1495. mesh_dir = g_strdup_printf("%s/.config/meshctl",
  1496. home);
  1497. }
  1498. if (!mesh_dir) {
  1499. g_printerr("Configuration directory not found\n");
  1500. goto fail;
  1501. }
  1502. } else {
  1503. mesh_dir = g_strdup_printf("%s", config_dir);
  1504. }
  1505. g_print("Reading prov_db.json and local_node.json from %s directory\n",
  1506. mesh_dir);
  1507. len = strlen(mesh_dir);
  1508. if (len && mesh_dir[len - 1] != '/')
  1509. extra = 1;
  1510. else
  1511. extra = 0;
  1512. mesh_local_config_filename = g_malloc(len + strlen("local_node.json")
  1513. + 2);
  1514. if (!mesh_local_config_filename)
  1515. goto fail;
  1516. mesh_prov_db_filename = g_malloc(len + strlen("prov_db.json") + 2);
  1517. if (!mesh_prov_db_filename)
  1518. goto fail;
  1519. sprintf(mesh_local_config_filename, "%s", mesh_dir);
  1520. if (extra)
  1521. sprintf(mesh_local_config_filename + len , "%c", '/');
  1522. sprintf(mesh_local_config_filename + len + extra, "%s",
  1523. "local_node.json");
  1524. len = len + extra + strlen("local_node.json");
  1525. if (!prov_db_read_local_node(mesh_local_config_filename, true)) {
  1526. g_printerr("Failed to parse local node configuration file %s\n",
  1527. mesh_local_config_filename);
  1528. goto fail;
  1529. }
  1530. sprintf(mesh_prov_db_filename, "%s", mesh_dir);
  1531. len = strlen(mesh_dir);
  1532. g_free(mesh_dir);
  1533. if (extra)
  1534. sprintf(mesh_prov_db_filename + len , "%c", '/');
  1535. sprintf(mesh_prov_db_filename + len + extra, "%s", "prov_db.json");
  1536. if (!prov_db_read(mesh_prov_db_filename)) {
  1537. g_printerr("Failed to parse provisioning database file %s\n",
  1538. mesh_prov_db_filename);
  1539. goto fail;
  1540. }
  1541. dbus_conn = g_dbus_setup_bus(DBUS_BUS_SYSTEM, NULL, NULL);
  1542. client = g_dbus_client_new(dbus_conn, "org.bluez", "/org/bluez");
  1543. g_dbus_client_set_connect_watch(client, connect_handler, NULL);
  1544. g_dbus_client_set_disconnect_watch(client, disconnect_handler, NULL);
  1545. g_dbus_client_set_signal_watch(client, message_handler, NULL);
  1546. g_dbus_client_set_proxy_handlers(client, proxy_added, proxy_removed,
  1547. property_changed, NULL);
  1548. g_dbus_client_set_ready_watch(client, client_ready, NULL);
  1549. if (!config_client_init())
  1550. g_printerr("Failed to initialize mesh configuration client\n");
  1551. if (!config_server_init())
  1552. g_printerr("Failed to initialize mesh configuration server\n");
  1553. if (!onoff_client_init(PRIMARY_ELEMENT_IDX))
  1554. g_printerr("Failed to initialize mesh generic On/Off client\n");
  1555. status = bt_shell_run();
  1556. g_dbus_client_unref(client);
  1557. dbus_connection_unref(dbus_conn);
  1558. node_cleanup();
  1559. g_list_free(char_list);
  1560. g_list_free(service_list);
  1561. g_list_free_full(ctrl_list, proxy_leak);
  1562. return status;
  1563. fail:
  1564. bt_shell_cleanup();
  1565. g_free(mesh_dir);
  1566. return EXIT_FAILURE;
  1567. }