ruuvi.drivers.c  ${PROJECT_VERSION}
Drivers for external sensors and peripherals on embedded systems.
ruuvi_task_advertisement.c
Go to the documentation of this file.
1 
15 #if RT_ADV_ENABLED
16 
17 #include "ruuvi_driver_error.h"
21 #include "ruuvi_task_gatt.h"
22 
23 // https://github.com/arm-embedded/gcc-arm-none-eabi.debian/blob/master/src/libiberty/strnlen.c
24 // Not included when compiled with std=c99.
25 static size_t safe_strlen (const char * s, size_t maxlen)
26 {
27  size_t i;
28 
29  for (i = 0; i < maxlen; ++i)
30  if (s[i] == '\0')
31  { break; }
32 
33  return i;
34 }
35 
36 static ri_comm_channel_t m_channel;
37 static bool m_is_init;
38 
39 rd_status_t rt_adv_init (rt_adv_init_t * const adv_init_settings)
40 {
41  rd_status_t err_code = RD_SUCCESS;
42 
43  if (m_is_init)
44  {
45  err_code |= RD_ERROR_INVALID_STATE;
46  }
47  else
48  {
49  err_code |= ri_adv_init (&m_channel);
51  adv_init_settings->is_rx_le_1m_phy_enabled,
52  adv_init_settings->is_rx_le_2m_phy_enabled,
53  adv_init_settings->is_rx_le_coded_phy_enabled);
55  err_code |= ri_adv_tx_interval_set (adv_init_settings->adv_interval_ms);
56  err_code |= ri_adv_tx_power_set (& (adv_init_settings->adv_pwr_dbm));
58  err_code |= ri_adv_manufacturer_id_set (adv_init_settings->manufacturer_id);
59  err_code |= ri_adv_channels_set (adv_init_settings->channels);
60 
61  if (RD_SUCCESS == err_code)
62  {
63  m_is_init = true;
64  }
65  }
66 
67  return err_code;
68 }
69 
71 {
72  m_is_init = false;
73  return ri_adv_uninit (&m_channel);
74 }
75 
77 {
78  rd_status_t err_code = RD_SUCCESS;
79 
80  if (!m_is_init)
81  {
82  err_code |= RD_ERROR_INVALID_STATE;
83  }
84  else
85  {
86  err_code |= ri_adv_stop();
87  }
88 
89  return err_code;
90 }
91 
93 {
94  rd_status_t err_code = RD_SUCCESS;
95 
96  if (NULL == msg)
97  {
98  err_code |= RD_ERROR_NULL;
99  }
100  else if (!rt_adv_is_init())
101  {
102  err_code |= RD_ERROR_INVALID_STATE;
103  }
104  else if (24 < msg->data_length)
105  {
106  err_code |= RD_ERROR_DATA_SIZE;
107  }
108  else
109  {
110  err_code |= m_channel.send (msg);
111  }
112 
113  return err_code;
114 }
115 
116 rd_status_t rt_adv_connectability_set (const bool enable, const char * const device_name)
117 {
118  rd_status_t err_code = RD_SUCCESS;
119 
120  if (!rt_adv_is_init())
121  {
122  err_code |= RD_ERROR_INVALID_STATE;
123  }
124  else if (!enable)
125  {
127  }
128  else if (NULL == device_name)
129  {
130  err_code |= RD_ERROR_NULL;
131  }
132  else if (SCAN_RSP_NAME_MAX_LEN < safe_strlen (device_name, (SCAN_RSP_NAME_MAX_LEN + 1)))
133  {
134  err_code |= RD_ERROR_INVALID_LENGTH;
135  }
136  else
137  {
139  err_code |= ri_adv_scan_response_setup (device_name, rt_gatt_is_nus_enabled());
140  }
141 
142  return err_code;
143 }
144 
145 inline bool rt_adv_is_init (void)
146 {
147  return ( (NULL != m_channel.send) && (true == m_is_init));
148 }
149 
151 {
152  rd_status_t err_code = RD_SUCCESS;
153 
154  if (!rt_adv_is_init())
155  {
156  err_code |= RD_ERROR_INVALID_STATE;
157  }
158  else
159  {
160  m_channel.on_evt = on_evt;
162  }
163 
164  return err_code;
165 }
166 
168 {
169  rd_status_t err_code = RD_SUCCESS;
170 
171  if (!m_is_init)
172  {
173  err_code |= RD_ERROR_INVALID_STATE;
174  }
175  else
176  {
177  err_code |= ri_adv_scan_stop();
178  }
179 
180  return err_code;
181 }
182 
183 #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_DATA_SIZE
Invalid Data size.
#define RD_ERROR_INVALID_STATE
Invalid state, operation disallowed in this state.
#define RT_ADV_SCAN_INTERVAL_MS
rd_status_t rt_adv_scan_stop(void)
Abort scanning.
rd_status_t rt_adv_send_data(ri_comm_message_t *const msg)
Send given message as a BLE advertisement.
rd_status_t rt_adv_connectability_set(const bool enable, const char *const device_name)
Start advertising BLE GATT connection.
rd_status_t rt_adv_scan_start(const ri_comm_evt_handler_fp_t on_evt)
Start scanning BLE advertisements.
bool rt_adv_is_init(void)
check if advertisement is initialized
rd_status_t rt_adv_uninit(void)
Uninitializes data advertising.
rd_status_t rt_adv_init(rt_adv_init_t *const adv_init_settings)
Initializes data advertising.
#define RT_ADV_SCAN_WINDOW_MS
Window of one scan per channel. Scan takes this * num_channels time.
rd_status_t rt_adv_stop(void)
Stops advertising. This is relevant only if the message was on repeat.
#define SCAN_RSP_NAME_MAX_LEN
Longer name gets truncated when advertised with UUID.
Header to enable and disable module compilation.
Ruuvi error codes and error check function.
rd_status_t(* ri_comm_evt_handler_fp_t)(const ri_comm_evt_t evt, void *p_data, size_t data_len)
Application event handler for communication events.
rd_status_t ri_adv_channels_set(const ri_radio_channels_t channels)
Set channels to use on radio. This has effect only on BLE advertising.
rd_status_t ri_adv_scan_start(const uint32_t window_interval_ms, const uint32_t window_size_ms)
setup scan window interval and window size.
@ NONCONNECTABLE_NONSCANNABLE
Nonconnectable, nonscannable.
@ CONNECTABLE_SCANNABLE
Connectable, scannable.
void ri_adv_rx_set_max_advertisement_data_length(const uint8_t max_adv_length)
Set maximum advertisement data length. This is used to limit the size of extended advertisement data.
rd_status_t ri_adv_manufacturer_id_set(const uint16_t id)
Set manufacturer ID of manufacturer specific advertisement.
rd_status_t ri_adv_init(ri_comm_channel_t *const channel)
Initialize Advertising module and scanning module.
rd_status_t ri_adv_scan_response_setup(const char *const name, const bool advertise_nus)
Configure advertising data with a scan response.
rd_status_t ri_adv_stop(void)
Stop ongoing advertisements.
rd_status_t ri_adv_uninit(ri_comm_channel_t *const channel)
rd_status_t ri_adv_type_set(ri_adv_type_t type)
Configure the type of advertisement.
rd_status_t ri_adv_tx_interval_set(const uint32_t ms)
rd_status_t ri_adv_tx_power_set(int8_t *dbm)
Set radio TX power.
rd_status_t ri_adv_scan_stop(void)
Stop ongoing scanning.
void ri_adv_rx_ble_phy_enabled_set(const bool is_le_1m_phy_enabled, const bool is_le_2m_phy_enabled, const bool is_le_coded_phy_enabled)
Set BLE PHYs enabled or disabled for scanning.
bool rt_gatt_is_nus_enabled(void)
Check if Nordic UART Service is enabled.
control API for communication via outside world
ri_comm_evt_handler_fp_t on_evt
Callback to application-level event handler, must be set in application.
ri_comm_xfer_fp_t send
Asynchronous send function.
Application message structure used for communication.
uint8_t data_length
Length of data.
Initial configuration for advertisement. PHY will be transferred to GATT.
uint16_t adv_interval_ms
ms / advertisement, not counting random delay
uint16_t manufacturer_id
BLE SIG id of board manufacturer.
bool is_rx_le_2m_phy_enabled
True if 2 MBit/s PHY is enabled in receiver.
ri_radio_channels_t channels
Radio channels, typically 37, 38, and/or 39.
uint8_t max_adv_length
Maximum length of advertisement data.
int8_t adv_pwr_dbm
Power to antenna, dBm.
bool is_rx_le_1m_phy_enabled
True if 1 MBit/s PHY is enabled in receiver.
bool is_rx_le_coded_phy_enabled
True if 125 kBit/s PHY is enabled in receiver.