fix(gateway): enhance DALI logging with query packet logging and configuration
Signed-off-by: Tony <tonylu@tony-cloud.com>
This commit is contained in:
@@ -128,6 +128,16 @@ std::vector<uint8_t> LegacyQueryResponse(uint8_t status, uint8_t value = 0x00) {
|
|||||||
return {status, value};
|
return {status, value};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void LogQueryRxPacket(const char* transport, int id, const std::vector<uint8_t>& packet,
|
||||||
|
const char* note = nullptr) {
|
||||||
|
const unsigned first = packet.size() > 0 ? packet[0] : 0;
|
||||||
|
const unsigned second = packet.size() > 1 ? packet[1] : 0;
|
||||||
|
ESP_LOGD(TAG, "%s query rx packet id=%d len=%u data=%02x %02x%s%s",
|
||||||
|
transport == nullptr ? "dali" : transport, id,
|
||||||
|
static_cast<unsigned>(packet.size()), first, second,
|
||||||
|
note == nullptr ? "" : " note=", note == nullptr ? "" : note);
|
||||||
|
}
|
||||||
|
|
||||||
bool SendHardwareFrame(uint8_t bus_id, const uint8_t* data, size_t len) {
|
bool SendHardwareFrame(uint8_t bus_id, const uint8_t* data, size_t len) {
|
||||||
if (data == nullptr || len != 3) {
|
if (data == nullptr || len != 3) {
|
||||||
return false;
|
return false;
|
||||||
@@ -159,7 +169,12 @@ std::vector<uint8_t> TransactHardwareFrame(uint8_t bus_id, const uint8_t* data,
|
|||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
if (len != 3) {
|
if (len != 3) {
|
||||||
return len > 0 && data[0] == 0x12 ? LegacyQueryResponse(0xFD) : std::vector<uint8_t>{};
|
if (len > 0 && data[0] == 0x12) {
|
||||||
|
const auto packet = LegacyQueryResponse(0xFD);
|
||||||
|
LogQueryRxPacket("hardware", bus_id, packet, "invalid-query-len");
|
||||||
|
return packet;
|
||||||
|
}
|
||||||
|
return std::vector<uint8_t>{};
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (data[0]) {
|
switch (data[0]) {
|
||||||
@@ -182,15 +197,21 @@ std::vector<uint8_t> TransactHardwareFrame(uint8_t bus_id, const uint8_t* data,
|
|||||||
ClearHardwareQueryRawSuppress(bus_id);
|
ClearHardwareQueryRawSuppress(bus_id);
|
||||||
if (rx.status != DALI_FRAME_OK || rx.length != 8) {
|
if (rx.status != DALI_FRAME_OK || rx.length != 8) {
|
||||||
ESP_LOGW(TAG, "hardware query response for bus=%u has invalid status or length", bus_id);
|
ESP_LOGW(TAG, "hardware query response for bus=%u has invalid status or length", bus_id);
|
||||||
return LegacyQueryResponse(0xFD);
|
const auto packet = LegacyQueryResponse(0xFD);
|
||||||
|
LogQueryRxPacket("hardware", bus_id, packet, "invalid-status-or-length");
|
||||||
|
return packet;
|
||||||
}
|
}
|
||||||
ESP_LOGD(TAG, "got hardware query response for bus=%u status=%u len=%u data=%02x %02x "
|
ESP_LOGD(TAG, "got hardware query response for bus=%u status=%u len=%u data=%02x %02x "
|
||||||
"%02x %02x",
|
"%02x %02x",
|
||||||
bus_id, rx.status, rx.length, rx.data[0], rx.data[1], rx.data[2], rx.data[3]);
|
bus_id, rx.status, rx.length, rx.data[0], rx.data[1], rx.data[2], rx.data[3]);
|
||||||
return {0xFF, rx.data[0]};
|
const std::vector<uint8_t> packet{0xFF, rx.data[0]};
|
||||||
|
LogQueryRxPacket("hardware", bus_id, packet, "ok");
|
||||||
|
return packet;
|
||||||
}
|
}
|
||||||
ClearHardwareQueryRawSuppress(bus_id);
|
ClearHardwareQueryRawSuppress(bus_id);
|
||||||
return LegacyQueryResponse(0xFE);
|
const auto packet = LegacyQueryResponse(0xFE);
|
||||||
|
LogQueryRxPacket("hardware", bus_id, packet, "no-response");
|
||||||
|
return packet;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
return {};
|
return {};
|
||||||
@@ -233,16 +254,22 @@ std::vector<uint8_t> TransactSerialFrame(int uart_port, QueueHandle_t queue,
|
|||||||
uint32_t query_timeout_ms, const uint8_t* data,
|
uint32_t query_timeout_ms, const uint8_t* data,
|
||||||
size_t len) {
|
size_t len) {
|
||||||
if (data == nullptr || len == 0) {
|
if (data == nullptr || len == 0) {
|
||||||
return LegacyQueryResponse(0xFD);
|
const auto packet = LegacyQueryResponse(0xFD);
|
||||||
|
LogQueryRxPacket("serial", uart_port, packet, "empty-query");
|
||||||
|
return packet;
|
||||||
}
|
}
|
||||||
if (data[0] == 0x12 && len != 3) {
|
if (data[0] == 0x12 && len != 3) {
|
||||||
return LegacyQueryResponse(0xFD);
|
const auto packet = LegacyQueryResponse(0xFD);
|
||||||
|
LogQueryRxPacket("serial", uart_port, packet, "invalid-query-len");
|
||||||
|
return packet;
|
||||||
}
|
}
|
||||||
if (data != nullptr && len > 0 && data[0] == 0x12) {
|
if (data != nullptr && len > 0 && data[0] == 0x12) {
|
||||||
DrainSerialQueue(queue);
|
DrainSerialQueue(queue);
|
||||||
}
|
}
|
||||||
if (!WriteSerialFrame(uart_port, data, len)) {
|
if (!WriteSerialFrame(uart_port, data, len)) {
|
||||||
return LegacyQueryResponse(0xFD);
|
const auto packet = LegacyQueryResponse(0xFD);
|
||||||
|
LogQueryRxPacket("serial", uart_port, packet, "write-failed");
|
||||||
|
return packet;
|
||||||
}
|
}
|
||||||
if (data[0] != 0x12) {
|
if (data[0] != 0x12) {
|
||||||
return {0xFF};
|
return {0xFF};
|
||||||
@@ -258,12 +285,17 @@ std::vector<uint8_t> TransactSerialFrame(int uart_port, QueueHandle_t queue,
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
auto response = PacketToVector(packet, 2);
|
auto response = PacketToVector(packet, 2);
|
||||||
|
LogQueryRxPacket("serial", uart_port, response, "raw");
|
||||||
if (!response.empty() &&
|
if (!response.empty() &&
|
||||||
(response[0] == 0xFF || response[0] == 0xFE || response[0] == 0xFD)) {
|
(response[0] == 0xFF || response[0] == 0xFE || response[0] == 0xFD)) {
|
||||||
return LegacyQueryResponse(response[0], response.size() > 1 ? response[1] : 0x00);
|
const auto packet = LegacyQueryResponse(response[0], response.size() > 1 ? response[1] : 0x00);
|
||||||
|
LogQueryRxPacket("serial", uart_port, packet, "accepted");
|
||||||
|
return packet;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return LegacyQueryResponse(0xFE);
|
const auto packet = LegacyQueryResponse(0xFE);
|
||||||
|
LogQueryRxPacket("serial", uart_port, packet, "timeout");
|
||||||
|
return packet;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
@@ -282,7 +314,9 @@ struct DaliDomainService::DaliChannel {
|
|||||||
};
|
};
|
||||||
|
|
||||||
DaliDomainService::DaliDomainService()
|
DaliDomainService::DaliDomainService()
|
||||||
: raw_frame_sink_lock_(xSemaphoreCreateMutex()) {}
|
: raw_frame_sink_lock_(xSemaphoreCreateMutex()) {
|
||||||
|
esp_log_level_set(TAG, (esp_log_level_t)CONFIG_DALI_LOG_LEVEL);
|
||||||
|
}
|
||||||
|
|
||||||
DaliDomainService::~DaliDomainService() = default;
|
DaliDomainService::~DaliDomainService() = default;
|
||||||
|
|
||||||
|
|||||||
@@ -24,6 +24,7 @@
|
|||||||
#define LOG_LOCAL_LEVEL ESP_LOG_DEBUG
|
#define LOG_LOCAL_LEVEL ESP_LOG_DEBUG
|
||||||
#endif
|
#endif
|
||||||
#include "esp_log.h"
|
#include "esp_log.h"
|
||||||
|
#include "sdkconfig.h"
|
||||||
#include "freertos/semphr.h"
|
#include "freertos/semphr.h"
|
||||||
#include "lwip/inet.h"
|
#include "lwip/inet.h"
|
||||||
#include "lwip/sockets.h"
|
#include "lwip/sockets.h"
|
||||||
@@ -69,6 +70,7 @@ void ConfigureDaliCppLogging() {
|
|||||||
if (configured) return;
|
if (configured) return;
|
||||||
configured = true;
|
configured = true;
|
||||||
DaliLog::instance().setLevel(LogLevel::debug);
|
DaliLog::instance().setLevel(LogLevel::debug);
|
||||||
|
esp_log_level_set(kDaliCppLogTag, (esp_log_level_t)CONFIG_DALI_LOG_LEVEL);
|
||||||
DaliLog::instance().setSink([](const std::string& line) {
|
DaliLog::instance().setSink([](const std::string& line) {
|
||||||
ESP_LOGD(kDaliCppLogTag, "%s", line.c_str());
|
ESP_LOGD(kDaliCppLogTag, "%s", line.c_str());
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user