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