ruuvi.drivers.c  ${PROJECT_VERSION}
Drivers for external sensors and peripherals on embedded systems.
ruuvi_task_communication.c
Go to the documentation of this file.
2 #if RT_COMMUNICATION_ENABLED
3 #include "ruuvi_driver_error.h"
4 #include "ruuvi_driver_sensor.h"
8 
9 #include <stdio.h>
10 #include <inttypes.h>
11 
12 #define MAC_BYTES (6U)
13 #define ID_BYTES (8U)
14 #define CHAR_PER_BYTE (3U)
15 #define NULL_LEN (1U)
16 #define BITS_PER_BYTE (8U)
17 
32 static rd_status_t u64_to_hexstr (const uint64_t value, char * const str,
33  const size_t str_len, const uint8_t bytes)
34 {
35  rd_status_t status = RD_SUCCESS;
36  uint8_t bit_offset = BITS_PER_BYTE * (bytes - 1U);
37  size_t written = 0;
38  uint8_t num_delimiters = bytes - 1U;
39 
40  for (size_t ii = 0; (ii < bytes) && (str_len > (written + NULL_LEN)); ii++)
41  {
42  uint8_t byte = (uint8_t) (value >> bit_offset);
43  written += snprintf (str + written, str_len - written, "%02X", byte);
44 
45  if ( (ii < num_delimiters) && (str_len > written))
46  {
47  written += snprintf (str + written, str_len - written, ":");
48  }
49 
50  bit_offset -= BITS_PER_BYTE;
51  }
52 
53  if ( (written + NULL_LEN) < CHAR_PER_BYTE * bytes
54  || (str_len < written + NULL_LEN))
55  {
56  status |= RD_ERROR_INVALID_LENGTH;
57  }
58 
59  return status;
60 }
61 
62 rd_status_t rt_com_get_mac_str (char * const mac_str, const size_t mac_len)
63 {
64  rd_status_t status = RD_SUCCESS;
65 
66  if (NULL == mac_str)
67  {
68  status = RD_ERROR_NULL;
69  }
70  else if (!ri_radio_is_init())
71  {
72  status |= RD_ERROR_INVALID_STATE;
73  }
74  else
75  {
76  uint64_t mac;
77  status |= ri_radio_address_get (&mac);
78  status |= u64_to_hexstr (mac, mac_str, mac_len, MAC_BYTES);
79  }
80 
81  return status;
82 }
83 
84 rd_status_t rt_com_get_id_str (char * const id_str, const size_t id_len)
85 {
86  rd_status_t status = RD_SUCCESS;
87 
88  if (NULL == id_str)
89  {
90  status = RD_ERROR_NULL;
91  }
92  else
93  {
94  uint64_t id;
95  status |= ri_comm_id_get (&id);
96  status |= u64_to_hexstr (id, id_str, id_len, ID_BYTES);
97  }
98 
99  return status;
100 }
101 
102 #endif
#define RD_ERROR_NULL
Null Pointer.
uint32_t rd_status_t
bitfield for representing errors
#define RD_ERROR_INVALID_LENGTH
Invalid Length.
#define RD_SUCCESS
Internal Error.
#define RD_ERROR_INVALID_STATE
Invalid state, operation disallowed in this state.
bool ri_radio_is_init()
Check if radio is initialized.
rd_status_t ri_radio_address_get(uint64_t *const address)
rd_status_t rt_com_get_mac_str(char *const mac_str, const size_t mac_len)
Get MAC address of the device from radio driver and write it to given string.
rd_status_t rt_com_get_id_str(char *const id_str, const size_t id_len)
Get Unique ID of the device and write it to given string.
Header to enable and disable module compilation.
Ruuvi error codes and error check function.
#define BITS_PER_BYTE
Number of bits in a byte.
Ruuvi sensor interface Lifecycle: Beta
rd_status_t ri_comm_id_get(uint64_t *const id)
Helper functions for communication.