added clang format C and H files.

This commit is contained in:
Steve Karg
2019-10-24 16:23:10 -05:00
parent da91a11454
commit 710173d6e0
205 changed files with 19377 additions and 25754 deletions
+111 -128
View File
@@ -1,43 +1,39 @@
/**
* @file
* @author Andriy Sukhynyuk, Vasyl Tkhir, Andriy Ivasiv
* @date 2012
* @brief Network layer for BACnet routing
*
* @section LICENSE
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
* @file
* @author Andriy Sukhynyuk, Vasyl Tkhir, Andriy Ivasiv
* @date 2012
* @brief Network layer for BACnet routing
*
* @section LICENSE
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "network_layer.h"
#include "bacint.h"
uint16_t process_network_message(
BACMSG * msg,
MSG_DATA * data,
uint8_t ** buff)
uint16_t process_network_message(BACMSG *msg, MSG_DATA *data, uint8_t **buff)
{
BACNET_NPDU_DATA npdu_data;
ROUTER_PORT *srcport;
ROUTER_PORT *destport;
@@ -56,7 +52,6 @@ uint16_t process_network_message(
data->src.net = srcport->route_info.net;
switch (npdu_data.network_message_type) {
case NETWORK_MESSAGE_WHO_IS_ROUTER_TO_NETWORK:
PRINT(INFO, "Recieved Who-Is-Router-To-Network message\n");
if (apdu_len) {
@@ -67,83 +62,83 @@ uint16_t process_network_message(
return -2;
}
destport = find_dnet(net, NULL); /* see if NET can be reached */
destport = find_dnet(net, NULL); /* see if NET can be reached */
if (destport) {
/* if TRUE send reply */
PRINT(INFO, "Sending I-Am-Router-To-Network message\n");
buff_len =
create_network_message
(NETWORK_MESSAGE_I_AM_ROUTER_TO_NETWORK, data, buff,
buff_len = create_network_message(
NETWORK_MESSAGE_I_AM_ROUTER_TO_NETWORK, data, buff,
&net);
} else {
data->dest.net = net; /* NET to look for */
return -1; /* else initiate NET search procedure */
data->dest.net = net; /* NET to look for */
return -1; /* else initiate NET search procedure */
}
} else {
/* if NET is omitted (message sent with -1) */
PRINT(INFO, "Sending I-Am-Router-To-Network message\n");
buff_len =
create_network_message
(NETWORK_MESSAGE_I_AM_ROUTER_TO_NETWORK, data, buff, NULL);
buff_len = create_network_message(
NETWORK_MESSAGE_I_AM_ROUTER_TO_NETWORK, data, buff, NULL);
}
break;
case NETWORK_MESSAGE_I_AM_ROUTER_TO_NETWORK:
{
PRINT(INFO, "Recieved I-Am-Router-To-Network message\n");
int net_count = apdu_len / 2;
int i;
for (i = 0; i < net_count; i++) {
decode_unsigned16(&data->pdu[apdu_offset + 2 * i], &net); /* decode received NET values */
add_dnet(&srcport->route_info, net, data->src); /* and update routing table */
}
break;
case NETWORK_MESSAGE_I_AM_ROUTER_TO_NETWORK: {
PRINT(INFO, "Recieved I-Am-Router-To-Network message\n");
int net_count = apdu_len / 2;
int i;
for (i = 0; i < net_count; i++) {
decode_unsigned16(&data->pdu[apdu_offset + 2 * i],
&net); /* decode received NET values */
add_dnet(&srcport->route_info, net,
data->src); /* and update routing table */
}
case NETWORK_MESSAGE_REJECT_MESSAGE_TO_NETWORK:
{
/* first octet of the message contains rejection reason */
/* next two octets contain NET (can be decoded for additional info on error) */
error_code = data->pdu[apdu_offset];
switch (error_code) {
case 0:
PRINT(ERROR, "Error!\n");
break;
case 1:
PRINT(ERROR, "Error: Network unreachable\n");
break;
case 2:
PRINT(ERROR, "Error: Network is busy\n");
break;
case 3:
PRINT(ERROR, "Error: Unknown network message type\n");
break;
case 4:
PRINT(ERROR, "Error: Message too long\n");
break;
}
break;
break;
}
case NETWORK_MESSAGE_REJECT_MESSAGE_TO_NETWORK: {
/* first octet of the message contains rejection reason */
/* next two octets contain NET (can be decoded for additional info
* on error) */
error_code = data->pdu[apdu_offset];
switch (error_code) {
case 0:
PRINT(ERROR, "Error!\n");
break;
case 1:
PRINT(ERROR, "Error: Network unreachable\n");
break;
case 2:
PRINT(ERROR, "Error: Network is busy\n");
break;
case 3:
PRINT(ERROR, "Error: Unknown network message type\n");
break;
case 4:
PRINT(ERROR, "Error: Message too long\n");
break;
}
break;
}
case NETWORK_MESSAGE_INIT_RT_TABLE:
PRINT(INFO, "Recieved Initialize-Routing-Table message\n");
if (data->pdu[apdu_offset] > 0) {
int net_count = data->pdu[apdu_offset];
while (net_count--) {
int i = 1;
decode_unsigned16(&data->pdu[apdu_offset + i], &net); /* decode received NET values */
add_dnet(&srcport->route_info, net, data->src); /* and update routing table */
if (data->pdu[apdu_offset + i + 3] > 0) /* find next NET value */
decode_unsigned16(&data->pdu[apdu_offset + i],
&net); /* decode received NET values */
add_dnet(&srcport->route_info, net,
data->src); /* and update routing table */
if (data->pdu[apdu_offset + i + 3] >
0) /* find next NET value */
i = data->pdu[apdu_offset + i + 3] + 4;
else
i = i + 4;
}
buff_len =
create_network_message(NETWORK_MESSAGE_INIT_RT_TABLE_ACK,
data, buff, NULL);
buff_len = create_network_message(
NETWORK_MESSAGE_INIT_RT_TABLE_ACK, data, buff, NULL);
} else
buff_len =
create_network_message(NETWORK_MESSAGE_INIT_RT_TABLE_ACK,
data, buff, &buff);
buff_len = create_network_message(
NETWORK_MESSAGE_INIT_RT_TABLE_ACK, data, buff, &buff);
break;
case NETWORK_MESSAGE_INIT_RT_TABLE_ACK:
@@ -152,9 +147,12 @@ uint16_t process_network_message(
int net_count = data->pdu[apdu_offset];
while (net_count--) {
int i = 1;
decode_unsigned16(&data->pdu[apdu_offset + i], &net); /* decode received NET values */
add_dnet(&srcport->route_info, net, data->src); /* and update routing table */
if (data->pdu[apdu_offset + i + 3] > 0) /* find next NET value */
decode_unsigned16(&data->pdu[apdu_offset + i],
&net); /* decode received NET values */
add_dnet(&srcport->route_info, net,
data->src); /* and update routing table */
if (data->pdu[apdu_offset + i + 3] >
0) /* find next NET value */
i = data->pdu[apdu_offset + i + 3] + 4;
else
i = i + 4;
@@ -182,12 +180,10 @@ uint16_t process_network_message(
/* security messages */
break;
case NETWORK_MESSAGE_WHAT_IS_NETWORK_NUMBER:
buff_len =
create_network_message(NETWORK_MESSAGE_NETWORK_NUMBER_IS,
data, buff, &buff);
buff_len = create_network_message(NETWORK_MESSAGE_NETWORK_NUMBER_IS,
data, buff, &buff);
break;
default:
PRINT(ERROR, "Error: Message unsupported\n");
break;
@@ -197,12 +193,9 @@ uint16_t process_network_message(
}
uint16_t create_network_message(
BACNET_NETWORK_MESSAGE_TYPE network_message_type,
MSG_DATA * data,
uint8_t ** buff,
void *val)
BACNET_NETWORK_MESSAGE_TYPE network_message_type, MSG_DATA *data,
uint8_t **buff, void *val)
{
int16_t buff_len;
bool data_expecting_reply = false;
BACNET_NPDU_DATA npdu_data;
@@ -211,17 +204,16 @@ uint16_t create_network_message(
data_expecting_reply = true;
init_npdu(&npdu_data, network_message_type, data_expecting_reply);
*buff = (uint8_t *) malloc(128); /* resolve different length */
*buff = (uint8_t *)malloc(128); /* resolve different length */
/* manual destination setup for Init-RT-Table-Ack message */
data->dest.net = BACNET_BROADCAST_NETWORK;
buff_len = npdu_encode_pdu(*buff, &data->dest, NULL, &npdu_data);
switch (network_message_type) {
case NETWORK_MESSAGE_WHO_IS_ROUTER_TO_NETWORK:
if (val != NULL) {
uint8_t *valptr = (uint8_t *) val;
uint8_t *valptr = (uint8_t *)val;
uint16_t val16 = (valptr[0]) + (valptr[1] << 8);
buff_len += encode_unsigned16(*buff + buff_len, val16);
}
@@ -229,7 +221,7 @@ uint16_t create_network_message(
case NETWORK_MESSAGE_I_AM_ROUTER_TO_NETWORK:
if (val != NULL) {
uint8_t *valptr = (uint8_t *) val;
uint8_t *valptr = (uint8_t *)val;
uint16_t val16 = (valptr[0]) + (valptr[1] << 8);
buff_len += encode_unsigned16(*buff + buff_len, val16);
} else {
@@ -237,9 +229,8 @@ uint16_t create_network_message(
DNET *dnet;
while (port != NULL) {
if (port->route_info.net != data->src.net) {
buff_len +=
encode_unsigned16(*buff + buff_len,
port->route_info.net);
buff_len += encode_unsigned16(*buff + buff_len,
port->route_info.net);
dnet = port->route_info.dnets;
while (dnet != NULL) {
buff_len +=
@@ -260,33 +251,31 @@ uint16_t create_network_message(
}
break;
case NETWORK_MESSAGE_REJECT_MESSAGE_TO_NETWORK:
{
uint8_t *valptr = (uint8_t *) val;
uint16_t val16 = (valptr[0]) + (valptr[1] << 8);
buff_len += encode_unsigned16(*buff + buff_len, val16);
break;
}
case NETWORK_MESSAGE_REJECT_MESSAGE_TO_NETWORK: {
uint8_t *valptr = (uint8_t *)val;
uint16_t val16 = (valptr[0]) + (valptr[1] << 8);
buff_len += encode_unsigned16(*buff + buff_len, val16);
break;
}
case NETWORK_MESSAGE_INIT_RT_TABLE:
case NETWORK_MESSAGE_INIT_RT_TABLE_ACK:
if ((uint8_t *) val) {
(*buff)[buff_len++] = (uint8_t) port_count;
if ((uint8_t *)val) {
(*buff)[buff_len++] = (uint8_t)port_count;
if (port_count > 0) {
ROUTER_PORT *port = head;
uint8_t portID = 1;
while (port != NULL) {
buff_len +=
encode_unsigned16(*buff + buff_len,
port->route_info.net);
buff_len += encode_unsigned16(*buff + buff_len,
port->route_info.net);
(*buff)[buff_len++] = portID++;
(*buff)[buff_len++] = 0;
port = port->next;
}
}
} else
(*buff)[buff_len++] = (uint8_t) 0;
(*buff)[buff_len++] = (uint8_t)0;
break;
case NETWORK_MESSAGE_INVALID:
@@ -317,19 +306,15 @@ uint16_t create_network_message(
return buff_len;
}
void send_network_message(
BACNET_NETWORK_MESSAGE_TYPE network_message_type,
MSG_DATA * data,
uint8_t ** buff,
void *val)
void send_network_message(BACNET_NETWORK_MESSAGE_TYPE network_message_type,
MSG_DATA *data, uint8_t **buff, void *val)
{
BACMSG msg;
ROUTER_PORT *port = head;
int16_t buff_len;
if (!data) {
data = (MSG_DATA *) malloc(sizeof(MSG_DATA));
data = (MSG_DATA *)malloc(sizeof(MSG_DATA));
data->dest.net = BACNET_BROADCAST_NETWORK;
data->dest.len = 0;
}
@@ -354,12 +339,10 @@ void send_network_message(
}
}
void init_npdu(
BACNET_NPDU_DATA * npdu_data,
BACNET_NETWORK_MESSAGE_TYPE network_message_type,
bool data_expecting_reply)
void init_npdu(BACNET_NPDU_DATA *npdu_data,
BACNET_NETWORK_MESSAGE_TYPE network_message_type,
bool data_expecting_reply)
{
if (npdu_data) {
npdu_data->data_expecting_reply = data_expecting_reply;
npdu_data->protocol_version = BACNET_PROTOCOL_VERSION;