Skip to content

Commit 257c1d3

Browse files
authored
Merge branch 'master' into master
2 parents bbfb070 + 6b40605 commit 257c1d3

File tree

196 files changed

+9183
-1311
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

196 files changed

+9183
-1311
lines changed

.gitignore

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,19 @@ build/
3131
yotta_modules/
3232
yotta_targets/
3333
.DS_Store
34+
35+
# clion
36+
.idea/
37+
cmake-build-*/
38+
CMakeLists.txt
39+
40+
# exporters
41+
GettingStarted.html
42+
43+
# mbed build system
44+
mbed-os/
45+
mbed_settings.py
46+
mbed_config.h
47+
*.pyc
48+
TARGET_CORDIO_BLUENRG/
49+

BLE_BatteryLevel/mbed_app.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@
1111
"DISCO_L475VG_IOT01A": {
1212
"target.features_add": ["BLE"],
1313
"target.extra_labels_add": ["CORDIO", "CORDIO_BLUENRG"]
14+
},
15+
"NRF52840_DK": {
16+
"target.features_add": ["BLE"],
17+
"target.extra_labels_add": ["CORDIO", "CORDIO_LL", "SOFTDEVICE_NONE", "NORDIC_CORDIO"],
18+
"target.extra_labels_remove": ["SOFTDEVICE_COMMON", "SOFTDEVICE_S140_FULL", "NORDIC_SOFTDEVICE"]
19+
},
20+
"NRF52_DK": {
21+
"target.features_add": ["BLE"],
22+
"target.extra_labels_add": ["CORDIO", "CORDIO_LL", "SOFTDEVICE_NONE", "NORDIC_CORDIO"],
23+
"target.extra_labels_remove": ["SOFTDEVICE_COMMON", "SOFTDEVICE_S132_FULL", "NORDIC_SOFTDEVICE"]
1424
}
1525
}
1626
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
https://github.com/ARMmbed/cordio-ble-x-nucleo-idb0xa1/#51f60dfea514e35e5aa13c8e24005ecadc24a9f5
1+
https://github.com/ARMmbed/cordio-ble-x-nucleo-idb0xa1/#811f3fea7aa8083c0bbf378e1b51a8b131d7efcc

BLE_BatteryLevel/source/main.cpp

Lines changed: 103 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -19,111 +19,139 @@
1919
#include "ble/BLE.h"
2020
#include "ble/Gap.h"
2121
#include "ble/services/BatteryService.h"
22+
#include "pretty_printer.h"
2223

23-
DigitalOut led1(LED1, 1);
24+
static DigitalOut led1(LED1, 1);
2425

25-
const static char DEVICE_NAME[] = "BATTERY";
26-
static const uint16_t uuid16_list[] = {GattService::UUID_BATTERY_SERVICE};
26+
const static char DEVICE_NAME[] = "BATTERY";
2727

28-
static uint8_t batteryLevel = 50;
29-
static BatteryService* batteryServicePtr;
28+
static events::EventQueue event_queue(/* event count */ 16 * EVENTS_EVENT_SIZE);
3029

31-
static EventQueue eventQueue(/* event count */ 16 * EVENTS_EVENT_SIZE);
30+
class BatteryDemo : ble::Gap::EventHandler {
31+
public:
32+
BatteryDemo(BLE &ble, events::EventQueue &event_queue) :
33+
_ble(ble),
34+
_event_queue(event_queue),
35+
_battery_uuid(GattService::UUID_BATTERY_SERVICE),
36+
_battery_level(50),
37+
_battery_service(ble, _battery_level),
38+
_adv_data_builder(_adv_buffer) { }
3239

33-
void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
34-
{
35-
BLE::Instance().gap().startAdvertising();
36-
}
40+
void start() {
41+
_ble.gap().setEventHandler(this);
42+
43+
_ble.init(this, &BatteryDemo::on_init_complete);
44+
45+
_event_queue.call_every(500, this, &BatteryDemo::blink);
46+
_event_queue.call_every(1000, this, &BatteryDemo::update_sensor_value);
3747

38-
void updateSensorValue() {
39-
batteryLevel++;
40-
if (batteryLevel > 100) {
41-
batteryLevel = 20;
48+
_event_queue.dispatch_forever();
4249
}
4350

44-
batteryServicePtr->updateBatteryLevel(batteryLevel);
45-
}
51+
private:
52+
/** Callback triggered when the ble initialization process has finished */
53+
void on_init_complete(BLE::InitializationCompleteCallbackContext *params) {
54+
if (params->error != BLE_ERROR_NONE) {
55+
print_error(params->error, "Ble initialization failed.");
56+
return;
57+
}
4658

47-
void blinkCallback(void)
48-
{
49-
led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
59+
print_mac_address();
5060

51-
BLE &ble = BLE::Instance();
52-
if (ble.gap().getState().connected) {
53-
eventQueue.call(updateSensorValue);
61+
start_advertising();
5462
}
55-
}
5663

57-
/**
58-
* This function is called when the ble initialization process has failled
59-
*/
60-
void onBleInitError(BLE &ble, ble_error_t error)
61-
{
62-
/* Initialization error handling should go here */
63-
}
64+
void start_advertising() {
65+
/* Create advertising parameters and payload */
6466

65-
void printMacAddress()
66-
{
67-
/* Print out device MAC address to the console*/
68-
Gap::AddressType_t addr_type;
69-
Gap::Address_t address;
70-
BLE::Instance().gap().getAddress(&addr_type, address);
71-
printf("DEVICE MAC ADDRESS: ");
72-
for (int i = 5; i >= 1; i--){
73-
printf("%02x:", address[i]);
67+
ble::AdvertisingParameters adv_parameters(
68+
ble::advertising_type_t::CONNECTABLE_UNDIRECTED,
69+
ble::adv_interval_t(ble::millisecond_t(1000))
70+
);
71+
72+
_adv_data_builder.setFlags();
73+
_adv_data_builder.setLocalServiceList(mbed::make_Span(&_battery_uuid, 1));
74+
_adv_data_builder.setName(DEVICE_NAME);
75+
76+
/* Setup advertising */
77+
78+
ble_error_t error = _ble.gap().setAdvertisingParameters(
79+
ble::LEGACY_ADVERTISING_HANDLE,
80+
adv_parameters
81+
);
82+
83+
if (error) {
84+
print_error(error, "_ble.gap().setAdvertisingParameters() failed");
85+
return;
86+
}
87+
88+
error = _ble.gap().setAdvertisingPayload(
89+
ble::LEGACY_ADVERTISING_HANDLE,
90+
_adv_data_builder.getAdvertisingData()
91+
);
92+
93+
if (error) {
94+
print_error(error, "_ble.gap().setAdvertisingPayload() failed");
95+
return;
96+
}
97+
98+
/* Start advertising */
99+
100+
error = _ble.gap().startAdvertising(ble::LEGACY_ADVERTISING_HANDLE);
101+
102+
if (error) {
103+
print_error(error, "_ble.gap().startAdvertising() failed");
104+
return;
105+
}
74106
}
75-
printf("%02x\r\n", address[0]);
76-
}
77107

78-
/**
79-
* Callback triggered when the ble initialization process has finished
80-
*/
81-
void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
82-
{
83-
BLE& ble = params->ble;
84-
ble_error_t error = params->error;
108+
void update_sensor_value() {
109+
if (_ble.gap().getState().connected) {
110+
_battery_level++;
111+
if (_battery_level > 100) {
112+
_battery_level = 20;
113+
}
85114

86-
if (error != BLE_ERROR_NONE) {
87-
/* In case of error, forward the error handling to onBleInitError */
88-
onBleInitError(ble, error);
89-
return;
115+
_battery_service.updateBatteryLevel(_battery_level);
116+
}
90117
}
91118

92-
/* Ensure that it is the default instance of BLE */
93-
if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
94-
return;
119+
void blink(void) {
120+
led1 = !led1;
95121
}
96122

97-
ble.gap().onDisconnection(disconnectionCallback);
123+
private:
124+
/* Event handler */
98125

99-
/* Setup primary service */
100-
batteryServicePtr = new BatteryService(ble, batteryLevel);
126+
void onDisconnectionComplete(const ble::DisconnectionCompleteEvent&) {
127+
_ble.gap().startAdvertising(ble::LEGACY_ADVERTISING_HANDLE);
128+
}
101129

102-
/* Setup advertising */
103-
ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
104-
ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *) uuid16_list, sizeof(uuid16_list));
105-
ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *) DEVICE_NAME, sizeof(DEVICE_NAME));
106-
ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
107-
ble.gap().setAdvertisingInterval(1000); /* 1000ms */
108-
ble.gap().startAdvertising();
130+
private:
131+
BLE &_ble;
132+
events::EventQueue &_event_queue;
109133

110-
printMacAddress();
111-
}
134+
UUID _battery_uuid;
112135

113-
void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) {
114-
BLE &ble = BLE::Instance();
115-
eventQueue.call(Callback<void()>(&ble, &BLE::processEvents));
136+
uint8_t _battery_level;
137+
BatteryService _battery_service;
138+
139+
uint8_t _adv_buffer[ble::LEGACY_ADVERTISING_MAX_SIZE];
140+
ble::AdvertisingDataBuilder _adv_data_builder;
141+
};
142+
143+
/** Schedule processing of events from the BLE middleware in the event queue. */
144+
void schedule_ble_events(BLE::OnEventsToProcessCallbackContext *context) {
145+
event_queue.call(Callback<void()>(&context->ble, &BLE::processEvents));
116146
}
117147

118148
int main()
119149
{
120-
eventQueue.call_every(500, blinkCallback);
121-
122150
BLE &ble = BLE::Instance();
123-
ble.onEventsToProcess(scheduleBleEventsProcessing);
124-
ble.init(bleInitComplete);
151+
ble.onEventsToProcess(schedule_ble_events);
125152

126-
eventQueue.dispatch_forever();
153+
BatteryDemo demo(ble, event_queue);
154+
demo.start();
127155

128156
return 0;
129157
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2018 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+
#include <mbed.h>
18+
#include "ble/BLE.h"
19+
20+
inline void print_error(ble_error_t error, const char* msg)
21+
{
22+
printf("%s: ", msg);
23+
switch(error) {
24+
case BLE_ERROR_NONE:
25+
printf("BLE_ERROR_NONE: No error");
26+
break;
27+
case BLE_ERROR_BUFFER_OVERFLOW:
28+
printf("BLE_ERROR_BUFFER_OVERFLOW: The requested action would cause a buffer overflow and has been aborted");
29+
break;
30+
case BLE_ERROR_NOT_IMPLEMENTED:
31+
printf("BLE_ERROR_NOT_IMPLEMENTED: Requested a feature that isn't yet implement or isn't supported by the target HW");
32+
break;
33+
case BLE_ERROR_PARAM_OUT_OF_RANGE:
34+
printf("BLE_ERROR_PARAM_OUT_OF_RANGE: One of the supplied parameters is outside the valid range");
35+
break;
36+
case BLE_ERROR_INVALID_PARAM:
37+
printf("BLE_ERROR_INVALID_PARAM: One of the supplied parameters is invalid");
38+
break;
39+
case BLE_STACK_BUSY:
40+
printf("BLE_STACK_BUSY: The stack is busy");
41+
break;
42+
case BLE_ERROR_INVALID_STATE:
43+
printf("BLE_ERROR_INVALID_STATE: Invalid state");
44+
break;
45+
case BLE_ERROR_NO_MEM:
46+
printf("BLE_ERROR_NO_MEM: Out of Memory");
47+
break;
48+
case BLE_ERROR_OPERATION_NOT_PERMITTED:
49+
printf("BLE_ERROR_OPERATION_NOT_PERMITTED");
50+
break;
51+
case BLE_ERROR_INITIALIZATION_INCOMPLETE:
52+
printf("BLE_ERROR_INITIALIZATION_INCOMPLETE");
53+
break;
54+
case BLE_ERROR_ALREADY_INITIALIZED:
55+
printf("BLE_ERROR_ALREADY_INITIALIZED");
56+
break;
57+
case BLE_ERROR_UNSPECIFIED:
58+
printf("BLE_ERROR_UNSPECIFIED: Unknown error");
59+
break;
60+
case BLE_ERROR_INTERNAL_STACK_FAILURE:
61+
printf("BLE_ERROR_INTERNAL_STACK_FAILURE: internal stack faillure");
62+
break;
63+
}
64+
printf("\r\n");
65+
}
66+
67+
/** print device address to the terminal */
68+
inline void print_address(const Gap::Address_t &addr)
69+
{
70+
printf("%02x:%02x:%02x:%02x:%02x:%02x\r\n",
71+
addr[5], addr[4], addr[3], addr[2], addr[1], addr[0]);
72+
}
73+
74+
inline void print_mac_address()
75+
{
76+
/* Print out device MAC address to the console*/
77+
Gap::AddressType_t addr_type;
78+
Gap::Address_t address;
79+
BLE::Instance().gap().getAddress(&addr_type, address);
80+
printf("DEVICE MAC ADDRESS: ");
81+
print_address(address);
82+
}
83+
84+
inline const char* phy_to_string(Gap::Phy_t phy) {
85+
switch(phy.value()) {
86+
case Gap::Phy_t::LE_1M:
87+
return "LE 1M";
88+
case Gap::Phy_t::LE_2M:
89+
return "LE 2M";
90+
case Gap::Phy_t::LE_CODED:
91+
return "LE coded";
92+
default:
93+
return "invalid PHY";
94+
}
95+
}

BLE_Beacon/mbed-os.lib

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
https://github.com/ARMmbed/mbed-os/#c966348d3f9ca80843be7cdc9b748f06ea73ced0
1+
https://github.com/ARMmbed/mbed-os/#c966348d3f9ca80843be7cdc9b748f06ea73ced0

BLE_Beacon/mbed_app.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@
1111
"DISCO_L475VG_IOT01A": {
1212
"target.features_add": ["BLE"],
1313
"target.extra_labels_add": ["CORDIO", "CORDIO_BLUENRG"]
14+
},
15+
"NRF52840_DK": {
16+
"target.features_add": ["BLE"],
17+
"target.extra_labels_add": ["CORDIO", "CORDIO_LL", "SOFTDEVICE_NONE", "NORDIC_CORDIO"],
18+
"target.extra_labels_remove": ["SOFTDEVICE_COMMON", "SOFTDEVICE_S140_FULL", "NORDIC_SOFTDEVICE"]
19+
},
20+
"NRF52_DK": {
21+
"target.features_add": ["BLE"],
22+
"target.extra_labels_add": ["CORDIO", "CORDIO_LL", "SOFTDEVICE_NONE", "NORDIC_CORDIO"],
23+
"target.extra_labels_remove": ["SOFTDEVICE_COMMON", "SOFTDEVICE_S132_FULL", "NORDIC_SOFTDEVICE"]
1424
}
1525
}
1626
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
https://github.com/ARMmbed/cordio-ble-x-nucleo-idb0xa1/#51f60dfea514e35e5aa13c8e24005ecadc24a9f5
1+
https://github.com/ARMmbed/cordio-ble-x-nucleo-idb0xa1/#811f3fea7aa8083c0bbf378e1b51a8b131d7efcc

0 commit comments

Comments
 (0)