Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Immediate Alert Service #176

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
Open

Conversation

kqkq
Copy link

@kqkq kqkq commented Feb 2, 2016

Immediate Alert Service is a simple service which exposes a control point to allow a peer device to cause the device to immediately alert.

This pull request implements this service.

Here's the demo program. Write 0, 1 or 2 to the characteristic Alert Level could cause the LED to blink.

#include "mbed.h"
#include "ble/BLE.h"
#include "ble/services/ImmediateAlertService.h"

Ticker alertTimer;

DigitalOut led(LED1, 0);

static const char *DEVICE_NAME = "Alert";
static const uint16_t uuid16_list[] = {GattService::UUID_IMMEDIATE_ALERT_SERVICE};

ImmediateAlertService *alert;

void tickAlertHandler()
{
    led = !led;
}

void alertHandler(ImmediateAlertService::AlertLevel_t alertLevel)
{
    /* Blink a LED if an alert is received */
    if(alertLevel == ImmediateAlertService::NO_ALERT)
    {
        alertTimer.detach();
        led = 0;
    }
    if(alertLevel == ImmediateAlertService::MILD_ALERT)
    {
        alertTimer.attach(tickAlertHandler, 1.0);
    }
    if(alertLevel == ImmediateAlertService::HIGH_ALERT)
    {
        alertTimer.attach(tickAlertHandler, 0.25);
    }
}

void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *)
{
    BLE::Instance(BLE::DEFAULT_INSTANCE).gap().startAdvertising();
    /* Cancel the alert on disconnecting */
    led = 0;
    alert->setAlertLevel(ImmediateAlertService::NO_ALERT);
    alertHandler(ImmediateAlertService::NO_ALERT);
}

void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
{
    BLE &ble          = params->ble;
    ble_error_t error = params->error;

    if (error != BLE_ERROR_NONE) {
        return;
    }

    ble.gap().onDisconnection(disconnectionCallback);

    /* Setup advertising */
    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); // BLE only, no classic BT
    ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); // advertising type
    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); // add name
    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); // UUID's broadcast in advertising packet
    ble.gap().setAdvertisingInterval(100); // 100ms.

    /* Add service */
    alert  = new ImmediateAlertService(ble, alertHandler);

    /* Start advertising */
    ble.gap().startAdvertising();
}

int main(void)
{
    BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE);
    ble.init(bleInitComplete);

    while (!ble.hasInitialized()) { /* spin loop */ }

    while (true) {
        ble.waitForEvent(); // allows or low power operation
    }
}

Rohit Grover and others added 8 commits December 2, 2015 09:42
=============

A minor release to separate the concept of minlen and len in
GattCharacteristic. Also contains some improvements to documentation.
=============

Minor update around GattCharacteristics having variable length.
=============

updating versions for dependencies
merge branch develop (v2.4.0)
merge version 2.5.0 into master
@ciarmcom
Copy link
Member

ciarmcom commented Feb 2, 2016

Automatic CI verification build not done, please verify manually.

1 similar comment
@ciarmcom
Copy link
Member

ciarmcom commented Feb 2, 2016

Automatic CI verification build not done, please verify manually.

@pan-
Copy link
Member

pan- commented Feb 2, 2016

Hello,

The message Automatic CI verification build not done, please verify manually. means that our continuous integration system didn't try to verify your pull request, I will look at it in the course of the day.

@crespum
Copy link
Contributor

crespum commented Mar 8, 2016

@kqkq I haven't seen this PR until now. I've submitted a similar one with the difference that I put some documentation in the methods and I included support for class methods to be callback functions. You can check it in #180 but I think we should merge just one of them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants