prov-db.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682
  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. #include <errno.h>
  14. #include <fcntl.h>
  15. #include <glib.h>
  16. #include <stdbool.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <unistd.h>
  21. #include <json-c/json.h>
  22. #include <sys/stat.h>
  23. #include <glib.h>
  24. #include "src/shared/util.h"
  25. #include "src/shared/shell.h"
  26. #include "tools/mesh-gatt/mesh-net.h"
  27. #include "tools/mesh-gatt/crypto.h"
  28. #include "tools/mesh-gatt/keys.h"
  29. #include "tools/mesh-gatt/net.h"
  30. #include "tools/mesh-gatt/node.h"
  31. #include "tools/mesh-gatt/util.h"
  32. #include "tools/mesh-gatt/prov-db.h"
  33. #define CHECK_KEY_IDX_RANGE(x) (((x) >= 0) && ((x) <= 4095))
  34. static const char *prov_filename;
  35. static const char *local_filename;
  36. static char* prov_file_read(const char *filename)
  37. {
  38. int fd;
  39. char *str;
  40. struct stat st;
  41. ssize_t sz;
  42. if (!filename)
  43. return NULL;
  44. fd = open(filename,O_RDONLY);
  45. if (!fd)
  46. return NULL;
  47. if (fstat(fd, &st) == -1) {
  48. close(fd);
  49. return NULL;
  50. }
  51. str = (char *) g_malloc0(st.st_size + 1);
  52. if (!str) {
  53. close(fd);
  54. return NULL;
  55. }
  56. sz = read(fd, str, st.st_size);
  57. if (sz != st.st_size)
  58. bt_shell_printf("Incomplete read: %d vs %d\n", (int)sz,
  59. (int)(st.st_size));
  60. close(fd);
  61. return str;
  62. }
  63. static void prov_file_write(json_object *jmain, bool local)
  64. {
  65. FILE *outfile;
  66. const char *out_str;
  67. const char *out_filename;
  68. if (local)
  69. out_filename = local_filename;
  70. else
  71. out_filename = prov_filename;
  72. outfile = fopen(out_filename, "wr");
  73. if (!outfile) {
  74. bt_shell_printf("Failed to open file %s for writing\n", out_filename);
  75. return;
  76. }
  77. out_str = json_object_to_json_string_ext(jmain,
  78. JSON_C_TO_STRING_PRETTY);
  79. fwrite(out_str, sizeof(char), strlen(out_str), outfile);
  80. fclose(outfile);
  81. }
  82. static void put_uint16(json_object *jobject, const char *desc, uint16_t value)
  83. {
  84. json_object *jstring;
  85. char buf[5];
  86. snprintf(buf, 5, "%4.4x", value);
  87. jstring = json_object_new_string(buf);
  88. json_object_object_add(jobject, desc, jstring);
  89. }
  90. static void put_uint32(json_object *jobject, const char *desc, uint32_t value)
  91. {
  92. json_object *jstring;
  93. char buf[9];
  94. snprintf(buf, 9, "%8.8x", value);
  95. jstring = json_object_new_string(buf);
  96. json_object_object_add(jobject, desc, jstring);
  97. }
  98. static void put_uint16_array_entry(json_object *jarray, uint16_t value)
  99. {
  100. json_object *jstring;
  101. char buf[5];
  102. snprintf(buf, 5, "%4.4x", value);
  103. jstring = json_object_new_string(buf);
  104. json_object_array_add(jarray, jstring);
  105. }
  106. static void put_uint32_array_entry(json_object *jarray, uint32_t value)
  107. {
  108. json_object *jstring;
  109. char buf[9];
  110. snprintf(buf, 9, "%8.8x", value);
  111. jstring = json_object_new_string(buf);
  112. json_object_array_add(jarray, jstring);
  113. }
  114. static void put_uint16_list(json_object *jarray, GList *list)
  115. {
  116. GList *l;
  117. if (!list)
  118. return;
  119. for (l = list; l; l = l->next) {
  120. uint32_t ivalue = GPOINTER_TO_UINT(l->data);
  121. put_uint16_array_entry(jarray, ivalue);
  122. }
  123. }
  124. static void add_node_idxs(json_object *jnode, const char *desc,
  125. GList *idxs)
  126. {
  127. json_object *jarray;
  128. jarray = json_object_new_array();
  129. put_uint16_list(jarray, idxs);
  130. json_object_object_add(jnode, desc, jarray);
  131. }
  132. static bool parse_unicast_range(json_object *jobject)
  133. {
  134. int cnt;
  135. int i;
  136. cnt = json_object_array_length(jobject);
  137. for (i = 0; i < cnt; ++i) {
  138. json_object *jrange;
  139. json_object *jvalue;
  140. uint16_t low, high;
  141. char *str;
  142. jrange = json_object_array_get_idx(jobject, i);
  143. json_object_object_get_ex(jrange, "lowAddress", &jvalue);
  144. str = (char *)json_object_get_string(jvalue);
  145. if (sscanf(str, "%04hx", &low) != 1)
  146. return false;
  147. json_object_object_get_ex(jrange, "highAddress", &jvalue);
  148. str = (char *)json_object_get_string(jvalue);
  149. if (sscanf(str, "%04hx", &high) != 1)
  150. return false;
  151. if(high < low)
  152. return false;
  153. net_add_address_pool(low, high);
  154. }
  155. return true;
  156. }
  157. static int parse_node_keys(struct mesh_node *node, json_object *jidxs,
  158. bool is_app_key)
  159. {
  160. int idx_cnt;
  161. int i;
  162. idx_cnt = json_object_array_length(jidxs);
  163. for (i = 0; i < idx_cnt; ++i) {
  164. int idx;
  165. json_object *jvalue;
  166. jvalue = json_object_array_get_idx(jidxs, i);
  167. if (!jvalue)
  168. break;
  169. idx = json_object_get_int(jvalue);
  170. if (!CHECK_KEY_IDX_RANGE(idx))
  171. break;
  172. if (is_app_key)
  173. node_app_key_add(node, idx);
  174. else
  175. node_net_key_add(node, idx);
  176. }
  177. return i;
  178. }
  179. static bool parse_composition_models(struct mesh_node *node, int index,
  180. json_object *jmodels)
  181. {
  182. int model_cnt;
  183. int i;
  184. model_cnt = json_object_array_length(jmodels);
  185. for (i = 0; i < model_cnt; ++i) {
  186. json_object *jmodel;
  187. char *str;
  188. uint32_t model_id;
  189. int len;
  190. jmodel = json_object_array_get_idx(jmodels, i);
  191. str = (char *)json_object_get_string(jmodel);
  192. len = strlen(str);
  193. if (len != 4 && len != 8)
  194. return false;
  195. if (sscanf(str, "%08x", &model_id) != 1)
  196. return false;
  197. if (len == 4)
  198. model_id += 0xffff0000;
  199. node_set_model(node, index, model_id);
  200. }
  201. return true;
  202. }
  203. static bool parse_composition_elements(struct mesh_node *node,
  204. json_object *jelements)
  205. {
  206. int el_cnt;
  207. int i;
  208. el_cnt = json_object_array_length(jelements);
  209. node_set_num_elements(node, el_cnt);
  210. for (i = 0; i < el_cnt; ++i) {
  211. json_object *jelement;
  212. json_object *jmodels;
  213. json_object *jvalue;
  214. int index;
  215. jelement = json_object_array_get_idx(jelements, i);
  216. json_object_object_get_ex(jelement, "elementIndex", &jvalue);
  217. if (jvalue) {
  218. index = json_object_get_int(jvalue);
  219. if (index >= el_cnt) {
  220. return false;
  221. }
  222. } else
  223. return false;
  224. if (!node_set_element(node, index))
  225. return false;
  226. json_object_object_get_ex(jelement, "models", &jmodels);
  227. if (!jmodels)
  228. continue;
  229. if(!parse_composition_models(node, index, jmodels))
  230. return false;
  231. }
  232. return true;
  233. }
  234. static bool parse_model_pub(struct mesh_node *node, int ele_idx,
  235. uint32_t model_id, json_object *jpub)
  236. {
  237. json_object *jvalue;
  238. struct mesh_publication pub;
  239. char *str;
  240. memset(&pub, 0, sizeof(struct mesh_publication));
  241. /* Read only required fields */
  242. json_object_object_get_ex(jpub, "address", &jvalue);
  243. if (!jvalue)
  244. return false;
  245. str = (char *)json_object_get_string(jvalue);
  246. if (sscanf(str, "%04hx", &pub.u.addr16) != 1)
  247. return false;
  248. json_object_object_get_ex(jpub, "index", &jvalue);
  249. if (!jvalue)
  250. return false;
  251. str = (char *)json_object_get_string(jvalue);
  252. if (sscanf(str, "%04hx", &pub.app_idx) != 1)
  253. return false;
  254. json_object_object_get_ex(jpub, "ttl", &jvalue);
  255. pub.ttl = json_object_get_int(jvalue);
  256. if (!node_model_pub_set(node, ele_idx, model_id, &pub))
  257. return false;
  258. return true;
  259. }
  260. static bool parse_bindings(struct mesh_node *node, int ele_idx,
  261. uint32_t model_id, json_object *jbindings)
  262. {
  263. int cnt;
  264. int i;
  265. cnt = json_object_array_length(jbindings);
  266. for (i = 0; i < cnt; ++i) {
  267. int key_idx;
  268. json_object *jvalue;
  269. jvalue = json_object_array_get_idx(jbindings, i);
  270. if (!jvalue)
  271. return true;
  272. key_idx = json_object_get_int(jvalue);
  273. if (!CHECK_KEY_IDX_RANGE(key_idx))
  274. return false;
  275. if (!node_add_binding(node, ele_idx, model_id, key_idx))
  276. return false;
  277. }
  278. return true;
  279. }
  280. static json_object* find_configured_model(struct mesh_node *node, int ele_idx,
  281. json_object *jmodels, uint32_t target_id)
  282. {
  283. int model_cnt;
  284. int i;
  285. model_cnt = json_object_array_length(jmodels);
  286. for (i = 0; i < model_cnt; ++i) {
  287. json_object *jmodel;
  288. json_object *jvalue;
  289. char *str;
  290. int len;
  291. uint32_t model_id;
  292. jmodel = json_object_array_get_idx(jmodels, i);
  293. json_object_object_get_ex(jmodel, "modelId", &jvalue);
  294. str = (char *)json_object_get_string(jvalue);
  295. len = strlen(str);
  296. if (len != 4 && len != 8)
  297. return NULL;
  298. if (sscanf(str, "%08x", &model_id) != 1)
  299. return NULL;
  300. if (len == 4)
  301. model_id += 0xffff0000;
  302. if (model_id == target_id)
  303. return jmodel;
  304. }
  305. return NULL;
  306. }
  307. static bool parse_subscriptions(struct mesh_node *node, int ele_idx,
  308. uint32_t model_id, json_object *jsubscriptions)
  309. {
  310. int cnt;
  311. int i;
  312. int addr;
  313. cnt = json_object_array_length(jsubscriptions);
  314. for (i = 0; i < cnt; ++i) {
  315. char *str;
  316. json_object *jsubscription;
  317. jsubscription = json_object_array_get_idx(jsubscriptions, i);
  318. if (!jsubscription)
  319. return false;
  320. str = (char *)json_object_get_string(jsubscription);
  321. if (sscanf(str, "%04x", &addr) != 1)
  322. return false;
  323. if (!node_add_subscription(node, ele_idx, model_id, addr))
  324. return false;
  325. }
  326. return true;
  327. }
  328. static bool parse_configuration_models(struct mesh_node *node, int ele_idx,
  329. json_object *jmodels)
  330. {
  331. int model_cnt;
  332. int i;
  333. model_cnt = json_object_array_length(jmodels);
  334. for (i = 0; i < model_cnt; ++i) {
  335. json_object *jmodel;
  336. json_object *jvalue;
  337. json_object *jarray;
  338. char *str;
  339. int len;
  340. uint32_t model_id;
  341. jmodel = json_object_array_get_idx(jmodels, i);
  342. json_object_object_get_ex(jmodel, "modelId", &jvalue);
  343. str = (char *)json_object_get_string(jvalue);
  344. len = strlen(str);
  345. if (len != 4 && len != 8)
  346. return false;
  347. if (sscanf(str, "%08x", &model_id) != 1)
  348. return false;
  349. if (len == 4)
  350. model_id += 0xffff0000;
  351. json_object_object_get_ex(jmodel, "bind", &jarray);
  352. if (jarray && !parse_bindings(node, ele_idx, model_id, jarray))
  353. return false;
  354. json_object_object_get_ex(jmodel, "subscribe", &jarray);
  355. if (jarray && !parse_subscriptions(node, ele_idx, model_id, jarray))
  356. return false;
  357. json_object_object_get_ex(jmodel, "publish", &jvalue);
  358. if (jvalue && !parse_model_pub(node, ele_idx, model_id, jvalue))
  359. return false;
  360. }
  361. return true;
  362. }
  363. static bool parse_configuration_elements(struct mesh_node *node,
  364. json_object *jelements, bool local)
  365. {
  366. int el_cnt;
  367. int i;
  368. el_cnt = json_object_array_length(jelements);
  369. node_set_num_elements(node, el_cnt);
  370. for (i = 0; i < el_cnt; ++i) {
  371. json_object *jelement;
  372. json_object *jmodels;
  373. json_object *jvalue;
  374. int index;
  375. uint16_t addr;
  376. jelement = json_object_array_get_idx(jelements, i);
  377. json_object_object_get_ex(jelement, "elementIndex", &jvalue);
  378. if (jvalue) {
  379. index = json_object_get_int(jvalue);
  380. if (index >= el_cnt) {
  381. return false;
  382. }
  383. } else
  384. return false;
  385. if (index == 0) {
  386. char *str;
  387. json_object_object_get_ex(jelement, "unicastAddress",
  388. &jvalue);
  389. str = (char *)json_object_get_string(jvalue);
  390. if (sscanf(str, "%04hx", &addr) != 1)
  391. return false;
  392. if (!local && !net_reserve_address_range(addr, el_cnt))
  393. return false;
  394. node_set_primary(node, addr);
  395. }
  396. json_object_object_get_ex(jelement, "models", &jmodels);
  397. if (!jmodels)
  398. continue;
  399. if(!parse_configuration_models(node, index, jmodels))
  400. return false;
  401. }
  402. return true;
  403. }
  404. static void add_key(json_object *jobject, const char *desc, uint8_t* key)
  405. {
  406. json_object *jstring;
  407. char hexstr[33];
  408. hex2str(key, 16, hexstr, 33);
  409. jstring = json_object_new_string(hexstr);
  410. json_object_object_add(jobject, desc, jstring);
  411. }
  412. static json_object *find_node_by_primary(json_object *jmain, uint16_t primary)
  413. {
  414. json_object *jarray;
  415. int i, len;
  416. json_object_object_get_ex(jmain, "nodes", &jarray);
  417. if (!jarray)
  418. return NULL;
  419. len = json_object_array_length(jarray);
  420. for (i = 0; i < len; ++i) {
  421. json_object *jnode;
  422. json_object *jconfig;
  423. json_object *jelements;
  424. json_object *jelement;
  425. json_object *jvalue;
  426. char *str;
  427. uint16_t addr;
  428. jnode = json_object_array_get_idx(jarray, i);
  429. if (!jnode)
  430. return NULL;
  431. json_object_object_get_ex(jnode, "configuration", &jconfig);
  432. if (!jconfig)
  433. return NULL;
  434. json_object_object_get_ex(jconfig, "elements", &jelements);
  435. if (!jelements)
  436. return NULL;
  437. jelement = json_object_array_get_idx(jelements, 0);
  438. if (!jelement)
  439. return NULL;
  440. json_object_object_get_ex(jelement, "unicastAddress",
  441. &jvalue);
  442. str = (char *)json_object_get_string(jvalue);
  443. if (sscanf(str, "%04hx", &addr) != 1)
  444. return NULL;
  445. if (addr == primary)
  446. return jnode;
  447. }
  448. return NULL;
  449. }
  450. void prov_db_print_node_composition(struct mesh_node *node)
  451. {
  452. char *in_str;
  453. const char *comp_str;
  454. json_object *jmain;
  455. json_object *jnode;
  456. json_object *jcomp;
  457. uint16_t primary = node_get_primary(node);
  458. const char *filename;
  459. bool res = false;
  460. if (!node || !node_get_composition(node))
  461. return;
  462. if (node == node_get_local_node())
  463. filename = local_filename;
  464. else
  465. filename = prov_filename;
  466. in_str = prov_file_read(filename);
  467. if (!in_str)
  468. return;
  469. jmain = json_tokener_parse(in_str);
  470. if (!jmain)
  471. goto done;
  472. jnode = find_node_by_primary(jmain, primary);
  473. if (!jnode)
  474. goto done;
  475. json_object_object_get_ex(jnode, "composition", &jcomp);
  476. if (!jcomp)
  477. goto done;
  478. comp_str = json_object_to_json_string_ext(jcomp,
  479. JSON_C_TO_STRING_PRETTY);
  480. res = true;
  481. done:
  482. if (res)
  483. bt_shell_printf("\tComposition data for node %4.4x %s\n",
  484. primary, comp_str);
  485. else
  486. bt_shell_printf("\tComposition data for node %4.4x not present\n",
  487. primary);
  488. g_free(in_str);
  489. if (jmain)
  490. json_object_put(jmain);
  491. }
  492. bool prov_db_add_node_composition(struct mesh_node *node, uint8_t *data,
  493. uint16_t len)
  494. {
  495. char *in_str;
  496. json_object *jmain;
  497. json_object *jnode;
  498. json_object *jcomp;
  499. json_object *jbool;
  500. json_object *jfeatures;
  501. json_object *jelements;
  502. struct mesh_node_composition *comp;
  503. uint8_t num_ele;
  504. int i;
  505. uint16_t primary = node_get_primary(node);
  506. bool res = NULL;
  507. comp = node_get_composition(node);
  508. if (!comp)
  509. return false;
  510. in_str = prov_file_read(prov_filename);
  511. if (!in_str)
  512. return false;
  513. jmain = json_tokener_parse(in_str);
  514. if (!jmain)
  515. goto done;
  516. jnode = find_node_by_primary(jmain, primary);
  517. if (!jnode)
  518. goto done;
  519. jcomp = json_object_new_object();
  520. put_uint16(jcomp, "cid", comp->cid);
  521. put_uint16(jcomp, "pid", comp->pid);
  522. put_uint16(jcomp, "vid", comp->vid);
  523. put_uint16(jcomp, "crpl", comp->crpl);
  524. jfeatures = json_object_new_object();
  525. jbool = json_object_new_boolean(comp->relay);
  526. json_object_object_add(jfeatures, "relay", jbool);
  527. jbool = json_object_new_boolean(comp->proxy);
  528. json_object_object_add(jfeatures, "proxy", jbool);
  529. jbool = json_object_new_boolean(comp->friend);
  530. json_object_object_add(jfeatures, "friend", jbool);
  531. jbool = json_object_new_boolean(comp->lpn);
  532. json_object_object_add(jfeatures, "lpn", jbool);
  533. json_object_object_add(jcomp, "features", jfeatures);
  534. data += 11;
  535. len -= 11;
  536. num_ele = node_get_num_elements(node);
  537. jelements = json_object_new_array();
  538. for (i = 0; i < num_ele; ++i) {
  539. json_object *jelement;
  540. json_object *jmodels;
  541. json_object *jint;
  542. uint32_t mod_id;
  543. uint16_t vendor_id;
  544. uint8_t m, v;
  545. jelement = json_object_new_object();
  546. /* Element Index */
  547. jint = json_object_new_int(i);
  548. json_object_object_add(jelement, "elementIndex", jint);
  549. /* Location */
  550. put_uint16(jelement, "location", get_le16(data));
  551. data += 2;
  552. m = *data++;
  553. v = *data++;
  554. len -= 4;
  555. /* Models */
  556. jmodels = json_object_new_array();
  557. while (len >= 2 && m--) {
  558. mod_id = get_le16(data);
  559. data += 2;
  560. len -= 2;
  561. put_uint16_array_entry(jmodels, (uint16_t) mod_id);
  562. }
  563. while (len >= 4 && v--) {
  564. mod_id = get_le16(data + 2);
  565. vendor_id = get_le16(data);
  566. mod_id |= (vendor_id << 16);
  567. data += 4;
  568. len -= 4;
  569. put_uint32_array_entry(jmodels, mod_id);
  570. }
  571. json_object_object_add(jelement, "models", jmodels);
  572. json_object_array_add(jelements, jelement);
  573. }
  574. json_object_object_add(jcomp, "elements", jelements);
  575. json_object_object_add(jnode, "composition", jcomp);
  576. prov_file_write(jmain, false);
  577. res = true;;
  578. done:
  579. g_free(in_str);
  580. if(jmain)
  581. json_object_put(jmain);
  582. return res;
  583. }
  584. bool prov_db_node_set_ttl(struct mesh_node *node, uint8_t ttl)
  585. {
  586. char *in_str;
  587. json_object *jmain;
  588. json_object *jnode;
  589. json_object *jconfig;
  590. json_object *jvalue;
  591. uint16_t primary = node_get_primary(node);
  592. const char *filename;
  593. bool local = node == node_get_local_node();
  594. bool res = false;
  595. if (local)
  596. filename = local_filename;
  597. else
  598. filename = prov_filename;
  599. in_str = prov_file_read(filename);
  600. if (!in_str)
  601. return false;
  602. jmain = json_tokener_parse(in_str);
  603. if (!jmain)
  604. goto done;
  605. if (local)
  606. json_object_object_get_ex(jmain, "node", &jnode);
  607. else
  608. jnode = find_node_by_primary(jmain, primary);
  609. if (!jnode)
  610. goto done;
  611. json_object_object_get_ex(jnode, "configuration", &jconfig);
  612. if (!jconfig)
  613. goto done;
  614. json_object_object_del(jconfig, "defaultTTL");
  615. jvalue = json_object_new_int(ttl);
  616. json_object_object_add(jconfig, "defaultTTL", jvalue);
  617. prov_file_write(jmain, local);
  618. res = true;
  619. done:
  620. g_free(in_str);
  621. if(jmain)
  622. json_object_put(jmain);
  623. return res;
  624. }
  625. static void set_local_iv_index(json_object *jobj, uint32_t idx, bool update)
  626. {
  627. json_object *jvalue;
  628. json_object_object_del(jobj, "IVindex");
  629. jvalue = json_object_new_int(idx);
  630. json_object_object_add(jobj, "IVindex", jvalue);
  631. json_object_object_del(jobj, "IVupdate");
  632. jvalue = json_object_new_int((update) ? 1 : 0);
  633. json_object_object_add(jobj, "IVupdate", jvalue);
  634. }
  635. bool prov_db_local_set_iv_index(uint32_t iv_index, bool update, bool prov)
  636. {
  637. char *in_str;
  638. json_object *jmain;
  639. json_object *jnode;
  640. bool res = false;
  641. in_str = prov_file_read(local_filename);
  642. if (!in_str)
  643. return false;
  644. jmain = json_tokener_parse(in_str);
  645. if (!jmain)
  646. goto done;
  647. json_object_object_get_ex(jmain, "node", &jnode);
  648. set_local_iv_index(jnode, iv_index, update);
  649. prov_file_write(jmain, true);
  650. g_free(in_str);
  651. json_object_put(jmain);
  652. /* If provisioner, save to global DB as well */
  653. if (prov) {
  654. in_str = prov_file_read(prov_filename);
  655. if (!in_str)
  656. return false;
  657. jmain = json_tokener_parse(in_str);
  658. if (!jmain)
  659. goto done;
  660. set_local_iv_index(jmain, iv_index, update);
  661. prov_file_write(jmain, false);
  662. }
  663. res = true;
  664. done:
  665. g_free(in_str);
  666. if(jmain)
  667. json_object_put(jmain);
  668. return res;
  669. }
  670. bool prov_db_local_set_seq_num(uint32_t seq_num)
  671. {
  672. char *in_str;
  673. json_object *jmain;
  674. json_object *jnode;
  675. json_object *jvalue;
  676. bool res = false;
  677. in_str = prov_file_read(local_filename);
  678. if (!in_str)
  679. return false;
  680. jmain = json_tokener_parse(in_str);
  681. if (!jmain)
  682. goto done;
  683. json_object_object_get_ex(jmain, "node", &jnode);
  684. json_object_object_del(jnode, "sequenceNumber");
  685. jvalue = json_object_new_int(seq_num);
  686. json_object_object_add(jnode, "sequenceNumber", jvalue);
  687. prov_file_write(jmain, true);
  688. res = true;
  689. done:
  690. g_free(in_str);
  691. if(jmain)
  692. json_object_put(jmain);
  693. return res;
  694. }
  695. bool prov_db_node_set_iv_seq(struct mesh_node *node, uint32_t iv, uint32_t seq)
  696. {
  697. char *in_str;
  698. json_object *jmain;
  699. json_object *jnode;
  700. json_object *jvalue;
  701. uint16_t primary = node_get_primary(node);
  702. bool res = false;
  703. in_str = prov_file_read(prov_filename);
  704. if (!in_str)
  705. return false;
  706. jmain = json_tokener_parse(in_str);
  707. if (!jmain)
  708. goto done;
  709. jnode = find_node_by_primary(jmain, primary);
  710. if (!jnode)
  711. goto done;
  712. json_object_object_del(jnode, "IVindex");
  713. jvalue = json_object_new_int(iv);
  714. json_object_object_add(jnode, "IVindex", jvalue);
  715. json_object_object_del(jnode, "sequenceNumber");
  716. jvalue = json_object_new_int(seq);
  717. json_object_object_add(jnode, "sequenceNumber", jvalue);
  718. prov_file_write(jmain, false);
  719. res = true;
  720. done:
  721. g_free(in_str);
  722. if(jmain)
  723. json_object_put(jmain);
  724. return res;
  725. }
  726. bool prov_db_node_keys(struct mesh_node *node, GList *idxs, const char *desc)
  727. {
  728. char *in_str;
  729. json_object *jmain;
  730. json_object *jnode;
  731. json_object *jconfig;
  732. json_object *jidxs;
  733. uint16_t primary = node_get_primary(node);
  734. const char *filename;
  735. bool local = (node == node_get_local_node());
  736. bool res = false;
  737. if (local)
  738. filename = local_filename;
  739. else
  740. filename = prov_filename;
  741. in_str = prov_file_read(filename);
  742. if (!in_str)
  743. return false;
  744. jmain = json_tokener_parse(in_str);
  745. if (!jmain)
  746. goto done;
  747. jnode = find_node_by_primary(jmain, primary);
  748. if (!jnode)
  749. goto done;
  750. json_object_object_get_ex(jnode, "configuration", &jconfig);
  751. if (!jconfig)
  752. goto done;
  753. json_object_object_del(jconfig, desc);
  754. if (idxs) {
  755. jidxs = json_object_new_array();
  756. put_uint16_list(jidxs, idxs);
  757. json_object_object_add(jconfig, desc, jidxs);
  758. }
  759. prov_file_write(jmain, local);
  760. res = true;
  761. done:
  762. g_free(in_str);
  763. if(jmain)
  764. json_object_put(jmain);
  765. return res;
  766. }
  767. static json_object *get_jmodel_obj(struct mesh_node *node, uint8_t ele_idx,
  768. uint32_t model_id, json_object **jmain)
  769. {
  770. char *in_str;
  771. json_object *jnode;
  772. json_object *jconfig;
  773. json_object *jelements, *jelement;
  774. json_object *jmodels, *jmodel = NULL;
  775. uint16_t primary = node_get_primary(node);
  776. const char *filename;
  777. bool local = (node == node_get_local_node());
  778. if (local)
  779. filename = local_filename;
  780. else
  781. filename = prov_filename;
  782. in_str = prov_file_read(filename);
  783. if (!in_str)
  784. return NULL;
  785. *jmain = json_tokener_parse(in_str);
  786. if (!(*jmain))
  787. goto done;
  788. if (local)
  789. json_object_object_get_ex(*jmain, "node", &jnode);
  790. else
  791. jnode = find_node_by_primary(*jmain, primary);
  792. if (!jnode)
  793. goto done;
  794. /* Configuration is mandatory for nodes in provisioning database */
  795. json_object_object_get_ex(jnode, "configuration", &jconfig);
  796. if (!jconfig)
  797. goto done;
  798. json_object_object_get_ex(jconfig, "elements", &jelements);
  799. if (!jelements) {
  800. goto done;
  801. }
  802. jelement = json_object_array_get_idx(jelements, ele_idx);
  803. if (!jelement) {
  804. goto done;
  805. }
  806. json_object_object_get_ex(jelement, "models", &jmodels);
  807. if (!jmodels) {
  808. jmodels = json_object_new_array();
  809. json_object_object_add(jelement, "models", jmodels);
  810. } else {
  811. jmodel = find_configured_model(node, ele_idx, jmodels,
  812. model_id);
  813. }
  814. if (!jmodel) {
  815. jmodel = json_object_new_object();
  816. if ((model_id & 0xffff0000) == 0xffff0000)
  817. put_uint16(jmodel, "modelId", model_id & 0xffff);
  818. else
  819. put_uint32(jmodel, "modelId", model_id);
  820. json_object_array_add(jmodels, jmodel);
  821. }
  822. done:
  823. g_free(in_str);
  824. if(!jmodel && *jmain)
  825. json_object_put(*jmain);
  826. return jmodel;
  827. }
  828. bool prov_db_add_binding(struct mesh_node *node, uint8_t ele_idx,
  829. uint32_t model_id, uint16_t app_idx)
  830. {
  831. bool local = (node == node_get_local_node());
  832. json_object *jbindings = NULL;
  833. json_object *jmodel;
  834. json_object *jvalue;
  835. json_object *jmain;
  836. jmodel = get_jmodel_obj(node, ele_idx, model_id, &jmain);
  837. if (!jmodel)
  838. return false;
  839. json_object_object_get_ex(jmodel, "bind", &jbindings);
  840. if (!jbindings) {
  841. jbindings = json_object_new_array();
  842. json_object_object_add(jmodel, "bind", jbindings);
  843. }
  844. jvalue = json_object_new_int(app_idx);
  845. json_object_array_add(jbindings, jvalue);
  846. prov_file_write(jmain, local);
  847. json_object_put(jmain);
  848. return true;
  849. }
  850. bool prov_db_add_subscription(struct mesh_node *node, uint8_t ele_idx,
  851. uint32_t model_id, uint16_t addr)
  852. {
  853. bool local = (node == node_get_local_node());
  854. json_object *jsubscriptions = NULL;
  855. json_object *jmodel;
  856. json_object *jmain;
  857. jmodel = get_jmodel_obj(node, ele_idx, model_id, &jmain);
  858. if (!jmodel)
  859. return false;
  860. json_object_object_get_ex(jmodel, "subscribe", &jsubscriptions);
  861. if (!jsubscriptions) {
  862. jsubscriptions = json_object_new_array();
  863. json_object_object_add(jmodel, "subscribe", jsubscriptions);
  864. }
  865. put_uint16_array_entry(jsubscriptions, addr);
  866. prov_file_write(jmain, local);
  867. json_object_put(jmain);
  868. return true;
  869. }
  870. bool prov_db_node_set_model_pub(struct mesh_node *node, uint8_t ele_idx,
  871. uint32_t model_id,
  872. struct mesh_publication *pub)
  873. {
  874. json_object *jmain;
  875. json_object *jmodel;
  876. json_object *jpub;
  877. json_object *jvalue;
  878. bool local = (node == node_get_local_node());
  879. jmodel = get_jmodel_obj(node, ele_idx, model_id, &jmain);
  880. if (!jmodel)
  881. return false;
  882. json_object_object_del(jmodel, "publish");
  883. if (!pub)
  884. goto done;
  885. jpub = json_object_new_object();
  886. /* Save only required fields */
  887. put_uint16(jpub, "address", pub->u.addr16);
  888. put_uint16(jpub, "index", pub->app_idx);
  889. jvalue = json_object_new_int(pub->ttl);
  890. json_object_object_add(jpub, "ttl", jvalue);
  891. json_object_object_add(jmodel, "publish", jpub);
  892. done:
  893. prov_file_write(jmain, local);
  894. json_object_put(jmain);
  895. return true;
  896. }
  897. bool prov_db_add_new_node(struct mesh_node *node)
  898. {
  899. char *in_str;
  900. json_object *jmain;
  901. json_object *jarray;
  902. json_object *jnode;
  903. json_object *jconfig;
  904. json_object *jelements;
  905. uint8_t num_ele;
  906. uint16_t primary;
  907. int i;
  908. bool first_node;
  909. bool res = false;
  910. in_str = prov_file_read(prov_filename);
  911. if (!in_str)
  912. return false;
  913. jmain = json_tokener_parse(in_str);
  914. if (!jmain)
  915. goto done;
  916. json_object_object_get_ex(jmain, "nodes", &jarray);
  917. if (!jarray) {
  918. jarray = json_object_new_array();
  919. first_node = true;
  920. } else
  921. first_node = false;
  922. jnode = json_object_new_object();
  923. /* Device key */
  924. add_key(jnode, "deviceKey", node_get_device_key(node));
  925. /* Net key */
  926. jconfig = json_object_new_object();
  927. add_node_idxs(jconfig, "netKeys", node_get_net_keys(node));
  928. num_ele = node_get_num_elements(node);
  929. if (num_ele == 0)
  930. goto done;
  931. jelements = json_object_new_array();
  932. primary = node_get_primary(node);
  933. if (IS_UNASSIGNED(primary))
  934. goto done;
  935. for (i = 0; i < num_ele; ++i) {
  936. json_object *jelement;
  937. json_object *jint;
  938. jelement = json_object_new_object();
  939. /* Element Index */
  940. jint = json_object_new_int(i);
  941. json_object_object_add(jelement, "elementIndex", jint);
  942. /* Unicast */
  943. put_uint16(jelement, "unicastAddress", primary + i);
  944. json_object_array_add(jelements, jelement);
  945. }
  946. json_object_object_add(jconfig, "elements", jelements);
  947. json_object_object_add(jnode, "configuration", jconfig);
  948. json_object_array_add(jarray, jnode);
  949. if (first_node)
  950. json_object_object_add(jmain, "nodes", jarray);
  951. prov_file_write(jmain, false);
  952. res = true;
  953. done:
  954. g_free(in_str);
  955. if (jmain)
  956. json_object_put(jmain);
  957. return res;
  958. }
  959. static bool parse_node_composition(struct mesh_node *node, json_object *jcomp)
  960. {
  961. json_object *jvalue;
  962. json_object *jelements;
  963. json_object *jfeatures;
  964. json_bool enable;
  965. char *str;
  966. struct mesh_node_composition comp;
  967. json_object_object_get_ex(jcomp, "cid", &jvalue);
  968. if (!jvalue)
  969. return false;
  970. str = (char *)json_object_get_string(jvalue);
  971. if (sscanf(str, "%04hx", &comp.cid) != 1)
  972. return false;
  973. json_object_object_get_ex(jcomp, "pid", &jvalue);
  974. if (!jvalue)
  975. return false;
  976. str = (char *)json_object_get_string(jvalue);
  977. if (sscanf(str, "%04hx", &comp.pid) != 1)
  978. return false;
  979. json_object_object_get_ex(jcomp, "vid", &jvalue);
  980. if (!jvalue)
  981. return false;
  982. str = (char *)json_object_get_string(jvalue);
  983. if (sscanf(str, "%04hx", &comp.vid) != 1)
  984. return false;
  985. json_object_object_get_ex(jcomp, "crpl", &jvalue);
  986. if (!jvalue)
  987. return false;
  988. str = (char *)json_object_get_string(jvalue);
  989. if (sscanf(str, "%04hx", &comp.crpl) != 1)
  990. return false;
  991. /* Extract features */
  992. json_object_object_get_ex(jcomp, "features", &jfeatures);
  993. if (!jfeatures)
  994. return false;
  995. json_object_object_get_ex(jfeatures, "relay", &jvalue);
  996. enable = json_object_get_boolean(jvalue);
  997. comp.relay = (enable) ? true : false;
  998. json_object_object_get_ex(jfeatures, "proxy", &jvalue);
  999. enable = json_object_get_boolean(jvalue);
  1000. comp.proxy = (enable) ? true : false;
  1001. json_object_object_get_ex(jfeatures, "friend", &jvalue);
  1002. enable = json_object_get_boolean(jvalue);
  1003. comp.friend = (enable) ? true : false;
  1004. json_object_object_get_ex(jfeatures, "lowPower", &jvalue);
  1005. enable = json_object_get_boolean(jvalue);
  1006. comp.lpn = (enable) ? true : false;
  1007. if (!node_set_composition(node, &comp))
  1008. return false;
  1009. json_object_object_get_ex(jcomp, "elements", &jelements);
  1010. if (!jelements)
  1011. return false;
  1012. return parse_composition_elements(node, jelements);
  1013. }
  1014. static bool parse_node(json_object *jnode, bool local)
  1015. {
  1016. json_object *jconfig;
  1017. json_object *jelements;
  1018. json_object *jidxs;
  1019. json_object *jvalue;
  1020. json_object *jint;
  1021. uint8_t key[16];
  1022. char *value_str;
  1023. uint32_t idx;
  1024. struct mesh_node *node;
  1025. /* Device key */
  1026. if (!json_object_object_get_ex(jnode, "deviceKey", &jvalue) ||
  1027. !jvalue) {
  1028. if (!mesh_get_random_bytes(key, 16))
  1029. return false;
  1030. add_key(jnode, "deviceKey", key);
  1031. } else {
  1032. value_str = (char *)json_object_get_string(jvalue);
  1033. if (!str2hex(value_str, strlen(value_str), key, 16))
  1034. return false;;
  1035. }
  1036. node = node_new();
  1037. if (!node)
  1038. return false;
  1039. node_set_device_key(node, key);
  1040. json_object_object_get_ex(jnode, "IVindex", &jint);
  1041. if (jint)
  1042. idx = json_object_get_int(jint);
  1043. else
  1044. idx = 0;
  1045. node_set_iv_index(node, idx);
  1046. if (local) {
  1047. bool update = false;
  1048. json_object_object_get_ex(jnode, "IVupdate", &jint);
  1049. if (jint)
  1050. update = json_object_get_int(jint) ? true : false;
  1051. net_set_iv_index(idx, update);
  1052. }
  1053. if (json_object_object_get_ex(jnode, "sequenceNumber", &jint) &&
  1054. jint) {
  1055. int seq = json_object_get_int(jint);
  1056. node_set_sequence_number(node, seq);
  1057. }
  1058. /* Composition is mandatory for local node */
  1059. json_object_object_get_ex(jnode, "composition", &jconfig);
  1060. if ((jconfig && !parse_node_composition(node, jconfig)) ||
  1061. (!jconfig && local)) {
  1062. node_free(node);
  1063. return false;
  1064. }
  1065. /* Configuration is mandatory for nodes in provisioning database */
  1066. json_object_object_get_ex(jnode, "configuration", &jconfig);
  1067. if (!jconfig) {
  1068. if (local) {
  1069. /* This is an unprovisioned local device */
  1070. goto done;
  1071. } else {
  1072. node_free(node);
  1073. return false;
  1074. }
  1075. }
  1076. json_object_object_get_ex(jconfig, "elements", &jelements);
  1077. if (!jelements) {
  1078. node_free(node);
  1079. return false;
  1080. }
  1081. if (!parse_configuration_elements(node, jelements, local)) {
  1082. node_free(node);
  1083. return false;;
  1084. }
  1085. json_object_object_get_ex(jconfig, "netKeys", &jidxs);
  1086. if (!jidxs || (parse_node_keys(node, jidxs, false) == 0)) {
  1087. node_free(node);
  1088. return false;
  1089. }
  1090. json_object_object_get_ex(jconfig, "appKeys", &jidxs);
  1091. if (jidxs)
  1092. parse_node_keys(node, jidxs, true);
  1093. json_object_object_get_ex(jconfig, "defaultTTL", &jvalue);
  1094. if (jvalue) {
  1095. int ttl = json_object_get_int(jvalue);
  1096. node_set_default_ttl(node, ttl &TTL_MASK);
  1097. }
  1098. done:
  1099. if (local && !node_set_local_node(node)) {
  1100. node_free(node);
  1101. return false;
  1102. }
  1103. return true;
  1104. }
  1105. bool prov_db_show(const char *filename)
  1106. {
  1107. char *str;
  1108. str = prov_file_read(filename);
  1109. if (!str)
  1110. return false;
  1111. bt_shell_printf("%s\n", str);
  1112. g_free(str);
  1113. return true;
  1114. }
  1115. static bool read_json_db(const char *filename, bool provisioner, bool local)
  1116. {
  1117. char *str;
  1118. json_object *jmain;
  1119. json_object *jarray;
  1120. json_object *jprov;
  1121. json_object *jvalue;
  1122. json_object *jtemp;
  1123. uint8_t key[16];
  1124. int value_int;
  1125. char *value_str;
  1126. int len;
  1127. int i;
  1128. uint32_t index;
  1129. bool refresh = false;
  1130. bool res = false;
  1131. str = prov_file_read(filename);
  1132. if (!str) return false;
  1133. jmain = json_tokener_parse(str);
  1134. if (!jmain)
  1135. goto done;
  1136. if (local) {
  1137. json_object *jnode;
  1138. bool result;
  1139. json_object_object_get_ex(jmain, "node", &jnode);
  1140. if (!jnode) {
  1141. bt_shell_printf("Cannot find \"node\" object");
  1142. goto done;
  1143. } else
  1144. result = parse_node(jnode, true);
  1145. /*
  1146. * If local node is provisioner, the rest of mesh settings
  1147. * are read from provisioning database.
  1148. */
  1149. if (provisioner) {
  1150. res = result;
  1151. goto done;
  1152. }
  1153. }
  1154. /* IV index */
  1155. json_object_object_get_ex(jmain, "IVindex", &jvalue);
  1156. if (!jvalue)
  1157. goto done;
  1158. index = json_object_get_int(jvalue);
  1159. json_object_object_get_ex(jmain, "IVupdate", &jvalue);
  1160. if (!jvalue)
  1161. goto done;
  1162. value_int = json_object_get_int(jvalue);
  1163. net_set_iv_index(index, value_int);
  1164. /* Network key(s) */
  1165. json_object_object_get_ex(jmain, "netKeys", &jarray);
  1166. if (!jarray)
  1167. goto done;
  1168. len = json_object_array_length(jarray);
  1169. bt_shell_printf("# netkeys = %d\n", len);
  1170. for (i = 0; i < len; ++i) {
  1171. uint32_t idx;
  1172. jtemp = json_object_array_get_idx(jarray, i);
  1173. json_object_object_get_ex(jtemp, "index", &jvalue);
  1174. if (!jvalue)
  1175. goto done;
  1176. idx = json_object_get_int(jvalue);
  1177. json_object_object_get_ex(jtemp, "key", &jvalue);
  1178. if (!jvalue) {
  1179. if (!mesh_get_random_bytes(key, 16))
  1180. goto done;
  1181. add_key(jtemp, "key", key);
  1182. refresh = true;
  1183. } else {
  1184. value_str = (char *)json_object_get_string(jvalue);
  1185. if (!str2hex(value_str, strlen(value_str), key, 16)) {
  1186. goto done;
  1187. }
  1188. }
  1189. if (!keys_net_key_add(idx, key, false))
  1190. goto done;
  1191. json_object_object_get_ex(jtemp, "keyRefresh", &jvalue);
  1192. if (!jvalue)
  1193. goto done;
  1194. keys_set_kr_phase(idx, (uint8_t) json_object_get_int(jvalue));
  1195. }
  1196. /* App keys */
  1197. json_object_object_get_ex(jmain, "appKeys", &jarray);
  1198. if (jarray) {
  1199. len = json_object_array_length(jarray);
  1200. bt_shell_printf("# appkeys = %d\n", len);
  1201. for (i = 0; i < len; ++i) {
  1202. int app_idx;
  1203. int net_idx;
  1204. jtemp = json_object_array_get_idx(jarray, i);
  1205. json_object_object_get_ex(jtemp, "index",
  1206. &jvalue);
  1207. if (!jvalue)
  1208. goto done;
  1209. app_idx = json_object_get_int(jvalue);
  1210. if (!CHECK_KEY_IDX_RANGE(app_idx))
  1211. goto done;
  1212. json_object_object_get_ex(jtemp, "key", &jvalue);
  1213. if (!jvalue) {
  1214. if (!mesh_get_random_bytes(key, 16))
  1215. goto done;
  1216. add_key(jtemp, "key", key);
  1217. refresh = true;
  1218. } else {
  1219. value_str =
  1220. (char *)json_object_get_string(jvalue);
  1221. str2hex(value_str, strlen(value_str), key, 16);
  1222. }
  1223. json_object_object_get_ex(jtemp, "boundNetKey",
  1224. &jvalue);
  1225. if (!jvalue)
  1226. goto done;
  1227. net_idx = json_object_get_int(jvalue);
  1228. if (!CHECK_KEY_IDX_RANGE(net_idx))
  1229. goto done;
  1230. keys_app_key_add(net_idx, app_idx, key, false);
  1231. }
  1232. }
  1233. /* Provisioner info */
  1234. json_object_object_get_ex(jmain, "provisioners", &jarray);
  1235. if (!jarray)
  1236. goto done;
  1237. len = json_object_array_length(jarray);
  1238. bt_shell_printf("# provisioners = %d\n", len);
  1239. for (i = 0; i < len; ++i) {
  1240. jprov = json_object_array_get_idx(jarray, i);
  1241. /* Allocated unicast range */
  1242. json_object_object_get_ex(jprov, "allocatedUnicastRange",
  1243. &jtemp);
  1244. if (!jtemp) {
  1245. goto done;
  1246. }
  1247. if (!parse_unicast_range(jtemp)) {
  1248. bt_shell_printf("Doneed to parse unicast range\n");
  1249. goto done;
  1250. }
  1251. }
  1252. json_object_object_get_ex(jmain, "nodes", &jarray);
  1253. if (!jarray) {
  1254. res = true;
  1255. goto done;
  1256. }
  1257. len = json_object_array_length(jarray);
  1258. bt_shell_printf("# provisioned nodes = %d\n", len);
  1259. for (i = 0; i < len; ++i) {
  1260. json_object *jnode;
  1261. jnode = json_object_array_get_idx(jarray, i);
  1262. if (!jnode || !parse_node(jnode, false))
  1263. goto done;
  1264. }
  1265. res = true;
  1266. done:
  1267. g_free(str);
  1268. if (res && refresh)
  1269. prov_file_write(jmain, false);
  1270. if (jmain)
  1271. json_object_put(jmain);
  1272. return res;
  1273. }
  1274. bool prov_db_read(const char *filename)
  1275. {
  1276. prov_filename = filename;
  1277. return read_json_db(filename, true, false);
  1278. }
  1279. bool prov_db_read_local_node(const char *filename, bool provisioner)
  1280. {
  1281. local_filename = filename;
  1282. return read_json_db(filename, provisioner, true);
  1283. }