javier cifuentes / Mbed 2 deprecated IMOB_with_encryption

Dependencies:   AccelSensor BLE_API mbed nRF51822

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "ble/BLE.h"
00003 #include "RELAYService.h"
00004 #include "ALARMService.h"
00005 #include "BatteryService.h"
00006 #include "InternalValuesService.h"
00007 #include "ImobStateService.h"
00008 #include "AccelSensorService.h"
00009 
00010 #define TIME_CICLE 80.0 //ms
00011 #define ANALOGIN 3
00012 #define DISCONNECTION_TIME (1000.0/TIME_CICLE)*10.0 // seg
00013 #define AUTHENTICATION_TIME (1000.0/TIME_CICLE)*25.0 // seg
00014 
00015 
00016 /* LED aliveness indicator system  */
00017 DigitalOut alivenessLED(P0_18, 0); // P0_25
00018 
00019 /* battery charge level, Pin P0_1 */
00020 AnalogIn batteryCharge(P0_1);
00021 /* lipo charger Status, Pin P0_2 */
00022 AnalogIn lcStat(P0_2);
00023 /* Chack contact, Pin P0_6 */
00024 AnalogIn contact(P0_6);
00025 
00026 
00027 /* Device name setting to identifying your device */
00028 const static char     DEVICE_NAME[] = "I-Mob";
00029 static const uint16_t uuid16_list[] = {ImobStateService::IMOB_STATE_SERVICE_UUID, RELAYService::RELAY_SERVICE_UUID, ALARMService::ALARM_SERVICE_UUID,  InternalValuesService::INTERNAL_VALUES_SERVICE_UUID, AccelSensorService::ACCEL_SENSOR_SERVICE_UUID, GattService::UUID_BATTERY_SERVICE};
00030 
00031 
00032 ImobStateService * imobStateServicePtr;
00033 
00034 InternalValuesService * internalValuesServicePtr;
00035 RELAYService * relayServicePtr;
00036 ALARMService * alarmServicePtr;
00037 AccelSensorService * accelSensorServicePtr;
00038 BatteryService * batteryServicePtr;
00039 
00040 float lipochargerState = 0;
00041 float contactState = -1;
00042 uint8_t batteryLevel = 0;
00043 
00044 uint8_t selectedAnalogIn = 0;
00045 
00046 uint32_t forceDisconnectionCounter = 0;
00047 uint32_t forceActivationCounter = 0;
00048 
00049 /* Calibration variables */
00050 bool batteryLevelCalibration = false;
00051 float batteryLevelConstant = 100.0f;
00052 float contactStateThreshold = 0.21f;
00053 /* ----- */
00054 
00055 Ticker ticker;
00056 
00057 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
00058 {
00059     /* Re-enable advertisements after a connection teardown */
00060     BLE::Instance().gap().startAdvertising();
00061 }
00062 
00063 void periodicCallback(void)
00064 {
00065     /* Do blinky on LED1 to indicate system aliveness. */
00066     alivenessLED = !alivenessLED;
00067 }
00068 
00069 /**
00070  * This function is called when the ble initialization process has failed
00071  */
00072 void onBleInitError(BLE &ble, ble_error_t error)
00073 {
00074     /* Initialization error handling should go here */
00075 }
00076 
00077 /**
00078  * Callback triggered when the ble initialization process has finished
00079  */
00080 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
00081 {
00082     BLE& ble = params->ble;
00083     ble_error_t error = params->error;
00084 
00085     if (error != BLE_ERROR_NONE) 
00086     {
00087         /* In case of error, forward the error handling to onBleInitError */
00088         onBleInitError(ble, error);
00089         return;
00090     }
00091 
00092     /* Ensure that it is the default instance of BLE */
00093     if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE)
00094     {
00095         return;
00096     }
00097  
00098     ble.gap().onDisconnection(disconnectionCallback);
00099         
00100     imobStateServicePtr = new ImobStateService(ble);
00101     
00102     internalValuesServicePtr = new InternalValuesService(ble, imobStateServicePtr);
00103     relayServicePtr = new RELAYService(ble, imobStateServicePtr);
00104     alarmServicePtr = new ALARMService(ble);
00105     accelSensorServicePtr = new AccelSensorService(ble);
00106     batteryServicePtr = new BatteryService(ble, batteryLevel);
00107     
00108     /* setup advertising */
00109     
00110     /* BREDR_NOT_SUPPORTED means classic bluetooth not supported;
00111     * LE_GENERAL_DISCOVERABLE means that this peripheral can be discovered by any BLE scanner--i.e. any phone. 
00112     */
00113     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
00114     /* Adding the RELAY service UUID to the advertising payload*/
00115     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
00116     /* This is where we're collecting the device name into the advertisement payload. */
00117     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
00118     /* We'd like for this BLE peripheral to be connectable. */
00119     ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
00120     /* set the interval at which advertisements are sent out, 1000ms. */
00121     ble.gap().setAdvertisingInterval(TIME_CICLE);
00122     /* we're finally good to go with advertisements. */
00123     ble.gap().startAdvertising();
00124 }
00125 
00126 int main(void)
00127 {    
00128     /* Setting up a callback to go at an interval of 1s. */
00129     ticker.attach(periodicCallback, TIME_CICLE/1000.0);
00130 
00131     /*  initialize the BLE stack and controller. */
00132     BLE &ble = BLE::Instance();
00133     ble.init(bleInitComplete);
00134     
00135     /* Accelerometer functions
00136     accelerometer.init();
00137     accelerometer.standby();
00138     accelerometer.active();
00139     int accel[3];
00140     accelerometer.readData(accel);
00141     accelerometer.standby();
00142     */
00143     
00144     /* SpinWait for initialization to complete. This is necessary because the
00145      * BLE object is used in the main loop below. */
00146     while (ble.hasInitialized()  == false) { /* spin loop */ }       
00147 
00148     while (true)
00149     {
00150         /* this will return upon any system event (such as an interrupt or a ticker wakeup) */
00151         ble.waitForEvent();
00152         bool accel = accelSensorServicePtr->updateAccelDetection();
00153                       
00154         /* Update battery charge level */        
00155         if (selectedAnalogIn == 0)
00156         {
00157             batteryLevel = (uint8_t)(batteryCharge.read()*batteryLevelConstant);
00158             batteryServicePtr->updateBatteryLevel(batteryLevel);           
00159         }
00160         /* Update lipo charger state */
00161         if (selectedAnalogIn == 1)
00162         {
00163             lipochargerState = lcStat.read();
00164             uint8_t aux_lipochargerState;
00165                         
00166             uint8_t pass_lipochargerState = internalValuesServicePtr->getLipoChargerState();
00167             
00168             if (lipochargerState > 0.4)
00169             {                   
00170                 if (lipochargerState > 0.9)
00171                 {                       
00172                     aux_lipochargerState = 1;                    
00173                     if (pass_lipochargerState == 0) internalValuesServicePtr->updateChargeProgramCyclesCharacteristic();
00174                 }
00175                 else
00176                 {
00177                     aux_lipochargerState = 2;
00178                     if (activated && accel) alarmServicePtr->updateAlarmState(1);
00179                 }
00180             }
00181             else
00182             {
00183                 aux_lipochargerState = 0;
00184                 if (pass_lipochargerState == 1) internalValuesServicePtr->updateDischargeProgramCyclesCharacteristic();
00185             }
00186                 
00187             if(!batteryLevelCalibration && aux_lipochargerState == 1)
00188             {
00189                 batteryLevelConstant *= 100.0f/batteryLevel;
00190                 batteryLevelCalibration = true;
00191             }
00192                 
00193             internalValuesServicePtr->updateLipoChargerState(aux_lipochargerState);
00194         }
00195         
00196         if (internalValuesServicePtr->getLipoChargerState() == 0) internalValuesServicePtr->incrementChargeProgramCycles();
00197         else if (internalValuesServicePtr->getLipoChargerState() == 1) internalValuesServicePtr->incrementDischargeProgramCycles();
00198         
00199         /* Update contact state */
00200         if (selectedAnalogIn == 2)
00201         {
00202             contactState = contact.read();
00203             uint8_t aux_contactState;                                   
00204             
00205             if (contactState > contactStateThreshold)
00206             {
00207                 aux_contactState = 1;
00208                 
00209                 if (!authenticated && activated)
00210                     relayServicePtr->activate();                
00211             }
00212             else
00213                 aux_contactState = 0;
00214                                 
00215             internalValuesServicePtr->updateContactState(aux_contactState);
00216         }       
00217         
00218         selectedAnalogIn++;
00219             
00220         if (selectedAnalogIn == ANALOGIN) selectedAnalogIn = 0;
00221         
00222         if (!authenticated && userIsConnected)
00223         {
00224             if (forceDisconnectionCounter > DISCONNECTION_TIME)
00225             {
00226                 forceDisconnectionCounter = 0;
00227                 Gap::DisconnectionReason_t res=Gap::LOCAL_HOST_TERMINATED_CONNECTION;
00228                 ble.disconnect(res);
00229             }
00230             else
00231                 forceDisconnectionCounter++;
00232         }
00233         else
00234             forceDisconnectionCounter = 0;
00235         
00236         if ( !initial_activation  && !userIsConnected)
00237             if (forceActivationCounter > AUTHENTICATION_TIME)
00238             {
00239                 imobStateServicePtr->updateActivationValue(1);
00240                 initial_activation = true;
00241             }
00242             else
00243                 forceActivationCounter++;
00244             
00245         
00246     }
00247 }