Firmware SDK
twr_radio_pub.c
1 #include <twr_radio_pub.h>
2 
3 #define _TWR_RADIO_PUB_BUFFER_SIZE_ACCELERATION (1 + sizeof(float) + sizeof(float) + sizeof(float))
4 
5 __attribute__((weak)) void twr_radio_pub_on_event_count(uint64_t *id, uint8_t event_id, uint16_t *event_count) { (void) id; (void) event_id; (void) event_count; }
6 __attribute__((weak)) void twr_radio_pub_on_push_button(uint64_t *id, uint16_t *event_count) { (void) id; (void) event_count; }
7 __attribute__((weak)) void twr_radio_pub_on_temperature(uint64_t *id, uint8_t channel, float *celsius) { (void) id; (void) channel; (void) celsius; }
8 __attribute__((weak)) void twr_radio_pub_on_humidity(uint64_t *id, uint8_t channel, float *percentage) { (void) id; (void) channel; (void) percentage; }
9 __attribute__((weak)) void twr_radio_pub_on_lux_meter(uint64_t *id, uint8_t channel, float *illuminance) { (void) id; (void) channel; (void) illuminance; }
10 __attribute__((weak)) void twr_radio_pub_on_barometer(uint64_t *id, uint8_t channel, float *pressure, float *altitude) { (void) id; (void) channel; (void) pressure; (void) altitude; }
11 __attribute__((weak)) void twr_radio_pub_on_co2(uint64_t *id, float *concentration) { (void) id; (void) concentration; }
12 __attribute__((weak)) void twr_radio_pub_on_battery(uint64_t *id, float *voltage) { (void) id; (void) voltage; }
13 __attribute__((weak)) void twr_radio_pub_on_acceleration(uint64_t *id, float *x_axis, float *y_axis, float *z_axis) { (void) id; (void) x_axis; (void) y_axis; (void) z_axis; }
14 __attribute__((weak)) void twr_radio_pub_on_buffer(uint64_t *id, void *buffer, size_t length) { (void) id; (void) buffer; (void) length; }
15 __attribute__((weak)) void twr_radio_pub_on_state(uint64_t *id, uint8_t state_id, bool *state) { (void) id; (void) state_id; (void) state; }
16 __attribute__((weak)) void twr_radio_pub_on_bool(uint64_t *id, char *subtopic, bool *value) { (void) id; (void) subtopic; (void) value; }
17 __attribute__((weak)) void twr_radio_pub_on_int(uint64_t *id, char *subtopic, int *value) { (void) id; (void) subtopic; (void) value; }
18 __attribute__((weak)) void twr_radio_pub_on_uint32(uint64_t *id, char *subtopic, uint32_t *value) { (void) id; (void) subtopic; (void) value; }
19 __attribute__((weak)) void twr_radio_pub_on_float(uint64_t *id, char *subtopic, float *value) { (void) id; (void) subtopic; (void) value; }
20 __attribute__((weak)) void twr_radio_pub_on_string(uint64_t *id, char *subtopic, char *value) { (void) id; (void) subtopic; (void) value; }
21 __attribute__((weak)) void twr_radio_pub_on_value_int(uint64_t *id, uint8_t value_id, int *value) { (void) id; (void) value_id; (void) value; }
22 
23 
24 bool twr_radio_pub_event_count(uint8_t event_id, uint16_t *event_count)
25 {
26  uint8_t buffer[1 + sizeof(event_id) + sizeof(*event_count)];
27 
28  buffer[0] = TWR_RADIO_HEADER_PUB_EVENT_COUNT;
29  buffer[1] = event_id;
30 
31  twr_radio_uint16_to_buffer(event_count, buffer + 2);
32 
33  return twr_radio_pub_queue_put(buffer, sizeof(buffer));
34 }
35 
36 bool twr_radio_pub_push_button(uint16_t *event_count)
37 {
38  return twr_radio_pub_event_count(TWR_RADIO_PUB_EVENT_PUSH_BUTTON, event_count);
39 }
40 
41 bool twr_radio_pub_temperature(uint8_t channel, float *celsius)
42 {
43  uint8_t buffer[2 + sizeof(*celsius)];
44 
45  buffer[0] = TWR_RADIO_HEADER_PUB_TEMPERATURE;
46  buffer[1] = channel;
47 
48  twr_radio_float_to_buffer(celsius, buffer + 2);
49 
50  return twr_radio_pub_queue_put(buffer, sizeof(buffer));
51 }
52 
53 bool twr_radio_pub_humidity(uint8_t channel, float *percentage)
54 {
55  uint8_t buffer[2 + sizeof(*percentage)];
56 
57  buffer[0] = TWR_RADIO_HEADER_PUB_HUMIDITY;
58  buffer[1] = channel;
59 
60  twr_radio_float_to_buffer(percentage, buffer + 2);
61 
62  return twr_radio_pub_queue_put(buffer, sizeof(buffer));
63 }
64 
65 bool twr_radio_pub_luminosity(uint8_t channel, float *lux)
66 {
67  uint8_t buffer[2 + sizeof(*lux)];
68 
69  buffer[0] = TWR_RADIO_HEADER_PUB_LUX_METER;
70  buffer[1] = channel;
71 
72  twr_radio_float_to_buffer(lux, buffer + 2);
73 
74  return twr_radio_pub_queue_put(buffer, sizeof(buffer));
75 }
76 
77 bool twr_radio_pub_barometer(uint8_t channel, float *pascal, float *meter)
78 {
79  uint8_t buffer[2 + sizeof(*pascal) + sizeof(*meter)];
80 
81  buffer[0] = TWR_RADIO_HEADER_PUB_BAROMETER;
82  buffer[1] = channel;
83 
84  uint8_t *pointer = twr_radio_float_to_buffer(pascal, buffer + 2);
85  twr_radio_float_to_buffer(meter, pointer);
86 
87  return twr_radio_pub_queue_put(buffer, sizeof(buffer));
88 }
89 
90 bool twr_radio_pub_co2(float *concentration)
91 {
92  uint8_t buffer[1 + sizeof(*concentration)];
93 
94  buffer[0] = TWR_RADIO_HEADER_PUB_CO2;
95 
96  twr_radio_float_to_buffer(concentration, buffer + 1);
97 
98  return twr_radio_pub_queue_put(buffer, sizeof(buffer));
99 }
100 
101 bool twr_radio_pub_battery(float *voltage)
102 {
103  uint8_t buffer[1 + sizeof(*voltage)];
104 
105  buffer[0] = TWR_RADIO_HEADER_PUB_BATTERY;
106 
107  twr_radio_float_to_buffer(voltage, buffer + 1);
108 
109  return twr_radio_pub_queue_put(buffer, sizeof(buffer));
110 }
111 
112 bool twr_radio_pub_acceleration(float *x_axis, float *y_axis, float *z_axis)
113 {
114  uint8_t buffer[_TWR_RADIO_PUB_BUFFER_SIZE_ACCELERATION];
115 
116  buffer[0] = TWR_RADIO_HEADER_PUB_ACCELERATION;
117 
118  uint8_t *pointer = twr_radio_float_to_buffer(x_axis, buffer + 1);
119 
120  pointer = twr_radio_float_to_buffer(y_axis, pointer);
121 
122  pointer = twr_radio_float_to_buffer(z_axis, pointer);
123 
124  return twr_radio_pub_queue_put(buffer, sizeof(buffer));
125 }
126 
127 bool twr_radio_pub_buffer(void *buffer, size_t length)
128 {
129  uint8_t qbuffer[TWR_RADIO_MAX_BUFFER_SIZE];
130 
131  if (length > sizeof(qbuffer) - 1)
132  {
133  return false;
134  }
135 
136  qbuffer[0] = TWR_RADIO_HEADER_PUB_BUFFER;
137 
138  memcpy(&qbuffer[1], buffer, length);
139 
140  return twr_radio_pub_queue_put(qbuffer, length + 1);
141 }
142 
143 bool twr_radio_pub_state(uint8_t state_id, bool *state)
144 {
145  uint8_t buffer[1 + sizeof(state_id) + sizeof(*state)];
146 
147  buffer[0] = TWR_RADIO_HEADER_PUB_STATE;
148  buffer[1] = state_id;
149 
150  twr_radio_bool_to_buffer(state, buffer + 2);
151 
152  return twr_radio_pub_queue_put(buffer, sizeof(buffer));
153 }
154 
155 bool twr_radio_pub_value_int(uint8_t value_id, int *value)
156 {
157  uint8_t buffer[1 + sizeof(uint8_t) + sizeof(int)];
158 
159  buffer[0] = TWR_RADIO_HEADER_PUB_VALUE_INT;
160  buffer[1] = value_id;
161 
162  twr_radio_int_to_buffer(value, buffer + 2);
163 
164  return twr_radio_pub_queue_put(buffer, sizeof(buffer));
165 }
166 
167 bool twr_radio_pub_bool(const char *subtopic, bool *value)
168 {
169  size_t len = strlen(subtopic);
170 
171  if (len > TWR_RADIO_MAX_TOPIC_LEN)
172  {
173  return false;
174  }
175 
176  uint8_t buffer[TWR_RADIO_MAX_BUFFER_SIZE];
177 
178  buffer[0] = TWR_RADIO_HEADER_PUB_TOPIC_BOOL;
179 
180  twr_radio_bool_to_buffer(value, buffer + 1);
181 
182  strcpy((char *)buffer + 2, subtopic);
183 
184  return twr_radio_pub_queue_put(buffer, len + 3);
185 }
186 
187 bool twr_radio_pub_int(const char *subtopic, int *value)
188 {
189  size_t len = strlen(subtopic);
190 
191  if (len > TWR_RADIO_MAX_TOPIC_LEN)
192  {
193  return false;
194  }
195 
196  uint8_t buffer[TWR_RADIO_MAX_BUFFER_SIZE];
197 
198  buffer[0] = TWR_RADIO_HEADER_PUB_TOPIC_INT;
199 
200  twr_radio_int_to_buffer(value, buffer + 1);
201 
202  strcpy((char *)buffer + 5, subtopic);
203 
204  return twr_radio_pub_queue_put(buffer, len + 6);
205 }
206 
207 bool twr_radio_pub_uint32(const char *subtopic, uint32_t *value)
208 {
209  size_t len = strlen(subtopic);
210 
211  if (len > TWR_RADIO_MAX_TOPIC_LEN)
212  {
213  return false;
214  }
215 
216  uint8_t buffer[TWR_RADIO_MAX_BUFFER_SIZE];
217 
218  buffer[0] = TWR_RADIO_HEADER_PUB_TOPIC_UINT32;
219 
220  twr_radio_uint32_to_buffer(value, buffer + 1);
221 
222  strcpy((char *)buffer + 5, subtopic);
223 
224  return twr_radio_pub_queue_put(buffer, len + 6);
225 }
226 
227 bool twr_radio_pub_float(const char *subtopic, float *value)
228 {
229  size_t len = strlen(subtopic);
230 
231  if (len > TWR_RADIO_MAX_TOPIC_LEN)
232  {
233  return false;
234  }
235 
236  uint8_t buffer[TWR_RADIO_MAX_BUFFER_SIZE];
237 
238  buffer[0] = TWR_RADIO_HEADER_PUB_TOPIC_FLOAT;
239 
240  twr_radio_float_to_buffer(value, buffer + 1);
241 
242  strcpy((char *)buffer + 5, subtopic);
243 
244  return twr_radio_pub_queue_put(buffer, len + 6);
245 }
246 
247 bool twr_radio_pub_string(const char *subtopic, const char *value)
248 {
249  size_t len = strlen(subtopic);
250  size_t len_value = strlen(value);
251 
252  if (len + len_value > (TWR_RADIO_MAX_BUFFER_SIZE - 3))
253  {
254  return false;
255  }
256 
257  uint8_t buffer[TWR_RADIO_MAX_BUFFER_SIZE];
258 
259  buffer[0] = TWR_RADIO_HEADER_PUB_TOPIC_STRING;
260 
261  strcpy((char *)buffer + 1, subtopic);
262  strcpy((char *)buffer + 1 + len + 1, value);
263 
264  return twr_radio_pub_queue_put(buffer, len + len_value + 3);
265 }
266 
267 void twr_radio_pub_decode(uint64_t *id, uint8_t *buffer, size_t length)
268 {
269 
270  if (buffer[0] == TWR_RADIO_HEADER_PUB_PUSH_BUTTON)
271  {
272  uint16_t event_count;
273  uint16_t *pevent_count;
274 
275  twr_radio_uint16_from_buffer(buffer + 1, &event_count, &pevent_count);
276 
277  twr_radio_pub_on_push_button(id, &event_count);
278 
279  twr_radio_pub_on_event_count(id, TWR_RADIO_PUB_EVENT_PUSH_BUTTON, pevent_count);
280  }
281  else if (buffer[0] == TWR_RADIO_HEADER_PUB_EVENT_COUNT)
282  {
283  uint16_t event_count;
284  uint16_t *pevent_count;
285 
286  if (length != (1 + sizeof(uint8_t) + sizeof(uint16_t)))
287  {
288  return;
289  }
290 
291  twr_radio_uint16_from_buffer(buffer + 2, &event_count, &pevent_count);
292 
293  if (buffer[1] == TWR_RADIO_PUB_EVENT_PUSH_BUTTON)
294  {
295  twr_radio_pub_on_push_button(id, pevent_count);
296  }
297 
298  twr_radio_pub_on_event_count(id, buffer[1], pevent_count);
299  }
300  else if (buffer[0] == TWR_RADIO_HEADER_PUB_TEMPERATURE)
301  {
302  float celsius;
303  float *pcelsius;
304 
305  if (length != (1 + sizeof(uint8_t) + sizeof(float)))
306  {
307  return;
308  }
309 
310  twr_radio_float_from_buffer(buffer + 2, &celsius, &pcelsius);
311 
312  twr_radio_pub_on_temperature(id, buffer[1], pcelsius);
313  }
314  else if (buffer[0] == TWR_RADIO_HEADER_PUB_HUMIDITY)
315  {
316  float percentage;
317  float *ppercentage;
318 
319  if (length != (1 + sizeof(uint8_t) + sizeof(float)))
320  {
321  return;
322  }
323 
324  twr_radio_float_from_buffer(buffer + 2, &percentage, &ppercentage);
325 
326  twr_radio_pub_on_humidity(id, buffer[1], ppercentage);
327  }
328  else if (buffer[0] == TWR_RADIO_HEADER_PUB_LUX_METER)
329  {
330  float lux;
331  float *plux;
332 
333  if (length != (1 + sizeof(uint8_t) + sizeof(float)))
334  {
335  return;
336  }
337 
338  twr_radio_float_from_buffer(buffer + 2, &lux, &plux);
339 
340  twr_radio_pub_on_lux_meter(id, buffer[1], plux);
341  }
342  else if (buffer[0] == TWR_RADIO_HEADER_PUB_BAROMETER)
343  {
344  float pascal;
345  float *ppascal;
346  float meter;
347  float *pmeter;
348 
349  if (length != (1 + sizeof(uint8_t) + sizeof(float) + sizeof(float)))
350  {
351  return;
352  }
353 
354  uint8_t *pointer = twr_radio_float_from_buffer(buffer + 2, &pascal, &ppascal);
355 
356  twr_radio_float_from_buffer(pointer, &meter, &pmeter);
357 
358  twr_radio_pub_on_barometer(id, buffer[1], ppascal, pmeter);
359  }
360  else if (buffer[0] == TWR_RADIO_HEADER_PUB_CO2)
361  {
362  float concentration;
363  float *pconcentration;
364 
365  if (length != (1 + sizeof(float)))
366  {
367  return;
368  }
369 
370  twr_radio_float_from_buffer(buffer + 1, &concentration, &pconcentration);
371 
372  twr_radio_pub_on_co2(id, pconcentration);
373  }
374  else if (buffer[0] == TWR_RADIO_HEADER_PUB_BATTERY)
375  {
376  float voltage;
377  float *pvoltage;
378 
379  if (length == (1 + sizeof(float)))
380  {
381  twr_radio_float_from_buffer(buffer + 1, &voltage, &pvoltage);
382  }
383  else if (length == (1 + 1 + sizeof(float)))
384  {
385  // Old format
386  twr_radio_float_from_buffer(buffer + 2, &voltage, &pvoltage);
387  }
388  else
389  {
390  return;
391  }
392 
393  twr_radio_pub_on_battery(id, pvoltage);
394  }
395  else if (buffer[0] == TWR_RADIO_HEADER_PUB_ACCELERATION)
396  {
397  if (length != _TWR_RADIO_PUB_BUFFER_SIZE_ACCELERATION)
398  {
399  return;
400  }
401 
402  float x_axis;
403  float *px_axis;
404  float y_axis;
405  float *py_axis;
406  float z_axis;
407  float *pz_axis;
408 
409  buffer = twr_radio_float_from_buffer(buffer + 1, &x_axis, &px_axis);
410 
411  buffer = twr_radio_float_from_buffer(buffer, &y_axis, &py_axis);
412 
413  twr_radio_float_from_buffer(buffer, &z_axis, &pz_axis);
414 
415  twr_radio_pub_on_acceleration(id, px_axis, py_axis, pz_axis);
416  }
417  else if (buffer[0] == TWR_RADIO_HEADER_PUB_BUFFER)
418  {
419  twr_radio_pub_on_buffer(id, buffer + 1, length - 1);
420  }
421  else if (buffer[0] == TWR_RADIO_HEADER_PUB_STATE)
422  {
423  bool state;
424  bool *pstate = NULL;
425 
426  if (length != (1 + sizeof(uint8_t) + sizeof(bool)))
427  {
428  return;
429  }
430 
431  twr_radio_bool_from_buffer(buffer + 2, &state, &pstate);
432 
433  twr_radio_pub_on_state(id, buffer[1], pstate);
434  }
435  else if (buffer[0] == TWR_RADIO_HEADER_PUB_TOPIC_BOOL)
436  {
437  bool value;
438  bool *pvalue;
439 
440  buffer = twr_radio_bool_from_buffer(buffer + 1, &value, &pvalue);
441 
442  buffer[length - 1] = 0;
443 
444  twr_radio_pub_on_bool(id, (char *) buffer, pvalue);
445  }
446  else if (buffer[0] == TWR_RADIO_HEADER_PUB_TOPIC_INT)
447  {
448  int value;
449  int *pvalue;
450 
451  buffer = twr_radio_int_from_buffer(buffer + 1, &value, &pvalue);
452 
453  buffer[length - 1] = 0;
454 
455  twr_radio_pub_on_int(id, (char *) buffer, pvalue);
456  }
457  else if (buffer[0] == TWR_RADIO_HEADER_PUB_TOPIC_UINT32)
458  {
459  uint32_t value;
460  uint32_t *pvalue;
461 
462  buffer = twr_radio_uint32_from_buffer(buffer + 1, &value, &pvalue);
463 
464  buffer[length - 1] = 0;
465 
466  twr_radio_pub_on_uint32(id, (char *) buffer, pvalue);
467  }
468  else if (buffer[0] == TWR_RADIO_HEADER_PUB_TOPIC_FLOAT)
469  {
470  float value;
471  float *pvalue;
472 
473  buffer = twr_radio_float_from_buffer(buffer + 1, &value, &pvalue);
474 
475  buffer[length - 1] = 0;
476 
477  twr_radio_pub_on_float(id, (char *) buffer, pvalue);
478  }
479  else if (buffer[0] == TWR_RADIO_HEADER_PUB_TOPIC_STRING)
480  {
481  buffer[length - 1] = 0;
482 
483  size_t len = strlen((char *) buffer + 1);
484 
485  twr_radio_pub_on_string(id, (char *) buffer + 1, (char *) buffer + 2 + len);
486  }
487  else if (buffer[0] == TWR_RADIO_HEADER_PUB_VALUE_INT)
488  {
489  int value;
490  int *pvalue;
491 
492  if (length != (1 + sizeof(uint8_t) + sizeof(int)))
493  {
494  return;
495  }
496 
497  twr_radio_int_from_buffer(buffer + 2, &value, &pvalue);
498 
499  twr_radio_pub_on_value_int(id, buffer[1], pvalue);
500  }
501 }
bool twr_radio_pub_float(const char *subtopic, float *value)
Publish float value in custom topic.
bool twr_radio_pub_luminosity(uint8_t channel, float *lux)
Publish luminosity.
Definition: twr_radio_pub.c:65
bool twr_radio_pub_value_int(uint8_t value_id, int *value)
Publish int value.
bool twr_radio_pub_state(uint8_t state_id, bool *state)
Publish battery.
bool twr_radio_pub_event_count(uint8_t event_id, uint16_t *event_count)
Publish event count.
Definition: twr_radio_pub.c:24
bool twr_radio_pub_barometer(uint8_t channel, float *pascal, float *meter)
Publish barometer.
Definition: twr_radio_pub.c:77
bool twr_radio_pub_acceleration(float *x_axis, float *y_axis, float *z_axis)
Publish acceleration.
bool twr_radio_pub_humidity(uint8_t channel, float *percentage)
Publish humidity.
Definition: twr_radio_pub.c:53
bool twr_radio_pub_int(const char *subtopic, int *value)
Publish int value in custom topic.
bool twr_radio_pub_string(const char *subtopic, const char *value)
Publish string value in custom topic.
bool twr_radio_pub_temperature(uint8_t channel, float *celsius)
Publish temperature.
Definition: twr_radio_pub.c:41
bool twr_radio_pub_uint32(const char *subtopic, uint32_t *value)
Publish uint32 value in custom topic.
bool twr_radio_pub_buffer(void *buffer, size_t length)
Publish buffer.
bool twr_radio_pub_battery(float *voltage)
Publish battery.
bool twr_radio_pub_push_button(uint16_t *event_count)
Publish push button event count, same as use twr_radio_pub_event_count with TWR_RADIO_PUB_EVENT_PUSH_...
Definition: twr_radio_pub.c:36
bool twr_radio_pub_bool(const char *subtopic, bool *value)
Publish bool value in custom topic.
void twr_radio_pub_decode(uint64_t *id, uint8_t *buffer, size_t length)
Internal decode function for twr_radio.c.
bool twr_radio_pub_co2(float *concentration)
Publish co2.
Definition: twr_radio_pub.c:90