ruuvi.drivers.c  ${PROJECT_VERSION}
Drivers for external sensors and peripherals on embedded systems.
ruuvi_task_sensor.c
Go to the documentation of this file.
2 #if RT_SENSOR_ENABLED
3 #include "ruuvi_driver_error.h"
4 #include "ruuvi_driver_sensor.h"
5 #include "ruuvi_interface_log.h"
6 #include "ruuvi_task_flash.h"
7 #include "ruuvi_task_sensor.h"
8 
9 #include <string.h>
10 
11 #ifndef TASK_SENSOR_LOG_LEVEL
12 #define TASK_SENSOR_LOG_LEVEL RI_LOG_LEVEL_DEBUG
13 #endif
14 
15 static inline void LOG (const char * const msg)
16 {
17  ri_log (TASK_SENSOR_LOG_LEVEL, msg);
18 }
19 
20 static inline void LOGD (const char * const msg)
21 {
23 }
24 
25 static inline void LOGHEX (const uint8_t * const msg, const size_t len)
26 {
27  ri_log_hex (TASK_SENSOR_LOG_LEVEL, msg, len);
28 }
29 
47 {
48  rd_status_t err_code = RD_SUCCESS;
49 
50  if ( (NULL == sensor) || (NULL == sensor->init))
51  {
52  err_code |= RD_ERROR_NULL;
53  }
54  else if (RD_HANDLE_UNUSED != sensor->handle)
55  {
56  err_code = sensor->init (& (sensor->sensor), sensor->bus, sensor->handle);
57  }
58  else
59  {
60  err_code |= RD_ERROR_NOT_FOUND;
61  }
62 
63  return err_code;
64 }
65 
75 {
76  rd_status_t err_code = RD_SUCCESS;
77 
78  if (NULL == sensor)
79  {
80  err_code |= RD_ERROR_NULL;
81  }
82  else if (rt_flash_busy())
83  {
84  err_code |= RD_ERROR_BUSY;
85  }
86  else
87  {
88  err_code |= rt_flash_store (sensor->nvm_file, sensor->nvm_record,
89  & (sensor->configuration),
90  sizeof (sensor->configuration));
91  }
92 
93  return err_code;
94 }
95 
105 {
106  rd_status_t err_code = RD_SUCCESS;
107 
108  if (NULL == sensor)
109  {
110  err_code |= RD_ERROR_NULL;
111  }
112  else if (rt_flash_busy())
113  {
114  err_code |= RD_ERROR_BUSY;
115  }
116  else
117  {
118  err_code |= rt_flash_load (sensor->nvm_file, sensor->nvm_record,
119  & (sensor->configuration),
120  sizeof (sensor->configuration));
121  }
122 
123  return err_code;
124 }
125 
136 {
137  rd_status_t err_code = RD_SUCCESS;
138 
139  if (NULL == ctx)
140  {
141  err_code |= RD_ERROR_NULL;
142  }
143  else if (!rd_sensor_is_init (& (ctx->sensor)))
144  {
145  err_code |= RD_ERROR_INVALID_STATE;
146  }
147  else
148  {
149  LOG ("\r\nAttempting to configure ");
150  LOG (ctx->sensor.name);
151  LOG (" with:\r\n");
152  ri_log_sensor_configuration (TASK_SENSOR_LOG_LEVEL,
153  & (ctx->configuration), "");
154  err_code |= ctx->sensor.configuration_set (& (ctx->sensor),
155  & (ctx->configuration));
156  LOG ("Actual configuration:\r\n");
157  ri_log_sensor_configuration (TASK_SENSOR_LOG_LEVEL,
158  & (ctx->configuration), "");
159  }
160 
161  return err_code;
162 }
163 
170 //ruuvi_endpoint_status_t task_sensor_encode_to_5 (uint8_t * const buffer);
171 
183  const size_t count, const char * const name)
184 {
185  rt_sensor_ctx_t * p_sensor = NULL;
186 
187  for (size_t ii = 0; (count > ii) && (NULL == p_sensor); ii++)
188  {
189  if (0 == strcmp (sensor_list[ii].sensor.name, name))
190  {
191  p_sensor = & (sensor_list[ii]);
192  }
193  }
194 
195  return p_sensor;
196 }
197 
209  sensor_list, const size_t count, rd_sensor_data_fields_t values)
210 {
211  rt_sensor_ctx_t * p_sensor = NULL;
212 
213  for (size_t ii = 0; (count > ii) && (NULL == p_sensor); ii++)
214  {
215  if ( (values.bitfield & sensor_list[ii].sensor.provides.bitfield) == values.bitfield)
216  {
217  p_sensor = & (sensor_list[ii]);
218  }
219  }
220 
221  return p_sensor;
222 }
223 #endif
#define RD_ERROR_NULL
Null Pointer.
uint32_t rd_status_t
bitfield for representing errors
#define RD_SUCCESS
Internal Error.
#define RD_ERROR_NOT_FOUND
Not found.
#define RD_ERROR_INVALID_STATE
Invalid state, operation disallowed in this state.
#define RD_ERROR_BUSY
Busy.
void ri_log_hex(const ri_log_severity_t severity, const uint8_t *const bytes, size_t byte_length)
Queues bytes to be logged out as a hex string.
void ri_log(const ri_log_severity_t severity, const char *const message)
Queues messages into log.
void ri_log_sensor_configuration(const ri_log_severity_t level, const rd_sensor_configuration_t *const configuration, const char *unit)
@ RI_LOG_LEVEL_DEBUG
bool rd_sensor_is_init(const rd_sensor_t *const sensor)
Check if given sensor structure is already initialized.
#define RD_HANDLE_UNUSED
Mark sensor as unused with this handle.
bool rt_flash_busy(void)
Check if flash is running an operation.
rd_status_t rt_flash_load(const uint16_t page_id, const uint16_t record_id, void *const message, const size_t message_length)
Load data from flash.
rd_status_t rt_flash_store(const uint16_t file_id, const uint16_t record_id, const void *const message, const size_t message_length)
Store data to flash.
Header to enable and disable module compilation.
Ruuvi error codes and error check function.
Ruuvi sensor interface Lifecycle: Beta
#define LOGD(fmt,...)
rt_sensor_ctx_t * rt_sensor_find_backend(rt_sensor_ctx_t *const sensor_list, const size_t count, const char *const name)
Search for requested sensor backend in given list of sensors.
rd_status_t rt_sensor_store(rt_sensor_ctx_t *const sensor)
Store the sensor state to NVM.
rd_status_t rt_sensor_configure(rt_sensor_ctx_t *const sensor)
Configure a sensor with given settings.
rd_status_t rt_sensor_load(rt_sensor_ctx_t *const sensor)
Load the sensor state from NVM.
rd_status_t rt_sensor_initialize(rt_sensor_ctx_t *const sensor)
Initialize sensor CTX.
rt_sensor_ctx_t * rt_sensor_find_provider(rt_sensor_ctx_t *const sensor_list, const size_t count, rd_sensor_data_fields_t values)
Search for a sensor which can provide requested values.
rd_configuration_fp configuration_set
rd_configuration_fp
const char * name
Sensor human-readable name. Should be at most 8 bytes long.
rd_sensor_t sensor
Control structure for sensor.
uint8_t handle
Handle of sensor.
uint16_t nvm_file
NVM file of configuration.
uint16_t nvm_record
NVM record of configuration.
rd_sensor_configuration_t configuration
Sensor configuration.
rd_sensor_init_fp init
Initialization function.
rd_bus_t bus
Bus of sensor.
Union to access sensor data.
uint32_t bitfield
Bitfield used to access sensor data.