added debugging
Fork of BLE_nRF8001 by
BLE_nRF8001/BLECharacteristic.cpp@2:7805a5595aab, 2014-11-10 (annotated)
- Committer:
- jn80842
- Date:
- Mon Nov 10 01:24:23 2014 +0000
- Revision:
- 2:7805a5595aab
- Parent:
- 0:075ea2812998
just added debugging
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
RedBearLab | 0:075ea2812998 | 1 | #include "Arduino.h" |
RedBearLab | 0:075ea2812998 | 2 | #include "BLECharacteristic.h" |
RedBearLab | 0:075ea2812998 | 3 | |
RedBearLab | 0:075ea2812998 | 4 | BLECharacteristic::BLECharacteristic(const char* uuid, unsigned char properties, unsigned char valueSize, bool fixedLength) : |
RedBearLab | 0:075ea2812998 | 5 | BLEAttribute(uuid, BLETypeCharacteristic), |
RedBearLab | 0:075ea2812998 | 6 | _properties(properties), |
RedBearLab | 0:075ea2812998 | 7 | _valueSize(min(valueSize, BLE_ATTRIBUTE_MAX_VALUE_LENGTH)), |
RedBearLab | 0:075ea2812998 | 8 | _valueLength(0), |
RedBearLab | 0:075ea2812998 | 9 | _fixedLength(fixedLength), |
RedBearLab | 0:075ea2812998 | 10 | _written(false), |
RedBearLab | 0:075ea2812998 | 11 | _subscribed(false), |
RedBearLab | 0:075ea2812998 | 12 | _listener(NULL) |
RedBearLab | 0:075ea2812998 | 13 | { |
RedBearLab | 0:075ea2812998 | 14 | memset(this->_eventHandlers, 0x00, sizeof(this->_eventHandlers)); |
RedBearLab | 0:075ea2812998 | 15 | |
RedBearLab | 0:075ea2812998 | 16 | _value = (unsigned char*)malloc(this->_valueSize); |
RedBearLab | 0:075ea2812998 | 17 | } |
RedBearLab | 0:075ea2812998 | 18 | |
RedBearLab | 0:075ea2812998 | 19 | BLECharacteristic::BLECharacteristic(const char* uuid, unsigned char properties, const char* value, bool fixedLength) : |
RedBearLab | 0:075ea2812998 | 20 | BLEAttribute(uuid, BLETypeCharacteristic), |
RedBearLab | 0:075ea2812998 | 21 | _properties(properties), |
RedBearLab | 0:075ea2812998 | 22 | _valueSize(min(strlen(value), BLE_ATTRIBUTE_MAX_VALUE_LENGTH)), |
RedBearLab | 0:075ea2812998 | 23 | _valueLength(0), |
RedBearLab | 0:075ea2812998 | 24 | _fixedLength(fixedLength), |
RedBearLab | 0:075ea2812998 | 25 | _written(false), |
RedBearLab | 0:075ea2812998 | 26 | _subscribed(false), |
RedBearLab | 0:075ea2812998 | 27 | _listener(NULL) |
RedBearLab | 0:075ea2812998 | 28 | { |
RedBearLab | 0:075ea2812998 | 29 | memset(this->_eventHandlers, 0x00, sizeof(this->_eventHandlers)); |
RedBearLab | 0:075ea2812998 | 30 | |
RedBearLab | 0:075ea2812998 | 31 | _value = (unsigned char*)malloc(this->_valueSize); |
RedBearLab | 0:075ea2812998 | 32 | this->setValue(value); |
RedBearLab | 0:075ea2812998 | 33 | } |
RedBearLab | 0:075ea2812998 | 34 | |
RedBearLab | 0:075ea2812998 | 35 | BLECharacteristic::~BLECharacteristic() { |
RedBearLab | 0:075ea2812998 | 36 | if (this->_value) { |
RedBearLab | 0:075ea2812998 | 37 | free(this->_value); |
RedBearLab | 0:075ea2812998 | 38 | } |
RedBearLab | 0:075ea2812998 | 39 | } |
RedBearLab | 0:075ea2812998 | 40 | |
RedBearLab | 0:075ea2812998 | 41 | unsigned char BLECharacteristic::properties() const { |
RedBearLab | 0:075ea2812998 | 42 | return this->_properties; |
RedBearLab | 0:075ea2812998 | 43 | } |
RedBearLab | 0:075ea2812998 | 44 | |
RedBearLab | 0:075ea2812998 | 45 | unsigned char BLECharacteristic::valueSize() const { |
RedBearLab | 0:075ea2812998 | 46 | return this->_valueSize; |
RedBearLab | 0:075ea2812998 | 47 | } |
RedBearLab | 0:075ea2812998 | 48 | |
RedBearLab | 0:075ea2812998 | 49 | unsigned const char* BLECharacteristic::value() const { |
RedBearLab | 0:075ea2812998 | 50 | return this->_value; |
RedBearLab | 0:075ea2812998 | 51 | } |
RedBearLab | 0:075ea2812998 | 52 | |
RedBearLab | 0:075ea2812998 | 53 | unsigned char BLECharacteristic::valueLength() const { |
RedBearLab | 0:075ea2812998 | 54 | return this->_valueLength; |
RedBearLab | 0:075ea2812998 | 55 | } |
RedBearLab | 0:075ea2812998 | 56 | |
RedBearLab | 0:075ea2812998 | 57 | bool BLECharacteristic::fixedLength() const { |
RedBearLab | 0:075ea2812998 | 58 | return this->_fixedLength; |
RedBearLab | 0:075ea2812998 | 59 | } |
RedBearLab | 0:075ea2812998 | 60 | |
RedBearLab | 0:075ea2812998 | 61 | bool BLECharacteristic::setValue(const unsigned char value[], unsigned char length) { |
RedBearLab | 0:075ea2812998 | 62 | bool success = true; |
RedBearLab | 0:075ea2812998 | 63 | |
RedBearLab | 0:075ea2812998 | 64 | this->_valueLength = min(length, this->_valueSize); |
RedBearLab | 0:075ea2812998 | 65 | |
RedBearLab | 0:075ea2812998 | 66 | memcpy(this->_value, value, this->_valueLength); |
RedBearLab | 0:075ea2812998 | 67 | |
RedBearLab | 0:075ea2812998 | 68 | if (this->_listener) { |
RedBearLab | 0:075ea2812998 | 69 | success = this->_listener->characteristicValueChanged(*this); |
RedBearLab | 0:075ea2812998 | 70 | } |
RedBearLab | 0:075ea2812998 | 71 | |
RedBearLab | 0:075ea2812998 | 72 | return success; |
RedBearLab | 0:075ea2812998 | 73 | } |
RedBearLab | 0:075ea2812998 | 74 | |
RedBearLab | 0:075ea2812998 | 75 | bool BLECharacteristic::setValue(const char* value) { |
RedBearLab | 0:075ea2812998 | 76 | return this->setValue((const unsigned char *)value, strlen(value)); |
RedBearLab | 0:075ea2812998 | 77 | } |
RedBearLab | 0:075ea2812998 | 78 | |
RedBearLab | 0:075ea2812998 | 79 | bool BLECharacteristic::written() { |
RedBearLab | 0:075ea2812998 | 80 | bool written = this->_written; |
RedBearLab | 0:075ea2812998 | 81 | |
RedBearLab | 0:075ea2812998 | 82 | if (written) { |
RedBearLab | 0:075ea2812998 | 83 | this->_written = false; |
RedBearLab | 0:075ea2812998 | 84 | } |
RedBearLab | 0:075ea2812998 | 85 | |
RedBearLab | 0:075ea2812998 | 86 | return written; |
RedBearLab | 0:075ea2812998 | 87 | } |
RedBearLab | 0:075ea2812998 | 88 | |
RedBearLab | 0:075ea2812998 | 89 | void BLECharacteristic::setValue(BLECentral& central, const unsigned char value[], unsigned char length) { |
RedBearLab | 0:075ea2812998 | 90 | this->setValue(value, length); |
RedBearLab | 0:075ea2812998 | 91 | |
RedBearLab | 0:075ea2812998 | 92 | this->_written = true; |
RedBearLab | 0:075ea2812998 | 93 | |
RedBearLab | 0:075ea2812998 | 94 | BLECharacteristicEventHandler eventHandler = this->_eventHandlers[BLEWritten]; |
RedBearLab | 0:075ea2812998 | 95 | if (eventHandler) { |
RedBearLab | 0:075ea2812998 | 96 | eventHandler(central, *this); |
RedBearLab | 0:075ea2812998 | 97 | } |
RedBearLab | 0:075ea2812998 | 98 | } |
RedBearLab | 0:075ea2812998 | 99 | |
RedBearLab | 0:075ea2812998 | 100 | bool BLECharacteristic::subscribed() { |
RedBearLab | 0:075ea2812998 | 101 | return this->_subscribed; |
RedBearLab | 0:075ea2812998 | 102 | } |
RedBearLab | 0:075ea2812998 | 103 | |
RedBearLab | 0:075ea2812998 | 104 | void BLECharacteristic::setSubscribed(BLECentral& central, bool subscribed) { |
RedBearLab | 0:075ea2812998 | 105 | this->_subscribed = subscribed; |
RedBearLab | 0:075ea2812998 | 106 | |
RedBearLab | 0:075ea2812998 | 107 | BLECharacteristicEventHandler eventHandler = this->_eventHandlers[subscribed ? BLESubscribed : BLEUnsubscribed]; |
RedBearLab | 0:075ea2812998 | 108 | |
RedBearLab | 0:075ea2812998 | 109 | if (eventHandler) { |
RedBearLab | 0:075ea2812998 | 110 | eventHandler(central, *this); |
RedBearLab | 0:075ea2812998 | 111 | } |
RedBearLab | 0:075ea2812998 | 112 | } |
RedBearLab | 0:075ea2812998 | 113 | |
RedBearLab | 0:075ea2812998 | 114 | bool BLECharacteristic::canNotify() { |
RedBearLab | 0:075ea2812998 | 115 | return (this->_listener) ? this->_listener->canNotifyCharacteristic(*this) : false; |
RedBearLab | 0:075ea2812998 | 116 | } |
RedBearLab | 0:075ea2812998 | 117 | |
RedBearLab | 0:075ea2812998 | 118 | bool BLECharacteristic::canIndicate() { |
RedBearLab | 0:075ea2812998 | 119 | return (this->_listener) ? this->_listener->canIndicateCharacteristic(*this) : false; |
RedBearLab | 0:075ea2812998 | 120 | } |
RedBearLab | 0:075ea2812998 | 121 | |
RedBearLab | 0:075ea2812998 | 122 | void BLECharacteristic::setEventHandler(BLECharacteristicEvent event, BLECharacteristicEventHandler eventHandler) { |
RedBearLab | 0:075ea2812998 | 123 | if (event < sizeof(this->_eventHandlers)) { |
RedBearLab | 0:075ea2812998 | 124 | this->_eventHandlers[event] = eventHandler; |
RedBearLab | 0:075ea2812998 | 125 | } |
RedBearLab | 0:075ea2812998 | 126 | } |
RedBearLab | 0:075ea2812998 | 127 | |
RedBearLab | 0:075ea2812998 | 128 | void BLECharacteristic::setValueChangeListener(BLECharacteristicValueChangeListener& listener) { |
RedBearLab | 0:075ea2812998 | 129 | this->_listener = &listener; |
RedBearLab | 0:075ea2812998 | 130 | } |