Files
esp-nimble-cpp/src/NimBLEEddystoneTLM.cpp
T

228 lines
5.4 KiB
C++
Raw Normal View History

2020-03-29 17:44:20 -06:00
/*
* NimBLEEddystoneTLM.cpp
*
* Created: on March 15 2020
* Author H2zero
*
2020-03-29 17:44:20 -06:00
* Originally:
*
* BLEEddystoneTLM.cpp
*
* Created on: Mar 12, 2018
* Author: pcbreflux
*/
#include "nimconfig.h"
2020-03-29 17:44:20 -06:00
#if defined(CONFIG_BT_ENABLED)
#include "NimBLEEddystoneTLM.h"
#include "NimBLELog.h"
#include <stdio.h>
2020-03-29 17:44:20 -06:00
#include <cstring>
#define ENDIAN_CHANGE_U16(x) ((((x)&0xFF00)>>8) + (((x)&0xFF)<<8))
#define ENDIAN_CHANGE_U32(x) ((((x)&0xFF000000)>>24) + (((x)&0x00FF0000)>>8)) + ((((x)&0xFF00)<<8) + (((x)&0xFF)<<24))
static const char LOG_TAG[] = "NimBLEEddystoneTLM";
2020-07-08 19:27:26 -06:00
/**
* @brief Construct a default EddystoneTLM beacon object.
*/
2020-03-29 17:44:20 -06:00
NimBLEEddystoneTLM::NimBLEEddystoneTLM() {
beaconUUID = 0xFEAA;
m_eddystoneData.frameType = EDDYSTONE_TLM_FRAME_TYPE;
m_eddystoneData.version = 0;
m_eddystoneData.volt = 3300; // 3300mV = 3.3V
m_eddystoneData.temp = (uint16_t) ((float) 23.00 * 256); // 8.8 fixed format
m_eddystoneData.advCount = 0;
m_eddystoneData.tmil = 0;
2020-03-29 17:44:20 -06:00
} // NimBLEEddystoneTLM
2020-07-08 19:27:26 -06:00
/**
* @brief Retrieve the data that is being advertised.
* @return The advertised data.
*/
2020-03-29 17:44:20 -06:00
std::string NimBLEEddystoneTLM::getData() {
return std::string((char*) &m_eddystoneData, sizeof(m_eddystoneData));
2020-03-29 17:44:20 -06:00
} // getData
2020-07-08 19:27:26 -06:00
/**
* @brief Get the UUID being advertised.
* @return The UUID advertised.
*/
2020-03-29 17:44:20 -06:00
NimBLEUUID NimBLEEddystoneTLM::getUUID() {
return NimBLEUUID(beaconUUID);
2020-03-29 17:44:20 -06:00
} // getUUID
2020-07-08 19:27:26 -06:00
/**
* @brief Get the version being advertised.
* @return The version number.
*/
2020-03-29 17:44:20 -06:00
uint8_t NimBLEEddystoneTLM::getVersion() {
return m_eddystoneData.version;
2020-03-29 17:44:20 -06:00
} // getVersion
2020-07-08 19:27:26 -06:00
/**
* @brief Get the battery voltage.
* @return The battery voltage.
*/
2020-03-29 17:44:20 -06:00
uint16_t NimBLEEddystoneTLM::getVolt() {
return ENDIAN_CHANGE_U16(m_eddystoneData.volt);
2020-03-29 17:44:20 -06:00
} // getVolt
2020-07-08 19:27:26 -06:00
/**
* @brief Get the temperature being advertised.
* @return The temperature value.
*/
2020-03-29 17:44:20 -06:00
float NimBLEEddystoneTLM::getTemp() {
return ENDIAN_CHANGE_U16(m_eddystoneData.temp) / 256.0f;
2020-03-29 17:44:20 -06:00
} // getTemp
2020-07-08 19:27:26 -06:00
/**
2022-07-31 11:00:12 -06:00
* @brief Get the count of advertisements sent.
* @return The number of advertisements.
2020-07-08 19:27:26 -06:00
*/
2020-03-29 17:44:20 -06:00
uint32_t NimBLEEddystoneTLM::getCount() {
return ENDIAN_CHANGE_U32(m_eddystoneData.advCount);
2020-03-29 17:44:20 -06:00
} // getCount
2020-07-08 19:27:26 -06:00
/**
2022-07-31 11:00:12 -06:00
* @brief Get the advertisement time.
* @return The advertisement time.
2020-07-08 19:27:26 -06:00
*/
2020-03-29 17:44:20 -06:00
uint32_t NimBLEEddystoneTLM::getTime() {
return (ENDIAN_CHANGE_U32(m_eddystoneData.tmil)) / 10;
2020-03-29 17:44:20 -06:00
} // getTime
2020-07-08 19:27:26 -06:00
/**
* @brief Get a string representation of the beacon.
* @return The string representation.
*/
2020-03-29 17:44:20 -06:00
std::string NimBLEEddystoneTLM::toString() {
std::string out = "";
uint32_t rawsec = ENDIAN_CHANGE_U32(m_eddystoneData.tmil);
char val[12];
out += "Version "; // + std::string(m_eddystoneData.version);
snprintf(val, sizeof(val), "%d", m_eddystoneData.version);
out += val;
out += "\n";
out += "Battery Voltage "; // + ENDIAN_CHANGE_U16(m_eddystoneData.volt);
snprintf(val, sizeof(val), "%d", ENDIAN_CHANGE_U16(m_eddystoneData.volt));
out += val;
out += " mV\n";
out += "Temperature ";
snprintf(val, sizeof(val), "%.2f", ENDIAN_CHANGE_U16(m_eddystoneData.temp) / 256.0f);
out += val;
out += " C\n";
out += "Adv. Count ";
snprintf(val, sizeof(val), "%" PRIu32, ENDIAN_CHANGE_U32(m_eddystoneData.advCount));
2020-03-29 17:44:20 -06:00
out += val;
out += "\n";
out += "Time in seconds ";
snprintf(val, sizeof(val), "%" PRIu32, rawsec/10);
2020-03-29 17:44:20 -06:00
out += val;
out += "\n";
out += "Time ";
snprintf(val, sizeof(val), "%04" PRIu32, rawsec / 864000);
2020-03-29 17:44:20 -06:00
out += val;
out += ".";
snprintf(val, sizeof(val), "%02" PRIu32, (rawsec / 36000) % 24);
2020-03-29 17:44:20 -06:00
out += val;
out += ":";
snprintf(val, sizeof(val), "%02" PRIu32, (rawsec / 600) % 60);
2020-03-29 17:44:20 -06:00
out += val;
out += ":";
snprintf(val, sizeof(val), "%02" PRIu32, (rawsec / 10) % 60);
2020-03-29 17:44:20 -06:00
out += val;
out += "\n";
return out;
} // toString
2020-07-08 19:27:26 -06:00
2020-03-29 17:44:20 -06:00
/**
2022-07-31 11:00:12 -06:00
* @brief Set the raw data for the beacon advertisement.
2020-07-08 19:27:26 -06:00
* @param [in] data The raw data to advertise.
2020-03-29 17:44:20 -06:00
*/
void NimBLEEddystoneTLM::setData(const std::string &data) {
if (data.length() != sizeof(m_eddystoneData)) {
NIMBLE_LOGE(LOG_TAG, "Unable to set the data ... length passed in was %d and expected %d",
2020-03-29 17:44:20 -06:00
data.length(), sizeof(m_eddystoneData));
return;
}
2020-03-29 17:44:20 -06:00
memcpy(&m_eddystoneData, data.data(), data.length());
} // setData
2020-07-08 19:27:26 -06:00
/**
* @brief Set the UUID to advertise.
* @param [in] l_uuid The UUID.
*/
void NimBLEEddystoneTLM::setUUID(const NimBLEUUID &l_uuid) {
beaconUUID = l_uuid.getNative()->u16.value;
2020-03-29 17:44:20 -06:00
} // setUUID
2020-07-08 19:27:26 -06:00
/**
* @brief Set the version to advertise.
* @param [in] version The version number.
*/
2020-03-29 17:44:20 -06:00
void NimBLEEddystoneTLM::setVersion(uint8_t version) {
m_eddystoneData.version = version;
2020-03-29 17:44:20 -06:00
} // setVersion
2020-07-08 19:27:26 -06:00
/**
* @brief Set the battery voltage to advertise.
* @param [in] volt The voltage in millivolts.
*/
2020-03-29 17:44:20 -06:00
void NimBLEEddystoneTLM::setVolt(uint16_t volt) {
m_eddystoneData.volt = volt;
2020-03-29 17:44:20 -06:00
} // setVolt
2020-07-08 19:27:26 -06:00
/**
* @brief Set the temperature to advertise.
* @param [in] temp The temperature value.
*/
2020-03-29 17:44:20 -06:00
void NimBLEEddystoneTLM::setTemp(float temp) {
m_eddystoneData.temp = (uint16_t)temp;
2020-03-29 17:44:20 -06:00
} // setTemp
2020-07-08 19:27:26 -06:00
/**
2022-07-31 11:00:12 -06:00
* @brief Set the advertisement count.
* @param [in] advCount The advertisement number.
2020-07-08 19:27:26 -06:00
*/
2020-03-29 17:44:20 -06:00
void NimBLEEddystoneTLM::setCount(uint32_t advCount) {
m_eddystoneData.advCount = advCount;
2020-03-29 17:44:20 -06:00
} // setCount
2020-07-08 19:27:26 -06:00
/**
2022-07-31 11:00:12 -06:00
* @brief Set the advertisement time.
* @param [in] tmil The advertisement time in milliseconds.
2020-07-08 19:27:26 -06:00
*/
2020-03-29 17:44:20 -06:00
void NimBLEEddystoneTLM::setTime(uint32_t tmil) {
m_eddystoneData.tmil = tmil;
2020-03-29 17:44:20 -06:00
} // setTime
#endif