This is a basic code to be used for Sequana BLE Lab exercises.

Committer:
lru
Date:
Thu Mar 14 10:00:59 2019 +0000
Revision:
1:7dd41f0bd204
Parent:
0:ff033dfc838b
Child:
2:06e62a299a74
Modified default device name to "ChangeMe"

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lru 0:ff033dfc838b 1 /* mbed Microcontroller Library
lru 0:ff033dfc838b 2 * Copyright (c) 2006-2015 ARM Limited
lru 0:ff033dfc838b 3 * Copyright (c) 2017-2019 Future Electronics
lru 0:ff033dfc838b 4 *
lru 0:ff033dfc838b 5 * Licensed under the Apache License, Version 2.0 (the "License");
lru 0:ff033dfc838b 6 * you may not use this file except in compliance with the License.
lru 0:ff033dfc838b 7 * You may obtain a copy of the License at
lru 0:ff033dfc838b 8 *
lru 0:ff033dfc838b 9 * http://www.apache.org/licenses/LICENSE-2.0
lru 0:ff033dfc838b 10 *
lru 0:ff033dfc838b 11 * Unless required by applicable law or agreed to in writing, software
lru 0:ff033dfc838b 12 * distributed under the License is distributed on an "AS IS" BASIS,
lru 0:ff033dfc838b 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
lru 0:ff033dfc838b 14 * See the License for the specific language governing permissions and
lru 0:ff033dfc838b 15 * limitations under the License.
lru 0:ff033dfc838b 16 */
lru 0:ff033dfc838b 17
lru 0:ff033dfc838b 18 #include <events/mbed_events.h>
lru 0:ff033dfc838b 19 #include <mbed.h>
lru 0:ff033dfc838b 20 #include "ble/BLE.h"
lru 0:ff033dfc838b 21 #include "pretty_printer.h"
lru 0:ff033dfc838b 22
lru 0:ff033dfc838b 23 #include <mbed.h>
lru 0:ff033dfc838b 24 #include <vector>
lru 0:ff033dfc838b 25 #include "events/mbed_events.h"
lru 0:ff033dfc838b 26 #include "ble/BLE.h"
lru 0:ff033dfc838b 27 #include "ble/gap/Gap.h"
lru 0:ff033dfc838b 28 #include "SequanaPrimaryService.h"
lru 0:ff033dfc838b 29 #include "Kx64.h"
lru 0:ff033dfc838b 30
lru 0:ff033dfc838b 31 using namespace sequana;
lru 0:ff033dfc838b 32
lru 1:7dd41f0bd204 33 const static char DEVICE_NAME[] = "ChangeMe";
lru 0:ff033dfc838b 34
lru 0:ff033dfc838b 35 static sequana::PrimaryService *sequana_service_ptr;
lru 0:ff033dfc838b 36
lru 0:ff033dfc838b 37 static EventQueue event_queue(/* event count */ 64 * EVENTS_EVENT_SIZE);
lru 0:ff033dfc838b 38
lru 0:ff033dfc838b 39 /* Buses and I/O */
lru 0:ff033dfc838b 40 SPI spi1(SPI_MOSI, SPI_MISO, SPI_CLK);
lru 0:ff033dfc838b 41
lru 0:ff033dfc838b 42 /* Sensor declarations */
lru 0:ff033dfc838b 43 Kx64Sensor kx64(spi1, P9_5);
lru 0:ff033dfc838b 44
lru 0:ff033dfc838b 45 class SequanaDemo : ble::Gap::EventHandler {
lru 0:ff033dfc838b 46 public:
lru 0:ff033dfc838b 47 SequanaDemo(BLE &ble, events::EventQueue &event_queue, sequana::PrimaryService& primary_service) :
lru 0:ff033dfc838b 48 _ble(ble),
lru 0:ff033dfc838b 49 _event_queue(event_queue),
lru 0:ff033dfc838b 50 _led1(LED1, 1),
lru 0:ff033dfc838b 51 _connected(false),
lru 0:ff033dfc838b 52 _primary_uuid(sequana::PrimaryService::UUID_SEQUANA_PRIMARY_SERVICE),
lru 0:ff033dfc838b 53 _primary_service(primary_service),
lru 0:ff033dfc838b 54 _adv_data_builder(_adv_buffer) { }
lru 0:ff033dfc838b 55
lru 0:ff033dfc838b 56 void start() {
lru 0:ff033dfc838b 57 _ble.gap().setEventHandler(this);
lru 0:ff033dfc838b 58
lru 0:ff033dfc838b 59 _ble.init(this, &SequanaDemo::on_init_complete);
lru 0:ff033dfc838b 60 }
lru 0:ff033dfc838b 61
lru 0:ff033dfc838b 62 private:
lru 0:ff033dfc838b 63 /** Callback triggered when the ble initialization process has finished */
lru 0:ff033dfc838b 64 void on_init_complete(BLE::InitializationCompleteCallbackContext *params) {
lru 0:ff033dfc838b 65 if (params->error != BLE_ERROR_NONE) {
lru 0:ff033dfc838b 66 printf("BLE initialization failed\n");
lru 0:ff033dfc838b 67 return;
lru 0:ff033dfc838b 68 }
lru 0:ff033dfc838b 69
lru 0:ff033dfc838b 70 print_mac_address();
lru 0:ff033dfc838b 71
lru 0:ff033dfc838b 72 _event_queue.call_every(500, this, &SequanaDemo::blink);
lru 0:ff033dfc838b 73
lru 0:ff033dfc838b 74 start_advertising();
lru 0:ff033dfc838b 75 }
lru 0:ff033dfc838b 76
lru 0:ff033dfc838b 77 void start_advertising() {
lru 0:ff033dfc838b 78 /* Create advertising parameters and payload */
lru 0:ff033dfc838b 79
lru 0:ff033dfc838b 80 ble::AdvertisingParameters adv_parameters(
lru 0:ff033dfc838b 81 ble::advertising_type_t::CONNECTABLE_UNDIRECTED,
lru 0:ff033dfc838b 82 ble::adv_interval_t(ble::millisecond_t(1000))
lru 0:ff033dfc838b 83 );
lru 0:ff033dfc838b 84
lru 0:ff033dfc838b 85 _adv_data_builder.setFlags();
lru 0:ff033dfc838b 86 _adv_data_builder.setLocalServiceList(mbed::make_Span(&_primary_uuid, 1));
lru 0:ff033dfc838b 87 _adv_data_builder.setName(DEVICE_NAME);
lru 0:ff033dfc838b 88
lru 0:ff033dfc838b 89 /* Setup advertising */
lru 0:ff033dfc838b 90
lru 0:ff033dfc838b 91 ble_error_t error = _ble.gap().setAdvertisingParameters(
lru 0:ff033dfc838b 92 ble::LEGACY_ADVERTISING_HANDLE,
lru 0:ff033dfc838b 93 adv_parameters
lru 0:ff033dfc838b 94 );
lru 0:ff033dfc838b 95
lru 0:ff033dfc838b 96 if (error) {
lru 0:ff033dfc838b 97 printf("_ble.gap().setAdvertisingParameters() failed\n");
lru 0:ff033dfc838b 98 return;
lru 0:ff033dfc838b 99 }
lru 0:ff033dfc838b 100
lru 0:ff033dfc838b 101 error = _ble.gap().setAdvertisingPayload(
lru 0:ff033dfc838b 102 ble::LEGACY_ADVERTISING_HANDLE,
lru 0:ff033dfc838b 103 _adv_data_builder.getAdvertisingData()
lru 0:ff033dfc838b 104 );
lru 0:ff033dfc838b 105
lru 0:ff033dfc838b 106 if (error) {
lru 0:ff033dfc838b 107 printf("_ble.gap().setAdvertisingPayload() failed\n");
lru 0:ff033dfc838b 108 return;
lru 0:ff033dfc838b 109 }
lru 0:ff033dfc838b 110
lru 0:ff033dfc838b 111 /* Start advertising */
lru 0:ff033dfc838b 112
lru 0:ff033dfc838b 113 error = _ble.gap().startAdvertising(ble::LEGACY_ADVERTISING_HANDLE);
lru 0:ff033dfc838b 114
lru 0:ff033dfc838b 115 if (error) {
lru 0:ff033dfc838b 116 printf("_ble.gap().startAdvertising() failed\n");
lru 0:ff033dfc838b 117 return;
lru 0:ff033dfc838b 118 }
lru 0:ff033dfc838b 119 }
lru 0:ff033dfc838b 120
lru 0:ff033dfc838b 121 void blink(void) {
lru 0:ff033dfc838b 122 /* LED will be on when a client is connected or will blink while advertising */
lru 0:ff033dfc838b 123 if (_connected) {
lru 0:ff033dfc838b 124 _led1 = 1;
lru 0:ff033dfc838b 125 } else {
lru 0:ff033dfc838b 126 _led1 = !_led1;
lru 0:ff033dfc838b 127 }
lru 0:ff033dfc838b 128 }
lru 0:ff033dfc838b 129
lru 0:ff033dfc838b 130 private:
lru 0:ff033dfc838b 131 /* Event handler */
lru 0:ff033dfc838b 132
lru 0:ff033dfc838b 133 void onDisconnectionComplete(const ble::DisconnectionCompleteEvent&) {
lru 0:ff033dfc838b 134 _ble.gap().startAdvertising(ble::LEGACY_ADVERTISING_HANDLE);
lru 0:ff033dfc838b 135 _connected = false;
lru 0:ff033dfc838b 136 }
lru 0:ff033dfc838b 137
lru 0:ff033dfc838b 138 virtual void onConnectionComplete(const ble::ConnectionCompleteEvent &event) {
lru 0:ff033dfc838b 139 if (event.getStatus() == BLE_ERROR_NONE) {
lru 0:ff033dfc838b 140 _connected = true;
lru 0:ff033dfc838b 141 }
lru 0:ff033dfc838b 142 }
lru 0:ff033dfc838b 143
lru 0:ff033dfc838b 144 private:
lru 0:ff033dfc838b 145 BLE &_ble;
lru 0:ff033dfc838b 146 events::EventQueue &_event_queue;
lru 0:ff033dfc838b 147 DigitalOut _led1;
lru 0:ff033dfc838b 148 bool _connected;
lru 0:ff033dfc838b 149 UUID _primary_uuid;
lru 0:ff033dfc838b 150 sequana::PrimaryService &_primary_service;
lru 0:ff033dfc838b 151 uint8_t _adv_buffer[ble::LEGACY_ADVERTISING_MAX_SIZE];
lru 0:ff033dfc838b 152 ble::AdvertisingDataBuilder _adv_data_builder;
lru 0:ff033dfc838b 153 };
lru 0:ff033dfc838b 154
lru 0:ff033dfc838b 155
lru 0:ff033dfc838b 156 /** Schedule processing of events from the BLE middleware in the event queue. */
lru 0:ff033dfc838b 157 void schedule_ble_events(BLE::OnEventsToProcessCallbackContext *context) {
lru 0:ff033dfc838b 158 event_queue.call(Callback<void()>(&context->ble, &BLE::processEvents));
lru 0:ff033dfc838b 159 }
lru 0:ff033dfc838b 160
lru 0:ff033dfc838b 161 int main()
lru 0:ff033dfc838b 162 {
lru 0:ff033dfc838b 163 printf("Application processor started.\n\n");
lru 0:ff033dfc838b 164
lru 0:ff033dfc838b 165 /* Initialize bus(es) (placeholder) */
lru 0:ff033dfc838b 166 spi1.frequency(1000000);
lru 0:ff033dfc838b 167
lru 0:ff033dfc838b 168 /* Setup BLE interface */
lru 0:ff033dfc838b 169 BLE &ble = BLE::Instance();
lru 0:ff033dfc838b 170 ble.onEventsToProcess(schedule_ble_events);
lru 0:ff033dfc838b 171
lru 0:ff033dfc838b 172 /* Setup primary service (placeholder). */
lru 0:ff033dfc838b 173 sequana_service_ptr = new sequana::PrimaryService(ble, kx64);
lru 0:ff033dfc838b 174
lru 0:ff033dfc838b 175 /* Start BLE / demo application */
lru 0:ff033dfc838b 176 SequanaDemo demo(ble, event_queue, *sequana_service_ptr);
lru 0:ff033dfc838b 177 demo.start();
lru 0:ff033dfc838b 178
lru 0:ff033dfc838b 179 printf("BLE started (%s).\n\n", DEVICE_NAME);
lru 0:ff033dfc838b 180
lru 0:ff033dfc838b 181 /* Process all events */
lru 0:ff033dfc838b 182 event_queue.dispatch_forever();
lru 0:ff033dfc838b 183
lru 0:ff033dfc838b 184 return 0;
lru 0:ff033dfc838b 185 }
lru 0:ff033dfc838b 186