2020-03-30 01:44:20 +02:00
|
|
|
/*
|
|
|
|
* NimBLEEddystoneURL.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:
|
|
|
|
*
|
|
|
|
* BLEEddystoneURL.cpp
|
|
|
|
*
|
|
|
|
* Created on: Mar 12, 2018
|
|
|
|
* Author: pcbreflux
|
|
|
|
*/
|
|
|
|
#include "sdkconfig.h"
|
|
|
|
#if defined(CONFIG_BT_ENABLED)
|
|
|
|
|
|
|
|
#include "NimBLEEddystoneURL.h"
|
|
|
|
#include "NimBLELog.h"
|
|
|
|
|
|
|
|
#include <cstring>
|
|
|
|
|
|
|
|
static const char LOG_TAG[] = "NimBLEEddystoneURL";
|
|
|
|
|
2020-07-09 03:27:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Construct a default EddystoneURL beacon object.
|
|
|
|
*/
|
2020-03-30 01:44:20 +02:00
|
|
|
NimBLEEddystoneURL::NimBLEEddystoneURL() {
|
2020-05-14 06:03:56 +02:00
|
|
|
beaconUUID = 0xFEAA;
|
|
|
|
lengthURL = 0;
|
|
|
|
m_eddystoneData.frameType = EDDYSTONE_URL_FRAME_TYPE;
|
|
|
|
m_eddystoneData.advertisedTxPower = 0;
|
|
|
|
memset(m_eddystoneData.url, 0, sizeof(m_eddystoneData.url));
|
2020-03-30 01:44:20 +02:00
|
|
|
} // BLEEddystoneURL
|
|
|
|
|
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 NimBLEEddystoneURL::getData() {
|
2020-05-14 06:03:56 +02:00
|
|
|
return std::string((char*) &m_eddystoneData, sizeof(m_eddystoneData));
|
2020-03-30 01:44:20 +02:00
|
|
|
} // getData
|
|
|
|
|
2020-07-09 03:27:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Get the UUID being advertised.
|
|
|
|
* @return The UUID advertised.
|
|
|
|
*/
|
2020-03-30 01:44:20 +02:00
|
|
|
NimBLEUUID NimBLEEddystoneURL::getUUID() {
|
2020-05-14 06:03:56 +02:00
|
|
|
return NimBLEUUID(beaconUUID);
|
2020-03-30 01:44:20 +02:00
|
|
|
} // getUUID
|
|
|
|
|
2020-07-09 03:27:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Get the transmit power being advertised.
|
|
|
|
* @return The transmit power.
|
|
|
|
*/
|
2020-03-30 01:44:20 +02:00
|
|
|
int8_t NimBLEEddystoneURL::getPower() {
|
2020-05-14 06:03:56 +02:00
|
|
|
return m_eddystoneData.advertisedTxPower;
|
2020-03-30 01:44:20 +02:00
|
|
|
} // getPower
|
|
|
|
|
2020-07-09 03:27:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Get the raw URL being advertised.
|
|
|
|
* @return The raw URL.
|
|
|
|
*/
|
2020-03-30 01:44:20 +02:00
|
|
|
std::string NimBLEEddystoneURL::getURL() {
|
2020-05-14 06:03:56 +02:00
|
|
|
return std::string((char*) &m_eddystoneData.url, sizeof(m_eddystoneData.url));
|
2020-03-30 01:44:20 +02:00
|
|
|
} // getURL
|
|
|
|
|
2020-07-09 03:27:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Get the full URL being advertised.
|
|
|
|
* @return The full URL.
|
|
|
|
*/
|
2020-03-30 01:44:20 +02:00
|
|
|
std::string NimBLEEddystoneURL::getDecodedURL() {
|
2020-05-14 06:03:56 +02:00
|
|
|
std::string decodedURL = "";
|
|
|
|
|
|
|
|
switch (m_eddystoneData.url[0]) {
|
|
|
|
case 0x00:
|
|
|
|
decodedURL += "http://www.";
|
|
|
|
break;
|
|
|
|
case 0x01:
|
|
|
|
decodedURL += "https://www.";
|
|
|
|
break;
|
|
|
|
case 0x02:
|
|
|
|
decodedURL += "http://";
|
|
|
|
break;
|
|
|
|
case 0x03:
|
|
|
|
decodedURL += "https://";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
decodedURL += m_eddystoneData.url[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 1; i < lengthURL; i++) {
|
|
|
|
if (m_eddystoneData.url[i] > 33 && m_eddystoneData.url[i] < 127) {
|
|
|
|
decodedURL += m_eddystoneData.url[i];
|
|
|
|
} else {
|
|
|
|
switch (m_eddystoneData.url[i]) {
|
|
|
|
case 0x00:
|
|
|
|
decodedURL += ".com/";
|
|
|
|
break;
|
|
|
|
case 0x01:
|
|
|
|
decodedURL += ".org/";
|
|
|
|
break;
|
|
|
|
case 0x02:
|
|
|
|
decodedURL += ".edu/";
|
|
|
|
break;
|
|
|
|
case 0x03:
|
|
|
|
decodedURL += ".net/";
|
|
|
|
break;
|
|
|
|
case 0x04:
|
|
|
|
decodedURL += ".info/";
|
|
|
|
break;
|
|
|
|
case 0x05:
|
|
|
|
decodedURL += ".biz/";
|
|
|
|
break;
|
|
|
|
case 0x06:
|
|
|
|
decodedURL += ".gov/";
|
|
|
|
break;
|
|
|
|
case 0x07:
|
|
|
|
decodedURL += ".com";
|
|
|
|
break;
|
|
|
|
case 0x08:
|
|
|
|
decodedURL += ".org";
|
|
|
|
break;
|
|
|
|
case 0x09:
|
|
|
|
decodedURL += ".edu";
|
|
|
|
break;
|
|
|
|
case 0x0A:
|
|
|
|
decodedURL += ".net";
|
|
|
|
break;
|
|
|
|
case 0x0B:
|
|
|
|
decodedURL += ".info";
|
|
|
|
break;
|
|
|
|
case 0x0C:
|
|
|
|
decodedURL += ".biz";
|
|
|
|
break;
|
|
|
|
case 0x0D:
|
|
|
|
decodedURL += ".gov";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return decodedURL;
|
2020-03-30 01:44:20 +02:00
|
|
|
} // getDecodedURL
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2020-07-09 03:27:26 +02:00
|
|
|
* @brief Set the raw data for the beacon advertisment.
|
|
|
|
* @param [in] data The raw data to advertise.
|
2020-03-30 01:44:20 +02:00
|
|
|
*/
|
2020-05-10 15:21:46 +02:00
|
|
|
void NimBLEEddystoneURL::setData(const std::string &data) {
|
2020-05-14 06:03:56 +02:00
|
|
|
if (data.length() > sizeof(m_eddystoneData)) {
|
|
|
|
NIMBLE_LOGE(LOG_TAG, "Unable to set the data ... length passed in was %d and max expected %d",
|
2020-03-30 01:44:20 +02:00
|
|
|
data.length(), sizeof(m_eddystoneData));
|
2020-05-14 06:03:56 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
memset(&m_eddystoneData, 0, sizeof(m_eddystoneData));
|
|
|
|
memcpy(&m_eddystoneData, data.data(), data.length());
|
|
|
|
lengthURL = data.length() - (sizeof(m_eddystoneData) - sizeof(m_eddystoneData.url));
|
2020-03-30 01:44:20 +02:00
|
|
|
} // setData
|
|
|
|
|
2020-07-09 03:27:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Set the UUID to advertise.
|
|
|
|
* @param [in] l_uuid The UUID.
|
|
|
|
*/
|
2020-05-10 15:21:46 +02:00
|
|
|
void NimBLEEddystoneURL::setUUID(const NimBLEUUID &l_uuid) {
|
2020-05-14 06:03:56 +02:00
|
|
|
beaconUUID = l_uuid.getNative()->u16.value;
|
2020-03-30 01:44:20 +02:00
|
|
|
} // setUUID
|
|
|
|
|
2020-07-09 03:27:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Set the transmit power to advertise.
|
|
|
|
* @param [in] advertisedTxPower The transmit power level.
|
|
|
|
*/
|
2020-03-30 01:44:20 +02:00
|
|
|
void NimBLEEddystoneURL::setPower(int8_t advertisedTxPower) {
|
2020-05-14 06:03:56 +02:00
|
|
|
m_eddystoneData.advertisedTxPower = advertisedTxPower;
|
2020-03-30 01:44:20 +02:00
|
|
|
} // setPower
|
|
|
|
|
2020-07-09 03:27:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Set the URL to advertise.
|
|
|
|
* @param [in] url The URL.
|
|
|
|
*/
|
2020-05-10 15:21:46 +02:00
|
|
|
void NimBLEEddystoneURL::setURL(const std::string &url) {
|
2020-03-30 01:44:20 +02:00
|
|
|
if (url.length() > sizeof(m_eddystoneData.url)) {
|
2020-05-14 06:03:56 +02:00
|
|
|
NIMBLE_LOGE(LOG_TAG, "Unable to set the url ... length passed in was %d and max expected %d",
|
2020-03-30 01:44:20 +02:00
|
|
|
url.length(), sizeof(m_eddystoneData.url));
|
2020-05-14 06:03:56 +02:00
|
|
|
return;
|
2020-03-30 01:44:20 +02:00
|
|
|
}
|
|
|
|
memset(m_eddystoneData.url, 0, sizeof(m_eddystoneData.url));
|
|
|
|
memcpy(m_eddystoneData.url, url.data(), url.length());
|
|
|
|
lengthURL = url.length();
|
|
|
|
} // setURL
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|