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

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_Default_APP by Yihui Xiong

main.cpp

Committer:
prussell
Date:
2015-01-30
Revision:
2:97f1622aef50
Parent:
0:29068834cf22

File content as of revision 2:97f1622aef50:

/* mbed Microcontroller Library
 * Copyright (c) 2006-2015 ARM Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
#include "mbed.h"
#include "BLEDevice.h"
 
#include "DFUService.h"
#include "UARTService.h"
#include "DeviceInformationService.h"
 
#define NEED_CONSOLE_OUTPUT 1 /* Set this if you need debug messages on the console;
                               * it will have an impact on code-size and power consumption. */
 
#if NEED_CONSOLE_OUTPUT
#define DEBUG(...) { printf(__VA_ARGS__); }
#else
#define DEBUG(...) /* nothing */
#endif /* #if NEED_CONSOLE_OUTPUT */
 
BLEDevice ble;
UARTService *uartServicePtr;
 
const char *deviceName = "DefaultAppR2";
const char *ping = "ping";
 
void periodicCallback(void)
{
    DEBUG("ping\r\n");
    if (uartServicePtr != NULL) { 
        ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (const uint8_t *) ping, strlen(ping));
    }
}
 
void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
{
    DEBUG("Disconnected!\n\r");
    DEBUG("Restarting the advertising process\n\r");
    ble.startAdvertising();
}
 
int main(void)
{
    Ticker ticker;
    ticker.attach(periodicCallback, 1);
 
    DEBUG("Initialising BTLE transport.\n\r");
    ble.init();
    ble.onDisconnection(disconnectionCallback);
 
    ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
    ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, (const uint8_t *)deviceName, strlen(deviceName));
 
    ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); /* needs to be connectable to allow use of DFUService */
    ble.setAdvertisingInterval(1600); /* 1s; in multiples of 0.625ms. */
    ble.startAdvertising();
 
    //DeviceInformationService deviceInfo(ble, "ARM", "Model1", "SN1000", "hw-rev1", "fw-rev1");
    DeviceInformationService deviceInfo(ble, "ARM", deviceName, "SN1000", "hw-rev1", "fw-rev1");
 
    /* Enable over-the-air firmware updates. Instantiating DFUSservice introduces a
     * control characteristic which can be used to trigger the application to
     * handover control to a resident bootloader. */
    DFUService dfu(ble);
 
    /* Setup a BLE service for console output. Redirect stdout to BLE-UART. */
    UARTService uartService(ble);
    uartServicePtr = &uartService;
 
    for (;;) {
        ble.waitForEvent();
    }
}