repo time

Dependencies:   mbed MAX14720 MAX30205 USBDevice

Committer:
darienf
Date:
Tue Apr 06 06:41:40 2021 +0000
Revision:
20:6d2af70c92ab
another repo

Who changed what in which revision?

UserRevisionLine numberNew contents of line
darienf 20:6d2af70c92ab 1 /*******************************************************************************
darienf 20:6d2af70c92ab 2 * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
darienf 20:6d2af70c92ab 3 *
darienf 20:6d2af70c92ab 4 * Permission is hereby granted, free of charge, to any person obtaining a
darienf 20:6d2af70c92ab 5 * copy of this software and associated documentation files (the "Software"),
darienf 20:6d2af70c92ab 6 * to deal in the Software without restriction, including without limitation
darienf 20:6d2af70c92ab 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
darienf 20:6d2af70c92ab 8 * and/or sell copies of the Software, and to permit persons to whom the
darienf 20:6d2af70c92ab 9 * Software is furnished to do so, subject to the following conditions:
darienf 20:6d2af70c92ab 10 *
darienf 20:6d2af70c92ab 11 * The above copyright notice and this permission notice shall be included
darienf 20:6d2af70c92ab 12 * in all copies or substantial portions of the Software.
darienf 20:6d2af70c92ab 13 *
darienf 20:6d2af70c92ab 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
darienf 20:6d2af70c92ab 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
darienf 20:6d2af70c92ab 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
darienf 20:6d2af70c92ab 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
darienf 20:6d2af70c92ab 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
darienf 20:6d2af70c92ab 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
darienf 20:6d2af70c92ab 20 * OTHER DEALINGS IN THE SOFTWARE.
darienf 20:6d2af70c92ab 21 *
darienf 20:6d2af70c92ab 22 * Except as contained in this notice, the name of Maxim Integrated
darienf 20:6d2af70c92ab 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
darienf 20:6d2af70c92ab 24 * Products, Inc. Branding Policy.
darienf 20:6d2af70c92ab 25 *
darienf 20:6d2af70c92ab 26 * The mere transfer of this software does not imply any licenses
darienf 20:6d2af70c92ab 27 * of trade secrets, proprietary technology, copyrights, patents,
darienf 20:6d2af70c92ab 28 * trademarks, maskwork rights, or any other form of intellectual
darienf 20:6d2af70c92ab 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
darienf 20:6d2af70c92ab 30 * ownership rights.
darienf 20:6d2af70c92ab 31 *******************************************************************************
darienf 20:6d2af70c92ab 32 */
darienf 20:6d2af70c92ab 33 #include "BluetoothLE.h"
darienf 20:6d2af70c92ab 34 #include "Characteristic.h"
darienf 20:6d2af70c92ab 35
darienf 20:6d2af70c92ab 36 /**
darienf 20:6d2af70c92ab 37 * @brief Constructor for class
darienf 20:6d2af70c92ab 38 */
darienf 20:6d2af70c92ab 39 BluetoothLE::BluetoothLE(BLE *ble, int numberOfCharacteristics)
darienf 20:6d2af70c92ab 40 : ble(ble), runningIndex(0),
darienf 20:6d2af70c92ab 41 numberOfCharacteristics(numberOfCharacteristics) {
darienf 20:6d2af70c92ab 42 characteristics = new Characteristic *[numberOfCharacteristics];
darienf 20:6d2af70c92ab 43 gattCharacteristics = new GattCharacteristic *[numberOfCharacteristics];
darienf 20:6d2af70c92ab 44 }
darienf 20:6d2af70c92ab 45
darienf 20:6d2af70c92ab 46 /**
darienf 20:6d2af70c92ab 47 * @brief Destructor for class
darienf 20:6d2af70c92ab 48 */
darienf 20:6d2af70c92ab 49 BluetoothLE::~BluetoothLE(void) {
darienf 20:6d2af70c92ab 50 for (int i = 0; i < numberOfCharacteristics; i++) delete characteristics[i];
darienf 20:6d2af70c92ab 51 delete[] gattCharacteristics;
darienf 20:6d2af70c92ab 52 delete[] characteristics;
darienf 20:6d2af70c92ab 53 }
darienf 20:6d2af70c92ab 54
darienf 20:6d2af70c92ab 55 /**
darienf 20:6d2af70c92ab 56 * @brief Initialize the advertising, characteristics, service for this platform
darienf 20:6d2af70c92ab 57 */
darienf 20:6d2af70c92ab 58 void BluetoothLE::addCharacteristic(Characteristic *characteristic) {
darienf 20:6d2af70c92ab 59 characteristic->setBLE(ble);
darienf 20:6d2af70c92ab 60 characteristic->setIndex(runningIndex);
darienf 20:6d2af70c92ab 61 characteristics[runningIndex] = characteristic;
darienf 20:6d2af70c92ab 62 gattCharacteristics[runningIndex] = characteristic->getGattCharacteristic();
darienf 20:6d2af70c92ab 63 runningIndex++;
darienf 20:6d2af70c92ab 64 }
darienf 20:6d2af70c92ab 65
darienf 20:6d2af70c92ab 66 /**
darienf 20:6d2af70c92ab 67 * @brief Initialize the advertising, characteristics, service for this platform
darienf 20:6d2af70c92ab 68 */
darienf 20:6d2af70c92ab 69 void BluetoothLE::initService(uint8_t *serialNumber, uint8_t *deviceName,
darienf 20:6d2af70c92ab 70 int nameSize, uint8_t *serviceUUID) {
darienf 20:6d2af70c92ab 71 ble->init();
darienf 20:6d2af70c92ab 72 ble->onDisconnection(this, &BluetoothLE::disconnectionCallback);
darienf 20:6d2af70c92ab 73
darienf 20:6d2af70c92ab 74 ble->gap().setAddress((BLEProtocol::AddressType_t)0, serialNumber);
darienf 20:6d2af70c92ab 75
darienf 20:6d2af70c92ab 76 // Setup Advertising
darienf 20:6d2af70c92ab 77 ble->accumulateAdvertisingPayload(
darienf 20:6d2af70c92ab 78 GapAdvertisingData::BREDR_NOT_SUPPORTED |
darienf 20:6d2af70c92ab 79 GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
darienf 20:6d2af70c92ab 80 ble->accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME,
darienf 20:6d2af70c92ab 81 (uint8_t *)deviceName, nameSize);
darienf 20:6d2af70c92ab 82 ble->setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
darienf 20:6d2af70c92ab 83 ble->setAdvertisingInterval(1600); // 1000ms; in multiples of 0.625ms.
darienf 20:6d2af70c92ab 84 GattService envService(serviceUUID, gattCharacteristics,
darienf 20:6d2af70c92ab 85 numberOfCharacteristics);
darienf 20:6d2af70c92ab 86 ble->gattServer().addService(envService);
darienf 20:6d2af70c92ab 87 ble->onDataWritten(this, &BluetoothLE::onDataWritten);
darienf 20:6d2af70c92ab 88 // Start Advertising
darienf 20:6d2af70c92ab 89 ble->startAdvertising();
darienf 20:6d2af70c92ab 90 }
darienf 20:6d2af70c92ab 91
darienf 20:6d2af70c92ab 92 static const Gap::ConnectionParams_t paramsLowPower = {
darienf 20:6d2af70c92ab 93 400, /**< Minimum Connection Interval in 1.25 ms units, see @ref BLE_GAP_CP_LIMITS.*/
darienf 20:6d2af70c92ab 94 400, /**< Maximum Connection Interval in 1.25 ms units, see @ref BLE_GAP_CP_LIMITS.*/
darienf 20:6d2af70c92ab 95 60, /**< Slave Latency in number of connection events, see @ref BLE_GAP_CP_LIMITS.*/
darienf 20:6d2af70c92ab 96 3100 /**< Connection Supervision Timeout in 10 ms units, see @ref BLE_GAP_CP_LIMITS.*/
darienf 20:6d2af70c92ab 97 };
darienf 20:6d2af70c92ab 98
darienf 20:6d2af70c92ab 99 static Gap::Handle_t connHandle = 0;
darienf 20:6d2af70c92ab 100
darienf 20:6d2af70c92ab 101 /**
darienf 20:6d2af70c92ab 102 * @brief Called on BLE connection
darienf 20:6d2af70c92ab 103 */
darienf 20:6d2af70c92ab 104 void BluetoothLE::connectionCallback(
darienf 20:6d2af70c92ab 105 const Gap::ConnectionCallbackParams_t *params) {
darienf 20:6d2af70c92ab 106 connHandle = params->handle;
darienf 20:6d2af70c92ab 107 }
darienf 20:6d2af70c92ab 108
darienf 20:6d2af70c92ab 109 /**
darienf 20:6d2af70c92ab 110 * @brief Start advertising on a disconnect
darienf 20:6d2af70c92ab 111 */
darienf 20:6d2af70c92ab 112 void BluetoothLE::disconnectionCallback(
darienf 20:6d2af70c92ab 113 const Gap::DisconnectionCallbackParams_t *params) {
darienf 20:6d2af70c92ab 114 _isConnected = false;
darienf 20:6d2af70c92ab 115 ble->startAdvertising();
darienf 20:6d2af70c92ab 116 }
darienf 20:6d2af70c92ab 117
darienf 20:6d2af70c92ab 118 /**
darienf 20:6d2af70c92ab 119 * @brief Called when the client writes to a writable characteristic
darienf 20:6d2af70c92ab 120 * @param params Pointer to a structure that contains details on what was
darienf 20:6d2af70c92ab 121 * written
darienf 20:6d2af70c92ab 122 */
darienf 20:6d2af70c92ab 123 void BluetoothLE::onDataWritten(const GattWriteCallbackParams *params) {
darienf 20:6d2af70c92ab 124 int i;
darienf 20:6d2af70c92ab 125 int index = 0;
darienf 20:6d2af70c92ab 126 // match the characteristic handle
darienf 20:6d2af70c92ab 127 printf("BluetoothLE::onDataWritten ");
darienf 20:6d2af70c92ab 128 for (i = 0; i < numberOfCharacteristics; i++) {
darienf 20:6d2af70c92ab 129 if (params->handle == gattCharacteristics[i]->getValueHandle()) {
darienf 20:6d2af70c92ab 130 characteristics[i]->copyDataWritten(params);
darienf 20:6d2af70c92ab 131 index = i;
darienf 20:6d2af70c92ab 132 break;
darienf 20:6d2af70c92ab 133 }
darienf 20:6d2af70c92ab 134 }
darienf 20:6d2af70c92ab 135 (*_onDataWritten)(index);
darienf 20:6d2af70c92ab 136 }
darienf 20:6d2af70c92ab 137
darienf 20:6d2af70c92ab 138 /**
darienf 20:6d2af70c92ab 139 * @brief Update the characteristic notification
darienf 20:6d2af70c92ab 140 * @param index Index of the characteristic
darienf 20:6d2af70c92ab 141 * @param data Pointer to the byte data to update the charateristic payload
darienf 20:6d2af70c92ab 142 */
darienf 20:6d2af70c92ab 143 void BluetoothLE::notifyCharacteristic(int index, uint8_t *data) {
darienf 20:6d2af70c92ab 144 for (int i = 0; i < characteristics[index]->getPayloadLength(); i++) {
darienf 20:6d2af70c92ab 145 characteristics[index]->getPayloadBytes()[i] = data[i];
darienf 20:6d2af70c92ab 146 }
darienf 20:6d2af70c92ab 147 characteristics[index]->update();
darienf 20:6d2af70c92ab 148 }
darienf 20:6d2af70c92ab 149
darienf 20:6d2af70c92ab 150 /**
darienf 20:6d2af70c92ab 151 * @brief Update the characteristic notification
darienf 20:6d2af70c92ab 152 * @param index Index of the characteristic
darienf 20:6d2af70c92ab 153 * @param data Pointer to the byte data to update the charateristic payload
darienf 20:6d2af70c92ab 154 */
darienf 20:6d2af70c92ab 155 void BluetoothLE::notifyCharacteristicTest(int index) {
darienf 20:6d2af70c92ab 156 for (int i = 0; i < characteristics[index]->getPayloadLength(); i++) {
darienf 20:6d2af70c92ab 157 characteristics[index]->getPayloadBytes()[i]++;
darienf 20:6d2af70c92ab 158 }
darienf 20:6d2af70c92ab 159 characteristics[index]->update();
darienf 20:6d2af70c92ab 160 }
darienf 20:6d2af70c92ab 161
darienf 20:6d2af70c92ab 162 uint8_t *BluetoothLE::getDataWritten(int index, int *length) {
darienf 20:6d2af70c92ab 163 return characteristics[index]->getDataWritten(length);
darienf 20:6d2af70c92ab 164 }
darienf 20:6d2af70c92ab 165
darienf 20:6d2af70c92ab 166 /**
darienf 20:6d2af70c92ab 167 * @brief Function to query if a BLE connection is active
darienf 20:6d2af70c92ab 168 * @return true if BLE connected, false if BLE is not connected
darienf 20:6d2af70c92ab 169 */
darienf 20:6d2af70c92ab 170 bool BluetoothLE::isConnected(void) {
darienf 20:6d2af70c92ab 171 return (ble->getGapState().connected == 1 ? true : false);
darienf 20:6d2af70c92ab 172 }