More logging

This commit is contained in:
Thomas Kunze 2024-08-17 22:37:15 +02:00
parent 59a76b7c3f
commit 8c3af52892
17 changed files with 92 additions and 47 deletions

View File

@ -40,7 +40,7 @@ uint8_t APDU::length() const
return _frame.npdu().octetCount();
}
string APDU::to_string() const
string APDU::toString() const
{
string value = "APDU: " + enum_name(type()) + " ";
value += byte2hex(_data[0] & 0x3);

View File

@ -37,7 +37,7 @@ class APDU
/**
* Convert APDU to string.
*/
std::string to_string() const;
std::string toString() const;
protected:
/**

View File

@ -10,7 +10,10 @@
#include "util/logger.h"
#define LOGGER Logger::logger("ApplicationLayer")
const SecurityControl ApplicationLayer::noSecurity {.toolAccess = false, .dataSecurity = DataSecurity::None};
const SecurityControl ApplicationLayer::noSecurity
{
.toolAccess = false, .dataSecurity = DataSecurity::None
};
ApplicationLayer::ApplicationLayer(BusAccessUnit& bau) : _bau(bau)
{
@ -35,6 +38,8 @@ 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());
if (_assocTable == nullptr)
return;
@ -86,6 +91,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());
switch (apdu.type())
{
case GroupValueRead:
@ -122,6 +128,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());
uint8_t* data = apdu.data();
switch (apdu.type())
@ -181,6 +188,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());
uint8_t* data = apdu.data();
switch (apdu.type())
@ -235,6 +243,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());
const uint8_t* data = apdu.data();
switch (apdu.type())
@ -271,8 +280,7 @@ void ApplicationLayer::dataSystemBroadcastIndication(HopCountType hopType, Prior
}
default:
print("SystemBroadcast-indication: unhandled APDU-Type: ");
println(apdu.type());
LOGGER.warning("SystemBroadcast-indication: unhandled APDU-Type: %d", apdu.type());
break;
}
}
@ -284,6 +292,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());
const uint8_t* data = apdu.data();
switch (apdu.type())
@ -1362,7 +1371,7 @@ void ApplicationLayer::individualIndication(HopCountType hopType, Priority prior
}
default:
LOGGER.warning("Individual-indication: unhandled APDU-Type: %s", apdu.to_string().c_str());
LOGGER.warning("Individual-indication: unhandled APDU-Type: %S", apdu.toString());
}
}

View File

@ -400,9 +400,9 @@ bool CemiFrame::valid() const
return true;
}
std::string CemiFrame::to_string() const
std::string CemiFrame::toString() const
{
std::string value = enum_name(frameType()) + " ";
std::string value = "DPDU:" + enum_name(frameType()) + " ";
value += enum_name(systemBroadcast()) + " ";
value += enum_name(ack()) + " ";
value += enum_name(repetition()) + " ";

View File

@ -72,7 +72,7 @@ class CemiFrame
uint8_t calcCrcTP(uint8_t* buffer, uint16_t len);
bool valid() const;
std::string to_string() const;
std::string toString() const;
private:
uint8_t buffer[0xff + NPDU_LPDU_DIFF] = {0}; //only valid of add info is zero

View File

@ -161,7 +161,7 @@ void DataLinkLayer::frameReceived(CemiFrame& frame)
SystemBroadcast systemBroadcast = frame.systemBroadcast();
LOGGER.info("frameReceived %s", frame.to_string().c_str());
LOGGER.info("frameReceived %S", frame.toString());
#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.to_string().c_str());
LOGGER.info("sendTelegram %S", frame.toString());
if (!frame.valid())
{

View File

@ -1137,7 +1137,7 @@ bool IpDataLinkLayer::sendMulicast(KnxIpFrame& ipFrame)
if (!_enabled)
return false;
LOGGER.info("sendMulicast %s", ipFrame.to_string().c_str());
LOGGER.info("sendMulicast %S", ipFrame.toString());
return _platform.sendBytesMultiCast(ipFrame.data(), ipFrame.totalLength());
}
@ -1147,7 +1147,7 @@ bool IpDataLinkLayer::sendUnicast(uint32_t addr, uint16_t port, KnxIpFrame& ipFr
if (!_enabled)
return false;
LOGGER.info("sendUnicast to %s:%d %s", ipaddr2str(addr).c_str(), port, ipFrame.to_string().c_str());
LOGGER.info("sendUnicast to %S:%d %S", ipaddr2str(addr), port, ipFrame.toString());
return _platform.sendBytesMultiCast(ipFrame.data(), ipFrame.totalLength());
}

View File

@ -77,7 +77,7 @@ KnxIpFrame::KnxIpFrame(uint16_t length)
totalLength(length);
}
const std::string KnxIpFrame::to_string() const
const std::string KnxIpFrame::toString() const
{
return enum_name(protocolVersion()) + " " + enum_name(serviceTypeIdentifier());
}

View File

@ -48,7 +48,7 @@ class KnxIpFrame
uint16_t totalLength() const;
void totalLength(uint16_t length);
uint8_t* data();
const std::string to_string() const;
const std::string toString() const;
protected:
bool _freeData = false;
uint8_t* _data = 0;

View File

@ -3,6 +3,9 @@
#include "tpdu.h"
#include "cemi_frame.h"
#include "bits.h"
#include "util/logger.h"
#define LOGGER Logger::logger("NetworkLayerDevice")
NetworkLayerDevice::NetworkLayerDevice(DeviceObject& deviceObj, TransportLayer& layer) :
NetworkLayer(deviceObj, layer),
@ -29,6 +32,7 @@ void NetworkLayerDevice::dataIndividualRequest(AckType ack, uint16_t destination
// print.print("-> NL ");
// tpdu.apdu().printPDU();
//}
LOGGER.info("dataIndividualRequest %S", npdu.toString());
_netLayerEntities[kInterfaceIndex].sendDataRequest(npdu, ack, destination, _deviceObj.individualAddress(), priority, IndividualAddress, Broadcast);
}
@ -41,6 +45,7 @@ void NetworkLayerDevice::dataGroupRequest(AckType ack, uint16_t destination, Hop
else
npdu.hopCount(hopCount());
LOGGER.info("dataGroupRequest %S", npdu.toString());
_netLayerEntities[kInterfaceIndex].sendDataRequest(npdu, ack, destination, _deviceObj.individualAddress(), priority, GroupAddress, Broadcast);
}
@ -53,6 +58,7 @@ void NetworkLayerDevice::dataBroadcastRequest(AckType ack, HopCountType hopType,
else
npdu.hopCount(hopCount());
LOGGER.info("dataBroadcastRequest %S", npdu.toString());
_netLayerEntities[kInterfaceIndex].sendDataRequest(npdu, ack, 0, _deviceObj.individualAddress(), priority, GroupAddress, Broadcast);
}
@ -69,11 +75,13 @@ void NetworkLayerDevice::dataSystemBroadcastRequest(AckType ack, HopCountType ho
else
npdu.hopCount(hopCount());
LOGGER.info("dataSystemBroadcastRequest %S", npdu.toString());
_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());
HopCountType hopType = npdu.hopCount() == 7 ? UnlimitedRouting : NetworkLayerParameter;
if (addrType == IndividualAddress)
@ -95,6 +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());
HopCountType hopType = npdu.hopCount() == 7 ? UnlimitedRouting : NetworkLayerParameter;
if (addressType == IndividualAddress)
@ -113,6 +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());
HopCountType hopType = npdu.hopCount() == 7 ? UnlimitedRouting : NetworkLayerParameter;
DptMedium mediumType = _netLayerEntities[srcIfIdx].mediumType();
@ -133,18 +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());
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());
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());
HopCountType hopType = npdu.hopCount() == 7 ? UnlimitedRouting : NetworkLayerParameter;
_transportLayer.dataSystemBroadcastConfirm(ack, hopType, npdu.tpdu(), priority, status);
}

View File

@ -42,3 +42,8 @@ TPDU& NPDU::tpdu()
{
return _frame.tpdu();
}
const std::string NPDU::toString() const
{
return std::string("NPDU: Octetcount:") + to_string(octetCount()) + " hopCount " + to_string(hopCount());
}

View File

@ -1,6 +1,7 @@
#pragma once
#include <stdint.h>
#include <string>
class CemiFrame;
class TPDU;
@ -17,6 +18,7 @@ class NPDU
void hopCount(uint8_t value);
CemiFrame& frame();
TPDU& tpdu();
const std::string toString() const;
protected:
NPDU(uint8_t* data, CemiFrame& frame);

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))
{
LOGGER.info("SyncRequest: %s", request.apdu().to_string().c_str());
LOGGER.info("SyncRequest: %S", request.apdu().toString());
if (_syncReqBroadcastOutgoing)
{
@ -723,7 +723,7 @@ void SecureApplicationLayer::sendSyncResponse(uint16_t dstAddr, bool dstAddrIsGr
{
_lastSyncRes = millis();
LOGGER.info("SyncResponse: %s", response.apdu().to_string().c_str());
LOGGER.info("SyncResponse: %S", 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.to_string().c_str());
LOGGER.info("decodeSecureApdu: Secure APDU: %S", secureApdu.toString());
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().to_string().c_str());
LOGGER.info("decodeSecureApdu: Plain APDU: %S", plainApdu.frame().apdu().toString());
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().to_string().c_str());
LOGGER.info("createSecureApdu: Plain APDU: %S", plainApdu.frame().apdu().toString());
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().to_string().c_str());
LOGGER.info("createSecureApdu: Secure APDU: %S", secureApdu.frame().apdu().toString());
return true;
}

View File

@ -118,15 +118,15 @@ CemiFrame& TPDU::frame()
return _frame;
}
void TPDU::printPDU()
const std::string TPDU::toString() const
{
/* print.print("TPDU: ");
print.print(type(), HEX, 2);
print.print(" ");
for (uint8_t i = 0; i < apdu().length() + 1; ++i)
{
if (i) print.print(" ");
print.print(_data[i], HEX, 2);
}
print.println()*/;
std::string value = std::string("TPDU: ") + enum_name(type()) + " ";
if (control())
value += "control ";
if (numbered())
value += "numbered sequence: " + to_string(sequenceNumber());
return value;
}

View File

@ -2,6 +2,7 @@
#include "stdint.h"
#include "knx_types.h"
#include <string>
class CemiFrame;
class APDU;
@ -25,7 +26,7 @@ class TPDU
APDU& apdu();
CemiFrame& frame();
void printPDU();
const std::string toString() const;
protected:
TPDU(uint8_t* data, CemiFrame& frame);

View File

@ -5,8 +5,11 @@
#include "application_layer.h"
#include "platform.h"
#include "bits.h"
#include "util/logger.h"
#include <stdio.h>
#define LOGGER Logger::logger("TransportLayer")
TransportLayer::TransportLayer(ApplicationLayer& layer): _savedFrame(0),
_savedFrameConnecting(0), _applicationLayer(layer)
{
@ -25,13 +28,7 @@ void TransportLayer::groupAddressTable(AddressTableObject& addrTable)
void TransportLayer::dataIndividualIndication(uint16_t destination, HopCountType hopType, Priority priority, uint16_t source, TPDU& tpdu)
{
//if (tpdu.apdu().length() > 0)
//{
// print.print("<- TL ");
// tpdu.printPDU();
// print.print("<- TL ");
// tpdu.apdu().printPDU();
//}
LOGGER.info("dataIndividualIndication %S", tpdu.toString());
uint8_t sequenceNo = tpdu.sequenceNumber();
@ -334,6 +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());
TpduType type = tpdu.type();
switch (type)
@ -407,39 +405,49 @@ 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());
if (_groupAddressTable == nullptr)
return;
uint16_t tsap = _groupAddressTable->getTsap(destination);
if (tsap == 0)
{
LOGGER.info("dataGroupIndication telegram is not for us");
return;
}
_applicationLayer.dataGroupIndication(hopType, priority, tsap, tpdu.apdu());
}
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());
_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());
_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());
_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());
_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());
_applicationLayer.dataSystemBroadcastConfirm(hopType, priority, tpdu.apdu(), status);
}
@ -450,26 +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());
_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());
_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());
return _networkLayer->dataSystemBroadcastRequest(ack, hopType, priority, tpdu);
}
void TransportLayer::dataIndividualRequest(AckType ack, HopCountType hopType, Priority priority, uint16_t destination, APDU& apdu)
{
//print.print("-> TL ");
//apdu.printPDU();
TPDU& tpdu = apdu.frame().tpdu();
LOGGER.info("dataIndividualRequest %S", tpdu.toString());
_networkLayer->dataIndividualRequest(ack, destination, hopType, priority, tpdu);
}
@ -631,6 +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());
_networkLayer->dataIndividualRequest(AckRequested, _connectionAddress, NetworkLayerParameter,
SystemPriority, tpdu);
}

View File

@ -65,16 +65,16 @@ class Logger
return "INFO";
case LogType::Warning:
return "WARNING";
return "WARN";
case LogType::Error:
return "ERROR";
return "ERR ";
case LogType::Critical:
return "CRITICAL";
return "CRIT";
case LogType::Exception:
return "EXCEPTION";
return "EXCE";
}
return to_string(type);
}
@ -84,11 +84,11 @@ class Logger
{
#ifndef KNX_NO_PRINT
print(millis());
print("\t");
print(" ");
print(_name.c_str());
print("\t");
print(enum_name(type).c_str());
print("\t");
print(" ");
while (*format)
{
@ -104,6 +104,10 @@ class Logger
{
print(va_arg(args, char*));
}
else if (*format == 'S')
{
print(va_arg(args, std::string).c_str());
}
else if (*format == 'f')
{
print(va_arg(args, double));