server_common.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 <errno.h>
  14. #include "mesh.h"
  15. #include "mesh_config.h"
  16. #include "access.h"
  17. #include "mesh_common.h"
  18. #include "generic_server.h"
  19. #include "lighting_server.h"
  20. #if CONFIG_BLE_MESH_SERVER_MODEL
  21. /**
  22. * According to Mesh Model Spec:
  23. * If the Transition Time field is not present and the Generic Default Transition
  24. * Time state is supported, the Generic Default Transition Time state shall be
  25. * used. Otherwise the transition shall be instantaneous.
  26. */
  27. #define INSTANTANEOUS_TRANS_TIME 0
  28. uint8_t bt_mesh_get_default_trans_time(struct bt_mesh_model *model)
  29. {
  30. /**
  31. * 1. If a Generic Default Transition Time Server model is present on the
  32. * main element of the model, that model instance shall be used.
  33. * 2. If a Generic Default Transition Time Server model is not present on
  34. * the main element of the model, then the Generic Default Transition
  35. * Time Server model instance that is present on the element with the
  36. * largest address that is smaller than the address of the main element
  37. * of the node shall be used; if no model instance is present on any
  38. * element with an address smaller than the address of the main element,
  39. * then the Generic Default Transition Time Server is not supported.
  40. */
  41. struct bt_mesh_elem *element = bt_mesh_model_elem(model);
  42. struct bt_mesh_gen_def_trans_time_srv *state = NULL;
  43. uint16_t primary_addr = bt_mesh_primary_addr();
  44. struct bt_mesh_model *srv = NULL;
  45. for (uint16_t addr = element->addr; addr >= primary_addr; addr--) {
  46. element = bt_mesh_elem_find(addr);
  47. if (element) {
  48. srv = bt_mesh_model_find(element, BLE_MESH_MODEL_ID_GEN_DEF_TRANS_TIME_SRV);
  49. if (srv) {
  50. state = (struct bt_mesh_gen_def_trans_time_srv *)srv->user_data;
  51. if (state) {
  52. return state->state.trans_time;
  53. }
  54. }
  55. }
  56. }
  57. return INSTANTANEOUS_TRANS_TIME;
  58. }
  59. int bt_mesh_get_light_lc_trans_time(struct bt_mesh_model *model, uint8_t *trans_time)
  60. {
  61. struct bt_mesh_light_lc_srv *srv = NULL;
  62. uint32_t value = 0U;
  63. if (model == NULL || trans_time == NULL) {
  64. BT_ERR("%s, Invalid parameter", __func__);
  65. return -EINVAL;
  66. }
  67. if (model->id != BLE_MESH_MODEL_ID_LIGHT_LC_SRV) {
  68. BT_ERR("Invalid a Light LC Server 0x%04x", model->id);
  69. return -EINVAL;
  70. }
  71. srv = (struct bt_mesh_light_lc_srv *)model->user_data;
  72. if (srv == NULL) {
  73. BT_ERR("Invalid Light LC Server user data");
  74. return -EINVAL;
  75. }
  76. /**
  77. * 1. Set transition time to 0x54 for BQB test case MESH/SR/LLC/BV-04-C.
  78. * Light LC Property Set: 0x3C, 0x004E20 -> Light LC Time Run On
  79. * Light LC Property Set: 0x37, 0x004E20 -> Light LC Time Fade On
  80. * Light LC Property Set: 0x39, 0x004E20 -> Light LC Time Fade Standby Manual
  81. *
  82. * 2. Set transition time to 0x0 for BQB test case MESH/SR/LLC/BV-08-C.
  83. *
  84. * TODO: Based on Light LC state and choose property property value as the
  85. * transition time. Currently directly use Light LC Time Run On property value.
  86. * Unit: Millisecond, range: [0, 16777214(0xFFFFFE)]
  87. */
  88. value = srv->lc->prop_state.time_run_on & 0xFFFFFF;
  89. /**
  90. * Convert value into Default Transition Time state format.
  91. * 0b00: 0 ~ 6.2s, 100 millisecond step resolution
  92. * 0b01: 0 ~ 62s, 1 second step resolution
  93. * 0b10: 0 ~ 620s, 10 seconds step resolution
  94. * 0b11: 0 ~ 620m, 10 minutes step resolution
  95. */
  96. if (value <= 6200) {
  97. *trans_time = (0 << 6) | (value / 100);
  98. } else if (value <= 62000) {
  99. *trans_time = (1 << 6) | (value / 1000);
  100. } else if (value <= 620000) {
  101. *trans_time = (2 << 6) | (value / 10000);
  102. } else {
  103. *trans_time = (3 << 6) | (value / 600000);
  104. }
  105. return 0;
  106. }
  107. int bt_mesh_server_get_optional(struct bt_mesh_model *model,
  108. struct bt_mesh_msg_ctx *ctx,
  109. struct net_buf_simple *buf,
  110. uint8_t *trans_time, uint8_t *delay,
  111. bool *optional)
  112. {
  113. if (model == NULL || buf == NULL || trans_time == NULL ||
  114. delay == NULL || optional == NULL) {
  115. BT_ERR("%s, Invalid parameter", __func__);
  116. return -EINVAL;
  117. }
  118. if (buf->len != 0x00 && buf->len != 0x02) {
  119. BT_ERR("Invalid optional message length %d", buf->len);
  120. return -EINVAL;
  121. }
  122. /* Currently we only get optional msg info which dst is set to a unicast address */
  123. if (!BLE_MESH_ADDR_IS_UNICAST(ctx->recv_dst)) {
  124. *trans_time = 0U;
  125. *delay = 0U;
  126. *optional = false;
  127. return 0;
  128. }
  129. /* No optional fields are available */
  130. if (buf->len == 0x00) {
  131. if (model->id == BLE_MESH_MODEL_ID_LIGHT_LC_SRV) {
  132. /**
  133. * Both messages(i.e. Light LC OnOff Set/Set Unack) may optionally include
  134. * a Transition Time field indicating the transition time to the target state.
  135. * If the Transition Time is not included, the Light LC Server shall use
  136. * its appropriate transition times defined by the Light LC Property states.
  137. */
  138. if (bt_mesh_get_light_lc_trans_time(model, trans_time)) {
  139. BT_ERR("Failed to get Light LC transition time");
  140. return -EIO;
  141. }
  142. } else {
  143. *trans_time = bt_mesh_get_default_trans_time(model);
  144. }
  145. *delay = 0U;
  146. *optional = false;
  147. return 0;
  148. }
  149. /* Optional fields are available */
  150. *trans_time = net_buf_simple_pull_u8(buf);
  151. if ((*trans_time & 0x3F) == 0x3F) {
  152. BT_ERR("Invalid Transaction Number of Steps 0x3f");
  153. return -EINVAL;
  154. }
  155. *delay = net_buf_simple_pull_u8(buf);
  156. *optional = true;
  157. return 0;
  158. }
  159. void bt_mesh_server_alloc_ctx(struct k_work *work)
  160. {
  161. /**
  162. * This function is used to allocate memory for storing "struct bt_mesh_msg_ctx"
  163. * of the received messages, because some server models will callback the "struct
  164. * bt_mesh_msg_ctx" info to the application layer after a certain delay.
  165. * Here we use the allocated heap memory to store the "struct bt_mesh_msg_ctx".
  166. */
  167. __ASSERT(work, "Invalid parameter");
  168. if (!work->_reserved) {
  169. work->_reserved = bt_mesh_calloc(sizeof(struct bt_mesh_msg_ctx));
  170. __ASSERT(work->_reserved, "Out of memory");
  171. }
  172. }
  173. #if CONFIG_BLE_MESH_DEINIT
  174. void bt_mesh_server_free_ctx(struct k_work *work)
  175. {
  176. __ASSERT(work, "Invalid parameter");
  177. if (work->_reserved) {
  178. bt_mesh_free(work->_reserved);
  179. work->_reserved = NULL;
  180. }
  181. }
  182. #endif /* CONFIG_BLE_MESH_DEINIT */
  183. bool bt_mesh_is_server_recv_last_msg(struct bt_mesh_last_msg_info *last,
  184. uint8_t tid, uint16_t src, uint16_t dst, int64_t *now)
  185. {
  186. *now = k_uptime_get();
  187. /* Currently we only compare msg info which dst is set to a unicast address */
  188. if (!BLE_MESH_ADDR_IS_UNICAST(dst)) {
  189. return false;
  190. }
  191. if (last->tid == tid && last->src == src && last->dst == dst &&
  192. (*now - last->timestamp <= K_SECONDS(6))) {
  193. return true;
  194. }
  195. return false;
  196. }
  197. void bt_mesh_server_update_last_msg(struct bt_mesh_last_msg_info *last,
  198. uint8_t tid, uint16_t src, uint16_t dst, int64_t *now)
  199. {
  200. /* Currently we only update msg info which dst is set to a unicast address */
  201. if (!BLE_MESH_ADDR_IS_UNICAST(dst)) {
  202. return;
  203. }
  204. last->tid = tid;
  205. last->src = src;
  206. last->dst = dst;
  207. last->timestamp = *now;
  208. return;
  209. }
  210. struct net_buf_simple *bt_mesh_server_get_pub_msg(struct bt_mesh_model *model, uint16_t msg_len)
  211. {
  212. struct net_buf_simple *buf = NULL;
  213. if (model == NULL) {
  214. BT_ERR("%s, Invalid parameter", __func__);
  215. return NULL;
  216. }
  217. if (model->pub == NULL || model->pub->msg == NULL ||
  218. model->pub->addr == BLE_MESH_ADDR_UNASSIGNED) {
  219. BT_DBG("No publication support, model id 0x%04x", model->id);
  220. return NULL;
  221. }
  222. buf = model->pub->msg;
  223. if (buf->size < msg_len) {
  224. BT_ERR("Too small publication msg size %d, model id 0x%04x",
  225. buf->size, model->id);
  226. return NULL;
  227. }
  228. return buf;
  229. }
  230. #endif /* CONFIG_BLE_MESH_SERVER_MODEL */