main.cpp 4.28 KB
/* mbed Microcontroller Library
 * Copyright (c) 2006-2013 ARM Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "mbed.h"
#include "BLEDevice.h"
#include "PollutionService.h"
#include "EnvironmentalSensingService.h"
#include "LocationAndNavigationService.h"
#include "CurrentTimeService.h"
#include "BatteryService.h"
#include "DeviceInformationService.h"
#include "Utils.h"

BLEDevice  ble;
DigitalOut led1(LED1);

const static char     DEVICE_NAME[]        = "Pollution";
static const uint16_t uuid16_list[]        = {0xFFFF,
                                              PollutionService::UUID_POLLUTION_SERVICE,
                                              EnvironmentalSensingService::UUID_ENVIRONMENTAL_SENSING_SERVICE,
                                              LocationAndNavigationService::UUID_LOCATION_AND_NAVIGATION_SERVICE,
                                              CurrentTimeService::UUID_CURRENT_TIME_SERVICE,
                                              GattService::UUID_BATTERY_SERVICE,
                                              GattService::UUID_DEVICE_INFORMATION_SERVICE};
static volatile bool  triggerSensorPolling = false;


void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
{
    DEBUG("Disconnected!\n\r");
    DEBUG("Restarting the advertising process\n\r");
    ble.startAdvertising(); // restart advertising
}

void connectionCallback(Gap::Handle_t handle, const Gap::ConnectionParams_t *reason)
{
    DEBUG("Connected\r\n");
}

void periodicCallback(void)
{
    led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
    triggerSensorPolling = true;
}

int main(void)
{
    led1 = 1;
    Ticker ticker;
    ticker.attach(periodicCallback, 1);

    DEBUG("Initialising \n\r");
    ble.init();
    ble.onDisconnection(disconnectionCallback);
    ble.onConnection(connectionCallback);

    /* Setup services. */
    PollutionService         pollutionService(ble, 76);
    EnvironmentalSensingService     environmentalSensingService(ble);
    LocationAndNavigationService locationAndNavigationService(ble, 0x0C0400);
    CurrentTimeService currentTimeService(ble);
    BatteryService           batteryService(ble, 61);
    DeviceInformationService deviceInfoService(ble, "ST", "Nucleo", "SN1", "hw-rev1", "fw-rev1", "soft-rev1");

    /* Setup advertising. */
    /* BREDR_NOT_SUPPORTED means classic bluetooth not supported;
    * LE_GENERAL_DISCOVERABLE means that this peripheral can be
    * discovered by any BLE scanner--i.e. any phone. */
    ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
    ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
    ble.accumulateAdvertisingPayload(GapAdvertisingData::POLLUTION_PARTICLES_SENSOR);
    ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
    ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
    ble.setAdvertisingInterval(1000); /* 1000ms; in multiples of 0.625ms. */
    ble.startAdvertising();
    
    while (true)
    {
        if (triggerSensorPolling)
        {
            triggerSensorPolling = false;
            pollutionService.updatePollutionLevel(73);
            pollutionService.updateTemperature(25.76);
            environmentalSensingService.updateHumidity(15.2);
            environmentalSensingService.updatePressure(3.5);
            locationAndNavigationService.updateLocationAndSpeed(0x0C00, 4836.5375, 00740.9373, 200.2);
            currentTimeService.updateDateTime(2018, 02, 02, 16, 29, 45);
            currentTimeService.updateDayOfWeek(5);
        }
        else
        {
            ble.waitForEvent();
        }
    }
}