Contiki 3.x
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 cpu
32  * @{
33  *
34  * \addtogroup nrf52832
35  * @{
36  *
37  * \addtogroup nrf52832-ble Bluetooth Low Energy drivers
38  * @{
39  *
40  * \file
41  * Basic BLE functions.
42  * \author
43  * Wojciech Bober <wojciech.bober@nordicsemi.no>
44  *
45  */
46 #include <stdbool.h>
47 #include <stdint.h>
48 #include "boards.h"
49 #include "nordic_common.h"
50 #include "nrf_delay.h"
51 #include "nrf_sdm.h"
52 #include "ble_advdata.h"
53 #include "ble_srv_common.h"
54 #include "ble_ipsp.h"
55 #include "softdevice_handler.h"
56 #include "app_error.h"
57 #include "iot_defines.h"
58 #include "ble-core.h"
59 
60 #define DEBUG 0
61 #if DEBUG
62 #include <stdio.h>
63 #define PRINTF(...) printf(__VA_ARGS__)
64 #else
65 #define PRINTF(...)
66 #endif
67 
68 #define IS_SRVC_CHANGED_CHARACT_PRESENT 1
69 #define APP_ADV_TIMEOUT 0 /**< Time for which the device must be advertising in non-connectable mode (in seconds). 0 disables timeout. */
70 #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). */
71 
72 static ble_gap_adv_params_t m_adv_params; /**< Parameters to be passed to the stack when starting advertising. */
73 
74 static void
75 ble_evt_dispatch(ble_evt_t * p_ble_evt);
76 /*---------------------------------------------------------------------------*/
77 /**
78  * \brief Initialize and enable the BLE stack.
79  */
80 void
82 {
83  uint32_t err_code;
84 
85  // Enable BLE stack.
86  ble_enable_params_t ble_enable_params;
87  memset(&ble_enable_params, 0, sizeof(ble_enable_params));
88  ble_enable_params.gatts_enable_params.attr_tab_size =
89  BLE_GATTS_ATTR_TAB_SIZE_DEFAULT;
90  ble_enable_params.gatts_enable_params.service_changed =
91  IS_SRVC_CHANGED_CHARACT_PRESENT;
92  err_code = sd_ble_enable(&ble_enable_params);
93  APP_ERROR_CHECK(err_code);
94 
95  // Register with the SoftDevice handler module for BLE events.
96  err_code = softdevice_ble_evt_handler_set(ble_evt_dispatch);
97  APP_ERROR_CHECK(err_code);
98 
99  // Setup address
100  ble_gap_addr_t ble_addr;
101  err_code = sd_ble_gap_address_get(&ble_addr);
102  APP_ERROR_CHECK(err_code);
103 
104  ble_addr.addr[5] = 0x00;
105  ble_addr.addr_type = BLE_GAP_ADDR_TYPE_PUBLIC;
106 
107  err_code = sd_ble_gap_address_set(BLE_GAP_ADDR_CYCLE_MODE_NONE, &ble_addr);
108  APP_ERROR_CHECK(err_code);
109 }
110 /*---------------------------------------------------------------------------*/
111 /**
112  * \brief Return device EUI64 MAC address
113  * \param addr pointer to a buffer to store the address
114  */
115 void
116 ble_get_mac(uint8_t addr[8])
117 {
118  uint32_t err_code;
119  ble_gap_addr_t ble_addr;
120 
121  err_code = sd_ble_gap_address_get(&ble_addr);
122  APP_ERROR_CHECK(err_code);
123 
124  IPV6_EUI64_CREATE_FROM_EUI48(addr, ble_addr.addr, ble_addr.addr_type);
125 }
126 /*---------------------------------------------------------------------------*/
127 /**
128  * \brief Initialize BLE advertising data.
129  * \param name Human readable device name that will be advertised
130  */
131 void
132 ble_advertising_init(const char *name)
133 {
134  uint32_t err_code;
135  ble_advdata_t advdata;
136  uint8_t flags = BLE_GAP_ADV_FLAG_BR_EDR_NOT_SUPPORTED;
137  ble_gap_conn_sec_mode_t sec_mode;
138 
139  BLE_GAP_CONN_SEC_MODE_SET_OPEN(&sec_mode);
140 
141  err_code = sd_ble_gap_device_name_set(&sec_mode, (const uint8_t *)name,
142  strlen(name));
143  APP_ERROR_CHECK(err_code);
144 
145  ble_uuid_t adv_uuids[] = {{BLE_UUID_IPSP_SERVICE, BLE_UUID_TYPE_BLE}};
146 
147  // Build and set advertising data.
148  memset(&advdata, 0, sizeof(advdata));
149 
150  advdata.name_type = BLE_ADVDATA_FULL_NAME;
151  advdata.flags = flags;
152  advdata.uuids_complete.uuid_cnt = sizeof(adv_uuids) / sizeof(adv_uuids[0]);
153  advdata.uuids_complete.p_uuids = adv_uuids;
154 
155  err_code = ble_advdata_set(&advdata, NULL);
156  APP_ERROR_CHECK(err_code);
157 
158  // Initialize advertising parameters (used when starting advertising).
159  memset(&m_adv_params, 0, sizeof(m_adv_params));
160 
161  m_adv_params.type = BLE_GAP_ADV_TYPE_ADV_IND;
162  m_adv_params.p_peer_addr = NULL; // Undirected advertisement.
163  m_adv_params.fp = BLE_GAP_ADV_FP_ANY;
165  m_adv_params.timeout = APP_ADV_TIMEOUT;
166 }
167 /*---------------------------------------------------------------------------*/
168 /**
169  * \brief Start BLE advertising.
170  */
171 void
173 {
174  uint32_t err_code;
175 
176  err_code = sd_ble_gap_adv_start(&m_adv_params);
177  APP_ERROR_CHECK(err_code);
178 
179  PRINTF("ble-core: advertising started\n");
180 }
181 /*---------------------------------------------------------------------------*/
182 /**
183  * \brief Print GAP address.
184  * \param addr a pointer to address
185  */
186 void
187 ble_gap_addr_print(const ble_gap_addr_t *addr)
188 {
189  unsigned int i;
190  for(i = 0; i < sizeof(addr->addr); i++) {
191  if(i > 0) {
192  PRINTF(":");
193  }PRINTF("%02x", addr->addr[i]);
194  }PRINTF(" (%d)", addr->addr_type);
195 }
196 /*---------------------------------------------------------------------------*/
197 /**
198  * \brief Function for handling the Application's BLE Stack events.
199  * \param[in] p_ble_evt Bluetooth stack event.
200  */
201 static void
202 on_ble_evt(ble_evt_t *p_ble_evt)
203 {
204  switch(p_ble_evt->header.evt_id) {
205  case BLE_GAP_EVT_CONNECTED:
206  PRINTF("ble-core: connected [handle:%d, peer: ", p_ble_evt->evt.gap_evt.conn_handle);
207  ble_gap_addr_print(&(p_ble_evt->evt.gap_evt.params.connected.peer_addr));
208  PRINTF("]\n");
209  sd_ble_gap_rssi_start(p_ble_evt->evt.gap_evt.conn_handle,
210  BLE_GAP_RSSI_THRESHOLD_INVALID,
211  0);
212  break;
213 
214  case BLE_GAP_EVT_DISCONNECTED:
215  PRINTF("ble-core: disconnected [handle:%d]\n", p_ble_evt->evt.gap_evt.conn_handle);
217  break;
218  default:
219  break;
220  }
221 }
222 /*---------------------------------------------------------------------------*/
223 /**
224  * \brief SoftDevice BLE event callback.
225  * \param[in] p_ble_evt Bluetooth stack event.
226  */
227 static void
228 ble_evt_dispatch(ble_evt_t *p_ble_evt)
229 {
230  ble_ipsp_evt_handler(p_ble_evt);
231  on_ble_evt(p_ble_evt);
232 }
233 /*---------------------------------------------------------------------------*/
234 /**
235  * @}
236  * @}
237  * @}
238  */
static void on_ble_evt(ble_evt_t *p_ble_evt)
Function for handling the Application's BLE Stack events.
Definition: ble-core.c:202
static void ble_evt_dispatch(ble_evt_t *p_ble_evt)
SoftDevice BLE event callback.
Definition: ble-core.c:228
static uip_ds6_addr_t * addr
Pointer to a router list entry.
Definition: uip-nd6.c:124
#define APP_ADV_ADV_INTERVAL
The advertising interval.
Definition: ble-core.c:70
static ble_gap_adv_params_t m_adv_params
Parameters to be passed to the stack when starting advertising.
Definition: ble-core.c:72
void ble_advertising_init(const char *name)
Initialize BLE advertising data.
Definition: ble-core.c:132
void ble_get_mac(uint8_t addr[8])
Return device EUI64 MAC address.
Definition: ble-core.c:116
#define NULL
The null pointer.
void ble_advertising_start(void)
Start BLE advertising.
Definition: ble-core.c:172
Basic BLE functions.
void ble_stack_init(void)
Initialize and enable the BLE stack.
Definition: ble-core.c:81
#define APP_ADV_TIMEOUT
Time for which the device must be advertising in non-connectable mode (in seconds).
Definition: ble-core.c:69
void ble_gap_addr_print(const ble_gap_addr_t *addr)
Print GAP address.
Definition: ble-core.c:187