Nordic Semiconductor nRF5 AirFuel SDK  version 2.2.0
ble_wpts.c
Go to the documentation of this file.
1 /* Copyright (c) 2013 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 
15 #include <stdint.h>
16 #include <string.h>
17 
18 #include "ble_wpts.h"
19 #include "ble_wpts_common.h"
20 #include "nordic_common.h"
21 
22 // Likely access out of bonds
23 //lint -save -e415 // Out of bonds
24 //lint -save -e416
25 //lint -save -e661 // Creating out of bond point
26 //lint -save -e662 // Creating out of bond point
27 //lint -save -e485 // Duplicate initialization of object element
28 
29 /******************************************************************************/
32 /******************************************************************************/
33 
34 const ble_gatts_rw_authorize_reply_params_t m_auth_reply = \
35  {.type = BLE_GATTS_AUTHORIZE_TYPE_READ, \
36  .params.read.gatt_status = BLE_GATT_STATUS_SUCCESS, \
37  .params.read.update = 0};
45 static void on_connect(ble_wpts_t * p_wpt, ble_evt_t * p_ble_evt)
46 {
47  p_wpt->conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
48 }
49 
55 static void on_disconnect(ble_wpts_t * p_wpt, ble_evt_t * p_ble_evt)
56 {
57  UNUSED_PARAMETER(p_ble_evt);
58  p_wpt->conn_handle = BLE_CONN_HANDLE_INVALID;
59 }
60 
66 static void on_pru_control_write(ble_wpts_t * p_wpts, ble_gatts_evt_write_t * p_evt_write)
67 {
68  if (p_evt_write->len == BLE_WPTS_PRU_CONTROL_LEN)
69  {
70  ble_wpts_evt_t evt;
71 
72  // Decode/parse control data
84 
85  // Generate service event
87  p_wpts->evt_handler(p_wpts, &evt);
88  }
89 }
90 
96 static void on_ptu_static_parameter_write(ble_wpts_t * p_wpts, ble_gatts_evt_write_t * p_evt_write)
97 {
98  if (p_evt_write->len == BLE_WPTS_PTU_STATIC_LEN)
99  {
100  ble_wpts_evt_t evt;
101  uint8_t * p = p_evt_write->data;
102 
106 
107  evt.data.ptu_static.option_valid_max_res = BLE_WPTS_BITFIELD_READ(p[BLE_WPTS_PTU_STATIC_OPTIONAL_FIELDS_POS], \
110 
114 
118 
122 
128 
129 
131  p_wpts->evt_handler(p_wpts, &evt);
132  }
133 }
134 
140 static void on_pru_alert_cccd_write(ble_wpts_t * p_wpts, ble_gatts_evt_write_t * p_evt_write)
141 {
142  // Ensure that CCCD data length is at least 16 bit
143  //(Needed for ble_srv_is_notification_enabled())
144  if (p_evt_write->len >= 2)
145  {
146  // CCCD written, update notification state
147  if (p_wpts->evt_handler != NULL)
148  {
149  ble_wpts_evt_t evt;
150 
151  if (ble_srv_is_notification_enabled(p_evt_write->data))
152  {
154  }
155  else
156  {
158  }
159 
160  p_wpts->evt_handler(p_wpts, &evt);
161  }
162  }
163 }
164 
165 
171 static void on_write(ble_wpts_t * p_wpt, ble_evt_t * p_ble_evt)
172 {
173  ble_gatts_evt_write_t * p_evt_write = &p_ble_evt->evt.gatts_evt.params.write;
174 
175  if (p_evt_write->handle == p_wpt->pru_control_handles.value_handle)
176  {
177  on_pru_control_write(p_wpt, p_evt_write);
178  }
179  else if (p_evt_write->handle == p_wpt->ptu_static_handles.value_handle)
180  {
181  on_ptu_static_parameter_write(p_wpt, p_evt_write);
182  }
183  else if (p_evt_write->handle == p_wpt->pru_alert_handles.cccd_handle)
184  {
185  on_pru_alert_cccd_write(p_wpt, p_evt_write);
186  }
187 }
188 
191 /******************************************************************************/
194 /******************************************************************************/
195 
196 void ble_wpts_on_ble_evt(ble_wpts_t * p_wpt, ble_evt_t * p_ble_evt)
197 {
198  switch (p_ble_evt->header.evt_id)
199  {
200  case BLE_GAP_EVT_CONNECTED:
201  on_connect(p_wpt, p_ble_evt);
202  break;
203 
204  case BLE_GAP_EVT_DISCONNECTED:
205  on_disconnect(p_wpt, p_ble_evt);
206  break;
207 
208  case BLE_GATTS_EVT_WRITE:
209  on_write(p_wpt, p_ble_evt);
210  break;
211 
212  default:
213  break;
214  }
215 }
216 
217 
226 static void pru_static_encode(ble_wpts_t * p_wpt, const pru_static_t * p_pru_static,
227  uint8_t * p_encoded_buffer)
228 {
229  memset(p_encoded_buffer,0,BLE_WPTS_PRU_STATIC_LEN);
230  p_encoded_buffer[BLE_WPTS_PRU_STATIC_PROTOCOL_REV_POS] = p_pru_static->protocol_rev;
231  p_encoded_buffer[BLE_WPTS_PRU_STATIC_PRU_CATEGORY_POS] = p_pru_static->pru_category;
232  p_encoded_buffer[BLE_WPTS_PRU_STATIC_HW_REV_POS] = p_pru_static->hw_revision;
233  p_encoded_buffer[BLE_WPTS_PRU_STATIC_FW_REV_POS] = p_pru_static->fw_revision;
234  p_encoded_buffer[BLE_WPTS_PRU_STATIC_PRECT_MAX_POS] = p_pru_static->prect_max;
235  (void)uint16_encode(p_pru_static->vrect_min_static, &p_encoded_buffer[BLE_WPTS_PRU_STATIC_VRECT_MIN_STATIC_POS]);
236  (void)uint16_encode(p_pru_static->vrect_high_static, &p_encoded_buffer[BLE_WPTS_PRU_STATIC_VRECT_HIGH_STATIC_POS]);
237  (void)uint16_encode(p_pru_static->vrect_set, &p_encoded_buffer[BLE_WPTS_PRU_STATIC_VRECT_SET_POS]);
238  (void)uint16_encode(p_pru_static->delta_r1, &p_encoded_buffer[BLE_WPTS_PRU_STATIC_DELTA_R1_POS]);
239 
243  p_pru_static->option_fields_r1_valid);
244 
248  p_pru_static->info_nfc_receiver);
249 
250  BLE_WPTS_BITFIELD_WRITE(p_encoded_buffer[BLE_WPTS_PRU_STATIC_PRU_INFORMATION_POS], \
253  p_pru_static->info_sep_btle_radio);
254 
255  BLE_WPTS_BITFIELD_WRITE(p_encoded_buffer[BLE_WPTS_PRU_STATIC_PRU_INFORMATION_POS], \
258  p_pru_static->info_pc_alg_pref);
259 
260  BLE_WPTS_BITFIELD_WRITE(p_encoded_buffer[BLE_WPTS_PRU_STATIC_PRU_INFORMATION_POS], \
263  p_pru_static->info_adj_power_capability);
264 
265  BLE_WPTS_BITFIELD_WRITE(p_encoded_buffer[BLE_WPTS_PRU_STATIC_PRU_INFORMATION_POS], \
268  p_pru_static->info_ccp_connected_mode);
269 
270  BLE_WPTS_BITFIELD_WRITE(p_encoded_buffer[BLE_WPTS_PRU_STATIC_PRU_INFORMATION_POS], \
273  p_pru_static->info_ptu_test_mode);
274 }
275 
284 static void pru_dynamic_encode(ble_wpts_t * p_wpt, const pru_dynamic_t * p_pru_dynamic,
285  uint8_t * p_encoded_buffer)
286 {
287 
288  memset(p_encoded_buffer,0,BLE_WPTS_PRU_DYNAMIC_LEN);
289  p_encoded_buffer[BLE_WPTS_PRU_DYNAMIC_OPTIONAL_FIELDS_POS] = p_pru_dynamic->optional_fields;
290  (void)uint16_encode(p_pru_dynamic->vrect, &p_encoded_buffer[BLE_WPTS_PRU_DYNAMIC_VRECT_POS]);
291  (void)uint16_encode(p_pru_dynamic->irect, &p_encoded_buffer[BLE_WPTS_PRU_DYNAMIC_IRECT_POS]);
292 
293  if(BLE_WPTS_BITFIELD_READ(p_pru_dynamic->optional_fields, \
296  {
297  (void)uint16_encode(p_pru_dynamic->vout, &p_encoded_buffer[BLE_WPTS_PRU_DYNAMIC_VOUT_POS]);
298  }
299 
300  if(BLE_WPTS_BITFIELD_READ(p_pru_dynamic->optional_fields, \
303  {
304  (void)uint16_encode(p_pru_dynamic->iout, &p_encoded_buffer[BLE_WPTS_PRU_DYNAMIC_IOUT_POS]);
305  }
306 
307  if(BLE_WPTS_BITFIELD_READ(p_pru_dynamic->optional_fields, \
310  {
311  p_encoded_buffer[BLE_WPTS_PRU_DYNAMIC_TEMPERATURE_POS] = (uint8_t)(p_pru_dynamic->temperature + 40);
312  }
313 
314  if(BLE_WPTS_BITFIELD_READ(p_pru_dynamic->optional_fields, \
317  {
318  (void)uint16_encode(p_pru_dynamic->vrect_min_dyn, &p_encoded_buffer[BLE_WPTS_PRU_DYNAMIC_VRECT_MIN_DYN_POS]);
319  }
320 
321  if(BLE_WPTS_BITFIELD_READ(p_pru_dynamic->optional_fields, \
324  {
325  (void)uint16_encode(p_pru_dynamic->vrect_set_dyn, &p_encoded_buffer[BLE_WPTS_PRU_DYNAMIC_VRECT_SET_DYN_POS]);
326  }
327 
328  if(BLE_WPTS_BITFIELD_READ(p_pru_dynamic->optional_fields, \
331  {
332  (void)uint16_encode(p_pru_dynamic->vrect_high_dyn, &p_encoded_buffer[BLE_WPTS_PRU_DYNAMIC_VRECT_HIGH_DYN_POS]);
333  }
334 
335 
339  p_pru_dynamic->alerts.pru_over_voltage);
340 
341  BLE_WPTS_BITFIELD_WRITE(p_encoded_buffer[BLE_WPTS_PRU_DYNAMIC_PRU_ALERT_POS], \
344  p_pru_dynamic->alerts.pru_over_current);
345 
346  BLE_WPTS_BITFIELD_WRITE(p_encoded_buffer[BLE_WPTS_PRU_DYNAMIC_PRU_ALERT_POS], \
349  p_pru_dynamic->alerts.pru_over_temperature);
350 
351  BLE_WPTS_BITFIELD_WRITE(p_encoded_buffer[BLE_WPTS_PRU_DYNAMIC_PRU_ALERT_POS], \
354  p_pru_dynamic->alerts.pru_self_protection);
355 
356  BLE_WPTS_BITFIELD_WRITE(p_encoded_buffer[BLE_WPTS_PRU_DYNAMIC_PRU_ALERT_POS], \
359  p_pru_dynamic->alerts.charge_complete);
360 
361  BLE_WPTS_BITFIELD_WRITE(p_encoded_buffer[BLE_WPTS_PRU_DYNAMIC_PRU_ALERT_POS], \
364  p_pru_dynamic->alerts.wired_charge_detect);
365 
366  BLE_WPTS_BITFIELD_WRITE(p_encoded_buffer[BLE_WPTS_PRU_DYNAMIC_PRU_ALERT_POS], \
369  p_pru_dynamic->adjust_power_response);
370 
371  BLE_WPTS_BITFIELD_WRITE(p_encoded_buffer[BLE_WPTS_PRU_DYNAMIC_PRU_ALERT_POS], \
374  p_pru_dynamic->pru_charge_port);
375 
376  p_encoded_buffer[BLE_WPTS_PRU_DYNAMIC_PTU_TESTER_COMMAND_POS] = (uint8_t)(p_pru_dynamic->tester_command);
377 }
378 
386 static uint32_t pru_control_char_add(ble_wpts_t * p_wpt, const ble_wpts_init_t * p_wpts_init)
387 {
388  ble_gatts_char_md_t char_md;
389  ble_gatts_attr_t attr_char_value;
390  ble_uuid_t ble_uuid;
391  ble_gatts_attr_md_t attr_md;
392 
393  memset(&char_md, 0, sizeof(char_md));
394 
395  char_md.char_props.write = 1;
396  char_md.char_props.read = 1;
397  char_md.char_props.write_wo_resp = 1; // Required for "GATT write without response" procedure
398  char_md.p_char_user_desc = NULL;
399  char_md.p_char_pf = NULL;
400  char_md.p_user_desc_md = NULL;
401  char_md.p_cccd_md = NULL;
402  char_md.p_sccd_md = NULL;
403 
405 
406  memset(&attr_md, 0, sizeof(attr_md));
407 
408  attr_md.write_perm = p_wpts_init->wpts_attr_md.write_perm;
409  attr_md.read_perm = p_wpts_init->wpts_attr_md.read_perm;
410  attr_md.vloc = BLE_GATTS_VLOC_STACK;
411  attr_md.rd_auth = 0;
412  attr_md.wr_auth = 0;
413  attr_md.vlen = 0;
414 
415  memset(&attr_char_value, 0, sizeof(attr_char_value));
416 
417  attr_char_value.p_uuid = &ble_uuid;
418  attr_char_value.p_attr_md = &attr_md;
419  attr_char_value.init_len = 0;
420  attr_char_value.init_offs = 0;
421  attr_char_value.max_len = BLE_WPTS_PRU_CONTROL_LEN;
422  attr_char_value.p_value = NULL;
423 
424  return sd_ble_gatts_characteristic_add(p_wpt->service_handle,
425  &char_md,
426  &attr_char_value,
427  &p_wpt->pru_control_handles);
428 }
429 
437 static uint32_t ptu_static_char_add(ble_wpts_t * p_wpt, const ble_wpts_init_t * p_wpts_init)
438 {
439  ble_gatts_char_md_t char_md;
440  ble_gatts_attr_t attr_char_value;
441  ble_uuid_t ble_uuid;
442  ble_gatts_attr_md_t attr_md;
443 
444  memset(&char_md, 0, sizeof(char_md));
445 
446  char_md.char_props.write = 1;
447  char_md.char_props.read = 1;
448  char_md.char_props.write_wo_resp = 1; // Required for "GATT write without response" procedure
449  char_md.p_char_user_desc = NULL;
450  char_md.p_char_pf = NULL;
451  char_md.p_user_desc_md = NULL;
452  char_md.p_cccd_md = NULL;
453  char_md.p_sccd_md = NULL;
454 
456 
457  memset(&attr_md, 0, sizeof(attr_md));
458 
459  attr_md.write_perm = p_wpts_init->wpts_attr_md.write_perm;
460  attr_md.read_perm = p_wpts_init->wpts_attr_md.read_perm;
461 
462  attr_md.vloc = BLE_GATTS_VLOC_STACK;
463  attr_md.rd_auth = 0;
464  attr_md.wr_auth = 0;
465  attr_md.vlen = 1;
466 
467  memset(&attr_char_value, 0, sizeof(attr_char_value));
468 
469  attr_char_value.p_uuid = &ble_uuid;
470  attr_char_value.p_attr_md = &attr_md;
471  attr_char_value.init_len = 0;
472  attr_char_value.init_offs = 0;
473  attr_char_value.max_len = BLE_WPTS_PTU_STATIC_LEN;
474  attr_char_value.p_value = NULL;
475 
476  return sd_ble_gatts_characteristic_add(p_wpt->service_handle,
477  &char_md,
478  &attr_char_value,
479  &p_wpt->ptu_static_handles);
480 }
481 
489 static uint32_t pru_alert_char_add(ble_wpts_t * p_wpt, const ble_wpts_init_t * p_wpts_init)
490 {
491  ble_gatts_char_md_t char_md;
492  ble_gatts_attr_md_t cccd_md;
493  ble_gatts_attr_t attr_char_value;
494  ble_uuid_t ble_uuid;
495  ble_gatts_attr_md_t attr_md;
496  uint8_t init_value = 0;
497 
498  memset(&char_md, 0, sizeof(char_md));
499  char_md.char_props.notify = 1;
500  char_md.char_props.read = 1;
501  char_md.p_char_user_desc = NULL;
502  char_md.p_char_pf = NULL;
503  char_md.p_user_desc_md = NULL;
504  char_md.p_cccd_md = &cccd_md;
505  char_md.p_sccd_md = NULL;
506 
507  memset(&cccd_md, 0, sizeof(cccd_md));
508  BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.read_perm);
509  BLE_GAP_CONN_SEC_MODE_SET_OPEN(&cccd_md.write_perm);
510  cccd_md.vloc = BLE_GATTS_VLOC_STACK;
511 
513 
514  memset(&attr_md, 0, sizeof(attr_md));
515 
516  BLE_GAP_CONN_SEC_MODE_SET_NO_ACCESS(&attr_md.write_perm);
517  attr_md.read_perm = p_wpts_init->wpts_attr_md.read_perm;
518  attr_md.vloc = BLE_GATTS_VLOC_STACK;
519  attr_md.rd_auth = 0;
520  attr_md.wr_auth = 0;
521  attr_md.vlen = 0;
522 
523  memset(&attr_char_value, 0, sizeof(attr_char_value));
524 
525  attr_char_value.p_uuid = &ble_uuid;
526  attr_char_value.p_attr_md = &attr_md;
527  attr_char_value.init_len = 1;
528  attr_char_value.init_offs = 0;
529  attr_char_value.max_len = BLE_WPTS_PRU_ALERT_MAX_LEN;
530  attr_char_value.p_value = &init_value;
531 
532  return sd_ble_gatts_characteristic_add(p_wpt->service_handle,
533  &char_md,
534  &attr_char_value,
535  &p_wpt->pru_alert_handles);
536 }
537 
545 static uint32_t pru_static_char_add(ble_wpts_t * p_wpt, const ble_wpts_init_t * p_wpts_init)
546 {
547  ble_gatts_char_md_t char_md;
548  ble_gatts_attr_t attr_char_value;
549  ble_uuid_t ble_uuid;
550  ble_gatts_attr_md_t attr_md;
551  uint8_t encoded_initial_val[BLE_WPTS_PRU_STATIC_LEN];
552 
553  memset(&char_md, 0, sizeof(char_md));
554 
555  char_md.char_props.read = 1;
556  char_md.p_char_user_desc = NULL;
557  char_md.p_char_pf = NULL;
558  char_md.p_user_desc_md = NULL;
559  char_md.p_cccd_md = NULL;
560  char_md.p_sccd_md = NULL;
561 
563 
564  memset(&attr_md, 0, sizeof(attr_md));
565 
566  attr_md.read_perm = p_wpts_init->wpts_attr_md.read_perm;
567  BLE_GAP_CONN_SEC_MODE_SET_NO_ACCESS(&attr_md.write_perm);
568  attr_md.vloc = BLE_GATTS_VLOC_STACK;
569  attr_md.rd_auth = 0;
570  attr_md.wr_auth = 0;
571  attr_md.vlen = 1;
572 
573  memset(&attr_char_value, 0, sizeof(attr_char_value));
574 
575  attr_char_value.p_uuid = &ble_uuid;
576  attr_char_value.p_attr_md = &attr_md;
577  attr_char_value.init_len = BLE_WPTS_PRU_STATIC_LEN;
578 
579  pru_static_encode(p_wpt, &p_wpts_init->pru_static, encoded_initial_val);
580 
581  attr_char_value.init_offs = 0;
582  attr_char_value.max_len = BLE_WPTS_PRU_STATIC_LEN;
583  attr_char_value.p_value = encoded_initial_val;
584 
585  return sd_ble_gatts_characteristic_add(p_wpt->service_handle,
586  &char_md,
587  &attr_char_value,
588  &p_wpt->pru_static_handles);
589 }
590 
598 static uint32_t pru_dynamic_char_add(ble_wpts_t * p_wpt, const ble_wpts_init_t * p_wpts_init)
599 {
600  ble_gatts_char_md_t char_md;
601  ble_gatts_attr_t attr_char_value;
602  ble_uuid_t ble_uuid;
603  ble_gatts_attr_md_t attr_md;
604  uint8_t encoded_initial_val[BLE_WPTS_PRU_DYNAMIC_LEN];
605 
606  memset(&char_md, 0, sizeof(char_md));
607 
608  char_md.char_props.read = 1;
609  char_md.p_char_user_desc = NULL;
610  char_md.p_char_pf = NULL;
611  char_md.p_user_desc_md = NULL;
612  char_md.p_cccd_md = NULL;
613  char_md.p_sccd_md = NULL;
614 
616 
617  memset(&attr_md, 0, sizeof(attr_md));
618 
619  attr_md.read_perm = p_wpts_init->wpts_attr_md.read_perm;
620  BLE_GAP_CONN_SEC_MODE_SET_NO_ACCESS(&attr_md.write_perm);
621  attr_md.vloc = BLE_GATTS_VLOC_STACK;
622  attr_md.rd_auth = 0;
623  attr_md.wr_auth = 0;
624  attr_md.vlen = 1;
625 
626  memset(&attr_char_value, 0, sizeof(attr_char_value));
627  memset(encoded_initial_val, 0, sizeof(encoded_initial_val));
628 
629  attr_char_value.p_uuid = &ble_uuid;
630  attr_char_value.p_attr_md = &attr_md;
631  attr_char_value.init_len = sizeof(encoded_initial_val);
632  attr_char_value.init_offs = 0;
633  attr_char_value.max_len = BLE_WPTS_PRU_DYNAMIC_LEN;
634  attr_char_value.p_value = encoded_initial_val;
635 
636  return sd_ble_gatts_characteristic_add(p_wpt->service_handle, \
637  &char_md, \
638  &attr_char_value, \
639  &p_wpt->pru_dynamic_handles);
640 }
641 
642 uint32_t ble_wpts_init(ble_wpts_t * p_wpt, const ble_wpts_init_t * p_wpts_init)
643 {
644  uint32_t err_code;
645  ble_uuid_t ble_uuid;
646 
647  // Initialize service structure
648  p_wpt->evt_handler = p_wpts_init->evt_handler;
649  p_wpt->conn_handle = BLE_CONN_HANDLE_INVALID;
650 
651  err_code = ble_wpts_uuid_init();
652  if (err_code != NRF_SUCCESS)
653  {
654  return err_code;
655  }
656 
657  BLE_UUID_BLE_ASSIGN(ble_uuid, BLE_WPTS_UUID16);
658 
659  err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY, &ble_uuid, &p_wpt->service_handle);
660  if (err_code != NRF_SUCCESS)
661  {
662  return err_code;
663  }
664 
665  // Add PRU Control characteristic
666  err_code = pru_control_char_add(p_wpt, p_wpts_init);
667  if (err_code != NRF_SUCCESS)
668  {
669  return err_code;
670  }
671 
672  // Add PTU Static Parameter characteristic
673  err_code = ptu_static_char_add(p_wpt, p_wpts_init);
674  if (err_code != NRF_SUCCESS)
675  {
676  return err_code;
677  }
678 
679  // Add PRU Alert characteristic
680  err_code = pru_alert_char_add(p_wpt, p_wpts_init);
681  if (err_code != NRF_SUCCESS)
682  {
683  return err_code;
684  }
685 
686  // Add PRU Static Parameter characteristic
687  err_code = pru_static_char_add(p_wpt, p_wpts_init);
688  if (err_code != NRF_SUCCESS)
689  {
690  return err_code;
691  }
692 
693  // Add PRU Dynamic Parameter characteristic
694  err_code = pru_dynamic_char_add(p_wpt, p_wpts_init);
695  if (err_code != NRF_SUCCESS)
696  {
697  return err_code;
698  }
699 
700  return NRF_SUCCESS;
701 }
702 
703 uint32_t ble_wpts_alert_send(ble_wpts_t * p_wpt, pru_alert_t * p_alert)
704 {
705  uint32_t err_code;
706 
707  // Send value if connected and notifying
708  if (p_wpt->conn_handle != BLE_CONN_HANDLE_INVALID)
709  {
710  uint16_t hvx_len;
711  ble_gatts_hvx_params_t hvx_params;
712  uint8_t alert[BLE_WPTS_PRU_ALERT_MAX_LEN];
713 
714  memset(&hvx_params, 0, sizeof(hvx_params));
715  memset(alert, 0, BLE_WPTS_PRU_ALERT_MAX_LEN);
716  hvx_params.p_data = alert;
717  hvx_len = BLE_WPTS_PRU_ALERT_MIN_LEN;
718 
719  hvx_params.handle = p_wpt->pru_alert_handles.value_handle;
720  hvx_params.type = BLE_GATT_HVX_NOTIFICATION; // As Mode Transition is not supported, we only send Alerts as notifications.
721  hvx_params.offset = 0;
722  hvx_params.p_len = &hvx_len;
723 
724  BLE_WPTS_BITFIELD_WRITE(hvx_params.p_data[BLE_WPTS_PRU_ALERT_POS], \
727  p_alert->alerts.pru_over_voltage );
728 
729  BLE_WPTS_BITFIELD_WRITE(hvx_params.p_data[BLE_WPTS_PRU_ALERT_POS], \
732  p_alert->alerts.pru_over_current);
733 
734  BLE_WPTS_BITFIELD_WRITE(hvx_params.p_data[BLE_WPTS_PRU_ALERT_POS], \
737  p_alert->alerts.pru_over_temperature);
738 
739  BLE_WPTS_BITFIELD_WRITE(hvx_params.p_data[BLE_WPTS_PRU_ALERT_POS], \
742  p_alert->alerts.pru_self_protection);
743 
744  BLE_WPTS_BITFIELD_WRITE(hvx_params.p_data[BLE_WPTS_PRU_ALERT_POS], \
747  p_alert->alerts.charge_complete);
748 
749  BLE_WPTS_BITFIELD_WRITE(hvx_params.p_data[BLE_WPTS_PRU_ALERT_POS], \
752  p_alert->alerts.wired_charge_detect);
753 
758 
759  err_code = sd_ble_gatts_hvx(p_wpt->conn_handle, &hvx_params);
760  }
761  else
762  {
763  err_code = NRF_ERROR_INVALID_STATE;
764  }
765 
766  return err_code;
767 }
768 
769 uint32_t ble_wpts_pru_static_set(ble_wpts_t * p_wpt, pru_static_t * p_pru_static)
770 {
771  uint8_t encoded_val[BLE_WPTS_PRU_STATIC_LEN];
772  ble_gatts_value_t val;
773 
774  val.len = BLE_WPTS_PRU_STATIC_LEN;
775  val.offset = 0;
776  val.p_value = encoded_val;
777 
778  // encode characteristic value
779  pru_static_encode(p_wpt, p_pru_static, encoded_val);
780 
781  // set value
782  return sd_ble_gatts_value_set(p_wpt->conn_handle,
783  p_wpt->pru_static_handles.value_handle,
784  &val);
785 }
786 
787 
790 uint32_t ble_wpts_pru_dynamic_set(ble_wpts_t * p_wpts, pru_dynamic_t * p_pru_dynamic)
791 {
792  uint8_t encoded_val[BLE_WPTS_PRU_DYNAMIC_LEN];
793  ble_gatts_value_t val;
794 
795  val.len = BLE_WPTS_PRU_DYNAMIC_LEN;
796  val.offset = 0;
797  val.p_value = encoded_val;
798 
799  // encode characteristic value
800  pru_dynamic_encode(p_wpts, p_pru_dynamic, encoded_val);
801 
802  // set value
803  return sd_ble_gatts_value_set(p_wpts->conn_handle,
804  p_wpts->pru_dynamic_handles.value_handle,
805  &val);
806 }
807 
810 //lint -restore
uint16_t irect
Definition: wpt.h:168
uint8_t protocol_rev
Definition: wpt.h:120
uint8_t info_ccp_connected_mode
Definition: wpt.h:105
ctl_time_set_t
Time set field in control packet.
Definition: wpt.h:65
#define BLE_WPTS_PRU_DYNAMIC_OPTION_TEMPERATURE_BITMSK
uint8_t info_adj_power_capability
Definition: wpt.h:104
#define BLE_WPTS_PRU_DYNAMIC_ALERT_ADJ_POWER_RESP_BITPOS
ble_gatts_char_handles_t pru_dynamic_handles
Definition: ble_wpts.h:79
#define BLE_WPTS_PRU_INFO_ADJUST_POWER_BITPOS
#define BLE_WPTS_PRU_CONTROL_LEN
Characteristic value lengths.
#define BLE_WPTS_PRU_DYNAMIC_TEMPERATURE_POS
pru_alert_bits_t alerts
Definition: wpt.h:175
static void on_pru_control_write(ble_wpts_t *p_wpts, ble_gatts_evt_write_t *p_evt_write)
Handle write events to the PRU Control characteristic.
Definition: ble_wpts.c:66
#define BLE_WPTS_PRU_DYNAMIC_OPTIONAL_FIELDS_POS
PRU dynamic characteristic byte positions.
#define BLE_WPTS_PTU_STATIC_PTU_MAX_LOAD_RES_POS
#define BLE_WPTS_PTU_STATIC_OPTION_MAX_RESISTANCE_BITPOS
ble_wpts_evt_handler_t evt_handler
Definition: ble_wpts.h:65
#define BLE_WPTS_PRU_ALERT_OVER_CURRENT_BITMSK
static uint32_t pru_control_char_add(ble_wpts_t *p_wpt, const ble_wpts_init_t *p_wpts_init)
Add PRU Control characteristic.
Definition: ble_wpts.c:386
#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 wired_charge_detect
Definition: wpt.h:139
uint32_t ble_wpts_pru_static_set(ble_wpts_t *p_wpt, pru_static_t *p_pru_static)
Sets value of the PRU Static Parameter characteristic.
Definition: ble_wpts.c:769
uint8_t pru_category
Definition: wpt.h:92
#define BLE_WPTS_PRU_DYNAMIC_OPTION_VOUT_BITMSK
#define BLE_WPTS_PRU_ALERT_MODE_NO_MODE_TRANSITION_VAL
PRU Alert Mode transition.
#define BLE_WPTS_UUID16
WPT Service UUIDs.
static uint32_t pru_alert_char_add(ble_wpts_t *p_wpt, const ble_wpts_init_t *p_wpts_init)
Add PRU Alert characteristic.
Definition: ble_wpts.c:489
#define BLE_WPTS_PTU_STATIC_PTU_MAX_SRC_IMPED_POS
#define BLE_WPTS_PTU_STATIC_HW_REV_POS
uint16_t vout
Definition: wpt.h:169
uint8_t adjust_power_response
Definition: wpt.h:177
#define BLE_WPTS_PRU_INFO_CHARGE_COMPLETE_CONN_MODE_BITPOS
#define BLE_WPTS_PTU_STATIC_MAX_LOAD_RESISTANCE_BITMSK
#define BLE_WPTS_PRU_DYNAMIC_LEN
static void on_connect(ble_wpts_t *p_wpt, ble_evt_t *p_ble_evt)
Connect event handler.
Definition: ble_wpts.c:45
#define BLE_WPTS_PTU_STATIC_NUM_DEVICES_BITMSK
uint32_t ble_wpts_uuid_init(void)
Prepare softdevice for using the WPTS 128 bit UUIDs.
#define BLE_WPTS_ENABLE_PRU_ADJ_POWER_BITPOS
#define BLE_WPTS_PRU_INFO_BLE_BITMSK
#define BLE_WPTS_PRU_DYNAMIC_OPTION_VRECT_MIN_DYN_BITPOS
uint8_t protocol_rev
Definition: wpt.h:91
#define BLE_WPTS_PTU_STATIC_MAX_SOURCE_IMPEDANCE_BITPOS
PTU Static characteristic PTU Max Source Impedance bitfield.
#define BLE_WPTS_PRU_ALERT_WIRED_CHARGER_DETECT_BITPOS
uint16_t service_handle
Definition: ble_wpts.h:74
uint16_t vrect
Definition: wpt.h:167
uint8_t info_pc_alg_pref
Definition: wpt.h:103
#define BLE_WPTS_PRU_DYNAMIC_VRECT_HIGH_DYN_POS
uint16_t vrect_set_dyn
Definition: wpt.h:173
uint8_t enable_pru_output
Definition: wpt.h:81
#define BLE_WPTS_PRU_DYNAMIC_OPTION_IOUT_BITPOS
#define BLE_WPTS_OPTION_VALID_VAL
#define BLE_WPTS_PRU_DYNAMIC_IRECT_POS
int16_t temperature
Definition: wpt.h:171
#define BLE_WPTS_PRU_ALERT_POS
PRU alert characteristic byte positions.
uint8_t ptu_max_source_impedance
Definition: wpt.h:115
ctl_perm_t
Permissions fields in control packet.
Definition: wpt.h:52
uint16_t iout
Definition: wpt.h:170
#define BLE_WPTS_PRU_DYNAMIC_PTU_TESTER_COMMAND_POS
PRU Alert Parameter structure.
Definition: wpt.h:145
#define BLE_WPTS_PRU_ALERT_CHARGE_COMPLETE_BITMSK
uint16_t conn_handle
Definition: ble_wpts.h:80
#define BLE_WPTS_PRU_ALERT_MIN_LEN
uint16_t vrect_high_dyn
Definition: wpt.h:174
#define BLE_WPTS_PTU_STATIC_OPTIONAL_FIELDS_POS
PTU Static Parameter characteristic byte positions.
#define BLE_WPTS_PRU_CONTROL_PERMISSION_POS
uint32_t ble_wpts_init(ble_wpts_t *p_wpt, const ble_wpts_init_t *p_wpts_init)
Initialize the WPT Service.
Definition: ble_wpts.c:642
#define BLE_WPTS_PTU_STATIC_PTU_CLASS_POS
#define BLE_WPTS_PTU_STATIC_OPTION_MAX_IMPEDANCE_BITPOS
PTU Static characteristic Option bitfield definitions.
const ble_gatts_rw_authorize_reply_params_t m_auth_reply
Definition: ble_wpts.c:34
uint8_t fw_revision
Definition: wpt.h:94
uint8_t ptu_power
Definition: wpt.h:114
#define BLE_WPTS_PRU_ALERT_MODE_TRANSITION_BITPOS
#define BLE_WPTS_PRU_DYNAMIC_OPTION_VRECT_SET_DYN_BITPOS
#define BLE_WPTS_PRU_STATIC_VRECT_MIN_STATIC_POS
#define BLE_WPTS_PTU_STATIC_OPTION_MAX_RESISTANCE_BITMSK
#define BLE_WPTS_PRU_DYNAMIC_VRECT_SET_DYN_POS
#define BLE_WPTS_PRU_ALERT_MAX_LEN
#define BLE_WPTS_PTU_STATIC_FW_REV_POS
pru_static_t pru_static
Definition: ble_wpts.h:66
uint32_t ble_wpts_pru_dynamic_set(ble_wpts_t *p_wpts, pru_dynamic_t *p_pru_dynamic)
Transfer latest read sensor data to characteristic attributes.
Definition: ble_wpts.c:790
ctl_time_set_t time_set
Definition: wpt.h:85
#define BLE_WPTS_PTU_STATIC_MAX_SOURCE_IMPEDANCE_BITMSK
uint8_t devices_supported_bitval
Definition: wpt.h:121
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...
#define BLE_WPTS_BITFIELD_WRITE(bitfield, msk, pos, val)
Write bitfield.
uint16_t vrect_min_dyn
Definition: wpt.h:172
#define BLE_WPTS_PRU_STATIC_HW_REV_POS
#define BLE_WPTS_PRU_STATIC_OPTION_DELTA_R1_BITMSK
#define BLE_WPTS_PTU_STATIC_NUM_DEVICES_BITPOS
PTU Static characteristic PTU MAx Number of Devices Supported field.
#define BLE_WPTS_PRU_STATIC_VRECT_SET_POS
uint8_t info_sep_btle_radio
Definition: wpt.h:102
#define BLE_WPTS_PRU_ALERT_MODE_TRANSITION_BITMSK
uint8_t option_fields_r1_valid
Definition: wpt.h:100
static void on_ptu_static_parameter_write(ble_wpts_t *p_wpts, ble_gatts_evt_write_t *p_evt_write)
Handle write events to the PTU Static Parameter characteristic.
Definition: ble_wpts.c:96
uint16_t vrect_high_static
Definition: wpt.h:97
#define BLE_WPTS_PRU_STATIC_FW_REV_POS
#define BLE_WPTS_PRU_ALERT_OVER_VOLTAGE_BITPOS
PRU Alert characteristic value bitfield.
ble_wpts_evt_type_t evt_type
Definition: ble_wpts.h:43
#define BLE_WPTS_PRU_DYNAMIC_OPTION_VOUT_BITPOS
PRU Dynamic Parameter characteristic Option field.
ble_gatts_char_handles_t pru_control_handles
Definition: ble_wpts.h:75
#define BLE_WPTS_PRU_DYNAMIC_ALERT_CHARGE_PORT_BITMSK
#define BLE_WPTS_PRU_DYNAMIC_PRU_ALERT_POS
uint8_t hw_revision
Definition: wpt.h:93
uint8_t ptu_max_load_resistance
Definition: wpt.h:116
uint32_t ble_wpts_alert_send(ble_wpts_t *p_wpt, pru_alert_t *p_alert)
Sends PRU alert if notification has been enabled.
Definition: ble_wpts.c:703
uint8_t pru_charge_port
Definition: wpt.h:176
#define BLE_WPTS_PTU_STATIC_OPTION_MAX_IMPEDANCE_BITMSK
#define BLE_WPTS_PRU_DYNAMIC_VOUT_POS
static void on_disconnect(ble_wpts_t *p_wpt, ble_evt_t *p_ble_evt)
Disconnect event handler.
Definition: ble_wpts.c:55
#define BLE_WPTS_ENABLE_PRU_CHARGE_INDICATOR_BITMSK
#define BLE_WPTS_ENABLE_PRU_ADJ_POWER_BITMSK
uint8_t hardware_rev
Definition: wpt.h:118
uint8_t pru_over_voltage
Definition: wpt.h:134
ctl_perm_t permissions
Definition: wpt.h:84
#define BLE_WPTS_PTU_STATIC_LEN
uint8_t option_valid_max_imp
Definition: wpt.h:112
WPT Service event.
Definition: ble_wpts.h:41
#define BLE_WPTS_PRU_DYNAMIC_OPTION_VRECT_HIGH_DYN_BITPOS
static uint32_t pru_dynamic_char_add(ble_wpts_t *p_wpt, const ble_wpts_init_t *p_wpts_init)
Add PRU Dynamic Parameter characteristic.
Definition: ble_wpts.c:598
#define BLE_WPTS_PRU_ALERT_OVER_VOLTAGE_BITMSK
#define BLE_WPTS_PRU_STATIC_LEN
PRU Static Parameter structure.
Definition: wpt.h:89
#define BLE_WPTS_PRU_ALERT_SELF_PROTECTION_BITPOS
uint8_t pru_over_current
Definition: wpt.h:135
#define BLE_WPTS_PRU_STATIC_PROTOCOL_REV_POS
static void pru_static_encode(ble_wpts_t *p_wpt, const pru_static_t *p_pru_static, uint8_t *p_encoded_buffer)
Encode PRU Static Parameter structure.
Definition: ble_wpts.c:226
uint8_t pru_over_temperature
Definition: wpt.h:136
#define BLE_WPTS_PRU_DYNAMIC_OPTION_VRECT_SET_DYN_BITMSK
static void on_write(ble_wpts_t *p_wpt, ble_evt_t *p_ble_evt)
Write event handler.
Definition: ble_wpts.c:171
#define BLE_WPTS_PRU_CONTROL_TIME_SET_POS
void ble_wpts_uuid_get(ble_wpts_uuid128_t uuid128, ble_uuid_t *ble_uuid)
Get ble_uuid representation of 128 bit UUID.
#define BLE_WPTS_PRU_DYNAMIC_OPTION_VRECT_MIN_DYN_BITMSK
#define BLE_WPTS_PRU_STATIC_VRECT_HIGH_STATIC_POS
uint16_t delta_r1
Definition: wpt.h:99
#define BLE_WPTS_PRU_DYNAMIC_OPTION_IOUT_BITMSK
#define BLE_WPTS_PRU_INFO_POWER_CONTROL_PREF_BITMSK
#define BLE_WPTS_PRU_ALERT_OVER_TEMPERATURE_BITMSK
#define BLE_WPTS_PRU_DYNAMIC_IOUT_POS
#define BLE_WPTS_PRU_ALERT_OVER_CURRENT_BITPOS
#define BLE_WPTS_PTU_STATIC_MAX_LOAD_RESISTANCE_BITPOS
PTU Static characteristic PTU Max Load Resistance bitfield.
union ble_wpts_evt_t::@0 data
#define BLE_WPTS_PRU_STATIC_PRU_INFORMATION_POS
uint8_t ptu_class
Definition: wpt.h:117
ble_gatts_char_handles_t pru_alert_handles
Definition: ble_wpts.h:77
WPT Service init structure.
Definition: ble_wpts.h:63
#define BLE_WPTS_ENABLE_PRU_OUTPUT_BITMSK
#define BLE_WPTS_PRU_STATIC_PRECT_MAX_POS
uint8_t firmware_rev
Definition: wpt.h:119
uint8_t option_valid_max_res
Definition: wpt.h:113
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
uint8_t optional_fields
Definition: wpt.h:166
pru_control_t pru_control
Definition: ble_wpts.h:46
#define BLE_WPTS_PRU_DYNAMIC_OPTION_TEMPERATURE_BITPOS
ctl_adj_power_t
Power adjust settings in control packet.
Definition: wpt.h:42
#define BLE_WPTS_PRU_DYNAMIC_ALERT_ADJ_POWER_RESP_BITMSK
#define BLE_WPTS_PRU_CONTROL_ENABLES_POS
PRU Control characteristic fields byte positions.
uint16_t vrect_set
Definition: wpt.h:98
uint8_t enable_pru_charge_indicator
Definition: wpt.h:82
#define BLE_WPTS_ENABLE_PRU_OUTPUT_BITPOS
PRU Control characteristic Enables bit field definitions.
#define BLE_WPTS_PRU_INFO_ADJUST_POWER_BITMSK
static void on_pru_alert_cccd_write(ble_wpts_t *p_wpts, ble_gatts_evt_write_t *p_evt_write)
Handle write events to the PRU Alert CCCD.
Definition: ble_wpts.c:140
void ble_wpts_on_ble_evt(ble_wpts_t *p_wpt, ble_evt_t *p_ble_evt)
WPT Service BLE stack event handler.
Definition: ble_wpts.c:196
#define BLE_WPTS_PRU_ALERT_SELF_PROTECTION_BITMSK
#define BLE_WPTS_PRU_DYNAMIC_VRECT_MIN_DYN_POS
#define BLE_WPTS_PTU_STATIC_PROTOCOL_REV_POS
uint8_t info_ptu_test_mode
Definition: wpt.h:106
uint8_t pru_self_protection
Definition: wpt.h:137
#define BLE_WPTS_PRU_ALERT_WIRED_CHARGER_DETECT_BITMSK
static uint32_t ptu_static_char_add(ble_wpts_t *p_wpt, const ble_wpts_init_t *p_wpts_init)
Add PTU Static Parameter characteristic.
Definition: ble_wpts.c:437
#define BLE_WPTS_ENABLE_PRU_CHARGE_INDICATOR_BITPOS
static uint32_t pru_static_char_add(ble_wpts_t *p_wpt, const ble_wpts_init_t *p_wpts_init)
Add PRU Static Parameter characteristic.
Definition: ble_wpts.c:545
ble_wpts_evt_handler_t evt_handler
Definition: ble_wpts.h:73
ble_gatts_char_handles_t ptu_static_handles
Definition: ble_wpts.h:76
#define BLE_WPTS_PRU_INFO_NFC_BITMSK
#define BLE_WPTS_PRU_DYNAMIC_OPTION_VRECT_HIGH_DYN_BITMSK
static void pru_dynamic_encode(ble_wpts_t *p_wpt, const pru_dynamic_t *p_pru_dynamic, uint8_t *p_encoded_buffer)
Encode PRU Dynamic Parameter structure.
Definition: ble_wpts.c:284
#define BLE_WPTS_PRU_INFO_NFC_BITPOS
PRU Static characteristic PRU Information field.
#define BLE_WPTS_PRU_ALERT_OVER_TEMPERATURE_BITPOS
#define BLE_WPTS_BITFIELD_READ(bitfield, msk, pos)
Read bitfield.
ble_srv_security_mode_t wpts_attr_md
Definition: ble_wpts.h:67
WPT Service status structure.
Definition: ble_wpts.h:71
#define BLE_WPTS_PRU_INFO_PTU_TEST_MODE_BITMSK
ctl_adj_power_t adj_power
Definition: wpt.h:83
#define BLE_WPTS_PRU_STATIC_DELTA_R1_POS
pru_alert_bits_t alerts
Definition: wpt.h:147
#define BLE_WPTS_PTU_STATIC_PTU_POWER_POS
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_INFO_BLE_BITPOS
ble_gatts_char_handles_t pru_static_handles
Definition: ble_wpts.h:78
#define BLE_WPTS_PRU_ALERT_CHARGE_COMPLETE_BITPOS
uint8_t prect_max
Definition: wpt.h:95
#define BLE_WPTS_PTU_STATIC_NUM_DEVICES_SUPPORTED_POS
#define BLE_WPTS_PRU_STATIC_PRU_CATEGORY_POS
ptu_static_t ptu_static
Definition: ble_wpts.h:47