From 1b68cd0ef71b549f358e580dc7d2083611c040ac Mon Sep 17 00:00:00 2001 From: Thomas Kunze Date: Mon, 19 Aug 2024 23:24:21 +0200 Subject: [PATCH] no more std::string --- examples/knx-cc1310/CMakeLists.txt | 4 +- src/CMakeLists.txt | 1 + src/knx/apdu.cpp | 16 +++---- src/knx/apdu.h | 8 ++-- src/knx/application_layer.cpp | 18 ++++---- src/knx/bits.cpp | 24 +--------- src/knx/bits.h | 6 +-- src/knx/cemi_frame.cpp | 37 ++++++++------- src/knx/cemi_frame.h | 8 ++-- src/knx/data_link_layer.cpp | 6 +-- src/knx/ip/ip_data_link_layer.cpp | 36 +++++++-------- src/knx/ip/knx_ip_frame.cpp | 5 +- src/knx/knx_types.cpp | 18 ++++++-- src/knx/knx_types.h | 5 +- src/knx/memory.cpp | 14 +++--- src/knx/network_layer_device.cpp | 20 ++++---- src/knx/npdu.cpp | 11 +++-- src/knx/npdu.h | 6 +-- src/knx/platform/linux_platform.cpp | 1 - src/knx/platform/linux_platform.h | 2 +- src/knx/property.h | 1 - src/knx/secure_application_layer.cpp | 12 ++--- src/knx/tp_frame.cpp | 26 +++++++++++ src/knx/tp_frame.h | 47 ++++++------------- src/knx/tpdu.cpp | 18 ++++---- src/knx/tpdu.h | 6 +-- src/knx/tpuart_data_link_layer.cpp | 33 +++----------- src/knx/tpuart_data_link_layer.h | 2 - src/knx/transport_layer.cpp | 26 +++++------ src/knx/usb_tunnel_interface.cpp | 3 +- src/knx/util/logger.cpp | 37 +++++++++++---- src/knx/util/logger.h | 68 +++++++++++++++++++++++++++- 32 files changed, 293 insertions(+), 232 deletions(-) create mode 100644 src/knx/tp_frame.cpp diff --git a/examples/knx-cc1310/CMakeLists.txt b/examples/knx-cc1310/CMakeLists.txt index 8a69c0a..38c137e 100644 --- a/examples/knx-cc1310/CMakeLists.txt +++ b/examples/knx-cc1310/CMakeLists.txt @@ -164,6 +164,8 @@ set(${PROJECT_NAME}_SOURCES ../../src/knx_facade.h ../../src/knx/platform/cc1310_platform.cpp ../../src/knx_facade.cpp + ../../src/knx/knx_types.h + ../../src/knx/knx_types.cpp ./RTT/SEGGER_RTT_Conf.h ./RTT/SEGGER_RTT_printf.c ./RTT/SEGGER_RTT.c @@ -189,7 +191,7 @@ include_directories( ${SimpleLinkCC13X0SDK_INCLUDE_DIRS} ) -add_definitions(-DMASK_VERSION=0x27B0 -Wno-unknown-pragmas) +add_definitions(-DMASK_VERSION=0x27B0 -Wno-unknown-pragmas -DKNX_NO_PRINT) add_executable(${PROJECT_NAME} ${${PROJECT_NAME}_SOURCES} diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 46f41ba..fd0c71c 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -167,6 +167,7 @@ knx/tpdu.cpp knx/tpdu.h knx/tpuart_data_link_layer.cpp knx/tpuart_data_link_layer.h +knx/tp_frame.cpp knx/tp_frame.h knx/transport_layer.cpp knx/transport_layer.h diff --git a/src/knx/apdu.cpp b/src/knx/apdu.cpp index e33e92d..1dc619a 100644 --- a/src/knx/apdu.cpp +++ b/src/knx/apdu.cpp @@ -40,22 +40,20 @@ uint8_t APDU::length() const return _frame.npdu().octetCount(); } -string APDU::toString() const +void APDU::printIt() const { #ifndef KNX_NO_PRINT - string value = std::string("APDU: ") + enum_name(type()) + " "; - value += byte2hex(_data[0] & 0x3); + print("APDU: "); + print(enum_name(type())); + print(" "); + print(_data[0] & 0x3, HEX); for (uint8_t i = 1; i < length() + 1; ++i) { if (i) - value += " "; + print(" "); - value += byte2hex(_data[i]); + print(_data[i], HEX); } - - return value; -#else - return ""; #endif } diff --git a/src/knx/apdu.h b/src/knx/apdu.h index c05d1b3..5033f5b 100644 --- a/src/knx/apdu.h +++ b/src/knx/apdu.h @@ -1,7 +1,7 @@ #pragma once #include -#include +#include "util/logger.h" #include "knx_types.h" class CemiFrame; @@ -9,7 +9,7 @@ class CemiFrame; /** * This class represents an Application Protocol Data Unit. It is part of a CemiFrame. */ -class APDU +class APDU : public IPrintable { friend class CemiFrame; @@ -35,9 +35,9 @@ class APDU */ uint8_t length() const; /** - * Convert APDU to string. + * print APDU */ - std::string toString() const; + void printIt() const; protected: /** diff --git a/src/knx/application_layer.cpp b/src/knx/application_layer.cpp index 55ef556..6f31b01 100644 --- a/src/knx/application_layer.cpp +++ b/src/knx/application_layer.cpp @@ -39,7 +39,7 @@ void ApplicationLayer::dataGroupIndication(HopCountType hopType, Priority priori void ApplicationLayer::dataGroupIndication(HopCountType hopType, Priority priority, uint16_t tsap, APDU& apdu, const SecurityControl& secCtrl) { - LOGGER.info("dataGroupIndication %s", apdu.toString().c_str()); + LOGGER.info("dataGroupIndication ", apdu); if (_assocTable == nullptr) return; @@ -92,7 +92,7 @@ void ApplicationLayer::dataGroupConfirm(AckType ack, HopCountType hopType, Prior void ApplicationLayer::dataGroupConfirm(AckType ack, HopCountType hopType, Priority priority, uint16_t tsap, APDU& apdu, const SecurityControl& secCtrl, bool status) { - LOGGER.info("dataGroupConfirm %s", apdu.toString().c_str()); + LOGGER.info("dataGroupConfirm ", apdu); switch (apdu.type()) { @@ -130,7 +130,7 @@ void ApplicationLayer::dataBroadcastIndication(HopCountType hopType, Priority pr void ApplicationLayer::dataBroadcastIndication(HopCountType hopType, Priority priority, uint16_t source, APDU& apdu, const SecurityControl& secCtrl) { - LOGGER.info("dataBroadcastIndication %s", apdu.toString().c_str()); + LOGGER.info("dataBroadcastIndication ", apdu); uint8_t* data = apdu.data(); switch (apdu.type()) @@ -190,7 +190,7 @@ void ApplicationLayer::dataBroadcastConfirm(AckType ack, HopCountType hopType, P void ApplicationLayer::dataBroadcastConfirm(AckType ack, HopCountType hopType, Priority priority, APDU& apdu, const SecurityControl& secCtrl, bool status) { - LOGGER.info("dataBroadcastConfirm %s", apdu.toString().c_str()); + LOGGER.info("dataBroadcastConfirm ", apdu); uint8_t* data = apdu.data(); switch (apdu.type()) @@ -245,7 +245,7 @@ void ApplicationLayer::dataSystemBroadcastIndication(HopCountType hopType, Prior void ApplicationLayer::dataSystemBroadcastIndication(HopCountType hopType, Priority priority, uint16_t source, APDU& apdu, const SecurityControl& secCtrl) { - LOGGER.info("dataSystemBroadcastIndication %s", apdu.toString().c_str()); + LOGGER.info("dataSystemBroadcastIndication ", apdu); const uint8_t* data = apdu.data(); switch (apdu.type()) @@ -294,7 +294,7 @@ void ApplicationLayer::dataSystemBroadcastConfirm(HopCountType hopType, Priority void ApplicationLayer::dataSystemBroadcastConfirm(HopCountType hopType, Priority priority, APDU& apdu, const SecurityControl& secCtrl, bool status) { - LOGGER.info("dataSystemBroadcastConfirm %s", apdu.toString().c_str()); + LOGGER.info("dataSystemBroadcastConfirm ", apdu); const uint8_t* data = apdu.data(); switch (apdu.type()) @@ -1118,7 +1118,7 @@ void ApplicationLayer::userMemorySend(ApduType type, AckType ack, Priority prior void ApplicationLayer::individualIndication(HopCountType hopType, Priority priority, uint16_t tsap, APDU& apdu, const SecurityControl& secCtrl) { - LOGGER.info("individualIndication %s", apdu.toString().c_str()); + LOGGER.info("individualIndication ", apdu); uint8_t* data = apdu.data(); switch (apdu.type()) @@ -1374,13 +1374,13 @@ void ApplicationLayer::individualIndication(HopCountType hopType, Priority prior } default: - LOGGER.warning("Individual-indication: unhandled APDU-Type: %s", apdu.toString().c_str()); + LOGGER.warning("Individual-indication: unhandled APDU-Type: ", apdu); } } void ApplicationLayer::individualConfirm(AckType ack, HopCountType hopType, Priority priority, uint16_t tsap, APDU& apdu, const SecurityControl& secCtrl, bool status) { - LOGGER.info("individualConfirm %s", apdu.toString().c_str()); + LOGGER.info("individualConfirm ", apdu); uint8_t* data = apdu.data(); switch (apdu.type()) diff --git a/src/knx/bits.cpp b/src/knx/bits.cpp index 50ff14c..3972ee7 100644 --- a/src/knx/bits.cpp +++ b/src/knx/bits.cpp @@ -1,3 +1,4 @@ +#include #include "bits.h" #include // for memcpy() @@ -9,29 +10,6 @@ const uint8_t* popByte(uint8_t& b, const uint8_t* data) } #ifndef KNX_NO_PRINT -std::string byte2hex(const uint8_t byte) -{ - const char* hex = "0123456789ABCDEF"; - char out[3] = {0}; - out[0] = hex[(byte >> 4) & 0xF]; - out[1] = hex[ byte & 0xF]; - return std::string(out); -} - -std::string word2hex(const uint16_t value) -{ - return byte2hex((uint8_t) (value & 0xFF00) >> 8) + byte2hex((uint8_t) (value & 0xFF)); -} - -std::string array2hex(const uint8_t* value, size_t length) -{ - std::string result(""); - - for (size_t i = 0; i < length; i++) - result += byte2hex(value[i]) + " "; - - return result; -} void printHex(const char* suffix, const uint8_t* data, size_t length, bool newline) { diff --git a/src/knx/bits.h b/src/knx/bits.h index 00b4fca..074dea3 100644 --- a/src/knx/bits.h +++ b/src/knx/bits.h @@ -1,8 +1,7 @@ #pragma once #include -#include -#include +#include #if defined(__linux__) #include @@ -71,9 +70,6 @@ #endif #ifndef KNX_NO_PRINT - std::string byte2hex(const uint8_t byte); - std::string word2hex(const uint16_t value); - std::string array2hex(const uint8_t* value, size_t length); void print(const char[]); void print(char); void print(unsigned char, int = DEC); diff --git a/src/knx/cemi_frame.cpp b/src/knx/cemi_frame.cpp index ed8db90..b2f446e 100644 --- a/src/knx/cemi_frame.cpp +++ b/src/knx/cemi_frame.cpp @@ -400,30 +400,29 @@ bool CemiFrame::valid() const return true; } -std::string CemiFrame::toString() const +void CemiFrame::printIt() const { #ifndef KNX_NO_PRINT - std::string value = std::string("DPDU:") + enum_name(frameType()) + " "; - value += enum_name(systemBroadcast()); - value += " "; - value += enum_name(ack()); - value += " "; - value += enum_name(repetition()); - value += " "; - value += enum_name(priority()); - value += " from "; - value += format_ia(sourceAddress()); - value += " to "; - value += enum_name(addressType()); - value += " "; + print("DPDU:"); + print(enum_name(frameType())); + print(" "); + print(enum_name(systemBroadcast())); + print(" "); + print(enum_name(ack())); + print(" "); + print(enum_name(repetition())); + print(" "); + print(enum_name(priority())); + print(" from "); + print_ia(sourceAddress()); + print(" to "); + print(enum_name(addressType())); + print(" "); if (addressType() == AddressType::IndividualAddress) - value += format_ia(destinationAddress()); + print_ia(destinationAddress()); else - value += format_ga(destinationAddress()); + print_ga(destinationAddress()); - return value; -#else - return ""; #endif } diff --git a/src/knx/cemi_frame.h b/src/knx/cemi_frame.h index d2ada47..bd30bcf 100644 --- a/src/knx/cemi_frame.h +++ b/src/knx/cemi_frame.h @@ -1,11 +1,13 @@ #pragma once #include "knx_types.h" -#include "stdint.h" +#include #include "npdu.h" #include "tpdu.h" #include "apdu.h" #include "config.h" +#include "util/logger.h" + #define NPDU_LPDU_DIFF 8 #define TPDU_NPDU_DIFF 1 @@ -16,7 +18,7 @@ // Mesg Code and additional info length #define CEMI_HEADER_SIZE 2 -class CemiFrame +class CemiFrame : public IPrintable { friend class DataLinkLayer; @@ -72,7 +74,7 @@ class CemiFrame uint8_t calcCrcTP(uint8_t* buffer, uint16_t len); bool valid() const; - std::string toString() const; + void printIt() const; private: uint8_t buffer[0xff + NPDU_LPDU_DIFF] = {0}; //only valid of add info is zero diff --git a/src/knx/data_link_layer.cpp b/src/knx/data_link_layer.cpp index 9de140d..bd5c641 100644 --- a/src/knx/data_link_layer.cpp +++ b/src/knx/data_link_layer.cpp @@ -59,7 +59,7 @@ bool DataLinkLayer::isTunnelAddress(uint16_t addr) } #endif -void DataLinkLayer::cddataRequestFromTunnel(CemiFrame& frame) +void DataLinkLayer::dataRequestFromTunnel(CemiFrame& frame) { _cemiServer->dataConfirmationToTunnel(frame); @@ -161,7 +161,7 @@ void DataLinkLayer::frameReceived(CemiFrame& frame) SystemBroadcast systemBroadcast = frame.systemBroadcast(); - LOGGER.info("frameReceived %s", frame.toString().c_str()); + LOGGER.info("frameReceived ", frame); #ifdef USE_CEMI_SERVER @@ -222,7 +222,7 @@ bool DataLinkLayer::sendTelegram(NPDU& npdu, AckType ack, uint16_t destinationAd else frame.frameType(format); - LOGGER.info("sendTelegram %s", frame.toString().c_str()); + LOGGER.info("sendTelegram ", frame); if (!frame.valid()) { diff --git a/src/knx/ip/ip_data_link_layer.cpp b/src/knx/ip/ip_data_link_layer.cpp index 901d321..581f2f2 100644 --- a/src/knx/ip/ip_data_link_layer.cpp +++ b/src/knx/ip/ip_data_link_layer.cpp @@ -10,25 +10,20 @@ #include "knx_ip_search_response_extended.h" #include "../util/logger.h" -const std::string ipaddr2str(const uint32_t addr) -{ - return to_string(addr & 0xFF000000 >> 24) + "." + to_string(addr & 0xFF0000 >> 16) + "." + to_string(addr & 0xFF00 >> 8) + "." + to_string(addr & 0xFF); -} - #define LOGGER Logger::logger("IpDataLinkLayer") - #include "knx_ip_connect_request.h" - #include "knx_ip_connect_response.h" - #include "knx_ip_state_request.h" - #include "knx_ip_state_response.h" - #include "knx_ip_disconnect_request.h" - #include "knx_ip_disconnect_response.h" - #include "knx_ip_tunneling_request.h" - #include "knx_ip_tunneling_ack.h" - #include "knx_ip_description_request.h" - #include "knx_ip_description_response.h" - #include "knx_ip_config_request.h" +#include "knx_ip_connect_request.h" +#include "knx_ip_connect_response.h" +#include "knx_ip_state_request.h" +#include "knx_ip_state_response.h" +#include "knx_ip_disconnect_request.h" +#include "knx_ip_disconnect_response.h" +#include "knx_ip_tunneling_request.h" +#include "knx_ip_tunneling_ack.h" +#include "knx_ip_description_request.h" +#include "knx_ip_description_response.h" +#include "knx_ip_config_request.h" #include @@ -304,6 +299,7 @@ void IpDataLinkLayer::loop() break; } } + #endif uint8_t buffer[512]; uint16_t remotePort = 0; @@ -358,6 +354,7 @@ void IpDataLinkLayer::loop() } #ifdef KNX_TUNNELING + case SearchRequestExt: { loopHandleSearchRequestExtended(buffer, len); @@ -413,9 +410,11 @@ void IpDataLinkLayer::loop() //println("got Ack"); break; } + #endif + default: - LOGGER.warning("Unhandled service identifier: %s", word2hex(code).c_str()); + LOGGER.warning("Unhandled service identifier: %x:", code); break; } } @@ -1148,7 +1147,8 @@ bool IpDataLinkLayer::sendUnicast(uint32_t addr, uint16_t port, KnxIpFrame& ipFr if (!_enabled) return false; - LOGGER.info("sendUnicast to %s:%d %s %s", ipaddr2str(addr), port, enum_name(ipFrame.protocolVersion()), enum_name(ipFrame.serviceTypeIdentifier())); + LOGGER.info("sendUnicast to %d.%d.%d.%d:%d %s %s", addr & 0xFF000000 >> 24, addr & 0xFF0000 >> 16, addr & 0xFF00 >> 8, addr & 0xFF + , port, enum_name(ipFrame.protocolVersion()), enum_name(ipFrame.serviceTypeIdentifier())); return _platform.sendBytesMultiCast(ipFrame.data(), ipFrame.totalLength()); } diff --git a/src/knx/ip/knx_ip_frame.cpp b/src/knx/ip/knx_ip_frame.cpp index c231ed8..83bc8cb 100644 --- a/src/knx/ip/knx_ip_frame.cpp +++ b/src/knx/ip/knx_ip_frame.cpp @@ -76,7 +76,7 @@ KnxIpFrame::KnxIpFrame(uint16_t length) protocolVersion(KnxIp1_0); totalLength(length); } - +#ifndef KNX_NO_PRINT const char* enum_name(const KnxIpVersion enum_val) { switch (enum_val) @@ -148,4 +148,5 @@ const char* enum_name(const KnxIpServiceType enum_val) } return ""; -} \ No newline at end of file +} +#endif \ No newline at end of file diff --git a/src/knx/knx_types.cpp b/src/knx/knx_types.cpp index d5efd0e..38d367a 100644 --- a/src/knx/knx_types.cpp +++ b/src/knx/knx_types.cpp @@ -1,4 +1,6 @@ #include "knx_types.h" +#include "bits.h" + #ifndef KNX_NO_PRINT const char* enum_name(const LCCONFIG enum_val) { @@ -691,13 +693,21 @@ const char* enum_name(const FrameFormat enum_val) return ""; } -const string format_ia(uint16_t ia) +void print_ia(const uint16_t ia) { - return to_string(ia & 0xF000 >> 24) + "/" + to_string(ia & 0x0F00 >> 16) + "/" + to_string(ia & 0x00FF); + print(ia & 0xF000 >> 24); + print("/"); + print(ia & 0x0F00 >> 16); + print("/"); + print(ia & 0x00FF); } -const string format_ga(uint16_t ga) +void print_ga(const uint16_t ga) { - return to_string(ga & 0xF800 >> 23) + "/" + to_string(ga & 0x70 >> 16) + "/" + to_string(ga & 0x00FF); + print(ga & 0xF800 >> 23); + print("/"); + print(ga & 0x70 >> 16); + print("/"); + print(ga & 0x00FF); } #endif \ No newline at end of file diff --git a/src/knx/knx_types.h b/src/knx/knx_types.h index 15846ed..3cf1e13 100644 --- a/src/knx/knx_types.h +++ b/src/knx/knx_types.h @@ -1,5 +1,4 @@ #pragma once -#include #include using namespace std; @@ -312,5 +311,5 @@ enum LCCONFIG }; const char* enum_name(const LCCONFIG enum_val); -const string format_ia(uint16_t ia); -const string format_ga(uint16_t ga); \ No newline at end of file +void print_ia(uint16_t ia); +void print_ga(uint16_t ga); \ No newline at end of file diff --git a/src/knx/memory.cpp b/src/knx/memory.cpp index e58d2f0..b2ecd57 100644 --- a/src/knx/memory.cpp +++ b/src/knx/memory.cpp @@ -27,7 +27,7 @@ void Memory::readMemory() return; } - LOGGER.info("content %s", array2hex(flashStart, _metadataSize).c_str()); + LOGGER.info("content %B", flashStart, _metadataSize); uint16_t metadataBlockSize = alignToPageSize(_metadataSize); @@ -70,17 +70,17 @@ void Memory::readMemory() else { LOGGER.warning("manufacturerId or hardwareType are different"); - LOGGER.warning("expexted manufacturerId: %s , stored manufacturerId: %s", - word2hex(_deviceObject.manufacturerId()), manufacturerId); - LOGGER.warning("expexted hardwareType: %s, stored hardwareType: %s", - array2hex(_deviceObject.hardwareType(), LEN_HARDWARE_TYPE), - array2hex(hardwareType, LEN_HARDWARE_TYPE)); + LOGGER.warning("expexted manufacturerId: %x , stored manufacturerId: %x", + _deviceObject.manufacturerId(), manufacturerId); + LOGGER.warning("expexted hardwareType: %B, stored hardwareType: %B", + _deviceObject.hardwareType(), LEN_HARDWARE_TYPE, + hardwareType, LEN_HARDWARE_TYPE); } } else { LOGGER.warning("DataObject api changed, any data stored in flash is invalid."); - LOGGER.warning("expexted DataObject api version: %s, stored api version: %s", word2hex(_deviceObject.apiVersion), word2hex(apiVersion)); + LOGGER.warning("expexted DataObject api version: %x, stored api version: %x", _deviceObject.apiVersion, apiVersion); } if (versionCheck == FlashAllInvalid) diff --git a/src/knx/network_layer_device.cpp b/src/knx/network_layer_device.cpp index 46b3783..37b9221 100644 --- a/src/knx/network_layer_device.cpp +++ b/src/knx/network_layer_device.cpp @@ -32,7 +32,7 @@ void NetworkLayerDevice::dataIndividualRequest(AckType ack, uint16_t destination // print.print("-> NL "); // tpdu.apdu().printPDU(); //} - LOGGER.info("dataIndividualRequest %s", npdu.toString().c_str()); + LOGGER.info("dataIndividualRequest ", npdu); _netLayerEntities[kInterfaceIndex].sendDataRequest(npdu, ack, destination, _deviceObj.individualAddress(), priority, IndividualAddress, Broadcast); } @@ -45,7 +45,7 @@ void NetworkLayerDevice::dataGroupRequest(AckType ack, uint16_t destination, Hop else npdu.hopCount(hopCount()); - LOGGER.info("dataGroupRequest %s", npdu.toString().c_str()); + LOGGER.info("dataGroupRequest ", npdu); _netLayerEntities[kInterfaceIndex].sendDataRequest(npdu, ack, destination, _deviceObj.individualAddress(), priority, GroupAddress, Broadcast); } @@ -58,7 +58,7 @@ void NetworkLayerDevice::dataBroadcastRequest(AckType ack, HopCountType hopType, else npdu.hopCount(hopCount()); - LOGGER.info("dataBroadcastRequest %s", npdu.toString().c_str()); + LOGGER.info("dataBroadcastRequest ", npdu); _netLayerEntities[kInterfaceIndex].sendDataRequest(npdu, ack, 0, _deviceObj.individualAddress(), priority, GroupAddress, Broadcast); } @@ -75,13 +75,13 @@ void NetworkLayerDevice::dataSystemBroadcastRequest(AckType ack, HopCountType ho else npdu.hopCount(hopCount()); - LOGGER.info("dataSystemBroadcastRequest %s", npdu.toString().c_str()); + LOGGER.info("dataSystemBroadcastRequest ", npdu); _netLayerEntities[kInterfaceIndex].sendDataRequest(npdu, ack, 0, _deviceObj.individualAddress(), priority, GroupAddress, broadcastType); } void NetworkLayerDevice::dataIndication(AckType ack, AddressType addrType, uint16_t destination, FrameFormat format, NPDU& npdu, Priority priority, uint16_t source, uint8_t srcIfIdx) { - LOGGER.info("dataIndication %s", npdu.toString().c_str()); + LOGGER.info("dataIndication ", npdu); HopCountType hopType = npdu.hopCount() == 7 ? UnlimitedRouting : NetworkLayerParameter; if (addrType == IndividualAddress) @@ -103,7 +103,7 @@ void NetworkLayerDevice::dataIndication(AckType ack, AddressType addrType, uint1 void NetworkLayerDevice::dataConfirm(AckType ack, AddressType addressType, uint16_t destination, FrameFormat format, Priority priority, uint16_t source, NPDU& npdu, bool status, uint8_t srcIfIdx) { - LOGGER.info("dataConfirm %s", npdu.toString().c_str()); + LOGGER.info("dataConfirm ", npdu); HopCountType hopType = npdu.hopCount() == 7 ? UnlimitedRouting : NetworkLayerParameter; if (addressType == IndividualAddress) @@ -122,7 +122,7 @@ void NetworkLayerDevice::dataConfirm(AckType ack, AddressType addressType, uint1 void NetworkLayerDevice::broadcastIndication(AckType ack, FrameFormat format, NPDU& npdu, Priority priority, uint16_t source, uint8_t srcIfIdx) { - LOGGER.info("broadcastIndication %s", npdu.toString().c_str()); + LOGGER.info("broadcastIndication ", npdu); HopCountType hopType = npdu.hopCount() == 7 ? UnlimitedRouting : NetworkLayerParameter; DptMedium mediumType = _netLayerEntities[srcIfIdx].mediumType(); @@ -143,21 +143,21 @@ void NetworkLayerDevice::broadcastIndication(AckType ack, FrameFormat format, NP void NetworkLayerDevice::broadcastConfirm(AckType ack, FrameFormat format, Priority priority, uint16_t source, NPDU& npdu, bool status, uint8_t srcIfIdx) { - LOGGER.info("broadcastConfirm %s", npdu.toString().c_str()); + LOGGER.info("broadcastConfirm ", npdu); HopCountType hopType = npdu.hopCount() == 7 ? UnlimitedRouting : NetworkLayerParameter; _transportLayer.dataBroadcastConfirm(ack, hopType, priority, npdu.tpdu(), status); } void NetworkLayerDevice::systemBroadcastIndication(AckType ack, FrameFormat format, NPDU& npdu, Priority priority, uint16_t source, uint8_t srcIfIdx) { - LOGGER.info("systemBroadcastIndication %s", npdu.toString().c_str()); + LOGGER.info("systemBroadcastIndication ", npdu); HopCountType hopType = npdu.hopCount() == 7 ? UnlimitedRouting : NetworkLayerParameter; _transportLayer.dataSystemBroadcastIndication(hopType, priority, source, npdu.tpdu()); } void NetworkLayerDevice::systemBroadcastConfirm(AckType ack, FrameFormat format, Priority priority, uint16_t source, NPDU& npdu, bool status, uint8_t srcIfIdx) { - LOGGER.info("systemBroadcastConfirm %s", npdu.toString().c_str()); + LOGGER.info("systemBroadcastConfirm ", npdu); HopCountType hopType = npdu.hopCount() == 7 ? UnlimitedRouting : NetworkLayerParameter; _transportLayer.dataSystemBroadcastConfirm(ack, hopType, npdu.tpdu(), priority, status); } diff --git a/src/knx/npdu.cpp b/src/knx/npdu.cpp index ce14c5a..6ee276c 100644 --- a/src/knx/npdu.cpp +++ b/src/knx/npdu.cpp @@ -1,7 +1,7 @@ #include "npdu.h" #include "cemi_frame.h" #include - +#include "bits.h" NPDU::NPDU(uint8_t* data, CemiFrame& frame): _data(data), _frame(frame) { @@ -43,7 +43,12 @@ TPDU& NPDU::tpdu() return _frame.tpdu(); } -const std::string NPDU::toString() const +void NPDU::printIt() const { - return std::string("NPDU: Octetcount:") + to_string(octetCount()) + " hopCount " + to_string(hopCount()); +#ifndef KNX_NO_PRINT + print("NPDU: Octetcount: "); + print(octetCount()); + print(" hopCount "); + print(hopCount()); +#endif } \ No newline at end of file diff --git a/src/knx/npdu.h b/src/knx/npdu.h index a578e6b..bd14ec6 100644 --- a/src/knx/npdu.h +++ b/src/knx/npdu.h @@ -1,12 +1,12 @@ #pragma once #include -#include +#include "util/logger.h" class CemiFrame; class TPDU; -class NPDU +class NPDU : public IPrintable { friend class CemiFrame; @@ -18,7 +18,7 @@ class NPDU void hopCount(uint8_t value); CemiFrame& frame(); TPDU& tpdu(); - const std::string toString() const; + void printIt() const; protected: NPDU(uint8_t* data, CemiFrame& frame); diff --git a/src/knx/platform/linux_platform.cpp b/src/knx/platform/linux_platform.cpp index 394ea12..c78a05a 100644 --- a/src/knx/platform/linux_platform.cpp +++ b/src/knx/platform/linux_platform.cpp @@ -2,7 +2,6 @@ #include "linux_platform.h" #include -#include #include #include #include diff --git a/src/knx/platform/linux_platform.h b/src/knx/platform/linux_platform.h index c7ee3ea..2eb48a5 100644 --- a/src/knx/platform/linux_platform.h +++ b/src/knx/platform/linux_platform.h @@ -2,8 +2,8 @@ #ifdef __linux__ -#include #include "../platform.h" +#include extern int gpio_direction(int pin, int dir); extern int gpio_read(int pin); diff --git a/src/knx/property.h b/src/knx/property.h index 46a899c..4c7fd9b 100644 --- a/src/knx/property.h +++ b/src/knx/property.h @@ -11,7 +11,6 @@ #include #include "save_restore.h" -#include /** The data type of a property. */ enum PropertyDataType diff --git a/src/knx/secure_application_layer.cpp b/src/knx/secure_application_layer.cpp index 39d0176..07200e6 100644 --- a/src/knx/secure_application_layer.cpp +++ b/src/knx/secure_application_layer.cpp @@ -660,7 +660,7 @@ void SecureApplicationLayer::sendSyncRequest(uint16_t dstAddr, bool dstAddrIsGro if (secure(request.data() + APDU_LPDU_DIFF, kSecureSyncRequest, _deviceObj.individualAddress(), dstAddr, dstAddrIsGroupAddr, tpci, asdu, sizeof(asdu), secCtrl, systemBcast)) { - LOGGER.info("SyncRequest: %s", request.apdu().toString().c_str()); + LOGGER.info("SyncRequest: ", request.apdu()); if (_syncReqBroadcastOutgoing) { @@ -723,7 +723,7 @@ void SecureApplicationLayer::sendSyncResponse(uint16_t dstAddr, bool dstAddrIsGr { _lastSyncRes = millis(); - LOGGER.info("SyncResponse: %s", response.apdu().c_str()); + LOGGER.info("SyncResponse: ", response.apdu()); if (_syncReqBroadcastIncoming) { @@ -1063,7 +1063,7 @@ bool SecureApplicationLayer::decrypt(uint8_t* plainApdu, uint16_t plainApduLengt bool SecureApplicationLayer::decodeSecureApdu(APDU& secureApdu, APDU& plainApdu, SecurityControl& secCtrl) { // Decode secure APDU - LOGGER.info("decodeSecureApdu: Secure APDU: %s", secureApdu.toString().c_str()); + LOGGER.info("decodeSecureApdu: Secure APDU: ", secureApdu); uint16_t srcAddress = secureApdu.frame().sourceAddress(); uint16_t dstAddress = secureApdu.frame().destinationAddress(); @@ -1085,7 +1085,7 @@ bool SecureApplicationLayer::decodeSecureApdu(APDU& secureApdu, APDU& plainApdu, // We are starting from TPCI octet (including): plainApdu.frame().data()+APDU_LPDU_DIFF if (decrypt(plainApdu.frame().data() + APDU_LPDU_DIFF, plainApdu.length() + 1, srcAddress, dstAddress, isDstAddrGroupAddr, tpci, secureApdu.data() + 1, secCtrl, isSystemBroadcast)) { - LOGGER.info("decodeSecureApdu: Plain APDU: %s", plainApdu.frame().apdu().toString().c_str()); + LOGGER.info("decodeSecureApdu: Plain APDU: ", plainApdu.frame().apdu()); return true; } @@ -1260,7 +1260,7 @@ bool SecureApplicationLayer::createSecureApdu(APDU& plainApdu, APDU& secureApdu, { // Create secure APDU - LOGGER.info("createSecureApdu: Plain APDU: %s", plainApdu.frame().apdu().toString().c_str()); + LOGGER.info("createSecureApdu: Plain APDU: ", plainApdu.frame().apdu()); uint16_t srcAddress = plainApdu.frame().sourceAddress(); uint16_t dstAddress = plainApdu.frame().destinationAddress(); @@ -1296,7 +1296,7 @@ bool SecureApplicationLayer::createSecureApdu(APDU& plainApdu, APDU& secureApdu, println(nextSequenceNumber(secCtrl.toolAccess), HEX); updateSequenceNumber(secCtrl.toolAccess, nextSequenceNumber(secCtrl.toolAccess) + 1); - LOGGER.info("createSecureApdu: Secure APDU: %s", secureApdu.frame().apdu().toString().c_str()); + LOGGER.info("createSecureApdu: Secure APDU: ", secureApdu.frame().apdu()); return true; } diff --git a/src/knx/tp_frame.cpp b/src/knx/tp_frame.cpp new file mode 100644 index 0000000..226fd33 --- /dev/null +++ b/src/knx/tp_frame.cpp @@ -0,0 +1,26 @@ +#include "tp_frame.h" +#include "bits.h" + + void TpFrame::printIt() const + { + print_ia(source()); + print(" -> "); + + if (isGroupAddress()) + print_ga(destination()); + else + print_ia(destination()); + + print(" ["); + print((flags() & TP_FRAME_FLAG_INVALID) ? 'I' : '_'); // Invalid + print((flags() & TP_FRAME_FLAG_EXTENDED) ? 'E' : '_'); // Extended + print((flags() & TP_FRAME_FLAG_REPEATED) ? 'R' : '_'); // Repeat + print((flags() & TP_FRAME_FLAG_ECHO) ? 'T' : '_'); // Send by me + print((flags() & TP_FRAME_FLAG_ADDRESSED) ? 'D' : '_'); // Recv for me + print((flags() & TP_FRAME_FLAG_ACK_NACK) ? 'N' : '_'); // ACK + NACK + print((flags() & TP_FRAME_FLAG_ACK_BUSY) ? 'B' : '_'); // ACK + BUSY + print((flags() & TP_FRAME_FLAG_ACK) ? 'A' : '_'); // ACK + print("] "); + printHex("( ", data(), size(), false); + print(")"); + } \ No newline at end of file diff --git a/src/knx/tp_frame.h b/src/knx/tp_frame.h index 7f576fe..22462ed 100644 --- a/src/knx/tp_frame.h +++ b/src/knx/tp_frame.h @@ -1,10 +1,9 @@ #pragma once -#pragma GCC optimize("O3") #include "cemi_frame.h" +#include "util/logger.h" #include #include -#include // Means that the frame is invalid #define TP_FRAME_FLAG_INVALID 0b10000000 @@ -58,7 +57,7 @@ class TpFrame { _size = cemiFrame.telegramLengthtTP(); _maxSize = cemiFrame.telegramLengthtTP(); - _data = (uint8_t*)malloc(cemiFrame.telegramLengthtTP()); + _data = new uint8_t[cemiFrame.telegramLengthtTP()]; cemiFrame.fillTelegramTP(_data); presetFlags(); } @@ -70,7 +69,7 @@ class TpFrame TpFrame(uint16_t maxSize = 263) : _maxSize(maxSize) { - _data = (uint8_t*)malloc(_maxSize); + _data = new uint8_t[_maxSize]; _size = 0; } @@ -79,7 +78,7 @@ class TpFrame */ ~TpFrame() { - free(_data); + delete[] _data; } /* @@ -102,7 +101,7 @@ class TpFrame /* * Current frame size. This may differ from the actual size as long as the frame is not complete. */ - inline uint16_t size() + inline uint16_t size() const { return _size; } @@ -110,7 +109,7 @@ class TpFrame /* * Returns the assigned flags */ - inline uint16_t flags() + inline uint16_t flags() const { return _flags; } @@ -126,7 +125,7 @@ class TpFrame /* * Returns a pointer to the data */ - inline uint8_t* data() + inline const uint8_t* data() const { return _data; } @@ -161,7 +160,7 @@ class TpFrame /* * Returns is the frame exteneded or not */ - inline bool isExtended() + inline bool isExtended() const { return (_data[0] & 0xD3) == 0x10; } @@ -170,37 +169,16 @@ class TpFrame * Returns the source * Assumes that enough data has been imported. */ - inline uint16_t source() + inline uint16_t source() const { return isExtended() ? (_data[2] << 8) + _data[3] : (_data[1] << 8) + _data[2]; } - inline std::string humanSource() - { - uint16_t value = source(); - char buffer[10]; - sprintf(buffer, "%02i.%02i.%03i", (value >> 12 & 0b1111), (value >> 8 & 0b1111), (value & 0b11111111)); - return buffer; - } - - inline std::string humanDestination() - { - uint16_t value = destination(); - char buffer[10]; - - if (isGroupAddress()) - sprintf(buffer, "%02i/%02i/%03i", (value >> 11 & 0b1111), (value >> 8 & 0b111), (value & 0b11111111)); - else - sprintf(buffer, "%02i.%02i.%03i", (value >> 12 & 0b1111), (value >> 8 & 0b1111), (value & 0b11111111)); - - return buffer; - } - /* * Returns the destination * Assumes that enough data has been imported. */ - inline uint16_t destination() + inline uint16_t destination() const { return isExtended() ? (_data[4] << 8) + _data[5] : (_data[3] << 8) + _data[4]; } @@ -235,7 +213,7 @@ class TpFrame * Returns if the destination is a group address * Assumes that enough data has been imported. */ - inline bool isGroupAddress() + inline bool isGroupAddress() const { return isExtended() ? (_data[1] >> 7) & 0b1 : (_data[5] >> 7) & 0b1; } @@ -255,7 +233,7 @@ class TpFrame */ uint8_t* cemiData() { - uint8_t* cemiBuffer = (uint8_t*)malloc(cemiSize()); + uint8_t* cemiBuffer = new uint8_t[cemiSize()]; // Das CEMI erwartet die Daten im Extended format inkl. zwei zusätzlicher Bytes am Anfang. cemiBuffer[0] = 0x29; @@ -306,4 +284,5 @@ class TpFrame { return !(_data[0] & 0b100000); } + void printIt() const; }; \ No newline at end of file diff --git a/src/knx/tpdu.cpp b/src/knx/tpdu.cpp index 6c1d0e1..1e7ba23 100644 --- a/src/knx/tpdu.cpp +++ b/src/knx/tpdu.cpp @@ -1,5 +1,6 @@ #include "tpdu.h" #include "cemi_frame.h" +#include "bits.h" TPDU::TPDU(uint8_t* data, CemiFrame& frame): _data(data), _frame(frame) { @@ -118,19 +119,20 @@ CemiFrame& TPDU::frame() return _frame; } -const std::string TPDU::toString() const +void TPDU::printIt() const { #ifndef KNX_NO_PRINT - std::string value = std::string("TPDU: ") + enum_name(type()) + " "; + print("TPDU: "); + print(enum_name(type())); + print(" "); if (control()) - value += "control "; + print("control "); if (numbered()) - value += "numbered sequence: " + to_string(sequenceNumber()); - - return value; -#else - return ""; + { + print("numbered sequence: "); + print(sequenceNumber()); + } #endif } \ No newline at end of file diff --git a/src/knx/tpdu.h b/src/knx/tpdu.h index 3fcdca7..2c33053 100644 --- a/src/knx/tpdu.h +++ b/src/knx/tpdu.h @@ -2,11 +2,11 @@ #include "stdint.h" #include "knx_types.h" -#include +#include "util/logger.h" class CemiFrame; class APDU; -class TPDU +class TPDU : public IPrintable { friend class CemiFrame; @@ -26,7 +26,7 @@ class TPDU APDU& apdu(); CemiFrame& frame(); - const std::string toString() const; + void printIt() const; protected: TPDU(uint8_t* data, CemiFrame& frame); diff --git a/src/knx/tpuart_data_link_layer.cpp b/src/knx/tpuart_data_link_layer.cpp index c96e9e8..bd9903e 100644 --- a/src/knx/tpuart_data_link_layer.cpp +++ b/src/knx/tpuart_data_link_layer.cpp @@ -1,11 +1,11 @@ #pragma GCC optimize("O3") #include "address_table_object.h" -#include "bits.h" #include "cemi_frame.h" #include "device_object.h" #include "platform.h" #include "tpuart_data_link_layer.h" +#include "bits.h" /* * A new implementation of the tpuart connection. @@ -129,25 +129,6 @@ enum RX_AWAITING_ACK }; -void printFrame(TpFrame* tpframe) -{ - print(tpframe->humanSource().c_str()); - print(" -> "); - print(tpframe->humanDestination().c_str()); - print(" ["); - print((tpframe->flags() & TP_FRAME_FLAG_INVALID) ? 'I' : '_'); // Invalid - print((tpframe->flags() & TP_FRAME_FLAG_EXTENDED) ? 'E' : '_'); // Extended - print((tpframe->flags() & TP_FRAME_FLAG_REPEATED) ? 'R' : '_'); // Repeat - print((tpframe->flags() & TP_FRAME_FLAG_ECHO) ? 'T' : '_'); // Send by me - print((tpframe->flags() & TP_FRAME_FLAG_ADDRESSED) ? 'D' : '_'); // Recv for me - print((tpframe->flags() & TP_FRAME_FLAG_ACK_NACK) ? 'N' : '_'); // ACK + NACK - print((tpframe->flags() & TP_FRAME_FLAG_ACK_BUSY) ? 'B' : '_'); // ACK + BUSY - print((tpframe->flags() & TP_FRAME_FLAG_ACK) ? 'A' : '_'); // ACK - print("] "); - printHex("( ", tpframe->data(), tpframe->size(), false); - print(")"); -} - /* * Processes all bytes. */ @@ -554,7 +535,7 @@ void TpUartDataLinkLayer::processTxFrameComplete(bool success) uint8_t* cemiData = _txFrame->cemiData(); CemiFrame cemiFrame(cemiData, _txFrame->cemiSize()); dataConReceived(cemiFrame, success); - free(cemiData); + delete[] cemiData; clearTxFrame(); _txProcessdFrameCounter++; _txState = TX_IDLE; @@ -880,7 +861,7 @@ void TpUartDataLinkLayer::processTxQueue() #ifdef DEBUG_TP_FRAMES print("Outbound: "); - printFrame(_txFrame); + _txFrame.printIt(); println(); #endif @@ -969,7 +950,7 @@ void TpUartDataLinkLayer::rxFrameReceived(TpFrame* tpFrame) #endif frameReceived(cemiFrame); - free(cemiData); + delete[] cemiData; } DptMedium TpUartDataLinkLayer::mediumType() const @@ -1130,7 +1111,7 @@ void TpUartDataLinkLayer::processRxFrame(TpFrame* tpFrame) if (_monitoring) { print("Monitor: "); - printFrame(tpFrame); + tpFrame->printIt(); println(); } else if (tpFrame->flags() & TP_FRAME_FLAG_INVALID) @@ -1139,7 +1120,7 @@ void TpUartDataLinkLayer::processRxFrame(TpFrame* tpFrame) print(31); print("m"); print("Invalid: "); - printFrame(tpFrame); + tpFrame->printIt(); print("\x1B["); print(0); println("m"); @@ -1148,7 +1129,7 @@ void TpUartDataLinkLayer::processRxFrame(TpFrame* tpFrame) { #ifdef DEBUG_TP_FRAMES print("Inbound: "); - printFrame(tpFrame); + tpFrame.printIt()); println(); #endif diff --git a/src/knx/tpuart_data_link_layer.h b/src/knx/tpuart_data_link_layer.h index c66ab20..a5b30d6 100644 --- a/src/knx/tpuart_data_link_layer.h +++ b/src/knx/tpuart_data_link_layer.h @@ -20,8 +20,6 @@ #define __isr #endif -void printFrame(TpFrame* tpframe); - class ITpUartCallBacks { public: diff --git a/src/knx/transport_layer.cpp b/src/knx/transport_layer.cpp index b2bc344..742bc4f 100644 --- a/src/knx/transport_layer.cpp +++ b/src/knx/transport_layer.cpp @@ -28,7 +28,7 @@ void TransportLayer::groupAddressTable(AddressTableObject& addrTable) void TransportLayer::dataIndividualIndication(uint16_t destination, HopCountType hopType, Priority priority, uint16_t source, TPDU& tpdu) { - LOGGER.info("dataIndividualIndication %s", tpdu.toString().c_str()); + LOGGER.info("dataIndividualIndication ", tpdu); uint8_t sequenceNo = tpdu.sequenceNumber(); @@ -331,7 +331,7 @@ void TransportLayer::dataIndividualIndication(uint16_t destination, HopCountType void TransportLayer::dataIndividualConfirm(AckType ack, uint16_t destination, HopCountType hopType, Priority priority, TPDU& tpdu, bool status) { - LOGGER.info("dataIndividualConfirm %s", tpdu.toString().c_str()); + LOGGER.info("dataIndividualConfirm ", tpdu); TpduType type = tpdu.type(); switch (type) @@ -405,7 +405,7 @@ void TransportLayer::dataIndividualConfirm(AckType ack, uint16_t destination, Ho void TransportLayer::dataGroupIndication(uint16_t destination, HopCountType hopType, Priority priority, uint16_t source, TPDU& tpdu) { - LOGGER.info("dataGroupIndication %s", tpdu.toString().c_str()); + LOGGER.info("dataGroupIndication ", tpdu); if (_groupAddressTable == nullptr) return; @@ -423,31 +423,31 @@ void TransportLayer::dataGroupIndication(uint16_t destination, HopCountType hopT void TransportLayer::dataGroupConfirm(AckType ack, uint16_t source, uint16_t destination, HopCountType hopType, Priority priority, TPDU& tpdu, bool status) { - LOGGER.info("dataGroupConfirm %s", tpdu.toString().c_str()); + LOGGER.info("dataGroupConfirm ", tpdu); _applicationLayer.dataGroupConfirm(ack, hopType, priority, destination, tpdu.apdu(), status); } void TransportLayer::dataBroadcastIndication(HopCountType hopType, Priority priority, uint16_t source, TPDU& tpdu) { - LOGGER.info("dataBroadcastIndication %s", tpdu.toString().c_str()); + LOGGER.info("dataBroadcastIndication ", tpdu); _applicationLayer.dataBroadcastIndication(hopType, priority, source, tpdu.apdu()); } void TransportLayer::dataBroadcastConfirm(AckType ack, HopCountType hopType, Priority priority, TPDU& tpdu, bool status) { - LOGGER.info("dataBroadcastConfirm %s", tpdu.toString().c_str()); + LOGGER.info("dataBroadcastConfirm ", tpdu); _applicationLayer.dataBroadcastConfirm(ack, hopType, priority, tpdu.apdu(), status); } void TransportLayer::dataSystemBroadcastIndication(HopCountType hopType, Priority priority, uint16_t source, TPDU& tpdu) { - LOGGER.info("dataSystemBroadcastIndication %s", tpdu.toString().c_str()); + LOGGER.info("dataSystemBroadcastIndication ", tpdu); _applicationLayer.dataSystemBroadcastIndication(hopType, priority, source, tpdu.apdu()); } void TransportLayer::dataSystemBroadcastConfirm(AckType ack, HopCountType hopType, TPDU& tpdu, Priority priority, bool status) { - LOGGER.info("dataSystemBroadcastConfirm %s", tpdu.toString().c_str()); + LOGGER.info("dataSystemBroadcastConfirm ", tpdu); _applicationLayer.dataSystemBroadcastConfirm(hopType, priority, tpdu.apdu(), status); } @@ -458,28 +458,28 @@ void TransportLayer::dataGroupRequest(AckType ack, HopCountType hopType, Priorit uint16_t groupAdress = _groupAddressTable->getGroupAddress(tsap); TPDU& tpdu = apdu.frame().tpdu(); - LOGGER.info("dataGroupRequest %s", tpdu.toString().c_str()); + LOGGER.info("dataGroupRequest ", tpdu); _networkLayer->dataGroupRequest(ack, groupAdress, hopType, priority, tpdu); } void TransportLayer::dataBroadcastRequest(AckType ack, HopCountType hopType, Priority priority, APDU& apdu) { TPDU& tpdu = apdu.frame().tpdu(); - LOGGER.info("dataBroadcastRequest %s", tpdu.toString().c_str()); + LOGGER.info("dataBroadcastRequest ", tpdu); _networkLayer->dataBroadcastRequest(ack, hopType, priority, tpdu); } void TransportLayer::dataSystemBroadcastRequest(AckType ack, HopCountType hopType, Priority priority, APDU& apdu) { TPDU& tpdu = apdu.frame().tpdu(); - LOGGER.info("dataSystemBroadcastRequest %s", tpdu.toString().c_str()); + LOGGER.info("dataSystemBroadcastRequest ", tpdu); return _networkLayer->dataSystemBroadcastRequest(ack, hopType, priority, tpdu); } void TransportLayer::dataIndividualRequest(AckType ack, HopCountType hopType, Priority priority, uint16_t destination, APDU& apdu) { TPDU& tpdu = apdu.frame().tpdu(); - LOGGER.info("dataIndividualRequest %s", tpdu.toString().c_str()); + LOGGER.info("dataIndividualRequest ", tpdu); _networkLayer->dataIndividualRequest(ack, destination, hopType, priority, tpdu); } @@ -641,7 +641,7 @@ void TransportLayer::sendControlTelegram(TpduType pduType, uint8_t seqNo) TPDU& tpdu = frame.tpdu(); tpdu.type(pduType); tpdu.sequenceNumber(seqNo); - LOGGER.info("sendControlTelegram %s", tpdu.toString().c_str()); + LOGGER.info("sendControlTelegram ", tpdu); _networkLayer->dataIndividualRequest(AckRequested, _connectionAddress, NetworkLayerParameter, SystemPriority, tpdu); } diff --git a/src/knx/usb_tunnel_interface.cpp b/src/knx/usb_tunnel_interface.cpp index 09772ca..b22b41f 100644 --- a/src/knx/usb_tunnel_interface.cpp +++ b/src/knx/usb_tunnel_interface.cpp @@ -1,4 +1,3 @@ -#include "bits.h" #include "usb_tunnel_interface.h" #include "cemi_server.h" #include "cemi_frame.h" @@ -6,7 +5,7 @@ #include #include -#define MIN(a, b) ((a < b) ? (a) : (b)) +#include "bits.h" #define MAX_EP_SIZE 64 #define HID_HEADER_SIZE 3 diff --git a/src/knx/util/logger.cpp b/src/knx/util/logger.cpp index fbfd971..18b98f1 100644 --- a/src/knx/util/logger.cpp +++ b/src/knx/util/logger.cpp @@ -66,29 +66,42 @@ void Logger::exception(const char* message, ...) #endif } -void Logger::log(LogType type, const char* format, va_list args) +bool Logger::log(LogType type) { #ifndef KNX_NO_PRINT - LogType* level = _loggers.get(_name); - if(level == nullptr) { + /*LogType* level = _loggers.get(_name); + + if (level == nullptr) + { print("Logger "); print(_name); print(" is disabled. Use Logger::logLevel(\""); print(_name); println("\", Logger::Info) to enable."); - _loggers.insertOrAssign(_name, Disabled); - return; + _loggers.insertOrAssign(_name, Info); + return false; } - if(*level > type) - return; - + if (*level > type) + return false; +*/ print(millis()); print(" "); print(_name); print("\t"); print(enum_name(type)); print(" "); + return true; +#else + return false; +#endif +} + +void Logger::log(LogType type, const char* format, va_list args) +{ +#ifndef KNX_NO_PRINT + if (!log(type)) + return; while (*format) { @@ -100,6 +113,10 @@ void Logger::log(LogType type, const char* format, va_list args) { print(va_arg(args, int)); } + if (*format == 'x') + { + print(va_arg(args, int), HEX); + } else if (*format == 's') { print(va_arg(args, char*)); @@ -108,6 +125,10 @@ void Logger::log(LogType type, const char* format, va_list args) { print(va_arg(args, double)); } + else if (*format == 'B') + { + printHex("", va_arg(args, uint8_t*), va_arg(args, size_t), false); + } } else { diff --git a/src/knx/util/logger.h b/src/knx/util/logger.h index 6543303..acd63b6 100644 --- a/src/knx/util/logger.h +++ b/src/knx/util/logger.h @@ -1,20 +1,86 @@ +#pragma once + #include #include "simple_map.h" + +class IPrintable +{ + public: + virtual void printIt() const = 0; + virtual ~IPrintable() = default; +}; + +#ifndef KNX_NO_PRINT + void println(); + void print(const char*); +#else + #define print(...) do {} while(0) + #define println(...) do {} while(0) +#endif class Logger { public: enum LogType { Info, Warning, Error, Critical, Exception, Disabled}; static Logger& logger(const char* name); static void logLevel(const char* name, LogType level); + void info(const char* message, IPrintable& object) + { + if (!log(LogType::Info)) + return; + + print(message); + object.printIt(); + println(); + } void info(const char* message, ...); + void warning(const char* message, IPrintable& object) + { + if (!log(LogType::Warning)) + return; + + print(message); + object.printIt(); + println(); + } void warning(const char* message, ...); + void error(const char* message, IPrintable& object) + { + if (!log(LogType::Error)) + return; + + print(message); + object.printIt(); + println(); + } void error(const char* message, ...); + void critical(const char* message, IPrintable& object) + { + if (!log(LogType::Critical)) + return; + + print(message); + object.printIt(); + println(); + } void critical(const char* message, ...); + void exception(const char* message, IPrintable& object) + { + if (!log(LogType::Exception)) + return; + + print(message); + object.printIt(); + println(); + } void exception(const char* message, ...); protected: Logger() {} + bool log(LogType type); void log(LogType type, const char* format, va_list args); - void name(const char* value) { _name = value; } + void name(const char* value) + { + _name = value; + } private: const char* enum_name(LogType type); const char* _name = "";