2020-03-30 01:44:20 +02:00
|
|
|
/*
|
|
|
|
* NimBLEBeacon2.cpp
|
|
|
|
*
|
|
|
|
* Created: on March 15 2020
|
|
|
|
* Author H2zero
|
2020-05-14 06:03:56 +02:00
|
|
|
*
|
2020-03-30 01:44:20 +02:00
|
|
|
* Originally:
|
|
|
|
*
|
|
|
|
* BLEBeacon.cpp
|
|
|
|
*
|
|
|
|
* Created on: Jan 4, 2018
|
|
|
|
* Author: kolban
|
|
|
|
*/
|
2021-09-07 05:14:43 +02:00
|
|
|
#include "nimconfig.h"
|
2020-03-30 01:44:20 +02:00
|
|
|
#if defined(CONFIG_BT_ENABLED)
|
2020-05-14 06:03:56 +02:00
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
#include <string.h>
|
2020-07-20 02:25:10 +02:00
|
|
|
#include <algorithm>
|
2020-03-30 01:44:20 +02:00
|
|
|
#include "NimBLEBeacon.h"
|
|
|
|
#include "NimBLELog.h"
|
|
|
|
|
|
|
|
#define ENDIAN_CHANGE_U16(x) ((((x)&0xFF00)>>8) + (((x)&0xFF)<<8))
|
|
|
|
|
|
|
|
static const char* LOG_TAG = "NimBLEBeacon";
|
|
|
|
|
2020-07-09 03:27:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Construct a default beacon object.
|
|
|
|
*/
|
2020-03-30 01:44:20 +02:00
|
|
|
NimBLEBeacon::NimBLEBeacon() {
|
2020-05-14 06:03:56 +02:00
|
|
|
m_beaconData.manufacturerId = 0x4c00;
|
|
|
|
m_beaconData.subType = 0x02;
|
|
|
|
m_beaconData.subTypeLength = 0x15;
|
|
|
|
m_beaconData.major = 0;
|
|
|
|
m_beaconData.minor = 0;
|
|
|
|
m_beaconData.signalPower = 0;
|
|
|
|
memset(m_beaconData.proximityUUID, 0, sizeof(m_beaconData.proximityUUID));
|
2020-03-30 01:44:20 +02:00
|
|
|
} // NimBLEBeacon
|
|
|
|
|
2020-07-09 03:27:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Retrieve the data that is being advertised.
|
|
|
|
* @return The advertised data.
|
|
|
|
*/
|
2020-03-30 01:44:20 +02:00
|
|
|
std::string NimBLEBeacon::getData() {
|
2020-05-14 06:03:56 +02:00
|
|
|
return std::string((char*) &m_beaconData, sizeof(m_beaconData));
|
2020-03-30 01:44:20 +02:00
|
|
|
} // getData
|
|
|
|
|
2020-07-09 03:27:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Get the major value being advertised.
|
|
|
|
* @return The major value advertised.
|
|
|
|
*/
|
2020-03-30 01:44:20 +02:00
|
|
|
uint16_t NimBLEBeacon::getMajor() {
|
2020-05-14 06:03:56 +02:00
|
|
|
return m_beaconData.major;
|
2020-03-30 01:44:20 +02:00
|
|
|
}
|
|
|
|
|
2020-07-09 03:27:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Get the manufacturer ID being advertised.
|
|
|
|
* @return The manufacturer ID value advertised.
|
|
|
|
*/
|
2020-03-30 01:44:20 +02:00
|
|
|
uint16_t NimBLEBeacon::getManufacturerId() {
|
2020-05-14 06:03:56 +02:00
|
|
|
return m_beaconData.manufacturerId;
|
2020-03-30 01:44:20 +02:00
|
|
|
}
|
|
|
|
|
2020-07-09 03:27:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Get the minor value being advertised.
|
|
|
|
* @return minor value advertised.
|
|
|
|
*/
|
2020-03-30 01:44:20 +02:00
|
|
|
uint16_t NimBLEBeacon::getMinor() {
|
2020-05-14 06:03:56 +02:00
|
|
|
return m_beaconData.minor;
|
2020-03-30 01:44:20 +02:00
|
|
|
}
|
|
|
|
|
2020-07-09 03:27:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Get the proximity UUID being advertised.
|
|
|
|
* @return The UUID advertised.
|
|
|
|
*/
|
2020-03-30 01:44:20 +02:00
|
|
|
NimBLEUUID NimBLEBeacon::getProximityUUID() {
|
2020-07-20 02:25:10 +02:00
|
|
|
return NimBLEUUID(m_beaconData.proximityUUID, 16, true);
|
2020-03-30 01:44:20 +02:00
|
|
|
}
|
|
|
|
|
2020-07-09 03:27:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Get the signal power being advertised.
|
|
|
|
* @return signal power level advertised.
|
|
|
|
*/
|
2020-03-30 01:44:20 +02:00
|
|
|
int8_t NimBLEBeacon::getSignalPower() {
|
2020-05-14 06:03:56 +02:00
|
|
|
return m_beaconData.signalPower;
|
2020-03-30 01:44:20 +02:00
|
|
|
}
|
|
|
|
|
2020-07-09 03:27:26 +02:00
|
|
|
|
2020-03-30 01:44:20 +02:00
|
|
|
/**
|
2020-07-09 03:27:26 +02:00
|
|
|
* @brief Set the raw data for the beacon record.
|
|
|
|
* @param [in] data The raw beacon data.
|
2020-03-30 01:44:20 +02:00
|
|
|
*/
|
2020-05-10 15:21:46 +02:00
|
|
|
void NimBLEBeacon::setData(const std::string &data) {
|
2020-05-14 06:03:56 +02:00
|
|
|
if (data.length() != sizeof(m_beaconData)) {
|
|
|
|
NIMBLE_LOGE(LOG_TAG, "Unable to set the data ... length passed in was %d and expected %d",
|
2020-03-30 01:44:20 +02:00
|
|
|
data.length(), sizeof(m_beaconData));
|
2020-05-14 06:03:56 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
memcpy(&m_beaconData, data.data(), sizeof(m_beaconData));
|
2020-03-30 01:44:20 +02:00
|
|
|
} // setData
|
|
|
|
|
2020-07-09 03:27:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Set the major value.
|
|
|
|
* @param [in] major The major value.
|
|
|
|
*/
|
2020-03-30 01:44:20 +02:00
|
|
|
void NimBLEBeacon::setMajor(uint16_t major) {
|
2020-05-14 06:03:56 +02:00
|
|
|
m_beaconData.major = ENDIAN_CHANGE_U16(major);
|
2020-03-30 01:44:20 +02:00
|
|
|
} // setMajor
|
|
|
|
|
2020-07-09 03:27:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Set the manufacturer ID.
|
|
|
|
* @param [in] manufacturerId The manufacturer ID value.
|
|
|
|
*/
|
2020-03-30 01:44:20 +02:00
|
|
|
void NimBLEBeacon::setManufacturerId(uint16_t manufacturerId) {
|
2020-05-14 06:03:56 +02:00
|
|
|
m_beaconData.manufacturerId = ENDIAN_CHANGE_U16(manufacturerId);
|
2020-03-30 01:44:20 +02:00
|
|
|
} // setManufacturerId
|
|
|
|
|
2020-07-09 03:27:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Set the minor value.
|
|
|
|
* @param [in] minor The minor value.
|
|
|
|
*/
|
2020-03-30 01:44:20 +02:00
|
|
|
void NimBLEBeacon::setMinor(uint16_t minor) {
|
2020-05-14 06:03:56 +02:00
|
|
|
m_beaconData.minor = ENDIAN_CHANGE_U16(minor);
|
2022-07-31 19:00:12 +02:00
|
|
|
} // setMinor
|
2020-03-30 01:44:20 +02:00
|
|
|
|
2020-07-09 03:27:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Set the proximity UUID.
|
|
|
|
* @param [in] uuid The proximity UUID.
|
|
|
|
*/
|
2020-05-10 15:21:46 +02:00
|
|
|
void NimBLEBeacon::setProximityUUID(const NimBLEUUID &uuid) {
|
2020-05-14 06:03:56 +02:00
|
|
|
NimBLEUUID temp_uuid = uuid;
|
|
|
|
temp_uuid.to128();
|
2020-07-20 02:25:10 +02:00
|
|
|
std::reverse_copy(temp_uuid.getNative()->u128.value,
|
|
|
|
temp_uuid.getNative()->u128.value + 16,
|
|
|
|
m_beaconData.proximityUUID);
|
2020-03-30 01:44:20 +02:00
|
|
|
} // setProximityUUID
|
|
|
|
|
2020-07-09 03:27:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Set the signal power.
|
|
|
|
* @param [in] signalPower The signal power value.
|
|
|
|
*/
|
2020-03-30 01:44:20 +02:00
|
|
|
void NimBLEBeacon::setSignalPower(int8_t signalPower) {
|
2020-05-14 06:03:56 +02:00
|
|
|
m_beaconData.signalPower = signalPower;
|
2020-03-30 01:44:20 +02:00
|
|
|
} // setSignalPower
|
|
|
|
|
|
|
|
#endif
|