Nordic Semiconductor nRF5 AirFuel SDK  version 2.2.0
advertiser_beacon.c
Go to the documentation of this file.
1 /* Copyright (c) 2014 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 "advertiser_beacon.h"
16 #include <stdio.h>
17 #include <string.h>
18 
19 #include "nrf_soc.h"
20 #include "app_error.h"
21 #include "app_util.h"
22 
23 #define APP_DEVICE_TYPE 0x02
26 /******************************************************************************/
27 
29 /******************************************************************************/
30 
32 static struct
33 {
34  ble_beacon_init_t init_vals; // Beacon inital values
35  ble_gap_adv_params_t adv_params; // Advertisement paramters
36 } m_beacon;
37 
38 
39 
41 static uint8_t * m_get_adv_data(uint8_t * data_length)
42 {
43  static uint8_t adv_pdu[31];
44  uint8_t manuf_data_len_start_idx, beacon_data_len_start_idx;
45  uint8_t offset = 0;
46 
47  // Adding advertising data: Manufacturer specific data.
48  adv_pdu[offset++] = 0x00; // Manufacturer specific data length field (will be filled later).
49  manuf_data_len_start_idx = offset;
50  adv_pdu[offset++] = BLE_GAP_AD_TYPE_MANUFACTURER_SPECIFIC_DATA;
51  offset += uint16_encode(m_beacon.init_vals.manuf_id, &adv_pdu[offset]);
52  adv_pdu[offset++] = APP_DEVICE_TYPE;
53 
54  // Adding manufacturer specific data (beacon data).
55  adv_pdu[offset++] = 0; // Beacon data length field (will be filled later).
56  beacon_data_len_start_idx = offset;
57  memcpy(&adv_pdu[offset], &m_beacon.init_vals.uuid, sizeof(ble_uuid128_t));
58  offset += sizeof(ble_uuid128_t);
59  offset += uint16_encode(m_beacon.init_vals.major, &adv_pdu[offset]);
60  offset += uint16_encode(m_beacon.init_vals.minor, &adv_pdu[offset]);
61  adv_pdu[offset++] = m_beacon.init_vals.rssi;
62 
63  // Filling in length fields.
64  adv_pdu[beacon_data_len_start_idx-1] = offset - beacon_data_len_start_idx;
65  adv_pdu[manuf_data_len_start_idx-1] = offset - manuf_data_len_start_idx;
66 
67  *data_length=offset;
68  return &adv_pdu[0];
69 }
70 
72 {
73  uint32_t err_code;
74  uint8_t data_length;
75 
76  memcpy(&m_beacon.init_vals, p_init, sizeof(m_beacon.init_vals));
77  memset(&m_beacon.adv_params, 0, sizeof(ble_gap_adv_params_t));
78  m_beacon.adv_params.interval=m_beacon.init_vals.adv_interval;
79  m_beacon.adv_params.type=BLE_GAP_ADV_TYPE_ADV_NONCONN_IND;
80 
81  err_code=sd_ble_gap_adv_data_set(m_get_adv_data(&data_length), data_length, 0, 0);
82  APP_ERROR_CHECK(err_code);
83 }
84 
85 void app_beacon_start(void)
86 {
87  uint32_t err_code;
88  err_code=sd_ble_gap_adv_start(&m_beacon.adv_params);
89  APP_ERROR_CHECK(err_code);
90 }
91 
92 void app_beacon_stop(void)
93 {
94  uint32_t err_code;
95  err_code=sd_ble_gap_adv_stop();
96  APP_ERROR_CHECK(err_code);
97 }
98 
void app_beacon_init(ble_beacon_init_t *p_init)
Function for initializing the advertiser module.
static uint8_t * m_get_adv_data(uint8_t *data_length)
Initialize adv packet.
Advertiser module. This module shows an example of using periodic timeslots on the radio when the Sof...
void app_beacon_stop(void)
Function for stopping the advertisement.
static struct @2 m_beacon
Struct containing all variables of this module.
void app_beacon_start(void)
Function for starting the advertisement.
BLE beacon init type.
#define APP_DEVICE_TYPE