Nordic Semiconductor nRF5 AirFuel SDK  version 2.2.0
ble_wpts_c.c File Reference
#include <stdint.h>
#include <string.h>
#include "ble_wpts_c.h"
#include "ble_srv_common.h"

Go to the source code of this file.

Functions

API implementation
void ble_wpts_c_on_ble_evt (ble_wpts_c_t *p_wpts_c, ble_evt_t *p_ble_evt)
 WPT Service Client BLE stack event handler. More...
 
uint32_t ble_wpts_c_init (const ble_wpts_c_init_t *p_wpts_c_init)
 Initialize the WPT Service Client. More...
 
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. More...
 
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. More...
 
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. More...
 
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. More...
 
uint32_t ble_wpts_c_enable_pru_alert_notification (ble_wpts_c_t *p_wpts_c)
 Enable alert notifications to be sent from PRU. More...
 
uint32_t ble_wpts_service_data_read (ble_gap_evt_adv_report_t *p_adv_report, ble_wpts_service_data_t *p_service_data)
 
void ble_wpts_char_handles_update (uint16_t prim_service_handle, ble_wpts_c_t *wpts_c)
 

Static funcitons and variables.

static ble_wpts_c_evt_handler_t m_wpts_c_evt_handler
 
static uint16_t m_pending_write_cmd_char_handle
 
static uint16_t m_pending_write_conn_handle
 
static void proc_pru_dynamic_read_rsp (ble_wpts_c_t *p_wpts_c, ble_evt_t *p_ble_evt)
 Process a PRU Dynamic Parameter read response. More...
 
static void proc_pru_static_read_rsp (ble_wpts_c_t *p_wpts_c, ble_evt_t *p_ble_evt)
 Process a PRU Static Parameter read response. More...
 
static void on_read_rsp (ble_wpts_c_t *p_wpts_c, ble_evt_t *p_ble_evt)
 Handle read response events. More...
 
static void on_write_rsp (ble_wpts_c_t *p_wpts_c, ble_evt_t *p_ble_evt)
 Handle write response events. More...
 
static void on_tx_complete (ble_wpts_c_t *p_wpts_c, ble_evt_t *p_ble_evt)
 Handle TX complete event. More...
 
static void on_hvx (ble_wpts_c_t *p_wpts_c, ble_evt_t *p_ble_evt)
 Handle value indication/notification events. More...
 

Function Documentation

static void proc_pru_dynamic_read_rsp ( ble_wpts_c_t p_wpts_c,
ble_evt_t *  p_ble_evt 
)
static

Process a PRU Dynamic Parameter read response.

Parameters
[in]p_wpts_cPRU Service Client structure.
[in]p_ble_evtRead response event received from the BLE stack.

Definition at line 37 of file ble_wpts_c.c.

38 {
39  ble_wpts_c_evt_t evt;
40  uint8_t * p_data;
41  uint16_t len;
42 
43  p_data = p_ble_evt->evt.gattc_evt.params.read_rsp.data;
44  len = p_ble_evt->evt.gattc_evt.params.read_rsp.len;
45  evt.gatt_status = p_ble_evt->evt.gattc_evt.gatt_status;
46  memset(&evt.data.pru_dynamic, 0, sizeof(pru_dynamic_t));
47 
48  if(evt.gatt_status == BLE_GATT_STATUS_SUCCESS)
49  {
50  if (len == BLE_WPTS_PRU_DYNAMIC_LEN)
51  {
53  evt.data.pru_dynamic.vrect = uint16_decode(&p_data[BLE_WPTS_PRU_DYNAMIC_VRECT_POS]);
54  evt.data.pru_dynamic.irect = uint16_decode(&p_data[BLE_WPTS_PRU_DYNAMIC_IRECT_POS]);
55  evt.data.pru_dynamic.vout = uint16_decode(&p_data[BLE_WPTS_PRU_DYNAMIC_VOUT_POS]);
56  evt.data.pru_dynamic.iout = uint16_decode(&p_data[BLE_WPTS_PRU_DYNAMIC_IOUT_POS]);
61 
65 
69 
73 
77 
81 
85 
89 
93 
95 
96  }
97  else
98  {
99  // Invalid length, report error to application
100  evt.gatt_status = BLE_GATT_STATUS_ATTERR_CPS_OUT_OF_RANGE;
101  }
102  }
104  m_wpts_c_evt_handler(p_wpts_c, &evt);
105 }
uint16_t irect
Definition: wpt.h:168
#define BLE_WPTS_PRU_DYNAMIC_ALERT_ADJ_POWER_RESP_BITPOS
#define BLE_WPTS_PRU_DYNAMIC_TEMPERATURE_POS
pru_alert_bits_t alerts
Definition: wpt.h:175
#define BLE_WPTS_PRU_DYNAMIC_OPTIONAL_FIELDS_POS
PRU dynamic characteristic byte positions.
#define BLE_WPTS_PRU_ALERT_OVER_CURRENT_BITMSK
union ble_wpts_c_evt_t::@1 data
uint8_t wired_charge_detect
Definition: wpt.h:139
uint16_t vout
Definition: wpt.h:169
uint8_t adjust_power_response
Definition: wpt.h:177
#define BLE_WPTS_PRU_DYNAMIC_LEN
uint16_t gatt_status
Definition: ble_wpts_c.h:47
#define BLE_WPTS_PRU_ALERT_WIRED_CHARGER_DETECT_BITPOS
uint16_t vrect
Definition: wpt.h:167
#define BLE_WPTS_PRU_DYNAMIC_VRECT_HIGH_DYN_POS
uint16_t vrect_set_dyn
Definition: wpt.h:173
#define BLE_WPTS_PRU_DYNAMIC_IRECT_POS
int16_t temperature
Definition: wpt.h:171
uint16_t iout
Definition: wpt.h:170
#define BLE_WPTS_PRU_DYNAMIC_PTU_TESTER_COMMAND_POS
#define BLE_WPTS_PRU_ALERT_CHARGE_COMPLETE_BITMSK
uint16_t vrect_high_dyn
Definition: wpt.h:174
#define BLE_WPTS_PRU_DYNAMIC_VRECT_SET_DYN_POS
ptu_tester_command_t tester_command
Definition: wpt.h:180
#define BLE_WPTS_PRU_DYNAMIC_ALERT_CHARGE_PORT_BITPOS
PRU dynamic characteristic Alert bitfield. Remaining bitifields is the same as PRU Alert characterist...
uint16_t vrect_min_dyn
Definition: wpt.h:172
#define BLE_WPTS_PRU_ALERT_OVER_VOLTAGE_BITPOS
PRU Alert characteristic value bitfield.
#define BLE_WPTS_PRU_DYNAMIC_ALERT_CHARGE_PORT_BITMSK
#define BLE_WPTS_PRU_DYNAMIC_PRU_ALERT_POS
uint8_t pru_charge_port
Definition: wpt.h:176
ble_wpts_c_evt_type_t type
Definition: ble_wpts_c.h:46
#define BLE_WPTS_PRU_DYNAMIC_VOUT_POS
uint8_t pru_over_voltage
Definition: wpt.h:134
#define BLE_WPTS_PRU_ALERT_OVER_VOLTAGE_BITMSK
#define BLE_WPTS_PRU_ALERT_SELF_PROTECTION_BITPOS
uint8_t pru_over_current
Definition: wpt.h:135
uint8_t pru_over_temperature
Definition: wpt.h:136
WPT Service Client event.
Definition: ble_wpts_c.h:44
#define BLE_WPTS_PRU_ALERT_OVER_TEMPERATURE_BITMSK
#define BLE_WPTS_PRU_DYNAMIC_IOUT_POS
#define BLE_WPTS_PRU_ALERT_OVER_CURRENT_BITPOS
uint8_t optional_fields
Definition: wpt.h:166
#define BLE_WPTS_PRU_DYNAMIC_ALERT_ADJ_POWER_RESP_BITMSK
pru_dynamic_t pru_dynamic
Definition: ble_wpts_c.h:51
#define BLE_WPTS_PRU_ALERT_SELF_PROTECTION_BITMSK
static ble_wpts_c_evt_handler_t m_wpts_c_evt_handler
Definition: ble_wpts_c.c:26
#define BLE_WPTS_PRU_DYNAMIC_VRECT_MIN_DYN_POS
uint8_t pru_self_protection
Definition: wpt.h:137
#define BLE_WPTS_PRU_ALERT_WIRED_CHARGER_DETECT_BITMSK
ptu_tester_command_t
PTU Tester commands.
Definition: wpt.h:155
#define BLE_WPTS_PRU_ALERT_OVER_TEMPERATURE_BITPOS
#define BLE_WPTS_BITFIELD_READ(bitfield, msk, pos)
Read bitfield.
uint8_t charge_complete
Definition: wpt.h:138
PRU Dynamic Parameter structure.
Definition: wpt.h:164
#define BLE_WPTS_PRU_DYNAMIC_VRECT_POS
#define BLE_WPTS_PRU_ALERT_CHARGE_COMPLETE_BITPOS
static void proc_pru_static_read_rsp ( ble_wpts_c_t p_wpts_c,
ble_evt_t *  p_ble_evt 
)
static

Process a PRU Static Parameter read response.

Parameters
[in]p_wpts_cPRU Service Client structure.
[in]p_ble_evtRead response event received from the BLE stack.

Definition at line 114 of file ble_wpts_c.c.

115 {
116  ble_wpts_c_evt_t evt;
117  uint8_t * p_data;
118  uint16_t len;
119 
120  p_data = p_ble_evt->evt.gattc_evt.params.read_rsp.data;
121  len = p_ble_evt->evt.gattc_evt.params.read_rsp.len;
122  evt.gatt_status = p_ble_evt->evt.gattc_evt.gatt_status;
123 
124  if (evt.gatt_status == BLE_GATT_STATUS_SUCCESS)
125  {
126  if (len == BLE_WPTS_PRU_STATIC_LEN )
127  {
135  evt.data.pru_static.vrect_set = uint16_decode(&p_data[BLE_WPTS_PRU_STATIC_VRECT_SET_POS]);
136  evt.data.pru_static.delta_r1 = uint16_decode(&p_data[BLE_WPTS_PRU_STATIC_DELTA_R1_POS]);
137 
141 
145 
149 
153 
154 
158 
162 
166  }
167  else
168  {
169  // Invalid length, report error to application
170  evt.gatt_status = BLE_GATT_STATUS_ATTERR_CPS_OUT_OF_RANGE;
171  }
173  m_wpts_c_evt_handler(p_wpts_c, &evt);
174  }
175 }
uint8_t info_ccp_connected_mode
Definition: wpt.h:105
uint8_t info_adj_power_capability
Definition: wpt.h:104
#define BLE_WPTS_PRU_INFO_ADJUST_POWER_BITPOS
union ble_wpts_c_evt_t::@1 data
#define BLE_WPTS_PRU_STATIC_OPTION_DELTA_R1_BITPOS
PRU Static characteristic Option field.
#define BLE_WPTS_PRU_INFO_CHARGE_COMPLETE_CONN_MODE_BITMSK
#define BLE_WPTS_PRU_INFO_PTU_TEST_MODE_BITPOS
uint8_t pru_category
Definition: wpt.h:92
#define BLE_WPTS_PRU_INFO_CHARGE_COMPLETE_CONN_MODE_BITPOS
uint16_t gatt_status
Definition: ble_wpts_c.h:47
#define BLE_WPTS_PRU_INFO_BLE_BITMSK
uint8_t protocol_rev
Definition: wpt.h:91
uint8_t info_pc_alg_pref
Definition: wpt.h:103
#define BLE_WPTS_PTU_STATIC_OPTIONAL_FIELDS_POS
PTU Static Parameter characteristic byte positions.
uint8_t fw_revision
Definition: wpt.h:94
#define BLE_WPTS_PRU_STATIC_VRECT_MIN_STATIC_POS
#define BLE_WPTS_PRU_STATIC_HW_REV_POS
#define BLE_WPTS_PRU_STATIC_OPTION_DELTA_R1_BITMSK
#define BLE_WPTS_PRU_STATIC_VRECT_SET_POS
uint8_t info_sep_btle_radio
Definition: wpt.h:102
uint8_t option_fields_r1_valid
Definition: wpt.h:100
uint16_t vrect_high_static
Definition: wpt.h:97
#define BLE_WPTS_PRU_STATIC_FW_REV_POS
pru_static_t pru_static
Definition: ble_wpts_c.h:50
uint8_t hw_revision
Definition: wpt.h:93
ble_wpts_c_evt_type_t type
Definition: ble_wpts_c.h:46
#define BLE_WPTS_PRU_STATIC_LEN
#define BLE_WPTS_PRU_STATIC_PROTOCOL_REV_POS
WPT Service Client event.
Definition: ble_wpts_c.h:44
#define BLE_WPTS_PRU_STATIC_VRECT_HIGH_STATIC_POS
uint16_t delta_r1
Definition: wpt.h:99
#define BLE_WPTS_PRU_INFO_POWER_CONTROL_PREF_BITMSK
#define BLE_WPTS_PRU_STATIC_PRU_INFORMATION_POS
#define BLE_WPTS_PRU_STATIC_PRECT_MAX_POS
uint8_t info_nfc_receiver
Definition: wpt.h:101
uint16_t vrect_min_static
Definition: wpt.h:96
#define BLE_WPTS_PRU_INFO_POWER_CONTROL_PREF_BITPOS
uint16_t vrect_set
Definition: wpt.h:98
#define BLE_WPTS_PRU_INFO_ADJUST_POWER_BITMSK
static ble_wpts_c_evt_handler_t m_wpts_c_evt_handler
Definition: ble_wpts_c.c:26
uint8_t info_ptu_test_mode
Definition: wpt.h:106
#define BLE_WPTS_PRU_INFO_NFC_BITMSK
#define BLE_WPTS_PRU_INFO_NFC_BITPOS
PRU Static characteristic PRU Information field.
#define BLE_WPTS_BITFIELD_READ(bitfield, msk, pos)
Read bitfield.
#define BLE_WPTS_PRU_INFO_PTU_TEST_MODE_BITMSK
#define BLE_WPTS_PRU_STATIC_DELTA_R1_POS
#define BLE_WPTS_PRU_INFO_BLE_BITPOS
uint8_t prect_max
Definition: wpt.h:95
#define BLE_WPTS_PRU_STATIC_PRU_CATEGORY_POS
static void on_read_rsp ( ble_wpts_c_t p_wpts_c,
ble_evt_t *  p_ble_evt 
)
static

Handle read response events.

Parameters
[in]p_wpts_cPRU Service Client structure.
[in]p_ble_evtRead response event received from the BLE stack.

Definition at line 184 of file ble_wpts_c.c.

185 {
186  if(p_ble_evt->evt.gattc_evt.gatt_status == BLE_GATT_STATUS_SUCCESS)
187  {
188  if (p_ble_evt->evt.gattc_evt.params.read_rsp.handle == p_wpts_c->prudp_handle)
189  {
190  proc_pru_dynamic_read_rsp(p_wpts_c, p_ble_evt);
191  }
192  else if (p_ble_evt->evt.gattc_evt.params.read_rsp.handle == p_wpts_c->prusp_handle)
193  {
194  proc_pru_static_read_rsp(p_wpts_c, p_ble_evt);
195  }
196  }
197 }
static void proc_pru_static_read_rsp(ble_wpts_c_t *p_wpts_c, ble_evt_t *p_ble_evt)
Process a PRU Static Parameter read response.
Definition: ble_wpts_c.c:114
uint16_t prusp_handle
Definition: ble_wpts_c.h:64
uint16_t prudp_handle
Definition: ble_wpts_c.h:65
static void proc_pru_dynamic_read_rsp(ble_wpts_c_t *p_wpts_c, ble_evt_t *p_ble_evt)
Process a PRU Dynamic Parameter read response.
Definition: ble_wpts_c.c:37
static void on_write_rsp ( ble_wpts_c_t p_wpts_c,
ble_evt_t *  p_ble_evt 
)
static

Handle write response events.

Parameters
[in]p_wpts_cPRU Service Client structure.
[in]p_ble_evtWrite response event received from the BLE stack.

Definition at line 204 of file ble_wpts_c.c.

205 {
206  ble_wpts_c_evt_t evt;
207  evt.gatt_status = p_ble_evt->evt.gattc_evt.gatt_status;
208 
209  if(p_ble_evt->evt.gattc_evt.gatt_status == BLE_GATT_STATUS_SUCCESS)
210  {
211  if (p_ble_evt->evt.gattc_evt.params.write_rsp.handle == p_wpts_c->prua_cccd_handle)
212  {
214  m_wpts_c_evt_handler(p_wpts_c, &evt);
215  }
216  }
217 }
uint16_t gatt_status
Definition: ble_wpts_c.h:47
uint16_t prua_cccd_handle
Definition: ble_wpts_c.h:63
ble_wpts_c_evt_type_t type
Definition: ble_wpts_c.h:46
WPT Service Client event.
Definition: ble_wpts_c.h:44
static ble_wpts_c_evt_handler_t m_wpts_c_evt_handler
Definition: ble_wpts_c.c:26
static void on_tx_complete ( ble_wpts_c_t p_wpts_c,
ble_evt_t *  p_ble_evt 
)
static

Handle TX complete event.

Parameters
[in]p_wpts_cPRU Service Client structure.
[in]p_ble_evtTX complete event received from the BLE stack.

Definition at line 224 of file ble_wpts_c.c.

225 {
226  ble_wpts_c_evt_t evt;
227 
228  // Here, we always assume success, even though we didn't get a response
229  evt.gatt_status = BLE_GATT_STATUS_SUCCESS;
230 
231  // Here we use a local variable as char handle is not included in the
232  // TX_COMPLETE event
234  {
236  m_wpts_c_evt_handler(p_wpts_c, &evt);
237  }
239  {
241  m_wpts_c_evt_handler(p_wpts_c, &evt);
242  }
243 }
uint16_t conn_handle
Definition: ble_wpts_c.h:66
uint16_t gatt_status
Definition: ble_wpts_c.h:47
uint16_t pruc_handle
Definition: ble_wpts_c.h:60
ble_wpts_c_evt_type_t type
Definition: ble_wpts_c.h:46
WPT Service Client event.
Definition: ble_wpts_c.h:44
uint16_t ptusp_handle
Definition: ble_wpts_c.h:61
static uint16_t m_pending_write_conn_handle
Definition: ble_wpts_c.c:28
static ble_wpts_c_evt_handler_t m_wpts_c_evt_handler
Definition: ble_wpts_c.c:26
static uint16_t m_pending_write_cmd_char_handle
Definition: ble_wpts_c.c:27
static void on_hvx ( ble_wpts_c_t p_wpts_c,
ble_evt_t *  p_ble_evt 
)
static

Handle value indication/notification events.

Parameters
[in]p_wpts_cPRU Service Client structure.
[in]p_ble_evtIndication/notification received from the BLE stack.

Definition at line 250 of file ble_wpts_c.c.

251 {
252  ble_wpts_c_evt_t evt;
253 
254 
255  evt.gatt_status = p_ble_evt->evt.gattc_evt.gatt_status;
256 
257  if ((p_ble_evt->evt.gattc_evt.params.hvx.handle == p_wpts_c->prua_handle) &&
258  ( (p_ble_evt->evt.gattc_evt.params.hvx.type == BLE_GATT_HVX_NOTIFICATION) ||
259  (p_ble_evt->evt.gattc_evt.params.hvx.type == BLE_GATT_HVX_INDICATION))
260  )
261  {
262 
263  uint8_t data = p_ble_evt->evt.gattc_evt.params.hvx.data[0];
264 
266 
267  memset(&evt.data.pru_alert,0,sizeof(pru_alert_t));
268 
276 
278  {
279  if(p_ble_evt->evt.gattc_evt.params.hvx.len == (BLE_GAP_ADDR_LEN + 1))
280  {
281  //lint -save -e420
282  memcpy(evt.data.pru_alert.device_address,&p_ble_evt->evt.gattc_evt.params.hvx.data[1],BLE_GAP_ADDR_LEN);
283  //lint -restore
284  }
285  }
286 
287  if(p_ble_evt->evt.gattc_evt.params.hvx.type == BLE_GATT_HVX_NOTIFICATION)
288  {
290  m_wpts_c_evt_handler(p_wpts_c, &evt);
291  }
292  else if (p_ble_evt->evt.gattc_evt.params.hvx.type == BLE_GATT_HVX_INDICATION)
293  {
295 
296  if(sd_ble_gattc_hv_confirm(p_wpts_c->conn_handle, p_wpts_c->prua_handle) == NRF_SUCCESS)
297  {
298  m_wpts_c_evt_handler(p_wpts_c, &evt);
299  }
300  }
301  }
302 }
#define BLE_WPTS_PRU_ALERT_OVER_CURRENT_BITMSK
union ble_wpts_c_evt_t::@1 data
uint16_t conn_handle
Definition: ble_wpts_c.h:66
uint8_t wired_charge_detect
Definition: wpt.h:139
uint8_t mode_transition
Definition: wpt.h:148
#define BLE_WPTS_PRU_ALERT_MODE_NO_MODE_TRANSITION_VAL
PRU Alert Mode transition.
uint16_t gatt_status
Definition: ble_wpts_c.h:47
pru_alert_type_t type
Definition: wpt.h:150
#define BLE_WPTS_PRU_ALERT_WIRED_CHARGER_DETECT_BITPOS
pru_alert_t pru_alert
Definition: ble_wpts_c.h:52
PRU Alert Parameter structure.
Definition: wpt.h:145
#define BLE_WPTS_PRU_ALERT_CHARGE_COMPLETE_BITMSK
#define BLE_WPTS_PRU_ALERT_MODE_TRANSITION_BITPOS
#define BLE_WPTS_PRU_ALERT_MODE_TRANSITION_BITMSK
#define BLE_WPTS_PRU_ALERT_OVER_VOLTAGE_BITPOS
PRU Alert characteristic value bitfield.
ble_wpts_c_evt_type_t type
Definition: ble_wpts_c.h:46
uint8_t pru_over_voltage
Definition: wpt.h:134
#define BLE_WPTS_PRU_ALERT_OVER_VOLTAGE_BITMSK
#define BLE_WPTS_PRU_ALERT_SELF_PROTECTION_BITPOS
uint16_t prua_handle
Definition: ble_wpts_c.h:62
uint8_t pru_over_current
Definition: wpt.h:135
uint8_t pru_over_temperature
Definition: wpt.h:136
WPT Service Client event.
Definition: ble_wpts_c.h:44
#define BLE_WPTS_PRU_ALERT_OVER_TEMPERATURE_BITMSK
#define BLE_WPTS_PRU_ALERT_OVER_CURRENT_BITPOS
uint8_t device_address[BLE_GAP_ADDR_LEN]
Definition: wpt.h:149
#define BLE_WPTS_PRU_ALERT_SELF_PROTECTION_BITMSK
static ble_wpts_c_evt_handler_t m_wpts_c_evt_handler
Definition: ble_wpts_c.c:26
uint8_t pru_self_protection
Definition: wpt.h:137
#define BLE_WPTS_PRU_ALERT_WIRED_CHARGER_DETECT_BITMSK
#define BLE_WPTS_PRU_ALERT_OVER_TEMPERATURE_BITPOS
#define BLE_WPTS_BITFIELD_READ(bitfield, msk, pos)
Read bitfield.
pru_alert_bits_t alerts
Definition: wpt.h:147
uint8_t charge_complete
Definition: wpt.h:138
#define BLE_WPTS_PRU_ALERT_CHARGE_COMPLETE_BITPOS

Variable Documentation

ble_wpts_c_evt_handler_t m_wpts_c_evt_handler
static

Event handler

Definition at line 26 of file ble_wpts_c.c.

uint16_t m_pending_write_cmd_char_handle
static

Write WO response temp variable

Definition at line 27 of file ble_wpts_c.c.

uint16_t m_pending_write_conn_handle
static

Write WO response temp conn_handle

Definition at line 28 of file ble_wpts_c.c.