client_common.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. // Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #include <string.h>
  14. #include <errno.h>
  15. #include "mesh.h"
  16. #include "mesh_main.h"
  17. #include "transport.h"
  18. #include "foundation.h"
  19. #include "client_common.h"
  20. #include "mesh_common.h"
  21. #define HCI_TIME_FOR_START_ADV K_MSEC(5) /* Three adv related hci commands may take 4 ~ 5ms */
  22. static bt_mesh_client_node_t *bt_mesh_client_pick_node(sys_slist_t *list, uint16_t tx_dst)
  23. {
  24. bt_mesh_client_node_t *node = NULL;
  25. sys_snode_t *cur = NULL;
  26. bt_mesh_list_lock();
  27. if (sys_slist_is_empty(list)) {
  28. bt_mesh_list_unlock();
  29. return NULL;
  30. }
  31. for (cur = sys_slist_peek_head(list);
  32. cur != NULL; cur = sys_slist_peek_next(cur)) {
  33. node = (bt_mesh_client_node_t *)cur;
  34. if (node->ctx.addr == tx_dst) {
  35. bt_mesh_list_unlock();
  36. return node;
  37. }
  38. }
  39. bt_mesh_list_unlock();
  40. return NULL;
  41. }
  42. bt_mesh_client_node_t *bt_mesh_is_client_recv_publish_msg(struct bt_mesh_model *model,
  43. struct bt_mesh_msg_ctx *ctx,
  44. struct net_buf_simple *buf, bool need_pub)
  45. {
  46. bt_mesh_client_internal_data_t *data = NULL;
  47. bt_mesh_client_user_data_t *cli = NULL;
  48. bt_mesh_client_node_t *node = NULL;
  49. if (!model || !ctx || !buf) {
  50. BT_ERR("%s, Invalid parameter", __func__);
  51. return NULL;
  52. }
  53. cli = (bt_mesh_client_user_data_t *)model->user_data;
  54. if (!cli) {
  55. BT_ERR("Invalid client user data");
  56. return NULL;
  57. }
  58. /** If the received message address is not a unicast address,
  59. * the address may be a group/virtual address, and we push
  60. * this message to the application layer.
  61. */
  62. if (!BLE_MESH_ADDR_IS_UNICAST(ctx->recv_dst)) {
  63. BT_DBG("Unexpected status message 0x%08x", ctx->recv_op);
  64. if (cli->publish_status && need_pub) {
  65. cli->publish_status(ctx->recv_op, model, ctx, buf);
  66. }
  67. return NULL;
  68. }
  69. /** If the source address of the received status message is
  70. * different with the destination address of the sending
  71. * message, then the message is from another element and
  72. * push it to application layer.
  73. */
  74. data = (bt_mesh_client_internal_data_t *)cli->internal_data;
  75. if (!data) {
  76. BT_ERR("Invalid client internal data");
  77. return NULL;
  78. }
  79. if ((node = bt_mesh_client_pick_node(&data->queue, ctx->addr)) == NULL) {
  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 (node->op_pending != ctx->recv_op) {
  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. if (k_delayed_work_remaining_get(&node->timer) == 0) {
  94. BT_DBG("Unexpected status message 0x%08x", ctx->recv_op);
  95. if (cli->publish_status && need_pub) {
  96. cli->publish_status(ctx->recv_op, model, ctx, buf);
  97. }
  98. return NULL;
  99. }
  100. return node;
  101. }
  102. static bool bt_mesh_client_check_node_in_list(sys_slist_t *list, uint16_t tx_dst)
  103. {
  104. bt_mesh_client_node_t *node = NULL;
  105. sys_snode_t *cur = NULL;
  106. bt_mesh_list_lock();
  107. if (sys_slist_is_empty(list)) {
  108. bt_mesh_list_unlock();
  109. return false;
  110. }
  111. for (cur = sys_slist_peek_head(list);
  112. cur != NULL; cur = sys_slist_peek_next(cur)) {
  113. node = (bt_mesh_client_node_t *)cur;
  114. if (node->ctx.addr == tx_dst) {
  115. bt_mesh_list_unlock();
  116. return true;
  117. }
  118. }
  119. bt_mesh_list_unlock();
  120. return false;
  121. }
  122. static uint32_t bt_mesh_client_get_status_op(const bt_mesh_client_op_pair_t *op_pair,
  123. int size, uint32_t opcode)
  124. {
  125. if (!op_pair || size == 0) {
  126. return 0;
  127. }
  128. const bt_mesh_client_op_pair_t *op = op_pair;
  129. for (int i = 0; i < size; i++) {
  130. if (op->cli_op == opcode) {
  131. return op->status_op;
  132. }
  133. op++;
  134. }
  135. return 0;
  136. }
  137. static int32_t bt_mesh_get_adv_duration(void)
  138. {
  139. uint16_t duration, adv_int;
  140. uint8_t xmit;
  141. xmit = bt_mesh_net_transmit_get(); /* Network transmit */
  142. adv_int = BLE_MESH_TRANSMIT_INT(xmit);
  143. duration = (BLE_MESH_TRANSMIT_COUNT(xmit) + 1) * (adv_int + 10);
  144. return (int32_t)duration;
  145. }
  146. static int32_t bt_mesh_client_calc_timeout(struct bt_mesh_msg_ctx *ctx,
  147. struct net_buf_simple *msg,
  148. uint32_t opcode, int32_t timeout)
  149. {
  150. int32_t seg_retrans_to = 0, duration = 0, time = 0;
  151. uint8_t seg_count = 0, seg_retrans_num = 0;
  152. bool need_seg = false;
  153. uint8_t mic_size = 0;
  154. if (msg->len > BLE_MESH_SDU_UNSEG_MAX || ctx->send_rel) {
  155. need_seg = true; /* Needs segmentation */
  156. }
  157. mic_size = (need_seg && net_buf_simple_tailroom(msg) >= BLE_MESH_MIC_LONG) ?
  158. BLE_MESH_MIC_LONG : BLE_MESH_MIC_SHORT;
  159. if (need_seg) {
  160. /* Based on the message length, calculate how many segments are needed.
  161. * All the messages sent from here are access messages.
  162. */
  163. seg_retrans_num = bt_mesh_get_seg_retrans_num();
  164. seg_retrans_to = bt_mesh_get_seg_retrans_timeout(ctx->send_ttl);
  165. seg_count = (msg->len + mic_size - 1) / 12U + 1U;
  166. duration = bt_mesh_get_adv_duration();
  167. /* Currenlty only consider the time consumption of the same segmented
  168. * messages, but if there are other messages between any two retrans-
  169. * missions of the same segmented messages, then the whole time will
  170. * be longer.
  171. *
  172. * Since the transport behavior has been changed, i.e. start retransmit
  173. * timer after the last segment is sent, so we can simplify the timeout
  174. * calculation here. And the retransmit timer will be started event if
  175. * the attempts reaches ZERO when the dst is a unicast address.
  176. */
  177. int32_t seg_duration = seg_count * (duration + HCI_TIME_FOR_START_ADV);
  178. time = (seg_duration + seg_retrans_to) * seg_retrans_num;
  179. BT_INFO("Original timeout %dms, calculated timeout %dms", timeout, time);
  180. if (time < timeout) {
  181. /* If the calculated time is smaller than the input timeout value,
  182. * then use the original timeout value.
  183. */
  184. time = timeout;
  185. }
  186. } else {
  187. /* For unsegmented access messages, directly use the timeout
  188. * value from the application layer.
  189. */
  190. time = timeout;
  191. }
  192. BT_INFO("Client message 0x%08x with timeout %dms", opcode, time);
  193. return time;
  194. }
  195. static void msg_send_start(uint16_t duration, int err, void *cb_data)
  196. {
  197. bt_mesh_client_node_t *node = cb_data;
  198. BT_DBG("%s, duration %ums", __func__, duration);
  199. if (err) {
  200. if (!k_delayed_work_free(&node->timer)) {
  201. bt_mesh_client_free_node(node);
  202. }
  203. return;
  204. }
  205. k_delayed_work_submit(&node->timer, node->timeout);
  206. }
  207. static const struct bt_mesh_send_cb send_cb = {
  208. .start = msg_send_start,
  209. .end = NULL,
  210. };
  211. int bt_mesh_client_send_msg(bt_mesh_client_common_param_t *param,
  212. struct net_buf_simple *msg, bool need_ack,
  213. k_work_handler_t timer_handler)
  214. {
  215. bt_mesh_client_internal_data_t *internal = NULL;
  216. bt_mesh_client_user_data_t *client = NULL;
  217. bt_mesh_client_node_t *node = NULL;
  218. int err = 0;
  219. if (!param || !param->model || !msg) {
  220. BT_ERR("%s, Invalid parameter", __func__);
  221. return -EINVAL;
  222. }
  223. client = (bt_mesh_client_user_data_t *)param->model->user_data;
  224. if (!client) {
  225. BT_ERR("Invalid client user data");
  226. return -EINVAL;
  227. }
  228. internal = (bt_mesh_client_internal_data_t *)client->internal_data;
  229. if (!internal) {
  230. BT_ERR("Invalid client internal data");
  231. return -EINVAL;
  232. }
  233. if (param->ctx.addr == BLE_MESH_ADDR_UNASSIGNED) {
  234. BT_ERR("Invalid DST 0x%04x", param->ctx.addr);
  235. return -EINVAL;
  236. }
  237. if (bt_mesh_set_client_model_role(param->model, param->msg_role)) {
  238. BT_ERR("Failed to set client role");
  239. return -EIO;
  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->ctx.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("Not found the status opcode in op_pair list");
  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. static inline void bt_mesh_client_model_mutex_new(void)
  301. {
  302. if (!client_model_lock.mutex) {
  303. bt_mesh_mutex_create(&client_model_lock);
  304. }
  305. }
  306. #if CONFIG_BLE_MESH_DEINIT
  307. static inline void bt_mesh_client_model_mutex_free(void)
  308. {
  309. bt_mesh_mutex_free(&client_model_lock);
  310. }
  311. #endif /* CONFIG_BLE_MESH_DEINIT */
  312. void bt_mesh_client_model_lock(void)
  313. {
  314. bt_mesh_mutex_lock(&client_model_lock);
  315. }
  316. void bt_mesh_client_model_unlock(void)
  317. {
  318. bt_mesh_mutex_unlock(&client_model_lock);
  319. }
  320. int bt_mesh_client_init(struct bt_mesh_model *model)
  321. {
  322. bt_mesh_client_internal_data_t *data = NULL;
  323. bt_mesh_client_user_data_t *client = NULL;
  324. if (!model || !model->op) {
  325. BT_ERR("Invalid vendor client model");
  326. return -EINVAL;
  327. }
  328. client = (bt_mesh_client_user_data_t *)model->user_data;
  329. if (!client) {
  330. BT_ERR("No vendor client context provided");
  331. return -EINVAL;
  332. }
  333. if (!client->internal_data) {
  334. data = bt_mesh_calloc(sizeof(bt_mesh_client_internal_data_t));
  335. if (!data) {
  336. BT_ERR("%s, Out of memory", __func__);
  337. return -ENOMEM;
  338. }
  339. /* Init the client data queue */
  340. sys_slist_init(&data->queue);
  341. client->model = model;
  342. client->internal_data = data;
  343. } else {
  344. bt_mesh_client_clear_list(client->internal_data);
  345. }
  346. bt_mesh_client_model_mutex_new();
  347. return 0;
  348. }
  349. #if CONFIG_BLE_MESH_DEINIT
  350. int bt_mesh_client_deinit(struct bt_mesh_model *model)
  351. {
  352. bt_mesh_client_user_data_t *client = NULL;
  353. if (!model) {
  354. BT_ERR("Invalid vendor client model");
  355. return -EINVAL;
  356. }
  357. client = (bt_mesh_client_user_data_t *)model->user_data;
  358. if (!client) {
  359. BT_ERR("No vendor client context provided");
  360. return -EINVAL;
  361. }
  362. if (client->internal_data) {
  363. /* Remove items from the list */
  364. bt_mesh_client_clear_list(client->internal_data);
  365. /* Free the allocated internal data */
  366. bt_mesh_free(client->internal_data);
  367. client->internal_data = NULL;
  368. }
  369. bt_mesh_client_model_mutex_free();
  370. return 0;
  371. }
  372. #endif /* CONFIG_BLE_MESH_DEINIT */
  373. int bt_mesh_client_free_node(bt_mesh_client_node_t *node)
  374. {
  375. bt_mesh_client_internal_data_t *internal = NULL;
  376. bt_mesh_client_user_data_t *client = NULL;
  377. if (!node || !node->ctx.model) {
  378. BT_ERR("Invalid client list item");
  379. return -EINVAL;
  380. }
  381. client = (bt_mesh_client_user_data_t *)node->ctx.model->user_data;
  382. if (!client) {
  383. BT_ERR("Invalid client user data");
  384. return -EINVAL;
  385. }
  386. internal = (bt_mesh_client_internal_data_t *)client->internal_data;
  387. if (!internal) {
  388. BT_ERR("Invalid client internal data");
  389. return -EINVAL;
  390. }
  391. // Release the client node from the queue
  392. bt_mesh_list_lock();
  393. sys_slist_find_and_remove(&internal->queue, &node->client_node);
  394. bt_mesh_list_unlock();
  395. // Free the node
  396. bt_mesh_free(node);
  397. return 0;
  398. }
  399. int bt_mesh_client_clear_list(void *data)
  400. {
  401. bt_mesh_client_internal_data_t *internal = NULL;
  402. bt_mesh_client_node_t *node = NULL;
  403. if (!data) {
  404. BT_ERR("%s, Invalid parameter", __func__);
  405. return -EINVAL;
  406. }
  407. internal = (bt_mesh_client_internal_data_t *)data;
  408. bt_mesh_list_lock();
  409. while (!sys_slist_is_empty(&internal->queue)) {
  410. node = (void *)sys_slist_get_not_empty(&internal->queue);
  411. k_delayed_work_free(&node->timer);
  412. bt_mesh_free(node);
  413. }
  414. bt_mesh_list_unlock();
  415. return 0;
  416. }
  417. int bt_mesh_set_client_model_role(struct bt_mesh_model *model, uint8_t role)
  418. {
  419. bt_mesh_client_user_data_t *client = NULL;
  420. if (!model) {
  421. BT_ERR("Invalid client model");
  422. return -EINVAL;
  423. }
  424. client = (bt_mesh_client_user_data_t *)model->user_data;
  425. if (!client) {
  426. BT_ERR("Invalid client user data");
  427. return -EINVAL;
  428. }
  429. if (role >= ROLE_NVAL) {
  430. BT_ERR("Invalid client role 0x%02x", role);
  431. return -EINVAL;
  432. }
  433. client->msg_role = role;
  434. return 0;
  435. }