client_common.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. /*
  2. * SPDX-FileCopyrightText: 2017-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <string.h>
  7. #include <errno.h>
  8. #include "mesh.h"
  9. #include "mesh_main.h"
  10. #include "transport.h"
  11. #include "foundation.h"
  12. #include "client_common.h"
  13. #include "mesh_common.h"
  14. #define HCI_TIME_FOR_START_ADV K_MSEC(5) /* Three adv related hci commands may take 4 ~ 5ms */
  15. static bt_mesh_client_node_t *bt_mesh_client_pick_node(sys_slist_t *list, uint16_t tx_dst)
  16. {
  17. bt_mesh_client_node_t *node = NULL;
  18. sys_snode_t *cur = NULL;
  19. bt_mesh_list_lock();
  20. if (sys_slist_is_empty(list)) {
  21. bt_mesh_list_unlock();
  22. return NULL;
  23. }
  24. for (cur = sys_slist_peek_head(list);
  25. cur != NULL; cur = sys_slist_peek_next(cur)) {
  26. node = (bt_mesh_client_node_t *)cur;
  27. if (node->ctx.addr == tx_dst) {
  28. bt_mesh_list_unlock();
  29. return node;
  30. }
  31. }
  32. bt_mesh_list_unlock();
  33. return NULL;
  34. }
  35. bt_mesh_client_node_t *bt_mesh_is_client_recv_publish_msg(struct bt_mesh_model *model,
  36. struct bt_mesh_msg_ctx *ctx,
  37. struct net_buf_simple *buf, bool need_pub)
  38. {
  39. bt_mesh_client_internal_data_t *data = NULL;
  40. bt_mesh_client_user_data_t *cli = NULL;
  41. bt_mesh_client_node_t *node = NULL;
  42. if (!model || !ctx || !buf) {
  43. BT_ERR("%s, Invalid parameter", __func__);
  44. return NULL;
  45. }
  46. cli = (bt_mesh_client_user_data_t *)model->user_data;
  47. if (!cli) {
  48. BT_ERR("Invalid client user data");
  49. return NULL;
  50. }
  51. /** If the received message address is not a unicast address,
  52. * the address may be a group/virtual address, and we push
  53. * this message to the application layer.
  54. */
  55. if (!BLE_MESH_ADDR_IS_UNICAST(ctx->recv_dst)) {
  56. BT_DBG("Unexpected status message 0x%08x", ctx->recv_op);
  57. if (cli->publish_status && need_pub) {
  58. cli->publish_status(ctx->recv_op, model, ctx, buf);
  59. }
  60. return NULL;
  61. }
  62. /** If the source address of the received status message is
  63. * different with the destination address of the sending
  64. * message, then the message is from another element and
  65. * push it to application layer.
  66. */
  67. data = (bt_mesh_client_internal_data_t *)cli->internal_data;
  68. if (!data) {
  69. BT_ERR("Invalid client internal data");
  70. return NULL;
  71. }
  72. if ((node = bt_mesh_client_pick_node(&data->queue, ctx->addr)) == NULL) {
  73. BT_DBG("Unexpected status message 0x%08x", ctx->recv_op);
  74. if (cli->publish_status && need_pub) {
  75. cli->publish_status(ctx->recv_op, model, ctx, buf);
  76. }
  77. return NULL;
  78. }
  79. if (node->op_pending != ctx->recv_op) {
  80. BT_DBG("Unexpected status message 0x%08x", ctx->recv_op);
  81. if (cli->publish_status && need_pub) {
  82. cli->publish_status(ctx->recv_op, model, ctx, buf);
  83. }
  84. return NULL;
  85. }
  86. if (k_delayed_work_remaining_get(&node->timer) == 0) {
  87. BT_DBG("Unexpected status message 0x%08x", ctx->recv_op);
  88. if (cli->publish_status && need_pub) {
  89. cli->publish_status(ctx->recv_op, model, ctx, buf);
  90. }
  91. return NULL;
  92. }
  93. return node;
  94. }
  95. static bool bt_mesh_client_check_node_in_list(sys_slist_t *list, uint16_t tx_dst)
  96. {
  97. bt_mesh_client_node_t *node = NULL;
  98. sys_snode_t *cur = NULL;
  99. bt_mesh_list_lock();
  100. if (sys_slist_is_empty(list)) {
  101. bt_mesh_list_unlock();
  102. return false;
  103. }
  104. for (cur = sys_slist_peek_head(list);
  105. cur != NULL; cur = sys_slist_peek_next(cur)) {
  106. node = (bt_mesh_client_node_t *)cur;
  107. if (node->ctx.addr == tx_dst) {
  108. bt_mesh_list_unlock();
  109. return true;
  110. }
  111. }
  112. bt_mesh_list_unlock();
  113. return false;
  114. }
  115. static uint32_t bt_mesh_client_get_status_op(const bt_mesh_client_op_pair_t *op_pair,
  116. int size, uint32_t opcode)
  117. {
  118. if (!op_pair || size == 0) {
  119. return 0;
  120. }
  121. const bt_mesh_client_op_pair_t *op = op_pair;
  122. for (int i = 0; i < size; i++) {
  123. if (op->cli_op == opcode) {
  124. return op->status_op;
  125. }
  126. op++;
  127. }
  128. return 0;
  129. }
  130. static int32_t bt_mesh_get_adv_duration(void)
  131. {
  132. uint16_t duration, adv_int;
  133. uint8_t xmit;
  134. xmit = bt_mesh_net_transmit_get(); /* Network transmit */
  135. adv_int = BLE_MESH_TRANSMIT_INT(xmit);
  136. duration = (BLE_MESH_TRANSMIT_COUNT(xmit) + 1) * (adv_int + 10);
  137. return (int32_t)duration;
  138. }
  139. static int32_t bt_mesh_client_calc_timeout(struct bt_mesh_msg_ctx *ctx,
  140. struct net_buf_simple *msg,
  141. uint32_t opcode, int32_t timeout)
  142. {
  143. int32_t seg_retrans_to = 0, duration = 0, time = 0;
  144. uint8_t seg_count = 0, seg_retrans_num = 0;
  145. bool need_seg = false;
  146. uint8_t mic_size = 0;
  147. if (msg->len > BLE_MESH_SDU_UNSEG_MAX || ctx->send_rel) {
  148. need_seg = true; /* Needs segmentation */
  149. }
  150. mic_size = (need_seg && net_buf_simple_tailroom(msg) >= BLE_MESH_MIC_LONG) ?
  151. BLE_MESH_MIC_LONG : BLE_MESH_MIC_SHORT;
  152. if (need_seg) {
  153. /* Based on the message length, calculate how many segments are needed.
  154. * All the messages sent from here are access messages.
  155. */
  156. seg_retrans_num = bt_mesh_get_seg_retrans_num();
  157. seg_retrans_to = bt_mesh_get_seg_retrans_timeout(ctx->send_ttl);
  158. seg_count = (msg->len + mic_size - 1) / 12U + 1U;
  159. duration = bt_mesh_get_adv_duration();
  160. /* Currenlty only consider the time consumption of the same segmented
  161. * messages, but if there are other messages between any two retrans-
  162. * missions of the same segmented messages, then the whole time will
  163. * be longer.
  164. *
  165. * Since the transport behavior has been changed, i.e. start retransmit
  166. * timer after the last segment is sent, so we can simplify the timeout
  167. * calculation here. And the retransmit timer will be started event if
  168. * the attempts reaches ZERO when the dst is a unicast address.
  169. */
  170. int32_t seg_duration = seg_count * (duration + HCI_TIME_FOR_START_ADV);
  171. time = (seg_duration + seg_retrans_to) * seg_retrans_num;
  172. BT_INFO("Original timeout %dms, calculated timeout %dms", timeout, time);
  173. if (time < timeout) {
  174. /* If the calculated time is smaller than the input timeout value,
  175. * then use the original timeout value.
  176. */
  177. time = timeout;
  178. }
  179. } else {
  180. /* For unsegmented access messages, directly use the timeout
  181. * value from the application layer.
  182. */
  183. time = timeout;
  184. }
  185. BT_INFO("Client message 0x%08x with timeout %dms", opcode, time);
  186. return time;
  187. }
  188. static void msg_send_start(uint16_t duration, int err, void *cb_data)
  189. {
  190. bt_mesh_client_node_t *node = cb_data;
  191. BT_DBG("%s, duration %ums", __func__, duration);
  192. if (err) {
  193. if (!k_delayed_work_free(&node->timer)) {
  194. bt_mesh_client_free_node(node);
  195. }
  196. return;
  197. }
  198. k_delayed_work_submit(&node->timer, node->timeout);
  199. }
  200. static const struct bt_mesh_send_cb send_cb = {
  201. .start = msg_send_start,
  202. .end = NULL,
  203. };
  204. int bt_mesh_client_send_msg(bt_mesh_client_common_param_t *param,
  205. struct net_buf_simple *msg, bool need_ack,
  206. k_work_handler_t timer_handler)
  207. {
  208. bt_mesh_client_internal_data_t *internal = NULL;
  209. bt_mesh_client_user_data_t *client = NULL;
  210. bt_mesh_client_node_t *node = NULL;
  211. int err = 0;
  212. if (!param || !param->model || !msg) {
  213. BT_ERR("%s, Invalid parameter", __func__);
  214. return -EINVAL;
  215. }
  216. client = (bt_mesh_client_user_data_t *)param->model->user_data;
  217. if (!client) {
  218. BT_ERR("Invalid client user data");
  219. return -EINVAL;
  220. }
  221. internal = (bt_mesh_client_internal_data_t *)client->internal_data;
  222. if (!internal) {
  223. BT_ERR("Invalid client internal data");
  224. return -EINVAL;
  225. }
  226. if (param->ctx.addr == BLE_MESH_ADDR_UNASSIGNED) {
  227. BT_ERR("Invalid DST 0x%04x", param->ctx.addr);
  228. return -EINVAL;
  229. }
  230. if (bt_mesh_set_client_model_role(param->model, param->msg_role)) {
  231. BT_ERR("Failed to set client role");
  232. return -EIO;
  233. }
  234. if (need_ack == false || !BLE_MESH_ADDR_IS_UNICAST(param->ctx.addr)) {
  235. /* 1. If this is an unacknowledged message, send it directly.
  236. * 2. If this is an acknowledged message, but the destination
  237. * is not a unicast address, e.g. a group/virtual address,
  238. * then all the corresponding responses will be treated as
  239. * publish messages, and no timeout will be used.
  240. */
  241. err = bt_mesh_model_send(param->model, &param->ctx, msg, param->cb, param->cb_data);
  242. if (err) {
  243. BT_ERR("Failed to send client message 0x%08x", param->opcode);
  244. }
  245. return err;
  246. }
  247. if (!timer_handler) {
  248. BT_ERR("Invalid timeout handler");
  249. return -EINVAL;
  250. }
  251. if (bt_mesh_client_check_node_in_list(&internal->queue, param->ctx.addr)) {
  252. BT_ERR("Busy sending message to DST 0x%04x", param->ctx.addr);
  253. return -EBUSY;
  254. }
  255. /* Don't forget to free the node in the timeout (timer_handler) function. */
  256. node = (bt_mesh_client_node_t *)bt_mesh_calloc(sizeof(bt_mesh_client_node_t));
  257. if (!node) {
  258. BT_ERR("%s, Out of memory", __func__);
  259. return -ENOMEM;
  260. }
  261. memcpy(&node->ctx, &param->ctx, sizeof(struct bt_mesh_msg_ctx));
  262. node->ctx.model = param->model;
  263. node->opcode = param->opcode;
  264. node->op_pending = bt_mesh_client_get_status_op(client->op_pair, client->op_pair_size, param->opcode);
  265. if (node->op_pending == 0U) {
  266. BT_ERR("Not found the status opcode in op_pair list");
  267. bt_mesh_free(node);
  268. return -EINVAL;
  269. }
  270. node->timeout = bt_mesh_client_calc_timeout(&param->ctx, msg, param->opcode,
  271. param->msg_timeout ? param->msg_timeout : CONFIG_BLE_MESH_CLIENT_MSG_TIMEOUT);
  272. if (k_delayed_work_init(&node->timer, timer_handler)) {
  273. BT_ERR("Failed to create a timer");
  274. bt_mesh_free(node);
  275. return -EIO;
  276. }
  277. bt_mesh_list_lock();
  278. sys_slist_append(&internal->queue, &node->client_node);
  279. bt_mesh_list_unlock();
  280. /* "bt_mesh_model_send" will post the mesh packet to the mesh adv queue.
  281. * Due to the higher priority of adv_thread (than btc task), we need to
  282. * send the packet after the list item "node" is initialized properly.
  283. */
  284. err = bt_mesh_model_send(param->model, &param->ctx, msg, &send_cb, node);
  285. if (err) {
  286. BT_ERR("Failed to send client message 0x%08x", node->opcode);
  287. k_delayed_work_free(&node->timer);
  288. bt_mesh_client_free_node(node);
  289. }
  290. return err;
  291. }
  292. static bt_mesh_mutex_t client_model_lock;
  293. static inline void bt_mesh_client_model_mutex_new(void)
  294. {
  295. if (!client_model_lock.mutex) {
  296. bt_mesh_mutex_create(&client_model_lock);
  297. }
  298. }
  299. #if CONFIG_BLE_MESH_DEINIT
  300. static inline void bt_mesh_client_model_mutex_free(void)
  301. {
  302. bt_mesh_mutex_free(&client_model_lock);
  303. }
  304. #endif /* CONFIG_BLE_MESH_DEINIT */
  305. void bt_mesh_client_model_lock(void)
  306. {
  307. bt_mesh_mutex_lock(&client_model_lock);
  308. }
  309. void bt_mesh_client_model_unlock(void)
  310. {
  311. bt_mesh_mutex_unlock(&client_model_lock);
  312. }
  313. int bt_mesh_client_init(struct bt_mesh_model *model)
  314. {
  315. bt_mesh_client_internal_data_t *data = NULL;
  316. bt_mesh_client_user_data_t *client = NULL;
  317. if (!model || !model->op) {
  318. BT_ERR("Invalid vendor client model");
  319. return -EINVAL;
  320. }
  321. client = (bt_mesh_client_user_data_t *)model->user_data;
  322. if (!client) {
  323. BT_ERR("No vendor client context provided");
  324. return -EINVAL;
  325. }
  326. if (!client->internal_data) {
  327. data = bt_mesh_calloc(sizeof(bt_mesh_client_internal_data_t));
  328. if (!data) {
  329. BT_ERR("%s, Out of memory", __func__);
  330. return -ENOMEM;
  331. }
  332. /* Init the client data queue */
  333. sys_slist_init(&data->queue);
  334. client->model = model;
  335. client->internal_data = data;
  336. } else {
  337. bt_mesh_client_clear_list(client->internal_data);
  338. }
  339. bt_mesh_client_model_mutex_new();
  340. return 0;
  341. }
  342. #if CONFIG_BLE_MESH_DEINIT
  343. int bt_mesh_client_deinit(struct bt_mesh_model *model)
  344. {
  345. bt_mesh_client_user_data_t *client = NULL;
  346. if (!model) {
  347. BT_ERR("Invalid vendor client model");
  348. return -EINVAL;
  349. }
  350. client = (bt_mesh_client_user_data_t *)model->user_data;
  351. if (!client) {
  352. BT_ERR("No vendor client context provided");
  353. return -EINVAL;
  354. }
  355. if (client->internal_data) {
  356. /* Remove items from the list */
  357. bt_mesh_client_clear_list(client->internal_data);
  358. /* Free the allocated internal data */
  359. bt_mesh_free(client->internal_data);
  360. client->internal_data = NULL;
  361. }
  362. bt_mesh_client_model_mutex_free();
  363. return 0;
  364. }
  365. #endif /* CONFIG_BLE_MESH_DEINIT */
  366. int bt_mesh_client_free_node(bt_mesh_client_node_t *node)
  367. {
  368. bt_mesh_client_internal_data_t *internal = NULL;
  369. bt_mesh_client_user_data_t *client = NULL;
  370. if (!node || !node->ctx.model) {
  371. BT_ERR("Invalid client list item");
  372. return -EINVAL;
  373. }
  374. client = (bt_mesh_client_user_data_t *)node->ctx.model->user_data;
  375. if (!client) {
  376. BT_ERR("Invalid client user data");
  377. return -EINVAL;
  378. }
  379. internal = (bt_mesh_client_internal_data_t *)client->internal_data;
  380. if (!internal) {
  381. BT_ERR("Invalid client internal data");
  382. return -EINVAL;
  383. }
  384. // Release the client node from the queue
  385. bt_mesh_list_lock();
  386. sys_slist_find_and_remove(&internal->queue, &node->client_node);
  387. bt_mesh_list_unlock();
  388. // Free the node
  389. bt_mesh_free(node);
  390. return 0;
  391. }
  392. int bt_mesh_client_clear_list(void *data)
  393. {
  394. bt_mesh_client_internal_data_t *internal = NULL;
  395. bt_mesh_client_node_t *node = NULL;
  396. if (!data) {
  397. BT_ERR("%s, Invalid parameter", __func__);
  398. return -EINVAL;
  399. }
  400. internal = (bt_mesh_client_internal_data_t *)data;
  401. bt_mesh_list_lock();
  402. while (!sys_slist_is_empty(&internal->queue)) {
  403. node = (void *)sys_slist_get_not_empty(&internal->queue);
  404. k_delayed_work_free(&node->timer);
  405. bt_mesh_free(node);
  406. }
  407. bt_mesh_list_unlock();
  408. return 0;
  409. }
  410. int bt_mesh_set_client_model_role(struct bt_mesh_model *model, uint8_t role)
  411. {
  412. bt_mesh_client_user_data_t *client = NULL;
  413. if (!model) {
  414. BT_ERR("Invalid client model");
  415. return -EINVAL;
  416. }
  417. client = (bt_mesh_client_user_data_t *)model->user_data;
  418. if (!client) {
  419. BT_ERR("Invalid client user data");
  420. return -EINVAL;
  421. }
  422. if (role >= ROLE_NVAL) {
  423. BT_ERR("Invalid client role 0x%02x", role);
  424. return -EINVAL;
  425. }
  426. client->msg_role = role;
  427. return 0;
  428. }