Jerry Bradshaw
/
mbed-os-test
ddd
Embed:
(wiki syntax)
Show/hide line numbers
BluetoothLE.cpp
00001 /******************************************************************************* 00002 * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. 00003 * 00004 * Permission is hereby granted, free of charge, to any person obtaining a 00005 * copy of this software and associated documentation files (the "Software"), 00006 * to deal in the Software without restriction, including without limitation 00007 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 00008 * and/or sell copies of the Software, and to permit persons to whom the 00009 * Software is furnished to do so, subject to the following conditions: 00010 * 00011 * The above copyright notice and this permission notice shall be included 00012 * in all copies or substantial portions of the Software. 00013 * 00014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 00015 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00016 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 00017 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES 00018 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 00019 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 00020 * OTHER DEALINGS IN THE SOFTWARE. 00021 * 00022 * Except as contained in this notice, the name of Maxim Integrated 00023 * Products, Inc. shall not be used except as stated in the Maxim Integrated 00024 * Products, Inc. Branding Policy. 00025 * 00026 * The mere transfer of this software does not imply any licenses 00027 * of trade secrets, proprietary technology, copyrights, patents, 00028 * trademarks, maskwork rights, or any other form of intellectual 00029 * property whatsoever. Maxim Integrated Products, Inc. retains all 00030 * ownership rights. 00031 ******************************************************************************* 00032 */ 00033 #include "BluetoothLE.h" 00034 #include "Characteristic.h" 00035 00036 /** 00037 * @brief Constructor for class 00038 */ 00039 BluetoothLE::BluetoothLE(BLE *ble, int numberOfCharacteristics) 00040 : ble(ble), runningIndex(0), 00041 numberOfCharacteristics(numberOfCharacteristics) { 00042 characteristics = new Characteristic *[numberOfCharacteristics]; 00043 gattCharacteristics = new GattCharacteristic *[numberOfCharacteristics]; 00044 } 00045 00046 /** 00047 * @brief Destructor for class 00048 */ 00049 BluetoothLE::~BluetoothLE(void) { 00050 for (int i = 0; i < numberOfCharacteristics; i++) delete characteristics[i]; 00051 delete[] gattCharacteristics; 00052 delete[] characteristics; 00053 } 00054 00055 /** 00056 * @brief Initialize the advertising, characteristics, service for this platform 00057 */ 00058 void BluetoothLE::addCharacteristic(Characteristic *characteristic) { 00059 characteristic->setBLE(ble); 00060 characteristic->setIndex(runningIndex); 00061 characteristics[runningIndex] = characteristic; 00062 gattCharacteristics[runningIndex] = characteristic->getGattCharacteristic(); 00063 runningIndex++; 00064 } 00065 00066 /** 00067 * @brief Initialize the advertising, characteristics, service for this platform 00068 */ 00069 void BluetoothLE::initService(uint8_t *serialNumber, uint8_t *deviceName, 00070 int nameSize, uint8_t *serviceUUID) { 00071 ble->init(); 00072 ble->onDisconnection(this, &BluetoothLE::disconnectionCallback); 00073 00074 ble->gap().setAddress((BLEProtocol::AddressType_t)0, serialNumber); 00075 00076 // Setup Advertising 00077 ble->accumulateAdvertisingPayload( 00078 GapAdvertisingData::BREDR_NOT_SUPPORTED | 00079 GapAdvertisingData::LE_GENERAL_DISCOVERABLE); 00080 ble->accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, 00081 (uint8_t *)deviceName, nameSize); 00082 ble->setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); 00083 ble->setAdvertisingInterval(1600); // 1000ms; in multiples of 0.625ms. 00084 GattService envService(serviceUUID, gattCharacteristics, 00085 numberOfCharacteristics); 00086 ble->gattServer().addService(envService); 00087 ble->onDataWritten(this, &BluetoothLE::onDataWritten); 00088 // Start Advertising 00089 ble->startAdvertising(); 00090 } 00091 00092 static const Gap::ConnectionParams_t paramsLowPower = { 00093 400, /**< Minimum Connection Interval in 1.25 ms units, see @ref BLE_GAP_CP_LIMITS.*/ 00094 400, /**< Maximum Connection Interval in 1.25 ms units, see @ref BLE_GAP_CP_LIMITS.*/ 00095 60, /**< Slave Latency in number of connection events, see @ref BLE_GAP_CP_LIMITS.*/ 00096 3100 /**< Connection Supervision Timeout in 10 ms units, see @ref BLE_GAP_CP_LIMITS.*/ 00097 }; 00098 00099 static Gap::Handle_t connHandle = 0; 00100 00101 /** 00102 * @brief Called on BLE connection 00103 */ 00104 void BluetoothLE::connectionCallback( 00105 const Gap::ConnectionCallbackParams_t *params) { 00106 connHandle = params->handle; 00107 } 00108 00109 /** 00110 * @brief Start advertising on a disconnect 00111 */ 00112 void BluetoothLE::disconnectionCallback( 00113 const Gap::DisconnectionCallbackParams_t *params) { 00114 _isConnected = false; 00115 ble->startAdvertising(); 00116 } 00117 00118 /** 00119 * @brief Called when the client writes to a writable characteristic 00120 * @param params Pointer to a structure that contains details on what was 00121 * written 00122 */ 00123 void BluetoothLE::onDataWritten(const GattWriteCallbackParams *params) { 00124 int i; 00125 int index = 0; 00126 // match the characteristic handle 00127 printf("BluetoothLE::onDataWritten "); 00128 for (i = 0; i < numberOfCharacteristics; i++) { 00129 if (params->handle == gattCharacteristics[i]->getValueHandle()) { 00130 characteristics[i]->copyDataWritten(params); 00131 index = i; 00132 break; 00133 } 00134 } 00135 (*_onDataWritten)(index); 00136 } 00137 00138 /** 00139 * @brief Update the characteristic notification 00140 * @param index Index of the characteristic 00141 * @param data Pointer to the byte data to update the charateristic payload 00142 */ 00143 void BluetoothLE::notifyCharacteristic(int index, uint8_t *data) { 00144 for (int i = 0; i < characteristics[index]->getPayloadLength(); i++) { 00145 characteristics[index]->getPayloadBytes()[i] = data[i]; 00146 } 00147 characteristics[index]->update(); 00148 } 00149 00150 /** 00151 * @brief Update the characteristic notification 00152 * @param index Index of the characteristic 00153 * @param data Pointer to the byte data to update the charateristic payload 00154 */ 00155 void BluetoothLE::notifyCharacteristicTest(int index) { 00156 for (int i = 0; i < characteristics[index]->getPayloadLength(); i++) { 00157 characteristics[index]->getPayloadBytes()[i]++; 00158 } 00159 characteristics[index]->update(); 00160 } 00161 00162 uint8_t *BluetoothLE::getDataWritten(int index, int *length) { 00163 return characteristics[index]->getDataWritten(length); 00164 } 00165 00166 /** 00167 * @brief Function to query if a BLE connection is active 00168 * @return true if BLE connected, false if BLE is not connected 00169 */ 00170 bool BluetoothLE::isConnected(void) { 00171 return (ble->getGapState().connected == 1 ? true : false); 00172 }
Generated on Tue Jul 12 2022 21:12:43 by 1.7.2