Dependencies:   AccelSensor BLE_API mbed nRF51822

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ALARMService.h Source File

ALARMService.h

00001 #ifndef __BLE_ALARM_SERVICE_H__
00002 #define __BLE_ALARM_SERVICE_H__
00003 
00004 #include "mbed.h"
00005 #include "ble/BLE.h"
00006 #include "ble/Gap.h"
00007 #include "ImobStateService.h"
00008 
00009 class ALARMService {
00010 public:
00011     const static uint16_t ALARM_SERVICE_UUID = 0xE000;
00012     const static uint16_t ALARM_STATE_CHARACTERISTIC_UUID = 0xE001;
00013 
00014     ALARMService(BLEDevice &_ble) : 
00015         ble(_ble),
00016         alarmState(0), 
00017         AlarmCharacteristic(ALARM_STATE_CHARACTERISTIC_UUID, &alarmState, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)
00018     {
00019         GattCharacteristic *charTable[] = {&AlarmCharacteristic};
00020         GattService alarmService(ALARM_SERVICE_UUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
00021 
00022         ble.addService(alarmService);
00023 
00024         ble.gattServer().onDataWritten(this, &ALARMService::onDataWritten);
00025     }
00026 
00027     GattAttribute::Handle_t getValueHandle() const 
00028     {
00029         return AlarmCharacteristic.getValueHandle();
00030     }
00031     
00032     void updateAlarmState(uint8_t newAlarmState) {
00033         alarmState = newAlarmState;
00034         ble.gattServer().write(AlarmCharacteristic.getValueHandle(), &alarmState, 1);
00035     }
00036     
00037 protected:
00038     
00039     virtual void onDataWritten(const GattWriteCallbackParams *params)
00040     {          
00041         if ((params->handle == AlarmCharacteristic.getValueHandle()) && (params->len == 1) && authenticated)
00042         {
00043             updateAlarmState(*(params->data));
00044         }
00045         else
00046             updateAlarmState(0);
00047     }     
00048 
00049 private:
00050     
00051     BLEDevice &ble;
00052     uint8_t alarmState;
00053     
00054     ReadWriteGattCharacteristic<uint8_t> AlarmCharacteristic;
00055 };
00056 
00057 #endif /* #ifndef __BLE_RELAY_SERVICE_H__ */