Run clang-format and enable CI check for it (#755)

* pre-commit: Update and enable clang-format check

There is newer version from clang-format so use that. We do not yet want
18 as that is little bit too new.

* Format some thing by hand which clang-format "breaks"

Clang-format will format some things little bit off in some cases.
Format some things by hand so we get cleaner end result.

* Run clang-format with

```
pre-commit run --all-files clang-format
```

We have already in previously checked places where clang-format does not
make good format and ignored those (hopefully most of the things).

---------

Co-authored-by: Kari Argillander <kari.argillander@fidelix.com>
This commit is contained in:
Kari Argillander
2024-08-30 19:20:58 +03:00
committed by GitHub
parent 622a9e609e
commit f806c5829b
547 changed files with 18286 additions and 16575 deletions
+54 -30
View File
@@ -62,8 +62,9 @@ bool arcnet_valid(void)
void arcnet_cleanup(void)
{
if (arcnet_valid())
if (arcnet_valid()) {
close(ARCNET_Sock_FD);
}
ARCNET_Sock_FD = -1;
return;
@@ -79,7 +80,8 @@ static int arcnet_bind(const char *interface_name)
/* check to see if we are being run as root */
uid = getuid();
if (uid != 0) {
fprintf(stderr,
fprintf(
stderr,
"arcnet: Unable to open an af_packet socket. "
"Try running with root priveleges.\n");
return sock_fd;
@@ -92,7 +94,8 @@ static int arcnet_bind(const char *interface_name)
if ((sock_fd = socket(PF_PACKET, SOCK_PACKET, htons(ETH_P_ALL))) < 0) {
/* Error occured */
fprintf(stderr, "arcnet: Error opening socket: %s\n", strerror(errno));
fprintf(stderr,
fprintf(
stderr,
"You might need to add the following to modules.conf\n"
"(or in /etc/modutils/alias on Debian with update-modules):\n"
"alias net-pf-17 af_packet\n"
@@ -108,19 +111,24 @@ static int arcnet_bind(const char *interface_name)
ARCNET_Socket_Address.sa_family = ARPHRD_ARCNET;
/*ARCNET_Socket_Address.sa_family = PF_INET; */
/* Clear the memory before copying */
memset(ARCNET_Socket_Address.sa_data, '\0',
memset(
ARCNET_Socket_Address.sa_data, '\0',
sizeof(ARCNET_Socket_Address.sa_data));
/* Strcpy the interface name into the address */
strncpy(ARCNET_Socket_Address.sa_data, interface_name,
strncpy(
ARCNET_Socket_Address.sa_data, interface_name,
sizeof(ARCNET_Socket_Address.sa_data) - 1);
fprintf(
stderr, "arcnet: binding \"%s\"\n", ARCNET_Socket_Address.sa_data);
if (bind(sock_fd, &ARCNET_Socket_Address,
if (bind(
sock_fd, &ARCNET_Socket_Address,
sizeof(ARCNET_Socket_Address)) != 0) {
/* Bind problem, close socket and return */
fprintf(stderr, "arcnet: Unable to bind socket : %s\n",
fprintf(
stderr, "arcnet: Unable to bind socket : %s\n",
strerror(errno));
fprintf(stderr,
fprintf(
stderr,
"You might need to add the following to modules.conf\n"
"(or in /etc/modutils/alias on Debian with update-modules):\n"
"alias net-pf-17 af_packet\n"
@@ -134,18 +142,22 @@ static int arcnet_bind(const char *interface_name)
}
strncpy(ifr.ifr_name, interface_name, sizeof(ifr.ifr_name));
rv = ioctl(sock_fd, SIOCGIFHWADDR, &ifr);
if (rv != -1) /* worked okay */
if (rv != -1) { /* worked okay */
ARCNET_MAC_Address = ifr.ifr_hwaddr.sa_data[0];
}
/* copy this info into the local copy since bind wiped it out */
ARCNET_Socket_Address.sa_family = ARPHRD_ARCNET;
/*ARCNET_Socket_Address.sa_family = PF_INET; */
/* Clear the memory before copying */
memset(ARCNET_Socket_Address.sa_data, '\0',
memset(
ARCNET_Socket_Address.sa_data, '\0',
sizeof(ARCNET_Socket_Address.sa_data));
/* Strcpy the interface name into the address */
strncpy(ARCNET_Socket_Address.sa_data, interface_name,
strncpy(
ARCNET_Socket_Address.sa_data, interface_name,
sizeof(ARCNET_Socket_Address.sa_data) - 1);
fprintf(stderr, "arcnet: MAC=%02Xh iface=\"%s\"\n", ARCNET_MAC_Address,
fprintf(
stderr, "arcnet: MAC=%02Xh iface=\"%s\"\n", ARCNET_MAC_Address,
ARCNET_Socket_Address.sa_data);
atexit(arcnet_cleanup);
@@ -155,17 +167,19 @@ static int arcnet_bind(const char *interface_name)
bool arcnet_init(char *interface_name)
{
if (interface_name)
if (interface_name) {
ARCNET_Sock_FD = arcnet_bind(interface_name);
else
} else {
ARCNET_Sock_FD = arcnet_bind("arc0");
}
return arcnet_valid();
}
/* function to send a PDU out the socket */
/* returns number of bytes sent on success, negative on failure */
int arcnet_send_pdu(BACNET_ADDRESS *dest, /* destination address */
int arcnet_send_pdu(
BACNET_ADDRESS *dest, /* destination address */
BACNET_NPDU_DATA *npdu_data, /* network information */
uint8_t *pdu, /* any data to be sent - may be null */
unsigned pdu_len)
@@ -186,15 +200,15 @@ int arcnet_send_pdu(BACNET_ADDRESS *dest, /* destination address */
return -1;
}
/* load destination MAC address */
if (dest->mac_len == 1)
if (dest->mac_len == 1) {
pkt->hard.dest = dest->mac[0];
else {
} else {
fprintf(stderr, "arcnet: invalid destination MAC address!\n");
return -2;
}
if (src.mac_len == 1)
if (src.mac_len == 1) {
pkt->hard.source = src.mac[0];
else {
} else {
fprintf(stderr, "arcnet: invalid source MAC address!\n");
return -3;
}
@@ -211,19 +225,22 @@ int arcnet_send_pdu(BACNET_ADDRESS *dest, /* destination address */
}
memcpy(&pkt->soft.raw[4], pdu, pdu_len);
/* Send the packet */
bytes = sendto(ARCNET_Sock_FD, &mtu, mtu_len, 0,
bytes = sendto(
ARCNET_Sock_FD, &mtu, mtu_len, 0,
(struct sockaddr *)&ARCNET_Socket_Address,
sizeof(ARCNET_Socket_Address));
/* did it get sent? */
if (bytes < 0)
if (bytes < 0) {
fprintf(stderr, "arcnet: Error sending packet: %s\n", strerror(errno));
}
return bytes;
}
/* receives an framed packet */
/* returns the number of octets in the PDU, or zero on failure */
uint16_t arcnet_receive(BACNET_ADDRESS *src, /* source address */
uint16_t arcnet_receive(
BACNET_ADDRESS *src, /* source address */
uint8_t *pdu, /* PDU data */
uint16_t max_pdu, /* amount of space available in the PDU */
unsigned timeout)
@@ -237,8 +254,9 @@ uint16_t arcnet_receive(BACNET_ADDRESS *src, /* source address */
struct archdr *pkt = (struct archdr *)buf;
/* Make sure the socket is open */
if (ARCNET_Sock_FD <= 0)
if (ARCNET_Sock_FD <= 0) {
return 0;
}
/* we could just use a non-blocking socket, but that consumes all
the CPU time. We can use a timeout; it is only supported as
@@ -255,24 +273,28 @@ uint16_t arcnet_receive(BACNET_ADDRESS *src, /* source address */
FD_SET(ARCNET_Sock_FD, &read_fds);
max = ARCNET_Sock_FD;
if (select(max + 1, &read_fds, NULL, NULL, &select_timeout) > 0)
if (select(max + 1, &read_fds, NULL, NULL, &select_timeout) > 0) {
received_bytes = read(ARCNET_Sock_FD, &buf[0], sizeof(buf));
else
} else {
return 0;
}
/* See if there is a problem */
if (received_bytes < 0) {
/* EAGAIN Non-blocking I/O has been selected */
/* using O_NONBLOCK and no data */
/* was immediately available for reading. */
if (errno != EAGAIN)
fprintf(stderr, "ethernet: Read error in receiving packet: %s\n",
if (errno != EAGAIN) {
fprintf(
stderr, "ethernet: Read error in receiving packet: %s\n",
strerror(errno));
}
return 0;
}
if (received_bytes == 0)
if (received_bytes == 0) {
return 0;
}
/* printf("arcnet: received %u bytes (offset=%02Xh %02Xh) "
"from %02Xh (proto==%02Xh)\n",
@@ -312,11 +334,13 @@ uint16_t arcnet_receive(BACNET_ADDRESS *src, /* source address */
pdu_len = received_bytes - ARC_HDR_SIZE;
pdu_len -= 4 /* SC, DSAP, SSAP, LLC Control */;
/* copy the buffer into the PDU */
if (pdu_len < max_pdu)
if (pdu_len < max_pdu) {
memmove(&pdu[0], &pkt->soft.raw[4], pdu_len);
}
/* silently ignore packets that are too large */
else
else {
pdu_len = 0;
}
return pdu_len;
}
+43 -47
View File
@@ -1,56 +1,56 @@
/**************************************************************************
*
* Copyright (C) 2005 Steve Karg <skarg@users.sourceforge.net>
*
* SPDX-License-Identifier: MIT
*
*********************************************************************/
*
* Copyright (C) 2005 Steve Karg <skarg@users.sourceforge.net>
*
* SPDX-License-Identifier: MIT
*
*********************************************************************/
#ifndef BACPORT_H
#define BACPORT_H
/* common unix sockets headers needed */
#include <sys/types.h> /* basic system data types */
#include <sys/time.h> /* timeval{} for select() */
#include <time.h> /* timespec{} for pselect() */
#include <netinet/in.h> /* sockaddr_in{} and other Internet defns */
#include <arpa/inet.h> /* inet(3) functions */
#include <fcntl.h> /* for nonblocking */
#include <netdb.h>
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h> /* for S_xxx file mode constants */
#include <sys/uio.h> /* for iovec{} and readv/writev */
#include <unistd.h>
#include <sys/wait.h>
#include <sys/un.h> /* for Unix domain sockets */
#include <sys/types.h> /* basic system data types */
#include <sys/time.h> /* timeval{} for select() */
#include <time.h> /* timespec{} for pselect() */
#include <netinet/in.h> /* sockaddr_in{} and other Internet defns */
#include <arpa/inet.h> /* inet(3) functions */
#include <fcntl.h> /* for nonblocking */
#include <netdb.h>
#include <errno.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h> /* for S_xxx file mode constants */
#include <sys/uio.h> /* for iovec{} and readv/writev */
#include <unistd.h>
#include <sys/wait.h>
#include <sys/un.h> /* for Unix domain sockets */
#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h> /* for convenience */
#ifdef HAVE_SYS_SELECT_H
#include <sys/select.h> /* for convenience */
#endif
#ifdef HAVE_POLL_H
#include <poll.h> /* for convenience */
#ifdef HAVE_POLL_H
#include <poll.h> /* for convenience */
#endif
#ifdef HAVE_STRINGS_H
#include <strings.h> /* for convenience */
#ifdef HAVE_STRINGS_H
#include <strings.h> /* for convenience */
#endif
/* Three headers are normally needed for socket/file ioctl's:
* <sys/ioctl.h>, <sys/filio.h>, and <sys/sockio.h>.
*/
#ifdef HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
#ifdef HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
#endif
#ifdef HAVE_SYS_FILIO_H
#include <sys/filio.h>
#ifdef HAVE_SYS_FILIO_H
#include <sys/filio.h>
#endif
#ifdef HAVE_SYS_SOCKIO_H
#include <sys/sockio.h>
#ifdef HAVE_SYS_SOCKIO_H
#include <sys/sockio.h>
#endif
#include <pthread.h>
@@ -65,10 +65,10 @@
#ifndef __CYGWIN__
#include <net/if_arp.h>
#endif
#include <features.h> /* for the glibc version number */
#include <features.h> /* for the glibc version number */
#if __GLIBC__ >= 2 && __GLIBC_MINOR >= 1
#include <netpacket/packet.h>
#include <net/ethernet.h> /* the L2 protocols */
#include <net/ethernet.h> /* the L2 protocols */
#else
#include <asm/types.h>
#ifndef __CYGWIN__
@@ -84,24 +84,20 @@
#include <netdb.h>
#include "bacnet/basic/sys/bacnet_stack_exports.h"
#define BACNET_OBJECT_TABLE(table_name, _type, _init, _count, \
_index_to_instance, _valid_instance, _object_name, \
_read_property, _write_property, _RPM_list, \
_RR_info, _iterator, _value_list, _COV, \
_COV_clear, _intrinsic_reporting) \
#define BACNET_OBJECT_TABLE( \
table_name, _type, _init, _count, _index_to_instance, _valid_instance, \
_object_name, _read_property, _write_property, _RPM_list, _RR_info, \
_iterator, _value_list, _COV, _COV_clear, _intrinsic_reporting) \
static_assert(false, "Unsupported BACNET_OBJECT_TABLE for this platform")
/** @file linux/bacport.h Includes Linux network headers. */
/* Local helper functions for this port */
BACNET_STACK_EXPORT
extern int bip_get_local_netmask(
struct in_addr *netmask);
extern int bip_get_local_netmask(struct in_addr *netmask);
BACNET_STACK_EXPORT
extern int bip_get_local_address_ioctl(
const char *ifname,
struct in_addr *addr,
int request);
const char *ifname, struct in_addr *addr, int request);
#endif
+40 -26
View File
@@ -60,13 +60,15 @@ static char BIP_Interface_Name[IF_NAMESIZE] = { 0 };
* @param str - debug info string
* @param addr - IPv4 address
*/
static void debug_print_ipv4(const char *str,
static void debug_print_ipv4(
const char *str,
const struct in_addr *addr,
const unsigned int port,
const unsigned int count)
{
if (BIP_Debug) {
fprintf(stderr, "BIP: %s %s:%hu (%u bytes)\n", str, inet_ntoa(*addr),
fprintf(
stderr, "BIP: %s %s:%hu (%u bytes)\n", str, inet_ntoa(*addr),
ntohs(port), count);
fflush(stderr);
}
@@ -293,8 +295,9 @@ int bip_send_mpdu(
/* Send the packet */
debug_print_ipv4(
"Sending MPDU->", &bip_dest.sin_addr, bip_dest.sin_port, mtu_len);
return sendto(BIP_Socket, (const char *)mtu, mtu_len, 0,
(struct sockaddr *)&bip_dest, sizeof(struct sockaddr));
return sendto(
BIP_Socket, (const char *)mtu, mtu_len, 0, (struct sockaddr *)&bip_dest,
sizeof(struct sockaddr));
}
/**
@@ -345,10 +348,11 @@ uint16_t bip_receive(
/* see if there is a packet for us */
if (select(max + 1, &read_fds, NULL, NULL, &select_timeout) > 0) {
socket = FD_ISSET(BIP_Socket, &read_fds) ? BIP_Socket :
BIP_Broadcast_Socket;
received_bytes = recvfrom(socket, (char *)&npdu[0], max_npdu, 0,
(struct sockaddr *)&sin, &sin_len);
socket =
FD_ISSET(BIP_Socket, &read_fds) ? BIP_Socket : BIP_Broadcast_Socket;
received_bytes = recvfrom(
socket, (char *)&npdu[0], max_npdu, 0, (struct sockaddr *)&sin,
&sin_len);
} else {
return 0;
}
@@ -423,7 +427,8 @@ uint16_t bip_receive(
* @return Upon successful completion, returns the number of bytes sent.
* Otherwise, -1 shall be returned and errno set to indicate the error.
*/
int bip_send_pdu(BACNET_ADDRESS *dest,
int bip_send_pdu(
BACNET_ADDRESS *dest,
BACNET_NPDU_DATA *npdu_data,
uint8_t *pdu,
unsigned pdu_len)
@@ -466,8 +471,8 @@ bool bip_get_addr_by_name(const char *host_name, BACNET_IP_ADDRESS *addr)
* @param request - the ioctl() request
* @return 0 on success, else the error from the ioctl() call.
*/
static int get_local_ifr_ioctl(
const char *ifname, struct ifreq *ifr, int request)
static int
get_local_ifr_ioctl(const char *ifname, struct ifreq *ifr, int request)
{
int fd;
int rv; /* return value */
@@ -526,8 +531,8 @@ struct route_info {
* @param pId - process identifier of our specific request response
* @return number of bytes placed into the buffer
*/
static int readNlSock(
int sockFd, char *bufPtr, size_t buf_size, int seqNum, int pId)
static int
readNlSock(int sockFd, char *bufPtr, size_t buf_size, int seqNum, int pId)
{
struct nlmsghdr *nlHdr;
int readLen = 0, msgLen = 0;
@@ -574,7 +579,8 @@ static char *ntoa(uint32_t addr)
{
static char buffer[18];
snprintf(buffer, sizeof(buffer), "%d.%d.%d.%d", (addr & 0x000000FF),
snprintf(
buffer, sizeof(buffer), "%d.%d.%d.%d", (addr & 0x000000FF),
(addr & 0x0000FF00) >> 8, (addr & 0x00FF0000) >> 16,
(addr & 0xFF000000) >> 24);
@@ -589,18 +595,21 @@ static void printRoute(struct route_info *rtInfo)
{
if (BIP_Debug) {
/* Print Destination address */
fprintf(stderr, "%s\t",
fprintf(
stderr, "%s\t",
rtInfo->dstAddr ? ntoa(rtInfo->dstAddr) : "0.0.0.0 ");
/* Print Gateway address */
fprintf(stderr, "%s\t",
fprintf(
stderr, "%s\t",
rtInfo->gateWay ? ntoa(rtInfo->gateWay) : "*.*.*.*");
/* Print Interface Name */
fprintf(stderr, "%s\t", rtInfo->ifName);
/* Print Source address */
fprintf(stderr, "%s\n",
fprintf(
stderr, "%s\n",
rtInfo->srcAddr ? ntoa(rtInfo->srcAddr) : "*.*.*.*");
}
}
@@ -620,8 +629,9 @@ static void parseRoutes(struct nlmsghdr *nlHdr, struct route_info *rtInfo)
/* If the route is not for AF_INET or does not belong to main routing table
then return. */
if ((rtMsg->rtm_family != AF_INET) || (rtMsg->rtm_table != RT_TABLE_MAIN))
if ((rtMsg->rtm_family != AF_INET) || (rtMsg->rtm_table != RT_TABLE_MAIN)) {
return;
}
/* get the rtattr field */
rtAttr = (struct rtattr *)RTM_RTA(rtMsg);
@@ -700,7 +710,8 @@ static char *ifname_default(void)
if (BIP_Interface_Name[0] == 0) {
if ((rtInfo->dstAddr == 0) && (rtInfo->ifName[0] != 0)) {
/* default route */
memcpy(BIP_Interface_Name, rtInfo->ifName,
memcpy(
BIP_Interface_Name, rtInfo->ifName,
sizeof(BIP_Interface_Name));
}
}
@@ -734,8 +745,7 @@ int bip_get_local_netmask(struct in_addr *netmask)
* @param baddr The broadcast socket binding address, in host order.
* @return 0 on success
*/
int bip_set_broadcast_binding(
const char *ip4_broadcast)
int bip_set_broadcast_binding(const char *ip4_broadcast)
{
BIP_Broadcast_Binding_Address.s_addr = inet_addr(ip4_broadcast);
BIP_Broadcast_Binding_Address_Override = true;
@@ -799,9 +809,11 @@ void bip_set_interface(const char *ifname)
}
#endif
if (BIP_Debug) {
fprintf(stderr, "BIP: Broadcast Address: %s\n",
fprintf(
stderr, "BIP: Broadcast Address: %s\n",
inet_ntoa(BIP_Broadcast_Addr));
fprintf(stderr, "BIP: UDP Port: 0x%04X [%hu]\n", ntohs(BIP_Port),
fprintf(
stderr, "BIP: UDP Port: 0x%04X [%hu]\n", ntohs(BIP_Port),
ntohs(BIP_Port));
fflush(stderr);
}
@@ -835,8 +847,9 @@ static int createSocket(const struct sockaddr_in *sin)
return status;
}
/* Bind to the proper interface to send without default gateway */
status = setsockopt(sock_fd, SOL_SOCKET, SO_BINDTODEVICE,
BIP_Interface_Name, strlen(BIP_Interface_Name));
status = setsockopt(
sock_fd, SOL_SOCKET, SO_BINDTODEVICE, BIP_Interface_Name,
strlen(BIP_Interface_Name));
if (status < 0) {
if (BIP_Debug) {
perror("SO_BINDTODEVICE: ");
@@ -882,7 +895,8 @@ bool bip_init(char *ifname)
bip_set_interface(ifname_default());
}
if (BIP_Address.s_addr == 0) {
fprintf(stderr, "BIP: Failed to get an IP address from %s!\n",
fprintf(
stderr, "BIP: Failed to get an IP address from %s!\n",
BIP_Interface_Name);
fflush(stderr);
return false;
+36 -24
View File
@@ -55,16 +55,16 @@ static void debug_fprintf_bip6(FILE *stream, const char *format, ...)
*/
static void debug_print_ipv6(const char *str, const struct in6_addr *addr)
{
debug_fprintf_bip6(stdout, "BIP6: %s "
debug_fprintf_bip6(
stdout,
"BIP6: %s "
"%02x%02x:%02x%02x:%02x%02x:%02x%02x:"
"%02x%02x:%02x%02x:%02x%02x:%02x%02x\n",
str, (int)addr->s6_addr[0], (int)addr->s6_addr[1],
(int)addr->s6_addr[2], (int)addr->s6_addr[3],
(int)addr->s6_addr[4], (int)addr->s6_addr[5],
(int)addr->s6_addr[6], (int)addr->s6_addr[7],
(int)addr->s6_addr[8], (int)addr->s6_addr[9],
(int)addr->s6_addr[10], (int)addr->s6_addr[11],
(int)addr->s6_addr[12], (int)addr->s6_addr[13],
(int)addr->s6_addr[2], (int)addr->s6_addr[3], (int)addr->s6_addr[4],
(int)addr->s6_addr[5], (int)addr->s6_addr[6], (int)addr->s6_addr[7],
(int)addr->s6_addr[8], (int)addr->s6_addr[9], (int)addr->s6_addr[10],
(int)addr->s6_addr[11], (int)addr->s6_addr[12], (int)addr->s6_addr[13],
(int)addr->s6_addr[14], (int)addr->s6_addr[15]);
}
@@ -106,12 +106,13 @@ void bip6_set_interface(char *ifname)
while (ifa_tmp) {
if ((ifa_tmp->ifa_addr) && (ifa_tmp->ifa_addr->sa_family == AF_INET6)) {
debug_fprintf_bip6(
stdout, "BIP6: found interface: %s\n",ifa_tmp->ifa_name);
stdout, "BIP6: found interface: %s\n", ifa_tmp->ifa_name);
}
if ((ifa_tmp->ifa_addr) && (ifa_tmp->ifa_addr->sa_family == AF_INET6) &&
(strcasecmp(ifa_tmp->ifa_name, ifname) == 0)) {
sin = (struct sockaddr_in6 *)ifa_tmp->ifa_addr;
bvlc6_address_set(&BIP6_Addr, ntohs(sin->sin6_addr.s6_addr16[0]),
bvlc6_address_set(
&BIP6_Addr, ntohs(sin->sin6_addr.s6_addr16[0]),
ntohs(sin->sin6_addr.s6_addr16[1]),
ntohs(sin->sin6_addr.s6_addr16[2]),
ntohs(sin->sin6_addr.s6_addr16[3]),
@@ -127,7 +128,8 @@ void bip6_set_interface(char *ifname)
ifa_tmp = ifa_tmp->ifa_next;
}
if (!found) {
debug_fprintf_bip6(stderr, "BIP6: unable to set interface: %s\n", ifname);
debug_fprintf_bip6(
stderr, "BIP6: unable to set interface: %s\n", ifname);
exit(1);
}
}
@@ -246,8 +248,9 @@ int bip6_send_mpdu(
}
/* load destination IP address */
bvlc_dest.sin6_family = AF_INET6;
bvlc6_address_get(dest, &addr16[0], &addr16[1], &addr16[2], &addr16[3],
&addr16[4], &addr16[5], &addr16[6], &addr16[7]);
bvlc6_address_get(
dest, &addr16[0], &addr16[1], &addr16[2], &addr16[3], &addr16[4],
&addr16[5], &addr16[6], &addr16[7]);
bvlc_dest.sin6_addr.s6_addr16[0] = htons(addr16[0]);
bvlc_dest.sin6_addr.s6_addr16[1] = htons(addr16[1]);
bvlc_dest.sin6_addr.s6_addr16[2] = htons(addr16[2]);
@@ -260,7 +263,8 @@ int bip6_send_mpdu(
bvlc_dest.sin6_scope_id = BIP6_Socket_Scope_Id;
debug_print_ipv6("Sending MPDU->", &bvlc_dest.sin6_addr);
/* Send the packet */
return sendto(BIP6_Socket, (const char *)mtu, mtu_len, 0,
return sendto(
BIP6_Socket, (const char *)mtu, mtu_len, 0,
(struct sockaddr *)&bvlc_dest, sizeof(bvlc_dest));
}
@@ -276,7 +280,8 @@ int bip6_send_mpdu(
* @return Upon successful completion, returns the number of bytes sent.
* Otherwise, -1 shall be returned and errno set to indicate the error.
*/
int bip6_send_pdu(BACNET_ADDRESS *dest,
int bip6_send_pdu(
BACNET_ADDRESS *dest,
BACNET_NPDU_DATA *npdu_data,
uint8_t *pdu,
unsigned pdu_len)
@@ -328,8 +333,9 @@ uint16_t bip6_receive(
max = BIP6_Socket;
/* see if there is a packet for us */
if (select(max + 1, &read_fds, NULL, NULL, &select_timeout) > 0) {
received_bytes = recvfrom(BIP6_Socket, (char *)&npdu[0], max_npdu, 0,
(struct sockaddr *)&sin, &sin_len);
received_bytes = recvfrom(
BIP6_Socket, (char *)&npdu[0], max_npdu, 0, (struct sockaddr *)&sin,
&sin_len);
} else {
return 0;
}
@@ -347,7 +353,8 @@ uint16_t bip6_receive(
}
/* pass the packet into the BBMD handler */
debug_print_ipv6("Received MPDU->", &sin.sin6_addr);
bvlc6_address_set(&addr, ntohs(sin.sin6_addr.s6_addr16[0]),
bvlc6_address_set(
&addr, ntohs(sin.sin6_addr.s6_addr16[0]),
ntohs(sin.sin6_addr.s6_addr16[1]), ntohs(sin.sin6_addr.s6_addr16[2]),
ntohs(sin.sin6_addr.s6_addr16[3]), ntohs(sin.sin6_addr.s6_addr16[4]),
ntohs(sin.sin6_addr.s6_addr16[5]), ntohs(sin.sin6_addr.s6_addr16[6]),
@@ -418,13 +425,15 @@ bool bip6_init(char *ifname)
}
debug_fprintf_bip6(stdout, "BIP6: IPv6 UDP port: 0x%04X\n", BIP6_Addr.port);
if (BIP6_Broadcast_Addr.address[0] == 0) {
bvlc6_address_set(&BIP6_Broadcast_Addr, BIP6_MULTICAST_SITE_LOCAL, 0, 0,
0, 0, 0, 0, BIP6_MULTICAST_GROUP_ID);
bvlc6_address_set(
&BIP6_Broadcast_Addr, BIP6_MULTICAST_SITE_LOCAL, 0, 0, 0, 0, 0, 0,
BIP6_MULTICAST_GROUP_ID);
}
/* assumes that the driver has already been initialized */
BIP6_Socket = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
if (BIP6_Socket < 0)
if (BIP6_Socket < 0) {
return false;
}
/* Allow us to use the same socket for sending and receiving */
/* This makes sure that the src port is correct when sending */
sockopt = 1;
@@ -444,14 +453,17 @@ bool bip6_init(char *ifname)
return false;
}
/* subscribe to a multicast address */
memcpy(&broadcast_address.s6_addr[0], &BIP6_Broadcast_Addr.address[0],
memcpy(
&broadcast_address.s6_addr[0], &BIP6_Broadcast_Addr.address[0],
IP6_ADDRESS_MAX);
memcpy(&join_request.ipv6mr_multiaddr, &broadcast_address,
memcpy(
&join_request.ipv6mr_multiaddr, &broadcast_address,
sizeof(struct in6_addr));
/* Let system not choose the interface */
join_request.ipv6mr_interface = BIP6_Socket_Scope_Id;
status = setsockopt(BIP6_Socket, IPPROTO_IPV6, IPV6_JOIN_GROUP,
&join_request, sizeof(join_request));
status = setsockopt(
BIP6_Socket, IPPROTO_IPV6, IPV6_JOIN_GROUP, &join_request,
sizeof(join_request));
if (status < 0) {
perror("BIP: setsockopt(IPV6_JOIN_GROUP)");
}
+1 -1
View File
@@ -23,7 +23,7 @@ static int32_t Time_Offset; /* Time offset in ms */
/**
* @brief Calculate the time offset from the system clock.
* @return Time offset in ms
*/
*/
static int32_t time_difference(struct timeval t0, struct timeval t1)
{
return (t0.tv_sec - t1.tv_sec) * 1000 + (t0.tv_usec - t1.tv_usec) / 1000;
+10 -5
View File
@@ -267,16 +267,19 @@ static void *dlmstp_master_fsm_task(void *pArg)
silence = MSTP_Port.SilenceTimer(&MSTP_Port);
switch (MSTP_Port.master_state) {
case MSTP_MASTER_STATE_IDLE:
if (silence >= Tno_token)
if (silence >= Tno_token) {
run_master = true;
}
break;
case MSTP_MASTER_STATE_WAIT_FOR_REPLY:
if (silence >= Treply_timeout)
if (silence >= Treply_timeout) {
run_master = true;
}
break;
case MSTP_MASTER_STATE_POLL_FOR_MASTER:
if (silence >= Tusage_timeout)
if (silence >= Tusage_timeout) {
run_master = true;
}
break;
default:
run_master = true;
@@ -290,8 +293,9 @@ static void *dlmstp_master_fsm_task(void *pArg)
/* do nothing while immediate transitioning */
run_loop = MSTP_Master_Node_FSM(&MSTP_Port);
pthread_mutex_lock(&Thread_Mutex);
if (!run_thread)
if (!run_thread) {
run_loop = false;
}
pthread_mutex_unlock(&Thread_Mutex);
}
} else if (MSTP_Port.This_Station < 255) {
@@ -578,8 +582,9 @@ void dlmstp_set_mac_address(uint8_t mac_address)
/* Master Nodes can only have address 0-127 */
if (mac_address <= 127) {
MSTP_Port.This_Station = mac_address;
if (mac_address > MSTP_Port.Nmax_master)
if (mac_address > MSTP_Port.Nmax_master) {
dlmstp_set_max_master(mac_address);
}
}
return;
+10 -5
View File
@@ -258,16 +258,19 @@ static void *dlmstp_master_fsm_task(void *pArg)
silence = mstp_port->SilenceTimer(NULL);
switch (mstp_port->master_state) {
case MSTP_MASTER_STATE_IDLE:
if (silence >= Tno_token)
if (silence >= Tno_token) {
run_master = true;
}
break;
case MSTP_MASTER_STATE_WAIT_FOR_REPLY:
if (silence >= mstp_port->Treply_timeout)
if (silence >= mstp_port->Treply_timeout) {
run_master = true;
}
break;
case MSTP_MASTER_STATE_POLL_FOR_MASTER:
if (silence >= mstp_port->Tusage_timeout)
if (silence >= mstp_port->Tusage_timeout) {
run_master = true;
}
break;
default:
run_master = true;
@@ -324,8 +327,9 @@ uint16_t MSTP_Put_Receive(struct mstp_port_struct_t *mstp_port)
if (!poSharedData->Receive_Packet.ready) {
/* bounds check - maybe this should send an abort? */
pdu_len = mstp_port->DataLength;
if (pdu_len > sizeof(poSharedData->Receive_Packet.pdu))
if (pdu_len > sizeof(poSharedData->Receive_Packet.pdu)) {
pdu_len = sizeof(poSharedData->Receive_Packet.pdu);
}
memmove(
(void *)&poSharedData->Receive_Packet.pdu[0],
(void *)&mstp_port->InputBuffer[0], pdu_len);
@@ -580,8 +584,9 @@ void dlmstp_set_mac_address(void *poPort, uint8_t mac_address)
/* Master Nodes can only have address 0-127 */
if (mac_address <= 127) {
mstp_port->This_Station = mac_address;
if (mac_address > mstp_port->Nmax_master)
if (mac_address > mstp_port->Nmax_master) {
dlmstp_set_max_master(mstp_port, mac_address);
}
}
return;
+71 -92
View File
@@ -1,10 +1,10 @@
/**************************************************************************
*
* Copyright (C) 2012 Steve Karg <skarg@users.sourceforge.net>
*
* SPDX-License-Identifier: MIT
*
*********************************************************************/
*
* Copyright (C) 2012 Steve Karg <skarg@users.sourceforge.net>
*
* SPDX-License-Identifier: MIT
*
*********************************************************************/
#ifndef DLMSTP_LINUX_H
#define DLMSTP_LINUX_H
@@ -25,8 +25,8 @@
#include "bacnet/basic/sys/ringbuf.h"
/* defines specific to MS/TP */
/* preamble+type+dest+src+len+crc8+crc16 */
#define DLMSTP_HEADER_MAX (2+1+1+1+2+1+2)
#define DLMSTP_MPDU_MAX (DLMSTP_HEADER_MAX+MAX_PDU)
#define DLMSTP_HEADER_MAX (2 + 1 + 1 + 1 + 2 + 1 + 2)
#define DLMSTP_MPDU_MAX (DLMSTP_HEADER_MAX + MAX_PDU)
/* count must be a power of 2 for ringbuf library */
#ifndef MSTP_PDU_PACKET_COUNT
@@ -35,10 +35,10 @@
typedef struct dlmstp_packet {
bool ready; /* true if ready to be sent or received */
BACNET_ADDRESS address; /* source address */
BACNET_ADDRESS address; /* source address */
uint8_t frame_type; /* type of message */
uint16_t pdu_len; /* packet length */
uint8_t pdu[DLMSTP_MPDU_MAX]; /* packet */
uint16_t pdu_len; /* packet length */
uint8_t pdu[DLMSTP_MPDU_MAX]; /* packet */
} DLMSTP_PACKET;
/* data structure for MS/TP PDU Queue */
@@ -103,96 +103,75 @@ typedef struct shared_mstp_data {
extern "C" {
#endif /* __cplusplus */
BACNET_STACK_EXPORT
bool dlmstp_init(
void *poShared,
char *ifname);
BACNET_STACK_EXPORT
void dlmstp_reset(
void *poShared);
BACNET_STACK_EXPORT
void dlmstp_cleanup(
void *poShared);
BACNET_STACK_EXPORT
bool dlmstp_init(void *poShared, char *ifname);
BACNET_STACK_EXPORT
void dlmstp_reset(void *poShared);
BACNET_STACK_EXPORT
void dlmstp_cleanup(void *poShared);
/* returns number of bytes sent on success, negative on failure */
BACNET_STACK_EXPORT
int dlmstp_send_pdu(
void *poShared,
BACNET_ADDRESS * dest, /* destination address */
uint8_t * pdu, /* any data to be sent - may be null */
unsigned pdu_len); /* number of bytes of data */
/* returns number of bytes sent on success, negative on failure */
BACNET_STACK_EXPORT
int dlmstp_send_pdu(
void *poShared,
BACNET_ADDRESS *dest, /* destination address */
uint8_t *pdu, /* any data to be sent - may be null */
unsigned pdu_len); /* number of bytes of data */
/* returns the number of octets in the PDU, or zero on failure */
BACNET_STACK_EXPORT
uint16_t dlmstp_receive(
void *poShared,
BACNET_ADDRESS * src, /* source address */
uint8_t * pdu, /* PDU data */
uint16_t max_pdu, /* amount of space available in the PDU */
unsigned timeout); /* milliseconds to wait for a packet */
/* returns the number of octets in the PDU, or zero on failure */
BACNET_STACK_EXPORT
uint16_t dlmstp_receive(
void *poShared,
BACNET_ADDRESS *src, /* source address */
uint8_t *pdu, /* PDU data */
uint16_t max_pdu, /* amount of space available in the PDU */
unsigned timeout); /* milliseconds to wait for a packet */
/* This parameter represents the value of the Max_Info_Frames property of */
/* the node's Device object. The value of Max_Info_Frames specifies the */
/* maximum number of information frames the node may send before it must */
/* pass the token. Max_Info_Frames may have different values on different */
/* nodes. This may be used to allocate more or less of the available link */
/* bandwidth to particular nodes. If Max_Info_Frames is not writable in a */
/* node, its value shall be 1. */
BACNET_STACK_EXPORT
void dlmstp_set_max_info_frames(
void *poShared,
uint8_t max_info_frames);
BACNET_STACK_EXPORT
uint8_t dlmstp_max_info_frames(
void *poShared);
/* This parameter represents the value of the Max_Info_Frames property of */
/* the node's Device object. The value of Max_Info_Frames specifies the */
/* maximum number of information frames the node may send before it must */
/* pass the token. Max_Info_Frames may have different values on different */
/* nodes. This may be used to allocate more or less of the available link */
/* bandwidth to particular nodes. If Max_Info_Frames is not writable in a */
/* node, its value shall be 1. */
BACNET_STACK_EXPORT
void dlmstp_set_max_info_frames(void *poShared, uint8_t max_info_frames);
BACNET_STACK_EXPORT
uint8_t dlmstp_max_info_frames(void *poShared);
/* This parameter represents the value of the Max_Master property of the */
/* node's Device object. The value of Max_Master specifies the highest */
/* allowable address for master nodes. The value of Max_Master shall be */
/* less than or equal to 127. If Max_Master is not writable in a node, */
/* its value shall be 127. */
BACNET_STACK_EXPORT
void dlmstp_set_max_master(
void *poShared,
uint8_t max_master);
BACNET_STACK_EXPORT
uint8_t dlmstp_max_master(
void *poShared);
/* This parameter represents the value of the Max_Master property of the */
/* node's Device object. The value of Max_Master specifies the highest */
/* allowable address for master nodes. The value of Max_Master shall be */
/* less than or equal to 127. If Max_Master is not writable in a node, */
/* its value shall be 127. */
BACNET_STACK_EXPORT
void dlmstp_set_max_master(void *poShared, uint8_t max_master);
BACNET_STACK_EXPORT
uint8_t dlmstp_max_master(void *poShared);
/* MAC address 0-127 */
BACNET_STACK_EXPORT
void dlmstp_set_mac_address(
void *poShared,
uint8_t my_address);
BACNET_STACK_EXPORT
uint8_t dlmstp_mac_address(
void *poShared);
/* MAC address 0-127 */
BACNET_STACK_EXPORT
void dlmstp_set_mac_address(void *poShared, uint8_t my_address);
BACNET_STACK_EXPORT
uint8_t dlmstp_mac_address(void *poShared);
BACNET_STACK_EXPORT
void dlmstp_get_my_address(
void *poShared,
BACNET_ADDRESS * my_address);
BACNET_STACK_EXPORT
void dlmstp_get_broadcast_address(
BACNET_ADDRESS * dest); /* destination address */
BACNET_STACK_EXPORT
void dlmstp_get_my_address(void *poShared, BACNET_ADDRESS *my_address);
BACNET_STACK_EXPORT
void dlmstp_get_broadcast_address(
BACNET_ADDRESS *dest); /* destination address */
/* RS485 Baud Rate 9600, 19200, 38400, 57600, 115200 */
BACNET_STACK_EXPORT
void dlmstp_set_baud_rate(
void *poShared,
uint32_t baud);
BACNET_STACK_EXPORT
uint32_t dlmstp_baud_rate(
void *poShared);
/* RS485 Baud Rate 9600, 19200, 38400, 57600, 115200 */
BACNET_STACK_EXPORT
void dlmstp_set_baud_rate(void *poShared, uint32_t baud);
BACNET_STACK_EXPORT
uint32_t dlmstp_baud_rate(void *poShared);
BACNET_STACK_EXPORT
void dlmstp_fill_bacnet_address(
BACNET_ADDRESS * src,
uint8_t mstp_address);
BACNET_STACK_EXPORT
void dlmstp_fill_bacnet_address(BACNET_ADDRESS *src, uint8_t mstp_address);
BACNET_STACK_EXPORT
bool dlmstp_sole_master(
void);
BACNET_STACK_EXPORT
bool dlmstp_sole_master(void);
#ifdef __cplusplus
}
+48 -27
View File
@@ -17,8 +17,9 @@
* BACnet/Ethernet. */
/* commonly used comparison address for ethernet */
uint8_t Ethernet_Broadcast[MAX_MAC_LEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF };
uint8_t Ethernet_Broadcast[MAX_MAC_LEN] = {
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
};
/* commonly used empty address for ethernet quick compare */
uint8_t Ethernet_Empty_MAC[MAX_MAC_LEN] = { 0, 0, 0, 0, 0, 0 };
@@ -35,8 +36,9 @@ bool ethernet_valid(void)
void ethernet_cleanup(void)
{
if (ethernet_valid())
if (ethernet_valid()) {
close(eth802_sockfd);
}
eth802_sockfd = -1;
return;
@@ -76,7 +78,8 @@ static int ethernet_bind(struct sockaddr *eth_addr, const char *interface_name)
/* check to see if we are being run as root */
uid = getuid();
if (uid != 0) {
fprintf(stderr,
fprintf(
stderr,
"ethernet: Unable to open an 802.2 socket. "
"Try running with root priveleges.\n");
return sock_fd;
@@ -93,7 +96,8 @@ static int ethernet_bind(struct sockaddr *eth_addr, const char *interface_name)
/* Error occured */
fprintf(
stderr, "ethernet: Error opening socket: %s\n", strerror(errno));
fprintf(stderr,
fprintf(
stderr,
"You might need to add the following to modules.conf\n"
"(or in /etc/modutils/alias on Debian with update-modules):\n"
"alias net-pf-17 af_packet\n"
@@ -121,9 +125,11 @@ static int ethernet_bind(struct sockaddr *eth_addr, const char *interface_name)
/* Attempt to bind the socket to the interface */
if (bind(sock_fd, eth_addr, sizeof(struct sockaddr)) != 0) {
/* Bind problem, close socket and return */
fprintf(stderr, "ethernet: Unable to bind 802.2 socket : %s\n",
fprintf(
stderr, "ethernet: Unable to bind 802.2 socket : %s\n",
strerror(errno));
fprintf(stderr,
fprintf(
stderr,
"You might need to add the following to modules.conf\n"
"(or in /etc/modutils/alias on Debian with update-modules):\n"
"alias net-pf-17 af_packet\n"
@@ -150,12 +156,13 @@ static int get_local_hwaddr(const char *ifname, unsigned char *mac)
/* determine the local MAC address */
strcpy(ifr.ifr_name, ifname);
fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
if (fd < 0)
if (fd < 0) {
rv = fd;
else {
} else {
rv = ioctl(fd, SIOCGIFHWADDR, &ifr);
if (rv >= 0) /* worked okay */
if (rv >= 0) { /* worked okay */
memcpy(mac, ifr.ifr_hwaddr.sa_data, IFHWADDRLEN);
}
}
return rv;
@@ -179,19 +186,22 @@ int ethernet_send(uint8_t *mtu, int mtu_len)
int bytes = 0;
/* Send the packet */
bytes = sendto(eth802_sockfd, &mtu, mtu_len, 0,
(struct sockaddr *)&eth_addr, sizeof(struct sockaddr));
bytes = sendto(
eth802_sockfd, &mtu, mtu_len, 0, (struct sockaddr *)&eth_addr,
sizeof(struct sockaddr));
/* did it get sent? */
if (bytes < 0)
if (bytes < 0) {
fprintf(
stderr, "ethernet: Error sending packet: %s\n", strerror(errno));
}
return bytes;
}
/* function to send a packet out the 802.2 socket */
/* returns number of bytes sent on success, negative on failure */
int ethernet_send_pdu(BACNET_ADDRESS *dest, /* destination address */
int ethernet_send_pdu(
BACNET_ADDRESS *dest, /* destination address */
BACNET_NPDU_DATA *npdu_data, /* network information */
uint8_t *pdu, /* any data to be sent - may be null */
unsigned pdu_len)
@@ -248,19 +258,22 @@ int ethernet_send_pdu(BACNET_ADDRESS *dest, /* destination address */
encode_unsigned16(&mtu[12], 3 + pdu_len);
/* Send the packet */
bytes = sendto(eth802_sockfd, &mtu, mtu_len, 0,
(struct sockaddr *)&eth_addr, sizeof(struct sockaddr));
bytes = sendto(
eth802_sockfd, &mtu, mtu_len, 0, (struct sockaddr *)&eth_addr,
sizeof(struct sockaddr));
/* did it get sent? */
if (bytes < 0)
if (bytes < 0) {
fprintf(
stderr, "ethernet: Error sending packet: %s\n", strerror(errno));
}
return bytes;
}
/* receives an 802.2 framed packet */
/* returns the number of octets in the PDU, or zero on failure */
uint16_t ethernet_receive(BACNET_ADDRESS *src, /* source address */
uint16_t ethernet_receive(
BACNET_ADDRESS *src, /* source address */
uint8_t *pdu, /* PDU data */
uint16_t max_pdu, /* amount of space available in the PDU */
unsigned timeout)
@@ -273,8 +286,9 @@ uint16_t ethernet_receive(BACNET_ADDRESS *src, /* source address */
struct timeval select_timeout;
/* Make sure the socket is open */
if (eth802_sockfd <= 0)
if (eth802_sockfd <= 0) {
return 0;
}
/* we could just use a non-blocking socket, but that consumes all
the CPU time. We can use a timeout; it is only supported as
@@ -291,24 +305,28 @@ uint16_t ethernet_receive(BACNET_ADDRESS *src, /* source address */
FD_SET(eth802_sockfd, &read_fds);
max = eth802_sockfd;
if (select(max + 1, &read_fds, NULL, NULL, &select_timeout) > 0)
if (select(max + 1, &read_fds, NULL, NULL, &select_timeout) > 0) {
received_bytes = read(eth802_sockfd, &buf[0], sizeof(buf));
else
} else {
return 0;
}
/* See if there is a problem */
if (received_bytes < 0) {
/* EAGAIN Non-blocking I/O has been selected */
/* using O_NONBLOCK and no data */
/* was immediately available for reading. */
if (errno != EAGAIN)
fprintf(stderr, "ethernet: Read error in receiving packet: %s\n",
if (errno != EAGAIN) {
fprintf(
stderr, "ethernet: Read error in receiving packet: %s\n",
strerror(errno));
}
return 0;
}
if (received_bytes == 0)
if (received_bytes == 0) {
return 0;
}
/* the signature of an 802.2 BACnet packet */
if ((buf[14] != 0x82) && (buf[15] != 0x82)) {
@@ -330,11 +348,13 @@ uint16_t ethernet_receive(BACNET_ADDRESS *src, /* source address */
(void)decode_unsigned16(&buf[12], &pdu_len);
pdu_len -= 3 /* DSAP, SSAP, LLC Control */;
/* copy the buffer into the PDU */
if (pdu_len < max_pdu)
if (pdu_len < max_pdu) {
memmove(&pdu[0], &buf[17], pdu_len);
}
/* ignore packets that are too large */
else
else {
pdu_len = 0;
}
return pdu_len;
}
@@ -391,8 +411,9 @@ void ethernet_debug_address(const char *info, const BACNET_ADDRESS *dest)
{
int i = 0; /* counter */
if (info)
if (info) {
fprintf(stderr, "%s", info);
}
if (dest) {
fprintf(stderr, "Address:\n");
fprintf(stderr, " MAC Length=%d\n", dest->mac_len);
+7 -8
View File
@@ -68,8 +68,7 @@ uint16_t MSTP_Put_Receive(struct mstp_port_struct_t *mstp_port)
/* for the MS/TP state machine to use for getting data to send */
/* Return: amount of PDU data */
uint16_t MSTP_Get_Send(
struct mstp_port_struct_t *mstp_port, unsigned timeout)
uint16_t MSTP_Get_Send(struct mstp_port_struct_t *mstp_port, unsigned timeout)
{ /* milliseconds to wait for a packet */
(void)mstp_port;
(void)timeout;
@@ -84,7 +83,7 @@ uint16_t MSTP_Get_Send(
*/
void MSTP_Send_Frame(
struct mstp_port_struct_t *mstp_port,
const uint8_t * buffer,
const uint8_t *buffer,
uint16_t nbytes)
{
(void)mstp_port;
@@ -92,8 +91,7 @@ void MSTP_Send_Frame(
(void)nbytes;
}
uint16_t MSTP_Get_Reply(
struct mstp_port_struct_t *mstp_port, unsigned timeout)
uint16_t MSTP_Get_Reply(struct mstp_port_struct_t *mstp_port, unsigned timeout)
{ /* milliseconds to wait for a packet */
(void)mstp_port;
(void)timeout;
@@ -140,8 +138,8 @@ static int network_init(const char *name, int protocol)
return sockfd;
}
static void snap_received_packet(
const struct mstp_port_struct_t *mstp_port, int sockfd)
static void
snap_received_packet(const struct mstp_port_struct_t *mstp_port, int sockfd)
{
uint16_t mtu_len = 0; /* number of octets of packet saved in file */
unsigned i = 0; /* counter */
@@ -267,7 +265,8 @@ int main(int argc, char *argv[])
MSTP_Port.SilenceTimer = Timer_Silence;
MSTP_Port.SilenceTimerReset = Timer_Silence_Reset;
MSTP_Init(mstp_port);
fprintf(stdout, "mstpcap: Using %s for capture at %ld bps.\n",
fprintf(
stdout, "mstpcap: Using %s for capture at %ld bps.\n",
RS485_Interface(), (long)RS485_Get_Baud_Rate());
atexit(cleanup);
#if defined(_WIN32)
+14 -8
View File
@@ -569,12 +569,14 @@ void RS485_Initialize(void)
newserial.custom_divisor = round(((float)newserial.baud_base) / 76800);
/* we must check that we calculated some sane value;
small baud bases yield bad custom divisor values */
baud_error = fabs(1 -
baud_error = fabs(
1 -
((float)newserial.baud_base) / ((float)newserial.custom_divisor) /
76800);
if ((newserial.custom_divisor == 0) || (baud_error > 0.02)) {
/* bad divisor */
fprintf(stderr, "RS485 bad custom divisor %d, base baud %d\n",
fprintf(
stderr, "RS485 bad custom divisor %d, base baud %d\n",
newserial.custom_divisor, newserial.baud_base);
exit(EXIT_FAILURE);
}
@@ -617,19 +619,22 @@ void RS485_Print_Ports(void)
while (n--) {
if (strcmp(namelist[n]->d_name, "..") &&
strcmp(namelist[n]->d_name, ".")) {
snprintf(device_dir, sizeof(device_dir), "%s%s/device", sysdir,
snprintf(
device_dir, sizeof(device_dir), "%s%s/device", sysdir,
namelist[n]->d_name);
/* Stat the devicedir and handle it if it is a symlink */
if (lstat(device_dir, &st) == 0 && S_ISLNK(st.st_mode)) {
memset(buffer, 0, sizeof(buffer));
snprintf(device_dir, sizeof(device_dir),
"%s%s/device/driver", sysdir, namelist[n]->d_name);
snprintf(
device_dir, sizeof(device_dir), "%s%s/device/driver",
sysdir, namelist[n]->d_name);
if (readlink(device_dir, buffer, sizeof(buffer)) > 0) {
valid_port = false;
driver_name = basename(buffer);
if (strcmp(driver_name, "serial8250") == 0) {
/* serial8250-devices must be probed */
snprintf(device_dir, sizeof(device_dir), "/dev/%s",
snprintf(
device_dir, sizeof(device_dir), "/dev/%s",
namelist[n]->d_name);
fd = open(
device_dir, O_RDWR | O_NONBLOCK | O_NOCTTY);
@@ -649,8 +654,9 @@ void RS485_Print_Ports(void)
}
if (valid_port) {
/* print full absolute file path */
printf("interface {value=/dev/%s}"
"{display=MS/TP Capture on /dev/%s}\n",
printf(
"interface {value=/dev/%s}"
"{display=MS/TP Capture on /dev/%s}\n",
namelist[n]->d_name, namelist[n]->d_name);
}
}
+24 -32
View File
@@ -15,42 +15,34 @@
extern "C" {
#endif /* __cplusplus */
BACNET_STACK_EXPORT
void RS485_Set_Interface(
char *ifname);
BACNET_STACK_EXPORT
const char *RS485_Interface(
void);
BACNET_STACK_EXPORT
void RS485_Set_Interface(char *ifname);
BACNET_STACK_EXPORT
const char *RS485_Interface(void);
BACNET_STACK_EXPORT
void RS485_Initialize(
void);
BACNET_STACK_EXPORT
void RS485_Initialize(void);
BACNET_STACK_EXPORT
void RS485_Send_Frame(
struct mstp_port_struct_t *mstp_port, /* port specific data */
const uint8_t * buffer, /* frame to send (up to 501 bytes of data) */
uint16_t nbytes); /* number of bytes of data (up to 501) */
BACNET_STACK_EXPORT
void RS485_Send_Frame(
struct mstp_port_struct_t *mstp_port, /* port specific data */
const uint8_t *buffer, /* frame to send (up to 501 bytes of data) */
uint16_t nbytes); /* number of bytes of data (up to 501) */
BACNET_STACK_EXPORT
void RS485_Check_UART_Data(
struct mstp_port_struct_t *mstp_port); /* port specific data */
BACNET_STACK_EXPORT
uint32_t RS485_Get_Port_Baud_Rate(
struct mstp_port_struct_t *mstp_port);
BACNET_STACK_EXPORT
uint32_t RS485_Get_Baud_Rate(
void);
BACNET_STACK_EXPORT
bool RS485_Set_Baud_Rate(
uint32_t baud);
BACNET_STACK_EXPORT
void RS485_Check_UART_Data(
struct mstp_port_struct_t *mstp_port); /* port specific data */
BACNET_STACK_EXPORT
uint32_t RS485_Get_Port_Baud_Rate(struct mstp_port_struct_t *mstp_port);
BACNET_STACK_EXPORT
uint32_t RS485_Get_Baud_Rate(void);
BACNET_STACK_EXPORT
bool RS485_Set_Baud_Rate(uint32_t baud);
BACNET_STACK_EXPORT
void RS485_Cleanup(
void);
BACNET_STACK_EXPORT
void RS485_Print_Ports(
void);
BACNET_STACK_EXPORT
void RS485_Cleanup(void);
BACNET_STACK_EXPORT
void RS485_Print_Ports(void);
#ifdef __cplusplus
}