ruuvi.drivers.c  ${PROJECT_VERSION}
Drivers for external sensors and peripherals on embedded systems.
ruuvi_interface_log.c
Go to the documentation of this file.
2 #if RI_LOG_ENABLED
18 #include "ruuvi_driver_error.h"
19 #include "ruuvi_driver_sensor.h"
20 #include "ruuvi_interface_log.h"
21 #include <stdio.h>
22 #include <string.h>
23 
24 size_t ri_error_to_string (rd_status_t error,
25  char * const error_string, const size_t space_remaining)
26 {
27  if (NULL == error_string)
28  {
30  return 0;
31  }
32 
33  size_t written = 0;
34  rd_status_t error_bit = 0;
35 
36  // Print each error individually
37  do
38  {
39  // Print comma + space if needed
40  if (written != 0)
41  {
42  written += snprintf (error_string + written, space_remaining - written, ", ");
43  }
44 
45  // If there is some error, print the lowest bit and reset the lowest bit in error code.
46  if (error)
47  {
48  for (uint8_t ii = 0; ii < 32; ii++)
49  {
50  if (error & (1 << ii))
51  {
52  error_bit = 1 << ii;
53  error &= 0xFFFFFFFF - (1 << ii);
54  }
55  }
56  }
57 
58  switch (error_bit)
59  {
60  case RD_SUCCESS:
61  written += snprintf (error_string + written, space_remaining - written, "%s", "SUCCESS");
62  break;
63 
64  case RD_ERROR_INTERNAL:
65  written += snprintf (error_string + written, space_remaining - written, "%s", "INTERNAL");
66  break;
67 
68  case RD_ERROR_NOT_FOUND:
69  written += snprintf (error_string + written, space_remaining - written, "%s",
70  "NOT_FOUND");
71  break;
72 
73  case RD_ERROR_NO_MEM:
74  written += snprintf (error_string + written, space_remaining - written, "%s", "NO_MEM");
75  break;
76 
78  written += snprintf (error_string + written, space_remaining - written, "%s",
79  "NOT_SUPPORTED");
80  break;
81 
83  written += snprintf (error_string + written, space_remaining - written, "%s",
84  "INVALID_STATE");
85  break;
86 
88  written += snprintf (error_string + written, space_remaining - written, "%s",
89  "INVALID_LENGTH");
90  break;
91 
93  written += snprintf (error_string + written, space_remaining - written, "%s",
94  "INVALID_FLAGS");
95  break;
96 
98  written += snprintf (error_string + written, space_remaining - written, "%s",
99  "INVALID_DATA");
100  break;
101 
103  written += snprintf (error_string + written, space_remaining - written, "%s",
104  "INVALID_PARAM");
105  break;
106 
107  case RD_ERROR_DATA_SIZE:
108  written += snprintf (error_string + written, space_remaining - written, "%s",
109  "DATA_SIZE");
110  break;
111 
112  case RD_ERROR_TIMEOUT:
113  written += snprintf (error_string + written, space_remaining - written, "%s", "TIMEOUT");
114  break;
115 
116  case RD_ERROR_NULL:
117  written += snprintf (error_string + written, space_remaining - written, "%s", "NULL");
118  break;
119 
120  case RD_ERROR_FORBIDDEN:
121  written += snprintf (error_string + written, space_remaining - written, "%s",
122  "FORBIDDEN");
123  break;
124 
126  written += snprintf (error_string + written, space_remaining - written, "%s",
127  "INVALID_ADDR");
128  break;
129 
130  case RD_ERROR_BUSY:
131  written += snprintf (error_string + written, space_remaining - written, "%s", "BUSY");
132  break;
133 
134  case RD_ERROR_RESOURCES:
135  written += snprintf (error_string + written, space_remaining - written, "%s",
136  "RESOURCES");
137  break;
138 
140  written += snprintf (error_string + written, space_remaining - written, "%s",
141  "NOT_IMPLEMENTED");
142  break;
143 
145  written += snprintf (error_string + written, space_remaining - written, "%s",
146  "NOT_INITIALIZED");
147  break;
148 
149  case RD_ERROR_SELFTEST:
150  written += snprintf (error_string + written, space_remaining - written, "%s", "SELFTEST");
151  break;
152 
154  written += snprintf (error_string + written, space_remaining - written, "%s",
155  "NOT ACKNOWLEDGED");
156  break;
157 
159  written += snprintf (error_string + written, space_remaining - written, "%s",
160  "NOT ENABLED");
161  break;
162 
164  written += snprintf (error_string + written, space_remaining - written, "%s",
165  "DEPRECATED");
166  break;
167 
168  case RD_ERROR_FATAL:
169  written += snprintf (error_string + written, space_remaining - written, "%s", "FATAL");
170  break;
171 
172  default:
173  written = snprintf (error_string + written, space_remaining - written, "%s", "UNKNOWN");
174  break;
175  }
176  } while (error);
177 
178  return written;
179 }
180 
181 // Convert configuration value to string.
182 static char * configuration_value_to_string (const uint8_t val)
183 {
184  static char msg[17]; // sizeof "Not implemented", including NULL
185  memset (msg, 0, sizeof (msg));
186 
187  if (val <= 200 && val > 0)
188  {
189  snprintf (msg, sizeof (msg), "%d", val);
190  }
191  else switch (val)
192  {
193  case RD_SENSOR_CFG_MIN:
194  snprintf (msg, sizeof (msg), "MIN");
195  break;
196 
197  case RD_SENSOR_CFG_MAX:
198  snprintf (msg, sizeof (msg), "MAX");
199  break;
200 
202  snprintf (msg, sizeof (msg), "CONTINUOUS");
203  break;
204 
206  snprintf (msg, sizeof (msg), "DEFAULT");
207  break;
208 
210  snprintf (msg, sizeof (msg), "No change");
211  break;
212 
214  snprintf (msg, sizeof (msg), "Single");
215  break;
216 
217  case RD_SENSOR_CFG_SLEEP:
218  snprintf (msg, sizeof (msg), "Sleep");
219  break;
220 
222  snprintf (msg, sizeof (msg), "Not supported");
223  break;
224 
226  snprintf (msg, sizeof (msg), "Not implemented");
227  break;
228 
230  snprintf (msg, sizeof (msg), "Invalid");
231  break;
232 
233  default:
234  snprintf (msg, sizeof (msg), "Unknown");
235  break;
236  }
237 
238  return msg;
239 }
240 
242  const rd_sensor_configuration_t * const configuration, const char * unit)
243 {
244  char msg[RD_LOG_BUFFER_SIZE] = {0};
245  snprintf (msg, RD_LOG_BUFFER_SIZE, "Sample rate: %s Hz\r\n",
246  configuration_value_to_string (configuration->samplerate));
247  ri_log (level, msg);
248  memset (msg, 0, sizeof (msg));
249  snprintf (msg, RD_LOG_BUFFER_SIZE, "Resolution: %s bits\r\n",
250  configuration_value_to_string (configuration->resolution));
251  ri_log (level, msg);
252  memset (msg, 0, sizeof (msg));
253  snprintf (msg, RD_LOG_BUFFER_SIZE, "Scale: %s %s\r\n",
254  configuration_value_to_string (configuration->scale), unit);
255  ri_log (level, msg);
256  memset (msg, 0, sizeof (msg));
257  size_t written = snprintf (msg, RD_LOG_BUFFER_SIZE, "DSP: ");
258 
259  switch (configuration->dsp_function)
260  {
262  written += snprintf (msg + written, RD_LOG_BUFFER_SIZE - written, "High pass x ");
263  break;
264 
265  case RD_SENSOR_DSP_LAST:
266  written += snprintf (msg + written, RD_LOG_BUFFER_SIZE - written, "Last x ");
267  break;
268 
270  written += snprintf (msg + written, RD_LOG_BUFFER_SIZE - written, "Lowpass x ");
271  break;
272 
273  case RD_SENSOR_DSP_OS:
274  written += snprintf (msg + written, RD_LOG_BUFFER_SIZE - written,
275  "Oversampling x ");
276  break;
277 
278  default:
279  written += snprintf (msg + written, RD_LOG_BUFFER_SIZE - written, "Unknown x");
280  break;
281  }
282 
283  snprintf (msg + written, RD_LOG_BUFFER_SIZE - written, "%s\r\n",
284  configuration_value_to_string (configuration->dsp_parameter));
285  ri_log (level, msg);
286  memset (msg, 0, sizeof (msg));
287  written = snprintf (msg, RD_LOG_BUFFER_SIZE, "Mode: %s\r\n",
288  configuration_value_to_string (configuration->mode));
289  ri_log (level, msg);
290 }
291 
292 void ri_log_hex (const ri_log_severity_t severity,
293  const uint8_t * const bytes,
294  size_t byte_length)
295 {
296  char msg[RD_LOG_BUFFER_SIZE] = { 0 };
297  size_t index = 0;
298 
299  for (size_t ii = 0; ii < byte_length; ii++)
300  {
301  index += snprintf (msg + index, sizeof (msg) - index, "%02X", bytes[ii]);
302 
303  if (ii < (byte_length - 1))
304  {
305  index += snprintf (msg + index, sizeof (msg) - index, ":");
306  }
307 
308  if (index >= sizeof (msg)) { return; }
309  }
310 
311  ri_log (severity, msg);
312 }
313 
314 #else
315 #include "ruuvi_driver_error.h"
316 #include "ruuvi_interface_log.h"
323  min_severity)
324 {
325  return RD_SUCCESS;
326 }
327 
334 {
335  return RD_SUCCESS;
336 }
337 
347 void ri_log (const ri_log_severity_t severity,
348  const char * const message)
349 {
350  return;
351 }
352 
363 void ri_log_hex (const ri_log_severity_t severity,
364  const uint8_t * const bytes,
365  size_t byte_length)
366 {
367  return;
368 }
369 
379 size_t ri_error_to_string (rd_status_t error, char * error_string,
380  size_t space_remaining)
381 {
382  return space_remaining;
383 }
384 
393  const rd_sensor_configuration_t * const configuration, const char * unit)
394 {
395  return;
396 }
397 
398 #endif
#define RD_ERROR_INVALID_PARAM
Invalid Parameter.
#define RD_WARNING_DEPRECATED
Deprecated function, warn user.
#define RD_ERROR_NULL
Null Pointer.
#define RD_ERROR_NOT_INITIALIZED
Driver is not initialized.
#define RD_ERROR_FATAL
Program should always reset after this.
uint32_t rd_status_t
bitfield for representing errors
#define RD_ERROR_INVALID_LENGTH
Invalid Length.
#define RD_ERROR_CHECK(error, mask)
Shorthand macro for calling the rd_error_check with current file & line.
#define RD_ERROR_FORBIDDEN
Forbidden Operation.
#define RD_ERROR_INVALID_DATA
Invalid Data.
#define RD_ERROR_NOT_IMPLEMENTED
Not implemented yet.
#define RD_ERROR_NOT_ENABLED
Driver is not enabled.
#define RD_ERROR_RESOURCES
Not enough resources for operation.
#define RD_ERROR_NOT_SUPPORTED
Not supported.
#define RD_SUCCESS
Internal Error.
#define RD_ERROR_NOT_ACKNOWLEDGED
Ack was expected but not received.
#define RD_ERROR_DATA_SIZE
Invalid Data size.
#define RD_ERROR_NO_MEM
No Memory for operation.
#define RD_ERROR_TIMEOUT
Operation timed out.
#define RD_ERROR_SELFTEST
Self-test fail.
#define RD_ERROR_INVALID_FLAGS
Invalid Flags.
#define RD_ERROR_NOT_FOUND
Not found.
#define RD_ERROR_INVALID_STATE
Invalid state, operation disallowed in this state.
#define RD_ERROR_INTERNAL
Internal Error.
#define RD_ERROR_BUSY
Busy.
#define RD_ERROR_INVALID_ADDR
Bad Memory Address.
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.
rd_status_t ri_log_init(const ri_log_severity_t min_severity)
Runs initialization code for the logging backend and sets the severity level.
ri_log_severity_t
Enable implementation selected by application.
void ri_log(const ri_log_severity_t severity, const char *const message)
Queues messages into log.
size_t ri_error_to_string(rd_status_t error, char *error_string, size_t space_remaining)
Write text description of error message into given string pointer and null-terminate it....
void ri_log_sensor_configuration(const ri_log_severity_t level, const rd_sensor_configuration_t *const configuration, const char *unit)
rd_status_t ri_log_flush(void)
Blocks until remaining log messages are sent out.
#define RD_SENSOR_DSP_LAST
Return last value from sensor. Parameter: No effect. Use default.
#define RD_SENSOR_ERR_NOT_SUPPORTED
Error code, given parameter is not supported by sensor.
#define RD_SENSOR_CFG_SLEEP
Sensor should go to sleep immediately.
#define RD_SENSOR_CFG_MAX
Configure largest supported and implemented value.
#define RD_SENSOR_CFG_DEFAULT
Default value, always valid for the sensor.
#define RD_SENSOR_DSP_OS
Oversample sensor values. Parameter: Number of samples.
#define RD_SENSOR_DSP_LOW_PASS
Low pass sensor values Parameter: coefficient.
#define RD_SENSOR_ERR_INVALID
Error code, given parameter is invalid.
#define RD_SENSOR_ERR_NOT_IMPLEMENTED
Error code, given parameter is not implemented (todo)
#define RD_SENSOR_CFG_MIN
Configure smallest supported and implemented value.
#define RD_SENSOR_CFG_CONTINUOUS
Sensor will keep sampling at defined sample rate.
#define RD_SENSOR_DSP_HIGH_PASS
High pass sensor values Parameter: coefficient.
#define RD_SENSOR_CFG_NO_CHANGE
Do not change configured value.
#define RD_SENSOR_CFG_SINGLE
Sensor should go to sleep after single measurement.
Header to enable and disable module compilation.
#define RD_LOG_BUFFER_SIZE
Maximum length of one log message.
Ruuvi error codes and error check function.
Ruuvi sensor interface Lifecycle: Beta
All sensors must implement configuration functions which accept this struct.
uint8_t resolution
Resolution, in bits.
uint8_t mode
Mode, RD_SENSOR_SLEEP, _SINGLE, _CONTINOUS.
uint8_t samplerate
Samplerate, in Hz.
uint8_t scale
Scale, in relevant Si-unit.
uint8_t dsp_parameter
Parameter to DSP functions.
uint8_t dsp_function
DSP function, one of RD_SENSOR_DSP_*.