Just a quick custom service
Diff: CountdownService.h
- Revision:
- 0:bc6cd13ebbdb
- Child:
- 1:79a62e074c35
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/CountdownService.h Fri Nov 23 08:59:41 2018 +0000 @@ -0,0 +1,55 @@ +/* code is property of aconno.de */ + + +#ifndef COUNTDOWN_SERVICE +#define COUNTDOWN_SERVICE + +#include <stdbool.h> +#include "mbed.h" +#include <events/mbed_events.h> +#include "ble/BLE.h" + + +class CountdownService { +typedef CountdownService Self; +public: + const static uint16_t UUID_Service = 0xFFFF; + const static uint16_t UUID_TimeCharacteristic = 0xFFFF; + + CountdownService(GattServer &server, Callback<void(uint32_t)> countdown_changed_callback = NULL): + server(server), + countdownChar(UUID_TimeCharacteristic,0), + onCountdownReset(countdown_changed_callback) { + + GattCharacteristic *charTable[] = {&countdownChar}; + GattService service(UUID_Service, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); + + server.addService(service); + server.onDataWritten(this, &CountdownService::onDataWritten); + } + + + void SetCountdown(uint32_t value) { + server.write(countdownChar.getValueHandle(), (const uint8_t*)&value, sizeof(uint32_t)); + } + uint32_t GetCountdown() { + uint16_t len = 4; + uint32_t value; + uint8_t* buffer = (uint8_t*)&value; + server.read(countdownChar.getValueHandle(), buffer, &len); + return value; + } + +private: + void onDataWritten(const GattWriteCallbackParams *params) { + if (params->handle == countdownChar.getValueHandle()) { + onCountdownReset(GetCountdown()); + } + } + +private: + GattServer &server; + ReadWriteGattCharacteristic<uint32_t> countdownChar; + Callback<void(uint32_t)> onCountdownReset; +}; +#endif \ No newline at end of file