Dependencies: AccelSensor BLE_API mbed nRF51822
ImobStateService.h
00001 #ifndef __BLE_IMOB_STATE_SERVICE_H__ 00002 #define __BLE_IMOB_STATE_SERVICE_H__ 00003 00004 #include "mbed.h" 00005 #include "ble/BLE.h" 00006 #include "ble/Gap.h" 00007 #include "crypt.h" 00008 00009 #include "softdevice_handler.h" 00010 00011 #define PASSLEN 16 00012 #define KEYLEN 16 00013 #define MACLEN 6 00014 00015 static bool authenticated = false; 00016 static bool activated = false; 00017 static bool userIsConnected = false; 00018 static bool initial_activation = false; 00019 00020 uint8_t defaultPass[PASSLEN] = {0}; 00021 uint8_t defaultMac[MACLEN] = {0}; 00022 00023 bool equal_arrays(uint8_t a1 [], const uint8_t a2 [], uint8_t n) 00024 { 00025 for (uint8_t i = 0; i < n; ++i) 00026 if (a1[i] != a2[i]) 00027 return false; 00028 return (true); 00029 } 00030 00031 class ImobStateService { 00032 public: 00033 const static uint16_t IMOB_STATE_SERVICE_UUID = 0xA000; 00034 const static uint16_t IMOB_STATE_PASS_CHARACTERISTIC_UUID = 0xA001; 00035 const static uint16_t IMOB_STATE_NONCE_CHARACTERISTIC_UUID = 0xA002; 00036 const static uint16_t IMOB_STATE_NONCE_UPDATED_CHARACTERISTIC_UUID = 0xA003; 00037 const static uint16_t IMOB_STATE_AUTHENTICATION_CHARACTERISTIC_UUID = 0xA004; 00038 const static uint16_t IMOB_STATE_ACTIVATION_CHARACTERISTIC_UUID = 0xA005; 00039 00040 ImobStateService(BLEDevice &_ble) : 00041 ble(_ble), 00042 passUpdated(false), 00043 nonceUpdated(false), 00044 activation(0), 00045 authentication(0), 00046 passCharacteristic(IMOB_STATE_PASS_CHARACTERISTIC_UUID, defaultPass), 00047 nonceCharacteristic(IMOB_STATE_NONCE_CHARACTERISTIC_UUID, defaultPass), 00048 nonceUpdatedCharacteristic(IMOB_STATE_NONCE_UPDATED_CHARACTERISTIC_UUID, (uint8_t*)&nonceUpdated), 00049 activationCharacteristic(IMOB_STATE_ACTIVATION_CHARACTERISTIC_UUID, &activation), 00050 authenticationCharacteristic(IMOB_STATE_AUTHENTICATION_CHARACTERISTIC_UUID, &authentication) 00051 00052 { 00053 GattCharacteristic *charTable[] = {&passCharacteristic, &nonceCharacteristic, &nonceUpdatedCharacteristic, &activationCharacteristic, &authenticationCharacteristic}; 00054 GattService imobStateService(IMOB_STATE_SERVICE_UUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); 00055 00056 ble.addService(imobStateService); 00057 00058 ble.gap().onDisconnection(this, &ImobStateService::onDisconnectionFilter); 00059 ble.gap().onConnection(this, &ImobStateService::onConnectionFilter); 00060 ble.gattServer().onDataWritten(this, &ImobStateService::onDataWritten); 00061 00062 resetAuthenticationValues(); 00063 00064 for(uint8_t i = 0; i < PASSLEN;i++) 00065 correctPass[i] = defaultPass[i]; 00066 00067 for(uint8_t i = 0; i < KEYLEN;i++) 00068 p_ecb_key[i] = defaultPass[i]; 00069 00070 for(uint8_t i = 0; i < MACLEN;i++) 00071 recentMac[i] = defaultMac[i]; 00072 00073 } 00074 00075 void resetAuthenticationValues() 00076 { 00077 updateAuthenticationValue(false); 00078 00079 passUpdated = false; 00080 00081 for(uint8_t i = 0; i < PASSLEN; i++) 00082 pass[i] = defaultPass[i]; 00083 00084 ble.updateCharacteristicValue(passCharacteristic.getValueHandle(), pass, PASSLEN); 00085 } 00086 00087 void updateAuthenticationPassValues(const uint8_t newpass[PASSLEN]) 00088 { 00089 passUpdated = true; 00090 00091 for(uint8_t i = 0; i < PASSLEN;i++) 00092 pass[i] = newpass[i]; 00093 00094 ctr_decrypt(pass); 00095 } 00096 00097 void updateAuthenticationNonceValues(const uint8_t newnonce[PASSLEN]) 00098 { 00099 updateNonceUpdatedValue(true); 00100 00101 for(uint8_t i = 0; i < PASSLEN;i++) 00102 nonce[i] = newnonce[i]; 00103 00104 ble.updateCharacteristicValue(nonceCharacteristic.getValueHandle(),nonce,PASSLEN); 00105 ctr_init(nonce, p_ecb_key); 00106 } 00107 00108 void updateNonceUpdatedValue(bool value) 00109 { 00110 nonceUpdated = value; 00111 uint8_t aux_nonceUpdated = (nonceUpdated) ? 1: 0; 00112 ble.gattServer().write(nonceUpdatedCharacteristic.getValueHandle(), &aux_nonceUpdated, 1); 00113 00114 } 00115 00116 void updateAuthenticationValue(bool value) 00117 { 00118 authenticated = value; 00119 authentication = (authenticated) ? 1: 0; 00120 ble.gattServer().write(authenticationCharacteristic.getValueHandle(), &authentication, 1); 00121 } 00122 00123 void updateActivationValue(const uint8_t value) 00124 { 00125 activated = (value == 1) ? true: false; 00126 activation = (activated) ? 1: 0; 00127 ble.gattServer().write(activationCharacteristic.getValueHandle(), &activation, 1); 00128 } 00129 00130 void setCorrectPass(const uint8_t * newCorrectPass) 00131 { 00132 for(uint8_t i = 0; i < PASSLEN;i++) 00133 correctPass[i] = newCorrectPass[i]; 00134 } 00135 00136 void setCryptKey(const uint8_t * newCryptKey) 00137 { 00138 for(uint8_t i = 0; i < PASSLEN;i++) 00139 p_ecb_key[i] = newCryptKey[i]; 00140 } 00141 00142 protected: 00143 virtual void onDataWritten(const GattWriteCallbackParams *params) 00144 { 00145 if ((params->handle == passCharacteristic.getValueHandle()) && (params->len == PASSLEN) && (nonceUpdated)) 00146 { 00147 updateAuthenticationPassValues((params->data)); 00148 } 00149 else if ((params->handle == nonceCharacteristic.getValueHandle()) && (params->len == PASSLEN)) 00150 { 00151 updateAuthenticationNonceValues((params->data)); 00152 } 00153 else if ((params->handle == activationCharacteristic.getValueHandle()) && (params->len == 1) && authenticated) 00154 { 00155 updateActivationValue(*(params->data)); 00156 } 00157 00158 if(passUpdated) 00159 { 00160 if(equal_arrays(pass, correctPass, PASSLEN)) 00161 { 00162 updateAuthenticationValue(true); 00163 initial_activation = true; 00164 } 00165 else 00166 { 00167 resetAuthenticationValues(); 00168 } 00169 } 00170 } 00171 00172 void onDisconnectionFilter(const Gap::DisconnectionCallbackParams_t *params) 00173 { 00174 resetAuthenticationValues(); 00175 userIsConnected = false; 00176 } 00177 00178 void onConnectionFilter(const Gap::ConnectionCallbackParams_t* params) 00179 { 00180 uint8_t newMac[MACLEN]; 00181 for(uint8_t i = 0; i < 6; i++) 00182 newMac[i] = params->peerAddr[i]; 00183 00184 if(!equal_arrays(recentMac, newMac, MACLEN)) 00185 { 00186 for(uint8_t i = 0; i < 6; i++) 00187 recentMac[i] = newMac[i]; 00188 00189 updateNonceUpdatedValue(false); 00190 00191 for(uint8_t i = 0; i < PASSLEN; i++) 00192 nonce[i] = defaultPass[i]; 00193 00194 ble.updateCharacteristicValue(nonceCharacteristic.getValueHandle(), nonce, PASSLEN); 00195 } 00196 00197 userIsConnected = true; 00198 } 00199 00200 private: 00201 BLEDevice &ble; 00202 bool passUpdated; 00203 bool nonceUpdated; 00204 00205 uint8_t pass[PASSLEN]; 00206 uint8_t nonce[PASSLEN]; 00207 uint8_t correctPass[PASSLEN]; 00208 uint8_t p_ecb_key[KEYLEN]; 00209 00210 uint8_t recentMac[MACLEN]; 00211 00212 uint8_t activation; 00213 uint8_t authentication; 00214 00215 WriteOnlyArrayGattCharacteristic <uint8_t, sizeof(pass)> passCharacteristic; 00216 WriteOnlyArrayGattCharacteristic <uint8_t, sizeof(pass)> nonceCharacteristic; 00217 00218 ReadOnlyGattCharacteristic < uint8_t > nonceUpdatedCharacteristic; 00219 ReadWriteGattCharacteristic < uint8_t > activationCharacteristic; 00220 ReadOnlyGattCharacteristic < uint8_t > authenticationCharacteristic; 00221 00222 00223 }; 00224 00225 #endif /* #ifndef __BLE_IMOB_STATE_SERVICE_H__ */
Generated on Thu Jul 14 2022 02:33:32 by
1.7.2