Skip to content

Commit

Permalink
Fix #79 by fixing iBeacon format parsing (#81)
Browse files Browse the repository at this point in the history
* TxPower should be signed value

* Fix iBeacon parse function

 - reverse UUID info in iBeacon packets. We did this is BLEPeripheral but not in the parse function.
 - TxPower should be signed value, so change the data type in the interface.
 - fix endianess of Major and Minor values in the parse function.

* Reverse UUID in iBeacon packet
  • Loading branch information
pablosun committed Jan 9, 2018
1 parent 9f190fc commit b2ef719
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ void printDeviceInfo(int i) {
if (LBLECentral.isIBeacon(i)) {
LBLEUuid uuid;
uint16_t major = 0, minor = 0;
uint8_t txPower = 0;
int8_t txPower = 0;
LBLECentral.getIBeaconInfo(i, uuid, major, minor, txPower);

Serial.print(" ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ LBLEUuid LBLEAdvertisements::getServiceUuid() const
return LBLEUuid(uuid_data);
}

bool LBLEAdvertisements::getIBeaconInfo(LBLEUuid& uuid, uint16_t& major, uint16_t& minor, uint8_t& txPower) const
bool LBLEAdvertisements::getIBeaconInfo(LBLEUuid& uuid, uint16_t& major, uint16_t& minor, int8_t& txPower) const
{
// for the iBeacon format, you may refer to
// https://developer.mbed.org/blog/entry/BLE-Beacons-URIBeacon-AltBeacons-iBeacon/
Expand Down Expand Up @@ -188,15 +188,24 @@ bool LBLEAdvertisements::getIBeaconInfo(LBLEUuid& uuid, uint16_t& major, uint16_
if(0x15 != beaconLength)
break;

// note that iBeacon UUID are reversed w.r.t. our BT system
bt_uuid_t tmpUuid;
memcpy(tmpUuid.uuid, iBeaconBuffer, sizeof(tmpUuid.uuid));
for(int i = 0; i < 16; ++i)
{
tmpUuid.uuid[i] = iBeaconBuffer[15-i];
}
uuid = tmpUuid;
iBeaconBuffer += 16;


major = *(unsigned short*)(iBeaconBuffer);
// note that the endian for major and minor are different.
major = *((uint16_t*)iBeaconBuffer);
major = (major >> 8) | ((major & 0xFF) << 8);
iBeaconBuffer += 2;

minor = *(unsigned short*)(iBeaconBuffer);

minor = *((uint16_t*)iBeaconBuffer);
minor = (minor >> 8) | ((minor & 0xFF) << 8);
iBeaconBuffer += 2;

txPower = *(int8_t*)iBeaconBuffer;
Expand Down Expand Up @@ -365,11 +374,11 @@ bool LBLECentralClass::isIBeacon(int index) const

LBLEUuid uuid;
uint16_t major, minor;
uint8_t txPower;
int8_t txPower;
return parser.getIBeaconInfo(uuid, major, minor, txPower);
}

bool LBLECentralClass::getIBeaconInfo(int index, LBLEUuid& uuid, uint16_t& major, uint16_t& minor, uint8_t& txPower) const
bool LBLECentralClass::getIBeaconInfo(int index, LBLEUuid& uuid, uint16_t& major, uint16_t& minor, int8_t& txPower) const
{
LBLEAdvertisements parser(m_peripherals_found[index]);
return parser.getIBeaconInfo(uuid, major, minor, txPower);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,9 @@ class LBLECentralClass : public LBLEEventObserver
/// \param uuid Output parameter of the iBeacon info - this UUID is defined by the iBeacon device.
/// \param major Output parameter, the major number in the iBeacon info.
/// \param minor Output parameter, the minor number in the iBeacon info.
/// \param minor Output parameter, the TX Power value in the iBeacon info.
/// \param txPower Output parameter, the signed TX Power value in the iBeacon info.
/// \returns true if the device has iBeacon info. false if the device is not an iBeacon device.
bool getIBeaconInfo(int index, LBLEUuid& uuid, uint16_t& major, uint16_t& minor, uint8_t& txPower)const;
bool getIBeaconInfo(int index, LBLEUuid& uuid, uint16_t& major, uint16_t& minor, int8_t& txPower)const;

public:
// Event handlers and required data structure
Expand Down Expand Up @@ -203,7 +203,7 @@ class LBLEAdvertisements
uint8_t getAdvertisementFlag()const;

// returns false if this advertisment is not of iBeacon format.
bool getIBeaconInfo(LBLEUuid& uuid, uint16_t& major, uint16_t& minor, uint8_t& txPower) const;
bool getIBeaconInfo(LBLEUuid& uuid, uint16_t& major, uint16_t& minor, int8_t& txPower) const;

// Copy the "original" advertisement packet data
// bufLen should be the size of dstBuf.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,8 @@ void LBLEAdvertisementData::configAsIBeacon(const LBLEUuid& uuid,
}

// 2 byte major number & 2 byte minor, note that the endian is different.
(*(uint16_t*)(item.adData + 20)) = (major >> 8) | (major << 8);
(*(uint16_t*)(item.adData + 22)) = (minor >> 8) | (minor << 8);
(*(uint16_t*)(item.adData + 20)) = (major >> 8) | ((major & 0xFF) << 8);
(*(uint16_t*)(item.adData + 22)) = (minor >> 8) | ((minor & 0xFF) << 8);

// 1 byte TxPower (signed)
item.adData[24] = (uint8_t)txPower;
Expand Down

0 comments on commit b2ef719

Please sign in to comment.