|
19 | 19 | #include "ble/BLE.h"
|
20 | 20 | #include "ble/Gap.h"
|
21 | 21 | #include "ble/services/BatteryService.h"
|
| 22 | +#include "pretty_printer.h" |
22 | 23 |
|
23 |
| -DigitalOut led1(LED1, 1); |
| 24 | +static DigitalOut led1(LED1, 1); |
24 | 25 |
|
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"; |
27 | 27 |
|
28 |
| -static uint8_t batteryLevel = 50; |
29 |
| -static BatteryService* batteryServicePtr; |
| 28 | +static events::EventQueue event_queue(/* event count */ 16 * EVENTS_EVENT_SIZE); |
30 | 29 |
|
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) { } |
32 | 39 |
|
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); |
37 | 47 |
|
38 |
| -void updateSensorValue() { |
39 |
| - batteryLevel++; |
40 |
| - if (batteryLevel > 100) { |
41 |
| - batteryLevel = 20; |
| 48 | + _event_queue.dispatch_forever(); |
42 | 49 | }
|
43 | 50 |
|
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 | + } |
46 | 58 |
|
47 |
| -void blinkCallback(void) |
48 |
| -{ |
49 |
| - led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */ |
| 59 | + print_mac_address(); |
50 | 60 |
|
51 |
| - BLE &ble = BLE::Instance(); |
52 |
| - if (ble.gap().getState().connected) { |
53 |
| - eventQueue.call(updateSensorValue); |
| 61 | + start_advertising(); |
54 | 62 | }
|
55 |
| -} |
56 | 63 |
|
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 */ |
64 | 66 |
|
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 | + } |
74 | 106 | }
|
75 |
| - printf("%02x\r\n", address[0]); |
76 |
| -} |
77 | 107 |
|
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 | + } |
85 | 114 |
|
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 | + } |
90 | 117 | }
|
91 | 118 |
|
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; |
95 | 121 | }
|
96 | 122 |
|
97 |
| - ble.gap().onDisconnection(disconnectionCallback); |
| 123 | +private: |
| 124 | + /* Event handler */ |
98 | 125 |
|
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 | + } |
101 | 129 |
|
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; |
109 | 133 |
|
110 |
| - printMacAddress(); |
111 |
| -} |
| 134 | + UUID _battery_uuid; |
112 | 135 |
|
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)); |
116 | 146 | }
|
117 | 147 |
|
118 | 148 | int main()
|
119 | 149 | {
|
120 |
| - eventQueue.call_every(500, blinkCallback); |
121 |
| - |
122 | 150 | BLE &ble = BLE::Instance();
|
123 |
| - ble.onEventsToProcess(scheduleBleEventsProcessing); |
124 |
| - ble.init(bleInitComplete); |
| 151 | + ble.onEventsToProcess(schedule_ble_events); |
125 | 152 |
|
126 |
| - eventQueue.dispatch_forever(); |
| 153 | + BatteryDemo demo(ble, event_queue); |
| 154 | + demo.start(); |
127 | 155 |
|
128 | 156 | return 0;
|
129 | 157 | }
|
0 commit comments