node.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330
  1. // SPDX-License-Identifier: LGPL-2.1-or-later
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2017-2020 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 <dirent.h>
  15. #include <limits.h>
  16. #include <stdio.h>
  17. #include <sys/time.h>
  18. #include <ell/ell.h>
  19. #include "mesh/mesh-defs.h"
  20. #include "mesh/mesh.h"
  21. #include "mesh/net.h"
  22. #include "mesh/net-keys.h"
  23. #include "mesh/appkey.h"
  24. #include "mesh/mesh-config.h"
  25. #include "mesh/provision.h"
  26. #include "mesh/keyring.h"
  27. #include "mesh/model.h"
  28. #include "mesh/cfgmod.h"
  29. #include "mesh/util.h"
  30. #include "mesh/error.h"
  31. #include "mesh/dbus.h"
  32. #include "mesh/agent.h"
  33. #include "mesh/manager.h"
  34. #include "mesh/rpl.h"
  35. #include "mesh/node.h"
  36. #define MESH_NODE_PATH_PREFIX "/node"
  37. /* Default values for a new locally created node */
  38. #define DEFAULT_NEW_UNICAST 0x0001
  39. #define DEFAULT_IV_INDEX 0x0000
  40. /* Default element location: unknown */
  41. #define DEFAULT_LOCATION 0x0000
  42. enum request_type {
  43. REQUEST_TYPE_JOIN,
  44. REQUEST_TYPE_ATTACH,
  45. REQUEST_TYPE_CREATE,
  46. REQUEST_TYPE_IMPORT,
  47. };
  48. struct node_element {
  49. char *path;
  50. struct l_queue *models;
  51. uint16_t location;
  52. uint8_t idx;
  53. };
  54. struct node_composition {
  55. uint16_t cid;
  56. uint16_t pid;
  57. uint16_t vid;
  58. uint16_t crpl;
  59. };
  60. struct mesh_node {
  61. struct mesh_net *net;
  62. struct l_queue *elements;
  63. struct l_queue *pages;
  64. char *app_path;
  65. char *owner;
  66. char *obj_path;
  67. struct mesh_agent *agent;
  68. struct mesh_config *cfg;
  69. char *storage_dir;
  70. uint32_t disc_watch;
  71. uint32_t seq_number;
  72. bool busy;
  73. bool provisioner;
  74. uint16_t primary;
  75. struct node_composition comp;
  76. struct {
  77. uint16_t interval;
  78. uint8_t cnt;
  79. uint8_t mode;
  80. } relay;
  81. uint8_t uuid[16];
  82. uint8_t dev_key[16];
  83. uint8_t token[8];
  84. uint8_t num_ele;
  85. uint8_t ttl;
  86. uint8_t lpn;
  87. uint8_t proxy;
  88. uint8_t friend;
  89. uint8_t beacon;
  90. };
  91. struct node_import {
  92. uint8_t dev_key[16];
  93. uint8_t net_key[16];
  94. uint16_t net_idx;
  95. struct {
  96. bool ivu;
  97. bool kr;
  98. } flags;
  99. uint32_t iv_index;
  100. uint16_t unicast;
  101. };
  102. struct managed_obj_request {
  103. struct mesh_node *node;
  104. union {
  105. node_ready_func_t ready_cb;
  106. node_join_ready_func_t join_ready_cb;
  107. };
  108. struct l_dbus_message *pending_msg;
  109. enum request_type type;
  110. union {
  111. struct mesh_node *attach;
  112. struct node_import *import;
  113. };
  114. };
  115. struct send_options {
  116. bool segmented;
  117. uint16_t vendor_id;
  118. };
  119. static struct l_queue *nodes;
  120. static bool match_device_uuid(const void *a, const void *b)
  121. {
  122. const struct mesh_node *node = a;
  123. const uint8_t *uuid = b;
  124. return (memcmp(node->uuid, uuid, 16) == 0);
  125. }
  126. static bool match_token(const void *a, const void *b)
  127. {
  128. const struct mesh_node *node = a;
  129. const uint64_t *token = b;
  130. const uint64_t tmp = l_get_be64(node->token);
  131. return *token == tmp;
  132. }
  133. static bool match_element_idx(const void *a, const void *b)
  134. {
  135. const struct node_element *element = a;
  136. uint32_t index = L_PTR_TO_UINT(b);
  137. return (element->idx == index);
  138. }
  139. static int compare_element_idx(const void *a, const void *b, void *user_data)
  140. {
  141. uint32_t a_idx = ((const struct node_element *)a)->idx;
  142. uint32_t b_idx = ((const struct node_element *)b)->idx;
  143. if (a_idx < b_idx)
  144. return -1;
  145. if (a_idx > b_idx)
  146. return 1;
  147. return 0;
  148. }
  149. static bool match_element_path(const void *a, const void *b)
  150. {
  151. const struct node_element *element = a;
  152. const char *path = b;
  153. if (!element->path)
  154. return false;
  155. return (!strcmp(element->path, path));
  156. }
  157. struct mesh_node *node_find_by_uuid(uint8_t uuid[16])
  158. {
  159. return l_queue_find(nodes, match_device_uuid, uuid);
  160. }
  161. struct mesh_node *node_find_by_token(uint64_t token)
  162. {
  163. return l_queue_find(nodes, match_token, (void *) &token);
  164. }
  165. uint8_t *node_uuid_get(struct mesh_node *node)
  166. {
  167. if (!node)
  168. return NULL;
  169. return node->uuid;
  170. }
  171. static void set_defaults(struct mesh_node *node)
  172. {
  173. node->lpn = MESH_MODE_UNSUPPORTED;
  174. node->proxy = MESH_MODE_UNSUPPORTED;
  175. node->friend = (mesh_friendship_supported()) ? MESH_MODE_DISABLED :
  176. MESH_MODE_UNSUPPORTED;
  177. node->beacon = (mesh_beacon_enabled()) ? MESH_MODE_ENABLED :
  178. MESH_MODE_DISABLED;
  179. node->relay.mode = (mesh_relay_supported()) ? MESH_MODE_DISABLED :
  180. MESH_MODE_UNSUPPORTED;
  181. node->ttl = TTL_MASK;
  182. node->seq_number = DEFAULT_SEQUENCE_NUMBER;
  183. }
  184. static struct mesh_node *node_new(const uint8_t uuid[16])
  185. {
  186. struct mesh_node *node;
  187. node = l_new(struct mesh_node, 1);
  188. node->net = mesh_net_new(node);
  189. node->elements = l_queue_new();
  190. node->pages = l_queue_new();
  191. memcpy(node->uuid, uuid, sizeof(node->uuid));
  192. set_defaults(node);
  193. return node;
  194. }
  195. static void free_element_path(void *a, void *b)
  196. {
  197. struct node_element *element = a;
  198. l_free(element->path);
  199. element->path = NULL;
  200. }
  201. static void element_free(void *data)
  202. {
  203. struct node_element *element = data;
  204. l_queue_destroy(element->models, mesh_model_free);
  205. l_free(element->path);
  206. l_free(element);
  207. }
  208. static void free_node_dbus_resources(struct mesh_node *node)
  209. {
  210. if (!node)
  211. return;
  212. if (node->disc_watch) {
  213. l_dbus_remove_watch(dbus_get_bus(), node->disc_watch);
  214. node->disc_watch = 0;
  215. }
  216. l_queue_foreach(node->elements, free_element_path, NULL);
  217. l_free(node->owner);
  218. node->owner = NULL;
  219. l_free(node->app_path);
  220. node->app_path = NULL;
  221. if (node->obj_path) {
  222. l_dbus_object_remove_interface(dbus_get_bus(), node->obj_path,
  223. MESH_NODE_INTERFACE);
  224. l_dbus_object_remove_interface(dbus_get_bus(), node->obj_path,
  225. MESH_MANAGEMENT_INTERFACE);
  226. l_dbus_object_remove_interface(dbus_get_bus(), node->obj_path,
  227. L_DBUS_INTERFACE_PROPERTIES);
  228. l_free(node->obj_path);
  229. node->obj_path = NULL;
  230. }
  231. }
  232. static void free_node_resources(void *data)
  233. {
  234. struct mesh_node *node = data;
  235. /* Unregister io callbacks */
  236. mesh_net_detach(node->net);
  237. /* In case of a provisioner, stop active scanning */
  238. if (node->provisioner)
  239. manager_scan_cancel(node);
  240. /* Free dynamic resources */
  241. free_node_dbus_resources(node);
  242. l_queue_destroy(node->elements, element_free);
  243. l_queue_destroy(node->pages, l_free);
  244. mesh_agent_remove(node->agent);
  245. mesh_config_release(node->cfg);
  246. mesh_net_free(node->net);
  247. l_free(node->storage_dir);
  248. l_free(node);
  249. }
  250. /*
  251. * This function is called to free resources and remove the
  252. * configuration files for the specified node.
  253. */
  254. void node_remove(struct mesh_node *node)
  255. {
  256. if (!node)
  257. return;
  258. l_queue_remove(nodes, node);
  259. mesh_config_destroy_nvm(node->cfg);
  260. free_node_resources(node);
  261. }
  262. static bool add_element_from_storage(struct mesh_node *node,
  263. struct mesh_config_element *db_ele)
  264. {
  265. struct node_element *ele;
  266. ele = l_new(struct node_element, 1);
  267. if (!ele)
  268. return false;
  269. ele->idx = db_ele->index;
  270. ele->location = db_ele->location;
  271. ele->models = l_queue_new();
  272. l_queue_push_tail(node->elements, ele);
  273. if (!mesh_model_add_from_storage(node, ele->idx, ele->models,
  274. db_ele->models))
  275. return false;
  276. return true;
  277. }
  278. static bool add_elements_from_storage(struct mesh_node *node,
  279. struct mesh_config_node *db_node)
  280. {
  281. const struct l_queue_entry *entry;
  282. entry = l_queue_get_entries(db_node->elements);
  283. for (; entry; entry = entry->next)
  284. if (!add_element_from_storage(node, entry->data))
  285. return false;
  286. return true;
  287. }
  288. static void set_net_key(void *a, void *b)
  289. {
  290. struct mesh_config_netkey *netkey = a;
  291. struct mesh_node *node = b;
  292. mesh_net_set_key(node->net, netkey->idx, netkey->key, netkey->new_key,
  293. netkey->phase);
  294. }
  295. static void set_appkey(void *a, void *b)
  296. {
  297. struct mesh_config_appkey *appkey = a;
  298. struct mesh_node *node = b;
  299. appkey_key_init(node->net, appkey->net_idx, appkey->app_idx,
  300. appkey->key, appkey->new_key);
  301. }
  302. static bool init_storage_dir(struct mesh_node *node)
  303. {
  304. char uuid[33];
  305. char dir_name[PATH_MAX];
  306. if (node->storage_dir)
  307. return true;
  308. if (!hex2str(node->uuid, 16, uuid, sizeof(uuid)))
  309. return false;
  310. snprintf(dir_name, PATH_MAX, "%s/%s", mesh_get_storage_dir(), uuid);
  311. if (strlen(dir_name) >= PATH_MAX)
  312. return false;
  313. create_dir(dir_name);
  314. node->storage_dir = l_strdup(dir_name);
  315. /* Initialize directory for storing RPL info */
  316. return rpl_init(node->storage_dir);
  317. }
  318. static void update_net_settings(struct mesh_node *node)
  319. {
  320. struct mesh_net *net = node->net;
  321. mesh_net_set_proxy_mode(net, node->proxy == MESH_MODE_ENABLED);
  322. mesh_net_set_friend_mode(net, node->friend == MESH_MODE_ENABLED);
  323. mesh_net_set_relay_mode(net, node->relay.mode == MESH_MODE_ENABLED,
  324. node->relay.cnt, node->relay.interval);
  325. mesh_net_set_beacon_mode(net, node->beacon == MESH_MODE_ENABLED);
  326. }
  327. static bool init_from_storage(struct mesh_config_node *db_node,
  328. const uint8_t uuid[16], struct mesh_config *cfg,
  329. void *user_data)
  330. {
  331. unsigned int num_ele;
  332. struct mesh_node *node = node_new(uuid);
  333. if (!nodes)
  334. nodes = l_queue_new();
  335. l_queue_push_tail(nodes, node);
  336. node->comp.cid = db_node->cid;
  337. node->comp.pid = db_node->pid;
  338. node->comp.vid = db_node->vid;
  339. node->comp.crpl = db_node->crpl;
  340. node->lpn = db_node->modes.lpn;
  341. node->proxy = db_node->modes.proxy;
  342. node->friend = db_node->modes.friend;
  343. node->relay.mode = db_node->modes.relay.state;
  344. node->relay.cnt = db_node->modes.relay.cnt;
  345. node->relay.interval = db_node->modes.relay.interval;
  346. node->beacon = db_node->modes.beacon;
  347. l_debug("relay %2.2x, proxy %2.2x, lpn %2.2x, friend %2.2x",
  348. node->relay.mode, node->proxy, node->lpn, node->friend);
  349. node->ttl = db_node->ttl;
  350. node->seq_number = db_node->seq_number;
  351. memcpy(node->dev_key, db_node->dev_key, 16);
  352. memcpy(node->token, db_node->token, 8);
  353. num_ele = l_queue_length(db_node->elements);
  354. if (num_ele > MAX_ELE_COUNT)
  355. goto fail;
  356. node->num_ele = num_ele;
  357. if (num_ele != 0 && !add_elements_from_storage(node, db_node))
  358. goto fail;
  359. node->primary = db_node->unicast;
  360. if (!db_node->netkeys)
  361. goto fail;
  362. if (!IS_UNASSIGNED(node->primary) &&
  363. !mesh_net_register_unicast(node->net, node->primary, num_ele))
  364. goto fail;
  365. mesh_net_set_iv_index(node->net, db_node->iv_index, db_node->iv_update);
  366. /* Initialize directory for storing keyring and RPL info */
  367. if (!init_storage_dir(node) || !mesh_net_load_rpl(node->net))
  368. goto fail;
  369. if (db_node->net_transmit)
  370. mesh_net_transmit_params_set(node->net,
  371. db_node->net_transmit->count,
  372. db_node->net_transmit->interval);
  373. l_queue_foreach(db_node->netkeys, set_net_key, node);
  374. l_queue_foreach(db_node->appkeys, set_appkey, node);
  375. while (l_queue_length(db_node->pages)) {
  376. struct mesh_config_comp_page *page;
  377. /* Move the composition pages to the node struct */
  378. page = l_queue_pop_head(db_node->pages);
  379. l_queue_push_tail(node->pages, page);
  380. }
  381. mesh_net_set_seq_num(node->net, node->seq_number);
  382. mesh_net_set_default_ttl(node->net, node->ttl);
  383. update_net_settings(node);
  384. /* Initialize configuration server model */
  385. cfgmod_server_init(node, PRIMARY_ELE_IDX);
  386. node->cfg = cfg;
  387. return true;
  388. fail:
  389. node_remove(node);
  390. return false;
  391. }
  392. static void cleanup_node(void *data)
  393. {
  394. struct mesh_node *node = data;
  395. uint32_t seq_num = mesh_net_get_seq_num(node->net);
  396. /* Preserve the last used sequence number */
  397. mesh_config_write_seq_number(node->cfg, seq_num, false);
  398. free_node_resources(node);
  399. }
  400. /*
  401. * This function is called to free resources and write the current
  402. * sequence numbers to the configuration file for each known node.
  403. */
  404. void node_cleanup_all(void)
  405. {
  406. l_queue_destroy(nodes, cleanup_node);
  407. l_dbus_unregister_interface(dbus_get_bus(), MESH_NODE_INTERFACE);
  408. l_dbus_unregister_interface(dbus_get_bus(), MESH_MANAGEMENT_INTERFACE);
  409. }
  410. bool node_is_provisioner(struct mesh_node *node)
  411. {
  412. return node->provisioner;
  413. }
  414. bool node_is_busy(struct mesh_node *node)
  415. {
  416. return node->busy;
  417. }
  418. void node_app_key_delete(struct mesh_node *node, uint16_t net_idx,
  419. uint16_t app_idx)
  420. {
  421. const struct l_queue_entry *entry;
  422. entry = l_queue_get_entries(node->elements);
  423. for (; entry; entry = entry->next) {
  424. struct node_element *ele = entry->data;
  425. mesh_model_app_key_delete(node, ele->idx, ele->models, app_idx);
  426. }
  427. }
  428. uint16_t node_get_primary(struct mesh_node *node)
  429. {
  430. if (!node)
  431. return UNASSIGNED_ADDRESS;
  432. else
  433. return node->primary;
  434. }
  435. const uint8_t *node_get_device_key(struct mesh_node *node)
  436. {
  437. if (!node)
  438. return NULL;
  439. else
  440. return node->dev_key;
  441. }
  442. void node_set_token(struct mesh_node *node, uint8_t token[8])
  443. {
  444. memcpy(node->token, token, 8);
  445. }
  446. const uint8_t *node_get_token(struct mesh_node *node)
  447. {
  448. if (!node)
  449. return NULL;
  450. else
  451. return node->token;
  452. }
  453. uint8_t node_get_num_elements(struct mesh_node *node)
  454. {
  455. return node->num_ele;
  456. }
  457. struct l_queue *node_get_element_models(struct mesh_node *node, uint8_t ele_idx)
  458. {
  459. struct node_element *ele;
  460. if (!node)
  461. return NULL;
  462. ele = l_queue_find(node->elements, match_element_idx,
  463. L_UINT_TO_PTR(ele_idx));
  464. if (!ele)
  465. return NULL;
  466. return ele->models;
  467. }
  468. uint8_t node_default_ttl_get(struct mesh_node *node)
  469. {
  470. if (!node)
  471. return TTL_MASK;
  472. return node->ttl;
  473. }
  474. bool node_default_ttl_set(struct mesh_node *node, uint8_t ttl)
  475. {
  476. bool res;
  477. if (!node)
  478. return false;
  479. res = mesh_config_write_ttl(node->cfg, ttl);
  480. if (res) {
  481. node->ttl = ttl;
  482. mesh_net_set_default_ttl(node->net, ttl);
  483. }
  484. return res;
  485. }
  486. bool node_set_sequence_number(struct mesh_node *node, uint32_t seq)
  487. {
  488. if (!node)
  489. return false;
  490. node->seq_number = seq;
  491. return mesh_config_write_seq_number(node->cfg, node->seq_number, true);
  492. }
  493. uint32_t node_get_sequence_number(struct mesh_node *node)
  494. {
  495. if (!node)
  496. return 0xffffffff;
  497. return node->seq_number;
  498. }
  499. int node_get_element_idx(struct mesh_node *node, uint16_t ele_addr)
  500. {
  501. uint16_t addr;
  502. uint8_t num_ele;
  503. if (!node)
  504. return -1;
  505. num_ele = node_get_num_elements(node);
  506. if (!num_ele)
  507. return -2;
  508. addr = node_get_primary(node);
  509. if (ele_addr < addr || ele_addr >= addr + num_ele)
  510. return -3;
  511. else
  512. return ele_addr - addr;
  513. }
  514. uint16_t node_get_crpl(struct mesh_node *node)
  515. {
  516. if (!node)
  517. return 0;
  518. return node->comp.crpl;
  519. }
  520. uint8_t node_relay_mode_get(struct mesh_node *node, uint8_t *count,
  521. uint16_t *interval)
  522. {
  523. if (!node) {
  524. *count = 0;
  525. *interval = 0;
  526. return MESH_MODE_DISABLED;
  527. }
  528. *count = node->relay.cnt;
  529. *interval = node->relay.interval;
  530. return node->relay.mode;
  531. }
  532. uint8_t node_lpn_mode_get(struct mesh_node *node)
  533. {
  534. if (!node)
  535. return MESH_MODE_DISABLED;
  536. return node->lpn;
  537. }
  538. bool node_relay_mode_set(struct mesh_node *node, bool enable, uint8_t cnt,
  539. uint16_t interval)
  540. {
  541. bool res;
  542. if (!node || node->relay.mode == MESH_MODE_UNSUPPORTED)
  543. return false;
  544. res = mesh_config_write_relay_mode(node->cfg, enable, cnt, interval);
  545. if (res) {
  546. node->relay.mode = enable ? MESH_MODE_ENABLED :
  547. MESH_MODE_DISABLED;
  548. node->relay.cnt = cnt;
  549. node->relay.interval = interval;
  550. mesh_net_set_relay_mode(node->net, enable, cnt, interval);
  551. }
  552. return res;
  553. }
  554. bool node_proxy_mode_set(struct mesh_node *node, bool enable)
  555. {
  556. bool res;
  557. uint8_t proxy;
  558. if (!node || node->proxy == MESH_MODE_UNSUPPORTED)
  559. return false;
  560. proxy = enable ? MESH_MODE_ENABLED : MESH_MODE_DISABLED;
  561. res = mesh_config_write_mode(node->cfg, "proxy", proxy);
  562. if (res) {
  563. node->proxy = proxy;
  564. mesh_net_set_proxy_mode(node->net, enable);
  565. }
  566. return res;
  567. }
  568. uint8_t node_proxy_mode_get(struct mesh_node *node)
  569. {
  570. if (!node)
  571. return MESH_MODE_DISABLED;
  572. return node->proxy;
  573. }
  574. bool node_beacon_mode_set(struct mesh_node *node, bool enable)
  575. {
  576. bool res;
  577. uint8_t beacon;
  578. if (!node)
  579. return false;
  580. beacon = enable ? MESH_MODE_ENABLED : MESH_MODE_DISABLED;
  581. res = mesh_config_write_mode(node->cfg, "beacon", beacon);
  582. if (res) {
  583. node->beacon = beacon;
  584. mesh_net_set_beacon_mode(node->net, enable);
  585. }
  586. return res;
  587. }
  588. uint8_t node_beacon_mode_get(struct mesh_node *node)
  589. {
  590. if (!node)
  591. return MESH_MODE_DISABLED;
  592. return node->beacon;
  593. }
  594. bool node_friend_mode_set(struct mesh_node *node, bool enable)
  595. {
  596. bool res;
  597. uint8_t friend;
  598. if (!node || node->friend == MESH_MODE_UNSUPPORTED)
  599. return false;
  600. friend = enable ? MESH_MODE_ENABLED : MESH_MODE_DISABLED;
  601. res = mesh_config_write_mode(node->cfg, "friend", friend);
  602. if (res) {
  603. node->friend = friend;
  604. mesh_net_set_friend_mode(node->net, enable);
  605. }
  606. return res;
  607. }
  608. uint8_t node_friend_mode_get(struct mesh_node *node)
  609. {
  610. if (!node)
  611. return MESH_MODE_DISABLED;
  612. return node->friend;
  613. }
  614. static uint16_t generate_node_comp(struct mesh_node *node, uint8_t *buf,
  615. uint16_t sz)
  616. {
  617. uint16_t n, features, num_ele = 0;
  618. const struct l_queue_entry *entry;
  619. n = 0;
  620. l_put_le16(node->comp.cid, buf + n);
  621. n += 2;
  622. l_put_le16(node->comp.pid, buf + n);
  623. n += 2;
  624. l_put_le16(node->comp.vid, buf + n);
  625. n += 2;
  626. l_put_le16(node->comp.crpl, buf + n);
  627. n += 2;
  628. features = 0;
  629. if (node->relay.mode != MESH_MODE_UNSUPPORTED)
  630. features |= FEATURE_RELAY;
  631. if (node->proxy != MESH_MODE_UNSUPPORTED)
  632. features |= FEATURE_PROXY;
  633. if (node->friend != MESH_MODE_UNSUPPORTED)
  634. features |= FEATURE_FRIEND;
  635. if (node->lpn != MESH_MODE_UNSUPPORTED)
  636. features |= FEATURE_LPN;
  637. l_put_le16(features, buf + n);
  638. n += 2;
  639. entry = l_queue_get_entries(node->elements);
  640. for (; entry; entry = entry->next) {
  641. struct node_element *ele = entry->data;
  642. if (ele->idx != num_ele)
  643. return 0;
  644. num_ele++;
  645. /* At least fit location and zeros for number of models */
  646. if ((n + 4) > sz)
  647. return n;
  648. l_put_le16(ele->location, buf + n);
  649. n += 2;
  650. n += mesh_model_generate_composition(ele->models, sz - n,
  651. buf + n);
  652. }
  653. if (!num_ele)
  654. return 0;
  655. return n;
  656. }
  657. static bool match_page(const void *a, const void *b)
  658. {
  659. const struct mesh_config_comp_page *page = a;
  660. uint8_t page_num = L_PTR_TO_UINT(b);
  661. return page->page_num == page_num;
  662. }
  663. static void convert_node_to_storage(struct mesh_node *node,
  664. struct mesh_config_node *db_node)
  665. {
  666. const struct l_queue_entry *entry;
  667. memset(db_node, 0, sizeof(struct mesh_config_node));
  668. db_node->cid = node->comp.cid;
  669. db_node->pid = node->comp.pid;
  670. db_node->vid = node->comp.vid;
  671. db_node->crpl = node->comp.crpl;
  672. db_node->modes.lpn = node->lpn;
  673. db_node->modes.proxy = node->proxy;
  674. db_node->modes.friend = node->friend;
  675. db_node->modes.relay.state = node->relay.mode;
  676. db_node->modes.relay.cnt = node->relay.cnt;
  677. db_node->modes.relay.interval = node->relay.interval;
  678. db_node->modes.beacon = node->beacon;
  679. db_node->ttl = node->ttl;
  680. db_node->seq_number = node->seq_number;
  681. db_node->elements = l_queue_new();
  682. entry = l_queue_get_entries(node->elements);
  683. for (; entry; entry = entry->next) {
  684. struct node_element *ele = entry->data;
  685. struct mesh_config_element *db_ele;
  686. db_ele = l_new(struct mesh_config_element, 1);
  687. db_ele->index = ele->idx;
  688. db_ele->location = ele->location;
  689. db_ele->models = l_queue_new();
  690. mesh_model_convert_to_storage(db_ele->models, ele->models);
  691. l_queue_push_tail(db_node->elements, db_ele);
  692. }
  693. }
  694. static bool create_node_config(struct mesh_node *node, const uint8_t uuid[16])
  695. {
  696. struct mesh_config_node db_node;
  697. const struct l_queue_entry *entry;
  698. const char *storage_dir;
  699. convert_node_to_storage(node, &db_node);
  700. storage_dir = mesh_get_storage_dir();
  701. node->cfg = mesh_config_create(storage_dir, uuid, &db_node);
  702. if (node->cfg)
  703. init_storage_dir(node);
  704. /* Free temporarily allocated resources */
  705. entry = l_queue_get_entries(db_node.elements);
  706. for (; entry; entry = entry->next) {
  707. struct mesh_config_element *db_ele = entry->data;
  708. l_queue_destroy(db_ele->models, l_free);
  709. }
  710. l_queue_destroy(db_node.elements, l_free);
  711. return node->cfg != NULL;
  712. }
  713. static bool set_node_comp(struct mesh_node *node, uint8_t page_num,
  714. const uint8_t *data, uint16_t len)
  715. {
  716. struct mesh_config_comp_page *page;
  717. if (len < MIN_COMP_SIZE)
  718. return false;
  719. page = l_queue_remove_if(node->pages, match_page,
  720. L_UINT_TO_PTR(page_num));
  721. l_free(page);
  722. page = l_malloc(sizeof(struct mesh_config_comp_page) + len);
  723. page->len = len;
  724. page->page_num = page_num;
  725. memcpy(page->data, data, len);
  726. l_queue_push_tail(node->pages, page);
  727. return mesh_config_comp_page_add(node->cfg, page_num, page->data, len);
  728. }
  729. static bool create_node_comp(struct mesh_node *node)
  730. {
  731. uint16_t len;
  732. uint8_t comp[MAX_MSG_LEN - 2];
  733. len = generate_node_comp(node, comp, sizeof(comp));
  734. return set_node_comp(node, 0, comp, len);
  735. }
  736. const uint8_t *node_get_comp(struct mesh_node *node, uint8_t page_num,
  737. uint16_t *len)
  738. {
  739. struct mesh_config_comp_page *page = NULL;
  740. if (node)
  741. page = l_queue_find(node->pages, match_page,
  742. L_UINT_TO_PTR(page_num));
  743. if (!page) {
  744. *len = 0;
  745. return NULL;
  746. }
  747. *len = page->len;
  748. return page->data;
  749. }
  750. bool node_replace_comp(struct mesh_node *node, uint8_t retire, uint8_t with)
  751. {
  752. struct mesh_config_comp_page *old_page, *keep;
  753. if (!node)
  754. return false;
  755. keep = l_queue_find(node->pages, match_page, L_UINT_TO_PTR(with));
  756. if (!keep)
  757. return false;
  758. old_page = l_queue_remove_if(node->pages, match_page,
  759. L_UINT_TO_PTR(retire));
  760. l_free(old_page);
  761. keep->page_num = retire;
  762. mesh_config_comp_page_mv(node->cfg, with, retire);
  763. return true;
  764. }
  765. static void attach_io(void *a, void *b)
  766. {
  767. struct mesh_node *node = a;
  768. struct mesh_io *io = b;
  769. if (node->net)
  770. mesh_net_attach(node->net, io);
  771. }
  772. /* Register callbacks for all nodes io */
  773. void node_attach_io_all(struct mesh_io *io)
  774. {
  775. l_queue_foreach(nodes, attach_io, io);
  776. }
  777. /* Register node object with D-Bus */
  778. static bool register_node_object(struct mesh_node *node)
  779. {
  780. char uuid[33];
  781. if (!hex2str(node->uuid, sizeof(node->uuid), uuid, sizeof(uuid)))
  782. return false;
  783. node->obj_path = l_strdup_printf(BLUEZ_MESH_PATH MESH_NODE_PATH_PREFIX
  784. "%s", uuid);
  785. if (!l_dbus_object_add_interface(dbus_get_bus(), node->obj_path,
  786. MESH_NODE_INTERFACE, node))
  787. return false;
  788. if (!l_dbus_object_add_interface(dbus_get_bus(), node->obj_path,
  789. MESH_MANAGEMENT_INTERFACE, node))
  790. return false;
  791. if (!l_dbus_object_add_interface(dbus_get_bus(), node->obj_path,
  792. L_DBUS_INTERFACE_PROPERTIES, NULL))
  793. return false;
  794. return true;
  795. }
  796. static void app_disc_cb(struct l_dbus *bus, void *user_data)
  797. {
  798. struct mesh_node *node = user_data;
  799. l_info("App %s disconnected (%u)", node->owner, node->disc_watch);
  800. node->disc_watch = 0;
  801. /* In case of a provisioner, stop active scanning */
  802. if (node->provisioner)
  803. manager_scan_cancel(node);
  804. free_node_dbus_resources(node);
  805. }
  806. static bool get_sig_models_from_properties(struct mesh_node *node,
  807. struct node_element *ele,
  808. struct l_dbus_message_iter *property)
  809. {
  810. struct l_dbus_message_iter mods, var;
  811. uint16_t m_id;
  812. if (!ele->models)
  813. ele->models = l_queue_new();
  814. if (!l_dbus_message_iter_get_variant(property, "a(qa{sv})", &mods))
  815. return false;
  816. /* Bluetooth SIG defined models */
  817. while (l_dbus_message_iter_next_entry(&mods, &m_id, &var)) {
  818. uint32_t id = SET_ID(SIG_VENDOR, m_id);
  819. /* Allow Config Server Model only on the primary element */
  820. if (ele->idx != PRIMARY_ELE_IDX && id == CONFIG_SRV_MODEL)
  821. return false;
  822. if (!mesh_model_add(node, ele->models, id, &var))
  823. return false;
  824. }
  825. return true;
  826. }
  827. static bool get_vendor_models_from_properties(struct mesh_node *node,
  828. struct node_element *ele,
  829. struct l_dbus_message_iter *property)
  830. {
  831. struct l_dbus_message_iter mods, var;
  832. uint16_t m_id, v_id;
  833. if (!ele->models)
  834. ele->models = l_queue_new();
  835. if (!l_dbus_message_iter_get_variant(property, "a(qqa{sv})", &mods))
  836. return false;
  837. /* Vendor defined models */
  838. while (l_dbus_message_iter_next_entry(&mods, &v_id, &m_id, &var)) {
  839. uint32_t id = SET_ID(v_id, m_id);
  840. if (!mesh_model_add(node, ele->models, id, &var))
  841. return false;
  842. }
  843. return true;
  844. }
  845. static bool get_element_properties(struct mesh_node *node, const char *path,
  846. struct l_dbus_message_iter *properties)
  847. {
  848. struct node_element *ele = l_new(struct node_element, 1);
  849. const char *key;
  850. struct l_dbus_message_iter var;
  851. bool idx = false;
  852. bool mods = false;
  853. bool vendor_mods = false;
  854. l_debug("path %s", path);
  855. ele->location = DEFAULT_LOCATION;
  856. while (l_dbus_message_iter_next_entry(properties, &key, &var)) {
  857. if (!strcmp(key, "Index")) {
  858. if (idx || !l_dbus_message_iter_get_variant(&var, "y",
  859. &ele->idx))
  860. goto fail;
  861. idx = true;
  862. } else if (!strcmp(key, "Models")) {
  863. if (mods)
  864. goto fail;
  865. if (!get_sig_models_from_properties(node, ele, &var))
  866. goto fail;
  867. mods = true;
  868. } else if (!strcmp(key, "VendorModels")) {
  869. if (vendor_mods)
  870. goto fail;
  871. if (!get_vendor_models_from_properties(node, ele, &var))
  872. goto fail;
  873. vendor_mods = true;
  874. } else if (!strcmp(key, "Location")) {
  875. if (!l_dbus_message_iter_get_variant(&var, "q",
  876. &ele->location))
  877. goto fail;
  878. }
  879. }
  880. /* Check for the presence of the required properties */
  881. if (!idx || !mods || !vendor_mods)
  882. goto fail;
  883. if (l_queue_find(node->elements, match_element_idx,
  884. L_UINT_TO_PTR(ele->idx)))
  885. goto fail;
  886. l_queue_insert(node->elements, ele, compare_element_idx, NULL);
  887. ele->path = l_strdup(path);
  888. /*
  889. * Add configuration server model on the primary element.
  890. * We allow the application not to specify the presense of
  891. * the Configuration Server model, since it's implemented by the
  892. * daemon. If the model is present in the application properties,
  893. * the operation below will be a "no-op".
  894. */
  895. if (ele->idx == PRIMARY_ELE_IDX)
  896. mesh_model_add(node, ele->models, CONFIG_SRV_MODEL, NULL);
  897. return true;
  898. fail:
  899. l_free(ele);
  900. return false;
  901. }
  902. static bool get_app_properties(struct mesh_node *node, const char *path,
  903. struct l_dbus_message_iter *properties)
  904. {
  905. const char *key;
  906. struct l_dbus_message_iter variant;
  907. bool cid = false;
  908. bool pid = false;
  909. bool vid = false;
  910. l_debug("path %s", path);
  911. node->comp.crpl = mesh_get_crpl();
  912. while (l_dbus_message_iter_next_entry(properties, &key, &variant)) {
  913. if (!cid && !strcmp(key, "CompanyID")) {
  914. if (!l_dbus_message_iter_get_variant(&variant, "q",
  915. &node->comp.cid))
  916. return false;
  917. cid = true;
  918. continue;
  919. }
  920. if (!pid && !strcmp(key, "ProductID")) {
  921. if (!l_dbus_message_iter_get_variant(&variant, "q",
  922. &node->comp.pid))
  923. return false;
  924. pid = true;
  925. continue;
  926. }
  927. if (!vid && !strcmp(key, "VersionID")) {
  928. if (!l_dbus_message_iter_get_variant(&variant, "q",
  929. &node->comp.vid))
  930. return false;
  931. vid = true;
  932. continue;
  933. }
  934. if (!strcmp(key, "CRPL")) {
  935. if (!l_dbus_message_iter_get_variant(&variant, "q",
  936. &node->comp.crpl))
  937. return false;
  938. continue;
  939. }
  940. }
  941. if (!cid || !pid || !vid)
  942. return false;
  943. return true;
  944. }
  945. static bool add_local_node(struct mesh_node *node, uint16_t unicast, bool kr,
  946. bool ivu, uint32_t iv_idx, uint8_t dev_key[16],
  947. uint16_t net_key_idx, uint8_t net_key[16])
  948. {
  949. if (!nodes)
  950. nodes = l_queue_new();
  951. l_queue_push_tail(nodes, node);
  952. if (!mesh_config_write_iv_index(node->cfg, iv_idx, ivu))
  953. return false;
  954. mesh_net_set_iv_index(node->net, iv_idx, ivu);
  955. if (!mesh_config_write_unicast(node->cfg, unicast))
  956. return false;
  957. l_getrandom(node->token, sizeof(node->token));
  958. if (!mesh_config_write_token(node->cfg, node->token))
  959. return false;
  960. memcpy(node->dev_key, dev_key, 16);
  961. if (!mesh_config_write_device_key(node->cfg, dev_key))
  962. return false;
  963. node->primary = unicast;
  964. mesh_net_register_unicast(node->net, unicast, node->num_ele);
  965. if (mesh_net_add_key(node->net, net_key_idx, net_key) !=
  966. MESH_STATUS_SUCCESS)
  967. return false;
  968. if (kr) {
  969. /* Duplicate net key, if the key refresh is on */
  970. if (mesh_net_update_key(node->net, net_key_idx, net_key) !=
  971. MESH_STATUS_SUCCESS)
  972. return false;
  973. if (!mesh_config_net_key_set_phase(node->cfg, net_key_idx,
  974. KEY_REFRESH_PHASE_TWO))
  975. return false;
  976. }
  977. update_net_settings(node);
  978. /* Initialize configuration server model */
  979. cfgmod_server_init(node, PRIMARY_ELE_IDX);
  980. node->busy = true;
  981. return true;
  982. }
  983. static void update_composition(struct mesh_node *node, struct mesh_node *attach)
  984. {
  985. if (node->comp.cid != attach->comp.cid)
  986. mesh_config_update_company_id(attach->cfg, node->comp.cid);
  987. if (node->comp.pid != attach->comp.pid)
  988. mesh_config_update_product_id(attach->cfg, node->comp.pid);
  989. if (node->comp.vid != attach->comp.vid)
  990. mesh_config_update_version_id(attach->cfg, node->comp.vid);
  991. if (node->comp.crpl != attach->comp.crpl)
  992. mesh_config_update_crpl(attach->cfg, node->comp.crpl);
  993. attach->comp = node->comp;
  994. }
  995. static void update_model_options(struct mesh_node *node,
  996. struct mesh_node *attach)
  997. {
  998. uint32_t len, i;
  999. struct node_element *ele, *ele_attach;
  1000. len = l_queue_length(node->elements);
  1001. for (i = 0; i < len; i++) {
  1002. ele = l_queue_find(node->elements, match_element_idx,
  1003. L_UINT_TO_PTR(i));
  1004. ele_attach = l_queue_find(attach->elements, match_element_idx,
  1005. L_UINT_TO_PTR(i));
  1006. if (!ele || !ele_attach)
  1007. continue;
  1008. mesh_model_update_opts(node, ele->idx, ele_attach->models,
  1009. ele->models);
  1010. }
  1011. }
  1012. static bool check_req_node(struct managed_obj_request *req)
  1013. {
  1014. const int offset = 8;
  1015. uint16_t node_len, len;
  1016. uint8_t comp[MAX_MSG_LEN - 2];
  1017. const uint8_t *node_comp;
  1018. len = generate_node_comp(req->node, comp, sizeof(comp));
  1019. if (len < MIN_COMP_SIZE)
  1020. return false;
  1021. node_comp = node_get_comp(req->attach, 0, &node_len);
  1022. /* If no page 0 exists, create it and accept */
  1023. if (!node_len || !node_comp)
  1024. return set_node_comp(req->attach, 0, comp, len);
  1025. /* Test Element/Model part of composition and reject if changed */
  1026. if (node_len != len || memcmp(&node_comp[offset], &comp[offset],
  1027. node_len - offset))
  1028. return false;
  1029. /* If comp has changed, but not Element/Models, resave and accept */
  1030. else if (memcmp(node_comp, comp, node_len))
  1031. return set_node_comp(req->attach, 0, comp, len);
  1032. /* Nothing has changed */
  1033. return true;
  1034. }
  1035. static bool attach_req_node(struct mesh_node *attach, struct mesh_node *node)
  1036. {
  1037. const struct l_queue_entry *attach_entry;
  1038. const struct l_queue_entry *node_entry;
  1039. attach->obj_path = node->obj_path;
  1040. node->obj_path = NULL;
  1041. if (!register_node_object(attach)) {
  1042. free_node_dbus_resources(attach);
  1043. return false;
  1044. }
  1045. attach_entry = l_queue_get_entries(attach->elements);
  1046. node_entry = l_queue_get_entries(node->elements);
  1047. /*
  1048. * Update existing node with paths collected in temporary node,
  1049. * then remove the temporary.
  1050. */
  1051. while (attach_entry && node_entry) {
  1052. struct node_element *attach_ele = attach_entry->data;
  1053. struct node_element *node_ele = node_entry->data;
  1054. attach_ele->path = node_ele->path;
  1055. node_ele->path = NULL;
  1056. attach_entry = attach_entry->next;
  1057. node_entry = node_entry->next;
  1058. }
  1059. mesh_agent_remove(attach->agent);
  1060. attach->agent = node->agent;
  1061. node->agent = NULL;
  1062. attach->provisioner = node->provisioner;
  1063. attach->app_path = node->app_path;
  1064. node->app_path = NULL;
  1065. attach->owner = node->owner;
  1066. node->owner = NULL;
  1067. update_composition(node, attach);
  1068. update_model_options(node, attach);
  1069. node_remove(node);
  1070. return true;
  1071. }
  1072. static void get_managed_objects_cb(struct l_dbus_message *msg, void *user_data)
  1073. {
  1074. struct l_dbus_message_iter objects, interfaces;
  1075. struct managed_obj_request *req = user_data;
  1076. const char *path;
  1077. struct mesh_node *node = req->node;
  1078. struct node_import *import;
  1079. bool have_app = false;
  1080. unsigned int num_ele;
  1081. struct keyring_net_key net_key;
  1082. uint8_t dev_key[16];
  1083. if (req->type == REQUEST_TYPE_ATTACH)
  1084. req->attach->busy = false;
  1085. if (!msg || l_dbus_message_is_error(msg)) {
  1086. l_error("Failed to get app's dbus objects");
  1087. goto fail;
  1088. }
  1089. if (!l_dbus_message_get_arguments(msg, "a{oa{sa{sv}}}", &objects)) {
  1090. l_error("Failed to parse app's dbus objects");
  1091. goto fail;
  1092. }
  1093. while (l_dbus_message_iter_next_entry(&objects, &path, &interfaces)) {
  1094. struct l_dbus_message_iter properties;
  1095. const char *interface;
  1096. while (l_dbus_message_iter_next_entry(&interfaces, &interface,
  1097. &properties)) {
  1098. bool res;
  1099. if (!strcmp(MESH_ELEMENT_INTERFACE, interface)) {
  1100. res = get_element_properties(node, path,
  1101. &properties);
  1102. if (!res)
  1103. goto fail;
  1104. } else if (!strcmp(MESH_APPLICATION_INTERFACE,
  1105. interface)) {
  1106. if (have_app)
  1107. goto fail;
  1108. req->node->app_path = l_strdup(path);
  1109. res = get_app_properties(node, path,
  1110. &properties);
  1111. if (!res)
  1112. goto fail;
  1113. have_app = true;
  1114. } else if (!strcmp(MESH_PROVISION_AGENT_INTERFACE,
  1115. interface)) {
  1116. const char *sender;
  1117. sender = l_dbus_message_get_sender(msg);
  1118. node->agent = mesh_agent_create(path, sender,
  1119. &properties);
  1120. if (!node->agent)
  1121. goto fail;
  1122. } else if (!strcmp(MESH_PROVISIONER_INTERFACE,
  1123. interface)) {
  1124. node->provisioner = true;
  1125. }
  1126. }
  1127. }
  1128. if (!have_app) {
  1129. l_error("Interface %s not found", MESH_APPLICATION_INTERFACE);
  1130. goto fail;
  1131. }
  1132. if (l_queue_isempty(node->elements)) {
  1133. l_error("Interface %s not found", MESH_ELEMENT_INTERFACE);
  1134. goto fail;
  1135. }
  1136. if (!l_queue_find(node->elements, match_element_idx,
  1137. L_UINT_TO_PTR(PRIMARY_ELE_IDX))) {
  1138. l_debug("Primary element not detected");
  1139. goto fail;
  1140. }
  1141. num_ele = l_queue_length(node->elements);
  1142. if (num_ele > MAX_ELE_COUNT)
  1143. goto fail;
  1144. node->num_ele = num_ele;
  1145. if (req->type != REQUEST_TYPE_ATTACH) {
  1146. /* Generate node configuration for a brand new node */
  1147. if (!create_node_config(node, node->uuid))
  1148. goto fail;
  1149. /* Create node composition */
  1150. if (!create_node_comp(node))
  1151. goto fail;
  1152. } else if (!check_req_node(req))
  1153. /* Check the integrity of the node composition */
  1154. goto fail;
  1155. switch (req->type) {
  1156. case REQUEST_TYPE_ATTACH:
  1157. if (!attach_req_node(req->attach, node))
  1158. goto fail;
  1159. req->attach->disc_watch = l_dbus_add_disconnect_watch(
  1160. dbus_get_bus(), req->attach->owner,
  1161. app_disc_cb, req->attach, NULL);
  1162. req->ready_cb(req->pending_msg, MESH_ERROR_NONE, req->attach);
  1163. return;
  1164. case REQUEST_TYPE_JOIN:
  1165. if (!node->agent) {
  1166. l_error("Interface %s not found",
  1167. MESH_PROVISION_AGENT_INTERFACE);
  1168. goto fail;
  1169. }
  1170. req->join_ready_cb(node, node->agent);
  1171. return;
  1172. case REQUEST_TYPE_IMPORT:
  1173. import = req->import;
  1174. if (!add_local_node(node, import->unicast, import->flags.kr,
  1175. import->flags.ivu,
  1176. import->iv_index, import->dev_key,
  1177. import->net_idx, import->net_key))
  1178. goto fail;
  1179. req->ready_cb(req->pending_msg, MESH_ERROR_NONE, node);
  1180. l_free(import);
  1181. return;
  1182. case REQUEST_TYPE_CREATE:
  1183. /* Generate device and primary network keys */
  1184. l_getrandom(dev_key, sizeof(dev_key));
  1185. l_getrandom(net_key.old_key, sizeof(net_key.old_key));
  1186. memcpy(net_key.new_key, net_key.old_key,
  1187. sizeof(net_key.old_key));
  1188. net_key.net_idx = PRIMARY_NET_IDX;
  1189. net_key.phase = KEY_REFRESH_PHASE_NONE;
  1190. if (!add_local_node(node, DEFAULT_NEW_UNICAST, false, false,
  1191. DEFAULT_IV_INDEX, dev_key,
  1192. PRIMARY_NET_IDX,
  1193. net_key.old_key))
  1194. goto fail;
  1195. if (!keyring_put_remote_dev_key(node, DEFAULT_NEW_UNICAST,
  1196. node->num_ele, dev_key))
  1197. goto fail;
  1198. if (!keyring_put_net_key(node, PRIMARY_NET_IDX, &net_key))
  1199. goto fail;
  1200. req->ready_cb(req->pending_msg, MESH_ERROR_NONE, node);
  1201. return;
  1202. default:
  1203. goto fail;
  1204. }
  1205. fail:
  1206. /* Handle failed requests */
  1207. node_remove(node);
  1208. if (req->type == REQUEST_TYPE_JOIN)
  1209. req->join_ready_cb(NULL, NULL);
  1210. else
  1211. req->ready_cb(req->pending_msg, MESH_ERROR_FAILED, NULL);
  1212. if (req->type == REQUEST_TYPE_IMPORT)
  1213. l_free(req->import);
  1214. }
  1215. static void send_managed_objects_request(const char *destination,
  1216. const char *path,
  1217. struct managed_obj_request *req)
  1218. {
  1219. struct l_dbus_message *msg;
  1220. msg = l_dbus_message_new_method_call(dbus_get_bus(), destination, path,
  1221. L_DBUS_INTERFACE_OBJECT_MANAGER,
  1222. "GetManagedObjects");
  1223. l_dbus_message_set_arguments(msg, "");
  1224. dbus_send_with_timeout(dbus_get_bus(), msg, get_managed_objects_cb,
  1225. req, l_free, DEFAULT_DBUS_TIMEOUT);
  1226. }
  1227. /* Establish relationship between application and mesh node */
  1228. void node_attach(const char *app_root, const char *sender, uint64_t token,
  1229. node_ready_func_t cb, void *user_data)
  1230. {
  1231. struct managed_obj_request *req;
  1232. struct mesh_node *node;
  1233. node = l_queue_find(nodes, match_token, (void *) &token);
  1234. if (!node) {
  1235. cb(user_data, MESH_ERROR_NOT_FOUND, NULL);
  1236. return;
  1237. }
  1238. /* Check if there is a pending request associated with this node */
  1239. if (node->busy) {
  1240. cb(user_data, MESH_ERROR_BUSY, NULL);
  1241. return;
  1242. }
  1243. /* Check if the node is already in use */
  1244. if (node->owner) {
  1245. l_warn("The node is already in use");
  1246. cb(user_data, MESH_ERROR_ALREADY_EXISTS, NULL);
  1247. return;
  1248. }
  1249. req = l_new(struct managed_obj_request, 1);
  1250. /*
  1251. * Create a temporary node to collect composition data from attaching
  1252. * application. Existing node is passed in req->attach.
  1253. */
  1254. req->node = node_new(node->uuid);
  1255. req->node->owner = l_strdup(sender);
  1256. req->ready_cb = cb;
  1257. req->pending_msg = user_data;
  1258. req->attach = node;
  1259. req->type = REQUEST_TYPE_ATTACH;
  1260. node->busy = true;
  1261. send_managed_objects_request(sender, app_root, req);
  1262. }
  1263. /* Create a temporary pre-provisioned node */
  1264. void node_join(const char *app_root, const char *sender, const uint8_t *uuid,
  1265. node_join_ready_func_t cb)
  1266. {
  1267. struct managed_obj_request *req;
  1268. l_debug("");
  1269. req = l_new(struct managed_obj_request, 1);
  1270. req->node = node_new(uuid);
  1271. req->join_ready_cb = cb;
  1272. req->type = REQUEST_TYPE_JOIN;
  1273. send_managed_objects_request(sender, app_root, req);
  1274. }
  1275. void node_import(const char *app_root, const char *sender, const uint8_t *uuid,
  1276. const uint8_t dev_key[16], const uint8_t net_key[16],
  1277. uint16_t net_idx, bool kr, bool ivu,
  1278. uint32_t iv_index, uint16_t unicast,
  1279. node_ready_func_t cb, void *user_data)
  1280. {
  1281. struct managed_obj_request *req;
  1282. l_debug("");
  1283. req = l_new(struct managed_obj_request, 1);
  1284. req->node = node_new(uuid);
  1285. req->ready_cb = cb;
  1286. req->pending_msg = user_data;
  1287. req->import = l_new(struct node_import, 1);
  1288. memcpy(req->import->dev_key, dev_key, 16);
  1289. memcpy(req->import->net_key, net_key, 16);
  1290. req->import->net_idx = net_idx;
  1291. req->import->flags.kr = kr;
  1292. req->import->flags.ivu = ivu;
  1293. req->import->iv_index = iv_index;
  1294. req->import->unicast = unicast;
  1295. req->type = REQUEST_TYPE_IMPORT;
  1296. send_managed_objects_request(sender, app_root, req);
  1297. }
  1298. void node_create(const char *app_root, const char *sender, const uint8_t *uuid,
  1299. node_ready_func_t cb, void *user_data)
  1300. {
  1301. struct managed_obj_request *req;
  1302. l_debug("");
  1303. req = l_new(struct managed_obj_request, 1);
  1304. req->node = node_new(uuid);
  1305. req->ready_cb = cb;
  1306. req->pending_msg = user_data;
  1307. req->type = REQUEST_TYPE_CREATE;
  1308. send_managed_objects_request(sender, app_root, req);
  1309. }
  1310. static void build_element_config(void *a, void *b)
  1311. {
  1312. struct node_element *ele = a;
  1313. struct l_dbus_message_builder *builder = b;
  1314. l_debug("Element %u", ele->idx);
  1315. l_dbus_message_builder_enter_struct(builder, "ya(qa{sv})");
  1316. /* Element index */
  1317. l_dbus_message_builder_append_basic(builder, 'y', &ele->idx);
  1318. l_dbus_message_builder_enter_array(builder, "(qa{sv})");
  1319. /* Iterate over models */
  1320. l_queue_foreach(ele->models, mesh_model_build_config, builder);
  1321. l_dbus_message_builder_leave_array(builder);
  1322. l_dbus_message_builder_leave_struct(builder);
  1323. }
  1324. void node_build_attach_reply(struct mesh_node *node,
  1325. struct l_dbus_message *reply)
  1326. {
  1327. struct l_dbus_message_builder *builder;
  1328. builder = l_dbus_message_builder_new(reply);
  1329. /* Node object path */
  1330. l_dbus_message_builder_append_basic(builder, 'o', node->obj_path);
  1331. /* Array of element configurations "a*/
  1332. l_dbus_message_builder_enter_array(builder, "(ya(qa{sv}))");
  1333. l_queue_foreach(node->elements, build_element_config, builder);
  1334. l_dbus_message_builder_leave_array(builder);
  1335. l_dbus_message_builder_finalize(builder);
  1336. l_dbus_message_builder_destroy(builder);
  1337. }
  1338. static bool parse_send_options(struct l_dbus_message_iter *itr,
  1339. struct send_options *opts)
  1340. {
  1341. const char *key;
  1342. struct l_dbus_message_iter var;
  1343. opts->segmented = false;
  1344. opts->vendor_id = SIG_VENDOR;
  1345. while (l_dbus_message_iter_next_entry(itr, &key, &var)) {
  1346. if (!strcmp(key, "ForceSegmented")) {
  1347. if (!l_dbus_message_iter_get_variant(&var, "b",
  1348. &opts->segmented))
  1349. return false;
  1350. }
  1351. if (!strcmp(key, "Vendor")) {
  1352. if (!l_dbus_message_iter_get_variant(&var, "q",
  1353. &opts->vendor_id))
  1354. return false;
  1355. }
  1356. }
  1357. return true;
  1358. }
  1359. static struct l_dbus_message *send_call(struct l_dbus *dbus,
  1360. struct l_dbus_message *msg,
  1361. void *user_data)
  1362. {
  1363. struct mesh_node *node = user_data;
  1364. const char *sender, *ele_path;
  1365. struct l_dbus_message_iter dict, iter_data;
  1366. struct send_options opts;
  1367. struct node_element *ele;
  1368. uint16_t dst, app_idx, net_idx, src;
  1369. uint8_t *data;
  1370. uint32_t len;
  1371. l_debug("Send");
  1372. sender = l_dbus_message_get_sender(msg);
  1373. if (strcmp(sender, node->owner))
  1374. return dbus_error(msg, MESH_ERROR_NOT_AUTHORIZED, NULL);
  1375. if (!l_dbus_message_get_arguments(msg, "oqqa{sv}ay", &ele_path, &dst,
  1376. &app_idx, &dict, &iter_data))
  1377. return dbus_error(msg, MESH_ERROR_INVALID_ARGS, NULL);
  1378. ele = l_queue_find(node->elements, match_element_path, ele_path);
  1379. if (!ele)
  1380. return dbus_error(msg, MESH_ERROR_NOT_FOUND,
  1381. "Element not found");
  1382. if (!parse_send_options(&dict, &opts))
  1383. return dbus_error(msg, MESH_ERROR_INVALID_ARGS, NULL);
  1384. src = node_get_primary(node) + ele->idx;
  1385. if (!l_dbus_message_iter_get_fixed_array(&iter_data, &data, &len) ||
  1386. !len || len > MAX_MSG_LEN)
  1387. return dbus_error(msg, MESH_ERROR_INVALID_ARGS,
  1388. "Incorrect data");
  1389. if (app_idx & ~APP_IDX_MASK)
  1390. return dbus_error(msg, MESH_ERROR_INVALID_ARGS,
  1391. "Invalid key index");
  1392. net_idx = appkey_net_idx(node_get_net(node), app_idx);
  1393. if (net_idx == NET_IDX_INVALID)
  1394. return dbus_error(msg, MESH_ERROR_INVALID_ARGS,
  1395. "Key not found");
  1396. if (!mesh_model_send(node, src, dst, app_idx, net_idx, DEFAULT_TTL,
  1397. opts.segmented, len, data))
  1398. return dbus_error(msg, MESH_ERROR_FAILED, NULL);
  1399. return l_dbus_message_new_method_return(msg);
  1400. }
  1401. static struct l_dbus_message *dev_key_send_call(struct l_dbus *dbus,
  1402. struct l_dbus_message *msg,
  1403. void *user_data)
  1404. {
  1405. struct mesh_node *node = user_data;
  1406. const char *sender, *ele_path;
  1407. struct l_dbus_message_iter iter_data, dict;
  1408. struct send_options opts;
  1409. struct node_element *ele;
  1410. uint16_t dst, app_idx, net_idx, src;
  1411. bool remote;
  1412. uint8_t *data;
  1413. uint32_t len;
  1414. l_debug("DevKeySend");
  1415. sender = l_dbus_message_get_sender(msg);
  1416. if (strcmp(sender, node->owner))
  1417. return dbus_error(msg, MESH_ERROR_NOT_AUTHORIZED, NULL);
  1418. if (!l_dbus_message_get_arguments(msg, "oqbqa{sv}ay", &ele_path, &dst,
  1419. &remote, &net_idx, &dict, &iter_data))
  1420. return dbus_error(msg, MESH_ERROR_INVALID_ARGS, NULL);
  1421. /* Loopbacks to local servers must use *remote* addressing */
  1422. if (!remote && mesh_net_is_local_address(node->net, dst, 1))
  1423. return dbus_error(msg, MESH_ERROR_INVALID_ARGS, NULL);
  1424. ele = l_queue_find(node->elements, match_element_path, ele_path);
  1425. if (!ele)
  1426. return dbus_error(msg, MESH_ERROR_NOT_FOUND,
  1427. "Element not found");
  1428. if (!parse_send_options(&dict, &opts))
  1429. return dbus_error(msg, MESH_ERROR_INVALID_ARGS, NULL);
  1430. src = node_get_primary(node) + ele->idx;
  1431. if (!l_dbus_message_iter_get_fixed_array(&iter_data, &data, &len) ||
  1432. !len || len > MAX_MSG_LEN)
  1433. return dbus_error(msg, MESH_ERROR_INVALID_ARGS,
  1434. "Incorrect data");
  1435. app_idx = remote ? APP_IDX_DEV_REMOTE : APP_IDX_DEV_LOCAL;
  1436. if (!mesh_model_send(node, src, dst, app_idx, net_idx, DEFAULT_TTL,
  1437. opts.segmented, len, data))
  1438. return dbus_error(msg, MESH_ERROR_NOT_FOUND, NULL);
  1439. return l_dbus_message_new_method_return(msg);
  1440. }
  1441. static struct l_dbus_message *add_netkey_call(struct l_dbus *dbus,
  1442. struct l_dbus_message *msg,
  1443. void *user_data)
  1444. {
  1445. struct mesh_node *node = user_data;
  1446. const char *sender, *ele_path;
  1447. struct node_element *ele;
  1448. uint16_t dst, sub_idx, net_idx, src;
  1449. bool update;
  1450. struct keyring_net_key key;
  1451. uint8_t data[20];
  1452. l_debug("AddNetKey");
  1453. sender = l_dbus_message_get_sender(msg);
  1454. if (strcmp(sender, node->owner))
  1455. return dbus_error(msg, MESH_ERROR_NOT_AUTHORIZED, NULL);
  1456. if (!l_dbus_message_get_arguments(msg, "oqqqb", &ele_path, &dst,
  1457. &sub_idx, &net_idx, &update))
  1458. return dbus_error(msg, MESH_ERROR_INVALID_ARGS, NULL);
  1459. ele = l_queue_find(node->elements, match_element_path, ele_path);
  1460. if (!ele)
  1461. return dbus_error(msg, MESH_ERROR_NOT_FOUND,
  1462. "Element not found");
  1463. src = node_get_primary(node) + ele->idx;
  1464. if (!keyring_get_net_key(node, sub_idx, &key))
  1465. return dbus_error(msg, MESH_ERROR_NOT_FOUND,
  1466. "NetKey not found");
  1467. if (!update) {
  1468. l_put_be16(OP_NETKEY_ADD, data);
  1469. if (key.phase != KEY_REFRESH_PHASE_TWO)
  1470. memcpy(data + 4, key.old_key, 16);
  1471. else
  1472. memcpy(data + 4, key.new_key, 16);
  1473. } else {
  1474. if (key.phase != KEY_REFRESH_PHASE_ONE)
  1475. return dbus_error(msg, MESH_ERROR_FAILED,
  1476. "Cannot update");
  1477. l_put_be16(OP_NETKEY_UPDATE, data);
  1478. memcpy(data + 4, key.new_key, 16);
  1479. }
  1480. l_put_le16(sub_idx, &data[2]);
  1481. if (!mesh_model_send(node, src, dst, APP_IDX_DEV_REMOTE, net_idx,
  1482. DEFAULT_TTL, false, 20, data))
  1483. return dbus_error(msg, MESH_ERROR_NOT_FOUND, NULL);
  1484. return l_dbus_message_new_method_return(msg);
  1485. }
  1486. static struct l_dbus_message *add_appkey_call(struct l_dbus *dbus,
  1487. struct l_dbus_message *msg,
  1488. void *user_data)
  1489. {
  1490. struct mesh_node *node = user_data;
  1491. const char *sender, *ele_path;
  1492. struct node_element *ele;
  1493. uint16_t dst, app_idx, net_idx, src;
  1494. bool update;
  1495. struct keyring_net_key net_key;
  1496. struct keyring_app_key app_key;
  1497. uint8_t data[20];
  1498. l_debug("AddAppKey");
  1499. sender = l_dbus_message_get_sender(msg);
  1500. if (strcmp(sender, node->owner))
  1501. return dbus_error(msg, MESH_ERROR_NOT_AUTHORIZED, NULL);
  1502. if (!l_dbus_message_get_arguments(msg, "oqqqb", &ele_path, &dst,
  1503. &app_idx, &net_idx, &update))
  1504. return dbus_error(msg, MESH_ERROR_INVALID_ARGS, NULL);
  1505. ele = l_queue_find(node->elements, match_element_path, ele_path);
  1506. if (!ele)
  1507. return dbus_error(msg, MESH_ERROR_NOT_FOUND,
  1508. "Element not found");
  1509. src = node_get_primary(node) + ele->idx;
  1510. if (!keyring_get_app_key(node, app_idx, &app_key))
  1511. return dbus_error(msg, MESH_ERROR_NOT_FOUND,
  1512. "AppKey not found");
  1513. if (!keyring_get_net_key(node, app_key.net_idx, &net_key)) {
  1514. return dbus_error(msg, MESH_ERROR_NOT_FOUND,
  1515. "Bound NetKey not found");
  1516. }
  1517. if (!update) {
  1518. data[0] = OP_APPKEY_ADD;
  1519. if (net_key.phase != KEY_REFRESH_PHASE_TWO)
  1520. memcpy(data + 4, app_key.old_key, 16);
  1521. else
  1522. memcpy(data + 4, app_key.new_key, 16);
  1523. } else {
  1524. if (net_key.phase != KEY_REFRESH_PHASE_ONE)
  1525. return dbus_error(msg, MESH_ERROR_FAILED,
  1526. "Cannot update");
  1527. data[0] = OP_APPKEY_UPDATE;
  1528. memcpy(data + 4, app_key.new_key, 16);
  1529. }
  1530. /* Pack bound NetKey and AppKey into 3 octets */
  1531. data[1] = app_key.net_idx;
  1532. data[2] = ((app_key.net_idx >> 8) & 0xf) | ((app_idx << 4) & 0xf0);
  1533. data[3] = app_idx >> 4;
  1534. if (!mesh_model_send(node, src, dst, APP_IDX_DEV_REMOTE, net_idx,
  1535. DEFAULT_TTL, false, 20, data))
  1536. return dbus_error(msg, MESH_ERROR_NOT_FOUND, NULL);
  1537. return l_dbus_message_new_method_return(msg);
  1538. }
  1539. static struct l_dbus_message *publish_call(struct l_dbus *dbus,
  1540. struct l_dbus_message *msg,
  1541. void *user_data)
  1542. {
  1543. struct mesh_node *node = user_data;
  1544. const char *sender, *ele_path;
  1545. struct l_dbus_message_iter iter_data, dict;
  1546. uint16_t mod_id, src;
  1547. struct send_options opts;
  1548. struct node_element *ele;
  1549. uint8_t *data;
  1550. uint32_t len, id;
  1551. int result;
  1552. l_debug("Publish");
  1553. sender = l_dbus_message_get_sender(msg);
  1554. if (strcmp(sender, node->owner))
  1555. return dbus_error(msg, MESH_ERROR_NOT_AUTHORIZED, NULL);
  1556. if (!l_dbus_message_get_arguments(msg, "oqa{sv}ay", &ele_path, &mod_id,
  1557. &dict, &iter_data))
  1558. return dbus_error(msg, MESH_ERROR_INVALID_ARGS, NULL);
  1559. ele = l_queue_find(node->elements, match_element_path, ele_path);
  1560. if (!ele)
  1561. return dbus_error(msg, MESH_ERROR_NOT_FOUND,
  1562. "Element not found");
  1563. if (!parse_send_options(&dict, &opts))
  1564. return dbus_error(msg, MESH_ERROR_INVALID_ARGS, NULL);
  1565. src = node_get_primary(node) + ele->idx;
  1566. if (!l_dbus_message_iter_get_fixed_array(&iter_data, &data, &len) ||
  1567. !len || len > MAX_MSG_LEN)
  1568. return dbus_error(msg, MESH_ERROR_INVALID_ARGS,
  1569. "Incorrect data");
  1570. id = SET_ID(opts.vendor_id, mod_id);
  1571. result = mesh_model_publish(node, id, src, opts.segmented, len, data);
  1572. if (result != MESH_ERROR_NONE)
  1573. return dbus_error(msg, result, NULL);
  1574. return l_dbus_message_new_method_return(msg);
  1575. }
  1576. static bool features_getter(struct l_dbus *dbus, struct l_dbus_message *msg,
  1577. struct l_dbus_message_builder *builder,
  1578. void *user_data)
  1579. {
  1580. struct mesh_node *node = user_data;
  1581. uint8_t friend = node_friend_mode_get(node);
  1582. uint8_t lpn = node_lpn_mode_get(node);
  1583. uint8_t proxy = node_proxy_mode_get(node);
  1584. uint8_t count;
  1585. uint16_t interval;
  1586. uint8_t relay = node_relay_mode_get(node, &count, &interval);
  1587. l_dbus_message_builder_enter_array(builder, "{sv}");
  1588. if (friend != MESH_MODE_UNSUPPORTED)
  1589. dbus_append_dict_entry_basic(builder, "Friend", "b", &friend);
  1590. if (lpn != MESH_MODE_UNSUPPORTED)
  1591. dbus_append_dict_entry_basic(builder, "LowPower", "b", &lpn);
  1592. if (proxy != MESH_MODE_UNSUPPORTED)
  1593. dbus_append_dict_entry_basic(builder, "Proxy", "b", &proxy);
  1594. if (relay != MESH_MODE_UNSUPPORTED)
  1595. dbus_append_dict_entry_basic(builder, "Relay", "b", &relay);
  1596. l_dbus_message_builder_leave_array(builder);
  1597. return true;
  1598. }
  1599. static bool beacon_getter(struct l_dbus *dbus, struct l_dbus_message *msg,
  1600. struct l_dbus_message_builder *builder,
  1601. void *user_data)
  1602. {
  1603. struct mesh_node *node = user_data;
  1604. bool beacon_mode = node_beacon_mode_get(node) == MESH_MODE_ENABLED;
  1605. l_dbus_message_builder_append_basic(builder, 'b', &beacon_mode);
  1606. return true;
  1607. }
  1608. static bool ivupdate_getter(struct l_dbus *dbus, struct l_dbus_message *msg,
  1609. struct l_dbus_message_builder *builder,
  1610. void *user_data)
  1611. {
  1612. struct mesh_node *node = user_data;
  1613. struct mesh_net *net = node_get_net(node);
  1614. uint8_t flags;
  1615. uint32_t iv_index;
  1616. bool ivu;
  1617. mesh_net_get_snb_state(net, &flags, &iv_index);
  1618. ivu = flags & IV_INDEX_UPDATE;
  1619. l_dbus_message_builder_append_basic(builder, 'b', &ivu);
  1620. return true;
  1621. }
  1622. static bool ivindex_getter(struct l_dbus *dbus, struct l_dbus_message *msg,
  1623. struct l_dbus_message_builder *builder,
  1624. void *user_data)
  1625. {
  1626. struct mesh_node *node = user_data;
  1627. struct mesh_net *net = node_get_net(node);
  1628. uint8_t flags;
  1629. uint32_t iv_index;
  1630. mesh_net_get_snb_state(net, &flags, &iv_index);
  1631. l_dbus_message_builder_append_basic(builder, 'u', &iv_index);
  1632. return true;
  1633. }
  1634. static bool seq_num_getter(struct l_dbus *dbus, struct l_dbus_message *msg,
  1635. struct l_dbus_message_builder *builder,
  1636. void *user_data)
  1637. {
  1638. struct mesh_node *node = user_data;
  1639. struct mesh_net *net = node_get_net(node);
  1640. uint32_t seq_nr = mesh_net_get_seq_num(net);
  1641. l_dbus_message_builder_append_basic(builder, 'u', &seq_nr);
  1642. return true;
  1643. }
  1644. static bool lastheard_getter(struct l_dbus *dbus, struct l_dbus_message *msg,
  1645. struct l_dbus_message_builder *builder,
  1646. void *user_data)
  1647. {
  1648. struct mesh_node *node = user_data;
  1649. struct mesh_net *net = node_get_net(node);
  1650. struct timeval now;
  1651. uint32_t last_heard;
  1652. gettimeofday(&now, NULL);
  1653. last_heard = now.tv_sec - mesh_net_get_instant(net);
  1654. l_dbus_message_builder_append_basic(builder, 'u', &last_heard);
  1655. return true;
  1656. }
  1657. static bool addresses_getter(struct l_dbus *dbus, struct l_dbus_message *msg,
  1658. struct l_dbus_message_builder *builder,
  1659. void *user_data)
  1660. {
  1661. struct mesh_node *node = user_data;
  1662. const struct l_queue_entry *entry;
  1663. l_dbus_message_builder_enter_array(builder, "q");
  1664. entry = l_queue_get_entries(node->elements);
  1665. for (; entry; entry = entry->next) {
  1666. const struct node_element *ele = entry->data;
  1667. uint16_t address = node->primary + ele->idx;
  1668. l_dbus_message_builder_append_basic(builder, 'q', &address);
  1669. }
  1670. l_dbus_message_builder_leave_array(builder);
  1671. return true;
  1672. }
  1673. static void setup_node_interface(struct l_dbus_interface *iface)
  1674. {
  1675. l_dbus_interface_method(iface, "Send", 0, send_call, "", "oqqa{sv}ay",
  1676. "element_path", "destination",
  1677. "key_index", "options", "data");
  1678. l_dbus_interface_method(iface, "DevKeySend", 0, dev_key_send_call, "",
  1679. "oqbqa{sv}ay", "element_path",
  1680. "destination", "remote",
  1681. "net_index", "options", "data");
  1682. l_dbus_interface_method(iface, "AddNetKey", 0, add_netkey_call, "",
  1683. "oqqqb", "element_path", "destination",
  1684. "subnet_index", "net_index", "update");
  1685. l_dbus_interface_method(iface, "AddAppKey", 0, add_appkey_call, "",
  1686. "oqqqb", "element_path", "destination",
  1687. "app_index", "net_index", "update");
  1688. l_dbus_interface_method(iface, "Publish", 0, publish_call, "",
  1689. "oqa{sv}ay", "element_path", "model_id",
  1690. "options", "data");
  1691. l_dbus_interface_property(iface, "Features", 0, "a{sv}",
  1692. features_getter, NULL);
  1693. l_dbus_interface_property(iface, "Beacon", 0, "b", beacon_getter, NULL);
  1694. l_dbus_interface_property(iface, "IvUpdate", 0, "b", ivupdate_getter,
  1695. NULL);
  1696. l_dbus_interface_property(iface, "IvIndex", 0, "u", ivindex_getter,
  1697. NULL);
  1698. l_dbus_interface_property(iface, "SequenceNumber", 0, "u",
  1699. seq_num_getter, NULL);
  1700. l_dbus_interface_property(iface, "SecondsSinceLastHeard", 0, "u",
  1701. lastheard_getter, NULL);
  1702. l_dbus_interface_property(iface, "Addresses", 0, "aq", addresses_getter,
  1703. NULL);
  1704. }
  1705. void node_property_changed(struct mesh_node *node, const char *property)
  1706. {
  1707. struct l_dbus *bus = dbus_get_bus();
  1708. if (bus && node->obj_path)
  1709. l_dbus_property_changed(dbus_get_bus(), node->obj_path,
  1710. MESH_NODE_INTERFACE, property);
  1711. }
  1712. bool node_dbus_init(struct l_dbus *bus)
  1713. {
  1714. if (!l_dbus_register_interface(bus, MESH_NODE_INTERFACE,
  1715. setup_node_interface,
  1716. NULL, false)) {
  1717. l_info("Unable to register %s interface", MESH_NODE_INTERFACE);
  1718. return false;
  1719. }
  1720. return true;
  1721. }
  1722. const char *node_get_owner(struct mesh_node *node)
  1723. {
  1724. return node->owner;
  1725. }
  1726. const char *node_get_element_path(struct mesh_node *node, uint8_t ele_idx)
  1727. {
  1728. struct node_element *ele;
  1729. ele = l_queue_find(node->elements, match_element_idx,
  1730. L_UINT_TO_PTR(ele_idx));
  1731. if (!ele)
  1732. return NULL;
  1733. return ele->path;
  1734. }
  1735. bool node_add_pending_local(struct mesh_node *node, void *prov_node_info)
  1736. {
  1737. struct mesh_prov_node_info *info = prov_node_info;
  1738. bool kr = !!(info->flags & PROV_FLAG_KR);
  1739. bool ivu = !!(info->flags & PROV_FLAG_IVU);
  1740. return add_local_node(node, info->unicast, kr, ivu, info->iv_index,
  1741. info->device_key, info->net_index, info->net_key);
  1742. }
  1743. struct mesh_config *node_config_get(struct mesh_node *node)
  1744. {
  1745. return node->cfg;
  1746. }
  1747. const char *node_get_storage_dir(struct mesh_node *node)
  1748. {
  1749. return node->storage_dir;
  1750. }
  1751. const char *node_get_app_path(struct mesh_node *node)
  1752. {
  1753. if (!node)
  1754. return NULL;
  1755. return node->app_path;
  1756. }
  1757. struct mesh_net *node_get_net(struct mesh_node *node)
  1758. {
  1759. return node->net;
  1760. }
  1761. struct mesh_agent *node_get_agent(struct mesh_node *node)
  1762. {
  1763. return node->agent;
  1764. }
  1765. bool node_load_from_storage(const char *storage_dir)
  1766. {
  1767. return mesh_config_load_nodes(storage_dir, init_from_storage, NULL);
  1768. }
  1769. /*
  1770. * This is called for a new node that:
  1771. * - has been created as a result of successful completion of Join()
  1772. * or Create() or Import() methods
  1773. * and
  1774. * - has been confirmed via successful token delivery to the application
  1775. *
  1776. * After a node has been created, the information gathered during initial
  1777. * GetManagedObjects() call is cleared. The subsequent call to Attach() would
  1778. * verify node's integrity and re-initialize node's D-Bus resources.
  1779. */
  1780. void node_finalize_new_node(struct mesh_node *node, struct mesh_io *io)
  1781. {
  1782. if (!node)
  1783. return;
  1784. free_node_dbus_resources(node);
  1785. mesh_agent_remove(node->agent);
  1786. node->agent = NULL;
  1787. node->busy = false;
  1788. /* Register callback for the node's io */
  1789. attach_io(node, io);
  1790. }