Implement Immediate Alert Service
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
}
}
Automatic CI verification build not done, please verify manually.
Automatic CI verification build not done, please verify manually.
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.
@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.