From d8feaf02acf4eb41a8f3ea4b748be1944fb62c56 Mon Sep 17 00:00:00 2001 From: lumapu Date: Sat, 30 Sep 2023 00:10:49 +0200 Subject: [PATCH] changed to generic packet_t --- src/app.cpp | 8 ++++---- src/hm/hmPayload.h | 6 +++--- src/hms/cmt2300a.h | 4 ++-- src/hms/esp32_3wSpi.h | 9 ++++++--- src/hms/hmsPayload.h | 23 ++++++++++++----------- src/hms/hmsRadio.h | 11 +++-------- 6 files changed, 30 insertions(+), 31 deletions(-) diff --git a/src/app.cpp b/src/app.cpp index ffc7c871..c7a20391 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -151,18 +151,18 @@ void app::loop(void) { #if defined(ESP32) if (mCmtRadio.loop() && mConfig->cmt.enabled) { while (!mCmtRadio.mBufCtrl.empty()) { - hmsPacket_t *p = &mCmtRadio.mBufCtrl.front(); + packet_t *p = &mCmtRadio.mBufCtrl.front(); if (mConfig->serial.debug) { DPRINT(DBG_INFO, F("RX ")); - DBGPRINT(String(p->data[0])); + DBGPRINT(String(p->len)); DBGPRINT(F(", ")); DBGPRINT(String(p->rssi)); DBGPRINT(F("dBm | ")); - ah::dumpBuf(&p->data[1], p->data[0]); + ah::dumpBuf(p->packet, p->len); } mCmtStat.frmCnt++; - Inverter<> *iv = mSys.findInverter(&p->data[2]); + Inverter<> *iv = mSys.findInverter(&p->packet[1]); if(NULL != iv) { if((iv->ivGen == IV_HMS) || (iv->ivGen == IV_HMT)) mHmsPayload.add(iv, p); diff --git a/src/hm/hmPayload.h b/src/hm/hmPayload.h index a07c57eb..aa8fdfe6 100644 --- a/src/hm/hmPayload.h +++ b/src/hm/hmPayload.h @@ -33,12 +33,12 @@ typedef std::function *)> payloadListenerType; typedef std::function *)> alarmListenerType; -template +template class HmPayload { public: HmPayload() {} - void setup(IApp *app, HMSYSTEM *sys, HMRADIO *radio, statistics_t *stat, uint8_t maxRetransmits, uint32_t *timestamp) { + void setup(IApp *app, HMSYSTEM *sys, RADIO *radio, statistics_t *stat, uint8_t maxRetransmits, uint32_t *timestamp) { mApp = app; mSys = sys; mRadio = radio; @@ -460,7 +460,7 @@ class HmPayload { IApp *mApp; HMSYSTEM *mSys; - HMRADIO *mRadio; + RADIO *mRadio; statistics_t *mStat; uint8_t mMaxRetrans; uint32_t *mTimestamp; diff --git a/src/hms/cmt2300a.h b/src/hms/cmt2300a.h index d99875b9..a4308519 100644 --- a/src/hms/cmt2300a.h +++ b/src/hms/cmt2300a.h @@ -239,7 +239,7 @@ class Cmt2300a { return CMT_SUCCESS; } - uint8_t getRx(uint8_t buf[], uint8_t len, int8_t *rssi) { + uint8_t getRx(uint8_t buf[], uint8_t *rxLen, uint8_t maxlen, int8_t *rssi) { if(mTxPending) return CMT_ERR_TX_PENDING; @@ -250,7 +250,7 @@ class Cmt2300a { if(!cmtSwitchStatus(CMT2300A_GO_STBY, CMT2300A_STA_STBY)) return CMT_ERR_SWITCH_STATE; - mSpi.readFifo(buf, len); + mSpi.readFifo(buf, rxLen, maxlen); *rssi = mSpi.readReg(CMT2300A_CUS_RSSI_DBM) - 128; if(!cmtSwitchStatus(CMT2300A_GO_SLEEP, CMT2300A_STA_SLEEP)) diff --git a/src/hms/esp32_3wSpi.h b/src/hms/esp32_3wSpi.h index 4fe47497..e1c4114b 100644 --- a/src/hms/esp32_3wSpi.h +++ b/src/hms/esp32_3wSpi.h @@ -141,7 +141,7 @@ class esp32_3wSpi { SPI_PARAM_UNLOCK(); } - void readFifo(uint8_t buf[], uint8_t len) { + void readFifo(uint8_t buf[], uint8_t *len, uint8_t maxlen) { if(!mInitialized) return; uint8_t rx_data; @@ -154,10 +154,13 @@ class esp32_3wSpi { }; SPI_PARAM_LOCK(); - for(uint8_t i = 0; i < len; i++) { + for(uint8_t i = 0; i < maxlen; i++) { ESP_ERROR_CHECK(spi_device_polling_transmit(spi_fifo, &t)); delayMicroseconds(4); // > 4 us - buf[i] = rx_data; + if(0 == i) + *len = rx_data; + else + buf[i-1] = rx_data; } SPI_PARAM_UNLOCK(); } diff --git a/src/hms/hmsPayload.h b/src/hms/hmsPayload.h index 489fe044..893dfee2 100644 --- a/src/hms/hmsPayload.h +++ b/src/hms/hmsPayload.h @@ -172,18 +172,19 @@ class HmsPayload { } } - void add(Inverter<> *iv, hmsPacket_t *p) { - if (p->data[1] == (TX_REQ_INFO + ALL_FRAMES)) { // response from get information command - mPayload[iv->id].txId = p->data[1]; + void add(Inverter<> *iv, packet_t *p) { + if (p->packet[0] == (TX_REQ_INFO + ALL_FRAMES)) { // response from get information command + mPayload[iv->id].txId = p->packet[0]; DPRINTLN(DBG_DEBUG, F("Response from info request received")); - uint8_t *pid = &p->data[10]; + uint8_t *pid = &p->packet[9]; if (*pid == 0x00) { DPRINTLN(DBG_DEBUG, F("fragment number zero received and ignored")); } else { - DPRINTLN(DBG_DEBUG, "PID: 0x" + String(*pid, HEX)); + DPRINT(DBG_DEBUG, F("PID: 0x")); + DPRINTLN(DBG_DEBUG, String(*pid, HEX)); if ((*pid & 0x7F) < MAX_PAYLOAD_ENTRIES) { - memcpy(mPayload[iv->id].data[(*pid & 0x7F) - 1], &p->data[11], p->data[0] - 11); - mPayload[iv->id].len[(*pid & 0x7F) - 1] = p->data[0] -11; + memcpy(mPayload[iv->id].data[(*pid & 0x7F) - 1], &p->packet[10], p->len - 11); + mPayload[iv->id].len[(*pid & 0x7F) - 1] = p->len - 11; mPayload[iv->id].gotFragment = true; mPayload[iv->id].rssi[(*pid & 0x7F) - 1] = p->rssi; } @@ -197,15 +198,15 @@ class HmsPayload { } } } - } else if (p->data[1] == (TX_REQ_DEVCONTROL + ALL_FRAMES)) { // response from dev control command + } else if (p->packet[0] == (TX_REQ_DEVCONTROL + ALL_FRAMES)) { // response from dev control command DPRINTLN(DBG_DEBUG, F("Response from devcontrol request received")); - mPayload[iv->id].txId = p->data[1]; + mPayload[iv->id].txId = p->packet[0]; iv->clearDevControlRequest(); - if ((p->data[13] == ActivePowerContr) && (p->data[14] == 0x00)) { + if ((p->packet[12] == ActivePowerContr) && (p->packet[13] == 0x00)) { bool ok = true; - if((p->data[11] == 0x00) && (p->data[12] == 0x00)) { + if((p->packet[10] == 0x00) && (p->packet[11] == 0x00)) { mApp->setMqttPowerLimitAck(iv); iv->powerLimitAck = true; } else diff --git a/src/hms/hmsRadio.h b/src/hms/hmsRadio.h index 4cc88d03..ff19e217 100644 --- a/src/hms/hmsRadio.h +++ b/src/hms/hmsRadio.h @@ -9,11 +9,6 @@ #include "../utils/dbg.h" #include "cmt2300a.h" -typedef struct { - int8_t rssi; - uint8_t data[28]; -} hmsPacket_t; - #define U32_B3(val) ((uint8_t)((val >> 24) & 0xff)) #define U32_B2(val) ((uint8_t)((val >> 16) & 0xff)) #define U32_B1(val) ((uint8_t)((val >> 8) & 0xff)) @@ -146,7 +141,7 @@ class CmtRadio { mStat->txCnt++; } - std::queue mBufCtrl; + std::queue mBufCtrl; private: inline void reset(bool genDtuSn) { @@ -206,8 +201,8 @@ class CmtRadio { } inline void getRx(void) { - hmsPacket_t p; - uint8_t status = mCmt.getRx(p.data, 28, &p.rssi); + packet_t p; + uint8_t status = mCmt.getRx(p.packet, &p.len, 28, &p.rssi); if(CMT_SUCCESS == status) mBufCtrl.push(p); }