diff --git a/doc/nrf/libraries/bluetooth_services/mesh/light_ctrl_cli.rst b/doc/nrf/libraries/bluetooth_services/mesh/light_ctrl_cli.rst index eb1d3c7889ce..e8f110438bfd 100644 --- a/doc/nrf/libraries/bluetooth_services/mesh/light_ctrl_cli.rst +++ b/doc/nrf/libraries/bluetooth_services/mesh/light_ctrl_cli.rst @@ -7,13 +7,6 @@ Light Lightness Control Client :local: :depth: 2 -.. note:: - This model interacts with the new sensor API introduced as of |NCS| v2.6.0. - As a consequence, parts of the model API have been changed as well. - The old API is deprecated, but still available by enabling the Kconfig option :kconfig:option:`CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE`. - The Kconfig option is enabled by default in the deprecation period. - See the documentation for |NCS| versions prior to v2.6.0 for documentation about the old sensor API. - The Light Lightness Control (LC) Client configures and interacts with the :ref:`bt_mesh_light_ctrl_srv_readme`. The Light LC Client creates a single model instance in the mesh composition data, and it can send messages to both the Light LC Server and the Light LC Setup Server, as long as it has the right application keys. diff --git a/doc/nrf/libraries/bluetooth_services/mesh/light_ctrl_srv.rst b/doc/nrf/libraries/bluetooth_services/mesh/light_ctrl_srv.rst index 329a7f94d1b1..404d16aa4a03 100644 --- a/doc/nrf/libraries/bluetooth_services/mesh/light_ctrl_srv.rst +++ b/doc/nrf/libraries/bluetooth_services/mesh/light_ctrl_srv.rst @@ -7,13 +7,6 @@ Light Lightness Control Server :local: :depth: 2 -.. note:: - This model interacts with the new sensor API introduced as of |NCS| v2.6.0. - As a consequence, parts of the model API have been changed as well. - The old API is deprecated, but still available by enabling the Kconfig option :kconfig:option:`CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE`. - The Kconfig option is enabled by default in the deprecation period. - See the documentation for |NCS| versions prior to v2.6.0 for documentation about the old sensor API. - The Light Lightness Control (LC) Server controls a single :ref:`bt_mesh_lightness_srv_readme` instance on the same device with a state machine. The state machine defines common behavior for a light fixture through three states, each with its own timing parameters and light levels. diff --git a/doc/nrf/libraries/bluetooth_services/mesh/sensor.rst b/doc/nrf/libraries/bluetooth_services/mesh/sensor.rst index d8331eda6708..c47881e11c1d 100644 --- a/doc/nrf/libraries/bluetooth_services/mesh/sensor.rst +++ b/doc/nrf/libraries/bluetooth_services/mesh/sensor.rst @@ -7,12 +7,6 @@ Bluetooth Mesh sensors :local: :depth: 2 -.. note:: - A new sensor API is introduced as of |NCS| v2.6.0. - The old API is deprecated, but still available by enabling the Kconfig option :kconfig:option:`CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE`. - The Kconfig option is enabled by default in the deprecation period. - See the documentation for |NCS| versions prior to v2.6.0 for documentation about the old sensor API. - The Bluetooth® Mesh specification provides a common scheme for representing all sensors. A single Bluetooth Mesh sensor instance represents a single physical sensor, and a mesh device may present any number of sensors to the network through a Sensor Server model. Sensors represent their measurements as a list of sensor channels, as described by the sensor's assigned type. diff --git a/doc/nrf/libraries/bluetooth_services/mesh/sensor_cli.rst b/doc/nrf/libraries/bluetooth_services/mesh/sensor_cli.rst index c1259a9ba01b..14c166e3c0fa 100644 --- a/doc/nrf/libraries/bluetooth_services/mesh/sensor_cli.rst +++ b/doc/nrf/libraries/bluetooth_services/mesh/sensor_cli.rst @@ -7,12 +7,6 @@ Sensor Client :local: :depth: 2 -.. note:: - A new sensor API is introduced as of |NCS| v2.6.0. - The old API is deprecated, but still available by enabling the Kconfig option :kconfig:option:`CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE`. - The Kconfig option is enabled by default in the deprecation period. - See the documentation for |NCS| versions prior to v2.6.0 for documentation about the old sensor API. - The Sensor Client model reads and configures the sensors exposed by :ref:`bt_mesh_sensor_srv_readme` models. Unlike the Sensor Server model, the Sensor Client only creates a single model instance in the mesh composition data. diff --git a/doc/nrf/libraries/bluetooth_services/mesh/sensor_srv.rst b/doc/nrf/libraries/bluetooth_services/mesh/sensor_srv.rst index 51b07dcf2641..2f9b4b55173a 100644 --- a/doc/nrf/libraries/bluetooth_services/mesh/sensor_srv.rst +++ b/doc/nrf/libraries/bluetooth_services/mesh/sensor_srv.rst @@ -7,12 +7,6 @@ Sensor Server :local: :depth: 2 -.. note:: - A new sensor API is introduced as of |NCS| v2.6.0. - The old API is deprecated, but still available by enabling the Kconfig option :kconfig:option:`CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE`. - The Kconfig option is enabled by default in the deprecation period. - See the documentation for |NCS| versions prior to v2.6.0 for documentation about the old sensor API. - The Sensor Server model holds a list of sensors, and exposes them to the mesh network. There may be multiple Sensor Server models on a single mesh node, and each model may hold up to 47 sensors. diff --git a/doc/nrf/releases_and_maturity/releases/release-notes-changelog.rst b/doc/nrf/releases_and_maturity/releases/release-notes-changelog.rst index f86e07875484..6ce4107af6da 100644 --- a/doc/nrf/releases_and_maturity/releases/release-notes-changelog.rst +++ b/doc/nrf/releases_and_maturity/releases/release-notes-changelog.rst @@ -145,6 +145,9 @@ Bluetooth Mesh * Added metadata as optional parameter for models Light Lightness Server, Light HSL Server, Light CTL Temperature Server, Sensor Server, and Time Server. To use the metadata, enable the :kconfig:option:`CONFIG_BT_MESH_LARGE_COMP_DATA_SRV` Kconfig option. +* Removed the ``BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE`` Kconfig option, deprecated in the |NCS| v2.6.0, as the old APIs, based on the :c:struct:`sensor_value` type, are removed. + Applications using the old APIs must be updated, as described in the :ref:`v2.6.0 migration guide `. + DECT NR+ -------- diff --git a/include/bluetooth/mesh/light_ctrl.h b/include/bluetooth/mesh/light_ctrl.h index 2eaf97e1f8b6..cb6525c9b6f5 100644 --- a/include/bluetooth/mesh/light_ctrl.h +++ b/include/bluetooth/mesh/light_ctrl.h @@ -264,21 +264,12 @@ bt_mesh_lc_prop_format_get(uint16_t id) .accuracy = CONFIG_BT_MESH_LIGHT_CTRL_SRV_REG_ACCURACY, \ } -#ifdef CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE -#define BT_MESH_LIGHT_CTRL_SRV_LUX_INIT \ - .lux = { \ - { CONFIG_BT_MESH_LIGHT_CTRL_SRV_REG_LUX_STANDBY }, \ - { CONFIG_BT_MESH_LIGHT_CTRL_SRV_REG_LUX_ON }, \ - { CONFIG_BT_MESH_LIGHT_CTRL_SRV_REG_LUX_PROLONG } \ - } -#else #define BT_MESH_LIGHT_CTRL_SRV_LUX_INIT \ .centilux = { \ CONFIG_BT_MESH_LIGHT_CTRL_SRV_REG_LUX_STANDBY * 100, \ CONFIG_BT_MESH_LIGHT_CTRL_SRV_REG_LUX_ON * 100, \ CONFIG_BT_MESH_LIGHT_CTRL_SRV_REG_LUX_PROLONG * 100 \ } -#endif #else #define BT_MESH_LIGHT_CTRL_SRV_LUX_INIT #endif diff --git a/include/bluetooth/mesh/light_ctrl_cli.h b/include/bluetooth/mesh/light_ctrl_cli.h index c605c530d479..e35b607cf007 100644 --- a/include/bluetooth/mesh/light_ctrl_cli.h +++ b/include/bluetooth/mesh/light_ctrl_cli.h @@ -93,7 +93,6 @@ struct bt_mesh_light_ctrl_cli_handlers { struct bt_mesh_msg_ctx *ctx, const struct bt_mesh_onoff_status *status); -#if !defined(CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE) || defined(__DOXYGEN__) /** @brief Light LC Property status handler. * * The Light Lightness Control Server's properties are configuration @@ -109,23 +108,6 @@ struct bt_mesh_light_ctrl_cli_handlers { struct bt_mesh_msg_ctx *ctx, enum bt_mesh_light_ctrl_prop id, const struct bt_mesh_sensor_value *value); -#else - /** @brief Light LC Property status handler. - * - * The Light Lightness Control Server's properties are configuration - * parameters for its behavior. All properties are represented as a - * single sensor value channel. - * - * @param[in] cli Client that received the message. - * @param[in] ctx Context of the message. - * @param[in] id ID of the property. - * @param[in] value Value of the property. - */ - void (*prop)(struct bt_mesh_light_ctrl_cli *cli, - struct bt_mesh_msg_ctx *ctx, - enum bt_mesh_light_ctrl_prop id, - const struct sensor_value *value); -#endif /** @brief Light LC Regulator Coefficient status handler. * @@ -397,7 +379,6 @@ int bt_mesh_light_ctrl_cli_light_onoff_set_unack( struct bt_mesh_light_ctrl_cli *cli, struct bt_mesh_msg_ctx *ctx, const struct bt_mesh_onoff_set *set); -#if !defined(CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE) || defined(__DOXYGEN__) /** @brief Get a Light Lightness Control Server property value. * * Properties are the configuration parameters for the Light Lightness Control @@ -480,89 +461,6 @@ int bt_mesh_light_ctrl_cli_prop_set_unack( struct bt_mesh_msg_ctx *ctx, enum bt_mesh_light_ctrl_prop id, const struct bt_mesh_sensor_value *val); -#else -/** @brief Get a Light Lightness Control Server property value. - * - * Properties are the configuration parameters for the Light Lightness Control - * Server. Each property value is represented as a single sensor channel. - * - * This call is blocking if the @c rsp buffer is non-NULL. Otherwise, this - * function will return, and the response will be passed to the - * @ref bt_mesh_light_ctrl_cli_handlers::prop callback. - * - * @param[in] cli Client model to send on. - * @param[in] ctx Message context, or NULL to use the configured publish - * parameters. - * @param[in] id Light Lightness Control Server property to get. - * @param[out] rsp Property value response buffer, or NULL to keep from - * blocking. - * - * @retval 0 Successfully sent the message and populated the @c - * rsp buffer. - * @retval -EALREADY A blocking request is already in progress. - * @retval -EADDRNOTAVAIL A message context was not provided and publishing is - * not configured. - * @retval -EAGAIN The device has not been provisioned. - * @retval -ETIMEDOUT The request timed out without a response. - */ -int bt_mesh_light_ctrl_cli_prop_get(struct bt_mesh_light_ctrl_cli *cli, - struct bt_mesh_msg_ctx *ctx, - enum bt_mesh_light_ctrl_prop id, - struct sensor_value *rsp); - -/** @brief Set a Light Lightness Control Server property value. - * - * Properties are the configuration parameters for the Light Lightness Control - * Server. Each property value is represented as a single sensor channel. - * - * This call is blocking if the @c rsp buffer is non-NULL. Otherwise, this - * function will return, and the response will be passed to the - * @ref bt_mesh_light_ctrl_cli_handlers::prop callback. - * - * @param[in] cli Client model to send on. - * @param[in] ctx Message context, or NULL to use the configured publish - * parameters. - * @param[in] id Light Lightness Control Server property to set. - * @param[in] val New property value. - * @param[out] rsp Property value response buffer, or NULL to keep from - * blocking. - * - * @retval 0 Successfully sent the message and populated the @c - * rsp buffer. - * @retval -EALREADY A blocking request is already in progress. - * @retval -EADDRNOTAVAIL A message context was not provided and publishing is - * not configured. - * @retval -EAGAIN The device has not been provisioned. - * @retval -ETIMEDOUT The request timed out without a response. - */ -int bt_mesh_light_ctrl_cli_prop_set(struct bt_mesh_light_ctrl_cli *cli, - struct bt_mesh_msg_ctx *ctx, - enum bt_mesh_light_ctrl_prop id, - const struct sensor_value *val, - struct sensor_value *rsp); - -/** @brief Set a Light Lightness Control Server property value without - * requesting a response. - * - * Properties are the configuration parameters for the Light Lightness Control - * Server. Each property value is represented as a single sensor channel. - * - * @param[in] cli Client model to send on. - * @param[in] ctx Message context, or NULL to use the configured publish - * parameters. - * @param[in] id Light Lightness Control Server property to set. - * @param[in] val New property value. - * - * @retval 0 Successfully sent the message. - * @retval -EADDRNOTAVAIL A message context was not provided and publishing is - * not configured. - * @retval -EAGAIN The device has not been provisioned. - */ -int bt_mesh_light_ctrl_cli_prop_set_unack(struct bt_mesh_light_ctrl_cli *cli, - struct bt_mesh_msg_ctx *ctx, - enum bt_mesh_light_ctrl_prop id, - const struct sensor_value *val); -#endif /** @brief Get a Light Lightness Control Server Regulator Coefficient value. * diff --git a/include/bluetooth/mesh/light_ctrl_srv.h b/include/bluetooth/mesh/light_ctrl_srv.h index 0d62efd9ac72..b36987427b93 100644 --- a/include/bluetooth/mesh/light_ctrl_srv.h +++ b/include/bluetooth/mesh/light_ctrl_srv.h @@ -136,13 +136,8 @@ struct bt_mesh_light_ctrl_srv_cfg { /** State-wise light levels. */ uint16_t light[LIGHT_CTRL_STATE_COUNT]; #if CONFIG_BT_MESH_LIGHT_CTRL_SRV_REG -#ifdef CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE - /** Target illuminance values. */ - struct sensor_value lux[LIGHT_CTRL_STATE_COUNT]; -#else /** Target illuminance values, in centilux */ uint32_t centilux[LIGHT_CTRL_STATE_COUNT]; -#endif /* CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE */ #endif /* CONFIG_BT_MESH_LIGHT_CTRL_SRV_REG */ }; @@ -159,13 +154,8 @@ struct bt_mesh_light_ctrl_srv { struct { /** Initial light level */ uint16_t initial_light; -#ifdef CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE - /** Initial illumination level */ - struct sensor_value initial_lux; -#else /** Initial illumination level, in centilux */ uint32_t initial_centilux; -#endif /** Fade duration */ uint32_t duration; } fade; diff --git a/include/bluetooth/mesh/sensor.h b/include/bluetooth/mesh/sensor.h index a6cdba72abaa..f15f50398e04 100644 --- a/include/bluetooth/mesh/sensor.h +++ b/include/bluetooth/mesh/sensor.h @@ -36,7 +36,6 @@ extern "C" { /** String length for representing a single sensor channel. */ #define BT_MESH_SENSOR_CH_STR_LEN 23 -#if !defined(CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE) || defined(__DOXYGEN__) /** @def BT_MESH_SENSOR_VALUE_IN_RANGE * * @brief Returns whether or not encoded sensor value _value is in the range @@ -49,15 +48,6 @@ extern "C" { #define BT_MESH_SENSOR_VALUE_IN_RANGE(_value, _start, _end) ( \ (_value)->format->cb->compare((_value), (_start)) >= 0 && \ (_value)->format->cb->compare((_end), (_value)) >= 0) -#else -#define BT_MESH_SENSOR_VALUE_IN_RANGE(_value, _start, _end) ( \ - ((_value)->val1 > (_start)->val1 || \ - ((_value)->val1 == (_start)->val1 && \ - (_value)->val2 >= (_start)->val2)) && \ - ((_value)->val1 < (_end)->val1 || \ - ((_value)->val1 == (_end)->val1 && \ - (_value)->val2 <= (_end)->val2))) -#endif /** Sensor sampling type. * @@ -142,8 +132,6 @@ struct bt_mesh_sensor_type { struct bt_mesh_sensor; struct bt_mesh_sensor_srv; -#if !defined(CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE) || defined(__DOXYGEN__) - /** Status of conversion from @ref bt_mesh_sensor_value. */ enum bt_mesh_sensor_value_status { /** The encoded sensor value represents a number. */ @@ -867,369 +855,6 @@ int bt_mesh_sensor_ch_to_str(const struct bt_mesh_sensor_value *ch, char *str, */ const char *bt_mesh_sensor_ch_str(const struct bt_mesh_sensor_value *ch); -#else /* defined(CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE) */ -/** Sensor descriptor representing various static metadata for the sensor. - */ -struct bt_mesh_sensor_descriptor { - /** Sensor measurement tolerance specification. */ - struct { - /** Maximum positive measurement error (in percent). - * A tolerance of 0 should be interpreted as "unspecified". - */ - struct sensor_value positive; - /** Maximum negative measurement error (in percent). - * A tolerance of 0 should be interpreted as "unspecified". - */ - struct sensor_value negative; - } tolerance; - /** Sampling type for the sensor data. */ - enum bt_mesh_sensor_sampling sampling_type; - /** Measurement period for the samples, if applicable. */ - uint64_t period; - /** Update interval for the samples, if applicable. */ - uint64_t update_interval; -}; - -/** Delta threshold type. */ -enum bt_mesh_sensor_delta { - /** Value based delta threshold. - * The delta threshold values are represented as absolute value - * changes. - */ - BT_MESH_SENSOR_DELTA_VALUE, - /** Percent based delta threshold. - * The delta threshold values are represented as percentages of their - * old value (resolution: 0.01 %). - */ - BT_MESH_SENSOR_DELTA_PERCENT, -}; - -/** Sensor thresholds for publishing. */ -struct bt_mesh_sensor_threshold { - /** Delta threshold values. - * - * Denotes the minimal sensor value change that should cause the sensor - * to publish its value. - */ - struct { - /** Type of delta threshold */ - enum bt_mesh_sensor_delta type; - /** Minimal delta for a positive change. */ - struct sensor_value up; - /** Minimal delta for a negative change. */ - struct sensor_value down; - } delta; - /** Range based threshold values. - * - * Denotes the value range in which the sensor should be in fast - * cadence mode. - */ - struct { - /** Cadence when the sensor value is inside the range. - * - * If the cadence is fast when the value is inside the range, - * it's normal when it's outside the range. If the cadence is - * normal when the value is inside the range, it's fast outside - * the range. - */ - enum bt_mesh_sensor_cadence cadence; - /** Lower boundary for the range based sensor cadence threshold. - */ - struct sensor_value low; - /** Upper boundary for the range based sensor cadence threshold. - */ - struct sensor_value high; - } range; -}; - -/** Sensor channel value format. */ -struct bt_mesh_sensor_format { - /** @brief Sensor channel value encode function. - * - * @param[in] format Pointer to the format structure. - * @param[in] val Sensor channel value to encode. - * @param[out] buf Buffer to encode the value into. - * - * @return 0 on success, or (negative) error code otherwise. - */ - int (*const encode)(const struct bt_mesh_sensor_format *format, - const struct sensor_value *val, - struct net_buf_simple *buf); - - /** @brief Sensor channel value decode function. - * - * @param[in] format Pointer to the format structure. - * @param[in] buf Buffer to decode the value from. - * @param[out] val Resulting sensor channel value. - * - * @return 0 on success, or (negative) error code otherwise. - */ - int (*const decode)(const struct bt_mesh_sensor_format *format, - struct net_buf_simple *buf, - struct sensor_value *val); - - /** User data pointer. Used internally by the sensor types. */ - void *user_data; - /** Size of the encoded data in bytes. */ - size_t size; - -#ifdef CONFIG_BT_MESH_SENSOR_LABELS - /** Pointer to the unit associated with this format. */ - const struct bt_mesh_sensor_unit *unit; -#endif -}; - -/** Single sensor setting. */ -struct bt_mesh_sensor_setting { - /** Sensor type of this setting. */ - const struct bt_mesh_sensor_type *type; - - /** @brief Getter for this sensor setting. - * - * @note This handler is mandatory. - * - * @param[in] srv Sensor server instance associated with this - * setting. - * @param[in] sensor Sensor this setting belongs to. - * @param[in] setting Pointer to this setting structure. - * @param[in] ctx Context parameters for the packet this call - * originated from, or NULL if this call wasn't - * triggered by a packet. - * @param[out] rsp Response buffer for the setting value. Points to - * an array with the number of channels specified - * by the setting sensor type. All channels must be - * filled. - */ - void (*get)(struct bt_mesh_sensor_srv *srv, - struct bt_mesh_sensor *sensor, - const struct bt_mesh_sensor_setting *setting, - struct bt_mesh_msg_ctx *ctx, - struct sensor_value *rsp); - - /** @brief Setter for this sensor setting. - * - * Should only be specified for writable sensor settings. - * - * @param[in] srv Sensor server instance associated with this - * setting. - * @param[in] sensor Sensor this setting belongs to. - * @param[in] setting Pointer to this setting structure. - * @param[in] ctx Context parameters for the packet this call - * originated from, or NULL if this call wasn't - * triggered by a packet. - * @param[in] value New setting value. Contains the number of - * channels specified by the setting sensor type. - * - * @return 0 on success, or (negative) error code otherwise. - */ - int (*set)(struct bt_mesh_sensor_srv *srv, - struct bt_mesh_sensor *sensor, - const struct bt_mesh_sensor_setting *setting, - struct bt_mesh_msg_ctx *ctx, - const struct sensor_value *value); -}; - -/** Single sensor series data column. - * - * The series data columns represent a range for specific measurement values, - * inside which a set of sensor measurements were made. The range is - * interpreted as a half-open interval (i.e. start <= value < end). - * - * @note Contrary to the Bluetooth Mesh specification, the column has an end - * value instead of a width, to match the conventional property format. - * This reduces implementation complexity for sensor series values that - * include the start and end (or min and max) of the measurement range, - * as the value of the column can be copied directly into the - * corresponding channels. - */ -struct bt_mesh_sensor_column { - /** Start of the column (inclusive). */ - struct sensor_value start; - /** End of the column (exclusive). */ - struct sensor_value end; -}; - -/** Sensor series specification. */ -struct bt_mesh_sensor_series { - /** Pointer to the list of columns. - * - * The columns may overlap, but the start value of each column must be - * unique. The list of columns do not have to cover the entire valid - * range, and values that don't fit in any of the columns should be - * ignored. If columns overlap, samples must be present in all columns - * they fall into. The columns may come in any order. - * - * This list is not used for sensor types with one or two channels. - */ - const struct bt_mesh_sensor_column *columns; - - /** Number of columns. */ - uint32_t column_count; - - /** @brief Getter for the series values. - * - * Should return the historical data for the latest sensor readings in - * the given column. - * - * @param[in] srv Sensor server associated with sensor instance. - * @param[in] sensor Sensor pointer. - * @param[in] ctx Message context pointer, or NULL if this call - * didn't originate from a mesh message. - * @param[in] column_index The index of the requested sensor column. - * Index into the @c columns array for sensors - * with more than two channels. - * @param[out] value Sensor value response buffer. Holds the number - * of channels indicated by the sensor type. All - * channels must be filled. - * - * @return 0 on success, or (negative) error code otherwise. - */ - int (*get)(struct bt_mesh_sensor_srv *srv, - struct bt_mesh_sensor *sensor, - struct bt_mesh_msg_ctx *ctx, - uint32_t column_index, - struct sensor_value *value); -}; - -/** Sensor instance. */ -struct bt_mesh_sensor { - /** Sensor type. - * - * Must be one of the specification defined types listed in @ref - * bt_mesh_sensor_types. - */ - const struct bt_mesh_sensor_type *type; - /** Optional sensor descriptor. */ - const struct bt_mesh_sensor_descriptor *descriptor; - /** Sensor settings access specification. */ - const struct { - /** Static array of sensor settings */ - const struct bt_mesh_sensor_setting *list; - /** Number of sensor settings. */ - size_t count; - } settings; - - /** Sensor series specification. - * - * Only sensors who have a non-zero column-count and a defined - * series getter will accept series messages. Sensors with more than - * two channels also require a non-empty list of columns. - */ - const struct bt_mesh_sensor_series series; - - /** @brief Getter function for the sensor value. - * - * @param[in] srv Sensor server associated with sensor instance. - * @param[in] sensor Sensor instance. - * @param[in] ctx Message context, or NULL if the call wasn't - * triggered by a mesh message. - * @param[out] rsp Value response buffer. Fits the number of - * channels specified by the sensor type. All - * channels must be filled. - * - * @return 0 on success, or (negative) error code otherwise. - */ - int (*const get)(struct bt_mesh_sensor_srv *srv, - struct bt_mesh_sensor *sensor, - struct bt_mesh_msg_ctx *ctx, - struct sensor_value *rsp); - - /* Internal state, overwritten on init. Should only be written to by - * internal modules. - */ - struct { - /** Sensor threshold specification. */ - struct bt_mesh_sensor_threshold threshold; - - /** Linked list node. */ - sys_snode_t node; - - /** The previously published sensor value. */ - struct sensor_value prev; - - /** Sequence number of the previous publication. */ - uint16_t seq; - - /** Minimum possible interval for fast cadence value publishing. - * The value is represented as 2 to the power of N milliseconds. - * - * @see BT_MESH_SENSOR_INTERVAL_MAX - */ - uint8_t min_int; - - /** Fast period divisor used when publishing with fast cadence. - */ - uint8_t pub_div : 4; - - /** Flag indicating whether the sensor is in fast cadence mode. - */ - uint8_t fast_pub : 1; - - /** Flag indicating whether the sensor cadence state has been configured. */ - uint8_t configured : 1; - } state; -}; - -/** @brief Check if a value change breaks the delta threshold. - * - * Sensors should publish their value if the measured sample is outside the - * delta threshold compared to the previously published value. This function - * checks the threshold and the previously published value for this sensor, - * and returns whether the sensor should publish its value. - * - * @note Only single-channel sensors support cadence. Multi-channel sensors are - * always considered out of their threshold range, and will always return - * true from this function. Single-channel sensors that haven't been - * assigned a threshold will return true if the value is different. - * - * @param[in] sensor The sensor instance. - * @param[in] value Sensor value. - * - * @return true if the difference between the measurements exceeds the delta - * threshold, false otherwise. - */ -bool bt_mesh_sensor_delta_threshold(const struct bt_mesh_sensor *sensor, - const struct sensor_value *value); - -/** @brief Check whether a single channel sensor value lies within a column. - * - * @param[in] value Value to check. Only the first channel is considered. - * @param[in] col Sensor column. - * - * @return true if the value belongs in the column, false otherwise. - */ -bool bt_mesh_sensor_value_in_column(const struct sensor_value *value, - const struct bt_mesh_sensor_column *col); - -/** @brief Get a human readable representation of a single sensor channel. - * - * @param[in] ch Sensor channel to represent. - * @param[out] str String buffer to fill. Should be @ref - * BT_MESH_SENSOR_CH_STR_LEN bytes long. - * @param[in] len Length of @c str buffer. - * - * @return Number of bytes that should have been written if @c str is - * sufficiently large. - */ -static inline int bt_mesh_sensor_ch_to_str(const struct sensor_value *ch, - char *str, size_t len) -{ - return snprintk(str, len, "%s%u.%06u", - ((ch->val1 < 0 || ch->val2 < 0) ? "-" : ""), - abs(ch->val1), abs(ch->val2)); -} - -/** @brief Get a human readable representation of a single sensor channel. - * - * @note This function is not thread safe. - * - * @param[in] ch Sensor channel to represent. - * - * @return A string representing the sensor channel. - */ -const char *bt_mesh_sensor_ch_str(const struct sensor_value *ch); -#endif /* !defined(CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE) */ - /** @brief Get the sensor type associated with the given Device Property ID. * * Only known sensor types from @ref bt_mesh_sensor_types will be available. diff --git a/include/bluetooth/mesh/sensor_cli.h b/include/bluetooth/mesh/sensor_cli.h index 7fba5ece7f84..31bcca30ab3b 100644 --- a/include/bluetooth/mesh/sensor_cli.h +++ b/include/bluetooth/mesh/sensor_cli.h @@ -89,7 +89,6 @@ struct bt_mesh_sensor_cadence_status { struct bt_mesh_sensor_threshold threshold; }; -#if !defined(CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE) || defined(__DOXYGEN__) /** Sensor settings parameters. */ struct bt_mesh_sensor_setting_status { /** Setting type */ @@ -437,339 +436,6 @@ int bt_mesh_sensor_cli_series_entries_get( const union bt_mesh_sensor_column_key *range_start, const union bt_mesh_sensor_column_key *range_end, struct bt_mesh_sensor_series_entry *rsp, uint32_t *count); -#else /* defined(CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE) */ -/** Sensor settings parameters. */ -struct bt_mesh_sensor_setting_status { - /** Setting type */ - const struct bt_mesh_sensor_type *type; - /** Setting value. */ - struct sensor_value value[CONFIG_BT_MESH_SENSOR_CHANNELS_MAX]; - /** Whether the setting can be written to. */ - bool writable; -}; - -/** Sensor series entry **/ -struct bt_mesh_sensor_series_entry { - /** Sensor column descriptor. */ - struct bt_mesh_sensor_column column; - /** Sensor column value. */ - struct sensor_value value[CONFIG_BT_MESH_SENSOR_CHANNELS_MAX]; -}; - -/** Sensor info structure. */ -struct bt_mesh_sensor_info { - /** Sensor Device Property ID */ - uint16_t id; - /** Sensor descriptor. */ - struct bt_mesh_sensor_descriptor descriptor; -}; - -/** Sensor data structure. */ -struct bt_mesh_sensor_data { - /** Sensor type. */ - const struct bt_mesh_sensor_type *type; - /** Sensor value. */ - struct sensor_value value[CONFIG_BT_MESH_SENSOR_CHANNELS_MAX]; -}; - -/** Sensor client handler functions. */ -struct bt_mesh_sensor_cli_handlers { - /** @brief Sensor data callback. - * - * Called when the client receives sensor sample data, either as a - * result of calling @ref bt_mesh_sensor_cli_get, or as an unsolicited - * message. - * - * @param[in] cli Sensor client receiving the message. - * @param[in] ctx Message context. - * @param[in] sensor Sensor instance. - * @param[in] value The interpreted sensor data value as an array of - * channels. The length of the array matches the - * sensor channel count. - */ - void (*data)(struct bt_mesh_sensor_cli *cli, - struct bt_mesh_msg_ctx *ctx, - const struct bt_mesh_sensor_type *sensor, - const struct sensor_value *value); - - /** @brief Sensor description callback. - * - * Called when the client receives sensor descriptors, either as a - * result of calling @ref bt_mesh_sensor_cli_all_get or @ref - * bt_mesh_sensor_cli_desc_get, or as an unsolicited message. - * - * The sensor description does not reference the sensor type directly, - * to allow discovery of sensor types unknown to the client. To get the - * sensor type of a known sensor, call @ref bt_mesh_sensor_type_get. - * - * @param[in] cli Sensor client receiving the message. - * @param[in] ctx Message context. - * @param[in] sensor Sensor information for a single sensor. - */ - void (*sensor)(struct bt_mesh_sensor_cli *cli, - struct bt_mesh_msg_ctx *ctx, - const struct bt_mesh_sensor_info *sensor); - - /** @brief Sensor cadence callback. - * - * Called when the client receives the cadence of a sensor, either as a - * result of calling one of @ref bt_mesh_sensor_cli_cadence_get, - * @ref bt_mesh_sensor_cli_cadence_set or - * @ref bt_mesh_sensor_cli_cadence_set_unack, or as an unsolicited - * message. - * - * @param[in] cli Sensor client receiving the message. - * @param[in] ctx Message context. - * @param[in] sensor Sensor instance. - * @param[in] cadence Sensor cadence information. - */ - void (*cadence)(struct bt_mesh_sensor_cli *cli, - struct bt_mesh_msg_ctx *ctx, - const struct bt_mesh_sensor_type *sensor, - const struct bt_mesh_sensor_cadence_status *cadence); - - /** @brief Sensor settings list callback. - * - * Called when the client receives the full list of sensor settings, as - * a result of calling @ref bt_mesh_sensor_cli_settings_get or as an - * unsolicited message. - * - * @param[in] cli Sensor client receiving the message. - * @param[in] ctx Message context. - * @param[in] sensor Sensor instance. - * @param[in] ids Available sensor setting IDs. - * @param[in] count The number of sensor setting IDs. - */ - void (*settings)(struct bt_mesh_sensor_cli *cli, - struct bt_mesh_msg_ctx *ctx, - const struct bt_mesh_sensor_type *sensor, - const uint16_t *ids, uint32_t count); - - /** @brief Sensor setting status callback. - * - * Called when the client receives a sensor setting status, either as - * result of calling @ref bt_mesh_sensor_cli_setting_get, @ref - * bt_mesh_sensor_cli_setting_set, @ref - * bt_mesh_sensor_cli_setting_set_unack, or as an unsolicited message. - * - * @param[in] cli Sensor client receiving the message. - * @param[in] ctx Message context. - * @param[in] sensor Sensor instance. - * @param[in] setting Sensor setting information. - */ - void (*setting_status)( - struct bt_mesh_sensor_cli *cli, struct bt_mesh_msg_ctx *ctx, - const struct bt_mesh_sensor_type *sensor, - const struct bt_mesh_sensor_setting_status *setting); - - /** @brief Series entry callback. - * - * Called when the client receives series entries, either as result of - * calling one of @ref bt_mesh_sensor_cli_series_entry_get or @ref - * bt_mesh_sensor_cli_series_entries_get, or as a result of an - * unsolicited message. - * - * If the received series entry message contains several entries, this - * callback is called once per entry, with the @c index and @c count - * parameters indicating the progress. - * - * @note The @c index and @c count parameters does not necessarily - * match the total number of series entries of the sensor, as the - * callback may be the result of a filtered query. - * - * @param[in] cli Sensor client receiving the message. - * @param[in] ctx Message context. - * @param[in] sensor Sensor instance. - * @param[in] index Index of this entry in the list of entries - * received. - * @param[in] count Total number of entries received. - * @param[in] entry Single sensor series entry. - */ - void (*series_entry)(struct bt_mesh_sensor_cli *cli, - struct bt_mesh_msg_ctx *ctx, - const struct bt_mesh_sensor_type *sensor, - uint8_t index, uint8_t count, - const struct bt_mesh_sensor_series_entry *entry); - - /** @brief Unknown type callback. - * - * Called when the client receives a message with a sensor type it - * doesn't have type information for. - * - * @param[in] cli Sensor client receiving the message. - * @param[in] ctx Message context. - * @param[in] id Sensor type ID. - * @param[in] opcode The opcode of the message containing the unknown - * sensor type. - */ - void (*unknown_type)(struct bt_mesh_sensor_cli *cli, - struct bt_mesh_msg_ctx *ctx, uint16_t id, - uint32_t opcode); -}; - -/** @brief Set a setting value for a sensor. - * - * This call is blocking if the @c rsp buffer is non-NULL. Otherwise, this - * function will return, and the response will be passed to the - * bt_mesh_sensor_cli_handlers::setting_status callback. - * - * @param[in] cli Sensor client instance. - * @param[in] ctx Message context parameters, or NULL to use the - * configured publish parameters. - * @param[in] sensor Sensor instance present on the targeted sensor server. - * @param[in] setting Setting to change. - * @param[in] value New setting value. Must contain values for all channels - * described by @c setting. - * @param[out] rsp Sensor setting value response buffer, or NULL to keep - * from blocking. - * - * @retval 0 Successfully changed the setting. The @c rsp - * buffer has been filled. - * @retval -ENOENT The sensor doesn't have the given setting. - * @retval -EACCES The setting can't be written to. - * @retval -EALREADY A blocking request is already in progress. - * @retval -EADDRNOTAVAIL A message context was not provided and publishing is - * not configured. - * @retval -EAGAIN The device has not been provisioned. - * @retval -ETIMEDOUT The request timed out without a response. - */ -int bt_mesh_sensor_cli_setting_set( - struct bt_mesh_sensor_cli *cli, struct bt_mesh_msg_ctx *ctx, - const struct bt_mesh_sensor_type *sensor, - const struct bt_mesh_sensor_type *setting, - const struct sensor_value *value, - struct bt_mesh_sensor_setting_status *rsp); - -/** @brief Set a setting value for a sensor without requesting a response. - * - * @param[in] cli Sensor client instance. - * @param[in] ctx Message context parameters, or NULL to use the configured - * publish parameters. - * @param[in] sensor Sensor instance present on the targeted sensor server. - * @param[in] setting Setting to change. - * @param[in] value New setting value. Must contain values for all channels - * described by @c setting. - * - * @retval 0 Successfully changed the setting. - * @retval -EADDRNOTAVAIL A message context was not provided and publishing is - * not configured. - * @retval -EAGAIN The device has not been provisioned. - */ -int bt_mesh_sensor_cli_setting_set_unack( - struct bt_mesh_sensor_cli *cli, struct bt_mesh_msg_ctx *ctx, - const struct bt_mesh_sensor_type *sensor, - const struct bt_mesh_sensor_type *setting, - const struct sensor_value *value); - -/** @brief Read sensor data from a sensor instance. - * - * This call is blocking if the @c rsp buffer is non-NULL. Otherwise, this - * function will return, and the response will be passed to the - * bt_mesh_sensor_cli_handlers::data callback. - * - * @param[in] cli Sensor client instance. - * @param[in] ctx Message context parameters, or NULL to use the configured - * publish parameters. - * @param[in] sensor Sensor instance present on the targeted sensor server. - * @param[out] rsp Response value buffer, or NULL to keep from blocking. - * Must be able to fit all channels described by the sensor - * type. - * - * @retval 0 Successfully received the sensor data. - * @retval -ENODEV The sensor server doesn't have the given sensor. - * @retval -EADDRNOTAVAIL A message context was not provided and publishing is - * not configured. - * @retval -EAGAIN The device has not been provisioned. - * @retval -ETIMEDOUT The request timed out without a response. - */ -int bt_mesh_sensor_cli_get( - struct bt_mesh_sensor_cli *cli, struct bt_mesh_msg_ctx *ctx, - const struct bt_mesh_sensor_type *sensor, - struct sensor_value *rsp); - -/** @brief Read a single sensor series data entry. - * - * This call is blocking if the @c rsp buffer is non-NULL. Otherwise, this - * function will return, and the response will be passed to the - * bt_mesh_sensor_cli_handlers::series_entry callback as a list of - * sensor descriptors. - * - * @param[in] cli Sensor client instance. - * @param[in] ctx Message context parameters, or NULL to use the configured - * publish parameters. - * @param[in] sensor Sensor instance present on the targeted sensor server. - * @param[in] column Column to read. The start value must match the start - * value of a series column on the sensor. The end value is - * ignored. For sensors with only one or two channels, this - * sensor value represents the index of the column to get. - * @param[out] rsp Response value buffer, or NULL to keep from blocking. - * Must be able to fit all channels described by the sensor - * type. - * - * @retval 0 Successfully received the sensor data. - * @retval -ENODEV The sensor server doesn't have the given sensor. - * @retval -ENOENT The sensor doesn't have the given column. - * @retval -EADDRNOTAVAIL A message context was not provided and publishing is - * not configured. - * @retval -EAGAIN The device has not been provisioned. - * @retval -ETIMEDOUT The request timed out without a response. - */ -int bt_mesh_sensor_cli_series_entry_get( - struct bt_mesh_sensor_cli *cli, struct bt_mesh_msg_ctx *ctx, - const struct bt_mesh_sensor_type *sensor, - const struct bt_mesh_sensor_column *column, - struct bt_mesh_sensor_series_entry *rsp); - -/** @brief Get multiple sensor series data entries. - * - * Retrieves all data series columns starting within the given range - * (inclusive), or all data series entries if @c range is NULL. For instance, - * requesting range [1, 5] from a sensor with columns - * [0, 2], [1, 4], [4, 5] and [5, 8] will return all columns except [0, 2]. - * For sensors with only one or two channels, the range values are indices - * into the column series, inclusive. - * - * If a @c rsp array is provided and the client received a response, the array - * will be filled with as many of the response columns as it can fit, even if - * the buffer isn't big enough. If the call fails in a way that results in no - * response, @c count is set to 0. - * - * This call is blocking if the @c rsp buffer is non-NULL. Otherwise, this - * function will return, and the response will be passed to the - * bt_mesh_sensor_cli_handlers::series_entry callback as a list of - * sensor descriptors. - * - * @param[in] cli Sensor client instance. - * @param[in] ctx Message context parameters, or NULL to use the - * configured publish parameters. - * @param[in] sensor Sensor instance present on the targeted sensor server. - * @param[in] range Range of columns to get, or NULL to get all columns. - * @param[out] rsp Array of entries to copy the response into, or NULL - * to keep from blocking. - * @param[in,out] count The number of entries in @c rsp. Is changed to reflect - * the number of entries in the response. - * - * @retval 0 Successfully received the full list of sensor series - * columns. The @c rsp array and @c count has been - * changed to reflect the response contents. - * @retval -ENODEV The sensor server doesn't have the given sensor. - * @retval -E2BIG The list of sensor columns in the response was too - * big to fit in the @c rsp array. The @c rsp array has - * been filled up to the original @c count, and @c count - * has been changed to the number of columns in the - * response. - * @retval -ENOTSUP The sensor doesn't support series data. - * @retval -EADDRNOTAVAIL A message context was not provided and publishing is - * not configured. - * @retval -EAGAIN The device has not been provisioned. - * @retval -ETIMEDOUT The request timed out without a response. - */ -int bt_mesh_sensor_cli_series_entries_get( - struct bt_mesh_sensor_cli *cli, struct bt_mesh_msg_ctx *ctx, - const struct bt_mesh_sensor_type *sensor, - const struct bt_mesh_sensor_column *range, - struct bt_mesh_sensor_series_entry *rsp, uint32_t *count); -#endif /* !defined(CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE) */ /** @brief Retrieve all sensor descriptors in a sensor server. * diff --git a/include/bluetooth/mesh/sensor_srv.h b/include/bluetooth/mesh/sensor_srv.h index 396750aa3cfc..6ce435d840e5 100644 --- a/include/bluetooth/mesh/sensor_srv.h +++ b/include/bluetooth/mesh/sensor_srv.h @@ -110,7 +110,6 @@ struct bt_mesh_sensor_srv { const struct bt_mesh_model *model; }; -#if !defined(CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE) || defined(__DOXYGEN__) /** @brief Publish a sensor value. * * Immediately publishes the given sensor value, without checking thresholds @@ -133,30 +132,6 @@ int bt_mesh_sensor_srv_pub(struct bt_mesh_sensor_srv *srv, struct bt_mesh_msg_ctx *ctx, struct bt_mesh_sensor *sensor, const struct bt_mesh_sensor_value *value); -#else /* defined(CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE) */ -/** @brief Publish a sensor value. - * - * Immediately publishes the given sensor value, without checking thresholds - * or intervals. - * - * @see bt_mesh_sensor_srv_pub - * - * @param[in] srv Sensor server instance. - * @param[in] ctx Message context to publish with, or NULL to publish on the - * configured publish parameters. - * @param[in] sensor Sensor to publish with. - * @param[in] value Sensor value to publish, interpreted as an array of sensor - * channel values matching the sensor channels specified by - * the sensor type. The length of the array must match the - * sensor channel count. - * - * @return 0 on success, or (negative) error code otherwise. - */ -int bt_mesh_sensor_srv_pub(struct bt_mesh_sensor_srv *srv, - struct bt_mesh_msg_ctx *ctx, - struct bt_mesh_sensor *sensor, - const struct sensor_value *value); -#endif /* !defined(CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE) */ /** @brief Make the server to take a sample of the sensor, and publish if the * value changed sufficiently. diff --git a/samples/bluetooth/mesh/light_ctrl/prj.conf b/samples/bluetooth/mesh/light_ctrl/prj.conf index 1ebd14d3886d..9c3b0a799c3e 100644 --- a/samples/bluetooth/mesh/light_ctrl/prj.conf +++ b/samples/bluetooth/mesh/light_ctrl/prj.conf @@ -64,4 +64,3 @@ CONFIG_BT_MESH_LIGHT_CTRL_SRV_TIME_PROLONG=3 CONFIG_BT_MESH_LIGHT_CTRL_SRV_RESUME_DELAY=30 CONFIG_BT_MESH_SCENE_SRV=y CONFIG_BT_MESH_SENSOR_SRV=y -CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE=n diff --git a/samples/bluetooth/mesh/sensor_client/prj.conf b/samples/bluetooth/mesh/sensor_client/prj.conf index 7fd2a45f58aa..8df6e9454765 100644 --- a/samples/bluetooth/mesh/sensor_client/prj.conf +++ b/samples/bluetooth/mesh/sensor_client/prj.conf @@ -61,6 +61,5 @@ CONFIG_BT_MESH_SHELL=y # Bluetooth Mesh models CONFIG_BT_MESH_SENSOR_CLI=y -CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE=n CONFIG_LOG_BACKEND_RTT=n diff --git a/samples/bluetooth/mesh/sensor_server/prj.conf b/samples/bluetooth/mesh/sensor_server/prj.conf index 49efbf78dcdf..9c8f55098ada 100644 --- a/samples/bluetooth/mesh/sensor_server/prj.conf +++ b/samples/bluetooth/mesh/sensor_server/prj.conf @@ -64,7 +64,6 @@ CONFIG_BT_MESH_MODEL_KEY_COUNT=3 # Bluetooth Mesh models CONFIG_BT_MESH_SENSOR_SRV=y CONFIG_BT_MESH_SENSOR_SRV_SENSORS_MAX=5 -CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE=n # Increase default delayable message config to avoid insuffisient resources # during normal sample operation diff --git a/subsys/bluetooth/mesh/Kconfig.sensor b/subsys/bluetooth/mesh/Kconfig.sensor index e5ddf8810915..1c3ffbfc7adb 100644 --- a/subsys/bluetooth/mesh/Kconfig.sensor +++ b/subsys/bluetooth/mesh/Kconfig.sensor @@ -10,17 +10,6 @@ config BT_MESH_SENSOR menu "Bluetooth Mesh Sensors" visible if BT_MESH_SENSOR -config BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE - bool "Use legacy sensor_value in Sensor APIs" - default y if BT_MESH_SENSOR - select DEPRECATED - help - Enables the legacy Sensor APIs using sensor_value for sensor - value representation. If this is not selected, the APIs will use - bt_mesh_sensor_value for representation which can accurately represent - any raw sensor value. This option is deprecated and will be removed in - a future version. - config BT_MESH_SENSOR_LABELS bool "Enable Sensor labels" help diff --git a/subsys/bluetooth/mesh/light_ctrl_cli.c b/subsys/bluetooth/mesh/light_ctrl_cli.c index f6546a7ebdfb..6d895c2e8abb 100644 --- a/subsys/bluetooth/mesh/light_ctrl_cli.c +++ b/subsys/bluetooth/mesh/light_ctrl_cli.c @@ -9,7 +9,7 @@ #include "sensor.h" union prop_value { - sensor_value_type prop; + struct bt_mesh_sensor_value prop; float coeff; }; struct prop_status_ctx { @@ -365,11 +365,7 @@ int bt_mesh_light_ctrl_cli_light_onoff_set_unack( int bt_mesh_light_ctrl_cli_prop_get(struct bt_mesh_light_ctrl_cli *cli, struct bt_mesh_msg_ctx *ctx, enum bt_mesh_light_ctrl_prop id, -#ifdef CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE - struct sensor_value *rsp) -#else struct bt_mesh_sensor_value *rsp) -#endif { struct prop_status_ctx ack = { .id = id, @@ -402,14 +398,8 @@ int bt_mesh_light_ctrl_cli_prop_get(struct bt_mesh_light_ctrl_cli *cli, int bt_mesh_light_ctrl_cli_prop_set(struct bt_mesh_light_ctrl_cli *cli, struct bt_mesh_msg_ctx *ctx, enum bt_mesh_light_ctrl_prop id, -#ifdef CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE - const struct sensor_value *val, - struct sensor_value *rsp) -#else const struct bt_mesh_sensor_value *val, struct bt_mesh_sensor_value *rsp) - -#endif { const struct bt_mesh_sensor_format *format; struct prop_status_ctx ack = { @@ -455,11 +445,7 @@ int bt_mesh_light_ctrl_cli_prop_set(struct bt_mesh_light_ctrl_cli *cli, int bt_mesh_light_ctrl_cli_prop_set_unack(struct bt_mesh_light_ctrl_cli *cli, struct bt_mesh_msg_ctx *ctx, enum bt_mesh_light_ctrl_prop id, -#ifdef CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE - const struct sensor_value *val) -#else const struct bt_mesh_sensor_value *val) -#endif { const struct bt_mesh_sensor_format *format; int err; diff --git a/subsys/bluetooth/mesh/light_ctrl_internal.h b/subsys/bluetooth/mesh/light_ctrl_internal.h deleted file mode 100644 index a2209b3a6e4d..000000000000 --- a/subsys/bluetooth/mesh/light_ctrl_internal.h +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright (c) 2020 Nordic Semiconductor ASA - * - * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause - */ - -/** - * @file - * @brief Light LC internal types and utilities - */ - -#ifndef LIGHT_LC_INTERNAL_H__ -#define LIGHT_LC_INTERNAL_H__ - -#include -#include -#include -#include "sensor.h" - -#ifdef __cplusplus -extern "C" { -#endif - -#ifdef CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE -static inline int prop_encode(struct net_buf_simple *buf, - enum bt_mesh_light_ctrl_prop id, - const struct sensor_value *val) -{ - const struct bt_mesh_sensor_format *format; - - format = bt_mesh_lc_prop_format_get(id); - if (!format) { - return -ENOENT; - } - - return sensor_ch_encode(buf, format, val); -} - -static inline int prop_decode(struct net_buf_simple *buf, - enum bt_mesh_light_ctrl_prop id, - struct sensor_value *val) -{ - const struct bt_mesh_sensor_format *format; - - format = bt_mesh_lc_prop_format_get(id); - if (!format) { - return -ENOENT; - } - - return sensor_ch_decode(buf, format, val); -} -#endif - -#ifdef __cplusplus -} -#endif - -#endif /* LIGHT_LC_INTERNAL_H__ */ diff --git a/subsys/bluetooth/mesh/light_ctrl_srv.c b/subsys/bluetooth/mesh/light_ctrl_srv.c index ca41866c42ec..5b070b2f8fc9 100644 --- a/subsys/bluetooth/mesh/light_ctrl_srv.c +++ b/subsys/bluetooth/mesh/light_ctrl_srv.c @@ -10,7 +10,6 @@ #include #include #include "lightness_internal.h" -#include "light_ctrl_internal.h" #include "gen_onoff_internal.h" #include "sensor.h" #include "model_utils.h" @@ -57,19 +56,6 @@ static void restart_timer(struct bt_mesh_light_ctrl_srv *srv, uint32_t delay) k_work_reschedule(&srv->timer, K_MSEC(delay)); } -#ifdef CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE -static inline uint32_t to_centi_lux(const struct sensor_value *lux) -{ - return lux->val1 * 100L + lux->val2 / 10000L; -} - -static inline void from_centi_lux(uint32_t centi_lux, struct sensor_value *lux) -{ - lux->val1 = centi_lux / 100L; - lux->val2 = centi_lux % 10000L; -} -#endif - static void store(struct bt_mesh_light_ctrl_srv *srv, enum flags kind) { #if CONFIG_BT_SETTINGS @@ -305,14 +291,6 @@ static uint16_t light_get(struct bt_mesh_light_ctrl_srv *srv) #if CONFIG_BT_MESH_LIGHT_CTRL_SRV_REG -#ifdef CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE -static float sensor_to_float(struct sensor_value *val) -{ - return val->val1 + val->val2 / 1000000.0f; -} -#endif - -#ifndef CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE static uint32_t centilux_get(struct bt_mesh_light_ctrl_srv *srv) { if (!is_enabled(srv)) { @@ -330,29 +308,6 @@ static uint32_t centilux_get(struct bt_mesh_light_ctrl_srv *srv) return init + ((cfg - init) * delta) / srv->fade.duration; } -#else -static void lux_get(struct bt_mesh_light_ctrl_srv *srv, - sensor_value_type *lux) -{ - if (!is_enabled(srv)) { - memset(lux, 0, sizeof(*lux)); - return; - } - - if (!atomic_test_bit(&srv->flags, FLAG_TRANSITION) || - !srv->fade.duration) { - *lux = srv->cfg.lux[srv->state]; - return; - } - - uint32_t delta = curr_fade_time(srv); - uint32_t init = to_centi_lux(&srv->fade.initial_lux); - uint32_t cfg = to_centi_lux(&srv->cfg.lux[srv->state]); - uint32_t centi_lux = init + ((cfg - init) * delta) / srv->fade.duration; - - from_centi_lux(centi_lux, lux); -} -#endif static float lux_getf(struct bt_mesh_light_ctrl_srv *srv) { @@ -360,11 +315,7 @@ static float lux_getf(struct bt_mesh_light_ctrl_srv *srv) return 0.0f; } -#ifdef CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE - return to_centi_lux(&srv->cfg.lux[srv->state]) / 100.0f; -#else return srv->cfg.centilux[srv->state] / 100.0f; -#endif } static void reg_updated(struct bt_mesh_light_ctrl_reg *reg, float value) @@ -430,11 +381,7 @@ static void transition_start(struct bt_mesh_light_ctrl_srv *srv, srv->state = state; srv->fade.initial_light = light_get(srv); srv->fade.duration = fade_time; -#ifdef CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE - lux_get(srv, &srv->fade.initial_lux); -#else srv->fade.initial_centilux = centilux_get(srv); -#endif atomic_set_bit(&srv->flags, FLAG_TRANSITION); if (!IS_ENABLED(CONFIG_BT_MESH_LIGHT_CTRL_SRV_REG) || @@ -989,7 +936,7 @@ static int handle_sensor_status(const struct bt_mesh_model *model, struct bt_mes while (buf->len >= 3) { uint8_t len; uint16_t id; - sensor_value_type value; + struct bt_mesh_sensor_value value; sensor_status_id_decode(buf, &len, &id); @@ -1030,11 +977,7 @@ static int handle_sensor_status(const struct bt_mesh_model *model, struct bt_mes #if CONFIG_BT_MESH_LIGHT_CTRL_SRV_REG if (id == BT_MESH_PROP_ID_PRESENT_AMB_LIGHT_LEVEL && srv->reg) { -#ifdef CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE - srv->reg->measured = sensor_to_float(&value); -#else (void)bt_mesh_sensor_value_to_float(&value, &srv->reg->measured); -#endif #if CONFIG_BT_MESH_LIGHT_CTRL_AMB_LIGHT_LEVEL_TIMEOUT srv->amb_light_level_timestamp = k_uptime_get(); #endif @@ -1070,11 +1013,7 @@ static int handle_sensor_status(const struct bt_mesh_model *model, struct bt_mes /* Decode entire value to float, to get actual sensor value. */ float val; -#ifdef CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE - val = sensor_to_float(&value); -#else (void)bt_mesh_sensor_value_to_float(&value, &val); -#endif LOG_DBG("Checking sensor val"); if (id == BT_MESH_PROP_ID_TIME_SINCE_MOTION_SENSED) { @@ -1163,7 +1102,6 @@ static void update_lightness_setpoint(struct bt_mesh_light_ctrl_srv *srv, } } -#ifndef CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE #define MICRO_PER_MILLI 1000LL #define MICRO_PER_UNIT 1000000LL #define MICRO_PER_CENTI 10000LL @@ -1391,218 +1329,6 @@ static int prop_set(struct net_buf_simple *buf, return 0; } -#else /* defined(CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE) */ - -static void to_prop_time(uint32_t time, struct sensor_value *prop) -{ - prop->val1 = time / MSEC_PER_SEC; - prop->val2 = (time % MSEC_PER_SEC) * 1000; -} - -static uint32_t from_prop_time(const struct sensor_value *prop) -{ - return prop->val1 * MSEC_PER_SEC + prop->val2 / 1000; -} - -static int prop_get(struct net_buf_simple *buf, - const struct bt_mesh_light_ctrl_srv *srv, uint16_t id) -{ - struct sensor_value val = { 0 }; -#if CONFIG_BT_MESH_LIGHT_CTRL_SRV_REG - if (srv->reg) { - switch (id) { - /* Regulator coefficients are raw IEEE-754 floats, push them straight - * to the buffer instead of using sensor to encode them: - */ - case BT_MESH_LIGHT_CTRL_COEFF_KID: - net_buf_simple_add_mem(buf, &srv->reg->cfg.ki.down, sizeof(float)); - return 0; - case BT_MESH_LIGHT_CTRL_COEFF_KIU: - net_buf_simple_add_mem(buf, &srv->reg->cfg.ki.up, sizeof(float)); - return 0; - case BT_MESH_LIGHT_CTRL_COEFF_KPD: - net_buf_simple_add_mem(buf, &srv->reg->cfg.kp.down, sizeof(float)); - return 0; - case BT_MESH_LIGHT_CTRL_COEFF_KPU: - net_buf_simple_add_mem(buf, &srv->reg->cfg.kp.up, sizeof(float)); - return 0; - case BT_MESH_LIGHT_CTRL_PROP_REG_ACCURACY: - val.val1 = srv->reg->cfg.accuracy; - break; - } - } - switch (id) { - case BT_MESH_LIGHT_CTRL_PROP_ILLUMINANCE_ON: - val = srv->cfg.lux[LIGHT_CTRL_STATE_ON]; - break; - case BT_MESH_LIGHT_CTRL_PROP_ILLUMINANCE_PROLONG: - val = srv->cfg.lux[LIGHT_CTRL_STATE_PROLONG]; - break; - case BT_MESH_LIGHT_CTRL_PROP_ILLUMINANCE_STANDBY: - val = srv->cfg.lux[LIGHT_CTRL_STATE_STANDBY]; - break; -#else - switch (id) { - case BT_MESH_LIGHT_CTRL_PROP_ILLUMINANCE_ON: - case BT_MESH_LIGHT_CTRL_PROP_ILLUMINANCE_PROLONG: - case BT_MESH_LIGHT_CTRL_PROP_ILLUMINANCE_STANDBY: -#endif - case BT_MESH_LIGHT_CTRL_COEFF_KID: - case BT_MESH_LIGHT_CTRL_COEFF_KIU: - case BT_MESH_LIGHT_CTRL_COEFF_KPD: - case BT_MESH_LIGHT_CTRL_COEFF_KPU: - case BT_MESH_LIGHT_CTRL_PROP_REG_ACCURACY: - break; /* Prevent returning -ENOENT */ - case BT_MESH_LIGHT_CTRL_PROP_LIGHTNESS_ON: - val.val1 = to_actual(srv->cfg.light[LIGHT_CTRL_STATE_ON]); - break; - case BT_MESH_LIGHT_CTRL_PROP_LIGHTNESS_PROLONG: - val.val1 = to_actual(srv->cfg.light[LIGHT_CTRL_STATE_PROLONG]); - break; - case BT_MESH_LIGHT_CTRL_PROP_LIGHTNESS_STANDBY: - val.val1 = to_actual(srv->cfg.light[LIGHT_CTRL_STATE_STANDBY]); - break; - case BT_MESH_LIGHT_CTRL_PROP_TIME_FADE_PROLONG: - to_prop_time(srv->cfg.fade_prolong, &val); - break; - case BT_MESH_LIGHT_CTRL_PROP_TIME_FADE_ON: - to_prop_time(srv->cfg.fade_on, &val); - break; - case BT_MESH_LIGHT_CTRL_PROP_TIME_FADE_STANDBY_AUTO: - to_prop_time(srv->cfg.fade_standby_auto, &val); - break; - case BT_MESH_LIGHT_CTRL_PROP_TIME_FADE_STANDBY_MANUAL: - to_prop_time(srv->cfg.fade_standby_manual, &val); - break; - case BT_MESH_LIGHT_CTRL_PROP_TIME_OCCUPANCY_DELAY: - to_prop_time(srv->cfg.occupancy_delay, &val); - break; - case BT_MESH_LIGHT_CTRL_PROP_TIME_PROLONG: - to_prop_time(srv->cfg.prolong, &val); - break; - case BT_MESH_LIGHT_CTRL_PROP_TIME_ON: - to_prop_time(srv->cfg.on, &val); - break; - default: - return -ENOENT; - } - - return prop_encode(buf, id, &val); -} - -static int prop_set(struct net_buf_simple *buf, - struct bt_mesh_light_ctrl_srv *srv, uint16_t id) -{ - struct sensor_value val; - int err; - -#if CONFIG_BT_MESH_LIGHT_CTRL_SRV_REG - /* Regulator coefficients are raw IEEE-754 floats, pull them straight - * from the buffer instead of using sensor to decode them: - */ - if (srv->reg) { - switch (id) { - case BT_MESH_LIGHT_CTRL_COEFF_KID: - memcpy(&srv->reg->cfg.ki.down, - net_buf_simple_pull_mem(buf, sizeof(float)), - sizeof(float)); - return 0; - case BT_MESH_LIGHT_CTRL_COEFF_KIU: - memcpy(&srv->reg->cfg.ki.up, - net_buf_simple_pull_mem(buf, sizeof(float)), - sizeof(float)); - return 0; - case BT_MESH_LIGHT_CTRL_COEFF_KPD: - memcpy(&srv->reg->cfg.kp.down, - net_buf_simple_pull_mem(buf, sizeof(float)), - sizeof(float)); - return 0; - case BT_MESH_LIGHT_CTRL_COEFF_KPU: - memcpy(&srv->reg->cfg.kp.up, - net_buf_simple_pull_mem(buf, sizeof(float)), - sizeof(float)); - return 0; - } - } -#endif - - err = prop_decode(buf, id, &val); - if (err) { - return err; - } - - if (buf->len > 0) { - LOG_ERR("Invalid message size"); - return -EMSGSIZE; - } - - LOG_DBG("Set Prop: 0x%04x: %s", id, bt_mesh_sensor_ch_str(&val)); - - switch (id) { -#if CONFIG_BT_MESH_LIGHT_CTRL_SRV_REG - case BT_MESH_LIGHT_CTRL_PROP_REG_ACCURACY: - if (srv->reg) { - srv->reg->cfg.accuracy = val.val1; - } - break; - case BT_MESH_LIGHT_CTRL_PROP_ILLUMINANCE_ON: - srv->cfg.lux[LIGHT_CTRL_STATE_ON] = val; - break; - case BT_MESH_LIGHT_CTRL_PROP_ILLUMINANCE_PROLONG: - srv->cfg.lux[LIGHT_CTRL_STATE_PROLONG] = val; - break; - case BT_MESH_LIGHT_CTRL_PROP_ILLUMINANCE_STANDBY: - srv->cfg.lux[LIGHT_CTRL_STATE_STANDBY] = val; - break; -#else - case BT_MESH_LIGHT_CTRL_PROP_ILLUMINANCE_ON: - case BT_MESH_LIGHT_CTRL_PROP_ILLUMINANCE_PROLONG: - case BT_MESH_LIGHT_CTRL_PROP_ILLUMINANCE_STANDBY: - case BT_MESH_LIGHT_CTRL_PROP_REG_ACCURACY: -#endif - case BT_MESH_LIGHT_CTRL_COEFF_KID: - case BT_MESH_LIGHT_CTRL_COEFF_KIU: - case BT_MESH_LIGHT_CTRL_COEFF_KPD: - case BT_MESH_LIGHT_CTRL_COEFF_KPU: - break; /* Prevent returning -ENOENT */ - /* Properties are always set in light actual representation: */ - case BT_MESH_LIGHT_CTRL_PROP_LIGHTNESS_ON: - update_lightness_setpoint(srv, LIGHT_CTRL_STATE_ON, from_actual(val.val1)); - break; - case BT_MESH_LIGHT_CTRL_PROP_LIGHTNESS_PROLONG: - update_lightness_setpoint(srv, LIGHT_CTRL_STATE_PROLONG, from_actual(val.val1)); - break; - case BT_MESH_LIGHT_CTRL_PROP_LIGHTNESS_STANDBY: - update_lightness_setpoint(srv, LIGHT_CTRL_STATE_STANDBY, from_actual(val.val1)); - break; - case BT_MESH_LIGHT_CTRL_PROP_TIME_FADE_PROLONG: - srv->cfg.fade_prolong = from_prop_time(&val); - break; - case BT_MESH_LIGHT_CTRL_PROP_TIME_FADE_ON: - srv->cfg.fade_on = from_prop_time(&val); - break; - case BT_MESH_LIGHT_CTRL_PROP_TIME_FADE_STANDBY_AUTO: - srv->cfg.fade_standby_auto = from_prop_time(&val); - break; - case BT_MESH_LIGHT_CTRL_PROP_TIME_FADE_STANDBY_MANUAL: - srv->cfg.fade_standby_manual = from_prop_time(&val); - break; - case BT_MESH_LIGHT_CTRL_PROP_TIME_OCCUPANCY_DELAY: - srv->cfg.occupancy_delay = from_prop_time(&val); - break; - case BT_MESH_LIGHT_CTRL_PROP_TIME_PROLONG: - srv->cfg.prolong = from_prop_time(&val); - break; - case BT_MESH_LIGHT_CTRL_PROP_TIME_ON: - srv->cfg.on = from_prop_time(&val); - break; - default: - return -ENOENT; - } - - return 0; -} -#endif /* !defined(CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE) */ static int prop_tx(struct bt_mesh_light_ctrl_srv *srv, struct bt_mesh_msg_ctx *ctx, uint16_t id) diff --git a/subsys/bluetooth/mesh/sensor.c b/subsys/bluetooth/mesh/sensor.c index 4ea8870e4b94..f546b340eea4 100644 --- a/subsys/bluetooth/mesh/sensor.c +++ b/subsys/bluetooth/mesh/sensor.c @@ -17,7 +17,6 @@ LOG_MODULE_REGISTER(bt_mesh_sensor); /** Scale away the sensor_value fraction, to allow integer math */ #define SENSOR_MILL(_val) ((1000000LL * (_val)->val1) + (_val)->val2) -#ifndef CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE /** Delta threshold type. */ enum cadence_delta_type { /** Value based delta threshold. @@ -31,24 +30,11 @@ enum cadence_delta_type { */ CADENCE_DELTA_TYPE_PERCENT, }; -#endif static enum bt_mesh_sensor_cadence sensor_cadence(const struct bt_mesh_sensor_threshold *threshold, - const sensor_value_type *curr) + const struct bt_mesh_sensor_value *curr) { -#ifdef CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE - int64_t high_mill = SENSOR_MILL(&threshold->range.high); - int64_t low_mill = SENSOR_MILL(&threshold->range.low); - - if (high_mill == low_mill) { - return BT_MESH_SENSOR_CADENCE_NORMAL; - } - - int64_t curr_mill = SENSOR_MILL(curr); - bool in_range = (curr_mill >= MIN(low_mill, high_mill) && - curr_mill <= MAX(low_mill, high_mill)); -#else const struct bt_mesh_sensor_value *high, *low; const struct bt_mesh_sensor_format *fmt = curr->format; @@ -64,66 +50,10 @@ sensor_cadence(const struct bt_mesh_sensor_threshold *threshold, return BT_MESH_SENSOR_CADENCE_NORMAL; } - bool in_range = BT_MESH_SENSOR_VALUE_IN_RANGE(curr, low, high); -#endif - return in_range ? threshold->range.cadence : !threshold->range.cadence; + return BT_MESH_SENSOR_VALUE_IN_RANGE(curr, low, high) ? threshold->range.cadence + : !threshold->range.cadence; } -#ifdef CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE -bool bt_mesh_sensor_delta_threshold(const struct bt_mesh_sensor *sensor, - const struct sensor_value *curr) -{ - struct sensor_value delta = { - curr->val1 - sensor->state.prev.val1, - curr->val2 - sensor->state.prev.val2, - }; - int64_t delta_mill = SENSOR_MILL(&delta); - int64_t thrsh_mill; - - if (delta_mill < 0) { - delta_mill = -delta_mill; - thrsh_mill = SENSOR_MILL(&sensor->state.threshold.delta.down); - } else { - thrsh_mill = SENSOR_MILL(&sensor->state.threshold.delta.up); - } - - /* If the threshold value is a perentage, we should calculate the actual - * threshold value relative to the previous value. - */ - if (sensor->state.threshold.delta.type == - BT_MESH_SENSOR_DELTA_PERCENT) { - /* Store the Status Delta Trigger threshold in sensor_value first to avoid int64_t - * overflow caused by multiplication of values converted by the SENSOR_MILL() macro. - */ - struct sensor_value thrsh_delta = delta_mill >= 0 ? - sensor->state.threshold.delta.up : sensor->state.threshold.delta.down; - struct sensor_value thrsh = { - .val1 = thrsh_delta.val1 * sensor->state.prev.val1, - .val2 = thrsh_delta.val2 * sensor->state.prev.val2, - }; - - thrsh_mill = llabs(SENSOR_MILL(&thrsh)) / 100LL; - } - - if (IS_ENABLED(CONFIG_BT_MESH_MODEL_LOG_LEVEL_DBG)) { - char delta_str[BT_MESH_SENSOR_CH_STR_LEN]; - char curr_str[BT_MESH_SENSOR_CH_STR_LEN]; - char prev_str[BT_MESH_SENSOR_CH_STR_LEN]; - char thrsh_str[BT_MESH_SENSOR_CH_STR_LEN]; - - strcpy(delta_str, bt_mesh_sensor_ch_str(&delta)); - strcpy(curr_str, bt_mesh_sensor_ch_str(curr)); - strcpy(prev_str, bt_mesh_sensor_ch_str(&sensor->state.prev)); - strcpy(thrsh_str, - delta_mill < 0 ? bt_mesh_sensor_ch_str(&sensor->state.threshold.delta.down) : - bt_mesh_sensor_ch_str(&sensor->state.threshold.delta.up)); - - LOG_DBG("Delta: %s (%s - %s) thrsh: %s", delta_str, curr_str, prev_str, thrsh_str); - } - - return (delta_mill > thrsh_mill); -} -#else /* !defined(CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE) */ bool bt_mesh_sensor_delta_threshold(const struct bt_mesh_sensor *sensor, const struct bt_mesh_sensor_value *curr) { @@ -134,7 +64,6 @@ bool bt_mesh_sensor_delta_threshold(const struct bt_mesh_sensor *sensor, return curr->format->cb->delta_check(curr, &sensor->state.prev, &sensor->state.threshold.deltas); } -#endif /* defined(CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE) */ void bt_mesh_sensor_cadence_set(struct bt_mesh_sensor *sensor, enum bt_mesh_sensor_cadence cadence) @@ -185,25 +114,6 @@ void sensor_status_id_decode(struct net_buf_simple *buf, uint8_t *len, uint16_t } } -#ifdef CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE -static void tolerance_decode(uint16_t encoded, struct sensor_value *tolerance) -{ - uint32_t toll_mill = (encoded * 100ULL * 1000000ULL) / 4095ULL; - - tolerance->val1 = toll_mill / 1000000ULL; - tolerance->val2 = toll_mill % 1000000ULL; -} -static uint16_t tolerance_encode(const struct sensor_value *tol) -{ - uint64_t tol_mill = 1000000ULL * tol->val1 + tol->val2; - - if (tol_mill > (1000000ULL * 100ULL)) { - return 0; - } - return (tol_mill * 4095ULL + (1000000ULL * 50ULL)) / (1000000ULL * 100ULL); -} -#endif - void sensor_descriptor_decode(struct net_buf_simple *buf, struct bt_mesh_sensor_info *sensor) { @@ -211,15 +121,8 @@ void sensor_descriptor_decode(struct net_buf_simple *buf, sensor->id = net_buf_simple_pull_le16(buf); tolerances = net_buf_simple_pull_le24(buf); -#ifdef CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE - tolerance_decode(tolerances & BIT_MASK(12), - &sensor->descriptor.tolerance.positive); - tolerance_decode(tolerances >> 12, - &sensor->descriptor.tolerance.negative); -#else sensor->descriptor.tolerance.positive = tolerances & BIT_MASK(12); sensor->descriptor.tolerance.negative = tolerances >> 12; -#endif sensor->descriptor.sampling_type = net_buf_simple_pull_u8(buf); sensor->descriptor.period = sensor_powtime_decode(net_buf_simple_pull_u8(buf)); @@ -236,13 +139,8 @@ void sensor_descriptor_encode(struct net_buf_simple *buf, const struct bt_mesh_sensor_descriptor *d = sensor->descriptor ? sensor->descriptor : &dummy; -#ifdef CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE - uint16_t tol_pos = tolerance_encode(&d->tolerance.positive); - uint16_t tol_neg = tolerance_encode(&d->tolerance.negative); -#else uint16_t tol_pos = d->tolerance.positive; uint16_t tol_neg = d->tolerance.negative; -#endif net_buf_simple_add_u8(buf, tol_pos & 0xff); net_buf_simple_add_u8(buf, @@ -256,7 +154,7 @@ void sensor_descriptor_encode(struct net_buf_simple *buf, int sensor_value_encode(struct net_buf_simple *buf, const struct bt_mesh_sensor_type *type, - const sensor_value_type *values) + const struct bt_mesh_sensor_value *values) { /* The API assumes that `values` array size is always CONFIG_BT_MESH_SENSOR_CHANNELS_MAX. */ __ASSERT_NO_MSG(type->channel_count <= CONFIG_BT_MESH_SENSOR_CHANNELS_MAX); @@ -276,7 +174,7 @@ int sensor_value_encode(struct net_buf_simple *buf, int sensor_value_decode(struct net_buf_simple *buf, const struct bt_mesh_sensor_type *type, - sensor_value_type *values) + struct bt_mesh_sensor_value *values) { int err; @@ -296,11 +194,8 @@ int sensor_value_decode(struct net_buf_simple *buf, int sensor_ch_encode(struct net_buf_simple *buf, const struct bt_mesh_sensor_format *format, - const sensor_value_type *value) + const struct bt_mesh_sensor_value *value) { -#ifdef CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE - return format->encode(format, value, buf); -#else if (format != value->format) { return -EINVAL; } @@ -311,16 +206,12 @@ int sensor_ch_encode(struct net_buf_simple *buf, net_buf_simple_add_mem(buf, value->raw, value->format->size); return 0; -#endif } int sensor_ch_decode(struct net_buf_simple *buf, const struct bt_mesh_sensor_format *format, - sensor_value_type *value) + struct bt_mesh_sensor_value *value) { -#ifdef CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE - return format->decode(format, buf, value); -#else if (buf->len < format->size) { return -ENOMEM; } @@ -328,12 +219,11 @@ int sensor_ch_decode(struct net_buf_simple *buf, memcpy(value->raw, net_buf_simple_pull_mem(buf, format->size), format->size); return 0; -#endif } int sensor_status_encode(struct net_buf_simple *buf, const struct bt_mesh_sensor *sensor, - const sensor_value_type *values) + const struct bt_mesh_sensor_value *values) { const struct bt_mesh_sensor_type *type = sensor->type; size_t size = 0; @@ -367,7 +257,7 @@ int sensor_column_value_encode(struct net_buf_simple *buf, struct bt_mesh_msg_ctx *ctx, uint32_t column_index) { - sensor_value_type values[CONFIG_BT_MESH_SENSOR_CHANNELS_MAX]; + struct bt_mesh_sensor_value values[CONFIG_BT_MESH_SENSOR_CHANNELS_MAX]; int err = sensor->series.get(srv, sensor, ctx, column_index, values); if (err) { @@ -392,18 +282,6 @@ int sensor_column_encode(struct net_buf_simple *buf, return -ENOTSUP; } -#ifdef CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE - const uint64_t width_million = - (col->end.val1 - col->start.val1) * 1000000ULL + - (col->end.val2 - col->start.val2); - const struct sensor_value width = { - .val1 = width_million / 1000000ULL, - .val2 = width_million % 1000000ULL, - }; - - LOG_DBG("Column width: %s", bt_mesh_sensor_ch_str(&width)); -#endif - int err; err = sensor_ch_encode(buf, col_format, &col->start); @@ -411,12 +289,7 @@ int sensor_column_encode(struct net_buf_simple *buf, return err; } -#ifdef CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE - /* The sensor columns are transmitted as start+width, not start+end: */ - err = sensor_ch_encode(buf, col_format, &width); -#else err = sensor_ch_encode(buf, col_format, &col->width); -#endif if (err) { return err; } @@ -427,7 +300,7 @@ int sensor_column_encode(struct net_buf_simple *buf, int sensor_column_decode( struct net_buf_simple *buf, const struct bt_mesh_sensor_type *type, struct bt_mesh_sensor_column *col, - sensor_value_type value[CONFIG_BT_MESH_SENSOR_CHANNELS_MAX]) + struct bt_mesh_sensor_value value[CONFIG_BT_MESH_SENSOR_CHANNELS_MAX]) { const struct bt_mesh_sensor_format *col_format; int err; @@ -448,25 +321,10 @@ int sensor_column_decode( return -ENOENT; } -#ifdef CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE - struct sensor_value width; - - err = sensor_ch_decode(buf, col_format, &width); - if (err) { - return err; - } - - uint64_t end_mill = (col->start.val1 + width.val1) * 1000000ULL + - (col->start.val2 + width.val2); - - col->end.val1 = end_mill / 1000000ULL; - col->end.val2 = end_mill % 1000000ULL; -#else err = sensor_ch_decode(buf, col_format, &col->width); if (err) { return err; } -#endif return sensor_value_decode(buf, type, value); } @@ -568,39 +426,22 @@ int sensor_cadence_encode(struct net_buf_simple *buf, uint8_t fast_period_div, uint8_t min_int, const struct bt_mesh_sensor_threshold *threshold) { -#ifdef CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE - const struct bt_mesh_sensor_format *delta_format = - (threshold->delta.type == BT_MESH_SENSOR_DELTA_PERCENT) ? - &bt_mesh_sensor_format_percentage_delta_trigger : - sensor_type->channels[0].format; - - enum bt_mesh_sensor_delta delta_type = !!threshold->delta.type; -#else const struct bt_mesh_sensor_format *delta_format = threshold->deltas.down.format; enum cadence_delta_type delta_type = delta_format == &bt_mesh_sensor_format_percentage_delta_trigger ? CADENCE_DELTA_TYPE_PERCENT : CADENCE_DELTA_TYPE_VALUE; -#endif net_buf_simple_add_u8(buf, (delta_type << 7) | (BIT_MASK(7) & fast_period_div)); int err; -#ifdef CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE - err = sensor_ch_encode(buf, delta_format, &threshold->delta.down); -#else err = sensor_ch_encode(buf, delta_format, &threshold->deltas.down); -#endif if (err) { return err; } -#ifdef CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE - err = sensor_ch_encode(buf, delta_format, &threshold->delta.up); -#else err = sensor_ch_encode(buf, delta_format, &threshold->deltas.up); -#endif if (err) { return err; } @@ -612,7 +453,7 @@ int sensor_cadence_encode(struct net_buf_simple *buf, net_buf_simple_add_u8(buf, min_int); /* Flip the order if the cadence is fast outside. */ - const sensor_value_type *first, *second; + const struct bt_mesh_sensor_value *first, *second; if (threshold->range.cadence == BT_MESH_SENSOR_CADENCE_FAST) { first = &threshold->range.low; @@ -636,45 +477,26 @@ int sensor_cadence_decode(struct net_buf_simple *buf, struct bt_mesh_sensor_threshold *threshold) { const struct bt_mesh_sensor_format *delta_format; -#ifdef CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE - enum bt_mesh_sensor_delta delta_type; -#else enum cadence_delta_type delta_type; -#endif uint8_t div_and_type; int err; div_and_type = net_buf_simple_pull_u8(buf); delta_type = div_and_type >> 7; -#ifdef CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE - threshold->delta.type = delta_type; - delta_format = (delta_type == BT_MESH_SENSOR_DELTA_PERCENT) ? - &bt_mesh_sensor_format_percentage_delta_trigger : - sensor_type->channels[0].format; -#else delta_format = (delta_type == CADENCE_DELTA_TYPE_PERCENT) ? &bt_mesh_sensor_format_percentage_delta_trigger : sensor_type->channels[0].format; -#endif *fast_period_div = div_and_type & BIT_MASK(7); if (*fast_period_div > BT_MESH_SENSOR_PERIOD_DIV_MAX) { return -EINVAL; } -#ifdef CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE - err = sensor_ch_decode(buf, delta_format, &threshold->delta.down); -#else err = sensor_ch_decode(buf, delta_format, &threshold->deltas.down); -#endif if (err) { return err; } -#ifdef CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE - err = sensor_ch_decode(buf, delta_format, &threshold->delta.up); -#else err = sensor_ch_decode(buf, delta_format, &threshold->deltas.up); -#endif if (err) { return err; } @@ -699,16 +521,6 @@ int sensor_cadence_decode(struct net_buf_simple *buf, /* According to MshMDLv1.1: 4.1.3.6, if range.high is lower than range.low, the cadence is * fast outside the range. Swap the two in this case. */ -#ifdef CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE - if (threshold->range.high.val1 < threshold->range.low.val1 || - (threshold->range.high.val1 == threshold->range.low.val1 && - threshold->range.high.val2 < threshold->range.low.val2)) { - struct sensor_value temp; - - temp = threshold->range.high; - threshold->range.high = threshold->range.low; - threshold->range.low = temp; -#else if (threshold->range.high.format->cb->compare(&threshold->range.low, &threshold->range.high) > 0) { uint8_t temp[CONFIG_BT_MESH_SENSOR_CHANNEL_ENCODED_SIZE_MAX]; @@ -719,7 +531,6 @@ int sensor_cadence_decode(struct net_buf_simple *buf, CONFIG_BT_MESH_SENSOR_CHANNEL_ENCODED_SIZE_MAX); memcpy(&threshold->range.low.raw, temp, CONFIG_BT_MESH_SENSOR_CHANNEL_ENCODED_SIZE_MAX); -#endif threshold->range.cadence = BT_MESH_SENSOR_CADENCE_NORMAL; } else { threshold->range.cadence = BT_MESH_SENSOR_CADENCE_FAST; @@ -739,17 +550,6 @@ uint8_t sensor_pub_div_get(const struct bt_mesh_sensor *s, uint32_t base_period) return div; } -#ifdef CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE -bool bt_mesh_sensor_value_in_column(const struct sensor_value *value, - const struct bt_mesh_sensor_column *col) -{ - return (value->val1 > col->start.val1 || - (value->val1 == col->start.val1 && - value->val2 >= col->start.val2)) && - (value->val1 < col->end.val1 || - (value->val1 == col->end.val1 && value->val2 <= col->end.val2)); -} -#else bool bt_mesh_sensor_value_in_column(const struct bt_mesh_sensor_value *value, const struct bt_mesh_sensor_column *col) { @@ -784,10 +584,9 @@ bool bt_mesh_sensor_value_in_column(const struct bt_mesh_sensor_value *value, return valuef <= (startf + widthf); } -#endif void sensor_cadence_update(struct bt_mesh_sensor *sensor, - const sensor_value_type *value) + const struct bt_mesh_sensor_value *value) { if (!sensor->state.configured) { /* Ignore any update before cadence is configured. */ @@ -815,11 +614,7 @@ void sensor_cadence_update(struct bt_mesh_sensor *sensor, sensor->state.fast_pub = new; } -#ifdef CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE -const char *bt_mesh_sensor_ch_str(const struct sensor_value *ch) -#else const char *bt_mesh_sensor_ch_str(const struct bt_mesh_sensor_value *ch) -#endif { static char str[BT_MESH_SENSOR_CH_STR_LEN]; @@ -830,7 +625,6 @@ const char *bt_mesh_sensor_ch_str(const struct bt_mesh_sensor_value *ch) return str; } -#ifndef CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE int bt_mesh_sensor_ch_to_str(const struct bt_mesh_sensor_value *ch, char *str, size_t len) { @@ -1009,4 +803,3 @@ int bt_mesh_sensor_value_from_special_status( } return format->cb->from_special_status(format, status, sensor_val); } -#endif /* defined(CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE) */ diff --git a/subsys/bluetooth/mesh/sensor.h b/subsys/bluetooth/mesh/sensor.h index b07aa66fc315..076c293a2396 100644 --- a/subsys/bluetooth/mesh/sensor.h +++ b/subsys/bluetooth/mesh/sensor.h @@ -55,18 +55,6 @@ extern const struct bt_mesh_sensor_unit bt_mesh_sensor_unit_unitless; /** @} */ -/* Temporary typedef while we need to support two different sensor value types - * in the deprecation period. Can be removed once support for - * struct sensor_value is removed, and internal APIs changed to use - * struct bt_mesh_sensor_value. - */ -#ifdef CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE -typedef struct sensor_value sensor_value_type; -#else -typedef struct bt_mesh_sensor_value sensor_value_type; -#endif - -#ifndef CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE /** @brief Check if a value change breaks the delta threshold. * * Sensors should publish their value if the measured sample is outside the @@ -87,11 +75,10 @@ typedef struct bt_mesh_sensor_value sensor_value_type; */ bool bt_mesh_sensor_delta_threshold(const struct bt_mesh_sensor *sensor, const struct bt_mesh_sensor_value *value); -#endif /* CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE */ int sensor_status_encode(struct net_buf_simple *buf, const struct bt_mesh_sensor *sensor, - const sensor_value_type *values); + const struct bt_mesh_sensor_value *values); int sensor_status_id_encode(struct net_buf_simple *buf, uint8_t len, uint16_t id); void sensor_status_id_decode(struct net_buf_simple *buf, uint8_t *len, uint16_t *id); @@ -103,17 +90,17 @@ void sensor_descriptor_encode(struct net_buf_simple *buf, int sensor_value_encode(struct net_buf_simple *buf, const struct bt_mesh_sensor_type *type, - const sensor_value_type *values); + const struct bt_mesh_sensor_value *values); int sensor_value_decode(struct net_buf_simple *buf, const struct bt_mesh_sensor_type *type, - sensor_value_type *values); + struct bt_mesh_sensor_value *values); int sensor_ch_encode(struct net_buf_simple *buf, const struct bt_mesh_sensor_format *format, - const sensor_value_type *value); + const struct bt_mesh_sensor_value *value); int sensor_ch_decode(struct net_buf_simple *buf, const struct bt_mesh_sensor_format *format, - sensor_value_type *value); + struct bt_mesh_sensor_value *value); int sensor_column_value_encode(struct net_buf_simple *buf, struct bt_mesh_sensor_srv *srv, @@ -128,7 +115,7 @@ int sensor_column_encode(struct net_buf_simple *buf, int sensor_column_decode( struct net_buf_simple *buf, const struct bt_mesh_sensor_type *type, struct bt_mesh_sensor_column *col, - sensor_value_type value[CONFIG_BT_MESH_SENSOR_CHANNELS_MAX]); + struct bt_mesh_sensor_value value[CONFIG_BT_MESH_SENSOR_CHANNELS_MAX]); int sensor_cadence_encode(struct net_buf_simple *buf, const struct bt_mesh_sensor_type *sensor_type, @@ -147,7 +134,7 @@ uint64_t sensor_powtime_decode_us(uint8_t val); uint8_t sensor_pub_div_get(const struct bt_mesh_sensor *s, uint32_t base_period); void sensor_cadence_update(struct bt_mesh_sensor *sensor, - const sensor_value_type *value); + const struct bt_mesh_sensor_value *value); #ifdef __cplusplus } diff --git a/subsys/bluetooth/mesh/sensor_cli.c b/subsys/bluetooth/mesh/sensor_cli.c index c8728122e80e..61a2f7d44286 100644 --- a/subsys/bluetooth/mesh/sensor_cli.c +++ b/subsys/bluetooth/mesh/sensor_cli.c @@ -40,11 +40,7 @@ struct sensor_data_list_rsp { struct series_data_rsp { struct bt_mesh_sensor_series_entry *entries; -#ifdef CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE - const struct bt_mesh_sensor_column *col; -#else const struct bt_mesh_sensor_value *col_start; -#endif uint16_t id; uint32_t count; }; @@ -156,7 +152,7 @@ static int handle_status(const struct bt_mesh_model *model, struct bt_mesh_msg_c return -EMSGSIZE; } - sensor_value_type value[CONFIG_BT_MESH_SENSOR_CHANNELS_MAX]; + struct bt_mesh_sensor_value value[CONFIG_BT_MESH_SENSOR_CHANNELS_MAX]; err = sensor_value_decode(buf, type, value); if (err) { @@ -170,7 +166,7 @@ static int handle_status(const struct bt_mesh_model *model, struct bt_mesh_msg_c if (rsp && count <= rsp->count) { memcpy(rsp->sensors[count].value, value, - sizeof(sensor_value_type) * type->channel_count); + sizeof(struct bt_mesh_sensor_value) * type->channel_count); rsp->sensors[count].type = type; ++count; @@ -239,12 +235,7 @@ static int handle_column_status(const struct bt_mesh_model *model, struct bt_mes (void **)&rsp)) { /* If column format exists verify Raw Value A */ if (col_format && -#ifdef CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE - (rsp->col->start.val1 != entry.column.start.val1 || - rsp->col->start.val2 != entry.column.start.val2)) { -#else col_format->cb->compare(rsp->col_start, &entry.column.start) != 0) { -#endif return 0; } @@ -821,11 +812,7 @@ int bt_mesh_sensor_cli_setting_set(struct bt_mesh_sensor_cli *cli, struct bt_mesh_msg_ctx *ctx, const struct bt_mesh_sensor_type *sensor, const struct bt_mesh_sensor_type *setting, -#ifdef CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE - const struct sensor_value *value, -#else const struct bt_mesh_sensor_value *value, -#endif struct bt_mesh_sensor_setting_status *rsp) { int err; @@ -874,11 +861,7 @@ int bt_mesh_sensor_cli_setting_set_unack( struct bt_mesh_sensor_cli *cli, struct bt_mesh_msg_ctx *ctx, const struct bt_mesh_sensor_type *sensor, const struct bt_mesh_sensor_type *setting, -#ifdef CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE - const struct sensor_value *value) -#else const struct bt_mesh_sensor_value *value) -#endif { int err; @@ -938,11 +921,7 @@ int bt_mesh_sensor_cli_all_get(struct bt_mesh_sensor_cli *cli, int bt_mesh_sensor_cli_get(struct bt_mesh_sensor_cli *cli, struct bt_mesh_msg_ctx *ctx, const struct bt_mesh_sensor_type *sensor, -#ifdef CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE - struct sensor_value *rsp) -#else struct bt_mesh_sensor_value *rsp) -#endif { int err; @@ -973,7 +952,7 @@ int bt_mesh_sensor_cli_get(struct bt_mesh_sensor_cli *cli, return -ENODEV; } - memcpy(rsp, sensor_data.value, sizeof(sensor_value_type) * sensor->channel_count); + memcpy(rsp, sensor_data.value, sizeof(struct bt_mesh_sensor_value) * sensor->channel_count); return 0; } @@ -981,11 +960,7 @@ int bt_mesh_sensor_cli_get(struct bt_mesh_sensor_cli *cli, int bt_mesh_sensor_cli_series_entry_get( struct bt_mesh_sensor_cli *cli, struct bt_mesh_msg_ctx *ctx, const struct bt_mesh_sensor_type *sensor, -#ifdef CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE - const struct bt_mesh_sensor_column *column, -#else const union bt_mesh_sensor_column_key *column, -#endif struct bt_mesh_sensor_series_entry *rsp) { const struct bt_mesh_sensor_format *col_format; @@ -997,16 +972,6 @@ int bt_mesh_sensor_cli_series_entry_get( net_buf_simple_add_le16(&msg, sensor->id); col_format = bt_mesh_sensor_column_format_get(sensor); -#ifdef CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE - if (col_format == NULL) { - net_buf_simple_add_le16(&msg, column->start.val1); - } else { - err = sensor_ch_encode(&msg, col_format, &column->start); - if (err) { - return err; - } - } -#else if (col_format == NULL) { net_buf_simple_add_le16(&msg, column->index); } else { @@ -1015,17 +980,12 @@ int bt_mesh_sensor_cli_series_entry_get( return err; } } -#endif struct series_data_rsp rsp_data = { .entries = rsp, .id = sensor->id, .count = 1, -#ifdef CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE - .col = column, -#else .col_start = col_format ? &column->sensor_value : NULL, -#endif }; struct bt_mesh_msg_rsp_ctx rsp_ctx = { @@ -1050,12 +1010,8 @@ int bt_mesh_sensor_cli_series_entry_get( int bt_mesh_sensor_cli_series_entries_get( struct bt_mesh_sensor_cli *cli, struct bt_mesh_msg_ctx *ctx, const struct bt_mesh_sensor_type *sensor, -#ifdef CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE - const struct bt_mesh_sensor_column *range, -#else const union bt_mesh_sensor_column_key *range_start, const union bt_mesh_sensor_column_key *range_end, -#endif struct bt_mesh_sensor_series_entry *rsp, uint32_t *count) { int err; @@ -1066,31 +1022,11 @@ int bt_mesh_sensor_cli_series_entries_get( net_buf_simple_add_le16(&msg, sensor->id); -#ifdef CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE - if (range) { -#else if (range_start && range_end) { -#endif const struct bt_mesh_sensor_format *col_format; col_format = bt_mesh_sensor_column_format_get(sensor); -#ifdef CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE - if (!col_format) { - net_buf_simple_add_le16(&msg, range->start.val1); - net_buf_simple_add_le16(&msg, range->end.val1); - } else { - err = sensor_ch_encode(&msg, col_format, &range->start); - if (err) { - return err; - } - - err = sensor_ch_encode(&msg, col_format, &range->end); - if (err) { - return err; - } - } -#else if (!col_format) { net_buf_simple_add_le16(&msg, range_start->index); net_buf_simple_add_le16(&msg, range_end->index); @@ -1105,7 +1041,6 @@ int bt_mesh_sensor_cli_series_entries_get( return err; } } -#endif } struct series_data_rsp rsp_data = { diff --git a/subsys/bluetooth/mesh/sensor_srv.c b/subsys/bluetooth/mesh/sensor_srv.c index ca9a474ca6e3..c669eef5ec69 100644 --- a/subsys/bluetooth/mesh/sensor_srv.c +++ b/subsys/bluetooth/mesh/sensor_srv.c @@ -80,7 +80,7 @@ static void cadence_store(struct bt_mesh_sensor_srv *srv) static int value_get(struct bt_mesh_sensor_srv *srv, struct bt_mesh_sensor *sensor, struct bt_mesh_msg_ctx *ctx, - sensor_value_type *value) + struct bt_mesh_sensor_value *value) { int err; @@ -104,7 +104,7 @@ static int buf_status_add(struct bt_mesh_sensor_srv *srv, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf) { - sensor_value_type value[CONFIG_BT_MESH_SENSOR_CHANNELS_MAX] = {}; + struct bt_mesh_sensor_value value[CONFIG_BT_MESH_SENSOR_CHANNELS_MAX] = {}; struct net_buf_simple_state state; int err; @@ -223,15 +223,10 @@ static int handle_get(const struct bt_mesh_model *model, struct bt_mesh_msg_ctx static const struct bt_mesh_sensor_column * column_get(const struct bt_mesh_sensor_series *series, - const sensor_value_type *val) + const struct bt_mesh_sensor_value *val) { for (uint32_t i = 0; i < series->column_count; ++i) { -#ifdef CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE - if (series->columns[i].start.val1 == val->val1 && - series->columns[i].start.val2 == val->val2) { -#else if (val->format->cb->compare(&series->columns[i].start, val) == 0) { -#endif return &series->columns[i]; } } @@ -291,7 +286,7 @@ static int handle_column_get(const struct bt_mesh_model *model, struct bt_mesh_m const struct bt_mesh_sensor_format *col_format; const struct bt_mesh_sensor_column *col; - sensor_value_type col_x; + struct bt_mesh_sensor_value col_x; col_format = bt_mesh_sensor_column_format_get(sensor->type); @@ -407,7 +402,7 @@ static int handle_series_get(const struct bt_mesh_model *model, struct bt_mesh_m /* Check buf->len different from 0, before decoding buf to range and buf->len changes. */ bool ranged = (buf->len != 0); - sensor_value_type range_start, range_end; + struct bt_mesh_sensor_value range_start, range_end; if (buf->len == col_format->size * 2) { int err; @@ -557,13 +552,8 @@ static int cadence_set(const struct bt_mesh_model *model, struct bt_mesh_msg_ctx char range_low_str[BT_MESH_SENSOR_CH_STR_LEN]; char range_high_str[BT_MESH_SENSOR_CH_STR_LEN]; -#ifdef CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE - strcpy(delta_down_str, bt_mesh_sensor_ch_str(&threshold.delta.down)); - strcpy(delta_up_str, bt_mesh_sensor_ch_str(&threshold.delta.up)); -#else strcpy(delta_down_str, bt_mesh_sensor_ch_str(&threshold.deltas.down)); strcpy(delta_up_str, bt_mesh_sensor_ch_str(&threshold.deltas.up)); -#endif strcpy(range_low_str, bt_mesh_sensor_ch_str(&threshold.range.low)); strcpy(range_high_str, bt_mesh_sensor_ch_str(&threshold.range.high)); @@ -705,7 +695,7 @@ static int handle_setting_get(const struct bt_mesh_model *model, struct bt_mesh_ net_buf_simple_add_u8(&rsp, setting->set ? 0x03 : 0x01); - sensor_value_type values[CONFIG_BT_MESH_SENSOR_CHANNELS_MAX] = { 0 }; + struct bt_mesh_sensor_value values[CONFIG_BT_MESH_SENSOR_CHANNELS_MAX] = { 0 }; setting->get(srv, sensor, setting, ctx, values); @@ -756,7 +746,7 @@ static int setting_set(const struct bt_mesh_model *model, struct bt_mesh_msg_ctx goto respond; } - sensor_value_type values[CONFIG_BT_MESH_SENSOR_CHANNELS_MAX]; + struct bt_mesh_sensor_value values[CONFIG_BT_MESH_SENSOR_CHANNELS_MAX]; err = sensor_value_decode(buf, setting->type, values); if (err) { @@ -913,7 +903,7 @@ static void pub_msg_add(struct bt_mesh_sensor_srv *srv, return; } - sensor_value_type value[CONFIG_BT_MESH_SENSOR_CHANNELS_MAX] = {}; + struct bt_mesh_sensor_value value[CONFIG_BT_MESH_SENSOR_CHANNELS_MAX] = {}; err = value_get(srv, s, NULL, value); if (err) { @@ -1151,11 +1141,7 @@ const struct bt_mesh_model_cb _bt_mesh_sensor_setup_srv_cb = { int bt_mesh_sensor_srv_pub(struct bt_mesh_sensor_srv *srv, struct bt_mesh_msg_ctx *ctx, struct bt_mesh_sensor *sensor, -#ifdef CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE - const struct sensor_value *value) -#else const struct bt_mesh_sensor_value *value) -#endif { int err; @@ -1182,7 +1168,7 @@ int bt_mesh_sensor_srv_pub(struct bt_mesh_sensor_srv *srv, int bt_mesh_sensor_srv_sample(struct bt_mesh_sensor_srv *srv, struct bt_mesh_sensor *sensor) { - sensor_value_type value[CONFIG_BT_MESH_SENSOR_CHANNELS_MAX] = {}; + struct bt_mesh_sensor_value value[CONFIG_BT_MESH_SENSOR_CHANNELS_MAX] = {}; int err; err = value_get(srv, sensor, NULL, value); diff --git a/subsys/bluetooth/mesh/sensor_types.c b/subsys/bluetooth/mesh/sensor_types.c index 9f81b6ed496e..a2a8179b712f 100644 --- a/subsys/bluetooth/mesh/sensor_types.c +++ b/subsys/bluetooth/mesh/sensor_types.c @@ -68,13 +68,8 @@ UNIT(va) = { "Volt Ampere", "VA" }; UNIT(kvah) = { "Kilo Volt Ampere hours", "kVAh" }; UNIT(db) = { "Decibel", "dB" }; UNIT(lux) = { "Lux", "lx" }; -#ifdef CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE -UNIT(klxh) = { "Kilo Lux hours", "klxh" }; -UNIT(klmh) = { "Kilo Lumen hours", "klmh" }; -#else UNIT(lxh) = { "Lux hours", "lxh" }; UNIT(lmh) = { "Lumen hours", "lmh" }; -#endif UNIT(lumen) = { "Lumen", "lm" }; UNIT(lumen_per_watt) = { "Lumen per Watt", "lm/W" }; UNIT(degrees) = { "Degrees", "degrees" }; @@ -108,11 +103,7 @@ UNIT(unitless) = { "Unitless" }; #define SCALAR_REPR(_scalar, _flags) SCALAR_REPR_RANGED(_scalar, _flags, 0, 0) -#ifdef CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE -#define SCALAR_CALLBACKS .encode = scalar_encode, .decode = scalar_decode -#else #define SCALAR_CALLBACKS .cb = &scalar_cb -#endif #ifdef CONFIG_BT_MESH_SENSOR_LABELS @@ -331,216 +322,6 @@ static int scalar_encode_raw(const struct bt_mesh_sensor_format *format, return 0; } -#ifdef CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE -#define MUL_SCALAR(_val, _repr) (((repr)->flags & DIVIDE) ? \ - ((_val) / (_repr)->value) : \ - ((_val) * (_repr)->value)) - -#define DIV_SCALAR(_val, _repr) (((repr)->flags & DIVIDE) ? \ - ((_val) * (_repr)->value) : \ - ((_val) / (_repr)->value)) - -static int scalar_encode(const struct bt_mesh_sensor_format *format, - const struct sensor_value *val, - struct net_buf_simple *buf) -{ - const struct scalar_repr *repr = format->user_data; - - if (net_buf_simple_tailroom(buf) < format->size) { - return -ENOMEM; - } - - int64_t raw = DIV_SCALAR(val->val1, repr) + - DIV_SCALAR(val->val2, repr) / 1000000LL; - - int64_t max_value = scalar_max(format); - int32_t min_value = scalar_min(format); - - if (raw > max_value || raw < min_value) { - uint32_t type_max = BIT64(8 * format->size) - 1; - if (repr->flags & SIGNED) { - type_max = BIT64(8 * format->size - 1) - 1; - } - - if ((repr->flags & HAS_HIGHER_THAN) && - !((repr->flags & HAS_UNDEFINED) && (val->val1 == type_max))) { - raw = max_value; - } else if ((repr->flags & HAS_INVALID) && val->val1 == type_max - 1) { - raw = type_max - 1; - } else if ((repr->flags & HAS_UNDEFINED) && val->val1 == type_max) { - raw = type_max; - } else if (repr->flags & HAS_UNDEFINED_MIN) { - raw = type_max + 1; - } else { - return -ERANGE; - } - } - - return scalar_encode_raw(format, raw, net_buf_simple_add(buf, format->size)); -} - -static int scalar_decode(const struct bt_mesh_sensor_format *format, - struct net_buf_simple *buf, struct sensor_value *val) -{ - const struct scalar_repr *repr = format->user_data; - - if (buf->len < format->size) { - return -ENOMEM; - } - - int64_t raw; - int err; - - err = scalar_decode_raw(format, - net_buf_simple_pull_mem(buf, format->size), - &raw); - if (err) { - return err; - } - - int64_t max_value = scalar_max(format); - int64_t min_value = scalar_min(format); - - if (raw < min_value || raw > max_value) { - if (!(repr->flags & (HAS_UNDEFINED | HAS_UNDEFINED_MIN | - HAS_HIGHER_THAN | HAS_INVALID))) { - return -ERANGE; - } - - uint32_t type_max = (repr->flags & SIGNED) ? - BIT64(8 * format->size - 1) - 1 : - BIT64(8 * format->size) - 1; - - if (repr->flags & HAS_UNDEFINED_MIN) { - type_max += 1; - } else if ((repr->flags & HAS_INVALID) && raw == type_max - 1) { - type_max -= 1; - } else if (raw != type_max) { - return -ERANGE; - } - - val->val1 = type_max; - val->val2 = 0; - return 0; - } - - int64_t million = MUL_SCALAR(raw * 1000000LL, repr); - - val->val1 = million / 1000000LL; - val->val2 = million % 1000000LL; - - return 0; -} - -static int boolean_encode(const struct bt_mesh_sensor_format *format, - const struct sensor_value *val, - struct net_buf_simple *buf) -{ - if (net_buf_simple_tailroom(buf) < 1) { - return -ENOMEM; - } - - net_buf_simple_add_u8(buf, !!val->val1); - return 0; -} - -static int boolean_decode(const struct bt_mesh_sensor_format *format, - struct net_buf_simple *buf, struct sensor_value *val) -{ - if (buf->len < 1) { - return -ENOMEM; - } - - uint8_t b = net_buf_simple_pull_u8(buf); - - if (b > 1) { - return -EINVAL; - } - - val->val1 = b; - val->val2 = 0; - return 0; -} - -static int exp_1_1_encode(const struct bt_mesh_sensor_format *format, - const struct sensor_value *val, - struct net_buf_simple *buf) -{ - if (net_buf_simple_tailroom(buf) < 1) { - return -ENOMEM; - } - - if (val->val1 == 0xffffffff && val->val2 == 0) { - net_buf_simple_add_u8(buf, 0xff); // Unknown time - return 0; - } - if (val->val1 == 0xfffffffe && val->val2 == 0) { - net_buf_simple_add_u8(buf, 0xfe); // Total lifetime - return 0; - } - - net_buf_simple_add_u8(buf, - sensor_powtime_encode((val->val1 * 1000ULL) + - (val->val2 / 1000ULL))); - return 0; -} - -static int exp_1_1_decode(const struct bt_mesh_sensor_format *format, - struct net_buf_simple *buf, struct sensor_value *val) -{ - if (buf->len < 1) { - return -ENOMEM; - } - - uint8_t raw = net_buf_simple_pull_u8(buf); - - val->val2 = 0; - if (raw == 0xff) { - val->val1 = 0xffffffff; - } else if (raw == 0xfe) { - val->val1 = 0xfffffffe; - } else { - uint64_t time_us = sensor_powtime_decode_us(raw); - - val->val1 = time_us / USEC_PER_SEC; - val->val2 = time_us % USEC_PER_SEC; - } - return 0; -} - -static int float32_encode(const struct bt_mesh_sensor_format *format, - const struct sensor_value *val, - struct net_buf_simple *buf) -{ - if (net_buf_simple_tailroom(buf) < sizeof(float)) { - return -ENOMEM; - } - - /* IEEE-754 32-bit floating point */ - float fvalue = (float)val->val1 + (float)val->val2 / 1000000L; - - net_buf_simple_add_mem(buf, &fvalue, sizeof(float)); - - return 0; -} - -static int float32_decode(const struct bt_mesh_sensor_format *format, - struct net_buf_simple *buf, struct sensor_value *val) -{ - if (buf->len < sizeof(float)) { - return -ENOMEM; - } - - float fvalue; - - memcpy(&fvalue, net_buf_simple_pull_mem(buf, sizeof(float)), - sizeof(float)); - - val->val1 = (int32_t)fvalue; - val->val2 = ((int64_t)(fvalue * 1000000.0f)) % 1000000L; - return 0; -} -#else /* !defined(CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE) */ static inline int64_t mul_scalar(int64_t val, const struct scalar_repr *repr) { return repr->flags & DIVIDE ? @@ -1249,7 +1030,6 @@ static struct bt_mesh_sensor_format_cb float32_cb = { .to_float = float32_to_float, .from_float = float32_from_float, }; -#endif /* defined(CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE) */ /******************************************************************************* * Sensor Formats @@ -1369,12 +1149,7 @@ FORMAT(time_exp_8) = { #ifdef CONFIG_BT_MESH_SENSOR_LABELS .unit = &bt_mesh_sensor_unit_seconds, #endif -#ifdef CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE - .encode = exp_1_1_encode, - .decode = exp_1_1_decode, -#else .cb = &exp_1_1_cb, -#endif }; /******************************************************************************* @@ -1417,21 +1192,12 @@ FORMAT(energy) = SCALAR_FORMAT(3, /******************************************************************************* * Lighting formats ******************************************************************************/ -#ifdef CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE -FORMAT(chromatic_distance) = SCALAR_FORMAT_MIN_MAX(2, - (SIGNED | - HAS_INVALID | - HAS_UNDEFINED), - unitless, SCALAR(1e-5, 0), - -5000, 5000); -#else FORMAT(chromatic_distance) = SCALAR_FORMAT_MIN_MAX(2, (SIGNED | HAS_INVALID | HAS_UNDEFINED_MAX_MINUS_1), unitless, SCALAR(1e-5, 0), -5000, 5000); -#endif FORMAT(chromaticity_coordinate) = SCALAR_FORMAT(2, UNSIGNED, unitless, @@ -1450,16 +1216,6 @@ FORMAT(luminous_efficacy) = SCALAR_FORMAT_MAX(2, lumen_per_watt, SCALAR(1e-1, 0), 18000); -#ifdef CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE -FORMAT(luminous_energy) = SCALAR_FORMAT(3, - (UNSIGNED | HAS_UNDEFINED), - klmh, - SCALAR(1, 0)); -FORMAT(luminous_exposure) = SCALAR_FORMAT(3, - (UNSIGNED | HAS_UNDEFINED), - klxh, - SCALAR(1, 0)); -#else FORMAT(luminous_energy) = SCALAR_FORMAT(3, (UNSIGNED | HAS_UNDEFINED), lmh, @@ -1468,7 +1224,6 @@ FORMAT(luminous_exposure) = SCALAR_FORMAT(3, (UNSIGNED | HAS_UNDEFINED), lxh, SCALAR(1e3, 0)); -#endif FORMAT(luminous_flux) = SCALAR_FORMAT(2, (UNSIGNED | HAS_UNDEFINED), lumen, @@ -1500,24 +1255,14 @@ FORMAT(cos_of_the_angle) = SCALAR_FORMAT_MIN_MAX(1, SCALAR(1, 0), -100, 100); FORMAT(boolean) = { -#ifdef CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE - .encode = boolean_encode, - .decode = boolean_decode, -#else .cb = &boolean_cb, -#endif .size = 1, #ifdef CONFIG_BT_MESH_SENSOR_LABELS .unit = &bt_mesh_sensor_unit_unitless, #endif }; FORMAT(coefficient) = { -#ifdef CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE - .encode = float32_encode, - .decode = float32_decode, -#else .cb = &float32_cb, -#endif .size = 4, #ifdef CONFIG_BT_MESH_SENSOR_LABELS .unit = &bt_mesh_sensor_unit_unitless, diff --git a/subsys/bluetooth/mesh/shell/shell_ctrl_cli.c b/subsys/bluetooth/mesh/shell/shell_ctrl_cli.c index 2c528cc303b3..529ec634c44c 100644 --- a/subsys/bluetooth/mesh/shell/shell_ctrl_cli.c +++ b/subsys/bluetooth/mesh/shell/shell_ctrl_cli.c @@ -197,7 +197,7 @@ static int cmd_light_onoff_set_unack(const struct shell *shell, size_t argc, cha return light_onoff_set(shell, argc, argv, false); } -static void prop_print(const struct shell *shell, int err, sensor_value_type *rsp) +static void prop_print(const struct shell *shell, int err, struct bt_mesh_sensor_value *rsp) { if (!err) { shell_fprintf(shell, SHELL_NORMAL, "Property value: "); @@ -222,7 +222,7 @@ static int cmd_prop_get(const struct shell *shell, size_t argc, char *argv[]) } struct bt_mesh_light_ctrl_cli *cli = mod->rt->user_data; - sensor_value_type rsp; + struct bt_mesh_sensor_value rsp; err = bt_mesh_light_ctrl_cli_prop_get(cli, NULL, id, &rsp); prop_print(shell, err, &rsp); @@ -247,7 +247,7 @@ static int prop_set(const struct shell *shell, size_t argc, char *argv[], bool a return -ENOENT; } - sensor_value_type set = shell_model_strtosensorval(prop_format, argv[2], &err); + struct bt_mesh_sensor_value set = shell_model_strtosensorval(prop_format, argv[2], &err); if (err) { shell_warn(shell, "Unable to parse input string arg"); @@ -261,7 +261,7 @@ static int prop_set(const struct shell *shell, size_t argc, char *argv[], bool a struct bt_mesh_light_ctrl_cli *cli = mod->rt->user_data; if (acked) { - sensor_value_type rsp; + struct bt_mesh_sensor_value rsp; err = bt_mesh_light_ctrl_cli_prop_set(cli, NULL, id, &set, &rsp); prop_print(shell, err, &rsp); diff --git a/subsys/bluetooth/mesh/shell/shell_sensor_cli.c b/subsys/bluetooth/mesh/shell/shell_sensor_cli.c index 6869437375e2..02773d2e8e13 100644 --- a/subsys/bluetooth/mesh/shell/shell_sensor_cli.c +++ b/subsys/bluetooth/mesh/shell/shell_sensor_cli.c @@ -17,16 +17,8 @@ static void descriptor_print(const struct shell *shell, int err, { if (!err) { shell_print(shell, "{"); -#ifdef CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE - shell_fprintf(shell, SHELL_NORMAL, "\ttolerance: { positive: "); - shell_model_print_sensorval(shell, &rsp->tolerance.positive); - shell_fprintf(shell, SHELL_NORMAL, ", negative: "); - shell_model_print_sensorval(shell, &rsp->tolerance.negative); - shell_print(shell, " }"); -#else shell_print(shell, "\ttolerance: { positive: %d, negative: %d }", rsp->tolerance.positive, rsp->tolerance.negative); -#endif shell_print(shell, "\tsampling type: %d", rsp->sampling_type); shell_print(shell, "\tperiod: %lld", rsp->period); shell_print(shell, "\tupdate interval: %lld", rsp->update_interval); @@ -91,13 +83,6 @@ static void cadence_print(const struct shell *shell, int err, if (!err) { shell_print(shell, "fast period div: %d\nmin interval: %d", rsp->fast_period_div, rsp->min_int); -#ifdef CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE - shell_fprintf(shell, SHELL_NORMAL, "delta threshold: { type: %d, up: ", - rsp->threshold.delta.type); - shell_model_print_sensorval(shell, &rsp->threshold.delta.up); - shell_fprintf(shell, SHELL_NORMAL, ", down: "); - shell_model_print_sensorval(shell, &rsp->threshold.delta.down); -#else int delta_type = rsp->threshold.deltas.up.format == &bt_mesh_sensor_format_percentage_delta_trigger; shell_fprintf(shell, SHELL_NORMAL, "delta threshold: { type: %d, up: ", @@ -105,7 +90,6 @@ static void cadence_print(const struct shell *shell, int err, shell_model_print_sensorval(shell, &rsp->threshold.deltas.up); shell_fprintf(shell, SHELL_NORMAL, ", down: "); shell_model_print_sensorval(shell, &rsp->threshold.deltas.down); -#endif shell_fprintf(shell, SHELL_NORMAL, " }\nfast cadence range: { cadence inside: %d, lower boundary: ", rsp->threshold.range.cadence); @@ -162,12 +146,7 @@ static int cadence_set(const struct shell *shell, size_t argc, char *argv[], boo .fast_period_div = shell_strtoul(argv[2], 0, &err), .min_int = shell_strtoul(argv[3], 0, &err), .threshold = { -#ifdef CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE - .delta = { - .type = shell_strtol(argv[4], 0, &err), -#else .deltas = { -#endif .up = shell_model_strtosensorval(delta_format, argv[5], &err), .down = shell_model_strtosensorval(delta_format, argv[6], &err) }, @@ -253,7 +232,7 @@ static int cmd_settings_get(const struct shell *shell, size_t argc, char *argv[] return err; } -static void values_print(const struct shell *shell, int err, sensor_value_type *values, +static void values_print(const struct shell *shell, int err, struct bt_mesh_sensor_value *values, const struct bt_mesh_sensor_type *sensor_type) { if (!err) { @@ -315,8 +294,8 @@ static int setting_set(const struct shell *shell, size_t argc, char *argv[], boo return -ENOENT; } - sensor_value_type value = shell_model_strtosensorval(setting_type->channels[0].format, - argv[3], &err); + struct bt_mesh_sensor_value value = + shell_model_strtosensorval(setting_type->channels[0].format, argv[3], &err); if (err) { shell_warn(shell, "Unable to parse input string arg"); @@ -391,7 +370,7 @@ static int cmd_get(const struct shell *shell, size_t argc, char *argv[]) return err; } - sensor_value_type rsp[CONFIG_BT_MESH_SENSOR_CHANNELS_MAX]; + struct bt_mesh_sensor_value rsp[CONFIG_BT_MESH_SENSOR_CHANNELS_MAX]; const struct bt_mesh_sensor_type *sensor_type = bt_mesh_sensor_type_get(sensor_id); if (sensor_type == NULL) { @@ -411,13 +390,8 @@ static void series_entry_print(const struct shell *shell, int err, if (!err) { shell_fprintf(shell, SHELL_NORMAL, "["); shell_model_print_sensorval(shell, &entry->column.start); -#ifdef CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE - shell_fprintf(shell, SHELL_NORMAL, " to "); - shell_model_print_sensorval(shell, &entry->column.end); -#else shell_fprintf(shell, SHELL_NORMAL, ", width "); shell_model_print_sensorval(shell, &entry->column.width); -#endif shell_fprintf(shell, SHELL_NORMAL, "]: "); values_print(shell, err, entry->value, sensor_type); } @@ -433,11 +407,6 @@ static int cmd_series_entry_get(const struct shell *shell, size_t argc, char *ar return -ENOENT; } -#ifdef CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE - struct bt_mesh_sensor_column column = { - .start = shell_model_strtosensorval(NULL, argv[2], &err) - }; -#else union bt_mesh_sensor_column_key column; if (sensor_type->channel_count == 3) { @@ -446,7 +415,6 @@ static int cmd_series_entry_get(const struct shell *shell, size_t argc, char *ar } else { column.index = shell_strtoul(argv[2], 0, &err); } -#endif if (err) { shell_warn(shell, "Unable to parse input string arg"); @@ -503,12 +471,6 @@ static int cmd_series_entries_get(const struct shell *shell, size_t argc, char * uint32_t count = CONFIG_BT_MESH_SHELL_SENSOR_CLI_MAX_COLUMNS; if (argc == 4) { -#ifdef CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE - struct bt_mesh_sensor_column range = { - .start = shell_model_strtosensorval(NULL, argv[2], &err), - .end = shell_model_strtosensorval(NULL, argv[3], &err) - }; -#else union bt_mesh_sensor_column_key range_start, range_end; if (sensor_type->channel_count == 3) { @@ -520,26 +482,17 @@ static int cmd_series_entries_get(const struct shell *shell, size_t argc, char * range_start.index = shell_strtoul(argv[2], 0, &err); range_end.index = shell_strtoul(argv[3], 0, &err); } -#endif if (err) { shell_warn(shell, "Unable to parse input string arg"); return err; } -#ifdef CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE - err = bt_mesh_sensor_cli_series_entries_get(cli, NULL, sensor_type, &range, rsp, - &count); - } else if (argc == 2) { - err = bt_mesh_sensor_cli_series_entries_get(cli, NULL, sensor_type, NULL, rsp, - &count); -#else err = bt_mesh_sensor_cli_series_entries_get(cli, NULL, sensor_type, &range_start, &range_end, rsp, &count); } else if (argc == 2) { err = bt_mesh_sensor_cli_series_entries_get(cli, NULL, sensor_type, NULL, NULL, rsp, &count); -#endif } else { return -ENOEXEC; } diff --git a/subsys/bluetooth/mesh/shell/shell_utils.c b/subsys/bluetooth/mesh/shell/shell_utils.c index 140a45fb11f6..6269dd08752e 100644 --- a/subsys/bluetooth/mesh/shell/shell_utils.c +++ b/subsys/bluetooth/mesh/shell/shell_utils.c @@ -19,11 +19,11 @@ struct shell_model_instance { uint8_t elem_idx; }; -sensor_value_type shell_model_strtosensorval( +struct bt_mesh_sensor_value shell_model_strtosensorval( const struct bt_mesh_sensor_format *format, const char *str, int *err) { int temp_err = 0; - sensor_value_type out = { 0 }; + struct bt_mesh_sensor_value out = { 0 }; double val = shell_model_strtodbl(str, &temp_err); @@ -32,37 +32,17 @@ sensor_value_type shell_model_strtosensorval( return out; } -#ifdef CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE - out.val1 = (int)val; - out.val2 = (val - out.val1) * 1000000; -#else temp_err = bt_mesh_sensor_value_from_float(format, val, &out); /* Ignore ERANGE to let the value be clamped to the format range. */ if (temp_err && temp_err != -ERANGE) { *err = temp_err; } -#endif return out; } -void shell_model_print_sensorval(const struct shell *shell, sensor_value_type *value) +void shell_model_print_sensorval(const struct shell *shell, struct bt_mesh_sensor_value *value) { -#ifdef CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE - shell_fprintf(shell, SHELL_NORMAL, "%s%d", (value->val1 < 0 || value->val2 < 0) ? "-" : "", - abs(value->val1)); - if (value->val2) { - int32_t val = abs(value->val2); - int digits = 6; - - while (!(val % 10)) { - val /= 10; - digits--; - } - shell_fprintf(shell, SHELL_NORMAL, ".%0*d", digits, val); - } -#else shell_fprintf(shell, SHELL_NORMAL, "%s", bt_mesh_sensor_ch_str(value)); -#endif } static size_t whitespace_trim(char *out, size_t len, const char *str) diff --git a/subsys/bluetooth/mesh/shell/shell_utils.h b/subsys/bluetooth/mesh/shell/shell_utils.h index 6657c8449193..e5fed197392b 100644 --- a/subsys/bluetooth/mesh/shell/shell_utils.h +++ b/subsys/bluetooth/mesh/shell/shell_utils.h @@ -8,10 +8,10 @@ #include #include "../sensor.h" -sensor_value_type shell_model_strtosensorval(const struct bt_mesh_sensor_format *format, - const char *str, int *err); +struct bt_mesh_sensor_value shell_model_strtosensorval(const struct bt_mesh_sensor_format *format, + const char *str, int *err); -void shell_model_print_sensorval(const struct shell *shell, sensor_value_type *value); +void shell_model_print_sensorval(const struct shell *shell, struct bt_mesh_sensor_value *value); double shell_model_strtodbl(const char *str, int *err); @@ -20,7 +20,7 @@ bool shell_model_first_get(uint16_t id, const struct bt_mesh_model **mod); bool shell_vnd_model_first_get(uint16_t cid, uint16_t id, const struct bt_mesh_model **mod); int shell_model_instance_set(const struct shell *shell, const struct bt_mesh_model **mod, - uint16_t mod_id, uint8_t elem_idx); + uint16_t mod_id, uint8_t elem_idx); int shell_vnd_model_instance_set(const struct shell *shell, const struct bt_mesh_model **mod, uint16_t mod_id, uint16_t cid, uint8_t elem_idx); diff --git a/tests/bluetooth/tester/prj.conf b/tests/bluetooth/tester/prj.conf index 9bcf9d29de1a..65c5d4db53f7 100644 --- a/tests/bluetooth/tester/prj.conf +++ b/tests/bluetooth/tester/prj.conf @@ -82,8 +82,6 @@ CONFIG_BT_MESH_LIGHT_CTRL_SRV_TIME_FADE_STANDBY_AUTO=0 CONFIG_BT_MESH_LIGHT_CTRL_SRV_TIME_FADE_STANDBY_MANUAL=0 CONFIG_BT_MESH_LIGHT_CTRL_SRV_TIME_PROLONG=0 -CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE=n - CONFIG_SETTINGS=y CONFIG_FLASH=y CONFIG_FLASH_MAP=y diff --git a/tests/bluetooth/tester/src/mmdl.c b/tests/bluetooth/tester/src/mmdl.c index 4541f17c8d72..a41efff90d80 100644 --- a/tests/bluetooth/tester/src/mmdl.c +++ b/tests/bluetooth/tester/src/mmdl.c @@ -1566,7 +1566,7 @@ static void sensor_get(uint8_t *data, uint16_t len) struct mesh_sensor_get *cmd = (void *)data; const struct bt_mesh_sensor_type *sensor; struct net_buf_simple *buf = NET_BUF_SIMPLE(BT_MESH_TX_SDU_MAX); - sensor_value_type value[CONFIG_BT_MESH_SENSOR_CHANNELS_MAX]; + struct bt_mesh_sensor_value value[CONFIG_BT_MESH_SENSOR_CHANNELS_MAX]; struct bt_mesh_sensor_data values[5]; uint32_t count = ARRAY_SIZE(values); struct model_data *model_bound; @@ -1885,7 +1885,7 @@ static void sensor_setting_set(uint8_t *data, uint16_t len) struct mesh_sensor_setting_set *cmd = (void *)data; const struct bt_mesh_sensor_type *sensor; const struct bt_mesh_sensor_type *setting; - sensor_value_type value[CONFIG_BT_MESH_SENSOR_CHANNELS_MAX]; + struct bt_mesh_sensor_value value[CONFIG_BT_MESH_SENSOR_CHANNELS_MAX]; struct bt_mesh_sensor_setting_status rsp; struct model_data *model_bound; struct bt_mesh_msg_ctx ctx = { @@ -1929,12 +1929,8 @@ static void sensor_setting_set(uint8_t *data, uint16_t len) } memset(value, 0, sizeof(value)); -#if !defined(CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE) memcpy(&value[0].raw, cmd->data, cmd->len); value[0].format = setting->channels[0].format; -#else - memcpy(value, cmd->data, cmd->len); -#endif if (cmd->ack) { err = bt_mesh_sensor_cli_setting_set(&sensor_cli, &ctx, sensor, @@ -1972,14 +1968,10 @@ static void sensor_column_get(uint8_t *data, uint16_t len) struct net_buf_simple *buf_rsp = NET_BUF_SIMPLE(BT_MESH_SENSOR_MSG_MAXLEN_COLUMN_STATUS); const struct bt_mesh_sensor_type *sensor; -#if !defined(CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE) union bt_mesh_sensor_column_key column; -#else - struct bt_mesh_sensor_column column = { 0 }; -#endif struct bt_mesh_sensor_series_entry rsp; const struct bt_mesh_sensor_format *col_format; - sensor_value_type width; + struct bt_mesh_sensor_value width; struct model_data *model_bound; struct bt_mesh_msg_ctx ctx = { .net_idx = net.net_idx, @@ -2010,17 +2002,9 @@ static void sensor_column_get(uint8_t *data, uint16_t len) col_format = bt_mesh_sensor_column_format_get(sensor); if (col_format == NULL) { -#if !defined(CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE) column.index = cmd->data[0]; -#else - column.start = val_to_sensorval(cmd->data[0]); -#endif } else { -#if !defined(CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE) err = sensor_ch_decode(buf, col_format, &column.sensor_value); -#else - err = sensor_ch_decode(buf, col_format, &column.start); -#endif if (err) { LOG_ERR("err=%d", err); goto fail; @@ -2048,12 +2032,7 @@ static void sensor_column_get(uint8_t *data, uint16_t len) goto fail; } -#if !defined(CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE) width = rsp.column.width; -#else - width.val1 = rsp.column.end.val1 - rsp.column.start.val1; - width.val2 = rsp.column.end.val2 - rsp.column.start.val2; -#endif err = sensor_ch_encode(buf_rsp, col_format, &width); if (err) { @@ -2083,7 +2062,7 @@ static void sensor_series_get(uint8_t *data, uint16_t len) struct net_buf_simple *buf_rsp = NET_BUF_SIMPLE(BT_MESH_TX_SDU_MAX); const struct bt_mesh_sensor_type *sensor; struct bt_mesh_sensor_series_entry rsp[10]; - sensor_value_type width; + struct bt_mesh_sensor_value width; uint32_t count = ARRAY_SIZE(rsp); const struct bt_mesh_sensor_format *col_format; struct model_data *model_bound; @@ -2116,7 +2095,6 @@ static void sensor_series_get(uint8_t *data, uint16_t len) net_buf_simple_init_with_data(buf, (void *)&cmd->data, cmd->len); col_format = bt_mesh_sensor_column_format_get(sensor); -#if !defined(CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE) union bt_mesh_sensor_column_key range_start, range_end; if (col_format != NULL) { @@ -2135,22 +2113,6 @@ static void sensor_series_get(uint8_t *data, uint16_t len) err = bt_mesh_sensor_cli_series_entries_get(&sensor_cli, &ctx, sensor, &range_start, &range_end, rsp, &count); -#else - struct bt_mesh_sensor_column range; - - if (col_format != NULL) { - err = sensor_ch_decode(buf, col_format, &range.end); - if (err) { - goto fail; - } - } else { - range.start.val1 = net_buf_simple_pull_u8(buf); - range.end.val1 = net_buf_simple_pull_u8(buf); - } - - err = bt_mesh_sensor_cli_series_entries_get(&sensor_cli, &ctx, sensor, - &range, rsp, &count); -#endif if (err) { LOG_ERR("err=%d", err); @@ -2173,12 +2135,7 @@ static void sensor_series_get(uint8_t *data, uint16_t len) LOG_ERR("err=%d", err); goto fail; } -#if !defined(CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE) width = rsp[i].column.width; -#else - width.val1 = rsp[i].column.end.val1 - rsp[i].column.start.val1; - width.val2 = rsp[i].column.end.val2 - rsp[i].column.start.val2; -#endif err = sensor_ch_encode(buf_rsp, col_format, &width); if (err) { LOG_ERR("err=%d", err); @@ -3294,7 +3251,7 @@ static void light_lc_property_get(uint8_t *data, uint16_t len) { struct mesh_light_lc_property_get *cmd = (void *)data; struct net_buf_simple *rsp_buf = NET_BUF_SIMPLE(4); - sensor_value_type status; + struct bt_mesh_sensor_value status; const struct bt_mesh_sensor_format *format; enum bt_mesh_light_ctrl_prop id; struct model_data *model_bound; @@ -3354,8 +3311,8 @@ static void light_lc_property_set(uint8_t *data, uint16_t len) struct net_buf_simple *rsp_buf = NET_BUF_SIMPLE(4); const struct bt_mesh_sensor_format *format; enum bt_mesh_light_ctrl_prop id; - sensor_value_type val; - sensor_value_type status; + struct bt_mesh_sensor_value val; + struct bt_mesh_sensor_value status; struct model_data *model_bound; struct bt_mesh_msg_ctx ctx = { .net_idx = net.net_idx, diff --git a/tests/bluetooth/tester/src/model_handler.c b/tests/bluetooth/tester/src/model_handler.c index 1ef0d211c9ae..b45b8239c0c5 100644 --- a/tests/bluetooth/tester/src/model_handler.c +++ b/tests/bluetooth/tester/src/model_handler.c @@ -284,7 +284,7 @@ static void prop_mfr_get(struct bt_mesh_prop_srv *srv, static int sensor_data_get(struct bt_mesh_sensor_srv *srv, struct bt_mesh_sensor *sensor, struct bt_mesh_msg_ctx *ctx, - sensor_value_type *rsp); + struct bt_mesh_sensor_value *rsp); static struct bt_mesh_sensor time_since_presence_detected = { .type = &bt_mesh_sensor_time_since_presence_detected, @@ -303,7 +303,7 @@ static struct bt_mesh_sensor_srv sensor_srv = static int sensor_data_get(struct bt_mesh_sensor_srv *srv, struct bt_mesh_sensor *sensor, struct bt_mesh_msg_ctx *ctx, - sensor_value_type *rsp) + struct bt_mesh_sensor_value *rsp) { int i; @@ -311,16 +311,12 @@ static int sensor_data_get(struct bt_mesh_sensor_srv *srv, if (sensor->type->id != sensors[i]->type->id) { continue; } -#if !defined(CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE) int err = bt_mesh_sensor_value_from_sensor_value(sensor->type->channels[0].format, &values[i], rsp); if (err) { LOG_ERR("Failed to convert sensor value (err: %d)", err); return err; } -#else - memcpy(rsp, &values[i], sizeof(*rsp)); -#endif break; } diff --git a/tests/subsys/bluetooth/mesh/light_ctrl/CMakeLists.txt b/tests/subsys/bluetooth/mesh/light_ctrl/CMakeLists.txt index b37bc2e2d06c..9c792a7f8fc8 100644 --- a/tests/subsys/bluetooth/mesh/light_ctrl/CMakeLists.txt +++ b/tests/subsys/bluetooth/mesh/light_ctrl/CMakeLists.txt @@ -58,8 +58,8 @@ target_compile_options(app -DCONFIG_BT_MESH_LIGHT_CTRL_SRV_REG_LUX_PROLONG=200 -DCONFIG_BT_MESH_LIGHT_CTRL_SRV_REG_LUX_STANDBY=100 -DCONFIG_BT_MESH_LIGHT_CTRL_AMB_LIGHT_LEVEL_TIMEOUT=20 - -DCONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE=1 -DCONFIG_BT_MESH_SENSOR_CHANNELS_MAX=1 + -DCONFIG_BT_MESH_SENSOR_CHANNEL_ENCODED_SIZE_MAX=4 -DCONFIG_BT_MESH_USES_TINYCRYPT ) diff --git a/tests/subsys/bluetooth/mesh/light_ctrl/src/main.c b/tests/subsys/bluetooth/mesh/light_ctrl/src/main.c index 31ea6a87cb15..6260a303a2e9 100644 --- a/tests/subsys/bluetooth/mesh/light_ctrl/src/main.c +++ b/tests/subsys/bluetooth/mesh/light_ctrl/src/main.c @@ -327,28 +327,16 @@ expected_statemachine_cond(bool enabled, light_ctrl_srv.flags); } -static struct sensor_value float_to_sensor_value(float fvalue) -{ - return (struct sensor_value) { - .val1 = (int32_t)(fvalue), - .val2 = ((int64_t)((fvalue) * 1000000.0f)) % 1000000L - }; -} - -static float sensor_to_float(struct sensor_value *val) -{ - return val->val1 + val->val2 / 1000000.0f; -} - static void send_amb_lux_level(float amb) { struct bt_mesh_msg_ctx ctx; - struct sensor_value value; + struct bt_mesh_sensor_value value; NET_BUF_SIMPLE_DEFINE(buf, BT_MESH_SENSOR_OP_STATUS); net_buf_simple_init(&buf, 0); - value = float_to_sensor_value(amb); + zassert_ok( + bt_mesh_sensor_value_from_float(&bt_mesh_sensor_format_illuminance, amb, &value)); zassert_ok(sensor_status_id_encode(&buf, 3, BT_MESH_PROP_ID_PRESENT_AMB_LIGHT_LEVEL)); zassert_ok(sensor_value_encode(&buf, &bt_mesh_sensor_present_amb_light_level, &value)); @@ -589,18 +577,15 @@ static void check_default_cfg(void) zassert_equal(light_ctrl_srv.reg->cfg.accuracy, CONFIG_BT_MESH_LIGHT_CTRL_SRV_REG_ACCURACY, "Invalid default value"); - zassert_mem_equal(&light_ctrl_srv.cfg.lux[LIGHT_CTRL_STATE_STANDBY], - &(struct sensor_value) - { .val1 = CONFIG_BT_MESH_LIGHT_CTRL_SRV_REG_LUX_STANDBY }, - sizeof(struct sensor_value), "Invalid default value"); - zassert_mem_equal(&light_ctrl_srv.cfg.lux[LIGHT_CTRL_STATE_ON], - &(struct sensor_value) - { .val1 = CONFIG_BT_MESH_LIGHT_CTRL_SRV_REG_LUX_ON }, - sizeof(struct sensor_value), "Invalid default value"); - zassert_mem_equal(&light_ctrl_srv.cfg.lux[LIGHT_CTRL_STATE_PROLONG], - &(struct sensor_value) - { .val1 = CONFIG_BT_MESH_LIGHT_CTRL_SRV_REG_LUX_PROLONG }, - sizeof(struct sensor_value), "Invalid default value"); + zassert_equal(light_ctrl_srv.cfg.centilux[LIGHT_CTRL_STATE_STANDBY], + CONFIG_BT_MESH_LIGHT_CTRL_SRV_REG_LUX_STANDBY * 100, + "Invalid default value"); + zassert_equal(light_ctrl_srv.cfg.centilux[LIGHT_CTRL_STATE_ON], + CONFIG_BT_MESH_LIGHT_CTRL_SRV_REG_LUX_ON * 100, + "Invalid default value"); + zassert_equal(light_ctrl_srv.cfg.centilux[LIGHT_CTRL_STATE_PROLONG], + CONFIG_BT_MESH_LIGHT_CTRL_SRV_REG_LUX_PROLONG * 100, + "Invalid default value"); } ZTEST(light_ctrl_test, test_default_cfg) @@ -625,10 +610,10 @@ ZTEST(light_ctrl_test, test_default_cfg) light_ctrl_srv.reg->cfg.kp.down = 9; light_ctrl_srv.reg->cfg.accuracy = 11; - - light_ctrl_srv.cfg.lux[LIGHT_CTRL_STATE_STANDBY] = float_to_sensor_value(13); - light_ctrl_srv.cfg.lux[LIGHT_CTRL_STATE_ON] = float_to_sensor_value(15); - light_ctrl_srv.cfg.lux[LIGHT_CTRL_STATE_PROLONG] = float_to_sensor_value(17); + /*Lux unit resolution is 0.01 */ + light_ctrl_srv.cfg.centilux[LIGHT_CTRL_STATE_STANDBY] = 13 * 100; + light_ctrl_srv.cfg.centilux[LIGHT_CTRL_STATE_ON] = 15 * 100; + light_ctrl_srv.cfg.centilux[LIGHT_CTRL_STATE_PROLONG] = 17 * 100; expect_ctrl_disable(); _bt_mesh_light_ctrl_srv_cb.reset(&mock_ligth_ctrl_model); @@ -975,7 +960,7 @@ ZTEST(light_ctrl_pi_reg_test, test_target_luxlevel_for_pi_regulator) expected_lightness, pi_reg_test_ctx.lightness); /* Set Ambient LuxLevel to target LuxLevel to stop PI Regulator changing lightness value. */ - send_amb_lux_level(sensor_to_float(&light_ctrl_srv.cfg.lux[LIGHT_CTRL_STATE_ON])); + send_amb_lux_level((float)light_ctrl_srv.cfg.centilux[LIGHT_CTRL_STATE_ON] * 0.01f); /* It will take long time to finish the test so that the regulator will drop the last * measured ambient value. Don't let it do that. */ @@ -1000,7 +985,7 @@ ZTEST(light_ctrl_pi_reg_test, test_target_luxlevel_for_pi_regulator) expected_lightness, pi_reg_test_ctx.lightness); /* Set Ambient LuxLevel to target LuxLevel to stop PI Regulator changing lightness value. */ - send_amb_lux_level(sensor_to_float(&light_ctrl_srv.cfg.lux[LIGHT_CTRL_STATE_PROLONG])); + send_amb_lux_level((float)light_ctrl_srv.cfg.centilux[LIGHT_CTRL_STATE_PROLONG] * 0.01f); expected_lightness -= light_ctrl_srv.reg->cfg.kp.up; trigger_pi_reg(1, true); zassert_equal(pi_reg_test_ctx.lightness, expected_lightness, "Expected: %d, got: %d", diff --git a/tests/subsys/bluetooth/mesh/models/prj.conf b/tests/subsys/bluetooth/mesh/models/prj.conf index d0177e025a14..d453f27bb413 100644 --- a/tests/subsys/bluetooth/mesh/models/prj.conf +++ b/tests/subsys/bluetooth/mesh/models/prj.conf @@ -67,4 +67,3 @@ CONFIG_BT_MESH_LIGHT_HSL_CLI=y CONFIG_BT_MESH_SCHEDULER_CLI=y CONFIG_DM_MODULE=y CONFIG_BT_MESH_DM_CLI=y -CONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE=n diff --git a/tests/subsys/bluetooth/mesh/sensor_subsys/CMakeLists.txt b/tests/subsys/bluetooth/mesh/sensor_subsys/CMakeLists.txt index 0692de532f1e..039fcf1ca0f7 100644 --- a/tests/subsys/bluetooth/mesh/sensor_subsys/CMakeLists.txt +++ b/tests/subsys/bluetooth/mesh/sensor_subsys/CMakeLists.txt @@ -1,5 +1,5 @@ # -# Copyright (c) 2021 Nordic Semiconductor ASA +# Copyright (c) 2024 Nordic Semiconductor ASA # # SPDX-License-Identifier: LicenseRef-Nordic-5-Clause # @@ -10,7 +10,6 @@ project(bt_mesh_sensor_subsystem_test) target_include_directories(app PUBLIC ${ZEPHYR_NRF_MODULE_DIR}/subsys/bluetooth/mesh - ${ZEPHYR_BASE}/subsys/bluetooth ) FILE(GLOB app_sources src/*.c) @@ -18,7 +17,6 @@ target_sources(app PRIVATE ${app_sources} ${ZEPHYR_NRF_MODULE_DIR}/subsys/bluetooth/mesh/sensor_types.c ${ZEPHYR_NRF_MODULE_DIR}/subsys/bluetooth/mesh/sensor.c - ${ZEPHYR_BASE}/subsys/bluetooth/mesh/msg.c ) target_compile_options(app @@ -26,7 +24,6 @@ target_compile_options(app -DCONFIG_BT_MESH_MODEL_KEY_COUNT=5 -DCONFIG_BT_MESH_MODEL_GROUP_COUNT=5 -DCONFIG_BT_MESH_SENSOR_ALL_TYPES=1 - -DCONFIG_BT_MESH_SENSOR_USE_LEGACY_SENSOR_VALUE=1 -DCONFIG_BT_MESH_SENSOR_LABELS=1 -DCONFIG_BT_MESH_SENSOR_CHANNELS_MAX=5 -DCONFIG_BT_MESH_SENSOR_CHANNEL_ENCODED_SIZE_MAX=4 diff --git a/tests/subsys/bluetooth/mesh/sensor_subsys/prj.conf b/tests/subsys/bluetooth/mesh/sensor_subsys/prj.conf index db33aa05519f..8415edeff196 100644 --- a/tests/subsys/bluetooth/mesh/sensor_subsys/prj.conf +++ b/tests/subsys/bluetooth/mesh/sensor_subsys/prj.conf @@ -1,5 +1,5 @@ # -# Copyright (c) 2021-2023 Nordic Semiconductor ASA +# Copyright (c) 2024 Nordic Semiconductor ASA # # SPDX-License-Identifier: LicenseRef-Nordic-5-Clause # diff --git a/tests/subsys/bluetooth/mesh/sensor_subsys/src/main.c b/tests/subsys/bluetooth/mesh/sensor_subsys/src/main.c index 0fda9542e4eb..09070b9fc870 100644 --- a/tests/subsys/bluetooth/mesh/sensor_subsys/src/main.c +++ b/tests/subsys/bluetooth/mesh/sensor_subsys/src/main.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Nordic Semiconductor ASA + * Copyright (c) 2024 Nordic Semiconductor ASA * * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause */ @@ -7,2456 +7,972 @@ #include #include #include +#include #include -#include #include -#include -#include // private header from the source folder +#include /* private header from the source folder */ -BUILD_ASSERT(__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__, - "This test is only supported on little endian platforms"); +#ifndef CONFIG_LITTLE_ENDIAN +BUILD_ASSERT(false, "This test is only supported on little endian platforms"); +#endif -/****************** types section **********************************/ - -struct __packed uint24_t { uint32_t v:24; }; - -/****************** types section **********************************/ - -/****************** mock section **********************************/ -/****************** mock section **********************************/ - -/****************** callback section ******************************/ -/****************** callback section ******************************/ - -static int64_t int_pow(int a, int b) -{ - int64_t res = 1; - - for (int i = 0; i < b; i++) { - res *= a; - } - return res; -} - -static int64_t raw_scalar_value_get(int64_t r, int m, int d, int b) -{ - /* Powers < 1 will be clamped to 1, so this works for both - * positive and negative d and b - */ - int64_t div = m * int_pow(10, d) * int_pow(2, b); - int64_t mul = int_pow(10, -d) * int_pow(2, -b); - - return ROUNDED_DIV(r, div) * mul; -} - -static void sensor_type_sanitize(const struct bt_mesh_sensor_type *sensor_type) -{ - zassert_not_null(sensor_type, "NULL sensor type pointer"); - zassert_not_null(sensor_type->channels, "NULL sensor channels pointer"); - zassert_true(sensor_type->channel_count > 0, "Wrong channel number"); - - for (int i = 0; i < sensor_type->channel_count; i++) { - printk("Sensor channel name: %s\n", sensor_type->channels[i].name); - } -} - -static void voltage_helper(uint16_t test_value, struct sensor_value *in_value, uint16_t *expected) -{ - in_value->val1 = test_value; - in_value->val2 = 0; - *expected = test_value > 1022 ? - test_value == UINT16_MAX ? UINT16_MAX - : raw_scalar_value_get(1022, 1, 0, -6) - : raw_scalar_value_get(test_value, 1, 0, -6); -} - -static void encoding_checking_proceed(const struct bt_mesh_sensor_type *sensor_type, - const struct sensor_value *value, - const void *expected, - size_t size) -{ - int err; - - BT_MESH_MODEL_BUF_DEFINE(buf, BT_MESH_SENSOR_OP_SETTING_SET, - BT_MESH_SENSOR_MSG_MAXLEN_SETTING_SET); - bt_mesh_model_msg_init(&buf, BT_MESH_SENSOR_OP_SETTING_SET); - - err = sensor_value_encode(&buf, sensor_type, value); - zassert_ok(err, "Encoding failed with error code: %i", err); - - zassert_mem_equal(&buf.data[1], expected, size, - "Encoded value is not equal expected"); -} - -static void decoding_checking_proceed_with_tolerance( - const struct bt_mesh_sensor_type *sensor_type, - const void *value, - size_t size, - const struct sensor_value *expected, - double tolerance) -{ - struct sensor_value out_value[CONFIG_BT_MESH_SENSOR_CHANNELS_MAX] = {}; - int err; - - BT_MESH_MODEL_BUF_DEFINE(buf, BT_MESH_SENSOR_OP_SETTING_SET, - BT_MESH_SENSOR_MSG_MAXLEN_SETTING_SET); - bt_mesh_model_msg_init(&buf, BT_MESH_SENSOR_OP_SETTING_SET); - - (void)net_buf_simple_add_mem(&buf, value, size); - (void)net_buf_simple_pull_u8(&buf); - - err = sensor_value_decode(&buf, sensor_type, out_value); - zassert_ok(err, "Decoding failed with error code: %i", err); - - for (int i = 0; i < sensor_type->channel_count; i++) { - if (tolerance == 0.0 || (expected[i].val1 == 0 && expected[i].val2 == 0)) { - zassert_equal(expected[i].val1, out_value[i].val1, - "Channel: %i - encoded value val1: %i is not equal decoded: %i", - i, expected[i].val1, out_value[i].val1); - zassert_equal(expected[i].val2, out_value[i].val2, - "Channel: %i - encoded value val2: %i is not equal decoded: %i", - i, expected[i].val2, out_value[i].val2); - } else { - double in_d = expected[i].val1 + expected[i].val2 * 0.000001; - double out_d = out_value[i].val1 + out_value[i].val2 * 0.000001; - double ratio = out_d / in_d; - - zassert_within(ratio, 1.0, tolerance, - "Channel: %i - decoded value %i.%i is more than %f away " - "from encoded value %i.%i, and is outside tolerance.", - i, out_value[i].val1, out_value[i].val2, - tolerance, expected[i].val1, expected[i].val2); - } - } -} - -static void decoding_checking_proceed(const struct bt_mesh_sensor_type *sensor_type, - const void *value, - size_t size, - const struct sensor_value *expected) -{ - decoding_checking_proceed_with_tolerance(sensor_type, value, size, expected, 0.0); -} - -static void invalid_encoding_checking_proceed(const struct bt_mesh_sensor_type *sensor_type, - const struct sensor_value *value) -{ - int err; - - BT_MESH_MODEL_BUF_DEFINE(msg, BT_MESH_SENSOR_OP_SETTING_SET, - BT_MESH_SENSOR_MSG_MAXLEN_SETTING_SET); - bt_mesh_model_msg_init(&msg, BT_MESH_SENSOR_OP_SETTING_SET); - - err = sensor_value_encode(&msg, sensor_type, value); - zassert_equal(err, -ERANGE, "Invalid error code when encoding: %i", err); -} - -static void invalid_decoding_checking_proceed(const struct bt_mesh_sensor_type *sensor_type, - const void *value, - size_t size) -{ - struct sensor_value out_value[CONFIG_BT_MESH_SENSOR_CHANNELS_MAX] = {}; - int err; - - BT_MESH_MODEL_BUF_DEFINE(buf, BT_MESH_SENSOR_OP_SETTING_SET, - BT_MESH_SENSOR_MSG_MAXLEN_SETTING_SET); - bt_mesh_model_msg_init(&buf, BT_MESH_SENSOR_OP_SETTING_SET); - - (void)net_buf_simple_add_mem(&buf, value, size); - (void)net_buf_simple_pull_u8(&buf); - - err = sensor_value_decode(&buf, sensor_type, out_value); - zassert_equal(err, -ERANGE, "Invalid error code when decoding: %i", err); -} - -static void percentage8_check(const struct bt_mesh_sensor_type *sensor_type) -{ - struct sensor_value in_value = {0}; - uint8_t test_vector[] = {0, 25, 50, 75, 100, 0xFF}; - - for (int i = 0; i < ARRAY_SIZE(test_vector); i++) { - in_value.val1 = test_vector[i]; - - uint8_t expected = test_vector[i] == 0xFF ? - 0xFF : raw_scalar_value_get(test_vector[i], 1, 0, -1); - - encoding_checking_proceed(sensor_type, &in_value, &expected, 1); - decoding_checking_proceed(sensor_type, &expected, 1, &in_value); - } -} - -static void count16_check(const struct bt_mesh_sensor_type *sensor_type) -{ - struct sensor_value in_value = {0}; - uint16_t test_vector[] = {0, 0x000F, 0x00FF, 0x0FFF, 0xFFFF}; - - for (int i = 0; i < ARRAY_SIZE(test_vector); i++) { - in_value.val1 = test_vector[i]; - - uint16_t expected = raw_scalar_value_get(test_vector[i], 1, 0, 0); - - encoding_checking_proceed(sensor_type, &in_value, &expected, 2); - decoding_checking_proceed(sensor_type, &expected, 2, &in_value); - } -} - -static void boolean_check(const struct bt_mesh_sensor_type *sensor_type) -{ - struct sensor_value in_value = {0}; - uint8_t test_vector[] = {0, 1}; - - for (int i = 0; i < ARRAY_SIZE(test_vector); i++) { - in_value.val1 = test_vector[i]; - - uint8_t expected = test_vector[i]; - - encoding_checking_proceed(sensor_type, &in_value, &expected, 1); - decoding_checking_proceed(sensor_type, &expected, 1, &in_value); - } -} - -static void time_second16_check(const struct bt_mesh_sensor_type *sensor_type) -{ - struct sensor_value in_value = {0}; - uint16_t test_vector[] = {0, 0x000F, 0x00FF, 0x0FFF, 0xFFFF}; - - for (int i = 0; i < ARRAY_SIZE(test_vector); i++) { - in_value.val1 = test_vector[i]; - - uint16_t expected = raw_scalar_value_get(test_vector[i], 1, 0, 0); - - encoding_checking_proceed(sensor_type, &in_value, &expected, 2); - decoding_checking_proceed(sensor_type, &expected, 2, &in_value); - } -} - -static void time_millisecond24_check(const struct bt_mesh_sensor_type *sensor_type) -{ - struct { - struct sensor_value val; - uint32_t expected; - } test_vector[] = { - { { 0, 0 }, 0 }, - { { 0, 1000 }, 1 }, - { { 1, 0 }, 1000 }, - { { 123, 456000 }, 123456 }, - { { 16777, 214000 }, 16777214 }, - { { 0xFFFFFF, 0 }, 0xFFFFFF } - }; - - for (int i = 0; i < ARRAY_SIZE(test_vector); i++) { - encoding_checking_proceed( - sensor_type, &test_vector[i].val, &test_vector[i].expected, 3); - decoding_checking_proceed( - sensor_type, &test_vector[i].expected, 3, &test_vector[i].val); - } -} - -static void humidity_check(const struct bt_mesh_sensor_type *sensor_type) -{ - struct sensor_value in_value = {0}; - uint16_t test_vector[] = {0, 1000, 5000, 10000, UINT16_MAX}; - - for (int i = 0; i < ARRAY_SIZE(test_vector); i++) { - if (test_vector[i] == UINT16_MAX) { - in_value.val1 = UINT16_MAX; - in_value.val2 = 0; - } else { - in_value.val1 = test_vector[i] / 100; - in_value.val2 = test_vector[i] % 100 * 10000; - } - - encoding_checking_proceed(sensor_type, &in_value, &test_vector[i], 2); - decoding_checking_proceed(sensor_type, &test_vector[i], 2, &in_value); - } - - /* Test invalid range. */ - uint16_t invalid_test_vector = 10001; - - in_value.val1 = invalid_test_vector / 100; - in_value.val2 = invalid_test_vector % 100 * 10000; - invalid_encoding_checking_proceed(sensor_type, &in_value); -} - -static void temp8_signed_celsius_check(const struct bt_mesh_sensor_type *sensor_type) -{ - struct sensor_value in_value = {0}; - int8_t test_vector[] = {-128, -50, 0, 50, 127}; - - for (int i = 0; i < ARRAY_SIZE(test_vector); i++) { - in_value.val1 = test_vector[i]; - - encoding_checking_proceed(sensor_type, &in_value, &test_vector[i], 1); - decoding_checking_proceed(sensor_type, &test_vector[i], 1, &in_value); - } -} - -static void plane_angle_check(const struct bt_mesh_sensor_type *sensor_type) -{ - struct sensor_value in_value = {0}; - uint16_t test_vector[] = {0, 1000, 5000, 10000, 35999}; - - for (int i = 0; i < ARRAY_SIZE(test_vector); i++) { - in_value.val1 = test_vector[i] / 100; - in_value.val2 = test_vector[i] % 100 * 10000; - - encoding_checking_proceed(sensor_type, &in_value, &test_vector[i], 2); - decoding_checking_proceed(sensor_type, &test_vector[i], 2, &in_value); - } - - /* Test invalid range. */ - uint16_t invalid_test_vector = 36000; - - in_value.val1 = invalid_test_vector / 100; - in_value.val2 = invalid_test_vector % 100 * 10000; - invalid_encoding_checking_proceed(sensor_type, &in_value); -} - -static void wind_speed_check(const struct bt_mesh_sensor_type *sensor_type) -{ - struct sensor_value in_value = {0}; - uint16_t test_vector[] = {0, 1000, 5432, 12345, UINT16_MAX}; - - for (int i = 0; i < ARRAY_SIZE(test_vector); i++) { - in_value.val1 = test_vector[i] / 100; - in_value.val2 = test_vector[i] % 100 * 10000; - - encoding_checking_proceed(sensor_type, &in_value, &test_vector[i], 2); - decoding_checking_proceed(sensor_type, &test_vector[i], 2, &in_value); - } -} - -static void pressure_check(const struct bt_mesh_sensor_type *sensor_type) -{ - struct sensor_value in_value = {0}; - uint32_t test_vector[] = {0, UINT32_MAX / 2, UINT32_MAX}; - - for (int i = 0; i < ARRAY_SIZE(test_vector); i++) { - in_value.val1 = test_vector[i] / 10; - in_value.val2 = test_vector[i] % 10 * 100000; - - encoding_checking_proceed(sensor_type, &in_value, &test_vector[i], 4); - decoding_checking_proceed(sensor_type, &test_vector[i], 4, &in_value); - } -} - -static void gust_factor_check(const struct bt_mesh_sensor_type *sensor_type) -{ - struct sensor_value in_value = {0}; - uint8_t test_vector[] = {0, 25, 50, 75, 100, UINT8_MAX}; - - for (int i = 0; i < ARRAY_SIZE(test_vector); i++) { - in_value.val1 = test_vector[i] / 10; - in_value.val2 = test_vector[i] % 10 * 100000; - - encoding_checking_proceed(sensor_type, &in_value, &test_vector[i], 1); - decoding_checking_proceed(sensor_type, &test_vector[i], 1, &in_value); - } -} - -static void concentration_check(const struct bt_mesh_sensor_type *sensor_type) -{ - struct sensor_value in_value = {0}; - uint32_t test_vector[] = {0, 1000, - UINT16_MAX - 2, UINT16_MAX - 1, UINT16_MAX, UINT16_MAX + 1}; - - for (int i = 0; i < ARRAY_SIZE(test_vector); i++) { - in_value.val1 = test_vector[i]; - - uint16_t expected = test_vector[i] > 65533 ? - test_vector[i] == UINT16_MAX ? - UINT16_MAX : UINT16_MAX - 1 : test_vector[i]; - - encoding_checking_proceed(sensor_type, &in_value, &expected, 2); - in_value.val1 = expected; - decoding_checking_proceed(sensor_type, &expected, 2, &in_value); - } -} - -static void noise_check(const struct bt_mesh_sensor_type *sensor_type) -{ - struct sensor_value in_value = {0}; - uint16_t test_vector[] = {0, 50, - UINT8_MAX - 2, UINT8_MAX - 1, UINT8_MAX, UINT8_MAX + 1}; - - for (int i = 0; i < ARRAY_SIZE(test_vector); i++) { - in_value.val1 = test_vector[i]; - - uint8_t expected = test_vector[i] > 253 ? - test_vector[i] == UINT8_MAX ? - UINT8_MAX : UINT8_MAX - 1 : test_vector[i]; - - encoding_checking_proceed(sensor_type, &in_value, &expected, 1); - in_value.val1 = expected; - decoding_checking_proceed(sensor_type, &expected, 1, &in_value); - } -} - -static void pollen_concentration_check(const struct bt_mesh_sensor_type *sensor_type) -{ - struct sensor_value in_value = {0}; - uint32_t test_vector[] = {0, 0xFF, 0xFFFF, 0xFFFFFF}; - - for (int i = 0; i < ARRAY_SIZE(test_vector); i++) { - in_value.val1 = test_vector[i]; - - encoding_checking_proceed(sensor_type, &in_value, &test_vector[i], 3); - decoding_checking_proceed(sensor_type, &test_vector[i], 3, &in_value); - } -} - -static void rainfall_check(const struct bt_mesh_sensor_type *sensor_type) -{ - struct sensor_value in_value = {0}; - uint16_t test_vector[] = {0, 1000, 15000, 30000, UINT16_MAX}; - - for (int i = 0; i < ARRAY_SIZE(test_vector); i++) { - in_value.val1 = test_vector[i] / 1000; - in_value.val2 = test_vector[i] % 1000 * 1000; - - encoding_checking_proceed(sensor_type, &in_value, &test_vector[i], 2); - decoding_checking_proceed(sensor_type, &test_vector[i], 2, &in_value); - } -} - -static void uv_index_check(const struct bt_mesh_sensor_type *sensor_type) -{ - struct sensor_value in_value = {0}; - uint8_t test_vector[] = {0, 25, 50, 200, UINT8_MAX}; - - for (int i = 0; i < ARRAY_SIZE(test_vector); i++) { - in_value.val1 = test_vector[i]; - - encoding_checking_proceed(sensor_type, &in_value, &test_vector[i], 1); - decoding_checking_proceed(sensor_type, &test_vector[i], 1, &in_value); - } -} - -static void flux_density_check(const struct bt_mesh_sensor_type *sensor_type) -{ - struct sensor_value in_value[sensor_type->channel_count]; - int16_t test_vector[] = {INT16_MIN, -30000, -15000, 0, 15000, 30000, INT16_MAX}; - - memset(in_value, 0, sizeof(in_value)); - - for (int i = 0; i < ARRAY_SIZE(test_vector); i++) { - int16_t expected[sensor_type->channel_count]; - - for (int j = 0; j < sensor_type->channel_count; j++) { - in_value[j].val1 = test_vector[i] / 10; - in_value[j].val2 = test_vector[i] % 10 * 100000; - expected[j] = test_vector[i]; - } - - encoding_checking_proceed(sensor_type, in_value, expected, sizeof(expected)); - decoding_checking_proceed(sensor_type, expected, sizeof(expected), in_value); - } -} - -static void chromaticity_coordinates_check(const struct bt_mesh_sensor_type *sensor_type) -{ - struct { - struct sensor_value represented[2]; - uint16_t raw[2]; - } test_vector[] = { - {{{0, 250000}, {0, 750000}}, {16384, 49152}}, - {{{0, 500000}, {0, 625000}}, {32768, 40960}}, - {{{0, 875000}, {0, 125000}}, {57344, 8192}}, - }; - - for (int i = 0; i < ARRAY_SIZE(test_vector); i++) { - encoding_checking_proceed(sensor_type, &test_vector[i].represented[0], - &test_vector[i].raw[0], 4); - decoding_checking_proceed(sensor_type, &test_vector[i].raw[0], 4, - &test_vector[i].represented[0]); - } - - /* Test invalid range on encoding. */ - struct sensor_value invalid_test_vector[][2] = { - {{0, -100000}, {0, 250000}}, - {{1, 0}, {0, 250000}}, - }; - - for (int i = 0; i < ARRAY_SIZE(invalid_test_vector); i++) { - invalid_encoding_checking_proceed(sensor_type, invalid_test_vector[i]); - } -} - -static void illuminance_check(const struct bt_mesh_sensor_type *sensor_type) -{ - struct { - struct sensor_value represented; - uint32_t raw; - } test_vector[] = { - {{0, 0}, 0}, - {{83886, 70000}, 0x7FFFFF}, - {{3000, 0}, 0x493E0}, - {{10000, 500000}, 0xF4272}, - {{167772, 140000}, 0xFFFFFE}, - {{0xFFFFFF, 0}, 0xFFFFFF}, - }; - - for (int i = 0; i < ARRAY_SIZE(test_vector); i++) { - encoding_checking_proceed(sensor_type, &test_vector[i].represented, - &test_vector[i].raw, 3); - decoding_checking_proceed(sensor_type, &test_vector[i].raw, 3, - &test_vector[i].represented); - } - - /* Test invalid range on encoding. */ - struct sensor_value invalid_test_vector[] = { - {-1, 0}, - {167772, 150000}, - }; - - for (int i = 0; i < ARRAY_SIZE(invalid_test_vector); i++) { - invalid_encoding_checking_proceed(sensor_type, &invalid_test_vector[i]); - } -} - -static void correlated_color_temp_check(const struct bt_mesh_sensor_type *sensor_type) -{ - struct { - struct sensor_value represented; - uint16_t raw; - } test_vector[] = { - {{800, 0}, 800}, - {{1234, 0}, 1234}, - {{65534, 0}, 65534}, - {{65535, 0}, 65535}, - }; - - for (int i = 0; i < ARRAY_SIZE(test_vector); i++) { - encoding_checking_proceed(sensor_type, &test_vector[i].represented, - &test_vector[i].raw, 2); - decoding_checking_proceed(sensor_type, &test_vector[i].raw, 2, - &test_vector[i].represented); - } - - /* Test invalid range on encoding. */ - struct sensor_value invalid_encoding_test_vector[] = { - {0, 0}, - {799, 0}, - }; - - for (int i = 0; i < ARRAY_SIZE(invalid_encoding_test_vector); i++) { - invalid_encoding_checking_proceed(sensor_type, &invalid_encoding_test_vector[i]); - } - - /* Test invalid range on decoding. */ - uint16_t invalid_decoding_test_vector[] = { - 0, - 799, - }; - - for (int i = 0; i < ARRAY_SIZE(invalid_decoding_test_vector); i++) { - invalid_decoding_checking_proceed(sensor_type, &invalid_decoding_test_vector[i], 2); - } -} - -static void luminous_flux_check(const struct bt_mesh_sensor_type *sensor_type) -{ - struct { - struct sensor_value represented; - uint16_t raw; - } test_vector[] = { - {{0, 0}, 0}, - {{800, 0}, 800}, - {{1234, 0}, 1234}, - {{65534, 0}, 65534}, - {{65535, 0}, 65535}, - }; - - for (int i = 0; i < ARRAY_SIZE(test_vector); i++) { - encoding_checking_proceed(sensor_type, &test_vector[i].represented, - &test_vector[i].raw, 2); - decoding_checking_proceed(sensor_type, &test_vector[i].raw, 2, - &test_vector[i].represented); - } - - /* Test invalid range. */ - struct sensor_value invalid_encoding_test_vector[] = { - {-1, 0}, - {65536, 0}, - }; - - for (int i = 0; i < ARRAY_SIZE(invalid_encoding_test_vector); i++) { - invalid_encoding_checking_proceed(sensor_type, &invalid_encoding_test_vector[i]); - } -} - -static void luminous_flux_range_check(const struct bt_mesh_sensor_type *sensor_type) -{ - struct { - struct sensor_value represented[2]; - uint16_t raw[2]; - } test_vector[] = { - {{{0, 0}, {1, 0}}, {0, 1}}, - {{{800, 0}, {400, 0}}, {800, 400}}, - {{{1234, 0}, {5678, 0}}, {1234, 5678}}, - {{{65534, 0}, {65535, 0}}, {65534, 65535}}, - }; - - for (int i = 0; i < ARRAY_SIZE(test_vector); i++) { - encoding_checking_proceed(sensor_type, &test_vector[i].represented[0], - &test_vector[i].raw[0], 4); - decoding_checking_proceed(sensor_type, &test_vector[i].raw[0], 4, - &test_vector[i].represented[0]); - } - - /* Test invalid range. */ - struct sensor_value invalid_encoding_test_vector[][2] = { - {{-1, 0}, {0, 0}}, - {{65536, 0}, {65534, 0}}, - }; - - for (int i = 0; i < ARRAY_SIZE(invalid_encoding_test_vector); i++) { - invalid_encoding_checking_proceed(sensor_type, &invalid_encoding_test_vector[i][0]); - } -} - -static void chromatic_distance_check(const struct bt_mesh_sensor_type *sensor_type) -{ - struct { - struct sensor_value represented; - uint32_t raw; - } test_vector[] = { - {{0, -50000}, -5000}, - {{0, 0}, 0}, - {{0, -10}, -1}, - {{0, 10}, 1}, - {{0, 50000}, 5000}, - {{0x7FFF, 0}, 0x7FFF}, - {{0x7FFE, 0}, 0x7FFE}, - }; - - for (int i = 0; i < ARRAY_SIZE(test_vector); i++) { - encoding_checking_proceed(sensor_type, &test_vector[i].represented, - &test_vector[i].raw, 2); - decoding_checking_proceed(sensor_type, &test_vector[i].raw, 2, - &test_vector[i].represented); - } - - /* Test invalid range on encoding. */ - struct sensor_value invalid_encoding_test_vector[] = { - {0, -60000}, - {0, 60000}, - }; - - for (int i = 0; i < ARRAY_SIZE(invalid_encoding_test_vector); i++) { - invalid_encoding_checking_proceed(sensor_type, &invalid_encoding_test_vector[i]); - } - - /* Test invalid range on decoding. */ - uint16_t invalid_decoding_test_vector[] = { - 5001, - -5001, - }; - - for (int i = 0; i < ARRAY_SIZE(invalid_decoding_test_vector); i++) { - invalid_decoding_checking_proceed(sensor_type, &invalid_decoding_test_vector[i], 2); - } -} - -static void percentage8_illuminance_check(const struct bt_mesh_sensor_type *sensor_type) -{ - struct { - struct sensor_value represented[3]; - struct __packed { - uint8_t p; - struct uint24_t i[2]; - } raw; - } test_vector[] = { - {{{1, 0}, {0, 0}, {83886, 70000}}, {2, {{0}, {0x7FFFFF}}}}, - {{{25, 0}, {10000, 500000}, {3000, 0}}, {50, {{0xF4272}, {0x493E0}}}}, - }; - - for (int i = 0; i < ARRAY_SIZE(test_vector); i++) { - encoding_checking_proceed(sensor_type, &test_vector[i].represented[0], - &test_vector[i].raw, 7); - decoding_checking_proceed(sensor_type, &test_vector[i].raw, 7, - &test_vector[i].represented[0]); - } - - /* Test invalid range on encoding. */ - struct sensor_value invalid_encoding_test_vector[][3] = { - {{256, 0}, {0x1000000, 0}, {83886, 70000}}, - {{-1, 0}, {0, 0}, {-1, 0}}, - }; - - for (int i = 0; i < ARRAY_SIZE(invalid_encoding_test_vector); i++) { - invalid_encoding_checking_proceed(sensor_type, &invalid_encoding_test_vector[i][0]); - } - - /* Test invalid range on decoding. */ - struct __packed { - uint8_t p; - struct uint24_t i[2]; - } invalid_decoding_test_vector[] = { - {201, {{0x1FFFFF}, {0x7FFFFF}}}, - {253, {{0}, {0xFF}}}, - }; - - for (int i = 0; i < ARRAY_SIZE(invalid_decoding_test_vector); i++) { - invalid_decoding_checking_proceed(sensor_type, &invalid_decoding_test_vector[i], 7); - } -} - -static void time_hour_24_check(const struct bt_mesh_sensor_type *sensor_type) -{ - struct { - struct sensor_value represented; - uint32_t raw; - } test_vector[] = { - {{0, 0}, 0}, - {{1000, 0}, 1000}, - {{12345678, 0}, 12345678}, - {{16777214, 0}, 16777214}, - {{0xFFFFFF, 0}, 0xFFFFFF}, - }; - - for (int i = 0; i < ARRAY_SIZE(test_vector); i++) { - encoding_checking_proceed(sensor_type, &test_vector[i].represented, - &test_vector[i].raw, 3); - decoding_checking_proceed(sensor_type, &test_vector[i].raw, 3, - &test_vector[i].represented); - } - - /* Test invalid range on encoding. */ - struct sensor_value invalid_encoding_test_vector[] = { - {-1, 0}, - {0x1000000, 0}, - }; - - for (int i = 0; i < ARRAY_SIZE(invalid_encoding_test_vector); i++) { - invalid_encoding_checking_proceed(sensor_type, &invalid_encoding_test_vector[i]); - } -} - -static void luminous_efficacy_check(const struct bt_mesh_sensor_type *sensor_type) -{ - struct { - struct sensor_value represented; - uint16_t raw; - } test_vector[] = { - {{0, 0}, 0}, - {{450, 500000}, 4505}, - {{900, 0}, 9000}, - {{1300, 200000}, 13002}, - {{1800, 0}, 18000}, - {{0xFFFF, 0}, 0xFFFF}, - }; - - for (int i = 0; i < ARRAY_SIZE(test_vector); i++) { - encoding_checking_proceed(sensor_type, &test_vector[i].represented, - &test_vector[i].raw, 2); - decoding_checking_proceed(sensor_type, &test_vector[i].raw, 2, - &test_vector[i].represented); - } - - /* Test invalid range on encoding. */ - struct sensor_value invalid_encoding_test_vector[] = { - {1800, 100000}, - {0xFFFE, 0}, - }; - - for (int i = 0; i < ARRAY_SIZE(invalid_encoding_test_vector); i++) { - invalid_encoding_checking_proceed(sensor_type, &invalid_encoding_test_vector[i]); - } - - /* Test invalid range on decoding. */ - uint32_t invalid_decoding_test_vector[] = { - 18001, - 0xFFFE, - }; - - for (int i = 0; i < ARRAY_SIZE(invalid_decoding_test_vector); i++) { - invalid_decoding_checking_proceed(sensor_type, &invalid_decoding_test_vector[i], 2); - } -} - -static void luminous_energy_check(const struct bt_mesh_sensor_type *sensor_type) -{ - struct { - struct sensor_value represented; - uint32_t raw; - } test_vector[] = { - {{0, 0}, 0}, - {{54321, 0}, 54321}, - {{8765432, 0}, 8765432}, - {{16777214, 0}, 16777214}, - {{0xFFFFFF, 0}, 0xFFFFFF}, - }; - - for (int i = 0; i < ARRAY_SIZE(test_vector); i++) { - encoding_checking_proceed(sensor_type, &test_vector[i].represented, - &test_vector[i].raw, 3); - decoding_checking_proceed(sensor_type, &test_vector[i].raw, 3, - &test_vector[i].represented); - } - - /* Test invalid range on encoding. */ - struct sensor_value invalid_encoding_test_vector[] = { - {-1, 0}, - {0x1000000, 0}, - }; - - for (int i = 0; i < ARRAY_SIZE(invalid_encoding_test_vector); i++) { - invalid_encoding_checking_proceed(sensor_type, &invalid_encoding_test_vector[i]); - } -} - -static void luminous_exposure_check(const struct bt_mesh_sensor_type *sensor_type) -{ - struct { - struct sensor_value represented; - uint32_t raw; - } test_vector[] = { - {{0, 0}, 0}, - {{54321, 0}, 54321}, - {{8765432, 0}, 8765432}, - {{16777214, 0}, 16777214}, - {{0xFFFFFF, 0}, 0xFFFFFF}, - }; - - for (int i = 0; i < ARRAY_SIZE(test_vector); i++) { - encoding_checking_proceed(sensor_type, &test_vector[i].represented, - &test_vector[i].raw, 3); - decoding_checking_proceed(sensor_type, &test_vector[i].raw, 3, - &test_vector[i].represented); - } - - /* Test invalid range on encoding. */ - struct sensor_value invalid_encoding_test_vector[] = { - {-1, 0}, - {0x1000000, 0}, - }; - - for (int i = 0; i < ARRAY_SIZE(invalid_encoding_test_vector); i++) { - invalid_encoding_checking_proceed(sensor_type, &invalid_encoding_test_vector[i]); - } -} - -static void temp8_in_period_of_day_check(const struct bt_mesh_sensor_type *sensor_type) -{ - struct sensor_value in_value[sensor_type->channel_count]; - int8_t test_vector_temp[] = {-64, -32, 0, 32, 53, 0x7F}; - uint8_t test_vector_time[] = {0, 5, 10, 17, 23, 0xff}; - uint8_t expected[3]; - - memset(in_value, 0, sizeof(in_value)); - - for (int i = 0; i < ARRAY_SIZE(test_vector_temp); i++) { - in_value[0].val1 = test_vector_temp[i]; - in_value[1].val1 = test_vector_time[i]; - in_value[2].val1 = test_vector_time[i]; - - expected[0] = test_vector_temp[i] == 0x7F ? - 0x7F : raw_scalar_value_get(test_vector_temp[i], 1, 0, -1); - expected[1] = test_vector_time[i] == 0xFF ? - 0xFF : raw_scalar_value_get(test_vector_time[i], 1, -1, 0); - expected[2] = expected[1]; - - encoding_checking_proceed(sensor_type, in_value, expected, 3 * sizeof(uint8_t)); - decoding_checking_proceed(sensor_type, expected, 3*sizeof(uint8_t), in_value); - } -} - -static void temp8_statistics_check(const struct bt_mesh_sensor_type *sensor_type) -{ - struct sensor_value in_value[sensor_type->channel_count]; - int8_t test_vector[] = {-64, -32, 0, 32, 63, 0x7F}; - - /* Time exponential, represented = 1.1^(N-64), where N is raw value. - * Expected raw values precomputed. (0, 0xFE and 0xFF map - * to themselves as they represent 0 seconds, total device lifetime - * and unknown value, respectively) - */ - int32_t test_vector_time[] = {0, 1, 2999, 66560640, 0xFFFFFFFE, - 0xFFFFFFFF}; - uint8_t expected_time[] = {0, 64, 148, 253, 0xFE, 0xFF}; - uint8_t expected[5]; - - memset(in_value, 0, sizeof(in_value)); - - for (int i = 0; i < ARRAY_SIZE(test_vector); i++) { - for (int j = 0; j < (sensor_type->channel_count - 1); j++) { - in_value[j].val1 = test_vector[i]; - } - in_value[sensor_type->channel_count - 1].val1 = - test_vector_time[i]; - int8_t expected_temp = test_vector[i] == 0x7F ? - 0x7F : raw_scalar_value_get(test_vector[i], 1, 0, -1); - expected[0] = expected_temp; - expected[1] = expected_temp; - expected[2] = expected_temp; - expected[3] = expected_temp; - expected[4] = expected_time[i]; - - encoding_checking_proceed(sensor_type, in_value, expected, 5 * sizeof(uint8_t)); - decoding_checking_proceed_with_tolerance(sensor_type, expected, - 5 * sizeof(uint8_t), in_value, 0.001); - } -} - -static void temp8_check(const struct bt_mesh_sensor_type *sensor_type) -{ - struct sensor_value in_value = {0}; - int8_t test_vector[] = {-64, -32, 0, 32, 63, 0x7F}; - - for (int i = 0; i < ARRAY_SIZE(test_vector); i++) { - in_value.val1 = test_vector[i]; - - int8_t expected = test_vector[i] == 0x7F ? - 0x7F : raw_scalar_value_get(test_vector[i], 1, 0, -1); - - encoding_checking_proceed(sensor_type, &in_value, &expected, sizeof(uint8_t)); - decoding_checking_proceed(sensor_type, &expected, sizeof(uint8_t), &in_value); - } -} - -static void temp_check(const struct bt_mesh_sensor_type *sensor_type) -{ - struct sensor_value in_value[sensor_type->channel_count]; - int32_t test_vector[] = {0x8000, -273, 0, 100, 327}; - - memset(in_value, 0, sizeof(in_value)); - - for (int i = 0; i < ARRAY_SIZE(test_vector); i++) { - int16_t expected[sensor_type->channel_count]; - - for (int j = 0; j < sensor_type->channel_count; j++) { - in_value[j].val1 = test_vector[i]; - in_value[j].val2 = 0; - expected[j] = test_vector[i] == 0x8000 ? - 0x8000 : raw_scalar_value_get(test_vector[i], 1, -2, 0); - } - - encoding_checking_proceed(sensor_type, in_value, expected, sizeof(expected)); - decoding_checking_proceed(sensor_type, expected, sizeof(expected), in_value); - } -} - -static void power_specification_check(const struct bt_mesh_sensor_type *sensor_type) -{ - struct { - struct sensor_value represented[3]; - struct uint24_t raw[3]; - } test_vector[] = { - { - {{0, 0}, {1, 0}, {65536, 0}}, - {{0}, {10}, {655360}} - }, - { - {{1234, 0}, {838860, 500000}, {1677721, 400000}}, - {{12340}, {8388605}, {16777214}} - }, - }; - - for (int i = 0; i < ARRAY_SIZE(test_vector); i++) { - encoding_checking_proceed(sensor_type, &test_vector[i].represented[0], - &test_vector[i].raw, 9); - decoding_checking_proceed(sensor_type, &test_vector[i].raw, 9, - &test_vector[i].represented[0]); - } - - /* Test invalid range on encoding. */ - struct sensor_value invalid_encoding_test_vector[][3] = { - {{1, 0}, {-1, 0}, {0, 0}}, - {{1234, 0}, {4567, 0}, {0x1000000, 0}}, - }; - - for (int i = 0; i < ARRAY_SIZE(invalid_encoding_test_vector); i++) { - invalid_encoding_checking_proceed(sensor_type, &invalid_encoding_test_vector[i][0]); - } -} - -static void power_check(const struct bt_mesh_sensor_type *sensor_type) -{ - struct { - struct sensor_value represented; - uint32_t raw; - } test_vector[] = { - {{0, 0}, 0}, - {{1234, 0}, 12340}, - {{87654, 300000}, 876543}, - {{1677721, 400000}, 0xFFFFFE}, - {{0xFFFFFF, 0}, 0xFFFFFF}, - }; - - for (int i = 0; i < ARRAY_SIZE(test_vector); i++) { - encoding_checking_proceed(sensor_type, &test_vector[i].represented, - &test_vector[i].raw, 3); - decoding_checking_proceed(sensor_type, &test_vector[i].raw, 3, - &test_vector[i].represented); - } - - /* Test invalid range on encoding. */ - struct sensor_value invalid_encoding_test_vector[] = { - {-1, 0}, - {0x1000000, 0}, - }; - - for (int i = 0; i < ARRAY_SIZE(invalid_encoding_test_vector); i++) { - invalid_encoding_checking_proceed(sensor_type, &invalid_encoding_test_vector[i]); - } -} - -static void energy_check(const struct bt_mesh_sensor_type *sensor_type) -{ - struct { - struct sensor_value represented; - uint32_t raw; - } test_vector[] = { - {{0, 0}, 0}, - {{1000, 0}, 1000}, - {{12345678, 0}, 12345678}, - {{16777214, 0}, 16777214}, - {{0xFFFFFF, 0}, 0xFFFFFF}, - }; - - for (int i = 0; i < ARRAY_SIZE(test_vector); i++) { - encoding_checking_proceed(sensor_type, &test_vector[i].represented, - &test_vector[i].raw, 3); - decoding_checking_proceed(sensor_type, &test_vector[i].raw, 3, - &test_vector[i].represented); - } - - /* Test invalid range on encoding. */ - struct sensor_value invalid_encoding_test_vector[] = { - {-1, 0}, - {0x1000000, 0}, - }; - - for (int i = 0; i < ARRAY_SIZE(invalid_encoding_test_vector); i++) { - invalid_encoding_checking_proceed(sensor_type, &invalid_encoding_test_vector[i]); - } -} - -static void energy32_check(const struct bt_mesh_sensor_type *sensor_type) -{ - struct { - struct sensor_value represented; - uint32_t raw; - } test_vector[] = { - {{0, 0}, 0}, - {{1000, 0}, 1000000}, - {{1234567, 890000}, 1234567890}, - {{4294967, 293000}, 0xFFFFFFFD}, - {{0xFFFFFFFE, 0}, 0xFFFFFFFE}, - {{0xFFFFFFFF, 0}, 0xFFFFFFFF}, - }; - - for (int i = 0; i < ARRAY_SIZE(test_vector); i++) { - encoding_checking_proceed(sensor_type, &test_vector[i].represented, - &test_vector[i].raw, 4); - decoding_checking_proceed(sensor_type, &test_vector[i].raw, 4, - &test_vector[i].represented); - } - - /* Test invalid range on encoding. */ - struct sensor_value invalid_encoding_test_vector[] = { - {-3, 0}, - }; - - for (int i = 0; i < ARRAY_SIZE(invalid_encoding_test_vector); i++) { - invalid_encoding_checking_proceed(sensor_type, &invalid_encoding_test_vector[i]); - } -} - -static void cos_of_the_angle_check(const struct bt_mesh_sensor_type *sensor_type) -{ - struct { - struct sensor_value represented; - uint8_t raw; - } test_vector[] = { - {{-100, 0}, 0x9C}, - {{-50, 0}, 0xCE}, - {{0, 0}, 0}, - {{50, 0}, 0x32}, - {{100, 0}, 0x64}, - {{0x7F, 0}, 0x7F}, - }; - - for (int i = 0; i < ARRAY_SIZE(test_vector); i++) { - encoding_checking_proceed(sensor_type, &test_vector[i].represented, - &test_vector[i].raw, 1); - decoding_checking_proceed(sensor_type, &test_vector[i].raw, 1, - &test_vector[i].represented); - } - - /* Test invalid range on encoding. */ - struct sensor_value invalid_encoding_test_vector[] = { - {-101, 0}, - {101, 0}, - }; - - for (int i = 0; i < ARRAY_SIZE(invalid_encoding_test_vector); i++) { - invalid_encoding_checking_proceed(sensor_type, &invalid_encoding_test_vector[i]); - } - - /* Test invalid range on decoding. */ - uint32_t invalid_decoding_test_vector[] = { - 0x9B, - 0x7E, - }; - - for (int i = 0; i < ARRAY_SIZE(invalid_decoding_test_vector); i++) { - invalid_decoding_checking_proceed(sensor_type, &invalid_decoding_test_vector[i], 1); - } -} - -static void energy_in_a_period_of_day_check(const struct bt_mesh_sensor_type *sensor_type) -{ - struct { - struct sensor_value represented[3]; - struct __packed { - struct uint24_t energy; - uint8_t start_time; - uint8_t end_time; - } raw; - } test_vector[] = { - {{{0, 0}, {0, 0}, {1, 0}}, {{0}, 0, 10}}, - {{{1000, 0}, {10, 100000}, {12, 200000}}, {{1000}, 101, 122}}, - {{{16777214, 0}, {15, 500000}, {23, 900000}}, {{16777214}, 155, 239}}, - {{{1, 0}, {2, 0}, {0xFF, 0}}, {{1}, 20, 0xFF}}, - }; - - for (int i = 0; i < ARRAY_SIZE(test_vector); i++) { - encoding_checking_proceed(sensor_type, &test_vector[i].represented[0], - &test_vector[i].raw, 5); - decoding_checking_proceed(sensor_type, &test_vector[i].raw, 5, - &test_vector[i].represented[0]); - } - - /* Test invalid range on encoding. */ - struct sensor_value invalid_encoding_test_vector[][3] = { - {{-1, 0}, {0, 0}, {0, 0}}, - {{0, 0}, {-1, 0}, {0, 0}}, - {{0, 0}, {0, 0}, {-1, 0}}, - {{1, 0}, {24, 0}, {0, 0}}, - {{1, 0}, {0, 0}, {24, 0}}, - {{0x1000000, 0}, {0, 0}, {0, 0}}, - }; - - for (int i = 0; i < ARRAY_SIZE(invalid_encoding_test_vector); i++) { - invalid_encoding_checking_proceed(sensor_type, &invalid_encoding_test_vector[i][0]); - } - - /* Test invalid range on decoding. */ - struct __packed { - struct uint24_t energy; - uint8_t start_time; - uint8_t end_time; - } invalid_decoding_test_vector[] = { - {{0}, 241, 0}, - {{0}, 0, 254}, - }; - - for (int i = 0; i < ARRAY_SIZE(invalid_decoding_test_vector); i++) { - invalid_decoding_checking_proceed(sensor_type, &invalid_decoding_test_vector[i], 5); - } -} - -static void relative_runtime_in_a_generic_level_range_check( - const struct bt_mesh_sensor_type *sensor_type) -{ - struct { - struct sensor_value represented[3]; - struct __packed { - uint8_t relative; - uint16_t min; - uint16_t max; - } raw; - } test_vector[] = { - {{{0, 0}, {0, 0}, {0, 0}}, {0, 0, 0}}, - {{{50, 0}, {32767, 0}, {16384, 0}}, {100, 32767, 16384}}, - {{{100, 0}, {65535, 0}, {8192, 0}}, {200, 65535, 8192}}, - }; - - for (int i = 0; i < ARRAY_SIZE(test_vector); i++) { - encoding_checking_proceed(sensor_type, &test_vector[i].represented[0], - &test_vector[i].raw, 5); - decoding_checking_proceed(sensor_type, &test_vector[i].raw, 5, - &test_vector[i].represented[0]); - } - - /* Test invalid range on encoding. */ - struct sensor_value invalid_encoding_test_vector[][3] = { - {{0, 0}, {-1, 0}, {0, 0}}, - {{0, 0}, {0, 0}, {-1, 0}}, - {{100, 500000}, {0, 0}, {0, 0}}, - }; - - for (int i = 0; i < ARRAY_SIZE(invalid_encoding_test_vector); i++) { - invalid_encoding_checking_proceed(sensor_type, &invalid_encoding_test_vector[i][0]); - } - - /* Test invalid range on decoding. */ - struct __packed { - uint8_t relative; - uint16_t min; - uint16_t max; - } invalid_decoding_test_vector[] = { - {251, 0, 0}, - {254, 0, 0}, - }; - - for (int i = 0; i < ARRAY_SIZE(invalid_decoding_test_vector); i++) { - invalid_decoding_checking_proceed(sensor_type, &invalid_decoding_test_vector[i], 5); - } -} - -static void apparent_energy32_check(const struct bt_mesh_sensor_type *sensor_type) -{ - struct { - struct sensor_value represented; - uint32_t raw; - } test_vector[] = { - {{0, 0}, 0}, - {{1000, 0}, 1000000}, - {{1234567, 890000}, 1234567890}, - {{4294967, 293000}, 0xFFFFFFFD}, - {{0xFFFFFFFE, 0}, 0xFFFFFFFE}, - {{0xFFFFFFFF, 0}, 0xFFFFFFFF}, - }; - - for (int i = 0; i < ARRAY_SIZE(test_vector); i++) { - encoding_checking_proceed(sensor_type, &test_vector[i].represented, - &test_vector[i].raw, 4); - decoding_checking_proceed(sensor_type, &test_vector[i].raw, 4, - &test_vector[i].represented); - } - - /* Test invalid range on encoding. */ - struct sensor_value invalid_encoding_test_vector[] = { - {-3, 0}, - }; - - for (int i = 0; i < ARRAY_SIZE(invalid_encoding_test_vector); i++) { - invalid_encoding_checking_proceed(sensor_type, &invalid_encoding_test_vector[i]); - } -} - -static void apparent_power_check(const struct bt_mesh_sensor_type *sensor_type) -{ - struct { - struct sensor_value represented; - uint32_t raw; - } test_vector[] = { - {{0, 0}, 0}, - {{1234, 0}, 12340}, - {{87654, 300000}, 876543}, - {{1677721, 300000}, 0xFFFFFD}, - {{0xFFFFFF, 0}, 0xFFFFFF}, - }; - - for (int i = 0; i < ARRAY_SIZE(test_vector); i++) { - encoding_checking_proceed(sensor_type, &test_vector[i].represented, - &test_vector[i].raw, 3); - decoding_checking_proceed(sensor_type, &test_vector[i].raw, 3, - &test_vector[i].represented); - } - - /* Test invalid range on encoding. */ - struct sensor_value invalid_encoding_test_vector[] = { - {-1, 0}, - {1677721, 400000}, - {0x1000000, 0}, - }; - - for (int i = 0; i < ARRAY_SIZE(invalid_encoding_test_vector); i++) { - invalid_encoding_checking_proceed(sensor_type, &invalid_encoding_test_vector[i]); - } -} - -static void electric_current_check(const struct bt_mesh_sensor_type *sensor_type) -{ - struct sensor_value in_value[sensor_type->channel_count]; - uint16_t test_vector[] = {0, 1234, 5555, 11111, 65534, UINT16_MAX}; - - memset(in_value, 0, sizeof(in_value)); - - for (int i = 0; i < ARRAY_SIZE(test_vector); i++) { - uint16_t expected[sensor_type->channel_count]; - - for (int j = 0; j < sensor_type->channel_count; j++) { - expected[j] = test_vector[i]; - in_value[j].val1 = test_vector[i] == UINT16_MAX ? - UINT16_MAX : test_vector[i] / 100; - in_value[j].val2 = test_vector[i] == UINT16_MAX ? - 0 : test_vector[i] % 100 * 10000; - } - - encoding_checking_proceed(sensor_type, in_value, expected, sizeof(expected)); - decoding_checking_proceed(sensor_type, expected, sizeof(expected), in_value); - } -} - -static void voltage_check(const struct bt_mesh_sensor_type *sensor_type) -{ - struct sensor_value in_value[sensor_type->channel_count]; - uint16_t test_vector[] = {0, 1, 159, 1000, 1022, 1023, UINT16_MAX}; - - memset(in_value, 0, sizeof(in_value)); - - for (int i = 0; i < ARRAY_SIZE(test_vector); i++) { - uint16_t expected[sensor_type->channel_count]; - - for (int j = 0; j < sensor_type->channel_count; j++) { - voltage_helper(test_vector[i], &in_value[j], &expected[j]); - } - - encoding_checking_proceed(sensor_type, in_value, expected, sizeof(expected)); - for (int j = 0; j < sensor_type->channel_count; j++) { - in_value[j].val1 = test_vector[i] > 1022 ? - test_vector[i] == UINT16_MAX ? UINT16_MAX - : 1022 : test_vector[i]; - } - decoding_checking_proceed(sensor_type, expected, sizeof(expected), in_value); - } -} - -static void average_current_check(const struct bt_mesh_sensor_type *sensor_type) -{ - struct sensor_value in_value[sensor_type->channel_count]; - uint16_t test_vector_current[] = {0, 1234, 5555, 11111, 65534, UINT16_MAX}; - - memset(in_value, 0, sizeof(in_value)); +#define MICRO (1000000LL) - /* Time exponential, represented = 1.1^(N-64), where N is raw value. - * Expected raw values precomputed. (0, 0xFE and 0xFF map - * to themselves as they represent 0 seconds, total device lifetime - * and unknown value, respectively) - */ - int32_t test_vector_time[] = {0, 1, 2999, 66560640, 0xFFFFFFFE, - 0xFFFFFFFF}; - uint8_t expected_time[] = {0, 64, 148, 253, 0xFE, 0xFF}; - struct __packed { - uint16_t expected_current; - uint8_t expected_time; - } expected; - - for (int i = 0; i < ARRAY_SIZE(test_vector_current); i++) { - in_value[0].val1 = test_vector_current[i] == UINT16_MAX ? - UINT16_MAX : test_vector_current[i] / 100; - in_value[0].val2 = test_vector_current[i] == UINT16_MAX ? - 0 : test_vector_current[i] % 100 * 10000; - - in_value[1].val1 = test_vector_time[i]; - in_value[1].val2 = 0; - - expected.expected_current = test_vector_current[i]; - expected.expected_time = expected_time[i]; - - encoding_checking_proceed(sensor_type, in_value, &expected, sizeof(expected)); - decoding_checking_proceed_with_tolerance(sensor_type, &expected, - sizeof(expected), in_value, 0.001); - } +/****************** types section **********************************/ +struct format_spec { + size_t size; + double ratio; + double min; + double max; + struct { + uint32_t *max_or_greater; + uint32_t *min_or_less; + uint32_t *unknown; + uint32_t *invalid; + uint32_t *prohibited; /* A single prohibited value to test */ + } specials; +}; + +#define SPECIAL(_name, _val) ._name = &(uint32_t){(_val)} + +#define FORMAT_SPEC(_size, _M, _d, _b, _min, _max, ...) \ +{ \ + .size = (_size), \ + .ratio = ((_M) * pow(10.0, (_d)) * exp2((_b))), \ + .min = (_min), \ + .max = (_max), \ + .specials = { __VA_ARGS__ } \ +} + +struct channel_spec { + int size; + const struct bt_mesh_sensor_format *format; +}; + +#define CHANNEL(_format, _size) { \ + .size = _size, \ + .format = &bt_mesh_sensor_format_##_format \ } +/****************** types section **********************************/ -static void average_voltage_check(const struct bt_mesh_sensor_type *sensor_type) +static void check_from_micro(const struct bt_mesh_sensor_format *format, + int64_t micro, int expected_err, + uint32_t expected_raw, bool exact) { - struct sensor_value in_value[sensor_type->channel_count]; - uint16_t test_vector_voltage[] = {0, 1, 159, 1022, 1023, UINT16_MAX}; - - memset(in_value, 0, sizeof(in_value)); - - /* Time exponential, represented = 1.1^(N-64), where N is raw value. - * Expected raw values precomputed. (0, 0xFE and 0xFF map - * to themselves as they represent 0 seconds, total device lifetime - * and unknown value, respectively) - */ - int32_t test_vector_time[] = {0, 1, 2999, 66560640, 0xFFFFFFFE, - 0xFFFFFFFF}; - uint8_t expected_time[] = {0, 64, 148, 253, 0xFE, 0xFF}; - struct __packed { - uint16_t expected_voltage; - uint8_t expected_time; - } expected; + struct bt_mesh_sensor_value val = { 0 }; + int err = bt_mesh_sensor_value_from_micro(format, micro, &val); - for (int i = 0; i < ARRAY_SIZE(test_vector_voltage); i++) { - uint16_t tmp; + zassert_equal(err, expected_err); + zassert_equal(val.format, format); - voltage_helper(test_vector_voltage[i], &in_value[0], &tmp); - expected.expected_voltage = tmp; + uint32_t raw = sys_get_le32(val.raw); - in_value[1].val1 = test_vector_time[i]; - in_value[1].val2 = 0; + double error; - expected.expected_time = expected_time[i]; + if (expected_raw == 0 || exact) { + zassert_mem_equal(val.raw, &expected_raw, 4); + } else if (micro < 0) { + int32_t expected_raw_signed = *(int32_t *)(&expected_raw); + int32_t raw_signed = *(int32_t *)(&raw); - encoding_checking_proceed(sensor_type, in_value, &expected, sizeof(expected)); - decoding_checking_proceed_with_tolerance(sensor_type, &expected, - sizeof(expected), in_value, 0.001); + error = (double)raw_signed / expected_raw_signed; + zassert_between_inclusive(error, 0.99999, 1.00001); + } else { + error = (double)raw / expected_raw; + zassert_between_inclusive(error, 0.99999, 1.00001); } } -static void current_stat_check(const struct bt_mesh_sensor_type *sensor_type) +static void check_to_micro(const struct bt_mesh_sensor_format *format, + uint32_t raw, + enum bt_mesh_sensor_value_status expected_status, + int64_t expected_micro, bool exact) { - struct sensor_value in_value[sensor_type->channel_count]; - uint16_t test_vector_current[] = {0, 1234, 5555, 11111, 65534, UINT16_MAX}; + struct bt_mesh_sensor_value val = { .format = format }; + enum bt_mesh_sensor_value_status status; + int64_t micro = 0; - memset(in_value, 0, sizeof(in_value)); + memcpy(val.raw, &raw, 4); + status = bt_mesh_sensor_value_to_micro(&val, µ); + zassert_equal(status, expected_status); + if (expected_micro == 0 || exact) { + zassert_equal(micro, expected_micro); + } else { + double error = (double)micro / (double)expected_micro; - /* Time exponential, represented = 1.1^(N-64), where N is raw value. - * Expected raw values precomputed. (0, 0xFE and 0xFF map - * to themselves as they represent 0 seconds, total device lifetime - * and unknown value, respectively) - */ - int32_t test_vector_time[] = {0, 1, 2999, 66560640, 0xFFFFFFFE, - 0xFFFFFFFF}; - uint8_t expected_time[] = {0, 64, 148, 253, 0xFE, 0xFF}; - struct __packed { - uint16_t expected_current[4]; - uint8_t expected_time; - } expected; - - for (int i = 0; i < ARRAY_SIZE(test_vector_current); i++) { - for (int j = 0; j < sensor_type->channel_count - 1; j++) { - in_value[j].val1 = test_vector_current[i] == UINT16_MAX ? - UINT16_MAX : test_vector_current[i] / 100; - in_value[j].val2 = test_vector_current[i] == UINT16_MAX ? - 0 : test_vector_current[i] % 100 * 10000; - expected.expected_current[j] = test_vector_current[i]; - } - - in_value[sensor_type->channel_count - 1].val1 = test_vector_time[i]; - in_value[sensor_type->channel_count - 1].val2 = 0; - expected.expected_time = expected_time[i]; - - encoding_checking_proceed(sensor_type, in_value, &expected, sizeof(expected)); - decoding_checking_proceed_with_tolerance(sensor_type, &expected, - sizeof(expected), in_value, 0.001); + zassert_between_inclusive(error, 0.99999, 1.00001); } } -static void voltage_stat_check(const struct bt_mesh_sensor_type *sensor_type) +static void check_from_float(const struct bt_mesh_sensor_format *format, + float f, int expected_err, + uint32_t expected_raw, bool exact) { - struct sensor_value in_value[sensor_type->channel_count]; - uint16_t test_vector_voltage[] = {0, 1, 159, 1022, 1023, UINT16_MAX}; - - memset(in_value, 0, sizeof(in_value)); + struct bt_mesh_sensor_value val = { 0 }; + int err = bt_mesh_sensor_value_from_float(format, f, &val); - /* Time exponential, represented = 1.1^(N-64), where N is raw value. - * Expected raw values precomputed. (0, 0xFE and 0xFF map - * to themselves as they represent 0 seconds, total device lifetime - * and unknown value, respectively) - */ - int32_t test_vector_time[] = {0, 1, 2999, 66560640, 0xFFFFFFFE, - 0xFFFFFFFF}; - uint8_t expected_time[] = {0, 64, 148, 253, 0xFE, 0xFF}; - struct __packed { - uint16_t expected_voltage[4]; - uint8_t expected_time; - } expected; - - for (int i = 0; i < ARRAY_SIZE(test_vector_voltage); i++) { - for (int j = 0; j < sensor_type->channel_count - 1; j++) { - uint16_t tmp; - - voltage_helper(test_vector_voltage[i], &in_value[j], &tmp); - expected.expected_voltage[j] = tmp; - } - - in_value[sensor_type->channel_count - 1].val1 = test_vector_time[i]; - in_value[sensor_type->channel_count - 1].val2 = 0; - expected.expected_time = expected_time[i]; - - encoding_checking_proceed(sensor_type, in_value, &expected, sizeof(expected)); - decoding_checking_proceed_with_tolerance(sensor_type, &expected, - sizeof(expected), in_value, 0.001); - } -} + zassert_equal(err, expected_err); + zassert_equal(val.format, format); -static void rel_runtime_in_current_range_check(const struct bt_mesh_sensor_type *sensor_type) -{ - struct sensor_value in_value[sensor_type->channel_count]; - uint16_t test_vector_current[] = {0, 1234, 5555, 11111, 65534, UINT16_MAX}; - uint8_t test_vector_percentage8[] = {0, 25, 50, 75, 100, 0xFF}; - struct __packed { - uint8_t expected_percentage8; - uint16_t expected_current[2]; - } expected; - - memset(in_value, 0, sizeof(in_value)); - - for (int i = 0; i < ARRAY_SIZE(test_vector_current); i++) { - for (int j = 1; j < sensor_type->channel_count; j++) { - expected.expected_current[j - 1] = test_vector_current[i]; - in_value[j].val1 = test_vector_current[i] == UINT16_MAX ? - UINT16_MAX : test_vector_current[i] / 100; - in_value[j].val2 = test_vector_current[i] == UINT16_MAX ? - 0 : test_vector_current[i] % 100 * 10000; - } - - in_value[0].val1 = test_vector_percentage8[i]; - in_value[0].val2 = 0; - expected.expected_percentage8 = test_vector_percentage8[i] == 0xFF ? - 0xFF : raw_scalar_value_get(test_vector_percentage8[i], 1, 0, -1); - - encoding_checking_proceed(sensor_type, in_value, &expected, sizeof(expected)); - decoding_checking_proceed(sensor_type, &expected, sizeof(expected), in_value); - } -} + uint32_t raw = sys_get_le32(val.raw); -static void rel_runtime_in_voltage_range_check(const struct bt_mesh_sensor_type *sensor_type) -{ - struct sensor_value in_value[sensor_type->channel_count]; - uint16_t test_vector_voltage[] = {0, 1, 159, 1022, 1023, UINT16_MAX}; - uint8_t test_vector_percentage8[] = {0, 25, 50, 75, 100, 0xFF}; - struct __packed { - uint8_t expected_percentage8; - uint16_t expected_voltage[2]; - } expected; - - memset(in_value, 0, sizeof(in_value)); - - for (int i = 0; i < ARRAY_SIZE(test_vector_voltage); i++) { - for (int j = 1; j < sensor_type->channel_count; j++) { - uint16_t tmp; - - voltage_helper(test_vector_voltage[i], &in_value[j], &tmp); - expected.expected_voltage[j - 1] = tmp; - } - - in_value[0].val1 = test_vector_percentage8[i]; - in_value[0].val2 = 0; - expected.expected_percentage8 = test_vector_percentage8[i] == 0xFF ? - 0xFF : raw_scalar_value_get(test_vector_percentage8[i], 1, 0, -1); - - for (int j = 1; j < sensor_type->channel_count; j++) { - in_value[j].val1 = test_vector_voltage[i] > 1022 ? - test_vector_voltage[i] == UINT16_MAX ? UINT16_MAX - : 1022 : test_vector_voltage[i]; - } - - encoding_checking_proceed(sensor_type, in_value, &expected, sizeof(expected)); - decoding_checking_proceed(sensor_type, &expected, sizeof(expected), in_value); - } -} + double error; -static void temp_stat_check(const struct bt_mesh_sensor_type *sensor_type) -{ - struct sensor_value in_value[sensor_type->channel_count]; - int32_t test_vector_temp[] = {0x8000, -273, 0, 100, 327}; + if (expected_raw == 0 || exact) { + zassert_equal(raw, expected_raw); + } else if (f < 0) { + int32_t expected_raw_signed = *(int32_t *)(&expected_raw); + int32_t raw_signed = *(int32_t *)(&raw); - /* Time exponential, represented = 1.1^(N-64), where N is raw value. - * Expected raw values precomputed. (0, 0xFE and 0xFF map - * to themselves as they represent 0 seconds, total device lifetime - * and unknown value, respectively) - */ - int32_t test_vector_time[] = {0, 1, 66560640, 0xFFFFFFFE, - 0xFFFFFFFF}; - uint8_t expected_time[] = {0, 64, 253, 0xFE, 0xFF}; - struct __packed { - uint16_t expected_temp[4]; - uint8_t expected_time; - } expected; - - memset(in_value, 0, sizeof(in_value)); - - for (int i = 0; i < ARRAY_SIZE(test_vector_temp); i++) { - for (int j = 0; j < 4; j++) { - in_value[j].val1 = test_vector_temp[i]; - expected.expected_temp[j] = test_vector_temp[i] == 0x8000 ? - 0x8000 : raw_scalar_value_get(test_vector_temp[i], 1, -2, 0); - } - in_value[4].val1 = test_vector_time[i]; - expected.expected_time = expected_time[i]; - - encoding_checking_proceed(sensor_type, in_value, &expected, sizeof(expected)); - decoding_checking_proceed_with_tolerance(sensor_type, &expected, sizeof(expected), - in_value, 0.001); + error = (double)raw_signed / expected_raw_signed; + zassert_between_inclusive(error, 0.99999, 1.00001); + } else { + error = (double)raw / expected_raw; + zassert_between_inclusive(error, 0.99999, 1.00001); } } -static void rel_val_in_a_temp_range_check(const struct bt_mesh_sensor_type *sensor_type) +static void check_to_float(const struct bt_mesh_sensor_format *format, + uint32_t raw, + enum bt_mesh_sensor_value_status expected_status, + float expected_f, bool exact) { - struct sensor_value in_value[sensor_type->channel_count]; - int32_t test_vector_temp[] = {0x8000, -273, 0, 100, 327}; - int32_t test_vector_percentage8[] = {0, 25, 50, 100, 0xFF}; - - struct __packed { - uint8_t expected_percentage8; - uint16_t expected_temp[2]; - } expected; - - memset(in_value, 0, sizeof(in_value)); - - for (int i = 0; i < ARRAY_SIZE(test_vector_temp); i++) { - in_value[0].val1 = test_vector_percentage8[i]; - in_value[1].val1 = test_vector_temp[i]; - in_value[2].val1 = test_vector_temp[i]; + struct bt_mesh_sensor_value val = { .format = format }; + enum bt_mesh_sensor_value_status status; + float f = 0.0f; - uint16_t expected_temp = test_vector_temp[i] == 0x8000 ? - 0x8000 : raw_scalar_value_get(test_vector_temp[i], 1, -2, 0); + memcpy(val.raw, &raw, 4); + status = bt_mesh_sensor_value_to_float(&val, &f); + zassert_equal(status, expected_status); + if (expected_f == 0 || exact) { + zassert_equal(f, expected_f); + } else { + double error = (double)f / (double)expected_f; - expected.expected_percentage8 = test_vector_percentage8[i] == 0xFF ? - 0xFF : raw_scalar_value_get(test_vector_percentage8[i], 1, 0, -1); - expected.expected_temp[0] = expected_temp; - expected.expected_temp[1] = expected_temp; - - encoding_checking_proceed(sensor_type, in_value, &expected, sizeof(expected)); - decoding_checking_proceed(sensor_type, &expected, sizeof(expected), in_value); + zassert_between_inclusive(error, 0.99999, 1.00001); } } -static void gain_check(const struct bt_mesh_sensor_type *sensor_type) +static void check_from_special(const struct bt_mesh_sensor_format *format, + enum bt_mesh_sensor_value_status special, + int expected_err, uint32_t expected_raw) { - struct sensor_value in_value[] = {{-10, -999999}, {0, -99999}, {0, 99999}, - {10, 1111}, {9, 999999}}; - - for (int i = 0; i < ARRAY_SIZE(in_value); i++) { - float expected = (float)in_value[i].val1 + in_value[i].val2 / 1000000.0f; + struct bt_mesh_sensor_value val = { 0 }; + int err = bt_mesh_sensor_value_from_special_status(format, special, &val); - encoding_checking_proceed(sensor_type, &in_value[i], &expected, sizeof(float)); - decoding_checking_proceed(sensor_type, &expected, sizeof(float), &in_value[i]); + zassert_equal(err, expected_err); + if (expected_err == 0) { + zassert_mem_equal(val.raw, &expected_raw, 4); + zassert_equal(val.format, format); } } -/* Occupancy sensors */ - -ZTEST(sensor_types_test, test_motion_sensor) -{ - const struct bt_mesh_sensor_type *sensor_type; - - sensor_type = bt_mesh_sensor_type_get(BT_MESH_PROP_ID_MOTION_SENSED); - sensor_type_sanitize(sensor_type); - percentage8_check(sensor_type); -} - -ZTEST(sensor_types_test, test_motion_threshold) -{ - const struct bt_mesh_sensor_type *sensor_type; - - sensor_type = bt_mesh_sensor_type_get(BT_MESH_PROP_ID_MOTION_THRESHOLD); - sensor_type_sanitize(sensor_type); - percentage8_check(sensor_type); -} - -ZTEST(sensor_types_test, test_people_count) -{ - const struct bt_mesh_sensor_type *sensor_type; - - sensor_type = bt_mesh_sensor_type_get(BT_MESH_PROP_ID_PEOPLE_COUNT); - sensor_type_sanitize(sensor_type); - count16_check(sensor_type); -} - -ZTEST(sensor_types_test, test_presence_detected) -{ - const struct bt_mesh_sensor_type *sensor_type; - - sensor_type = bt_mesh_sensor_type_get(BT_MESH_PROP_ID_PRESENCE_DETECTED); - sensor_type_sanitize(sensor_type); - boolean_check(sensor_type); -} - -ZTEST(sensor_types_test, test_time_since_motion_sensed) -{ - const struct bt_mesh_sensor_type *sensor_type; - - sensor_type = bt_mesh_sensor_type_get(BT_MESH_PROP_ID_TIME_SINCE_MOTION_SENSED); - sensor_type_sanitize(sensor_type); - time_millisecond24_check(sensor_type); -} - -ZTEST(sensor_types_test, test_time_since_presence_detected) -{ - const struct bt_mesh_sensor_type *sensor_type; - - sensor_type = bt_mesh_sensor_type_get(BT_MESH_PROP_ID_TIME_SINCE_PRESENCE_DETECTED); - sensor_type_sanitize(sensor_type); - time_second16_check(sensor_type); -} - -/* Environmental sensors */ - -ZTEST(sensor_types_test, test_apparent_wind_direction) -{ - const struct bt_mesh_sensor_type *sensor_type; - - sensor_type = bt_mesh_sensor_type_get(BT_MESH_PROP_ID_APPARENT_WIND_DIRECTION); - sensor_type_sanitize(sensor_type); - plane_angle_check(sensor_type); -} - -ZTEST(sensor_types_test, test_apparent_wind_speed) +static void check_to_status(const struct bt_mesh_sensor_format *format, + uint32_t raw, + enum bt_mesh_sensor_value_status expected_status) { - const struct bt_mesh_sensor_type *sensor_type; + struct bt_mesh_sensor_value val = { .format = format }; + enum bt_mesh_sensor_value_status status; - sensor_type = bt_mesh_sensor_type_get(BT_MESH_PROP_ID_APPARENT_WIND_SPEED); - sensor_type_sanitize(sensor_type); - wind_speed_check(sensor_type); + memcpy(val.raw, &raw, 4); + status = bt_mesh_sensor_value_get_status(&val); + zassert_equal(status, expected_status); } -ZTEST(sensor_types_test, test_dew_point) +static uint32_t encoded(const struct format_spec *spec, double value) { - const struct bt_mesh_sensor_type *sensor_type; + int64_t val = round(value / spec->ratio); - sensor_type = bt_mesh_sensor_type_get(BT_MESH_PROP_ID_DEW_POINT); - sensor_type_sanitize(sensor_type); - temp8_signed_celsius_check(sensor_type); + val &= GENMASK((spec->size * 8) - 1, 0); + return (uint32_t)val; } -ZTEST(sensor_types_test, test_gust_factor) +/** Finds the biggest float less than or equal to @c limit. */ +static float biggest_float_leq(double limit) { - const struct bt_mesh_sensor_type *sensor_type; + float val = limit; - sensor_type = bt_mesh_sensor_type_get(BT_MESH_PROP_ID_GUST_FACTOR); - sensor_type_sanitize(sensor_type); - gust_factor_check(sensor_type); + return (double)val <= limit ? val : nextafterf(val, -INFINITY); } -ZTEST(sensor_types_test, test_heat_index) +/** Finds the smallest float greater than or equal to @c limit. */ +static float smallest_float_geq(double limit) { - const struct bt_mesh_sensor_type *sensor_type; + float val = limit; - sensor_type = bt_mesh_sensor_type_get(BT_MESH_PROP_ID_HEAT_INDEX); - sensor_type_sanitize(sensor_type); - temp8_signed_celsius_check(sensor_type); + return (double)val >= limit ? val : nextafterf(val, INFINITY); } -ZTEST(sensor_types_test, test_present_amb_rel_humidity) +static void check_scalar_format(const struct bt_mesh_sensor_format *format, + const struct format_spec *spec) { - const struct bt_mesh_sensor_type *sensor_type; + /* Decoded values inside [min, max] range */ + int64_t micro_max = round(spec->max * MICRO); + int64_t micro_min = round(spec->min * MICRO); + float float_max = biggest_float_leq(spec->max); + float float_min = smallest_float_geq(spec->min); - sensor_type = bt_mesh_sensor_type_get(BT_MESH_PROP_ID_PRESENT_AMB_REL_HUMIDITY); - sensor_type_sanitize(sensor_type); - humidity_check(sensor_type); -} - -ZTEST(sensor_types_test, test_present_amb_co2_concentration) -{ - const struct bt_mesh_sensor_type *sensor_type; - - sensor_type = bt_mesh_sensor_type_get(BT_MESH_PROP_ID_PRESENT_AMB_CO2_CONCENTRATION); - sensor_type_sanitize(sensor_type); - concentration_check(sensor_type); -} + /* Checking encoding/decoding maximum value */ + check_from_micro(format, micro_max, 0, encoded(spec, spec->max), true); + check_from_float(format, float_max, 0, encoded(spec, float_max), false); + check_to_micro(format, encoded(spec, spec->max), + BT_MESH_SENSOR_VALUE_NUMBER, micro_max, true); + check_to_float(format, encoded(spec, spec->max), + BT_MESH_SENSOR_VALUE_NUMBER, spec->max, false); + check_to_status(format, encoded(spec, spec->max), BT_MESH_SENSOR_VALUE_NUMBER); -ZTEST(sensor_types_test, test_present_amb_voc_concentration) -{ - const struct bt_mesh_sensor_type *sensor_type; + /* Checking encoding/decoding minimum value */ + check_from_micro(format, micro_min, 0, encoded(spec, spec->min), true); + check_from_float(format, float_min, 0, encoded(spec, float_min), false); + check_to_micro(format, encoded(spec, spec->min), + BT_MESH_SENSOR_VALUE_NUMBER, micro_min, true); + check_to_float(format, encoded(spec, spec->min), + BT_MESH_SENSOR_VALUE_NUMBER, spec->min, false); + check_to_status(format, encoded(spec, spec->min), BT_MESH_SENSOR_VALUE_NUMBER); - sensor_type = bt_mesh_sensor_type_get(BT_MESH_PROP_ID_PRESENT_AMB_VOC_CONCENTRATION); - sensor_type_sanitize(sensor_type); - concentration_check(sensor_type); -} + /* Decoded values outside [min, max] range */ + int64_t micro_max_plus_one = micro_max + round(MICRO * spec->ratio); + int64_t micro_min_minus_one = micro_min - round(MICRO * spec->ratio); + float float_max_plus_one = spec->max + spec->ratio; + float float_min_minus_one = spec->min - spec->ratio; -ZTEST(sensor_types_test, test_present_amb_noise) -{ - const struct bt_mesh_sensor_type *sensor_type; + uint32_t special_raw; + + /* Checking values bigger than maximum */ + if (spec->specials.max_or_greater) { + /* Checking special value "maximum or greater" */ + special_raw = *(spec->specials.max_or_greater); + check_from_micro(format, micro_max_plus_one, 0, special_raw, true); + check_from_float(format, float_max_plus_one, 0, special_raw, true); + check_from_special(format, BT_MESH_SENSOR_VALUE_MAX_OR_GREATER, 0, special_raw); + check_to_micro(format, special_raw, BT_MESH_SENSOR_VALUE_MAX_OR_GREATER, + micro_max_plus_one, true); + check_to_float(format, special_raw, BT_MESH_SENSOR_VALUE_MAX_OR_GREATER, + float_max_plus_one, false); + check_to_status(format, special_raw, BT_MESH_SENSOR_VALUE_MAX_OR_GREATER); - sensor_type = bt_mesh_sensor_type_get(BT_MESH_PROP_ID_PRESENT_AMB_NOISE); - sensor_type_sanitize(sensor_type); - noise_check(sensor_type); -} + /* Checking clamping of values out of range */ + int64_t micro_max_plus_two = micro_max + round(MICRO * 2 * spec->ratio); + float float_max_plus_two = spec->max + (2 * spec->ratio); -ZTEST(sensor_types_test, test_present_indoor_relative_humidity) -{ - const struct bt_mesh_sensor_type *sensor_type; + check_from_micro(format, micro_max_plus_two, -ERANGE, special_raw, true); + check_from_float(format, float_max_plus_two, -ERANGE, special_raw, true); + } else { + /* Checking clamping of values out of range */ + check_from_micro(format, micro_max_plus_one, -ERANGE, + encoded(spec, spec->max), true); + check_from_float(format, float_max_plus_one, -ERANGE, + encoded(spec, spec->max), true); - sensor_type = bt_mesh_sensor_type_get(BT_MESH_PROP_ID_PRESENT_INDOOR_RELATIVE_HUMIDITY); - sensor_type_sanitize(sensor_type); - humidity_check(sensor_type); + /* Checking non-supported special value */ + check_from_special(format, BT_MESH_SENSOR_VALUE_MAX_OR_GREATER, -ERANGE, 0); + } + + /* Checking values smaller than minimum */ + if (spec->specials.min_or_less) { + /* Checking special value "minimum or less" */ + special_raw = *(spec->specials.min_or_less); + check_from_micro(format, micro_min_minus_one, 0, special_raw, true); + check_from_float(format, float_min_minus_one, 0, special_raw, true); + check_from_special(format, BT_MESH_SENSOR_VALUE_MIN_OR_LESS, 0, special_raw); + check_to_micro(format, special_raw, BT_MESH_SENSOR_VALUE_MIN_OR_LESS, + micro_min_minus_one, true); + check_to_float(format, special_raw, BT_MESH_SENSOR_VALUE_MIN_OR_LESS, + float_min_minus_one, false); + check_to_status(format, special_raw, BT_MESH_SENSOR_VALUE_MIN_OR_LESS); + + /* Checking clamping of values out of range */ + int64_t micro_min_minus_two = micro_min - round(MICRO * 2 * spec->ratio); + float float_min_minus_two = spec->min - (2 * spec->ratio); + + check_from_micro(format, micro_min_minus_two, -ERANGE, special_raw, true); + check_from_float(format, float_min_minus_two, -ERANGE, special_raw, true); + } else { + /* Checking clamping of values out of range */ + check_from_micro(format, micro_min_minus_one, -ERANGE, + encoded(spec, spec->min), true); + check_from_float(format, float_min_minus_one, -ERANGE, + encoded(spec, spec->min), true); + + /* Checking non-supported special value */ + check_from_special(format, BT_MESH_SENSOR_VALUE_MIN_OR_LESS, -ERANGE, 0); + } + + if (spec->specials.unknown) { + /* Checking special value "value is not known" */ + special_raw = *(spec->specials.unknown); + check_from_special(format, BT_MESH_SENSOR_VALUE_UNKNOWN, 0, special_raw); + check_to_status(format, special_raw, BT_MESH_SENSOR_VALUE_UNKNOWN); + check_to_float(format, special_raw, BT_MESH_SENSOR_VALUE_UNKNOWN, 0.0f, true); + check_to_micro(format, special_raw, BT_MESH_SENSOR_VALUE_UNKNOWN, 0, true); + } else { + /* Checking non-supported special value "value is not known" */ + check_from_special(format, BT_MESH_SENSOR_VALUE_UNKNOWN, -ERANGE, 0); + } + + if (spec->specials.invalid) { + /* Checking special value "value is invalid" */ + special_raw = *(spec->specials.invalid); + check_from_special(format, BT_MESH_SENSOR_VALUE_INVALID, 0, special_raw); + check_to_status(format, special_raw, BT_MESH_SENSOR_VALUE_INVALID); + check_to_float(format, special_raw, BT_MESH_SENSOR_VALUE_INVALID, 0.0f, true); + check_to_micro(format, special_raw, BT_MESH_SENSOR_VALUE_INVALID, 0, true); + } else { + /* Checking non-supported special value "value is invalid" */ + check_from_special(format, BT_MESH_SENSOR_VALUE_INVALID, -ERANGE, 0); + } + + if (spec->specials.prohibited) { + /* Checking decoding a prohibited raw value */ + special_raw = *(spec->specials.prohibited); + check_to_status(format, special_raw, BT_MESH_SENSOR_VALUE_CONVERSION_ERROR); + check_to_float(format, special_raw, BT_MESH_SENSOR_VALUE_CONVERSION_ERROR, + 0.0f, true); + check_to_micro(format, special_raw, BT_MESH_SENSOR_VALUE_CONVERSION_ERROR, + 0, true); + } +} + +#define TEST_SCALAR_FORMAT(_name, _spec) \ +ZTEST(sensor_types_test, test_format_##_name) \ +{ \ + struct format_spec spec = _spec; \ + check_scalar_format(&bt_mesh_sensor_format_##_name, &spec); \ +} + +static void check_sensor_type(const struct bt_mesh_sensor_type *type, + uint16_t id, + const struct channel_spec *channels, + size_t channels_count) +{ + uint8_t test_data[BT_MESH_SENSOR_ENCODED_VALUE_MAXLEN]; + struct bt_mesh_sensor_value values[CONFIG_BT_MESH_SENSOR_CHANNELS_MAX]; + int total_size = 0; + + for (int i = 0; i < ARRAY_SIZE(test_data); i++) { + test_data[i] = i + 1; + } + + for (int i = 0; i < channels_count; i++) { + total_size += channels[i].size; + } + + zassert_equal(bt_mesh_sensor_type_get(id), type); + zassert_true(total_size <= ARRAY_SIZE(test_data), + "Not enough test data available to test this type"); + + NET_BUF_SIMPLE_DEFINE(buf, total_size); + (void)net_buf_simple_add_mem(&buf, test_data, total_size); + + zassert_equal(buf.len, total_size); + zassert_true(channels_count <= CONFIG_BT_MESH_SENSOR_CHANNELS_MAX); + zassert_ok(sensor_value_decode(&buf, type, values)); + zassert_equal(buf.len, 0); + + const uint8_t *current_data = test_data; + + for (int i = 0; i < channels_count; i++) { + zassert_equal(values[i].format, channels[i].format); + zassert_equal(values[i].format->size, channels[i].size); + zassert_mem_equal(values[i].raw, current_data, channels[i].size); + current_data += channels[i].size; + } + + net_buf_simple_reset(&buf); + memset(buf.data, 0, total_size); + zassert_ok(sensor_value_encode(&buf, type, values)); + zassert_equal(buf.len, total_size); + zassert_mem_equal(buf.data, test_data, total_size); } -ZTEST(sensor_types_test, test_present_outdoor_relative_humidity) -{ - const struct bt_mesh_sensor_type *sensor_type; - - sensor_type = bt_mesh_sensor_type_get(BT_MESH_PROP_ID_PRESENT_OUTDOOR_RELATIVE_HUMIDITY); - sensor_type_sanitize(sensor_type); - humidity_check(sensor_type); +#define TEST_SENSOR_TYPE(_name, _id, ...) \ +ZTEST(sensor_types_test, test_type_##_name) \ +{ \ + check_sensor_type( \ + &bt_mesh_sensor_##_name, \ + (_id), \ + ((struct channel_spec[]){ __VA_ARGS__ }), \ + ARRAY_SIZE(((struct channel_spec[]){ __VA_ARGS__ }))); \ } -ZTEST(sensor_types_test, test_magnetic_declination) -{ - const struct bt_mesh_sensor_type *sensor_type; +TEST_SCALAR_FORMAT(percentage_8, + FORMAT_SPEC(1, 1, 0, -1, 0.0, 100.0, + SPECIAL(unknown, 0xff), + SPECIAL(prohibited, 0xC9))) - sensor_type = bt_mesh_sensor_type_get(BT_MESH_PROP_ID_MAGNETIC_DECLINATION); - sensor_type_sanitize(sensor_type); - plane_angle_check(sensor_type); -} +TEST_SCALAR_FORMAT(percentage_16, + FORMAT_SPEC(2, 1, -2, 0, 0.0, 100.0, + SPECIAL(unknown, 0xffff), + SPECIAL(prohibited, 0x2711))) -ZTEST(sensor_types_test, test_magnetic_flux_density_2d) -{ - const struct bt_mesh_sensor_type *sensor_type; +TEST_SCALAR_FORMAT(percentage_delta_trigger, + FORMAT_SPEC(2, 1, -2, 0, 0.0, 655.35)) - sensor_type = bt_mesh_sensor_type_get(BT_MESH_PROP_ID_MAGNETIC_FLUX_DENSITY_2D); - sensor_type_sanitize(sensor_type); - flux_density_check(sensor_type); -} +TEST_SCALAR_FORMAT(temp_8, + FORMAT_SPEC(1, 1, 0, -1, -64.0, 63.0, + SPECIAL(unknown, 0x7f))) -ZTEST(sensor_types_test, test_magnetic_flux_density_3d) -{ - const struct bt_mesh_sensor_type *sensor_type; +TEST_SCALAR_FORMAT(temp, + FORMAT_SPEC(2, 1, -2, 0, -273.15, 327.67, + SPECIAL(unknown, 0x8000), + SPECIAL(prohibited, 0x954C))) - sensor_type = bt_mesh_sensor_type_get(BT_MESH_PROP_ID_MAGNETIC_FLUX_DENSITY_3D); - sensor_type_sanitize(sensor_type); - flux_density_check(sensor_type); -} +TEST_SCALAR_FORMAT(co2_concentration, + FORMAT_SPEC(2, 1, 0, 0, 0, 65533, + SPECIAL(max_or_greater, 0xFFFE), + SPECIAL(unknown, 0xFFFF))) -ZTEST(sensor_types_test, test_pollen_concentration) -{ - const struct bt_mesh_sensor_type *sensor_type; +TEST_SCALAR_FORMAT(noise, + FORMAT_SPEC(1, 1, 0, 0, 0, 253, + SPECIAL(max_or_greater, 0xFE), + SPECIAL(unknown, 0xFF))) - sensor_type = bt_mesh_sensor_type_get(BT_MESH_PROP_ID_POLLEN_CONCENTRATION); - sensor_type_sanitize(sensor_type); - pollen_concentration_check(sensor_type); -} +TEST_SCALAR_FORMAT(voc_concentration, + FORMAT_SPEC(2, 1, 0, 0, 0, 65533, + SPECIAL(max_or_greater, 0xFFFE), + SPECIAL(unknown, 0xFFFF))) -ZTEST(sensor_types_test, test_air_pressure) -{ - const struct bt_mesh_sensor_type *sensor_type; +TEST_SCALAR_FORMAT(wind_speed, + FORMAT_SPEC(2, 1, -2, 0, 0, 655.35)) - sensor_type = bt_mesh_sensor_type_get(BT_MESH_PROP_ID_AIR_PRESSURE); - sensor_type_sanitize(sensor_type); - pressure_check(sensor_type); -} +TEST_SCALAR_FORMAT(temp_8_wide, + FORMAT_SPEC(1, 1, 0, 0, -128, 127)) -ZTEST(sensor_types_test, test_pressure) -{ - const struct bt_mesh_sensor_type *sensor_type; +TEST_SCALAR_FORMAT(gust_factor, + FORMAT_SPEC(1, 1, -1, 0, 0, 25.5)) - sensor_type = bt_mesh_sensor_type_get(BT_MESH_PROP_ID_PRESSURE); - sensor_type_sanitize(sensor_type); - pressure_check(sensor_type); -} +/* GSS specifies d = -7. Implemented in the stack as d = -1 with unit of + * microtesla, to be able to work with full precision using to_micro/from_micro + * and to/from sensor_value. + * Testing using d = -1 + */ +TEST_SCALAR_FORMAT(magnetic_flux_density, + FORMAT_SPEC(2, 1, -1, 0, -3276.8, 3276.7)) -ZTEST(sensor_types_test, test_rainfall) -{ - const struct bt_mesh_sensor_type *sensor_type; +TEST_SCALAR_FORMAT(pollen_concentration, + FORMAT_SPEC(3, 1, 0, 0, 0, 16777215)) - sensor_type = bt_mesh_sensor_type_get(BT_MESH_PROP_ID_RAINFALL); - sensor_type_sanitize(sensor_type); - rainfall_check(sensor_type); -} +TEST_SCALAR_FORMAT(pressure, + FORMAT_SPEC(4, 1, -1, 0, 0, 429496729.5)) -ZTEST(sensor_types_test, test_true_wind_direction) -{ - const struct bt_mesh_sensor_type *sensor_type; +TEST_SCALAR_FORMAT(rainfall, + FORMAT_SPEC(2, 1, -3, 0, 0, 65.535)) - sensor_type = bt_mesh_sensor_type_get(BT_MESH_PROP_ID_TRUE_WIND_DIRECTION); - sensor_type_sanitize(sensor_type); - plane_angle_check(sensor_type); -} +TEST_SCALAR_FORMAT(uv_index, + FORMAT_SPEC(1, 1, 0, 0, 0, 255)) -ZTEST(sensor_types_test, test_true_wind_speed) -{ - const struct bt_mesh_sensor_type *sensor_type; +TEST_SCALAR_FORMAT(time_decihour_8, + FORMAT_SPEC(1, 1, -1, 0, 0, 23.9, + SPECIAL(unknown, 0xFF), + SPECIAL(prohibited, 0xF0))) - sensor_type = bt_mesh_sensor_type_get(BT_MESH_PROP_ID_TRUE_WIND_SPEED); - sensor_type_sanitize(sensor_type); - wind_speed_check(sensor_type); -} +TEST_SCALAR_FORMAT(time_hour_24, + FORMAT_SPEC(3, 1, 0, 0, 0, 16777214, + SPECIAL(unknown, 0xFFFFFF))) -ZTEST(sensor_types_test, test_uv_index) -{ - const struct bt_mesh_sensor_type *sensor_type; +TEST_SCALAR_FORMAT(time_second_16, + FORMAT_SPEC(2, 1, 0, 0, 0, 65534, + SPECIAL(unknown, 0xFFFF))) - sensor_type = bt_mesh_sensor_type_get(BT_MESH_PROP_ID_UV_INDEX); - sensor_type_sanitize(sensor_type); - uv_index_check(sensor_type); -} +TEST_SCALAR_FORMAT(time_millisecond_24, + FORMAT_SPEC(3, 1, -3, 0, 0, 16777.214, + SPECIAL(unknown, 0xFFFFFF))) -ZTEST(sensor_types_test, test_wind_chill) +ZTEST(sensor_types_test, test_format_time_exp_8) { - const struct bt_mesh_sensor_type *sensor_type; - - sensor_type = bt_mesh_sensor_type_get(BT_MESH_PROP_ID_WIND_CHILL); - sensor_type_sanitize(sensor_type); - temp8_signed_celsius_check(sensor_type); -} + const struct bt_mesh_sensor_format *fmt = &bt_mesh_sensor_format_time_exp_8; -/* Photometry sensors */ + /* Specially encoded 0 + * | Lowest normal value + * | | Encodes "1" + * | | | Encodes "1.1" + * | | | | Middle of range + * | | | | | Highest normal value + * v v v v v v + */ + uint32_t test_vector[] = { 0x00, 0x01, 0x40, 0x41, 0x80, 0xfd}; + + for (int i = 0; i < ARRAY_SIZE(test_vector); i++) { + /* Representation: decoded value = 1.1^(encoded - 64) seconds */ + uint32_t raw = test_vector[i]; + /* raw value == 0 is defined to be 0 seconds */ + double exp_1_1 = raw == 0 ? 0.0 : pow(1.1, raw - 64.0); + float f = exp_1_1; + int64_t micro = exp_1_1 * MICRO; + + check_from_micro(fmt, micro, 0, raw, true); + check_from_float(fmt, f, 0, raw, true); + /* Checking with exact == false for to_micro because in this case it is computed + * via float. + */ + check_to_micro(fmt, raw, BT_MESH_SENSOR_VALUE_NUMBER, micro, false); + check_to_float(fmt, raw, BT_MESH_SENSOR_VALUE_NUMBER, f, false); + check_to_status(fmt, raw, BT_MESH_SENSOR_VALUE_NUMBER); + } + + /* Checking special values*/ + uint32_t total_device_life_raw = 0xFE; + uint32_t unknown_raw = 0xFF; + + check_from_special(fmt, BT_MESH_SENSOR_VALUE_TOTAL_DEVICE_LIFE, 0, total_device_life_raw); + check_to_micro(fmt, total_device_life_raw, + BT_MESH_SENSOR_VALUE_TOTAL_DEVICE_LIFE, 0, true); + check_to_float(fmt, total_device_life_raw, + BT_MESH_SENSOR_VALUE_TOTAL_DEVICE_LIFE, 0.0f, true); + check_to_status(fmt, total_device_life_raw, BT_MESH_SENSOR_VALUE_TOTAL_DEVICE_LIFE); + + check_from_special(fmt, BT_MESH_SENSOR_VALUE_UNKNOWN, 0, unknown_raw); + check_to_micro(fmt, unknown_raw, BT_MESH_SENSOR_VALUE_UNKNOWN, 0, true); + check_to_float(fmt, unknown_raw, BT_MESH_SENSOR_VALUE_UNKNOWN, 0.0f, true); + check_to_status(fmt, unknown_raw, BT_MESH_SENSOR_VALUE_UNKNOWN); +} + +TEST_SCALAR_FORMAT(electric_current, + FORMAT_SPEC(2, 1, -2, 0, 0, 655.34, + SPECIAL(unknown, 0xFFFF))) + +/* GSS defines this in a weird way compared to all other characteristics, where + * the values for min (0.0) and max (1022.0) are actually the values for + * "0.0 or less" and "1022.0 or greater". Since the test format spec expects + * these special values to be excluded from the min and max range specified, + * the min and max used here are 0 + 2^-6 and 1022.0 - 2^-6. + */ +TEST_SCALAR_FORMAT(voltage, + FORMAT_SPEC(2, 1, 0, -6, 0.015625, 1021.984375, + SPECIAL(unknown, 0xFFFF), + SPECIAL(max_or_greater, 0xFF80), + SPECIAL(min_or_less, 0x0000), + SPECIAL(prohibited, 0xFF81))) + +TEST_SCALAR_FORMAT(energy32, + FORMAT_SPEC(4, 1, -3, 0, 0, 4294967.293, + SPECIAL(invalid, 0xFFFFFFFE), + SPECIAL(unknown, 0xFFFFFFFF))) + +TEST_SCALAR_FORMAT(apparent_energy32, + FORMAT_SPEC(4, 1, -3, 0, 0, 4294967.293, + SPECIAL(invalid, 0xFFFFFFFE), + SPECIAL(unknown, 0xFFFFFFFF))) + +TEST_SCALAR_FORMAT(apparent_power, + FORMAT_SPEC(3, 1, -1, 0, 0, 1677721.3, + SPECIAL(invalid, 0xFFFFFE), + SPECIAL(unknown, 0xFFFFFF))) + +TEST_SCALAR_FORMAT(power, + FORMAT_SPEC(3, 1, -1, 0, 0, 1677721.4, + SPECIAL(unknown, 0xFFFFFF))) + +TEST_SCALAR_FORMAT(energy, + FORMAT_SPEC(3, 1, 0, 0, 0, 16777214, + SPECIAL(unknown, 0xFFFFFF))) + +TEST_SCALAR_FORMAT(chromatic_distance, + FORMAT_SPEC(2, 1, -5, 0, -0.05, 0.05, + SPECIAL(invalid, 0x7FFF), + SPECIAL(unknown, 0x7FFE), + SPECIAL(prohibited, 0x1389))) + +/* Note: According to GSS, "Unit is unitless with a resolution of 1/65535" and + * "Maximum: 1.0". However, it also states that "b = -16". Since this is + * contradictory and the stack currently encodes/decodes according to + * "b = -16", the max is recomputed in this test to 0.9999847 (~= 65535/65536) + */ +TEST_SCALAR_FORMAT(chromaticity_coordinate, + FORMAT_SPEC(2, 1, 0, -16, 0, 0.9999847)) + +TEST_SCALAR_FORMAT(correlated_color_temp, + FORMAT_SPEC(2, 1, 0, 0, 800, 65534, + SPECIAL(unknown, 0xFFFF), + SPECIAL(prohibited, 0x031f))) + +TEST_SCALAR_FORMAT(illuminance, + FORMAT_SPEC(3, 1, -2, 0, 0, 167772.14, + SPECIAL(unknown, 0xFFFFFF))) + +TEST_SCALAR_FORMAT(luminous_efficacy, + FORMAT_SPEC(2, 1, -1, 0, 0, 1800, + SPECIAL(unknown, 0xFFFF), + SPECIAL(prohibited, 0x4651))) + +TEST_SCALAR_FORMAT(luminous_energy, + FORMAT_SPEC(3, 1, 3, 0, 0, 16777214000.0, + SPECIAL(unknown, 0xFFFFFF))) + +TEST_SCALAR_FORMAT(luminous_exposure, + FORMAT_SPEC(3, 1, 3, 0, 0, 16777214000.0, + SPECIAL(unknown, 0xFFFFFF))) + +TEST_SCALAR_FORMAT(luminous_flux, + FORMAT_SPEC(2, 1, 0, 0, 0, 65534, + SPECIAL(unknown, 0xFFFF))) + +TEST_SCALAR_FORMAT(perceived_lightness, + FORMAT_SPEC(2, 1, 0, 0, 0, 65535)) + +TEST_SCALAR_FORMAT(direction_16, + FORMAT_SPEC(2, 1, -2, 0, 0, 359.99, + SPECIAL(prohibited, 0x8CA0))) + +TEST_SCALAR_FORMAT(count_16, + FORMAT_SPEC(2, 1, 0, 0, 0, 65534, + SPECIAL(unknown, 0xFFFF))) + +TEST_SCALAR_FORMAT(gen_lvl, + FORMAT_SPEC(2, 1, 0, 0, 0, 65535)) + +/* GSS specifies d = -2, however, it also says that the value is + * "expressed as Cos(theta) x 100, with a resolution of 1", implying d = 1. + * Min = -100 and max = 100 are also specified in terms of 100ths of the cosine. + * The stack currently returns values in hundredths, i.e. d = 0, so this is + * what is tested. + */ +TEST_SCALAR_FORMAT(cos_of_the_angle, + FORMAT_SPEC(1, 1, 0, 0, -100, 100, + SPECIAL(unknown, 0x7F), + SPECIAL(prohibited, 0x9B))) -ZTEST(sensor_types_test, test_cie_1931_chromaticity_coords) +ZTEST(sensor_types_test, test_format_boolean) { - const struct bt_mesh_sensor_type *sensor_type; + const struct bt_mesh_sensor_format *fmt = &bt_mesh_sensor_format_boolean; - uint16_t sensor[] = { - BT_MESH_PROP_ID_INITIAL_CIE_1931_CHROMATICITY_COORDS, - BT_MESH_PROP_ID_PRESENT_CIE_1931_CHROMATICITY_COORDS, - }; + /* Checking false (0) */ + check_from_micro(fmt, 0, 0, 0x00, true); + check_from_float(fmt, 0.0f, 0, 0x00, true); + check_to_micro(fmt, 0x00, BT_MESH_SENSOR_VALUE_NUMBER, 0, true); + check_to_float(fmt, 0x00, BT_MESH_SENSOR_VALUE_NUMBER, 0.0f, true); + check_to_status(fmt, 0x00, BT_MESH_SENSOR_VALUE_NUMBER); - for (int i = 0; i < ARRAY_SIZE(sensor); i++) { - sensor_type = bt_mesh_sensor_type_get(sensor[i]); - sensor_type_sanitize(sensor_type); - chromaticity_coordinates_check(sensor_type); - } -} + /* Checking true (1) */ + check_from_micro(fmt, 1 * MICRO, 0, 0x01, true); + check_from_float(fmt, 1.0f, 0, 0x01, true); + check_to_micro(fmt, 0x01, BT_MESH_SENSOR_VALUE_NUMBER, 1 * MICRO, true); + check_to_float(fmt, 0x01, BT_MESH_SENSOR_VALUE_NUMBER, 1.0f, true); + check_to_status(fmt, 0x01, BT_MESH_SENSOR_VALUE_NUMBER); -ZTEST(sensor_types_test, test_present_amb_light_level) -{ - const struct bt_mesh_sensor_type *sensor_type; + /* Checking out of range float/micro */ + check_from_micro(fmt, 2 * MICRO, -ERANGE, 0x01, true); + check_from_float(fmt, 2.0f, -ERANGE, 0x01, true); - sensor_type = bt_mesh_sensor_type_get(BT_MESH_PROP_ID_PRESENT_AMB_LIGHT_LEVEL); - sensor_type_sanitize(sensor_type); - illuminance_check(sensor_type); + /* Checking prohibited */ + check_to_micro(fmt, 0x02, BT_MESH_SENSOR_VALUE_CONVERSION_ERROR, 0, true); + check_to_float(fmt, 0x02, BT_MESH_SENSOR_VALUE_CONVERSION_ERROR, 0.0f, true); } -ZTEST(sensor_types_test, test_correlated_col_temp) +ZTEST(sensor_types_test, test_format_coefficient) { - const struct bt_mesh_sensor_type *sensor_type; + const struct bt_mesh_sensor_format *fmt = &bt_mesh_sensor_format_coefficient; - uint16_t sensor[] = { - BT_MESH_PROP_ID_INITIAL_CORRELATED_COL_TEMP, - BT_MESH_PROP_ID_PRESENT_CORRELATED_COL_TEMP, + /* Coefficient is encoded as a raw 32 bit float */ + float test_vector_float[] = { + -FLT_MAX, -FLT_MIN, 0.0, FLT_MIN, *(float *)&(uint32_t){ 0x12345678 }, FLT_MAX }; - for (int i = 0; i < ARRAY_SIZE(sensor); i++) { - sensor_type = bt_mesh_sensor_type_get(sensor[i]); - sensor_type_sanitize(sensor_type); - correlated_color_temp_check(sensor_type); - } -} - -ZTEST(sensor_types_test, test_present_illuminance) -{ - const struct bt_mesh_sensor_type *sensor_type; - - sensor_type = bt_mesh_sensor_type_get(BT_MESH_PROP_ID_PRESENT_ILLUMINANCE); - sensor_type_sanitize(sensor_type); - illuminance_check(sensor_type); -} - -ZTEST(sensor_types_test, test_luminous_flux) -{ - const struct bt_mesh_sensor_type *sensor_type; - - uint16_t sensor[] = { - BT_MESH_PROP_ID_INITIAL_LUMINOUS_FLUX, - BT_MESH_PROP_ID_PRESENT_LUMINOUS_FLUX, - }; + for (int i = 0; i < ARRAY_SIZE(test_vector_float); i++) { + float f = test_vector_float[i]; + uint32_t raw = *(uint32_t *)&f; - for (int i = 0; i < ARRAY_SIZE(sensor); i++) { - sensor_type = bt_mesh_sensor_type_get(sensor[i]); - sensor_type_sanitize(sensor_type); - luminous_flux_check(sensor_type); + check_from_float(fmt, f, 0, raw, true); + check_to_float(fmt, raw, BT_MESH_SENSOR_VALUE_NUMBER, f, true); + check_to_status(fmt, raw, BT_MESH_SENSOR_VALUE_NUMBER); } -} -ZTEST(sensor_types_test, test_planckian_distance) -{ - const struct bt_mesh_sensor_type *sensor_type; + int64_t test_vector_micro[] = { INT64_MIN, 0, 1, 1000000, INT64_MAX }; - uint16_t sensor[] = { - BT_MESH_PROP_ID_INITIAL_PLANCKIAN_DISTANCE, - BT_MESH_PROP_ID_PRESENT_PLANCKIAN_DISTANCE, - }; + for (int i = 0; i < ARRAY_SIZE(test_vector_micro); i++) { + int64_t micro = test_vector_micro[i]; + float f = micro / (double)MICRO; + uint32_t raw = *(uint32_t *)&f; - for (int i = 0; i < ARRAY_SIZE(sensor); i++) { - sensor_type = bt_mesh_sensor_type_get(sensor[i]); - sensor_type_sanitize(sensor_type); - chromatic_distance_check(sensor_type); + check_from_micro(fmt, micro, 0, raw, false); + check_to_micro(fmt, raw, BT_MESH_SENSOR_VALUE_NUMBER, micro, false); + check_to_status(fmt, raw, BT_MESH_SENSOR_VALUE_NUMBER); } -} - -ZTEST(sensor_types_test, test_rel_exposure_time_in_an_illuminance_range) -{ - const struct bt_mesh_sensor_type *sensor_type; - - sensor_type = bt_mesh_sensor_type_get( - BT_MESH_PROP_ID_REL_EXPOSURE_TIME_IN_AN_ILLUMINANCE_RANGE); - sensor_type_sanitize(sensor_type); - percentage8_illuminance_check(sensor_type); -} - -ZTEST(sensor_types_test, test_tot_light_exposure_time) -{ - const struct bt_mesh_sensor_type *sensor_type; - - sensor_type = bt_mesh_sensor_type_get(BT_MESH_PROP_ID_TOT_LIGHT_EXPOSURE_TIME); - sensor_type_sanitize(sensor_type); - time_hour_24_check(sensor_type); -} - -ZTEST(sensor_types_test, test_lumen_maintenance_factor) -{ - const struct bt_mesh_sensor_type *sensor_type; - - sensor_type = bt_mesh_sensor_type_get(BT_MESH_PROP_ID_LUMEN_MAINTENANCE_FACTOR); - sensor_type_sanitize(sensor_type); - percentage8_check(sensor_type); -} - -ZTEST(sensor_types_test, test_luminous_efficacy) -{ - const struct bt_mesh_sensor_type *sensor_type; - - sensor_type = bt_mesh_sensor_type_get(BT_MESH_PROP_ID_LUMINOUS_EFFICACY); - sensor_type_sanitize(sensor_type); - luminous_efficacy_check(sensor_type); -} - -ZTEST(sensor_types_test, test_luminous_energy_since_turn_on) -{ - const struct bt_mesh_sensor_type *sensor_type; - - sensor_type = bt_mesh_sensor_type_get(BT_MESH_PROP_ID_LUMINOUS_ENERGY_SINCE_TURN_ON); - sensor_type_sanitize(sensor_type); - luminous_energy_check(sensor_type); -} - -ZTEST(sensor_types_test, test_luminous_exposure) -{ - const struct bt_mesh_sensor_type *sensor_type; - - sensor_type = bt_mesh_sensor_type_get(BT_MESH_PROP_ID_LUMINOUS_EXPOSURE); - sensor_type_sanitize(sensor_type); - luminous_exposure_check(sensor_type); -} - -ZTEST(sensor_types_test, test_luminous_flux_range) -{ - const struct bt_mesh_sensor_type *sensor_type; - - sensor_type = bt_mesh_sensor_type_get(BT_MESH_PROP_ID_LUMINOUS_FLUX_RANGE); - sensor_type_sanitize(sensor_type); - luminous_flux_range_check(sensor_type); -} - -/* Ambient temperature sensors */ - -ZTEST(sensor_types_test, test_avg_amb_temp_in_period_of_day) -{ - const struct bt_mesh_sensor_type *sensor_type; - - sensor_type = bt_mesh_sensor_type_get(BT_MESH_PROP_ID_AVG_AMB_TEMP_IN_A_PERIOD_OF_DAY); - sensor_type_sanitize(sensor_type); - temp8_in_period_of_day_check(sensor_type); -} - -ZTEST(sensor_types_test, test_indoor_amb_temp_stat_values) -{ - const struct bt_mesh_sensor_type *sensor_type; - - sensor_type = bt_mesh_sensor_type_get(BT_MESH_PROP_ID_INDOOR_AMB_TEMP_STAT_VALUES); - sensor_type_sanitize(sensor_type); - temp8_statistics_check(sensor_type); -} -ZTEST(sensor_types_test, test_outdoor_stat_values) -{ - const struct bt_mesh_sensor_type *sensor_type; + /* Check clamping behavior for converting large numbers to micro */ + float gt_i64_max = nextafterf(INT64_MAX, INFINITY); + float lt_i64_min = nextafterf(INT64_MIN, -INFINITY); - sensor_type = bt_mesh_sensor_type_get(BT_MESH_PROP_ID_OUTDOOR_STAT_VALUES); - sensor_type_sanitize(sensor_type); - temp8_statistics_check(sensor_type); + check_to_micro(fmt, *(uint32_t *)>_i64_max, + BT_MESH_SENSOR_VALUE_CLAMPED, INT64_MAX, true); + check_to_micro(fmt, *(uint32_t *)<_i64_min, + BT_MESH_SENSOR_VALUE_CLAMPED, INT64_MIN, true); } -ZTEST(sensor_types_test, test_present_amb_temp) -{ - const struct bt_mesh_sensor_type *sensor_type; +TEST_SENSOR_TYPE(motion_sensed, 0x0042, CHANNEL(percentage_8, 1)) - sensor_type = bt_mesh_sensor_type_get(BT_MESH_PROP_ID_PRESENT_AMB_TEMP); - sensor_type_sanitize(sensor_type); - temp8_check(sensor_type); -} +TEST_SENSOR_TYPE(motion_threshold, 0x0043, CHANNEL(percentage_8, 1)) -ZTEST(sensor_types_test, test_present_indoor_amb_temp) -{ - const struct bt_mesh_sensor_type *sensor_type; +TEST_SENSOR_TYPE(people_count, 0x004c, CHANNEL(count_16, 2)) - sensor_type = bt_mesh_sensor_type_get(BT_MESH_PROP_ID_PRESENT_INDOOR_AMB_TEMP); - sensor_type_sanitize(sensor_type); - temp8_check(sensor_type); -} +TEST_SENSOR_TYPE(presence_detected, 0x004d, CHANNEL(boolean, 1)) -ZTEST(sensor_types_test, test_present_outdoor_amb_temp) -{ - const struct bt_mesh_sensor_type *sensor_type; +TEST_SENSOR_TYPE(time_since_motion_sensed, 0x0068, CHANNEL(time_millisecond_24, 3)) - sensor_type = bt_mesh_sensor_type_get(BT_MESH_PROP_ID_PRESENT_OUTDOOR_AMB_TEMP); - sensor_type_sanitize(sensor_type); - temp8_check(sensor_type); -} +TEST_SENSOR_TYPE(time_since_presence_detected, 0x0069, CHANNEL(time_second_16, 2)) -ZTEST(sensor_types_test, test_desired_amb_temp) -{ - const struct bt_mesh_sensor_type *sensor_type; +TEST_SENSOR_TYPE(avg_amb_temp_in_day, 0x0001, + CHANNEL(temp_8, 1), + CHANNEL(time_decihour_8, 1), + CHANNEL(time_decihour_8, 1)) - sensor_type = bt_mesh_sensor_type_get(BT_MESH_PROP_ID_DESIRED_AMB_TEMP); - sensor_type_sanitize(sensor_type); - temp8_check(sensor_type); -} +TEST_SENSOR_TYPE(indoor_amb_temp_stat_values, 0x001C, + CHANNEL(temp_8, 1), + CHANNEL(temp_8, 1), + CHANNEL(temp_8, 1), + CHANNEL(temp_8, 1), + CHANNEL(time_exp_8, 1)) -ZTEST(sensor_types_test, test_precise_present_amb_temp) -{ - const struct bt_mesh_sensor_type *sensor_type; +TEST_SENSOR_TYPE(outdoor_stat_values, 0x0045, + CHANNEL(temp_8, 1), + CHANNEL(temp_8, 1), + CHANNEL(temp_8, 1), + CHANNEL(temp_8, 1), + CHANNEL(time_exp_8, 1)) - sensor_type = bt_mesh_sensor_type_get(BT_MESH_PROP_ID_PRECISE_PRESENT_AMB_TEMP); - sensor_type_sanitize(sensor_type); - temp_check(sensor_type); -} +TEST_SENSOR_TYPE(present_amb_temp, 0x004f, CHANNEL(temp_8, 1)) -/* Energy management sensors */ +TEST_SENSOR_TYPE(present_indoor_amb_temp, 0x0056, CHANNEL(temp_8, 1)) -ZTEST(sensor_types_test, test_dev_power_range_spec) -{ - const struct bt_mesh_sensor_type *sensor_type; +TEST_SENSOR_TYPE(present_outdoor_amb_temp, 0x005b, CHANNEL(temp_8, 1)) - sensor_type = bt_mesh_sensor_type_get(BT_MESH_PROP_ID_DEV_POWER_RANGE_SPEC); - sensor_type_sanitize(sensor_type); - power_specification_check(sensor_type); -} +TEST_SENSOR_TYPE(desired_amb_temp, 0x0071, CHANNEL(temp_8, 1)) -ZTEST(sensor_types_test, test_present_dev_input_power) -{ - const struct bt_mesh_sensor_type *sensor_type; +TEST_SENSOR_TYPE(precise_present_amb_temp, 0x0075, CHANNEL(temp, 2)) - sensor_type = bt_mesh_sensor_type_get(BT_MESH_PROP_ID_PRESENT_DEV_INPUT_POWER); - sensor_type_sanitize(sensor_type); - power_check(sensor_type); -} +TEST_SENSOR_TYPE(apparent_wind_direction, 0x0085, CHANNEL(direction_16, 2)) -ZTEST(sensor_types_test, test_present_dev_op_efficiency) -{ - const struct bt_mesh_sensor_type *sensor_type; +TEST_SENSOR_TYPE(apparent_wind_speed, 0x0086, CHANNEL(wind_speed, 2)) - sensor_type = bt_mesh_sensor_type_get(BT_MESH_PROP_ID_PRESENT_DEV_OP_EFFICIENCY); - sensor_type_sanitize(sensor_type); - percentage8_check(sensor_type); -} +TEST_SENSOR_TYPE(dew_point, 0x0087, CHANNEL(temp_8_wide, 1)) -ZTEST(sensor_types_test, test_tot_dev_energy_use) -{ - const struct bt_mesh_sensor_type *sensor_type; +TEST_SENSOR_TYPE(gust_factor, 0x008a, CHANNEL(gust_factor, 1)) - sensor_type = bt_mesh_sensor_type_get(BT_MESH_PROP_ID_TOT_DEV_ENERGY_USE); - sensor_type_sanitize(sensor_type); - energy_check(sensor_type); -} +TEST_SENSOR_TYPE(heat_index, 0x008b, CHANNEL(temp_8_wide, 1)) -ZTEST(sensor_types_test, test_precise_tot_dev_energy_use) -{ - const struct bt_mesh_sensor_type *sensor_type; +TEST_SENSOR_TYPE(present_amb_rel_humidity, 0x0076, CHANNEL(percentage_16, 2)) - sensor_type = bt_mesh_sensor_type_get(BT_MESH_PROP_ID_PRECISE_TOT_DEV_ENERGY_USE); - sensor_type_sanitize(sensor_type); - energy32_check(sensor_type); -} +TEST_SENSOR_TYPE(present_amb_co2_concentration, 0x0077, CHANNEL(co2_concentration, 2)) -ZTEST(sensor_types_test, test_dev_energy_use_since_turn_on) -{ - const struct bt_mesh_sensor_type *sensor_type; +TEST_SENSOR_TYPE(present_amb_voc_concentration, 0x0078, CHANNEL(voc_concentration, 2)) - sensor_type = bt_mesh_sensor_type_get(BT_MESH_PROP_ID_DEV_ENERGY_USE_SINCE_TURN_ON); - sensor_type_sanitize(sensor_type); - energy_check(sensor_type); -} +TEST_SENSOR_TYPE(present_amb_noise, 0x0079, CHANNEL(noise, 1)) -ZTEST(sensor_types_test, test_power_factor) -{ - const struct bt_mesh_sensor_type *sensor_type; +TEST_SENSOR_TYPE(present_indoor_relative_humidity, 0x00a7, CHANNEL(percentage_16, 2)) - sensor_type = bt_mesh_sensor_type_get(BT_MESH_PROP_ID_POWER_FACTOR); - sensor_type_sanitize(sensor_type); - cos_of_the_angle_check(sensor_type); -} +TEST_SENSOR_TYPE(present_outdoor_relative_humidity, 0x00a8, CHANNEL(percentage_16, 2)) -ZTEST(sensor_types_test, test_rel_dev_energy_use_in_a_period_of_day) -{ - const struct bt_mesh_sensor_type *sensor_type; +TEST_SENSOR_TYPE(magnetic_declination, 0x00a1, CHANNEL(direction_16, 2)) - sensor_type = bt_mesh_sensor_type_get( - BT_MESH_PROP_ID_REL_DEV_ENERGY_USE_IN_A_PERIOD_OF_DAY); - sensor_type_sanitize(sensor_type); - energy_in_a_period_of_day_check(sensor_type); -} +TEST_SENSOR_TYPE(magnetic_flux_density_2d, 0x00a2, + CHANNEL(magnetic_flux_density, 2), + CHANNEL(magnetic_flux_density, 2)) -ZTEST(sensor_types_test, test_apparent_energy) -{ - const struct bt_mesh_sensor_type *sensor_type; +TEST_SENSOR_TYPE(magnetic_flux_density_3d, 0x00a3, + CHANNEL(magnetic_flux_density, 2), + CHANNEL(magnetic_flux_density, 2), + CHANNEL(magnetic_flux_density, 2)) - sensor_type = bt_mesh_sensor_type_get(BT_MESH_PROP_ID_APPARENT_ENERGY); - sensor_type_sanitize(sensor_type); - apparent_energy32_check(sensor_type); -} +TEST_SENSOR_TYPE(pollen_concentration, 0x00a6, CHANNEL(pollen_concentration, 3)) -ZTEST(sensor_types_test, test_apparent_power) -{ - const struct bt_mesh_sensor_type *sensor_type; +TEST_SENSOR_TYPE(air_pressure, 0x0082, CHANNEL(pressure, 4)) - sensor_type = bt_mesh_sensor_type_get(BT_MESH_PROP_ID_APPARENT_POWER); - sensor_type_sanitize(sensor_type); - apparent_power_check(sensor_type); -} +TEST_SENSOR_TYPE(pressure, 0x00a9, CHANNEL(pressure, 4)) -ZTEST(sensor_types_test, test_active_energy_loadside) -{ - const struct bt_mesh_sensor_type *sensor_type; +TEST_SENSOR_TYPE(rainfall, 0x00aa, CHANNEL(rainfall, 2)) - sensor_type = bt_mesh_sensor_type_get(BT_MESH_PROP_ID_ACTIVE_ENERGY_LOADSIDE); - sensor_type_sanitize(sensor_type); - energy32_check(sensor_type); -} +TEST_SENSOR_TYPE(true_wind_direction, 0x00af, CHANNEL(direction_16, 2)) -ZTEST(sensor_types_test, test_active_power_loadside) -{ - const struct bt_mesh_sensor_type *sensor_type; +TEST_SENSOR_TYPE(true_wind_speed, 0x00b0, CHANNEL(wind_speed, 2)) - sensor_type = bt_mesh_sensor_type_get(BT_MESH_PROP_ID_ACTIVE_POWER_LOADSIDE); - sensor_type_sanitize(sensor_type); - power_check(sensor_type); -} +TEST_SENSOR_TYPE(uv_index, 0x00b1, CHANNEL(uv_index, 1)) -/* Warranty and service sensors */ +TEST_SENSOR_TYPE(wind_chill, 0x00b2, CHANNEL(temp_8_wide, 1)) -ZTEST(sensor_types_test, test_rel_dev_runtime_in_a_generic_level_range) -{ - const struct bt_mesh_sensor_type *sensor_type; +TEST_SENSOR_TYPE(dev_op_temp_range_spec, 0x0013, CHANNEL(temp, 2), CHANNEL(temp, 2)) - sensor_type = bt_mesh_sensor_type_get( - BT_MESH_PROP_ID_REL_DEV_RUNTIME_IN_A_GENERIC_LEVEL_RANGE); - sensor_type_sanitize(sensor_type); - relative_runtime_in_a_generic_level_range_check(sensor_type); -} +TEST_SENSOR_TYPE(dev_op_temp_stat_values, 0x0014, + CHANNEL(temp, 2), + CHANNEL(temp, 2), + CHANNEL(temp, 2), + CHANNEL(temp, 2), + CHANNEL(time_exp_8, 1)) -ZTEST(sensor_types_test, test_gain) -{ - const struct bt_mesh_sensor_type *sensor_type; +TEST_SENSOR_TYPE(present_dev_op_temp, 0x0054, CHANNEL(temp, 2)) - sensor_type = bt_mesh_sensor_type_get(BT_MESH_PROP_ID_SENSOR_GAIN); - sensor_type_sanitize(sensor_type); - gain_check(sensor_type); -} +TEST_SENSOR_TYPE(rel_runtime_in_a_dev_op_temp_range, 0x0064, + CHANNEL(percentage_8, 1), + CHANNEL(temp, 2), + CHANNEL(temp, 2)) -/* Electrical input sensors */ +TEST_SENSOR_TYPE(avg_input_current, 0x0002, + CHANNEL(electric_current, 2), + CHANNEL(time_exp_8, 1)) -ZTEST(sensor_types_test, test_avg_input_current) -{ - const struct bt_mesh_sensor_type *sensor_type; +TEST_SENSOR_TYPE(avg_input_voltage, 0x0003, + CHANNEL(voltage, 2), + CHANNEL(time_exp_8, 1)) - sensor_type = bt_mesh_sensor_type_get(BT_MESH_PROP_ID_AVG_INPUT_CURRENT); - sensor_type_sanitize(sensor_type); - average_current_check(sensor_type); -} +TEST_SENSOR_TYPE(input_current_range_spec, 0x0021, + CHANNEL(electric_current, 2), + CHANNEL(electric_current, 2), + CHANNEL(electric_current, 2)) -ZTEST(sensor_types_test, test_avg_input_voltage) -{ - const struct bt_mesh_sensor_type *sensor_type; +TEST_SENSOR_TYPE(input_current_stat, 0x0022, + CHANNEL(electric_current, 2), + CHANNEL(electric_current, 2), + CHANNEL(electric_current, 2), + CHANNEL(electric_current, 2), + CHANNEL(time_exp_8, 1)) - sensor_type = bt_mesh_sensor_type_get(BT_MESH_PROP_ID_AVG_INPUT_VOLTAGE); - sensor_type_sanitize(sensor_type); - average_voltage_check(sensor_type); -} +TEST_SENSOR_TYPE(input_voltage_range_spec, 0x0028, + CHANNEL(voltage, 2), + CHANNEL(voltage, 2), + CHANNEL(voltage, 2)) -ZTEST(sensor_types_test, test_input_current_range_spec) -{ - const struct bt_mesh_sensor_type *sensor_type; +TEST_SENSOR_TYPE(input_voltage_stat, 0x002a, + CHANNEL(voltage, 2), + CHANNEL(voltage, 2), + CHANNEL(voltage, 2), + CHANNEL(voltage, 2), + CHANNEL(time_exp_8, 1)) - sensor_type = bt_mesh_sensor_type_get(BT_MESH_PROP_ID_INPUT_CURRENT_RANGE_SPEC); - sensor_type_sanitize(sensor_type); - electric_current_check(sensor_type); -} +TEST_SENSOR_TYPE(present_input_current, 0x0057, CHANNEL(electric_current, 2)) -ZTEST(sensor_types_test, test_input_current_stat) -{ - const struct bt_mesh_sensor_type *sensor_type; +TEST_SENSOR_TYPE(present_input_ripple_voltage, 0x0058, CHANNEL(percentage_8, 1)) - sensor_type = bt_mesh_sensor_type_get(BT_MESH_PROP_ID_INPUT_CURRENT_STAT); - sensor_type_sanitize(sensor_type); - current_stat_check(sensor_type); -} +TEST_SENSOR_TYPE(present_input_voltage, 0x0059, CHANNEL(voltage, 2)) -ZTEST(sensor_types_test, test_input_voltage_range_spec) -{ - const struct bt_mesh_sensor_type *sensor_type; +TEST_SENSOR_TYPE(rel_runtime_in_an_input_current_range, 0x0065, + CHANNEL(percentage_8, 1), + CHANNEL(electric_current, 2), + CHANNEL(electric_current, 2)) - sensor_type = bt_mesh_sensor_type_get(BT_MESH_PROP_ID_INPUT_VOLTAGE_RANGE_SPEC); - sensor_type_sanitize(sensor_type); - voltage_check(sensor_type); -} +TEST_SENSOR_TYPE(rel_runtime_in_an_input_voltage_range, 0x0066, + CHANNEL(percentage_8, 1), + CHANNEL(voltage, 2), + CHANNEL(voltage, 2)) -ZTEST(sensor_types_test, test_input_voltage_stat) -{ - const struct bt_mesh_sensor_type *sensor_type; +TEST_SENSOR_TYPE(dev_power_range_spec, 0x0016, + CHANNEL(power, 3), + CHANNEL(power, 3), + CHANNEL(power, 3)) - sensor_type = bt_mesh_sensor_type_get(BT_MESH_PROP_ID_INPUT_VOLTAGE_STAT); - sensor_type_sanitize(sensor_type); - voltage_stat_check(sensor_type); -} +TEST_SENSOR_TYPE(present_dev_input_power, 0x0052, CHANNEL(power, 3)) -ZTEST(sensor_types_test, test_present_input_current) -{ - const struct bt_mesh_sensor_type *sensor_type; +TEST_SENSOR_TYPE(present_dev_op_efficiency, 0x0053, CHANNEL(percentage_8, 1)) - sensor_type = bt_mesh_sensor_type_get(BT_MESH_PROP_ID_PRESENT_INPUT_CURRENT); - sensor_type_sanitize(sensor_type); - electric_current_check(sensor_type); -} +TEST_SENSOR_TYPE(tot_dev_energy_use, 0x006a, CHANNEL(energy, 3)) -ZTEST(sensor_types_test, test_present_input_ripple_voltage) -{ - const struct bt_mesh_sensor_type *sensor_type; +TEST_SENSOR_TYPE(precise_tot_dev_energy_use, 0x0072, CHANNEL(energy32, 4)) - sensor_type = bt_mesh_sensor_type_get(BT_MESH_PROP_ID_PRESENT_INPUT_RIPPLE_VOLTAGE); - sensor_type_sanitize(sensor_type); - percentage8_check(sensor_type); -} +TEST_SENSOR_TYPE(dev_energy_use_since_turn_on, 0x000d, CHANNEL(energy, 3)) -ZTEST(sensor_types_test, test_present_input_voltage) -{ - const struct bt_mesh_sensor_type *sensor_type; +TEST_SENSOR_TYPE(power_factor, 0x0073, CHANNEL(cos_of_the_angle, 1)) - sensor_type = bt_mesh_sensor_type_get(BT_MESH_PROP_ID_PRESENT_INPUT_VOLTAGE); - sensor_type_sanitize(sensor_type); - voltage_check(sensor_type); -} +TEST_SENSOR_TYPE(rel_dev_energy_use_in_a_period_of_day, 0x0060, + CHANNEL(energy, 3), + CHANNEL(time_decihour_8, 1), + CHANNEL(time_decihour_8, 1)) -ZTEST(sensor_types_test, test_rel_runtime_in_an_input_current_range) -{ - const struct bt_mesh_sensor_type *sensor_type; +TEST_SENSOR_TYPE(apparent_energy, 0x0083, CHANNEL(apparent_energy32, 4)) - sensor_type = bt_mesh_sensor_type_get( - BT_MESH_PROP_ID_REL_RUNTIME_IN_AN_INPUT_CURRENT_RANGE); - sensor_type_sanitize(sensor_type); - rel_runtime_in_current_range_check(sensor_type); -} +TEST_SENSOR_TYPE(apparent_power, 0x0084, CHANNEL(apparent_power, 3)) -ZTEST(sensor_types_test, test_rel_runtime_in_an_input_voltage_range) -{ - const struct bt_mesh_sensor_type *sensor_type; +TEST_SENSOR_TYPE(active_energy_loadside, 0x0080, CHANNEL(energy32, 4)) - sensor_type = bt_mesh_sensor_type_get( - BT_MESH_PROP_ID_REL_RUNTIME_IN_AN_INPUT_VOLTAGE_RANGE); - sensor_type_sanitize(sensor_type); - rel_runtime_in_voltage_range_check(sensor_type); -} +TEST_SENSOR_TYPE(active_power_loadside, 0x0081, CHANNEL(power, 3)) -/* Device operating temperature sensors */ +TEST_SENSOR_TYPE(present_amb_light_level, 0x004E, CHANNEL(illuminance, 3)) -ZTEST(sensor_types_test, test_dev_op_temp_range_spec) -{ - const struct bt_mesh_sensor_type *sensor_type; +TEST_SENSOR_TYPE(initial_cie_1931_chromaticity_coords, 0x001d, + CHANNEL(chromaticity_coordinate, 2), + CHANNEL(chromaticity_coordinate, 2)) - sensor_type = bt_mesh_sensor_type_get(BT_MESH_PROP_ID_DEV_OP_TEMP_RANGE_SPEC); - sensor_type_sanitize(sensor_type); - temp_check(sensor_type); -} +TEST_SENSOR_TYPE(present_cie_1931_chromaticity_coords, 0x0050, + CHANNEL(chromaticity_coordinate, 2), + CHANNEL(chromaticity_coordinate, 2)) -ZTEST(sensor_types_test, test_present_dev_op_temp) -{ - const struct bt_mesh_sensor_type *sensor_type; +TEST_SENSOR_TYPE(initial_correlated_col_temp, 0x001e, CHANNEL(correlated_color_temp, 2)) - sensor_type = bt_mesh_sensor_type_get(BT_MESH_PROP_ID_PRESENT_DEV_OP_TEMP); - sensor_type_sanitize(sensor_type); - temp_check(sensor_type); -} +TEST_SENSOR_TYPE(present_correlated_col_temp, 0x0051, CHANNEL(correlated_color_temp, 2)) -ZTEST(sensor_types_test, test_dev_op_temp_stat_values) -{ - const struct bt_mesh_sensor_type *sensor_type; +TEST_SENSOR_TYPE(present_illuminance, 0x0055, CHANNEL(illuminance, 3)) - sensor_type = bt_mesh_sensor_type_get(BT_MESH_PROP_ID_DEV_OP_TEMP_STAT_VALUES); - sensor_type_sanitize(sensor_type); - temp_stat_check(sensor_type); -} +TEST_SENSOR_TYPE(initial_luminous_flux, 0x001f, CHANNEL(luminous_flux, 2)) -ZTEST(sensor_types_test, test_rel_runtime_in_a_dev_op_temp_range) -{ - const struct bt_mesh_sensor_type *sensor_type; +TEST_SENSOR_TYPE(present_luminous_flux, 0x005a, CHANNEL(luminous_flux, 2)) - sensor_type = bt_mesh_sensor_type_get(BT_MESH_PROP_ID_REL_RUNTIME_IN_A_DEV_OP_TEMP_RANGE); - sensor_type_sanitize(sensor_type); - rel_val_in_a_temp_range_check(sensor_type); -} +TEST_SENSOR_TYPE(initial_planckian_distance, 0x0020, CHANNEL(chromatic_distance, 2)) -/* Power output supply sensors*/ +TEST_SENSOR_TYPE(present_planckian_distance, 0x005e, CHANNEL(chromatic_distance, 2)) -ZTEST(sensor_types_test, test_avg_output_current) -{ - const struct bt_mesh_sensor_type *sensor_type; +TEST_SENSOR_TYPE(rel_exposure_time_in_an_illuminance_range, 0x0062, + CHANNEL(percentage_8, 1), + CHANNEL(illuminance, 3), + CHANNEL(illuminance, 3)) - sensor_type = bt_mesh_sensor_type_get(BT_MESH_PROP_ID_AVG_OUTPUT_CURRENT); - sensor_type_sanitize(sensor_type); - average_current_check(sensor_type); -} +TEST_SENSOR_TYPE(tot_light_exposure_time, 0x006f, CHANNEL(time_hour_24, 3)) -ZTEST(sensor_types_test, test_avg_output_voltage) -{ - const struct bt_mesh_sensor_type *sensor_type; +TEST_SENSOR_TYPE(lumen_maintenance_factor, 0x003d, CHANNEL(percentage_8, 1)) - sensor_type = bt_mesh_sensor_type_get(BT_MESH_PROP_ID_AVG_OUTPUT_VOLTAGE); - sensor_type_sanitize(sensor_type); - average_voltage_check(sensor_type); -} +TEST_SENSOR_TYPE(luminous_efficacy, 0x003e, CHANNEL(luminous_efficacy, 2)) -ZTEST(sensor_types_test, test_output_current_range) -{ - const struct bt_mesh_sensor_type *sensor_type; +TEST_SENSOR_TYPE(luminous_energy_since_turn_on, 0x003f, CHANNEL(luminous_energy, 3)) - sensor_type = bt_mesh_sensor_type_get(BT_MESH_PROP_ID_OUTPUT_CURRENT_RANGE); - sensor_type_sanitize(sensor_type); - electric_current_check(sensor_type); -} +TEST_SENSOR_TYPE(luminous_exposure, 0x0040, CHANNEL(luminous_exposure, 3)) -ZTEST(sensor_types_test, test_output_current_stat) -{ - const struct bt_mesh_sensor_type *sensor_type; +TEST_SENSOR_TYPE(luminous_flux_range, 0x0041, + CHANNEL(luminous_flux, 2), + CHANNEL(luminous_flux, 2)) - sensor_type = bt_mesh_sensor_type_get(BT_MESH_PROP_ID_OUTPUT_CURRENT_STAT); - sensor_type_sanitize(sensor_type); - current_stat_check(sensor_type); -} +TEST_SENSOR_TYPE(avg_output_current, 0x0004, + CHANNEL(electric_current, 2), + CHANNEL(time_exp_8, 1)) -ZTEST(sensor_types_test, test_output_ripple_voltage_spec) -{ - const struct bt_mesh_sensor_type *sensor_type; +TEST_SENSOR_TYPE(avg_output_voltage, 0x0005, + CHANNEL(voltage, 2), + CHANNEL(time_exp_8, 1)) - sensor_type = bt_mesh_sensor_type_get(BT_MESH_PROP_ID_OUTPUT_RIPPLE_VOLTAGE_SPEC); - sensor_type_sanitize(sensor_type); - percentage8_check(sensor_type); -} +TEST_SENSOR_TYPE(output_current_range, 0x0046, + CHANNEL(electric_current, 2), + CHANNEL(electric_current, 2)) -ZTEST(sensor_types_test, test_output_voltage_range) -{ - const struct bt_mesh_sensor_type *sensor_type; +TEST_SENSOR_TYPE(output_current_stat, 0x0047, + CHANNEL(electric_current, 2), + CHANNEL(electric_current, 2), + CHANNEL(electric_current, 2), + CHANNEL(electric_current, 2), + CHANNEL(time_exp_8, 1)) - sensor_type = bt_mesh_sensor_type_get(BT_MESH_PROP_ID_OUTPUT_VOLTAGE_RANGE); - sensor_type_sanitize(sensor_type); - voltage_check(sensor_type); -} +TEST_SENSOR_TYPE(output_ripple_voltage_spec, 0x0048, CHANNEL(percentage_8, 1)) -ZTEST(sensor_types_test, test_output_voltage_stat) -{ - const struct bt_mesh_sensor_type *sensor_type; +TEST_SENSOR_TYPE(output_voltage_range, 0x0049, + CHANNEL(voltage, 2), + CHANNEL(voltage, 2), + CHANNEL(voltage, 2)) - sensor_type = bt_mesh_sensor_type_get(BT_MESH_PROP_ID_OUTPUT_VOLTAGE_STAT); - sensor_type_sanitize(sensor_type); - voltage_stat_check(sensor_type); -} +TEST_SENSOR_TYPE(output_voltage_stat, 0x004a, + CHANNEL(voltage, 2), + CHANNEL(voltage, 2), + CHANNEL(voltage, 2), + CHANNEL(voltage, 2), + CHANNEL(time_exp_8, 1)) -ZTEST(sensor_types_test, test_present_output_current) -{ - const struct bt_mesh_sensor_type *sensor_type; +TEST_SENSOR_TYPE(present_output_current, 0x005c, CHANNEL(electric_current, 2)) - sensor_type = bt_mesh_sensor_type_get(BT_MESH_PROP_ID_PRESENT_OUTPUT_CURRENT); - sensor_type_sanitize(sensor_type); - electric_current_check(sensor_type); -} +TEST_SENSOR_TYPE(present_output_voltage, 0x005d, CHANNEL(voltage, 2)) -ZTEST(sensor_types_test, test_present_output_voltage) -{ - const struct bt_mesh_sensor_type *sensor_type; +TEST_SENSOR_TYPE(present_rel_output_ripple_voltage, 0x005f, CHANNEL(percentage_8, 1)) - sensor_type = bt_mesh_sensor_type_get(BT_MESH_PROP_ID_PRESENT_OUTPUT_VOLTAGE); - sensor_type_sanitize(sensor_type); - voltage_check(sensor_type); -} +TEST_SENSOR_TYPE(gain, 0x0074, CHANNEL(coefficient, 4)) -ZTEST(sensor_types_test, test_present_rel_output_ripple_voltage) -{ - const struct bt_mesh_sensor_type *sensor_type; +TEST_SENSOR_TYPE(rel_dev_runtime_in_a_generic_level_range, 0x0061, + CHANNEL(percentage_8, 1), + CHANNEL(gen_lvl, 2), + CHANNEL(gen_lvl, 2)) - sensor_type = bt_mesh_sensor_type_get(BT_MESH_PROP_ID_PRESENT_REL_OUTPUT_RIPPLE_VOLTAGE); - sensor_type_sanitize(sensor_type); - percentage8_check(sensor_type); -} +TEST_SENSOR_TYPE(total_dev_runtime, 0x006e, CHANNEL(time_hour_24, 3)) ZTEST_SUITE(sensor_types_test, NULL, NULL, NULL, NULL, NULL); diff --git a/tests/subsys/bluetooth/mesh/sensor_subsys_new/CMakeLists.txt b/tests/subsys/bluetooth/mesh/sensor_subsys_new/CMakeLists.txt deleted file mode 100644 index aceb71623e81..000000000000 --- a/tests/subsys/bluetooth/mesh/sensor_subsys_new/CMakeLists.txt +++ /dev/null @@ -1,34 +0,0 @@ -# -# Copyright (c) 2024 Nordic Semiconductor ASA -# -# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause -# -cmake_minimum_required(VERSION 3.20.0) - -find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) -project(bt_mesh_sensor_subsystem_new_test) - -target_include_directories(app PUBLIC - ${ZEPHYR_NRF_MODULE_DIR}/subsys/bluetooth/mesh - ) - -FILE(GLOB app_sources src/*.c) -target_sources(app PRIVATE - ${app_sources} - ${ZEPHYR_NRF_MODULE_DIR}/subsys/bluetooth/mesh/sensor_types.c - ${ZEPHYR_NRF_MODULE_DIR}/subsys/bluetooth/mesh/sensor.c - ) - -target_compile_options(app - PRIVATE - -DCONFIG_BT_MESH_MODEL_KEY_COUNT=5 - -DCONFIG_BT_MESH_MODEL_GROUP_COUNT=5 - -DCONFIG_BT_MESH_SENSOR_ALL_TYPES=1 - -DCONFIG_BT_MESH_SENSOR_LABELS=1 - -DCONFIG_BT_MESH_SENSOR_CHANNELS_MAX=5 - -DCONFIG_BT_MESH_SENSOR_CHANNEL_ENCODED_SIZE_MAX=4 - -DCONFIG_BT_LOG_LEVEL=0 - -DCONFIG_BT_MESH_USES_TINYCRYPT - ) - -zephyr_linker_sources(SECTIONS sensor_types.ld) diff --git a/tests/subsys/bluetooth/mesh/sensor_subsys_new/prj.conf b/tests/subsys/bluetooth/mesh/sensor_subsys_new/prj.conf deleted file mode 100644 index 8415edeff196..000000000000 --- a/tests/subsys/bluetooth/mesh/sensor_subsys_new/prj.conf +++ /dev/null @@ -1,10 +0,0 @@ -# -# Copyright (c) 2024 Nordic Semiconductor ASA -# -# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause -# - -# Ztest configuration -CONFIG_ZTEST=y -CONFIG_CBPRINTF_FP_SUPPORT=y -CONFIG_NET_BUF=y diff --git a/tests/subsys/bluetooth/mesh/sensor_subsys_new/sensor_types.ld b/tests/subsys/bluetooth/mesh/sensor_subsys_new/sensor_types.ld deleted file mode 100644 index 0e9d7b328033..000000000000 --- a/tests/subsys/bluetooth/mesh/sensor_subsys_new/sensor_types.ld +++ /dev/null @@ -1,6 +0,0 @@ -SECTION_DATA_PROLOGUE(bt_mesh_sensor_types_sections,,SUBALIGN(4)) -{ - _bt_mesh_sensor_type_list_start = .; - KEEP(*(SORT_BY_NAME("._bt_mesh_sensor_type.static.*"))); - _bt_mesh_sensor_type_list_end = .; -} GROUP_LINK_IN(ROMABLE_REGION) diff --git a/tests/subsys/bluetooth/mesh/sensor_subsys_new/src/main.c b/tests/subsys/bluetooth/mesh/sensor_subsys_new/src/main.c deleted file mode 100644 index c2a7aa5f2e8a..000000000000 --- a/tests/subsys/bluetooth/mesh/sensor_subsys_new/src/main.c +++ /dev/null @@ -1,978 +0,0 @@ -/* - * Copyright (c) 2024 Nordic Semiconductor ASA - * - * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause - */ - -#include -#include -#include -#include - -#include -#include -#include /* private header from the source folder */ - -#ifndef CONFIG_LITTLE_ENDIAN -BUILD_ASSERT(false, "This test is only supported on little endian platforms"); -#endif - -#define MICRO (1000000LL) - -/****************** types section **********************************/ -struct format_spec { - size_t size; - double ratio; - double min; - double max; - struct { - uint32_t *max_or_greater; - uint32_t *min_or_less; - uint32_t *unknown; - uint32_t *invalid; - uint32_t *prohibited; /* A single prohibited value to test */ - } specials; -}; - -#define SPECIAL(_name, _val) ._name = &(uint32_t){(_val)} - -#define FORMAT_SPEC(_size, _M, _d, _b, _min, _max, ...) \ -{ \ - .size = (_size), \ - .ratio = ((_M) * pow(10.0, (_d)) * exp2((_b))), \ - .min = (_min), \ - .max = (_max), \ - .specials = { __VA_ARGS__ } \ -} - -struct channel_spec { - int size; - const struct bt_mesh_sensor_format *format; -}; - -#define CHANNEL(_format, _size) { \ - .size = _size, \ - .format = &bt_mesh_sensor_format_##_format \ -} -/****************** types section **********************************/ - -static void check_from_micro(const struct bt_mesh_sensor_format *format, - int64_t micro, int expected_err, - uint32_t expected_raw, bool exact) -{ - struct bt_mesh_sensor_value val = { 0 }; - int err = bt_mesh_sensor_value_from_micro(format, micro, &val); - - zassert_equal(err, expected_err); - zassert_equal(val.format, format); - - uint32_t raw = sys_get_le32(val.raw); - - double error; - - if (expected_raw == 0 || exact) { - zassert_mem_equal(val.raw, &expected_raw, 4); - } else if (micro < 0) { - int32_t expected_raw_signed = *(int32_t *)(&expected_raw); - int32_t raw_signed = *(int32_t *)(&raw); - - error = (double)raw_signed / expected_raw_signed; - zassert_between_inclusive(error, 0.99999, 1.00001); - } else { - error = (double)raw / expected_raw; - zassert_between_inclusive(error, 0.99999, 1.00001); - } -} - -static void check_to_micro(const struct bt_mesh_sensor_format *format, - uint32_t raw, - enum bt_mesh_sensor_value_status expected_status, - int64_t expected_micro, bool exact) -{ - struct bt_mesh_sensor_value val = { .format = format }; - enum bt_mesh_sensor_value_status status; - int64_t micro = 0; - - memcpy(val.raw, &raw, 4); - status = bt_mesh_sensor_value_to_micro(&val, µ); - zassert_equal(status, expected_status); - if (expected_micro == 0 || exact) { - zassert_equal(micro, expected_micro); - } else { - double error = (double)micro / (double)expected_micro; - - zassert_between_inclusive(error, 0.99999, 1.00001); - } -} - -static void check_from_float(const struct bt_mesh_sensor_format *format, - float f, int expected_err, - uint32_t expected_raw, bool exact) -{ - struct bt_mesh_sensor_value val = { 0 }; - int err = bt_mesh_sensor_value_from_float(format, f, &val); - - zassert_equal(err, expected_err); - zassert_equal(val.format, format); - - uint32_t raw = sys_get_le32(val.raw); - - double error; - - if (expected_raw == 0 || exact) { - zassert_equal(raw, expected_raw); - } else if (f < 0) { - int32_t expected_raw_signed = *(int32_t *)(&expected_raw); - int32_t raw_signed = *(int32_t *)(&raw); - - error = (double)raw_signed / expected_raw_signed; - zassert_between_inclusive(error, 0.99999, 1.00001); - } else { - error = (double)raw / expected_raw; - zassert_between_inclusive(error, 0.99999, 1.00001); - } -} - -static void check_to_float(const struct bt_mesh_sensor_format *format, - uint32_t raw, - enum bt_mesh_sensor_value_status expected_status, - float expected_f, bool exact) -{ - struct bt_mesh_sensor_value val = { .format = format }; - enum bt_mesh_sensor_value_status status; - float f = 0.0f; - - memcpy(val.raw, &raw, 4); - status = bt_mesh_sensor_value_to_float(&val, &f); - zassert_equal(status, expected_status); - if (expected_f == 0 || exact) { - zassert_equal(f, expected_f); - } else { - double error = (double)f / (double)expected_f; - - zassert_between_inclusive(error, 0.99999, 1.00001); - } -} - -static void check_from_special(const struct bt_mesh_sensor_format *format, - enum bt_mesh_sensor_value_status special, - int expected_err, uint32_t expected_raw) -{ - struct bt_mesh_sensor_value val = { 0 }; - int err = bt_mesh_sensor_value_from_special_status(format, special, &val); - - zassert_equal(err, expected_err); - if (expected_err == 0) { - zassert_mem_equal(val.raw, &expected_raw, 4); - zassert_equal(val.format, format); - } -} - -static void check_to_status(const struct bt_mesh_sensor_format *format, - uint32_t raw, - enum bt_mesh_sensor_value_status expected_status) -{ - struct bt_mesh_sensor_value val = { .format = format }; - enum bt_mesh_sensor_value_status status; - - memcpy(val.raw, &raw, 4); - status = bt_mesh_sensor_value_get_status(&val); - zassert_equal(status, expected_status); -} - -static uint32_t encoded(const struct format_spec *spec, double value) -{ - int64_t val = round(value / spec->ratio); - - val &= GENMASK((spec->size * 8) - 1, 0); - return (uint32_t)val; -} - -/** Finds the biggest float less than or equal to @c limit. */ -static float biggest_float_leq(double limit) -{ - float val = limit; - - return (double)val <= limit ? val : nextafterf(val, -INFINITY); -} - -/** Finds the smallest float greater than or equal to @c limit. */ -static float smallest_float_geq(double limit) -{ - float val = limit; - - return (double)val >= limit ? val : nextafterf(val, INFINITY); -} - -static void check_scalar_format(const struct bt_mesh_sensor_format *format, - const struct format_spec *spec) -{ - /* Decoded values inside [min, max] range */ - int64_t micro_max = round(spec->max * MICRO); - int64_t micro_min = round(spec->min * MICRO); - float float_max = biggest_float_leq(spec->max); - float float_min = smallest_float_geq(spec->min); - - /* Checking encoding/decoding maximum value */ - check_from_micro(format, micro_max, 0, encoded(spec, spec->max), true); - check_from_float(format, float_max, 0, encoded(spec, float_max), false); - check_to_micro(format, encoded(spec, spec->max), - BT_MESH_SENSOR_VALUE_NUMBER, micro_max, true); - check_to_float(format, encoded(spec, spec->max), - BT_MESH_SENSOR_VALUE_NUMBER, spec->max, false); - check_to_status(format, encoded(spec, spec->max), BT_MESH_SENSOR_VALUE_NUMBER); - - /* Checking encoding/decoding minimum value */ - check_from_micro(format, micro_min, 0, encoded(spec, spec->min), true); - check_from_float(format, float_min, 0, encoded(spec, float_min), false); - check_to_micro(format, encoded(spec, spec->min), - BT_MESH_SENSOR_VALUE_NUMBER, micro_min, true); - check_to_float(format, encoded(spec, spec->min), - BT_MESH_SENSOR_VALUE_NUMBER, spec->min, false); - check_to_status(format, encoded(spec, spec->min), BT_MESH_SENSOR_VALUE_NUMBER); - - /* Decoded values outside [min, max] range */ - int64_t micro_max_plus_one = micro_max + round(MICRO * spec->ratio); - int64_t micro_min_minus_one = micro_min - round(MICRO * spec->ratio); - float float_max_plus_one = spec->max + spec->ratio; - float float_min_minus_one = spec->min - spec->ratio; - - uint32_t special_raw; - - /* Checking values bigger than maximum */ - if (spec->specials.max_or_greater) { - /* Checking special value "maximum or greater" */ - special_raw = *(spec->specials.max_or_greater); - check_from_micro(format, micro_max_plus_one, 0, special_raw, true); - check_from_float(format, float_max_plus_one, 0, special_raw, true); - check_from_special(format, BT_MESH_SENSOR_VALUE_MAX_OR_GREATER, 0, special_raw); - check_to_micro(format, special_raw, BT_MESH_SENSOR_VALUE_MAX_OR_GREATER, - micro_max_plus_one, true); - check_to_float(format, special_raw, BT_MESH_SENSOR_VALUE_MAX_OR_GREATER, - float_max_plus_one, false); - check_to_status(format, special_raw, BT_MESH_SENSOR_VALUE_MAX_OR_GREATER); - - /* Checking clamping of values out of range */ - int64_t micro_max_plus_two = micro_max + round(MICRO * 2 * spec->ratio); - float float_max_plus_two = spec->max + (2 * spec->ratio); - - check_from_micro(format, micro_max_plus_two, -ERANGE, special_raw, true); - check_from_float(format, float_max_plus_two, -ERANGE, special_raw, true); - } else { - /* Checking clamping of values out of range */ - check_from_micro(format, micro_max_plus_one, -ERANGE, - encoded(spec, spec->max), true); - check_from_float(format, float_max_plus_one, -ERANGE, - encoded(spec, spec->max), true); - - /* Checking non-supported special value */ - check_from_special(format, BT_MESH_SENSOR_VALUE_MAX_OR_GREATER, -ERANGE, 0); - } - - /* Checking values smaller than minimum */ - if (spec->specials.min_or_less) { - /* Checking special value "minimum or less" */ - special_raw = *(spec->specials.min_or_less); - check_from_micro(format, micro_min_minus_one, 0, special_raw, true); - check_from_float(format, float_min_minus_one, 0, special_raw, true); - check_from_special(format, BT_MESH_SENSOR_VALUE_MIN_OR_LESS, 0, special_raw); - check_to_micro(format, special_raw, BT_MESH_SENSOR_VALUE_MIN_OR_LESS, - micro_min_minus_one, true); - check_to_float(format, special_raw, BT_MESH_SENSOR_VALUE_MIN_OR_LESS, - float_min_minus_one, false); - check_to_status(format, special_raw, BT_MESH_SENSOR_VALUE_MIN_OR_LESS); - - /* Checking clamping of values out of range */ - int64_t micro_min_minus_two = micro_min - round(MICRO * 2 * spec->ratio); - float float_min_minus_two = spec->min - (2 * spec->ratio); - - check_from_micro(format, micro_min_minus_two, -ERANGE, special_raw, true); - check_from_float(format, float_min_minus_two, -ERANGE, special_raw, true); - } else { - /* Checking clamping of values out of range */ - check_from_micro(format, micro_min_minus_one, -ERANGE, - encoded(spec, spec->min), true); - check_from_float(format, float_min_minus_one, -ERANGE, - encoded(spec, spec->min), true); - - /* Checking non-supported special value */ - check_from_special(format, BT_MESH_SENSOR_VALUE_MIN_OR_LESS, -ERANGE, 0); - } - - if (spec->specials.unknown) { - /* Checking special value "value is not known" */ - special_raw = *(spec->specials.unknown); - check_from_special(format, BT_MESH_SENSOR_VALUE_UNKNOWN, 0, special_raw); - check_to_status(format, special_raw, BT_MESH_SENSOR_VALUE_UNKNOWN); - check_to_float(format, special_raw, BT_MESH_SENSOR_VALUE_UNKNOWN, 0.0f, true); - check_to_micro(format, special_raw, BT_MESH_SENSOR_VALUE_UNKNOWN, 0, true); - } else { - /* Checking non-supported special value "value is not known" */ - check_from_special(format, BT_MESH_SENSOR_VALUE_UNKNOWN, -ERANGE, 0); - } - - if (spec->specials.invalid) { - /* Checking special value "value is invalid" */ - special_raw = *(spec->specials.invalid); - check_from_special(format, BT_MESH_SENSOR_VALUE_INVALID, 0, special_raw); - check_to_status(format, special_raw, BT_MESH_SENSOR_VALUE_INVALID); - check_to_float(format, special_raw, BT_MESH_SENSOR_VALUE_INVALID, 0.0f, true); - check_to_micro(format, special_raw, BT_MESH_SENSOR_VALUE_INVALID, 0, true); - } else { - /* Checking non-supported special value "value is invalid" */ - check_from_special(format, BT_MESH_SENSOR_VALUE_INVALID, -ERANGE, 0); - } - - if (spec->specials.prohibited) { - /* Checking decoding a prohibited raw value */ - special_raw = *(spec->specials.prohibited); - check_to_status(format, special_raw, BT_MESH_SENSOR_VALUE_CONVERSION_ERROR); - check_to_float(format, special_raw, BT_MESH_SENSOR_VALUE_CONVERSION_ERROR, - 0.0f, true); - check_to_micro(format, special_raw, BT_MESH_SENSOR_VALUE_CONVERSION_ERROR, - 0, true); - } -} - -#define TEST_SCALAR_FORMAT(_name, _spec) \ -ZTEST(sensor_types_test_new, test_format_##_name) \ -{ \ - struct format_spec spec = _spec; \ - check_scalar_format(&bt_mesh_sensor_format_##_name, &spec); \ -} - -static void check_sensor_type(const struct bt_mesh_sensor_type *type, - uint16_t id, - const struct channel_spec *channels, - size_t channels_count) -{ - uint8_t test_data[BT_MESH_SENSOR_ENCODED_VALUE_MAXLEN]; - struct bt_mesh_sensor_value values[CONFIG_BT_MESH_SENSOR_CHANNELS_MAX]; - int total_size = 0; - - for (int i = 0; i < ARRAY_SIZE(test_data); i++) { - test_data[i] = i + 1; - } - - for (int i = 0; i < channels_count; i++) { - total_size += channels[i].size; - } - - zassert_equal(bt_mesh_sensor_type_get(id), type); - zassert_true(total_size <= ARRAY_SIZE(test_data), - "Not enough test data available to test this type"); - - NET_BUF_SIMPLE_DEFINE(buf, total_size); - (void)net_buf_simple_add_mem(&buf, test_data, total_size); - - zassert_equal(buf.len, total_size); - zassert_true(channels_count <= CONFIG_BT_MESH_SENSOR_CHANNELS_MAX); - zassert_ok(sensor_value_decode(&buf, type, values)); - zassert_equal(buf.len, 0); - - const uint8_t *current_data = test_data; - - for (int i = 0; i < channels_count; i++) { - zassert_equal(values[i].format, channels[i].format); - zassert_equal(values[i].format->size, channels[i].size); - zassert_mem_equal(values[i].raw, current_data, channels[i].size); - current_data += channels[i].size; - } - - net_buf_simple_reset(&buf); - memset(buf.data, 0, total_size); - zassert_ok(sensor_value_encode(&buf, type, values)); - zassert_equal(buf.len, total_size); - zassert_mem_equal(buf.data, test_data, total_size); -} - -#define TEST_SENSOR_TYPE(_name, _id, ...) \ -ZTEST(sensor_types_test_new, test_type_##_name) \ -{ \ - check_sensor_type( \ - &bt_mesh_sensor_##_name, \ - (_id), \ - ((struct channel_spec[]){ __VA_ARGS__ }), \ - ARRAY_SIZE(((struct channel_spec[]){ __VA_ARGS__ }))); \ -} - -TEST_SCALAR_FORMAT(percentage_8, - FORMAT_SPEC(1, 1, 0, -1, 0.0, 100.0, - SPECIAL(unknown, 0xff), - SPECIAL(prohibited, 0xC9))) - -TEST_SCALAR_FORMAT(percentage_16, - FORMAT_SPEC(2, 1, -2, 0, 0.0, 100.0, - SPECIAL(unknown, 0xffff), - SPECIAL(prohibited, 0x2711))) - -TEST_SCALAR_FORMAT(percentage_delta_trigger, - FORMAT_SPEC(2, 1, -2, 0, 0.0, 655.35)) - -TEST_SCALAR_FORMAT(temp_8, - FORMAT_SPEC(1, 1, 0, -1, -64.0, 63.0, - SPECIAL(unknown, 0x7f))) - -TEST_SCALAR_FORMAT(temp, - FORMAT_SPEC(2, 1, -2, 0, -273.15, 327.67, - SPECIAL(unknown, 0x8000), - SPECIAL(prohibited, 0x954C))) - -TEST_SCALAR_FORMAT(co2_concentration, - FORMAT_SPEC(2, 1, 0, 0, 0, 65533, - SPECIAL(max_or_greater, 0xFFFE), - SPECIAL(unknown, 0xFFFF))) - -TEST_SCALAR_FORMAT(noise, - FORMAT_SPEC(1, 1, 0, 0, 0, 253, - SPECIAL(max_or_greater, 0xFE), - SPECIAL(unknown, 0xFF))) - -TEST_SCALAR_FORMAT(voc_concentration, - FORMAT_SPEC(2, 1, 0, 0, 0, 65533, - SPECIAL(max_or_greater, 0xFFFE), - SPECIAL(unknown, 0xFFFF))) - -TEST_SCALAR_FORMAT(wind_speed, - FORMAT_SPEC(2, 1, -2, 0, 0, 655.35)) - -TEST_SCALAR_FORMAT(temp_8_wide, - FORMAT_SPEC(1, 1, 0, 0, -128, 127)) - -TEST_SCALAR_FORMAT(gust_factor, - FORMAT_SPEC(1, 1, -1, 0, 0, 25.5)) - -/* GSS specifies d = -7. Implemented in the stack as d = -1 with unit of - * microtesla, to be able to work with full precision using to_micro/from_micro - * and to/from sensor_value. - * Testing using d = -1 - */ -TEST_SCALAR_FORMAT(magnetic_flux_density, - FORMAT_SPEC(2, 1, -1, 0, -3276.8, 3276.7)) - -TEST_SCALAR_FORMAT(pollen_concentration, - FORMAT_SPEC(3, 1, 0, 0, 0, 16777215)) - -TEST_SCALAR_FORMAT(pressure, - FORMAT_SPEC(4, 1, -1, 0, 0, 429496729.5)) - -TEST_SCALAR_FORMAT(rainfall, - FORMAT_SPEC(2, 1, -3, 0, 0, 65.535)) - -TEST_SCALAR_FORMAT(uv_index, - FORMAT_SPEC(1, 1, 0, 0, 0, 255)) - -TEST_SCALAR_FORMAT(time_decihour_8, - FORMAT_SPEC(1, 1, -1, 0, 0, 23.9, - SPECIAL(unknown, 0xFF), - SPECIAL(prohibited, 0xF0))) - -TEST_SCALAR_FORMAT(time_hour_24, - FORMAT_SPEC(3, 1, 0, 0, 0, 16777214, - SPECIAL(unknown, 0xFFFFFF))) - -TEST_SCALAR_FORMAT(time_second_16, - FORMAT_SPEC(2, 1, 0, 0, 0, 65534, - SPECIAL(unknown, 0xFFFF))) - -TEST_SCALAR_FORMAT(time_millisecond_24, - FORMAT_SPEC(3, 1, -3, 0, 0, 16777.214, - SPECIAL(unknown, 0xFFFFFF))) - -ZTEST(sensor_types_test_new, test_format_time_exp_8) -{ - const struct bt_mesh_sensor_format *fmt = &bt_mesh_sensor_format_time_exp_8; - - /* Specially encoded 0 - * | Lowest normal value - * | | Encodes "1" - * | | | Encodes "1.1" - * | | | | Middle of range - * | | | | | Highest normal value - * v v v v v v - */ - uint32_t test_vector[] = { 0x00, 0x01, 0x40, 0x41, 0x80, 0xfd}; - - for (int i = 0; i < ARRAY_SIZE(test_vector); i++) { - /* Representation: decoded value = 1.1^(encoded - 64) seconds */ - uint32_t raw = test_vector[i]; - /* raw value == 0 is defined to be 0 seconds */ - double exp_1_1 = raw == 0 ? 0.0 : pow(1.1, raw - 64.0); - float f = exp_1_1; - int64_t micro = exp_1_1 * MICRO; - - check_from_micro(fmt, micro, 0, raw, true); - check_from_float(fmt, f, 0, raw, true); - /* Checking with exact == false for to_micro because in this case it is computed - * via float. - */ - check_to_micro(fmt, raw, BT_MESH_SENSOR_VALUE_NUMBER, micro, false); - check_to_float(fmt, raw, BT_MESH_SENSOR_VALUE_NUMBER, f, false); - check_to_status(fmt, raw, BT_MESH_SENSOR_VALUE_NUMBER); - } - - /* Checking special values*/ - uint32_t total_device_life_raw = 0xFE; - uint32_t unknown_raw = 0xFF; - - check_from_special(fmt, BT_MESH_SENSOR_VALUE_TOTAL_DEVICE_LIFE, 0, total_device_life_raw); - check_to_micro(fmt, total_device_life_raw, - BT_MESH_SENSOR_VALUE_TOTAL_DEVICE_LIFE, 0, true); - check_to_float(fmt, total_device_life_raw, - BT_MESH_SENSOR_VALUE_TOTAL_DEVICE_LIFE, 0.0f, true); - check_to_status(fmt, total_device_life_raw, BT_MESH_SENSOR_VALUE_TOTAL_DEVICE_LIFE); - - check_from_special(fmt, BT_MESH_SENSOR_VALUE_UNKNOWN, 0, unknown_raw); - check_to_micro(fmt, unknown_raw, BT_MESH_SENSOR_VALUE_UNKNOWN, 0, true); - check_to_float(fmt, unknown_raw, BT_MESH_SENSOR_VALUE_UNKNOWN, 0.0f, true); - check_to_status(fmt, unknown_raw, BT_MESH_SENSOR_VALUE_UNKNOWN); -} - -TEST_SCALAR_FORMAT(electric_current, - FORMAT_SPEC(2, 1, -2, 0, 0, 655.34, - SPECIAL(unknown, 0xFFFF))) - -/* GSS defines this in a weird way compared to all other characteristics, where - * the values for min (0.0) and max (1022.0) are actually the values for - * "0.0 or less" and "1022.0 or greater". Since the test format spec expects - * these special values to be excluded from the min and max range specified, - * the min and max used here are 0 + 2^-6 and 1022.0 - 2^-6. - */ -TEST_SCALAR_FORMAT(voltage, - FORMAT_SPEC(2, 1, 0, -6, 0.015625, 1021.984375, - SPECIAL(unknown, 0xFFFF), - SPECIAL(max_or_greater, 0xFF80), - SPECIAL(min_or_less, 0x0000), - SPECIAL(prohibited, 0xFF81))) - -TEST_SCALAR_FORMAT(energy32, - FORMAT_SPEC(4, 1, -3, 0, 0, 4294967.293, - SPECIAL(invalid, 0xFFFFFFFE), - SPECIAL(unknown, 0xFFFFFFFF))) - -TEST_SCALAR_FORMAT(apparent_energy32, - FORMAT_SPEC(4, 1, -3, 0, 0, 4294967.293, - SPECIAL(invalid, 0xFFFFFFFE), - SPECIAL(unknown, 0xFFFFFFFF))) - -TEST_SCALAR_FORMAT(apparent_power, - FORMAT_SPEC(3, 1, -1, 0, 0, 1677721.3, - SPECIAL(invalid, 0xFFFFFE), - SPECIAL(unknown, 0xFFFFFF))) - -TEST_SCALAR_FORMAT(power, - FORMAT_SPEC(3, 1, -1, 0, 0, 1677721.4, - SPECIAL(unknown, 0xFFFFFF))) - -TEST_SCALAR_FORMAT(energy, - FORMAT_SPEC(3, 1, 0, 0, 0, 16777214, - SPECIAL(unknown, 0xFFFFFF))) - -TEST_SCALAR_FORMAT(chromatic_distance, - FORMAT_SPEC(2, 1, -5, 0, -0.05, 0.05, - SPECIAL(invalid, 0x7FFF), - SPECIAL(unknown, 0x7FFE), - SPECIAL(prohibited, 0x1389))) - -/* Note: According to GSS, "Unit is unitless with a resolution of 1/65535" and - * "Maximum: 1.0". However, it also states that "b = -16". Since this is - * contradictory and the stack currently encodes/decodes according to - * "b = -16", the max is recomputed in this test to 0.9999847 (~= 65535/65536) - */ -TEST_SCALAR_FORMAT(chromaticity_coordinate, - FORMAT_SPEC(2, 1, 0, -16, 0, 0.9999847)) - -TEST_SCALAR_FORMAT(correlated_color_temp, - FORMAT_SPEC(2, 1, 0, 0, 800, 65534, - SPECIAL(unknown, 0xFFFF), - SPECIAL(prohibited, 0x031f))) - -TEST_SCALAR_FORMAT(illuminance, - FORMAT_SPEC(3, 1, -2, 0, 0, 167772.14, - SPECIAL(unknown, 0xFFFFFF))) - -TEST_SCALAR_FORMAT(luminous_efficacy, - FORMAT_SPEC(2, 1, -1, 0, 0, 1800, - SPECIAL(unknown, 0xFFFF), - SPECIAL(prohibited, 0x4651))) - -TEST_SCALAR_FORMAT(luminous_energy, - FORMAT_SPEC(3, 1, 3, 0, 0, 16777214000.0, - SPECIAL(unknown, 0xFFFFFF))) - -TEST_SCALAR_FORMAT(luminous_exposure, - FORMAT_SPEC(3, 1, 3, 0, 0, 16777214000.0, - SPECIAL(unknown, 0xFFFFFF))) - -TEST_SCALAR_FORMAT(luminous_flux, - FORMAT_SPEC(2, 1, 0, 0, 0, 65534, - SPECIAL(unknown, 0xFFFF))) - -TEST_SCALAR_FORMAT(perceived_lightness, - FORMAT_SPEC(2, 1, 0, 0, 0, 65535)) - -TEST_SCALAR_FORMAT(direction_16, - FORMAT_SPEC(2, 1, -2, 0, 0, 359.99, - SPECIAL(prohibited, 0x8CA0))) - -TEST_SCALAR_FORMAT(count_16, - FORMAT_SPEC(2, 1, 0, 0, 0, 65534, - SPECIAL(unknown, 0xFFFF))) - -TEST_SCALAR_FORMAT(gen_lvl, - FORMAT_SPEC(2, 1, 0, 0, 0, 65535)) - -/* GSS specifies d = -2, however, it also says that the value is - * "expressed as Cos(theta) x 100, with a resolution of 1", implying d = 1. - * Min = -100 and max = 100 are also specified in terms of 100ths of the cosine. - * The stack currently returns values in hundredths, i.e. d = 0, so this is - * what is tested. - */ -TEST_SCALAR_FORMAT(cos_of_the_angle, - FORMAT_SPEC(1, 1, 0, 0, -100, 100, - SPECIAL(unknown, 0x7F), - SPECIAL(prohibited, 0x9B))) - -ZTEST(sensor_types_test_new, test_format_boolean) -{ - const struct bt_mesh_sensor_format *fmt = &bt_mesh_sensor_format_boolean; - - /* Checking false (0) */ - check_from_micro(fmt, 0, 0, 0x00, true); - check_from_float(fmt, 0.0f, 0, 0x00, true); - check_to_micro(fmt, 0x00, BT_MESH_SENSOR_VALUE_NUMBER, 0, true); - check_to_float(fmt, 0x00, BT_MESH_SENSOR_VALUE_NUMBER, 0.0f, true); - check_to_status(fmt, 0x00, BT_MESH_SENSOR_VALUE_NUMBER); - - /* Checking true (1) */ - check_from_micro(fmt, 1 * MICRO, 0, 0x01, true); - check_from_float(fmt, 1.0f, 0, 0x01, true); - check_to_micro(fmt, 0x01, BT_MESH_SENSOR_VALUE_NUMBER, 1 * MICRO, true); - check_to_float(fmt, 0x01, BT_MESH_SENSOR_VALUE_NUMBER, 1.0f, true); - check_to_status(fmt, 0x01, BT_MESH_SENSOR_VALUE_NUMBER); - - /* Checking out of range float/micro */ - check_from_micro(fmt, 2 * MICRO, -ERANGE, 0x01, true); - check_from_float(fmt, 2.0f, -ERANGE, 0x01, true); - - /* Checking prohibited */ - check_to_micro(fmt, 0x02, BT_MESH_SENSOR_VALUE_CONVERSION_ERROR, 0, true); - check_to_float(fmt, 0x02, BT_MESH_SENSOR_VALUE_CONVERSION_ERROR, 0.0f, true); -} - -ZTEST(sensor_types_test_new, test_format_coefficient) -{ - const struct bt_mesh_sensor_format *fmt = &bt_mesh_sensor_format_coefficient; - - /* Coefficient is encoded as a raw 32 bit float */ - float test_vector_float[] = { - -FLT_MAX, -FLT_MIN, 0.0, FLT_MIN, *(float *)&(uint32_t){ 0x12345678 }, FLT_MAX - }; - - for (int i = 0; i < ARRAY_SIZE(test_vector_float); i++) { - float f = test_vector_float[i]; - uint32_t raw = *(uint32_t *)&f; - - check_from_float(fmt, f, 0, raw, true); - check_to_float(fmt, raw, BT_MESH_SENSOR_VALUE_NUMBER, f, true); - check_to_status(fmt, raw, BT_MESH_SENSOR_VALUE_NUMBER); - } - - int64_t test_vector_micro[] = { INT64_MIN, 0, 1, 1000000, INT64_MAX }; - - for (int i = 0; i < ARRAY_SIZE(test_vector_micro); i++) { - int64_t micro = test_vector_micro[i]; - float f = micro / (double)MICRO; - uint32_t raw = *(uint32_t *)&f; - - check_from_micro(fmt, micro, 0, raw, false); - check_to_micro(fmt, raw, BT_MESH_SENSOR_VALUE_NUMBER, micro, false); - check_to_status(fmt, raw, BT_MESH_SENSOR_VALUE_NUMBER); - } - - /* Check clamping behavior for converting large numbers to micro */ - float gt_i64_max = nextafterf(INT64_MAX, INFINITY); - float lt_i64_min = nextafterf(INT64_MIN, -INFINITY); - - check_to_micro(fmt, *(uint32_t *)>_i64_max, - BT_MESH_SENSOR_VALUE_CLAMPED, INT64_MAX, true); - check_to_micro(fmt, *(uint32_t *)<_i64_min, - BT_MESH_SENSOR_VALUE_CLAMPED, INT64_MIN, true); -} - -TEST_SENSOR_TYPE(motion_sensed, 0x0042, CHANNEL(percentage_8, 1)) - -TEST_SENSOR_TYPE(motion_threshold, 0x0043, CHANNEL(percentage_8, 1)) - -TEST_SENSOR_TYPE(people_count, 0x004c, CHANNEL(count_16, 2)) - -TEST_SENSOR_TYPE(presence_detected, 0x004d, CHANNEL(boolean, 1)) - -TEST_SENSOR_TYPE(time_since_motion_sensed, 0x0068, CHANNEL(time_millisecond_24, 3)) - -TEST_SENSOR_TYPE(time_since_presence_detected, 0x0069, CHANNEL(time_second_16, 2)) - -TEST_SENSOR_TYPE(avg_amb_temp_in_day, 0x0001, - CHANNEL(temp_8, 1), - CHANNEL(time_decihour_8, 1), - CHANNEL(time_decihour_8, 1)) - -TEST_SENSOR_TYPE(indoor_amb_temp_stat_values, 0x001C, - CHANNEL(temp_8, 1), - CHANNEL(temp_8, 1), - CHANNEL(temp_8, 1), - CHANNEL(temp_8, 1), - CHANNEL(time_exp_8, 1)) - -TEST_SENSOR_TYPE(outdoor_stat_values, 0x0045, - CHANNEL(temp_8, 1), - CHANNEL(temp_8, 1), - CHANNEL(temp_8, 1), - CHANNEL(temp_8, 1), - CHANNEL(time_exp_8, 1)) - -TEST_SENSOR_TYPE(present_amb_temp, 0x004f, CHANNEL(temp_8, 1)) - -TEST_SENSOR_TYPE(present_indoor_amb_temp, 0x0056, CHANNEL(temp_8, 1)) - -TEST_SENSOR_TYPE(present_outdoor_amb_temp, 0x005b, CHANNEL(temp_8, 1)) - -TEST_SENSOR_TYPE(desired_amb_temp, 0x0071, CHANNEL(temp_8, 1)) - -TEST_SENSOR_TYPE(precise_present_amb_temp, 0x0075, CHANNEL(temp, 2)) - -TEST_SENSOR_TYPE(apparent_wind_direction, 0x0085, CHANNEL(direction_16, 2)) - -TEST_SENSOR_TYPE(apparent_wind_speed, 0x0086, CHANNEL(wind_speed, 2)) - -TEST_SENSOR_TYPE(dew_point, 0x0087, CHANNEL(temp_8_wide, 1)) - -TEST_SENSOR_TYPE(gust_factor, 0x008a, CHANNEL(gust_factor, 1)) - -TEST_SENSOR_TYPE(heat_index, 0x008b, CHANNEL(temp_8_wide, 1)) - -TEST_SENSOR_TYPE(present_amb_rel_humidity, 0x0076, CHANNEL(percentage_16, 2)) - -TEST_SENSOR_TYPE(present_amb_co2_concentration, 0x0077, CHANNEL(co2_concentration, 2)) - -TEST_SENSOR_TYPE(present_amb_voc_concentration, 0x0078, CHANNEL(voc_concentration, 2)) - -TEST_SENSOR_TYPE(present_amb_noise, 0x0079, CHANNEL(noise, 1)) - -TEST_SENSOR_TYPE(present_indoor_relative_humidity, 0x00a7, CHANNEL(percentage_16, 2)) - -TEST_SENSOR_TYPE(present_outdoor_relative_humidity, 0x00a8, CHANNEL(percentage_16, 2)) - -TEST_SENSOR_TYPE(magnetic_declination, 0x00a1, CHANNEL(direction_16, 2)) - -TEST_SENSOR_TYPE(magnetic_flux_density_2d, 0x00a2, - CHANNEL(magnetic_flux_density, 2), - CHANNEL(magnetic_flux_density, 2)) - -TEST_SENSOR_TYPE(magnetic_flux_density_3d, 0x00a3, - CHANNEL(magnetic_flux_density, 2), - CHANNEL(magnetic_flux_density, 2), - CHANNEL(magnetic_flux_density, 2)) - -TEST_SENSOR_TYPE(pollen_concentration, 0x00a6, CHANNEL(pollen_concentration, 3)) - -TEST_SENSOR_TYPE(air_pressure, 0x0082, CHANNEL(pressure, 4)) - -TEST_SENSOR_TYPE(pressure, 0x00a9, CHANNEL(pressure, 4)) - -TEST_SENSOR_TYPE(rainfall, 0x00aa, CHANNEL(rainfall, 2)) - -TEST_SENSOR_TYPE(true_wind_direction, 0x00af, CHANNEL(direction_16, 2)) - -TEST_SENSOR_TYPE(true_wind_speed, 0x00b0, CHANNEL(wind_speed, 2)) - -TEST_SENSOR_TYPE(uv_index, 0x00b1, CHANNEL(uv_index, 1)) - -TEST_SENSOR_TYPE(wind_chill, 0x00b2, CHANNEL(temp_8_wide, 1)) - -TEST_SENSOR_TYPE(dev_op_temp_range_spec, 0x0013, CHANNEL(temp, 2), CHANNEL(temp, 2)) - -TEST_SENSOR_TYPE(dev_op_temp_stat_values, 0x0014, - CHANNEL(temp, 2), - CHANNEL(temp, 2), - CHANNEL(temp, 2), - CHANNEL(temp, 2), - CHANNEL(time_exp_8, 1)) - -TEST_SENSOR_TYPE(present_dev_op_temp, 0x0054, CHANNEL(temp, 2)) - -TEST_SENSOR_TYPE(rel_runtime_in_a_dev_op_temp_range, 0x0064, - CHANNEL(percentage_8, 1), - CHANNEL(temp, 2), - CHANNEL(temp, 2)) - -TEST_SENSOR_TYPE(avg_input_current, 0x0002, - CHANNEL(electric_current, 2), - CHANNEL(time_exp_8, 1)) - -TEST_SENSOR_TYPE(avg_input_voltage, 0x0003, - CHANNEL(voltage, 2), - CHANNEL(time_exp_8, 1)) - -TEST_SENSOR_TYPE(input_current_range_spec, 0x0021, - CHANNEL(electric_current, 2), - CHANNEL(electric_current, 2), - CHANNEL(electric_current, 2)) - -TEST_SENSOR_TYPE(input_current_stat, 0x0022, - CHANNEL(electric_current, 2), - CHANNEL(electric_current, 2), - CHANNEL(electric_current, 2), - CHANNEL(electric_current, 2), - CHANNEL(time_exp_8, 1)) - -TEST_SENSOR_TYPE(input_voltage_range_spec, 0x0028, - CHANNEL(voltage, 2), - CHANNEL(voltage, 2), - CHANNEL(voltage, 2)) - -TEST_SENSOR_TYPE(input_voltage_stat, 0x002a, - CHANNEL(voltage, 2), - CHANNEL(voltage, 2), - CHANNEL(voltage, 2), - CHANNEL(voltage, 2), - CHANNEL(time_exp_8, 1)) - -TEST_SENSOR_TYPE(present_input_current, 0x0057, CHANNEL(electric_current, 2)) - -TEST_SENSOR_TYPE(present_input_ripple_voltage, 0x0058, CHANNEL(percentage_8, 1)) - -TEST_SENSOR_TYPE(present_input_voltage, 0x0059, CHANNEL(voltage, 2)) - -TEST_SENSOR_TYPE(rel_runtime_in_an_input_current_range, 0x0065, - CHANNEL(percentage_8, 1), - CHANNEL(electric_current, 2), - CHANNEL(electric_current, 2)) - -TEST_SENSOR_TYPE(rel_runtime_in_an_input_voltage_range, 0x0066, - CHANNEL(percentage_8, 1), - CHANNEL(voltage, 2), - CHANNEL(voltage, 2)) - -TEST_SENSOR_TYPE(dev_power_range_spec, 0x0016, - CHANNEL(power, 3), - CHANNEL(power, 3), - CHANNEL(power, 3)) - -TEST_SENSOR_TYPE(present_dev_input_power, 0x0052, CHANNEL(power, 3)) - -TEST_SENSOR_TYPE(present_dev_op_efficiency, 0x0053, CHANNEL(percentage_8, 1)) - -TEST_SENSOR_TYPE(tot_dev_energy_use, 0x006a, CHANNEL(energy, 3)) - -TEST_SENSOR_TYPE(precise_tot_dev_energy_use, 0x0072, CHANNEL(energy32, 4)) - -TEST_SENSOR_TYPE(dev_energy_use_since_turn_on, 0x000d, CHANNEL(energy, 3)) - -TEST_SENSOR_TYPE(power_factor, 0x0073, CHANNEL(cos_of_the_angle, 1)) - -TEST_SENSOR_TYPE(rel_dev_energy_use_in_a_period_of_day, 0x0060, - CHANNEL(energy, 3), - CHANNEL(time_decihour_8, 1), - CHANNEL(time_decihour_8, 1)) - -TEST_SENSOR_TYPE(apparent_energy, 0x0083, CHANNEL(apparent_energy32, 4)) - -TEST_SENSOR_TYPE(apparent_power, 0x0084, CHANNEL(apparent_power, 3)) - -TEST_SENSOR_TYPE(active_energy_loadside, 0x0080, CHANNEL(energy32, 4)) - -TEST_SENSOR_TYPE(active_power_loadside, 0x0081, CHANNEL(power, 3)) - -TEST_SENSOR_TYPE(present_amb_light_level, 0x004E, CHANNEL(illuminance, 3)) - -TEST_SENSOR_TYPE(initial_cie_1931_chromaticity_coords, 0x001d, - CHANNEL(chromaticity_coordinate, 2), - CHANNEL(chromaticity_coordinate, 2)) - -TEST_SENSOR_TYPE(present_cie_1931_chromaticity_coords, 0x0050, - CHANNEL(chromaticity_coordinate, 2), - CHANNEL(chromaticity_coordinate, 2)) - -TEST_SENSOR_TYPE(initial_correlated_col_temp, 0x001e, CHANNEL(correlated_color_temp, 2)) - -TEST_SENSOR_TYPE(present_correlated_col_temp, 0x0051, CHANNEL(correlated_color_temp, 2)) - -TEST_SENSOR_TYPE(present_illuminance, 0x0055, CHANNEL(illuminance, 3)) - -TEST_SENSOR_TYPE(initial_luminous_flux, 0x001f, CHANNEL(luminous_flux, 2)) - -TEST_SENSOR_TYPE(present_luminous_flux, 0x005a, CHANNEL(luminous_flux, 2)) - -TEST_SENSOR_TYPE(initial_planckian_distance, 0x0020, CHANNEL(chromatic_distance, 2)) - -TEST_SENSOR_TYPE(present_planckian_distance, 0x005e, CHANNEL(chromatic_distance, 2)) - -TEST_SENSOR_TYPE(rel_exposure_time_in_an_illuminance_range, 0x0062, - CHANNEL(percentage_8, 1), - CHANNEL(illuminance, 3), - CHANNEL(illuminance, 3)) - -TEST_SENSOR_TYPE(tot_light_exposure_time, 0x006f, CHANNEL(time_hour_24, 3)) - -TEST_SENSOR_TYPE(lumen_maintenance_factor, 0x003d, CHANNEL(percentage_8, 1)) - -TEST_SENSOR_TYPE(luminous_efficacy, 0x003e, CHANNEL(luminous_efficacy, 2)) - -TEST_SENSOR_TYPE(luminous_energy_since_turn_on, 0x003f, CHANNEL(luminous_energy, 3)) - -TEST_SENSOR_TYPE(luminous_exposure, 0x0040, CHANNEL(luminous_exposure, 3)) - -TEST_SENSOR_TYPE(luminous_flux_range, 0x0041, - CHANNEL(luminous_flux, 2), - CHANNEL(luminous_flux, 2)) - -TEST_SENSOR_TYPE(avg_output_current, 0x0004, - CHANNEL(electric_current, 2), - CHANNEL(time_exp_8, 1)) - -TEST_SENSOR_TYPE(avg_output_voltage, 0x0005, - CHANNEL(voltage, 2), - CHANNEL(time_exp_8, 1)) - -TEST_SENSOR_TYPE(output_current_range, 0x0046, - CHANNEL(electric_current, 2), - CHANNEL(electric_current, 2)) - -TEST_SENSOR_TYPE(output_current_stat, 0x0047, - CHANNEL(electric_current, 2), - CHANNEL(electric_current, 2), - CHANNEL(electric_current, 2), - CHANNEL(electric_current, 2), - CHANNEL(time_exp_8, 1)) - -TEST_SENSOR_TYPE(output_ripple_voltage_spec, 0x0048, CHANNEL(percentage_8, 1)) - -TEST_SENSOR_TYPE(output_voltage_range, 0x0049, - CHANNEL(voltage, 2), - CHANNEL(voltage, 2), - CHANNEL(voltage, 2)) - -TEST_SENSOR_TYPE(output_voltage_stat, 0x004a, - CHANNEL(voltage, 2), - CHANNEL(voltage, 2), - CHANNEL(voltage, 2), - CHANNEL(voltage, 2), - CHANNEL(time_exp_8, 1)) - -TEST_SENSOR_TYPE(present_output_current, 0x005c, CHANNEL(electric_current, 2)) - -TEST_SENSOR_TYPE(present_output_voltage, 0x005d, CHANNEL(voltage, 2)) - -TEST_SENSOR_TYPE(present_rel_output_ripple_voltage, 0x005f, CHANNEL(percentage_8, 1)) - -TEST_SENSOR_TYPE(gain, 0x0074, CHANNEL(coefficient, 4)) - -TEST_SENSOR_TYPE(rel_dev_runtime_in_a_generic_level_range, 0x0061, - CHANNEL(percentage_8, 1), - CHANNEL(gen_lvl, 2), - CHANNEL(gen_lvl, 2)) - -TEST_SENSOR_TYPE(total_dev_runtime, 0x006e, CHANNEL(time_hour_24, 3)) - -ZTEST_SUITE(sensor_types_test_new, NULL, NULL, NULL, NULL, NULL); diff --git a/tests/subsys/bluetooth/mesh/sensor_subsys_new/testcase.yaml b/tests/subsys/bluetooth/mesh/sensor_subsys_new/testcase.yaml deleted file mode 100644 index e85265092e75..000000000000 --- a/tests/subsys/bluetooth/mesh/sensor_subsys_new/testcase.yaml +++ /dev/null @@ -1,8 +0,0 @@ -tests: - bluetooth.mesh.sensor_subsys_new: - sysbuild: true - platform_allow: native_posix qemu_cortex_m3 - tags: bluetooth ci_build sysbuild - integration_platforms: - - native_posix - - qemu_cortex_m3