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
+18 -20
View File
@@ -1,10 +1,10 @@
/**************************************************************************
*
* 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
@@ -15,10 +15,10 @@
#define STRICT 1
/* Windows XP minimum */
#if (_WIN32_WINNT < _WIN32_WINNT_WINXP)
#undef _WIN32_WINNT
#define _WIN32_WINNT _WIN32_WINNT_WINXP
#undef NTDDI_VERSION
#define NTDDI_VERSION NTDDI_WINXP
#undef _WIN32_WINNT
#define _WIN32_WINNT _WIN32_WINNT_WINXP
#undef NTDDI_VERSION
#define NTDDI_VERSION NTDDI_WINXP
#endif
#include <windows.h>
@@ -38,10 +38,10 @@
#ifdef __MINGW32__
#include <ws2spi.h>
#else
#pragma warning( push )
#pragma warning(disable: 4101 4191)
#pragma warning(push)
#pragma warning(disable : 4101 4191)
#include <wspiapi.h>
#pragma warning( pop )
#pragma warning(pop)
/* add winmm.lib to our build */
#pragma comment(lib, "winmm.lib")
#endif
@@ -60,14 +60,12 @@
#endif
BACNET_STACK_EXPORT
extern int bip_get_local_netmask(
struct in_addr *netmask);
extern int bip_get_local_netmask(struct in_addr *netmask);
#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")
#endif
+36 -25
View File
@@ -44,13 +44,15 @@ static bool BIP_Debug;
* @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);
}
@@ -222,7 +224,8 @@ static char *winsock_error_code_text(int code)
static void print_last_error(const char *info)
{
int Code = WSAGetLastError();
fprintf(stderr, "BIP: %s [error code %i] %s\n", info, Code,
fprintf(
stderr, "BIP: %s [error code %i] %s\n", info, Code,
winsock_error_code_text(Code));
fflush(stderr);
}
@@ -433,8 +436,9 @@ int bip_send_mpdu(
/* Send the packet */
debug_print_ipv4(
"Sending MPDU->", &bip_dest.sin_addr, bip_dest.sin_port, mtu_len);
rv = sendto(BIP_Socket, (const char *)mtu, mtu_len, 0,
(struct sockaddr *)&bip_dest, sizeof(struct sockaddr));
rv = sendto(
BIP_Socket, (const char *)mtu, mtu_len, 0, (struct sockaddr *)&bip_dest,
sizeof(struct sockaddr));
if (rv == SOCKET_ERROR) {
print_last_error("sendto");
}
@@ -490,10 +494,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;
}
@@ -530,9 +535,9 @@ uint16_t bip_receive(
debug_print_ipv4(
"Received MPDU->", &sin.sin_addr, sin.sin_port, received_bytes);
/* pass the packet into the BBMD handler */
offset = socket == BIP_Socket ?
bvlc_handler(&addr, src, npdu, received_bytes) :
bvlc_broadcast_handler(&addr, src, npdu, received_bytes);
offset = socket == BIP_Socket
? bvlc_handler(&addr, src, npdu, received_bytes)
: bvlc_broadcast_handler(&addr, src, npdu, received_bytes);
if (offset > 0) {
npdu_len = received_bytes - offset;
if (npdu_len <= max_npdu) {
@@ -560,7 +565,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)
@@ -612,7 +618,8 @@ static long gethostaddr(void)
exit(1);
}
if (BIP_Debug) {
fprintf(stderr, "BIP: host %s at %u.%u.%u.%u\n", host_name,
fprintf(
stderr, "BIP: host %s at %u.%u.%u.%u\n", host_name,
(unsigned)((uint8_t *)host_ent->h_addr)[0],
(unsigned)((uint8_t *)host_ent->h_addr)[1],
(unsigned)((uint8_t *)host_ent->h_addr)[2],
@@ -685,8 +692,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;
@@ -699,20 +705,21 @@ static void set_broadcast_address(uint32_t net_address)
#ifdef BACNET_IP_BROADCAST_USE_CLASSADDR
long broadcast_address = 0;
if (IN_CLASSA(ntohl(net_address)))
if (IN_CLASSA(ntohl(net_address))) {
broadcast_address =
(ntohl(net_address) & ~IN_CLASSA_HOST) | IN_CLASSA_HOST;
else if (IN_CLASSB(ntohl(net_address)))
} else if (IN_CLASSB(ntohl(net_address))) {
broadcast_address =
(ntohl(net_address) & ~IN_CLASSB_HOST) | IN_CLASSB_HOST;
else if (IN_CLASSC(ntohl(net_address)))
} else if (IN_CLASSC(ntohl(net_address))) {
broadcast_address =
(ntohl(net_address) & ~IN_CLASSC_HOST) | IN_CLASSC_HOST;
else if (IN_CLASSD(ntohl(net_address)))
} else if (IN_CLASSD(ntohl(net_address))) {
broadcast_address =
(ntohl(net_address) & ~IN_CLASSD_HOST) | IN_CLASSD_HOST;
else
} else {
broadcast_address = INADDR_BROADCAST;
}
BIP_Broadcast_Addr.s_addr = htonl(broadcast_address);
#else
/* these are network byte order variables */
@@ -830,9 +837,11 @@ bool bip_init(char *ifname)
}
if (BIP_Debug) {
fprintf(stderr, "BIP: Address: %s\n", inet_ntoa(BIP_Address));
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);
}
@@ -842,7 +851,8 @@ bool bip_init(char *ifname)
memset(&(sin.sin_zero), '\0', sizeof(sin.sin_zero));
sin.sin_addr.s_addr = BIP_Address.s_addr;
if (BIP_Debug) {
fprintf(stderr, "BIP: bind %s:%hu\n", inet_ntoa(sin.sin_addr),
fprintf(
stderr, "BIP: bind %s:%hu\n", inet_ntoa(sin.sin_addr),
ntohs(sin.sin_port));
fflush(stderr);
}
@@ -863,7 +873,8 @@ bool bip_init(char *ifname)
#endif
}
if (BIP_Debug) {
fprintf(stderr, "BIP: broadcast bind %s:%hu\n", inet_ntoa(sin.sin_addr),
fprintf(
stderr, "BIP: broadcast bind %s:%hu\n", inet_ntoa(sin.sin_addr),
ntohs(sin.sin_port));
fflush(stderr);
}
+2 -1
View File
@@ -348,7 +348,8 @@ bool bip6_get_broadcast_addr(BACNET_IP6_ADDRESS *addr)
* @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_mpdu(const BACNET_IP6_ADDRESS *dest, const uint8_t *mtu, uint16_t mtu_len)
int bip6_send_mpdu(
const BACNET_IP6_ADDRESS *dest, const uint8_t *mtu, uint16_t mtu_len)
{
struct sockaddr_in6 bvlc_dest = { 0 };
uint16_t addr16[8];
+10 -7
View File
@@ -118,7 +118,8 @@ void datetime_timesync(BACNET_DATE *bdate, BACNET_TIME *btime, bool utc)
* @param true if DST is enabled and active
* @return true if local time was retrieved
*/
bool datetime_local(BACNET_DATE *bdate,
bool datetime_local(
BACNET_DATE *bdate,
BACNET_TIME *btime,
int16_t *utc_offset_minutes,
bool *dst_active)
@@ -152,15 +153,17 @@ bool datetime_local(BACNET_DATE *bdate,
* int tm_yday Day of year [0,365].
* int tm_isdst Daylight Savings flag.
*/
datetime_set_date(bdate, (uint16_t)tblock->tm_year + 1900,
datetime_set_date(
bdate, (uint16_t)tblock->tm_year + 1900,
(uint8_t)tblock->tm_mon + 1, (uint8_t)tblock->tm_mday);
#if !defined(_MSC_VER)
datetime_set_time(btime, (uint8_t)tblock->tm_hour,
(uint8_t)tblock->tm_min, (uint8_t)tblock->tm_sec,
(uint8_t)(tv.tv_usec / 10000));
datetime_set_time(
btime, (uint8_t)tblock->tm_hour, (uint8_t)tblock->tm_min,
(uint8_t)tblock->tm_sec, (uint8_t)(tv.tv_usec / 10000));
#else
datetime_set_time(btime, (uint8_t)tblock->tm_hour,
(uint8_t)tblock->tm_min, (uint8_t)tblock->tm_sec, 0);
datetime_set_time(
btime, (uint8_t)tblock->tm_hour, (uint8_t)tblock->tm_min,
(uint8_t)tblock->tm_sec, 0);
#endif
if (dst_active) {
/* The value of tm_isdst is:
+49 -37
View File
@@ -93,7 +93,8 @@ void dlmstp_cleanup(void)
}
/* returns number of bytes sent on success, zero on failure */
int dlmstp_send_pdu(BACNET_ADDRESS *dest, /* destination address */
int dlmstp_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)
@@ -120,7 +121,8 @@ int dlmstp_send_pdu(BACNET_ADDRESS *dest, /* destination address */
return bytes_sent;
}
uint16_t dlmstp_receive(BACNET_ADDRESS *src, /* source address */
uint16_t dlmstp_receive(
BACNET_ADDRESS *src, /* source address */
uint8_t *pdu, /* PDU data */
uint16_t max_pdu, /* amount of space available in the PDU */
unsigned timeout)
@@ -137,7 +139,8 @@ uint16_t dlmstp_receive(BACNET_ADDRESS *src, /* source address */
if (Receive_Packet.pdu_len) {
MSTP_Packets++;
if (src) {
memmove(src, &Receive_Packet.address,
memmove(
src, &Receive_Packet.address,
sizeof(Receive_Packet.address));
}
if (pdu) {
@@ -198,8 +201,9 @@ static void dlmstp_master_fsm_task(void *pArg)
dwMilliseconds = 0;
break;
}
if (dwMilliseconds)
if (dwMilliseconds) {
WaitForSingleObject(Received_Frame_Flag, dwMilliseconds);
}
MSTP_Master_Node_FSM(&MSTP_Port);
}
}
@@ -236,10 +240,12 @@ uint16_t MSTP_Put_Receive(struct mstp_port_struct_t *mstp_port)
if (!Receive_Packet.ready) {
/* bounds check - maybe this should send an abort? */
pdu_len = mstp_port->DataLength;
if (pdu_len > sizeof(Receive_Packet.pdu))
if (pdu_len > sizeof(Receive_Packet.pdu)) {
pdu_len = sizeof(Receive_Packet.pdu);
memmove((void *)&Receive_Packet.pdu[0],
(void *)&mstp_port->InputBuffer[0], pdu_len);
}
memmove(
(void *)&Receive_Packet.pdu[0], (void *)&mstp_port->InputBuffer[0],
pdu_len);
dlmstp_fill_bacnet_address(
&Receive_Packet.address, mstp_port->SourceAddress);
Receive_Packet.pdu_len = mstp_port->DataLength;
@@ -252,8 +258,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 */
uint16_t pdu_len = 0;
uint8_t destination = 0; /* destination address */
@@ -273,11 +278,11 @@ uint16_t MSTP_Get_Send(
return 0;
}
/* convert the PDU into the MSTP Frame */
pdu_len =
MSTP_Create_Frame(&mstp_port->OutputBuffer[0], /* <-- loading this */
mstp_port->OutputBufferSize, Transmit_Packet.frame_type,
destination, mstp_port->This_Station, &Transmit_Packet.pdu[0],
Transmit_Packet.pdu_len);
pdu_len = MSTP_Create_Frame(
&mstp_port->OutputBuffer[0], /* <-- loading this */
mstp_port->OutputBufferSize, Transmit_Packet.frame_type, destination,
mstp_port->This_Station, &Transmit_Packet.pdu[0],
Transmit_Packet.pdu_len);
Transmit_Packet.ready = false;
return pdu_len;
@@ -291,13 +296,14 @@ 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)
{
RS485_Send_Frame(mstp_port, buffer, nbytes);
}
bool dlmstp_compare_data_expecting_reply(const uint8_t *request_pdu,
bool dlmstp_compare_data_expecting_reply(
const uint8_t *request_pdu,
uint16_t request_pdu_len,
uint8_t src_address,
const uint8_t *reply_pdu,
@@ -324,8 +330,9 @@ bool dlmstp_compare_data_expecting_reply(const uint8_t *request_pdu,
/* decode the request data */
request.address.mac[0] = src_address;
request.address.mac_len = 1;
offset = bacnet_npdu_decode(request_pdu, request_pdu_len, NULL,
&request.address, &request.npdu_data);
offset = bacnet_npdu_decode(
request_pdu, request_pdu_len, NULL, &request.address,
&request.npdu_data);
if (request.npdu_data.network_layer_message) {
return false;
}
@@ -335,10 +342,11 @@ bool dlmstp_compare_data_expecting_reply(const uint8_t *request_pdu,
}
request.invoke_id = request_pdu[offset + 2];
/* segmented message? */
if (request_pdu[offset] & BIT(3))
if (request_pdu[offset] & BIT(3)) {
request.service_choice = request_pdu[offset + 5];
else
} else {
request.service_choice = request_pdu[offset + 3];
}
/* decode the reply data */
bacnet_address_copy(&reply.address, dest_address);
offset = bacnet_npdu_decode(
@@ -357,10 +365,11 @@ bool dlmstp_compare_data_expecting_reply(const uint8_t *request_pdu,
case PDU_TYPE_COMPLEX_ACK:
reply.invoke_id = reply_pdu[offset + 1];
/* segmented message? */
if (reply_pdu[offset] & BIT(3))
if (reply_pdu[offset] & BIT(3)) {
reply.service_choice = reply_pdu[offset + 4];
else
} else {
reply.service_choice = reply_pdu[offset + 2];
}
break;
case PDU_TYPE_ERROR:
reply.invoke_id = reply_pdu[offset + 1];
@@ -406,8 +415,7 @@ bool dlmstp_compare_data_expecting_reply(const uint8_t *request_pdu,
}
/* Get the reply to a DATA_EXPECTING_REPLY frame, or nothing */
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 */
uint16_t pdu_len = 0; /* return value */
uint8_t destination = 0; /* destination address */
@@ -427,18 +435,19 @@ uint16_t MSTP_Get_Reply(
return 0;
}
/* is this the reply to the DER? */
matched = dlmstp_compare_data_expecting_reply(&mstp_port->InputBuffer[0],
mstp_port->DataLength, mstp_port->SourceAddress,
&Transmit_Packet.pdu[0], Transmit_Packet.pdu_len,
&Transmit_Packet.address);
if (!matched)
matched = dlmstp_compare_data_expecting_reply(
&mstp_port->InputBuffer[0], mstp_port->DataLength,
mstp_port->SourceAddress, &Transmit_Packet.pdu[0],
Transmit_Packet.pdu_len, &Transmit_Packet.address);
if (!matched) {
return 0;
}
/* convert the PDU into the MSTP Frame */
pdu_len =
MSTP_Create_Frame(&mstp_port->OutputBuffer[0], /* <-- loading this */
mstp_port->OutputBufferSize, Transmit_Packet.frame_type,
destination, mstp_port->This_Station, &Transmit_Packet.pdu[0],
Transmit_Packet.pdu_len);
pdu_len = MSTP_Create_Frame(
&mstp_port->OutputBuffer[0], /* <-- loading this */
mstp_port->OutputBufferSize, Transmit_Packet.frame_type, destination,
mstp_port->This_Station, &Transmit_Packet.pdu[0],
Transmit_Packet.pdu_len);
Transmit_Packet.ready = false;
return pdu_len;
@@ -454,8 +463,9 @@ void dlmstp_set_mac_address(uint8_t mac_address)
EEPROM_DEVICE_ADDRESS,
mac_address,
EEPROM_MSTP_MAC_ADDR); */
if (mac_address > MSTP_Port.Nmax_master)
if (mac_address > MSTP_Port.Nmax_master) {
dlmstp_set_max_master(mac_address);
}
}
return;
@@ -571,8 +581,9 @@ bool dlmstp_init(char *ifname)
Receive_Packet.ready = false;
Receive_Packet.pdu_len = 0;
Receive_Packet_Flag = CreateSemaphore(NULL, 0, 1, "dlmstpReceivePacket");
if (Receive_Packet_Flag == NULL)
if (Receive_Packet_Flag == NULL) {
exit(1);
}
Received_Frame_Flag = CreateSemaphore(NULL, 0, 1, "dlsmtpReceiveFrame");
if (Received_Frame_Flag == NULL) {
CloseHandle(Receive_Packet_Flag);
@@ -651,7 +662,8 @@ bool dlmstp_init(char *ifname)
#ifdef TEST_DLMSTP
#include <stdio.h>
void apdu_handler(BACNET_ADDRESS *src, /* source address */
void apdu_handler(
BACNET_ADDRESS *src, /* source address */
uint8_t *apdu, /* APDU data */
uint16_t pdu_len)
{ /* for confirmed messages */
+53 -39
View File
@@ -79,7 +79,8 @@ void dlmstp_cleanup(void)
}
/* returns number of bytes sent on success, zero on failure */
int dlmstp_send_pdu(BACNET_ADDRESS *dest, /* destination address */
int dlmstp_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)
@@ -106,7 +107,8 @@ int dlmstp_send_pdu(BACNET_ADDRESS *dest, /* destination address */
return bytes_sent;
}
uint16_t dlmstp_receive(BACNET_ADDRESS *src, /* source address */
uint16_t dlmstp_receive(
BACNET_ADDRESS *src, /* source address */
uint8_t *pdu, /* PDU data */
uint16_t max_pdu, /* amount of space available in the PDU */
unsigned timeout)
@@ -123,7 +125,8 @@ uint16_t dlmstp_receive(BACNET_ADDRESS *src, /* source address */
if (Receive_Packet.pdu_len) {
MSTP_Packets++;
if (src) {
memmove(src, &Receive_Packet.address,
memmove(
src, &Receive_Packet.address,
sizeof(Receive_Packet.address));
}
if (pdu) {
@@ -184,9 +187,11 @@ static void dlmstp_master_fsm_task(void *pArg)
dwMilliseconds = 0;
break;
}
if (dwMilliseconds)
if (dwMilliseconds) {
WaitForSingleObject(Received_Frame_Flag, dwMilliseconds);
while (MSTP_Master_Node_FSM(&MSTP_Port));
}
while (MSTP_Master_Node_FSM(&MSTP_Port))
;
}
}
@@ -222,10 +227,12 @@ uint16_t MSTP_Put_Receive(struct mstp_port_struct_t *mstp_port)
if (!Receive_Packet.ready) {
/* bounds check - maybe this should send an abort? */
pdu_len = mstp_port->DataLength;
if (pdu_len > sizeof(Receive_Packet.pdu))
if (pdu_len > sizeof(Receive_Packet.pdu)) {
pdu_len = sizeof(Receive_Packet.pdu);
memmove((void *)&Receive_Packet.pdu[0],
(void *)&mstp_port->InputBuffer[0], pdu_len);
}
memmove(
(void *)&Receive_Packet.pdu[0], (void *)&mstp_port->InputBuffer[0],
pdu_len);
dlmstp_fill_bacnet_address(
&Receive_Packet.address, mstp_port->SourceAddress);
Receive_Packet.pdu_len = mstp_port->DataLength;
@@ -239,8 +246,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 */
uint16_t pdu_len = 0;
uint8_t destination = 0; /* destination address */
@@ -259,11 +265,11 @@ uint16_t MSTP_Get_Send(
return 0;
}
/* convert the PDU into the MSTP Frame */
pdu_len =
MSTP_Create_Frame(&mstp_port->OutputBuffer[0], /* <-- loading this */
mstp_port->OutputBufferSize, Transmit_Packet.frame_type,
destination, mstp_port->This_Station, &Transmit_Packet.pdu[0],
Transmit_Packet.pdu_len);
pdu_len = MSTP_Create_Frame(
&mstp_port->OutputBuffer[0], /* <-- loading this */
mstp_port->OutputBufferSize, Transmit_Packet.frame_type, destination,
mstp_port->This_Station, &Transmit_Packet.pdu[0],
Transmit_Packet.pdu_len);
Transmit_Packet.ready = false;
return pdu_len;
@@ -277,13 +283,14 @@ 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)
{
RS485_Send_Frame(mstp_port, buffer, nbytes);
}
static bool dlmstp_compare_data_expecting_reply(const uint8_t *request_pdu,
static bool dlmstp_compare_data_expecting_reply(
const uint8_t *request_pdu,
uint16_t request_pdu_len,
uint8_t src_address,
const uint8_t *reply_pdu,
@@ -310,8 +317,9 @@ static bool dlmstp_compare_data_expecting_reply(const uint8_t *request_pdu,
/* decode the request data */
request.address.mac[0] = src_address;
request.address.mac_len = 1;
offset = (uint16_t)bacnet_npdu_decode(request_pdu, request_pdu_len, NULL,
&request.address, &request.npdu_data);
offset = (uint16_t)bacnet_npdu_decode(
request_pdu, request_pdu_len, NULL, &request.address,
&request.npdu_data);
if (request.npdu_data.network_layer_message) {
return false;
}
@@ -321,10 +329,11 @@ static bool dlmstp_compare_data_expecting_reply(const uint8_t *request_pdu,
}
request.invoke_id = request_pdu[offset + 2];
/* segmented message? */
if (request_pdu[offset] & BIT(3))
if (request_pdu[offset] & BIT(3)) {
request.service_choice = request_pdu[offset + 5];
else
} else {
request.service_choice = request_pdu[offset + 3];
}
/* decode the reply data */
bacnet_address_copy(&reply.address, dest_address);
offset = (uint16_t)bacnet_npdu_decode(
@@ -343,10 +352,11 @@ static bool dlmstp_compare_data_expecting_reply(const uint8_t *request_pdu,
case PDU_TYPE_COMPLEX_ACK:
reply.invoke_id = reply_pdu[offset + 1];
/* segmented message? */
if (reply_pdu[offset] & BIT(3))
if (reply_pdu[offset] & BIT(3)) {
reply.service_choice = reply_pdu[offset + 4];
else
} else {
reply.service_choice = reply_pdu[offset + 2];
}
break;
case PDU_TYPE_ERROR:
reply.invoke_id = reply_pdu[offset + 1];
@@ -392,8 +402,7 @@ static bool dlmstp_compare_data_expecting_reply(const uint8_t *request_pdu,
}
/* Get the reply to a DATA_EXPECTING_REPLY frame, or nothing */
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 */
uint16_t pdu_len = 0; /* return value */
uint8_t destination = 0; /* destination address */
@@ -413,18 +422,19 @@ uint16_t MSTP_Get_Reply(
return 0;
}
/* is this the reply to the DER? */
matched = dlmstp_compare_data_expecting_reply(&mstp_port->InputBuffer[0],
mstp_port->DataLength, mstp_port->SourceAddress,
&Transmit_Packet.pdu[0], Transmit_Packet.pdu_len,
&Transmit_Packet.address);
if (!matched)
matched = dlmstp_compare_data_expecting_reply(
&mstp_port->InputBuffer[0], mstp_port->DataLength,
mstp_port->SourceAddress, &Transmit_Packet.pdu[0],
Transmit_Packet.pdu_len, &Transmit_Packet.address);
if (!matched) {
return 0;
}
/* convert the PDU into the MSTP Frame */
pdu_len =
MSTP_Create_Frame(&mstp_port->OutputBuffer[0], /* <-- loading this */
mstp_port->OutputBufferSize, Transmit_Packet.frame_type,
destination, mstp_port->This_Station, &Transmit_Packet.pdu[0],
Transmit_Packet.pdu_len);
pdu_len = MSTP_Create_Frame(
&mstp_port->OutputBuffer[0], /* <-- loading this */
mstp_port->OutputBufferSize, Transmit_Packet.frame_type, destination,
mstp_port->This_Station, &Transmit_Packet.pdu[0],
Transmit_Packet.pdu_len);
Transmit_Packet.ready = false;
return pdu_len;
@@ -440,8 +450,9 @@ void dlmstp_set_mac_address(uint8_t mac_address)
EEPROM_DEVICE_ADDRESS,
mac_address,
EEPROM_MSTP_MAC_ADDR); */
if (mac_address > MSTP_Port.Nmax_master)
if (mac_address > MSTP_Port.Nmax_master) {
dlmstp_set_max_master(mac_address);
}
}
return;
@@ -556,8 +567,9 @@ bool dlmstp_init(char *ifname)
Receive_Packet.ready = false;
Receive_Packet.pdu_len = 0;
Receive_Packet_Flag = CreateSemaphore(NULL, 0, 1, "dlmstpReceivePacket");
if (Receive_Packet_Flag == NULL)
if (Receive_Packet_Flag == NULL) {
exit(1);
}
Received_Frame_Flag = CreateSemaphore(NULL, 0, 1, "dlsmtpReceiveFrame");
if (Received_Frame_Flag == NULL) {
CloseHandle(Receive_Packet_Flag);
@@ -608,7 +620,8 @@ bool dlmstp_init(char *ifname)
#if PRINT_ENABLED
fprintf(stderr, "MS/TP MAC: %02X\n", MSTP_Port.This_Station);
fprintf(stderr, "MS/TP Max_Master: %02X\n", MSTP_Port.Nmax_master);
fprintf(stderr, "MS/TP Max_Info_Frames: %u\n",
fprintf(
stderr, "MS/TP Max_Info_Frames: %u\n",
(unsigned)MSTP_Port.Nmax_info_frames);
#endif
hThread = _beginthread(dlmstp_receive_fsm_task, 4096, &arg_value);
@@ -626,7 +639,8 @@ bool dlmstp_init(char *ifname)
#ifdef TEST_DLMSTP
#include <stdio.h>
void apdu_handler(BACNET_ADDRESS *src, /* source address */
void apdu_handler(
BACNET_ADDRESS *src, /* source address */
uint8_t *apdu, /* APDU data */
uint16_t pdu_len)
{ /* for confirmed messages */
+14 -7
View File
@@ -110,8 +110,9 @@ bool ethernet_init(char *if_name)
int i;
char msg[200];
if (ethernet_valid())
if (ethernet_valid()) {
ethernet_cleanup();
}
/**
* Find the interface user specified
@@ -126,8 +127,9 @@ bool ethernet_init(char *if_name)
}
/* Scan the list printing every entry */
for (dev = pcap_all_if; dev; dev = dev->next) {
if (strcmp(if_name, dev->name) == 0)
if (strcmp(if_name, dev->name) == 0) {
break;
}
}
pcap_freealldevs(pcap_all_if); /* we don't need it anymore */
if (dev == NULL) {
@@ -161,8 +163,9 @@ bool ethernet_init(char *if_name)
LogError("ethernet.c: error in PacketRequest()\n");
return false;
}
for (i = 0; i < 6; ++i)
for (i = 0; i < 6; ++i) {
Ethernet_MAC_Address[i] = pOidData->Data[i];
}
PacketCloseAdapter(lpAdapter);
/**
@@ -317,11 +320,13 @@ uint16_t ethernet_receive(
msg, sizeof(), "ethernet.c: error in receiving packet: %s\n",
pcap_geterr(pcap_eth802_fp));
return 0;
} else if (res == 0)
} else if (res == 0) {
return 0;
}
if (header->len == 0 || header->caplen == 0)
if (header->len == 0 || header->caplen == 0) {
return 0;
}
/* the signature of an 802.2 BACnet packet */
if ((pkt_data[14] != 0x82) && (pkt_data[15] != 0x82)) {
@@ -343,11 +348,13 @@ uint16_t ethernet_receive(
(void)decode_unsigned16(&pkt_data[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], &pkt_data[17], pdu_len);
}
/* ignore packets that are too large */
else
else {
pdu_len = 0;
}
return pdu_len;
}
+28 -18
View File
@@ -42,14 +42,17 @@
#include "bacnet/basic/object/bacfile.h"
#endif
/** Static receive buffer, initialized with zeros by the C Library Startup Code. */
/** Static receive buffer, initialized with zeros by the C Library Startup Code.
*/
static uint8_t Rx_Buf[MAX_MPDU + 16 /* Add a little safety margin to the buffer,
* so that in the rare case, the message
* would be filled up to MAX_MPDU and some
* decoding functions would overrun, these
* decoding functions will just end up in
* a safe field of static zeros. */] = { 0 };
static uint8_t Rx_Buf
[MAX_MPDU + 16 /* Add a little safety margin to the buffer,
* so that in the rare case, the message
* would be filled up to MAX_MPDU and some
* decoding functions would overrun, these
* decoding functions will just end up in
* a safe field of static zeros. */
] = { 0 };
/* send a whois to see who is on the network */
static bool Who_Is_Request = true;
@@ -68,9 +71,10 @@ static void Read_Properties(void)
Device Object
note: you could just loop through
all the properties in all the objects. */
const int object_props[] = { PROP_OBJECT_IDENTIFIER, PROP_OBJECT_NAME,
PROP_OBJECT_TYPE, PROP_SYSTEM_STATUS, PROP_VENDOR_NAME,
PROP_VENDOR_IDENTIFIER, PROP_MODEL_NAME, PROP_FIRMWARE_REVISION,
const int object_props[] = {
PROP_OBJECT_IDENTIFIER, PROP_OBJECT_NAME, PROP_OBJECT_TYPE,
PROP_SYSTEM_STATUS, PROP_VENDOR_NAME, PROP_VENDOR_IDENTIFIER,
PROP_MODEL_NAME, PROP_FIRMWARE_REVISION,
PROP_APPLICATION_SOFTWARE_VERSION, PROP_PROTOCOL_VERSION,
PROP_PROTOCOL_SERVICES_SUPPORTED, PROP_PROTOCOL_OBJECT_TYPES_SUPPORTED,
PROP_MAX_APDU_LENGTH_ACCEPTED, PROP_SEGMENTATION_SUPPORTED,
@@ -85,27 +89,31 @@ static void Read_Properties(void)
/* some proprietary properties */
514, 515,
/* end of list */
-1 };
-1
};
if (address_count()) {
if (address_get_by_index(index, &device_id, &max_apdu, &src)) {
if (object_props[property] < 0)
if (object_props[property] < 0) {
next_device = true;
else {
} else {
status = Send_Read_Property_Request(
device_id, /* destination device */
OBJECT_DEVICE, device_id, object_props[property],
BACNET_ARRAY_ALL);
if (status)
if (status) {
property++;
}
}
} else
} else {
next_device = true;
}
if (next_device) {
next_device = false;
index++;
if (index >= MAX_ADDRESS_CACHE)
if (index >= MAX_ADDRESS_CACHE) {
index = 0;
}
property = 0;
}
}
@@ -130,8 +138,9 @@ static void LocalIAmHandler(
if (len != -1) {
fprintf(stderr, " from %u!\n", device_id);
address_add(device_id, max_apdu, src);
} else
} else {
fprintf(stderr, "!\n");
}
return;
}
@@ -261,8 +270,9 @@ int main(int argc, char *argv[])
/* blink LEDs, Turn on or off outputs, etc */
/* wait for ESC from keyboard before quitting */
if (kbhit() && (getch() == 0x1B))
if (kbhit() && (getch() == 0x1B)) {
break;
}
}
print_address_cache();
+2 -1
View File
@@ -49,7 +49,8 @@ void mstimer_init(void)
/* configure for 1ms resolution - if possible */
Timer_Period = min(max(tc.wPeriodMin, 1L), tc.wPeriodMax);
if (Timer_Period != 1L) {
fprintf(stderr,
fprintf(
stderr,
"Failed to set timer to 1ms. "
"Time period set to %ums\n",
(unsigned)Timer_Period);
+13 -10
View File
@@ -137,8 +137,9 @@ void RS485_Print_Error(void)
{
LPVOID lpMsgBuf;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL,
GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)&lpMsgBuf, 0, NULL);
MessageBox(NULL, lpMsgBuf, "GetLastError", MB_OK | MB_ICONINFORMATION);
LocalFree(lpMsgBuf);
@@ -237,8 +238,8 @@ static void RS485_Cleanup(void)
*****************************************************************************/
void RS485_Initialize(void)
{
RS485_Handle = CreateFile(RS485_Port_Name, GENERIC_READ | GENERIC_WRITE, 0,
0, OPEN_EXISTING,
RS485_Handle = CreateFile(
RS485_Port_Name, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING,
/*FILE_FLAG_OVERLAPPED */ 0, 0);
if (RS485_Handle == INVALID_HANDLE_VALUE) {
fprintf(stderr, "RS485 unable to open %s\n", RS485_Port_Name);
@@ -397,7 +398,7 @@ bool RS485_Set_Baud_Rate(uint32_t baud)
/* Transmits a Frame on the wire */
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) */
const uint8_t *buffer, /* frame to send (up to 501 bytes of data) */
uint16_t nbytes)
{ /* number of bytes of data (up to 501) */
DWORD dwWritten = 0;
@@ -407,12 +408,13 @@ void RS485_Send_Frame(
uint8_t turnaround_time;
baud = RS485_Get_Baud_Rate();
/* wait about 40 bit times since reception */
if (baud == 9600)
if (baud == 9600) {
turnaround_time = 4;
else if (baud == 19200)
} else if (baud == 19200) {
turnaround_time = 2;
else
} else {
turnaround_time = 2;
}
while (mstp_port->SilenceTimer(NULL) < turnaround_time) {
/* do nothing - wait for timer to increment */
};
@@ -465,8 +467,9 @@ void RS485_Print_Ports(void)
for (i = 1; i < 256; i++) {
if (RS485_Interface_Valid(i)) {
/* note: format for Wireshark ExtCap */
printf("interface {value=COM%u}"
"{display=BACnet MS/TP on COM%u}\n",
printf(
"interface {value=COM%u}"
"{display=BACnet MS/TP on COM%u}\n",
i, i);
}
}
+24 -33
View File
@@ -16,45 +16,36 @@
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
void RS485_Check_UART_Data(
struct mstp_port_struct_t *mstp_port); /* port specific data */
BACNET_STACK_EXPORT
uint32_t RS485_Get_Baud_Rate(
void);
BACNET_STACK_EXPORT
bool RS485_Set_Baud_Rate(
uint32_t baud);
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_Print_Error(
void);
BACNET_STACK_EXPORT
bool RS485_Interface_Valid(
unsigned port_number);
BACNET_STACK_EXPORT
void RS485_Print_Ports(
void);
BACNET_STACK_EXPORT
void RS485_Print_Error(void);
BACNET_STACK_EXPORT
bool RS485_Interface_Valid(unsigned port_number);
BACNET_STACK_EXPORT
void RS485_Print_Ports(void);
#ifdef __cplusplus
}