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