Skip to content

Commit d2f7e10

Browse files
authored
Merge pull request #434 from pcbreflux/master
BLEEddystone. New Classes to Send or Scann Eddystone Beacon Data
2 parents 761869f + 5c89301 commit d2f7e10

File tree

4 files changed

+375
-0
lines changed

4 files changed

+375
-0
lines changed

cpp_utils/BLEEddystoneTLM.cpp

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
/*
2+
* BLEEddystoneTLM.cpp
3+
*
4+
* Created on: Mar 12, 2018
5+
* Author: pcbreflux
6+
*/
7+
#include "Arduino.h"
8+
#include "sdkconfig.h"
9+
#if defined(CONFIG_BT_ENABLED)
10+
#include <string.h>
11+
#include <esp_log.h>
12+
#include "BLEEddystoneTLM.h"
13+
14+
static const char LOG_TAG[] = "BLEEddystoneTLM";
15+
#define ENDIAN_CHANGE_U16(x) ((((x)&0xFF00)>>8) + (((x)&0xFF)<<8))
16+
#define ENDIAN_CHANGE_U32(x) ((((x)&0xFF000000)>>24) + (((x)&0x00FF0000)>>8)) + ((((x)&0xFF00)<<8) + (((x)&0xFF)<<24))
17+
18+
BLEEddystoneTLM::BLEEddystoneTLM() {
19+
beconUUID = 0xFEAA;
20+
m_eddystoneData.frameType = EDDYSTONE_TLM_FRAME_TYPE;
21+
m_eddystoneData.version = 0;
22+
m_eddystoneData.volt = 3300; // 3300mV = 3.3V
23+
m_eddystoneData.temp = (uint16_t)((float)23.00);
24+
m_eddystoneData.advCount = 0;
25+
m_eddystoneData.tmil = 0;
26+
} // BLEEddystoneTLM
27+
28+
std::string BLEEddystoneTLM::getData() {
29+
return std::string((char*)&m_eddystoneData, sizeof(m_eddystoneData));
30+
} // getData
31+
32+
BLEUUID BLEEddystoneTLM::getUUID() {
33+
return BLEUUID(beconUUID);
34+
} // getUUID
35+
36+
uint8_t BLEEddystoneTLM::getVersion() {
37+
return m_eddystoneData.version;
38+
} // getVersion
39+
40+
uint16_t BLEEddystoneTLM::getVolt() {
41+
return m_eddystoneData.volt;
42+
} // getVolt
43+
44+
float BLEEddystoneTLM::getTemp() {
45+
return (float)m_eddystoneData.temp;
46+
} // getTemp
47+
48+
uint32_t BLEEddystoneTLM::getCount() {
49+
return m_eddystoneData.advCount;
50+
} // getCount
51+
52+
uint32_t BLEEddystoneTLM::getTime() {
53+
return m_eddystoneData.tmil;
54+
} // getTime
55+
56+
std::string BLEEddystoneTLM::toString() {
57+
std::string out = "";
58+
String buff;
59+
uint32_t rawsec;
60+
61+
out += "Version ";
62+
buff = String(m_eddystoneData.version, DEC);
63+
out += buff.c_str();
64+
out += "\n";
65+
66+
out += "Battery Voltage ";
67+
buff = String(ENDIAN_CHANGE_U16(m_eddystoneData.volt), DEC);
68+
out += buff.c_str();
69+
out += " mV\n";
70+
71+
out += "Temperature ";
72+
buff = String((float)m_eddystoneData.temp, 1);
73+
out += buff.c_str();
74+
out += " °C\n";
75+
76+
out += "Adv. Count ";
77+
buff = String(ENDIAN_CHANGE_U32(m_eddystoneData.advCount), DEC);
78+
out += buff.c_str();
79+
out += "\n";
80+
81+
out += "Time ";
82+
rawsec = ENDIAN_CHANGE_U32(m_eddystoneData.tmil);
83+
buff = "0000"+String(rawsec/864000, DEC);
84+
out += buff.substring(buff.length()-4,buff.length()).c_str();
85+
out += ".";
86+
buff = "00"+String((rawsec/36000)%24, DEC);
87+
out += buff.substring(buff.length()-2,buff.length()).c_str();
88+
out += ":";
89+
buff = "00"+String((rawsec/600)%60, DEC);
90+
out += buff.substring(buff.length()-2,buff.length()).c_str();
91+
out += ":";
92+
buff = "00"+String((rawsec/10)%60, DEC);
93+
out += buff.substring(buff.length()-2,buff.length()).c_str();
94+
out += "\n";
95+
96+
return out;
97+
} // toString
98+
99+
/**
100+
* Set the raw data for the beacon record.
101+
*/
102+
void BLEEddystoneTLM::setData(std::string data) {
103+
if (data.length() != sizeof(m_eddystoneData)) {
104+
ESP_LOGE(LOG_TAG, "Unable to set the data ... length passed in was %d and expected %d", data.length(), sizeof(m_eddystoneData));
105+
return;
106+
}
107+
memcpy(&m_eddystoneData, data.data(), data.length());
108+
} // setData
109+
110+
void BLEEddystoneTLM::setUUID(BLEUUID l_uuid) {
111+
beconUUID = l_uuid.getNative()->uuid.uuid16;
112+
} // setUUID
113+
114+
void BLEEddystoneTLM::setVersion(uint8_t version) {
115+
m_eddystoneData.version = version;
116+
} // setVersion
117+
118+
void BLEEddystoneTLM::setVolt(uint16_t volt) {
119+
m_eddystoneData.volt = volt;
120+
} // setVolt
121+
122+
void BLEEddystoneTLM::setTemp(float temp) {
123+
m_eddystoneData.temp = (uint16_t)temp;
124+
} // setTemp
125+
126+
void BLEEddystoneTLM::setCount(uint32_t advCount) {
127+
m_eddystoneData.advCount = advCount;
128+
} // setCount
129+
130+
void BLEEddystoneTLM::setTime(uint32_t tmil) {
131+
m_eddystoneData.tmil = tmil;
132+
} // setTime
133+
134+
#endif

cpp_utils/BLEEddystoneTLM.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* BLEEddystoneTLM.cpp
3+
*
4+
* Created on: Mar 12, 2018
5+
* Author: pcbreflux
6+
*/
7+
8+
#ifndef _BLEEddystoneTLM_H_
9+
#define _BLEEddystoneTLM_H_
10+
#include "BLEUUID.h"
11+
12+
#define EDDYSTONE_TLM_FRAME_TYPE 0x20
13+
14+
/**
15+
* @brief Representation of a beacon.
16+
* See:
17+
* * https://github.com/google/eddystone
18+
*/
19+
class BLEEddystoneTLM {
20+
private:
21+
uint16_t beconUUID;
22+
struct {
23+
uint8_t frameType;
24+
int8_t version;
25+
uint16_t volt;
26+
uint16_t temp;
27+
uint32_t advCount;
28+
uint32_t tmil;
29+
} __attribute__((packed))m_eddystoneData;
30+
public:
31+
BLEEddystoneTLM();
32+
std::string getData();
33+
BLEUUID getUUID();
34+
uint8_t getVersion();
35+
uint16_t getVolt();
36+
float getTemp();
37+
uint32_t getCount();
38+
uint32_t getTime();
39+
std::string toString();
40+
void setData(std::string data);
41+
void setUUID(BLEUUID l_uuid);
42+
void setVersion(uint8_t version);
43+
void setVolt(uint16_t volt);
44+
void setTemp(float temp);
45+
void setCount(uint32_t advCount);
46+
void setTime(uint32_t tmil);
47+
48+
}; // BLEEddystoneTLM
49+
50+
#endif /* _BLEEddystoneTLM_H_ */

cpp_utils/BLEEddystoneURL.cpp

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
/*
2+
* BLEEddystoneURL.cpp
3+
*
4+
* Created on: Mar 12, 2018
5+
* Author: pcbreflux
6+
*/
7+
#include "sdkconfig.h"
8+
#if defined(CONFIG_BT_ENABLED)
9+
#include <string.h>
10+
#include <esp_log.h>
11+
#include "BLEEddystoneURL.h"
12+
13+
static const char LOG_TAG[] = "BLEEddystoneURL";
14+
15+
BLEEddystoneURL::BLEEddystoneURL() {
16+
beconUUID = 0xFEAA;
17+
lengthURL = 0;
18+
m_eddystoneData.frameType = EDDYSTONE_URL_FRAME_TYPE;
19+
m_eddystoneData.advertisedTxPower = 0;
20+
memset(m_eddystoneData.url, 0, sizeof(m_eddystoneData.url));
21+
} // BLEEddystoneURL
22+
23+
std::string BLEEddystoneURL::getData() {
24+
return std::string((char*)&m_eddystoneData, sizeof(m_eddystoneData));
25+
} // getData
26+
27+
BLEUUID BLEEddystoneURL::getUUID() {
28+
return BLEUUID(beconUUID);
29+
} // getUUID
30+
31+
int8_t BLEEddystoneURL::getPower() {
32+
return m_eddystoneData.advertisedTxPower;
33+
} // getPower
34+
35+
std::string BLEEddystoneURL::getURL() {
36+
return std::string((char*)&m_eddystoneData.url, sizeof(m_eddystoneData.url));
37+
} // getURL
38+
39+
std::string BLEEddystoneURL::getDecodedURL() {
40+
std::string decodedURL = "";
41+
42+
switch (m_eddystoneData.url[0]) {
43+
case 0x00:
44+
decodedURL += "http://www.";
45+
break;
46+
case 0x01:
47+
decodedURL += "https://www.";
48+
break;
49+
case 0x02:
50+
decodedURL += "http://";
51+
break;
52+
case 0x03:
53+
decodedURL += "https://";
54+
break;
55+
default:
56+
decodedURL += m_eddystoneData.url[0];
57+
}
58+
59+
for (int i=1;i<lengthURL;i++) {
60+
if (m_eddystoneData.url[i]>33&&m_eddystoneData.url[i]<127) {
61+
decodedURL += m_eddystoneData.url[i];
62+
} else {
63+
switch (m_eddystoneData.url[i]) {
64+
case 0x00:
65+
decodedURL += ".com/";
66+
break;
67+
case 0x01:
68+
decodedURL += ".org/";
69+
break;
70+
case 0x02:
71+
decodedURL += ".edu/";
72+
break;
73+
case 0x03:
74+
decodedURL += ".net/";
75+
break;
76+
case 0x04:
77+
decodedURL += ".info/";
78+
break;
79+
case 0x05:
80+
decodedURL += ".biz/";
81+
break;
82+
case 0x06:
83+
decodedURL += ".gov/";
84+
break;
85+
case 0x07:
86+
decodedURL += ".com";
87+
break;
88+
case 0x08:
89+
decodedURL += ".org";
90+
break;
91+
case 0x09:
92+
decodedURL += ".edu";
93+
break;
94+
case 0x0A:
95+
decodedURL += ".net";
96+
break;
97+
case 0x0B:
98+
decodedURL += ".info";
99+
break;
100+
case 0x0C:
101+
decodedURL += ".biz";
102+
break;
103+
case 0x0D:
104+
decodedURL += ".gov";
105+
break;
106+
}
107+
}
108+
}
109+
110+
111+
return decodedURL;
112+
} // getDecodedURL
113+
114+
115+
116+
/**
117+
* Set the raw data for the beacon record.
118+
*/
119+
void BLEEddystoneURL::setData(std::string data) {
120+
if (data.length() > sizeof(m_eddystoneData)) {
121+
ESP_LOGE(LOG_TAG, "Unable to set the data ... length passed in was %d and max expected %d", data.length(), sizeof(m_eddystoneData));
122+
return;
123+
}
124+
memset(&m_eddystoneData, 0, sizeof(m_eddystoneData));
125+
memcpy(&m_eddystoneData, data.data(), data.length());
126+
lengthURL=data.length()-(sizeof(m_eddystoneData)-sizeof(m_eddystoneData.url));
127+
128+
} // setData
129+
130+
void BLEEddystoneURL::setUUID(BLEUUID l_uuid) {
131+
beconUUID = l_uuid.getNative()->uuid.uuid16;
132+
} // setUUID
133+
134+
void BLEEddystoneURL::setPower(int8_t advertisedTxPower) {
135+
m_eddystoneData.advertisedTxPower = advertisedTxPower;
136+
} // setPower
137+
138+
void BLEEddystoneURL::setURL(std::string url) {
139+
if (url.length() > sizeof(m_eddystoneData.url)) {
140+
ESP_LOGE(LOG_TAG, "Unable to set the url ... length passed in was %d and max expected %d", url.length(), sizeof(m_eddystoneData.url));
141+
return;
142+
}
143+
memset(m_eddystoneData.url, 0, sizeof(m_eddystoneData.url));
144+
memcpy(m_eddystoneData.url, url.data(), url.length());
145+
lengthURL=url.length();
146+
} // setURL
147+
148+
149+
#endif

cpp_utils/BLEEddystoneURL.h

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* BLEEddystoneURL.cpp
3+
*
4+
* Created on: Mar 12, 2018
5+
* Author: pcbreflux
6+
*/
7+
8+
#ifndef _BLEEddystoneURL_H_
9+
#define _BLEEddystoneURL_H_
10+
#include "BLEUUID.h"
11+
12+
#define EDDYSTONE_URL_FRAME_TYPE 0x10
13+
14+
/**
15+
* @brief Representation of a beacon.
16+
* See:
17+
* * https://github.com/google/eddystone
18+
*/
19+
class BLEEddystoneURL {
20+
private:
21+
uint16_t beconUUID;
22+
uint8_t lengthURL;
23+
struct {
24+
uint8_t frameType;
25+
int8_t advertisedTxPower;
26+
uint8_t url[16];
27+
} __attribute__((packed))m_eddystoneData;
28+
public:
29+
BLEEddystoneURL();
30+
std::string getData();
31+
BLEUUID getUUID();
32+
int8_t getPower();
33+
std::string getURL();
34+
std::string getDecodedURL();
35+
void setData(std::string data);
36+
void setUUID(BLEUUID l_uuid);
37+
void setPower(int8_t advertisedTxPower);
38+
void setURL(std::string url);
39+
40+
}; // BLEEddystoneURL
41+
42+
#endif /* _BLEEddystoneURL_H_ */

0 commit comments

Comments
 (0)