1 /*
2  * SPDX-FileCopyrightText: 2017-2021 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /** @file
8  *  @brief Bluetooth Mesh Sensor Client Model APIs.
9  */
10 
11 #ifndef _ESP_BLE_MESH_SENSOR_MODEL_API_H_
12 #define _ESP_BLE_MESH_SENSOR_MODEL_API_H_
13 
14 #include "esp_ble_mesh_defs.h"
15 
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19 
20 /** @def    ESP_BLE_MESH_MODEL_SENSOR_CLI
21  *
22  *  @brief  Define a new Sensor Client Model.
23  *
24  *  @note   This API needs to be called for each element on which
25  *          the application needs to have a Sensor Client Model.
26  *
27  *  @param  cli_pub  Pointer to the unique struct esp_ble_mesh_model_pub_t.
28  *  @param  cli_data Pointer to the unique struct esp_ble_mesh_client_t.
29  *
30  *  @return New Sensor Client Model instance.
31  */
32 #define ESP_BLE_MESH_MODEL_SENSOR_CLI(cli_pub, cli_data)            \
33         ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_SENSOR_CLI,    \
34                     NULL, cli_pub, cli_data)
35 
36 /**
37  *  @brief Bluetooth Mesh Sensor Client Model Get and Set parameters structure.
38  */
39 
40 /** Parameters of Sensor Descriptor Get */
41 typedef struct {
42     bool  op_en;        /*!< Indicate if optional parameters are included */
43     uint16_t property_id;  /*!< Property ID of a sensor (optional) */
44 } esp_ble_mesh_sensor_descriptor_get_t;
45 
46 /** Parameter of Sensor Cadence Get */
47 typedef struct {
48     uint16_t property_id;  /*!< Property ID of a sensor */
49 } esp_ble_mesh_sensor_cadence_get_t;
50 
51 /** Parameters of Sensor Cadence Set */
52 typedef struct {
53     uint16_t property_id;                               /*!< Property ID for the sensor */
54     uint8_t  fast_cadence_period_divisor : 7,           /*!< Divisor for the publish period */
55              status_trigger_type : 1;                   /*!< The unit and format of the Status Trigger Delta fields */
56     struct net_buf_simple *status_trigger_delta_down;   /*!< Delta down value that triggers a status message */
57     struct net_buf_simple *status_trigger_delta_up;     /*!< Delta up value that triggers a status message */
58     uint8_t  status_min_interval;                       /*!< Minimum interval between two consecutive Status messages */
59     struct net_buf_simple *fast_cadence_low;            /*!< Low value for the fast cadence range */
60     struct net_buf_simple *fast_cadence_high;           /*!< Fast value for the fast cadence range */
61 } esp_ble_mesh_sensor_cadence_set_t;
62 
63 /** Parameter of Sensor Settings Get */
64 typedef struct {
65     uint16_t sensor_property_id;   /*!< Property ID of a sensor */
66 } esp_ble_mesh_sensor_settings_get_t;
67 
68 /** Parameters of Sensor Setting Get */
69 typedef struct {
70     uint16_t sensor_property_id;           /*!< Property ID of a sensor */
71     uint16_t sensor_setting_property_id;   /*!< Setting ID identifying a setting within a sensor */
72 } esp_ble_mesh_sensor_setting_get_t;
73 
74 /** Parameters of Sensor Setting Set */
75 typedef struct {
76     uint16_t sensor_property_id;           /*!< Property ID identifying a sensor */
77     uint16_t sensor_setting_property_id;   /*!< Setting ID identifying a setting within a sensor */
78     struct net_buf_simple *sensor_setting_raw;  /*!< Raw value for the setting */
79 } esp_ble_mesh_sensor_setting_set_t;
80 
81 /** Parameters of Sensor Get */
82 typedef struct {
83     bool     op_en;        /*!< Indicate if optional parameters are included  */
84     uint16_t property_id;  /*!< Property ID for the sensor (optional) */
85 } esp_ble_mesh_sensor_get_t;
86 
87 /** Parameters of Sensor Column Get */
88 typedef struct {
89     uint16_t property_id;  /*!< Property identifying a sensor */
90     struct net_buf_simple *raw_value_x; /*!< Raw value identifying a column */
91 } esp_ble_mesh_sensor_column_get_t;
92 
93 /** Parameters of Sensor Series Get */
94 typedef struct {
95     bool     op_en;        /*!< Indicate if optional parameters are included */
96     uint16_t property_id;  /*!< Property identifying a sensor */
97     struct net_buf_simple *raw_value_x1;    /*!< Raw value identifying a starting column (optional) */
98     struct net_buf_simple *raw_value_x2;    /*!< Raw value identifying an ending column (C.1) */
99 } esp_ble_mesh_sensor_series_get_t;
100 
101 /**
102  * @brief Sensor Client Model get message union
103  */
104 typedef union {
105     esp_ble_mesh_sensor_descriptor_get_t descriptor_get;    /*!< For ESP_BLE_MESH_MODEL_OP_SENSOR_DESCRIPTOR_GET */
106     esp_ble_mesh_sensor_cadence_get_t    cadence_get;       /*!< For ESP_BLE_MESH_MODEL_OP_SENSOR_CADENCE_GET */
107     esp_ble_mesh_sensor_settings_get_t   settings_get;      /*!< For ESP_BLE_MESH_MODEL_OP_SENSOR_SETTINGS_GET */
108     esp_ble_mesh_sensor_setting_get_t    setting_get;       /*!< For ESP_BLE_MESH_MODEL_OP_SENSOR_SETTING_GET */
109     esp_ble_mesh_sensor_get_t            sensor_get;        /*!< For ESP_BLE_MESH_MODEL_OP_SENSOR_GET */
110     esp_ble_mesh_sensor_column_get_t     column_get;        /*!< For ESP_BLE_MESH_MODEL_OP_SENSOR_COLUMN_GET */
111     esp_ble_mesh_sensor_series_get_t     series_get;        /*!< For ESP_BLE_MESH_MODEL_OP_SENSOR_SERIES_GET */
112 } esp_ble_mesh_sensor_client_get_state_t;
113 
114 /**
115  * @brief Sensor Client Model set message union
116  */
117 typedef union {
118     esp_ble_mesh_sensor_cadence_set_t cadence_set;          /*!< For ESP_BLE_MESH_MODEL_OP_SENSOR_CADENCE_SET & ESP_BLE_MESH_MODEL_OP_SENSOR_CADENCE_SET_UNACK */
119     esp_ble_mesh_sensor_setting_set_t setting_set;          /*!< For ESP_BLE_MESH_MODEL_OP_SENSOR_SETTING_SET & ESP_BLE_MESH_MODEL_OP_SENSOR_SETTING_SET_UNACK */
120 } esp_ble_mesh_sensor_client_set_state_t;
121 
122 /**
123  *  @brief Bluetooth Mesh Sensor Client Model Get and Set callback parameters structure.
124  */
125 
126 /** Parameter of Sensor Descriptor Status */
127 typedef struct {
128     struct net_buf_simple *descriptor;  /*!< Sequence of 8-octet sensor descriptors (optional) */
129 } esp_ble_mesh_sensor_descriptor_status_cb_t;
130 
131 /** Parameters of Sensor Cadence Status */
132 typedef struct {
133     uint16_t property_id;  /*!< Property for the sensor */
134     struct net_buf_simple *sensor_cadence_value;    /*!< Value of sensor cadence state */
135 } esp_ble_mesh_sensor_cadence_status_cb_t;
136 
137 /** Parameters of Sensor Settings Status */
138 typedef struct {
139     uint16_t sensor_property_id;   /*!< Property ID identifying a sensor */
140     struct net_buf_simple *sensor_setting_property_ids; /*!< A sequence of N sensor setting property IDs (optional) */
141 } esp_ble_mesh_sensor_settings_status_cb_t;
142 
143 /** Parameters of Sensor Setting Status */
144 typedef struct  {
145     bool     op_en;                        /*!< Indicate id optional parameters are included */
146     uint16_t sensor_property_id;           /*!< Property ID identifying a sensor */
147     uint16_t sensor_setting_property_id;   /*!< Setting ID identifying a setting within a sensor */
148     uint8_t  sensor_setting_access;        /*!< Read/Write access rights for the setting (optional) */
149     struct net_buf_simple *sensor_setting_raw;  /*!< Raw value for the setting */
150 } esp_ble_mesh_sensor_setting_status_cb_t;
151 
152 /** Parameter of Sensor Status */
153 typedef struct {
154     struct net_buf_simple *marshalled_sensor_data;  /*!< Value of sensor data state (optional) */
155 } esp_ble_mesh_sensor_status_cb_t;
156 
157 /** Parameters of Sensor Column Status */
158 typedef struct {
159     uint16_t property_id;  /*!< Property identifying a sensor and the Y axis  */
160     struct net_buf_simple *sensor_column_value; /*!< Left values of sensor column status */
161 } esp_ble_mesh_sensor_column_status_cb_t;
162 
163 /** Parameters of Sensor Series Status */
164 typedef struct {
165     uint16_t property_id;  /*!< Property identifying a sensor and the Y axis  */
166     struct net_buf_simple *sensor_series_value; /*!< Left values of sensor series status */
167 } esp_ble_mesh_sensor_series_status_cb_t;
168 
169 /**
170  * @brief Sensor Client Model received message union
171  */
172 typedef union {
173     esp_ble_mesh_sensor_descriptor_status_cb_t descriptor_status;   /*!< For ESP_BLE_MESH_MODEL_OP_SENSOR_DESCRIPTOR_STATUS */
174     esp_ble_mesh_sensor_cadence_status_cb_t    cadence_status;      /*!< For ESP_BLE_MESH_MODEL_OP_SENSOR_CADENCE_STATUS */
175     esp_ble_mesh_sensor_settings_status_cb_t   settings_status;     /*!< For ESP_BLE_MESH_MODEL_OP_SENSOR_SETTINGS_STATUS */
176     esp_ble_mesh_sensor_setting_status_cb_t    setting_status;      /*!< For ESP_BLE_MESH_MODEL_OP_SENSOR_SETTING_STATUS */
177     esp_ble_mesh_sensor_status_cb_t            sensor_status;       /*!< For ESP_BLE_MESH_MODEL_OP_SENSOR_STATUS */
178     esp_ble_mesh_sensor_column_status_cb_t     column_status;       /*!< For ESP_BLE_MESH_MODEL_OP_SENSOR_COLUMN_STATUS */
179     esp_ble_mesh_sensor_series_status_cb_t     series_status;       /*!< For ESP_BLE_MESH_MODEL_OP_SENSOR_SERIES_STATUS */
180 } esp_ble_mesh_sensor_client_status_cb_t;
181 
182 /** Sensor Client Model callback parameters */
183 typedef struct {
184     int error_code;     /*!< 0: success,
185                          *   otherwise failure. For the error code values please refer to errno.h file.
186                          *   A negative sign is added to the standard error codes in errno.h. */
187     esp_ble_mesh_client_common_param_t    *params;      /*!< The client common parameters. */
188     esp_ble_mesh_sensor_client_status_cb_t status_cb;   /*!< The sensor status message callback values */
189 } esp_ble_mesh_sensor_client_cb_param_t;
190 
191 /** This enum value is the event of Sensor Client Model */
192 typedef enum {
193     ESP_BLE_MESH_SENSOR_CLIENT_GET_STATE_EVT,
194     ESP_BLE_MESH_SENSOR_CLIENT_SET_STATE_EVT,
195     ESP_BLE_MESH_SENSOR_CLIENT_PUBLISH_EVT,
196     ESP_BLE_MESH_SENSOR_CLIENT_TIMEOUT_EVT,
197     ESP_BLE_MESH_SENSOR_CLIENT_EVT_MAX,
198 } esp_ble_mesh_sensor_client_cb_event_t;
199 
200 /**
201  *  @brief Bluetooth Mesh Sensor Client Model function.
202  */
203 
204 /**
205  * @brief   Sensor Client Model callback function type
206  * @param   event: Event type
207  * @param   param: Pointer to callback parameter
208  */
209 typedef void (* esp_ble_mesh_sensor_client_cb_t)(esp_ble_mesh_sensor_client_cb_event_t event,
210                                                  esp_ble_mesh_sensor_client_cb_param_t *param);
211 
212 /**
213  * @brief       Register BLE Mesh Sensor Client Model callback.
214  *
215  * @param[in]   callback: Pointer to the callback function.
216  *
217  * @return      ESP_OK on success or error code otherwise.
218  *
219  */
220 esp_err_t esp_ble_mesh_register_sensor_client_callback(esp_ble_mesh_sensor_client_cb_t callback);
221 
222 /**
223  * @brief       Get the value of Sensor Server Model states using the Sensor Client Model get messages.
224  *
225  * @note        If you want to know the opcodes and corresponding meanings accepted by this API,
226  *              please refer to esp_ble_mesh_sensor_message_opcode_t in esp_ble_mesh_defs.h
227  *
228  * @param[in]   params:    Pointer to BLE Mesh common client parameters.
229  * @param[in]   get_state: Pointer to sensor get message value.
230  *                         Shall not be set to NULL.
231  *
232  * @return      ESP_OK on success or error code otherwise.
233  *
234  */
235 esp_err_t esp_ble_mesh_sensor_client_get_state(esp_ble_mesh_client_common_param_t *params,
236                                                esp_ble_mesh_sensor_client_get_state_t *get_state);
237 
238 /**
239  * @brief       Set the value of Sensor Server Model states using the Sensor Client Model set messages.
240  *
241  * @note        If you want to know the opcodes and corresponding meanings accepted by this API,
242  *              please refer to esp_ble_mesh_sensor_message_opcode_t in esp_ble_mesh_defs.h
243  *
244  * @param[in]   params:    Pointer to BLE Mesh common client parameters.
245  * @param[in]   set_state: Pointer to sensor set message value.
246  *                         Shall not be set to NULL.
247  *
248  * @return      ESP_OK on success or error code otherwise.
249  *
250  */
251 esp_err_t esp_ble_mesh_sensor_client_set_state(esp_ble_mesh_client_common_param_t *params,
252                                                esp_ble_mesh_sensor_client_set_state_t *set_state);
253 
254 /**
255  * @brief Sensor Server Models related context.
256  */
257 
258 /** @def    ESP_BLE_MESH_MODEL_SENSOR_SRV
259  *
260  *  @brief  Define a new Sensor Server Model.
261  *
262  *  @note   1. The Sensor Server model is a root model. When this model is present
263  *             on an element, the corresponding Sensor Setup Server model shall
264  *             also be present.
265  *          2. This model shall support model publication and model subscription.
266  *
267  *  @param  srv_pub  Pointer to the unique struct esp_ble_mesh_model_pub_t.
268  *  @param  srv_data Pointer to the unique struct esp_ble_mesh_sensor_srv_t.
269  *
270  *  @return New Sensor Server Model instance.
271  */
272 #define ESP_BLE_MESH_MODEL_SENSOR_SRV(srv_pub, srv_data)             \
273         ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_SENSOR_SRV,     \
274                     NULL, srv_pub, srv_data)
275 
276 /** @def    ESP_BLE_MESH_MODEL_SENSOR_SETUP_SRV
277  *
278  *  @brief  Define a new Sensor Setup Server Model.
279  *
280  *  @note   1. The Sensor Setup Server model extends the Sensor Server model.
281  *          2. This model shall support model publication and model subscription.
282  *
283  *  @param  srv_pub  Pointer to the unique struct esp_ble_mesh_model_pub_t.
284  *  @param  srv_data Pointer to the unique struct esp_ble_mesh_sensor_setup_srv_t.
285  *
286  *  @return New Sensor Setup Server Model instance.
287  */
288 #define ESP_BLE_MESH_MODEL_SENSOR_SETUP_SRV(srv_pub, srv_data)             \
289         ESP_BLE_MESH_SIG_MODEL(ESP_BLE_MESH_MODEL_ID_SENSOR_SETUP_SRV,     \
290                     NULL, srv_pub, srv_data)
291 
292 #define ESP_BLE_MESH_INVALID_SENSOR_PROPERTY_ID 0x0000  /*!< Invalid Sensor Property ID */
293 
294 #define ESP_BLE_MESH_SENSOR_PROPERTY_ID_LEN     0x02    /*!< Length of Sensor Property ID */
295 
296 #define ESP_BLE_MESH_SENSOR_DESCRIPTOR_LEN      0x08    /*!< Length of Sensor Descriptor state */
297 
298 #define ESP_BLE_MESH_SENSOR_UNSPECIFIED_POS_TOLERANCE   0x000   /*!< Unspecified Sensor Positive Tolerance */
299 #define ESP_BLE_MESH_SENSOR_UNSPECIFIED_NEG_TOLERANCE   0x000   /*!< Unspecified Sensor Negative Tolerance */
300 
301 #define ESP_BLE_MESH_SENSOR_NOT_APPL_MEASURE_PERIOD     0x00    /*!< Not applicable Sensor Measurement Period */
302 
303 #define ESP_BLE_MESH_SENSOR_NOT_APPL_UPDATE_INTERVAL    0x00    /*!< Not applicable Sensor Update Interval */
304 
305 #define ESP_BLE_MESH_INVALID_SENSOR_SETTING_PROPERTY_ID 0x0000  /*!< Invalid Sensor Setting Property ID */
306 
307 #define ESP_BLE_MESH_SENSOR_SETTING_PROPERTY_ID_LEN     0x02    /*!< Length of Sensor Setting Property ID */
308 #define ESP_BLE_MESH_SENSOR_SETTING_ACCESS_LEN          0x01    /*!< Length of Sensor Setting Access */
309 
310 #define ESP_BLE_MESH_SENSOR_SETTING_ACCESS_READ         0x01    /*!< Sensor Setting Access - Read */
311 #define ESP_BLE_MESH_SENSOR_SETTING_ACCESS_READ_WRITE   0x03    /*!< Sensor Setting Access - Read & Write */
312 
313 #define ESP_BLE_MESH_SENSOR_DIVISOR_TRIGGER_TYPE_LEN    0x01    /*!< Length of Sensor Divisor Trigger Type */
314 #define ESP_BLE_MESH_SENSOR_STATUS_MIN_INTERVAL_LEN     0x01    /*!< Length of Sensor Status Min Interval */
315 
316 #define ESP_BLE_MESH_SENSOR_PERIOD_DIVISOR_MAX_VALUE    15  /*!< Maximum value of Sensor Period Divisor */
317 
318 #define ESP_BLE_MESH_SENSOR_STATUS_MIN_INTERVAL_MAX     26  /*!< Maximum value of Sensor Status Min Interval */
319 
320 /**
321  * Sensor Status Trigger Type - Format Type of the characteristic
322  * that the Sensor Property ID state references
323  */
324 #define ESP_BLE_MESH_SENSOR_STATUS_TRIGGER_TYPE_CHAR    0
325 /** Sensor Status Trigger Type - Format Type "uint16" */
326 #define ESP_BLE_MESH_SENSOR_STATUS_TRIGGER_TYPE_UINT16  1
327 
328 #define ESP_BLE_MESH_SENSOR_DATA_FORMAT_A   0x00    /*!< Sensor Data Format A */
329 #define ESP_BLE_MESH_SENSOR_DATA_FORMAT_B   0x01    /*!< Sensor Data Format B */
330 
331 #define ESP_BLE_MESH_SENSOR_DATA_FORMAT_A_MPID_LEN  0x02    /*!< MPID length of Sensor Data Format A */
332 #define ESP_BLE_MESH_SENSOR_DATA_FORMAT_B_MPID_LEN  0x03    /*!< MPID length of Sensor Data Format B */
333 
334 /**
335  * Zero length of Sensor Data.
336  *
337  * Note:
338  * The Length field is a 1-based uint7 value (valid range 0x0–0x7F,
339  * representing range of 1–127). The value 0x7F represents a length
340  * of zero.
341  */
342 #define ESP_BLE_MESH_SENSOR_DATA_ZERO_LEN   0x7F
343 
344 /** @def    ESP_BLE_MESH_GET_SENSOR_DATA_FORMAT
345  *
346  *  @brief  Get format of the sensor data.
347  *
348  *  @note   Multiple sensor data may be concatenated. Make sure the _data pointer is
349  *          updated before getting the format of the corresponding sensor data.
350  *
351  *  @param  _data Pointer to the start of the sensor data.
352  *
353  *  @return Format of the sensor data.
354  */
355 #define ESP_BLE_MESH_GET_SENSOR_DATA_FORMAT(_data)      (((_data)[0]) & BIT_MASK(1))
356 
357 /** @def    ESP_BLE_MESH_GET_SENSOR_DATA_LENGTH
358  *
359  *  @brief  Get length of the sensor data.
360  *
361  *  @note   Multiple sensor data may be concatenated. Make sure the _data pointer is
362  *          updated before getting the length of the corresponding sensor data.
363  *
364  *  @param  _data Pointer to the start of the sensor data.
365  *  @param  _fmt  Format of the sensor data.
366  *
367  *  @return Length (zero-based) of the sensor data.
368  */
369 #define ESP_BLE_MESH_GET_SENSOR_DATA_LENGTH(_data, _fmt)    \
370             (((_fmt) == ESP_BLE_MESH_SENSOR_DATA_FORMAT_A) ? ((((_data)[0]) >> 1) & BIT_MASK(4)) : ((((_data)[0]) >> 1) & BIT_MASK(7)))
371 
372 /** @def    ESP_BLE_MESH_GET_SENSOR_DATA_PROPERTY_ID
373  *
374  *  @brief  Get Sensor Property ID of the sensor data.
375  *
376  *  @note   Multiple sensor data may be concatenated. Make sure the _data pointer is
377  *          updated before getting Sensor Property ID of the corresponding sensor data.
378  *
379  *  @param  _data Pointer to the start of the sensor data.
380  *  @param  _fmt  Format of the sensor data.
381  *
382  *  @return Sensor Property ID of the sensor data.
383  */
384 #define ESP_BLE_MESH_GET_SENSOR_DATA_PROPERTY_ID(_data, _fmt)   \
385             (((_fmt) == ESP_BLE_MESH_SENSOR_DATA_FORMAT_A) ? ((((_data)[1]) << 3) | (((_data)[0]) >> 5)) : ((((_data)[2]) << 8) | ((_data)[1])))
386 
387 /** @def    ESP_BLE_MESH_SENSOR_DATA_FORMAT_A_MPID
388  *
389  *  @brief  Generate a MPID value for sensor data with Format A.
390  *
391  *  @note   1. The Format field is 0b0 and indicates that Format A is used.
392  *          2. The Length field is a 1-based uint4 value (valid range 0x0–0xF,
393  *             representing range of 1–16).
394  *          3. The Property ID is an 11-bit bit field representing 11 LSb of a Property ID.
395  *          4. This format may be used for Property Values that are not longer than 16
396  *             octets and for Property IDs less than 0x0800.
397  *
398  *  @param  _len Length of Sensor Raw value.
399  *  @param  _id  Sensor Property ID.
400  *
401  *  @return 2-octet MPID value for sensor data with Format A.
402  *
403  */
404 #define ESP_BLE_MESH_SENSOR_DATA_FORMAT_A_MPID(_len, _id) \
405         ((((_id) & BIT_MASK(11)) << 5) | (((_len) & BIT_MASK(4)) << 1) | ESP_BLE_MESH_SENSOR_DATA_FORMAT_A)
406 
407 /** @def    ESP_BLE_MESH_SENSOR_DATA_FORMAT_B_MPID
408  *
409  *  @brief  Generate a MPID value for sensor data with Format B.
410  *
411  *  @note   1. The Format field is 0b1 and indicates Format B is used.
412  *          2. The Length field is a 1-based uint7 value (valid range 0x0–0x7F, representing
413  *             range of 1–127). The value 0x7F represents a length of zero.
414  *          3. The Property ID is a 16-bit bit field representing a Property ID.
415  *          4. This format may be used for Property Values not longer than 128 octets and for
416  *             any Property IDs. Property values longer than 128 octets are not supported by
417  *             the Sensor Status message.
418  *          5. Exclude the generated 1-octet value, the 2-octet Sensor Property ID
419  *
420  *  @param  _len Length of Sensor Raw value.
421  *  @param  _id  Sensor Property ID.
422  *
423  *  @return 3-octet MPID value for sensor data with Format B.
424  *
425  */
426 #define ESP_BLE_MESH_SENSOR_DATA_FORMAT_B_MPID(_len, _id) \
427         (((_id) << 8) | (((_len) & BIT_MASK(7)) << 1) | ESP_BLE_MESH_SENSOR_DATA_FORMAT_B)
428 
429 /** This enum value is value of Sensor Sampling Function */
430 enum esp_ble_mesh_sensor_sample_func {
431     ESP_BLE_MESH_SAMPLE_FUNC_UNSPECIFIED,
432     ESP_BLE_MESH_SAMPLE_FUNC_INSTANTANEOUS,
433     ESP_BLE_MESH_SAMPLE_FUNC_ARITHMETIC_MEAN,
434     ESP_BLE_MESH_SAMPLE_FUNC_RMS,
435     ESP_BLE_MESH_SAMPLE_FUNC_MAXIMUM,
436     ESP_BLE_MESH_SAMPLE_FUNC_MINIMUM,
437     ESP_BLE_MESH_SAMPLE_FUNC_ACCUMULATED,
438     ESP_BLE_MESH_SAMPLE_FUNC_COUNT,
439 };
440 
441 /** Parameters of Sensor Descriptor state */
442 typedef struct {
443     uint32_t positive_tolerance : 12,   /*!< The value of Sensor Positive Tolerance field */
444              negative_tolerance : 12,   /*!< The value of Sensor Negative Tolerance field */
445              sampling_function : 8;     /*!< The value of Sensor Sampling Function field */
446     uint8_t  measure_period;            /*!< The value of Sensor Measurement Period field */
447     uint8_t  update_interval;           /*!< The value of Sensor Update Interval field */
448 } esp_ble_mesh_sensor_descriptor_t;
449 
450 /** Parameters of Sensor Setting state */
451 typedef struct {
452     uint16_t property_id;       /*!< The value of Sensor Setting Property ID field */
453     uint8_t  access;            /*!< The value of Sensor Setting Access field */
454     struct net_buf_simple *raw; /*!< The value of Sensor Setting Raw field */
455 } esp_ble_mesh_sensor_setting_t;
456 
457 /** Parameters of Sensor Cadence state */
458 typedef struct {
459     uint8_t period_divisor : 7, /*!< The value of Fast Cadence Period Divisor field */
460             trigger_type : 1;   /*!< The value of Status Trigger Type field */
461     /**
462      * Note:
463      * The parameter "size" in trigger_delta_down, trigger_delta_up, fast_cadence_low &
464      * fast_cadence_high indicates the exact length of these four parameters, and they
465      * are associated with the Sensor Property ID. Users need to initialize the "size"
466      * precisely.
467      */
468     struct net_buf_simple *trigger_delta_down;  /*!< The value of Status Trigger Delta Down field */
469     struct net_buf_simple *trigger_delta_up;    /*!< The value of Status Trigger Delta Up field */
470     uint8_t min_interval;                       /*!< The value of Status Min Interval field */
471     struct net_buf_simple *fast_cadence_low;    /*!< The value of Fast Cadence Low field */
472     struct net_buf_simple *fast_cadence_high;   /*!< The value of Fast Cadence High field */
473 } esp_ble_mesh_sensor_cadence_t;
474 
475 /** Parameters of Sensor Data state */
476 typedef struct {
477     /**
478      * Format A: The Length field is a 1-based uint4 value (valid range 0x0–0xF,
479      *           representing range of 1 – 16).
480      * Format B: The Length field is a 1-based uint7 value (valid range 0x0–0x7F,
481      *           representing range of 1 – 127). The value 0x7F represents a
482      *           length of zero.
483      */
484     uint8_t format : 1, /*!< The value of the Sensor Data format */
485             length : 7; /*!< The value of the Sensor Data length */
486     struct net_buf_simple *raw_value;   /*!< The value of Sensor Data raw value */
487 } esp_ble_mesh_sensor_data_t;
488 
489 /** Parameters of Sensor Series Column state */
490 typedef struct {
491     struct net_buf_simple *raw_value_x;     /*!< The value of Sensor Raw Value X field */
492     struct net_buf_simple *column_width;    /*!< The value of Sensor Column Width field */
493     struct net_buf_simple *raw_value_y;     /*!< The value of Sensor Raw Value Y field */
494 } esp_ble_mesh_sensor_series_column_t;
495 
496 /** Parameters of Sensor states */
497 typedef struct {
498     uint16_t sensor_property_id;    /*!< The value of Sensor Property ID field */
499 
500     /* Constant throughout the lifetime of an element */
501     esp_ble_mesh_sensor_descriptor_t descriptor;    /*!< Parameters of the Sensor Descriptor state */
502 
503     /**
504      * Multiple Sensor Setting states may be present for each sensor.
505      * The Sensor Setting Property ID values shall be unique for each
506      * Sensor Property ID that identifies a sensor within an element.
507      */
508     const uint8_t setting_count;                /*!<  */
509     esp_ble_mesh_sensor_setting_t *settings;    /*!< Parameters of the Sensor Setting state */
510 
511     /**
512      * The Sensor Cadence state may be not supported by sensors based
513      * on device properties referencing "non-scalar characteristics"
514      * such as "histograms" or "composite characteristics".
515      */
516     esp_ble_mesh_sensor_cadence_t *cadence;     /*!< Parameters of the Sensor Cadence state */
517 
518     esp_ble_mesh_sensor_data_t sensor_data;     /*!< Parameters of the Sensor Data state */
519 
520     esp_ble_mesh_sensor_series_column_t series_column;  /*!< Parameters of the Sensor Series Column state */
521 } esp_ble_mesh_sensor_state_t;
522 
523 /** User data of Sensor Server Model */
524 typedef struct {
525     esp_ble_mesh_model_t *model;                /*!< Pointer to the Sensor Server Model. Initialized internally. */
526     esp_ble_mesh_server_rsp_ctrl_t rsp_ctrl;    /*!< Response control of the server model received messages */
527     const uint8_t state_count;                  /*!< Sensor state count */
528     esp_ble_mesh_sensor_state_t *states;        /*!< Parameters of the Sensor states */
529 } esp_ble_mesh_sensor_srv_t;
530 
531 /** User data of Sensor Setup Server Model */
532 typedef struct {
533     esp_ble_mesh_model_t *model;                /*!< Pointer to the Sensor Setup Server Model. Initialized internally. */
534     esp_ble_mesh_server_rsp_ctrl_t rsp_ctrl;    /*!< Response control of the server model received messages */
535     const uint8_t state_count;                  /*!< Sensor state count */
536     esp_ble_mesh_sensor_state_t *states;        /*!< Parameters of the Sensor states */
537 } esp_ble_mesh_sensor_setup_srv_t;
538 
539 /** Parameters of Sensor Cadence Set state change event */
540 typedef struct {
541     uint16_t property_id;           /*!< The value of Sensor Property ID state */
542     uint8_t  period_divisor : 7,    /*!< The value of Fast Cadence Period Divisor state */
543              trigger_type : 1;      /*!< The value of Status Trigger Type state */
544     struct net_buf_simple *trigger_delta_down;  /*!< The value of Status Trigger Delta Down state */
545     struct net_buf_simple *trigger_delta_up;    /*!< The value of Status Trigger Delta Up state */
546     uint8_t min_interval;                       /*!< The value of Status Min Interval state */
547     struct net_buf_simple *fast_cadence_low;    /*!< The value of Fast Cadence Low state */
548     struct net_buf_simple *fast_cadence_high;   /*!< The value of Fast Cadence High state */
549 } esp_ble_mesh_state_change_sensor_cadence_set_t;
550 
551 /** Parameters of Sensor Setting Set state change event */
552 typedef struct {
553     uint16_t property_id;           /*!< The value of Sensor Property ID state */
554     uint16_t setting_property_id;   /*!< The value of Sensor Setting Property ID state */
555     struct net_buf_simple *setting_value;   /*!< The value of Sensor Property Value state */
556 } esp_ble_mesh_state_change_sensor_setting_set_t;
557 
558 /**
559  * @brief Sensor Server Model state change value union
560  */
561 typedef union {
562     /**
563      * The recv_op in ctx can be used to decide which state is changed.
564      */
565     esp_ble_mesh_state_change_sensor_cadence_set_t sensor_cadence_set;  /*!< Sensor Cadence Set */
566     esp_ble_mesh_state_change_sensor_setting_set_t sensor_setting_set;  /*!< Sensor Setting Set */
567 } esp_ble_mesh_sensor_server_state_change_t;
568 
569 /** Context of the received Sensor Descriptor Get message */
570 typedef struct {
571     bool  op_en;    /*!< Indicate if optional parameters are included */
572     uint16_t property_id;   /*!< Property ID of a sensor (optional) */
573 } esp_ble_mesh_server_recv_sensor_descriptor_get_t;
574 
575 /** Context of the received Sensor Cadence Get message */
576 typedef struct {
577     uint16_t property_id;   /*!< Property ID of a sensor */
578 } esp_ble_mesh_server_recv_sensor_cadence_get_t;
579 
580 /** Context of the received Sensor Settings Get message */
581 typedef struct {
582     uint16_t property_id;   /*!< Property ID of a sensor */
583 } esp_ble_mesh_server_recv_sensor_settings_get_t;
584 
585 /** Context of the received Sensor Setting Get message */
586 typedef struct {
587     uint16_t property_id;           /*!< Property ID of a sensor */
588     uint16_t setting_property_id;   /*!< Setting ID identifying a setting within a sensor */
589 } esp_ble_mesh_server_recv_sensor_setting_get_t;
590 
591 /** Context of the received Sensor Get message */
592 typedef struct {
593     bool     op_en;         /*!< Indicate if optional parameters are included  */
594     uint16_t property_id;   /*!< Property ID for the sensor (optional) */
595 } esp_ble_mesh_server_recv_sensor_get_t;
596 
597 /** Context of the received Sensor Column Get message */
598 typedef struct {
599     uint16_t property_id;   /*!< Property identifying a sensor */
600     struct net_buf_simple *raw_value_x; /*!< Raw value identifying a column */
601 } esp_ble_mesh_server_recv_sensor_column_get_t;
602 
603 /** Context of the received Sensor Series Get message */
604 typedef struct {
605     bool     op_en;         /*!< Indicate if optional parameters are included */
606     uint16_t property_id;   /*!< Property identifying a sensor */
607     struct net_buf_simple *raw_value;   /*!< Raw value containing X1 and X2 (optional) */
608 } esp_ble_mesh_server_recv_sensor_series_get_t;
609 
610 /**
611  * @brief Sensor Server Model received get message union
612  */
613 typedef union {
614     esp_ble_mesh_server_recv_sensor_descriptor_get_t sensor_descriptor; /*!< Sensor Descriptor Get */
615     esp_ble_mesh_server_recv_sensor_cadence_get_t    sensor_cadence;    /*!< Sensor Cadence Get */
616     esp_ble_mesh_server_recv_sensor_settings_get_t   sensor_settings;   /*!< Sensor Settings Get */
617     esp_ble_mesh_server_recv_sensor_setting_get_t    sensor_setting;    /*!< Sensor Setting Get */
618     esp_ble_mesh_server_recv_sensor_get_t            sensor_data;       /*!< Sensor Get */
619     esp_ble_mesh_server_recv_sensor_column_get_t     sensor_column;     /*!< Sensor Column Get */
620     esp_ble_mesh_server_recv_sensor_series_get_t     sensor_series;     /*!< Sensor Series Get */
621 } esp_ble_mesh_sensor_server_recv_get_msg_t;
622 
623 /** Context of the received Sensor Cadence Set message */
624 typedef struct {
625     uint16_t property_id;           /*!< Property ID for the sensor */
626     struct net_buf_simple *cadence; /*!< Value of Sensor Cadence state */
627 } esp_ble_mesh_server_recv_sensor_cadence_set_t;
628 
629 /** Context of the received Sensor Setting Set message */
630 typedef struct {
631     uint16_t property_id;           /*!< Property ID identifying a sensor */
632     uint16_t setting_property_id;   /*!< Setting ID identifying a setting within a sensor */
633     struct net_buf_simple *setting_raw; /*!< Raw value for the setting */
634 } esp_ble_mesh_server_recv_sensor_setting_set_t;
635 
636 /**
637  * @brief Sensor Server Model received set message union
638  */
639 typedef union {
640     esp_ble_mesh_server_recv_sensor_cadence_set_t sensor_cadence;   /*!< Sensor Cadence Set */
641     esp_ble_mesh_server_recv_sensor_setting_set_t sensor_setting;   /*!< Sensor Setting Set */
642 } esp_ble_mesh_sensor_server_recv_set_msg_t;
643 
644 /**
645  * @brief Sensor Server Model callback value union
646  */
647 typedef union {
648     esp_ble_mesh_sensor_server_state_change_t state_change; /*!< ESP_BLE_MESH_SENSOR_SERVER_STATE_CHANGE_EVT */
649     esp_ble_mesh_sensor_server_recv_get_msg_t get;  /*!< ESP_BLE_MESH_SENSOR_SERVER_RECV_GET_MSG_EVT */
650     esp_ble_mesh_sensor_server_recv_set_msg_t set;  /*!< ESP_BLE_MESH_SENSOR_SERVER_RECV_SET_MSG_EVT */
651 } esp_ble_mesh_sensor_server_cb_value_t;
652 
653 /** Sensor Server Model callback parameters */
654 typedef struct {
655     esp_ble_mesh_model_t  *model;   /*!< Pointer to Sensor Server Models */
656     esp_ble_mesh_msg_ctx_t ctx;     /*!< Context of the received messages */
657     esp_ble_mesh_sensor_server_cb_value_t value;    /*!< Value of the received Sensor Messages */
658 } esp_ble_mesh_sensor_server_cb_param_t;
659 
660 /** This enum value is the event of Sensor Server Model */
661 typedef enum {
662     /**
663      * 1. When get_auto_rsp is set to ESP_BLE_MESH_SERVER_AUTO_RSP, no event will be
664      *    callback to the application layer when Sensor Get messages are received.
665      * 2. When set_auto_rsp is set to ESP_BLE_MESH_SERVER_AUTO_RSP, this event will
666      *    be callback to the application layer when Sensor Set/Set Unack messages
667      *    are received.
668      */
669     ESP_BLE_MESH_SENSOR_SERVER_STATE_CHANGE_EVT,
670     /**
671      * When get_auto_rsp is set to ESP_BLE_MESH_SERVER_RSP_BY_APP, this event will be
672      * callback to the application layer when Sensor Get messages are received.
673      */
674     ESP_BLE_MESH_SENSOR_SERVER_RECV_GET_MSG_EVT,
675     /**
676      * When set_auto_rsp is set to ESP_BLE_MESH_SERVER_RSP_BY_APP, this event will be
677      * callback to the application layer when Sensor Set/Set Unack messages are received.
678      */
679     ESP_BLE_MESH_SENSOR_SERVER_RECV_SET_MSG_EVT,
680     ESP_BLE_MESH_SENSOR_SERVER_EVT_MAX,
681 } esp_ble_mesh_sensor_server_cb_event_t;
682 
683 /**
684  *  @brief Bluetooth Mesh Sensor Server Model function.
685  */
686 
687 /**
688  * @brief   Sensor Server Model callback function type
689  * @param   event: Event type
690  * @param   param: Pointer to callback parameter
691  */
692 typedef void (* esp_ble_mesh_sensor_server_cb_t)(esp_ble_mesh_sensor_server_cb_event_t event,
693                                                  esp_ble_mesh_sensor_server_cb_param_t *param);
694 
695 /**
696  * @brief       Register BLE Mesh Sensor Server Model callback.
697  *
698  * @param[in]   callback: Pointer to the callback function.
699  *
700  * @return      ESP_OK on success or error code otherwise.
701  *
702  */
703 esp_err_t esp_ble_mesh_register_sensor_server_callback(esp_ble_mesh_sensor_server_cb_t callback);
704 
705 #ifdef __cplusplus
706 }
707 #endif
708 
709 #endif /* _ESP_BLE_MESH_SENSOR_MODEL_API_H_ */
710