Contiki-NG
ble-core.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015, Nordic Semiconductor
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  */
30 /**
31  * \addtogroup nrf52832-ble
32  * @{
33  *
34  * \file
35  * Basic BLE functions.
36  * \author
37  * Wojciech Bober <wojciech.bober@nordicsemi.no>
38  *
39  */
40 #include <stdbool.h>
41 #include <stdint.h>
42 #include "boards.h"
43 #include "nordic_common.h"
44 #include "nrf_delay.h"
45 #include "nrf_sdm.h"
46 #include "ble_advdata.h"
47 #include "ble_srv_common.h"
48 #include "ble_ipsp.h"
49 #include "softdevice_handler.h"
50 #include "app_error.h"
51 #include "iot_defines.h"
52 #include "ble-core.h"
53 
54 #define DEBUG 0
55 #if DEBUG
56 #include <stdio.h>
57 #define PRINTF(...) printf(__VA_ARGS__)
58 #else
59 #define PRINTF(...)
60 #endif
61 
62 #define IS_SRVC_CHANGED_CHARACT_PRESENT 1
63 #define APP_ADV_TIMEOUT 0 /**< Time for which the device must be advertising in non-connectable mode (in seconds). 0 disables timeout. */
64 #define APP_ADV_ADV_INTERVAL MSEC_TO_UNITS(333, UNIT_0_625_MS) /**< The advertising interval. This value can vary between 100ms to 10.24s). */
65 
66 static ble_gap_adv_params_t m_adv_params; /**< Parameters to be passed to the stack when starting advertising. */
67 
68 static void
69 ble_evt_dispatch(ble_evt_t * p_ble_evt);
70 /*---------------------------------------------------------------------------*/
71 /**
72  * \brief Initialize and enable the BLE stack.
73  */
74 void
76 {
77  uint32_t err_code;
78 
79  // Enable BLE stack.
80  ble_enable_params_t ble_enable_params;
81  memset(&ble_enable_params, 0, sizeof(ble_enable_params));
82  ble_enable_params.gatts_enable_params.attr_tab_size =
83  BLE_GATTS_ATTR_TAB_SIZE_DEFAULT;
84  ble_enable_params.gatts_enable_params.service_changed =
85  IS_SRVC_CHANGED_CHARACT_PRESENT;
86  err_code = sd_ble_enable(&ble_enable_params);
87  APP_ERROR_CHECK(err_code);
88 
89  // Register with the SoftDevice handler module for BLE events.
90  err_code = softdevice_ble_evt_handler_set(ble_evt_dispatch);
91  APP_ERROR_CHECK(err_code);
92 
93  // Setup address
94  ble_gap_addr_t ble_addr;
95  err_code = sd_ble_gap_address_get(&ble_addr);
96  APP_ERROR_CHECK(err_code);
97 
98  ble_addr.addr[5] = 0x00;
99  ble_addr.addr_type = BLE_GAP_ADDR_TYPE_PUBLIC;
100 
101  err_code = sd_ble_gap_address_set(BLE_GAP_ADDR_CYCLE_MODE_NONE, &ble_addr);
102  APP_ERROR_CHECK(err_code);
103 }
104 /*---------------------------------------------------------------------------*/
105 /**
106  * \brief Return device EUI64 MAC address
107  * \param addr pointer to a buffer to store the address
108  */
109 void
110 ble_get_mac(uint8_t addr[8])
111 {
112  uint32_t err_code;
113  ble_gap_addr_t ble_addr;
114 
115  err_code = sd_ble_gap_address_get(&ble_addr);
116  APP_ERROR_CHECK(err_code);
117 
118  IPV6_EUI64_CREATE_FROM_EUI48(addr, ble_addr.addr, ble_addr.addr_type);
119 }
120 /*---------------------------------------------------------------------------*/
121 /**
122  * \brief Initialize BLE advertising data.
123  * \param name Human readable device name that will be advertised
124  */
125 void
126 ble_advertising_init(const char *name)
127 {
128  uint32_t err_code;
129  ble_advdata_t advdata;
130  uint8_t flags = BLE_GAP_ADV_FLAG_BR_EDR_NOT_SUPPORTED;
131  ble_gap_conn_sec_mode_t sec_mode;
132 
133  BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode);
134 
135  err_code = sd_ble_gap_device_name_set(&sec_mode, (const uint8_t *)name,
136  strlen(name));
137  APP_ERROR_CHECK(err_code);
138 
139  ble_uuid_t adv_uuids[] = {{BLE_UUID_IPSP_SERVICE, BLE_UUID_TYPE_BLE}};
140 
141  // Build and set advertising data.
142  memset(&advdata, 0, sizeof(advdata));
143 
144  advdata.name_type = BLE_ADVDATA_FULL_NAME;
145  advdata.flags = flags;
146  advdata.uuids_complete.uuid_cnt = sizeof(adv_uuids) / sizeof(adv_uuids[0]);
147  advdata.uuids_complete.p_uuids = adv_uuids;
148 
149  err_code = ble_advdata_set(&advdata, NULL);
150  APP_ERROR_CHECK(err_code);
151 
152  // Initialize advertising parameters (used when starting advertising).
153  memset(&m_adv_params, 0, sizeof(m_adv_params));
154 
155  m_adv_params.type = BLE_GAP_ADV_TYPE_ADV_IND;
156  m_adv_params.p_peer_addr = NULL; // Undirected advertisement.
157  m_adv_params.fp = BLE_GAP_ADV_FP_ANY;
159  m_adv_params.timeout = APP_ADV_TIMEOUT;
160 }
161 /*---------------------------------------------------------------------------*/
162 /**
163  * \brief Start BLE advertising.
164  */
165 void
167 {
168  uint32_t err_code;
169 
170  err_code = sd_ble_gap_adv_start(&m_adv_params);
171  APP_ERROR_CHECK(err_code);
172 
173  PRINTF("ble-core: advertising started\n");
174 }
175 /*---------------------------------------------------------------------------*/
176 /**
177  * \brief Print GAP address.
178  * \param addr a pointer to address
179  */
180 void
181 ble_gap_addr_print(const ble_gap_addr_t *addr)
182 {
183  unsigned int i;
184  for(i = 0; i < sizeof(addr->addr); i++) {
185  if(i > 0) {
186  PRINTF(":");
187  }PRINTF("%02x", addr->addr[i]);
188  }PRINTF(" (%d)", addr->addr_type);
189 }
190 /*---------------------------------------------------------------------------*/
191 /**
192  * \brief Function for handling the Application's BLE Stack events.
193  * \param[in] p_ble_evt Bluetooth stack event.
194  */
195 static void
196 on_ble_evt(ble_evt_t *p_ble_evt)
197 {
198  switch(p_ble_evt->header.evt_id) {
199  case BLE_GAP_EVT_CONNECTED:
200  PRINTF("ble-core: connected [handle:%d, peer: ", p_ble_evt->evt.gap_evt.conn_handle);
201  ble_gap_addr_print(&(p_ble_evt->evt.gap_evt.params.connected.peer_addr));
202  PRINTF("]\n");
203  sd_ble_gap_rssi_start(p_ble_evt->evt.gap_evt.conn_handle,
204  BLE_GAP_RSSI_THRESHOLD_INVALID,
205  0);
206  break;
207 
208  case BLE_GAP_EVT_DISCONNECTED:
209  PRINTF("ble-core: disconnected [handle:%d]\n", p_ble_evt->evt.gap_evt.conn_handle);
211  break;
212  default:
213  break;
214  }
215 }
216 /*---------------------------------------------------------------------------*/
217 /**
218  * \brief SoftDevice BLE event callback.
219  * \param[in] p_ble_evt Bluetooth stack event.
220  */
221 static void
222 ble_evt_dispatch(ble_evt_t *p_ble_evt)
223 {
224  ble_ipsp_evt_handler(p_ble_evt);
225  on_ble_evt(p_ble_evt);
226 }
227 /*---------------------------------------------------------------------------*/
228 /** @} */
static void on_ble_evt(ble_evt_t *p_ble_evt)
Function for handling the Application&#39;s BLE Stack events.
Definition: ble-core.c:196
static uip_ds6_addr_t * addr
Pointer to a nbr cache entry.
Definition: uip-nd6.c:116
void ble_stack_init(void)
Initialize and enable the BLE stack.
Definition: ble-core.c:75
static ble_gap_adv_params_t m_adv_params
Parameters to be passed to the stack when starting advertising.
Definition: ble-core.c:66
void ble_advertising_init(const char *name)
Initialize BLE advertising data.
Definition: ble-core.c:126
static void ble_evt_dispatch(ble_evt_t *p_ble_evt)
SoftDevice BLE event callback.
Definition: ble-core.c:222
void ble_get_mac(uint8_t addr[8])
Return device EUI64 MAC address.
Definition: ble-core.c:110
#define APP_ADV_TIMEOUT
Time for which the device must be advertising in non-connectable mode (in seconds).
Definition: ble-core.c:63
void ble_advertising_start(void)
Start BLE advertising.
Definition: ble-core.c:166
#define APP_ADV_ADV_INTERVAL
The advertising interval.
Definition: ble-core.c:64
void ble_gap_addr_print(const ble_gap_addr_t *addr)
Print GAP address.
Definition: ble-core.c:181
Basic BLE functions.