no more std::string

This commit is contained in:
Thomas Kunze 2024-08-19 23:24:21 +02:00
parent e0f0b49db4
commit 1b68cd0ef7
32 changed files with 293 additions and 232 deletions

View File

@ -164,6 +164,8 @@ set(${PROJECT_NAME}_SOURCES
../../src/knx_facade.h ../../src/knx_facade.h
../../src/knx/platform/cc1310_platform.cpp ../../src/knx/platform/cc1310_platform.cpp
../../src/knx_facade.cpp ../../src/knx_facade.cpp
../../src/knx/knx_types.h
../../src/knx/knx_types.cpp
./RTT/SEGGER_RTT_Conf.h ./RTT/SEGGER_RTT_Conf.h
./RTT/SEGGER_RTT_printf.c ./RTT/SEGGER_RTT_printf.c
./RTT/SEGGER_RTT.c ./RTT/SEGGER_RTT.c
@ -189,7 +191,7 @@ include_directories(
${SimpleLinkCC13X0SDK_INCLUDE_DIRS} ${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} add_executable(${PROJECT_NAME}
${${PROJECT_NAME}_SOURCES} ${${PROJECT_NAME}_SOURCES}

View File

@ -167,6 +167,7 @@ knx/tpdu.cpp
knx/tpdu.h knx/tpdu.h
knx/tpuart_data_link_layer.cpp knx/tpuart_data_link_layer.cpp
knx/tpuart_data_link_layer.h knx/tpuart_data_link_layer.h
knx/tp_frame.cpp
knx/tp_frame.h knx/tp_frame.h
knx/transport_layer.cpp knx/transport_layer.cpp
knx/transport_layer.h knx/transport_layer.h

View File

@ -40,22 +40,20 @@ uint8_t APDU::length() const
return _frame.npdu().octetCount(); return _frame.npdu().octetCount();
} }
string APDU::toString() const void APDU::printIt() const
{ {
#ifndef KNX_NO_PRINT #ifndef KNX_NO_PRINT
string value = std::string("APDU: ") + enum_name(type()) + " "; print("APDU: ");
value += byte2hex(_data[0] & 0x3); print(enum_name(type()));
print(" ");
print(_data[0] & 0x3, HEX);
for (uint8_t i = 1; i < length() + 1; ++i) for (uint8_t i = 1; i < length() + 1; ++i)
{ {
if (i) if (i)
value += " "; print(" ");
value += byte2hex(_data[i]); print(_data[i], HEX);
} }
return value;
#else
return "";
#endif #endif
} }

View File

@ -1,7 +1,7 @@
#pragma once #pragma once
#include <stdint.h> #include <stdint.h>
#include <string> #include "util/logger.h"
#include "knx_types.h" #include "knx_types.h"
class CemiFrame; class CemiFrame;
@ -9,7 +9,7 @@ class CemiFrame;
/** /**
* This class represents an Application Protocol Data Unit. It is part of a CemiFrame. * This class represents an Application Protocol Data Unit. It is part of a CemiFrame.
*/ */
class APDU class APDU : public IPrintable
{ {
friend class CemiFrame; friend class CemiFrame;
@ -35,9 +35,9 @@ class APDU
*/ */
uint8_t length() const; uint8_t length() const;
/** /**
* Convert APDU to string. * print APDU
*/ */
std::string toString() const; void printIt() const;
protected: protected:
/** /**

View File

@ -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) 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) if (_assocTable == nullptr)
return; 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) 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()) 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) 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(); uint8_t* data = apdu.data();
switch (apdu.type()) 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) 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(); uint8_t* data = apdu.data();
switch (apdu.type()) 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) 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(); const uint8_t* data = apdu.data();
switch (apdu.type()) 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) 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(); const uint8_t* data = apdu.data();
switch (apdu.type()) 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) 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(); uint8_t* data = apdu.data();
switch (apdu.type()) switch (apdu.type())
@ -1374,13 +1374,13 @@ void ApplicationLayer::individualIndication(HopCountType hopType, Priority prior
} }
default: 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) 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(); uint8_t* data = apdu.data();
switch (apdu.type()) switch (apdu.type())

View File

@ -1,3 +1,4 @@
#include <stdint.h>
#include "bits.h" #include "bits.h"
#include <cstring> // for memcpy() #include <cstring> // for memcpy()
@ -9,29 +10,6 @@ const uint8_t* popByte(uint8_t& b, const uint8_t* data)
} }
#ifndef KNX_NO_PRINT #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) void printHex(const char* suffix, const uint8_t* data, size_t length, bool newline)
{ {

View File

@ -1,8 +1,7 @@
#pragma once #pragma once
#include <cstddef> #include <cstddef>
#include <cstdint> #include <stdint.h>
#include <string>
#if defined(__linux__) #if defined(__linux__)
#include <arpa/inet.h> #include <arpa/inet.h>
@ -71,9 +70,6 @@
#endif #endif
#ifndef KNX_NO_PRINT #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(const char[]);
void print(char); void print(char);
void print(unsigned char, int = DEC); void print(unsigned char, int = DEC);

View File

@ -400,30 +400,29 @@ bool CemiFrame::valid() const
return true; return true;
} }
std::string CemiFrame::toString() const void CemiFrame::printIt() const
{ {
#ifndef KNX_NO_PRINT #ifndef KNX_NO_PRINT
std::string value = std::string("DPDU:") + enum_name(frameType()) + " "; print("DPDU:");
value += enum_name(systemBroadcast()); print(enum_name(frameType()));
value += " "; print(" ");
value += enum_name(ack()); print(enum_name(systemBroadcast()));
value += " "; print(" ");
value += enum_name(repetition()); print(enum_name(ack()));
value += " "; print(" ");
value += enum_name(priority()); print(enum_name(repetition()));
value += " from "; print(" ");
value += format_ia(sourceAddress()); print(enum_name(priority()));
value += " to "; print(" from ");
value += enum_name(addressType()); print_ia(sourceAddress());
value += " "; print(" to ");
print(enum_name(addressType()));
print(" ");
if (addressType() == AddressType::IndividualAddress) if (addressType() == AddressType::IndividualAddress)
value += format_ia(destinationAddress()); print_ia(destinationAddress());
else else
value += format_ga(destinationAddress()); print_ga(destinationAddress());
return value;
#else
return "";
#endif #endif
} }

View File

@ -1,11 +1,13 @@
#pragma once #pragma once
#include "knx_types.h" #include "knx_types.h"
#include "stdint.h" #include <stdint.h>
#include "npdu.h" #include "npdu.h"
#include "tpdu.h" #include "tpdu.h"
#include "apdu.h" #include "apdu.h"
#include "config.h" #include "config.h"
#include "util/logger.h"
#define NPDU_LPDU_DIFF 8 #define NPDU_LPDU_DIFF 8
#define TPDU_NPDU_DIFF 1 #define TPDU_NPDU_DIFF 1
@ -16,7 +18,7 @@
// Mesg Code and additional info length // Mesg Code and additional info length
#define CEMI_HEADER_SIZE 2 #define CEMI_HEADER_SIZE 2
class CemiFrame class CemiFrame : public IPrintable
{ {
friend class DataLinkLayer; friend class DataLinkLayer;
@ -72,7 +74,7 @@ class CemiFrame
uint8_t calcCrcTP(uint8_t* buffer, uint16_t len); uint8_t calcCrcTP(uint8_t* buffer, uint16_t len);
bool valid() const; bool valid() const;
std::string toString() const; void printIt() const;
private: private:
uint8_t buffer[0xff + NPDU_LPDU_DIFF] = {0}; //only valid of add info is zero uint8_t buffer[0xff + NPDU_LPDU_DIFF] = {0}; //only valid of add info is zero

View File

@ -59,7 +59,7 @@ bool DataLinkLayer::isTunnelAddress(uint16_t addr)
} }
#endif #endif
void DataLinkLayer::cddataRequestFromTunnel(CemiFrame& frame) void DataLinkLayer::dataRequestFromTunnel(CemiFrame& frame)
{ {
_cemiServer->dataConfirmationToTunnel(frame); _cemiServer->dataConfirmationToTunnel(frame);
@ -161,7 +161,7 @@ void DataLinkLayer::frameReceived(CemiFrame& frame)
SystemBroadcast systemBroadcast = frame.systemBroadcast(); SystemBroadcast systemBroadcast = frame.systemBroadcast();
LOGGER.info("frameReceived %s", frame.toString().c_str()); LOGGER.info("frameReceived ", frame);
#ifdef USE_CEMI_SERVER #ifdef USE_CEMI_SERVER
@ -222,7 +222,7 @@ bool DataLinkLayer::sendTelegram(NPDU& npdu, AckType ack, uint16_t destinationAd
else else
frame.frameType(format); frame.frameType(format);
LOGGER.info("sendTelegram %s", frame.toString().c_str()); LOGGER.info("sendTelegram ", frame);
if (!frame.valid()) if (!frame.valid())
{ {

View File

@ -10,25 +10,20 @@
#include "knx_ip_search_response_extended.h" #include "knx_ip_search_response_extended.h"
#include "../util/logger.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") #define LOGGER Logger::logger("IpDataLinkLayer")
#include "knx_ip_connect_request.h" #include "knx_ip_connect_request.h"
#include "knx_ip_connect_response.h" #include "knx_ip_connect_response.h"
#include "knx_ip_state_request.h" #include "knx_ip_state_request.h"
#include "knx_ip_state_response.h" #include "knx_ip_state_response.h"
#include "knx_ip_disconnect_request.h" #include "knx_ip_disconnect_request.h"
#include "knx_ip_disconnect_response.h" #include "knx_ip_disconnect_response.h"
#include "knx_ip_tunneling_request.h" #include "knx_ip_tunneling_request.h"
#include "knx_ip_tunneling_ack.h" #include "knx_ip_tunneling_ack.h"
#include "knx_ip_description_request.h" #include "knx_ip_description_request.h"
#include "knx_ip_description_response.h" #include "knx_ip_description_response.h"
#include "knx_ip_config_request.h" #include "knx_ip_config_request.h"
#include <stdio.h> #include <stdio.h>
@ -304,6 +299,7 @@ void IpDataLinkLayer::loop()
break; break;
} }
} }
#endif #endif
uint8_t buffer[512]; uint8_t buffer[512];
uint16_t remotePort = 0; uint16_t remotePort = 0;
@ -358,6 +354,7 @@ void IpDataLinkLayer::loop()
} }
#ifdef KNX_TUNNELING #ifdef KNX_TUNNELING
case SearchRequestExt: case SearchRequestExt:
{ {
loopHandleSearchRequestExtended(buffer, len); loopHandleSearchRequestExtended(buffer, len);
@ -413,9 +410,11 @@ void IpDataLinkLayer::loop()
//println("got Ack"); //println("got Ack");
break; break;
} }
#endif #endif
default: default:
LOGGER.warning("Unhandled service identifier: %s", word2hex(code).c_str()); LOGGER.warning("Unhandled service identifier: %x:", code);
break; break;
} }
} }
@ -1148,7 +1147,8 @@ bool IpDataLinkLayer::sendUnicast(uint32_t addr, uint16_t port, KnxIpFrame& ipFr
if (!_enabled) if (!_enabled)
return false; 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()); return _platform.sendBytesMultiCast(ipFrame.data(), ipFrame.totalLength());
} }

View File

@ -76,7 +76,7 @@ KnxIpFrame::KnxIpFrame(uint16_t length)
protocolVersion(KnxIp1_0); protocolVersion(KnxIp1_0);
totalLength(length); totalLength(length);
} }
#ifndef KNX_NO_PRINT
const char* enum_name(const KnxIpVersion enum_val) const char* enum_name(const KnxIpVersion enum_val)
{ {
switch (enum_val) switch (enum_val)
@ -148,4 +148,5 @@ const char* enum_name(const KnxIpServiceType enum_val)
} }
return ""; return "";
} }
#endif

View File

@ -1,4 +1,6 @@
#include "knx_types.h" #include "knx_types.h"
#include "bits.h"
#ifndef KNX_NO_PRINT #ifndef KNX_NO_PRINT
const char* enum_name(const LCCONFIG enum_val) const char* enum_name(const LCCONFIG enum_val)
{ {
@ -691,13 +693,21 @@ const char* enum_name(const FrameFormat enum_val)
return ""; 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 #endif

View File

@ -1,5 +1,4 @@
#pragma once #pragma once
#include <string>
#include <stdint.h> #include <stdint.h>
using namespace std; using namespace std;
@ -312,5 +311,5 @@ enum LCCONFIG
}; };
const char* enum_name(const LCCONFIG enum_val); const char* enum_name(const LCCONFIG enum_val);
const string format_ia(uint16_t ia); void print_ia(uint16_t ia);
const string format_ga(uint16_t ga); void print_ga(uint16_t ga);

View File

@ -27,7 +27,7 @@ void Memory::readMemory()
return; return;
} }
LOGGER.info("content %s", array2hex(flashStart, _metadataSize).c_str()); LOGGER.info("content %B", flashStart, _metadataSize);
uint16_t metadataBlockSize = alignToPageSize(_metadataSize); uint16_t metadataBlockSize = alignToPageSize(_metadataSize);
@ -70,17 +70,17 @@ void Memory::readMemory()
else else
{ {
LOGGER.warning("manufacturerId or hardwareType are different"); LOGGER.warning("manufacturerId or hardwareType are different");
LOGGER.warning("expexted manufacturerId: %s , stored manufacturerId: %s", LOGGER.warning("expexted manufacturerId: %x , stored manufacturerId: %x",
word2hex(_deviceObject.manufacturerId()), manufacturerId); _deviceObject.manufacturerId(), manufacturerId);
LOGGER.warning("expexted hardwareType: %s, stored hardwareType: %s", LOGGER.warning("expexted hardwareType: %B, stored hardwareType: %B",
array2hex(_deviceObject.hardwareType(), LEN_HARDWARE_TYPE), _deviceObject.hardwareType(), LEN_HARDWARE_TYPE,
array2hex(hardwareType, LEN_HARDWARE_TYPE)); hardwareType, LEN_HARDWARE_TYPE);
} }
} }
else else
{ {
LOGGER.warning("DataObject api changed, any data stored in flash is invalid."); 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) if (versionCheck == FlashAllInvalid)

View File

@ -32,7 +32,7 @@ void NetworkLayerDevice::dataIndividualRequest(AckType ack, uint16_t destination
// print.print("-> NL "); // print.print("-> NL ");
// tpdu.apdu().printPDU(); // 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); _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 else
npdu.hopCount(hopCount()); 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); _netLayerEntities[kInterfaceIndex].sendDataRequest(npdu, ack, destination, _deviceObj.individualAddress(), priority, GroupAddress, Broadcast);
} }
@ -58,7 +58,7 @@ void NetworkLayerDevice::dataBroadcastRequest(AckType ack, HopCountType hopType,
else else
npdu.hopCount(hopCount()); 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); _netLayerEntities[kInterfaceIndex].sendDataRequest(npdu, ack, 0, _deviceObj.individualAddress(), priority, GroupAddress, Broadcast);
} }
@ -75,13 +75,13 @@ void NetworkLayerDevice::dataSystemBroadcastRequest(AckType ack, HopCountType ho
else else
npdu.hopCount(hopCount()); 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); _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) 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; HopCountType hopType = npdu.hopCount() == 7 ? UnlimitedRouting : NetworkLayerParameter;
if (addrType == IndividualAddress) 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) 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; HopCountType hopType = npdu.hopCount() == 7 ? UnlimitedRouting : NetworkLayerParameter;
if (addressType == IndividualAddress) 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) 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; HopCountType hopType = npdu.hopCount() == 7 ? UnlimitedRouting : NetworkLayerParameter;
DptMedium mediumType = _netLayerEntities[srcIfIdx].mediumType(); 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) 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; HopCountType hopType = npdu.hopCount() == 7 ? UnlimitedRouting : NetworkLayerParameter;
_transportLayer.dataBroadcastConfirm(ack, hopType, priority, npdu.tpdu(), status); _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) 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; HopCountType hopType = npdu.hopCount() == 7 ? UnlimitedRouting : NetworkLayerParameter;
_transportLayer.dataSystemBroadcastIndication(hopType, priority, source, npdu.tpdu()); _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) 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; HopCountType hopType = npdu.hopCount() == 7 ? UnlimitedRouting : NetworkLayerParameter;
_transportLayer.dataSystemBroadcastConfirm(ack, hopType, npdu.tpdu(), priority, status); _transportLayer.dataSystemBroadcastConfirm(ack, hopType, npdu.tpdu(), priority, status);
} }

View File

@ -1,7 +1,7 @@
#include "npdu.h" #include "npdu.h"
#include "cemi_frame.h" #include "cemi_frame.h"
#include <string.h> #include <string.h>
#include "bits.h"
NPDU::NPDU(uint8_t* data, CemiFrame& frame): _data(data), _frame(frame) NPDU::NPDU(uint8_t* data, CemiFrame& frame): _data(data), _frame(frame)
{ {
@ -43,7 +43,12 @@ TPDU& NPDU::tpdu()
return _frame.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
} }

View File

@ -1,12 +1,12 @@
#pragma once #pragma once
#include <stdint.h> #include <stdint.h>
#include <string> #include "util/logger.h"
class CemiFrame; class CemiFrame;
class TPDU; class TPDU;
class NPDU class NPDU : public IPrintable
{ {
friend class CemiFrame; friend class CemiFrame;
@ -18,7 +18,7 @@ class NPDU
void hopCount(uint8_t value); void hopCount(uint8_t value);
CemiFrame& frame(); CemiFrame& frame();
TPDU& tpdu(); TPDU& tpdu();
const std::string toString() const; void printIt() const;
protected: protected:
NPDU(uint8_t* data, CemiFrame& frame); NPDU(uint8_t* data, CemiFrame& frame);

View File

@ -2,7 +2,6 @@
#include "linux_platform.h" #include "linux_platform.h"
#include <cstdio> #include <cstdio>
#include <string>
#include <cstring> #include <cstring>
#include <cstdlib> #include <cstdlib>
#include <stdexcept> #include <stdexcept>

View File

@ -2,8 +2,8 @@
#ifdef __linux__ #ifdef __linux__
#include <string>
#include "../platform.h" #include "../platform.h"
#include <string>
extern int gpio_direction(int pin, int dir); extern int gpio_direction(int pin, int dir);
extern int gpio_read(int pin); extern int gpio_read(int pin);

View File

@ -11,7 +11,6 @@
#include <stdint.h> #include <stdint.h>
#include "save_restore.h" #include "save_restore.h"
#include <string>
/** The data type of a property. */ /** The data type of a property. */
enum PropertyDataType enum PropertyDataType

View File

@ -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)) 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) if (_syncReqBroadcastOutgoing)
{ {
@ -723,7 +723,7 @@ void SecureApplicationLayer::sendSyncResponse(uint16_t dstAddr, bool dstAddrIsGr
{ {
_lastSyncRes = millis(); _lastSyncRes = millis();
LOGGER.info("SyncResponse: %s", response.apdu().c_str()); LOGGER.info("SyncResponse: ", response.apdu());
if (_syncReqBroadcastIncoming) if (_syncReqBroadcastIncoming)
{ {
@ -1063,7 +1063,7 @@ bool SecureApplicationLayer::decrypt(uint8_t* plainApdu, uint16_t plainApduLengt
bool SecureApplicationLayer::decodeSecureApdu(APDU& secureApdu, APDU& plainApdu, SecurityControl& secCtrl) bool SecureApplicationLayer::decodeSecureApdu(APDU& secureApdu, APDU& plainApdu, SecurityControl& secCtrl)
{ {
// Decode secure APDU // 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 srcAddress = secureApdu.frame().sourceAddress();
uint16_t dstAddress = secureApdu.frame().destinationAddress(); 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 // 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)) 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; return true;
} }
@ -1260,7 +1260,7 @@ bool SecureApplicationLayer::createSecureApdu(APDU& plainApdu, APDU& secureApdu,
{ {
// Create secure APDU // 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 srcAddress = plainApdu.frame().sourceAddress();
uint16_t dstAddress = plainApdu.frame().destinationAddress(); uint16_t dstAddress = plainApdu.frame().destinationAddress();
@ -1296,7 +1296,7 @@ bool SecureApplicationLayer::createSecureApdu(APDU& plainApdu, APDU& secureApdu,
println(nextSequenceNumber(secCtrl.toolAccess), HEX); println(nextSequenceNumber(secCtrl.toolAccess), HEX);
updateSequenceNumber(secCtrl.toolAccess, nextSequenceNumber(secCtrl.toolAccess) + 1); 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; return true;
} }

26
src/knx/tp_frame.cpp Normal file
View File

@ -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(")");
}

View File

@ -1,10 +1,9 @@
#pragma once #pragma once
#pragma GCC optimize("O3")
#include "cemi_frame.h" #include "cemi_frame.h"
#include "util/logger.h"
#include <cstring> #include <cstring>
#include <stdint.h> #include <stdint.h>
#include <string>
// Means that the frame is invalid // Means that the frame is invalid
#define TP_FRAME_FLAG_INVALID 0b10000000 #define TP_FRAME_FLAG_INVALID 0b10000000
@ -58,7 +57,7 @@ class TpFrame
{ {
_size = cemiFrame.telegramLengthtTP(); _size = cemiFrame.telegramLengthtTP();
_maxSize = cemiFrame.telegramLengthtTP(); _maxSize = cemiFrame.telegramLengthtTP();
_data = (uint8_t*)malloc(cemiFrame.telegramLengthtTP()); _data = new uint8_t[cemiFrame.telegramLengthtTP()];
cemiFrame.fillTelegramTP(_data); cemiFrame.fillTelegramTP(_data);
presetFlags(); presetFlags();
} }
@ -70,7 +69,7 @@ class TpFrame
TpFrame(uint16_t maxSize = 263) TpFrame(uint16_t maxSize = 263)
: _maxSize(maxSize) : _maxSize(maxSize)
{ {
_data = (uint8_t*)malloc(_maxSize); _data = new uint8_t[_maxSize];
_size = 0; _size = 0;
} }
@ -79,7 +78,7 @@ class TpFrame
*/ */
~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. * 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; return _size;
} }
@ -110,7 +109,7 @@ class TpFrame
/* /*
* Returns the assigned flags * Returns the assigned flags
*/ */
inline uint16_t flags() inline uint16_t flags() const
{ {
return _flags; return _flags;
} }
@ -126,7 +125,7 @@ class TpFrame
/* /*
* Returns a pointer to the data * Returns a pointer to the data
*/ */
inline uint8_t* data() inline const uint8_t* data() const
{ {
return _data; return _data;
} }
@ -161,7 +160,7 @@ class TpFrame
/* /*
* Returns is the frame exteneded or not * Returns is the frame exteneded or not
*/ */
inline bool isExtended() inline bool isExtended() const
{ {
return (_data[0] & 0xD3) == 0x10; return (_data[0] & 0xD3) == 0x10;
} }
@ -170,37 +169,16 @@ class TpFrame
* Returns the source * Returns the source
* Assumes that enough data has been imported. * 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]; 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 * Returns the destination
* Assumes that enough data has been imported. * 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]; 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 * Returns if the destination is a group address
* Assumes that enough data has been imported. * Assumes that enough data has been imported.
*/ */
inline bool isGroupAddress() inline bool isGroupAddress() const
{ {
return isExtended() ? (_data[1] >> 7) & 0b1 : (_data[5] >> 7) & 0b1; return isExtended() ? (_data[1] >> 7) & 0b1 : (_data[5] >> 7) & 0b1;
} }
@ -255,7 +233,7 @@ class TpFrame
*/ */
uint8_t* cemiData() 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. // Das CEMI erwartet die Daten im Extended format inkl. zwei zusätzlicher Bytes am Anfang.
cemiBuffer[0] = 0x29; cemiBuffer[0] = 0x29;
@ -306,4 +284,5 @@ class TpFrame
{ {
return !(_data[0] & 0b100000); return !(_data[0] & 0b100000);
} }
void printIt() const;
}; };

View File

@ -1,5 +1,6 @@
#include "tpdu.h" #include "tpdu.h"
#include "cemi_frame.h" #include "cemi_frame.h"
#include "bits.h"
TPDU::TPDU(uint8_t* data, CemiFrame& frame): _data(data), _frame(frame) TPDU::TPDU(uint8_t* data, CemiFrame& frame): _data(data), _frame(frame)
{ {
@ -118,19 +119,20 @@ CemiFrame& TPDU::frame()
return _frame; return _frame;
} }
const std::string TPDU::toString() const void TPDU::printIt() const
{ {
#ifndef KNX_NO_PRINT #ifndef KNX_NO_PRINT
std::string value = std::string("TPDU: ") + enum_name(type()) + " "; print("TPDU: ");
print(enum_name(type()));
print(" ");
if (control()) if (control())
value += "control "; print("control ");
if (numbered()) if (numbered())
value += "numbered sequence: " + to_string(sequenceNumber()); {
print("numbered sequence: ");
return value; print(sequenceNumber());
#else }
return "";
#endif #endif
} }

View File

@ -2,11 +2,11 @@
#include "stdint.h" #include "stdint.h"
#include "knx_types.h" #include "knx_types.h"
#include <string> #include "util/logger.h"
class CemiFrame; class CemiFrame;
class APDU; class APDU;
class TPDU class TPDU : public IPrintable
{ {
friend class CemiFrame; friend class CemiFrame;
@ -26,7 +26,7 @@ class TPDU
APDU& apdu(); APDU& apdu();
CemiFrame& frame(); CemiFrame& frame();
const std::string toString() const; void printIt() const;
protected: protected:
TPDU(uint8_t* data, CemiFrame& frame); TPDU(uint8_t* data, CemiFrame& frame);

View File

@ -1,11 +1,11 @@
#pragma GCC optimize("O3") #pragma GCC optimize("O3")
#include "address_table_object.h" #include "address_table_object.h"
#include "bits.h"
#include "cemi_frame.h" #include "cemi_frame.h"
#include "device_object.h" #include "device_object.h"
#include "platform.h" #include "platform.h"
#include "tpuart_data_link_layer.h" #include "tpuart_data_link_layer.h"
#include "bits.h"
/* /*
* A new implementation of the tpuart connection. * A new implementation of the tpuart connection.
@ -129,25 +129,6 @@ enum
RX_AWAITING_ACK 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. * Processes all bytes.
*/ */
@ -554,7 +535,7 @@ void TpUartDataLinkLayer::processTxFrameComplete(bool success)
uint8_t* cemiData = _txFrame->cemiData(); uint8_t* cemiData = _txFrame->cemiData();
CemiFrame cemiFrame(cemiData, _txFrame->cemiSize()); CemiFrame cemiFrame(cemiData, _txFrame->cemiSize());
dataConReceived(cemiFrame, success); dataConReceived(cemiFrame, success);
free(cemiData); delete[] cemiData;
clearTxFrame(); clearTxFrame();
_txProcessdFrameCounter++; _txProcessdFrameCounter++;
_txState = TX_IDLE; _txState = TX_IDLE;
@ -880,7 +861,7 @@ void TpUartDataLinkLayer::processTxQueue()
#ifdef DEBUG_TP_FRAMES #ifdef DEBUG_TP_FRAMES
print("Outbound: "); print("Outbound: ");
printFrame(_txFrame); _txFrame.printIt();
println(); println();
#endif #endif
@ -969,7 +950,7 @@ void TpUartDataLinkLayer::rxFrameReceived(TpFrame* tpFrame)
#endif #endif
frameReceived(cemiFrame); frameReceived(cemiFrame);
free(cemiData); delete[] cemiData;
} }
DptMedium TpUartDataLinkLayer::mediumType() const DptMedium TpUartDataLinkLayer::mediumType() const
@ -1130,7 +1111,7 @@ void TpUartDataLinkLayer::processRxFrame(TpFrame* tpFrame)
if (_monitoring) if (_monitoring)
{ {
print("Monitor: "); print("Monitor: ");
printFrame(tpFrame); tpFrame->printIt();
println(); println();
} }
else if (tpFrame->flags() & TP_FRAME_FLAG_INVALID) else if (tpFrame->flags() & TP_FRAME_FLAG_INVALID)
@ -1139,7 +1120,7 @@ void TpUartDataLinkLayer::processRxFrame(TpFrame* tpFrame)
print(31); print(31);
print("m"); print("m");
print("Invalid: "); print("Invalid: ");
printFrame(tpFrame); tpFrame->printIt();
print("\x1B["); print("\x1B[");
print(0); print(0);
println("m"); println("m");
@ -1148,7 +1129,7 @@ void TpUartDataLinkLayer::processRxFrame(TpFrame* tpFrame)
{ {
#ifdef DEBUG_TP_FRAMES #ifdef DEBUG_TP_FRAMES
print("Inbound: "); print("Inbound: ");
printFrame(tpFrame); tpFrame.printIt());
println(); println();
#endif #endif

View File

@ -20,8 +20,6 @@
#define __isr #define __isr
#endif #endif
void printFrame(TpFrame* tpframe);
class ITpUartCallBacks class ITpUartCallBacks
{ {
public: public:

View File

@ -28,7 +28,7 @@ void TransportLayer::groupAddressTable(AddressTableObject& addrTable)
void TransportLayer::dataIndividualIndication(uint16_t destination, HopCountType hopType, Priority priority, uint16_t source, TPDU& tpdu) 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(); 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) 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(); TpduType type = tpdu.type();
switch (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) 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) if (_groupAddressTable == nullptr)
return; 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) 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); _applicationLayer.dataGroupConfirm(ack, hopType, priority, destination, tpdu.apdu(), status);
} }
void TransportLayer::dataBroadcastIndication(HopCountType hopType, Priority priority, uint16_t source, TPDU& tpdu) 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()); _applicationLayer.dataBroadcastIndication(hopType, priority, source, tpdu.apdu());
} }
void TransportLayer::dataBroadcastConfirm(AckType ack, HopCountType hopType, Priority priority, TPDU& tpdu, bool status) 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); _applicationLayer.dataBroadcastConfirm(ack, hopType, priority, tpdu.apdu(), status);
} }
void TransportLayer::dataSystemBroadcastIndication(HopCountType hopType, Priority priority, uint16_t source, TPDU& tpdu) 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()); _applicationLayer.dataSystemBroadcastIndication(hopType, priority, source, tpdu.apdu());
} }
void TransportLayer::dataSystemBroadcastConfirm(AckType ack, HopCountType hopType, TPDU& tpdu, Priority priority, bool status) 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); _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); uint16_t groupAdress = _groupAddressTable->getGroupAddress(tsap);
TPDU& tpdu = apdu.frame().tpdu(); TPDU& tpdu = apdu.frame().tpdu();
LOGGER.info("dataGroupRequest %s", tpdu.toString().c_str()); LOGGER.info("dataGroupRequest ", tpdu);
_networkLayer->dataGroupRequest(ack, groupAdress, hopType, priority, tpdu); _networkLayer->dataGroupRequest(ack, groupAdress, hopType, priority, tpdu);
} }
void TransportLayer::dataBroadcastRequest(AckType ack, HopCountType hopType, Priority priority, APDU& apdu) void TransportLayer::dataBroadcastRequest(AckType ack, HopCountType hopType, Priority priority, APDU& apdu)
{ {
TPDU& tpdu = apdu.frame().tpdu(); TPDU& tpdu = apdu.frame().tpdu();
LOGGER.info("dataBroadcastRequest %s", tpdu.toString().c_str()); LOGGER.info("dataBroadcastRequest ", tpdu);
_networkLayer->dataBroadcastRequest(ack, hopType, priority, tpdu); _networkLayer->dataBroadcastRequest(ack, hopType, priority, tpdu);
} }
void TransportLayer::dataSystemBroadcastRequest(AckType ack, HopCountType hopType, Priority priority, APDU& apdu) void TransportLayer::dataSystemBroadcastRequest(AckType ack, HopCountType hopType, Priority priority, APDU& apdu)
{ {
TPDU& tpdu = apdu.frame().tpdu(); TPDU& tpdu = apdu.frame().tpdu();
LOGGER.info("dataSystemBroadcastRequest %s", tpdu.toString().c_str()); LOGGER.info("dataSystemBroadcastRequest ", tpdu);
return _networkLayer->dataSystemBroadcastRequest(ack, hopType, priority, tpdu); return _networkLayer->dataSystemBroadcastRequest(ack, hopType, priority, tpdu);
} }
void TransportLayer::dataIndividualRequest(AckType ack, HopCountType hopType, Priority priority, uint16_t destination, APDU& apdu) void TransportLayer::dataIndividualRequest(AckType ack, HopCountType hopType, Priority priority, uint16_t destination, APDU& apdu)
{ {
TPDU& tpdu = apdu.frame().tpdu(); TPDU& tpdu = apdu.frame().tpdu();
LOGGER.info("dataIndividualRequest %s", tpdu.toString().c_str()); LOGGER.info("dataIndividualRequest ", tpdu);
_networkLayer->dataIndividualRequest(ack, destination, hopType, priority, 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& tpdu = frame.tpdu();
tpdu.type(pduType); tpdu.type(pduType);
tpdu.sequenceNumber(seqNo); tpdu.sequenceNumber(seqNo);
LOGGER.info("sendControlTelegram %s", tpdu.toString().c_str()); LOGGER.info("sendControlTelegram ", tpdu);
_networkLayer->dataIndividualRequest(AckRequested, _connectionAddress, NetworkLayerParameter, _networkLayer->dataIndividualRequest(AckRequested, _connectionAddress, NetworkLayerParameter,
SystemPriority, tpdu); SystemPriority, tpdu);
} }

View File

@ -1,4 +1,3 @@
#include "bits.h"
#include "usb_tunnel_interface.h" #include "usb_tunnel_interface.h"
#include "cemi_server.h" #include "cemi_server.h"
#include "cemi_frame.h" #include "cemi_frame.h"
@ -6,7 +5,7 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#define MIN(a, b) ((a < b) ? (a) : (b)) #include "bits.h"
#define MAX_EP_SIZE 64 #define MAX_EP_SIZE 64
#define HID_HEADER_SIZE 3 #define HID_HEADER_SIZE 3

View File

@ -66,29 +66,42 @@ void Logger::exception(const char* message, ...)
#endif #endif
} }
void Logger::log(LogType type, const char* format, va_list args) bool Logger::log(LogType type)
{ {
#ifndef KNX_NO_PRINT #ifndef KNX_NO_PRINT
LogType* level = _loggers.get(_name); /*LogType* level = _loggers.get(_name);
if(level == nullptr) {
if (level == nullptr)
{
print("Logger "); print("Logger ");
print(_name); print(_name);
print(" is disabled. Use Logger::logLevel(\""); print(" is disabled. Use Logger::logLevel(\"");
print(_name); print(_name);
println("\", Logger::Info) to enable."); println("\", Logger::Info) to enable.");
_loggers.insertOrAssign(_name, Disabled); _loggers.insertOrAssign(_name, Info);
return; return false;
} }
if(*level > type) if (*level > type)
return; return false;
*/
print(millis()); print(millis());
print(" "); print(" ");
print(_name); print(_name);
print("\t"); print("\t");
print(enum_name(type)); print(enum_name(type));
print(" "); 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) while (*format)
{ {
@ -100,6 +113,10 @@ void Logger::log(LogType type, const char* format, va_list args)
{ {
print(va_arg(args, int)); print(va_arg(args, int));
} }
if (*format == 'x')
{
print(va_arg(args, int), HEX);
}
else if (*format == 's') else if (*format == 's')
{ {
print(va_arg(args, char*)); 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)); print(va_arg(args, double));
} }
else if (*format == 'B')
{
printHex("", va_arg(args, uint8_t*), va_arg(args, size_t), false);
}
} }
else else
{ {

View File

@ -1,20 +1,86 @@
#pragma once
#include <stdarg.h> #include <stdarg.h>
#include "simple_map.h" #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 class Logger
{ {
public: public:
enum LogType { Info, Warning, Error, Critical, Exception, Disabled}; enum LogType { Info, Warning, Error, Critical, Exception, Disabled};
static Logger& logger(const char* name); static Logger& logger(const char* name);
static void logLevel(const char* name, LogType level); 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 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 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 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 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, ...); void exception(const char* message, ...);
protected: protected:
Logger() {} Logger() {}
bool log(LogType type);
void log(LogType type, const char* format, va_list args); 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: private:
const char* enum_name(LogType type); const char* enum_name(LogType type);
const char* _name = ""; const char* _name = "";