Lizzy project

Dependencies:   aconno_I2C Lis2dh12 adc52832_common aconno_SEGGER_RTT

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers aconno_ble.cpp Source File

aconno_ble.cpp

00001 /*
00002  *   Made by Jurica Resetar @ aconno
00003  *   More info @ aconno.de
00004  *
00005  */
00006 
00007 #include "main.h"
00008 #include "aconno_ble.h"
00009 #include "tasks.h"
00010 #include "lizzy_service.h"
00011 #include "proj_config.h"
00012 
00013 
00014 /* BLE event queue size */
00015 #define BLE_EVENT_COUNT         (4)
00016 
00017 init_lizzy_t init_lizzy = {
00018     .buzz = false,
00019     .leds = {false ,false, false},
00020     .acc_lsb = LSB_VALUE,
00021     .acc_data = {0, 0, 0}
00022 };
00023 LizzyService *lizzy_service;
00024 
00025 static bool clientConected = false;
00026 static EventQueue eventQueue( BLE_EVENT_COUNT * EVENTS_EVENT_SIZE );
00027 
00028 
00029 void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context)
00030 {
00031     BLE &ble = context->ble;
00032     eventQueue.call(Callback<void()>(&ble, &BLE::processEvents));
00033 }
00034 
00035 EventQueue *getBLEEventQueue(void)
00036 {
00037     return &eventQueue;
00038 }
00039 
00040 bool bleIsClientConnected(void)
00041 {
00042     return clientConected;
00043 }
00044 
00045 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
00046 {
00047     clientConected = false;
00048     (lizzy_service->get_ble())->gap().startAdvertising();
00049 }
00050 
00051 void onConnectionCallback(const Gap::ConnectionCallbackParams_t *params){   
00052     clientConected = true;
00053 }
00054 
00055 void onDataWrittenCallback(const GattWriteCallbackParams *params) 
00056 {
00057     if ((params->handle == lizzy_service->getBuzzHandle()))
00058     {
00059 #if VODAFONE_COMPATIBILITY == 1
00060 #else
00061         if ((uint8_t)true < *(params->data))
00062             lizzy_service->setBuzz(true);
00063 #endif
00064         updateBuzzLedsT.signal_set(UPDATE_BUZZ_LEDS);
00065     }
00066 #if VODAFONE_COMPATIBILITY == 1
00067 #else
00068     else if ((params->handle == lizzy_service->getRedLedHandle()) &&
00069         (params->len == 1))
00070     {
00071         if ((uint8_t)true < *(params->data))
00072             lizzy_service->setRedLed(true);
00073             
00074         updateBuzzLedsT.signal_set(UPDATE_BUZZ_LEDS);
00075     }
00076 #endif
00077     else if ((params->handle == lizzy_service->get_green_handle()) &&
00078         (params->len == 1))
00079     {
00080         if ((uint8_t)true < *(params->data))
00081             lizzy_service->set_green_state(true);
00082             
00083         updateBuzzLedsT.signal_set(UPDATE_BUZZ_LEDS);
00084     }
00085     else if ((params->handle == lizzy_service->get_blue_handle()) &&
00086         (params->len == 1))
00087     {
00088         if ((uint8_t)true < *(params->data))
00089             lizzy_service->set_blue_state(true);
00090             
00091         updateBuzzLedsT.signal_set(UPDATE_BUZZ_LEDS);
00092     }
00093 }
00094 
00095 
00096 /**
00097 * Callback triggered when the ble initialization process has finished
00098 */
00099 
00100 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
00101 {
00102     advertising_packet advertisementPacket;
00103     BLE&        ble   = params->ble;
00104     ble_error_t error = params->error;
00105 
00106     if (error != BLE_ERROR_NONE) {
00107         return;
00108     }
00109 
00110     /* Ensure that it is the default instance of BLE */
00111     if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
00112         return;
00113     }
00114     
00115     lizzy_service = new LizzyService(ble, &init_lizzy);
00116     ble.gap().onDisconnection(disconnectionCallback);
00117     ble.gap().onConnection(onConnectionCallback);         
00118     ble.gattServer().onDataWritten(onDataWrittenCallback);
00119     
00120 #if VODAFONE_COMPATIBILITY == 1
00121     uint8_t myMacAddress[6];
00122     BLEProtocol::AddressType_t temp_address_type;
00123     ble.gap().getAddress(&temp_address_type, myMacAddress);
00124     lizzy_service->setMac(myMacAddress);    // Update MAC address
00125 #endif
00126     
00127     // Setup event handling. This is needed only with services.
00128     ble.onEventsToProcess(scheduleBleEventsProcessing);
00129     
00130     advertisementPacket.header = APPLICATION_ID;
00131     advertisementPacket.type = 0x00;
00132     advertisementPacket.gyroscope[0] = (int16_t)0;
00133     advertisementPacket.gyroscope[1] = (int16_t)0;
00134     advertisementPacket.gyroscope[2] = (int16_t)0;
00135     advertisementPacket.accelerometer[0] = (int16_t)0;
00136     advertisementPacket.accelerometer[1] = (int16_t)0;
00137     advertisementPacket.accelerometer[2] = (int16_t)0;
00138     advertisementPacket.magnetometer[0] = (int16_t)0;
00139     advertisementPacket.magnetometer[1] = (int16_t)0;
00140     advertisementPacket.magnetometer[2] = (int16_t)0;
00141     
00142     /* setup advertising */
00143 #if VODAFONE_COMPATIBILITY == 1
00144     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::INCOMPLETE_LIST_128BIT_SERVICE_IDS  , (uint8_t*)UUID, sizeof(UUID));
00145 #else
00146     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
00147     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)&advertisementPacket, sizeof(advertisementPacket));
00148 #endif
00149     ble.gap().setAdvertisingInterval(ADV_INTERVAL_MS);
00150     //ble.gap().startAdvertising();
00151 }
00152 
00153 void updatePayload(BLE *ble, advertising_packet *advertisementPacket)
00154 {
00155     ble->gap().stopAdvertising();
00156     GapAdvertisingData advetisementData = GapAdvertisingData();
00157     advetisementData = ble->getAdvertisingData();
00158     advetisementData.updateData(advetisementData.MANUFACTURER_SPECIFIC_DATA, (uint8_t *)advertisementPacket, sizeof(advertising_packet));
00159     ble->setAdvertisingData(advetisementData);
00160     //ble->gap().startAdvertising();
00161 }