1 /*
2  * SPDX-FileCopyrightText: 2017-2021 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <errno.h>
8 
9 #include "btc_ble_mesh_sensor_model.h"
10 
11 #include "mesh_config.h"
12 #include "access.h"
13 #include "transport.h"
14 #include "model_opcode.h"
15 #include "state_transition.h"
16 #include "device_property.h"
17 
18 #if CONFIG_BLE_MESH_SENSOR_SERVER
19 
20 static void update_sensor_periodic_pub(struct bt_mesh_model *model, uint16_t prop_id);
21 
22 /* message handlers (Start) */
23 
24 /* Sensor Server & Sensor Setup Server message handlers */
send_sensor_descriptor_status(struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,uint16_t prop_id,bool get_all)25 static void send_sensor_descriptor_status(struct bt_mesh_model *model,
26                                           struct bt_mesh_msg_ctx *ctx,
27                                           uint16_t prop_id, bool get_all)
28 {
29     struct bt_mesh_sensor_srv *srv = model->user_data;
30     struct bt_mesh_sensor_state *state = NULL;
31     struct net_buf_simple *msg = NULL;
32     uint16_t total_len = 5U;
33     int i;
34 
35     msg = bt_mesh_alloc_buf(MIN(BLE_MESH_TX_SDU_MAX, BLE_MESH_SERVER_RSP_MAX_LEN));
36     if (msg == NULL) {
37         BT_ERR("%s, Out of memory", __func__);
38         return;
39     }
40 
41     bt_mesh_model_msg_init(msg, BLE_MESH_MODEL_OP_SENSOR_DESCRIPTOR_STATUS);
42 
43     if (get_all == true) {
44         for (i = 0; i < srv->state_count; i++) {
45             state = &srv->states[i];
46             if (state->sensor_property_id != INVALID_SENSOR_PROPERTY_ID) {
47                 total_len += SENSOR_DESCRIPTOR_LEN;
48                 if (total_len > MIN(BLE_MESH_TX_SDU_MAX, BLE_MESH_SERVER_RSP_MAX_LEN)) {
49                     /* Add this in case the message is too long */
50                     BT_WARN("Too large sensor descriptor status");
51                     break;
52                 }
53                 net_buf_simple_add_le16(msg, state->sensor_property_id);
54                 net_buf_simple_add_le32(msg, (state->descriptor.sample_function << 24) |
55                                         (state->descriptor.negative_tolerance << 12) |
56                                         (state->descriptor.positive_tolerance));
57                 net_buf_simple_add_u8(msg, state->descriptor.measure_period);
58                 net_buf_simple_add_u8(msg, state->descriptor.update_interval);
59             }
60         }
61     } else {
62         for (i = 0; i < srv->state_count; i++) {
63             state = &srv->states[i];
64             if (state->sensor_property_id != INVALID_SENSOR_PROPERTY_ID &&
65                     state->sensor_property_id == prop_id) {
66                 total_len += SENSOR_DESCRIPTOR_LEN;
67                 if (total_len > MIN(BLE_MESH_TX_SDU_MAX, BLE_MESH_SERVER_RSP_MAX_LEN)) {
68                     /* Add this in case the message is too long */
69                     BT_WARN("Too large sensor descriptor status");
70                     break;
71                 }
72                 net_buf_simple_add_le16(msg, state->sensor_property_id);
73                 net_buf_simple_add_le32(msg, (state->descriptor.sample_function << 24) |
74                                         (state->descriptor.negative_tolerance << 12) |
75                                         (state->descriptor.positive_tolerance));
76                 net_buf_simple_add_u8(msg, state->descriptor.measure_period);
77                 net_buf_simple_add_u8(msg, state->descriptor.update_interval);
78                 break;
79             }
80         }
81         if (i == srv->state_count) {
82             BT_WARN("Sensor Property ID 0x%04x not exists", prop_id);
83             net_buf_simple_add_le16(msg, prop_id);
84         }
85     }
86 
87     BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, msg, NULL, NULL));
88     bt_mesh_free_buf(msg);
89     return;
90 }
91 
send_sensor_data_status(struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,uint16_t prop_id,bool get_all)92 static void send_sensor_data_status(struct bt_mesh_model *model,
93                                     struct bt_mesh_msg_ctx *ctx,
94                                     uint16_t prop_id, bool get_all)
95 {
96     struct bt_mesh_sensor_srv *srv = model->user_data;
97     struct bt_mesh_sensor_state *state = NULL;
98     struct net_buf_simple *msg = NULL;
99     uint16_t total_len = 5U;
100     int i;
101 
102     msg = bt_mesh_alloc_buf(MIN(BLE_MESH_TX_SDU_MAX, BLE_MESH_SERVER_RSP_MAX_LEN));
103     if (msg == NULL) {
104         BT_ERR("%s, Out of memory", __func__);
105         return;
106     }
107 
108     bt_mesh_model_msg_init(msg, BLE_MESH_MODEL_OP_SENSOR_STATUS);
109 
110     if (get_all == true) {
111         for (i = 0; i < srv->state_count; i++) {
112             state = &srv->states[i];
113             if (state->sensor_property_id != INVALID_SENSOR_PROPERTY_ID) {
114                 uint8_t mpid_len = (state->sensor_data.format == SENSOR_DATA_FORMAT_A) ?
115                                     SENSOR_DATA_FORMAT_A_MPID_LEN : SENSOR_DATA_FORMAT_B_MPID_LEN;
116                 total_len += (mpid_len + (state->sensor_data.raw_value ?
117                                           state->sensor_data.raw_value->len : 0));
118                 if (total_len > MIN(BLE_MESH_TX_SDU_MAX, BLE_MESH_SERVER_RSP_MAX_LEN)) {
119                     /* Add this in case the message is too long */
120                     BT_WARN("Too large sensor status");
121                     break;
122                 }
123                 if (state->sensor_data.format == SENSOR_DATA_FORMAT_A) {
124                     uint16_t mpid = ((state->sensor_property_id & BIT_MASK(11)) << 5) |
125                                     ((state->sensor_data.length & BIT_MASK(4)) << 1) |
126                                     state->sensor_data.format;
127                     net_buf_simple_add_le16(msg, mpid);
128                 } else if (state->sensor_data.format == SENSOR_DATA_FORMAT_B) {
129                     uint8_t mpid = (state->sensor_data.length << 1) | state->sensor_data.format;
130                     net_buf_simple_add_u8(msg, mpid);
131                     net_buf_simple_add_le16(msg, state->sensor_property_id);
132                 }
133                 if (state->sensor_data.raw_value) {
134                     net_buf_simple_add_mem(msg, state->sensor_data.raw_value->data, state->sensor_data.raw_value->len);
135                 }
136             }
137         }
138     } else {
139         for (i = 0; i < srv->state_count; i++) {
140             state = &srv->states[i];
141             if (state->sensor_property_id != INVALID_SENSOR_PROPERTY_ID &&
142                     state->sensor_property_id == prop_id) {
143                 uint8_t mpid_len = (state->sensor_data.format == SENSOR_DATA_FORMAT_A) ?
144                                     SENSOR_DATA_FORMAT_A_MPID_LEN : SENSOR_DATA_FORMAT_B_MPID_LEN;
145                 total_len += (mpid_len + (state->sensor_data.raw_value ?
146                                           state->sensor_data.raw_value->len : 0));
147                 if (total_len > MIN(BLE_MESH_TX_SDU_MAX, BLE_MESH_SERVER_RSP_MAX_LEN)) {
148                     /* Add this in case the message is too long */
149                     BT_WARN("Too large sensor status");
150                     break;
151                 }
152                 if (state->sensor_data.format == SENSOR_DATA_FORMAT_A) {
153                     uint16_t mpid = ((state->sensor_property_id & BIT_MASK(11)) << 5) |
154                                     ((state->sensor_data.length & BIT_MASK(4)) << 1) |
155                                     state->sensor_data.format;
156                     net_buf_simple_add_le16(msg, mpid);
157                 } else if (state->sensor_data.format == SENSOR_DATA_FORMAT_B) {
158                     uint8_t mpid = (state->sensor_data.length << 1) | state->sensor_data.format;
159                     net_buf_simple_add_u8(msg, mpid);
160                     net_buf_simple_add_le16(msg, state->sensor_property_id);
161                 }
162                 if (state->sensor_data.raw_value) {
163                     net_buf_simple_add_mem(msg, state->sensor_data.raw_value->data,
164                                            state->sensor_data.raw_value->len);
165                 }
166                 break;
167             }
168         }
169         if (i == srv->state_count) {
170             BT_WARN("Sensor Property ID 0x%04x not exists", prop_id);
171             uint8_t mpid = (SENSOR_DATA_ZERO_LEN << 1) | SENSOR_DATA_FORMAT_B;
172             net_buf_simple_add_u8(msg, mpid);
173             net_buf_simple_add_le16(msg, prop_id);
174         }
175     }
176 
177     BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, msg, NULL, NULL));
178     bt_mesh_free_buf(msg);
179     return;
180 }
181 
send_sensor_cadence_status(struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,uint16_t prop_id,bool publish)182 static void send_sensor_cadence_status(struct bt_mesh_model *model,
183                                        struct bt_mesh_msg_ctx *ctx,
184                                        uint16_t prop_id, bool publish)
185 {
186     struct bt_mesh_sensor_setup_srv *srv = model->user_data;
187     struct bt_mesh_sensor_state *state = NULL;
188     struct net_buf_simple *msg = NULL;
189     uint16_t length = 0U;
190     int i;
191 
192     for (i = 0; i < srv->state_count; i++) {
193         state = &srv->states[i];
194         if (state->sensor_property_id != INVALID_SENSOR_PROPERTY_ID &&
195                 state->sensor_property_id == prop_id && state->cadence) {
196             length = SENSOR_PROPERTY_ID_LEN + 1 + 1;
197             if (state->cadence->trigger_delta_down) {
198                 if (state->cadence->trigger_type == SENSOR_STATUS_TRIGGER_TYPE_CHAR) {
199                     length += state->cadence->trigger_delta_down->len;
200                 } else {
201                     length += SENSOR_STATUS_TRIGGER_UINT16_LEN;
202                 }
203             }
204             if (state->cadence->trigger_delta_up) {
205                 if (state->cadence->trigger_type == SENSOR_STATUS_TRIGGER_TYPE_CHAR) {
206                     length += state->cadence->trigger_delta_up->len;
207                 } else {
208                     length += SENSOR_STATUS_TRIGGER_UINT16_LEN;
209                 }
210             }
211             if (state->cadence->fast_cadence_low) {
212                 length += state->cadence->fast_cadence_low->len;
213             }
214             if (state->cadence->fast_cadence_high) {
215                 length += state->cadence->fast_cadence_high->len;
216             }
217             break;
218         }
219     }
220     if (i == srv->state_count) {
221         BT_WARN("Sensor Property ID 0x%04x not exists", prop_id);
222         length = SENSOR_PROPERTY_ID_LEN;
223     }
224 
225     if (publish == false) {
226         msg = bt_mesh_alloc_buf(1 + length + BLE_MESH_SERVER_TRANS_MIC_SIZE);
227         if (msg == NULL) {
228             BT_ERR("%s, Out of memory", __func__);
229             return;
230         }
231     } else {
232         msg = bt_mesh_server_get_pub_msg(model, 1 + length);
233         if (msg == NULL) {
234             return;
235         }
236     }
237 
238     bt_mesh_model_msg_init(msg, BLE_MESH_MODEL_OP_SENSOR_CADENCE_STATUS);
239     net_buf_simple_add_le16(msg, prop_id);
240     if (i != srv->state_count) {
241         if (state->cadence) {
242             net_buf_simple_add_u8(msg, (state->cadence->trigger_type << 7) |
243                                   state->cadence->period_divisor);
244             if (state->cadence->trigger_delta_down) {
245                 if (state->cadence->trigger_type == SENSOR_STATUS_TRIGGER_TYPE_CHAR) {
246                     net_buf_simple_add_mem(msg, state->cadence->trigger_delta_down->data,
247                                            state->cadence->trigger_delta_down->len);
248                 } else {
249                     net_buf_simple_add_mem(msg, state->cadence->trigger_delta_down->data,
250                                            SENSOR_STATUS_TRIGGER_UINT16_LEN);
251                 }
252             }
253             if (state->cadence->trigger_delta_up) {
254                 if (state->cadence->trigger_type == SENSOR_STATUS_TRIGGER_TYPE_CHAR) {
255                     net_buf_simple_add_mem(msg, state->cadence->trigger_delta_up->data,
256                                            state->cadence->trigger_delta_up->len);
257                 } else {
258                     net_buf_simple_add_mem(msg, state->cadence->trigger_delta_up->data,
259                                            SENSOR_STATUS_TRIGGER_UINT16_LEN);
260                 }
261             }
262             net_buf_simple_add_u8(msg, state->cadence->min_interval);
263             if (state->cadence->fast_cadence_low) {
264                 net_buf_simple_add_mem(msg, state->cadence->fast_cadence_low->data,
265                                        state->cadence->fast_cadence_low->len);
266             }
267             if (state->cadence->fast_cadence_high) {
268                 net_buf_simple_add_mem(msg, state->cadence->fast_cadence_high->data,
269                                        state->cadence->fast_cadence_high->len);
270             }
271         }
272     }
273 
274     if (publish == false) {
275         BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, msg, NULL, NULL));
276         bt_mesh_free_buf(msg);
277     } else {
278         BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_publish(model));
279     }
280     return;
281 }
282 
send_sensor_settings_status(struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,uint16_t prop_id)283 static void send_sensor_settings_status(struct bt_mesh_model *model,
284                                         struct bt_mesh_msg_ctx *ctx,
285                                         uint16_t prop_id)
286 {
287     struct bt_mesh_sensor_setup_srv *srv = model->user_data;
288     struct bt_mesh_sensor_state *state = NULL;
289     struct sensor_setting *item = NULL;
290     struct net_buf_simple *msg = NULL;
291     uint16_t total_len = 7U;
292     int i, j;
293 
294     msg = bt_mesh_alloc_buf(MIN(BLE_MESH_TX_SDU_MAX, BLE_MESH_SERVER_RSP_MAX_LEN));
295     if (msg == NULL) {
296         BT_ERR("%s, Out of memory", __func__);
297         return;
298     }
299 
300     bt_mesh_model_msg_init(msg, BLE_MESH_MODEL_OP_SENSOR_SETTINGS_STATUS);
301     net_buf_simple_add_le16(msg, prop_id);
302 
303     for (i = 0; i < srv->state_count; i++) {
304         state = &srv->states[i];
305         if (state->sensor_property_id != INVALID_SENSOR_PROPERTY_ID &&
306                 state->sensor_property_id == prop_id &&
307                 state->setting_count && state->settings) {
308             for (j = 0; j < state->setting_count; j++) {
309                 item = &state->settings[j];
310                 if (item->property_id != INVALID_SENSOR_SETTING_PROPERTY_ID) {
311                     total_len += SENSOR_SETTING_PROPERTY_ID_LEN;
312                     if (total_len > MIN(BLE_MESH_TX_SDU_MAX, BLE_MESH_SERVER_RSP_MAX_LEN)) {
313                         /* Add this in case the message is too long */
314                         BT_WARN("Too large sensor settings status");
315                         break;
316                     }
317                     net_buf_simple_add_le16(msg, item->property_id);
318                 }
319             }
320             break;
321         }
322     }
323     if (i == srv->state_count) {
324         BT_WARN("Sensor Property ID 0x%04x not exists", prop_id);
325     }
326 
327     BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, msg, NULL, NULL));
328     bt_mesh_free_buf(msg);
329     return;
330 }
331 
find_sensor_setting(struct bt_mesh_model * model,uint16_t prop_id,uint16_t set_prop_id)332 static struct sensor_setting *find_sensor_setting(struct bt_mesh_model *model,
333                                                   uint16_t prop_id, uint16_t set_prop_id)
334 {
335     struct bt_mesh_sensor_setup_srv *srv = model->user_data;
336     struct bt_mesh_sensor_state *state = NULL;
337     struct sensor_setting *item = NULL;
338     int i, j;
339 
340     for (i = 0; i < srv->state_count; i++) {
341         state = &srv->states[i];
342         if (state->sensor_property_id != INVALID_SENSOR_PROPERTY_ID &&
343                 state->sensor_property_id == prop_id &&
344                 state->setting_count && state->settings) {
345             for (j = 0; j < state->setting_count; j++) {
346                 item = &state->settings[j];
347                 if (item->property_id != INVALID_SENSOR_SETTING_PROPERTY_ID &&
348                         item->property_id == set_prop_id) {
349                     return item;
350                 }
351             }
352         }
353     }
354 
355     return NULL;
356 }
357 
send_sensor_setting_status(struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,uint16_t prop_id,uint16_t set_prop_id,bool publish)358 static void send_sensor_setting_status(struct bt_mesh_model *model,
359                                        struct bt_mesh_msg_ctx *ctx, uint16_t prop_id,
360                                        uint16_t set_prop_id, bool publish)
361 {
362     struct sensor_setting *item = NULL;
363     struct net_buf_simple *msg = NULL;
364     uint16_t length = 0U;
365 
366     item = find_sensor_setting(model, prop_id, set_prop_id);
367     if (item) {
368         length = SENSOR_PROPERTY_ID_LEN + SENSOR_SETTING_PROPERTY_ID_LEN +
369                  SENSOR_SETTING_ACCESS_LEN + (item->raw ? item->raw->len : 0);
370     } else {
371         /* If the message is sent as a response to the Sensor Setting Get message or
372          * a Sensor Setting Set message with an unknown Sensor Property ID field or
373          * an unknown Sensor Setting Property ID field, the Sensor Setting Access
374          * field and the Sensor Setting Raw field shall be omitted.
375          */
376         BT_WARN("Sensor Setting not found, 0x%04x, 0x%04x", prop_id, set_prop_id);
377         length = SENSOR_PROPERTY_ID_LEN + SENSOR_SETTING_PROPERTY_ID_LEN;
378     }
379 
380     if (publish == false) {
381         msg = bt_mesh_alloc_buf(1 + length + BLE_MESH_SERVER_TRANS_MIC_SIZE);
382         if (msg == NULL) {
383             BT_ERR("%s, Out of memory", __func__);
384             return;
385         }
386     } else {
387         msg = bt_mesh_server_get_pub_msg(model, 1 + length);
388         if (msg == NULL) {
389             return;
390         }
391     }
392 
393     bt_mesh_model_msg_init(msg, BLE_MESH_MODEL_OP_SENSOR_SETTING_STATUS);
394     net_buf_simple_add_le16(msg, prop_id);
395     net_buf_simple_add_le16(msg, set_prop_id);
396     if (item) {
397         /**
398          * If the message is sent as a response to the Sensor Setting Set message with
399          * a Sensor Setting Property ID field that identifies an existing Sensor Setting,
400          * and the value of the Sensor Setting Access state is 0x01 (can be read), the
401          * Sensor Setting Property ID field shall be set to the value of the Sensor
402          * Setting Property ID field of the incoming message, the Sensor Setting Access
403          * field shall be set to the value of the Sensor Setting Access state field, and
404          * the Sensor Setting Raw field shall be omitted.
405          *
406          * TODO: What if the Sensor Setting Access is Prohibited?
407          */
408         net_buf_simple_add_u8(msg, item->access);
409         if (ctx->recv_op != BLE_MESH_MODEL_OP_SENSOR_SETTING_SET ||
410                 item->access == SENSOR_SETTING_ACCESS_READ_WRITE) {
411             if (item->raw) {
412                 net_buf_simple_add_mem(msg, item->raw->data, item->raw->len);
413             }
414         }
415     }
416 
417     if (publish == false) {
418         BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, msg, NULL, NULL));
419         bt_mesh_free_buf(msg);
420     } else {
421         BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_publish(model));
422     }
423     return;
424 }
425 
send_sensor_column_status(struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,struct net_buf_simple * buf,uint16_t prop_id)426 static void send_sensor_column_status(struct bt_mesh_model *model,
427                                       struct bt_mesh_msg_ctx *ctx,
428                                       struct net_buf_simple *buf, uint16_t prop_id)
429 {
430     struct bt_mesh_sensor_srv *srv = model->user_data;
431     struct bt_mesh_sensor_state *state = NULL;
432     struct net_buf_simple *msg = NULL;
433     bool optional = false;
434     uint16_t length = 0U;
435     int i;
436 
437     for (i = 0; i < srv->state_count; i++) {
438         state = &srv->states[i];
439         if (state->sensor_property_id != INVALID_SENSOR_PROPERTY_ID &&
440                 state->sensor_property_id == prop_id) {
441             length = SENSOR_PROPERTY_ID_LEN;
442             if (state->series_column.raw_value_x) {
443                 length += state->series_column.raw_value_x->len;
444             }
445             /**
446              * TODO: column width & raw value y in Sensor Column Status are optional,
447              * here we need to add some conditions to decide whether put these two
448              * in the status message.
449              */
450             if (optional) {
451                 if (state->series_column.column_width) {
452                     length += state->series_column.column_width->len;
453                 }
454                 if (state->series_column.raw_value_y) {
455                     length += state->series_column.raw_value_y->len;
456                 }
457             }
458             break;
459         }
460     }
461     if (i == srv->state_count) {
462         BT_WARN("Sensor Property ID 0x%04x not exists", prop_id);
463         length = SENSOR_PROPERTY_ID_LEN;
464     }
465 
466     msg = bt_mesh_alloc_buf(1 + length + BLE_MESH_SERVER_TRANS_MIC_SIZE);
467     if (msg == NULL) {
468         BT_ERR("%s, Out of memory", __func__);
469         return;
470     }
471 
472     /**
473      * TODO: Sensor Column Get contains Raw Value X which identifies a column,
474      * we need to use this value to decide the column.
475      */
476 
477     bt_mesh_model_msg_init(msg, BLE_MESH_MODEL_OP_SENSOR_COLUMN_STATUS);
478     net_buf_simple_add_le16(msg, prop_id);
479     if (i != srv->state_count) {
480         if (state->series_column.raw_value_x) {
481             net_buf_simple_add_mem(msg, state->series_column.raw_value_x->data,
482                                    state->series_column.raw_value_x->len);
483         }
484         if (optional) {
485             if (state->series_column.column_width) {
486                 net_buf_simple_add_mem(msg, state->series_column.column_width->data,
487                                        state->series_column.column_width->len);
488             }
489             if (state->series_column.raw_value_y) {
490                 net_buf_simple_add_mem(msg, state->series_column.raw_value_y->data,
491                                        state->series_column.raw_value_y->len);
492             }
493         }
494     }
495 
496     BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, msg, NULL, NULL));
497     bt_mesh_free_buf(msg);
498     return;
499 }
500 
send_sensor_series_status(struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,struct net_buf_simple * buf,uint16_t prop_id)501 static void send_sensor_series_status(struct bt_mesh_model *model,
502                                       struct bt_mesh_msg_ctx *ctx,
503                                       struct net_buf_simple *buf, uint16_t prop_id)
504 {
505     struct bt_mesh_sensor_srv *srv = model->user_data;
506     struct bt_mesh_sensor_state *state = NULL;
507     struct net_buf_simple *msg = NULL;
508     bool optional = false;
509     uint16_t length = 0U;
510     int i;
511 
512     for (i = 0; i < srv->state_count; i++) {
513         state = &srv->states[i];
514         if (state->sensor_property_id != INVALID_SENSOR_PROPERTY_ID &&
515                 state->sensor_property_id == prop_id) {
516             length = SENSOR_PROPERTY_ID_LEN;
517             /* TODO: raw value x, column width & raw value y in Sensor Series
518              * Status are optional, here we need to add some conditions to
519              * decide whether put these three in the status message.
520              */
521             if (optional) {
522                 if (state->series_column.raw_value_x) {
523                     length += state->series_column.raw_value_x->len;
524                 }
525                 if (state->series_column.column_width) {
526                     length += state->series_column.column_width->len;
527                 }
528                 if (state->series_column.raw_value_y) {
529                     length += state->series_column.raw_value_y->len;
530                 }
531             }
532             break;
533         }
534     }
535     if (i == srv->state_count) {
536         BT_WARN("Sensor Property ID 0x%04x not exists", prop_id);
537         length = SENSOR_PROPERTY_ID_LEN;
538     }
539 
540     msg = bt_mesh_alloc_buf(1 + length + BLE_MESH_SERVER_TRANS_MIC_SIZE);
541     if (msg == NULL) {
542         BT_ERR("%s, Out of memory", __func__);
543         return;
544     }
545 
546     /**
547      * TODO: Sensor Series Get may contain Raw Value X1 and Raw Value X2 which
548      * identifies a starting column and a ending column, we need to use these
549      * values to decide the columns.
550      */
551 
552     bt_mesh_model_msg_init(msg, BLE_MESH_MODEL_OP_SENSOR_SERIES_STATUS);
553     net_buf_simple_add_le16(msg, prop_id);
554     if (i != srv->state_count) {
555         if (optional) {
556             if (state->series_column.raw_value_x) {
557                 net_buf_simple_add_mem(msg, state->series_column.raw_value_x->data,
558                                        state->series_column.raw_value_x->len);
559             }
560             if (state->series_column.column_width) {
561                 net_buf_simple_add_mem(msg, state->series_column.column_width->data,
562                                        state->series_column.column_width->len);
563             }
564             if (state->series_column.raw_value_y) {
565                 net_buf_simple_add_mem(msg, state->series_column.raw_value_y->data,
566                                        state->series_column.raw_value_y->len);
567             }
568         }
569     }
570 
571     BLE_MESH_CHECK_SEND_STATUS(bt_mesh_model_send(model, ctx, msg, NULL, NULL));
572     bt_mesh_free_buf(msg);
573     return;
574 }
575 
sensor_get(struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,struct net_buf_simple * buf)576 static void sensor_get(struct bt_mesh_model *model,
577                        struct bt_mesh_msg_ctx *ctx,
578                        struct net_buf_simple *buf)
579 {
580     uint16_t set_prop_id = INVALID_SENSOR_PROPERTY_ID;
581     uint16_t prop_id = INVALID_SENSOR_PROPERTY_ID;
582 
583     if (model->user_data == NULL) {
584         BT_ERR("%s, Invalid model user data", __func__);
585         return;
586     }
587 
588     switch (ctx->recv_op) {
589     case BLE_MESH_MODEL_OP_SENSOR_DESCRIPTOR_GET:
590     case BLE_MESH_MODEL_OP_SENSOR_GET:
591     case BLE_MESH_MODEL_OP_SENSOR_COLUMN_GET:
592     case BLE_MESH_MODEL_OP_SENSOR_SERIES_GET: {
593         struct bt_mesh_sensor_srv *srv = model->user_data;
594         if (srv->state_count == 0U || srv->states == NULL) {
595             BT_ERR("Invalid Sensor Server state");
596             return;
597         }
598         if (ctx->recv_op == BLE_MESH_MODEL_OP_SENSOR_DESCRIPTOR_GET ||
599                 ctx->recv_op == BLE_MESH_MODEL_OP_SENSOR_GET) {
600             bool get_all = buf->len ? false : true;
601             if (buf->len) {
602                 prop_id = net_buf_simple_pull_le16(buf);
603                 if (prop_id == INVALID_SENSOR_PROPERTY_ID) {
604                     BT_ERR("Prohibited Sensor Property ID 0x0000");
605                     return;
606                 }
607             }
608             if (ctx->recv_op == BLE_MESH_MODEL_OP_SENSOR_DESCRIPTOR_GET) {
609                 if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
610                     bt_mesh_sensor_server_recv_get_msg_t get = {
611                         .sensor_descriptor_get.op_en = !get_all,
612                         .sensor_descriptor_get.id = prop_id,
613                     };
614                     bt_mesh_sensor_server_cb_evt_to_btc(
615                         BTC_BLE_MESH_EVT_SENSOR_SERVER_RECV_GET_MSG, model, ctx, (const uint8_t *)&get, sizeof(get));
616                 } else {
617                     send_sensor_descriptor_status(model, ctx, prop_id, get_all);
618                 }
619             } else {
620                 if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
621                     bt_mesh_sensor_server_recv_get_msg_t get = {
622                         .sensor_get.op_en = !get_all,
623                         .sensor_get.id = prop_id,
624                     };
625                     bt_mesh_sensor_server_cb_evt_to_btc(
626                         BTC_BLE_MESH_EVT_SENSOR_SERVER_RECV_GET_MSG, model, ctx, (const uint8_t *)&get, sizeof(get));
627                 } else {
628                     send_sensor_data_status(model, ctx, prop_id, get_all);
629                 }
630             }
631         } else {
632             prop_id = net_buf_simple_pull_le16(buf);
633             if (prop_id == INVALID_SENSOR_PROPERTY_ID) {
634                 BT_ERR("Prohibited Sensor Property ID 0x0000");
635                 return;
636             }
637             if (ctx->recv_op == BLE_MESH_MODEL_OP_SENSOR_COLUMN_GET) {
638                 if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
639                     bt_mesh_sensor_server_recv_get_msg_t get = {
640                         .sensor_column_get.id = prop_id,
641                         .sensor_column_get.raw_x = buf,
642                     };
643                     bt_mesh_sensor_server_cb_evt_to_btc(
644                         BTC_BLE_MESH_EVT_SENSOR_SERVER_RECV_GET_MSG, model, ctx, (const uint8_t *)&get, sizeof(get));
645                 } else {
646                     send_sensor_column_status(model, ctx, buf, prop_id);
647                 }
648             } else {
649                 if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
650                     bt_mesh_sensor_server_recv_get_msg_t get = {
651                         .sensor_series_get.id = prop_id,
652                         .sensor_series_get.raw = buf,
653                     };
654                     bt_mesh_sensor_server_cb_evt_to_btc(
655                         BTC_BLE_MESH_EVT_SENSOR_SERVER_RECV_GET_MSG, model, ctx, (const uint8_t *)&get, sizeof(get));
656                 } else {
657                     send_sensor_series_status(model, ctx, buf, prop_id);
658                 }
659             }
660         }
661         return;
662     }
663     case BLE_MESH_MODEL_OP_SENSOR_CADENCE_GET:
664     case BLE_MESH_MODEL_OP_SENSOR_SETTINGS_GET:
665     case BLE_MESH_MODEL_OP_SENSOR_SETTING_GET: {
666         struct bt_mesh_sensor_setup_srv *srv = model->user_data;
667         if (srv->state_count == 0U || srv->states == NULL) {
668             BT_ERR("Invalid Sensor Setup Server state");
669             return;
670         }
671         if (ctx->recv_op == BLE_MESH_MODEL_OP_SENSOR_CADENCE_GET ||
672                 ctx->recv_op == BLE_MESH_MODEL_OP_SENSOR_SETTINGS_GET) {
673             prop_id = net_buf_simple_pull_le16(buf);
674             if (prop_id == INVALID_SENSOR_PROPERTY_ID) {
675                 BT_ERR("Prohibited Sensor Property ID 0x0000");
676                 return;
677             }
678             if (ctx->recv_op == BLE_MESH_MODEL_OP_SENSOR_CADENCE_GET) {
679                 if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
680                     bt_mesh_sensor_server_recv_get_msg_t get = {
681                         .sensor_cadence_get.id = prop_id,
682                     };
683                     bt_mesh_sensor_server_cb_evt_to_btc(
684                         BTC_BLE_MESH_EVT_SENSOR_SERVER_RECV_GET_MSG, model, ctx, (const uint8_t *)&get, sizeof(get));
685                 } else {
686                     send_sensor_cadence_status(model, ctx, prop_id, false);
687                 }
688             } else {
689                 if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
690                     bt_mesh_sensor_server_recv_get_msg_t get = {
691                         .sensor_settings_get.id = prop_id,
692                     };
693                     bt_mesh_sensor_server_cb_evt_to_btc(
694                         BTC_BLE_MESH_EVT_SENSOR_SERVER_RECV_GET_MSG, model, ctx, (const uint8_t *)&get, sizeof(get));
695                 } else {
696                     send_sensor_settings_status(model, ctx, prop_id);
697                 }
698             }
699         } else {
700             prop_id = net_buf_simple_pull_le16(buf);
701             if (prop_id == INVALID_SENSOR_PROPERTY_ID) {
702                 BT_ERR("Prohibited Sensor Property ID 0x0000");
703                 return;
704             }
705             set_prop_id = net_buf_simple_pull_le16(buf);
706             if (set_prop_id == INVALID_SENSOR_PROPERTY_ID) {
707                 BT_ERR("Prohibited Sensor Setting Property ID 0x0000");
708                 return;
709             }
710 
711             if (srv->rsp_ctrl.get_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
712                 bt_mesh_sensor_server_recv_get_msg_t get = {
713                     .sensor_setting_get.id = prop_id,
714                     .sensor_setting_get.setting_id = set_prop_id,
715                 };
716                 bt_mesh_sensor_server_cb_evt_to_btc(
717                     BTC_BLE_MESH_EVT_SENSOR_SERVER_RECV_GET_MSG, model, ctx, (const uint8_t *)&get, sizeof(get));
718             } else {
719                 send_sensor_setting_status(model, ctx, prop_id, set_prop_id, false);
720             }
721         }
722         return;
723     }
724     default:
725         BT_WARN("Unknown Sensor Get opcode 0x%04x", ctx->recv_op);
726         return;
727     }
728 }
729 
sensor_cadence_set(struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,struct net_buf_simple * buf)730 static void sensor_cadence_set(struct bt_mesh_model *model,
731                                struct bt_mesh_msg_ctx *ctx,
732                                struct net_buf_simple *buf)
733 {
734     struct bt_mesh_sensor_setup_srv *srv = model->user_data;
735     bt_mesh_sensor_server_state_change_t change = {0};
736     struct bt_mesh_sensor_state *state = NULL;
737     struct bt_mesh_model *sensor_model = NULL;
738     struct bt_mesh_elem *element = NULL;
739     uint16_t prop_id = 0U, trigger_len = 0U;
740     uint8_t val = 0U, divisor = 0U;
741     int i;
742 
743     if (srv == NULL || srv->state_count == 0U || srv->states == NULL) {
744         BT_ERR("%s, Invalid model user data", __func__);
745         return;
746     }
747 
748     prop_id = net_buf_simple_pull_le16(buf);
749     if (prop_id == INVALID_SENSOR_PROPERTY_ID) {
750         BT_ERR("Prohibited Sensor Property ID 0x0000");
751         return;
752     }
753 
754     if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
755         bt_mesh_sensor_server_recv_set_msg_t set = {
756             .sensor_cadence_set.id = prop_id,
757             .sensor_cadence_set.cadence = buf,
758         };
759         bt_mesh_sensor_server_cb_evt_to_btc(
760             BTC_BLE_MESH_EVT_SENSOR_SERVER_RECV_SET_MSG, model, ctx, (const uint8_t *)&set, sizeof(set));
761         return;
762     }
763 
764     for (i = 0; i < srv->state_count; i++) {
765         state = &srv->states[i];
766         if (state->sensor_property_id != INVALID_SENSOR_PROPERTY_ID &&
767                 state->sensor_property_id == prop_id) {
768             break;
769         }
770     }
771     if (i == srv->state_count || state->cadence == NULL) {
772         /* When the message is sent as a response to the Sensor Cadence Get message or
773          * a Sensor Cadence Set message with an unknown Property ID field or the Sensor
774          * Server does not support the Sensor Cadence state for the sensor referred by
775          * the Property ID, the following fields shall be omitted:
776          * • Fast Cadence Period Divisor
777          * • Status Trigger Type
778          * • Status Trigger Delta Down
779          * • Status Trigger Delta Up
780          * • Status Min Interval
781          * • Fast Cadence Low
782          * • Fast Cadence High
783          */
784         send_sensor_cadence_status(model, ctx, prop_id, false);
785         return;
786     }
787 
788     val = net_buf_simple_pull_u8(buf);
789     divisor = val & BIT_MASK(7);
790     if (divisor > SENSOR_PERIOD_DIVISOR_MAX_VALUE) {
791         BT_ERR("Prohibited Fast Cadence Period Divisor 0x%02x", divisor);
792         return;
793     }
794     state->cadence->period_divisor = divisor;
795     state->cadence->trigger_type = (val >> 7) & BIT_MASK(1);
796 
797     if (state->cadence->trigger_type == SENSOR_STATUS_TRIGGER_TYPE_CHAR) {
798         trigger_len = bt_mesh_get_dev_prop_len(prop_id);
799     } else {
800         trigger_len = SENSOR_STATUS_TRIGGER_UINT16_LEN;
801     }
802     if (buf->len < (trigger_len << 1) + SENSOR_STATUS_MIN_INTERVAL_LEN) {
803         BT_ERR("Invalid Sensor Cadence Set length %d, trigger type %d",
804                 buf->len + 3, state->cadence->trigger_type);
805         return;
806     }
807 
808     if (state->cadence->trigger_delta_down) {
809         net_buf_simple_reset(state->cadence->trigger_delta_down);
810         net_buf_simple_add_mem(state->cadence->trigger_delta_down, buf->data, trigger_len);
811         net_buf_simple_pull_mem(buf, trigger_len);
812     }
813     if (state->cadence->trigger_delta_up) {
814         net_buf_simple_reset(state->cadence->trigger_delta_up);
815         net_buf_simple_add_mem(state->cadence->trigger_delta_up, buf->data, trigger_len);
816         net_buf_simple_pull_mem(buf, trigger_len);
817     }
818 
819     /* The valid range for the Status Min Interval is 0–26 and other values are Prohibited. */
820     val = net_buf_simple_pull_u8(buf);
821     if (val > SENSOR_STATUS_MIN_INTERVAL_MAX) {
822         BT_ERR("Invalid Status Min Interval %d", val);
823         return;
824     }
825     state->cadence->min_interval = val;
826 
827     if (buf->len % 2) {
828         BT_ERR("Different length of Fast Cadence Low & High, length %d", buf->len);
829         return;
830     }
831     if (buf->len) {
832         uint8_t range_len = buf->len / 2;
833         if (state->cadence->fast_cadence_low) {
834             net_buf_simple_reset(state->cadence->fast_cadence_low);
835             net_buf_simple_add_mem(state->cadence->fast_cadence_low, buf->data, range_len);
836             net_buf_simple_pull_mem(buf, range_len);
837         }
838         if (state->cadence->fast_cadence_high) {
839             net_buf_simple_reset(state->cadence->fast_cadence_high);
840             net_buf_simple_add_mem(state->cadence->fast_cadence_high, buf->data, range_len);
841             net_buf_simple_pull_mem(buf, range_len);
842         }
843     }
844 
845     change.sensor_cadence_set.id = prop_id;
846     change.sensor_cadence_set.period_divisor = state->cadence->period_divisor;
847     change.sensor_cadence_set.trigger_type = state->cadence->trigger_type;
848     change.sensor_cadence_set.trigger_delta_down = state->cadence->trigger_delta_down;
849     change.sensor_cadence_set.trigger_delta_up = state->cadence->trigger_delta_up;
850     change.sensor_cadence_set.min_interval = state->cadence->min_interval;
851     change.sensor_cadence_set.fast_cadence_low = state->cadence->fast_cadence_low;
852     change.sensor_cadence_set.fast_cadence_high = state->cadence->fast_cadence_high;
853     bt_mesh_sensor_server_cb_evt_to_btc(
854         BTC_BLE_MESH_EVT_SENSOR_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change));
855 
856     if (ctx->recv_op == BLE_MESH_MODEL_OP_SENSOR_CADENCE_SET) {
857         send_sensor_cadence_status(model, ctx, prop_id, false);
858     }
859     send_sensor_cadence_status(model, ctx, prop_id, true);
860 
861     /* Try to find the corresponding Sensor Server Model */
862     element = bt_mesh_model_elem(model);
863     sensor_model = bt_mesh_model_find(element, BLE_MESH_MODEL_ID_SENSOR_SRV);
864     if (sensor_model == NULL) {
865         BT_WARN("Sensor Server model not exists in the element");
866         return;
867     }
868 
869     /**
870      * Based on the configured Sensor Cadence state, change Periodic Sensor
871      * status publication mechanism.
872      */
873     update_sensor_periodic_pub(sensor_model, prop_id);
874     return;
875 }
876 
update_sensor_periodic_pub(struct bt_mesh_model * model,uint16_t prop_id)877 static void update_sensor_periodic_pub(struct bt_mesh_model *model, uint16_t prop_id)
878 {
879     struct bt_mesh_sensor_state *state = NULL;
880     struct bt_mesh_sensor_srv *srv = NULL;
881     int i;
882 
883     if (model->id != BLE_MESH_MODEL_ID_SENSOR_SRV) {
884         BT_ERR("Invalid Sensor Server model 0x%04x", model->id);
885         return;
886     }
887 
888     srv = (struct bt_mesh_sensor_srv *)model->user_data;
889     if (srv == NULL || srv->state_count == 0U || srv->states == NULL) {
890         BT_ERR("%s, Invalid model user data", __func__);
891         return;
892     }
893 
894     for (i = 0; i < srv->state_count; i++) {
895         state = &srv->states[i];
896         if (state->sensor_property_id != INVALID_SENSOR_PROPERTY_ID &&
897                 state->sensor_property_id == prop_id) {
898             break;
899         }
900     }
901     if (i == srv->state_count) {
902         BT_ERR("Sensor Property ID 0x%04x not exists", prop_id);
903         return;
904     }
905 
906     if (state->cadence == NULL) {
907         BT_WARN("Sensor Cadence state not exists");
908         return;
909     }
910 
911     /**
912      * Currently when the device receives a Sensor Cadence Set message,
913      * a event will be callback to the application layer, and users can
914      * change the Sensor Data publication period in the event. And this
915      * is exactly what we do for the BQB test.
916      */
917 }
918 
sensor_setting_set(struct bt_mesh_model * model,struct bt_mesh_msg_ctx * ctx,struct net_buf_simple * buf)919 static void sensor_setting_set(struct bt_mesh_model *model,
920                                struct bt_mesh_msg_ctx *ctx,
921                                struct net_buf_simple *buf)
922 {
923     struct bt_mesh_sensor_setup_srv *srv = model->user_data;
924     bt_mesh_sensor_server_state_change_t change = {0};
925     struct sensor_setting *item = NULL;
926     uint16_t prop_id = 0U, set_prop_id = 0U;
927 
928     if (srv == NULL || srv->state_count == 0U || srv->states == NULL) {
929         BT_ERR("%s, Invalid model user data", __func__);
930         return;
931     }
932 
933     prop_id = net_buf_simple_pull_le16(buf);
934     if (prop_id == INVALID_SENSOR_PROPERTY_ID) {
935         BT_ERR("Prohibited Sensor Property ID 0x0000");
936         return;
937     }
938 
939     set_prop_id = net_buf_simple_pull_le16(buf);
940     if (set_prop_id == INVALID_SENSOR_PROPERTY_ID) {
941         BT_ERR("Prohibited Sensor Setting Property ID 0x0000");
942         return;
943     }
944 
945     if (srv->rsp_ctrl.set_auto_rsp == BLE_MESH_SERVER_RSP_BY_APP) {
946         bt_mesh_sensor_server_recv_set_msg_t set = {
947             .sensor_setting_set.id = prop_id,
948             .sensor_setting_set.setting_id = set_prop_id,
949             .sensor_setting_set.raw = buf,
950         };
951         bt_mesh_sensor_server_cb_evt_to_btc(
952             BTC_BLE_MESH_EVT_SENSOR_SERVER_RECV_SET_MSG, model, ctx, (const uint8_t *)&set, sizeof(set));
953         return;
954     }
955 
956     item = find_sensor_setting(model, prop_id, set_prop_id);
957     if (item) {
958         if (item->access == SENSOR_SETTING_ACCESS_READ_WRITE && item->raw) {
959             net_buf_simple_reset(item->raw);
960             net_buf_simple_add_mem(item->raw, buf->data,
961                                    MIN(buf->len, item->raw->size));
962 
963             change.sensor_setting_set.id = prop_id;
964             change.sensor_setting_set.setting_id = set_prop_id;
965             change.sensor_setting_set.value = item->raw;
966             bt_mesh_sensor_server_cb_evt_to_btc(
967                 BTC_BLE_MESH_EVT_SENSOR_SERVER_STATE_CHANGE, model, ctx, (const uint8_t *)&change, sizeof(change));
968         }
969     }
970 
971     if (ctx->recv_op == BLE_MESH_MODEL_OP_SENSOR_SETTING_SET) {
972         send_sensor_setting_status(model, ctx, prop_id, set_prop_id, false);
973     }
974     if (item) {
975         send_sensor_setting_status(model, ctx, prop_id, set_prop_id, true);
976     }
977 
978     return;
979 }
980 
981 /* message handlers (End) */
982 
983 /* Mapping of message handlers for Sensor Server (0x1100) */
984 const struct bt_mesh_model_op bt_mesh_sensor_srv_op[] = {
985     { BLE_MESH_MODEL_OP_SENSOR_DESCRIPTOR_GET, 0, sensor_get },
986     { BLE_MESH_MODEL_OP_SENSOR_GET,            0, sensor_get },
987     { BLE_MESH_MODEL_OP_SENSOR_COLUMN_GET,     2, sensor_get },
988     { BLE_MESH_MODEL_OP_SENSOR_SERIES_GET,     2, sensor_get },
989     BLE_MESH_MODEL_OP_END,
990 };
991 
992 /* Mapping of message handlers for Sensor Setup Server (0x1101) */
993 const struct bt_mesh_model_op bt_mesh_sensor_setup_srv_op[] = {
994     { BLE_MESH_MODEL_OP_SENSOR_CADENCE_GET,       2, sensor_get         },
995     { BLE_MESH_MODEL_OP_SENSOR_CADENCE_SET,       4, sensor_cadence_set },
996     { BLE_MESH_MODEL_OP_SENSOR_CADENCE_SET_UNACK, 4, sensor_cadence_set },
997     { BLE_MESH_MODEL_OP_SENSOR_SETTINGS_GET,      2, sensor_get         },
998     { BLE_MESH_MODEL_OP_SENSOR_SETTING_GET,       4, sensor_get         },
999     { BLE_MESH_MODEL_OP_SENSOR_SETTING_SET,       4, sensor_setting_set },
1000     { BLE_MESH_MODEL_OP_SENSOR_SETTING_SET_UNACK, 4, sensor_setting_set },
1001     BLE_MESH_MODEL_OP_END,
1002 };
1003 
check_sensor_server_init(struct bt_mesh_sensor_state * state_start,const uint8_t state_count)1004 static int check_sensor_server_init(struct bt_mesh_sensor_state *state_start,
1005                                     const uint8_t state_count)
1006 {
1007     struct bt_mesh_sensor_state *state = NULL;
1008     struct sensor_setting *setting = NULL;
1009     int i, j;
1010 
1011     for (i = 0; i < state_count; i++) {
1012         state = &state_start[i];
1013         if (state->sensor_property_id == INVALID_SENSOR_PROPERTY_ID) {
1014             BT_ERR("Invalid Sensor Property ID 0x%04x", state->sensor_property_id);
1015             return -EINVAL;
1016         }
1017         /* Check if the same Sensor Property ID exists */
1018         for (int k = i + 1; k < state_count; k++) {
1019             if (state->sensor_property_id == state_start[k].sensor_property_id) {
1020                 BT_ERR("Same Sensor Property ID 0x%04x exists", state->sensor_property_id);
1021                 return -EINVAL;
1022             }
1023         }
1024         if (state->setting_count && state->settings) {
1025             for (j = 0; j < state->setting_count; j++) {
1026                 setting = &state->settings[j];
1027                 if (setting->property_id == INVALID_SENSOR_SETTING_PROPERTY_ID || setting->raw == NULL) {
1028                     BT_ERR("Invalid Sensor Setting state");
1029                     return -EINVAL;
1030                 }
1031                 /* Check if the same Sensor Setting Property ID exists */
1032                 for (int k = j + 1; k < state->setting_count; k++) {
1033                     if (setting->property_id == state->settings[k].property_id) {
1034                         BT_ERR("Same Sensor Setting Property ID 0x%04x exists", setting->property_id);
1035                         return -EINVAL;
1036                     }
1037                 }
1038             }
1039         }
1040         if (state->cadence) {
1041             if (state->cadence->trigger_delta_down == NULL ||
1042                     state->cadence->trigger_delta_up == NULL ||
1043                     state->cadence->fast_cadence_low == NULL ||
1044                     state->cadence->fast_cadence_high == NULL) {
1045                 BT_ERR("Invalid Sensor Cadence state");
1046                 return -EINVAL;
1047             }
1048         }
1049         if (state->sensor_data.raw_value == NULL) {
1050             BT_ERR("Invalid Sensor Data state");
1051             return -EINVAL;
1052         }
1053     }
1054 
1055     return 0;
1056 }
1057 
sensor_server_init(struct bt_mesh_model * model)1058 static int sensor_server_init(struct bt_mesh_model *model)
1059 {
1060     if (model->user_data == NULL) {
1061         BT_ERR("Invalid Sensor Server user data, model id 0x%04x", model->id);
1062         return -EINVAL;
1063     }
1064 
1065     switch (model->id) {
1066     case BLE_MESH_MODEL_ID_SENSOR_SRV: {
1067         struct bt_mesh_sensor_srv *srv = model->user_data;
1068         if (srv->state_count == 0U || srv->states == NULL) {
1069             BT_ERR("Invalid Sensor state, model id 0x%04x", model->id);
1070             return -EINVAL;
1071         }
1072         if (check_sensor_server_init(srv->states, srv->state_count)) {
1073             return -EINVAL;
1074         }
1075         srv->model = model;
1076         break;
1077     }
1078     case BLE_MESH_MODEL_ID_SENSOR_SETUP_SRV: {
1079         struct bt_mesh_sensor_setup_srv *srv = model->user_data;
1080         if (srv->state_count == 0U || srv->states == NULL) {
1081             BT_ERR("Invalid Sensor state, model id 0x%04x", model->id);
1082             return -EINVAL;
1083         }
1084         if (check_sensor_server_init(srv->states, srv->state_count)) {
1085             return -EINVAL;
1086         }
1087         srv->model = model;
1088         break;
1089     }
1090     default:
1091         BT_WARN("Unknown Sensor Server, model id 0x%04x", model->id);
1092         return -EINVAL;
1093     }
1094 
1095     return 0;
1096 }
1097 
sensor_srv_init(struct bt_mesh_model * model)1098 static int sensor_srv_init(struct bt_mesh_model *model)
1099 {
1100     if (model->pub == NULL) {
1101         BT_ERR("Sensor Server has no publication support");
1102         return -EINVAL;
1103     }
1104 
1105     /* When this model is present on an element, the corresponding Sensor Setup
1106      * Server model shall also be present.
1107      */
1108     struct bt_mesh_elem *element = bt_mesh_model_elem(model);
1109     if (bt_mesh_model_find(element, BLE_MESH_MODEL_ID_SENSOR_SETUP_SRV) == NULL) {
1110         BT_WARN("Sensor Setup Server not present");
1111         /* Just give a warning here, continue with the initialization */
1112     }
1113     return sensor_server_init(model);
1114 }
1115 
sensor_setup_srv_init(struct bt_mesh_model * model)1116 static int sensor_setup_srv_init(struct bt_mesh_model *model)
1117 {
1118     if (model->pub == NULL) {
1119         BT_ERR("Sensor Setup Server has no publication support");
1120         return -EINVAL;
1121     }
1122 
1123     return sensor_server_init(model);
1124 }
1125 
1126 #if CONFIG_BLE_MESH_DEINIT
sensor_server_deinit(struct bt_mesh_model * model)1127 static int sensor_server_deinit(struct bt_mesh_model *model)
1128 {
1129     if (model->user_data == NULL) {
1130         BT_ERR("Invalid Sensor Server user, model id 0x%04x", model->id);
1131         return -EINVAL;
1132     }
1133 
1134     return 0;
1135 }
1136 
sensor_srv_deinit(struct bt_mesh_model * model)1137 static int sensor_srv_deinit(struct bt_mesh_model *model)
1138 {
1139     if (model->pub == NULL) {
1140         BT_ERR("Sensor Server has no publication support");
1141         return -EINVAL;
1142     }
1143 
1144     return sensor_server_deinit(model);
1145 }
1146 
sensor_setup_srv_deinit(struct bt_mesh_model * model)1147 static int sensor_setup_srv_deinit(struct bt_mesh_model *model)
1148 {
1149     if (model->pub == NULL) {
1150         BT_ERR("Sensor Setup Server has no publication support");
1151         return -EINVAL;
1152     }
1153 
1154     return sensor_server_deinit(model);
1155 }
1156 #endif /* CONFIG_BLE_MESH_DEINIT */
1157 
1158 const struct bt_mesh_model_cb bt_mesh_sensor_srv_cb = {
1159     .init = sensor_srv_init,
1160 #if CONFIG_BLE_MESH_DEINIT
1161     .deinit = sensor_srv_deinit,
1162 #endif /* CONFIG_BLE_MESH_DEINIT */
1163 };
1164 
1165 const struct bt_mesh_model_cb bt_mesh_sensor_setup_srv_cb = {
1166     .init = sensor_setup_srv_init,
1167 #if CONFIG_BLE_MESH_DEINIT
1168     .deinit = sensor_setup_srv_deinit,
1169 #endif /* CONFIG_BLE_MESH_DEINIT */
1170 };
1171 
1172 #endif /* CONFIG_BLE_MESH_SENSOR_SERVER */
1173