Jyri Lehtinen / SersorCharacteristic
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SensorService.h Source File

SensorService.h

00001 #ifndef SENSOR_SERVICE_H
00002 #define SENSOR_SERVICE_H
00003 
00004 #include "BLE.h"
00005 #include "UUID.h"
00006 //#include "Utils.h"
00007 
00008 const UUID::LongUUIDBytes_t SENS_SERVICE_UUID = {0x00,0x00,0x00,0x00,0x00,0x01,0x11,0xe1,0x9a,0xb4,0x00,0x02,0xa5,0xd5,0xc5,0x1b}; //Primary service
00009 const UUID::LongUUIDBytes_t VOLT_CHAR_UUID = {0x40,0x00,0x00,0x00,0x00,0x01,0x11,0xe1,0xac,0x36,0x00,0x02,0xa5,0xd5,0xc5,0x1b}; //Voltage characteristic
00010 const UUID::LongUUIDBytes_t CURR_CHAR_UUID = {0x08,0x00,0x00,0x00,0x00,0x01,0x11,0xe1,0xac,0x36,0x00,0x02,0xa5,0xd5,0xc5,0x1b}; //Current characteristic
00011 
00012 #define VOLT_DATA_LEN 2+4 //TimeStamp(uint16_t) + VoltageData(uint32_t)
00013 #define CURR_DATA_LEN 2+4 //TimeStamp(uint16_t) + VoltageData(uint32_t)
00014 
00015 class SensorService
00016 {
00017     public:
00018        SensorService(BLE &_ble) :
00019             ble(_ble),
00020             voltage(), //six byte array
00021             voltageCharacteristic(VOLT_CHAR_UUID, voltage, VOLT_DATA_LEN, VOLT_DATA_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
00022             currentCharacteristic(CURR_CHAR_UUID, current, CURR_DATA_LEN, CURR_DATA_LEN, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ | GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)
00023             {
00024                 static bool serviceAdded = false; //To be sure to define just once
00025                 if(serviceAdded)
00026                     { return; }
00027                 GattCharacteristic *charTable[] = {&voltageCharacteristic, &currentCharacteristic};
00028                 GattService sensorService(SENS_SERVICE_UUID, charTable, sizeof(charTable)/sizeof (GattCharacteristic *));
00029 
00030                 ble.addService(sensorService);
00031                 memset(voltage, 0, VOLT_DATA_LEN);
00032                 memset(current, 0, CURR_DATA_LEN);
00033                 serviceAdded = true;
00034             }
00035             
00036         //Voltage GATT server functions
00037             void sendVoltage (uint32_t volt_value, uint16_t TimeStamp)
00038             {
00039                 memcpy(&voltage, &TimeStamp, 2);
00040                 memcpy(&voltage+2, &volt_value, 4);
00041                 uint16_t volt_handle = voltageCharacteristic.getValueAttribute().getHandle();
00042                 printf("Send voltage handle: %d\n", volt_handle);
00043                 memcpy(pastVoltage, voltage, VOLT_DATA_LEN);
00044                 ble.gattServer().write(volt_handle, voltage, VOLT_DATA_LEN, 0);
00045                 printf("Update\n");
00046             }
00047             
00048             void updateVoltage(uint32_t volt_value, uint16_t TimeStamp)
00049             {
00050                 if(ble.getGapState().connected && isEnabledVoltageNotify)
00051                 {
00052                     if(memcmp(&pastVoltage[2], &volt_value, 4) != 0)
00053                     {
00054                         sendVoltage(volt_value, TimeStamp);
00055                     }
00056                 }
00057             }
00058             
00059           //Current GATT server functions
00060             void sendCurrent (uint32_t curr_value, uint16_t TimeStamp)
00061             {
00062                 memcpy(&current, &TimeStamp, 2);
00063                 memcpy(&current+2, &curr_value, 4);
00064                 uint16_t curr_handle = currentCharacteristic.getValueAttribute().getHandle();
00065                 printf("Send current handle: %d\n", curr_handle);
00066                 memcpy(pastCurrent, current, CURR_DATA_LEN);
00067                 ble.gattServer().write(curr_handle, current, CURR_DATA_LEN, 0);
00068                 printf("Update\n");
00069             }
00070             
00071             void updateCurrent(uint32_t curr_value, uint16_t TimeStamp)
00072             {
00073                 if(ble.getGapState().connected && isEnabledCurrentNotify)
00074                 {
00075                     if(memcmp(&pastCurrent[2], &curr_value, 4) != 0)
00076                     {
00077                         sendCurrent(curr_value, TimeStamp);
00078                     }
00079                 }
00080             }  
00081             
00082             
00083             void enNotify (Gap::Handle_t handle)
00084             {
00085  //               printf("\n\r\n\renNotify, Handle: 0x%02X\n", handle);
00086                 if (isVoltageHandle(handle))
00087                 {
00088                     isEnabledVoltageNotify = true;
00089                     memset(voltage,0,VOLT_DATA_LEN);
00090                     memset(pastVoltage, 0, VOLT_DATA_LEN);
00091                     return;
00092                 }
00093                 
00094                 if (isCurrentHandle(handle))
00095                 {
00096                     isEnabledCurrentNotify = true;
00097                     memset(current,0,CURR_DATA_LEN);
00098                     memset(pastCurrent, 0, CURR_DATA_LEN);
00099                     return;
00100                 }                    
00101             }
00102 
00103     void disNotify (Gap::Handle_t handle)
00104             {
00105  //               printf("\n\r\n\rdisNotify, Handle: 0x%02X\n", handle);
00106                 if (isVoltageHandle(handle)) { isEnabledVoltageNotify = false; memset(voltage,0,VOLT_DATA_LEN); return; }
00107                 if (isCurrentHandle(handle)) { isEnabledCurrentNotify = false; memset(current,0,CURR_DATA_LEN); return; }          
00108             }
00109     
00110 //----    
00111       
00112     bool isVoltageNotificationEn (void)
00113     {
00114             return isEnabledVoltageNotify;
00115     }  
00116  
00117             
00118     bool isVoltageHandle(Gap::Handle_t handle)
00119             {
00120                 if(handle == voltageCharacteristic.getValueAttribute().getHandle())
00121                     { return true; }
00122                 return false;
00123             }
00124             
00125      bool isCurrentNotificationEn (void)
00126     {
00127             return isEnabledCurrentNotify;
00128     }  
00129  
00130             
00131     bool isCurrentHandle(Gap::Handle_t handle)
00132             {
00133                 if(handle == currentCharacteristic.getValueAttribute().getHandle())
00134                     { return true; }
00135                 return false;
00136             }
00137     
00138     protected:
00139         BLE &ble;
00140         uint8_t voltage[VOLT_DATA_LEN];
00141         uint8_t pastVoltage[VOLT_DATA_LEN];
00142         uint8_t current[CURR_DATA_LEN];
00143         uint8_t pastCurrent[CURR_DATA_LEN];
00144         
00145         GattCharacteristic voltageCharacteristic;
00146         GattCharacteristic currentCharacteristic;
00147         
00148         bool                 isEnabledVoltageNotify;
00149         bool                 isEnabledCurrentNotify;
00150 };
00151 
00152 #endif