Skip to content

Commit

Permalink
Add the possibility to have a callback function signaling knx activit…
Browse files Browse the repository at this point in the history
…y (for e.g. flashing LEDs)

Can be enabled with KNX_ACTIVITYCALLBACK

# Conflicts:
#	src/knx/ip_data_link_layer.cpp
  • Loading branch information
Ing-Dom committed Dec 17, 2023
1 parent d44606d commit 53842ad
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/knx/bits.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ void printHex(const char* suffix, const uint8_t *data, size_t length, bool newli
#define printHex(...) do {} while(0)
#endif

#ifdef KNX_ACTIVITYCALLBACK
#define KNX_ACTIVITYCALLBACK_DIR 0x00
#define KNX_ACTIVITYCALLBACK_DIR_RECV 0x00
#define KNX_ACTIVITYCALLBACK_DIR_SEND 0x01
#define KNX_ACTIVITYCALLBACK_IPUNICAST 0x02
#define KNX_ACTIVITYCALLBACK_NET 0x04
#endif

const uint8_t* popByte(uint8_t& b, const uint8_t* data);
const uint8_t* popWord(uint16_t& w, const uint8_t* data);
const uint8_t* popInt(uint32_t& i, const uint8_t* data);
Expand Down
3 changes: 3 additions & 0 deletions src/knx/data_link_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
DataLinkLayer::DataLinkLayer(DeviceObject& devObj, NetworkLayerEntity& netLayerEntity, Platform& platform) :
_deviceObject(devObj), _networkLayerEntity(netLayerEntity), _platform(platform)
{
#ifdef KNX_ACTIVITYCALLBACK
_netIndex = netLayerEntity.getEntityIndex();
#endif
}

#ifdef USE_CEMI_SERVER
Expand Down
5 changes: 4 additions & 1 deletion src/knx/data_link_layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,8 @@ class DataLinkLayer
Platform& _platform;
#ifdef USE_CEMI_SERVER
CemiServer* _cemiServer;
#endif
#endif
#ifdef KNX_ACTIVITYCALLBACK
uint8_t _netIndex = 0;
#endif
};
10 changes: 10 additions & 0 deletions src/knx/ip_data_link_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ bool IpDataLinkLayer::sendFrame(CemiFrame& frame)
if(isSendLimitReached())
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));
#endif
dataConReceived(frame, success);
return success;
}
Expand All @@ -51,6 +54,10 @@ void IpDataLinkLayer::loop()
|| buffer[1] != KNXIP_PROTOCOL_VERSION)
return;

#ifdef KNX_ACTIVITYCALLBACK
knx.Activity((_netIndex << KNX_ACTIVITYCALLBACK_NET) | (KNX_ACTIVITYCALLBACK_DIR_RECV << KNX_ACTIVITYCALLBACK_DIR));
#endif

uint16_t code;
popWord(code, buffer + 2);
switch ((KnxIpServiceType)code)
Expand All @@ -67,6 +74,9 @@ void IpDataLinkLayer::loop()
KnxIpSearchResponse searchResponse(_ipParameters, _deviceObject);

auto hpai = searchRequest.hpai();
#ifdef KNX_ACTIVITYCALLBACK
knx.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;
}
Expand Down
5 changes: 5 additions & 0 deletions src/knx/network_layer_entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ DptMedium NetworkLayerEntity::mediumType() const
return _dataLinkLayer->mediumType();
}

uint8_t NetworkLayerEntity::getEntityIndex()
{
return _entityIndex;
}

void NetworkLayerEntity::dataIndication(AckType ack, AddressType addrType, uint16_t destination, FrameFormat format, NPDU& npdu, Priority priority, uint16_t source)
{
_netLayer.dataIndication(ack, addrType, destination, format, npdu, priority, source, _entityIndex);
Expand Down
1 change: 1 addition & 0 deletions src/knx/network_layer_entity.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class NetworkLayerEntity
DataLinkLayer& dataLinkLayer();

DptMedium mediumType() const;
uint8_t getEntityIndex();

// from data link layer
void dataIndication(AckType ack, AddressType addType, uint16_t destination, FrameFormat format, NPDU& npdu,
Expand Down
8 changes: 7 additions & 1 deletion src/knx/tpuart_data_link_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "device_object.h"
#include "address_table_object.h"
#include "cemi_frame.h"
#include "knx_facade.h"

#include <stdio.h>
#include <string.h>
Expand Down Expand Up @@ -546,8 +547,10 @@ TpUartDataLinkLayer::TpUartDataLinkLayer(DeviceObject& devObj,
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));
#endif
CemiFrame frame(buffer, length);

frameReceived(frame);
}

Expand Down Expand Up @@ -653,6 +656,9 @@ bool TpUartDataLinkLayer::sendSingleFrameByte()
if (_TxByteCnt >= _sendBufferLength)
{
_TxByteCnt = 0;
#ifdef KNX_ACTIVITYCALLBACK
knx.Activity((_netIndex << KNX_ACTIVITYCALLBACK_NET) | (KNX_ACTIVITYCALLBACK_DIR_SEND << KNX_ACTIVITYCALLBACK_DIR));
#endif
return false;
}
return true;
Expand Down
21 changes: 21 additions & 0 deletions src/knx_facade.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ typedef uint8_t* (*SaveCallback)(uint8_t* buffer);
typedef void (*IsrFunctionPtr)();
typedef void (*ProgLedOnCallback)();
typedef void (*ProgLedOffCallback)();
#ifdef KNX_ACTIVITYCALLBACK
typedef void (*ActivityCallback)(uint8_t info);
#endif

template <class P, class B> class KnxFacade : private SaveRestore
{
Expand Down Expand Up @@ -187,6 +190,21 @@ 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 Expand Up @@ -414,6 +432,9 @@ template <class P, class B> class KnxFacade : private SaveRestore
B& _bau;
ProgLedOnCallback _progLedOnCallback = 0;
ProgLedOffCallback _progLedOffCallback = 0;
#ifdef KNX_ACTIVITYCALLBACK
ActivityCallback _activityCallback = 0;
#endif
uint32_t _ledPinActiveOn = KNX_LED_ACTIVE_ON;
uint32_t _ledPin = KNX_LED;
int32_t _buttonPin = KNX_BUTTON;
Expand Down

0 comments on commit 53842ad

Please sign in to comment.