diff --git a/module-cellular/Modem/GSM0710.cpp b/module-cellular/Modem/GSM0710.cpp index 853e30346..5efbdbc0b 100644 --- a/module-cellular/Modem/GSM0710.cpp +++ b/module-cellular/Modem/GSM0710.cpp @@ -14,6 +14,7 @@ #include "log/log.hpp" constexpr unsigned char GSM0710Buffer::crcTable[]; +const uint32_t GSM0710Buffer::cmux_N1; GSM0710Frame* GSM0710Buffer::GetCompleteFrame(GSM0710Frame* frame) { diff --git a/module-cellular/Modem/GSM0710.hpp b/module-cellular/Modem/GSM0710.hpp index 52960102a..02167239d 100644 --- a/module-cellular/Modem/GSM0710.hpp +++ b/module-cellular/Modem/GSM0710.hpp @@ -68,12 +68,12 @@ class GSM0710Buffer { public: // +CMUX=[,[,[,[,[,[,[,[,]]]]]]]] - uint32_t cmux_mode = 0; + static const uint32_t cmux_mode = 0; uint32_t vir_ports = 3; /* number of virtual ports to create */ - uint32_t cmux_N1 = 127; //lots of modem only support short frame, like quectel 2G modules - uint32_t cmux_FRAME = (cmux_N1 + 6); //lots of modem only support short frame, like quectel 2G modules - uint32_t QUECTEL_CACHE_FRAMES = 20; - uint32_t GSM0710_BUFFER_SIZE = (2 * QUECTEL_CACHE_FRAMES * cmux_FRAME); + static const uint32_t cmux_N1 = 127; //lots of modem only support short frame, like quectel 2G modules + static const uint32_t cmux_FRAME = (cmux_N1 + 6); //lots of modem only support short frame, like quectel 2G modules + static const uint32_t QUECTEL_CACHE_FRAMES = 20; + static const uint32_t GSM0710_BUFFER_SIZE = (2 * QUECTEL_CACHE_FRAMES * cmux_FRAME); unsigned char* data = nullptr; unsigned char *readp = nullptr; @@ -84,10 +84,8 @@ public: - GSM0710Buffer(uint32_t virtualPortsCount, uint32_t frameSize=127, uint32_t mode=0) : - cmux_mode(mode), - vir_ports(virtualPortsCount), - cmux_N1(frameSize) { + GSM0710Buffer(uint32_t virtualPortsCount) : + vir_ports(virtualPortsCount){ currentFrame = std::make_unique(); diff --git a/module-cellular/Modem/InputSerialWorker.cpp b/module-cellular/Modem/InputSerialWorker.cpp index 878575063..6889adbd8 100644 --- a/module-cellular/Modem/InputSerialWorker.cpp +++ b/module-cellular/Modem/InputSerialWorker.cpp @@ -15,6 +15,9 @@ InputSerialWorker::InputSerialWorker(MuxDaemon *mux) : muxDaemon(mux) { + + inputBuffer = std::make_unique(mux->virtualPortsCount); + BaseType_t task_error = xTaskCreate( workerTaskFunction, "cellInSerWorker", @@ -35,19 +38,19 @@ InputSerialWorker::~InputSerialWorker() { void workerTaskFunction(void *ptr) { - InputSerialWorker *worker = reinterpret_cast(ptr); + InputSerialWorker *inst = reinterpret_cast(ptr); while (1) { - worker->muxDaemon->cellular->Wait(UINT32_MAX); - if (worker->ReadIncomingData() > 0) { - if (worker->muxDaemon->inputBuffer->GetDataLength() > 0) { + inst->muxDaemon->cellular->Wait(UINT32_MAX); + if (inst->ReadIncomingData() > 0) { + if (inst->inputBuffer->GetDataLength() > 0) { - if (worker->ExtractFrames()) { + if (inst->ExtractFrames()) { //TODO:M.P implement error handling ? } // Reorganize data in buffer if necessary - worker->muxDaemon->inputBuffer->ReorganizeBuffer(); + inst->inputBuffer->ReorganizeBuffer(); } } } @@ -58,29 +61,29 @@ int InputSerialWorker::ReadIncomingData() { switch (muxDaemon->state) { case MuxDaemon::States::MUX_STATE_MUXING: - length = std::min(muxDaemon->inputBuffer->GetFreeSpace(), - (uint32_t) (muxDaemon->inputBuffer->endp - muxDaemon->inputBuffer->writep)); + length = std::min(inputBuffer->GetFreeSpace(), + (uint32_t) (inputBuffer->endp - inputBuffer->writep)); if (length > 0) /*available space in buffer (not locked since we want to utilize all available space)*/ { - length = muxDaemon->cellular->Read(muxDaemon->inputBuffer->writep, length); + length = muxDaemon->cellular->Read(inputBuffer->writep, length); if (length > 0) { LOG_DEBUG("READ SIZE : %d", length); //syslogdump("in_buf->writep, length); - muxDaemon->inputBuffer->writep += length; - if (muxDaemon->inputBuffer->writep == muxDaemon->inputBuffer->endp) - muxDaemon->inputBuffer->writep = muxDaemon->inputBuffer->data; + inputBuffer->writep += length; + if (inputBuffer->writep == inputBuffer->endp) + inputBuffer->writep = inputBuffer->data; - muxDaemon->inputBuffer->datacount += length; /*updating the data-not-yet-read counter*/ + inputBuffer->datacount += length; /*updating the data-not-yet-read counter*/ LOG_DEBUG("GSM0710 buffer (up-to-date): written %d, free %d, stored %d", - length, muxDaemon->inputBuffer->GetFreeSpace(), muxDaemon->inputBuffer->GetDataLength()); + length, inputBuffer->GetFreeSpace(), inputBuffer->GetDataLength()); - if (length > muxDaemon->inputBuffer->max_count) { + if (length > inputBuffer->max_count) { LOG_INFO("MAX READ SIZE : %d", length); LOG_INFO("GSM0710 buffer (up-to-date): written %d, free %d, stored %d", - length, muxDaemon->inputBuffer->GetFreeSpace(), - muxDaemon->inputBuffer->GetDataLength()); - muxDaemon->inputBuffer->max_count = length; + length, inputBuffer->GetFreeSpace(), + inputBuffer->GetDataLength()); + inputBuffer->max_count = length; } } else { LOG_INFO("READ SIZE : %d", length); @@ -103,11 +106,11 @@ int InputSerialWorker::ExtractFrames() { GSM0710Frame local_frame; GSM0710Frame *frame = &local_frame; - while (muxDaemon->inputBuffer->GetCompleteFrame(frame) != NULL) { + while (inputBuffer->GetCompleteFrame(frame) != NULL) { frames_extracted++; - if ((muxDaemon->inputBuffer->GSM0710_FRAME_IS(MuxDefines::GSM0710_TYPE_UI, frame) || - muxDaemon->inputBuffer->GSM0710_FRAME_IS(MuxDefines::GSM0710_TYPE_UIH, frame))) { + if ((inputBuffer->GSM0710_FRAME_IS(MuxDefines::GSM0710_TYPE_UI, frame) || + inputBuffer->GSM0710_FRAME_IS(MuxDefines::GSM0710_TYPE_UIH, frame))) { LOG_DEBUG("Frame is UI or UIH"); if (frame->control & static_cast(MuxDefines::GSM0710_PF)) { muxDaemon->uih_pf_bit_received = 1; diff --git a/module-cellular/Modem/InputSerialWorker.hpp b/module-cellular/Modem/InputSerialWorker.hpp index 6d1f4de91..4b7c73a28 100644 --- a/module-cellular/Modem/InputSerialWorker.hpp +++ b/module-cellular/Modem/InputSerialWorker.hpp @@ -41,6 +41,8 @@ private: xTaskHandle taskHandle; const uint32_t taskPriority = 0; + std::unique_ptr inputBuffer=nullptr; + }; diff --git a/module-cellular/Modem/MuxChannel.cpp b/module-cellular/Modem/MuxChannel.cpp index 7e62c677d..9b533ae4f 100644 --- a/module-cellular/Modem/MuxChannel.cpp +++ b/module-cellular/Modem/MuxChannel.cpp @@ -86,12 +86,8 @@ int MuxChannel::Close() { vQueueDelete(workerQueueHandle); vTaskDelete(workerHandle); - if (mux->inputBuffer->cmux_mode) - mux->WriteMuxFrame(GetChannelNumber(), NULL, 0, static_cast(MuxDefines::GSM0710_CONTROL_CLD) | - static_cast(MuxDefines::GSM0710_CR)); - else - mux->WriteMuxFrame(GetChannelNumber(), mux->closeChannelCmd, sizeof(mux->closeChannelCmd), - static_cast(MuxDefines::GSM0710_TYPE_UIH)); + mux->WriteMuxFrame(GetChannelNumber(), mux->closeChannelCmd, sizeof(mux->closeChannelCmd), + static_cast(MuxDefines::GSM0710_TYPE_UIH)); return 0; } diff --git a/module-cellular/Modem/MuxDaemon.cpp b/module-cellular/Modem/MuxDaemon.cpp index 500ad5b8d..36513e99e 100644 --- a/module-cellular/Modem/MuxDaemon.cpp +++ b/module-cellular/Modem/MuxDaemon.cpp @@ -23,9 +23,7 @@ #include "CommunicationMuxChannel.hpp" constexpr unsigned char MuxDaemon::closeChannelCmd[]; -const uint32_t MuxDaemon::frameSize; const uint32_t MuxDaemon::virtualPortsCount; -const bool MuxDaemon::cmuxMode; namespace QuectelBaudrates { @@ -80,7 +78,6 @@ namespace QuectelBaudrates { MuxDaemon::MuxDaemon() { cellular = bsp::Cellular::Create(); - inputBuffer = std::make_unique(virtualPortsCount, frameSize, cmuxMode); } MuxDaemon::~MuxDaemon() { @@ -148,14 +145,12 @@ int MuxDaemon::Start() { SendAT("AT+CNMI=1,2,0,1,0\r", 500);*/ + // This driver supports only Basic mode (max frame length = 127bytes and no frame errors correction) char gsm_command[128] = {}; - if (cmuxMode) { - snprintf(gsm_command, sizeof(gsm_command), "AT+CMUX=1\r\n"); - } else { - snprintf(gsm_command, sizeof(gsm_command), "AT+CMUX=%d,%d,%d,%d\r\n", cmuxMode, - cmuxSubset, QuectelBaudrates::Baudrates::Value, frameSize - ); - } + snprintf(gsm_command, sizeof(gsm_command), "AT+CMUX=%d,%d,%d,%d\r\n", GSM0710Buffer::cmux_mode, + 0, QuectelBaudrates::Baudrates::Value, GSM0710Buffer::cmux_N1 + ); + // Start CMUX multiplexer SendAT(gsm_command, 500); @@ -226,7 +221,7 @@ ssize_t MuxDaemon::WriteMuxFrame(int channel, const unsigned char *input, int le unsigned char postfix[2] = {0xFF, static_cast(MuxDefines::GSM0710_FRAME_FLAG )}; ssize_t prefix_length = 4; int c; - unsigned char tmp[inputBuffer->cmux_FRAME]; + unsigned char tmp[GSM0710Buffer::cmux_FRAME]; LOG_DEBUG("Sending frame to channel %d", channel); @@ -238,14 +233,14 @@ ssize_t MuxDaemon::WriteMuxFrame(int channel, const unsigned char *input, int le if ((type == static_cast(MuxDefines::GSM0710_TYPE_UIH) || type == static_cast(MuxDefines::GSM0710_TYPE_UI)) && uih_pf_bit_received == 1 && - inputBuffer->GSM0710_COMMAND_IS(MuxDefines::GSM0710_CONTROL_MSC, input[0])) { + GSM0710Buffer::GSM0710_COMMAND_IS(MuxDefines::GSM0710_CONTROL_MSC, input[0])) { prefix[2] = prefix[2] | static_cast(MuxDefines::GSM0710_PF); //Set the P/F bit in Response if Command from modem had it set uih_pf_bit_received = 0; //Reset the variable, so it is ready for next command } /* let's not use too big frames */ - length = std::min(inputBuffer->cmux_N1, static_cast(length)); - if (!cmuxMode)//basic + length = std::min(GSM0710Buffer::cmux_N1, static_cast(length)); + // Only basic mode is supported { /* Modified acording PATCH CRC checksum */ /* postfix[0] = frame_calc_crc (prefix + 1, prefix_length - 1); */ diff --git a/module-cellular/Modem/MuxDaemon.hpp b/module-cellular/Modem/MuxDaemon.hpp index 75af12602..f451647cc 100644 --- a/module-cellular/Modem/MuxDaemon.hpp +++ b/module-cellular/Modem/MuxDaemon.hpp @@ -26,6 +26,14 @@ class MuxDaemon { public: + /** + * Multiplexer configuration + */ + + const static uint32_t baudRate = 115200; + const static uint32_t virtualPortsCount = 4; // max number of virtual channels supported by EG25 + const static bool hardwareControlFlowEnable = false; + MuxDaemon(); ~MuxDaemon(); @@ -90,27 +98,12 @@ private: States state = States::MUX_STATE_OPENING; - std::unique_ptr inputBuffer=nullptr; std::vector channels; cpp_freertos::MutexStandard serOutMutex; int uih_pf_bit_received = 0; - /** - * Multiplexer configuration - */ - - const static uint32_t baudRate = 115200; - const static uint32_t virtualPortsCount = 4; // max number of virtual channels supported by EG25 - const static uint32_t frameSize = 127; // default basic frame size - const static bool hardwareControlFlowEnable = false; - const static bool cmuxMode = false; - const static uint32_t cmuxSubset = 0; - - - - };