Nordic Semiconductor nRF5 AirFuel SDK  version 2.2.0
PTU connection manager API

Functions

uint32_t ptu_cm_init (ptu_sm_handler_t sm_handler)
 Initialize connection manager. More...
 
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...
 
void ptu_cm_scan_enable (void)
 Enable scanning.
 
void ptu_cm_scan_disable (void)
 Disable scanning.
 
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_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.
 

Detailed Description

Function Documentation

uint32_t ptu_cm_init ( ptu_sm_handler_t  sm_handler)

Initialize connection manager.

Parameters
[in]sm_handlerState machine event handler
Returns
NRF_ERROR_INVALID_PARAM if sm_handler is NULL, otherwise NRF_SUCCESS.

Definition at line 861 of file ptu_conn_man.c.

862 {
863  int i;
864  uint32_t err_code;
865  ptu_reg_item_t *reg_item;
866 
867  if(sm_handler == NULL)
868  return NRF_ERROR_INVALID_PARAM;
869 
870  m_sm_handler = sm_handler;
871  memset(&m_status, 0, sizeof(ptu_cm_status_t));
872  m_status.scan_params.active = 1;
873 
874  // Instantiate registration and connection timers
875  for(i = 0; i < PTU_MAX_CONNECTIONS; i++)
876  {
877  err_code = ptu_reg_item_get_from_index(i, &reg_item);
878  APP_ERROR_CHECK(err_code);
879 
880  err_code = app_timer_create(&(reg_item->timer_id), APP_TIMER_MODE_SINGLE_SHOT, m_reg_timer_handle);
881  APP_ERROR_CHECK(err_code);
882  }
883 
884  // Initiate time set impedance shift detect timer
885  err_code = app_timer_create(&m_time_set_check_timer_id, APP_TIMER_MODE_REPEATED, m_ptu_time_set_check_timer_handler);
886  APP_ERROR_CHECK(err_code);
887 
888  err_code = app_timer_create(&m_mode_trans_timer_id, APP_TIMER_MODE_SINGLE_SHOT, m_mode_trans_timer_handle);
889  APP_ERROR_CHECK(err_code);
890 
891  return NRF_SUCCESS;
892 }
Registry item.
Definition: ptu_registry.h:47
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.
Definition: ptu_registry.c:126
static void m_ptu_time_set_check_timer_handler(void *p_context)
Timeout handler for the time set functionality.
Definition: ptu_conn_man.c:270
static ptu_sm_handler_t m_sm_handler
State machine event handler.
Definition: ptu_sensors.c:55
app_timer_id_t timer_id
Definition: ptu_registry.h:56
static ptu_cm_status_t m_status
Definition: ptu_conn_man.c:64
#define PTU_MAX_CONNECTIONS
Definition: ptu_config.h:29
void ptu_cm_remove_device ( ptu_reg_item_t reg_item_p)

Disconnect and/or unregister any device.

Parameters
[in]reg_item_preg_item.

Definition at line 836 of file ptu_conn_man.c.

837 {
838  uint32_t err_code;
839 
840  err_code = app_timer_stop(reg_item_p->timer_id);
841  APP_ERROR_CHECK(err_code);
842 
843  // If a connection exists for this item.
844  if((reg_item_p->state != REG_ITEM_STATE_UNUSED) &&
845  (reg_item_p->state != REG_ITEM_STATE_PRE_CONNECT) &&
846  (reg_item_p->state != REG_ITEM_STATE_WAITING_TO_CONNECT) &&
847  (reg_item_p->state != REG_ITEM_STATE_FULLY_ACCEPTED))
848  {
849  m_disconnect(reg_item_p->ble_wpts_c.conn_handle);
850  }
851 
852  ptu_reg_item_delete(reg_item_p);
853 }
void ptu_reg_item_delete(ptu_reg_item_t *item)
Delete item that has previoulsy been added to registry.
Definition: ptu_registry.c:220
uint16_t conn_handle
Definition: ble_wpts_c.h:66
ble_wpts_c_t ble_wpts_c
Definition: ptu_registry.h:52
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
ptu_reg_item_state_t state
Definition: ptu_registry.h:49
bool ptu_cm_mode_trans_in_progress ( void  )

Get mode transition status.

Return values
trueif mode transition in progress

Definition at line 855 of file ptu_conn_man.c.

856 {
857  return m_status.mode_trans_in_progess;
858 }
static ptu_cm_status_t m_status
Definition: ptu_conn_man.c:64
void ptu_cm_on_ble_evt ( ble_evt_t *  p_ble_evt)

BLE event handler.

Parameters
[in]p_ble_evtble event data.

Definition at line 894 of file ptu_conn_man.c.

895 {
896  ble_wpts_service_data_t curr_service_data;
897 
898  switch (p_ble_evt->header.evt_id)
899  {
900  case BLE_GAP_EVT_ADV_REPORT:
901  // If AD received (not scan response)
902  if( p_ble_evt->evt.gap_evt.params.adv_report.scan_rsp == 0)
903  {
904  // If WPT advertisement
905  if (ble_wpts_service_data_read(&p_ble_evt->evt.gap_evt.params.adv_report, &curr_service_data) == NRF_SUCCESS &&
906  curr_service_data.uuid16 == BLE_WPTS_UUID16)
907  m_on_wpt_adv_report(p_ble_evt, curr_service_data.adv_flags);
908  }
909  else // If scan response received
910  {
911  m_on_scan_response(p_ble_evt);
912  }
913  break;
914 
915  case BLE_GAP_EVT_CONNECTED:
916  m_on_evt_connected(p_ble_evt);
917  break;
918 
919  case BLE_GAP_EVT_TIMEOUT:
920  m_on_evt_timeout(p_ble_evt);
921  break;
922 
923  case BLE_GAP_EVT_DISCONNECTED:
924  m_on_evt_disconnected(p_ble_evt);
925  break;
926 
927  case BLE_GATTC_EVT_READ_RSP:
928  case BLE_GATTC_EVT_WRITE_RSP:
929  m_on_evt_rw_response(p_ble_evt);
930  break;
931 
932  case BLE_GAP_EVT_SEC_REQUEST:
933  m_on_evt_seq_request(p_ble_evt);
934  break;
935 
936  case BLE_GAP_EVT_SEC_PARAMS_REQUEST:
937  m_on_evt_sec_params_request(p_ble_evt);
938  break;
939 
940  case BLE_GAP_EVT_CONN_SEC_UPDATE:
941  m_on_evt_conn_sec_update(p_ble_evt);
942  break;
943 
944  default:
945  break;
946  }
947 
948  m_scan_maintain();
949 }
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
static void m_scan_maintain(void)
Definition: ptu_conn_man.c:314
#define BLE_WPTS_UUID16
WPT Service UUIDs.
static void m_on_scan_response(ble_evt_t *p_ble_evt)
Definition: ptu_conn_man.c:492
static void m_on_evt_rw_response(ble_evt_t *p_ble_evt)
Definition: ptu_conn_man.c:687
Struct holding contents of "Service data AD type".
static void m_on_evt_timeout(ble_evt_t *p_ble_evt)
Definition: ptu_conn_man.c:577
static void m_on_evt_seq_request(ble_evt_t *p_ble_evt)
Definition: ptu_conn_man.c:699
static void m_on_evt_sec_params_request(ble_evt_t *p_ble_evt)
Definition: ptu_conn_man.c:708
static void m_on_evt_disconnected(ble_evt_t *p_ble_evt)
Definition: ptu_conn_man.c:610
static void m_on_evt_connected(ble_evt_t *p_ble_evt)
Definition: ptu_conn_man.c:528
static void m_on_evt_conn_sec_update(ble_evt_t *p_ble_evt)
Definition: ptu_conn_man.c:719
static void m_on_wpt_adv_report(ble_evt_t *p_ble_evt, uint8_t adv_flags)
Definition: ptu_conn_man.c:447
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.

Parameters
[in]p_wpts_chandles.
[in]p_wpts_c_evtevent data.

Definition at line 952 of file ptu_conn_man.c.

953 {
955  uint32_t err_code;
956 
957  if (reg_item_p != NULL)
958  {
959  switch (p_wpts_c_evt->type)
960  {
963  err_code = ble_wpts_c_write_ptu_static(&(reg_item_p -> ble_wpts_c), &m_ptu_static);
965  // Make a local copy in registry of PRU static parameters
966  reg_item_p->prev_pru_static = p_wpts_c_evt->data.pru_static;
967  break;
968 
971  err_code = ble_wpts_c_read_pru_dynamic(&(reg_item_p -> ble_wpts_c));
973  reg_item_p->pending_dyn_read = true;
974  break;
975 
977  if(reg_item_p->state != REG_ITEM_STATE_REGISTERED)
978  {
979  const ptu_sensor_data_t * sensor_data;
980  err_code = ptu_sensors_data_get(&sensor_data);
981  APP_ERROR_CHECK(err_code);
982  m_status.time_set_p_tx_in_reference = sensor_data->p_tx_in;
983 
984  //ASSERT(reg_item_p != 0);
985  reg_item_p->state = REG_ITEM_STATE_PRU_CTL_SEND;
986 
987  // Info: Now, a control packet will be sent by the "power adjust"
988  // part of the code. So we can always rely on getting a
989  // BLE_WPTS_C_EVT_PRU_CONTROL_WRITE_RESP that will end registration.
990  }
991  break;
992 
994  if(reg_item_p->state != REG_ITEM_STATE_REGISTERED)
995  {
996  m_device_registration_complete(reg_item_p);
997  // Stop registration timer.
998  err_code = app_timer_stop(reg_item_p->timer_id);
999  APP_ERROR_CHECK(err_code);
1000 
1001  // If this PRU has "Time set" support, and we just enabled PRU Charge Output
1003  reg_item_p->prev_ctl.enable_pru_output == 1)
1004  {
1005  err_code = app_timer_start(m_time_set_check_timer_id, APP_TIMER_TICKS(PTU_TIME_SET_CHECK_INTERVAL_MS, APP_TIMER_PRESCALER), reg_item_p);
1006  APP_ERROR_CHECK(err_code);
1007  ptu_power_ctrl_set_disable_power_amplifier_adjustments(true); // Disable adjustments of I_TX_COIL during time set.
1008  }
1009 
1010  err_code = ble_wpts_c_enable_pru_alert_notification(&(reg_item_p -> ble_wpts_c));
1012 
1013  // We need to test permissions, so save the control packet data.
1014  if(reg_item_p->prev_ctl.permissions == CTL_PERMISSION_PERMITTED)
1015  {
1016  m_sm_handler(PTU_SM_SIGNAL_CHARGE_START);
1017  }
1018  }
1019  break;
1020 
1022  m_on_wpt_alert(p_wpts_c, p_wpts_c_evt);
1023  break;
1024 
1025  default:
1026  break;
1027  }
1028  }
1029  m_scan_maintain();
1030 }
pru_control_t prev_ctl
Definition: ptu_registry.h:55
Registry item.
Definition: ptu_registry.h:47
static void m_scan_maintain(void)
Definition: ptu_conn_man.c:314
union ble_wpts_c_evt_t::@1 data
uint16_t conn_handle
Definition: ble_wpts_c.h:66
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_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
uint8_t enable_pru_output
Definition: wpt.h:81
#define PTU_TIME_SET_CHECK_INTERVAL_MS
Definition: ptu_config.h:90
uint8_t adv_flags
Definition: ptu_registry.h:50
#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
void ptu_power_ctrl_set_disable_power_amplifier_adjustments(bool val)
Enable/Disable adjustments of power amplifier (I_TX_COIL).
pru_static_t pru_static
Definition: ble_wpts_c.h:50
ble_wpts_c_evt_type_t type
Definition: ble_wpts_c.h:46
ctl_perm_t permissions
Definition: wpt.h:84
pru_static_t prev_pru_static
Definition: ptu_registry.h:53
#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
#define BLE_WPTS_BITFIELD_READ(bitfield, msk, pos)
Read bitfield.
ptu_reg_item_state_t state
Definition: ptu_registry.h:49
static ptu_static_t m_ptu_static
Definition: ptu_conn_man.c:65
Definition of PTU sensor data.
Definition: ptu_hw_config.h:99
#define BLE_WPTS_ADV_FLAG_TIME_SET_SUPPORT_BITMSK
#define BLE_WPTS_ADV_FLAG_TIME_SET_SUPPORT_BITPOS