client_common.c 14 KB

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