Kris Scholte Lubberink
/
SSS_Ble
Ble for smart sOlutions
source/main.cpp@1:9fc54848a198, 2019-03-25 (annotated)
- Committer:
- krissl
- Date:
- Mon Mar 25 18:14:12 2019 +0000
- Revision:
- 1:9fc54848a198
- Parent:
- 0:fe5389573740
- Child:
- 3:f594022fe519
Ble;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
krissl | 0:fe5389573740 | 1 | /* mbed Microcontroller Library |
krissl | 0:fe5389573740 | 2 | * Copyright (c) 2006-2014 ARM Limited |
krissl | 0:fe5389573740 | 3 | * |
krissl | 0:fe5389573740 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
krissl | 0:fe5389573740 | 5 | * you may not use this file except in compliance with the License. |
krissl | 0:fe5389573740 | 6 | * You may obtain a copy of the License at |
krissl | 0:fe5389573740 | 7 | * |
krissl | 0:fe5389573740 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
krissl | 0:fe5389573740 | 9 | * |
krissl | 0:fe5389573740 | 10 | * Unless required by applicable law or agreed to in writing, software |
krissl | 0:fe5389573740 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
krissl | 0:fe5389573740 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
krissl | 0:fe5389573740 | 13 | * See the License for the specific language governing permissions and |
krissl | 0:fe5389573740 | 14 | * limitations under the License. |
krissl | 0:fe5389573740 | 15 | */ |
krissl | 0:fe5389573740 | 16 | |
krissl | 0:fe5389573740 | 17 | #include <events/mbed_events.h> |
krissl | 0:fe5389573740 | 18 | #include <mbed.h> |
krissl | 0:fe5389573740 | 19 | #include "ble/BLE.h" |
krissl | 0:fe5389573740 | 20 | #include "ble/Gap.h" |
krissl | 0:fe5389573740 | 21 | #include "GattCallbackParamTypes.h" |
krissl | 1:9fc54848a198 | 22 | #include "mbed.h" |
krissl | 1:9fc54848a198 | 23 | #include "ColorService.h" |
krissl | 0:fe5389573740 | 24 | |
krissl | 0:fe5389573740 | 25 | Serial pc(USBTX, USBRX); |
krissl | 0:fe5389573740 | 26 | |
krissl | 0:fe5389573740 | 27 | DigitalOut led1(LED1, 1); |
krissl | 0:fe5389573740 | 28 | |
krissl | 1:9fc54848a198 | 29 | const static char DEVICE_NAME[] = "SSS-Wearable"; |
krissl | 1:9fc54848a198 | 30 | static const uint16_t uuid16_list[] = {0xF0C0FF}; |
krissl | 0:fe5389573740 | 31 | |
krissl | 1:9fc54848a198 | 32 | static ColorService* colorServicePtr; |
krissl | 0:fe5389573740 | 33 | |
krissl | 0:fe5389573740 | 34 | static EventQueue eventQueue(/* event count */ 16 * EVENTS_EVENT_SIZE); |
krissl | 0:fe5389573740 | 35 | |
krissl | 0:fe5389573740 | 36 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
krissl | 0:fe5389573740 | 37 | { |
krissl | 0:fe5389573740 | 38 | BLE::Instance().gap().startAdvertising(); |
krissl | 0:fe5389573740 | 39 | } |
krissl | 0:fe5389573740 | 40 | void connectionCallback(const Gap::ConnectionCallbackParams_t *params) |
krissl | 0:fe5389573740 | 41 | { |
krissl | 0:fe5389573740 | 42 | pc.printf("\r\n *******Got connection****** \r\n"); |
krissl | 0:fe5389573740 | 43 | } |
krissl | 0:fe5389573740 | 44 | void updateSensorValue() { |
krissl | 1:9fc54848a198 | 45 | pc.printf("Right now, i should update sensor value. This runs only during connection \r\n"); |
krissl | 0:fe5389573740 | 46 | } |
krissl | 0:fe5389573740 | 47 | |
krissl | 0:fe5389573740 | 48 | void blinkCallback(void) |
krissl | 0:fe5389573740 | 49 | { |
krissl | 0:fe5389573740 | 50 | led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */ |
krissl | 0:fe5389573740 | 51 | |
krissl | 0:fe5389573740 | 52 | BLE &ble = BLE::Instance(); |
krissl | 0:fe5389573740 | 53 | if (ble.gap().getState().connected) { |
krissl | 0:fe5389573740 | 54 | eventQueue.call(updateSensorValue); |
krissl | 0:fe5389573740 | 55 | } |
krissl | 0:fe5389573740 | 56 | } |
krissl | 0:fe5389573740 | 57 | |
krissl | 0:fe5389573740 | 58 | /** |
krissl | 0:fe5389573740 | 59 | * This function is called when the ble initialization process has failled |
krissl | 0:fe5389573740 | 60 | */ |
krissl | 0:fe5389573740 | 61 | void onBleInitError(BLE &ble, ble_error_t error) |
krissl | 0:fe5389573740 | 62 | { |
krissl | 0:fe5389573740 | 63 | /* Initialization error handling should go here */ |
krissl | 0:fe5389573740 | 64 | } |
krissl | 0:fe5389573740 | 65 | |
krissl | 0:fe5389573740 | 66 | void printMacAddress() |
krissl | 0:fe5389573740 | 67 | { |
krissl | 0:fe5389573740 | 68 | /* Print out device MAC address to the console*/ |
krissl | 0:fe5389573740 | 69 | Gap::AddressType_t addr_type; |
krissl | 0:fe5389573740 | 70 | Gap::Address_t address; |
krissl | 0:fe5389573740 | 71 | BLE::Instance().gap().getAddress(&addr_type, address); |
krissl | 0:fe5389573740 | 72 | pc.printf("DEVICE MAC ADDRESS: "); |
krissl | 0:fe5389573740 | 73 | for (int i = 5; i >= 1; i--){ |
krissl | 0:fe5389573740 | 74 | pc.printf("%02x:", address[i]); |
krissl | 0:fe5389573740 | 75 | } |
krissl | 0:fe5389573740 | 76 | pc.printf("%02x\r\n", address[0]); |
krissl | 0:fe5389573740 | 77 | } |
krissl | 0:fe5389573740 | 78 | |
krissl | 0:fe5389573740 | 79 | /** |
krissl | 0:fe5389573740 | 80 | * Callback triggered when the ble initialization process has finished |
krissl | 0:fe5389573740 | 81 | */ |
krissl | 0:fe5389573740 | 82 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
krissl | 0:fe5389573740 | 83 | { |
krissl | 0:fe5389573740 | 84 | BLE& ble = params->ble; |
krissl | 0:fe5389573740 | 85 | ble_error_t error = params->error; |
krissl | 0:fe5389573740 | 86 | |
krissl | 0:fe5389573740 | 87 | if (error != BLE_ERROR_NONE) { |
krissl | 0:fe5389573740 | 88 | /* In case of error, forward the error handling to onBleInitError */ |
krissl | 0:fe5389573740 | 89 | onBleInitError(ble, error); |
krissl | 0:fe5389573740 | 90 | return; |
krissl | 0:fe5389573740 | 91 | } |
krissl | 0:fe5389573740 | 92 | |
krissl | 0:fe5389573740 | 93 | /* Ensure that it is the default instance of BLE */ |
krissl | 0:fe5389573740 | 94 | if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { |
krissl | 0:fe5389573740 | 95 | return; |
krissl | 0:fe5389573740 | 96 | } |
krissl | 0:fe5389573740 | 97 | |
krissl | 0:fe5389573740 | 98 | ble.gap().onDisconnection(disconnectionCallback); |
krissl | 0:fe5389573740 | 99 | ble.gap().onConnection(connectionCallback); |
krissl | 0:fe5389573740 | 100 | /* Setup primary service */ |
krissl | 1:9fc54848a198 | 101 | colorServicePtr = new ColorService(ble); |
krissl | 0:fe5389573740 | 102 | |
krissl | 0:fe5389573740 | 103 | /* Setup advertising */ |
krissl | 0:fe5389573740 | 104 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
krissl | 0:fe5389573740 | 105 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *) uuid16_list, sizeof(uuid16_list)); |
krissl | 0:fe5389573740 | 106 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *) DEVICE_NAME, sizeof(DEVICE_NAME)); |
krissl | 0:fe5389573740 | 107 | // ble.gattServer().onDataRead(blinkCallback); |
krissl | 0:fe5389573740 | 108 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
krissl | 0:fe5389573740 | 109 | ble.gap().setAdvertisingInterval(1000); /* 1000ms */ |
krissl | 1:9fc54848a198 | 110 | pc.printf("Advertising"); |
krissl | 0:fe5389573740 | 111 | ble.gap().startAdvertising(); |
krissl | 0:fe5389573740 | 112 | |
krissl | 0:fe5389573740 | 113 | printMacAddress(); |
krissl | 0:fe5389573740 | 114 | } |
krissl | 0:fe5389573740 | 115 | |
krissl | 0:fe5389573740 | 116 | void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) { |
krissl | 0:fe5389573740 | 117 | BLE &ble = BLE::Instance(); |
krissl | 0:fe5389573740 | 118 | eventQueue.call(Callback<void()>(&ble, &BLE::processEvents)); |
krissl | 0:fe5389573740 | 119 | } |
krissl | 0:fe5389573740 | 120 | |
krissl | 0:fe5389573740 | 121 | int main() |
krissl | 0:fe5389573740 | 122 | { |
krissl | 1:9fc54848a198 | 123 | pc.baud (115200); |
krissl | 0:fe5389573740 | 124 | |
krissl | 0:fe5389573740 | 125 | eventQueue.call_every(500, blinkCallback); |
krissl | 0:fe5389573740 | 126 | |
krissl | 0:fe5389573740 | 127 | BLE &ble = BLE::Instance(); |
krissl | 0:fe5389573740 | 128 | ble.onEventsToProcess(scheduleBleEventsProcessing); |
krissl | 0:fe5389573740 | 129 | ble.init(bleInitComplete); |
krissl | 0:fe5389573740 | 130 | |
krissl | 0:fe5389573740 | 131 | eventQueue.dispatch_forever(); |
krissl | 0:fe5389573740 | 132 | |
krissl | 0:fe5389573740 | 133 | return 0; |
krissl | 0:fe5389573740 | 134 | } |