Nordic Semiconductor nRF5 AirFuel SDK  version 2.2.0
ptu_distant_list_handler.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 
16 #include <string.h>
17 
18 #include "nrf_assert.h"
19 #include "app_timer.h"
20 #include "ptu.h"
21 #include "ptu_config.h"
23 
24 APP_TIMER_DEF(m_ignore_latency_timer_id);
25 static uint8_t m_distant_list_index;
26 static bool m_ignoring_devices;
27 static ble_gap_addr_t m_distant_list[PTU_DISTANT_LIST_LEN];
28 static uint32_t m_ignore_latency_ms;
38 static bool m_address_equals(ble_gap_addr_t const * p_a1, ble_gap_addr_t const * p_a2)
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 }
44 
51 static uint8_t m_next_index(uint8_t current_index)
52 {
53  uint8_t incremented_index = current_index + 1;
54  return incremented_index >= PTU_DISTANT_LIST_LEN ? 0 : incremented_index;
55 }
56 
62 static void m_ignore_latency_timer_id_handler(void * p_context)
63 {
64  m_ignoring_devices = false;
65 }
66 
72 static bool m_address_in_distant_list(ble_gap_addr_t const * p_addr)
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 }
84 
91 static void m_remove_device(ble_gap_addr_t const * p_addr)
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 }
103 
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 }
111 
113 {
114  m_ignoring_devices = true;
115 }
116 
117 void ptu_dlh_on_wpt_adv_report(ble_gap_addr_t const * p_addr, uint8_t adv_flags)
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 }
136 
137 bool ptu_dlh_device_is_in_distant_list(ble_gap_addr_t const * p_addr)
138 {
139  APP_ERROR_CHECK_BOOL(p_addr != NULL);
140 
141  return m_address_in_distant_list(p_addr);
142 }
143 
144 uint32_t ptu_dlh_init(ptu_dlh_init_t const * p_init)
145 {
146  uint32_t err_code;
147 
148  if(p_init == NULL)
149  {
150  return NRF_ERROR_INVALID_PARAM;
151  }
152 
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 }
void ptu_dlh_notify_beacon_disabled(void)
Notify distant list handler that the beacon is now disabled.
#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
void ptu_dlh_notify_beacon_active(void)
Notify distant list handler that the beacon is now active.
static uint32_t m_ignore_latency_ms
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.
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.
#define BLE_WPTS_ADV_FLAG_REBOOT_INDICATOR_BITPOS
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.
#define TIMER_START(timer_id, ms, p_ctx)
Definition: wpt.h:32
static void m_ignore_latency_timer_id_handler(void *p_context)
Hander for the ignore latency timer id.
static uint8_t m_next_index(uint8_t current_index)
Get next index in distant list array.
#define PTU_DISTANT_LIST_LEN
Definition: ptu_config.h:45
uint32_t ptu_dlh_init(ptu_dlh_init_t const *p_init)
Check if device with provided address is in the distant list.
#define BLE_WPTS_BITFIELD_READ(bitfield, msk, pos)
Read bitfield.
APP_TIMER_DEF(m_ignore_latency_timer_id)
static uint8_t m_distant_list_index
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.