Oscar Silva / Mbed 2 deprecated IMOB_without_encryption_txpowermax

Dependencies:   mbedtls mbed BLE_API nRF51822 AccelSensor

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers RELAYService.h Source File

RELAYService.h

00001 #ifndef __BLE_RELAY_SERVICE_H__
00002 #define __BLE_RELAY_SERVICE_H__
00003 
00004 #include "mbed.h"
00005 #include "ble/BLE.h"
00006 #include "ble/Gap.h"
00007 #include "ImobStateService.h"
00008 
00009 #define RELAY_TIME 120000000 // us
00010 #define CTR12V_TIME 100000 // us
00011 
00012 Ticker waitTicker;
00013 bool activation_in_progress = false;
00014 
00015 
00016 
00017 class RELAYService {
00018 public:
00019     const static uint16_t RELAY_SERVICE_UUID = 0xC000;
00020     const static uint16_t RELAY_STATE_CHARACTERISTIC_UUID = 0xC001;
00021 
00022     RELAYService(BLEDevice &_ble, ImobStateService * imobStateServicePtr) : 
00023         ble(_ble),
00024         relayState(0),
00025         actuatedRelay(P0_10,0),
00026         Ctr12v(P0_3,0),        
00027         ISS(imobStateServicePtr),
00028         RelayCharacteristic(RELAY_STATE_CHARACTERISTIC_UUID, &relayState, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)
00029     {
00030         GattCharacteristic *charTable[] = {&RelayCharacteristic};
00031         GattService relayService(RELAY_SERVICE_UUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
00032 
00033         ble.addService(relayService);
00034 
00035         ble.gap().onDisconnection(this, &RELAYService::onDisconnectionFilter);
00036         ble.gattServer().onDataWritten(this, &RELAYService::onDataWritten);
00037     }
00038 
00039     GattAttribute::Handle_t getValueHandle() const 
00040     {
00041         return RelayCharacteristic.getValueHandle();
00042     }
00043     
00044     void updateRelayState(uint8_t newRelayState) {
00045         relayState = newRelayState;
00046         actuatedRelay = newRelayState;
00047         ble.gattServer().write(RelayCharacteristic.getValueHandle(), &relayState, 1);
00048     }
00049     
00050     void activate()
00051     {
00052         if(!activation_in_progress)
00053         {
00054             flipCtr12v();       
00055             waitTicker.attach(callback(this, &RELAYService::internalUpdateRelaystate), CTR12V_TIME/1000000.0);
00056         }
00057     }
00058     
00059     
00060 
00061 protected:
00062 
00063     void onDisconnectionFilter(const Gap::DisconnectionCallbackParams_t *params)
00064     {   
00065         if(authenticated && activated)
00066         {
00067             activate();
00068             ISS->resetAuthenticationValues();
00069         }
00070     }
00071     
00072     virtual void onDataWritten(const GattWriteCallbackParams *params)
00073     {          
00074         if ((params->handle == RelayCharacteristic.getValueHandle()) && (params->len == 1) && authenticated)
00075         {
00076             activate();
00077             ISS->resetAuthenticationValues();
00078             
00079             if(!activated)
00080                 ISS->updateActivationValue(1);
00081         }
00082         //else
00083         //    updateRelayState(0);
00084     }     
00085 
00086 private:
00087 
00088     void internalUpdateRelaystate()
00089     {   
00090         uint8_t aux_relayState = (relayState) ? 0: 1;
00091         updateRelayState(aux_relayState);
00092         
00093         if(aux_relayState) 
00094         {
00095             waitTicker.detach();
00096             waitTicker.attach(callback(this, &RELAYService::internalUpdateRelaystate), RELAY_TIME/1000000.0);
00097         }
00098         else
00099         {
00100             waitTicker.detach();
00101             waitTicker.attach(callback(this, &RELAYService::flipCtr12v), CTR12V_TIME/1000000.0);
00102         }
00103     }
00104     
00105     void flipCtr12v()
00106     {
00107          Ctr12v = !Ctr12v;
00108          if(!Ctr12v)
00109          {
00110              waitTicker.detach();
00111              activation_in_progress = false;
00112          }
00113          else activation_in_progress = true;
00114     }
00115     
00116     BLEDevice &ble;
00117     uint8_t relayState;
00118     DigitalOut actuatedRelay;
00119     DigitalOut Ctr12v;
00120     
00121     ImobStateService * ISS;
00122 
00123     ReadWriteGattCharacteristic<uint8_t> RelayCharacteristic;
00124 };
00125 
00126 #endif /* #ifndef __BLE_RELAY_SERVICE_H__ */