Just a quick custom service
Diff: CountdownService.h
- Revision:
- 1:79a62e074c35
- Parent:
- 0:bc6cd13ebbdb
diff -r bc6cd13ebbdb -r 79a62e074c35 CountdownService.h --- a/CountdownService.h Fri Nov 23 08:59:41 2018 +0000 +++ b/CountdownService.h Fri Feb 15 11:27:45 2019 +0000 @@ -10,46 +10,48 @@ #include "ble/BLE.h" +/** definitions for UUID's */ +static const char* UUID_COUNTDOWN_SERVICE = "8ca81a4b-6182-4b04-ba9c-e4002a4a7a8b"; // UUID of the contdown service +static const char* UUID_W_CHARACTERISTIC = "6a9e371d-a3fc-4a34-bd21-ad887188532c"; // UUID of the time characteristic +static UUID UUID_Service(UUID_COUNTDOWN_SERVICE); +static UUID UUID_WCharacteristic(UUID_W_CHARACTERISTIC); + +typedef struct Buffer { + uint8_t data[512]; +} Buffer; + 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): +public: + CountdownService(GattServer &server, Callback<void(uint8_t*, uint16_t)> w_changed_callback): server(server), - countdownChar(UUID_TimeCharacteristic,0), - onCountdownReset(countdown_changed_callback) { + wChar(UUID_WCharacteristic, NULL), + onWCharWritten(w_changed_callback) { - GattCharacteristic *charTable[] = {&countdownChar}; + GattCharacteristic *charTable[] = {&wChar}; 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; + void GetWCharValue(uint8_t *data, uint16_t *length) { + server.read(wChar.getValueHandle(), data, length); } private: void onDataWritten(const GattWriteCallbackParams *params) { - if (params->handle == countdownChar.getValueHandle()) { - onCountdownReset(GetCountdown()); + if (params->handle == wChar.getValueHandle()) { + uint16_t length; + uint8_t data[512]; + GetWCharValue(data, &length); + onWCharWritten(data, length); } } private: GattServer &server; - ReadWriteGattCharacteristic<uint32_t> countdownChar; - Callback<void(uint32_t)> onCountdownReset; + WriteOnlyGattCharacteristic<Buffer> wChar; + Callback<void(uint8_t, 512)> onWCharWritten; }; #endif \ No newline at end of file