BTLE demo for MAXWSNENV.

Dependencies:   BLE_API BMP180 Si7020 mbed MaximBLE

Committer:
enginerd
Date:
Thu Aug 18 21:09:13 2016 +0000
Revision:
2:6f76d6160601
Parent:
0:f71931ae3db1
Updated library versions.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kgills 0:f71931ae3db1 1 /*******************************************************************************
kgills 0:f71931ae3db1 2 * Copyright (C) 2015 Maxim Integrated Products, Inc., All Rights Reserved.
kgills 0:f71931ae3db1 3 *
kgills 0:f71931ae3db1 4 * Permission is hereby granted, free of charge, to any person obtaining a
kgills 0:f71931ae3db1 5 * copy of this software and associated documentation files (the "Software"),
kgills 0:f71931ae3db1 6 * to deal in the Software without restriction, including without limitation
kgills 0:f71931ae3db1 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
kgills 0:f71931ae3db1 8 * and/or sell copies of the Software, and to permit persons to whom the
kgills 0:f71931ae3db1 9 * Software is furnished to do so, subject to the following conditions:
kgills 0:f71931ae3db1 10 *
kgills 0:f71931ae3db1 11 * The above copyright notice and this permission notice shall be included
kgills 0:f71931ae3db1 12 * in all copies or substantial portions of the Software.
kgills 0:f71931ae3db1 13 *
kgills 0:f71931ae3db1 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
kgills 0:f71931ae3db1 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
kgills 0:f71931ae3db1 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
kgills 0:f71931ae3db1 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
kgills 0:f71931ae3db1 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
kgills 0:f71931ae3db1 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
kgills 0:f71931ae3db1 20 * OTHER DEALINGS IN THE SOFTWARE.
kgills 0:f71931ae3db1 21 *
kgills 0:f71931ae3db1 22 * Except as contained in this notice, the name of Maxim Integrated
kgills 0:f71931ae3db1 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
kgills 0:f71931ae3db1 24 * Products, Inc. Branding Policy.
kgills 0:f71931ae3db1 25 *
kgills 0:f71931ae3db1 26 * The mere transfer of this software does not imply any licenses
kgills 0:f71931ae3db1 27 * of trade secrets, proprietary technology, copyrights, patents,
kgills 0:f71931ae3db1 28 * trademarks, maskwork rights, or any other form of intellectual
kgills 0:f71931ae3db1 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
kgills 0:f71931ae3db1 30 * ownership rights.
kgills 0:f71931ae3db1 31 *******************************************************************************
kgills 0:f71931ae3db1 32 */
kgills 0:f71931ae3db1 33
kgills 0:f71931ae3db1 34 #ifndef __BLE_CURRENTTIMESERVICE_H__
kgills 0:f71931ae3db1 35 #define __BLE_CURRENTTIMESERVICE_H__
kgills 0:f71931ae3db1 36
kgills 0:f71931ae3db1 37 #include <time.h>
kgills 0:f71931ae3db1 38 #include "Characteristic.h"
kgills 0:f71931ae3db1 39 #include "CurrentTimeChar.h"
kgills 0:f71931ae3db1 40 #include "ReferenceTimeInfoChar.h"
kgills 0:f71931ae3db1 41 #include "LocalTimeInfoChar.h"
kgills 0:f71931ae3db1 42
kgills 0:f71931ae3db1 43 /**
kgills 0:f71931ae3db1 44 * @class CurrentTimeService
kgills 0:f71931ae3db1 45 * @brief BLE Current Time service.
kgills 0:f71931ae3db1 46 * @details Standard service for communicating time information.
kgills 0:f71931ae3db1 47 */
kgills 0:f71931ae3db1 48 class CurrentTimeService
kgills 0:f71931ae3db1 49 {
kgills 0:f71931ae3db1 50 public:
kgills 0:f71931ae3db1 51
kgills 0:f71931ae3db1 52 /**
kgills 0:f71931ae3db1 53 * @brief CurrentTimeSerivce constructor.
kgills 0:f71931ae3db1 54 * @param ble Reference to BLE device.
kgills 0:f71931ae3db1 55 */
kgills 0:f71931ae3db1 56 CurrentTimeService(BLEDevice &_ble) :
kgills 0:f71931ae3db1 57 ble(_ble),
kgills 0:f71931ae3db1 58 currentTime(),
kgills 0:f71931ae3db1 59 localTimeInfo(),
kgills 0:f71931ae3db1 60 referenceTimeInfo()
kgills 0:f71931ae3db1 61 {
kgills 0:f71931ae3db1 62
kgills 0:f71931ae3db1 63 GattCharacteristic *currentTimeChars[] = {currentTime.getChar(), localTimeInfo.getChar(),
kgills 0:f71931ae3db1 64 referenceTimeInfo.getChar()};
kgills 0:f71931ae3db1 65
kgills 0:f71931ae3db1 66 GattService currentTimeService(GattService::UUID_CURRENT_TIME_SERVICE, currentTimeChars,
kgills 0:f71931ae3db1 67 sizeof(currentTimeChars) / sizeof(GattCharacteristic *));
kgills 0:f71931ae3db1 68
kgills 0:f71931ae3db1 69 ble.gattServer().addService(currentTimeService);
kgills 0:f71931ae3db1 70 ble.gattServer().onDataWritten(this, &CurrentTimeService::onDataWritten);
kgills 0:f71931ae3db1 71 ble.gattServer().onDataRead(this, &CurrentTimeService::onDataRead);
kgills 0:f71931ae3db1 72 }
kgills 0:f71931ae3db1 73
kgills 0:f71931ae3db1 74 // Update the value in the current time characteristic from RTC
kgills 0:f71931ae3db1 75 void updateCurrentTimeValue()
kgills 0:f71931ae3db1 76 {
kgills 0:f71931ae3db1 77 currentTime.update();
kgills 0:f71931ae3db1 78 ble.gattServer().write(currentTime.getChar()->getValueAttribute().getHandle(),
kgills 0:f71931ae3db1 79 currentTime.getBytes(), currentTime.getNumBytes());
kgills 0:f71931ae3db1 80 }
kgills 0:f71931ae3db1 81
kgills 0:f71931ae3db1 82 private:
kgills 0:f71931ae3db1 83
kgills 0:f71931ae3db1 84 void onDataWritten(const GattWriteCallbackParams *params)
kgills 0:f71931ae3db1 85 {
kgills 0:f71931ae3db1 86
kgills 0:f71931ae3db1 87 // Check to see which value is being written
kgills 0:f71931ae3db1 88 if (params->handle == currentTime.getChar()->getValueAttribute().
kgills 0:f71931ae3db1 89 getHandle()) {
kgills 0:f71931ae3db1 90
kgills 0:f71931ae3db1 91 // Update RTC from CurrentTimeChar
kgills 0:f71931ae3db1 92 currentTime.update_rtc();
kgills 0:f71931ae3db1 93
kgills 0:f71931ae3db1 94 // Reset the time since update
kgills 0:f71931ae3db1 95 referenceTimeInfo.resetUpdateTime();
kgills 0:f71931ae3db1 96 }
kgills 0:f71931ae3db1 97 }
kgills 0:f71931ae3db1 98
kgills 0:f71931ae3db1 99 void onDataRead(const GattReadCallbackParams *eventDataP)
kgills 0:f71931ae3db1 100 {
kgills 0:f71931ae3db1 101 if (eventDataP->handle == currentTime.getChar()->getValueAttribute().getHandle()) {
kgills 0:f71931ae3db1 102 // Update the characteristic value with the rtc
kgills 0:f71931ae3db1 103 currentTime.update();
kgills 0:f71931ae3db1 104
kgills 0:f71931ae3db1 105 } else if (eventDataP->handle == referenceTimeInfo.getChar()->getValueAttribute().getHandle()) {
kgills 0:f71931ae3db1 106 // Update time since update
kgills 0:f71931ae3db1 107 referenceTimeInfo.updateUpdateTime();
kgills 0:f71931ae3db1 108 }
kgills 0:f71931ae3db1 109 }
kgills 0:f71931ae3db1 110
kgills 0:f71931ae3db1 111 BLEDevice &ble;
kgills 0:f71931ae3db1 112 CurrentTimeChar currentTime;
kgills 0:f71931ae3db1 113 LocalTimeInfoChar localTimeInfo;
kgills 0:f71931ae3db1 114 ReferenceTimeInfoChar referenceTimeInfo;
kgills 0:f71931ae3db1 115 };
kgills 0:f71931ae3db1 116
kgills 0:f71931ae3db1 117 #endif /* #ifndef __BLE_CURRENTTIMESERVICE_H__*/