added debugging
Fork of BLE_nRF8001 by
BLE_nRF8001/BLEPeripheral.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 "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() { |
jn80842 | 2:7805a5595aab | 42 | serial.printf("called to begin\r\n"); |
RedBearLab | 0:075ea2812998 | 43 | unsigned char advertisementData[20]; |
RedBearLab | 0:075ea2812998 | 44 | unsigned char scanData[20]; |
RedBearLab | 0:075ea2812998 | 45 | |
RedBearLab | 0:075ea2812998 | 46 | unsigned char advertisementDataLength = 0; |
RedBearLab | 0:075ea2812998 | 47 | unsigned char scanDataLength = 0; |
RedBearLab | 0:075ea2812998 | 48 | |
RedBearLab | 0:075ea2812998 | 49 | if (this->_advertisedServiceUuid){ |
jn80842 | 2:7805a5595aab | 50 | serial.printf("we have an advertised service uuid\r\n"); |
RedBearLab | 0:075ea2812998 | 51 | BLEUuid advertisedServiceUuid = BLEUuid(this->_advertisedServiceUuid); |
RedBearLab | 0:075ea2812998 | 52 | unsigned char advertisedServiceUuidLength = advertisedServiceUuid.length(); |
RedBearLab | 0:075ea2812998 | 53 | |
RedBearLab | 0:075ea2812998 | 54 | advertisementDataLength = 2 + advertisedServiceUuidLength; |
RedBearLab | 0:075ea2812998 | 55 | |
RedBearLab | 0:075ea2812998 | 56 | advertisementData[0] = (advertisedServiceUuidLength > 2) ? 0x06 : 0x02; |
RedBearLab | 0:075ea2812998 | 57 | advertisementData[1] = advertisedServiceUuidLength; |
RedBearLab | 0:075ea2812998 | 58 | |
RedBearLab | 0:075ea2812998 | 59 | memcpy(&advertisementData[2], advertisedServiceUuid.data(), advertisedServiceUuidLength); |
RedBearLab | 0:075ea2812998 | 60 | } else if (this->_manufacturerData && this->_manufacturerDataLength > 0) { |
RedBearLab | 0:075ea2812998 | 61 | if (this->_manufacturerDataLength > sizeof(advertisementData)) { |
RedBearLab | 0:075ea2812998 | 62 | this->_manufacturerDataLength = sizeof(advertisementData); |
RedBearLab | 0:075ea2812998 | 63 | } |
RedBearLab | 0:075ea2812998 | 64 | |
RedBearLab | 0:075ea2812998 | 65 | advertisementDataLength = 2 + this->_manufacturerDataLength; |
RedBearLab | 0:075ea2812998 | 66 | |
RedBearLab | 0:075ea2812998 | 67 | advertisementData[0] = 0xff; |
RedBearLab | 0:075ea2812998 | 68 | advertisementData[1] = this->_manufacturerDataLength; |
RedBearLab | 0:075ea2812998 | 69 | memcpy(&advertisementData[2], this->_manufacturerData, this->_manufacturerDataLength); |
RedBearLab | 0:075ea2812998 | 70 | } |
RedBearLab | 0:075ea2812998 | 71 | |
RedBearLab | 0:075ea2812998 | 72 | if (this->_localName){ |
jn80842 | 2:7805a5595aab | 73 | serial.printf("we have a local name\r\n"); |
RedBearLab | 0:075ea2812998 | 74 | unsigned char originalLocalNameLength = strlen(this->_localName); |
RedBearLab | 0:075ea2812998 | 75 | unsigned char localNameLength = originalLocalNameLength; |
jn80842 | 2:7805a5595aab | 76 | serial.printf("ok local name variables set up\r\n"); |
RedBearLab | 0:075ea2812998 | 77 | if (localNameLength > sizeof(scanData)) { |
RedBearLab | 0:075ea2812998 | 78 | localNameLength = sizeof(scanData); |
RedBearLab | 0:075ea2812998 | 79 | } |
jn80842 | 2:7805a5595aab | 80 | serial.printf("set scan length\r\n"); |
RedBearLab | 0:075ea2812998 | 81 | scanDataLength = 2 + localNameLength; |
RedBearLab | 0:075ea2812998 | 82 | |
RedBearLab | 0:075ea2812998 | 83 | scanData[0] = (originalLocalNameLength > sizeof(scanData)) ? 0x08 : 0x09; |
RedBearLab | 0:075ea2812998 | 84 | scanData[1] = localNameLength; |
jn80842 | 2:7805a5595aab | 85 | serial.printf("set up scanData arr\r\n"); |
RedBearLab | 0:075ea2812998 | 86 | memcpy(&scanData[2], this->_localName, localNameLength); |
jn80842 | 2:7805a5595aab | 87 | serial.printf("done dealing with local name\r\n"); |
RedBearLab | 0:075ea2812998 | 88 | } |
jn80842 | 2:7805a5595aab | 89 | serial.printf("ok look at %d attributes now\r\n", this->_numAttributes); |
RedBearLab | 0:075ea2812998 | 90 | for (int i = 0; i < this->_numAttributes; i++) { |
jn80842 | 2:7805a5595aab | 91 | //serial.printf("cycled through attribute %d\r\n",i); |
RedBearLab | 0:075ea2812998 | 92 | BLEAttribute* attribute = this->_attributes[i]; |
RedBearLab | 0:075ea2812998 | 93 | if (attribute->type() == BLETypeCharacteristic) { |
jn80842 | 2:7805a5595aab | 94 | // serial.printf("found a characteristic\r\n"); |
RedBearLab | 0:075ea2812998 | 95 | BLECharacteristic* characteristic = (BLECharacteristic*)attribute; |
RedBearLab | 0:075ea2812998 | 96 | |
RedBearLab | 0:075ea2812998 | 97 | characteristic->setValueChangeListener(*this); |
RedBearLab | 0:075ea2812998 | 98 | } |
RedBearLab | 0:075ea2812998 | 99 | } |
jn80842 | 2:7805a5595aab | 100 | serial.printf("now really beginning\r\n"); |
RedBearLab | 0:075ea2812998 | 101 | this->_nRF8001.begin(advertisementData, advertisementDataLength, scanData, scanDataLength, this->_attributes, this->_numAttributes); |
jn80842 | 2:7805a5595aab | 102 | serial.printf("now request address\r\n"); |
RedBearLab | 0:075ea2812998 | 103 | this->_nRF8001.requestAddress(); |
jn80842 | 2:7805a5595aab | 104 | serial.printf("we are done beginning!\r\n"); |
RedBearLab | 0:075ea2812998 | 105 | } |
RedBearLab | 0:075ea2812998 | 106 | |
RedBearLab | 0:075ea2812998 | 107 | void BLEPeripheral::poll() { |
RedBearLab | 0:075ea2812998 | 108 | this->_nRF8001.poll(); |
RedBearLab | 0:075ea2812998 | 109 | } |
RedBearLab | 0:075ea2812998 | 110 | |
RedBearLab | 0:075ea2812998 | 111 | void BLEPeripheral::setAdvertisedServiceUuid(const char* advertisedServiceUuid) { |
RedBearLab | 0:075ea2812998 | 112 | this->_advertisedServiceUuid = advertisedServiceUuid; |
RedBearLab | 0:075ea2812998 | 113 | } |
RedBearLab | 0:075ea2812998 | 114 | |
RedBearLab | 0:075ea2812998 | 115 | void BLEPeripheral::setManufacturerData(const unsigned char manufacturerData[], unsigned char manufacturerDataLength) { |
RedBearLab | 0:075ea2812998 | 116 | this->_manufacturerData = manufacturerData; |
RedBearLab | 0:075ea2812998 | 117 | this->_manufacturerDataLength = manufacturerDataLength; |
RedBearLab | 0:075ea2812998 | 118 | } |
RedBearLab | 0:075ea2812998 | 119 | |
RedBearLab | 0:075ea2812998 | 120 | void BLEPeripheral::setLocalName(const char* localName) { |
RedBearLab | 0:075ea2812998 | 121 | this->_localName = localName; |
RedBearLab | 0:075ea2812998 | 122 | } |
RedBearLab | 0:075ea2812998 | 123 | |
RedBearLab | 0:075ea2812998 | 124 | void BLEPeripheral::setDeviceName(const char* deviceName) { |
RedBearLab | 0:075ea2812998 | 125 | this->_deviceNameCharacteristic.setValue(deviceName); |
RedBearLab | 0:075ea2812998 | 126 | } |
RedBearLab | 0:075ea2812998 | 127 | |
RedBearLab | 0:075ea2812998 | 128 | void BLEPeripheral::setAppearance(unsigned short appearance) { |
RedBearLab | 0:075ea2812998 | 129 | this->_appearanceCharacteristic.setValue((unsigned char *)&appearance, sizeof(appearance)); |
RedBearLab | 0:075ea2812998 | 130 | } |
RedBearLab | 0:075ea2812998 | 131 | |
RedBearLab | 0:075ea2812998 | 132 | void BLEPeripheral::addAttribute(BLEAttribute& attribute) { |
RedBearLab | 0:075ea2812998 | 133 | if (this->_attributes == NULL) { |
RedBearLab | 0:075ea2812998 | 134 | this->_attributes = (BLEAttribute**)malloc(BLEAttribute::numAttributes() * sizeof(BLEAttribute*)); |
RedBearLab | 0:075ea2812998 | 135 | |
RedBearLab | 0:075ea2812998 | 136 | this->_attributes[0] = &this->_genericAccessService; |
RedBearLab | 0:075ea2812998 | 137 | this->_attributes[1] = &this->_deviceNameCharacteristic; |
RedBearLab | 0:075ea2812998 | 138 | this->_attributes[2] = &this->_appearanceCharacteristic; |
RedBearLab | 0:075ea2812998 | 139 | |
RedBearLab | 0:075ea2812998 | 140 | this->_attributes[3] = &this->_genericAttributeService; |
RedBearLab | 0:075ea2812998 | 141 | this->_attributes[4] = &this->_servicesChangedCharacteristic; |
RedBearLab | 0:075ea2812998 | 142 | |
RedBearLab | 0:075ea2812998 | 143 | this->_numAttributes = 5; |
RedBearLab | 0:075ea2812998 | 144 | } |
RedBearLab | 0:075ea2812998 | 145 | |
RedBearLab | 0:075ea2812998 | 146 | this->_attributes[this->_numAttributes] = &attribute; |
RedBearLab | 0:075ea2812998 | 147 | this->_numAttributes++; |
RedBearLab | 0:075ea2812998 | 148 | } |
RedBearLab | 0:075ea2812998 | 149 | |
RedBearLab | 0:075ea2812998 | 150 | void BLEPeripheral::disconnect() { |
RedBearLab | 0:075ea2812998 | 151 | this->_nRF8001.disconnect(); |
RedBearLab | 0:075ea2812998 | 152 | } |
RedBearLab | 0:075ea2812998 | 153 | |
RedBearLab | 0:075ea2812998 | 154 | BLECentral BLEPeripheral::central() { |
RedBearLab | 0:075ea2812998 | 155 | this->poll(); |
RedBearLab | 0:075ea2812998 | 156 | |
RedBearLab | 0:075ea2812998 | 157 | return this->_central; |
RedBearLab | 0:075ea2812998 | 158 | } |
RedBearLab | 0:075ea2812998 | 159 | |
RedBearLab | 0:075ea2812998 | 160 | bool BLEPeripheral::connected() { |
RedBearLab | 0:075ea2812998 | 161 | this->poll(); |
RedBearLab | 0:075ea2812998 | 162 | |
RedBearLab | 0:075ea2812998 | 163 | return this->_central; |
RedBearLab | 0:075ea2812998 | 164 | } |
RedBearLab | 0:075ea2812998 | 165 | |
RedBearLab | 0:075ea2812998 | 166 | void BLEPeripheral::setEventHandler(BLEPeripheralEvent event, BLEPeripheralEventHandler eventHandler) { |
RedBearLab | 0:075ea2812998 | 167 | if (event < sizeof(this->_eventHandlers)) { |
RedBearLab | 0:075ea2812998 | 168 | this->_eventHandlers[event] = eventHandler; |
RedBearLab | 0:075ea2812998 | 169 | } |
RedBearLab | 0:075ea2812998 | 170 | } |
RedBearLab | 0:075ea2812998 | 171 | |
RedBearLab | 0:075ea2812998 | 172 | bool BLEPeripheral::characteristicValueChanged(BLECharacteristic& characteristic) { |
RedBearLab | 0:075ea2812998 | 173 | return this->_nRF8001.updateCharacteristicValue(characteristic); |
RedBearLab | 0:075ea2812998 | 174 | } |
RedBearLab | 0:075ea2812998 | 175 | |
RedBearLab | 0:075ea2812998 | 176 | bool BLEPeripheral::canNotifyCharacteristic(BLECharacteristic& characteristic) { |
RedBearLab | 0:075ea2812998 | 177 | return this->_nRF8001.canNotifyCharacteristic(characteristic); |
RedBearLab | 0:075ea2812998 | 178 | } |
RedBearLab | 0:075ea2812998 | 179 | |
RedBearLab | 0:075ea2812998 | 180 | bool BLEPeripheral::canIndicateCharacteristic(BLECharacteristic& characteristic) { |
RedBearLab | 0:075ea2812998 | 181 | return this->_nRF8001.canIndicateCharacteristic(characteristic); |
RedBearLab | 0:075ea2812998 | 182 | } |
RedBearLab | 0:075ea2812998 | 183 | |
RedBearLab | 0:075ea2812998 | 184 | void BLEPeripheral::nRF8001Connected(nRF8001& nRF8001, const unsigned char* address) { |
RedBearLab | 0:075ea2812998 | 185 | this->_central.setAddress(address); |
RedBearLab | 0:075ea2812998 | 186 | |
RedBearLab | 0:075ea2812998 | 187 | #ifdef BLE_PERIPHERAL_DEBUG |
RedBearLab | 0:075ea2812998 | 188 | Serial.print(F("Peripheral connected to central: ")); |
RedBearLab | 0:075ea2812998 | 189 | Serial.println(this->_central.address()); |
RedBearLab | 0:075ea2812998 | 190 | #endif |
RedBearLab | 0:075ea2812998 | 191 | |
RedBearLab | 0:075ea2812998 | 192 | BLEPeripheralEventHandler eventHandler = this->_eventHandlers[BLEConnected]; |
RedBearLab | 0:075ea2812998 | 193 | if (eventHandler) { |
RedBearLab | 0:075ea2812998 | 194 | eventHandler(this->_central); |
RedBearLab | 0:075ea2812998 | 195 | } |
RedBearLab | 0:075ea2812998 | 196 | } |
RedBearLab | 0:075ea2812998 | 197 | |
RedBearLab | 0:075ea2812998 | 198 | void BLEPeripheral::nRF8001Disconnected(nRF8001& nRF8001) { |
RedBearLab | 0:075ea2812998 | 199 | #ifdef BLE_PERIPHERAL_DEBUG |
RedBearLab | 0:075ea2812998 | 200 | Serial.print(F("Peripheral disconnected from central: ")); |
RedBearLab | 0:075ea2812998 | 201 | Serial.println(this->_central.address()); |
RedBearLab | 0:075ea2812998 | 202 | #endif |
RedBearLab | 0:075ea2812998 | 203 | |
RedBearLab | 0:075ea2812998 | 204 | BLEPeripheralEventHandler eventHandler = this->_eventHandlers[BLEDisconnected]; |
RedBearLab | 0:075ea2812998 | 205 | if (eventHandler) { |
RedBearLab | 0:075ea2812998 | 206 | eventHandler(this->_central); |
RedBearLab | 0:075ea2812998 | 207 | } |
RedBearLab | 0:075ea2812998 | 208 | |
RedBearLab | 0:075ea2812998 | 209 | this->_central.clearAddress(); |
RedBearLab | 0:075ea2812998 | 210 | } |
RedBearLab | 0:075ea2812998 | 211 | |
RedBearLab | 0:075ea2812998 | 212 | void BLEPeripheral::nRF8001CharacteristicValueChanged(nRF8001& nRF8001, BLECharacteristic& characteristic, const unsigned char* value, unsigned char valueLength) { |
RedBearLab | 0:075ea2812998 | 213 | characteristic.setValue(this->_central, value, valueLength); |
RedBearLab | 0:075ea2812998 | 214 | } |
RedBearLab | 0:075ea2812998 | 215 | |
RedBearLab | 0:075ea2812998 | 216 | void BLEPeripheral::nRF8001CharacteristicSubscribedChanged(nRF8001& nRF8001, BLECharacteristic& characteristic, bool subscribed) { |
RedBearLab | 0:075ea2812998 | 217 | characteristic.setSubscribed(this->_central, subscribed); |
RedBearLab | 0:075ea2812998 | 218 | } |
RedBearLab | 0:075ea2812998 | 219 | |
RedBearLab | 0:075ea2812998 | 220 | void BLEPeripheral::nRF8001AddressReceived(nRF8001& nRF8001, const unsigned char* address) { |
RedBearLab | 0:075ea2812998 | 221 | #ifdef BLE_PERIPHERAL_DEBUG |
RedBearLab | 0:075ea2812998 | 222 | char addressStr[18]; |
RedBearLab | 0:075ea2812998 | 223 | |
RedBearLab | 0:075ea2812998 | 224 | sprintf(addressStr, "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x", |
RedBearLab | 0:075ea2812998 | 225 | address[5], |
RedBearLab | 0:075ea2812998 | 226 | address[4], |
RedBearLab | 0:075ea2812998 | 227 | address[3], |
RedBearLab | 0:075ea2812998 | 228 | address[2], |
RedBearLab | 0:075ea2812998 | 229 | address[1], |
RedBearLab | 0:075ea2812998 | 230 | address[0]); |
RedBearLab | 0:075ea2812998 | 231 | |
RedBearLab | 0:075ea2812998 | 232 | Serial.print(F("Peripheral address: ")); |
RedBearLab | 0:075ea2812998 | 233 | Serial.println(addressStr); |
RedBearLab | 0:075ea2812998 | 234 | #endif |
RedBearLab | 0:075ea2812998 | 235 | } |
RedBearLab | 0:075ea2812998 | 236 | |
RedBearLab | 0:075ea2812998 | 237 | void BLEPeripheral::nRF8001TemperatureReceived(nRF8001& nRF8001, float temperature) { |
RedBearLab | 0:075ea2812998 | 238 | } |
RedBearLab | 0:075ea2812998 | 239 | |
RedBearLab | 0:075ea2812998 | 240 | void BLEPeripheral::nRF8001BatteryLevelReceived(nRF8001& nRF8001, float batteryLevel) { |
RedBearLab | 0:075ea2812998 | 241 | } |