nRF5 SDK for Mesh v5.0.0
sensor_utils.h
1 /* Copyright (c) 2010 - 2020, Nordic Semiconductor ASA
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification,
5  * are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice, this
8  * list of conditions and the following disclaimer.
9  *
10  * 2. Redistributions in binary form, except as embedded into a Nordic
11  * Semiconductor ASA integrated circuit in a product or a software update for
12  * such product, must reproduce the above copyright notice, this list of
13  * conditions and the following disclaimer in the documentation and/or other
14  * materials provided with the distribution.
15  *
16  * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
17  * contributors may be used to endorse or promote products derived from this
18  * software without specific prior written permission.
19  *
20  * 4. This software, with or without modification, must only be used with a
21  * Nordic Semiconductor ASA integrated circuit.
22  *
23  * 5. Any software provided in binary form under this license must not be reverse
24  * engineered, decompiled, modified and/or disassembled.
25  *
26  * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
27  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28  * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
29  * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
30  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
32  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
35  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  */
37 
38 #ifndef SENSOR_UTILS_H__
39 #define SENSOR_UTILS_H__
40 
41 #include <stdint.h>
42 
43 #include "utils.h"
44 #include "access_config.h"
45 #include "log.h"
46 #include "nrf_mesh_assert.h"
47 
48 /* Common sensor status indices */
49 #define SENSOR_PROP_ID_INDEX0 0
50 #define SENSOR_PROP_ID_INDEX1 1
51 
52 /* Sensor settings status constant */
53 #define SENSOR_SETTINGS_SPID_INDEX 2
54 
55 /* Sensor setting status constants */
56 #define SENSOR_SETTING_PROP_ID_INDEX0 2
57 #define SENSOR_SETTING_PROP_ID_INDEX1 3
58 #define SENSOR_SETTING_ACCESS_INDEX 4
59 #define SENSOR_SETTING_RAW_INDEX 5
60 
61 
62 /* Constants used to pack/unpack status messages. */
63 #define SENSOR_MPID_A_BYTES 2
64 #define SENSOR_MPID_B_BYTES 3
65 
66 #define SENSOR_FORMAT_A_BIT 0
67 #define SENSOR_FORMAT_B_BIT 1
68 
69 #define SENSOR_MPID_B_ZERO_DATA_BYTES (0x7F)
70 
82 typedef struct __attribute((packed))
83 {
84  uint16_t format : 1;
85  uint16_t length : 4;
86  uint16_t property_id : 11;
88 
91 typedef struct __attribute((packed))
92 {
93  uint8_t format : 1;
94  uint8_t length : 7;
95  uint16_t property_id : 16;
97 
106 static inline bool sensor_property_id_supported(uint16_t * p_properties, uint16_t property_id)
107 {
108  NRF_MESH_ASSERT(p_properties);
109 
110  /* The 0th element of p_properties gives the number of properties it defines.
111  */
112  for (uint16_t i = 1; i <= p_properties[0]; i++)
113  {
114  if (property_id == p_properties[i])
115  {
116  return true;
117  }
118  }
119 
120  __LOG(LOG_SRC_APP, LOG_LEVEL_ERROR, "property_id 0x%04x not supported\n", property_id);
121  return false;
122 }
123 
131 static inline uint64_t publish_period_get(access_model_handle_t model_handle)
132 {
133  access_publish_resolution_t publish_resolution;
134  uint8_t publish_steps = 0;
135  uint32_t status = access_model_publish_period_get(model_handle, &publish_resolution, &publish_steps);
136 
137  uint64_t resolution_ms = 0;
138 
139  if (NRF_SUCCESS == status)
140  {
141  switch (publish_resolution) {
143  resolution_ms = 100;
144  break;
146  resolution_ms = 1000;
147  break;
149  resolution_ms = 10000;
150  break;
152  resolution_ms = 10000 * 6;
153  break;
154  default:
155  __LOG(LOG_SRC_APP, LOG_LEVEL_ERROR,
156  "invalid publish resolution (%d) = (value).\n",
157  publish_resolution);
158  }
159  }
160 
161  /* If access_model_publish_period_get() returns with failure or with publish_steps set to 0 or
162  * with undefined publish_resolution, then the return value is 0. This means that no publish
163  * period is set.
164  */
165  return MS_TO_US(resolution_ms * publish_steps);
166 }
167 
168 
178 static inline void sensor_mpid_a_create(uint16_t property_id,
179  uint8_t data_bytes,
180  uint8_t * p_buffer)
181 {
182  sensor_mpid_a_t * p_mpid = (sensor_mpid_a_t *)p_buffer;
183 
184  p_mpid->format = SENSOR_FORMAT_A_BIT;
185  p_mpid->length = data_bytes;
186  p_mpid->property_id = property_id;
187 }
188 
198 static inline void sensor_mpid_b_create(uint16_t property_id,
199  uint8_t data_bytes,
200  uint8_t * p_buffer)
201 {
202  sensor_mpid_b_t * p_mpid = (sensor_mpid_b_t *)p_buffer;
203 
204  p_mpid->format = SENSOR_FORMAT_B_BIT;
205  p_mpid->length = data_bytes;
206  p_mpid->property_id = property_id;
207 }
208 
210 #endif /* SENSOR_UTILS_H__ */
Step resolution: 100ms / step.
Definition: access.h:324
uint16_t access_model_handle_t
Access layer handle type.
Definition: access.h:162
#define NRF_MESH_ASSERT(cond)
Run-time assertion.
uint16_t property_id
Property identifying a sensor.
Definition: sensor_utils.h:86
access_publish_resolution_t
Periodic publishing step resolution.
Definition: access.h:321
uint16_t property_id
Property identifying a sensor.
Definition: sensor_utils.h:95
static void sensor_mpid_a_create(uint16_t property_id, uint8_t data_bytes, uint8_t *p_buffer)
Create format A of the Marshalled Property ID (MPID) field.
Definition: sensor_utils.h:178
static uint64_t publish_period_get(access_model_handle_t model_handle)
Gets the publish period for the given model in microseconds.
Definition: sensor_utils.h:131
static void sensor_mpid_b_create(uint16_t property_id, uint8_t data_bytes, uint8_t *p_buffer)
Create format B of the Marshalled Property ID (MPID) field.
Definition: sensor_utils.h:198
uint8_t length
Length of the Property Value.
Definition: sensor_utils.h:94
uint16_t length
Length of the Property Value.
Definition: sensor_utils.h:85
Step resolution: 10s / step.
Definition: access.h:328
Format B of the Marshalled Property ID (MPID) field.
Definition: sensor_utils.h:91
Step resolution: 1s / step.
Definition: access.h:326
static bool sensor_property_id_supported(uint16_t *p_properties, uint16_t property_id)
Look through the supported property_id list to check for property_id support.
Definition: sensor_utils.h:106
uint32_t access_model_publish_period_get(access_model_handle_t handle, access_publish_resolution_t *p_resolution, uint8_t *p_step_number)
Gets the publish period for the given model.
uint8_t format
Format B tag, 0b1.
Definition: sensor_utils.h:93
uint16_t format
Format A tag, 0b0.
Definition: sensor_utils.h:84
Step resolution: 10min / step.
Definition: access.h:330
Format A of the Marshalled Property ID (MPID) field.
Definition: sensor_utils.h:82

Documentation feedback | Developer Zone | Subscribe | Updated