added debugging
Fork of BLE_nRF8001 by
BLE_nRF8001/BLEPeripheral.cpp@0:075ea2812998, 2014-10-17 (annotated)
- Committer:
- RedBearLab
- Date:
- Fri Oct 17 22:40:32 2014 +0800
- Revision:
- 0:075ea2812998
- Child:
- 2:7805a5595aab
BLE_nRF8001 library first commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
RedBearLab | 0:075ea2812998 | 1 | #include "BLEUuid.h" |
RedBearLab | 0:075ea2812998 | 2 | #include "BLEPeripheral.h" |
RedBearLab | 0:075ea2812998 | 3 | |
RedBearLab | 0:075ea2812998 | 4 | //#define BLE_PERIPHERAL_DEBUG |
RedBearLab | 0:075ea2812998 | 5 | |
RedBearLab | 0:075ea2812998 | 6 | #define DEFAULT_DEVICE_NAME "Arduino" |
RedBearLab | 0:075ea2812998 | 7 | #define DEFAULT_APPEARANCE 0x0000 |
RedBearLab | 0:075ea2812998 | 8 | |
RedBearLab | 0:075ea2812998 | 9 | BLEPeripheral::BLEPeripheral(DigitalInOut *req, DigitalInOut *rdy, DigitalInOut *rst) : |
RedBearLab | 0:075ea2812998 | 10 | _nRF8001(req, rdy, rst), |
RedBearLab | 0:075ea2812998 | 11 | |
RedBearLab | 0:075ea2812998 | 12 | _localName(NULL), |
RedBearLab | 0:075ea2812998 | 13 | _manufacturerData(NULL), |
RedBearLab | 0:075ea2812998 | 14 | _manufacturerDataLength(0), |
RedBearLab | 0:075ea2812998 | 15 | |
RedBearLab | 0:075ea2812998 | 16 | _attributes(NULL), |
RedBearLab | 0:075ea2812998 | 17 | _numAttributes(0), |
RedBearLab | 0:075ea2812998 | 18 | |
RedBearLab | 0:075ea2812998 | 19 | _genericAccessService("1800"), |
RedBearLab | 0:075ea2812998 | 20 | _deviceNameCharacteristic("2a00", BLERead, 19), |
RedBearLab | 0:075ea2812998 | 21 | _appearanceCharacteristic("2a01", BLERead, 2), |
RedBearLab | 0:075ea2812998 | 22 | _genericAttributeService("1801"), |
RedBearLab | 0:075ea2812998 | 23 | _servicesChangedCharacteristic("2a05", BLEIndicate, 4), |
RedBearLab | 0:075ea2812998 | 24 | |
RedBearLab | 0:075ea2812998 | 25 | _central(this) |
RedBearLab | 0:075ea2812998 | 26 | { |
RedBearLab | 0:075ea2812998 | 27 | memset(this->_eventHandlers, 0x00, sizeof(this->_eventHandlers)); |
RedBearLab | 0:075ea2812998 | 28 | |
RedBearLab | 0:075ea2812998 | 29 | this->setDeviceName(DEFAULT_DEVICE_NAME); |
RedBearLab | 0:075ea2812998 | 30 | this->setAppearance(DEFAULT_APPEARANCE); |
RedBearLab | 0:075ea2812998 | 31 | |
RedBearLab | 0:075ea2812998 | 32 | this->_nRF8001.setEventListener(this); |
RedBearLab | 0:075ea2812998 | 33 | } |
RedBearLab | 0:075ea2812998 | 34 | |
RedBearLab | 0:075ea2812998 | 35 | BLEPeripheral::~BLEPeripheral() { |
RedBearLab | 0:075ea2812998 | 36 | if (this->_attributes) { |
RedBearLab | 0:075ea2812998 | 37 | free(this->_attributes); |
RedBearLab | 0:075ea2812998 | 38 | } |
RedBearLab | 0:075ea2812998 | 39 | } |
RedBearLab | 0:075ea2812998 | 40 | |
RedBearLab | 0:075ea2812998 | 41 | void BLEPeripheral::begin() { |
RedBearLab | 0:075ea2812998 | 42 | unsigned char advertisementData[20]; |
RedBearLab | 0:075ea2812998 | 43 | unsigned char scanData[20]; |
RedBearLab | 0:075ea2812998 | 44 | |
RedBearLab | 0:075ea2812998 | 45 | unsigned char advertisementDataLength = 0; |
RedBearLab | 0:075ea2812998 | 46 | unsigned char scanDataLength = 0; |
RedBearLab | 0:075ea2812998 | 47 | |
RedBearLab | 0:075ea2812998 | 48 | if (this->_advertisedServiceUuid){ |
RedBearLab | 0:075ea2812998 | 49 | BLEUuid advertisedServiceUuid = BLEUuid(this->_advertisedServiceUuid); |
RedBearLab | 0:075ea2812998 | 50 | unsigned char advertisedServiceUuidLength = advertisedServiceUuid.length(); |
RedBearLab | 0:075ea2812998 | 51 | |
RedBearLab | 0:075ea2812998 | 52 | advertisementDataLength = 2 + advertisedServiceUuidLength; |
RedBearLab | 0:075ea2812998 | 53 | |
RedBearLab | 0:075ea2812998 | 54 | advertisementData[0] = (advertisedServiceUuidLength > 2) ? 0x06 : 0x02; |
RedBearLab | 0:075ea2812998 | 55 | advertisementData[1] = advertisedServiceUuidLength; |
RedBearLab | 0:075ea2812998 | 56 | |
RedBearLab | 0:075ea2812998 | 57 | memcpy(&advertisementData[2], advertisedServiceUuid.data(), advertisedServiceUuidLength); |
RedBearLab | 0:075ea2812998 | 58 | } else if (this->_manufacturerData && this->_manufacturerDataLength > 0) { |
RedBearLab | 0:075ea2812998 | 59 | if (this->_manufacturerDataLength > sizeof(advertisementData)) { |
RedBearLab | 0:075ea2812998 | 60 | this->_manufacturerDataLength = sizeof(advertisementData); |
RedBearLab | 0:075ea2812998 | 61 | } |
RedBearLab | 0:075ea2812998 | 62 | |
RedBearLab | 0:075ea2812998 | 63 | advertisementDataLength = 2 + this->_manufacturerDataLength; |
RedBearLab | 0:075ea2812998 | 64 | |
RedBearLab | 0:075ea2812998 | 65 | advertisementData[0] = 0xff; |
RedBearLab | 0:075ea2812998 | 66 | advertisementData[1] = this->_manufacturerDataLength; |
RedBearLab | 0:075ea2812998 | 67 | memcpy(&advertisementData[2], this->_manufacturerData, this->_manufacturerDataLength); |
RedBearLab | 0:075ea2812998 | 68 | } |
RedBearLab | 0:075ea2812998 | 69 | |
RedBearLab | 0:075ea2812998 | 70 | if (this->_localName){ |
RedBearLab | 0:075ea2812998 | 71 | unsigned char originalLocalNameLength = strlen(this->_localName); |
RedBearLab | 0:075ea2812998 | 72 | unsigned char localNameLength = originalLocalNameLength; |
RedBearLab | 0:075ea2812998 | 73 | |
RedBearLab | 0:075ea2812998 | 74 | if (localNameLength > sizeof(scanData)) { |
RedBearLab | 0:075ea2812998 | 75 | localNameLength = sizeof(scanData); |
RedBearLab | 0:075ea2812998 | 76 | } |
RedBearLab | 0:075ea2812998 | 77 | |
RedBearLab | 0:075ea2812998 | 78 | scanDataLength = 2 + localNameLength; |
RedBearLab | 0:075ea2812998 | 79 | |
RedBearLab | 0:075ea2812998 | 80 | scanData[0] = (originalLocalNameLength > sizeof(scanData)) ? 0x08 : 0x09; |
RedBearLab | 0:075ea2812998 | 81 | scanData[1] = localNameLength; |
RedBearLab | 0:075ea2812998 | 82 | |
RedBearLab | 0:075ea2812998 | 83 | memcpy(&scanData[2], this->_localName, localNameLength); |
RedBearLab | 0:075ea2812998 | 84 | } |
RedBearLab | 0:075ea2812998 | 85 | |
RedBearLab | 0:075ea2812998 | 86 | for (int i = 0; i < this->_numAttributes; i++) { |
RedBearLab | 0:075ea2812998 | 87 | BLEAttribute* attribute = this->_attributes[i]; |
RedBearLab | 0:075ea2812998 | 88 | if (attribute->type() == BLETypeCharacteristic) { |
RedBearLab | 0:075ea2812998 | 89 | BLECharacteristic* characteristic = (BLECharacteristic*)attribute; |
RedBearLab | 0:075ea2812998 | 90 | |
RedBearLab | 0:075ea2812998 | 91 | characteristic->setValueChangeListener(*this); |
RedBearLab | 0:075ea2812998 | 92 | } |
RedBearLab | 0:075ea2812998 | 93 | } |
RedBearLab | 0:075ea2812998 | 94 | |
RedBearLab | 0:075ea2812998 | 95 | this->_nRF8001.begin(advertisementData, advertisementDataLength, scanData, scanDataLength, this->_attributes, this->_numAttributes); |
RedBearLab | 0:075ea2812998 | 96 | |
RedBearLab | 0:075ea2812998 | 97 | this->_nRF8001.requestAddress(); |
RedBearLab | 0:075ea2812998 | 98 | } |
RedBearLab | 0:075ea2812998 | 99 | |
RedBearLab | 0:075ea2812998 | 100 | void BLEPeripheral::poll() { |
RedBearLab | 0:075ea2812998 | 101 | this->_nRF8001.poll(); |
RedBearLab | 0:075ea2812998 | 102 | } |
RedBearLab | 0:075ea2812998 | 103 | |
RedBearLab | 0:075ea2812998 | 104 | void BLEPeripheral::setAdvertisedServiceUuid(const char* advertisedServiceUuid) { |
RedBearLab | 0:075ea2812998 | 105 | this->_advertisedServiceUuid = advertisedServiceUuid; |
RedBearLab | 0:075ea2812998 | 106 | } |
RedBearLab | 0:075ea2812998 | 107 | |
RedBearLab | 0:075ea2812998 | 108 | void BLEPeripheral::setManufacturerData(const unsigned char manufacturerData[], unsigned char manufacturerDataLength) { |
RedBearLab | 0:075ea2812998 | 109 | this->_manufacturerData = manufacturerData; |
RedBearLab | 0:075ea2812998 | 110 | this->_manufacturerDataLength = manufacturerDataLength; |
RedBearLab | 0:075ea2812998 | 111 | } |
RedBearLab | 0:075ea2812998 | 112 | |
RedBearLab | 0:075ea2812998 | 113 | void BLEPeripheral::setLocalName(const char* localName) { |
RedBearLab | 0:075ea2812998 | 114 | this->_localName = localName; |
RedBearLab | 0:075ea2812998 | 115 | } |
RedBearLab | 0:075ea2812998 | 116 | |
RedBearLab | 0:075ea2812998 | 117 | void BLEPeripheral::setDeviceName(const char* deviceName) { |
RedBearLab | 0:075ea2812998 | 118 | this->_deviceNameCharacteristic.setValue(deviceName); |
RedBearLab | 0:075ea2812998 | 119 | } |
RedBearLab | 0:075ea2812998 | 120 | |
RedBearLab | 0:075ea2812998 | 121 | void BLEPeripheral::setAppearance(unsigned short appearance) { |
RedBearLab | 0:075ea2812998 | 122 | this->_appearanceCharacteristic.setValue((unsigned char *)&appearance, sizeof(appearance)); |
RedBearLab | 0:075ea2812998 | 123 | } |
RedBearLab | 0:075ea2812998 | 124 | |
RedBearLab | 0:075ea2812998 | 125 | void BLEPeripheral::addAttribute(BLEAttribute& attribute) { |
RedBearLab | 0:075ea2812998 | 126 | if (this->_attributes == NULL) { |
RedBearLab | 0:075ea2812998 | 127 | this->_attributes = (BLEAttribute**)malloc(BLEAttribute::numAttributes() * sizeof(BLEAttribute*)); |
RedBearLab | 0:075ea2812998 | 128 | |
RedBearLab | 0:075ea2812998 | 129 | this->_attributes[0] = &this->_genericAccessService; |
RedBearLab | 0:075ea2812998 | 130 | this->_attributes[1] = &this->_deviceNameCharacteristic; |
RedBearLab | 0:075ea2812998 | 131 | this->_attributes[2] = &this->_appearanceCharacteristic; |
RedBearLab | 0:075ea2812998 | 132 | |
RedBearLab | 0:075ea2812998 | 133 | this->_attributes[3] = &this->_genericAttributeService; |
RedBearLab | 0:075ea2812998 | 134 | this->_attributes[4] = &this->_servicesChangedCharacteristic; |
RedBearLab | 0:075ea2812998 | 135 | |
RedBearLab | 0:075ea2812998 | 136 | this->_numAttributes = 5; |
RedBearLab | 0:075ea2812998 | 137 | } |
RedBearLab | 0:075ea2812998 | 138 | |
RedBearLab | 0:075ea2812998 | 139 | this->_attributes[this->_numAttributes] = &attribute; |
RedBearLab | 0:075ea2812998 | 140 | this->_numAttributes++; |
RedBearLab | 0:075ea2812998 | 141 | } |
RedBearLab | 0:075ea2812998 | 142 | |
RedBearLab | 0:075ea2812998 | 143 | void BLEPeripheral::disconnect() { |
RedBearLab | 0:075ea2812998 | 144 | this->_nRF8001.disconnect(); |
RedBearLab | 0:075ea2812998 | 145 | } |
RedBearLab | 0:075ea2812998 | 146 | |
RedBearLab | 0:075ea2812998 | 147 | BLECentral BLEPeripheral::central() { |
RedBearLab | 0:075ea2812998 | 148 | this->poll(); |
RedBearLab | 0:075ea2812998 | 149 | |
RedBearLab | 0:075ea2812998 | 150 | return this->_central; |
RedBearLab | 0:075ea2812998 | 151 | } |
RedBearLab | 0:075ea2812998 | 152 | |
RedBearLab | 0:075ea2812998 | 153 | bool BLEPeripheral::connected() { |
RedBearLab | 0:075ea2812998 | 154 | this->poll(); |
RedBearLab | 0:075ea2812998 | 155 | |
RedBearLab | 0:075ea2812998 | 156 | return this->_central; |
RedBearLab | 0:075ea2812998 | 157 | } |
RedBearLab | 0:075ea2812998 | 158 | |
RedBearLab | 0:075ea2812998 | 159 | void BLEPeripheral::setEventHandler(BLEPeripheralEvent event, BLEPeripheralEventHandler eventHandler) { |
RedBearLab | 0:075ea2812998 | 160 | if (event < sizeof(this->_eventHandlers)) { |
RedBearLab | 0:075ea2812998 | 161 | this->_eventHandlers[event] = eventHandler; |
RedBearLab | 0:075ea2812998 | 162 | } |
RedBearLab | 0:075ea2812998 | 163 | } |
RedBearLab | 0:075ea2812998 | 164 | |
RedBearLab | 0:075ea2812998 | 165 | bool BLEPeripheral::characteristicValueChanged(BLECharacteristic& characteristic) { |
RedBearLab | 0:075ea2812998 | 166 | return this->_nRF8001.updateCharacteristicValue(characteristic); |
RedBearLab | 0:075ea2812998 | 167 | } |
RedBearLab | 0:075ea2812998 | 168 | |
RedBearLab | 0:075ea2812998 | 169 | bool BLEPeripheral::canNotifyCharacteristic(BLECharacteristic& characteristic) { |
RedBearLab | 0:075ea2812998 | 170 | return this->_nRF8001.canNotifyCharacteristic(characteristic); |
RedBearLab | 0:075ea2812998 | 171 | } |
RedBearLab | 0:075ea2812998 | 172 | |
RedBearLab | 0:075ea2812998 | 173 | bool BLEPeripheral::canIndicateCharacteristic(BLECharacteristic& characteristic) { |
RedBearLab | 0:075ea2812998 | 174 | return this->_nRF8001.canIndicateCharacteristic(characteristic); |
RedBearLab | 0:075ea2812998 | 175 | } |
RedBearLab | 0:075ea2812998 | 176 | |
RedBearLab | 0:075ea2812998 | 177 | void BLEPeripheral::nRF8001Connected(nRF8001& nRF8001, const unsigned char* address) { |
RedBearLab | 0:075ea2812998 | 178 | this->_central.setAddress(address); |
RedBearLab | 0:075ea2812998 | 179 | |
RedBearLab | 0:075ea2812998 | 180 | #ifdef BLE_PERIPHERAL_DEBUG |
RedBearLab | 0:075ea2812998 | 181 | Serial.print(F("Peripheral connected to central: ")); |
RedBearLab | 0:075ea2812998 | 182 | Serial.println(this->_central.address()); |
RedBearLab | 0:075ea2812998 | 183 | #endif |
RedBearLab | 0:075ea2812998 | 184 | |
RedBearLab | 0:075ea2812998 | 185 | BLEPeripheralEventHandler eventHandler = this->_eventHandlers[BLEConnected]; |
RedBearLab | 0:075ea2812998 | 186 | if (eventHandler) { |
RedBearLab | 0:075ea2812998 | 187 | eventHandler(this->_central); |
RedBearLab | 0:075ea2812998 | 188 | } |
RedBearLab | 0:075ea2812998 | 189 | } |
RedBearLab | 0:075ea2812998 | 190 | |
RedBearLab | 0:075ea2812998 | 191 | void BLEPeripheral::nRF8001Disconnected(nRF8001& nRF8001) { |
RedBearLab | 0:075ea2812998 | 192 | #ifdef BLE_PERIPHERAL_DEBUG |
RedBearLab | 0:075ea2812998 | 193 | Serial.print(F("Peripheral disconnected from central: ")); |
RedBearLab | 0:075ea2812998 | 194 | Serial.println(this->_central.address()); |
RedBearLab | 0:075ea2812998 | 195 | #endif |
RedBearLab | 0:075ea2812998 | 196 | |
RedBearLab | 0:075ea2812998 | 197 | BLEPeripheralEventHandler eventHandler = this->_eventHandlers[BLEDisconnected]; |
RedBearLab | 0:075ea2812998 | 198 | if (eventHandler) { |
RedBearLab | 0:075ea2812998 | 199 | eventHandler(this->_central); |
RedBearLab | 0:075ea2812998 | 200 | } |
RedBearLab | 0:075ea2812998 | 201 | |
RedBearLab | 0:075ea2812998 | 202 | this->_central.clearAddress(); |
RedBearLab | 0:075ea2812998 | 203 | } |
RedBearLab | 0:075ea2812998 | 204 | |
RedBearLab | 0:075ea2812998 | 205 | void BLEPeripheral::nRF8001CharacteristicValueChanged(nRF8001& nRF8001, BLECharacteristic& characteristic, const unsigned char* value, unsigned char valueLength) { |
RedBearLab | 0:075ea2812998 | 206 | characteristic.setValue(this->_central, value, valueLength); |
RedBearLab | 0:075ea2812998 | 207 | } |
RedBearLab | 0:075ea2812998 | 208 | |
RedBearLab | 0:075ea2812998 | 209 | void BLEPeripheral::nRF8001CharacteristicSubscribedChanged(nRF8001& nRF8001, BLECharacteristic& characteristic, bool subscribed) { |
RedBearLab | 0:075ea2812998 | 210 | characteristic.setSubscribed(this->_central, subscribed); |
RedBearLab | 0:075ea2812998 | 211 | } |
RedBearLab | 0:075ea2812998 | 212 | |
RedBearLab | 0:075ea2812998 | 213 | void BLEPeripheral::nRF8001AddressReceived(nRF8001& nRF8001, const unsigned char* address) { |
RedBearLab | 0:075ea2812998 | 214 | #ifdef BLE_PERIPHERAL_DEBUG |
RedBearLab | 0:075ea2812998 | 215 | char addressStr[18]; |
RedBearLab | 0:075ea2812998 | 216 | |
RedBearLab | 0:075ea2812998 | 217 | sprintf(addressStr, "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x", |
RedBearLab | 0:075ea2812998 | 218 | address[5], |
RedBearLab | 0:075ea2812998 | 219 | address[4], |
RedBearLab | 0:075ea2812998 | 220 | address[3], |
RedBearLab | 0:075ea2812998 | 221 | address[2], |
RedBearLab | 0:075ea2812998 | 222 | address[1], |
RedBearLab | 0:075ea2812998 | 223 | address[0]); |
RedBearLab | 0:075ea2812998 | 224 | |
RedBearLab | 0:075ea2812998 | 225 | Serial.print(F("Peripheral address: ")); |
RedBearLab | 0:075ea2812998 | 226 | Serial.println(addressStr); |
RedBearLab | 0:075ea2812998 | 227 | #endif |
RedBearLab | 0:075ea2812998 | 228 | } |
RedBearLab | 0:075ea2812998 | 229 | |
RedBearLab | 0:075ea2812998 | 230 | void BLEPeripheral::nRF8001TemperatureReceived(nRF8001& nRF8001, float temperature) { |
RedBearLab | 0:075ea2812998 | 231 | } |
RedBearLab | 0:075ea2812998 | 232 | |
RedBearLab | 0:075ea2812998 | 233 | void BLEPeripheral::nRF8001BatteryLevelReceived(nRF8001& nRF8001, float batteryLevel) { |
RedBearLab | 0:075ea2812998 | 234 | } |