Skip to content

Commit 368dff9

Browse files
94xhnh2zero
authored andcommitted
Fix NimBLEEddystoneTLM byte-order mismatch between getters and setters (#431)
* Fix NimBLEEddystoneTLM byte-order mismatch between getters and setters m_eddystoneData must always hold Eddystone-TLM wire-format (big-endian) bytes, since getData() returns it directly for the caller to memcpy straight into the BLE advertisement payload (see the BLE_EddystoneTLM_Beacon example), and setData()/getData() already treat it as raw wire bytes. The getters (getVolt/getTemp) correctly un-swap on read, but the setters (setVolt/setTemp/setCount/setTime) stored their host-order input directly with no swap, and the BeaconData default member initializers were host-order values instead of pre-swapped wire-format values. This breaks the setter/getter round trip for any value set locally (as opposed to arriving over the air via setData()), and means a freshly constructed object reports a garbage battery voltage. toString() also failed to un-swap the temperature field at all, and used unsigned division/modulo on a signed 8.8 fixed-point value, producing garbage output for negative temperatures. This mirrors the existing, correct pattern already used by the sibling NimBLEBeacon class, which swaps in its setters (setMajor/setMinor/ setManufacturerId). Fix: - setVolt/setTemp/setCount/setTime now apply ENDIAN_CHANGE_U16/U32 to their input before storing, matching what the getters already assume. - BeaconData's default volt/temp member initializers are now the wire-format (byte-swapped) encoding of the intended defaults (3300 mV, 23.0C), so a freshly constructed object reports correct values without calling a setter first. - toString() now swaps the temperature field the same way it already swaps voltage, and computes the displayed magnitude from a signed value with explicit sign handling instead of unsigned arithmetic on the raw field.
1 parent 9bc4b9f commit 368dff9

2 files changed

Lines changed: 12 additions & 10 deletions

File tree

src/NimBLEEddystoneTLM.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,12 @@ std::string NimBLEEddystoneTLM::toString() {
101101
out += val;
102102
out += " mV\n";
103103

104-
out += "Temperature ";
105-
uint8_t intTemp = m_eddystoneData.temp / 256;
106-
uint8_t frac = m_eddystoneData.temp % 256 * 100 / 256;
107-
snprintf(val, sizeof(val), "%d.%d", intTemp, frac);
104+
out += "Temperature ";
105+
int16_t temp = ENDIAN_CHANGE_U16(m_eddystoneData.temp);
106+
uint32_t absTemp = temp < 0 ? -static_cast<int32_t>(temp) : temp;
107+
uint8_t intTemp = absTemp / 256;
108+
uint8_t frac = absTemp % 256 * 100 / 256;
109+
snprintf(val, sizeof(val), "%s%d.%02d", (temp < 0 ? "-" : ""), intTemp, frac);
108110
out += val;
109111
out += " C\n";
110112

@@ -188,31 +190,31 @@ void NimBLEEddystoneTLM::setVersion(uint8_t version) {
188190
* @param [in] volt The voltage in millivolts.
189191
*/
190192
void NimBLEEddystoneTLM::setVolt(uint16_t volt) {
191-
m_eddystoneData.volt = volt;
193+
m_eddystoneData.volt = ENDIAN_CHANGE_U16(volt);
192194
} // setVolt
193195

194196
/**
195197
* @brief Set the temperature to advertise.
196198
* @param [in] temp The temperature value in 8.8 fixed point format.
197199
*/
198200
void NimBLEEddystoneTLM::setTemp(int16_t temp) {
199-
m_eddystoneData.temp = temp;
201+
m_eddystoneData.temp = ENDIAN_CHANGE_U16(temp);
200202
} // setTemp
201203

202204
/**
203205
* @brief Set the advertisement count.
204206
* @param [in] advCount The advertisement number.
205207
*/
206208
void NimBLEEddystoneTLM::setCount(uint32_t advCount) {
207-
m_eddystoneData.advCount = advCount;
209+
m_eddystoneData.advCount = ENDIAN_CHANGE_U32(advCount);
208210
} // setCount
209211

210212
/**
211213
* @brief Set the advertisement time.
212214
* @param [in] tmil The advertisement time in milliseconds.
213215
*/
214216
void NimBLEEddystoneTLM::setTime(uint32_t tmil) {
215-
m_eddystoneData.tmil = tmil;
217+
m_eddystoneData.tmil = ENDIAN_CHANGE_U32(tmil);
216218
} // setTime
217219

218220
#endif // CONFIG_BT_NIMBLE_ENABLED && MYNEWT_VAL(BLE_ROLE_BROADCASTER)

src/NimBLEEddystoneTLM.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ class NimBLEEddystoneTLM {
3737
struct BeaconData {
3838
uint8_t frameType{EDDYSTONE_TLM_FRAME_TYPE};
3939
uint8_t version{0};
40-
uint16_t volt{3300};
41-
uint16_t temp{23 * 256};
40+
uint16_t volt{0xE40C}; // Big-endian (wire format) encoding of 3300 mV; getVolt() swaps back to host order.
41+
uint16_t temp{0x0017}; // Big-endian (wire format) encoding of 23.0C (23 * 256); getTemp() swaps back to host order.
4242
uint32_t advCount{0};
4343
uint32_t tmil{0};
4444
} __attribute__((packed));

0 commit comments

Comments
 (0)