Nordic Semiconductor nRF5 AirFuel SDK  version 2.2.0
PTU registry API

Data Structures

struct  ptu_reg_item_t
 Registry item. More...
 
struct  ptu_reg_t
 Registry. More...
 

Enumerations

enum  ptu_reg_item_state_t {
  REG_ITEM_STATE_UNUSED = 0, REG_ITEM_STATE_PRE_CONNECT, REG_ITEM_STATE_FULLY_ACCEPTED, REG_ITEM_STATE_WAITING_TO_CONNECT,
  REG_ITEM_STATE_CONNECTING, REG_ITEM_STATE_REGISTERING_EN_ALERT, REG_ITEM_STATE_REGISTERING_PRU_STAT_RD, REG_ITEM_STATE_REGISTERING_PTU_STAT_WR,
  REG_ITEM_STATE_REGISTERING_PRU_DYN_RD, REG_ITEM_STATE_PRU_CTL_SEND, REG_ITEM_STATE_REGISTERED
}
 Registry item status. More...
 

General API functions

void ptu_reg_init (void)
 Initialize registry database.
 
uint8_t ptu_reg_registered_devices_get (ptu_reg_item_t **registered_devices)
 Get handles for all registered devices. More...
 
uint8_t ptu_reg_n_entries_get (void)
 Get the number of devices currently in registry. This will include all connected devices, as well as all devices which is currently being registered. More...
 
uint32_t ptu_reg_item_get_from_index (uint8_t index, ptu_reg_item_t **item_p)
 Get registry item from index in database. Index must be < PTU_MAX_CONNECTIONS. More...
 
ptu_reg_item_tptu_reg_item_get_from_conn_handle (uint16_t conn_handle)
 Get registry item from connection handle. More...
 
ptu_reg_item_tptu_reg_item_get_from_address (ble_gap_addr_t *address)
 Get registry item from GAP address. More...
 
ptu_reg_item_tptu_reg_item_oldest_get (ptu_reg_item_state_t state)
 Get oldest registry item having a specific state. More...
 
ptu_reg_item_tptu_reg_item_add (ble_gap_evt_adv_report_t *p_adv_report, ptu_reg_item_state_t init_state)
 Initiate registration of new device. More...
 
uint32_t ptu_reg_set_device_waiting_to_connect (ptu_reg_item_t *reg_item_p, uint8_t *address)
 Set the state of registry item to "ready to connect" state. Associates the address with the registry item. Should only be called with non-zero address values. More...
 
void ptu_reg_item_delete (ptu_reg_item_t *item)
 Delete item that has previoulsy been added to registry. More...
 
bool ptu_reg_all_charged (void)
 Function returning true if all items in registry are charged or there are not items in registry. More...
 

Detailed Description

Enumeration Type Documentation

Registry item status.

Enumerator
REG_ITEM_STATE_UNUSED 

Item not used

REG_ITEM_STATE_PRE_CONNECT 

Adv. packet received, but only 1 of 2 criteria for connection fulfilled

REG_ITEM_STATE_FULLY_ACCEPTED 

State of previously partially accepted device that is now fully accepted (11 ADV packets or 1700ms)

REG_ITEM_STATE_WAITING_TO_CONNECT 

Item granted connection. Waiting for scan response after which BLE connection shall start

REG_ITEM_STATE_CONNECTING 

In process of establishing BLE connection

REG_ITEM_STATE_REGISTERING_EN_ALERT 

In process of completing registration

REG_ITEM_STATE_REGISTERING_PRU_STAT_RD 

PRU static read while completing registration, first connected state

REG_ITEM_STATE_REGISTERING_PTU_STAT_WR 

PRU static write while completing registration

REG_ITEM_STATE_REGISTERING_PRU_DYN_RD 

PRU dynamic read while completing registration

REG_ITEM_STATE_PRU_CTL_SEND 

Send control packet completing registration

REG_ITEM_STATE_REGISTERED 

Registration completed

Definition at line 31 of file ptu_registry.h.

Function Documentation

uint8_t ptu_reg_registered_devices_get ( ptu_reg_item_t **  registered_devices)

Get handles for all registered devices.

Parameters
[out]registered_devicesPlaceholder to write the registry item pointers of all registered devices to.
Returns
Number of registered devices

Definition at line 254 of file ptu_registry.c.

255 {
256  uint8_t n_devices = 0;
257 
258  for(uint8_t i = 0; i < PTU_MAX_CONNECTIONS; i++)
259  {
261  {
262  registered_devices[n_devices++] = &m_registry.items[i];
263  }
264  }
265  return n_devices;
266 }
ptu_reg_item_t items[PTU_MAX_CONNECTIONS]
Definition: ptu_registry.h:77
static ptu_reg_t m_registry
Definition: ptu_registry.c:51
#define PTU_MAX_CONNECTIONS
Definition: ptu_config.h:29
ptu_reg_item_state_t state
Definition: ptu_registry.h:49
uint8_t ptu_reg_n_entries_get ( void  )

Get the number of devices currently in registry. This will include all connected devices, as well as all devices which is currently being registered.

Returns
pointer to the registry variable holding the number of connected devices.

Definition at line 268 of file ptu_registry.c.

269 {
270  return m_registry.n_entries;
271 }
uint8_t n_entries
Definition: ptu_registry.h:76
static ptu_reg_t m_registry
Definition: ptu_registry.c:51
uint32_t ptu_reg_item_get_from_index ( uint8_t  index,
ptu_reg_item_t **  item_p 
)

Get registry item from index in database. Index must be < PTU_MAX_CONNECTIONS.

Parameters
[in]index
[out]item_ppointer to where pointer to registry item will be written.
Returns
error code if index is not < PTU_MAX_CONNECTIONS

Definition at line 126 of file ptu_registry.c.

127 {
128  if(index < PTU_MAX_CONNECTIONS)
129  {
130  *item = &m_registry.items[index];
131  return NRF_SUCCESS;
132  }
133 
134  return NRF_ERROR_INVALID_PARAM;
135 }
ptu_reg_item_t items[PTU_MAX_CONNECTIONS]
Definition: ptu_registry.h:77
static ptu_reg_t m_registry
Definition: ptu_registry.c:51
#define PTU_MAX_CONNECTIONS
Definition: ptu_config.h:29
ptu_reg_item_t* ptu_reg_item_get_from_conn_handle ( uint16_t  conn_handle)

Get registry item from connection handle.

Parameters
[in]conn_handle
Returns
pointer to registry item. NULL if item not found.

Definition at line 137 of file ptu_registry.c.

138 {
139  if(m_registry.n_entries > 0 && conn_handle != BLE_CONN_HANDLE_INVALID)
140  {
141  for(uint8_t i = 0; i < PTU_MAX_CONNECTIONS; i++)
142  {
143  if( m_registry.items[i].ble_wpts_c.conn_handle == conn_handle && \
145  {
146  return &m_registry.items[i];
147  }
148  }
149  }
150  return NULL;
151 }
uint8_t n_entries
Definition: ptu_registry.h:76
ptu_reg_item_t items[PTU_MAX_CONNECTIONS]
Definition: ptu_registry.h:77
uint16_t conn_handle
Definition: ble_wpts_c.h:66
ble_wpts_c_t ble_wpts_c
Definition: ptu_registry.h:52
static ptu_reg_t m_registry
Definition: ptu_registry.c:51
#define PTU_MAX_CONNECTIONS
Definition: ptu_config.h:29
ptu_reg_item_state_t state
Definition: ptu_registry.h:49
ptu_reg_item_t* ptu_reg_item_get_from_address ( ble_gap_addr_t *  address)

Get registry item from GAP address.

Parameters
[in]address
Returns
pointer to registry item. NULL if item not found.

Definition at line 153 of file ptu_registry.c.

154 {
155  if(m_registry.n_entries > 0)
156  {
157  for(uint8_t i = 0; i < PTU_MAX_CONNECTIONS; i++)
158  {
159  if(memcmp(m_registry.items[i].address.addr, address->addr, BLE_GAP_ADDR_LEN) == 0 && \
161  {
162  return &m_registry.items[i];
163  }
164  }
165  }
166  return NULL;
167 }
uint8_t n_entries
Definition: ptu_registry.h:76
ptu_reg_item_t items[PTU_MAX_CONNECTIONS]
Definition: ptu_registry.h:77
static ptu_reg_t m_registry
Definition: ptu_registry.c:51
ble_gap_addr_t address
Definition: ptu_registry.h:51
#define PTU_MAX_CONNECTIONS
Definition: ptu_config.h:29
ptu_reg_item_state_t state
Definition: ptu_registry.h:49
ptu_reg_item_t* ptu_reg_item_oldest_get ( ptu_reg_item_state_t  state)

Get oldest registry item having a specific state.

Parameters
[in]statestate of registry item. Note: REG_ITEM_STATE_UNUSED is not a legal value.
Returns
pointer to registry item. NULL if item not found.

Definition at line 169 of file ptu_registry.c.

170 {
171  uint8_t age_max = 0;
172  ptu_reg_item_t * retval = NULL;
173 
174  ASSERT(state != REG_ITEM_STATE_UNUSED);
175 
176  for(uint8_t i = 0; i < PTU_MAX_CONNECTIONS; i++)
177  {
178  if(m_registry.items[i].state == state && m_registry.items[i].age >= age_max)
179  {
180  age_max = m_registry.items[i].age;
181  retval = &m_registry.items[i];
182  }
183  }
184  return retval;
185 }
ptu_reg_item_t items[PTU_MAX_CONNECTIONS]
Definition: ptu_registry.h:77
Registry item.
Definition: ptu_registry.h:47
static ptu_reg_t m_registry
Definition: ptu_registry.c:51
#define PTU_MAX_CONNECTIONS
Definition: ptu_config.h:29
ptu_reg_item_state_t state
Definition: ptu_registry.h:49
ptu_reg_item_t* ptu_reg_item_add ( ble_gap_evt_adv_report_t *  p_adv_report,
ptu_reg_item_state_t  init_state 
)

Initiate registration of new device.

Parameters
p_adv_reportpointer to adv report from device from which to register.
init_stateinitial state for the added registry item.
Returns
pointer to registry item. NULL if no item available.

Definition at line 187 of file ptu_registry.c.

188 {
189  ptu_reg_item_t * reg_item = ptu_reg_item_get_from_address(&p_adv_report->peer_addr);
190  ble_wpts_service_data_t curr_service_data;
191  uint32_t err_code;
192 
193  ASSERT(reg_item == NULL); // Assert this device not registered allready
194  reg_item = m_reg_item_unused_get();
195 
196  if(reg_item != NULL)
197  {
198  // We are about to add a new item. Increase age of all other items in registry.
199  for(uint8_t i = 0; i < PTU_MAX_CONNECTIONS; i++)
200  {
202  {
203  m_registry.items[i].age++;
204  }
205  }
206 
207  // Need to extract advdata here to get primary service handle
208  err_code = ble_wpts_service_data_read(p_adv_report, &curr_service_data);
209  APP_ERROR_CHECK(err_code);
210  ble_wpts_char_handles_update(curr_service_data.gatt_prim_srv_handle, &reg_item->ble_wpts_c);
211  memcpy(&reg_item->address, &p_adv_report->peer_addr, sizeof(ble_gap_addr_t));
212  reg_item->adv_flags = curr_service_data.adv_flags;
213  reg_item->state = init_state;
215  return reg_item;
216  }
217  return NULL;
218 }
uint8_t n_entries
Definition: ptu_registry.h:76
ptu_reg_item_t items[PTU_MAX_CONNECTIONS]
Definition: ptu_registry.h:77
Registry item.
Definition: ptu_registry.h:47
uint32_t ble_wpts_service_data_read(ble_gap_evt_adv_report_t *p_adv_report, ble_wpts_service_data_t *p_service_data)
Definition: ble_wpts_c.c:483
ble_wpts_c_t ble_wpts_c
Definition: ptu_registry.h:52
ptu_reg_item_t * ptu_reg_item_get_from_address(ble_gap_addr_t *address)
Get registry item from GAP address.
Definition: ptu_registry.c:153
uint8_t adv_flags
Definition: ptu_registry.h:50
void ble_wpts_char_handles_update(uint16_t prim_service_handle, ble_wpts_c_t *p_wpts_c)
Definition: ble_wpts_c.c:529
Struct holding contents of "Service data AD type".
static ptu_reg_t m_registry
Definition: ptu_registry.c:51
ble_gap_addr_t address
Definition: ptu_registry.h:51
#define PTU_MAX_CONNECTIONS
Definition: ptu_config.h:29
ptu_reg_item_state_t state
Definition: ptu_registry.h:49
static ptu_reg_item_t * m_reg_item_unused_get(void)
Get unused (available) registry item.
Definition: ptu_registry.c:69
uint32_t ptu_reg_set_device_waiting_to_connect ( ptu_reg_item_t reg_item_p,
uint8_t *  address 
)

Set the state of registry item to "ready to connect" state. Associates the address with the registry item. Should only be called with non-zero address values.

Parameters
reg_item_pThe registry item to update.
addressThe non-zero BLE address.
Returns
NRF_SUCCESS if successful.

Definition at line 287 of file ptu_registry.c.

288 {
289  uint8_t zero_address[BLE_GAP_ADDR_LEN] = {0};
290  if (memcmp(zero_address, address, BLE_GAP_ADDR_LEN) == 0)
291  return NRF_ERROR_INVALID_ADDR;
292 
294  reg_item_p->ble_wpts_c.conn_handle = BLE_CONN_HANDLE_INVALID;
295  memcpy(reg_item_p->address.addr, address, BLE_GAP_ADDR_LEN);
296 
297  return NRF_SUCCESS;
298 }
uint16_t conn_handle
Definition: ble_wpts_c.h:66
ble_wpts_c_t ble_wpts_c
Definition: ptu_registry.h:52
ble_gap_addr_t address
Definition: ptu_registry.h:51
ptu_reg_item_state_t state
Definition: ptu_registry.h:49
void ptu_reg_item_delete ( ptu_reg_item_t item)

Delete item that has previoulsy been added to registry.

Parameters
itemis the index in registry of the item to be deleted.

Definition at line 220 of file ptu_registry.c.

221 {
222  uint8_t i;
223 
224  for(i = 0; i < PTU_MAX_CONNECTIONS; i++)
225  {
226  if(item == &m_registry.items[i])
227  {
228  break;
229  }
230  }
231 
232  // Assert that this is a valid registry address.
233  ASSERT(i < PTU_MAX_CONNECTIONS);
234 
235  if(item->state != REG_ITEM_STATE_UNUSED)
236  {
237  ASSERT(m_registry.n_entries != 0);
238 
239  // Decrease age of items older than the one being deleted.
240  for(i = 0; i < PTU_MAX_CONNECTIONS; i++)
241  {
243  m_registry.items[i].age > item->age)
244  {
245  m_registry.items[i].age--;
246  }
247  }
248 
250  }
251  m_reg_item_init(item);
252 }
uint8_t n_entries
Definition: ptu_registry.h:76
ptu_reg_item_t items[PTU_MAX_CONNECTIONS]
Definition: ptu_registry.h:77
static ptu_reg_t m_registry
Definition: ptu_registry.c:51
static void m_reg_item_init(ptu_reg_item_t *item)
Set all values in a registry item to default values. 'timer_id' will be retained. ...
Definition: ptu_registry.c:56
#define PTU_MAX_CONNECTIONS
Definition: ptu_config.h:29
ptu_reg_item_state_t state
Definition: ptu_registry.h:49
bool ptu_reg_all_charged ( void  )

Function returning true if all items in registry are charged or there are not items in registry.

Returns
true if all items in registry are charged.

Definition at line 273 of file ptu_registry.c.

274 {
275  int i;
276 
277  for(i = 0; i < PTU_MAX_CONNECTIONS; i++)
278  {
280  {
281  return false;
282  }
283  }
284  return true;
285 }
ptu_reg_item_t items[PTU_MAX_CONNECTIONS]
Definition: ptu_registry.h:77
static ptu_reg_t m_registry
Definition: ptu_registry.c:51
uint8_t charged
Definition: ptu_registry.h:66
#define PTU_MAX_CONNECTIONS
Definition: ptu_config.h:29
ptu_reg_item_state_t state
Definition: ptu_registry.h:49