added debugging
Fork of BLE_nRF8001 by
Embed:
(wiki syntax)
Show/hide line numbers
BLEPeripheral.cpp
00001 #include "BLEUuid.h" 00002 #include "BLEPeripheral.h" 00003 00004 //#define BLE_PERIPHERAL_DEBUG 00005 00006 #define DEFAULT_DEVICE_NAME "Arduino" 00007 #define DEFAULT_APPEARANCE 0x0000 00008 00009 BLEPeripheral::BLEPeripheral(DigitalInOut *req, DigitalInOut *rdy, DigitalInOut *rst) : 00010 _nRF8001(req, rdy, rst), 00011 00012 _localName(NULL), 00013 _manufacturerData(NULL), 00014 _manufacturerDataLength(0), 00015 00016 _attributes(NULL), 00017 _numAttributes(0), 00018 00019 _genericAccessService("1800"), 00020 _deviceNameCharacteristic("2a00", BLERead, 19), 00021 _appearanceCharacteristic("2a01", BLERead, 2), 00022 _genericAttributeService("1801"), 00023 _servicesChangedCharacteristic("2a05", BLEIndicate, 4), 00024 00025 _central(this) 00026 { 00027 memset(this->_eventHandlers, 0x00, sizeof(this->_eventHandlers)); 00028 00029 this->setDeviceName(DEFAULT_DEVICE_NAME); 00030 this->setAppearance(DEFAULT_APPEARANCE); 00031 00032 this->_nRF8001.setEventListener(this); 00033 } 00034 00035 BLEPeripheral::~BLEPeripheral() { 00036 if (this->_attributes) { 00037 free(this->_attributes); 00038 } 00039 } 00040 00041 void BLEPeripheral::begin() { 00042 serial.printf("called to begin\r\n"); 00043 unsigned char advertisementData[20]; 00044 unsigned char scanData[20]; 00045 00046 unsigned char advertisementDataLength = 0; 00047 unsigned char scanDataLength = 0; 00048 00049 if (this->_advertisedServiceUuid){ 00050 serial.printf("we have an advertised service uuid\r\n"); 00051 BLEUuid advertisedServiceUuid = BLEUuid(this->_advertisedServiceUuid); 00052 unsigned char advertisedServiceUuidLength = advertisedServiceUuid.length(); 00053 00054 advertisementDataLength = 2 + advertisedServiceUuidLength; 00055 00056 advertisementData[0] = (advertisedServiceUuidLength > 2) ? 0x06 : 0x02; 00057 advertisementData[1] = advertisedServiceUuidLength; 00058 00059 memcpy(&advertisementData[2], advertisedServiceUuid.data(), advertisedServiceUuidLength); 00060 } else if (this->_manufacturerData && this->_manufacturerDataLength > 0) { 00061 if (this->_manufacturerDataLength > sizeof(advertisementData)) { 00062 this->_manufacturerDataLength = sizeof(advertisementData); 00063 } 00064 00065 advertisementDataLength = 2 + this->_manufacturerDataLength; 00066 00067 advertisementData[0] = 0xff; 00068 advertisementData[1] = this->_manufacturerDataLength; 00069 memcpy(&advertisementData[2], this->_manufacturerData, this->_manufacturerDataLength); 00070 } 00071 00072 if (this->_localName){ 00073 serial.printf("we have a local name\r\n"); 00074 unsigned char originalLocalNameLength = strlen(this->_localName); 00075 unsigned char localNameLength = originalLocalNameLength; 00076 serial.printf("ok local name variables set up\r\n"); 00077 if (localNameLength > sizeof(scanData)) { 00078 localNameLength = sizeof(scanData); 00079 } 00080 serial.printf("set scan length\r\n"); 00081 scanDataLength = 2 + localNameLength; 00082 00083 scanData[0] = (originalLocalNameLength > sizeof(scanData)) ? 0x08 : 0x09; 00084 scanData[1] = localNameLength; 00085 serial.printf("set up scanData arr\r\n"); 00086 memcpy(&scanData[2], this->_localName, localNameLength); 00087 serial.printf("done dealing with local name\r\n"); 00088 } 00089 serial.printf("ok look at %d attributes now\r\n", this->_numAttributes); 00090 for (int i = 0; i < this->_numAttributes; i++) { 00091 //serial.printf("cycled through attribute %d\r\n",i); 00092 BLEAttribute* attribute = this->_attributes[i]; 00093 if (attribute->type() == BLETypeCharacteristic) { 00094 // serial.printf("found a characteristic\r\n"); 00095 BLECharacteristic* characteristic = (BLECharacteristic*)attribute; 00096 00097 characteristic->setValueChangeListener(*this); 00098 } 00099 } 00100 serial.printf("now really beginning\r\n"); 00101 this->_nRF8001.begin(advertisementData, advertisementDataLength, scanData, scanDataLength, this->_attributes, this->_numAttributes); 00102 serial.printf("now request address\r\n"); 00103 this->_nRF8001.requestAddress(); 00104 serial.printf("we are done beginning!\r\n"); 00105 } 00106 00107 void BLEPeripheral::poll() { 00108 this->_nRF8001.poll(); 00109 } 00110 00111 void BLEPeripheral::setAdvertisedServiceUuid(const char* advertisedServiceUuid) { 00112 this->_advertisedServiceUuid = advertisedServiceUuid; 00113 } 00114 00115 void BLEPeripheral::setManufacturerData(const unsigned char manufacturerData[], unsigned char manufacturerDataLength) { 00116 this->_manufacturerData = manufacturerData; 00117 this->_manufacturerDataLength = manufacturerDataLength; 00118 } 00119 00120 void BLEPeripheral::setLocalName(const char* localName) { 00121 this->_localName = localName; 00122 } 00123 00124 void BLEPeripheral::setDeviceName(const char* deviceName) { 00125 this->_deviceNameCharacteristic.setValue(deviceName); 00126 } 00127 00128 void BLEPeripheral::setAppearance(unsigned short appearance) { 00129 this->_appearanceCharacteristic.setValue((unsigned char *)&appearance, sizeof(appearance)); 00130 } 00131 00132 void BLEPeripheral::addAttribute(BLEAttribute& attribute) { 00133 if (this->_attributes == NULL) { 00134 this->_attributes = (BLEAttribute**)malloc(BLEAttribute::numAttributes() * sizeof(BLEAttribute*)); 00135 00136 this->_attributes[0] = &this->_genericAccessService; 00137 this->_attributes[1] = &this->_deviceNameCharacteristic; 00138 this->_attributes[2] = &this->_appearanceCharacteristic; 00139 00140 this->_attributes[3] = &this->_genericAttributeService; 00141 this->_attributes[4] = &this->_servicesChangedCharacteristic; 00142 00143 this->_numAttributes = 5; 00144 } 00145 00146 this->_attributes[this->_numAttributes] = &attribute; 00147 this->_numAttributes++; 00148 } 00149 00150 void BLEPeripheral::disconnect() { 00151 this->_nRF8001.disconnect(); 00152 } 00153 00154 BLECentral BLEPeripheral::central() { 00155 this->poll(); 00156 00157 return this->_central; 00158 } 00159 00160 bool BLEPeripheral::connected() { 00161 this->poll(); 00162 00163 return this->_central; 00164 } 00165 00166 void BLEPeripheral::setEventHandler(BLEPeripheralEvent event, BLEPeripheralEventHandler eventHandler) { 00167 if (event < sizeof(this->_eventHandlers)) { 00168 this->_eventHandlers[event] = eventHandler; 00169 } 00170 } 00171 00172 bool BLEPeripheral::characteristicValueChanged(BLECharacteristic& characteristic) { 00173 return this->_nRF8001.updateCharacteristicValue(characteristic); 00174 } 00175 00176 bool BLEPeripheral::canNotifyCharacteristic(BLECharacteristic& characteristic) { 00177 return this->_nRF8001.canNotifyCharacteristic(characteristic); 00178 } 00179 00180 bool BLEPeripheral::canIndicateCharacteristic(BLECharacteristic& characteristic) { 00181 return this->_nRF8001.canIndicateCharacteristic(characteristic); 00182 } 00183 00184 void BLEPeripheral::nRF8001Connected(nRF8001& nRF8001, const unsigned char* address) { 00185 this->_central.setAddress(address); 00186 00187 #ifdef BLE_PERIPHERAL_DEBUG 00188 Serial.print(F("Peripheral connected to central: ")); 00189 Serial.println(this->_central.address()); 00190 #endif 00191 00192 BLEPeripheralEventHandler eventHandler = this->_eventHandlers[BLEConnected]; 00193 if (eventHandler) { 00194 eventHandler(this->_central); 00195 } 00196 } 00197 00198 void BLEPeripheral::nRF8001Disconnected(nRF8001& nRF8001) { 00199 #ifdef BLE_PERIPHERAL_DEBUG 00200 Serial.print(F("Peripheral disconnected from central: ")); 00201 Serial.println(this->_central.address()); 00202 #endif 00203 00204 BLEPeripheralEventHandler eventHandler = this->_eventHandlers[BLEDisconnected]; 00205 if (eventHandler) { 00206 eventHandler(this->_central); 00207 } 00208 00209 this->_central.clearAddress(); 00210 } 00211 00212 void BLEPeripheral::nRF8001CharacteristicValueChanged(nRF8001& nRF8001, BLECharacteristic& characteristic, const unsigned char* value, unsigned char valueLength) { 00213 characteristic.setValue(this->_central, value, valueLength); 00214 } 00215 00216 void BLEPeripheral::nRF8001CharacteristicSubscribedChanged(nRF8001& nRF8001, BLECharacteristic& characteristic, bool subscribed) { 00217 characteristic.setSubscribed(this->_central, subscribed); 00218 } 00219 00220 void BLEPeripheral::nRF8001AddressReceived(nRF8001& nRF8001, const unsigned char* address) { 00221 #ifdef BLE_PERIPHERAL_DEBUG 00222 char addressStr[18]; 00223 00224 sprintf(addressStr, "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x", 00225 address[5], 00226 address[4], 00227 address[3], 00228 address[2], 00229 address[1], 00230 address[0]); 00231 00232 Serial.print(F("Peripheral address: ")); 00233 Serial.println(addressStr); 00234 #endif 00235 } 00236 00237 void BLEPeripheral::nRF8001TemperatureReceived(nRF8001& nRF8001, float temperature) { 00238 } 00239 00240 void BLEPeripheral::nRF8001BatteryLevelReceived(nRF8001& nRF8001, float batteryLevel) { 00241 }
Generated on Tue Jul 12 2022 15:15:46 by 1.7.2