1 /*
2  * SPDX-FileCopyrightText: 2017-2021 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef _SENSOR_SERVER_H_
8 #define _SENSOR_SERVER_H_
9 
10 #include "server_common.h"
11 
12 #ifdef __cplusplus
13 extern "C" {
14 #endif
15 
16 /* Sensor Property ID related */
17 #define INVALID_SENSOR_PROPERTY_ID          0x0000
18 
19 #define SENSOR_PROPERTY_ID_LEN              0x02
20 
21 /* Sensor Descriptor state related */
22 #define SENSOR_DESCRIPTOR_LEN               0x08
23 
24 #define SENSOR_UNSPECIFIED_POS_TOLERANCE    0x000
25 #define SENSOR_UNSPECIFIED_NEG_TOLERANCE    0x000
26 
27 #define SENSOR_NOT_APPL_MEASURE_PERIOD      0x00
28 
29 #define SENSOR_NOT_APPL_UPDATE_INTERVAL     0x00
30 
31 /* Sensor Setting state related */
32 #define INVALID_SENSOR_SETTING_PROPERTY_ID  0x0000
33 
34 #define SENSOR_SETTING_PROPERTY_ID_LEN      0x02
35 #define SENSOR_SETTING_ACCESS_LEN           0x01
36 
37 #define SENSOR_SETTING_ACCESS_READ          0x01
38 #define SENSOR_SETTING_ACCESS_READ_WRITE    0x03
39 
40 /* Sensor Cadence state related */
41 #define SENSOR_DIVISOR_TRIGGER_TYPE_LEN     0x01
42 #define SENSOR_STATUS_MIN_INTERVAL_LEN      0x01
43 
44 #define SENSOR_PERIOD_DIVISOR_MAX_VALUE     15
45 
46 #define SENSOR_STATUS_MIN_INTERVAL_MAX      26
47 
48 #define SENSOR_STATUS_TRIGGER_TYPE_CHAR     0
49 #define SENSOR_STATUS_TRIGGER_TYPE_UINT16   1
50 
51 #define SENSOR_STATUS_TRIGGER_UINT16_LEN    0x02
52 
53 /* Sensor Data state related */
54 #define SENSOR_DATA_FORMAT_A                0x00
55 #define SENSOR_DATA_FORMAT_B                0x01
56 
57 #define SENSOR_DATA_FORMAT_A_MPID_LEN       0x02
58 #define SENSOR_DATA_FORMAT_B_MPID_LEN       0x03
59 
60 #define SENSOR_DATA_ZERO_LEN                0x7F
61 
62 enum bt_mesh_sensor_sample_func {
63     UNSPECIFIED,
64     INSTANTANEOUS,
65     ARITHMETIC_MEAN,
66     RMS,
67     MAXIMUM,
68     MINIMUM,
69     ACCUMULATED,
70     COUNT,
71 };
72 
73 struct sensor_descriptor {
74     uint32_t positive_tolerance : 12,
75              negative_tolerance : 12,
76              sample_function : 8;
77     uint8_t  measure_period;
78     uint8_t  update_interval;
79 };
80 
81 struct sensor_setting {
82     uint16_t property_id;
83     uint8_t  access;
84     /* Or use union to include all possible types */
85     struct net_buf_simple *raw;
86 };
87 
88 struct sensor_cadence {
89     uint8_t period_divisor : 7,
90             trigger_type : 1;
91     struct net_buf_simple *trigger_delta_down;
92     struct net_buf_simple *trigger_delta_up;
93     uint8_t min_interval;
94     struct net_buf_simple *fast_cadence_low;
95     struct net_buf_simple *fast_cadence_high;
96 };
97 
98 struct sensor_data {
99     /**
100      * Format A: The Length field is a 1-based uint4 value (valid range 0x0–0xF,
101      *           representing range of 1 – 16).
102      * Format B: The Length field is a 1-based uint7 value (valid range 0x0–0x7F,
103      *           representing range of 1 – 127). The value 0x7F represents a
104      *           length of zero.
105      */
106     uint8_t format : 1,
107             length : 7;
108     struct net_buf_simple *raw_value;
109 };
110 
111 struct sensor_series_column {
112     struct net_buf_simple *raw_value_x;
113     struct net_buf_simple *column_width;
114     struct net_buf_simple *raw_value_y;
115 };
116 
117 struct bt_mesh_sensor_state {
118     uint16_t sensor_property_id;
119 
120     /* Constant throughout the lifetime of an element */
121     struct sensor_descriptor descriptor;
122 
123     /* Multiple Sensor Setting states may be present for each sensor.
124      * The Sensor Setting Property ID values shall be unique for each
125      * Sensor Property ID that identifies a sensor within an element.
126      */
127     const uint8_t setting_count;
128     struct sensor_setting *settings;
129 
130     /* The Sensor Cadence state may be not supported by sensors based
131      * on device properties referencing "non-scalar characteristics"
132      * such as "histograms" or "composite characteristics".
133      */
134     struct sensor_cadence *cadence;
135 
136     struct sensor_data sensor_data;
137 
138     /* Values measured by sensors may be organized as arrays (and
139      * represented as series of columns, such as histograms).
140      * 1. The Sensor Raw Value X field has a size and representation
141      *    defined by the Sensor Property ID and represents the left
142      *    corner of the column on the X axis.
143      * 2. The Sensor Column Width field has a size and representation
144      *    defined by the Sensor Property ID and represents the width
145      *    of the column on the X axis.
146      * 3. The Sensor Raw Value Y field has a size and representation
147      *    defined by the Sensor Property ID and represents the height
148      *    of the column on the Y axis.
149      * Note: Values outside the bins defined by a Sensor Property are
150      * not included. For example, if the histogram is defined as 3 bins
151      * representing “lamp operating hours in a given temperature range”
152      * and the bins are [40,60), [60, 80), and [80,100], then any hours
153      * outside that [40, 100] range would not be included.
154      */
155     struct sensor_series_column series_column;
156 };
157 
158 /* 1. Multiple instances of the Sensor states may be present within the
159  *    same model, provided that each instance has a unique value of the
160  *    Sensor Property ID to allow the instances to be differentiated.
161  * 2. Note: The number of sensors within a multisensor is limited by the
162  *    size of the message payload for the Sensor Descriptor Status message.
163  *    A single Sensor Descriptor may be sent using a single Unsegmented
164  *    Access message. Using Segmentation and Reassembly (SAR), up to 38
165  *    Sensor Descriptor states may be sent.
166  */
167 
168 struct bt_mesh_sensor_srv {
169     struct bt_mesh_model *model;
170     struct bt_mesh_server_rsp_ctrl rsp_ctrl;
171     const uint8_t state_count;
172     struct bt_mesh_sensor_state *states;
173 };
174 
175 struct bt_mesh_sensor_setup_srv {
176     struct bt_mesh_model *model;
177     struct bt_mesh_server_rsp_ctrl rsp_ctrl;
178     const uint8_t state_count;
179     struct bt_mesh_sensor_state *states;
180 };
181 
182 typedef union {
183     struct {
184         uint16_t id;
185         uint8_t  period_divisor : 7,
186                  trigger_type : 1;
187         struct net_buf_simple *trigger_delta_down;
188         struct net_buf_simple *trigger_delta_up;
189         uint8_t min_interval;
190         struct net_buf_simple *fast_cadence_low;
191         struct net_buf_simple *fast_cadence_high;
192     } sensor_cadence_set;
193     struct {
194         uint16_t id;
195         uint16_t setting_id;
196         struct net_buf_simple *value;
197     } sensor_setting_set;
198 } bt_mesh_sensor_server_state_change_t;
199 
200 typedef union {
201     struct {
202         bool     op_en;
203         uint16_t id;
204     } sensor_descriptor_get;
205     struct {
206         uint16_t id;
207     } sensor_cadence_get;
208     struct {
209         uint16_t id;
210     } sensor_settings_get;
211     struct {
212         uint16_t id;
213         uint16_t setting_id;
214     } sensor_setting_get;
215     struct {
216         bool     op_en;
217         uint16_t id;
218     } sensor_get;
219     struct {
220         uint16_t id;
221         struct net_buf_simple *raw_x;
222     } sensor_column_get;
223     struct {
224         bool     op_en;
225         uint16_t id;
226         struct net_buf_simple *raw;
227     } sensor_series_get;
228 } bt_mesh_sensor_server_recv_get_msg_t;
229 
230 typedef union {
231     struct {
232         uint16_t id;
233         struct net_buf_simple *cadence;
234     } sensor_cadence_set;
235     struct {
236         uint16_t id;
237         uint16_t setting_id;
238         struct net_buf_simple *raw;
239     } sensor_setting_set;
240 } bt_mesh_sensor_server_recv_set_msg_t;
241 
242 #ifdef __cplusplus
243 }
244 #endif
245 
246 #endif /* _SENSOR_SERVER_H_ */
247