Nordic Semiconductor nRF5 AirFuel SDK  version 2.2.0
ptu_conn_man.c File Reference
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include "nrf_assert.h"
#include "ble_hci.h"
#include "ptu_sensors.h"
#include "ptu_conn_man.h"
#include "ptu_power_ctl.h"
#include "ptu_distant_list_handler.h"

Go to the source code of this file.

Functions

API implementation
void ptu_cm_remove_device (ptu_reg_item_t *reg_item_p)
 Disconnect and/or unregister any device. More...
 
bool ptu_cm_mode_trans_in_progress (void)
 Get mode transition status. More...
 
uint32_t ptu_cm_init (ptu_sm_handler_t sm_handler)
 Initialize connection manager. More...
 
void ptu_cm_on_ble_evt (ble_evt_t *p_ble_evt)
 BLE event handler. More...
 
void ptu_cm_on_wpt_service_evt (ble_wpts_c_t *p_wpts_c, ble_wpts_c_evt_t *const p_wpts_c_evt)
 Handle service events. More...
 
void ptu_cm_scan_disable (void)
 Disable scanning.
 
void ptu_cm_scan_enable (void)
 Enable scanning.
 
void ptu_cm_disconnect_all (void)
 Disconnect all PRUs.
 
void ptu_cm_dynamic_read_all (void)
 Issue read request for the PRU dynamic characteristic to all registerred PRUs.
 

Static functions and variables

enum  ptu_cm_time_set_state_t { PTU_CM_TIME_SET_STATE_IDLE = 0, PTU_CM_TIME_SET_STATE_P_TX_LOW, PTU_CM_TIME_SET_STATE_P_TX_HIGH }
 
static ptu_cm_status_t m_status
 
static ptu_static_t m_ptu_static
 
static ptu_sm_handler_t m_sm_handler
 
static const ble_gap_conn_params_t m_conn_params
 
 APP_TIMER_DEF (m_mode_trans_timer_id)
 
 APP_TIMER_DEF (m_time_set_check_timer_id)
 
static void m_connect (ble_gap_addr_t const *p_addr)
 
static void m_disconnect (uint16_t conn_handle)
 
static void m_on_fully_accepted_adv_packet (ptu_reg_item_t *reg_item_p, ble_gap_evt_adv_report_t *p_adv_report)
 
static void m_connection_init (ptu_reg_item_t *reg_item)
 
static void m_on_reg_timeout (ptu_reg_item_t *reg_item_p)
 
static void m_mode_trans_timer_handle (void *p_context)
 
static void m_set_reg_item_fully_accepted (ptu_reg_item_t *reg_item_p)
 
static void m_reg_timer_handle (void *p_context)
 
static void m_send_ctrl_denied_due_to_cc (ptu_reg_item_t *reg_item_p)
 
static void m_stop_time_set_procedure (ptu_cm_timeset_data_t *p_ts)
 
static void m_ptu_time_set_check_timer_handler (void *p_context)
 Timeout handler for the time set functionality. More...
 
static void m_scan_maintain (void)
 
static bool m_imp_shift_required (uint8_t adv_flags)
 
static void m_update_pre_connect_adv_cnt_for_reg_item (ptu_reg_item_t *reg_item_p)
 
static void m_ensure_device_is_in_registry (ptu_reg_item_t **reg_item_p, ble_gap_evt_adv_report_t *p_adv_report, ptu_reg_item_state_t init_state)
 
static bool m_load_var_detected (void)
 
static void m_on_partially_accepted_adv_packet (ptu_reg_item_t *reg_item_p, ble_gap_evt_adv_report_t *p_adv_report)
 
static void m_on_wpt_adv_report (ble_evt_t *p_ble_evt, uint8_t adv_flags)
 
static void m_on_scan_response (ble_evt_t *p_ble_evt)
 
static void m_device_registration_complete (ptu_reg_item_t *reg_item_p)
 
static void m_on_evt_connected (ble_evt_t *p_ble_evt)
 
static void m_on_evt_timeout (ble_evt_t *p_ble_evt)
 
static void m_on_evt_disconnected (ble_evt_t *p_ble_evt)
 
static void m_pairing_start (uint16_t conn_handle)
 
static void m_on_evt_rw_response (ble_evt_t *p_ble_evt)
 
static void m_on_evt_seq_request (ble_evt_t *p_ble_evt)
 
static void m_on_evt_sec_params_request (ble_evt_t *p_ble_evt)
 
static void m_on_evt_conn_sec_update (ble_evt_t *p_ble_evt)
 
static void m_on_wpt_alert (ble_wpts_c_t *p_wpts_c, ble_wpts_c_evt_t *const p_wpts_c_evt)
 
static void ptu_char_pru_dynamic_read (uint16_t conn_handle)
 Set pending dynamic read for PRU.
 

Enumeration Type Documentation

Substates during recognition of load variance shift "time set pattern"

Enumerator
PTU_CM_TIME_SET_STATE_IDLE 

No search for time set pattern.

PTU_CM_TIME_SET_STATE_P_TX_LOW 

Expecting low P_TX shift.

PTU_CM_TIME_SET_STATE_P_TX_HIGH 

Expecting P_TX to return to initial value.

Definition at line 33 of file ptu_conn_man.c.

Function Documentation

APP_TIMER_DEF ( m_mode_trans_timer_id  )

Mode transition timer ID.

APP_TIMER_DEF ( m_time_set_check_timer_id  )

"Time set check" timer ID.

static void m_connect ( ble_gap_addr_t const *  p_addr)
static

Wrapper function for calling sd_ble_gap_connect(). Will update current state of m_status.

Parameters
[in]p_addrPointer to GAP address to connect to

Definition at line 90 of file ptu_conn_man.c.

91 {
92  ble_gap_scan_params_t scan_params;
93  uint32_t err_code, diff;
94 
95  if(m_status.connecting || m_status.nof_connections >= PTU_MAX_CONNECTIONS || p_addr == NULL)
96  return;
97 
98  m_status.connecting = true;
99  m_status.scanning = false;
100 
101  memcpy(&m_status.curr_device_connecting, p_addr, sizeof(ble_gap_addr_t));
102 
103  memcpy(&scan_params, &m_status.scan_params, sizeof(scan_params));
104  scan_params.timeout = 1;
105 
106  if(m_status.mode_trans_in_progess)
107  {
108  // Can safely assume that ticks now is lower than TICKS_AT_MT
109  uint32_t ticks_now;
110 
111  err_code = app_timer_cnt_get(&ticks_now);
112  APP_ERROR_CHECK(err_code);
113 
114  diff = ticks_diff(m_status.ticks_at_mt_end, ticks_now) / 32000 ;
115  scan_params.timeout += diff;
116  }
117 
118  do
119  {
120  err_code = sd_ble_gap_connect(p_addr, &scan_params, &m_conn_params);
121  }while(err_code == NRF_ERROR_BUSY);
122 
123  APP_ERROR_CHECK(err_code);
124 }
uint32_t ticks_diff(uint32_t ticks_now, uint32_t ticks_old)
Definition: common.c:78
static const ble_gap_conn_params_t m_conn_params
Definition: ptu_conn_man.c:79
static ptu_cm_status_t m_status
Definition: ptu_conn_man.c:64
#define PTU_MAX_CONNECTIONS
Definition: ptu_config.h:29
static void m_disconnect ( uint16_t  conn_handle)
static

Disconnect from conn_handle. Will not fail if disconnection has already been initialized.

Parameters
conn_handleConnection handle to disconnect from.

Definition at line 132 of file ptu_conn_man.c.

133 {
134  uint32_t err_code = sd_ble_gap_disconnect(conn_handle, BLE_HCI_REMOTE_USER_TERMINATED_CONNECTION);
135 
136  if((err_code != NRF_ERROR_INVALID_STATE) && (err_code != BLE_ERROR_INVALID_CONN_HANDLE))
137  APP_ERROR_CHECK(err_code);
138 }
static void m_connection_init ( ptu_reg_item_t reg_item)
static

Initiate connection and registration of PRU. Shall be called after adv. report only.

Parameters
reg_itemPRU for which to start connection/registration.

Definition at line 148 of file ptu_conn_man.c.

149 {
150  uint32_t err_code;
151 
152  ASSERT(reg_item != NULL);
154 
155  err_code = app_timer_stop(reg_item->timer_id);
156  APP_ERROR_CHECK(err_code);
157 
158  // Start registrartion timer for this registry item
159  err_code = app_timer_start(reg_item->timer_id, APP_TIMER_TICKS(PTU_REG_TIMEOUT_MS, APP_TIMER_PRESCALER), reg_item);
160  APP_ERROR_CHECK(err_code);
161 }
#define APP_TIMER_PRESCALER
Definition: pru.h:33
app_timer_id_t timer_id
Definition: ptu_registry.h:56
ptu_reg_item_state_t state
Definition: ptu_registry.h:49
#define PTU_REG_TIMEOUT_MS
Definition: ptu_config.h:132
static void m_ptu_time_set_check_timer_handler ( void *  p_context)
static

Timeout handler for the time set functionality.

Will be called every PTU_TIME_SET_CHECK_INTERVAL_MS after receiving CONTROL RSP from PRU that the PTU will perform time set on. To handle artifacts and jitter in the timer, the upper and lower limits are expanded compared to the BSS.

Parameters
p_contextTimeout context, contains pointer to registry item for time set target PRU.

Definition at line 270 of file ptu_conn_man.c.

271 {
272  static ptu_cm_timeset_data_t ts;
273  uint32_t err_code;
274  const ptu_sensor_data_t * sensor_data;
275  ptu_reg_item_t * reg_item_p = p_context;
276 
277  err_code = ptu_sensors_read();
278  APP_ERROR_CHECK(err_code);
279 
280  err_code = ptu_sensors_data_get(&sensor_data);
281  APP_ERROR_CHECK(err_code);
282 
283  if(abs(sensor_data->p_tx_in - m_status.time_set_p_tx_in_reference) >= PTU_P_TX_IN_LOAD_DETECT)
284  {
285  ts.variation_duration+= 2;
286  ts.load_variation = true;
287  }
288  // load variation has stopped, check length
289  else if(ts.load_variation)
290  {
291  if(ts.variation_duration >= PTU_TIME_SET_LOWER_LIMIT_MS &&
292  ts.variation_duration <= PTU_TIME_SET_UPPER_LIMIT_MS)
293  {
294  m_stop_time_set_procedure(&ts);
295  }
296  // reset time set data in case valid variation is found later
297  ts.variation_duration = 0;
298  ts.load_variation = false;
299  }
300 
301  // > 500 ms has passed with no valid time set load found.
302  if(ts.nof_samples_taken++ >= PTU_TIME_SET_500_MS_OF_SAMPLES)
303  {
304  m_stop_time_set_procedure(&ts);
305  m_send_ctrl_denied_due_to_cc(reg_item_p);
306  }
307 }
#define PTU_P_TX_IN_LOAD_DETECT
Definition: ptu_config.h:73
Registry item.
Definition: ptu_registry.h:47
uint32_t ptu_sensors_read(void)
Read sensors. This function should typically be called regularly by a timer and can generate events b...
Definition: ptu_sensors.c:303
#define PTU_TIME_SET_500_MS_OF_SAMPLES
Definition: ptu_config.h:91
#define PTU_TIME_SET_UPPER_LIMIT_MS
Definition: ptu_config.h:97
#define PTU_TIME_SET_LOWER_LIMIT_MS
Definition: ptu_config.h:93
uint32_t ptu_sensors_data_get(const ptu_sensor_data_t **sensors_data)
Get the latest data read from the PTU sensors.
Definition: ptu_sensors.c:404
static ptu_cm_status_t m_status
Definition: ptu_conn_man.c:64
Definition of PTU sensor data.
Definition: ptu_hw_config.h:99
static void m_scan_maintain ( void  )
static

Function evaluating flag m_scanning_requested and starts / stops scanning accordingly. Updates flag m_scanning_on.

Definition at line 314 of file ptu_conn_man.c.

315 {
316  uint32_t err_code;
317 
318  // Shall we start scanning?
319  if(m_status.scanning_reqested && !m_status.scanning && !m_status.connecting)
320  {
321  if(ptu_reg_n_entries_get() == 0)
322  {
323  m_status.scan_params.window = PTU_POWER_SAVE_STATE_SCAN_WINDOW;
324  m_status.scan_params.interval = PTU_POWER_SAVE_STATE_SCAN_INTERVAL;
325  }
326  else
327  {
328  m_status.scan_params.window = PTU_NORMAL_SCAN_WINDOW;
329  m_status.scan_params.interval = PTU_NORMAL_SCAN_INTERVAL;
330  }
331 
332  do{
333  err_code = sd_ble_gap_scan_start(&m_status.scan_params);
334  }while(err_code != NRF_SUCCESS);
335 
336  //m_reconnect_enable = true;
337  m_status.scanning = true;
338  }
339 
340  // Shall we stop scanning?
341  if(!m_status.scanning_reqested && m_status.scanning)
342  {
343  if(m_status.scanning)
344  {
345  do{
346  err_code = sd_ble_gap_scan_stop();
347  }while(err_code != NRF_SUCCESS);
348  m_status.scanning = false;
349  }
350  }
351 }
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
#define PTU_NORMAL_SCAN_WINDOW
Definition: ptu_config.h:120
#define PTU_POWER_SAVE_STATE_SCAN_INTERVAL
Definition: ptu_config.h:113
#define PTU_NORMAL_SCAN_INTERVAL
Definition: ptu_config.h:118
static ptu_cm_status_t m_status
Definition: ptu_conn_man.c:64
#define PTU_POWER_SAVE_STATE_SCAN_WINDOW
Definition: ptu_config.h:114
static void m_on_wpt_adv_report ( ble_evt_t *  p_ble_evt,
uint8_t  adv_flags 
)
static

Function to be when advertise packet with WPT UUID has been received.

Parameters
p_ble_evtevent data forwarded from softdevice.
adv_flagsthe adv flags from the adv packet.

Definition at line 447 of file ptu_conn_man.c.

448 {
449  ptu_reg_item_t * reg_item_p;
450  bool imp_shift_ok, rssi_ok, fully_accepted, state_ok;
451 
452 #ifndef PTU_DISABLE_DISTANT_LIST
453  ptu_dlh_on_wpt_adv_report(&p_ble_evt->evt.gap_evt.params.adv_report.peer_addr, adv_flags);
454 #endif //PTU_DISABLE_DISTANT_LIST
455 
456  if( !ptu_dlh_device_is_in_distant_list(&p_ble_evt->evt.gap_evt.params.adv_report.peer_addr) &&
457  (ptu_sm_execute(PTU_SM_SIGNAL_NULL, NULL) != PTU_SM_STATE_LOW_POWER || ptu_reg_n_entries_get() == 0))
458  {
459  reg_item_p = ptu_reg_item_get_from_address(&p_ble_evt->evt.gap_evt.params.adv_report.peer_addr);
460 
461  if(reg_item_p != NULL && reg_item_p->reconnect)
462  {
463  reg_item_p->reconnect = false;
464  m_connect(&reg_item_p->address);
465  }
466 
467  else
468  {
469  state_ok = (reg_item_p == NULL || reg_item_p->state == REG_ITEM_STATE_PRE_CONNECT || reg_item_p->state == REG_ITEM_STATE_FULLY_ACCEPTED);
470  rssi_ok = ((p_ble_evt->evt.gap_evt.params.adv_report.rssi > PTU_ADV_PWR_MIN) || !PTU_CCA_RSSI_ENABLE);
471  imp_shift_ok = ((m_imp_shift_required(adv_flags) && m_load_var_detected()) || (!m_imp_shift_required(adv_flags) || !PTU_CCA_ADV_IMP_SHIFT_ENABLE));
472  fully_accepted = (rssi_ok && imp_shift_ok) || reg_item_p->state == REG_ITEM_STATE_FULLY_ACCEPTED;
473 
474  // If all connection conditions are fulfilled
475  if(fully_accepted && state_ok)
476  {
477  m_on_fully_accepted_adv_packet(reg_item_p, &p_ble_evt->evt.gap_evt.params.adv_report);
478  }
479  // If one of the connection conditions are fulfilled
480  else if(rssi_ok || imp_shift_ok)
481  {
482  m_on_partially_accepted_adv_packet(reg_item_p, &p_ble_evt->evt.gap_evt.params.adv_report);
483  }
484  }
485  }
486 }
Registry item.
Definition: ptu_registry.h:47
static void m_connect(ble_gap_addr_t const *p_addr)
Definition: ptu_conn_man.c:90
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 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
uint8_t reconnect
Definition: ptu_registry.h:70
#define PTU_ADV_PWR_MIN
Definition: ptu_hw_config.h:37
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.
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.
ble_gap_addr_t address
Definition: ptu_registry.h:51
#define PTU_CCA_RSSI_ENABLE
Definition: ptu_config.h:85
ptu_reg_item_state_t state
Definition: ptu_registry.h:49
#define PTU_CCA_ADV_IMP_SHIFT_ENABLE
Definition: ptu_hw_config.h:51
ptu_sm_state_t ptu_sm_execute(ptu_sm_signal_type_t signal, ptu_sm_state_vars_t *p_state)
State machine input signal handler.
Definition: ptu_sm.c:94
static void m_on_scan_response ( ble_evt_t *  p_ble_evt)
static

Function to be called after "scan response".

Parameters
p_ble_evtevent data forwarded from softdevice.

Definition at line 492 of file ptu_conn_man.c.

493 {
494  ptu_reg_item_t * reg_item_p;
495 
496  reg_item_p = ptu_reg_item_get_from_address(&p_ble_evt->evt.gap_evt.params.adv_report.peer_addr);
497 
498  // Only care about scan response from devices added to registry
499  if( reg_item_p != NULL &&
501  {
502  m_connect(&reg_item_p->address);
503 
504  reg_item_p->state = REG_ITEM_STATE_CONNECTING;
505  }
506 }
Registry item.
Definition: ptu_registry.h:47
static void m_connect(ble_gap_addr_t const *p_addr)
Definition: ptu_conn_man.c:90
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
ble_gap_addr_t address
Definition: ptu_registry.h:51
ptu_reg_item_state_t state
Definition: ptu_registry.h:49
static void m_on_evt_connected ( ble_evt_t *  p_ble_evt)
static

Function to be called after connection to a device is established. on BLE_GAP_EVT_CONNECTED.

Parameters
p_ble_evtevent data forwarded from softdevice.

Definition at line 528 of file ptu_conn_man.c.

529 {
530  ptu_reg_item_t * reg_item_p;
531  uint32_t err_code;
532  reg_item_p = ptu_reg_item_get_from_address(&p_ble_evt->evt.gap_evt.params.connected.peer_addr);
533 
534  ASSERT(m_status.connecting);
535  m_status.connecting = false;
536  m_status.nof_connections++;
537 
538  if(reg_item_p != NULL)
539  {
540  // State is 'REGISTERED' if it is a reconnection, otherwise it is 'CONNECTING'
541  ASSERT(reg_item_p->state == REG_ITEM_STATE_CONNECTING || reg_item_p->state == REG_ITEM_STATE_REGISTERED);
542  reg_item_p->ble_wpts_c.conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
543 
544  memset(&m_status.curr_device_connecting, 0, sizeof(ble_gap_addr_t));
545 
546  // Connection is of a new device, perform registration
547  if(reg_item_p->state == REG_ITEM_STATE_CONNECTING)
548  {
550  err_code = ble_wpts_c_read_pru_static(&(reg_item_p -> ble_wpts_c));
552  }
553 
554  // Connection is a reconnection
555  else if(reg_item_p->state == REG_ITEM_STATE_REGISTERED)
556  {
557  // Stop reconnection timeout timer
558  err_code = app_timer_stop(reg_item_p->timer_id);
559  APP_ERROR_CHECK(err_code);
560 
561  // Re-send PRU control packet to ensure it is set to same state as before reconnection
562  err_code = ble_wpts_c_send_pru_control(&reg_item_p->ble_wpts_c, &reg_item_p->prev_ctl);
563  APP_ERROR_CHECK(err_code);
564  }
565 
566  }
567  else
568  {
569  m_disconnect(p_ble_evt->evt.common_evt.conn_handle);
570  }
571 }
pru_control_t prev_ctl
Definition: ptu_registry.h:55
Registry item.
Definition: ptu_registry.h:47
uint16_t conn_handle
Definition: ble_wpts_c.h:66
ble_wpts_c_t ble_wpts_c
Definition: ptu_registry.h:52
uint32_t ble_wpts_c_read_pru_static(ble_wpts_c_t *p_wpts_c)
Read the PRU Static Parameter characterisic value from the PRU server.
Definition: ble_wpts_c.c:403
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
#define APP_ERROR_CHECK_ALLOW_DISCONNECT(ERR_CODE)
Macro equivalent to APP_ERROR_CHECK, except that error codes returned if connection is lost will be a...
Definition: wpt.h:228
static void m_disconnect(uint16_t conn_handle)
Definition: ptu_conn_man.c:132
app_timer_id_t timer_id
Definition: ptu_registry.h:56
static ptu_cm_status_t m_status
Definition: ptu_conn_man.c:64
uint32_t ble_wpts_c_send_pru_control(ble_wpts_c_t *p_wpts_c, pru_control_t *p_wpts_pru_control)
Send a PRU Control message to the PRU server.
Definition: ble_wpts_c.c:366
ptu_reg_item_state_t state
Definition: ptu_registry.h:49
static void m_on_evt_timeout ( ble_evt_t *  p_ble_evt)
static

Function to be called on BLE_GAP_EVT_TIMEOUT event.

Parameters
p_ble_evtevent data forwarded from softdevice.

Definition at line 577 of file ptu_conn_man.c.

578 {
579  ptu_reg_item_t * reg_item_p;
580 
581  // If connection timeout
582  if(p_ble_evt->evt.gap_evt.params.timeout.src == BLE_GAP_TIMEOUT_SRC_CONN)
583  {
584  ASSERT(m_status.connecting);
585 
586  m_status.connecting = false;
587 
588  reg_item_p = ptu_reg_item_get_from_address(&m_status.curr_device_connecting);
589 
590  if(reg_item_p == NULL)
591  return;
592 
593  ptu_reg_item_delete(reg_item_p);
594 
595  if(m_status.nof_connections == 0)
596  {
597  m_sm_handler(PTU_SM_SIGNAL_ALL_DEVICES_DISCONNECTED);
598  }
599  }
600  else if(p_ble_evt->evt.gap_evt.params.timeout.src == BLE_GAP_TIMEOUT_SRC_SECURITY_REQUEST)
601  {
602  // We will not perform any handling if security request times out. This is done to be compatible with wrongly implemented devices.
603  }
604 }
void ptu_reg_item_delete(ptu_reg_item_t *item)
Delete item that has previoulsy been added to registry.
Definition: ptu_registry.c:220
Registry item.
Definition: ptu_registry.h:47
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
static ptu_sm_handler_t m_sm_handler
State machine event handler.
Definition: ptu_sensors.c:55
static ptu_cm_status_t m_status
Definition: ptu_conn_man.c:64
static void m_on_evt_disconnected ( ble_evt_t *  p_ble_evt)
static

Function to be called on BLE_GAP_EVT_DISCONNECTED event.

Parameters
p_ble_evtevent data forwarded from softdevice.

Definition at line 610 of file ptu_conn_man.c.

611 {
612  const ptu_sensor_data_t * p_sensor_data;
613  ptu_reg_item_t * reg_item_p;
614  uint32_t err_code;
615  uint8_t zero_address[BLE_GAP_ADDR_LEN];
616 
617  memset(zero_address, 0, BLE_GAP_ADDR_LEN);
618 
619  m_status.nof_connections--;
620 
621  reg_item_p = ptu_reg_item_get_from_conn_handle(p_ble_evt->evt.gap_evt.conn_handle);
622 
623  if(memcmp(reg_item_p->address.addr, zero_address, BLE_GAP_ADDR_LEN) == 0)
624  {
625  ptu_reg_item_delete(reg_item_p); // Mode transition with Zero-address, delete device to ensure that registration performed.
626  }
627 
628  else if(reg_item_p != NULL)
629  {
630  reg_item_p->pending_dyn_read = false;
631 
632  err_code = ptu_sensors_data_get(&p_sensor_data);
633  APP_ERROR_CHECK(err_code);
634 
635  if( !p_sensor_data->load_var_detected && reg_item_p->state == REG_ITEM_STATE_REGISTERED)
636  {
637  if(!m_status.mode_trans_in_progess)
638  {
639  reg_item_p->reconnect = true;
640 
641  err_code = app_timer_start(reg_item_p->timer_id, APP_TIMER_TICKS(PTU_RECONNECT_TIMEOUT_MS, APP_TIMER_PRESCALER), reg_item_p);
642  APP_ERROR_CHECK(err_code);
643  }
644 
645  else // Mode transition with non-zero address
646  {
647  m_connect(&reg_item_p->address);
648  }
649 
650  }
651 
652  else
653  {
654  ptu_reg_item_delete(reg_item_p);
655 
656  if(m_status.nof_connections == 0)
657  {
658  m_sm_handler(PTU_SM_SIGNAL_ALL_DEVICES_DISCONNECTED);
659  }
660  }
661 
662  }
663 }
void ptu_reg_item_delete(ptu_reg_item_t *item)
Delete item that has previoulsy been added to registry.
Definition: ptu_registry.c:220
Registry item.
Definition: ptu_registry.h:47
static void m_connect(ble_gap_addr_t const *p_addr)
Definition: ptu_conn_man.c:90
uint8_t reconnect
Definition: ptu_registry.h:70
#define PTU_RECONNECT_TIMEOUT_MS
Definition: ptu_config.h:44
ble_gap_addr_t address
Definition: ptu_registry.h:51
#define APP_TIMER_PRESCALER
Definition: pru.h:33
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
static ptu_sm_handler_t m_sm_handler
State machine event handler.
Definition: ptu_sensors.c:55
uint32_t ptu_sensors_data_get(const ptu_sensor_data_t **sensors_data)
Get the latest data read from the PTU sensors.
Definition: ptu_sensors.c:404
app_timer_id_t timer_id
Definition: ptu_registry.h:56
uint8_t pending_dyn_read
Definition: ptu_registry.h:64
static ptu_cm_status_t m_status
Definition: ptu_conn_man.c:64
ptu_reg_item_state_t state
Definition: ptu_registry.h:49
Definition of PTU sensor data.
Definition: ptu_hw_config.h:99
static void m_pairing_start ( uint16_t  conn_handle)
static

Function starting pairing sequence.

Parameters
conn_handleconnection handle.

Definition at line 669 of file ptu_conn_man.c.

670 {
671  uint32_t err_code;
672 
673  ble_gap_sec_params_t sec_params;
674  memset(&sec_params, 0, sizeof(ble_gap_sec_params_t));
675  sec_params.io_caps = BLE_GAP_IO_CAPS_NONE;
676  sec_params.min_key_size = PTU_SEC_MIN_KEYSIZE;
677  sec_params.max_key_size = PTU_SEC_MAX_KEYSIZE;
678 
679  err_code = sd_ble_gap_authenticate(conn_handle, &sec_params);
680  APP_ERROR_CHECK(err_code);
681 }
#define PTU_SEC_MAX_KEYSIZE
Definition: ptu_config.h:40
#define PTU_SEC_MIN_KEYSIZE
Definition: ptu_config.h:39
static void m_on_evt_rw_response ( ble_evt_t *  p_ble_evt)
static

Function to be called on BLE_GATTC_EVT_READ_RSP and BLE_GATTC_EVT_WRITE_RSP events.

Parameters
p_ble_evtevent data forwarded from softdevice.

Definition at line 687 of file ptu_conn_man.c.

688 {
689  if(p_ble_evt->evt.gattc_evt.gatt_status == BLE_GATT_STATUS_ATTERR_INSUF_AUTHENTICATION)
690  {
691  m_pairing_start(p_ble_evt->evt.gattc_evt.conn_handle);
692  }
693 }
static void m_pairing_start(uint16_t conn_handle)
Definition: ptu_conn_man.c:669
static void m_on_evt_seq_request ( ble_evt_t *  p_ble_evt)
static

Function to be called on BLE_GAP_EVT_SEC_REQUEST event.

Parameters
p_ble_evtevent data forwarded from softdevice.

Definition at line 699 of file ptu_conn_man.c.

700 {
701  m_pairing_start(p_ble_evt->evt.gattc_evt.conn_handle);
702 }
static void m_pairing_start(uint16_t conn_handle)
Definition: ptu_conn_man.c:669
static void m_on_evt_sec_params_request ( ble_evt_t *  p_ble_evt)
static

Function to be called on BLE_GAP_EVT_SEC_PARAMS_REQUEST event.

Parameters
p_ble_evtevent data forwarded from softdevice.

Definition at line 708 of file ptu_conn_man.c.

709 {
710  uint32_t err_code;
711  err_code = sd_ble_gap_sec_params_reply(p_ble_evt->evt.gap_evt.conn_handle, BLE_GAP_SEC_STATUS_SUCCESS, NULL, NULL);
712  APP_ERROR_CHECK(err_code);
713 }
static void m_on_evt_conn_sec_update ( ble_evt_t *  p_ble_evt)
static

Function to be called on BLE_GAP_EVT_CONN_SEC_UPDATE event.

Parameters
p_ble_evtevent data forwarded from softdevice.

Definition at line 719 of file ptu_conn_man.c.

720 {
721  uint32_t err_code;
722  ptu_reg_item_t * reg_item_p = ptu_reg_item_get_from_conn_handle(p_ble_evt->evt.gap_evt.conn_handle);
723 
724  if(reg_item_p != NULL)
725  {
726  // If we were in the middle of registration process -> continue registration.
727  switch(reg_item_p->state)
728  {
730  err_code = ble_wpts_c_enable_pru_alert_notification(&(reg_item_p -> ble_wpts_c));
732  break;
733 
735  err_code = ble_wpts_c_read_pru_static(&(reg_item_p -> ble_wpts_c));
737  break;
738 
740  err_code = ble_wpts_c_write_ptu_static(&(reg_item_p -> ble_wpts_c), &m_ptu_static);
742  break;
743 
745  err_code = ble_wpts_c_read_pru_dynamic(&(reg_item_p -> ble_wpts_c));
747  break;
748 
750  // ptu_power_ctl.c module will send control packet complpeting registration.
751  break;
752 
753  default:
754  break;
755  }
756 
757  // Security mode 1 level 2 is expected when IO capabilities = BLE_GAP_IO_CAPS_NONE
758  ASSERT(p_ble_evt->evt.gap_evt.params.conn_sec_update.conn_sec.sec_mode.sm == 1);
759  ASSERT(p_ble_evt->evt.gap_evt.params.conn_sec_update.conn_sec.sec_mode.lv == 2);
760 
761  reg_item_p->link_encrypted = true;
762  }
763 }
Registry item.
Definition: ptu_registry.h:47
uint8_t link_encrypted
Definition: ptu_registry.h:68
uint32_t ble_wpts_c_enable_pru_alert_notification(ble_wpts_c_t *p_wpts_c)
Enable alert notifications to be sent from PRU.
Definition: ble_wpts_c.c:465
uint32_t ble_wpts_c_read_pru_static(ble_wpts_c_t *p_wpts_c)
Read the PRU Static Parameter characterisic value from the PRU server.
Definition: ble_wpts_c.c:403
uint32_t ble_wpts_c_write_ptu_static(ble_wpts_c_t *p_wpts_c, ptu_static_t *p_wpts_ptu_static)
Write the PTU Static characteristic to the PRU server.
Definition: ble_wpts_c.c:413
#define APP_ERROR_CHECK_ALLOW_DISCONNECT(ERR_CODE)
Macro equivalent to APP_ERROR_CHECK, except that error codes returned if connection is lost will be a...
Definition: wpt.h:228
uint32_t ble_wpts_c_read_pru_dynamic(ble_wpts_c_t *p_wpts_c)
Read the PRU Dynamic characterisic value from the PRU server.
Definition: ble_wpts_c.c:408
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
ptu_reg_item_state_t state
Definition: ptu_registry.h:49
static ptu_static_t m_ptu_static
Definition: ptu_conn_man.c:65

Variable Documentation

ptu_cm_status_t m_status
static

Connection manager status variables

Definition at line 64 of file ptu_conn_man.c.

ptu_static_t m_ptu_static
static
Initial value:
= {
.ptu_power = BLE_WPTS_PTU_POWER,
.ptu_max_source_impedance = BLE_WPTS_PTU_STATIC_MAX_SOURCE_IMPEDANCE_VAL,
.ptu_max_load_resistance = BLE_WPTS_PTU_STATIC_MAX_LOAD_RESISTANCE_VAL,
.ptu_class = PTU_CLASS,
.hardware_rev = PTU_HW_REVISION,
.firmware_rev = PTU_FW_REVISION,
.protocol_rev = PTU_PROTOCOL_REVISION,
.devices_supported_bitval = BLE_WPTS_PTU_STATIC_NUM_DEVICES_BITVAL}
#define BLE_WPTS_PTU_STATIC_MAX_LOAD_RESISTANCE_VAL
Definition: ptu_config.h:219
#define BLE_WPTS_PTU_STATIC_NUM_DEVICES_BITVAL
Definition: ptu_config.h:256
#define PTU_FW_REVISION
Definition: ptu_config.h:33
#define PTU_PROTOCOL_REVISION
Definition: ptu_config.h:34
#define PTU_CLASS
Definition: ptu_hw_config.h:30
#define PTU_HW_REVISION
Definition: ptu_config.h:32
#define BLE_WPTS_PTU_STATIC_MAX_SOURCE_IMPEDANCE_VAL
Definition: ptu_config.h:217
#define BLE_WPTS_PTU_POWER
Definition: ptu_config.h:221
#define BLE_WPTS_PTU_STATIC_MAX_SOURCE_IMPEDANCE_VALID
Definition: ptu_config.h:218
#define BLE_WPTS_PTU_STATIC_MAX_LOAD_RESISTANCE_VALID
Definition: ptu_config.h:220

PTU Static parameter.

Definition at line 65 of file ptu_conn_man.c.

const ble_gap_conn_params_t m_conn_params
static
Initial value:
= {
.min_conn_interval = PTU_MIN_CONN_INTERVAL,
.max_conn_interval = PTU_MAX_CONN_INTERVAL,
.slave_latency = PTU_SLAVE_LATENCY,
.conn_sup_timeout = PTU_CONN_SUP_TIMEOUT}
#define PTU_MAX_CONN_INTERVAL
Definition: ptu_config.h:127
#define PTU_MIN_CONN_INTERVAL
Definition: ptu_config.h:126
#define PTU_SLAVE_LATENCY
Definition: ptu_config.h:128
#define PTU_CONN_SUP_TIMEOUT
Definition: ptu_config.h:129

PTU Connection parameters.

Definition at line 79 of file ptu_conn_man.c.