Lizzy project

Dependencies:   aconno_I2C Lis2dh12 adc52832_common aconno_SEGGER_RTT

aconno_ble/aconno_ble.cpp

Committer:
jurica238814
Date:
2018-09-13
Branch:
SimpleGATTExample
Revision:
22:7dae8496b97c
Parent:
19:2681edc2f2b9

File content as of revision 22:7dae8496b97c:

/*
 *   Made by Jurica Resetar @ aconno
 *   More info @ aconno.de
 *
 */

#include "aconno_ble.h"
#include "tasks.h"
#include "lizzy_service.h"
#include "aconnoConfig.h"
#include "bsp.h"

const static char deviceName[] = "LAUT";

init_lizzy_t init_lizzy = {
    .buzz = false,
    .leds = {false ,false, false},
    .acc_lsb = 023,
    .acc_data = {0, 0, 0}
};

LizzyService *lizzy_service;

void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
{
    (lizzy_service->get_ble())->gap().startAdvertising();
}

void onConnectionCallback(const Gap::ConnectionCallbackParams_t *params){

}

void onDataWrittenCallback(const GattWriteCallbackParams *params)
{

    if ((params->handle == lizzy_service->get_red_handle()) &&
        (params->len == 1))
    {
        if (*(params->data))
		{
            lizzy_service->set_red_state(LED_ON);
		}

        updateBuzzLedsT.signal_set(UPDATE_BUZZ_LEDS);
    }
    else if ((params->handle == lizzy_service->get_green_handle()) &&
        (params->len == 1))
    {
        if (*(params->data))
		{
            lizzy_service->set_green_state(LED_ON);
		}
        updateBuzzLedsT.signal_set(UPDATE_BUZZ_LEDS);
    }
}


/**
* Callback triggered when the ble initialization process has finished
*/

void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
{
    advertising_packet advertisementPacket;
    BLE&        ble   = params->ble;
    ble_error_t error = params->error;

    if (error != BLE_ERROR_NONE) {
        return;
    }

    /* Ensure that it is the default instance of BLE */
    if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
        return;
    }


    lizzy_service = new LizzyService(ble, &init_lizzy);
    ble.gap().onDisconnection(disconnectionCallback);
    //ble.gap().onConnection(onConnectionCallback);
    ble.gattServer().onDataWritten(onDataWrittenCallback);

    advertisementPacket.header = APPLICATION_ID;
    advertisementPacket.type = 0x00;

    /* setup advertising */
    ble.gap().accumulateAdvertisingPayload(
		GapAdvertisingData::BREDR_NOT_SUPPORTED);
    ble.gap().accumulateAdvertisingPayload(
		GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA,
		(uint8_t *)&advertisementPacket, sizeof(advertisementPacket));
	ble.gap().accumulateAdvertisingPayload(
		GapAdvertisingData::COMPLETE_LOCAL_NAME,
		(uint8_t *)deviceName, sizeof(deviceName));
    ble.gap().setAdvertisingInterval(ADV_INTERVAL_MS);
    ble.gap().startAdvertising();
}

void updatePayload(BLE *ble, advertising_packet *advertisementPacket)
{
    ble->gap().stopAdvertising();
    GapAdvertisingData advetisementData = GapAdvertisingData();
    advetisementData = ble->getAdvertisingData();
    advetisementData.updateData(advetisementData.MANUFACTURER_SPECIFIC_DATA, (uint8_t *)advertisementPacket, sizeof(advertising_packet));
    ble->setAdvertisingData(advetisementData);
    //ble->gap().startAdvertising();
}