imob

Dependencies:   mbedtls mbed BLE_API nRF51822 AccelSensor

Committer:
osilvam
Date:
Sat Mar 20 19:09:06 2021 +0000
Revision:
1:471d502617fe
Parent:
0:5284859bb3e8
last version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
osilvam 0:5284859bb3e8 1 #ifndef __BLE_RELAY_SERVICE_H__
osilvam 0:5284859bb3e8 2 #define __BLE_RELAY_SERVICE_H__
osilvam 0:5284859bb3e8 3
osilvam 0:5284859bb3e8 4 #include "mbed.h"
osilvam 0:5284859bb3e8 5 #include "ble/BLE.h"
osilvam 0:5284859bb3e8 6 #include "ble/Gap.h"
osilvam 0:5284859bb3e8 7 #include "ImobStateService.h"
osilvam 0:5284859bb3e8 8
osilvam 1:471d502617fe 9 #define RELAY_TIME 120000000 // us
osilvam 0:5284859bb3e8 10 #define CTR12V_TIME 100000 // us
osilvam 0:5284859bb3e8 11
osilvam 1:471d502617fe 12 Ticker waitTicker;
osilvam 1:471d502617fe 13 bool activation_in_progress = false;
osilvam 1:471d502617fe 14
osilvam 1:471d502617fe 15
osilvam 1:471d502617fe 16
osilvam 0:5284859bb3e8 17 class RELAYService {
osilvam 0:5284859bb3e8 18 public:
osilvam 0:5284859bb3e8 19 const static uint16_t RELAY_SERVICE_UUID = 0xC000;
osilvam 0:5284859bb3e8 20 const static uint16_t RELAY_STATE_CHARACTERISTIC_UUID = 0xC001;
osilvam 0:5284859bb3e8 21
osilvam 0:5284859bb3e8 22 RELAYService(BLEDevice &_ble, ImobStateService * imobStateServicePtr) :
osilvam 0:5284859bb3e8 23 ble(_ble),
osilvam 0:5284859bb3e8 24 relayState(0),
osilvam 0:5284859bb3e8 25 actuatedRelay(P0_10,0),
osilvam 0:5284859bb3e8 26 Ctr12v(P0_3,0),
osilvam 0:5284859bb3e8 27 ISS(imobStateServicePtr),
osilvam 0:5284859bb3e8 28 RelayCharacteristic(RELAY_STATE_CHARACTERISTIC_UUID, &relayState, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)
osilvam 0:5284859bb3e8 29 {
osilvam 0:5284859bb3e8 30 GattCharacteristic *charTable[] = {&RelayCharacteristic};
osilvam 0:5284859bb3e8 31 GattService relayService(RELAY_SERVICE_UUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
osilvam 0:5284859bb3e8 32
osilvam 0:5284859bb3e8 33 ble.addService(relayService);
osilvam 0:5284859bb3e8 34
osilvam 0:5284859bb3e8 35 ble.gap().onDisconnection(this, &RELAYService::onDisconnectionFilter);
osilvam 0:5284859bb3e8 36 ble.gattServer().onDataWritten(this, &RELAYService::onDataWritten);
osilvam 0:5284859bb3e8 37 }
osilvam 0:5284859bb3e8 38
osilvam 0:5284859bb3e8 39 GattAttribute::Handle_t getValueHandle() const
osilvam 0:5284859bb3e8 40 {
osilvam 0:5284859bb3e8 41 return RelayCharacteristic.getValueHandle();
osilvam 0:5284859bb3e8 42 }
osilvam 0:5284859bb3e8 43
osilvam 0:5284859bb3e8 44 void updateRelayState(uint8_t newRelayState) {
osilvam 0:5284859bb3e8 45 relayState = newRelayState;
osilvam 0:5284859bb3e8 46 actuatedRelay = newRelayState;
osilvam 0:5284859bb3e8 47 ble.gattServer().write(RelayCharacteristic.getValueHandle(), &relayState, 1);
osilvam 0:5284859bb3e8 48 }
osilvam 0:5284859bb3e8 49
osilvam 0:5284859bb3e8 50 void activate()
osilvam 0:5284859bb3e8 51 {
osilvam 1:471d502617fe 52 if(!activation_in_progress)
osilvam 1:471d502617fe 53 {
osilvam 1:471d502617fe 54 flipCtr12v();
osilvam 1:471d502617fe 55 waitTicker.attach(callback(this, &RELAYService::internalUpdateRelaystate), CTR12V_TIME/1000000.0);
osilvam 1:471d502617fe 56 }
osilvam 0:5284859bb3e8 57 }
osilvam 1:471d502617fe 58
osilvam 1:471d502617fe 59
osilvam 0:5284859bb3e8 60
osilvam 0:5284859bb3e8 61 protected:
osilvam 0:5284859bb3e8 62
osilvam 0:5284859bb3e8 63 void onDisconnectionFilter(const Gap::DisconnectionCallbackParams_t *params)
osilvam 0:5284859bb3e8 64 {
osilvam 0:5284859bb3e8 65 if(authenticated && activated)
osilvam 0:5284859bb3e8 66 {
osilvam 0:5284859bb3e8 67 activate();
osilvam 1:471d502617fe 68 ISS->resetAuthenticationValues();
osilvam 0:5284859bb3e8 69 }
osilvam 0:5284859bb3e8 70 }
osilvam 0:5284859bb3e8 71
osilvam 0:5284859bb3e8 72 virtual void onDataWritten(const GattWriteCallbackParams *params)
osilvam 0:5284859bb3e8 73 {
osilvam 0:5284859bb3e8 74 if ((params->handle == RelayCharacteristic.getValueHandle()) && (params->len == 1) && authenticated)
osilvam 0:5284859bb3e8 75 {
osilvam 0:5284859bb3e8 76 activate();
osilvam 0:5284859bb3e8 77 ISS->resetAuthenticationValues();
osilvam 0:5284859bb3e8 78
osilvam 0:5284859bb3e8 79 if(!activated)
osilvam 0:5284859bb3e8 80 ISS->updateActivationValue(1);
osilvam 0:5284859bb3e8 81 }
osilvam 1:471d502617fe 82 //else
osilvam 1:471d502617fe 83 // updateRelayState(0);
osilvam 0:5284859bb3e8 84 }
osilvam 0:5284859bb3e8 85
osilvam 0:5284859bb3e8 86 private:
osilvam 1:471d502617fe 87
osilvam 1:471d502617fe 88 void internalUpdateRelaystate()
osilvam 1:471d502617fe 89 {
osilvam 1:471d502617fe 90 uint8_t aux_relayState = (relayState) ? 0: 1;
osilvam 1:471d502617fe 91 updateRelayState(aux_relayState);
osilvam 1:471d502617fe 92
osilvam 1:471d502617fe 93 if(aux_relayState)
osilvam 1:471d502617fe 94 {
osilvam 1:471d502617fe 95 waitTicker.detach();
osilvam 1:471d502617fe 96 waitTicker.attach(callback(this, &RELAYService::internalUpdateRelaystate), RELAY_TIME/1000000.0);
osilvam 1:471d502617fe 97 }
osilvam 1:471d502617fe 98 else
osilvam 1:471d502617fe 99 {
osilvam 1:471d502617fe 100 waitTicker.detach();
osilvam 1:471d502617fe 101 waitTicker.attach(callback(this, &RELAYService::flipCtr12v), CTR12V_TIME/1000000.0);
osilvam 1:471d502617fe 102 }
osilvam 1:471d502617fe 103 }
osilvam 1:471d502617fe 104
osilvam 1:471d502617fe 105 void flipCtr12v()
osilvam 1:471d502617fe 106 {
osilvam 1:471d502617fe 107 Ctr12v = !Ctr12v;
osilvam 1:471d502617fe 108 if(!Ctr12v)
osilvam 1:471d502617fe 109 {
osilvam 1:471d502617fe 110 waitTicker.detach();
osilvam 1:471d502617fe 111 activation_in_progress = false;
osilvam 1:471d502617fe 112 }
osilvam 1:471d502617fe 113 else activation_in_progress = true;
osilvam 1:471d502617fe 114 }
osilvam 0:5284859bb3e8 115
osilvam 0:5284859bb3e8 116 BLEDevice &ble;
osilvam 0:5284859bb3e8 117 uint8_t relayState;
osilvam 0:5284859bb3e8 118 DigitalOut actuatedRelay;
osilvam 0:5284859bb3e8 119 DigitalOut Ctr12v;
osilvam 0:5284859bb3e8 120
osilvam 0:5284859bb3e8 121 ImobStateService * ISS;
osilvam 0:5284859bb3e8 122
osilvam 0:5284859bb3e8 123 ReadWriteGattCharacteristic<uint8_t> RelayCharacteristic;
osilvam 0:5284859bb3e8 124 };
osilvam 0:5284859bb3e8 125
osilvam 0:5284859bb3e8 126 #endif /* #ifndef __BLE_RELAY_SERVICE_H__ */