Nordic Semiconductor nRF5 AirFuel SDK  version 2.2.0
ptu_registry.c
Go to the documentation of this file.
1 /* Copyright (c) Nordic Semiconductor. All Rights Reserved.
2  *
3  * The information contained herein is property of Nordic Semiconductor ASA.
4  * Terms and conditions of usage are described in detail in NORDIC
5  * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
6  *
7  * Licensees are granted free, non-transferable use of the information. NO
8  * WARRANTY of ANY KIND is provided. This heading must NOT be removed from
9  * the file.
10  *
11  */
12 
15 #include <stdint.h>
16 #include <string.h>
17 
18 #include "nrf_assert.h"
19 #include "ptu_registry.h"
20 
21 /******************************************************************************/
24 /******************************************************************************/
25 
26 #if PTU_MAX_CONNECTIONS > 0
27 APP_TIMER_DEF(m_reg_timer_0);
28 #endif
29 #if PTU_MAX_CONNECTIONS > 1
30 APP_TIMER_DEF(m_reg_timer_1);
31 #endif
32 #if PTU_MAX_CONNECTIONS > 2
33 APP_TIMER_DEF(m_reg_timer_2);
34 #endif
35 #if PTU_MAX_CONNECTIONS > 3
36 APP_TIMER_DEF(m_reg_timer_3);
37 #endif
38 #if PTU_MAX_CONNECTIONS > 4
39 APP_TIMER_DEF(m_reg_timer_4);
40 #endif
41 #if PTU_MAX_CONNECTIONS > 5
42 APP_TIMER_DEF(m_reg_timer_5);
43 #endif
44 #if PTU_MAX_CONNECTIONS > 6
45 APP_TIMER_DEF(m_reg_timer_6);
46 #endif
47 #if PTU_MAX_CONNECTIONS > 7
48 APP_TIMER_DEF(m_reg_timer_7);
49 #endif
50 
56 static void m_reg_item_init(ptu_reg_item_t * item)
57 {
58  app_timer_id_t temp;
59  temp = item->timer_id;
60  memset(item, 0, sizeof(ptu_reg_item_t));
61  item->ble_wpts_c.conn_handle = BLE_CONN_HANDLE_INVALID;
62  item->timer_id = temp;
63 }
64 
70 {
71  int i;
72 
73  for(i = 0; i < PTU_MAX_CONNECTIONS; i++)
74  {
75  if(m_registry.items[i].state == REG_ITEM_STATE_UNUSED)
76  {
77  return &m_registry.items[i];
78  }
79  }
80 
81  return NULL;
82 }
83 
86 /******************************************************************************/
89 /******************************************************************************/
90 
91 void ptu_reg_init(void)
92 {
93  int i;
94  m_registry.n_entries = 0;
95 
96  for(i = 0; i < PTU_MAX_CONNECTIONS; i++)
97  {
98  m_reg_item_init(&m_registry.items[i]);
99  }
100 #if PTU_MAX_CONNECTIONS > 0
101  m_registry.items[0].timer_id = m_reg_timer_0;
102 #endif
103 #if PTU_MAX_CONNECTIONS > 1
104  m_registry.items[1].timer_id = m_reg_timer_1;
105 #endif
106 #if PTU_MAX_CONNECTIONS > 2
107  m_registry.items[2].timer_id = m_reg_timer_2;
108 #endif
109 #if PTU_MAX_CONNECTIONS > 3
110  m_registry.items[3].timer_id = m_reg_timer_3;
111 #endif
112 #if PTU_MAX_CONNECTIONS > 4
113  m_registry.items[4].timer_id = m_reg_timer_4;
114 #endif
115 #if PTU_MAX_CONNECTIONS > 5
116  m_registry.items[5].timer_id = m_reg_timer_5;
117 #endif
118 #if PTU_MAX_CONNECTIONS > 6
119  m_registry.items[6].timer_id = m_reg_timer_6;
120 #endif
121 #if PTU_MAX_CONNECTIONS > 7
122  m_registry.items[7].timer_id = m_reg_timer_7;
123 #endif
124 }
125 
126 uint32_t ptu_reg_item_get_from_index(uint8_t index, ptu_reg_item_t ** item)
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 }
136 
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 && \
144  m_registry.items[i].state != REG_ITEM_STATE_UNUSED)
145  {
146  return &m_registry.items[i];
147  }
148  }
149  }
150  return NULL;
151 }
152 
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 && \
160  m_registry.items[i].state != REG_ITEM_STATE_UNUSED)
161  {
162  return &m_registry.items[i];
163  }
164  }
165  }
166  return NULL;
167 }
168 
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 }
186 
187 ptu_reg_item_t * ptu_reg_item_add(ble_gap_evt_adv_report_t * p_adv_report, ptu_reg_item_state_t init_state)
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  {
201  if(m_registry.items[i].state != REG_ITEM_STATE_UNUSED)
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;
214  m_registry.n_entries++;
215  return reg_item;
216  }
217  return NULL;
218 }
219 
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  {
242  if(m_registry.items[i].state != REG_ITEM_STATE_UNUSED && \
243  m_registry.items[i].age > item->age)
244  {
245  m_registry.items[i].age--;
246  }
247  }
248 
249  m_registry.n_entries--;
250  }
251  m_reg_item_init(item);
252 }
253 
254 uint8_t ptu_reg_registered_devices_get(ptu_reg_item_t** registered_devices)
255 {
256  uint8_t n_devices = 0;
257 
258  for(uint8_t i = 0; i < PTU_MAX_CONNECTIONS; i++)
259  {
260  if(m_registry.items[i].state == REG_ITEM_STATE_REGISTERED)
261  {
262  registered_devices[n_devices++] = &m_registry.items[i];
263  }
264  }
265  return n_devices;
266 }
267 
269 {
270  return m_registry.n_entries;
271 }
272 
274 {
275  int i;
276 
277  for(i = 0; i < PTU_MAX_CONNECTIONS; i++)
278  {
279  if((m_registry.items[i].state != REG_ITEM_STATE_UNUSED) && (!m_registry.items[i].charged) )
280  {
281  return false;
282  }
283  }
284  return true;
285 }
286 
287 uint32_t ptu_reg_set_device_waiting_to_connect(ptu_reg_item_t * reg_item_p, uint8_t * address)
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 }
void ptu_reg_item_delete(ptu_reg_item_t *item)
Delete item that has previoulsy been added to registry.
Definition: ptu_registry.c:220
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
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.
Definition: ptu_registry.c:187
uint16_t conn_handle
Definition: ble_wpts_c.h:66
uint8_t ptu_reg_registered_devices_get(ptu_reg_item_t **registered_devices)
Get handles for all registered devices.
Definition: ptu_registry.c:254
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
uint32_t ptu_reg_item_get_from_index(uint8_t index, ptu_reg_item_t **item)
Get registry item from index in database. Index must be < PTU_MAX_CONNECTIONS.
Definition: ptu_registry.c:126
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.
Definition: ptu_registry.c:268
APP_TIMER_DEF(m_battery_timer_id)
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
Registry.
Definition: ptu_registry.h:74
uint8_t charged
Definition: ptu_registry.h:66
ptu_reg_item_state_t
Registry item status.
Definition: ptu_registry.h:31
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
ble_gap_addr_t address
Definition: ptu_registry.h:51
bool ptu_reg_all_charged(void)
Function returning true if all items in registry are charged or there are not items in registry...
Definition: ptu_registry.c:273
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 ...
Definition: ptu_registry.c:287
ptu_reg_item_t * ptu_reg_item_oldest_get(ptu_reg_item_state_t state)
Get oldest registry item having a specific state.
Definition: ptu_registry.c:169
void ptu_reg_init(void)
Initialize registry database.
Definition: ptu_registry.c:91
ptu_reg_item_t * ptu_reg_item_get_from_conn_handle(uint16_t conn_handle)
Get registry item from connection handle.
Definition: ptu_registry.c:137
app_timer_id_t timer_id
Definition: ptu_registry.h:56
#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