2018-04-09 22:30:23 +02:00
|
|
|
#include "data_link_layer.h"
|
|
|
|
|
|
|
|
#include "bits.h"
|
|
|
|
#include "platform.h"
|
|
|
|
#include "device_object.h"
|
|
|
|
#include "address_table_object.h"
|
|
|
|
|
|
|
|
|
2018-06-05 00:32:26 +02:00
|
|
|
DataLinkLayer::DataLinkLayer(DeviceObject& devObj, AddressTableObject& addrTab,
|
2018-04-09 22:30:23 +02:00
|
|
|
NetworkLayer& layer, Platform& platform) :
|
2018-06-05 00:32:26 +02:00
|
|
|
_deviceObject(devObj), _groupAddressTable(addrTab), _networkLayer(layer), _platform(platform)
|
2018-04-09 22:30:23 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataLinkLayer::dataRequest(AckType ack, AddressType addrType, uint16_t destinationAddr, FrameFormat format, Priority priority, NPDU& npdu)
|
|
|
|
{
|
2018-06-05 00:32:26 +02:00
|
|
|
bool success = sendTelegram(npdu, ack, destinationAddr, addrType, format, priority);
|
2018-04-09 22:30:23 +02:00
|
|
|
_networkLayer.dataConfirm(ack, addrType, destinationAddr, format, priority, npdu.frame().sourceAddress(), npdu, success);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DataLinkLayer::systemBroadcastRequest(AckType ack, FrameFormat format, Priority priority, NPDU& npdu)
|
|
|
|
{
|
2018-06-05 00:32:26 +02:00
|
|
|
bool success = sendTelegram(npdu, ack, 0, GroupAddress, format, priority);
|
2018-04-09 22:30:23 +02:00
|
|
|
_networkLayer.systemBroadcastConfirm(ack, format, priority, npdu.frame().sourceAddress(), npdu, success);
|
|
|
|
}
|
|
|
|
|
2018-07-14 01:15:26 +02:00
|
|
|
void DataLinkLayer::frameRecieved(CemiFrame& frame)
|
2018-04-09 22:30:23 +02:00
|
|
|
{
|
|
|
|
AckType ack = frame.ack();
|
|
|
|
AddressType addrType = frame.addressType();
|
|
|
|
uint16_t destination = frame.destinationAddress();
|
|
|
|
uint16_t source = frame.sourceAddress();
|
|
|
|
FrameFormat type = frame.frameType();
|
|
|
|
Priority priority = frame.priority();
|
|
|
|
NPDU& npdu = frame.npdu();
|
|
|
|
uint16_t ownAddr = _deviceObject.induvidualAddress();
|
|
|
|
|
|
|
|
if (source == ownAddr)
|
|
|
|
_deviceObject.induvidualAddressDuplication(true);
|
2018-06-05 00:32:26 +02:00
|
|
|
|
2018-04-09 22:30:23 +02:00
|
|
|
if (addrType == GroupAddress && destination == 0)
|
|
|
|
_networkLayer.systemBroadcastIndication(ack, type, npdu, priority, source);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (addrType == InduvidualAddress && destination != _deviceObject.induvidualAddress())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (addrType == GroupAddress && !_groupAddressTable.contains(destination))
|
|
|
|
return;
|
|
|
|
|
|
|
|
//if (frame.npdu().octetCount() > 0)
|
|
|
|
//{
|
|
|
|
// print.print("<- DLL ");
|
|
|
|
// frame.apdu().printPDU();
|
|
|
|
//}
|
|
|
|
|
|
|
|
_networkLayer.dataIndication(ack, addrType, destination, type, npdu, priority, source);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-05 00:32:26 +02:00
|
|
|
bool DataLinkLayer::sendTelegram(NPDU & npdu, AckType ack, uint16_t destinationAddr, AddressType addrType, FrameFormat format, Priority priority)
|
2018-04-09 22:30:23 +02:00
|
|
|
{
|
2018-06-05 00:32:26 +02:00
|
|
|
CemiFrame& frame = npdu.frame();
|
|
|
|
frame.messageCode(L_data_ind);
|
|
|
|
frame.destinationAddress(destinationAddr);
|
|
|
|
frame.sourceAddress(_deviceObject.induvidualAddress());
|
|
|
|
frame.addressType(addrType);
|
|
|
|
frame.priority(priority);
|
|
|
|
frame.repetition(RepititionAllowed);
|
|
|
|
|
|
|
|
if (npdu.octetCount() <= 15)
|
|
|
|
frame.frameType(StandardFrame);
|
|
|
|
else
|
|
|
|
frame.frameType(format);
|
|
|
|
|
2018-04-09 22:30:23 +02:00
|
|
|
|
2018-06-05 00:32:26 +02:00
|
|
|
if (!frame.valid())
|
2018-04-09 22:30:23 +02:00
|
|
|
{
|
2018-08-16 22:56:51 +02:00
|
|
|
_println("invalid frame");
|
2018-06-05 00:32:26 +02:00
|
|
|
return false;
|
2018-04-09 22:30:23 +02:00
|
|
|
}
|
|
|
|
|
2018-06-05 00:32:26 +02:00
|
|
|
//if (frame.npdu().octetCount() > 0)
|
|
|
|
//{
|
|
|
|
// print.print("-> DLL ");
|
|
|
|
// frame.apdu().printPDU();
|
|
|
|
//}
|
2018-04-09 22:30:23 +02:00
|
|
|
|
2018-06-05 00:32:26 +02:00
|
|
|
return sendFrame(frame);
|
|
|
|
}
|
2018-04-09 22:30:23 +02:00
|
|
|
|
2018-06-05 00:32:26 +02:00
|
|
|
uint8_t* DataLinkLayer::frameData(CemiFrame& frame)
|
2018-04-09 22:30:23 +02:00
|
|
|
{
|
2018-06-05 00:32:26 +02:00
|
|
|
return frame._data;
|
|
|
|
}
|