A library for easier setup and prototyping of IoT devices (pucks), by collecting everything that is common for all pucks in one place.

Dependencies:   BLE_API nRF51822

Fork of Puck by Nordic Pucks

Revision:
14:9eda2d99fc1d
Parent:
13:93d3574e9e36
--- a/Puck.h	Mon Aug 11 09:42:01 2014 +0000
+++ b/Puck.h	Mon Feb 23 15:25:22 2015 +0000
@@ -19,8 +19,9 @@
 #define __PUCK_HPP__
  
 #include "BLEDevice.h"
+#include "mbed.h"
+#include "Log.h"
 #include <vector>
-#include "Log.h"
 
 enum PuckState {
     CONNECTING,
@@ -82,18 +83,21 @@
     return _puckSingletonInstance;
 }
 
-void onDisconnection(Gap::Handle_t handle) {
+void onDisconnection(Gap::Handle_t handle, Gap::DisconnectionReason_t disconnectReason) {
     LOG_INFO("Disconnected.\n");
     Puck::getPuck().setState(DISCONNECTED);
 }
 
-void onConnection(Gap::Handle_t handle) {
+void onConnection(Gap::Handle_t handle,
+        Gap::addr_type_t peerAddrType,
+        const Gap::address_t peerAddr,
+        const Gap::ConnectionParams_t * connectionParams) {
     LOG_INFO("Connected.\n");
     Puck::getPuck().setState(CONNECTED);
 }
 
-void onDataWrittenCallback(uint16_t handle) {
-    Puck::getPuck().onDataWritten(handle);
+void onDataWrittenCallback(const GattCharacteristicWriteCBParams *context) {
+    Puck::getPuck().onDataWritten(context->charHandle);
 }
 
 bool isEqualUUID(const UUID* uuidA, const UUID uuidB) {
@@ -120,7 +124,7 @@
 }
 
 void Puck::disconnect() {
-    ble.disconnect();    
+    ble.disconnect(Gap::LOCAL_HOST_TERMINATED_CONNECTION);    
 }
 
 int Puck::countFreeMemory() {
@@ -271,17 +275,17 @@
     LOG_DEBUG("Added characteristic.\n");
 }
 
-
 void Puck::updateCharacteristicValue(const UUID uuid, uint8_t* value, int length) {
     GattCharacteristic* characteristic = NULL;
-    for( int i = 0; i < characteristics.size(); i++) {
-        if(isEqualUUID(&characteristics[i]->getUUID(), uuid)) {
+    for(int i = 0; i < characteristics.size(); i++) {
+        GattAttribute &gattAttribute = characteristics[i]->getValueAttribute();
+        if(isEqualUUID(&gattAttribute.getUUID(), uuid)) {
             characteristic = characteristics[i];
-            break;    
-        }    
+            break;
+        }
     }
     if(characteristic != NULL) {
-        ble.updateCharacteristicValue(characteristic->getHandle(), value, length);
+        ble.updateCharacteristicValue(characteristic->getValueHandle(), value, length);
         LOG_VERBOSE("Updated characteristic value.\n");
     } else {
         LOG_WARN("Tried to update an unkown characteristic!\n");    
@@ -289,10 +293,12 @@
 }
 
 bool Puck::drive() {
+    if(state == DISCONNECTED) {
+        startAdvertising();
+    }
+
     ble.waitForEvent();
-    if(state == DISCONNECTED) {
-        startAdvertising();    
-    }
+
     while(pendingCallbackStack.size() > 0) {
         pendingCallbackStack.back()(pendingCallbackParameterStack.back());
         pendingCallbackStack.pop_back();
@@ -327,9 +333,9 @@
 uint8_t* Puck::getCharacteristicValue(const UUID uuid) {
     LOG_VERBOSE("Reading characteristic value for UUID %x\n", uuid);
     for(int i = 0; i < characteristics.size(); i++) {
-        GattCharacteristic* characteristic = characteristics[i];
-        if(isEqualUUID(&characteristic->getUUID(), uuid)) {
-            return characteristic->getValuePtr();
+        GattAttribute &gattAttribute = characteristics[i]->getValueAttribute();
+        if(isEqualUUID(&gattAttribute.getUUID(), uuid)) {
+            return gattAttribute.getValuePtr();
         }
     }
     LOG_WARN("Tried to read an unknown characteristic!");
@@ -340,15 +346,17 @@
 void Puck::onDataWritten(uint16_t handle) {
     for (int i = 0; i < characteristics.size(); i++) {
         GattCharacteristic* characteristic = characteristics[i];
-        if (characteristic->getHandle() == handle) {
-            uint16_t maxLength = characteristic->getMaxLength();
-            ble.readCharacteristicValue(handle, characteristic->getValuePtr(), &maxLength);
+        
+        if (characteristic->getValueHandle() == handle) {
+            GattAttribute &gattAttribute = characteristic->getValueAttribute();
+
             for(int j = 0; j < writeCallbacks.size(); j++) {    
                 CharacteristicWriteCallbacks* characteristicWriteCallbacks = writeCallbacks[j];
-                if(isEqualUUID(characteristicWriteCallbacks->uuid, characteristic->getUUID())) {
+
+                if(isEqualUUID(characteristicWriteCallbacks->uuid, gattAttribute.getUUID())) {
                     for(int k = 0; k < characteristicWriteCallbacks->callbacks->size(); k++) {
                         pendingCallbackStack.push_back(characteristicWriteCallbacks->callbacks->at(k));
-                        pendingCallbackParameterStack.push_back(characteristic->getValuePtr());
+                        pendingCallbackParameterStack.push_back(gattAttribute.getValuePtr());
                     }
                     return;
                 }
@@ -358,5 +366,4 @@
 }
 
 
-
  #endif // __PUCK_HPP__
\ No newline at end of file