Skip to content

Commit

Permalink
Merge branch 'rp2040_ip_routing' of https://github.com/OpenKNX/knx in…
Browse files Browse the repository at this point in the history
…to rp2040_ip_routing
  • Loading branch information
thewhobox committed Jan 2, 2024
2 parents 27dc260 + 6f1dca7 commit afb577f
Show file tree
Hide file tree
Showing 21 changed files with 135 additions and 71 deletions.
25 changes: 25 additions & 0 deletions src/esp32_platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,42 @@
#define KNX_SERIAL Serial1
#endif

#ifndef KNX_UART_RX_PIN
#define KNX_UART_RX_PIN -1
#endif

#ifndef KNX_UART_TX_PIN
#define KNX_UART_TX_PIN -1
#endif

Esp32Platform::Esp32Platform()
#ifndef KNX_NO_DEFAULT_UART
: ArduinoPlatform(&KNX_SERIAL)
#endif
{
#ifndef KNX_NO_DEFAULT_UART
knxUartPins(KNX_UART_RX_PIN, KNX_UART_TX_PIN);
#endif
}

Esp32Platform::Esp32Platform(HardwareSerial* s) : ArduinoPlatform(s)
{
}

void Esp32Platform::knxUartPins(int8_t rxPin, int8_t txPin)
{
_rxPin = rxPin;
_txPin = txPin;
}

// ESP specific uart handling with pins
void Esp32Platform::setupUart()
{
_knxSerial->begin(19200, SERIAL_8E1, _rxPin, _txPin);
while (!_knxSerial)
;
}

uint32_t Esp32Platform::currentIpAddress()
{
return WiFi.localIP();
Expand Down
6 changes: 6 additions & 0 deletions src/esp32_platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ class Esp32Platform : public ArduinoPlatform
Esp32Platform();
Esp32Platform(HardwareSerial* s);

// uart
void knxUartPins(int8_t rxPin, int8_t txPin);
void setupUart() override;

// ip stuff
uint32_t currentIpAddress() override;
uint32_t currentSubnetMask() override;
Expand All @@ -36,6 +40,8 @@ class Esp32Platform : public ArduinoPlatform
void commitToEeprom();
private:
WiFiUDP _udp;
int8_t _rxPin = -1;
int8_t _txPin = -1;
};

#endif
10 changes: 5 additions & 5 deletions src/knx/application_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ void ApplicationLayer::systemNetworkParameterReadResponse(Priority priority, Hop
void ApplicationLayer::domainAddressSerialNumberReadResponse(Priority priority, HopCountType hopType, const SecurityControl &secCtrl, const uint8_t* rfDoA,
const uint8_t* knxSerialNumber)
{
CemiFrame frame(11);
CemiFrame frame(13);
APDU& apdu = frame.apdu();
apdu.type(DomainAddressSerialNumberResponse);

Expand All @@ -567,19 +567,19 @@ void ApplicationLayer::domainAddressSerialNumberReadResponse(Priority priority,

//TODO: ApplicationLayer::IndividualAddressSerialNumberWriteRequest()
//TODO: ApplicationLayer::IndividualAddressSerialNumberReadRequest()
void ApplicationLayer::IndividualAddressSerialNumberReadResponse(Priority priority, HopCountType hopType, const SecurityControl &secCtrl, const uint8_t* rfDoA,
void ApplicationLayer::IndividualAddressSerialNumberReadResponse(Priority priority, HopCountType hopType, const SecurityControl &secCtrl, const uint8_t* domainAddress,
const uint8_t* knxSerialNumber)
{
CemiFrame frame(13);
CemiFrame frame(11);
APDU& apdu = frame.apdu();
apdu.type(IndividualAddressSerialNumberResponse);

uint8_t* data = apdu.data() + 1;

memcpy(data, knxSerialNumber, 6);
memcpy(data + 6, rfDoA, 6);
memcpy(data + 6, domainAddress, 2);

//apdu.printPDU();
//apdu.printPDU();

dataBroadcastRequest(AckDontCare, hopType, SystemPriority, apdu, secCtrl);
}
Expand Down
2 changes: 1 addition & 1 deletion src/knx/application_layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ class ApplicationLayer
uint8_t* testResult, uint16_t testResultLength);
void domainAddressSerialNumberReadResponse(Priority priority, HopCountType hopType, const SecurityControl& secCtrl, const uint8_t* rfDoA,
const uint8_t* knxSerialNumber);
void IndividualAddressSerialNumberReadResponse(Priority priority, HopCountType hopType, const SecurityControl& secCtrl, const uint8_t* rfDoA,
void IndividualAddressSerialNumberReadResponse(Priority priority, HopCountType hopType, const SecurityControl& secCtrl, const uint8_t* domainAddress,
const uint8_t* knxSerialNumber);
#pragma endregion

Expand Down
3 changes: 2 additions & 1 deletion src/knx/bau07B0.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ using namespace std;

Bau07B0::Bau07B0(Platform& platform)
: BauSystemBDevice(platform),
_dlLayer(_deviceObj, _netLayer.getInterface(), _platform, (ITpUartCallBacks&) *this)
_dlLayer(_deviceObj, _netLayer.getInterface(), _platform, (ITpUartCallBacks&) *this, (DataLinkLayerCallbacks*) this),
DataLinkLayerCallbacks()
#ifdef USE_CEMI_SERVER
, _cemiServer(*this)
#endif
Expand Down
2 changes: 1 addition & 1 deletion src/knx/bau07B0.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "cemi_server.h"
#include "cemi_server_object.h"

class Bau07B0 : public BauSystemBDevice, public ITpUartCallBacks
class Bau07B0 : public BauSystemBDevice, public ITpUartCallBacks, public DataLinkLayerCallbacks
{
public:
Bau07B0(Platform& platform);
Expand Down
5 changes: 3 additions & 2 deletions src/knx/bau091A.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ Bau091A::Bau091A(Platform& platform)
: BauSystemBCoupler(platform),
_routerObj(memory(), 0x200, 0x2000), // the Filtertable of 0x091A IP Routers is fixed at 0x200 and 0x2000 long
_ipParameters(_deviceObj, platform),
_dlLayerPrimary(_deviceObj, _ipParameters, _netLayer.getPrimaryInterface(), _platform),
_dlLayerSecondary(_deviceObj, _netLayer.getSecondaryInterface(), platform, (ITpUartCallBacks&) *this)
_dlLayerPrimary(_deviceObj, _ipParameters, _netLayer.getPrimaryInterface(), _platform, (DataLinkLayerCallbacks*) this),
_dlLayerSecondary(_deviceObj, _netLayer.getSecondaryInterface(), platform, (ITpUartCallBacks&) *this, (DataLinkLayerCallbacks*) this),
DataLinkLayerCallbacks()
#ifdef USE_CEMI_SERVER
,
_cemiServer(*this)
Expand Down
2 changes: 1 addition & 1 deletion src/knx/bau091A.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include "tpuart_data_link_layer.h"
#include "cemi_server_object.h"

class Bau091A : public BauSystemBCoupler, public ITpUartCallBacks
class Bau091A : public BauSystemBCoupler, public ITpUartCallBacks, public DataLinkLayerCallbacks
{
public:
Bau091A(Platform& platform);
Expand Down
5 changes: 1 addition & 4 deletions src/knx/bau27B0.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,7 @@ void Bau27B0::domainAddressSerialNumberReadIndication(Priority priority, HopCoun

void Bau27B0::individualAddressSerialNumberReadIndication(Priority priority, HopCountType hopType, const SecurityControl &secCtrl, uint8_t* knxSerialNumber)
{
// If the received serial number matches our serial number
// then send a response with the current RF domain address stored in the RF medium object and the serial number
if (!memcmp(knxSerialNumber, _deviceObj.propertyData(PID_SERIAL_NUMBER), 6))
_appLayer.IndividualAddressSerialNumberReadResponse(priority, hopType, secCtrl, _rfMediumObj.rfDomainAddress(), knxSerialNumber);
#pragma warning "individualAddressSerialNumberReadIndication is not available for rf"
}

void Bau27B0::domainAddressSerialNumberWriteLocalConfirm(Priority priority, HopCountType hopType, const SecurityControl &secCtrl, const uint8_t* rfDoA,
Expand Down
3 changes: 2 additions & 1 deletion src/knx/bau57B0.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ using namespace std;
Bau57B0::Bau57B0(Platform& platform)
: BauSystemBDevice(platform),
_ipParameters(_deviceObj, platform),
_dlLayer(_deviceObj, _ipParameters, _netLayer.getInterface(), _platform)
_dlLayer(_deviceObj, _ipParameters, _netLayer.getInterface(), _platform, (DataLinkLayerCallbacks*) this),
DataLinkLayerCallbacks()
#ifdef USE_CEMI_SERVER
,
_cemiServer(*this)
Expand Down
2 changes: 1 addition & 1 deletion src/knx/bau57B0.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "ip_data_link_layer.h"
#include "cemi_server_object.h"

class Bau57B0 : public BauSystemBDevice
class Bau57B0 : public BauSystemBDevice, public DataLinkLayerCallbacks
{
public:
Bau57B0(Platform& platform);
Expand Down
2 changes: 1 addition & 1 deletion src/knx/bau_systemB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,7 @@ void BauSystemB::individualAddressSerialNumberReadIndication(Priority priority,
// An open medium BAU has to override this method and provide a proper domain address.
if (!memcmp(knxSerialNumber, _deviceObj.propertyData(PID_SERIAL_NUMBER), 6))
{
uint8_t emptyDomainAddress[6] = {0x00};
uint8_t emptyDomainAddress[2] = {0x00};
applicationLayer().IndividualAddressSerialNumberReadResponse(priority, hopType, secCtrl, emptyDomainAddress, knxSerialNumber);
}
}
Expand Down
12 changes: 12 additions & 0 deletions src/knx/data_link_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@
#include "cemi_server.h"
#include "cemi_frame.h"


void DataLinkLayerCallbacks::activity(uint8_t info)
{
if(_activityCallback)
_activityCallback(info);
}

void DataLinkLayerCallbacks::setActivityCallback(ActivityCallback activityCallback)
{
_activityCallback = activityCallback;
}

DataLinkLayer::DataLinkLayer(DeviceObject& devObj, NetworkLayerEntity& netLayerEntity, Platform& platform) :
_deviceObject(devObj), _networkLayerEntity(netLayerEntity), _platform(platform)
{
Expand Down
12 changes: 12 additions & 0 deletions src/knx/data_link_layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@

class Platform;

typedef void (*ActivityCallback)(uint8_t info);

class DataLinkLayerCallbacks
{
protected:
ActivityCallback _activityCallback = nullptr;
public:
virtual ~DataLinkLayerCallbacks() = default;
virtual void activity(uint8_t info);
virtual void setActivityCallback(ActivityCallback activityCallback);
};

class DataLinkLayer
{
public:
Expand Down
15 changes: 7 additions & 8 deletions src/knx/ip_data_link_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#define MIN_LEN_CEMI 10

IpDataLinkLayer::IpDataLinkLayer(DeviceObject& devObj, IpParameterObject& ipParam,
NetworkLayerEntity &netLayerEntity, Platform& platform) : DataLinkLayer(devObj, netLayerEntity, platform), _ipParameters(ipParam)
NetworkLayerEntity &netLayerEntity, Platform& platform, DataLinkLayerCallbacks* dllcb) : DataLinkLayer(devObj, netLayerEntity, platform), _ipParameters(ipParam), _dllcb(dllcb)
{
}

Expand All @@ -45,7 +45,8 @@ bool IpDataLinkLayer::sendFrame(CemiFrame& frame)
return false;
bool success = sendBytes(packet.data(), packet.totalLength());
#ifdef KNX_ACTIVITYCALLBACK
knx.Activity((_netIndex << KNX_ACTIVITYCALLBACK_NET) | (KNX_ACTIVITYCALLBACK_DIR_SEND << KNX_ACTIVITYCALLBACK_DIR));
if(_dllcb)
_dllcb->activity((_netIndex << KNX_ACTIVITYCALLBACK_NET) | (KNX_ACTIVITYCALLBACK_DIR_SEND << KNX_ACTIVITYCALLBACK_DIR));
#endif
dataConReceived(frame, success);
return success;
Expand Down Expand Up @@ -261,7 +262,8 @@ void IpDataLinkLayer::loop()
return;

#ifdef KNX_ACTIVITYCALLBACK
knx.Activity((_netIndex << KNX_ACTIVITYCALLBACK_NET) | (KNX_ACTIVITYCALLBACK_DIR_RECV << KNX_ACTIVITYCALLBACK_DIR));
if(_dllcb)
_dllcb->activity((_netIndex << KNX_ACTIVITYCALLBACK_NET) | (KNX_ACTIVITYCALLBACK_DIR_RECV << KNX_ACTIVITYCALLBACK_DIR));
#endif

uint16_t code;
Expand All @@ -282,18 +284,17 @@ void IpDataLinkLayer::loop()

auto hpai = searchRequest.hpai();
#ifdef KNX_ACTIVITYCALLBACK
knx.Activity((_netIndex << KNX_ACTIVITYCALLBACK_NET) | (KNX_ACTIVITYCALLBACK_DIR_SEND << KNX_ACTIVITYCALLBACK_DIR) | (KNX_ACTIVITYCALLBACK_IPUNICAST));
if(_dllcb)
_dllcb->activity((_netIndex << KNX_ACTIVITYCALLBACK_NET) | (KNX_ACTIVITYCALLBACK_DIR_SEND << KNX_ACTIVITYCALLBACK_DIR) | (KNX_ACTIVITYCALLBACK_IPUNICAST));
#endif
_platform.sendBytesUniCast(hpai.ipAddress(), hpai.ipPortNumber(), searchResponse.data(), searchResponse.totalLength());
break;
}

case SearchRequestExt:
{
// FIXME, implement (not needed atm)
break;
}

#ifdef KNX_TUNNELING
case ConnectRequest:
{
Expand Down Expand Up @@ -346,10 +347,8 @@ void IpDataLinkLayer::loop()
}
#endif
default:
#if defined(KNX_LOG_TUNNELING) || defined(KNX_LOG_IP)
print("Unhandled service identifier: ");
println(code, HEX);
#endif
break;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/knx/ip_data_link_layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class IpDataLinkLayer : public DataLinkLayer

public:
IpDataLinkLayer(DeviceObject& devObj, IpParameterObject& ipParam, NetworkLayerEntity& netLayerEntity,
Platform& platform);
Platform& platform, DataLinkLayerCallbacks* dllcb = nullptr);

void loop();
void enabled(bool value);
Expand Down Expand Up @@ -45,7 +45,7 @@ class IpDataLinkLayer : public DataLinkLayer
bool isSendLimitReached();

IpParameterObject& _ipParameters;

DataLinkLayerCallbacks* _dllcb;
#ifdef KNX_TUNNELING
KnxIpTunnelConnection tunnels[KNX_TUNNELING];
uint8_t _lastChannelId = 1;
Expand Down
12 changes: 8 additions & 4 deletions src/knx/tpuart_data_link_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -553,17 +553,20 @@ void TpUartDataLinkLayer::setFrameRepetition(uint8_t nack, uint8_t busy)
TpUartDataLinkLayer::TpUartDataLinkLayer(DeviceObject& devObj,
NetworkLayerEntity &netLayerEntity,
Platform& platform,
ITpUartCallBacks& cb)
ITpUartCallBacks& cb,
DataLinkLayerCallbacks* dllcb)
: DataLinkLayer(devObj, netLayerEntity, platform),
_cb(cb)
_cb(cb),
_dllcb(dllcb)
{
}

void TpUartDataLinkLayer::frameBytesReceived(uint8_t* buffer, uint16_t length)
{
//printHex("=>", buffer, length);
#ifdef KNX_ACTIVITYCALLBACK
knx.Activity((_netIndex << KNX_ACTIVITYCALLBACK_NET) | (KNX_ACTIVITYCALLBACK_DIR_RECV << KNX_ACTIVITYCALLBACK_DIR));
if(_dllcb)
_dllcb->activity((_netIndex << KNX_ACTIVITYCALLBACK_NET) | (KNX_ACTIVITYCALLBACK_DIR_RECV << KNX_ACTIVITYCALLBACK_DIR));
#endif
CemiFrame frame(buffer, length);
frameReceived(frame);
Expand Down Expand Up @@ -672,7 +675,8 @@ bool TpUartDataLinkLayer::sendSingleFrameByte()
{
_TxByteCnt = 0;
#ifdef KNX_ACTIVITYCALLBACK
knx.Activity((_netIndex << KNX_ACTIVITYCALLBACK_NET) | (KNX_ACTIVITYCALLBACK_DIR_SEND << KNX_ACTIVITYCALLBACK_DIR));
if(_dllcb)
_dllcb->activity((_netIndex << KNX_ACTIVITYCALLBACK_NET) | (KNX_ACTIVITYCALLBACK_DIR_SEND << KNX_ACTIVITYCALLBACK_DIR));
#endif
return false;
}
Expand Down
5 changes: 4 additions & 1 deletion src/knx/tpuart_data_link_layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ class TpUartDataLinkLayer : public DataLinkLayer

public:
TpUartDataLinkLayer(DeviceObject& devObj, NetworkLayerEntity& netLayerEntity,
Platform& platform, ITpUartCallBacks& cb);
Platform& platform, ITpUartCallBacks& cb, DataLinkLayerCallbacks* dllcb = nullptr);



void loop();
void enabled(bool value);
Expand Down Expand Up @@ -77,5 +79,6 @@ class TpUartDataLinkLayer : public DataLinkLayer
void stopChip();

ITpUartCallBacks& _cb;
DataLinkLayerCallbacks* _dllcb;
};
#endif
16 changes: 0 additions & 16 deletions src/knx_facade.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,22 +189,6 @@ template <class P, class B> class KnxFacade : private SaveRestore
{
_progLedOnCallback = progLedOnCallback;
}

#ifdef KNX_ACTIVITYCALLBACK
/// @brief sets the Callback Function indicating sent or received telegrams
/// @param activityCallback
/// @details the info parameter
void setActivityCallback(ActivityCallback activityCallback)
{
_activityCallback = activityCallback;
}

void Activity(uint8_t info)
{
if(_activityCallback)
_activityCallback(info);
}
#endif

int32_t buttonPin()
{
Expand Down
Loading

0 comments on commit afb577f

Please sign in to comment.