Nordic Semiconductor nRF5 AirFuel SDK  version 2.2.0
ptu_distant_list_handler.c File Reference
#include <string.h>
#include "nrf_assert.h"
#include "app_timer.h"
#include "ptu.h"
#include "ptu_config.h"
#include "ptu_distant_list_handler.h"

Go to the source code of this file.

Functions

 APP_TIMER_DEF (m_ignore_latency_timer_id)
 
static bool m_address_equals (ble_gap_addr_t const *p_a1, ble_gap_addr_t const *p_a2)
 Check if two ble_gap_addr_t addresses are equal. More...
 
static uint8_t m_next_index (uint8_t current_index)
 Get next index in distant list array. More...
 
static void m_ignore_latency_timer_id_handler (void *p_context)
 Hander for the ignore latency timer id. More...
 
static bool m_address_in_distant_list (ble_gap_addr_t const *p_addr)
 Check if given address is in the distant list. More...
 
static void m_remove_device (ble_gap_addr_t const *p_addr)
 Removes given address from distant list. More...
 
void ptu_dlh_notify_beacon_active (void)
 Notify distant list handler that the beacon is now active. More...
 
void ptu_dlh_notify_beacon_disabled (void)
 Notify distant list handler that the beacon is now disabled. More...
 
void ptu_dlh_on_wpt_adv_report (ble_gap_addr_t const *p_addr, uint8_t adv_flags)
 Distance list handler WPT Advertisement report handler. More...
 
bool ptu_dlh_device_is_in_distant_list (ble_gap_addr_t const *p_addr)
 Check if device with provided address is in the distant list. More...
 
uint32_t ptu_dlh_init (ptu_dlh_init_t const *p_init)
 Check if device with provided address is in the distant list. More...
 

Variables

static uint8_t m_distant_list_index
 
static bool m_ignoring_devices
 
static ble_gap_addr_t m_distant_list [PTU_DISTANT_LIST_LEN]
 
static uint32_t m_ignore_latency_ms
 

Function Documentation

APP_TIMER_DEF ( m_ignore_latency_timer_id  )

Timer ID for ensuring latency from notification of beacon being disabled to starting to add devices .

static bool m_address_equals ( ble_gap_addr_t const *  p_a1,
ble_gap_addr_t const *  p_a2 
)
static

Check if two ble_gap_addr_t addresses are equal.

Will assert if either of the address pointers are NULL

Parameters
p_a1First address to check
p_a2Address to check against
Returns
True if the addresses match, false otherwise

Definition at line 38 of file ptu_distant_list_handler.c.

39 {
40  APP_ERROR_CHECK_BOOL(p_a1 != NULL);
41  APP_ERROR_CHECK_BOOL(p_a2 != NULL);
42  return memcmp(p_a1, p_a2, sizeof(ble_gap_addr_t)) == 0;
43 }
static uint8_t m_next_index ( uint8_t  current_index)
static

Get next index in distant list array.

Works as a circular buffer where values are overwritten when it is full.

Parameters
current_indexThe current index
Returns
Next valid index of the distant list array.

Definition at line 51 of file ptu_distant_list_handler.c.

52 {
53  uint8_t incremented_index = current_index + 1;
54  return incremented_index >= PTU_DISTANT_LIST_LEN ? 0 : incremented_index;
55 }
#define PTU_DISTANT_LIST_LEN
Definition: ptu_config.h:45
static void m_ignore_latency_timer_id_handler ( void *  p_context)
static

Hander for the ignore latency timer id.

Will set 'm_ignoring_devices' to false, I.E. stop adding devices to the distant list

Parameters
p_contextThe given context

Definition at line 62 of file ptu_distant_list_handler.c.

63 {
64  m_ignoring_devices = false;
65 }
static bool m_ignoring_devices
static bool m_address_in_distant_list ( ble_gap_addr_t const *  p_addr)
static

Check if given address is in the distant list.

Parameters
p_addrAddress to check
Returns
True if the addresses is in distant list, false otherwise

Definition at line 72 of file ptu_distant_list_handler.c.

73 {
74  APP_ERROR_CHECK_BOOL(p_addr != NULL);
75 
76  for(uint8_t i = 0; i < PTU_DISTANT_LIST_LEN; ++i)
77  {
78  if(m_address_equals(&m_distant_list[i], p_addr))
79  return true;
80  }
81 
82  return false;
83 }
static ble_gap_addr_t m_distant_list[PTU_DISTANT_LIST_LEN]
#define PTU_DISTANT_LIST_LEN
Definition: ptu_config.h:45
static bool m_address_equals(ble_gap_addr_t const *p_a1, ble_gap_addr_t const *p_a2)
Check if two ble_gap_addr_t addresses are equal.
static void m_remove_device ( ble_gap_addr_t const *  p_addr)
static

Removes given address from distant list.

Will assert if p_addr is NULL

Parameters
p_addrAddress of device to remove
Returns
True if the addresses match, false otherwise

Definition at line 91 of file ptu_distant_list_handler.c.

92 {
93  APP_ERROR_CHECK_BOOL(p_addr != NULL);
94 
95  for(uint8_t i = 0; i < PTU_DISTANT_LIST_LEN; ++i)
96  {
97  if(m_address_equals(&m_distant_list[i], p_addr))
98  {
99  memset(&m_distant_list[i], 0, sizeof(ble_gap_addr_t));
100  }
101  }
102 }
static ble_gap_addr_t m_distant_list[PTU_DISTANT_LIST_LEN]
#define PTU_DISTANT_LIST_LEN
Definition: ptu_config.h:45
static bool m_address_equals(ble_gap_addr_t const *p_a1, ble_gap_addr_t const *p_a2)
Check if two ble_gap_addr_t addresses are equal.
void ptu_dlh_notify_beacon_active ( void  )

Notify distant list handler that the beacon is now active.

When this function is called, the distant list handler will not add new devices discovered to the distant list.

Definition at line 104 of file ptu_distant_list_handler.c.

105 {
106  uint32_t err_code;
107 
108  err_code = TIMER_START(m_ignore_latency_timer_id, m_ignore_latency_ms, NULL);
109  APP_ERROR_CHECK(err_code);
110 }
static uint32_t m_ignore_latency_ms
#define TIMER_START(timer_id, ms, p_ctx)
Definition: wpt.h:32
void ptu_dlh_notify_beacon_disabled ( void  )

Notify distant list handler that the beacon is now disabled.

When this function is called, the distant list handler will not add new devices discovered to the distant list.

Definition at line 112 of file ptu_distant_list_handler.c.

113 {
114  m_ignoring_devices = true;
115 }
static bool m_ignoring_devices
void ptu_dlh_on_wpt_adv_report ( ble_gap_addr_t const *  p_addr,
uint8_t  adv_flags 
)

Distance list handler WPT Advertisement report handler.

Will check address of advertisement and depending on the current state of the beacon, it will either add it to the distant list, or not.

Parameters
p_addrAddress of device advertising
adv_flagsWPT Advertisement Flags found in advertisement

Definition at line 117 of file ptu_distant_list_handler.c.

118 {
119  // Check reboot bit and remove device if reboot bit is set, and the device is currently in the distance list
120  if(BLE_WPTS_BITFIELD_READ( adv_flags,
123  {
124  if(m_address_in_distant_list(p_addr))
125  {
126  m_remove_device(p_addr);
127  }
128  }
129 
130  else if(m_ignoring_devices && !m_address_in_distant_list(p_addr))
131  {
133  memcpy(&m_distant_list[m_distant_list_index], p_addr, sizeof(ble_gap_addr_t));
134  }
135 }
#define BLE_WPTS_ADV_FLAG_REBOOT_INDICATOR_BITMSK
static ble_gap_addr_t m_distant_list[PTU_DISTANT_LIST_LEN]
static bool m_ignoring_devices
static bool m_address_in_distant_list(ble_gap_addr_t const *p_addr)
Check if given address is in the distant list.
static void m_remove_device(ble_gap_addr_t const *p_addr)
Removes given address from distant list.
#define BLE_WPTS_ADV_FLAG_REBOOT_INDICATOR_BITPOS
static uint8_t m_next_index(uint8_t current_index)
Get next index in distant list array.
#define BLE_WPTS_BITFIELD_READ(bitfield, msk, pos)
Read bitfield.
static uint8_t m_distant_list_index
bool ptu_dlh_device_is_in_distant_list ( ble_gap_addr_t const *  p_addr)

Check if device with provided address is in the distant list.

Will check address of advertisement and depending on the current state of the beacon, it will either add it to the distant list, or not.

Parameters
p_addrAddress to check
Returns
true if given address is in distant list, false otherwise

Definition at line 137 of file ptu_distant_list_handler.c.

138 {
139  APP_ERROR_CHECK_BOOL(p_addr != NULL);
140 
141  return m_address_in_distant_list(p_addr);
142 }
static bool m_address_in_distant_list(ble_gap_addr_t const *p_addr)
Check if given address is in the distant list.
uint32_t ptu_dlh_init ( ptu_dlh_init_t const *  p_init)

Check if device with provided address is in the distant list.

Parameters
p_initInitializing parameters
Returns
NRF_SUCCESS if success, otherwise error code.

Definition at line 144 of file ptu_distant_list_handler.c.

145 {
146  uint32_t err_code;
147 
148  if(p_init == NULL)
149  {
150  return NRF_ERROR_INVALID_PARAM;
151  }
152 
153  m_ignore_latency_ms = p_init->ignore_latency_ms;
154 
156 
157  m_ignoring_devices = true;
158 
159  memset(m_distant_list, 0, sizeof(m_distant_list));
160 
161  err_code = app_timer_create(&m_ignore_latency_timer_id, APP_TIMER_MODE_SINGLE_SHOT, m_ignore_latency_timer_id_handler);
162  if(err_code != NRF_SUCCESS)
163  {
164  return err_code;
165  }
166 
167  return NRF_SUCCESS;
168 }
static ble_gap_addr_t m_distant_list[PTU_DISTANT_LIST_LEN]
static bool m_ignoring_devices
static uint32_t m_ignore_latency_ms
static void m_ignore_latency_timer_id_handler(void *p_context)
Hander for the ignore latency timer id.
static uint8_t m_distant_list_index

Variable Documentation

uint8_t m_distant_list_index
static

Current index in distant list array.

Definition at line 25 of file ptu_distant_list_handler.c.

bool m_ignoring_devices
static

Is devices currently being added to the distant list? I.E. is the Long Beacon NOT active?

Definition at line 26 of file ptu_distant_list_handler.c.

ble_gap_addr_t m_distant_list[PTU_DISTANT_LIST_LEN]
static

Distant list, list of GAP addresses to be ignored.

Definition at line 27 of file ptu_distant_list_handler.c.

uint32_t m_ignore_latency_ms
static

How long after getting notified of beacon disabled should the distant list handler start adding devices to the distant list?

Definition at line 28 of file ptu_distant_list_handler.c.