From 4d6fe7516f8469062a063cd36c6ceff0c6b446da Mon Sep 17 00:00:00 2001 From: Serhii Voloshynov Date: Fri, 28 Feb 2020 10:07:39 +0200 Subject: add debug info to SerialConnection::doOpen --- src/MTS_IO_SerialConnection.cpp | 74 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 73 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/MTS_IO_SerialConnection.cpp b/src/MTS_IO_SerialConnection.cpp index a9f3d6e..c91f5d5 100644 --- a/src/MTS_IO_SerialConnection.cpp +++ b/src/MTS_IO_SerialConnection.cpp @@ -254,6 +254,77 @@ int SerialConnection::getFileDescriptor() { return h; } +const char* SerialConnection::humanSpeed(speed_t speed) { + const char *hspeed; + switch (speed) { + case B0: hspeed = "B0"; + break; + case B50: hspeed = "B50"; + break; + case B75: hspeed = "B75"; + break; + case B110: hspeed = "B110"; + break; + case B134: hspeed = "B134"; + break; + case B150: hspeed = "B150"; + break; + case B200: hspeed = "B200"; + break; + case B300: hspeed = "B300"; + break; + case B600: hspeed = "B600"; + break; + case B1200: hspeed = "B1200"; + break; + case B1800: hspeed = "B1800"; + break; + case B2400: hspeed = "B2400"; + break; + case B4800: hspeed = "B4800"; + break; + case B9600: hspeed = "B9600"; + break; + case B19200: hspeed = "B19200"; + break; + case B57600: hspeed = "B57600"; + break; + case B115200: hspeed = "B115200"; + break; + default: hspeed = "unknown"; + } + return hspeed; +} + +void SerialConnection::printPortSetting(const termios *options){ + printDebug("SERIAL| port settings:"); + printDebug("SERIAL| in speed:%s out speed:%s", + humanSpeed(cfgetispeed(options)), + humanSpeed(cfgetospeed(options))); + int data_length=0; + if ((options->c_cflag&CSIZE) == CS5) { + data_length = 5; + } + if ((options->c_cflag&CSIZE) == CS6) { + data_length = 6; + } + if ((options->c_cflag&CSIZE) == CS7) { + data_length = 7; + } + if ((options->c_cflag&CSIZE) == CS8) { + data_length = 8; + } + printDebug("SERIAL| data:%d stop bits:%d", data_length,options->c_cflag&CSTOPB?2:1); + if (!(options->c_cflag&PARENB)) { + printDebug("SERIAL| parity: NONE"); + } else if (options->c_cflag&PARODD) { + printDebug("SERIAL| parity: ODD"); + } else { + printDebug("SERIAL| parity: EVEN"); + } + printDebug("SERIAL| CRTSCTS:%d", options->c_cflag&CRTSCTS?1:0); +} + bool SerialConnection::doOpen(const int32_t& timeoutMillis) { m_apHandleLock->lock(); @@ -412,8 +483,9 @@ bool SerialConnection::doOpen(const int32_t& timeoutMillis) { //Set Control Modes options.c_cc[VMIN] = 0; options.c_cc[VTIME] = 2; // tenths of seconds - + printPortSetting(&options); result = tcsetattr(m_iHandle, TCSANOW, &options); + if (result == -1) { printWarning("SERIAL| Failed to set configurations on port [%s] [%d]%s", m_sPortName.c_str(), errno, errno == 13 ? " (Permission Denied)" : ""); -- cgit v1.2.3 From 383d4a2f1f54c8adf8b1dd86baa7edd4440e76d4 Mon Sep 17 00:00:00 2001 From: Serhii Voloshynov Date: Fri, 28 Feb 2020 10:49:45 +0200 Subject: fix unitialized m_eFlowControl and serial settings --- src/MTS_IO_SerialConnection.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/MTS_IO_SerialConnection.cpp b/src/MTS_IO_SerialConnection.cpp index c91f5d5..a12a09b 100644 --- a/src/MTS_IO_SerialConnection.cpp +++ b/src/MTS_IO_SerialConnection.cpp @@ -50,6 +50,7 @@ SerialConnection::Builder::Builder(const std::string& sPortName) , m_eParity(OFF) , m_eDataBits(B8) , m_eStopBits(B1) +, m_eFlowControl(NONE) , m_bBuilt(false) { @@ -424,6 +425,7 @@ bool SerialConnection::doOpen(const int32_t& timeoutMillis) { } //Set Parity, Stop Bits, and Data Length + options.c_cflag &= ~(PARODD | PARENB); switch(m_eParity) { case ODD: options.c_cflag |= ( PARODD | PARENB ); @@ -434,8 +436,6 @@ bool SerialConnection::doOpen(const int32_t& timeoutMillis) { break; case OFF: - default: - options.c_cflag &= ~PARENB; break; } @@ -461,7 +461,7 @@ bool SerialConnection::doOpen(const int32_t& timeoutMillis) { switch(m_eStopBits) { case B2: - options.c_cflag &= CSTOPB; + options.c_cflag |= CSTOPB; break; case B1: @@ -472,11 +472,11 @@ bool SerialConnection::doOpen(const int32_t& timeoutMillis) { switch (m_eFlowControl) { case RTS_CTS: - options.c_cflag &= CRTSCTS; + options.c_cflag |= CRTSCTS; break; case NONE: default: - options.c_cflag &= ~(IXON | IXOFF | IXANY); + options.c_cflag &= ~CRTSCTS; break; } -- cgit v1.2.3 From b954f9d5f9126a993f30eb220645b4e8797d9fcc Mon Sep 17 00:00:00 2001 From: Serhii Voloshynov Date: Fri, 28 Feb 2020 16:48:40 +0200 Subject: fixes after codereview --- src/MTS_IO_SerialConnection.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/MTS_IO_SerialConnection.cpp b/src/MTS_IO_SerialConnection.cpp index a12a09b..936d28d 100644 --- a/src/MTS_IO_SerialConnection.cpp +++ b/src/MTS_IO_SerialConnection.cpp @@ -436,6 +436,7 @@ bool SerialConnection::doOpen(const int32_t& timeoutMillis) { break; case OFF: + default: break; } @@ -472,12 +473,12 @@ bool SerialConnection::doOpen(const int32_t& timeoutMillis) { switch (m_eFlowControl) { case RTS_CTS: - options.c_cflag |= CRTSCTS; + options.c_cflag |= CRTSCTS; break; case NONE: default: - options.c_cflag &= ~CRTSCTS; - break; + options.c_cflag &= ~CRTSCTS; + break; } //Set Control Modes -- cgit v1.2.3 From 620bd2637b40d718dd5886ce7df0a19b73ea8875 Mon Sep 17 00:00:00 2001 From: Maksym Telychko Date: Tue, 24 Mar 2020 17:41:04 +0200 Subject: MTX-3262 mpower: lockfile rewrite for interprocess communication safety Previous implementation was not thread/interprocess safe due to pid management Fixes: Single instance guard. --- src/MTS_IO_LockFile.cpp | 92 ++++++++++++++----------------------------------- 1 file changed, 25 insertions(+), 67 deletions(-) (limited to 'src') diff --git a/src/MTS_IO_LockFile.cpp b/src/MTS_IO_LockFile.cpp index b74ba73..4312ce4 100644 --- a/src/MTS_IO_LockFile.cpp +++ b/src/MTS_IO_LockFile.cpp @@ -18,9 +18,9 @@ * */ -/*! +/*! \file MTS_IO_LockFile.cpp - \brief A brief description + \brief A brief description \date Oct 8, 2014 \author sgodinez @@ -29,13 +29,7 @@ #include #include -#include -#include -#include #include -#include -#include -#include #include using namespace MTS::IO; @@ -43,79 +37,43 @@ using namespace MTS::IO; LockFile::LockFile(const std::string& sFile) : m_sFile(sFile) , m_iLockFd(-1) +, m_iLockErr(-1) { - } -LockFile::~LockFile() { +LockFile::~LockFile() +{ unlock(); } -bool LockFile::lock(uint32_t attemptMillis) { - if(isLocked()) { +bool LockFile::lock(uint32_t attemptMillis) +{ + if (isLocked()) return true; - } MTS::Timer timer; - timer.start(); - while(timer.getMillis() < attemptMillis) { - m_iLockFd =::open(m_sFile.c_str(), O_CREAT | O_WRONLY | O_TRUNC | O_EXCL, 0644); - if (m_iLockFd < 0) { - // device already locked -> bail out - printWarning("LockFile| Failed to Lock [%s] [%d][%s]", m_sFile.c_str(), errno, strerror(errno)); - - //Check if lock file's process still exists - std::string sResult; - if(MTS::System::readFile(m_sFile, sResult) == 0 && sResult.size() > 0) { - struct stat sts; - std::string sProc = "/proc/" + MTS::Text::trim(sResult); - if (stat(sProc.c_str(), &sts) == -1 && errno == ENOENT) { - printWarning("LockFile| Current Lock's Process [%s] does not exist. Removing lock.", sResult.c_str()); - // process doesn't exist -> remove file - ::unlink(m_sFile.c_str()); - } - } - - } else { - //TODO: Investigate using flock in addition to minicom-style lock : flock(m_iLockFd, LOCK_EX | LOCK_NB); - - // %4d to make concurrent mgetty (if any) happy. - // Mgetty treats 4-bytes lock files as binary, - // not text, PID. Making 5+ char file. Brrr... - char buf[256] = {0}; - size_t written = 0; - - sprintf(buf, "%4d\n", getpid()); - size_t wsize = strlen(buf); - - written = write(m_iLockFd, buf, wsize); - - if (wsize != written) { - printError("LockFile| Error writing to lock file [%s] [%d][%s]", m_sFile.c_str(), errno, strerror(errno)); - close(m_iLockFd); - return false; - } - else { - close(m_iLockFd); - return true; - } - } - uint32_t randomSleepTime = ((rand() % 10) + 1) * 100000; //Sleep from 100ms to 1 Second - ::usleep(randomSleepTime); + m_iLockFd = ::open(m_sFile.c_str(), O_CREAT | O_RDWR, 0666); + while (timer.getMillis() < attemptMillis) { + m_iLockErr = ::flock(m_iLockFd, LOCK_EX | LOCK_NB); + if (m_iLockErr == 0) + return true; + ::usleep(((rand() % 10) + 1) * 100000); //Sleep from 100ms to 1 Second } + ::close(m_iLockFd); + m_iLockFd = -1; return false; } -void LockFile::unlock() { - if(isLocked()) { - ::unlink(m_sFile.c_str()); - //TODO: flock cleanup : flock(m_iLockFd, LOCK_UN); - m_iLockFd = -1; - } +void LockFile::unlock() +{ + ::flock(m_iLockFd, LOCK_UN); + ::close(m_iLockFd); + m_iLockFd = -1; + m_iLockErr = -1; } -bool LockFile::isLocked() { - return m_iLockFd >= 0; +bool LockFile::isLocked() +{ + return m_iLockErr == 0; } - -- cgit v1.2.3 From 2418037578c64516803d0ac261545c1b8fb69ac2 Mon Sep 17 00:00:00 2001 From: Maksym Telychko Date: Tue, 24 Mar 2020 19:13:58 +0200 Subject: MTX-3262 mpower lockfile: additional fd check on lock --- src/MTS_IO_LockFile.cpp | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/MTS_IO_LockFile.cpp b/src/MTS_IO_LockFile.cpp index 4312ce4..842ffb7 100644 --- a/src/MTS_IO_LockFile.cpp +++ b/src/MTS_IO_LockFile.cpp @@ -48,19 +48,23 @@ LockFile::~LockFile() bool LockFile::lock(uint32_t attemptMillis) { - if (isLocked()) + if (isLocked()) { return true; + } - MTS::Timer timer; - timer.start(); m_iLockFd = ::open(m_sFile.c_str(), O_CREAT | O_RDWR, 0666); - while (timer.getMillis() < attemptMillis) { - m_iLockErr = ::flock(m_iLockFd, LOCK_EX | LOCK_NB); - if (m_iLockErr == 0) - return true; - ::usleep(((rand() % 10) + 1) * 100000); //Sleep from 100ms to 1 Second + if (m_iLockFd >= 0) { + MTS::Timer timer; + timer.start(); + while (timer.getMillis() < attemptMillis) { + m_iLockErr = ::flock(m_iLockFd, LOCK_EX | LOCK_NB); + if (m_iLockErr == 0) { + return true; + } + ::usleep(((rand() % 10) + 1) * 100000); //Sleep from 100ms to 1 Second + } + ::close(m_iLockFd); } - ::close(m_iLockFd); m_iLockFd = -1; return false; } -- cgit v1.2.3 From b565b66b48e2ced3a6b2c6c327e458213b08201e Mon Sep 17 00:00:00 2001 From: Maksym Telychko Date: Wed, 25 Mar 2020 22:28:30 +0200 Subject: MTX-3262 mpower lockfile: flock error check --- src/MTS_IO_LockFile.cpp | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src') diff --git a/src/MTS_IO_LockFile.cpp b/src/MTS_IO_LockFile.cpp index 842ffb7..ce6655c 100644 --- a/src/MTS_IO_LockFile.cpp +++ b/src/MTS_IO_LockFile.cpp @@ -60,6 +60,8 @@ bool LockFile::lock(uint32_t attemptMillis) m_iLockErr = ::flock(m_iLockFd, LOCK_EX | LOCK_NB); if (m_iLockErr == 0) { return true; + } else if (errno != EWOULDBLOCK) { + break; } ::usleep(((rand() % 10) + 1) * 100000); //Sleep from 100ms to 1 Second } -- cgit v1.2.3 From eb58146a8e5387968a2b45367ad7fd29f6db2fbe Mon Sep 17 00:00:00 2001 From: Maksym Telychko Date: Mon, 30 Mar 2020 14:36:58 +0300 Subject: MTX-3262 mpower lockfile: fix file access rights --- src/MTS_IO_LockFile.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/MTS_IO_LockFile.cpp b/src/MTS_IO_LockFile.cpp index ce6655c..7f84f42 100644 --- a/src/MTS_IO_LockFile.cpp +++ b/src/MTS_IO_LockFile.cpp @@ -52,7 +52,7 @@ bool LockFile::lock(uint32_t attemptMillis) return true; } - m_iLockFd = ::open(m_sFile.c_str(), O_CREAT | O_RDWR, 0666); + m_iLockFd = ::open(m_sFile.c_str(), O_CREAT | O_RDWR, 0644); if (m_iLockFd >= 0) { MTS::Timer timer; timer.start(); -- cgit v1.2.3 From 04b90430c91f4257ebeff3bcccbe8c5d7413e7ae Mon Sep 17 00:00:00 2001 From: "mykola.salomatin" Date: Fri, 29 May 2020 11:44:57 +0300 Subject: mPower Oct20: L4G1 libmts-io support --- src/MTS_IO_CellularRadioFactory.cpp | 9 +++++-- src/MTS_IO_EG25Radio.cpp | 52 +++++++++++++++++++++++++++++++++++++ src/MTS_IO_EG95Radio.cpp | 40 +--------------------------- src/MTS_IO_ICellularRadio.cpp | 23 ++++++++++++++++ src/MTS_IO_QuectelRadio.cpp | 34 ++++++++++++++++++++++++ 5 files changed, 117 insertions(+), 41 deletions(-) create mode 100644 src/MTS_IO_EG25Radio.cpp (limited to 'src') diff --git a/src/MTS_IO_CellularRadioFactory.cpp b/src/MTS_IO_CellularRadioFactory.cpp index 08c6315..1a2cee2 100644 --- a/src/MTS_IO_CellularRadioFactory.cpp +++ b/src/MTS_IO_CellularRadioFactory.cpp @@ -37,6 +37,7 @@ #include #include #include "mts/MTS_IO_EG95Radio.h" +#include "mts/MTS_IO_EG25Radio.h" #include using namespace MTS::IO; @@ -60,6 +61,7 @@ CellularRadioFactory::CellularRadioFactory() { m_mCreationMap[CE910Radio::MODEL_NAME] = &CellularRadioFactory::createCE910; m_mCreationMap[LE866A1JSRadio::MODEL_NAME] = &CellularRadioFactory::createLE866A1JS; m_mCreationMap[EG95Radio::MODEL_NAME] = &CellularRadioFactory::createEG95Radio; + m_mCreationMap[EG25Radio::MODEL_NAME] = &CellularRadioFactory::createEG25Radio; } ICellularRadio* CellularRadioFactory::create(const std::string& sModel, const std::string& sPort) { @@ -191,7 +193,10 @@ ICellularRadio* CellularRadioFactory::createLE866A1JS(const std::string &sPort) return new LE866A1JSRadio(sPort); } -ICellularRadio* CellularRadioFactory::createEG95Radio(const std::string& sPort) const -{ +ICellularRadio* CellularRadioFactory::createEG95Radio(const std::string& sPort) const { return new EG95Radio(sPort); } + +ICellularRadio* CellularRadioFactory::createEG25Radio(const std::string& sPort) const { + return new EG25Radio(sPort); +} diff --git a/src/MTS_IO_EG25Radio.cpp b/src/MTS_IO_EG25Radio.cpp new file mode 100644 index 0000000..aa5d453 --- /dev/null +++ b/src/MTS_IO_EG25Radio.cpp @@ -0,0 +1,52 @@ +/* + * Copyright (C) 2019 by Multi-Tech Systems + * + * This file is part of libmts-io. + * + * libmts-io is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * libmts-io is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with libmts-io. If not, see . + * + */ + + +#include + +using namespace MTS::IO; + +const std::string EG25Radio::MODEL_NAME("EG25"); + +EG25Radio::EG25Radio(const std::string& sPort) +: QuectelRadio(MODEL_NAME, sPort) +{ + +} + +EG25Radio::~EG25Radio() { + +} + +ICellularRadio::CODE EG25Radio::setRxDiversity(const Json::Value& jArgs) { + /* Command string for EG25 radios: AT+QCFG="diversity",(0-1) */ + if (jArgs["enabled"].asString() != "1" && jArgs["enabled"].asString() != "0") { + return FAILURE; + } + std::string sCmd = "AT+QCFG=\"diversity\","; + sCmd += jArgs["enabled"].asString(); + + return sendBasicCommand(sCmd); +} + +ICellularRadio::CODE EG25Radio::getSupportedCellularModes(CELLULAR_MODES &networks) { + networks = static_cast(CELLULAR_MODE_2G | CELLULAR_MODE_3G | CELLULAR_MODE_4G); + return SUCCESS; +} \ No newline at end of file diff --git a/src/MTS_IO_EG95Radio.cpp b/src/MTS_IO_EG95Radio.cpp index f1c040a..296f183 100644 --- a/src/MTS_IO_EG95Radio.cpp +++ b/src/MTS_IO_EG95Radio.cpp @@ -18,14 +18,8 @@ * */ - #include -#include - -#include -#include - using namespace MTS::IO; const std::string EG95Radio::MODEL_NAME("EG95"); @@ -54,36 +48,4 @@ ICellularRadio::CODE EG95Radio::setRxDiversity(const Json::Value& jArgs) { ICellularRadio::CODE EG95Radio::getSupportedCellularModes(CELLULAR_MODES &networks) { networks = static_cast(CELLULAR_MODE_2G | CELLULAR_MODE_3G | CELLULAR_MODE_4G); return SUCCESS; -} - -ICellularRadio::CODE EG95Radio::setCellularMode(CELLULAR_MODES networks) { - std::string prefNet; - unsigned int prefOnly = 0, prefCount = 0; - for (int i = sizeof(networks)*CHAR_BIT-1; i>=0; --i){ - switch (1<getFirmware(sValue); + if (eCode != SUCCESS) { + sCode = VALUE_NOT_SUPPORTED; + eCode = ERROR; + } else if (sValue.find("EG25G") != std::string::npos) { + sCode = "L4G1"; + eCode = SUCCESS; + } else { + sCode = VALUE_NOT_SUPPORTED; + eCode = ERROR; + } + } } else { sCode = VALUE_NOT_SUPPORTED; printError("RADIO| Could not identify MTS short code from model. [%s]", sModel.c_str()); @@ -300,6 +318,9 @@ MTS::IO::ICellularRadio::CODE MTS::IO::ICellularRadio::convertModelToType(const } else if (sModel.find("EG95") == 0) { sType = VALUE_TYPE_LTE; eCode = SUCCESS; + } else if (sModel.find("EG25") == 0) { + sType = VALUE_TYPE_LTE; + eCode = SUCCESS; } else { sType = VALUE_TYPE_GSM; eCode = ERROR; @@ -445,6 +466,8 @@ std::string MTS::IO::ICellularRadio::extractModelFromResult(const std::string& s sModel = "CE910"; } else if(sResult.find("EG95") != std::string::npos) { sModel = "EG95"; + } else if(sResult.find("EG25") != std::string::npos) { + sModel = "EG25"; } return sModel; } diff --git a/src/MTS_IO_QuectelRadio.cpp b/src/MTS_IO_QuectelRadio.cpp index c4bdc56..5024f54 100644 --- a/src/MTS_IO_QuectelRadio.cpp +++ b/src/MTS_IO_QuectelRadio.cpp @@ -20,6 +20,8 @@ #include "mts/MTS_IO_QuectelRadio.h" +#include + #include #include #include @@ -568,3 +570,35 @@ ICellularRadio::CODE QuectelRadio::convertToActiveBand(const std::string& sQuect return SUCCESS; } + +ICellularRadio::CODE QuectelRadio::setCellularMode(CELLULAR_MODES networks) { + std::string prefNet; + unsigned int prefOnly = 0, prefCount = 0; + for (int i = sizeof(networks)*CHAR_BIT-1; i>=0; --i){ + switch (1<