Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbedtls mbed BLE_API nRF51822 AccelSensor
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_init(nonce, p_ecb_key);//solo para pruebas!!! 00095 ctr_encrypt(pass);// se encripta en este punto solo para pruebas 00096 ctr_init(nonce, p_ecb_key); // debería llegar la pass encriptada. ctr_init se llama para reiniciar el contador de paquetes 00097 ctr_decrypt(pass); 00098 } 00099 00100 void updateAuthenticationNonceValues(const uint8_t newnonce[PASSLEN]) 00101 { 00102 updateNonceUpdatedValue(true); 00103 00104 for(uint8_t i = 0; i < PASSLEN;i++) 00105 nonce[i] = newnonce[i]; 00106 00107 nonce_generate(nonce);// se sobreescribe el nonce para las pruebas (no se toma el nonce entrante) 00108 ble.updateCharacteristicValue(nonceCharacteristic.getValueHandle(),nonce,PASSLEN); 00109 ctr_init(nonce, p_ecb_key); 00110 } 00111 00112 void updateNonceUpdatedValue(bool value) 00113 { 00114 nonceUpdated = value; 00115 uint8_t aux_nonceUpdated = (nonceUpdated) ? 1: 0; 00116 ble.gattServer().write(nonceUpdatedCharacteristic.getValueHandle(), &aux_nonceUpdated, 1); 00117 00118 } 00119 00120 void updateAuthenticationValue(bool value) 00121 { 00122 authenticated = value; 00123 authentication = (authenticated) ? 1: 0; 00124 ble.gattServer().write(authenticationCharacteristic.getValueHandle(), &authentication, 1); 00125 } 00126 00127 void updateActivationValue(const uint8_t value) 00128 { 00129 activated = (value == 1) ? true: false; 00130 activation = (activated) ? 1: 0; 00131 ble.gattServer().write(activationCharacteristic.getValueHandle(), &activation, 1); 00132 } 00133 00134 void setCorrectPass(const uint8_t * newCorrectPass) 00135 { 00136 for(uint8_t i = 0; i < PASSLEN;i++) 00137 correctPass[i] = newCorrectPass[i]; 00138 } 00139 00140 void setCryptKey(const uint8_t * newCryptKey) 00141 { 00142 for(uint8_t i = 0; i < PASSLEN;i++) 00143 p_ecb_key[i] = newCryptKey[i]; 00144 } 00145 00146 protected: 00147 virtual void onDataWritten(const GattWriteCallbackParams *params) 00148 { 00149 if ((params->handle == passCharacteristic.getValueHandle()) && (params->len == PASSLEN) && (nonceUpdated)) 00150 { 00151 updateAuthenticationPassValues((params->data)); 00152 } 00153 else if ((params->handle == nonceCharacteristic.getValueHandle()) && (params->len == PASSLEN)) 00154 { 00155 updateAuthenticationNonceValues((params->data)); 00156 } 00157 else if ((params->handle == activationCharacteristic.getValueHandle()) && (params->len == 1) && authenticated) 00158 { 00159 updateActivationValue(*(params->data)); 00160 } 00161 00162 if(passUpdated) 00163 { 00164 if(equal_arrays(pass, correctPass, PASSLEN)) 00165 { 00166 updateAuthenticationValue(true); 00167 initial_activation = true; 00168 } 00169 else 00170 { 00171 resetAuthenticationValues(); 00172 } 00173 } 00174 } 00175 00176 void onDisconnectionFilter(const Gap::DisconnectionCallbackParams_t *params) 00177 { 00178 resetAuthenticationValues(); 00179 userIsConnected = false; 00180 } 00181 00182 void onConnectionFilter(const Gap::ConnectionCallbackParams_t* params) 00183 { 00184 uint8_t newMac[MACLEN]; 00185 for(uint8_t i = 0; i < 6; i++) 00186 newMac[i] = params->peerAddr[i]; 00187 00188 if(!equal_arrays(recentMac, newMac, MACLEN)) 00189 { 00190 for(uint8_t i = 0; i < 6; i++) 00191 recentMac[i] = newMac[i]; 00192 00193 updateNonceUpdatedValue(false); 00194 00195 for(uint8_t i = 0; i < PASSLEN; i++) 00196 nonce[i] = defaultPass[i]; 00197 00198 ble.updateCharacteristicValue(nonceCharacteristic.getValueHandle(), nonce, PASSLEN); 00199 } 00200 00201 userIsConnected = true; 00202 } 00203 00204 private: 00205 BLEDevice &ble; 00206 bool passUpdated; 00207 bool nonceUpdated; 00208 00209 uint8_t pass[PASSLEN]; 00210 uint8_t nonce[PASSLEN]; 00211 uint8_t correctPass[PASSLEN]; 00212 uint8_t p_ecb_key[KEYLEN]; 00213 00214 uint8_t recentMac[MACLEN]; 00215 00216 uint8_t activation; 00217 uint8_t authentication; 00218 00219 WriteOnlyArrayGattCharacteristic <uint8_t, sizeof(pass)> passCharacteristic; 00220 WriteOnlyArrayGattCharacteristic <uint8_t, sizeof(pass)> nonceCharacteristic; 00221 00222 ReadOnlyGattCharacteristic < uint8_t > nonceUpdatedCharacteristic; 00223 ReadWriteGattCharacteristic < uint8_t > activationCharacteristic; 00224 ReadOnlyGattCharacteristic < uint8_t > authenticationCharacteristic; 00225 00226 00227 }; 00228 00229 #endif /* #ifndef __BLE_IMOB_STATE_SERVICE_H__ */
Generated on Mon Dec 12 2022 13:13:31 by
1.7.2