Bluetooth Low Energy enabled device with "Switch" feature, compatible with BlueST Protocol.
Bluetooth Low Energy enabled device with "Switch" feature, compatible with BlueST Protocol.
source/main.cpp@3:b880b9a9ccdf, 2018-05-10 (annotated)
- Committer:
- Davidroid
- Date:
- Thu May 10 11:59:17 2018 +0000
- Revision:
- 3:b880b9a9ccdf
- Parent:
- 2:251cc22a254c
- Child:
- 4:67ff1ddd11e2
Update device name length
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Davidroid | 0:6b34c49b5285 | 1 | /** |
Davidroid | 0:6b34c49b5285 | 2 | ******************************************************************************* |
Davidroid | 0:6b34c49b5285 | 3 | * @file main.cpp |
Davidroid | 0:6b34c49b5285 | 4 | * @author Davide Aliprandi, STMicroelectronics |
Davidroid | 0:6b34c49b5285 | 5 | * @version V1.0.0 |
Davidroid | 0:6b34c49b5285 | 6 | * @date March 31st, 2018 |
Davidroid | 0:6b34c49b5285 | 7 | * @brief mbed test application for the STMicroelectronics X-NUCLEO-IDB05A1 |
Davidroid | 0:6b34c49b5285 | 8 | * Bluetooth Low energy Expansion Board. |
Davidroid | 0:6b34c49b5285 | 9 | ******************************************************************************* |
Davidroid | 0:6b34c49b5285 | 10 | * @attention |
Davidroid | 0:6b34c49b5285 | 11 | * |
Davidroid | 0:6b34c49b5285 | 12 | * <h2><center>© COPYRIGHT(c) 2018 STMicroelectronics</center></h2> |
Davidroid | 0:6b34c49b5285 | 13 | * |
Davidroid | 0:6b34c49b5285 | 14 | * Redistribution and use in source and binary forms, with or without |
Davidroid | 0:6b34c49b5285 | 15 | * modification, are permitted provided that the following conditions are met: |
Davidroid | 0:6b34c49b5285 | 16 | * 1. Redistributions of source code must retain the above copyright notice, |
Davidroid | 0:6b34c49b5285 | 17 | * this list of conditions and the following disclaimer. |
Davidroid | 0:6b34c49b5285 | 18 | * 2. Redistributions in binary form must reproduce the above copyright |
Davidroid | 0:6b34c49b5285 | 19 | * notice, this list of conditions and the following disclaimer in the |
Davidroid | 0:6b34c49b5285 | 20 | * documentation and/or other materials provided with the distribution. |
Davidroid | 0:6b34c49b5285 | 21 | * 3. Neither the name of STMicroelectronics nor the names of its |
Davidroid | 0:6b34c49b5285 | 22 | * contributors may be used to endorse or promote products derived from |
Davidroid | 0:6b34c49b5285 | 23 | * this software without specific prior written permission. |
Davidroid | 0:6b34c49b5285 | 24 | * |
Davidroid | 0:6b34c49b5285 | 25 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
Davidroid | 0:6b34c49b5285 | 26 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
Davidroid | 0:6b34c49b5285 | 27 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
Davidroid | 0:6b34c49b5285 | 28 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS |
Davidroid | 0:6b34c49b5285 | 29 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
Davidroid | 0:6b34c49b5285 | 30 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
Davidroid | 0:6b34c49b5285 | 31 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
Davidroid | 0:6b34c49b5285 | 32 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
Davidroid | 0:6b34c49b5285 | 33 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
Davidroid | 0:6b34c49b5285 | 34 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
Davidroid | 0:6b34c49b5285 | 35 | * POSSIBILITY OF SUCH DAMAGE. |
Davidroid | 0:6b34c49b5285 | 36 | * |
Davidroid | 0:6b34c49b5285 | 37 | ******************************************************************************* |
Davidroid | 0:6b34c49b5285 | 38 | */ |
Davidroid | 0:6b34c49b5285 | 39 | |
Davidroid | 0:6b34c49b5285 | 40 | |
Davidroid | 0:6b34c49b5285 | 41 | /* Includes ------------------------------------------------------------------*/ |
Davidroid | 0:6b34c49b5285 | 42 | |
Davidroid | 0:6b34c49b5285 | 43 | /* mbed specific header files. */ |
Davidroid | 0:6b34c49b5285 | 44 | #include <events/mbed_events.h> |
Davidroid | 0:6b34c49b5285 | 45 | #include <mbed.h> |
Davidroid | 0:6b34c49b5285 | 46 | #include "ble/BLE.h" |
Davidroid | 0:6b34c49b5285 | 47 | #include "CustomService.h" |
Davidroid | 0:6b34c49b5285 | 48 | |
Davidroid | 0:6b34c49b5285 | 49 | |
Davidroid | 0:6b34c49b5285 | 50 | /* Definitions ---------------------------------------------------------------*/ |
Davidroid | 0:6b34c49b5285 | 51 | |
Davidroid | 0:6b34c49b5285 | 52 | #define BLE_ADVERTISING_INTERVAL_ms 1000 |
Davidroid | 0:6b34c49b5285 | 53 | #define BLUETOOTH_PERIODIC_CALLBACK_ms 500 |
Davidroid | 0:6b34c49b5285 | 54 | #define DELAY_1 100 |
Davidroid | 0:6b34c49b5285 | 55 | |
Davidroid | 0:6b34c49b5285 | 56 | |
Davidroid | 0:6b34c49b5285 | 57 | /* Types ---------------------------------------------------------------------*/ |
Davidroid | 0:6b34c49b5285 | 58 | |
Davidroid | 0:6b34c49b5285 | 59 | /* Toggle state. */ |
Davidroid | 0:6b34c49b5285 | 60 | typedef enum |
Davidroid | 0:6b34c49b5285 | 61 | { |
Davidroid | 0:6b34c49b5285 | 62 | TOGGLE_OFF = 0, |
Davidroid | 0:6b34c49b5285 | 63 | TOGGLE_ON |
Davidroid | 0:6b34c49b5285 | 64 | } toggle_t; |
Davidroid | 0:6b34c49b5285 | 65 | |
Davidroid | 0:6b34c49b5285 | 66 | |
Davidroid | 0:6b34c49b5285 | 67 | /* Variables -----------------------------------------------------------------*/ |
Davidroid | 0:6b34c49b5285 | 68 | |
Davidroid | 0:6b34c49b5285 | 69 | /* Button state. */ |
Davidroid | 0:6b34c49b5285 | 70 | bool toggle_state; |
Davidroid | 0:6b34c49b5285 | 71 | |
Davidroid | 0:6b34c49b5285 | 72 | /* LED to indicate system state. */ |
Davidroid | 0:6b34c49b5285 | 73 | DigitalOut led_state(LED1); //Conflicts with SPI CLK on D13. |
Davidroid | 0:6b34c49b5285 | 74 | |
Davidroid | 0:6b34c49b5285 | 75 | /* Bluetooth. */ |
Davidroid | 1:bb6dbece87c1 | 76 | const static char DEVICE_NAME[] = "SWITCH_DEVICE"; |
Davidroid | 0:6b34c49b5285 | 77 | const static uint8_t MANUFACTURER_SPECIFIC_DATA[]= {0x01,0x80,0x20,0x00,0x00,0x00}; |
Davidroid | 0:6b34c49b5285 | 78 | static EventQueue event_queue(/* event count */ 10 * EVENTS_EVENT_SIZE); |
Davidroid | 0:6b34c49b5285 | 79 | CustomService *custom_service; |
Davidroid | 0:6b34c49b5285 | 80 | |
Davidroid | 0:6b34c49b5285 | 81 | /* Button event. */ |
Davidroid | 0:6b34c49b5285 | 82 | InterruptIn event(USER_BUTTON); |
Davidroid | 0:6b34c49b5285 | 83 | |
Davidroid | 0:6b34c49b5285 | 84 | |
Davidroid | 0:6b34c49b5285 | 85 | /* Bluetooth related functions -----------------------------------------------*/ |
Davidroid | 0:6b34c49b5285 | 86 | |
Davidroid | 0:6b34c49b5285 | 87 | void on_disconnection_callback(const Gap::DisconnectionCallbackParams_t *params) |
Davidroid | 0:6b34c49b5285 | 88 | { |
Davidroid | 0:6b34c49b5285 | 89 | (void) params; |
Davidroid | 0:6b34c49b5285 | 90 | BLE::Instance().gap().startAdvertising(); |
Davidroid | 0:6b34c49b5285 | 91 | } |
Davidroid | 0:6b34c49b5285 | 92 | |
Davidroid | 0:6b34c49b5285 | 93 | /** |
Davidroid | 0:6b34c49b5285 | 94 | * This callback allows the custom service to read updates from the |
Davidroid | 0:6b34c49b5285 | 95 | * characteristic. |
Davidroid | 0:6b34c49b5285 | 96 | * |
Davidroid | 0:6b34c49b5285 | 97 | * @param[in] params |
Davidroid | 0:6b34c49b5285 | 98 | * Information about the characterisitc being updated. |
Davidroid | 0:6b34c49b5285 | 99 | */ |
Davidroid | 0:6b34c49b5285 | 100 | void on_data_read_callback(const GattReadCallbackParams *params) { |
Davidroid | 0:6b34c49b5285 | 101 | static uint16_t time_stamp = 0; |
Davidroid | 0:6b34c49b5285 | 102 | |
Davidroid | 0:6b34c49b5285 | 103 | if (params->handle == custom_service->getValueHandle()) { |
Davidroid | 0:6b34c49b5285 | 104 | // Reading led state and sending it via bluetooth. |
Davidroid | 0:6b34c49b5285 | 105 | if (BLE::Instance().getGapState().connected) { |
Davidroid | 0:6b34c49b5285 | 106 | //event_queue.call(Callback<void(uint16_t, uint8_t)>(custom_service, &CustomService::send_state), time_stamp++, led_state.read() ? CustomService::SWITCH_ON : CustomService::SWITCH_OFF); |
Davidroid | 0:6b34c49b5285 | 107 | event_queue.call(Callback<void(uint16_t, uint8_t)>(custom_service, &CustomService::send_state), time_stamp++, TOGGLE_OFF); // To clean the BLE buffer. |
Davidroid | 0:6b34c49b5285 | 108 | } |
Davidroid | 0:6b34c49b5285 | 109 | } |
Davidroid | 0:6b34c49b5285 | 110 | } |
Davidroid | 0:6b34c49b5285 | 111 | |
Davidroid | 0:6b34c49b5285 | 112 | /** |
Davidroid | 0:6b34c49b5285 | 113 | * This callback allows the custom service to write updates to the |
Davidroid | 0:6b34c49b5285 | 114 | * characteristic. |
Davidroid | 0:6b34c49b5285 | 115 | * |
Davidroid | 0:6b34c49b5285 | 116 | * @param[in] params |
Davidroid | 0:6b34c49b5285 | 117 | * Information about the characterisitc being updated. |
Davidroid | 0:6b34c49b5285 | 118 | */ |
Davidroid | 0:6b34c49b5285 | 119 | void on_data_written_callback(const GattWriteCallbackParams *params) { |
Davidroid | 0:6b34c49b5285 | 120 | |
Davidroid | 0:6b34c49b5285 | 121 | if (params->handle == custom_service->getValueHandle()) { |
Davidroid | 0:6b34c49b5285 | 122 | /* Receiving command via bluetooth and writing it to the led state. */ |
Davidroid | 0:6b34c49b5285 | 123 | led_state.write(((CustomService::switch_state_t) ((uint8_t *) (params->data))[0])); |
Davidroid | 0:6b34c49b5285 | 124 | } |
Davidroid | 0:6b34c49b5285 | 125 | } |
Davidroid | 0:6b34c49b5285 | 126 | |
Davidroid | 0:6b34c49b5285 | 127 | /** |
Davidroid | 0:6b34c49b5285 | 128 | * This function is called periodically. |
Davidroid | 0:6b34c49b5285 | 129 | */ |
Davidroid | 0:6b34c49b5285 | 130 | void periodic_callback(void) |
Davidroid | 0:6b34c49b5285 | 131 | { |
Davidroid | 0:6b34c49b5285 | 132 | static uint16_t time_stamp = 0; |
Davidroid | 0:6b34c49b5285 | 133 | |
Davidroid | 0:6b34c49b5285 | 134 | /* Reading toggle command and sending it via bluetooth. */ |
Davidroid | 0:6b34c49b5285 | 135 | if (toggle_state) { |
Davidroid | 0:6b34c49b5285 | 136 | if (BLE::Instance().getGapState().connected) { |
Davidroid | 0:6b34c49b5285 | 137 | event_queue.call(Callback<void(uint16_t, uint8_t)>(custom_service, &CustomService::send_state), time_stamp++, toggle_state ? TOGGLE_ON : TOGGLE_OFF); |
Davidroid | 0:6b34c49b5285 | 138 | toggle_state = false; |
Davidroid | 0:6b34c49b5285 | 139 | } |
Davidroid | 0:6b34c49b5285 | 140 | } |
Davidroid | 0:6b34c49b5285 | 141 | } |
Davidroid | 0:6b34c49b5285 | 142 | |
Davidroid | 0:6b34c49b5285 | 143 | /** |
Davidroid | 0:6b34c49b5285 | 144 | * This function is called when the ble initialization process has failled. |
Davidroid | 0:6b34c49b5285 | 145 | */ |
Davidroid | 0:6b34c49b5285 | 146 | void on_ble_init_error_callback(BLE &ble, ble_error_t error) |
Davidroid | 0:6b34c49b5285 | 147 | { |
Davidroid | 0:6b34c49b5285 | 148 | /* Initialization error handling should go here */ |
Davidroid | 0:6b34c49b5285 | 149 | } |
Davidroid | 0:6b34c49b5285 | 150 | |
Davidroid | 0:6b34c49b5285 | 151 | /** |
Davidroid | 0:6b34c49b5285 | 152 | * Callback triggered when the ble initialization process has finished. |
Davidroid | 0:6b34c49b5285 | 153 | */ |
Davidroid | 0:6b34c49b5285 | 154 | void ble_init_complete(BLE::InitializationCompleteCallbackContext *params) |
Davidroid | 0:6b34c49b5285 | 155 | { |
Davidroid | 0:6b34c49b5285 | 156 | BLE& ble = params->ble; |
Davidroid | 0:6b34c49b5285 | 157 | ble_error_t error = params->error; |
Davidroid | 0:6b34c49b5285 | 158 | |
Davidroid | 0:6b34c49b5285 | 159 | if (error != BLE_ERROR_NONE) { |
Davidroid | 0:6b34c49b5285 | 160 | /* In case of error, forward the error handling to on_ble_init_error_callback */ |
Davidroid | 0:6b34c49b5285 | 161 | on_ble_init_error_callback(ble, error); |
Davidroid | 0:6b34c49b5285 | 162 | return; |
Davidroid | 0:6b34c49b5285 | 163 | } |
Davidroid | 0:6b34c49b5285 | 164 | |
Davidroid | 0:6b34c49b5285 | 165 | /* Ensure that it is the default instance of BLE */ |
Davidroid | 0:6b34c49b5285 | 166 | if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { |
Davidroid | 0:6b34c49b5285 | 167 | return; |
Davidroid | 0:6b34c49b5285 | 168 | } |
Davidroid | 0:6b34c49b5285 | 169 | |
Davidroid | 0:6b34c49b5285 | 170 | ble.gap().onDisconnection(on_disconnection_callback); |
Davidroid | 0:6b34c49b5285 | 171 | ble.gattServer().onDataWritten(on_data_written_callback); |
Davidroid | 0:6b34c49b5285 | 172 | //ble.gattServer().onDataRead(on_data_read_callback); |
Davidroid | 0:6b34c49b5285 | 173 | |
Davidroid | 0:6b34c49b5285 | 174 | custom_service = new CustomService(ble); |
Davidroid | 0:6b34c49b5285 | 175 | |
Davidroid | 0:6b34c49b5285 | 176 | /* Setup advertising data. */ |
Davidroid | 0:6b34c49b5285 | 177 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
Davidroid | 0:6b34c49b5285 | 178 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS, (uint8_t *) CUSTOM_SWITCH_SERVICE_UUID, sizeof(CUSTOM_SWITCH_SERVICE_UUID)); |
Davidroid | 0:6b34c49b5285 | 179 | ble.gap().accumulateScanResponse(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, MANUFACTURER_SPECIFIC_DATA, sizeof(MANUFACTURER_SPECIFIC_DATA)); |
Davidroid | 3:b880b9a9ccdf | 180 | ble.gap().accumulateScanResponse(GapAdvertisingData::COMPLETE_LOCAL_NAME, (const uint8_t *) DEVICE_NAME, sizeof(DEVICE_NAME) - 1); |
Davidroid | 0:6b34c49b5285 | 181 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
Davidroid | 0:6b34c49b5285 | 182 | ble.gap().setAdvertisingInterval(BLE_ADVERTISING_INTERVAL_ms); |
Davidroid | 0:6b34c49b5285 | 183 | ble.gap().startAdvertising(); |
Davidroid | 0:6b34c49b5285 | 184 | } |
Davidroid | 0:6b34c49b5285 | 185 | |
Davidroid | 0:6b34c49b5285 | 186 | void schedule_ble_events_processing(BLE::OnEventsToProcessCallbackContext* context) { |
Davidroid | 0:6b34c49b5285 | 187 | BLE &ble = BLE::Instance(); |
Davidroid | 0:6b34c49b5285 | 188 | event_queue.call(Callback<void()>(&ble, &BLE::processEvents)); |
Davidroid | 0:6b34c49b5285 | 189 | } |
Davidroid | 0:6b34c49b5285 | 190 | |
Davidroid | 0:6b34c49b5285 | 191 | |
Davidroid | 0:6b34c49b5285 | 192 | /* Custom service related functions ------------------------------------------*/ |
Davidroid | 0:6b34c49b5285 | 193 | |
Davidroid | 0:6b34c49b5285 | 194 | void button_callback(void) { |
Davidroid | 0:6b34c49b5285 | 195 | toggle_state = true; |
Davidroid | 0:6b34c49b5285 | 196 | } |
Davidroid | 0:6b34c49b5285 | 197 | |
Davidroid | 0:6b34c49b5285 | 198 | |
Davidroid | 0:6b34c49b5285 | 199 | /* Main function -------------------------------------------------------------*/ |
Davidroid | 0:6b34c49b5285 | 200 | |
Davidroid | 0:6b34c49b5285 | 201 | int main() |
Davidroid | 0:6b34c49b5285 | 202 | { |
Davidroid | 0:6b34c49b5285 | 203 | /*----- Initialization. -----*/ |
Davidroid | 0:6b34c49b5285 | 204 | |
Davidroid | 0:6b34c49b5285 | 205 | /* Printing to the console. */ |
Davidroid | 0:6b34c49b5285 | 206 | printf("Switch Node Application Example\r\n\n"); |
Davidroid | 0:6b34c49b5285 | 207 | |
Davidroid | 0:6b34c49b5285 | 208 | /* Bluetooth. */ |
Davidroid | 0:6b34c49b5285 | 209 | event_queue.call_every(BLUETOOTH_PERIODIC_CALLBACK_ms, periodic_callback); |
Davidroid | 0:6b34c49b5285 | 210 | BLE &ble = BLE::Instance(); |
Davidroid | 0:6b34c49b5285 | 211 | ble.onEventsToProcess(schedule_ble_events_processing); |
Davidroid | 0:6b34c49b5285 | 212 | ble.init(ble_init_complete); |
Davidroid | 0:6b34c49b5285 | 213 | |
Davidroid | 0:6b34c49b5285 | 214 | /* Attaching and enabling interrupt handlers. */ |
Davidroid | 0:6b34c49b5285 | 215 | event.fall(button_callback); |
Davidroid | 0:6b34c49b5285 | 216 | |
Davidroid | 0:6b34c49b5285 | 217 | /* Setting up initial state: led OFF, no toggle state. */ |
Davidroid | 0:6b34c49b5285 | 218 | led_state.write(CustomService::SWITCH_OFF); |
Davidroid | 0:6b34c49b5285 | 219 | toggle_state = false; |
Davidroid | 0:6b34c49b5285 | 220 | |
Davidroid | 0:6b34c49b5285 | 221 | /* Start. */ |
Davidroid | 0:6b34c49b5285 | 222 | event_queue.dispatch_forever(); |
Davidroid | 0:6b34c49b5285 | 223 | |
Davidroid | 0:6b34c49b5285 | 224 | return 0; |
Davidroid | 0:6b34c49b5285 | 225 | } |