From e3f2ca70dce1666319bea9a7b6781a6f80b5ad62 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 12 Feb 2023 11:17:14 +0100 Subject: [PATCH] add comments. --- components/truma_inetbox/LinBusProtocol.cpp | 31 +++++++++++++++----- components/truma_inetbox/LinBusProtocol.h | 1 + components/truma_inetbox/TrumaiNetBoxApp.cpp | 28 ++++++++++-------- 3 files changed, 40 insertions(+), 20 deletions(-) diff --git a/components/truma_inetbox/LinBusProtocol.cpp b/components/truma_inetbox/LinBusProtocol.cpp index fe22e6f..4436961 100644 --- a/components/truma_inetbox/LinBusProtocol.cpp +++ b/components/truma_inetbox/LinBusProtocol.cpp @@ -87,6 +87,7 @@ void LinBusProtocol::lin_message_recieved_diagnostic_(const u_int8_t *message, u // End any open Multi frame mode message this->multi_pdu_message_expected_size_ = 0; this->multi_pdu_message_len_ = 0; + this->multi_pdu_message_frame_counter_ = 0; } message_length = protocol_control_information; service_identifier = message[2]; @@ -111,6 +112,7 @@ void LinBusProtocol::lin_message_recieved_diagnostic_(const u_int8_t *message, u } this->multi_pdu_message_expected_size_ = message_length; this->multi_pdu_message_len_ = 0; + this->multi_pdu_message_frame_counter_ = 1; for (size_t i = 3; i < 8; i++) { this->multi_pdu_message_[this->multi_pdu_message_len_++] = message[i]; } @@ -122,6 +124,17 @@ void LinBusProtocol::lin_message_recieved_diagnostic_(const u_int8_t *message, u // ignore, because i don't await a consecutive frame return; } + u_int8_t frame_counter = protocol_control_information & 0x0F; + if (frame_counter != this->multi_pdu_message_frame_counter_) { + // ignore, because i don't await this consecutive frame + return; + } + this->multi_pdu_message_frame_counter_++; + if (this->multi_pdu_message_frame_counter_ > 0x0F) { + // Frame counter has only 4 bit and wraps around. + this->multi_pdu_message_frame_counter_ = 0x00; + } + this->lin_message_recieved_diagnostic_multi_(message, length, protocol_control_information); // Message is handeld return; @@ -190,50 +203,52 @@ void LinBusProtocol::lin_message_recieved_diagnostic_(const u_int8_t *message, u void LinBusProtocol::lin_message_recieved_diagnostic_multi_(const u_int8_t *message, u_int8_t length, u_int8_t protocol_control_information) { - u_int8_t frame_counter = protocol_control_information - 0x21; - for (size_t i = 2; i < 8; i++) { + // Copy recieved message over to `multi_pdu_message_` buffer. + for (u_int8_t i = 2; i < 8; i++) { if (this->multi_pdu_message_len_ < this->multi_pdu_message_expected_size_) { this->multi_pdu_message_[this->multi_pdu_message_len_++] = message[i]; } } + if (this->multi_pdu_message_len_ == this->multi_pdu_message_expected_size_) { ESP_LOGD(TAG, "Multi package request %s", format_hex_pretty(this->multi_pdu_message_, this->multi_pdu_message_len_).c_str()); u_int8_t answer_len = 0; + // Ask handling class what to answer to this request. auto answer = this->lin_multiframe_recieved(this->multi_pdu_message_, this->multi_pdu_message_len_, &answer_len); if (answer_len > 0) { ESP_LOGD(TAG, "Multi package response %s", format_hex_pretty(answer, answer_len).c_str()); + std::array response = this->lin_empty_response_; if (answer_len <= 6) { - // Single Frame response - std::array response = this->lin_empty_response_; + // Single Frame response - first frame response[0] = this->lin_node_address_; response[1] = answer_len; /* bytes length */ response[2] = answer[0] | LIN_SID_RESPONSE; - for (size_t i = 1; i < answer_len; i++) { + for (u_int8_t i = 1; i < answer_len; i++) { response[i + 2] = answer[i]; } this->prepare_update_msg_(response); } else { // Multi Frame response - std::array response = this->lin_empty_response_; response[0] = this->lin_node_address_; response[1] = 0x10 | ((answer_len >> 8) & 0x0F); response[2] = answer_len & 0xFF; response[3] = answer[0] | LIN_SID_RESPONSE; - for (size_t i = 1; i < 5; i++) { + for (u_int8_t i = 1; i < 5; i++) { response[i + 3] = answer[i]; } this->prepare_update_msg_(response); + // Multi Frame response - consecutive frame u_int16_t answer_position = 5; // The first 5 bytes are sent in First frame of multi frame response. u_int8_t answer_frame_counter = 0; // Each answer frame can contain 6 bytes while (answer_position < answer_len) { response = this->lin_empty_response_; response[0] = this->lin_node_address_; response[1] = ((answer_frame_counter + 1) & 0x0F) | 0x20; - for (size_t i = 0; i < 6; i++) { + for (u_int8_t i = 0; i < 6; i++) { if (answer_position < answer_len) { response[i + 2] = answer[answer_position++]; } diff --git a/components/truma_inetbox/LinBusProtocol.h b/components/truma_inetbox/LinBusProtocol.h index 08a17fb..969c44b 100644 --- a/components/truma_inetbox/LinBusProtocol.h +++ b/components/truma_inetbox/LinBusProtocol.h @@ -31,6 +31,7 @@ class LinBusProtocol : public LinBusListener { u_int16_t multi_pdu_message_expected_size_ = 0; u_int8_t multi_pdu_message_len_ = 0; + u_int8_t multi_pdu_message_frame_counter_ = 0; u_int8_t multi_pdu_message_[64]; void lin_message_recieved_diagnostic_(const u_int8_t *message, u_int8_t length); void lin_message_recieved_diagnostic_multi_(const u_int8_t *message, u_int8_t length, diff --git a/components/truma_inetbox/TrumaiNetBoxApp.cpp b/components/truma_inetbox/TrumaiNetBoxApp.cpp index 5518829..626e691 100644 --- a/components/truma_inetbox/TrumaiNetBoxApp.cpp +++ b/components/truma_inetbox/TrumaiNetBoxApp.cpp @@ -230,16 +230,18 @@ const u_int8_t *TrumaiNetBoxApp::lin_multiframe_recieved(const u_int8_t *message } if (message[0] == LIN_SID_READ_STATE_BUFFER) { + ESP_LOGI(TAG, "Requested read update"); // Example: BA.00.1F.00.1E.00.00.22.FF.FF.FF (11) memset(response, 0, sizeof(response)); auto response_frame = reinterpret_cast(response); // The order must match with the method 'has_update_to_submit_'. if (this->init_recieved_ == 0) { + ESP_LOGI(TAG, "Sending init"); status_frame_create_init(response_frame, return_len, this->message_counter++); return response; - } - if (this->update_status_heater_unsubmitted_) { + } else if (this->update_status_heater_unsubmitted_) { + ESP_LOGI(TAG, "Sending heater update"); status_frame_create_update_heater( response_frame, return_len, this->message_counter++, this->update_status_heater_.target_temp_room, this->update_status_heater_.target_temp_water, this->update_status_heater_.heating_mode, @@ -250,8 +252,8 @@ const u_int8_t *TrumaiNetBoxApp::lin_multiframe_recieved(const u_int8_t *message this->update_status_heater_unsubmitted_ = false; this->update_status_heater_stale_ = true; return response; - } - if (this->update_status_timer_unsubmitted_) { + } else if (this->update_status_timer_unsubmitted_) { + ESP_LOGI(TAG, "Sending timer update"); status_frame_create_update_timer( response_frame, return_len, this->message_counter++, this->update_status_timer_.timer_resp_active, this->update_status_timer_.timer_resp_start_hours, this->update_status_timer_.timer_resp_start_minutes, @@ -265,8 +267,8 @@ const u_int8_t *TrumaiNetBoxApp::lin_multiframe_recieved(const u_int8_t *message this->update_status_timer_unsubmitted_ = false; this->update_status_timer_stale_ = true; return response; - } - if (this->update_status_clock_unsubmitted_) { + } else if (this->update_status_clock_unsubmitted_) { + ESP_LOGI(TAG, "Sending clock update"); // read time live auto now = this->time_->now(); @@ -275,6 +277,8 @@ const u_int8_t *TrumaiNetBoxApp::lin_multiframe_recieved(const u_int8_t *message this->update_status_clock_unsubmitted_ = false; return response; + } else { + ESP_LOGW(TAG, "CP Plus asks for an update, but I have none."); } } @@ -298,7 +302,7 @@ const u_int8_t *TrumaiNetBoxApp::lin_multiframe_recieved(const u_int8_t *message if (header->message_type == STATUS_FRAME_HEATER && header->message_length == sizeof(StatusFrameHeater)) { ESP_LOGI(TAG, "StatusFrameHeater"); // Example: - // SID<---------PREAMBLE --------->|<---MSG HEAD --->|tRoom|mo| |elecA|tWate|elecB|mi|mi|cWate|cRoom|st|err | | + // SID<---------PREAMBLE---------->|<---MSG_HEAD---->|tRoom|mo| |elecA|tWate|elecB|mi|mi|cWate|cRoom|st|err | | // BB.00.1F.00.1E.00.00.22.FF.FF.FF.54.01.14.33.00.12.00.00.00.00.00.00.00.00.00.00.01.01.CC.0B.6C.0B.00.00.00.00 this->status_heater_ = statusFrame->inner.heater; this->status_heater_valid_ = true; @@ -309,7 +313,7 @@ const u_int8_t *TrumaiNetBoxApp::lin_multiframe_recieved(const u_int8_t *message } else if (header->message_type == STATUS_FRAME_TIMER && header->message_length == sizeof(StatusFrameTimer)) { ESP_LOGI(TAG, "StatusFrameTimer"); // EXAMPLE: - // SID<---------PREAMBLE --------->|<---MSG HEAD --->|tRoom|mo| |elecA|tWate|elecB|mi|mi|<--response-->| | |on|start|stop | + // SID<---------PREAMBLE---------->|<---MSG_HEAD---->|tRoom|mo|??|elecA|tWate|elecB|mi|mi|<--response-->|??|??|on|start|stop-| // BB.00.1F.00.1E.00.00.22.FF.FF.FF.54.01.18.3D.00.1D.18.0B.01.00.00.00.00.00.00.00.01.01.00.00.00.00.00.00.00.01.00.08.00.09 // BB.00.1F.00.1E.00.00.22.FF.FF.FF.54.01.18.3D.00.13.18.0B.0B.00.00.00.00.00.00.00.01.01.00.00.00.00.00.00.00.01.00.08.00.09 this->status_timer_ = statusFrame->inner.timer; @@ -328,7 +332,7 @@ const u_int8_t *TrumaiNetBoxApp::lin_multiframe_recieved(const u_int8_t *message } else if (header->message_type == STATUS_FRAME_RESPONSE_ACK && header->message_length == sizeof(StatusFrameResponseAck)) { // Example: - // SID<---------PREAMBLE --------->|<---MSG HEAD --->| + // SID<---------PREAMBLE---------->|<---MSG_HEAD---->| // BB.00.1F.00.1E.00.00.22.FF.FF.FF.54.01.02.0D.01.98.02.00 auto data = statusFrame->inner.responseAck; @@ -350,7 +354,7 @@ const u_int8_t *TrumaiNetBoxApp::lin_multiframe_recieved(const u_int8_t *message } else if (header->message_type == STATUS_FRAME_CLOCK && header->message_length == sizeof(StatusFrameClock)) { ESP_LOGI(TAG, "StatusFrameClock"); // Example: - // SID<---------PREAMBLE --------->|<---MSG HEAD --->| + // SID<---------PREAMBLE---------->|<---MSG_HEAD---->| // BB.00.1F.00.1E.00.00.22.FF.FF.FF.54.01.0A.15.00.5B.0D.20.00.01.01.00.00.01.00.00 // BB.00.1F.00.1E.00.00.22.FF.FF.FF.54.01.0A.15.00.71.16.00.00.01.01.00.00.02.00.00 // BB.00.1F.00.1E.00.00.22.FF.FF.FF.54.01.0A.15.00.2B.16.1F.28.01.01.00.00.01.00.00 @@ -365,7 +369,7 @@ const u_int8_t *TrumaiNetBoxApp::lin_multiframe_recieved(const u_int8_t *message } else if (header->message_type == STAUTS_FRAME_CONFIG && header->message_length == sizeof(StatusFrameConfig)) { ESP_LOGI(TAG, "StatusFrameConfig"); // Example: - // SID<---------PREAMBLE --------->|<---MSG HEAD --->| + // SID<---------PREAMBLE---------->|<---MSG_HEAD---->| // BB.00.1F.00.1E.00.00.22.FF.FF.FF.54.01.0A.17.00.0F.06.01.B4.0A.AA.0A.00.00.00.00 // BB.00.1F.00.1E.00.00.22.FF.FF.FF.54.01.0A.17.00.41.06.01.B4.0A.78.0A.00.00.00.00 // BB.00.1F.00.1E.00.00.22.FF.FF.FF.54.01.0A.17.00.0F.06.01.B4.0A.AA.0A.00.00.00.00 @@ -380,7 +384,7 @@ const u_int8_t *TrumaiNetBoxApp::lin_multiframe_recieved(const u_int8_t *message ESP_LOGI(TAG, "StatusFrameDevice"); // This message is special. I recieve one response per registered (at CP plus) device. // Example: - // SID<---------PREAMBLE --------->|<---MSG HEAD --->| + // SID<---------PREAMBLE---------->|<---MSG_HEAD---->| // BB.00.1F.00.1E.00.00.22.FF.FF.FF.54.01.0C.0B.00.79.02.00.01.00.50.00.00.04.03.02.AD.10 - C4.03.02 0050.00 // BB.00.1F.00.1E.00.00.22.FF.FF.FF.54.01.0C.0B.00.27.02.01.01.00.40.03.22.02.00.01.00.00 - H2.00.01 0340.22 auto device = statusFrame->inner.device;