iotio

Fork of Nucleo_BLE_DemoApp by Cortex Challenge Team

Revision:
0:866f2c528c01
Child:
1:fd7adeffebbe
diff -r 000000000000 -r 866f2c528c01 DemoAppService.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DemoAppService.h	Sun May 10 15:41:38 2015 +0000
@@ -0,0 +1,71 @@
+#ifndef __BLE_DEMOAPP_SERVICE_H__
+#define __BLE_DEMOAPP_SERVICE_H__
+
+#include "Stream.h"
+
+#include "UUID.h"
+#include "BLEDevice.h"
+#include "Utils.h"
+
+class DemoAppService {
+    public:
+        static const uint8_t ServiceUUID[LENGTH_OF_LONG_UUID];
+        static const uint8_t slider1CharacteristicUUID[LENGTH_OF_LONG_UUID];
+        static const uint8_t slider2CharacteristicUUID[LENGTH_OF_LONG_UUID];
+        static const uint8_t buttonCharacteristicUUID[LENGTH_OF_LONG_UUID];
+    protected:
+        BLEDevice &ble;
+        
+        GattCharacteristic slider1Characteristic;
+        GattCharacteristic slider2Characteristic;
+        GattCharacteristic buttonCharacteristic;
+        
+        uint16_t lastButtonPressed;
+        uint16_t slider1Value;
+        uint16_t slider2Value;
+    public:
+        DemoAppService(BLEDevice &_ble) :
+            ble(_ble),
+            slider1Characteristic(slider1CharacteristicUUID, (uint8_t*)&slider1Value, 1, 2,
+                         GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE),
+            slider2Characteristic(slider2CharacteristicUUID, (uint8_t*)&slider2Value, 1, 2,
+                         GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE),
+            buttonCharacteristic(buttonCharacteristicUUID, (uint8_t*)&lastButtonPressed, 1, 2,
+                         GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE){
+                             
+            GattCharacteristic *charTable[] = {&slider1Characteristic, &slider2Characteristic, &buttonCharacteristic};
+            GattService         demoService(ServiceUUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
+    
+            ble.addService(demoService);
+            ble.onDataWritten(this, &DemoAppService::onDataWritten);
+        }
+        
+        virtual void onDataWritten(const GattCharacteristicWriteCBParams *params) {
+            DEBUG("Demo service onWrite\n\r");
+            if (params->charHandle == slider1Characteristic.getValueAttribute().getHandle()) {
+                uint16_t bytesRead = params->len;
+                if (bytesRead == 2) {
+                    memcpy(&slider1Value, params->data, 2);
+                    DEBUG("Slider1: %d\n\r", slider1Value);
+                }
+            }
+            else if (params->charHandle == slider2Characteristic.getValueAttribute().getHandle()) {
+                uint16_t bytesRead = params->len;
+                if (bytesRead == 2) {
+                    memcpy(&slider2Value, params->data, 2);
+                    DEBUG("Slider2: %d\n\r", slider2Value);
+                }
+            }
+            else if (params->charHandle == buttonCharacteristic.getValueAttribute().getHandle()) {
+                uint16_t bytesRead = params->len;
+                if (bytesRead == 2) {
+                    memcpy(&lastButtonPressed, params->data, 2);
+                }
+            }
+        }
+        
+        uint16_t getSlider1Value()const{return slider1Value;}
+        uint16_t getSlider2Value()const{return slider2Value;}
+};
+
+#endif
\ No newline at end of file