Skip to content

Commit e0fe166

Browse files
committed
[BREAKING] - Refactor NimBLEEddystoneTLM
* `NimBLEEddystoneTLM::BeaconData` struct is now public and usable by the application. * `NimBLEEddystoneTLM::setTemp` now takes an `int16_t` parameter instead of float to be friendly to devices without floating point support. * `NimBLEEddystoneTLM::getTemp` now returns `int16_t` to work with devices that don't have floating point support. * `NimBLEEddystoneTLM::setData` now takes a reference to * `NimBLEEddystoneTLM::BeaconData` instead of `std::string`. * `NimBLEEddystoneTLM::getData` now returns a reference to * `NimBLEEddystoneTLM::BeaconData` instead of `std::string`.
1 parent 308b81e commit e0fe166

File tree

2 files changed

+126
-135
lines changed

2 files changed

+126
-135
lines changed

src/NimBLEEddystoneTLM.cpp

Lines changed: 86 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -13,42 +13,26 @@
1313
*/
1414

1515
#include "nimconfig.h"
16-
#if defined(CONFIG_BT_ENABLED)
16+
#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER)
1717

18-
#include "NimBLEEddystoneTLM.h"
19-
#include "NimBLELog.h"
18+
# include "NimBLEEddystoneTLM.h"
19+
# include "NimBLEUUID.h"
20+
# include "NimBLELog.h"
2021

21-
#include <stdio.h>
22-
#include <cstring>
23-
24-
#define ENDIAN_CHANGE_U16(x) ((((x)&0xFF00)>>8) + (((x)&0xFF)<<8))
25-
#define ENDIAN_CHANGE_U32(x) ((((x)&0xFF000000)>>24) + (((x)&0x00FF0000)>>8)) + ((((x)&0xFF00)<<8) + (((x)&0xFF)<<24))
26-
27-
static const char LOG_TAG[] = "NimBLEEddystoneTLM";
28-
29-
/**
30-
* @brief Construct a default EddystoneTLM beacon object.
31-
*/
32-
NimBLEEddystoneTLM::NimBLEEddystoneTLM() {
33-
beaconUUID = 0xFEAA;
34-
m_eddystoneData.frameType = EDDYSTONE_TLM_FRAME_TYPE;
35-
m_eddystoneData.version = 0;
36-
m_eddystoneData.volt = 3300; // 3300mV = 3.3V
37-
m_eddystoneData.temp = (uint16_t) ((float) 23.00 * 256); // 8.8 fixed format
38-
m_eddystoneData.advCount = 0;
39-
m_eddystoneData.tmil = 0;
40-
} // NimBLEEddystoneTLM
22+
# define ENDIAN_CHANGE_U16(x) ((((x) & 0xFF00) >> 8) + (((x) & 0xFF) << 8))
23+
# define ENDIAN_CHANGE_U32(x) \
24+
((((x) & 0xFF000000) >> 24) + (((x) & 0x00FF0000) >> 8)) + ((((x) & 0xFF00) << 8) + (((x) & 0xFF) << 24))
4125

26+
static const char* LOG_TAG = "NimBLEEddystoneTLM";
4227

4328
/**
4429
* @brief Retrieve the data that is being advertised.
4530
* @return The advertised data.
4631
*/
47-
std::string NimBLEEddystoneTLM::getData() {
48-
return std::string((char*) &m_eddystoneData, sizeof(m_eddystoneData));
32+
const NimBLEEddystoneTLM::BeaconData NimBLEEddystoneTLM::getData() {
33+
return m_eddystoneData;
4934
} // getData
5035

51-
5236
/**
5337
* @brief Get the UUID being advertised.
5438
* @return The UUID advertised.
@@ -57,7 +41,6 @@ NimBLEUUID NimBLEEddystoneTLM::getUUID() {
5741
return NimBLEUUID(beaconUUID);
5842
} // getUUID
5943

60-
6144
/**
6245
* @brief Get the version being advertised.
6346
* @return The version number.
@@ -66,7 +49,6 @@ uint8_t NimBLEEddystoneTLM::getVersion() {
6649
return m_eddystoneData.version;
6750
} // getVersion
6851

69-
7052
/**
7153
* @brief Get the battery voltage.
7254
* @return The battery voltage.
@@ -75,13 +57,12 @@ uint16_t NimBLEEddystoneTLM::getVolt() {
7557
return ENDIAN_CHANGE_U16(m_eddystoneData.volt);
7658
} // getVolt
7759

78-
7960
/**
8061
* @brief Get the temperature being advertised.
8162
* @return The temperature value.
8263
*/
83-
float NimBLEEddystoneTLM::getTemp() {
84-
return (int16_t)ENDIAN_CHANGE_U16(m_eddystoneData.temp) / 256.0f;
64+
int16_t NimBLEEddystoneTLM::getTemp() {
65+
return ENDIAN_CHANGE_U16(m_eddystoneData.temp);
8566
} // getTemp
8667

8768
/**
@@ -92,7 +73,6 @@ uint32_t NimBLEEddystoneTLM::getCount() {
9273
return ENDIAN_CHANGE_U32(m_eddystoneData.advCount);
9374
} // getCount
9475

95-
9676
/**
9777
* @brief Get the advertisement time.
9878
* @return The advertisement time.
@@ -101,89 +81,98 @@ uint32_t NimBLEEddystoneTLM::getTime() {
10181
return (ENDIAN_CHANGE_U32(m_eddystoneData.tmil)) / 10;
10282
} // getTime
10383

104-
10584
/**
10685
* @brief Get a string representation of the beacon.
10786
* @return The string representation.
10887
*/
10988
std::string NimBLEEddystoneTLM::toString() {
110-
std::string out = "";
111-
uint32_t rawsec = ENDIAN_CHANGE_U32(m_eddystoneData.tmil);
112-
char val[12];
113-
114-
out += "Version "; // + std::string(m_eddystoneData.version);
115-
snprintf(val, sizeof(val), "%d", m_eddystoneData.version);
116-
out += val;
117-
out += "\n";
118-
out += "Battery Voltage "; // + ENDIAN_CHANGE_U16(m_eddystoneData.volt);
119-
snprintf(val, sizeof(val), "%d", ENDIAN_CHANGE_U16(m_eddystoneData.volt));
120-
out += val;
121-
out += " mV\n";
122-
123-
out += "Temperature ";
124-
snprintf(val, sizeof(val), "%.2f", ENDIAN_CHANGE_U16(m_eddystoneData.temp) / 256.0f);
125-
out += val;
126-
out += " C\n";
127-
128-
out += "Adv. Count ";
129-
snprintf(val, sizeof(val), "%" PRIu32, ENDIAN_CHANGE_U32(m_eddystoneData.advCount));
130-
out += val;
131-
out += "\n";
132-
133-
out += "Time in seconds ";
134-
snprintf(val, sizeof(val), "%" PRIu32, rawsec/10);
135-
out += val;
136-
out += "\n";
137-
138-
out += "Time ";
139-
140-
snprintf(val, sizeof(val), "%04" PRIu32, rawsec / 864000);
141-
out += val;
142-
out += ".";
143-
144-
snprintf(val, sizeof(val), "%02" PRIu32, (rawsec / 36000) % 24);
145-
out += val;
146-
out += ":";
147-
148-
snprintf(val, sizeof(val), "%02" PRIu32, (rawsec / 600) % 60);
149-
out += val;
150-
out += ":";
151-
152-
snprintf(val, sizeof(val), "%02" PRIu32, (rawsec / 10) % 60);
153-
out += val;
154-
out += "\n";
155-
156-
return out;
89+
std::string out = "";
90+
uint32_t rawsec = ENDIAN_CHANGE_U32(m_eddystoneData.tmil);
91+
char val[12];
92+
93+
out += "Version ";
94+
snprintf(val, sizeof(val), "%d", m_eddystoneData.version);
95+
out += val;
96+
out += "\n";
97+
out += "Battery Voltage ";
98+
snprintf(val, sizeof(val), "%d", ENDIAN_CHANGE_U16(m_eddystoneData.volt));
99+
out += val;
100+
out += " mV\n";
101+
102+
out += "Temperature ";
103+
uint8_t intTemp = m_eddystoneData.temp / 256;
104+
uint8_t frac = m_eddystoneData.temp % 256 * 100 / 256;
105+
snprintf(val, sizeof(val), "%d.%d", intTemp, frac);
106+
out += val;
107+
out += " C\n";
108+
109+
out += "Adv. Count ";
110+
snprintf(val, sizeof(val), "%" PRIu32, ENDIAN_CHANGE_U32(m_eddystoneData.advCount));
111+
out += val;
112+
out += "\n";
113+
114+
out += "Time in seconds ";
115+
snprintf(val, sizeof(val), "%" PRIu32, rawsec / 10);
116+
out += val;
117+
out += "\n";
118+
119+
out += "Time ";
120+
121+
snprintf(val, sizeof(val), "%04" PRIu32, rawsec / 864000);
122+
out += val;
123+
out += ".";
124+
125+
snprintf(val, sizeof(val), "%02" PRIu32, (rawsec / 36000) % 24);
126+
out += val;
127+
out += ":";
128+
129+
snprintf(val, sizeof(val), "%02" PRIu32, (rawsec / 600) % 60);
130+
out += val;
131+
out += ":";
132+
133+
snprintf(val, sizeof(val), "%02" PRIu32, (rawsec / 10) % 60);
134+
out += val;
135+
out += "\n";
136+
137+
return out;
157138
} // toString
158139

159-
160140
/**
161141
* @brief Set the raw data for the beacon advertisement.
162-
* @param [in] data The raw data to advertise.
163-
*/
164-
void NimBLEEddystoneTLM::setData(const std::string &data) {
165-
if (data.length() != sizeof(m_eddystoneData)) {
166-
NIMBLE_LOGE(LOG_TAG, "Unable to set the data ... length passed in was %d and expected %d",
167-
data.length(), sizeof(m_eddystoneData));
142+
* @param [in] data A pointer to the data to advertise.
143+
* @param [in] length The length of the data.
144+
*/
145+
void NimBLEEddystoneTLM::setData(const uint8_t* data, uint8_t length) {
146+
if (length != sizeof(m_eddystoneData)) {
147+
NIMBLE_LOGE(LOG_TAG,
148+
"Unable to set the data ... length passed in was %d and expected %d",
149+
length,
150+
sizeof(m_eddystoneData));
168151
return;
169152
}
170-
memcpy(&m_eddystoneData, data.data(), data.length());
153+
memcpy(&m_eddystoneData, data, length);
171154
} // setData
172155

156+
/**
157+
* @brief Set the raw data for the beacon advertisement.
158+
* @param [in] data The raw data to advertise.
159+
*/
160+
void NimBLEEddystoneTLM::setData(const NimBLEEddystoneTLM::BeaconData& data) {
161+
m_eddystoneData = data;
162+
} // setData
173163

174164
/**
175165
* @brief Set the UUID to advertise.
176-
* @param [in] l_uuid The UUID.
166+
* @param [in] uuid The UUID.
177167
*/
178-
void NimBLEEddystoneTLM::setUUID(const NimBLEUUID &l_uuid) {
179-
if (l_uuid.bitSize() != 16) {
168+
void NimBLEEddystoneTLM::setUUID(const NimBLEUUID& uuid) {
169+
if (uuid.bitSize() != 16) {
180170
NIMBLE_LOGE(LOG_TAG, "UUID must be 16 bits");
181171
return;
182172
}
183-
beaconUUID = *reinterpret_cast<const uint16_t*>(l_uuid.getValue());
173+
beaconUUID = *reinterpret_cast<const uint16_t*>(uuid.getValue());
184174
} // setUUID
185175

186-
187176
/**
188177
* @brief Set the version to advertise.
189178
* @param [in] version The version number.
@@ -192,7 +181,6 @@ void NimBLEEddystoneTLM::setVersion(uint8_t version) {
192181
m_eddystoneData.version = version;
193182
} // setVersion
194183

195-
196184
/**
197185
* @brief Set the battery voltage to advertise.
198186
* @param [in] volt The voltage in millivolts.
@@ -201,16 +189,14 @@ void NimBLEEddystoneTLM::setVolt(uint16_t volt) {
201189
m_eddystoneData.volt = volt;
202190
} // setVolt
203191

204-
205192
/**
206193
* @brief Set the temperature to advertise.
207-
* @param [in] temp The temperature value.
194+
* @param [in] temp The temperature value in 8.8 fixed point format.
208195
*/
209-
void NimBLEEddystoneTLM::setTemp(float temp) {
210-
m_eddystoneData.temp = ENDIAN_CHANGE_U16((int16_t)(temp * 256.0f));
196+
void NimBLEEddystoneTLM::setTemp(int16_t temp) {
197+
m_eddystoneData.temp = temp;
211198
} // setTemp
212199

213-
214200
/**
215201
* @brief Set the advertisement count.
216202
* @param [in] advCount The advertisement number.
@@ -219,7 +205,6 @@ void NimBLEEddystoneTLM::setCount(uint32_t advCount) {
219205
m_eddystoneData.advCount = advCount;
220206
} // setCount
221207

222-
223208
/**
224209
* @brief Set the advertisement time.
225210
* @param [in] tmil The advertisement time in milliseconds.
@@ -228,4 +213,4 @@ void NimBLEEddystoneTLM::setTime(uint32_t tmil) {
228213
m_eddystoneData.tmil = tmil;
229214
} // setTime
230215

231-
#endif
216+
#endif // CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_BROADCASTER

src/NimBLEEddystoneTLM.h

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -12,50 +12,56 @@
1212
* Author: pcbreflux
1313
*/
1414

15-
#ifndef _NimBLEEddystoneTLM_H_
16-
#define _NimBLEEddystoneTLM_H_
15+
#ifndef NIMBLE_CPP_EDDYSTONETLM_H_
16+
#define NIMBLE_CPP_EDDYSTONETLM_H_
1717

18-
#include "NimBLEUUID.h"
18+
#include "nimconfig.h"
19+
#if defined(CONFIG_BT_ENABLED) && defined(CONFIG_BT_NIMBLE_ROLE_BROADCASTER)
1920

20-
#include <string>
21+
class NimBLEUUID;
2122

22-
#define EDDYSTONE_TLM_FRAME_TYPE 0x20
23+
# include <string>
24+
25+
# define EDDYSTONE_TLM_FRAME_TYPE 0x20
2326

2427
/**
2528
* @brief Representation of a beacon.
2629
* See:
2730
* * https://github.com/google/eddystone
2831
*/
2932
class NimBLEEddystoneTLM {
30-
public:
31-
NimBLEEddystoneTLM();
32-
std::string getData();
33-
NimBLEUUID 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(const std::string &data);
41-
void setUUID(const NimBLEUUID &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-
private:
49-
uint16_t beaconUUID;
50-
struct {
51-
uint8_t frameType;
52-
uint8_t version;
53-
uint16_t volt;
54-
uint16_t temp;
55-
uint32_t advCount;
56-
uint32_t tmil;
57-
} __attribute__((packed)) m_eddystoneData;
33+
public:
34+
struct BeaconData {
35+
uint8_t frameType{EDDYSTONE_TLM_FRAME_TYPE};
36+
uint8_t version{0};
37+
uint16_t volt{3300};
38+
uint16_t temp{23 * 256};
39+
uint32_t advCount{0};
40+
uint32_t tmil{0};
41+
} __attribute__((packed));
42+
43+
const BeaconData getData();
44+
NimBLEUUID getUUID();
45+
uint8_t getVersion();
46+
uint16_t getVolt();
47+
int16_t getTemp();
48+
uint32_t getCount();
49+
uint32_t getTime();
50+
std::string toString();
51+
void setData(const uint8_t* data, uint8_t length);
52+
void setData(const BeaconData& data);
53+
void setUUID(const NimBLEUUID& l_uuid);
54+
void setVersion(uint8_t version);
55+
void setVolt(uint16_t volt);
56+
void setTemp(int16_t temp);
57+
void setCount(uint32_t advCount);
58+
void setTime(uint32_t tmil);
59+
60+
private:
61+
uint16_t beaconUUID{0xFEAA};
62+
BeaconData m_eddystoneData;
5863

5964
}; // NimBLEEddystoneTLM
6065

61-
#endif /* _NimBLEEddystoneTLM_H_ */
66+
#endif // CONFIG_BT_ENABLED && CONFIG_BT_NIMBLE_ROLE_BROADCASTER
67+
#endif // NIMBLE_CPP_EDDYSTONETLM_H_

0 commit comments

Comments
 (0)