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