RedBearLab / BLE_nRF8001

Dependents:   nRF8001_SimpleChat nRF8001_SimpleControls mbed_BLE2 mbed_BLEtry2 ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers BLEPeripheral.cpp Source File

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   unsigned char advertisementData[20];
00043   unsigned char scanData[20];
00044 
00045   unsigned char advertisementDataLength = 0;
00046   unsigned char scanDataLength = 0;
00047 
00048   if (this->_advertisedServiceUuid){
00049     BLEUuid advertisedServiceUuid = BLEUuid(this->_advertisedServiceUuid);
00050     unsigned char advertisedServiceUuidLength = advertisedServiceUuid.length();
00051 
00052     advertisementDataLength = 2 + advertisedServiceUuidLength;
00053 
00054     advertisementData[0] = (advertisedServiceUuidLength > 2) ? 0x06 : 0x02;
00055     advertisementData[1] = advertisedServiceUuidLength;
00056 
00057     memcpy(&advertisementData[2], advertisedServiceUuid.data(), advertisedServiceUuidLength);
00058   } else if (this->_manufacturerData && this->_manufacturerDataLength > 0) {
00059     if (this->_manufacturerDataLength > sizeof(advertisementData)) {
00060       this->_manufacturerDataLength = sizeof(advertisementData);
00061     }
00062 
00063     advertisementDataLength = 2 + this->_manufacturerDataLength;
00064 
00065     advertisementData[0] = 0xff;
00066     advertisementData[1] = this->_manufacturerDataLength;
00067     memcpy(&advertisementData[2], this->_manufacturerData, this->_manufacturerDataLength);
00068   }
00069 
00070   if (this->_localName){
00071     unsigned char originalLocalNameLength = strlen(this->_localName);
00072     unsigned char localNameLength = originalLocalNameLength;
00073 
00074     if (localNameLength > sizeof(scanData)) {
00075       localNameLength = sizeof(scanData);
00076     }
00077 
00078     scanDataLength = 2 + localNameLength;
00079 
00080     scanData[0] = (originalLocalNameLength > sizeof(scanData)) ? 0x08 : 0x09;
00081     scanData[1] = localNameLength;
00082 
00083     memcpy(&scanData[2], this->_localName, localNameLength);
00084   }
00085 
00086   for (int i = 0; i < this->_numAttributes; i++) {
00087     BLEAttribute* attribute = this->_attributes[i];
00088     if (attribute->type() == BLETypeCharacteristic) {
00089       BLECharacteristic* characteristic = (BLECharacteristic*)attribute;
00090 
00091       characteristic->setValueChangeListener(*this);
00092     }
00093   }
00094 
00095   this->_nRF8001.begin(advertisementData, advertisementDataLength, scanData, scanDataLength, this->_attributes, this->_numAttributes);
00096 
00097   this->_nRF8001.requestAddress();
00098 }
00099 
00100 void BLEPeripheral::poll() {
00101   this->_nRF8001.poll();
00102 }
00103 
00104 void BLEPeripheral::setAdvertisedServiceUuid(const char* advertisedServiceUuid) {
00105   this->_advertisedServiceUuid = advertisedServiceUuid;
00106 }
00107 
00108 void BLEPeripheral::setManufacturerData(const unsigned char manufacturerData[], unsigned char manufacturerDataLength) {
00109   this->_manufacturerData = manufacturerData;
00110   this->_manufacturerDataLength = manufacturerDataLength;
00111 }
00112 
00113 void BLEPeripheral::setLocalName(const char* localName) {
00114   this->_localName = localName;
00115 }
00116 
00117 void BLEPeripheral::setDeviceName(const char* deviceName) {
00118   this->_deviceNameCharacteristic.setValue(deviceName);
00119 }
00120 
00121 void BLEPeripheral::setAppearance(unsigned short appearance) {
00122   this->_appearanceCharacteristic.setValue((unsigned char *)&appearance, sizeof(appearance));
00123 }
00124 
00125 void BLEPeripheral::addAttribute(BLEAttribute& attribute) {
00126   if (this->_attributes == NULL) {
00127     this->_attributes = (BLEAttribute**)malloc(BLEAttribute::numAttributes() * sizeof(BLEAttribute*));
00128 
00129     this->_attributes[0] = &this->_genericAccessService;
00130     this->_attributes[1] = &this->_deviceNameCharacteristic;
00131     this->_attributes[2] = &this->_appearanceCharacteristic;
00132 
00133     this->_attributes[3] = &this->_genericAttributeService;
00134     this->_attributes[4] = &this->_servicesChangedCharacteristic;
00135 
00136     this->_numAttributes = 5;
00137   }
00138 
00139   this->_attributes[this->_numAttributes] = &attribute;
00140   this->_numAttributes++;
00141 }
00142 
00143 void BLEPeripheral::disconnect() {
00144   this->_nRF8001.disconnect();
00145 }
00146 
00147 BLECentral BLEPeripheral::central() {
00148   this->poll();
00149 
00150   return this->_central;
00151 }
00152 
00153 bool BLEPeripheral::connected() {
00154   this->poll();
00155 
00156   return this->_central;
00157 }
00158 
00159 void BLEPeripheral::setEventHandler(BLEPeripheralEvent event, BLEPeripheralEventHandler eventHandler) {
00160   if (event < sizeof(this->_eventHandlers)) {
00161     this->_eventHandlers[event] = eventHandler;
00162   }
00163 }
00164 
00165 bool BLEPeripheral::characteristicValueChanged(BLECharacteristic& characteristic) {
00166   return this->_nRF8001.updateCharacteristicValue(characteristic);
00167 }
00168 
00169 bool BLEPeripheral::canNotifyCharacteristic(BLECharacteristic& characteristic) {
00170   return this->_nRF8001.canNotifyCharacteristic(characteristic);
00171 }
00172 
00173 bool BLEPeripheral::canIndicateCharacteristic(BLECharacteristic& characteristic) {
00174   return this->_nRF8001.canIndicateCharacteristic(characteristic);
00175 }
00176 
00177 void BLEPeripheral::nRF8001Connected(nRF8001& nRF8001, const unsigned char* address) {
00178   this->_central.setAddress(address);
00179 
00180 #ifdef BLE_PERIPHERAL_DEBUG
00181   Serial.print(F("Peripheral connected to central: "));
00182   Serial.println(this->_central.address());
00183 #endif
00184 
00185   BLEPeripheralEventHandler eventHandler = this->_eventHandlers[BLEConnected];
00186   if (eventHandler) {
00187     eventHandler(this->_central);
00188   }
00189 }
00190 
00191 void BLEPeripheral::nRF8001Disconnected(nRF8001& nRF8001) {
00192 #ifdef BLE_PERIPHERAL_DEBUG
00193   Serial.print(F("Peripheral disconnected from central: "));
00194   Serial.println(this->_central.address());
00195 #endif
00196 
00197   BLEPeripheralEventHandler eventHandler = this->_eventHandlers[BLEDisconnected];
00198   if (eventHandler) {
00199     eventHandler(this->_central);
00200   }
00201 
00202   this->_central.clearAddress();
00203 }
00204 
00205 void BLEPeripheral::nRF8001CharacteristicValueChanged(nRF8001& nRF8001, BLECharacteristic& characteristic, const unsigned char* value, unsigned char valueLength) {
00206   characteristic.setValue(this->_central, value, valueLength);
00207 }
00208 
00209 void BLEPeripheral::nRF8001CharacteristicSubscribedChanged(nRF8001& nRF8001, BLECharacteristic& characteristic, bool subscribed) {
00210   characteristic.setSubscribed(this->_central, subscribed);
00211 }
00212 
00213 void BLEPeripheral::nRF8001AddressReceived(nRF8001& nRF8001, const unsigned char* address) {
00214 #ifdef BLE_PERIPHERAL_DEBUG
00215   char addressStr[18];
00216 
00217   sprintf(addressStr, "%.2x:%.2x:%.2x:%.2x:%.2x:%.2x",
00218     address[5],
00219     address[4],
00220     address[3],
00221     address[2],
00222     address[1],
00223     address[0]);
00224 
00225   Serial.print(F("Peripheral address: "));
00226   Serial.println(addressStr);
00227 #endif
00228 }
00229 
00230 void BLEPeripheral::nRF8001TemperatureReceived(nRF8001& nRF8001, float temperature) {
00231 }
00232 
00233 void BLEPeripheral::nRF8001BatteryLevelReceived(nRF8001& nRF8001, float batteryLevel) {
00234 }