Updated to latest online libraries (See also mbed Apps: HelloBlue, FOTA4)

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_Default_APP by Yihui Xiong

Committer:
prussell
Date:
Fri Jan 30 11:39:58 2015 +0000
Revision:
2:97f1622aef50
Parent:
0:29068834cf22
Updated to latest online libraries, and changes BLE UART appropriately.
; (See also mbed Apps: HelloBlue, FOTA4)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rgrover1 0:29068834cf22 1 /* mbed Microcontroller Library
prussell 2:97f1622aef50 2 * Copyright (c) 2006-2015 ARM Limited
rgrover1 0:29068834cf22 3 *
rgrover1 0:29068834cf22 4 * Licensed under the Apache License, Version 2.0 (the "License");
rgrover1 0:29068834cf22 5 * you may not use this file except in compliance with the License.
rgrover1 0:29068834cf22 6 * You may obtain a copy of the License at
rgrover1 0:29068834cf22 7 *
rgrover1 0:29068834cf22 8 * http://www.apache.org/licenses/LICENSE-2.0
rgrover1 0:29068834cf22 9 *
rgrover1 0:29068834cf22 10 * Unless required by applicable law or agreed to in writing, software
rgrover1 0:29068834cf22 11 * distributed under the License is distributed on an "AS IS" BASIS,
rgrover1 0:29068834cf22 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
rgrover1 0:29068834cf22 13 * See the License for the specific language governing permissions and
rgrover1 0:29068834cf22 14 * limitations under the License.
rgrover1 0:29068834cf22 15 */
prussell 2:97f1622aef50 16
rgrover1 0:29068834cf22 17 #include "mbed.h"
rgrover1 0:29068834cf22 18 #include "BLEDevice.h"
prussell 2:97f1622aef50 19
rgrover1 0:29068834cf22 20 #include "DFUService.h"
rgrover1 0:29068834cf22 21 #include "UARTService.h"
rgrover1 0:29068834cf22 22 #include "DeviceInformationService.h"
prussell 2:97f1622aef50 23
rgrover1 0:29068834cf22 24 #define NEED_CONSOLE_OUTPUT 1 /* Set this if you need debug messages on the console;
rgrover1 0:29068834cf22 25 * it will have an impact on code-size and power consumption. */
prussell 2:97f1622aef50 26
rgrover1 0:29068834cf22 27 #if NEED_CONSOLE_OUTPUT
rgrover1 0:29068834cf22 28 #define DEBUG(...) { printf(__VA_ARGS__); }
rgrover1 0:29068834cf22 29 #else
rgrover1 0:29068834cf22 30 #define DEBUG(...) /* nothing */
rgrover1 0:29068834cf22 31 #endif /* #if NEED_CONSOLE_OUTPUT */
prussell 2:97f1622aef50 32
rgrover1 0:29068834cf22 33 BLEDevice ble;
prussell 2:97f1622aef50 34 UARTService *uartServicePtr;
prussell 2:97f1622aef50 35
prussell 2:97f1622aef50 36 const char *deviceName = "DefaultAppR2";
prussell 2:97f1622aef50 37 const char *ping = "ping";
prussell 2:97f1622aef50 38
rgrover1 0:29068834cf22 39 void periodicCallback(void)
rgrover1 0:29068834cf22 40 {
rgrover1 0:29068834cf22 41 DEBUG("ping\r\n");
prussell 2:97f1622aef50 42 if (uartServicePtr != NULL) {
prussell 2:97f1622aef50 43 ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (const uint8_t *) ping, strlen(ping));
prussell 2:97f1622aef50 44 }
rgrover1 0:29068834cf22 45 }
prussell 2:97f1622aef50 46
rgrover1 0:29068834cf22 47 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
rgrover1 0:29068834cf22 48 {
rgrover1 0:29068834cf22 49 DEBUG("Disconnected!\n\r");
rgrover1 0:29068834cf22 50 DEBUG("Restarting the advertising process\n\r");
rgrover1 0:29068834cf22 51 ble.startAdvertising();
rgrover1 0:29068834cf22 52 }
prussell 2:97f1622aef50 53
rgrover1 0:29068834cf22 54 int main(void)
rgrover1 0:29068834cf22 55 {
rgrover1 0:29068834cf22 56 Ticker ticker;
rgrover1 0:29068834cf22 57 ticker.attach(periodicCallback, 1);
prussell 2:97f1622aef50 58
prussell 2:97f1622aef50 59 DEBUG("Initialising BTLE transport.\n\r");
rgrover1 0:29068834cf22 60 ble.init();
rgrover1 0:29068834cf22 61 ble.onDisconnection(disconnectionCallback);
prussell 2:97f1622aef50 62
rgrover1 0:29068834cf22 63 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
rgrover1 0:29068834cf22 64 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, (const uint8_t *)deviceName, strlen(deviceName));
prussell 2:97f1622aef50 65
rgrover1 0:29068834cf22 66 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); /* needs to be connectable to allow use of DFUService */
rgrover1 0:29068834cf22 67 ble.setAdvertisingInterval(1600); /* 1s; in multiples of 0.625ms. */
rgrover1 0:29068834cf22 68 ble.startAdvertising();
prussell 2:97f1622aef50 69
prussell 2:97f1622aef50 70 //DeviceInformationService deviceInfo(ble, "ARM", "Model1", "SN1000", "hw-rev1", "fw-rev1");
prussell 2:97f1622aef50 71 DeviceInformationService deviceInfo(ble, "ARM", deviceName, "SN1000", "hw-rev1", "fw-rev1");
prussell 2:97f1622aef50 72
rgrover1 0:29068834cf22 73 /* Enable over-the-air firmware updates. Instantiating DFUSservice introduces a
rgrover1 0:29068834cf22 74 * control characteristic which can be used to trigger the application to
rgrover1 0:29068834cf22 75 * handover control to a resident bootloader. */
rgrover1 0:29068834cf22 76 DFUService dfu(ble);
prussell 2:97f1622aef50 77
rgrover1 0:29068834cf22 78 /* Setup a BLE service for console output. Redirect stdout to BLE-UART. */
rgrover1 0:29068834cf22 79 UARTService uartService(ble);
prussell 2:97f1622aef50 80 uartServicePtr = &uartService;
prussell 2:97f1622aef50 81
rgrover1 0:29068834cf22 82 for (;;) {
rgrover1 0:29068834cf22 83 ble.waitForEvent();
rgrover1 0:29068834cf22 84 }
rgrover1 0:29068834cf22 85 }