Just a quick custom service

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers CountdownService.h Source File

CountdownService.h

00001 /*  code is property of aconno.de */
00002 
00003 
00004 #ifndef COUNTDOWN_SERVICE
00005 #define COUNTDOWN_SERVICE
00006 
00007 #include <stdbool.h>
00008 #include "mbed.h"
00009 #include <events/mbed_events.h>
00010 #include "ble/BLE.h"
00011 
00012 
00013 /** definitions for UUID's */
00014 static const char* UUID_COUNTDOWN_SERVICE = "8ca81a4b-6182-4b04-ba9c-e4002a4a7a8b";    // UUID of the contdown service
00015 static const char* UUID_W_CHARACTERISTIC = "6a9e371d-a3fc-4a34-bd21-ad887188532c";  // UUID of the time characteristic
00016 static UUID UUID_Service(UUID_COUNTDOWN_SERVICE);
00017 static UUID UUID_WCharacteristic(UUID_W_CHARACTERISTIC);
00018 
00019 typedef struct Buffer {
00020     uint8_t data[512];
00021 } Buffer;
00022 
00023 class CountdownService {
00024 typedef CountdownService Self;
00025 public:    
00026     CountdownService(GattServer &server, Callback<void(uint8_t*, uint16_t)> w_changed_callback):
00027     server(server),
00028     wChar(UUID_WCharacteristic, NULL),
00029     onWCharWritten(w_changed_callback) {
00030         
00031         GattCharacteristic *charTable[] = {&wChar};
00032         GattService service(UUID_Service, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
00033         
00034         server.addService(service); 
00035         server.onDataWritten(this, &CountdownService::onDataWritten);
00036     }
00037     
00038     void GetWCharValue(uint8_t *data, uint16_t *length) {
00039         server.read(wChar.getValueHandle(), data, length);
00040     }
00041     
00042 private:
00043     void onDataWritten(const GattWriteCallbackParams *params) {
00044         if (params->handle == wChar.getValueHandle()) {
00045             uint16_t length;
00046             uint8_t data[512];
00047             GetWCharValue(data, &length);
00048             onWCharWritten(data, length);
00049         }
00050     }
00051     
00052 private:
00053     GattServer &server;
00054     WriteOnlyGattCharacteristic<Buffer> wChar;
00055     Callback<void(uint8_t, 512)> onWCharWritten;
00056 };
00057 #endif