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