Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of BLE_nRF8001 by
BLECharacteristic.cpp
00001 #include "Arduino.h" 00002 #include "BLECharacteristic.h" 00003 00004 BLECharacteristic::BLECharacteristic(const char* uuid, unsigned char properties, unsigned char valueSize, bool fixedLength) : 00005 BLEAttribute(uuid, BLETypeCharacteristic), 00006 _properties(properties), 00007 _valueSize(min(valueSize, BLE_ATTRIBUTE_MAX_VALUE_LENGTH)), 00008 _valueLength(0), 00009 _fixedLength(fixedLength), 00010 _written(false), 00011 _subscribed(false), 00012 _listener(NULL) 00013 { 00014 memset(this->_eventHandlers, 0x00, sizeof(this->_eventHandlers)); 00015 00016 _value = (unsigned char*)malloc(this->_valueSize); 00017 } 00018 00019 BLECharacteristic::BLECharacteristic(const char* uuid, unsigned char properties, const char* value, bool fixedLength) : 00020 BLEAttribute(uuid, BLETypeCharacteristic), 00021 _properties(properties), 00022 _valueSize(min(strlen(value), BLE_ATTRIBUTE_MAX_VALUE_LENGTH)), 00023 _valueLength(0), 00024 _fixedLength(fixedLength), 00025 _written(false), 00026 _subscribed(false), 00027 _listener(NULL) 00028 { 00029 memset(this->_eventHandlers, 0x00, sizeof(this->_eventHandlers)); 00030 00031 _value = (unsigned char*)malloc(this->_valueSize); 00032 this->setValue(value); 00033 } 00034 00035 BLECharacteristic::~BLECharacteristic() { 00036 if (this->_value) { 00037 free(this->_value); 00038 } 00039 } 00040 00041 unsigned char BLECharacteristic::properties() const { 00042 return this->_properties; 00043 } 00044 00045 unsigned char BLECharacteristic::valueSize() const { 00046 return this->_valueSize; 00047 } 00048 00049 unsigned const char* BLECharacteristic::value() const { 00050 return this->_value; 00051 } 00052 00053 unsigned char BLECharacteristic::valueLength() const { 00054 return this->_valueLength; 00055 } 00056 00057 bool BLECharacteristic::fixedLength() const { 00058 return this->_fixedLength; 00059 } 00060 00061 bool BLECharacteristic::setValue(const unsigned char value[], unsigned char length) { 00062 bool success = true; 00063 00064 this->_valueLength = min(length, this->_valueSize); 00065 00066 memcpy(this->_value, value, this->_valueLength); 00067 00068 if (this->_listener) { 00069 success = this->_listener->characteristicValueChanged(*this); 00070 } 00071 00072 return success; 00073 } 00074 00075 bool BLECharacteristic::setValue(const char* value) { 00076 return this->setValue((const unsigned char *)value, strlen(value)); 00077 } 00078 00079 bool BLECharacteristic::written() { 00080 bool written = this->_written; 00081 00082 if (written) { 00083 this->_written = false; 00084 } 00085 00086 return written; 00087 } 00088 00089 void BLECharacteristic::setValue(BLECentral& central, const unsigned char value[], unsigned char length) { 00090 this->setValue(value, length); 00091 00092 this->_written = true; 00093 00094 BLECharacteristicEventHandler eventHandler = this->_eventHandlers[BLEWritten]; 00095 if (eventHandler) { 00096 eventHandler(central, *this); 00097 } 00098 } 00099 00100 bool BLECharacteristic::subscribed() { 00101 return this->_subscribed; 00102 } 00103 00104 void BLECharacteristic::setSubscribed(BLECentral& central, bool subscribed) { 00105 this->_subscribed = subscribed; 00106 00107 BLECharacteristicEventHandler eventHandler = this->_eventHandlers[subscribed ? BLESubscribed : BLEUnsubscribed]; 00108 00109 if (eventHandler) { 00110 eventHandler(central, *this); 00111 } 00112 } 00113 00114 bool BLECharacteristic::canNotify() { 00115 return (this->_listener) ? this->_listener->canNotifyCharacteristic(*this) : false; 00116 } 00117 00118 bool BLECharacteristic::canIndicate() { 00119 return (this->_listener) ? this->_listener->canIndicateCharacteristic(*this) : false; 00120 } 00121 00122 void BLECharacteristic::setEventHandler(BLECharacteristicEvent event, BLECharacteristicEventHandler eventHandler) { 00123 if (event < sizeof(this->_eventHandlers)) { 00124 this->_eventHandlers[event] = eventHandler; 00125 } 00126 } 00127 00128 void BLECharacteristic::setValueChangeListener(BLECharacteristicValueChangeListener& listener) { 00129 this->_listener = &listener; 00130 }
Generated on Tue Jul 12 2022 15:15:46 by
1.7.2
