Dependencies:   AccelSensor BLE_API mbed nRF51822

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 1000000 // us
00010 #define CTR12V_TIME 100000 // us
00011 
00012 class RELAYService {
00013 public:
00014     const static uint16_t RELAY_SERVICE_UUID = 0xC000;
00015     const static uint16_t RELAY_STATE_CHARACTERISTIC_UUID = 0xC001;
00016 
00017     RELAYService(BLEDevice &_ble, ImobStateService * imobStateServicePtr) : 
00018         ble(_ble),
00019         relayState(0),
00020         actuatedRelay(P0_10,0),
00021         Ctr12v(P0_3,0),        
00022         ISS(imobStateServicePtr),
00023         RelayCharacteristic(RELAY_STATE_CHARACTERISTIC_UUID, &relayState, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)
00024     {
00025         GattCharacteristic *charTable[] = {&RelayCharacteristic};
00026         GattService relayService(RELAY_SERVICE_UUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
00027 
00028         ble.addService(relayService);
00029 
00030         ble.gap().onDisconnection(this, &RELAYService::onDisconnectionFilter);
00031         ble.gattServer().onDataWritten(this, &RELAYService::onDataWritten);
00032     }
00033 
00034     GattAttribute::Handle_t getValueHandle() const 
00035     {
00036         return RelayCharacteristic.getValueHandle();
00037     }
00038     
00039     void updateRelayState(uint8_t newRelayState) {
00040         relayState = newRelayState;
00041         actuatedRelay = newRelayState;
00042         ble.gattServer().write(RelayCharacteristic.getValueHandle(), &relayState, 1);
00043     }
00044     
00045     void activate()
00046     {
00047         Ctr12v = 1;
00048         wait_us(CTR12V_TIME);
00049         updateRelayState(1);
00050         wait_us(RELAY_TIME);//depending of the time switching desired
00051         updateRelayState(0);
00052         wait_us(CTR12V_TIME);
00053         Ctr12v = 0;   
00054     }
00055 
00056 protected:
00057 
00058     void onDisconnectionFilter(const Gap::DisconnectionCallbackParams_t *params)
00059     {   
00060         if(authenticated && activated)
00061         {
00062             activate();
00063             //ISS->resetAuthenticationValues();
00064         }
00065     }
00066     
00067     virtual void onDataWritten(const GattWriteCallbackParams *params)
00068     {          
00069         if ((params->handle == RelayCharacteristic.getValueHandle()) && (params->len == 1) && authenticated)
00070         {
00071             activate();
00072             ISS->resetAuthenticationValues();
00073             
00074             if(!activated)
00075                 ISS->updateActivationValue(1);
00076         }
00077         else
00078             updateRelayState(0);
00079     }     
00080 
00081 private:
00082     
00083     BLEDevice &ble;
00084     uint8_t relayState;
00085     DigitalOut actuatedRelay;
00086     DigitalOut Ctr12v;
00087     
00088     ImobStateService * ISS;
00089 
00090     ReadWriteGattCharacteristic<uint8_t> RelayCharacteristic;
00091 };
00092 
00093 #endif /* #ifndef __BLE_RELAY_SERVICE_H__ */