mirror of
https://github.com/thelsing/knx.git
synced 2025-04-23 01:16:29 +02:00
- add some knx-ip classes
- return no data if Property has no elements - implement get{ipAddress, netmask, defaultGw, macAddress) for linux platform
This commit is contained in:
parent
8f61ef8608
commit
6ae000482f
@ -51,6 +51,8 @@ add_executable(knx-linux
|
||||
../../src/knx/ip_parameter_object.h
|
||||
../../src/knx/knx_ip_frame.cpp
|
||||
../../src/knx/knx_ip_routing_indication.cpp
|
||||
../../src/knx/knx_ip_search_request.cpp
|
||||
../../src/knx/ip_host_protocol_address_information.cpp
|
||||
../../src/knx/knx_value.cpp
|
||||
../../src/knx/knx_value.h
|
||||
../../src/knx/memory.cpp
|
||||
|
@ -97,9 +97,13 @@
|
||||
<ClInclude Include="..\..\src\knx\group_object_table_object.h" />
|
||||
<ClInclude Include="..\..\src\knx\interface_object.h" />
|
||||
<ClInclude Include="..\..\src\knx\ip_data_link_layer.h" />
|
||||
<ClInclude Include="..\..\src\knx\ip_host_protocol_address_information.h" />
|
||||
<ClInclude Include="..\..\src\knx\ip_parameter_object.h" />
|
||||
<ClInclude Include="..\..\src\knx\knx_ip_dib.h" />
|
||||
<ClInclude Include="..\..\src\knx\knx_ip_frame.h" />
|
||||
<ClInclude Include="..\..\src\knx\knx_ip_routing_indication.h" />
|
||||
<ClInclude Include="..\..\src\knx\knx_ip_search_request.h" />
|
||||
<ClInclude Include="..\..\src\knx\knx_ip_search_response.h" />
|
||||
<ClInclude Include="..\..\src\knx\knx_types.h" />
|
||||
<ClInclude Include="..\..\src\knx\knx_value.h" />
|
||||
<ClInclude Include="..\..\src\knx\memory.h" />
|
||||
@ -151,9 +155,13 @@
|
||||
<ClCompile Include="..\..\src\knx\group_object_table_object.cpp" />
|
||||
<ClCompile Include="..\..\src\knx\interface_object.cpp" />
|
||||
<ClCompile Include="..\..\src\knx\ip_data_link_layer.cpp" />
|
||||
<ClCompile Include="..\..\src\knx\ip_host_protocol_address_information.cpp" />
|
||||
<ClCompile Include="..\..\src\knx\ip_parameter_object.cpp" />
|
||||
<ClCompile Include="..\..\src\knx\knx_ip_dib.cpp" />
|
||||
<ClCompile Include="..\..\src\knx\knx_ip_frame.cpp" />
|
||||
<ClCompile Include="..\..\src\knx\knx_ip_routing_indication.cpp" />
|
||||
<ClCompile Include="..\..\src\knx\knx_ip_search_request.cpp" />
|
||||
<ClCompile Include="..\..\src\knx\knx_ip_search_response.cpp" />
|
||||
<ClCompile Include="..\..\src\knx\knx_value.cpp" />
|
||||
<ClCompile Include="..\..\src\knx\memory.cpp" />
|
||||
<ClCompile Include="..\..\src\knx\network_layer.cpp" />
|
||||
|
@ -176,6 +176,18 @@
|
||||
<ClInclude Include="..\..\src\knx\knx_ip_routing_indication.h">
|
||||
<Filter>Header files\knx</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\src\knx\knx_ip_search_request.h">
|
||||
<Filter>Header files\knx</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\src\knx\ip_host_protocol_address_information.h">
|
||||
<Filter>Header files\knx</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\src\knx\knx_ip_search_response.h">
|
||||
<Filter>Header files\knx</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="..\..\src\knx\knx_ip_dib.h">
|
||||
<Filter>Header files\knx</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="main.cpp">
|
||||
@ -310,5 +322,17 @@
|
||||
<ClCompile Include="..\..\src\knx\knx_ip_routing_indication.cpp">
|
||||
<Filter>Source files\knx</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\..\src\knx\ip_host_protocol_address_information.cpp">
|
||||
<Filter>Source files\knx</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\..\src\knx\knx_ip_search_request.cpp">
|
||||
<Filter>Source files\knx</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\..\src\knx\knx_ip_search_response.cpp">
|
||||
<Filter>Source files\knx</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="..\..\src\knx\knx_ip_dib.cpp">
|
||||
<Filter>Source files\knx</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
</Project>
|
@ -232,6 +232,10 @@ void BauSystemB::propertyValueReadIndication(Priority priority, HopCountType hop
|
||||
uint8_t data[size];
|
||||
if(obj)
|
||||
obj->readProperty((PropertyID)propertyId, startIndex, elementCount, data);
|
||||
|
||||
if (elementCount == 0)
|
||||
size = 0;
|
||||
|
||||
_appLayer.propertyValueReadResponse(AckRequested, priority, hopType, asap, objectIndex, propertyId, elementCount,
|
||||
startIndex, data, size);
|
||||
}
|
||||
|
@ -7,6 +7,7 @@
|
||||
#include "device_object.h"
|
||||
#include "address_table_object.h"
|
||||
#include "knx_ip_routing_indication.h"
|
||||
#include "knx_ip_search_request.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
@ -55,10 +56,20 @@ void IpDataLinkLayer::loop()
|
||||
switch ((KnxIpServiceType)code)
|
||||
{
|
||||
case RoutingIndication:
|
||||
{
|
||||
KnxIpRoutingIndication routingIndication(buffer, len);
|
||||
frameRecieved(routingIndication.frame());
|
||||
break;
|
||||
}
|
||||
case SearchRequest:
|
||||
{
|
||||
KnxIpSearchRequest searchRequest(buffer, len);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
print("Unhandled service identifier: ");
|
||||
println(code, HEX);
|
||||
}
|
||||
}
|
||||
|
||||
void IpDataLinkLayer::enabled(bool value)
|
||||
|
31
src/knx/ip_host_protocol_address_information.cpp
Normal file
31
src/knx/ip_host_protocol_address_information.cpp
Normal file
@ -0,0 +1,31 @@
|
||||
#include "ip_host_protocol_address_information.h"
|
||||
#include "bits.h"
|
||||
#ifdef USE_IP
|
||||
IpHostProtocolAddressInformation::IpHostProtocolAddressInformation(uint8_t* data)
|
||||
: _data(data)
|
||||
{}
|
||||
|
||||
|
||||
uint8_t IpHostProtocolAddressInformation::length()
|
||||
{
|
||||
return *_data;
|
||||
}
|
||||
|
||||
|
||||
HostProtocolCode IpHostProtocolAddressInformation::code()
|
||||
{
|
||||
return (HostProtocolCode)_data[1];
|
||||
}
|
||||
|
||||
|
||||
uint32_t IpHostProtocolAddressInformation::ipAddress()
|
||||
{
|
||||
return getInt(_data + 2);
|
||||
}
|
||||
|
||||
|
||||
uint16_t IpHostProtocolAddressInformation::ipPortNumber()
|
||||
{
|
||||
return getWord(_data + 6);
|
||||
}
|
||||
#endif
|
24
src/knx/ip_host_protocol_address_information.h
Normal file
24
src/knx/ip_host_protocol_address_information.h
Normal file
@ -0,0 +1,24 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include "config.h"
|
||||
|
||||
enum HostProtocolCode : uint8_t
|
||||
{
|
||||
IPV4_UDP = 1,
|
||||
IPV4_TCP = 2
|
||||
};
|
||||
|
||||
#ifdef USE_IP
|
||||
class IpHostProtocolAddressInformation
|
||||
{
|
||||
public:
|
||||
IpHostProtocolAddressInformation(uint8_t* data);
|
||||
uint8_t length();
|
||||
HostProtocolCode code();
|
||||
uint32_t ipAddress();
|
||||
uint16_t ipPortNumber();
|
||||
private:
|
||||
uint8_t* _data;
|
||||
};
|
||||
#endif
|
15
src/knx/knx_ip_dib.cpp
Normal file
15
src/knx/knx_ip_dib.cpp
Normal file
@ -0,0 +1,15 @@
|
||||
#include "knx_ip_dib.h"
|
||||
|
||||
DIB::DIB(uint8_t* data) : _data(data)
|
||||
{}
|
||||
|
||||
DescriptionTypeCode DIB::code()
|
||||
{
|
||||
return (DescriptionTypeCode)_data[1];
|
||||
}
|
||||
|
||||
|
||||
uint8_t DIB::length()
|
||||
{
|
||||
return *_data;
|
||||
}
|
27
src/knx/knx_ip_dib.h
Normal file
27
src/knx/knx_ip_dib.h
Normal file
@ -0,0 +1,27 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include "config.h"
|
||||
|
||||
#ifdef USE_IP
|
||||
|
||||
enum DescriptionTypeCode : uint8_t
|
||||
{
|
||||
DEVICE_INFO = 0x01,
|
||||
SUPP_SVC_FAMILIES = 0x02,
|
||||
IP_CONFIG = 0x03,
|
||||
IP_CUR_CONFIG = 0x04,
|
||||
KNX_ADDRESSES = 0x05,
|
||||
MFR_DATA = 0xFE
|
||||
};
|
||||
|
||||
class DIB
|
||||
{
|
||||
public:
|
||||
DIB(uint8_t* data);
|
||||
DescriptionTypeCode code();
|
||||
uint8_t length();
|
||||
private:
|
||||
uint8_t* _data = 0;
|
||||
};
|
||||
#endif
|
@ -10,6 +10,7 @@ KnxIpFrame::KnxIpFrame(uint8_t* data,
|
||||
uint16_t length)
|
||||
{
|
||||
_data = data;
|
||||
_dataLength = length;
|
||||
}
|
||||
|
||||
uint8_t KnxIpFrame::headerLength() const
|
||||
@ -62,13 +63,14 @@ uint8_t* KnxIpFrame::data()
|
||||
KnxIpFrame::~KnxIpFrame()
|
||||
{
|
||||
if (_freeData)
|
||||
delete _data;
|
||||
delete[] _data;
|
||||
}
|
||||
|
||||
|
||||
KnxIpFrame::KnxIpFrame(uint16_t length)
|
||||
{
|
||||
_data = new uint8_t[length];
|
||||
_dataLength = length;
|
||||
_freeData = true;
|
||||
headerLength(KNXIP_HEADER_LEN);
|
||||
protocolVersion(KnxIp1_0);
|
||||
|
@ -53,5 +53,6 @@ class KnxIpFrame
|
||||
protected:
|
||||
bool _freeData = false;
|
||||
uint8_t* _data = 0;
|
||||
uint16_t _dataLength;
|
||||
};
|
||||
#endif
|
13
src/knx/knx_ip_search_request.cpp
Normal file
13
src/knx/knx_ip_search_request.cpp
Normal file
@ -0,0 +1,13 @@
|
||||
#include "knx_ip_search_request.h"
|
||||
#ifdef USE_IP
|
||||
KnxIpSearchRequest::KnxIpSearchRequest(uint8_t* data, uint16_t length)
|
||||
: KnxIpFrame(data, length), _hpai(data + KNXIP_HEADER_LEN)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
IpHostProtocolAddressInformation& KnxIpSearchRequest::hpai()
|
||||
{
|
||||
return _hpai;
|
||||
}
|
||||
#endif
|
14
src/knx/knx_ip_search_request.h
Normal file
14
src/knx/knx_ip_search_request.h
Normal file
@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#include "knx_ip_frame.h"
|
||||
#include "ip_host_protocol_address_information.h"
|
||||
#ifdef USE_IP
|
||||
class KnxIpSearchRequest : public KnxIpFrame
|
||||
{
|
||||
public:
|
||||
KnxIpSearchRequest(uint8_t* data, uint16_t length);
|
||||
IpHostProtocolAddressInformation& hpai();
|
||||
private:
|
||||
IpHostProtocolAddressInformation _hpai;
|
||||
};
|
||||
#endif
|
4
src/knx/knx_ip_search_response.cpp
Normal file
4
src/knx/knx_ip_search_response.cpp
Normal file
@ -0,0 +1,4 @@
|
||||
#include "knx_ip_search_response.h"
|
||||
#ifdef USE_IP
|
||||
|
||||
#endif
|
13
src/knx/knx_ip_search_response.h
Normal file
13
src/knx/knx_ip_search_response.h
Normal file
@ -0,0 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
#include "knx_ip_frame.h"
|
||||
#include "ip_host_protocol_address_information.h"
|
||||
#ifdef USE_IP
|
||||
|
||||
class KnxIpSearchResponse : public KnxIpFrame
|
||||
{
|
||||
IpHostProtocolAddressInformation& controlEndpoint();
|
||||
|
||||
};
|
||||
|
||||
#endif
|
@ -87,3 +87,21 @@ int Platform::readBytesMultiCast(uint8_t *buffer, uint16_t maxLen)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
void Platform::setupUniCast(uint32_t addr, uint16_t port, uint8_t type)
|
||||
{}
|
||||
|
||||
|
||||
void Platform::closeUniCast()
|
||||
{}
|
||||
|
||||
|
||||
bool Platform::sendBytesUniCast(uint8_t* buffer, uint16_t len)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
int Platform::readBytesUniCast(uint8_t *buffer, uint16_t maxLen)
|
||||
{
|
||||
return 0;
|
||||
}
|
@ -30,6 +30,12 @@ class Platform
|
||||
virtual bool sendBytesMultiCast(uint8_t* buffer, uint16_t len);
|
||||
virtual int readBytesMultiCast(uint8_t* buffer, uint16_t maxLen);
|
||||
|
||||
//unicast socket
|
||||
virtual void setupUniCast(uint32_t addr, uint16_t port, uint8_t type);
|
||||
virtual void closeUniCast();
|
||||
virtual bool sendBytesUniCast(uint8_t* buffer, uint16_t len);
|
||||
virtual int readBytesUniCast(uint8_t* buffer, uint16_t maxLen);
|
||||
|
||||
//UART
|
||||
virtual void setupUart();
|
||||
virtual void closeUart();
|
||||
|
@ -15,6 +15,9 @@
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <net/if.h>
|
||||
#include <net/if_arp.h>
|
||||
#include <netdb.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
@ -31,18 +34,97 @@
|
||||
#include "knx/application_program_object.h"
|
||||
#include "knx/ip_parameter_object.h"
|
||||
#include "knx/bits.h"
|
||||
#include "knx/ip_host_protocol_address_information.h"
|
||||
|
||||
#define MAX_MEM 4096
|
||||
|
||||
LinuxPlatform::LinuxPlatform()
|
||||
{}
|
||||
{
|
||||
int socketMac = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
|
||||
if (socketMac < 0)
|
||||
{
|
||||
printf("Lookup socket creation failed");
|
||||
return;
|
||||
}
|
||||
|
||||
struct ifreq ifr;
|
||||
struct ifconf ifc;
|
||||
char buf[1024];
|
||||
|
||||
ifc.ifc_len = sizeof(buf);
|
||||
ifc.ifc_buf = buf;
|
||||
if (ioctl(socketMac, SIOCGIFCONF, &ifc) < 0)
|
||||
return;
|
||||
|
||||
struct ifreq* it = ifc.ifc_req;
|
||||
const struct ifreq* const end = it + (ifc.ifc_len / sizeof(struct ifreq));
|
||||
|
||||
for (; it != end; ++it)
|
||||
{
|
||||
strcpy(ifr.ifr_name, it->ifr_name);
|
||||
if (ioctl(socketMac, SIOCGIFFLAGS, &ifr))
|
||||
continue;
|
||||
|
||||
if (ifr.ifr_flags & IFF_LOOPBACK) // don't count loopback
|
||||
continue;
|
||||
|
||||
if (ioctl(socketMac, SIOCGIFHWADDR, &ifr))
|
||||
continue;
|
||||
|
||||
if (ifr.ifr_hwaddr.sa_family != ARPHRD_ETHER)
|
||||
continue;
|
||||
|
||||
memcpy(_macAddress, ifr.ifr_hwaddr.sa_data, IFHWADDRLEN);
|
||||
|
||||
ioctl(socketMac, SIOCGIFADDR, &ifr);
|
||||
|
||||
struct sockaddr_in* ipaddr = (struct sockaddr_in*)&ifr.ifr_addr;
|
||||
_ipAddress = ntohl(ipaddr->sin_addr.s_addr);
|
||||
|
||||
//printf("IP address: %s\n", inet_ntoa(ipaddr->sin_addr));
|
||||
ioctl(socketMac, SIOCGIFNETMASK, &ifr);
|
||||
struct sockaddr_in* netmask = (struct sockaddr_in*)&ifr.ifr_netmask;
|
||||
_netmask = ntohl(netmask->sin_addr.s_addr);
|
||||
//printf("Netmask: %s\n", inet_ntoa(ipaddr->sin_addr));
|
||||
break;
|
||||
}
|
||||
close(socketMac);
|
||||
|
||||
// default GW
|
||||
FILE* f;
|
||||
char line[100], *p, *c, *g, *saveptr;
|
||||
|
||||
f = fopen("/proc/net/route", "r");
|
||||
|
||||
while (fgets(line, 100, f))
|
||||
{
|
||||
p = strtok_r(line, " \t", &saveptr);
|
||||
c = strtok_r(NULL, " \t", &saveptr);
|
||||
g = strtok_r(NULL, " \t", &saveptr);
|
||||
|
||||
if (p != NULL && c != NULL)
|
||||
{
|
||||
if (strcmp(c, "00000000") == 0)
|
||||
{
|
||||
//printf("Default interface is : %s \n" , p);
|
||||
if (g)
|
||||
{
|
||||
char* pEnd;
|
||||
_defaultGateway = ntohl(strtol(g, &pEnd, 16));
|
||||
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
fclose(f);
|
||||
}
|
||||
|
||||
LinuxPlatform::~LinuxPlatform()
|
||||
{
|
||||
delete[] _args;
|
||||
}
|
||||
|
||||
|
||||
uint32_t millis()
|
||||
{
|
||||
struct timespec spec;
|
||||
@ -59,8 +141,6 @@ void delay(uint32_t millis)
|
||||
nanosleep(&ts, NULL);
|
||||
}
|
||||
|
||||
|
||||
|
||||
void LinuxPlatform::restart()
|
||||
{
|
||||
execv(_args[0], _args);
|
||||
@ -75,8 +155,11 @@ void LinuxPlatform::fatalError()
|
||||
|
||||
void LinuxPlatform::setupMultiCast(uint32_t addr, uint16_t port)
|
||||
{
|
||||
if (_multicastSocketFd >= 0)
|
||||
closeMultiCast();
|
||||
|
||||
_multicastAddr = addr;
|
||||
_port = port;
|
||||
_multicastPort = port;
|
||||
|
||||
struct ip_mreq command;
|
||||
uint32_t loop = 1;
|
||||
@ -87,21 +170,22 @@ void LinuxPlatform::setupMultiCast(uint32_t addr, uint16_t port)
|
||||
sin.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||
sin.sin_port = htons(port);
|
||||
|
||||
_socketFd = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
if (_socketFd == -1) {
|
||||
_multicastSocketFd = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
if (_multicastSocketFd == -1)
|
||||
{
|
||||
perror("socket()");
|
||||
fatalError();
|
||||
}
|
||||
|
||||
/* Mehr Prozessen erlauben, denselben Port zu nutzen */
|
||||
loop = 1;
|
||||
if (setsockopt(_socketFd, SOL_SOCKET, SO_REUSEADDR, &loop, sizeof(loop)) < 0)
|
||||
if (setsockopt(_multicastSocketFd, SOL_SOCKET, SO_REUSEADDR, &loop, sizeof(loop)) < 0)
|
||||
{
|
||||
perror("setsockopt:SO_REUSEADDR");
|
||||
fatalError();
|
||||
}
|
||||
|
||||
if (bind(_socketFd, (struct sockaddr *)&sin, sizeof(sin)) < 0)
|
||||
if (bind(_multicastSocketFd, (struct sockaddr*)&sin, sizeof(sin)) < 0)
|
||||
{
|
||||
perror("bind");
|
||||
fatalError();
|
||||
@ -109,7 +193,7 @@ void LinuxPlatform::setupMultiCast(uint32_t addr, uint16_t port)
|
||||
|
||||
/* loopback */
|
||||
loop = 0;
|
||||
if (setsockopt(_socketFd, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop)) < 0)
|
||||
if (setsockopt(_multicastSocketFd, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop)) < 0)
|
||||
{
|
||||
perror("setsockopt:IP_MULTICAST_LOOP");
|
||||
fatalError();
|
||||
@ -119,15 +203,15 @@ void LinuxPlatform::setupMultiCast(uint32_t addr, uint16_t port)
|
||||
command.imr_multiaddr.s_addr = htonl(addr);
|
||||
command.imr_interface.s_addr = htonl(INADDR_ANY);
|
||||
|
||||
if (setsockopt(_socketFd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &command, sizeof(command)) < 0)
|
||||
if (setsockopt(_multicastSocketFd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &command, sizeof(command)) < 0)
|
||||
{
|
||||
perror("setsockopt:IP_ADD_MEMBERSHIP");
|
||||
fatalError();
|
||||
}
|
||||
|
||||
uint32_t flags = fcntl(_socketFd, F_GETFL);
|
||||
uint32_t flags = fcntl(_multicastSocketFd, F_GETFL);
|
||||
flags |= O_NONBLOCK;
|
||||
fcntl(_socketFd, F_SETFL, flags);
|
||||
fcntl(_multicastSocketFd, F_SETFL, flags);
|
||||
}
|
||||
|
||||
void LinuxPlatform::closeMultiCast()
|
||||
@ -136,13 +220,15 @@ void LinuxPlatform::closeMultiCast()
|
||||
command.imr_multiaddr.s_addr = htonl(_multicastAddr);
|
||||
command.imr_interface.s_addr = htonl(INADDR_ANY);
|
||||
|
||||
if (setsockopt(_socketFd,
|
||||
if (setsockopt(_multicastSocketFd,
|
||||
IPPROTO_IP,
|
||||
IP_DROP_MEMBERSHIP,
|
||||
&command, sizeof(command)) < 0) {
|
||||
&command, sizeof(command)) < 0)
|
||||
{
|
||||
perror("setsockopt:IP_DROP_MEMBERSHIP");
|
||||
}
|
||||
close(_socketFd);
|
||||
close(_multicastSocketFd);
|
||||
_multicastSocketFd = -1;
|
||||
}
|
||||
|
||||
bool LinuxPlatform::sendBytesMultiCast(uint8_t* buffer, uint16_t len)
|
||||
@ -150,12 +236,12 @@ bool LinuxPlatform::sendBytesMultiCast(uint8_t* buffer, uint16_t len)
|
||||
struct sockaddr_in address = {0};
|
||||
address.sin_family = AF_INET;
|
||||
address.sin_addr.s_addr = htonl(_multicastAddr);
|
||||
address.sin_port = htons(_port);
|
||||
address.sin_port = htons(_multicastPort);
|
||||
|
||||
ssize_t retVal = 0;
|
||||
do
|
||||
{
|
||||
retVal = sendto(_socketFd, buffer, len, 0, (struct sockaddr *) &address, sizeof(address));
|
||||
retVal = sendto(_multicastSocketFd, buffer, len, 0, (struct sockaddr*)&address, sizeof(address));
|
||||
if (retVal == -1)
|
||||
{
|
||||
if (errno != EAGAIN && errno != EWOULDBLOCK)
|
||||
@ -172,7 +258,7 @@ int LinuxPlatform::readBytesMultiCast(uint8_t * buffer, uint16_t maxLen)
|
||||
struct sockaddr_in sin;
|
||||
|
||||
sin_len = sizeof(sin);
|
||||
ssize_t len = recvfrom(_socketFd, buffer, maxLen, 0, (struct sockaddr *) &sin, &sin_len);
|
||||
ssize_t len = recvfrom(_multicastSocketFd, buffer, maxLen, 0, (struct sockaddr*)&sin, &sin_len);
|
||||
// if (len > 0)
|
||||
// printHex("->", buffer, len);
|
||||
|
||||
@ -303,8 +389,6 @@ void LinuxPlatform::setupSpi()
|
||||
}
|
||||
|
||||
printf("SPI device setup ok.\r\n");
|
||||
|
||||
|
||||
}
|
||||
|
||||
void LinuxPlatform::flashFilePath(const std::string path)
|
||||
@ -312,7 +396,6 @@ void LinuxPlatform::flashFilePath(const std::string path)
|
||||
_flashFilePath = path;
|
||||
}
|
||||
|
||||
|
||||
std::string LinuxPlatform::flashFilePath()
|
||||
{
|
||||
return _flashFilePath;
|
||||
@ -523,10 +606,70 @@ void LinuxPlatform::cmdLineArgs(int argc, char** argv)
|
||||
|
||||
static int gpioFds[MAX_NUM_GPIO] =
|
||||
{
|
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
-1,
|
||||
};
|
||||
|
||||
/* Activate GPIO-Pin
|
||||
@ -622,9 +765,15 @@ int gpio_direction(int pin, int dir)
|
||||
|
||||
switch (dir)
|
||||
{
|
||||
case INPUT : res = write(fd,"in",2); break;
|
||||
case OUTPUT: res = write(fd,"out",3); break;
|
||||
default: res = -1; break;
|
||||
case INPUT:
|
||||
res = write(fd, "in", 2);
|
||||
break;
|
||||
case OUTPUT:
|
||||
res = write(fd, "out", 3);
|
||||
break;
|
||||
default:
|
||||
res = -1;
|
||||
break;
|
||||
}
|
||||
|
||||
if (res < 0)
|
||||
@ -684,9 +833,15 @@ int gpio_write(int pin, int value)
|
||||
|
||||
switch (value)
|
||||
{
|
||||
case LOW : res = write(gpioFds[pin], "0\n", 2); break;
|
||||
case HIGH: res = write(gpioFds[pin], "1\n", 2); break;
|
||||
default: res = -1; break;
|
||||
case LOW:
|
||||
res = write(gpioFds[pin], "0\n", 2);
|
||||
break;
|
||||
case HIGH:
|
||||
res = write(gpioFds[pin], "1\n", 2);
|
||||
break;
|
||||
default:
|
||||
res = -1;
|
||||
break;
|
||||
}
|
||||
|
||||
if (res < 0)
|
||||
@ -719,11 +874,21 @@ int gpio_edge(unsigned int pin, char edge)
|
||||
|
||||
switch (edge)
|
||||
{
|
||||
case 'r': strncpy(path,"rising",8); break;
|
||||
case 'f': strncpy(path,"falling",8); break;
|
||||
case 'b': strncpy(path,"both",8); break;
|
||||
case 'n': strncpy(path,"none",8); break;
|
||||
default: close(fd);return(-2);
|
||||
case 'r':
|
||||
strncpy(path, "rising", 8);
|
||||
break;
|
||||
case 'f':
|
||||
strncpy(path, "falling", 8);
|
||||
break;
|
||||
case 'b':
|
||||
strncpy(path, "both", 8);
|
||||
break;
|
||||
case 'n':
|
||||
strncpy(path, "none", 8);
|
||||
break;
|
||||
default:
|
||||
close(fd);
|
||||
return (-2);
|
||||
}
|
||||
|
||||
write(fd, path, strlen(path) + 1);
|
||||
@ -828,3 +993,116 @@ void delayMicroseconds (unsigned int howLong)
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
void LinuxPlatform::setupUniCast(uint32_t addr, uint16_t port, uint8_t type)
|
||||
{
|
||||
if (_unicastSocketFd >= 0)
|
||||
closeUniCast();
|
||||
|
||||
_unicastAddr = addr;
|
||||
_unicastPort = port;
|
||||
_unicastType = type;
|
||||
|
||||
uint32_t loop = 1;
|
||||
|
||||
struct sockaddr_in sin;
|
||||
memset(&sin, 0, sizeof(sin));
|
||||
sin.sin_family = AF_INET;
|
||||
sin.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||
sin.sin_port = htons(port);
|
||||
|
||||
int socketType = 0;
|
||||
if (type == IPV4_UDP)
|
||||
socketType = SOCK_DGRAM;
|
||||
else
|
||||
socketType = SOCK_STREAM;
|
||||
|
||||
_unicastSocketFd = socket(AF_INET, socketType, 0);
|
||||
if (_unicastSocketFd == -1)
|
||||
{
|
||||
perror("socket()");
|
||||
fatalError();
|
||||
}
|
||||
|
||||
/* Mehr Prozessen erlauben, denselben Port zu nutzen */
|
||||
loop = 1;
|
||||
if (setsockopt(_unicastSocketFd, SOL_SOCKET, SO_REUSEADDR, &loop, sizeof(loop)) < 0)
|
||||
{
|
||||
perror("setsockopt:SO_REUSEADDR");
|
||||
fatalError();
|
||||
}
|
||||
|
||||
if (bind(_unicastSocketFd, (struct sockaddr*)&sin, sizeof(sin)) < 0)
|
||||
{
|
||||
perror("bind");
|
||||
fatalError();
|
||||
}
|
||||
|
||||
uint32_t flags = fcntl(_unicastSocketFd, F_GETFL);
|
||||
flags |= O_NONBLOCK;
|
||||
fcntl(_unicastSocketFd, F_SETFL, flags);
|
||||
}
|
||||
|
||||
void LinuxPlatform::closeUniCast()
|
||||
{
|
||||
close(_unicastSocketFd);
|
||||
_unicastSocketFd = -1;
|
||||
}
|
||||
|
||||
bool LinuxPlatform::sendBytesUniCast(uint8_t* buffer, uint16_t len)
|
||||
{
|
||||
struct sockaddr_in address = {0};
|
||||
address.sin_family = AF_INET;
|
||||
address.sin_addr.s_addr = htonl(_unicastAddr);
|
||||
address.sin_port = htons(_unicastPort);
|
||||
|
||||
ssize_t retVal = 0;
|
||||
do
|
||||
{
|
||||
retVal = sendto(_unicastSocketFd, buffer, len, 0, (struct sockaddr*)&address, sizeof(address));
|
||||
if (retVal == -1)
|
||||
{
|
||||
if (errno != EAGAIN && errno != EWOULDBLOCK)
|
||||
return false;
|
||||
}
|
||||
} while (retVal == -1);
|
||||
// printHex("<-", buffer, len);
|
||||
return true;
|
||||
}
|
||||
|
||||
int LinuxPlatform::readBytesUniCast(uint8_t* buffer,
|
||||
uint16_t maxLen)
|
||||
{
|
||||
uint32_t sin_len;
|
||||
struct sockaddr_in sin;
|
||||
|
||||
sin_len = sizeof(sin);
|
||||
ssize_t len = recvfrom(_unicastSocketFd, buffer, maxLen, 0, (struct sockaddr*)&sin, &sin_len);
|
||||
// if (len > 0)
|
||||
// printHex("->", buffer, len);
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
void LinuxPlatform::macAddress(uint8_t* mac_address)
|
||||
{
|
||||
memcpy(mac_address, _macAddress, IFHWADDRLEN);
|
||||
}
|
||||
|
||||
|
||||
uint32_t LinuxPlatform::currentIpAddress()
|
||||
{
|
||||
return _ipAddress;
|
||||
}
|
||||
|
||||
|
||||
uint32_t LinuxPlatform::currentSubnetMask()
|
||||
{
|
||||
return _netmask;
|
||||
}
|
||||
|
||||
|
||||
uint32_t LinuxPlatform::currentDefaultGateway()
|
||||
{
|
||||
return _defaultGateway;
|
||||
}
|
||||
|
@ -26,12 +26,24 @@ public:
|
||||
void restart() override;
|
||||
void fatalError() override;
|
||||
|
||||
// ip config
|
||||
uint32_t currentIpAddress() override;
|
||||
uint32_t currentSubnetMask() override;
|
||||
uint32_t currentDefaultGateway() override;
|
||||
void macAddress(uint8_t* data) override;
|
||||
|
||||
|
||||
//multicast
|
||||
void setupMultiCast(uint32_t addr, uint16_t port) override;
|
||||
void closeMultiCast() override;
|
||||
bool sendBytesMultiCast(uint8_t* buffer, uint16_t len) override;
|
||||
int readBytesMultiCast(uint8_t* buffer, uint16_t maxLen) override;
|
||||
|
||||
void setupUniCast(uint32_t addr, uint16_t port, uint8_t type);
|
||||
void closeUniCast();
|
||||
bool sendBytesUniCast(uint8_t* buffer, uint16_t len);
|
||||
int readBytesUniCast(uint8_t* buffer, uint16_t maxLen);
|
||||
|
||||
//spi
|
||||
void setupSpi() override;
|
||||
void closeSpi() override;
|
||||
@ -44,15 +56,25 @@ public:
|
||||
|
||||
private:
|
||||
uint32_t _multicastAddr = -1;
|
||||
uint16_t _port = -1;
|
||||
int _socketFd = -1;
|
||||
uint16_t _multicastPort = -1;
|
||||
int _multicastSocketFd = -1;
|
||||
|
||||
uint32_t _unicastAddr = -1;
|
||||
uint16_t _unicastPort = -1;
|
||||
int _unicastSocketFd = -1;
|
||||
uint8_t _unicastType = -1;
|
||||
|
||||
void doMemoryMapping();
|
||||
uint8_t* _mappedFile = 0;
|
||||
int _fd = -1;
|
||||
int _spiFd = -1;
|
||||
uint8_t* _currentMaxMem = 0;
|
||||
std::string _flashFilePath = "flash.bin";
|
||||
char** _args = 0;
|
||||
|
||||
uint8_t _macAddress[6] = {0, 0, 0, 0, 0, 0};
|
||||
uint32_t _ipAddress = 0;
|
||||
uint32_t _netmask = 0;
|
||||
uint32_t _defaultGateway = 0;
|
||||
};
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user