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