From dfaedb450b4eb4c8b6165da9383771f801cb8609 Mon Sep 17 00:00:00 2001 From: SASANO Takayoshi Date: Thu, 2 Jul 2020 06:40:26 +0900 Subject: [PATCH 1/3] replace rand() -> MT19937 random number generator sometimes rand() makes problem so replace it to MT19937 in C++11 . --- DMRNetwork.cpp | 18 +++++++++++------- DMRNetwork.h | 2 ++ DStarNetwork.cpp | 11 +++++++---- DStarNetwork.h | 2 ++ 4 files changed, 22 insertions(+), 11 deletions(-) diff --git a/DMRNetwork.cpp b/DMRNetwork.cpp index b2aedb5..fece49c 100644 --- a/DMRNetwork.cpp +++ b/DMRNetwork.cpp @@ -66,7 +66,8 @@ m_height(0), m_location(), m_description(), m_url(), -m_beacon(false) +m_beacon(false), +m_random() { assert(!address.empty()); assert(port > 0U); @@ -85,11 +86,13 @@ m_beacon(false) m_id[2U] = id >> 8; m_id[3U] = id >> 0; - CStopWatch stopWatch; - ::srand(stopWatch.start()); + std::random_device rd; + std::mt19937 mt(rd()); + m_random = mt; - m_streamId[0U] = ::rand() + 1U; - m_streamId[1U] = ::rand() + 1U; + std::uniform_int_distribution dist(0x00000001, 0xfffffffe); + m_streamId[0U] = dist(m_random); + m_streamId[1U] = dist(m_random); } CDMRNetwork::~CDMRNetwork() @@ -246,6 +249,7 @@ bool CDMRNetwork::write(const CDMRData& data) unsigned int slotIndex = slotNo - 1U; + std::uniform_int_distribution dist(0x00000001, 0xfffffffe); unsigned char dataType = data.getDataType(); if (dataType == DT_VOICE_SYNC) { buffer[15U] |= 0x10U; @@ -253,10 +257,10 @@ bool CDMRNetwork::write(const CDMRData& data) buffer[15U] |= data.getN(); } else { if (dataType == DT_VOICE_LC_HEADER) - m_streamId[slotIndex] = ::rand() + 1U; + m_streamId[slotIndex] = dist(m_random); if (dataType == DT_CSBK || dataType == DT_DATA_HEADER) - m_streamId[slotIndex] = ::rand() + 1U; + m_streamId[slotIndex] = dist(m_random); buffer[15U] |= (0x20U | dataType); } diff --git a/DMRNetwork.h b/DMRNetwork.h index 9d1c52e..754c970 100644 --- a/DMRNetwork.h +++ b/DMRNetwork.h @@ -27,6 +27,7 @@ #include #include +#include class CDMRNetwork { @@ -106,6 +107,7 @@ private: std::string m_url; bool m_beacon; + std::mt19937 m_random; bool writeLogin(); bool writeAuthorisation(); diff --git a/DStarNetwork.cpp b/DStarNetwork.cpp index de0af69..d13d773 100644 --- a/DStarNetwork.cpp +++ b/DStarNetwork.cpp @@ -44,14 +44,16 @@ m_inId(0U), m_buffer(1000U, "D-Star Network"), m_pollTimer(1000U, 60U), m_linkStatus(LS_NONE), -m_linkReflector(NULL) +m_linkReflector(NULL), +m_random() { m_address = CUDPSocket::lookup(gatewayAddress); m_linkReflector = new unsigned char[DSTAR_LONG_CALLSIGN_LENGTH]; - CStopWatch stopWatch; - ::srand(stopWatch.start()); + std::random_device rd; + std::mt19937 mt(rd()); + m_random = mt; } CDStarNetwork::~CDStarNetwork() @@ -85,7 +87,8 @@ bool CDStarNetwork::writeHeader(const unsigned char* header, unsigned int length buffer[4] = busy ? 0x22U : 0x20U; // Create a random id for this transmission - m_outId = (::rand() % 65535U) + 1U; + std::uniform_int_distribution dist(0x0001, 0xfffe); + m_outId = dist(m_random); buffer[5] = m_outId / 256U; // Unique session id buffer[6] = m_outId % 256U; diff --git a/DStarNetwork.h b/DStarNetwork.h index aeebbbf..991c031 100644 --- a/DStarNetwork.h +++ b/DStarNetwork.h @@ -26,6 +26,7 @@ #include #include +#include class CDStarNetwork { public: @@ -64,6 +65,7 @@ private: CTimer m_pollTimer; LINK_STATUS m_linkStatus; unsigned char* m_linkReflector; + std::mt19937 m_random; bool writePoll(const char* text); }; From 4217ccebc4266f972db50d11e64df356b9ab54f5 Mon Sep 17 00:00:00 2001 From: SASANO Takayoshi Date: Thu, 2 Jul 2020 06:59:51 +0900 Subject: [PATCH 2/3] remove getLocalAddress() It was used for NXDNKenwoodNetwork.cpp to creates SSRC. currently SSRC uses random value, this function is no longer used. --- UDPSocket.cpp | 28 ---------------------------- UDPSocket.h | 2 -- 2 files changed, 30 deletions(-) diff --git a/UDPSocket.cpp b/UDPSocket.cpp index 2899faf..abfa885 100644 --- a/UDPSocket.cpp +++ b/UDPSocket.cpp @@ -258,31 +258,3 @@ void CUDPSocket::close() ::close(m_fd); #endif } - -unsigned long CUDPSocket::getLocalAddress() const -{ - unsigned long address = 0UL; - - char hostname[80U]; - int ret = ::gethostname(hostname, 80); - if (ret == -1) - return 0UL; - - struct hostent* phe = ::gethostbyname(hostname); - if (phe == NULL) - return 0UL; - - if (phe->h_addrtype != AF_INET) - return 0UL; - - for (unsigned int i = 0U; phe->h_addr_list[i] != NULL; i++) { - struct in_addr addr; - ::memcpy(&addr, phe->h_addr_list[i], sizeof(struct in_addr)); - if (addr.s_addr != INADDR_LOOPBACK) { - address = addr.s_addr; - break; - } - } - - return address; -} diff --git a/UDPSocket.h b/UDPSocket.h index 4c21a43..4aa16bd 100644 --- a/UDPSocket.h +++ b/UDPSocket.h @@ -47,8 +47,6 @@ public: void close(); - unsigned long getLocalAddress() const; - static in_addr lookup(const std::string& hostName); private: From 15a8e87c3ecc9c770271de65b1937b5e433e152b Mon Sep 17 00:00:00 2001 From: SASANO Takayoshi Date: Thu, 2 Jul 2020 18:47:33 +0900 Subject: [PATCH 3/3] permit inline comment and ScreenLayout of Nextion two minor improvements - ScreenLayout in Nextion section can use hexadecimal (0xXXXXX), octal (0XXXX) and decimal value - in-line comment enabled example: # conventional comment, the line starts with # [Section] Key=value # this is new style comment Key="quoted value # this is not comment" Key="quoted value" # this is prohibited (not comment) --- Conf.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Conf.cpp b/Conf.cpp index 5bc7dba..6602b3c 100644 --- a/Conf.cpp +++ b/Conf.cpp @@ -390,6 +390,9 @@ bool CConf::read() if (len > 1U && *value == '"' && value[len - 1U] == '"') { value[len - 1U] = '\0'; value++; + } else { + // if value is not quoted, remove after # (to make comment) + strtok(value, "#"); } if (section == SECTION_GENERAL) { @@ -916,7 +919,7 @@ bool CConf::read() else if (::strcmp(key, "IdleBrightness") == 0) m_nextionIdleBrightness = (unsigned int)::atoi(value); else if (::strcmp(key, "ScreenLayout") == 0) - m_nextionScreenLayout = (unsigned int)::atoi(value); + m_nextionScreenLayout = (unsigned int)::strtoul(value, NULL, 0); else if (::strcmp(key, "DisplayTempInFahrenheit") == 0) m_nextionTempInFahrenheit = ::atoi(value) == 1; } else if (section == SECTION_OLED) {