feat(gateway): implement LegacyRawPayload function for DALI and USB components
Signed-off-by: Tony <tonylu@tony-cloud.com>
This commit is contained in:
@@ -10,6 +10,10 @@
|
|||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
|
#ifndef CONFIG_DALI_QUERY_RESPONSE_TIMEOUT_MS
|
||||||
|
#define CONFIG_DALI_QUERY_RESPONSE_TIMEOUT_MS 25
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace gateway {
|
namespace gateway {
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
@@ -17,6 +21,48 @@ namespace {
|
|||||||
constexpr const char* kTag = "dali_domain";
|
constexpr const char* kTag = "dali_domain";
|
||||||
constexpr size_t kSerialRxPacketMaxBytes = 8;
|
constexpr size_t kSerialRxPacketMaxBytes = 8;
|
||||||
constexpr UBaseType_t kSerialRxQueueDepth = 8;
|
constexpr UBaseType_t kSerialRxQueueDepth = 8;
|
||||||
|
constexpr uint32_t kHardwareQueryRawSuppressMs = CONFIG_DALI_QUERY_RESPONSE_TIMEOUT_MS + 10;
|
||||||
|
|
||||||
|
portMUX_TYPE s_query_raw_suppress_lock = portMUX_INITIALIZER_UNLOCKED;
|
||||||
|
TickType_t s_query_raw_suppress_until[DALI_PHY_COUNT] = {};
|
||||||
|
|
||||||
|
void BeginHardwareQueryRawSuppress(uint8_t bus_id) {
|
||||||
|
if (bus_id >= DALI_PHY_COUNT) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const TickType_t until = xTaskGetTickCount() + pdMS_TO_TICKS(kHardwareQueryRawSuppressMs);
|
||||||
|
portENTER_CRITICAL(&s_query_raw_suppress_lock);
|
||||||
|
s_query_raw_suppress_until[bus_id] = until;
|
||||||
|
portEXIT_CRITICAL(&s_query_raw_suppress_lock);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool TakeHardwareQueryRawSuppress(uint8_t bus_id) {
|
||||||
|
if (bus_id >= DALI_PHY_COUNT) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool suppress = false;
|
||||||
|
const TickType_t now = xTaskGetTickCount();
|
||||||
|
portENTER_CRITICAL(&s_query_raw_suppress_lock);
|
||||||
|
const TickType_t until = s_query_raw_suppress_until[bus_id];
|
||||||
|
if (until != 0 && now <= until) {
|
||||||
|
suppress = true;
|
||||||
|
s_query_raw_suppress_until[bus_id] = 0;
|
||||||
|
} else if (until != 0) {
|
||||||
|
s_query_raw_suppress_until[bus_id] = 0;
|
||||||
|
}
|
||||||
|
portEXIT_CRITICAL(&s_query_raw_suppress_lock);
|
||||||
|
return suppress;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ClearHardwareQueryRawSuppress(uint8_t bus_id) {
|
||||||
|
if (bus_id >= DALI_PHY_COUNT) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
portENTER_CRITICAL(&s_query_raw_suppress_lock);
|
||||||
|
s_query_raw_suppress_until[bus_id] = 0;
|
||||||
|
portEXIT_CRITICAL(&s_query_raw_suppress_lock);
|
||||||
|
}
|
||||||
|
|
||||||
DaliDomainSnapshot MakeSnapshot(uint8_t gateway_id, int address, const char* kind) {
|
DaliDomainSnapshot MakeSnapshot(uint8_t gateway_id, int address, const char* kind) {
|
||||||
DaliDomainSnapshot snapshot;
|
DaliDomainSnapshot snapshot;
|
||||||
@@ -113,12 +159,15 @@ std::vector<uint8_t> TransactHardwareFrame(uint8_t bus_id, const uint8_t* data,
|
|||||||
Dali_msg_t tx = dali_msg_new(data[1], data[2]);
|
Dali_msg_t tx = dali_msg_new(data[1], data[2]);
|
||||||
tx.id = bus_id;
|
tx.id = bus_id;
|
||||||
Dali_msg_t rx = {};
|
Dali_msg_t rx = {};
|
||||||
|
BeginHardwareQueryRawSuppress(bus_id);
|
||||||
if (dali_query(&tx, &rx) == pdTRUE) {
|
if (dali_query(&tx, &rx) == pdTRUE) {
|
||||||
if (rx.status != DALI_FRAME_OK || rx.length != 8) {
|
if (rx.status != DALI_FRAME_OK || rx.length != 8) {
|
||||||
|
ClearHardwareQueryRawSuppress(bus_id);
|
||||||
return LegacyQueryResponse(0xFD);
|
return LegacyQueryResponse(0xFD);
|
||||||
}
|
}
|
||||||
return {0xFF, rx.data[0]};
|
return {0xFF, rx.data[0]};
|
||||||
}
|
}
|
||||||
|
ClearHardwareQueryRawSuppress(bus_id);
|
||||||
return LegacyQueryResponse(0xFE);
|
return LegacyQueryResponse(0xFE);
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
@@ -1250,6 +1299,12 @@ void DaliDomainService::rawFrameTaskLoop() {
|
|||||||
if (byte_count > DALI_MAX_BYTES) {
|
if (byte_count > DALI_MAX_BYTES) {
|
||||||
byte_count = DALI_MAX_BYTES;
|
byte_count = DALI_MAX_BYTES;
|
||||||
}
|
}
|
||||||
|
if (byte_count == 1 && TakeHardwareQueryRawSuppress(message.id)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (byte_count != 1 && byte_count != 2 && byte_count != 3) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
DaliRawFrame frame;
|
DaliRawFrame frame;
|
||||||
frame.channel_index = channel->config.channel_index;
|
frame.channel_index = channel->config.channel_index;
|
||||||
frame.gateway_id = channel->config.gateway_id;
|
frame.gateway_id = channel->config.gateway_id;
|
||||||
|
|||||||
@@ -141,6 +141,13 @@ void RegisterGatt(struct ble_gatt_register_ctxt* ctxt, void* arg) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::vector<uint8_t> LegacyRawPayload(const std::vector<uint8_t>& data) {
|
||||||
|
if (data.size() == 1) {
|
||||||
|
return {0xBE, data[0]};
|
||||||
|
}
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
const struct ble_gatt_svc_def kGattServices[] = {
|
const struct ble_gatt_svc_def kGattServices[] = {
|
||||||
{
|
{
|
||||||
.type = BLE_GATT_SVC_TYPE_PRIMARY,
|
.type = BLE_GATT_SVC_TYPE_PRIMARY,
|
||||||
@@ -377,7 +384,7 @@ void GatewayBleBridge::handleDaliRawFrame(const DaliRawFrame& frame) {
|
|||||||
if (!enabled_ || conn_handle_ == kInvalidConnectionHandle || frame.data.empty()) {
|
if (!enabled_ || conn_handle_ == kInvalidConnectionHandle || frame.data.empty()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
notifyCharacteristic(frame.channel_index, frame.data);
|
notifyCharacteristic(frame.channel_index, LegacyRawPayload(frame.data));
|
||||||
}
|
}
|
||||||
|
|
||||||
void GatewayBleBridge::handleRawWrite(size_t channel_index, const std::vector<uint8_t>& payload) {
|
void GatewayBleBridge::handleRawWrite(size_t channel_index, const std::vector<uint8_t>& payload) {
|
||||||
|
|||||||
@@ -12,6 +12,13 @@ namespace gateway {
|
|||||||
namespace {
|
namespace {
|
||||||
constexpr const char* kTag = "gateway_usb";
|
constexpr const char* kTag = "gateway_usb";
|
||||||
constexpr size_t kCommandFrameMinLen = 7;
|
constexpr size_t kCommandFrameMinLen = 7;
|
||||||
|
|
||||||
|
std::vector<uint8_t> LegacyRawPayload(const std::vector<uint8_t>& data) {
|
||||||
|
if (data.size() == 1) {
|
||||||
|
return {0xBE, data[0]};
|
||||||
|
}
|
||||||
|
return data;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
GatewayUsbSetupBridge::GatewayUsbSetupBridge(GatewayController& controller,
|
GatewayUsbSetupBridge::GatewayUsbSetupBridge(GatewayController& controller,
|
||||||
@@ -106,11 +113,12 @@ void GatewayUsbSetupBridge::handleRawFrame(const DaliRawFrame& frame) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const int written = usb_serial_jtag_write_bytes(frame.data.data(), frame.data.size(),
|
const auto payload = LegacyRawPayload(frame.data);
|
||||||
|
const int written = usb_serial_jtag_write_bytes(payload.data(), payload.size(),
|
||||||
pdMS_TO_TICKS(config_.write_timeout_ms));
|
pdMS_TO_TICKS(config_.write_timeout_ms));
|
||||||
if (written < 0 || static_cast<size_t>(written) != frame.data.size()) {
|
if (written < 0 || static_cast<size_t>(written) != payload.size()) {
|
||||||
ESP_LOGW(kTag, "failed to forward USB raw setup frame channel=%u len=%u", frame.channel_index,
|
ESP_LOGW(kTag, "failed to forward USB raw setup frame channel=%u len=%u", frame.channel_index,
|
||||||
static_cast<unsigned>(frame.data.size()));
|
static_cast<unsigned>(payload.size()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user