Merge branch 'feat/ble_mesh_sensor_model_example_v3.3' into 'release/v3.3'
ble_mesh: Add ble mesh sensor model examples (v3.3) See merge request espressif/esp-idf!8509
This commit is contained in:
@@ -345,6 +345,91 @@ esp_err_t esp_ble_mesh_sensor_client_set_state(esp_ble_mesh_client_common_param_
|
||||
*/
|
||||
#define ESP_BLE_MESH_SENSOR_DATA_ZERO_LEN 0x7F
|
||||
|
||||
/** @def ESP_BLE_MESH_GET_SENSOR_DATA_FORMAT
|
||||
*
|
||||
* @brief Get format of the sensor data.
|
||||
*
|
||||
* @note Multiple sensor data may be concatenated. Make sure the _data pointer is
|
||||
* updated before getting the format of the corresponding sensor data.
|
||||
*
|
||||
* @param _data Pointer to the start of the sensor data.
|
||||
*
|
||||
* @return Format of the sensor data.
|
||||
*/
|
||||
#define ESP_BLE_MESH_GET_SENSOR_DATA_FORMAT(_data) (((_data)[0]) & BIT_MASK(1))
|
||||
|
||||
/** @def ESP_BLE_MESH_GET_SENSOR_DATA_LENGTH
|
||||
*
|
||||
* @brief Get length of the sensor data.
|
||||
*
|
||||
* @note Multiple sensor data may be concatenated. Make sure the _data pointer is
|
||||
* updated before getting the length of the corresponding sensor data.
|
||||
*
|
||||
* @param _data Pointer to the start of the sensor data.
|
||||
* @param _fmt Format of the sensor data.
|
||||
*
|
||||
* @return Length (zero-based) of the sensor data.
|
||||
*/
|
||||
#define ESP_BLE_MESH_GET_SENSOR_DATA_LENGTH(_data, _fmt) \
|
||||
(((_fmt) == ESP_BLE_MESH_SENSOR_DATA_FORMAT_A) ? ((((_data)[0]) >> 1) & BIT_MASK(4)) : ((((_data)[0]) >> 1) & BIT_MASK(7)))
|
||||
|
||||
/** @def ESP_BLE_MESH_GET_SENSOR_DATA_PROPERTY_ID
|
||||
*
|
||||
* @brief Get Sensor Property ID of the sensor data.
|
||||
*
|
||||
* @note Multiple sensor data may be concatenated. Make sure the _data pointer is
|
||||
* updated before getting Sensor Property ID of the corresponding sensor data.
|
||||
*
|
||||
* @param _data Pointer to the start of the sensor data.
|
||||
* @param _fmt Format of the sensor data.
|
||||
*
|
||||
* @return Sensor Property ID of the sensor data.
|
||||
*/
|
||||
#define ESP_BLE_MESH_GET_SENSOR_DATA_PROPERTY_ID(_data, _fmt) \
|
||||
(((_fmt) == ESP_BLE_MESH_SENSOR_DATA_FORMAT_A) ? ((((_data)[1]) << 3) | (((_data)[0]) >> 5)) : ((((_data)[2]) << 8) | ((_data)[1])))
|
||||
|
||||
/** @def ESP_BLE_MESH_SENSOR_DATA_FORMAT_A_MPID
|
||||
*
|
||||
* @brief Generate a MPID value for sensor data with Format A.
|
||||
*
|
||||
* @note 1. The Format field is 0b0 and indicates that Format A is used.
|
||||
* 2. The Length field is a 1-based uint4 value (valid range 0x0–0xF,
|
||||
* representing range of 1–16).
|
||||
* 3. The Property ID is an 11-bit bit field representing 11 LSb of a Property ID.
|
||||
* 4. This format may be used for Property Values that are not longer than 16
|
||||
* octets and for Property IDs less than 0x0800.
|
||||
*
|
||||
* @param _len Length of Sensor Raw value.
|
||||
* @param _id Sensor Property ID.
|
||||
*
|
||||
* @return 2-octet MPID value for sensor data with Format A.
|
||||
*
|
||||
*/
|
||||
#define ESP_BLE_MESH_SENSOR_DATA_FORMAT_A_MPID(_len, _id) \
|
||||
((((_id) & BIT_MASK(11)) << 5) | (((_len) & BIT_MASK(4)) << 1) | ESP_BLE_MESH_SENSOR_DATA_FORMAT_A)
|
||||
|
||||
/** @def ESP_BLE_MESH_SENSOR_DATA_FORMAT_B_MPID
|
||||
*
|
||||
* @brief Generate a MPID value for sensor data with Format B.
|
||||
*
|
||||
* @note 1. The Format field is 0b1 and indicates Format B is used.
|
||||
* 2. The Length field is a 1-based uint7 value (valid range 0x0–0x7F, representing
|
||||
* range of 1–127). The value 0x7F represents a length of zero.
|
||||
* 3. The Property ID is a 16-bit bit field representing a Property ID.
|
||||
* 4. This format may be used for Property Values not longer than 128 octets and for
|
||||
* any Property IDs. Property values longer than 128 octets are not supported by
|
||||
* the Sensor Status message.
|
||||
* 5. Exclude the generated 1-octet value, the 2-octet Sensor Property ID
|
||||
*
|
||||
* @param _len Length of Sensor Raw value.
|
||||
* @param _id Sensor Property ID.
|
||||
*
|
||||
* @return 3-octet MPID value for sensor data with Format B.
|
||||
*
|
||||
*/
|
||||
#define ESP_BLE_MESH_SENSOR_DATA_FORMAT_B_MPID(_len, _id) \
|
||||
(((_id) << 8) | (((_len) & BIT_MASK(7)) << 1) | ESP_BLE_MESH_SENSOR_DATA_FORMAT_B)
|
||||
|
||||
/** This enum value is value of Sensor Sampling Function */
|
||||
enum esp_ble_mesh_sensor_sample_func {
|
||||
ESP_BLE_MESH_SAMPLE_FUNC_UNSPECIFIED,
|
||||
|
||||
@@ -290,7 +290,8 @@ static void send_sensor_settings_status(struct bt_mesh_model *model,
|
||||
for (i = 0; i < srv->state_count; i++) {
|
||||
state = &srv->states[i];
|
||||
if (state->sensor_property_id != INVALID_SENSOR_PROPERTY_ID &&
|
||||
state->sensor_property_id == prop_id) {
|
||||
state->sensor_property_id == prop_id &&
|
||||
state->setting_count && state->settings) {
|
||||
for (j = 0; j < state->setting_count; j++) {
|
||||
item = &state->settings[j];
|
||||
if (item->property_id != INVALID_SENSOR_SETTING_PROPERTY_ID) {
|
||||
@@ -325,7 +326,8 @@ static struct sensor_setting *find_sensor_setting(struct bt_mesh_model *model,
|
||||
for (i = 0; i < srv->state_count; i++) {
|
||||
state = &srv->states[i];
|
||||
if (state->sensor_property_id != INVALID_SENSOR_PROPERTY_ID &&
|
||||
state->sensor_property_id == prop_id) {
|
||||
state->sensor_property_id == prop_id &&
|
||||
state->setting_count && state->settings) {
|
||||
for (j = 0; j < state->setting_count; j++) {
|
||||
item = &state->settings[j];
|
||||
if (item->property_id != INVALID_SENSOR_SETTING_PROPERTY_ID &&
|
||||
@@ -422,14 +424,22 @@ static void send_sensor_column_status(struct bt_mesh_model *model,
|
||||
state = &srv->states[i];
|
||||
if (state->sensor_property_id != INVALID_SENSOR_PROPERTY_ID &&
|
||||
state->sensor_property_id == prop_id) {
|
||||
length = SENSOR_PROPERTY_ID_LEN + state->series_column.raw_value_x->len;
|
||||
length = SENSOR_PROPERTY_ID_LEN;
|
||||
if (state->series_column.raw_value_x) {
|
||||
length += state->series_column.raw_value_x->len;
|
||||
}
|
||||
/**
|
||||
* TODO: column width & raw value y in Sensor Column Status are optional,
|
||||
* here we need to add some conditions to decide whether put these two
|
||||
* in the status message.
|
||||
*/
|
||||
if (optional) {
|
||||
length += state->series_column.column_width->len + state->series_column.raw_value_y->len;
|
||||
if (state->series_column.column_width) {
|
||||
length += state->series_column.column_width->len;
|
||||
}
|
||||
if (state->series_column.raw_value_y) {
|
||||
length += state->series_column.raw_value_y->len;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -453,13 +463,19 @@ static void send_sensor_column_status(struct bt_mesh_model *model,
|
||||
bt_mesh_model_msg_init(msg, BLE_MESH_MODEL_OP_SENSOR_COLUMN_STATUS);
|
||||
net_buf_simple_add_le16(msg, prop_id);
|
||||
if (i != srv->state_count) {
|
||||
net_buf_simple_add_mem(msg, state->series_column.raw_value_x->data,
|
||||
state->series_column.raw_value_x->len);
|
||||
if (state->series_column.raw_value_x) {
|
||||
net_buf_simple_add_mem(msg, state->series_column.raw_value_x->data,
|
||||
state->series_column.raw_value_x->len);
|
||||
}
|
||||
if (optional) {
|
||||
net_buf_simple_add_mem(msg, state->series_column.column_width->data,
|
||||
state->series_column.column_width->len);
|
||||
net_buf_simple_add_mem(msg, state->series_column.raw_value_y->data,
|
||||
state->series_column.raw_value_y->len);
|
||||
if (state->series_column.column_width) {
|
||||
net_buf_simple_add_mem(msg, state->series_column.column_width->data,
|
||||
state->series_column.column_width->len);
|
||||
}
|
||||
if (state->series_column.raw_value_y) {
|
||||
net_buf_simple_add_mem(msg, state->series_column.raw_value_y->data,
|
||||
state->series_column.raw_value_y->len);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -489,9 +505,15 @@ static void send_sensor_series_status(struct bt_mesh_model *model,
|
||||
* decide whether put these three in the status message.
|
||||
*/
|
||||
if (optional) {
|
||||
length += state->series_column.raw_value_x->len +
|
||||
state->series_column.column_width->len +
|
||||
state->series_column.raw_value_y->len;
|
||||
if (state->series_column.raw_value_x) {
|
||||
length += state->series_column.raw_value_x->len;
|
||||
}
|
||||
if (state->series_column.column_width) {
|
||||
length += state->series_column.column_width->len;
|
||||
}
|
||||
if (state->series_column.raw_value_y) {
|
||||
length += state->series_column.raw_value_y->len;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -517,12 +539,18 @@ static void send_sensor_series_status(struct bt_mesh_model *model,
|
||||
net_buf_simple_add_le16(msg, prop_id);
|
||||
if (i != srv->state_count) {
|
||||
if (optional) {
|
||||
net_buf_simple_add_mem(msg, state->series_column.raw_value_x->data,
|
||||
state->series_column.raw_value_x->len);
|
||||
net_buf_simple_add_mem(msg, state->series_column.column_width->data,
|
||||
state->series_column.column_width->len);
|
||||
net_buf_simple_add_mem(msg, state->series_column.raw_value_y->data,
|
||||
state->series_column.raw_value_y->len);
|
||||
if (state->series_column.raw_value_x) {
|
||||
net_buf_simple_add_mem(msg, state->series_column.raw_value_x->data,
|
||||
state->series_column.raw_value_x->len);
|
||||
}
|
||||
if (state->series_column.column_width) {
|
||||
net_buf_simple_add_mem(msg, state->series_column.column_width->data,
|
||||
state->series_column.column_width->len);
|
||||
}
|
||||
if (state->series_column.raw_value_y) {
|
||||
net_buf_simple_add_mem(msg, state->series_column.raw_value_y->data,
|
||||
state->series_column.raw_value_y->len);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -972,17 +1000,29 @@ static int check_sensor_server_init(struct bt_mesh_sensor_state *state_start,
|
||||
BT_ERR("%s, Invalid Sensor Property ID 0x%04x", __func__, state->sensor_property_id);
|
||||
return -EINVAL;
|
||||
}
|
||||
if (state->setting_count == 0U || state->settings == NULL) {
|
||||
BT_ERR("%s, Invalid Sensor Setting state", __func__);
|
||||
return -EINVAL;
|
||||
}
|
||||
for (j = 0; j < state->setting_count; j++) {
|
||||
setting = &state->settings[j];
|
||||
if (setting->property_id == INVALID_SENSOR_SETTING_PROPERTY_ID || setting->raw == NULL) {
|
||||
BT_ERR("%s, Invalid Sensor Setting state internal parameter", __func__);
|
||||
/* Check if the same Sensor Property ID exists */
|
||||
for (int k = i + 1; k < state_count; k++) {
|
||||
if (state->sensor_property_id == state_start[k].sensor_property_id) {
|
||||
BT_ERR("%s, Same Sensor Property ID 0x%04x exists", __func__, state->sensor_property_id);
|
||||
return -EINVAL;
|
||||
}
|
||||
}
|
||||
if (state->setting_count && state->settings) {
|
||||
for (j = 0; j < state->setting_count; j++) {
|
||||
setting = &state->settings[j];
|
||||
if (setting->property_id == INVALID_SENSOR_SETTING_PROPERTY_ID || setting->raw == NULL) {
|
||||
BT_ERR("%s, Invalid Sensor Setting state", __func__);
|
||||
return -EINVAL;
|
||||
}
|
||||
/* Check if the same Sensor Setting Property ID exists */
|
||||
for (int k = j + 1; k < state->setting_count; k++) {
|
||||
if (setting->property_id == state->settings[k].property_id) {
|
||||
BT_ERR("%s, Same Sensor Setting Property ID 0x%04x exists", __func__, setting->property_id);
|
||||
return -EINVAL;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (state->cadence) {
|
||||
if (state->cadence->trigger_delta_down == NULL ||
|
||||
state->cadence->trigger_delta_up == NULL ||
|
||||
@@ -996,12 +1036,6 @@ static int check_sensor_server_init(struct bt_mesh_sensor_state *state_start,
|
||||
BT_ERR("%s, Invalid Sensor Data state", __func__);
|
||||
return -EINVAL;
|
||||
}
|
||||
if (state->series_column.raw_value_x == NULL ||
|
||||
state->series_column.column_width == NULL ||
|
||||
state->series_column.raw_value_y == NULL) {
|
||||
BT_ERR("%s, Invalid Sensor Series column state", __func__);
|
||||
return -EINVAL;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
@@ -1018,11 +1052,10 @@ static int sensor_server_init(struct bt_mesh_model *model)
|
||||
case BLE_MESH_MODEL_ID_SENSOR_SRV: {
|
||||
struct bt_mesh_sensor_srv *srv = model->user_data;
|
||||
if (srv->state_count == 0U || srv->states == NULL) {
|
||||
BT_ERR("%s, Invalid Sensor state parameter, model_id 0x%04x", __func__, model->id);
|
||||
BT_ERR("%s, Invalid Sensor state, model_id 0x%04x", __func__, model->id);
|
||||
return -EINVAL;
|
||||
}
|
||||
if (check_sensor_server_init(srv->states, srv->state_count)) {
|
||||
BT_ERR("%s, Invalid Sensor Server init value", __func__);
|
||||
return -EINVAL;
|
||||
}
|
||||
srv->model = model;
|
||||
@@ -1031,11 +1064,10 @@ static int sensor_server_init(struct bt_mesh_model *model)
|
||||
case BLE_MESH_MODEL_ID_SENSOR_SETUP_SRV: {
|
||||
struct bt_mesh_sensor_setup_srv *srv = model->user_data;
|
||||
if (srv->state_count == 0U || srv->states == NULL) {
|
||||
BT_ERR("%s, Invalid parameter, model_id 0x%04x", __func__, model->id);
|
||||
BT_ERR("%s, Invalid Sensor state, model_id 0x%04x", __func__, model->id);
|
||||
return -EINVAL;
|
||||
}
|
||||
if (check_sensor_server_init(srv->states, srv->state_count)) {
|
||||
BT_ERR("%s, Invalid Sensor Setup Server init value", __func__);
|
||||
return -EINVAL;
|
||||
}
|
||||
srv->model = model;
|
||||
|
||||
Reference in New Issue
Block a user