BLE Library with custom services for the tortuga bike

Dependents:   TORTUGA_BLE

Fork of BLE_API by aapje monkey

Revision:
1205:86f0783a5420
Parent:
1204:c87f50212c84
Child:
1206:8afbec0520f5
--- a/ble/services/BikeService.h	Tue Jul 05 12:17:18 2016 +0000
+++ b/ble/services/BikeService.h	Tue Jul 12 11:55:13 2016 +0000
@@ -2,6 +2,7 @@
 #define __BLE_BIKE_SERVICE_H__
 
 #include "BikeData.h"
+#include "ble/BLE.h"
 class BikeService {
 public:
 /*
@@ -22,27 +23,29 @@
         0x6973: pulses in last second
         0x6974: Speed
         0x6975: Control
+        0x6976: Device Name
     */
 
 
     /*defining the UUID's for the service and characters*/
-    const static uint16_t BIKE_SERVICE_UUID              = 0x3969;
+    const static uint16_t BIKE_SERVICE_UUID              = 0x6969;
     
-    const static uint16_t TRIP_DISTANCE_CHARACTERISTIC_UUID = 0x396A;
-    const static uint16_t TRIP_AVERAGE_CHARACTERISTIC_UUID = 0x396B;
-    const static uint16_t TRIP_TIME_CHARACTERISTIC_UUID = 0x396C;
+    const static uint16_t TRIP_DISTANCE_CHARACTERISTIC_UUID = 0x696A;
+    const static uint16_t TRIP_AVERAGE_CHARACTERISTIC_UUID = 0x696B;
+    const static uint16_t TRIP_TIME_CHARACTERISTIC_UUID = 0x696C;
     
-    const static uint16_t OVERALL_DISTANCE_CHARACTERISTIC_UUID = 0x396D;
-    const static uint16_t OVERALL_AVERAGE_CHARACTERISTIC_UUID = 0x396E;
-    const static uint16_t OVERALL_TIME_CHARACTERISTIC_UUID = 0x396F;
+    const static uint16_t OVERALL_DISTANCE_CHARACTERISTIC_UUID = 0x696D;
+    const static uint16_t OVERALL_AVERAGE_CHARACTERISTIC_UUID = 0x696E;
+    const static uint16_t OVERALL_TIME_CHARACTERISTIC_UUID = 0x696F;
     
-    const static uint16_t BATCHANGE_DISTANCE_CHARACTERISTIC_UUID = 0x3970;
-    const static uint16_t BATCHANGE_AVERAGE_CHARACTERISTIC_UUID = 0x3971;
-    const static uint16_t BATCHANGE_TIME_CHARACTERISTIC_UUID = 0x3972;
+    const static uint16_t BATCHANGE_DISTANCE_CHARACTERISTIC_UUID = 0x6970;
+    const static uint16_t BATCHANGE_AVERAGE_CHARACTERISTIC_UUID = 0x6971;
+    const static uint16_t BATCHANGE_TIME_CHARACTERISTIC_UUID = 0x6972;
     
-    const static uint16_t PULSE_CHARACTERISTIC_UUID = 0x3973;
-    const static uint16_t SPEED_CHARACTERISTIC_UUID = 0x3974;
-    const static uint16_t CONTROL_CHARACTERISTIC_UUID = 0x3975;
+    const static uint16_t PULSE_CHARACTERISTIC_UUID = 0x6973;
+    const static uint16_t SPEED_CHARACTERISTIC_UUID = 0x6974;
+    const static uint16_t CONTROL_CHARACTERISTIC_UUID = 0x6975;
+    const static uint16_t DEVICE_NAME_CHARACTERISTIC_UUID = 0x6976;
     
     //CONSTRUCTOR
     BikeService(BLE &_ble, BikeData *bikeData) :
@@ -64,7 +67,22 @@
                 pulseChar(PULSE_CHARACTERISTIC_UUID,&pulse, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
                 speedChar(SPEED_CHARACTERISTIC_UUID,&speed, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
                 controlChar(CONTROL_CHARACTERISTIC_UUID,&control, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)
+                /*deviceNameChar(DEVICE_NAME_CHARACTERISTIC_UUID,
+                                              (uint8_t *)bd->getBikeName(),
+                                              bd->getBikeNameSize(), 
+                                              bd->getBikeNameSize(), 
+                                              GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE)*/
     {
+        static char devName[] = "tmp";
+        bd->getBikeName(devName);
+        deviceNameChar = new GattCharacteristic(DEVICE_NAME_CHARACTERISTIC_UUID,
+                                              (uint8_t *)devName,
+                                              bd->getBikeNameSize(), 
+                                              bd->getBikeNameSize(), 
+                                              GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE);
+        /*static char devName[] = "tmp";
+        bd->getBikeName(devName);
+        ble.gattServer().write(deviceNameChar.getValueHandle(),(uint8_t *)devName,bd->getBikeNameSize());*/
         //read al the values out of the BikeData object
         tripDistance = (bd->getDataSet(BikeData::TRIP))->getDistance() *100;
         tripAverage = (bd->getDataSet(BikeData::TRIP))->getAverage() *100;
@@ -86,17 +104,17 @@
         GattCharacteristic *charTable[] = {&tripDistanceChar, &tripAverageChar, &tripTimeChar,
                                            &overallDistanceChar, &overallAverageChar, &overallTimeChar,
                                            &batChangeDistanceChar, &batChangeAverageChar, &batChangeTimeChar,
-                                           &pulseChar, &speedChar, &controlChar};
+                                           &pulseChar, &speedChar, &controlChar, deviceNameChar};
         //creating the gattService
         GattService         bikeService(BikeService::BIKE_SERVICE_UUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
         ble.gattServer().addService(bikeService);
-        //define the function to call when data is written
+        //define the function to call when data is written to service
         ble.onDataWritten(this, &BikeService::onDataWrittenCallback);
     }
     
     void onDataWrittenCallback(const GattWriteCallbackParams *writeParams) {
         //when data is written in the controlCharacteristic
-        printf("data");
+        printf("data\n");
         if (writeParams->handle == controlChar.getValueHandle()) {
                 //performing different actions according to the written value
                 if (*(writeParams->data) == '1'){
@@ -109,6 +127,26 @@
                     bd->stopTrip();
                 }
             }
+            
+            if (writeParams->handle == deviceNameChar->getValueHandle()) {
+                printf("name changing, length: %i\n",(uint8_t)writeParams->len);
+                //uint8_t *recievedName = writeParams->data;
+                //writeParams->data->push_back(0x00);
+                uint8_t recievedName[writeParams->len +1];
+                for (uint8_t i = 0 ; i < writeParams->len ; i++){
+                    recievedName[i] = *(writeParams->data+i);
+                    }
+                recievedName[writeParams->len] = 0x00;
+                bd->setBikeName((char *)recievedName,((uint8_t)writeParams->len)+1);
+                ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)recievedName, (writeParams->len)+1);//passing the device name
+    
+                printf("written length: %i\n", bd->getBikeNameSize());
+                static char devName[] = "tmp";
+                bd->getBikeName(devName);
+                ble.gattServer().write(deviceNameChar->getValueHandle(),(uint8_t *)devName,bd->getBikeNameSize());
+                /*char name[] = "otherName";
+                bd->setBikeName(name,sizeof(name));*/
+            }
     }
     
  
@@ -210,6 +248,9 @@
     ReadOnlyGattCharacteristic<uint8_t>  pulseChar;
     ReadOnlyGattCharacteristic<uint32_t>  speedChar;
     ReadWriteGattCharacteristic<uint8_t>  controlChar;
+    
+    
+    GattCharacteristic  *deviceNameChar;
 };
  
 #endif
\ No newline at end of file