ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
bedjet_codec.cpp
Go to the documentation of this file.
1#include "bedjet_codec.h"
2#include <algorithm>
3#include <cstdio>
4#include <cstring>
5
6namespace esphome::bedjet {
7
9float bedjet_temp_to_f(const uint8_t temp) {
10 // BedJet temp is "C*2"; to get F, multiply by 0.9 (half 1.8) and add 32.
11 return 0.9f * temp + 32.0f;
12}
13
16 // So far no commands require more than 2 bytes of data
17 if (this->packet_.data_length > 2) {
18 ESP_LOGW(TAG, "Packet may be malformed");
19 }
20 for (int i = this->packet_.data_length; i < 2; i++) {
21 this->packet_.data[i] = '\0';
22 }
23 ESP_LOGV(TAG, "Created packet: %02X, %02X %02X", this->packet_.command, this->packet_.data[0], this->packet_.data[1]);
24 return &this->packet_;
25}
26
30 this->packet_.data_length = 1;
31 this->packet_.data[0] = button;
32 return this->clean_packet_();
33}
34
38 this->packet_.data_length = 1;
39 this->packet_.data[0] = temperature * 2;
40 return this->clean_packet_();
41}
42
46 this->packet_.data_length = 1;
47 this->packet_.data[0] = fan_step;
48 return this->clean_packet_();
49}
50
54 this->packet_.data_length = 2;
55 this->packet_.data[0] = hour;
56 this->packet_.data[1] = minute;
57 return this->clean_packet_();
58}
59
63 this->packet_.data_length = 2;
64 this->packet_.data[0] = hour;
65 this->packet_.data[1] = minute;
66 return this->clean_packet_();
67}
68
70void BedjetCodec::decode_extra(const uint8_t *data, uint16_t length) {
71 if (length < 5) {
72 ESP_LOGVV(TAG, "Received extra: %d bytes (too short)", length);
73 return;
74 }
75 ESP_LOGVV(TAG, "Received extra: %d bytes: %d %d %d %d", length, data[1], data[2], data[3], data[4]);
76 uint8_t offset = this->last_buffer_size_;
77 if (offset > 0 && length + offset <= sizeof(BedjetStatusPacket)) {
78 memcpy(((uint8_t *) (&this->buf_)) + offset, data, length);
79 ESP_LOGVV(TAG,
80 "Extra bytes: skip1=0x%08x, skip2=0x%04x, skip3=0x%02x; update phase=0x%02x, "
81 "flags=BedjetFlags <conn=%c, leds=%c, units=%c, mute=%c; packed=%02x>",
82 this->buf_.unused_1, this->buf_.unused_2, this->buf_.unused_3, this->buf_.update_phase,
83 this->buf_.flags.conn_test_passed ? '1' : '0', this->buf_.flags.leds_enabled ? '1' : '0',
84 this->buf_.flags.units_setup ? '1' : '0', this->buf_.flags.beeps_muted ? '1' : '0',
85 this->buf_.flags_packed);
86 } else {
87 ESP_LOGI(TAG, "Could not determine where to append to, last offset=%d, max size=%u, new size would be %d", offset,
88 sizeof(BedjetStatusPacket), length + offset);
89 }
90}
91
96bool BedjetCodec::decode_notify(const uint8_t *data, uint16_t length) {
97 if (length < 5) {
98 ESP_LOGW(TAG, "Received short packet: %d bytes", length);
99 return false;
100 }
101 ESP_LOGV(TAG, "Received: %d bytes: %d %d %d %d", length, data[1], data[2], data[3], data[4]);
102
103 if (data[1] == PACKET_FORMAT_V3_HOME && data[3] == PACKET_TYPE_STATUS) {
104 // Clear old buffer
105 memset(&this->buf_, 0, sizeof(BedjetStatusPacket));
106 // Copy new data into buffer
107 size_t copy_len = std::min(static_cast<size_t>(length), sizeof(BedjetStatusPacket));
108 memcpy(&this->buf_, data, copy_len);
109 this->last_buffer_size_ = copy_len;
110
111 // TODO: validate the packet checksum?
112 if (this->buf_.mode < 7 && this->buf_.target_temp_step >= 38 && this->buf_.target_temp_step <= 86 &&
113 this->buf_.actual_temp_step > 1 && this->buf_.actual_temp_step <= 100 && this->buf_.ambient_temp_step > 1 &&
114 this->buf_.ambient_temp_step <= 100) {
115 // and save it for the update() loop
116 this->status_packet_ = &this->buf_;
117 return this->buf_.is_partial;
118 } else {
119 this->status_packet_ = nullptr;
120 // TODO: log a warning if we detect that we connected to a non-V3 device.
121 ESP_LOGW(TAG, "Received potentially invalid packet (len %d):", length);
122 }
123 } else if (data[1] == PACKET_FORMAT_DEBUG || data[3] == PACKET_TYPE_DEBUG) {
124 // We don't actually know the packet format for this. Dump packets to log, in case a pattern presents itself.
125 if (length >= 13) {
126 ESP_LOGVV(TAG,
127 "received DEBUG packet: set1=%01fF, set2=%01fF, air=%01fF; [7]=%d, [8]=%d, [9]=%d, [10]=%d, [11]=%d, "
128 "[12]=%d, [-1]=%d",
129 bedjet_temp_to_f(data[4]), bedjet_temp_to_f(data[5]), bedjet_temp_to_f(data[6]), data[7], data[8],
130 data[9], data[10], data[11], data[12], data[length - 1]);
131 }
132
133 if (this->has_status() && length >= 7) {
134 this->status_packet_->ambient_temp_step = data[6];
135 }
136 } else {
137 // TODO: log a warning if we detect that we connected to a non-V3 device.
138 }
139
140 return false;
141}
142
144bool BedjetCodec::compare(const uint8_t *data, uint16_t length) {
145 if (data == nullptr) {
146 return false;
147 }
148
149 if (length < 17) {
150 // New packet looks small, skip it.
151 return false;
152 }
153
155 this->buf_.packet_type != PACKET_TYPE_STATUS) { // No last seen packet, so take the new one.
156 return true;
157 }
158
159 if (data[1] != PACKET_FORMAT_V3_HOME || data[3] != PACKET_TYPE_STATUS) { // New packet is not a v3 status, skip it.
160 return false;
161 }
162
163 // Now coerce it to a status packet and compare some key fields
164 const BedjetStatusPacket *test = reinterpret_cast<const BedjetStatusPacket *>(data);
165 // These are fields that will only change due to explicit action.
166 // That is why we do not check ambient or actual temp here, because those are environmental.
167 bool explicit_fields_changed = this->buf_.mode != test->mode || this->buf_.fan_step != test->fan_step ||
169
170 return explicit_fields_changed;
171}
172
174float bedjet_temp_to_c(uint8_t temp) {
175 // BedJet temp is "C*2"; to get C, divide by 2.
176 return temp / 2.0f;
177}
178
179} // namespace esphome::bedjet
uint8_t fan_step
BedJet fan speed; value is in the 0-19 range, representing 5% increments (5%-100%): 5 + 5 /< * fan_st...
BedjetPacket * get_set_target_temp_request(float temperature)
Returns a BedjetPacket that will set the device's target temperature.
bool compare(const uint8_t *data, uint16_t length)
void decode_extra(const uint8_t *data, uint16_t length)
Decodes the extra bytes that were received after being notified with a partial packet.
BedjetPacket * clean_packet_()
Cleans up the packet before sending.
BedjetStatusPacket * status_packet_
bool decode_notify(const uint8_t *data, uint16_t length)
Decodes the incoming status packet received on the BEDJET_STATUS_UUID.
BedjetPacket * get_set_runtime_remaining_request(uint8_t hour, uint8_t minute)
Returns a BedjetPacket that will set the device's remaining runtime.
BedjetPacket * get_button_request(BedjetButton button)
Returns a BedjetPacket that will initiate a BedjetButton press.
BedjetPacket * get_set_time_request(uint8_t hour, uint8_t minute)
Returns a BedjetPacket that will set the device's current time.
BedjetPacket * get_set_fan_speed_request(uint8_t fan_step)
Returns a BedjetPacket that will set the device's target fan speed.
uint8_t minute
uint8_t hour
float bedjet_temp_to_c(uint8_t temp)
Converts a BedJet temp step into degrees Celsius.
float bedjet_temp_to_f(const uint8_t temp)
Converts a BedJet temp step into degrees Fahrenheit.
The format of a BedJet V3 status packet.
uint8_t target_temp_step
Target temp that the BedJet will try to heat to. See actual_temp_step.
uint8_t fan_step
BedJet fan speed; value is in the 0-19 range, representing 5% increments (5%-100%): 5 + 5 /< * fan_st...
bool is_partial
1 indicates that this is a partial packet, and more data can be read directly from the characteristic...
BedjetMode mode
BedJet operating mode.
BedjetPacketFormat packet_format
BedjetPacketFormat::PACKET_FORMAT_V3_HOME for BedJet V3 status packet format.
uint8_t ambient_temp_step
Current ambient air temp.
uint16_t temperature
Definition sun_gtil2.cpp:12
uint16_t length
Definition tt21100.cpp:0