Skip to content

Commit 5f467b6

Browse files
author
Donatien Garnier
committed
Merge branch 'pan--gatt_server_example'
2 parents c1139fb + e0296ae commit 5f467b6

File tree

6 files changed

+639
-0
lines changed

6 files changed

+639
-0
lines changed

BLE_GattServer/.mbed

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
ROOT=.
2+
TARGET=NRF52_DK
3+
TOOLCHAIN=GCC_ARM

BLE_GattServer/BLEProcess.h

Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2017 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef GATT_SERVER_EXAMPLE_BLE_PROCESS_H_
18+
#define GATT_SERVER_EXAMPLE_BLE_PROCESS_H_
19+
20+
#include <stdint.h>
21+
#include <stdio.h>
22+
23+
#include "events/Eventqueue.h"
24+
#include "platform/Callback.h"
25+
#include "platform/NonCopyable.h"
26+
27+
#include "ble/BLE.h"
28+
#include "ble/Gap.h"
29+
#include "ble/GapAdvertisingParams.h"
30+
#include "ble/GapAdvertisingData.h"
31+
#include "ble/FunctionPointerWithContext.h"
32+
33+
/**
34+
* Handle initialization adn shutdown of the BLE Instance.
35+
*
36+
* Setup advertising payload and manage advertising state.
37+
* Delegate to GattClientProcess once the connection is established.
38+
*/
39+
class BLEProcess : private mbed::NonCopyable<BLEProcess> {
40+
public:
41+
/**
42+
* Construct a BLEProcess from an event queue and a ble interface.
43+
*
44+
* Call start() to initiate ble processing.
45+
*/
46+
BLEProcess(events::EventQueue &event_queue, BLE &ble_interface) :
47+
_event_queue(event_queue),
48+
_ble_interface(ble_interface),
49+
_post_init_cb() {
50+
}
51+
52+
~BLEProcess()
53+
{
54+
stop();
55+
}
56+
57+
/**
58+
* Subscription to the ble interface initialization event.
59+
*
60+
* @param[in] cb The callback object that will be called when the ble
61+
* interface is initialized.
62+
*/
63+
void on_init(mbed::Callback<void(BLE&, events::EventQueue&)> cb)
64+
{
65+
_post_init_cb = cb;
66+
}
67+
68+
/**
69+
* Initialize the ble interface, configure it and start advertising.
70+
*/
71+
bool start()
72+
{
73+
printf("Ble process started.\r\n");
74+
75+
if (_ble_interface.hasInitialized()) {
76+
printf("Error: the ble instance has already been initialized.\r\n");
77+
return false;
78+
}
79+
80+
_ble_interface.onEventsToProcess(
81+
makeFunctionPointer(this, &BLEProcess::schedule_ble_events)
82+
);
83+
84+
ble_error_t error = _ble_interface.init(
85+
this, &BLEProcess::when_init_complete
86+
);
87+
88+
if (error) {
89+
printf("Error: %u returned by BLE::init.\r\n", error);
90+
return false;
91+
}
92+
93+
return true;
94+
}
95+
96+
/**
97+
* Close existing connections and stop the process.
98+
*/
99+
void stop()
100+
{
101+
if (_ble_interface.hasInitialized()) {
102+
_ble_interface.shutdown();
103+
printf("Ble process stopped.");
104+
}
105+
}
106+
107+
private:
108+
109+
/**
110+
* Schedule processing of events from the BLE middleware in the event queue.
111+
*/
112+
void schedule_ble_events(BLE::OnEventsToProcessCallbackContext *event)
113+
{
114+
_event_queue.call(mbed::callback(&event->ble, &BLE::processEvents));
115+
}
116+
117+
/**
118+
* Sets up adverting payload and start advertising.
119+
*
120+
* This function is invoked when the ble interface is initialized.
121+
*/
122+
void when_init_complete(BLE::InitializationCompleteCallbackContext *event)
123+
{
124+
if (event->error) {
125+
printf("Error %u during the initialization\r\n", event->error);
126+
return;
127+
}
128+
printf("Ble instance initialized\r\n");
129+
130+
Gap &gap = _ble_interface.gap();
131+
ble_error_t error = gap.setAdvertisingPayload(make_advertising_data());
132+
if (error) {
133+
printf("Error %u during gap.setAdvertisingPayload\r\n", error);
134+
return;
135+
}
136+
137+
gap.setAdvertisingParams(make_advertising_params());
138+
139+
gap.onConnection(this, &BLEProcess::when_connection);
140+
gap.onDisconnection(this, &BLEProcess::when_disconnection);
141+
142+
start_advertising();
143+
144+
if (_post_init_cb) {
145+
_post_init_cb(_ble_interface, _event_queue);
146+
}
147+
}
148+
149+
void when_connection(const Gap::ConnectionCallbackParams_t *connection_event)
150+
{
151+
printf("Connected.\r\n");
152+
}
153+
154+
void when_disconnection(const Gap::DisconnectionCallbackParams_t *event)
155+
{
156+
printf("Disconnected.\r\n");
157+
start_advertising();
158+
}
159+
160+
void start_advertising(void)
161+
{
162+
ble_error_t error = _ble_interface.gap().startAdvertising();
163+
if (error) {
164+
printf("Error %u during gap.startAdvertising.\r\n", error);
165+
return;
166+
} else {
167+
printf("Advertising started.\r\n");
168+
}
169+
}
170+
171+
static GapAdvertisingData make_advertising_data(void)
172+
{
173+
static const uint8_t device_name[] = "GattServer";
174+
GapAdvertisingData advertising_data;
175+
176+
// add advertising flags
177+
advertising_data.addFlags(
178+
GapAdvertisingData::LE_GENERAL_DISCOVERABLE |
179+
GapAdvertisingData::BREDR_NOT_SUPPORTED
180+
);
181+
182+
// add device name
183+
advertising_data.addData(
184+
GapAdvertisingData::COMPLETE_LOCAL_NAME,
185+
device_name,
186+
sizeof(device_name)
187+
);
188+
189+
return advertising_data;
190+
}
191+
192+
static GapAdvertisingParams make_advertising_params(void)
193+
{
194+
return GapAdvertisingParams(
195+
/* type */ GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED,
196+
/* interval */ GapAdvertisingParams::MSEC_TO_ADVERTISEMENT_DURATION_UNITS(500),
197+
/* timeout */ 0
198+
);
199+
}
200+
201+
events::EventQueue &_event_queue;
202+
BLE &_ble_interface;
203+
mbed::Callback<void(BLE&, events::EventQueue&)> _post_init_cb;
204+
};
205+
206+
#endif /* GATT_SERVER_EXAMPLE_BLE_PROCESS_H_ */

0 commit comments

Comments
 (0)