sensor_client.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  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 "btc_ble_mesh_sensor_model.h"
  9. #include "mesh/config.h"
  10. #include "mesh/model_opcode.h"
  11. #if CONFIG_BLE_MESH_SENSOR_CLI
  12. #include "mesh/sensor_client.h"
  13. /* The followings are the macro definitions of Sensor client
  14. * model message length, and a message is composed of 3 parts:
  15. * Opcode + Payload + MIC
  16. */
  17. /* Sensor client messages length */
  18. #define BLE_MESH_SENSOR_DESCRIPTOR_GET_MSG_LEN (2 + 2 + 4)
  19. #define BLE_MESH_SENSOR_CADENCE_GET_MSG_LEN (2 + 2 + 4)
  20. #define BLE_MESH_SENSOR_CADENCE_SET_MSG_LEN /* variable */
  21. #define BLE_MESH_SENSOR_SETTINGS_GET_MSG_LEN (2 + 2 + 4)
  22. #define BLE_MESH_SENSOR_SETTING_GET_MSG_LEN (2 + 4 + 4)
  23. #define BLE_MESH_SENSOR_SETTING_SET_MSG_LEN /* variable */
  24. #define BLE_MESH_SENSOR_GET_MSG_LEN (2 + 2 + 4)
  25. #define BLE_MESH_SENSOR_COLUMN_GET_MSG_LEN /* variable */
  26. #define BLE_MESH_SENSOR_SERIES_GET_MSG_LEN /* variable */
  27. static const bt_mesh_client_op_pair_t sensor_op_pair[] = {
  28. { BLE_MESH_MODEL_OP_SENSOR_DESCRIPTOR_GET, BLE_MESH_MODEL_OP_SENSOR_DESCRIPTOR_STATUS },
  29. { BLE_MESH_MODEL_OP_SENSOR_CADENCE_GET, BLE_MESH_MODEL_OP_SENSOR_CADENCE_STATUS },
  30. { BLE_MESH_MODEL_OP_SENSOR_CADENCE_SET, BLE_MESH_MODEL_OP_SENSOR_CADENCE_STATUS },
  31. { BLE_MESH_MODEL_OP_SENSOR_SETTINGS_GET, BLE_MESH_MODEL_OP_SENSOR_SETTINGS_STATUS },
  32. { BLE_MESH_MODEL_OP_SENSOR_SETTING_GET, BLE_MESH_MODEL_OP_SENSOR_SETTING_STATUS },
  33. { BLE_MESH_MODEL_OP_SENSOR_SETTING_SET, BLE_MESH_MODEL_OP_SENSOR_SETTING_STATUS },
  34. { BLE_MESH_MODEL_OP_SENSOR_GET, BLE_MESH_MODEL_OP_SENSOR_STATUS },
  35. { BLE_MESH_MODEL_OP_SENSOR_COLUMN_GET, BLE_MESH_MODEL_OP_SENSOR_COLUMN_STATUS },
  36. { BLE_MESH_MODEL_OP_SENSOR_SERIES_GET, BLE_MESH_MODEL_OP_SENSOR_SERIES_STATUS },
  37. };
  38. static bt_mesh_mutex_t sensor_client_lock;
  39. static void timeout_handler(struct k_work *work)
  40. {
  41. struct k_delayed_work *timer = NULL;
  42. bt_mesh_client_node_t *node = NULL;
  43. struct bt_mesh_model *model = NULL;
  44. struct bt_mesh_msg_ctx ctx = {0};
  45. uint32_t opcode = 0U;
  46. BT_WARN("Receive sensor status message timeout");
  47. bt_mesh_mutex_lock(&sensor_client_lock);
  48. timer = CONTAINER_OF(work, struct k_delayed_work, work);
  49. if (timer && !k_delayed_work_free(timer)) {
  50. node = CONTAINER_OF(work, bt_mesh_client_node_t, timer.work);
  51. if (node) {
  52. memcpy(&ctx, &node->ctx, sizeof(ctx));
  53. opcode = node->opcode;
  54. model = node->model;
  55. bt_mesh_client_free_node(node);
  56. bt_mesh_sensor_client_cb_evt_to_btc(
  57. opcode, BTC_BLE_MESH_EVT_SENSOR_CLIENT_TIMEOUT, model, &ctx, NULL, 0);
  58. }
  59. }
  60. bt_mesh_mutex_unlock(&sensor_client_lock);
  61. }
  62. static void sensor_status(struct bt_mesh_model *model,
  63. struct bt_mesh_msg_ctx *ctx,
  64. struct net_buf_simple *buf)
  65. {
  66. bt_mesh_client_node_t *node = NULL;
  67. uint8_t *val = NULL;
  68. uint8_t evt = 0xFF;
  69. size_t len = 0U;
  70. BT_DBG("len %d, bytes %s", buf->len, bt_hex(buf->data, buf->len));
  71. switch (ctx->recv_op) {
  72. case BLE_MESH_MODEL_OP_SENSOR_DESCRIPTOR_STATUS: {
  73. struct bt_mesh_sensor_descriptor_status *status = NULL;
  74. status = bt_mesh_calloc(sizeof(struct bt_mesh_sensor_descriptor_status));
  75. if (!status) {
  76. BT_ERR("%s, Out of memory", __func__);
  77. return;
  78. }
  79. status->descriptor = bt_mesh_alloc_buf(buf->len);
  80. if (!status->descriptor) {
  81. BT_ERR("%s, Out of memory", __func__);
  82. bt_mesh_free(status);
  83. return;
  84. }
  85. net_buf_simple_add_mem(status->descriptor, buf->data, buf->len);
  86. val = (uint8_t *)status;
  87. len = sizeof(struct bt_mesh_sensor_descriptor_status);
  88. break;
  89. }
  90. case BLE_MESH_MODEL_OP_SENSOR_CADENCE_STATUS: {
  91. struct bt_mesh_sensor_cadence_status *status = NULL;
  92. status = bt_mesh_calloc(sizeof(struct bt_mesh_sensor_cadence_status));
  93. if (!status) {
  94. BT_ERR("%s, Out of memory", __func__);
  95. return;
  96. }
  97. status->property_id = net_buf_simple_pull_le16(buf);
  98. status->sensor_cadence_value = bt_mesh_alloc_buf(buf->len);
  99. if (!status->sensor_cadence_value) {
  100. BT_ERR("%s, Out of memory", __func__);
  101. bt_mesh_free(status);
  102. return;
  103. }
  104. net_buf_simple_add_mem(status->sensor_cadence_value, buf->data, buf->len);
  105. val = (uint8_t *)status;
  106. len = sizeof(struct bt_mesh_sensor_cadence_status);
  107. break;
  108. }
  109. case BLE_MESH_MODEL_OP_SENSOR_SETTINGS_STATUS: {
  110. struct bt_mesh_sensor_settings_status *status = NULL;
  111. status = bt_mesh_calloc(sizeof(struct bt_mesh_sensor_settings_status));
  112. if (!status) {
  113. BT_ERR("%s, Out of memory", __func__);
  114. return;
  115. }
  116. status->sensor_property_id = net_buf_simple_pull_le16(buf);
  117. status->sensor_setting_property_ids = bt_mesh_alloc_buf(buf->len);
  118. if (!status->sensor_setting_property_ids) {
  119. BT_ERR("%s, Out of memory", __func__);
  120. bt_mesh_free(status);
  121. return;
  122. }
  123. net_buf_simple_add_mem(status->sensor_setting_property_ids, buf->data, buf->len);
  124. val = (uint8_t *)status;
  125. len = sizeof(struct bt_mesh_sensor_settings_status);
  126. break;
  127. }
  128. case BLE_MESH_MODEL_OP_SENSOR_SETTING_STATUS: {
  129. struct bt_mesh_sensor_setting_status *status = NULL;
  130. status = bt_mesh_calloc(sizeof(struct bt_mesh_sensor_setting_status));
  131. if (!status) {
  132. BT_ERR("%s, Out of memory", __func__);
  133. return;
  134. }
  135. status->sensor_property_id = net_buf_simple_pull_le16(buf);
  136. status->sensor_setting_property_id = net_buf_simple_pull_le16(buf);
  137. if (buf->len) {
  138. status->op_en = true;
  139. status->sensor_setting_access = net_buf_simple_pull_u8(buf);
  140. status->sensor_setting_raw = bt_mesh_alloc_buf(buf->len);
  141. if (!status->sensor_setting_raw) {
  142. BT_ERR("%s, Out of memory", __func__);
  143. bt_mesh_free(status);
  144. return;
  145. }
  146. net_buf_simple_add_mem(status->sensor_setting_raw, buf->data, buf->len);
  147. }
  148. val = (uint8_t *)status;
  149. len = sizeof(struct bt_mesh_sensor_setting_status);
  150. break;
  151. }
  152. case BLE_MESH_MODEL_OP_SENSOR_STATUS: {
  153. struct bt_mesh_sensor_status *status = NULL;
  154. status = bt_mesh_calloc(sizeof(struct bt_mesh_sensor_status));
  155. if (!status) {
  156. BT_ERR("%s, Out of memory", __func__);
  157. return;
  158. }
  159. status->marshalled_sensor_data = bt_mesh_alloc_buf(buf->len);
  160. if (!status->marshalled_sensor_data) {
  161. BT_ERR("%s, Out of memory", __func__);
  162. bt_mesh_free(status);
  163. return;
  164. }
  165. net_buf_simple_add_mem(status->marshalled_sensor_data, buf->data, buf->len);
  166. val = (uint8_t *)status;
  167. len = sizeof(struct bt_mesh_sensor_status);
  168. break;
  169. }
  170. case BLE_MESH_MODEL_OP_SENSOR_COLUMN_STATUS: {
  171. struct bt_mesh_sensor_column_status *status = NULL;
  172. status = bt_mesh_calloc(sizeof(struct bt_mesh_sensor_column_status));
  173. if (!status) {
  174. BT_ERR("%s, Out of memory", __func__);
  175. return;
  176. }
  177. status->property_id = net_buf_simple_pull_le16(buf);
  178. status->sensor_column_value = bt_mesh_alloc_buf(buf->len);
  179. if (!status->sensor_column_value) {
  180. BT_ERR("%s, Out of memory", __func__);
  181. bt_mesh_free(status);
  182. return;
  183. }
  184. net_buf_simple_add_mem(status->sensor_column_value, buf->data, buf->len);
  185. val = (uint8_t *)status;
  186. len = sizeof(struct bt_mesh_sensor_column_status);
  187. break;
  188. }
  189. case BLE_MESH_MODEL_OP_SENSOR_SERIES_STATUS: {
  190. struct bt_mesh_sensor_series_status *status = NULL;
  191. status = bt_mesh_calloc(sizeof(struct bt_mesh_sensor_series_status));
  192. if (!status) {
  193. BT_ERR("%s, Out of memory", __func__);
  194. return;
  195. }
  196. status->property_id = net_buf_simple_pull_le16(buf);
  197. status->sensor_series_value = bt_mesh_alloc_buf(buf->len);
  198. if (!status->sensor_series_value) {
  199. BT_ERR("%s, Out of memory", __func__);
  200. bt_mesh_free(status);
  201. return;
  202. }
  203. net_buf_simple_add_mem(status->sensor_series_value, buf->data, buf->len);
  204. val = (uint8_t *)status;
  205. len = sizeof(struct bt_mesh_sensor_series_status);
  206. break;
  207. }
  208. default:
  209. BT_ERR("Invalid Sensor Status opcode 0x%04x", ctx->recv_op);
  210. return;
  211. }
  212. buf->data = val;
  213. buf->len = len;
  214. bt_mesh_mutex_lock(&sensor_client_lock);
  215. node = bt_mesh_is_client_recv_publish_msg(model, ctx, buf, true);
  216. if (!node) {
  217. BT_DBG("Unexpected Sensor Status 0x%04x", ctx->recv_op);
  218. } else {
  219. switch (node->opcode) {
  220. case BLE_MESH_MODEL_OP_SENSOR_DESCRIPTOR_GET:
  221. case BLE_MESH_MODEL_OP_SENSOR_CADENCE_GET:
  222. case BLE_MESH_MODEL_OP_SENSOR_SETTINGS_GET:
  223. case BLE_MESH_MODEL_OP_SENSOR_SETTING_GET:
  224. case BLE_MESH_MODEL_OP_SENSOR_GET:
  225. case BLE_MESH_MODEL_OP_SENSOR_COLUMN_GET:
  226. case BLE_MESH_MODEL_OP_SENSOR_SERIES_GET:
  227. evt = BTC_BLE_MESH_EVT_SENSOR_CLIENT_GET_STATE;
  228. break;
  229. case BLE_MESH_MODEL_OP_SENSOR_CADENCE_SET:
  230. case BLE_MESH_MODEL_OP_SENSOR_SETTING_SET:
  231. evt = BTC_BLE_MESH_EVT_SENSOR_CLIENT_SET_STATE;
  232. break;
  233. default:
  234. break;
  235. }
  236. if (!k_delayed_work_free(&node->timer)) {
  237. uint32_t opcode = node->opcode;
  238. bt_mesh_client_free_node(node);
  239. bt_mesh_sensor_client_cb_evt_to_btc(opcode, evt, model, ctx, val, len);
  240. }
  241. }
  242. bt_mesh_mutex_unlock(&sensor_client_lock);
  243. switch (ctx->recv_op) {
  244. case BLE_MESH_MODEL_OP_SENSOR_DESCRIPTOR_STATUS: {
  245. struct bt_mesh_sensor_descriptor_status *status;
  246. status = (struct bt_mesh_sensor_descriptor_status *)val;
  247. bt_mesh_free_buf(status->descriptor);
  248. break;
  249. }
  250. case BLE_MESH_MODEL_OP_SENSOR_CADENCE_STATUS: {
  251. struct bt_mesh_sensor_cadence_status *status;
  252. status = (struct bt_mesh_sensor_cadence_status *)val;
  253. bt_mesh_free_buf(status->sensor_cadence_value);
  254. break;
  255. }
  256. case BLE_MESH_MODEL_OP_SENSOR_SETTINGS_STATUS: {
  257. struct bt_mesh_sensor_settings_status *status;
  258. status = (struct bt_mesh_sensor_settings_status *)val;
  259. bt_mesh_free_buf(status->sensor_setting_property_ids);
  260. break;
  261. }
  262. case BLE_MESH_MODEL_OP_SENSOR_SETTING_STATUS: {
  263. struct bt_mesh_sensor_setting_status *status;
  264. status = (struct bt_mesh_sensor_setting_status *)val;
  265. bt_mesh_free_buf(status->sensor_setting_raw);
  266. break;
  267. }
  268. case BLE_MESH_MODEL_OP_SENSOR_STATUS: {
  269. struct bt_mesh_sensor_status *status;
  270. status = (struct bt_mesh_sensor_status *)val;
  271. bt_mesh_free_buf(status->marshalled_sensor_data);
  272. break;
  273. }
  274. case BLE_MESH_MODEL_OP_SENSOR_COLUMN_STATUS: {
  275. struct bt_mesh_sensor_column_status *status;
  276. status = (struct bt_mesh_sensor_column_status *)val;
  277. bt_mesh_free_buf(status->sensor_column_value);
  278. break;
  279. }
  280. case BLE_MESH_MODEL_OP_SENSOR_SERIES_STATUS: {
  281. struct bt_mesh_sensor_series_status *status;
  282. status = (struct bt_mesh_sensor_series_status *)val;
  283. bt_mesh_free_buf(status->sensor_series_value);
  284. break;
  285. }
  286. default:
  287. break;
  288. }
  289. bt_mesh_free(val);
  290. }
  291. const struct bt_mesh_model_op bt_mesh_sensor_cli_op[] = {
  292. { BLE_MESH_MODEL_OP_SENSOR_DESCRIPTOR_STATUS, 0, sensor_status },
  293. { BLE_MESH_MODEL_OP_SENSOR_CADENCE_STATUS, 2, sensor_status },
  294. { BLE_MESH_MODEL_OP_SENSOR_SETTINGS_STATUS, 2, sensor_status },
  295. { BLE_MESH_MODEL_OP_SENSOR_SETTING_STATUS, 4, sensor_status },
  296. { BLE_MESH_MODEL_OP_SENSOR_STATUS, 0, sensor_status },
  297. { BLE_MESH_MODEL_OP_SENSOR_COLUMN_STATUS, 2, sensor_status },
  298. { BLE_MESH_MODEL_OP_SENSOR_SERIES_STATUS, 2, sensor_status },
  299. BLE_MESH_MODEL_OP_END,
  300. };
  301. static int sensor_act_state(bt_mesh_client_common_param_t *common,
  302. void *value, uint16_t value_len, bool need_ack)
  303. {
  304. struct net_buf_simple *msg = NULL;
  305. int err = 0;
  306. msg = bt_mesh_alloc_buf(value_len);
  307. if (!msg) {
  308. BT_ERR("%s, Out of memory", __func__);
  309. return -ENOMEM;
  310. }
  311. bt_mesh_model_msg_init(msg, common->opcode);
  312. switch (common->opcode) {
  313. case BLE_MESH_MODEL_OP_SENSOR_DESCRIPTOR_GET: {
  314. struct bt_mesh_sensor_descriptor_get *act;
  315. act = (struct bt_mesh_sensor_descriptor_get *)value;
  316. if (act->op_en) {
  317. net_buf_simple_add_le16(msg, act->property_id);
  318. }
  319. break;
  320. }
  321. case BLE_MESH_MODEL_OP_SENSOR_CADENCE_GET: {
  322. struct bt_mesh_sensor_cadence_get *act;
  323. act = (struct bt_mesh_sensor_cadence_get *)value;
  324. net_buf_simple_add_le16(msg, act->property_id);
  325. break;
  326. }
  327. case BLE_MESH_MODEL_OP_SENSOR_CADENCE_SET:
  328. case BLE_MESH_MODEL_OP_SENSOR_CADENCE_SET_UNACK: {
  329. struct bt_mesh_sensor_cadence_set *act;
  330. act = (struct bt_mesh_sensor_cadence_set *)value;
  331. net_buf_simple_add_le16(msg, act->property_id);
  332. net_buf_simple_add_u8(msg, act->status_trigger_type << 7 | act->fast_cadence_period_divisor);
  333. net_buf_simple_add_mem(msg, act->status_trigger_delta_down->data, act->status_trigger_delta_down->len);
  334. net_buf_simple_add_mem(msg, act->status_trigger_delta_up->data, act->status_trigger_delta_up->len);
  335. net_buf_simple_add_u8(msg, act->status_min_interval);
  336. net_buf_simple_add_mem(msg, act->fast_cadence_low->data, act->fast_cadence_low->len);
  337. net_buf_simple_add_mem(msg, act->fast_cadence_high->data, act->fast_cadence_high->len);
  338. break;
  339. }
  340. case BLE_MESH_MODEL_OP_SENSOR_SETTINGS_GET: {
  341. struct bt_mesh_sensor_settings_get *act;
  342. act = (struct bt_mesh_sensor_settings_get *)value;
  343. net_buf_simple_add_le16(msg, act->sensor_property_id);
  344. break;
  345. }
  346. case BLE_MESH_MODEL_OP_SENSOR_SETTING_GET: {
  347. struct bt_mesh_sensor_setting_get *act;
  348. act = (struct bt_mesh_sensor_setting_get *)value;
  349. net_buf_simple_add_le16(msg, act->sensor_property_id);
  350. net_buf_simple_add_le16(msg, act->sensor_setting_property_id);
  351. break;
  352. }
  353. case BLE_MESH_MODEL_OP_SENSOR_SETTING_SET:
  354. case BLE_MESH_MODEL_OP_SENSOR_SETTING_SET_UNACK: {
  355. struct bt_mesh_sensor_setting_set *act;
  356. act = (struct bt_mesh_sensor_setting_set *)value;
  357. net_buf_simple_add_le16(msg, act->sensor_property_id);
  358. net_buf_simple_add_le16(msg, act->sensor_setting_property_id);
  359. net_buf_simple_add_mem(msg, act->sensor_setting_raw->data, act->sensor_setting_raw->len);
  360. break;
  361. }
  362. case BLE_MESH_MODEL_OP_SENSOR_GET: {
  363. struct bt_mesh_sensor_get *act;
  364. act = (struct bt_mesh_sensor_get *)value;
  365. if (act->op_en) {
  366. net_buf_simple_add_le16(msg, act->property_id);
  367. }
  368. break;
  369. }
  370. case BLE_MESH_MODEL_OP_SENSOR_COLUMN_GET: {
  371. struct bt_mesh_sensor_column_get *act;
  372. act = (struct bt_mesh_sensor_column_get *)value;
  373. net_buf_simple_add_le16(msg, act->property_id);
  374. net_buf_simple_add_mem(msg, act->raw_value_x->data, act->raw_value_x->len);
  375. break;
  376. }
  377. case BLE_MESH_MODEL_OP_SENSOR_SERIES_GET: {
  378. struct bt_mesh_sensor_series_get *act;
  379. act = (struct bt_mesh_sensor_series_get *)value;
  380. net_buf_simple_add_le16(msg, act->property_id);
  381. if (act->op_en) {
  382. net_buf_simple_add_mem(msg, act->raw_value_x1->data, act->raw_value_x1->len);
  383. net_buf_simple_add_mem(msg, act->raw_value_x2->data, act->raw_value_x2->len);
  384. }
  385. break;
  386. }
  387. default:
  388. BT_ERR("Invalid Sensor client opcode 0x%04x", common->opcode);
  389. err = -EINVAL;
  390. goto end;
  391. }
  392. err = bt_mesh_client_send_msg(common, msg, need_ack, timeout_handler);
  393. end:
  394. bt_mesh_free_buf(msg);
  395. return err;
  396. }
  397. int bt_mesh_sensor_client_get_state(bt_mesh_client_common_param_t *common, void *get)
  398. {
  399. bt_mesh_sensor_client_t *client = NULL;
  400. uint16_t length = 0U;
  401. if (!common || !common->model || !get) {
  402. BT_ERR("%s, Invalid parameter", __func__);
  403. return -EINVAL;
  404. }
  405. client = (bt_mesh_sensor_client_t *)common->model->user_data;
  406. if (!client || !client->internal_data) {
  407. BT_ERR("Invalid Sensor client data");
  408. return -EINVAL;
  409. }
  410. switch (common->opcode) {
  411. case BLE_MESH_MODEL_OP_SENSOR_DESCRIPTOR_GET:
  412. length = BLE_MESH_SENSOR_DESCRIPTOR_GET_MSG_LEN;
  413. break;
  414. case BLE_MESH_MODEL_OP_SENSOR_CADENCE_GET:
  415. length = BLE_MESH_SENSOR_CADENCE_GET_MSG_LEN;
  416. break;
  417. case BLE_MESH_MODEL_OP_SENSOR_SETTINGS_GET:
  418. length = BLE_MESH_SENSOR_SETTINGS_GET_MSG_LEN;
  419. break;
  420. case BLE_MESH_MODEL_OP_SENSOR_SETTING_GET:
  421. length = BLE_MESH_SENSOR_SETTING_GET_MSG_LEN;
  422. break;
  423. case BLE_MESH_MODEL_OP_SENSOR_GET:
  424. length = BLE_MESH_SENSOR_GET_MSG_LEN;
  425. break;
  426. case BLE_MESH_MODEL_OP_SENSOR_COLUMN_GET: {
  427. struct bt_mesh_sensor_column_get *value;
  428. value = (struct bt_mesh_sensor_column_get *)get;
  429. if (!value->raw_value_x) {
  430. BT_ERR("Invalid Sensor Column Get");
  431. return -EINVAL;
  432. }
  433. length = (2 + 2 + value->raw_value_x->len + 4);
  434. break;
  435. }
  436. case BLE_MESH_MODEL_OP_SENSOR_SERIES_GET: {
  437. struct bt_mesh_sensor_series_get *value;
  438. value = (struct bt_mesh_sensor_series_get *)get;
  439. if (value->op_en) {
  440. if (!value->raw_value_x1 || !value->raw_value_x2) {
  441. BT_ERR("Invalid Sensor Series Get");
  442. return -EINVAL;
  443. }
  444. }
  445. if (value->op_en) {
  446. length = value->raw_value_x1->len + value->raw_value_x2->len;
  447. }
  448. length += (2 + 2 + 4);
  449. break;
  450. }
  451. default:
  452. BT_ERR("Invalid Sensor Get opcode 0x%04x", common->opcode);
  453. return -EINVAL;
  454. }
  455. return sensor_act_state(common, get, length, true);
  456. }
  457. int bt_mesh_sensor_client_set_state(bt_mesh_client_common_param_t *common, void *set)
  458. {
  459. bt_mesh_sensor_client_t *client = NULL;
  460. uint16_t length = 0U;
  461. bool need_ack = false;
  462. if (!common || !common->model || !set) {
  463. BT_ERR("%s, Invalid parameter", __func__);
  464. return -EINVAL;
  465. }
  466. client = (bt_mesh_sensor_client_t *)common->model->user_data;
  467. if (!client || !client->internal_data) {
  468. BT_ERR("Invalid Sensor client data");
  469. return -EINVAL;
  470. }
  471. switch (common->opcode) {
  472. case BLE_MESH_MODEL_OP_SENSOR_CADENCE_SET:
  473. need_ack = true;
  474. case BLE_MESH_MODEL_OP_SENSOR_CADENCE_SET_UNACK: {
  475. struct bt_mesh_sensor_cadence_set *value;
  476. value = (struct bt_mesh_sensor_cadence_set *)set;
  477. if (!value->status_trigger_delta_down || !value->status_trigger_delta_up ||
  478. !value->fast_cadence_low || !value->fast_cadence_high) {
  479. BT_ERR("Invalid Sensor Cadence Set");
  480. return -EINVAL;
  481. }
  482. length = value->status_trigger_delta_down->len + \
  483. value->status_trigger_delta_up->len + \
  484. value->fast_cadence_low->len + \
  485. value->fast_cadence_high->len;
  486. length += (1 + 2 + 1 + 1 + 4);
  487. break;
  488. }
  489. case BLE_MESH_MODEL_OP_SENSOR_SETTING_SET:
  490. need_ack = true;
  491. case BLE_MESH_MODEL_OP_SENSOR_SETTING_SET_UNACK: {
  492. struct bt_mesh_sensor_setting_set *value;
  493. value = (struct bt_mesh_sensor_setting_set *)set;
  494. if (!value->sensor_setting_raw) {
  495. BT_ERR("Invalid Sensor Setting Raw value");
  496. return -EINVAL;
  497. }
  498. length = (1 + 2 + 2 + value->sensor_setting_raw->len + 4);
  499. break;
  500. }
  501. default:
  502. BT_ERR("Invalid Sensor Set opcode 0x%04x", common->opcode);
  503. return -EINVAL;
  504. }
  505. return sensor_act_state(common, set, length, need_ack);
  506. }
  507. static int sensor_client_init(struct bt_mesh_model *model)
  508. {
  509. sensor_internal_data_t *internal = NULL;
  510. bt_mesh_sensor_client_t *client = NULL;
  511. if (!model) {
  512. BT_ERR("Invalid Sensor client model");
  513. return -EINVAL;
  514. }
  515. client = (bt_mesh_sensor_client_t *)model->user_data;
  516. if (!client) {
  517. BT_ERR("No Sensor client context provided");
  518. return -EINVAL;
  519. }
  520. if (client->internal_data) {
  521. BT_WARN("%s, Already", __func__);
  522. return -EALREADY;
  523. }
  524. internal = bt_mesh_calloc(sizeof(sensor_internal_data_t));
  525. if (!internal) {
  526. BT_ERR("%s, Out of memory", __func__);
  527. return -ENOMEM;
  528. }
  529. sys_slist_init(&internal->queue);
  530. client->model = model;
  531. client->op_pair_size = ARRAY_SIZE(sensor_op_pair);
  532. client->op_pair = sensor_op_pair;
  533. client->internal_data = internal;
  534. bt_mesh_mutex_create(&sensor_client_lock);
  535. return 0;
  536. }
  537. #if CONFIG_BLE_MESH_DEINIT
  538. static int sensor_client_deinit(struct bt_mesh_model *model)
  539. {
  540. bt_mesh_sensor_client_t *client = NULL;
  541. if (!model) {
  542. BT_ERR("Invalid Sensor client model");
  543. return -EINVAL;
  544. }
  545. client = (bt_mesh_sensor_client_t *)model->user_data;
  546. if (!client) {
  547. BT_ERR("No Sensor client context provided");
  548. return -EINVAL;
  549. }
  550. if (client->internal_data) {
  551. /* Remove items from the list */
  552. bt_mesh_client_clear_list(client->internal_data);
  553. /* Free the allocated internal data */
  554. bt_mesh_free(client->internal_data);
  555. client->internal_data = NULL;
  556. }
  557. bt_mesh_mutex_free(&sensor_client_lock);
  558. return 0;
  559. }
  560. #endif /* CONFIG_BLE_MESH_DEINIT */
  561. const struct bt_mesh_model_cb bt_mesh_sensor_client_cb = {
  562. .init = sensor_client_init,
  563. #if CONFIG_BLE_MESH_DEINIT
  564. .deinit = sensor_client_deinit,
  565. #endif /* CONFIG_BLE_MESH_DEINIT */
  566. };
  567. #endif /* CONFIG_BLE_MESH_SENSOR_CLI */