access.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228
  1. /* Bluetooth Mesh */
  2. /*
  3. * SPDX-FileCopyrightText: 2017 Intel Corporation
  4. * SPDX-FileContributor: 2018-2021 Espressif Systems (Shanghai) CO LTD
  5. *
  6. * SPDX-License-Identifier: Apache-2.0
  7. */
  8. #include <stdint.h>
  9. #include <stdbool.h>
  10. #include <errno.h>
  11. #include "mesh.h"
  12. #include "adv.h"
  13. #include "lpn.h"
  14. #include "friend.h"
  15. #include "transport.h"
  16. #include "access.h"
  17. #include "foundation.h"
  18. #include "mesh/main.h"
  19. #include "mesh/common.h"
  20. #include "fast_prov.h"
  21. #include "pvnr_mgmt.h"
  22. #include "mesh_v1.1/utils.h"
  23. #define BLE_MESH_SDU_MAX_LEN 384
  24. extern const struct bt_mesh_comp *comp_0;
  25. static uint16_t dev_primary_addr;
  26. static int model_send(struct bt_mesh_model *model,
  27. struct bt_mesh_net_tx *tx, bool implicit_bind,
  28. struct net_buf_simple *msg,
  29. const struct bt_mesh_send_cb *cb, void *cb_data);
  30. void bt_mesh_model_foreach(void (*func)(struct bt_mesh_model *mod,
  31. struct bt_mesh_elem *elem,
  32. bool vnd, bool primary,
  33. void *user_data),
  34. void *user_data)
  35. {
  36. int i, j;
  37. if (comp_0 == NULL) {
  38. BT_ERR("Invalid device composition");
  39. return;
  40. }
  41. for (i = 0; i < comp_0->elem_count; i++) {
  42. struct bt_mesh_elem *elem = &comp_0->elem[i];
  43. for (j = 0; j < elem->model_count; j++) {
  44. struct bt_mesh_model *model = &elem->models[j];
  45. func(model, elem, false, i == 0, user_data);
  46. }
  47. for (j = 0; j < elem->vnd_model_count; j++) {
  48. struct bt_mesh_model *model = &elem->vnd_models[j];
  49. func(model, elem, true, i == 0, user_data);
  50. }
  51. }
  52. }
  53. int32_t bt_mesh_model_pub_period_get(struct bt_mesh_model *mod)
  54. {
  55. int period = 0;
  56. if (!mod->pub) {
  57. BT_ERR("Model has no publication support");
  58. return 0;
  59. }
  60. switch (mod->pub->period >> 6) {
  61. case 0x00:
  62. /* 1 step is 100 ms */
  63. period = K_MSEC((mod->pub->period & BIT_MASK(6)) * 100U);
  64. break;
  65. case 0x01:
  66. /* 1 step is 1 second */
  67. period = K_SECONDS(mod->pub->period & BIT_MASK(6));
  68. break;
  69. case 0x02:
  70. /* 1 step is 10 seconds */
  71. period = K_SECONDS((mod->pub->period & BIT_MASK(6)) * 10U);
  72. break;
  73. case 0x03:
  74. /* 1 step is 10 minutes */
  75. period = K_MINUTES((mod->pub->period & BIT_MASK(6)) * 10U);
  76. break;
  77. default:
  78. BT_ERR("Unknown model publication period");
  79. return 0;
  80. }
  81. if (mod->pub->fast_period) {
  82. return period >> mod->pub->period_div;
  83. }
  84. return period;
  85. }
  86. static int32_t next_period(struct bt_mesh_model *mod)
  87. {
  88. struct bt_mesh_model_pub *pub = mod->pub;
  89. uint32_t elapsed = 0U, period = 0U;
  90. if (!pub) {
  91. BT_ERR("Model has no publication support");
  92. return -ENOTSUP;
  93. }
  94. period = bt_mesh_model_pub_period_get(mod);
  95. if (!period) {
  96. return 0;
  97. }
  98. elapsed = k_uptime_get_32() - pub->period_start;
  99. BT_INFO("Publishing took %ums", elapsed);
  100. if (elapsed >= period) {
  101. BT_WARN("Publication sending took longer than the period");
  102. /* Return smallest positive number since 0 means disabled */
  103. return K_MSEC(1);
  104. }
  105. return period - elapsed;
  106. }
  107. static void publish_sent(int err, void *user_data)
  108. {
  109. struct bt_mesh_model *mod = user_data;
  110. int32_t delay = 0;
  111. BT_DBG("err %d", err);
  112. if (!mod->pub) {
  113. BT_ERR("Model has no publication support");
  114. return;
  115. }
  116. if (mod->pub->count) {
  117. delay = BLE_MESH_PUB_TRANSMIT_INT(mod->pub->retransmit);
  118. } else {
  119. delay = next_period(mod);
  120. }
  121. if (delay) {
  122. BT_INFO("Publishing next time in %dms", delay);
  123. k_delayed_work_submit(&mod->pub->timer, delay);
  124. }
  125. }
  126. static void publish_start(uint16_t duration, int err, void *user_data)
  127. {
  128. struct bt_mesh_model *mod = user_data;
  129. struct bt_mesh_model_pub *pub = mod->pub;
  130. if (err) {
  131. BT_ERR("Failed to publish: err %d", err);
  132. return;
  133. }
  134. /* Initialize the timestamp for the beginning of a new period */
  135. if (pub->count == BLE_MESH_PUB_TRANSMIT_COUNT(pub->retransmit)) {
  136. pub->period_start = k_uptime_get_32();
  137. }
  138. }
  139. static const struct bt_mesh_send_cb pub_sent_cb = {
  140. .start = publish_start,
  141. .end = publish_sent,
  142. };
  143. static int publish_retransmit(struct bt_mesh_model *mod)
  144. {
  145. struct bt_mesh_model_pub *pub = mod->pub;
  146. struct bt_mesh_app_key *key = NULL;
  147. struct net_buf_simple *sdu = NULL;
  148. struct bt_mesh_msg_ctx ctx = {0};
  149. struct bt_mesh_net_tx tx = {
  150. .ctx = &ctx,
  151. .src = bt_mesh_model_elem(mod)->addr,
  152. .xmit = bt_mesh_net_transmit_get(),
  153. };
  154. int err = 0;
  155. if (!pub || !pub->msg) {
  156. BT_ERR("Model has no publication support");
  157. return -ENOTSUP;
  158. }
  159. key = bt_mesh_app_key_get(pub->key);
  160. if (!key) {
  161. BT_ERR("Publish, AppKey 0x%03x not found", pub->key);
  162. return -EADDRNOTAVAIL;
  163. }
  164. tx.sub = bt_mesh_subnet_get(key->net_idx);
  165. if (!tx.sub) {
  166. BT_ERR("Publish, NetKey 0x%04x not found", key->net_idx);
  167. return -EADDRNOTAVAIL;
  168. }
  169. ctx.addr = pub->addr;
  170. ctx.net_idx = key->net_idx;
  171. ctx.app_idx = key->app_idx;
  172. ctx.send_ttl = pub->ttl;
  173. ctx.send_cred = pub->cred ? BLE_MESH_FRIENDSHIP_CRED : BLE_MESH_FLOODING_CRED;
  174. ctx.send_szmic = pub->send_szmic;
  175. if (pub->send_rel) {
  176. /* Tag with send-segmented */
  177. ctx.send_tag |= BLE_MESH_TAG_SEND_SEGMENTED;
  178. }
  179. #if CONFIG_BLE_MESH_DF_SRV
  180. bt_mesh_model_pub_use_directed(&tx, pub->directed_pub_policy);
  181. #endif
  182. sdu = bt_mesh_alloc_buf(pub->msg->len + BLE_MESH_MIC_LONG);
  183. if (!sdu) {
  184. BT_ERR("%s, Out of memory", __func__);
  185. return -ENOMEM;
  186. }
  187. net_buf_simple_add_mem(sdu, pub->msg->data, pub->msg->len);
  188. pub->count--;
  189. err = model_send(mod, &tx, true, sdu, &pub_sent_cb, mod);
  190. bt_mesh_free_buf(sdu);
  191. return err;
  192. }
  193. static void publish_retransmit_end(int err, struct bt_mesh_model_pub *pub)
  194. {
  195. /* Cancel all retransmits for this publish attempt */
  196. pub->count = 0U;
  197. /* Make sure the publish timer gets reset */
  198. publish_sent(err, pub->mod);
  199. }
  200. static void mod_publish(struct k_work *work)
  201. {
  202. struct bt_mesh_model_pub *pub = CONTAINER_OF(work,
  203. struct bt_mesh_model_pub,
  204. timer.work);
  205. int32_t period_ms = 0;
  206. int err = 0;
  207. period_ms = bt_mesh_model_pub_period_get(pub->mod);
  208. BT_INFO("Publish period %u ms", period_ms);
  209. if (pub->count) {
  210. err = publish_retransmit(pub->mod);
  211. if (err) {
  212. BT_ERR("Failed to retransmit (err %d)", err);
  213. pub->count = 0U;
  214. /* Continue with normal publication */
  215. if (period_ms) {
  216. k_delayed_work_submit(&pub->timer, period_ms);
  217. }
  218. }
  219. return;
  220. }
  221. if (!period_ms) {
  222. return;
  223. }
  224. /* Callback the model publish update event to the application layer.
  225. * In the event, users can update the context of the publish message
  226. * which will be published in the next period.
  227. */
  228. if (pub->update && pub->update(pub->mod)) {
  229. /* Cancel this publish attempt. */
  230. BT_ERR("Update failed, skipping publish (err %d)", err);
  231. pub->period_start = k_uptime_get_32();
  232. publish_retransmit_end(err, pub);
  233. return;
  234. }
  235. err = bt_mesh_model_publish(pub->mod);
  236. if (err) {
  237. BT_ERR("Publishing failed (err %d)", err);
  238. }
  239. }
  240. struct bt_mesh_elem *bt_mesh_model_elem(struct bt_mesh_model *mod)
  241. {
  242. return &comp_0->elem[mod->elem_idx];
  243. }
  244. struct bt_mesh_model *bt_mesh_model_get(bool vnd, uint8_t elem_idx, uint8_t mod_idx)
  245. {
  246. struct bt_mesh_elem *elem = NULL;
  247. if (!comp_0) {
  248. BT_ERR("comp_0 not initialized");
  249. return NULL;
  250. }
  251. if (elem_idx >= comp_0->elem_count) {
  252. BT_ERR("Invalid element index %u", elem_idx);
  253. return NULL;
  254. }
  255. elem = &comp_0->elem[elem_idx];
  256. if (vnd) {
  257. if (mod_idx >= elem->vnd_model_count) {
  258. BT_ERR("Invalid vendor model index %u", mod_idx);
  259. return NULL;
  260. }
  261. return &elem->vnd_models[mod_idx];
  262. } else {
  263. if (mod_idx >= elem->model_count) {
  264. BT_ERR("Invalid SIG model index %u", mod_idx);
  265. return NULL;
  266. }
  267. return &elem->models[mod_idx];
  268. }
  269. }
  270. static void mod_init(struct bt_mesh_model *mod, struct bt_mesh_elem *elem,
  271. bool vnd, bool primary, void *user_data)
  272. {
  273. int *err = user_data;
  274. int i;
  275. if (!user_data) {
  276. BT_ERR("Invalid model init user data");
  277. return;
  278. }
  279. if (*err) {
  280. BT_ERR("Model init failed (err %d)", *err);
  281. return;
  282. }
  283. mod->elem = elem;
  284. if (mod->pub) {
  285. mod->pub->mod = mod;
  286. k_delayed_work_init(&mod->pub->timer, mod_publish);
  287. }
  288. for (i = 0; i < ARRAY_SIZE(mod->keys); i++) {
  289. mod->keys[i] = BLE_MESH_KEY_UNUSED;
  290. }
  291. mod->flags = 0;
  292. mod->elem_idx = elem - comp_0->elem;
  293. if (vnd) {
  294. mod->model_idx = mod - elem->vnd_models;
  295. } else {
  296. mod->model_idx = mod - elem->models;
  297. }
  298. if (vnd) {
  299. return;
  300. }
  301. if (mod->cb && mod->cb->init) {
  302. *err = mod->cb->init(mod);
  303. }
  304. }
  305. int bt_mesh_comp_register(const struct bt_mesh_comp *comp)
  306. {
  307. int err = 0;
  308. /* There must be at least one element */
  309. if (!comp->elem_count) {
  310. return -EINVAL;
  311. }
  312. comp_0 = comp;
  313. bt_mesh_model_foreach(mod_init, &err);
  314. return err;
  315. }
  316. #if CONFIG_BLE_MESH_DEINIT
  317. static void mod_deinit(struct bt_mesh_model *mod, struct bt_mesh_elem *elem,
  318. bool vnd, bool primary, void *user_data)
  319. {
  320. int *err = user_data;
  321. int i;
  322. if (!user_data) {
  323. BT_ERR("Invalid model deinit user data");
  324. return;
  325. }
  326. if (*err) {
  327. BT_ERR("Model deinit failed (err %d)", *err);
  328. return;
  329. }
  330. mod->elem = NULL;
  331. if (mod->pub) {
  332. mod->pub->mod = NULL;
  333. k_delayed_work_free(&mod->pub->timer);
  334. }
  335. for (i = 0; i < ARRAY_SIZE(mod->keys); i++) {
  336. mod->keys[i] = BLE_MESH_KEY_UNUSED;
  337. }
  338. mod->flags = 0U;
  339. mod->elem_idx = 0U;
  340. mod->model_idx = 0U;
  341. if (vnd) {
  342. return;
  343. }
  344. if (mod->cb && mod->cb->deinit) {
  345. *err = mod->cb->deinit(mod);
  346. }
  347. }
  348. int bt_mesh_comp_deregister(void)
  349. {
  350. int err = 0;
  351. if (comp_0 == NULL) {
  352. return -EINVAL;
  353. }
  354. bt_mesh_model_foreach(mod_deinit, &err);
  355. comp_0 = NULL;
  356. return err;
  357. }
  358. #endif /* CONFIG_BLE_MESH_DEINIT */
  359. void bt_mesh_comp_provision(uint16_t addr)
  360. {
  361. int i;
  362. dev_primary_addr = addr;
  363. BT_INFO("Primary address 0x%04x, element count %u", addr, comp_0->elem_count);
  364. for (i = 0; i < comp_0->elem_count; i++) {
  365. struct bt_mesh_elem *elem = &comp_0->elem[i];
  366. elem->addr = addr++;
  367. BT_DBG("addr 0x%04x mod_count %u vnd_mod_count %u",
  368. elem->addr, elem->model_count, elem->vnd_model_count);
  369. }
  370. }
  371. void bt_mesh_comp_unprovision(void)
  372. {
  373. dev_primary_addr = BLE_MESH_ADDR_UNASSIGNED;
  374. }
  375. uint16_t bt_mesh_primary_addr(void)
  376. {
  377. return dev_primary_addr;
  378. }
  379. uint16_t *bt_mesh_model_find_group(struct bt_mesh_model *mod, uint16_t addr)
  380. {
  381. int i;
  382. for (i = 0; i < ARRAY_SIZE(mod->groups); i++) {
  383. if (mod->groups[i] == addr) {
  384. return &mod->groups[i];
  385. }
  386. }
  387. return NULL;
  388. }
  389. static struct bt_mesh_model *bt_mesh_elem_find_group(struct bt_mesh_elem *elem,
  390. uint16_t group_addr)
  391. {
  392. struct bt_mesh_model *model = NULL;
  393. uint16_t *match = NULL;
  394. int i;
  395. for (i = 0; i < elem->model_count; i++) {
  396. model = &elem->models[i];
  397. match = bt_mesh_model_find_group(model, group_addr);
  398. if (match) {
  399. return model;
  400. }
  401. }
  402. for (i = 0; i < elem->vnd_model_count; i++) {
  403. model = &elem->vnd_models[i];
  404. match = bt_mesh_model_find_group(model, group_addr);
  405. if (match) {
  406. return model;
  407. }
  408. }
  409. return NULL;
  410. }
  411. struct bt_mesh_elem *bt_mesh_elem_find(uint16_t addr)
  412. {
  413. uint16_t index = 0U;
  414. if (BLE_MESH_ADDR_IS_UNICAST(addr)) {
  415. index = (addr - comp_0->elem[0].addr);
  416. if (index < comp_0->elem_count) {
  417. return &comp_0->elem[index];
  418. }
  419. return NULL;
  420. }
  421. for (index = 0; index < comp_0->elem_count; index++) {
  422. struct bt_mesh_elem *elem = &comp_0->elem[index];
  423. if (bt_mesh_elem_find_group(elem, addr)) {
  424. return elem;
  425. }
  426. }
  427. return NULL;
  428. }
  429. uint8_t bt_mesh_elem_count(void)
  430. {
  431. return comp_0->elem_count;
  432. }
  433. static bool model_has_key(struct bt_mesh_model *mod, uint16_t key)
  434. {
  435. int i;
  436. for (i = 0; i < ARRAY_SIZE(mod->keys); i++) {
  437. if (mod->keys[i] == key) {
  438. return true;
  439. }
  440. }
  441. return false;
  442. }
  443. static bool model_has_dst(struct bt_mesh_model *model,
  444. struct bt_mesh_subnet *sub,
  445. uint16_t dst)
  446. {
  447. if (BLE_MESH_ADDR_IS_UNICAST(dst)) {
  448. return (comp_0->elem[model->elem_idx].addr == dst);
  449. }
  450. if (BLE_MESH_ADDR_IS_GROUP(dst) || BLE_MESH_ADDR_IS_VIRTUAL(dst)) {
  451. return !!bt_mesh_model_find_group(model, dst);
  452. }
  453. return (model->elem_idx == 0 && (bt_mesh_fixed_group_match(dst) ||
  454. bt_mesh_fixed_direct_match(sub, dst)));
  455. }
  456. static const struct bt_mesh_model_op *find_op(struct bt_mesh_model *models,
  457. uint8_t model_count, uint32_t opcode,
  458. struct bt_mesh_model **model)
  459. {
  460. int i;
  461. for (i = 0; i < model_count; i++) {
  462. const struct bt_mesh_model_op *op;
  463. *model = &models[i];
  464. for (op = (*model)->op; op->func; op++) {
  465. if (op->opcode == opcode) {
  466. return op;
  467. }
  468. }
  469. }
  470. *model = NULL;
  471. return NULL;
  472. }
  473. static int get_opcode(struct net_buf_simple *buf, uint32_t *opcode, bool pull_buf)
  474. {
  475. switch (buf->data[0] >> 6) {
  476. case 0x00:
  477. case 0x01:
  478. if (buf->data[0] == 0x7f) {
  479. BT_ERR("Ignoring RFU OpCode");
  480. return -EINVAL;
  481. }
  482. *opcode = pull_buf ? net_buf_simple_pull_u8(buf) : buf->data[0];
  483. return 0;
  484. case 0x02:
  485. if (buf->len < 2) {
  486. BT_ERR("Too short payload for 2-octet OpCode");
  487. return -EINVAL;
  488. }
  489. *opcode = pull_buf ? net_buf_simple_pull_be16(buf) : sys_get_be16(buf->data);
  490. return 0;
  491. case 0x03:
  492. if (buf->len < 3) {
  493. BT_ERR("Too short payload for 3-octet OpCode");
  494. return -EINVAL;
  495. }
  496. if (pull_buf) {
  497. *opcode = net_buf_simple_pull_u8(buf) << 16;
  498. /* Using LE for the CID since the model layer is defined as
  499. * little-endian in the mesh spec and using BT_MESH_MODEL_OP_3
  500. * will declare the opcode in this way.
  501. */
  502. *opcode |= net_buf_simple_pull_le16(buf);
  503. } else {
  504. *opcode = buf->data[0] << 16 | sys_get_le16(&buf->data[1]);
  505. }
  506. return 0;
  507. }
  508. return -EINVAL;
  509. }
  510. int bt_mesh_get_opcode(struct net_buf_simple *buf,
  511. uint32_t *opcode, bool pull_buf)
  512. {
  513. if (buf == NULL || buf->len == 0 || opcode == NULL) {
  514. BT_ERR("%s, Invalid parameter", __func__);
  515. return -EINVAL;
  516. }
  517. return get_opcode(buf, opcode, pull_buf);
  518. }
  519. bool bt_mesh_fixed_group_match(uint16_t addr)
  520. {
  521. /* Check for fixed group addresses */
  522. switch (addr) {
  523. case BLE_MESH_ADDR_ALL_NODES:
  524. return true;
  525. case BLE_MESH_ADDR_PROXIES:
  526. return (bt_mesh_gatt_proxy_get() == BLE_MESH_GATT_PROXY_ENABLED);
  527. case BLE_MESH_ADDR_FRIENDS:
  528. return (bt_mesh_friend_get() == BLE_MESH_FRIEND_ENABLED);
  529. case BLE_MESH_ADDR_RELAYS:
  530. return (bt_mesh_relay_get() == BLE_MESH_RELAY_ENABLED);
  531. default:
  532. return false;
  533. }
  534. }
  535. bool bt_mesh_fixed_direct_match(struct bt_mesh_subnet *sub, uint16_t addr)
  536. {
  537. /* A message sent to the all-directed-forwarding-nodes address
  538. * shall be processed by the primary element of all nodes that
  539. * have directed forwarding functionality enabled.
  540. */
  541. #if CONFIG_BLE_MESH_DF_SRV
  542. if (addr == BLE_MESH_ADDR_DIRECTS && sub &&
  543. sub->directed_forwarding == BLE_MESH_DIRECTED_FORWARDING_ENABLED) {
  544. return true;
  545. }
  546. #endif
  547. return false;
  548. }
  549. void bt_mesh_model_recv(struct bt_mesh_net_rx *rx, struct net_buf_simple *buf)
  550. {
  551. struct bt_mesh_model *models = NULL, *model = NULL;
  552. const struct bt_mesh_model_op *op = NULL;
  553. uint32_t opcode = 0U;
  554. uint8_t count = 0U;
  555. int i;
  556. BT_INFO("recv, app_idx 0x%04x src 0x%04x dst 0x%04x", rx->ctx.app_idx,
  557. rx->ctx.addr, rx->ctx.recv_dst);
  558. BT_INFO("recv, len %u: %s", buf->len, bt_hex(buf->data, buf->len));
  559. if (get_opcode(buf, &opcode, true) < 0) {
  560. BT_WARN("Unable to decode OpCode");
  561. return;
  562. }
  563. BT_DBG("OpCode 0x%08x", opcode);
  564. for (i = 0; i < comp_0->elem_count; i++) {
  565. struct bt_mesh_elem *elem = &comp_0->elem[i];
  566. struct net_buf_simple_state state = {0};
  567. /* SIG models cannot contain 3-byte (vendor) OpCodes, and
  568. * vendor models cannot contain SIG (1- or 2-byte) OpCodes, so
  569. * we only need to do the lookup in one of the model lists.
  570. */
  571. if (BLE_MESH_MODEL_OP_LEN(opcode) < 3) {
  572. models = elem->models;
  573. count = elem->model_count;
  574. } else {
  575. models = elem->vnd_models;
  576. count = elem->vnd_model_count;
  577. }
  578. op = find_op(models, count, opcode, &model);
  579. if (!op) {
  580. BT_DBG("No OpCode 0x%08x for elem %d", opcode, i);
  581. continue;
  582. }
  583. if (!model_has_key(model, rx->ctx.app_idx)) {
  584. continue;
  585. }
  586. if (!model_has_dst(model, rx->sub, rx->ctx.recv_dst)) {
  587. continue;
  588. }
  589. if (buf->len < op->min_len) {
  590. BT_ERR("Too short message for OpCode 0x%08x", opcode);
  591. continue;
  592. }
  593. /* The following operation is added by Espressif.
  594. * Update the "recv_op" with the opcode got from the buf;
  595. */
  596. rx->ctx.recv_op = opcode;
  597. /* The message is transmitted by a model in response to a message that
  598. * it has received.
  599. *
  600. * The TTL field shall be set to the value of the Default TTL state.
  601. *
  602. * The response message shall use master security credentials. However,
  603. * the security credentials may be changed by a lower layer unless the
  604. * received message uses the master security credentials. If the received
  605. * message uses master security credentials, then the response message
  606. * shall be tagged with the immutable-credentials tag, and the security
  607. * credentials will not be changed by any lower layer.
  608. */
  609. rx->ctx.send_ttl = BLE_MESH_TTL_DEFAULT;
  610. if (rx->ctx.recv_cred == BLE_MESH_FLOODING_CRED) {
  611. rx->ctx.send_tag |= BLE_MESH_TAG_IMMUTABLE_CRED;
  612. rx->ctx.send_cred = BLE_MESH_FLOODING_CRED;
  613. }
  614. /* The callback will likely parse the buffer, so store
  615. * the parsing state in case multiple models receive
  616. * the message.
  617. */
  618. net_buf_simple_save(buf, &state);
  619. op->func(model, &rx->ctx, buf);
  620. net_buf_simple_restore(buf, &state);
  621. }
  622. }
  623. void bt_mesh_model_msg_init(struct net_buf_simple *msg, uint32_t opcode)
  624. {
  625. net_buf_simple_init(msg, 0);
  626. switch (BLE_MESH_MODEL_OP_LEN(opcode)) {
  627. case 1:
  628. net_buf_simple_add_u8(msg, opcode);
  629. break;
  630. case 2:
  631. net_buf_simple_add_be16(msg, opcode);
  632. break;
  633. case 3:
  634. net_buf_simple_add_u8(msg, ((opcode >> 16) & 0xff));
  635. /* Using LE for the CID since the model layer is defined as
  636. * little-endian in the mesh spec and using BT_MESH_MODEL_OP_3
  637. * will declare the opcode in this way.
  638. */
  639. net_buf_simple_add_le16(msg, opcode & 0xffff);
  640. break;
  641. default:
  642. BT_WARN("Unknown opcode format");
  643. break;
  644. }
  645. }
  646. static bool ready_to_send(uint16_t dst)
  647. {
  648. if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_provisioned()) {
  649. return true;
  650. }
  651. if (IS_ENABLED(CONFIG_BLE_MESH_PROVISIONER) && bt_mesh_is_provisioner_en()) {
  652. if (bt_mesh_provisioner_check_msg_dst(dst) == false &&
  653. bt_mesh_elem_find(dst) == false) {
  654. BT_ERR("Failed to find DST 0x%04x", dst);
  655. return false;
  656. }
  657. return true;
  658. }
  659. return false;
  660. }
  661. static int model_send(struct bt_mesh_model *model,
  662. struct bt_mesh_net_tx *tx, bool implicit_bind,
  663. struct net_buf_simple *msg,
  664. const struct bt_mesh_send_cb *cb, void *cb_data)
  665. {
  666. int err = 0;
  667. BT_INFO("send, app_idx 0x%04x src 0x%04x dst 0x%04x",
  668. tx->ctx->app_idx, tx->src, tx->ctx->addr);
  669. BT_INFO("send, len %u: %s", msg->len, bt_hex(msg->data, msg->len));
  670. if (ready_to_send(tx->ctx->addr) == false) {
  671. BT_ERR("Not ready to send");
  672. return -EINVAL;
  673. }
  674. if (tx->ctx->send_ttl != BLE_MESH_TTL_DEFAULT &&
  675. tx->ctx->send_ttl > BLE_MESH_TTL_MAX) {
  676. BT_ERR("Too big send TTL 0x%02x", tx->ctx->send_ttl);
  677. return -EINVAL;
  678. }
  679. if (net_buf_simple_tailroom(msg) < BLE_MESH_MIC_SHORT) {
  680. BT_ERR("Not enough tailroom for TransMIC");
  681. return -EINVAL;
  682. }
  683. if (msg->len > MIN(BLE_MESH_TX_SDU_MAX, BLE_MESH_SDU_MAX_LEN) - BLE_MESH_MIC_SHORT) {
  684. BT_ERR("Too big message (len %d)", msg->len);
  685. return -EMSGSIZE;
  686. }
  687. if (!implicit_bind && !model_has_key(model, tx->ctx->app_idx)) {
  688. BT_ERR("Model not bound to AppKey 0x%04x", tx->ctx->app_idx);
  689. return -EINVAL;
  690. }
  691. if (bt_mesh_valid_security_cred(tx) == false) {
  692. BT_ERR("Can not use security cred 0x%02x", tx->ctx->send_cred);
  693. return -EIO;
  694. }
  695. bt_mesh_choose_better_security_cred(tx);
  696. err = bt_mesh_trans_send(tx, msg, cb, cb_data);
  697. #if CONFIG_BLE_MESH_DF_SRV
  698. bt_mesh_is_directed_path_needed(tx);
  699. #endif
  700. return err;
  701. }
  702. int bt_mesh_model_send_implicit(struct bt_mesh_model *model,
  703. struct bt_mesh_msg_ctx *ctx,bool implicit_bind,
  704. struct net_buf_simple *msg,
  705. const struct bt_mesh_send_cb *cb, void *cb_data)
  706. {
  707. struct bt_mesh_subnet *sub = NULL;
  708. sub = bt_mesh_subnet_get(ctx->net_idx);
  709. if (!sub) {
  710. BT_ERR("Send, NetKey 0x%04x not found", ctx->net_idx);
  711. return -EADDRNOTAVAIL;
  712. }
  713. struct bt_mesh_net_tx tx = {
  714. .sub = sub,
  715. .ctx = ctx,
  716. .src = bt_mesh_model_elem(model)->addr,
  717. .xmit = bt_mesh_net_transmit_get(),
  718. };
  719. return model_send(model, &tx, implicit_bind, msg, cb, cb_data);
  720. }
  721. int bt_mesh_model_send(struct bt_mesh_model *model,
  722. struct bt_mesh_msg_ctx *ctx,
  723. struct net_buf_simple *msg,
  724. const struct bt_mesh_send_cb *cb, void *cb_data)
  725. {
  726. struct bt_mesh_subnet *sub = NULL;
  727. sub = bt_mesh_subnet_get(ctx->net_idx);
  728. if (!sub) {
  729. BT_ERR("Send, NetKey 0x%04x not found", ctx->net_idx);
  730. return -EADDRNOTAVAIL;
  731. }
  732. struct bt_mesh_net_tx tx = {
  733. .sub = sub,
  734. .ctx = ctx,
  735. .src = bt_mesh_model_elem(model)->addr,
  736. .xmit = bt_mesh_net_transmit_get(),
  737. };
  738. return model_send(model, &tx, false, msg, cb, cb_data);
  739. }
  740. int bt_mesh_model_publish(struct bt_mesh_model *model)
  741. {
  742. struct bt_mesh_model_pub *pub = model->pub;
  743. struct bt_mesh_app_key *key = NULL;
  744. struct net_buf_simple *sdu = NULL;
  745. struct bt_mesh_msg_ctx ctx = {0};
  746. struct bt_mesh_net_tx tx = {
  747. .ctx = &ctx,
  748. .src = bt_mesh_model_elem(model)->addr,
  749. .xmit = bt_mesh_net_transmit_get(),
  750. };
  751. int err = 0;
  752. if (!pub || !pub->msg) {
  753. BT_ERR("Model has no publication support");
  754. return -ENOTSUP;
  755. }
  756. if (pub->addr == BLE_MESH_ADDR_UNASSIGNED) {
  757. BT_WARN("Unassigned publish address");
  758. return -EADDRNOTAVAIL;
  759. }
  760. if (pub->msg->len + BLE_MESH_MIC_SHORT > MIN(BLE_MESH_TX_SDU_MAX, BLE_MESH_SDU_MAX_LEN)) {
  761. BT_ERR("Message does not fit maximum SDU size");
  762. return -EMSGSIZE;
  763. }
  764. if (pub->count) {
  765. BT_WARN("Clearing publish retransmit timer");
  766. k_delayed_work_cancel(&pub->timer);
  767. }
  768. key = bt_mesh_app_key_get(pub->key);
  769. if (!key) {
  770. BT_ERR("Publish, AppKey 0x%03x not found", pub->key);
  771. return -EADDRNOTAVAIL;
  772. }
  773. tx.sub = bt_mesh_subnet_get(key->net_idx);
  774. if (!tx.sub) {
  775. BT_ERR("Publish, NetKey 0x%04x not found", ctx.net_idx);
  776. return -EADDRNOTAVAIL;
  777. }
  778. ctx.addr = pub->addr;
  779. ctx.net_idx = key->net_idx;
  780. ctx.app_idx = key->app_idx;
  781. ctx.send_ttl = pub->ttl;
  782. ctx.send_cred = pub->cred ? BLE_MESH_FRIENDSHIP_CRED : BLE_MESH_FLOODING_CRED;
  783. ctx.send_szmic = pub->send_szmic;
  784. #if 0
  785. ctx.send_tag |= BLE_MESH_TAG_IMMUTABLE_CRED;
  786. #endif
  787. if (pub->send_rel) {
  788. /* Tag with send-segmented */
  789. ctx.send_tag |= BLE_MESH_TAG_SEND_SEGMENTED;
  790. }
  791. #if CONFIG_BLE_MESH_DF_SRV
  792. bt_mesh_model_pub_use_directed(&tx, pub->directed_pub_policy);
  793. #endif
  794. pub->count = BLE_MESH_PUB_TRANSMIT_COUNT(pub->retransmit);
  795. BT_INFO("Publish Retransmit Count %u Interval %ums", pub->count,
  796. BLE_MESH_PUB_TRANSMIT_INT(pub->retransmit));
  797. sdu = bt_mesh_alloc_buf(pub->msg->len + BLE_MESH_MIC_LONG);
  798. if (!sdu) {
  799. BT_ERR("%s, Out of memory", __func__);
  800. return -ENOMEM;
  801. }
  802. net_buf_simple_add_mem(sdu, pub->msg->data, pub->msg->len);
  803. err = model_send(model, &tx, true, sdu, &pub_sent_cb, model);
  804. if (err) {
  805. publish_retransmit_end(err, pub);
  806. }
  807. bt_mesh_free_buf(sdu);
  808. return err;
  809. }
  810. struct bt_mesh_model *bt_mesh_model_find_vnd(struct bt_mesh_elem *elem,
  811. uint16_t company, uint16_t id)
  812. {
  813. int i;
  814. for (i = 0; i < elem->vnd_model_count; i++) {
  815. if (elem->vnd_models[i].vnd.company == company &&
  816. elem->vnd_models[i].vnd.id == id) {
  817. return &elem->vnd_models[i];
  818. }
  819. }
  820. return NULL;
  821. }
  822. struct bt_mesh_model *bt_mesh_model_find(struct bt_mesh_elem *elem, uint16_t id)
  823. {
  824. int i;
  825. for (i = 0; i < elem->model_count; i++) {
  826. if (elem->models[i].id == id) {
  827. return &elem->models[i];
  828. }
  829. }
  830. return NULL;
  831. }
  832. const struct bt_mesh_comp *bt_mesh_comp_get(void)
  833. {
  834. return comp_0;
  835. }
  836. const uint8_t *bt_mesh_dev_key_get(uint16_t dst)
  837. {
  838. const uint8_t *key = NULL;
  839. if (IS_ENABLED(CONFIG_BLE_MESH_NODE) && bt_mesh_is_provisioned()) {
  840. if (!IS_ENABLED(CONFIG_BLE_MESH_FAST_PROV)) {
  841. key = bt_mesh.dev_key;
  842. } else {
  843. key = bt_mesh_fast_prov_dev_key_get(dst);
  844. }
  845. } else if (IS_ENABLED(CONFIG_BLE_MESH_PROVISIONER) && bt_mesh_is_provisioner_en()) {
  846. key = bt_mesh_provisioner_dev_key_get(dst);
  847. }
  848. return key;
  849. }
  850. size_t bt_mesh_rx_netkey_size(void)
  851. {
  852. size_t size = 0U;
  853. #if CONFIG_BLE_MESH_NODE && !CONFIG_BLE_MESH_PROVISIONER
  854. if (bt_mesh_is_provisioned()) {
  855. size = ARRAY_SIZE(bt_mesh.sub);
  856. }
  857. #endif
  858. #if !CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER
  859. if (bt_mesh_is_provisioner_en()) {
  860. size = ARRAY_SIZE(bt_mesh.p_sub);
  861. }
  862. #endif
  863. #if CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER
  864. size = ARRAY_SIZE(bt_mesh.sub);
  865. if (bt_mesh_is_provisioner_en()) {
  866. size += ARRAY_SIZE(bt_mesh.p_sub);
  867. }
  868. #endif
  869. return size;
  870. }
  871. struct bt_mesh_subnet *bt_mesh_rx_netkey_get(size_t index)
  872. {
  873. struct bt_mesh_subnet *sub = NULL;
  874. #if CONFIG_BLE_MESH_NODE && !CONFIG_BLE_MESH_PROVISIONER
  875. if (bt_mesh_is_provisioned()) {
  876. sub = &bt_mesh.sub[index];
  877. }
  878. #endif
  879. #if !CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER
  880. if (bt_mesh_is_provisioner_en()) {
  881. sub = bt_mesh.p_sub[index];
  882. }
  883. #endif
  884. #if CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER
  885. if (index < ARRAY_SIZE(bt_mesh.sub)) {
  886. sub = &bt_mesh.sub[index];
  887. } else {
  888. sub = bt_mesh.p_sub[index - ARRAY_SIZE(bt_mesh.sub)];
  889. }
  890. #endif
  891. return sub;
  892. }
  893. size_t bt_mesh_rx_devkey_size(void)
  894. {
  895. size_t size = 0U;
  896. #if CONFIG_BLE_MESH_NODE && !CONFIG_BLE_MESH_PROVISIONER
  897. if (bt_mesh_is_provisioned()) {
  898. size = 1;
  899. if (bt_mesh_dev_key_ca_valid()) {
  900. size += 1;
  901. }
  902. }
  903. #endif
  904. #if !CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER
  905. if (bt_mesh_is_provisioner_en()) {
  906. size = 1;
  907. }
  908. #endif
  909. #if CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER
  910. size = 1;
  911. if (bt_mesh_dev_key_ca_valid()) {
  912. size += 1;
  913. }
  914. if (bt_mesh_is_provisioner_en()) {
  915. size += 1;
  916. }
  917. #endif
  918. return size;
  919. }
  920. const uint8_t *bt_mesh_rx_devkey_get(size_t index, uint16_t src)
  921. {
  922. const uint8_t *key = NULL;
  923. #if CONFIG_BLE_MESH_NODE && !CONFIG_BLE_MESH_PROVISIONER
  924. if (bt_mesh_is_provisioned()) {
  925. if (index == 0) {
  926. key = bt_mesh.dev_key;
  927. } else if (index == 1) {
  928. key = bt_mesh.dev_key_ca;
  929. }
  930. }
  931. #endif
  932. #if !CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER
  933. if (bt_mesh_is_provisioner_en()) {
  934. key = bt_mesh_provisioner_dev_key_get(src);
  935. }
  936. #endif
  937. #if CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER
  938. if (index == 0) {
  939. key = bt_mesh.dev_key;
  940. } else if (index == 1 && bt_mesh_dev_key_ca_valid()) {
  941. /* If index == 1, there are two cases.
  942. * 1. bt_mesh_dev_key_ca_valid() is true, it should be return bt_mesh.dev_key_ca.
  943. * 2. bt_mesh_is_provisioner_en() is true, it should be return bt_mesh_provisioner_dev_key_get(src).
  944. *
  945. * If index == 2, that means bt_mesh_dev_key_ca_valid() and bt_mesh_is_provisioner_en() are true.
  946. * So the previous round of function bt_mesh_rx_devkey_get(1, src) will return bt_mesh.dev_key_ca.
  947. * Then this round of function bt_mesh_rx_devkey_get(2, src) will return bt_mesh_provisioner_dev_key_get(src).
  948. */
  949. key = bt_mesh.dev_key_ca;
  950. } else {
  951. key = bt_mesh_provisioner_dev_key_get(src);
  952. }
  953. #endif
  954. return key;
  955. }
  956. size_t bt_mesh_rx_appkey_size(void)
  957. {
  958. size_t size = 0U;
  959. #if CONFIG_BLE_MESH_NODE && !CONFIG_BLE_MESH_PROVISIONER
  960. if (bt_mesh_is_provisioned()) {
  961. size = ARRAY_SIZE(bt_mesh.app_keys);
  962. }
  963. #endif
  964. #if !CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER
  965. if (bt_mesh_is_provisioner_en()) {
  966. size = ARRAY_SIZE(bt_mesh.p_app_keys);
  967. }
  968. #endif
  969. #if CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER
  970. size = ARRAY_SIZE(bt_mesh.app_keys);
  971. if (bt_mesh_is_provisioner_en()) {
  972. size += ARRAY_SIZE(bt_mesh.p_app_keys);
  973. }
  974. #endif
  975. return size;
  976. }
  977. struct bt_mesh_app_key *bt_mesh_rx_appkey_get(size_t index)
  978. {
  979. struct bt_mesh_app_key *key = NULL;
  980. #if CONFIG_BLE_MESH_NODE && !CONFIG_BLE_MESH_PROVISIONER
  981. if (bt_mesh_is_provisioned()) {
  982. key = &bt_mesh.app_keys[index];
  983. }
  984. #endif
  985. #if !CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER
  986. if (bt_mesh_is_provisioner_en()) {
  987. key = bt_mesh.p_app_keys[index];
  988. }
  989. #endif
  990. #if CONFIG_BLE_MESH_NODE && CONFIG_BLE_MESH_PROVISIONER
  991. if (index < ARRAY_SIZE(bt_mesh.app_keys)) {
  992. key = &bt_mesh.app_keys[index];
  993. } else {
  994. key = bt_mesh.p_app_keys[index - ARRAY_SIZE(bt_mesh.app_keys)];
  995. }
  996. #endif
  997. return key;
  998. }