forked from kylegordon/ESP32-mqtt-room
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
667 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
/* | ||
* BLEEddystoneTLM.cpp | ||
* | ||
* Created on: Mar 12, 2018 | ||
* Author: pcbreflux | ||
*/ | ||
#include "Arduino.h" | ||
#include "sdkconfig.h" | ||
#if defined(CONFIG_BT_ENABLED) | ||
#include <string.h> | ||
#include <esp_log.h> | ||
#include "BLEEddystoneTLM.h" | ||
|
||
static const char LOG_TAG[] = "BLEEddystoneTLM"; | ||
#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)) | ||
|
||
BLEEddystoneTLM::BLEEddystoneTLM() { | ||
beconUUID = 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); | ||
m_eddystoneData.advCount = 0; | ||
m_eddystoneData.tmil = 0; | ||
} // BLEEddystoneTLM | ||
|
||
std::string BLEEddystoneTLM::getData() { | ||
return std::string((char*)&m_eddystoneData, sizeof(m_eddystoneData)); | ||
} // getData | ||
|
||
BLEUUID BLEEddystoneTLM::getUUID() { | ||
return BLEUUID(beconUUID); | ||
} // getUUID | ||
|
||
uint8_t BLEEddystoneTLM::getVersion() { | ||
return m_eddystoneData.version; | ||
} // getVersion | ||
|
||
uint16_t BLEEddystoneTLM::getVolt() { | ||
return m_eddystoneData.volt; | ||
} // getVolt | ||
|
||
float BLEEddystoneTLM::getTemp() { | ||
return (float)m_eddystoneData.temp; | ||
} // getTemp | ||
|
||
uint32_t BLEEddystoneTLM::getCount() { | ||
return m_eddystoneData.advCount; | ||
} // getCount | ||
|
||
uint32_t BLEEddystoneTLM::getTime() { | ||
return m_eddystoneData.tmil; | ||
} // getTime | ||
|
||
std::string BLEEddystoneTLM::toString() { | ||
std::string out = ""; | ||
String buff; | ||
uint32_t rawsec; | ||
|
||
out += "Version "; | ||
buff = String(m_eddystoneData.version, DEC); | ||
out += buff.c_str(); | ||
out += "\n"; | ||
|
||
out += "Battery Voltage "; | ||
buff = String(ENDIAN_CHANGE_U16(m_eddystoneData.volt), DEC); | ||
out += buff.c_str(); | ||
out += " mV\n"; | ||
|
||
out += "Temperature "; | ||
buff = String((float)m_eddystoneData.temp, 1); | ||
out += buff.c_str(); | ||
out += " °C\n"; | ||
|
||
out += "Adv. Count "; | ||
buff = String(ENDIAN_CHANGE_U32(m_eddystoneData.advCount), DEC); | ||
out += buff.c_str(); | ||
out += "\n"; | ||
|
||
out += "Time "; | ||
rawsec = ENDIAN_CHANGE_U32(m_eddystoneData.tmil); | ||
buff = "0000"+String(rawsec/864000, DEC); | ||
out += buff.substring(buff.length()-4,buff.length()).c_str(); | ||
out += "."; | ||
buff = "00"+String((rawsec/36000)%24, DEC); | ||
out += buff.substring(buff.length()-2,buff.length()).c_str(); | ||
out += ":"; | ||
buff = "00"+String((rawsec/600)%60, DEC); | ||
out += buff.substring(buff.length()-2,buff.length()).c_str(); | ||
out += ":"; | ||
buff = "00"+String((rawsec/10)%60, DEC); | ||
out += buff.substring(buff.length()-2,buff.length()).c_str(); | ||
out += "\n"; | ||
|
||
return out; | ||
} // toString | ||
|
||
/** | ||
* Set the raw data for the beacon record. | ||
*/ | ||
void BLEEddystoneTLM::setData(std::string data) { | ||
if (data.length() != sizeof(m_eddystoneData)) { | ||
ESP_LOGE(LOG_TAG, "Unable to set the data ... length passed in was %d and expected %d", data.length(), sizeof(m_eddystoneData)); | ||
return; | ||
} | ||
memcpy(&m_eddystoneData, data.data(), data.length()); | ||
} // setData | ||
|
||
void BLEEddystoneTLM::setUUID(BLEUUID l_uuid) { | ||
beconUUID = l_uuid.getNative()->uuid.uuid16; | ||
} // setUUID | ||
|
||
void BLEEddystoneTLM::setVersion(uint8_t version) { | ||
m_eddystoneData.version = version; | ||
} // setVersion | ||
|
||
void BLEEddystoneTLM::setVolt(uint16_t volt) { | ||
m_eddystoneData.volt = volt; | ||
} // setVolt | ||
|
||
void BLEEddystoneTLM::setTemp(float temp) { | ||
m_eddystoneData.temp = (uint16_t)temp; | ||
} // setTemp | ||
|
||
void BLEEddystoneTLM::setCount(uint32_t advCount) { | ||
m_eddystoneData.advCount = advCount; | ||
} // setCount | ||
|
||
void BLEEddystoneTLM::setTime(uint32_t tmil) { | ||
m_eddystoneData.tmil = tmil; | ||
} // setTime | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* BLEEddystoneTLM.cpp | ||
* | ||
* Created on: Mar 12, 2018 | ||
* Author: pcbreflux | ||
*/ | ||
|
||
#ifndef _BLEEddystoneTLM_H_ | ||
#define _BLEEddystoneTLM_H_ | ||
#include "BLEUUID.h" | ||
|
||
#define EDDYSTONE_TLM_FRAME_TYPE 0x20 | ||
|
||
/** | ||
* @brief Representation of a beacon. | ||
* See: | ||
* * https://github.com/google/eddystone | ||
*/ | ||
class BLEEddystoneTLM { | ||
private: | ||
uint16_t beconUUID; | ||
struct { | ||
uint8_t frameType; | ||
int8_t version; | ||
uint16_t volt; | ||
uint16_t temp; | ||
uint32_t advCount; | ||
uint32_t tmil; | ||
} __attribute__((packed))m_eddystoneData; | ||
public: | ||
BLEEddystoneTLM(); | ||
std::string getData(); | ||
BLEUUID getUUID(); | ||
uint8_t getVersion(); | ||
uint16_t getVolt(); | ||
float getTemp(); | ||
uint32_t getCount(); | ||
uint32_t getTime(); | ||
std::string toString(); | ||
void setData(std::string data); | ||
void setUUID(BLEUUID l_uuid); | ||
void setVersion(uint8_t version); | ||
void setVolt(uint16_t volt); | ||
void setTemp(float temp); | ||
void setCount(uint32_t advCount); | ||
void setTime(uint32_t tmil); | ||
|
||
}; // BLEEddystoneTLM | ||
|
||
#endif /* _BLEEddystoneTLM_H_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
/* | ||
* BLEEddystoneURL.cpp | ||
* | ||
* Created on: Mar 12, 2018 | ||
* Author: pcbreflux | ||
*/ | ||
#include "sdkconfig.h" | ||
#if defined(CONFIG_BT_ENABLED) | ||
#include <string.h> | ||
#include <esp_log.h> | ||
#include "BLEEddystoneURL.h" | ||
|
||
static const char LOG_TAG[] = "BLEEddystoneURL"; | ||
|
||
BLEEddystoneURL::BLEEddystoneURL() { | ||
beconUUID = 0xFEAA; | ||
lengthURL = 0; | ||
m_eddystoneData.frameType = EDDYSTONE_URL_FRAME_TYPE; | ||
m_eddystoneData.advertisedTxPower = 0; | ||
memset(m_eddystoneData.url, 0, sizeof(m_eddystoneData.url)); | ||
} // BLEEddystoneURL | ||
|
||
std::string BLEEddystoneURL::getData() { | ||
return std::string((char*)&m_eddystoneData, sizeof(m_eddystoneData)); | ||
} // getData | ||
|
||
BLEUUID BLEEddystoneURL::getUUID() { | ||
return BLEUUID(beconUUID); | ||
} // getUUID | ||
|
||
int8_t BLEEddystoneURL::getPower() { | ||
return m_eddystoneData.advertisedTxPower; | ||
} // getPower | ||
|
||
std::string BLEEddystoneURL::getURL() { | ||
return std::string((char*)&m_eddystoneData.url, sizeof(m_eddystoneData.url)); | ||
} // getURL | ||
|
||
std::string BLEEddystoneURL::getDecodedURL() { | ||
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; | ||
} | ||
} | ||
} | ||
|
||
|
||
return decodedURL; | ||
} // getDecodedURL | ||
|
||
|
||
|
||
/** | ||
* Set the raw data for the beacon record. | ||
*/ | ||
void BLEEddystoneURL::setData(std::string data) { | ||
if (data.length() > sizeof(m_eddystoneData)) { | ||
ESP_LOGE(LOG_TAG, "Unable to set the data ... length passed in was %d and max expected %d", data.length(), sizeof(m_eddystoneData)); | ||
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)); | ||
|
||
} // setData | ||
|
||
void BLEEddystoneURL::setUUID(BLEUUID l_uuid) { | ||
beconUUID = l_uuid.getNative()->uuid.uuid16; | ||
} // setUUID | ||
|
||
void BLEEddystoneURL::setPower(int8_t advertisedTxPower) { | ||
m_eddystoneData.advertisedTxPower = advertisedTxPower; | ||
} // setPower | ||
|
||
void BLEEddystoneURL::setURL(std::string url) { | ||
if (url.length() > sizeof(m_eddystoneData.url)) { | ||
ESP_LOGE(LOG_TAG, "Unable to set the url ... length passed in was %d and max expected %d", url.length(), sizeof(m_eddystoneData.url)); | ||
return; | ||
} | ||
memset(m_eddystoneData.url, 0, sizeof(m_eddystoneData.url)); | ||
memcpy(m_eddystoneData.url, url.data(), url.length()); | ||
lengthURL=url.length(); | ||
} // setURL | ||
|
||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* BLEEddystoneURL.cpp | ||
* | ||
* Created on: Mar 12, 2018 | ||
* Author: pcbreflux | ||
*/ | ||
|
||
#ifndef _BLEEddystoneURL_H_ | ||
#define _BLEEddystoneURL_H_ | ||
#include "BLEUUID.h" | ||
|
||
#define EDDYSTONE_URL_FRAME_TYPE 0x10 | ||
|
||
/** | ||
* @brief Representation of a beacon. | ||
* See: | ||
* * https://github.com/google/eddystone | ||
*/ | ||
class BLEEddystoneURL { | ||
private: | ||
uint16_t beconUUID; | ||
uint8_t lengthURL; | ||
struct { | ||
uint8_t frameType; | ||
int8_t advertisedTxPower; | ||
uint8_t url[16]; | ||
} __attribute__((packed))m_eddystoneData; | ||
public: | ||
BLEEddystoneURL(); | ||
std::string getData(); | ||
BLEUUID getUUID(); | ||
int8_t getPower(); | ||
std::string getURL(); | ||
std::string getDecodedURL(); | ||
void setData(std::string data); | ||
void setUUID(BLEUUID l_uuid); | ||
void setPower(int8_t advertisedTxPower); | ||
void setURL(std::string url); | ||
|
||
}; // BLEEddystoneURL | ||
|
||
#endif /* _BLEEddystoneURL_H_ */ |
Oops, something went wrong.