~aleteoryx/muditaos

c8e7e706d8149e570abee332afc4cb731e496cfb — Mateusz Grzywacz 5 years ago 6aae5f5
[EGD-6021] Cellular flush receiving buffer before initialization

Upon hot start the first data we receive comes from
modem from previous boot. This data is meaningless and
potenitally harmful. This adds: ignoring it
2 files changed, 22 insertions(+), 2 deletions(-)

M module-cellular/Modem/TS0710/TS0710.cpp
M module-cellular/Modem/TS0710/TS0710.h
M module-cellular/Modem/TS0710/TS0710.cpp => module-cellular/Modem/TS0710/TS0710.cpp +21 -2
@@ 63,10 63,16 @@ TS0710::TS0710(PortSpeed_e portSpeed, sys::Service *parent)
    startParams.WakeUpRespTime          = 10;  // 10s default
    startParams.ErrRecovWindowSize      = 2;   // 2 default

    if (auto flushed = FlushReceiveData(); flushed > 0) {
        LOG_INFO("Discarded initial %" PRIu32 " bytes sent by modem",
                 static_cast<uint32_t>(flushed)); // not baud-accurate. Might be 460800÷115200 times more
    }

    constexpr auto workerName = "TS0710SerialRxWorker";
    BaseType_t task_error =
        xTaskCreate(workerTaskFunction, "TS0710Worker", threadSizeWords, this, taskPriority, &taskHandle);
        xTaskCreate(workerTaskFunction, workerName, threadSizeWords, this, taskPriority, &taskHandle);
    if (task_error != pdPASS) {
        LOG_ERROR("Failed to start inputSerialWorker task");
        LOG_ERROR("Failed to start %s task", workerName);
        return;
    }
}


@@ 501,6 507,19 @@ void workerTaskFunction(void *ptr)
    }
}

size_t TS0710::FlushReceiveData()
{
    using namespace std::chrono_literals;

    auto flushed                          = 0U;
    constexpr auto flushInactivityTimeout = 20ms;
    std::uint8_t dummyRead[50];
    while (pv_cellular->Wait(flushInactivityTimeout.count())) {
        flushed += pv_cellular->Read(dummyRead, sizeof(dummyRead));
    }
    return flushed;
}

ssize_t TS0710::ReceiveData(std::vector<uint8_t> &data, uint32_t timeout)
{
    ssize_t ret = -1;

M module-cellular/Modem/TS0710/TS0710.h => module-cellular/Modem/TS0710/TS0710.h +1 -0
@@ 363,6 363,7 @@ class TS0710
    ConfState AudioConfProcedure();
    ConfState StartMultiplexer();

    size_t FlushReceiveData();
    ssize_t ReceiveData(std::vector<uint8_t> &data, uint32_t timeout);

    bsp::Cellular *getCellular()