Dependencies:   AccelSensor BLE_API mbed nRF51822

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers InternalValuesService.h Source File

InternalValuesService.h

00001 #ifndef __BLE_INTERNAL_VALUES_SERVICE_H__
00002 #define __BLE_INTERNAL_VALUES_SERVICE_H__
00003 
00004 #include "mbed.h"
00005 #include "ble/BLE.h"
00006 #include "ble/Gap.h"
00007 #include "ImobStateService.h"
00008 
00009 class InternalValuesService {
00010 public:
00011     const static uint16_t INTERNAL_VALUES_SERVICE_UUID = 0xB000;
00012     const static uint16_t LIPOCHARGER_STATE_CHARACTERISTIC_UUID = 0xB001;
00013     const static uint16_t CONTACT_STATE_CHARACTERISTIC_UUID = 0xB002;
00014     const static uint16_t ID1_CHARACTERISTIC_UUID = 0xB003;
00015     const static uint16_t ID2_CHARACTERISTIC_UUID = 0xB004;
00016     const static uint16_t CPC_CHARACTERISTIC_UUID = 0xB005;
00017     const static uint16_t DPC_CHARACTERISTIC_UUID = 0xB006;
00018 
00019     InternalValuesService(BLEDevice &_ble, ImobStateService * imobStateServicePtr) : 
00020         ble(_ble),
00021         lipoChargerState(2),
00022         contactState(0),
00023         id1(NRF_FICR->DEVICEID[0]),
00024         id2(NRF_FICR->DEVICEID[1]),
00025         chargeProgramCycles(0),
00026         dischargeProgramCycles(0),
00027         ISS(imobStateServicePtr),
00028         LipoChargerCharacteristic(LIPOCHARGER_STATE_CHARACTERISTIC_UUID, &lipoChargerState, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
00029         ContactCharacteristic(CONTACT_STATE_CHARACTERISTIC_UUID, &contactState, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
00030         Id1Characteristic(ID1_CHARACTERISTIC_UUID, &id1),
00031         Id2Characteristic(ID2_CHARACTERISTIC_UUID, &id2),
00032         ChargeProgramCyclesCharacteristic(CPC_CHARACTERISTIC_UUID, &chargeProgramCycles),
00033         DischargeProgramCyclesCharacteristic(DPC_CHARACTERISTIC_UUID, &dischargeProgramCycles)
00034     {
00035         GattCharacteristic *charTable[] = {&LipoChargerCharacteristic, &ContactCharacteristic, &Id1Characteristic, &Id2Characteristic, &ChargeProgramCyclesCharacteristic, &DischargeProgramCyclesCharacteristic};
00036         GattService internalValuesService(INTERNAL_VALUES_SERVICE_UUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
00037 
00038         ble.addService(internalValuesService);
00039                
00040         uint8_t auxPass[PASSLEN];
00041         uint8_t auxKey[KEYLEN];
00042                         
00043         id1Array[0] = id1 >> 24;
00044         id1Array[1] = id1 >> 16;
00045         id1Array[2] = id1 >> 8;
00046         id1Array[3] = id1;
00047         
00048         id2Array[0] = id2 >> 24;
00049         id2Array[1] = id2 >> 16;
00050         id2Array[2] = id2 >> 8;
00051         id2Array[3] = id2;
00052         
00053         
00054         for(uint8_t i = 0; i < 8; i++)
00055             auxPass[i] = id1Array[i%4];
00056             
00057         for(uint8_t i = 8; i < PASSLEN; i++)
00058             auxPass[i] = id2Array[i%4];
00059         
00060         for(uint8_t i = 0; i < KEYLEN; i++)
00061             auxKey[i] = id2Array[i%4];
00062             
00063         ISS->setCorrectPass(auxPass);
00064         ISS->setCryptKey(auxKey);
00065         
00066         ble.gattServer().write(Id1Characteristic.getValueHandle(), id1Array, 4);
00067         ble.gattServer().write(Id2Characteristic.getValueHandle(), id2Array, 4);
00068     }
00069     
00070     void updateLipoChargerState(uint8_t newLipoChargerState)
00071     {
00072         lipoChargerState = newLipoChargerState;
00073         ble.gattServer().write(LipoChargerCharacteristic.getValueHandle(), &lipoChargerState, 1);
00074     }
00075     
00076     uint8_t getLipoChargerState()
00077     {
00078         return lipoChargerState;
00079     }
00080     
00081     void updateContactState(uint8_t newContactState)
00082     {
00083         contactState = newContactState;
00084         ble.gattServer().write(ContactCharacteristic.getValueHandle(), &contactState, 1);
00085     }
00086         
00087     void updateDischargeProgramCyclesCharacteristic()
00088     {
00089         dischargeProgramCyclesArray[0] = dischargeProgramCycles >> 24;
00090         dischargeProgramCyclesArray[1] = dischargeProgramCycles >> 16;
00091         dischargeProgramCyclesArray[2] = dischargeProgramCycles >> 8;
00092         dischargeProgramCyclesArray[3] = dischargeProgramCycles;
00093         
00094         ble.gattServer().write(DischargeProgramCyclesCharacteristic.getValueHandle(), dischargeProgramCyclesArray, 4);
00095         dischargeProgramCycles = 0;
00096     }
00097     
00098     void incrementDischargeProgramCycles()
00099     {
00100         dischargeProgramCycles++;
00101     }
00102     
00103     void updateChargeProgramCyclesCharacteristic()
00104     {
00105         chargeProgramCyclesArray[0] = chargeProgramCycles >> 24;
00106         chargeProgramCyclesArray[1] = chargeProgramCycles >> 16;
00107         chargeProgramCyclesArray[2] = chargeProgramCycles >> 8;
00108         chargeProgramCyclesArray[3] = chargeProgramCycles;
00109         
00110         ble.gattServer().write(ChargeProgramCyclesCharacteristic.getValueHandle(), chargeProgramCyclesArray, 4);
00111         chargeProgramCycles = 0;
00112     }
00113     
00114     void incrementChargeProgramCycles()
00115     {
00116         chargeProgramCycles++;
00117     }        
00118     
00119 private:
00120     BLEDevice &ble;
00121     uint8_t lipoChargerState;
00122     uint8_t contactState;
00123     
00124     uint32_t id1;
00125     uint32_t id2;    
00126     uint32_t chargeProgramCycles;
00127     uint32_t dischargeProgramCycles;    
00128     
00129     uint8_t id1Array[4];    
00130     uint8_t id2Array[4];
00131     uint8_t chargeProgramCyclesArray[4];
00132     uint8_t dischargeProgramCyclesArray[4];
00133             
00134     ImobStateService * ISS;
00135     
00136     ReadOnlyGattCharacteristic < uint8_t > LipoChargerCharacteristic;
00137     ReadOnlyGattCharacteristic < uint8_t > ContactCharacteristic;
00138         
00139     ReadOnlyGattCharacteristic < uint32_t > Id1Characteristic;
00140     ReadOnlyGattCharacteristic < uint32_t > Id2Characteristic;
00141     ReadOnlyGattCharacteristic < uint32_t > ChargeProgramCyclesCharacteristic;
00142     ReadOnlyGattCharacteristic < uint32_t > DischargeProgramCyclesCharacteristic;    
00143     
00144 };
00145 
00146 #endif /* #ifndef __BLE_INTERNAL_VALUES_SERVICE_H__ */