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
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions ble/services/ImmediateAlertService.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* ImmediateAlertService.h
*
* Created on: 2016-02-02
* Author: KUANG Qi
*/

#ifndef __BLE_IMMEDIATE_ALERT_SERVICE_H__
#define __BLE_IMMEDIATE_ALERT_SERVICE_H__

#include "ble/BLE.h"

class ImmediateAlertService
{
public:
enum AlertLevel_t
{
NO_ALERT = 0,
MILD_ALERT = 1,
HIGH_ALERT = 2
};

typedef void (*callback_t)(AlertLevel_t level);

/**
* @param[ref] ble
* BLE object for the underlying controller.
*/
ImmediateAlertService(BLE &bleIn, callback_t callbackIn,
AlertLevel_t levelIn = NO_ALERT) :
ble(bleIn), alertLevel(levelIn), callback(callbackIn), alertLevelChar(
GattCharacteristic::UUID_ALERT_LEVEL_CHAR,
reinterpret_cast<uint8_t *>(&alertLevel), 1, 1,
GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE)
{
static bool serviceAdded = false; /* We should only ever add one Immdiate Alert service. */
if (serviceAdded)
{
return;
}

GattCharacteristic *charTable[] =
{ &alertLevelChar };
GattService immediateAlertService(
GattService::UUID_IMMEDIATE_ALERT_SERVICE, charTable,
sizeof(charTable) / sizeof(GattCharacteristic *));
ble.gattServer().addService(immediateAlertService);
serviceAdded = true;

ble.gattServer().onDataWritten(this,
&ImmediateAlertService::onDataWritten);
}

/**
* Update the callback.
*/
void setCallback(callback_t newCallback)
{
callback = newCallback;
}

/**
* Update alertness level.
*/
void setAlertLevel(AlertLevel_t newLevel)
{
alertLevel = newLevel;
}

protected:
/**
* This callback allows receiving updates to the AlertLevel characteristic.
*
* @param[in] params
* Information about the characterisitc being updated.
*/
virtual void onDataWritten(const GattWriteCallbackParams *params)
{
if (params->handle == alertLevelChar.getValueHandle())
{
alertLevel = *reinterpret_cast<const AlertLevel_t *>(params->data);
callback(alertLevel);
}
}

protected:
BLE &ble;
AlertLevel_t alertLevel;
callback_t callback;
GattCharacteristic alertLevelChar;

};

#endif /* __BLE_IMMEDIATE_ALERT_SERVICE_H__ */