Indented.
This commit is contained in:
@@ -15,4 +15,4 @@ typedef signed long int32_t; /* 4 bytes -2147483647 to 2147483647 */
|
||||
/* typedef signed long long int64_t; */
|
||||
/* typedef unsigned long long uint64_t; */
|
||||
|
||||
#endif /* STDINT_H */
|
||||
#endif /* STDINT_H */
|
||||
|
||||
+105
-93
@@ -32,12 +32,12 @@
|
||||
-------------------------------------------
|
||||
####COPYRIGHTEND####*/
|
||||
|
||||
#include <stdint.h> /* for standard integer types uint8_t etc. */
|
||||
#include <stdbool.h> /* for the standard bool type. */
|
||||
#include <stdint.h> /* for standard integer types uint8_t etc. */
|
||||
#include <stdbool.h> /* for the standard bool type. */
|
||||
#include "bacdcode.h"
|
||||
#include "bip.h"
|
||||
#include "eth.h"
|
||||
#include "net.h" /* custom per port */
|
||||
#include "net.h" /* custom per port */
|
||||
|
||||
static int BIP_Socket = -1;
|
||||
/* port to use - stored in host byte order */
|
||||
@@ -47,22 +47,26 @@ static struct in_addr BIP_Address;
|
||||
/* Broadcast Address - stored in host byte order */
|
||||
static struct in_addr BIP_Broadcast_Address;
|
||||
|
||||
void bip_set_socket(int sock_fd)
|
||||
void bip_set_socket(
|
||||
int sock_fd)
|
||||
{
|
||||
BIP_Socket = sock_fd;
|
||||
}
|
||||
|
||||
int bip_socket(void)
|
||||
int bip_socket(
|
||||
void)
|
||||
{
|
||||
return BIP_Socket;
|
||||
}
|
||||
|
||||
bool bip_valid(void)
|
||||
bool bip_valid(
|
||||
void)
|
||||
{
|
||||
return (BIP_Socket != -1);
|
||||
}
|
||||
|
||||
void bip_cleanup(void)
|
||||
void bip_cleanup(
|
||||
void)
|
||||
{
|
||||
/* if (bip_valid()) */
|
||||
/* close(BIP_Socket); */
|
||||
@@ -72,142 +76,149 @@ void bip_cleanup(void)
|
||||
}
|
||||
|
||||
/* set using network byte order */
|
||||
void bip_set_addr(uint32_t net_address)
|
||||
void bip_set_addr(
|
||||
uint32_t net_address)
|
||||
{
|
||||
/* BIP_Address.s_addr = ntohl(net_address); */
|
||||
BIP_Address.s_addr = net_address;
|
||||
}
|
||||
|
||||
/* returns host byte order */
|
||||
uint32_t bip_get_addr(void)
|
||||
uint32_t bip_get_addr(
|
||||
void)
|
||||
{
|
||||
return BIP_Address.s_addr;
|
||||
}
|
||||
|
||||
/* set using network byte order */
|
||||
void bip_set_broadcast_addr(uint32_t net_address)
|
||||
void bip_set_broadcast_addr(
|
||||
uint32_t net_address)
|
||||
{
|
||||
/* BIP_Broadcast_Address.s_addr = ntohl(net_address); */
|
||||
BIP_Broadcast_Address.s_addr = net_address;
|
||||
}
|
||||
|
||||
/* returns host byte order */
|
||||
uint32_t bip_get_broadcast_addr(void)
|
||||
uint32_t bip_get_broadcast_addr(
|
||||
void)
|
||||
{
|
||||
return BIP_Broadcast_Address.s_addr;
|
||||
}
|
||||
|
||||
/* set using host byte order */
|
||||
void bip_set_port(uint16_t port)
|
||||
void bip_set_port(
|
||||
uint16_t port)
|
||||
{
|
||||
BIP_Port = port;
|
||||
}
|
||||
|
||||
/* returns host byte order */
|
||||
uint16_t bip_get_port(void)
|
||||
uint16_t bip_get_port(
|
||||
void)
|
||||
{
|
||||
return BIP_Port;
|
||||
}
|
||||
|
||||
/* function to send a packet out the BACnet/IP socket (Annex J) */
|
||||
/* returns number of bytes sent on success, negative number on failure */
|
||||
int bip_send_pdu(BACNET_ADDRESS * dest, /* destination address */
|
||||
int bip_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 */
|
||||
uint8_t * pdu, /* any data to be sent - may be null */
|
||||
unsigned pdu_len)
|
||||
{ /* number of bytes of data */
|
||||
{ /* number of bytes of data */
|
||||
|
||||
struct sockaddr_in bip_dest;
|
||||
uint8_t mtu[4];
|
||||
int mtu_len = 0;
|
||||
int bytes_sent = 0;
|
||||
UDP_HDR udphdr;
|
||||
IP_HDR iphdr;
|
||||
uint8_t mac[6];
|
||||
struct sockaddr_in bip_dest;
|
||||
uint8_t mtu[4];
|
||||
int mtu_len = 0;
|
||||
int bytes_sent = 0;
|
||||
UDP_HDR udphdr;
|
||||
IP_HDR iphdr;
|
||||
uint8_t mac[6];
|
||||
|
||||
(void) npdu_data;
|
||||
|
||||
/* assumes that the driver has already been initialized */
|
||||
(void) npdu_data;
|
||||
|
||||
/* assumes that the driver has already been initialized */
|
||||
/* if (BIP_Socket < 0) */
|
||||
/* return BIP_Socket; */
|
||||
|
||||
mtu[0] = BVLL_TYPE_BACNET_IP;
|
||||
bip_dest.sin_family = AF_INET;
|
||||
|
||||
if (dest->mac_len == 6)
|
||||
{
|
||||
memcpy(&(bip_dest.sin_addr.s_addr),&dest->mac[0],4);
|
||||
decode_unsigned16(&dest->mac[4], &(bip_dest.sin_port));
|
||||
memset(&(bip_dest.sin_zero), '\0', 8);
|
||||
mtu[1] = BVLC_ORIGINAL_UNICAST_NPDU;
|
||||
}
|
||||
/* broadcast */
|
||||
else if (dest->mac_len == 0)
|
||||
{
|
||||
bip_dest.sin_addr.s_addr = BIP_Broadcast_Address.s_addr;
|
||||
bip_dest.sin_port = htons(BIP_Port);
|
||||
memset(&(bip_dest.sin_zero), '\0', 8);
|
||||
mtu[1] = BVLC_ORIGINAL_BROADCAST_NPDU;
|
||||
}
|
||||
else
|
||||
return -1;
|
||||
|
||||
mtu_len = 2;
|
||||
mtu_len += encode_unsigned16(&mtu[mtu_len], (uint16_t) (pdu_len + 4 /*inclusive */ ));
|
||||
mtu_len += pdu_len;
|
||||
|
||||
/* IP address should be in network byte order */
|
||||
ARPIsResolved(bip_dest.sin_addr.s_addr, mac);
|
||||
mtu[0] = BVLL_TYPE_BACNET_IP;
|
||||
bip_dest.sin_family = AF_INET;
|
||||
|
||||
iphdr.vhl = 0x45;
|
||||
iphdr.tos = 0;
|
||||
iphdr.iplen = htons(mtu_len + sizeof(IP_HDR) + sizeof(UDP_HDR));
|
||||
++ipid;
|
||||
iphdr.ipid = htons(ipid);
|
||||
iphdr.ipoffset[0] = iphdr.ipoffset[1] = 0;
|
||||
iphdr.ttl = UIP_TTL;
|
||||
iphdr.proto = UIP_PROTO_UDP;
|
||||
iphdr.ipchksum = 0;
|
||||
iphdr.srcipaddr = BIP_Address.s_addr;
|
||||
iphdr.destipaddr = bip_dest.sin_addr.s_addr;
|
||||
|
||||
/* Calculate IP checksum. */
|
||||
iphdr.ipchksum = ~(ip_getchksum((uint8_t*)&iphdr));
|
||||
if (dest->mac_len == 6) {
|
||||
memcpy(&(bip_dest.sin_addr.s_addr), &dest->mac[0], 4);
|
||||
decode_unsigned16(&dest->mac[4], &(bip_dest.sin_port));
|
||||
memset(&(bip_dest.sin_zero), '\0', 8);
|
||||
mtu[1] = BVLC_ORIGINAL_UNICAST_NPDU;
|
||||
}
|
||||
/* broadcast */
|
||||
else if (dest->mac_len == 0) {
|
||||
bip_dest.sin_addr.s_addr = BIP_Broadcast_Address.s_addr;
|
||||
bip_dest.sin_port = htons(BIP_Port);
|
||||
memset(&(bip_dest.sin_zero), '\0', 8);
|
||||
mtu[1] = BVLC_ORIGINAL_BROADCAST_NPDU;
|
||||
} else
|
||||
return -1;
|
||||
|
||||
/* Ports are sent in Network byte order. BIP_Port is stored in Host byte order */
|
||||
udphdr.srcport = htons(BIP_Port);
|
||||
udphdr.destport = bip_dest.sin_port;
|
||||
udphdr.udplen = htons(mtu_len + sizeof(UDP_HDR));
|
||||
/* Not using UDP checksums */
|
||||
udphdr.udpchksum = 0;
|
||||
mtu_len = 2;
|
||||
mtu_len +=
|
||||
encode_unsigned16(&mtu[mtu_len],
|
||||
(uint16_t) (pdu_len + 4 /*inclusive */ ));
|
||||
mtu_len += pdu_len;
|
||||
|
||||
/* IP address should be in network byte order */
|
||||
ARPIsResolved(bip_dest.sin_addr.s_addr, mac);
|
||||
|
||||
iphdr.vhl = 0x45;
|
||||
iphdr.tos = 0;
|
||||
iphdr.iplen = htons(mtu_len + sizeof(IP_HDR) + sizeof(UDP_HDR));
|
||||
++ipid;
|
||||
iphdr.ipid = htons(ipid);
|
||||
iphdr.ipoffset[0] = iphdr.ipoffset[1] = 0;
|
||||
iphdr.ttl = UIP_TTL;
|
||||
iphdr.proto = UIP_PROTO_UDP;
|
||||
iphdr.ipchksum = 0;
|
||||
iphdr.srcipaddr = BIP_Address.s_addr;
|
||||
iphdr.destipaddr = bip_dest.sin_addr.s_addr;
|
||||
|
||||
/* Calculate IP checksum. */
|
||||
iphdr.ipchksum = ~(ip_getchksum((uint8_t *) & iphdr));
|
||||
|
||||
/* Ports are sent in Network byte order. BIP_Port is stored in Host byte order */
|
||||
udphdr.srcport = htons(BIP_Port);
|
||||
udphdr.destport = bip_dest.sin_port;
|
||||
udphdr.udplen = htons(mtu_len + sizeof(UDP_HDR));
|
||||
/* Not using UDP checksums */
|
||||
udphdr.udpchksum = 0;
|
||||
|
||||
EthernetSendHeader(mac, UIP_ETHTYPE_IP);
|
||||
EthernetSend((uint8_t *) & iphdr, sizeof(IP_HDR));
|
||||
EthernetSend((uint8_t *) & udphdr, sizeof(UDP_HDR));
|
||||
EthernetSend(mtu, 4);
|
||||
EthernetSend(pdu, pdu_len);
|
||||
EthernetFlush();
|
||||
uip_stat.ip.sent++;
|
||||
|
||||
EthernetSendHeader(mac, UIP_ETHTYPE_IP);
|
||||
EthernetSend((uint8_t*)&iphdr, sizeof(IP_HDR));
|
||||
EthernetSend((uint8_t*)&udphdr, sizeof(UDP_HDR));
|
||||
EthernetSend(mtu, 4);
|
||||
EthernetSend(pdu, pdu_len);
|
||||
EthernetFlush();
|
||||
uip_stat.ip.sent++;
|
||||
|
||||
}
|
||||
|
||||
/* receives a BACnet/IP packet */
|
||||
/* returns the number of octets in the PDU, or zero on failure */
|
||||
uint16_t bip_receive(BACNET_ADDRESS * src, /* source address */
|
||||
uint8_t * pdu, /* PDU data */
|
||||
uint16_t max_pdu, /* amount of space available in the PDU */
|
||||
uint16_t bip_receive(
|
||||
BACNET_ADDRESS * src, /* source address */
|
||||
uint8_t * pdu, /* PDU data */
|
||||
uint16_t max_pdu, /* amount of space available in the PDU */
|
||||
unsigned timeout)
|
||||
{ /* number of milliseconds to wait for a packet */
|
||||
return 0;
|
||||
{ /* number of milliseconds to wait for a packet */
|
||||
return 0;
|
||||
}
|
||||
|
||||
void bip_get_my_address(BACNET_ADDRESS * my_address)
|
||||
void bip_get_my_address(
|
||||
BACNET_ADDRESS * my_address)
|
||||
{
|
||||
int i = 0;
|
||||
|
||||
my_address->mac_len = 6;
|
||||
(void) encode_unsigned32(&my_address->mac[0],
|
||||
htonl(BIP_Address.s_addr));
|
||||
(void) encode_unsigned32(&my_address->mac[0], htonl(BIP_Address.s_addr));
|
||||
(void) encode_unsigned16(&my_address->mac[4], htons(BIP_Port));
|
||||
my_address->net = 0; /* local only, no routing */
|
||||
my_address->len = 0; /* no SLEN */
|
||||
@@ -219,9 +230,10 @@ void bip_get_my_address(BACNET_ADDRESS * my_address)
|
||||
return;
|
||||
}
|
||||
|
||||
void bip_get_broadcast_address(BACNET_ADDRESS * dest)
|
||||
{ /* destination address */
|
||||
int i = 0; /* counter */
|
||||
void bip_get_broadcast_address(
|
||||
BACNET_ADDRESS * dest)
|
||||
{ /* destination address */
|
||||
int i = 0; /* counter */
|
||||
|
||||
if (dest) {
|
||||
dest->mac_len = 6;
|
||||
@@ -229,7 +241,7 @@ void bip_get_broadcast_address(BACNET_ADDRESS * dest)
|
||||
htonl(BIP_Broadcast_Address.s_addr));
|
||||
(void) encode_unsigned16(&dest->mac[4], htons(BIP_Port));
|
||||
dest->net = BACNET_BROADCAST_NETWORK;
|
||||
dest->len = 0; /* no SLEN */
|
||||
dest->len = 0; /* no SLEN */
|
||||
for (i = 0; i < MAX_MAC_LEN; i++) {
|
||||
/* no SADR */
|
||||
dest->adr[i] = 0;
|
||||
|
||||
@@ -29,21 +29,20 @@
|
||||
#include "stdint.h"
|
||||
|
||||
struct sockaddr {
|
||||
uint16_t sa_family;
|
||||
char sa_data[14];
|
||||
uint16_t sa_family;
|
||||
char sa_data[14];
|
||||
};
|
||||
|
||||
|
||||
struct in_addr {
|
||||
uint32_t s_addr; /* load with inet_aton() */
|
||||
uint32_t s_addr; /* load with inet_aton() */
|
||||
};
|
||||
|
||||
struct sockaddr_in
|
||||
{
|
||||
int16_t sin_family; /* e.g. AF_INET */
|
||||
uint16_t sin_port; /* e.g. htons(3490) */
|
||||
struct in_addr sin_addr; /* see struct in_addr, below */
|
||||
char sin_zero[8]; /* zero this if you want to */
|
||||
struct sockaddr_in {
|
||||
int16_t sin_family; /* e.g. AF_INET */
|
||||
uint16_t sin_port; /* e.g. htons(3490) */
|
||||
struct in_addr sin_addr; /* see struct in_addr, below */
|
||||
char sin_zero[8]; /* zero this if you want to */
|
||||
};
|
||||
|
||||
|
||||
@@ -74,42 +73,47 @@ typedef int socklen_t;
|
||||
((((uint32_t)(n) & 0xFF000000)) >> 24))
|
||||
|
||||
|
||||
#define AF_UNIX 1 /* local to host (pipes, portals) */
|
||||
#define AF_INET 2 /* internetwork: UDP, TCP, etc. */
|
||||
#define AF_IMPLINK 3 /* arpanet imp addresses */
|
||||
#define AF_PUP 4 /* pup protocols: e.g. BSP */
|
||||
#define AF_CHAOS 5 /* mit CHAOS protocols */
|
||||
#define AF_NS 6 /* XEROX NS protocols */
|
||||
#define AF_IPX AF_NS /* IPX protocols: IPX, SPX, etc. */
|
||||
#define AF_ISO 7 /* ISO protocols */
|
||||
#define AF_OSI AF_ISO /* OSI is ISO */
|
||||
#define AF_ECMA 8 /* european computer manufacturers */
|
||||
#define AF_DATAKIT 9 /* datakit protocols */
|
||||
#define AF_CCITT 10 /* CCITT protocols, X.25 etc */
|
||||
#define AF_SNA 11 /* IBM SNA */
|
||||
#define AF_DECnet 12 /* DECnet */
|
||||
#define AF_DLI 13 /* Direct data link interface */
|
||||
#define AF_LAT 14 /* LAT */
|
||||
#define AF_HYLINK 15 /* NSC Hyperchannel */
|
||||
#define AF_APPLETALK 16 /* AppleTalk */
|
||||
#define AF_NETBIOS 17 /* NetBios-style addresses */
|
||||
#define AF_VOICEVIEW 18 /* VoiceView */
|
||||
#define AF_FIREFOX 19 /* Protocols from Firefox */
|
||||
#define AF_UNKNOWN1 20 /* Somebody is using this! */
|
||||
#define AF_BAN 21 /* Banyan */
|
||||
#define AF_ATM 22 /* Native ATM Services */
|
||||
#define AF_INET6 23 /* Internetwork Version 6 */
|
||||
#define AF_CLUSTER 24 /* Microsoft Wolfpack */
|
||||
#define AF_12844 25 /* IEEE 1284.4 WG AF */
|
||||
#define AF_IRDA 26 /* IrDA */
|
||||
#define AF_NETDES 28 /* Network Designers OSI & gateway
|
||||
enabled protocols */
|
||||
#define AF_UNIX 1 /* local to host (pipes, portals) */
|
||||
#define AF_INET 2 /* internetwork: UDP, TCP, etc. */
|
||||
#define AF_IMPLINK 3 /* arpanet imp addresses */
|
||||
#define AF_PUP 4 /* pup protocols: e.g. BSP */
|
||||
#define AF_CHAOS 5 /* mit CHAOS protocols */
|
||||
#define AF_NS 6 /* XEROX NS protocols */
|
||||
#define AF_IPX AF_NS /* IPX protocols: IPX, SPX, etc. */
|
||||
#define AF_ISO 7 /* ISO protocols */
|
||||
#define AF_OSI AF_ISO /* OSI is ISO */
|
||||
#define AF_ECMA 8 /* european computer manufacturers */
|
||||
#define AF_DATAKIT 9 /* datakit protocols */
|
||||
#define AF_CCITT 10 /* CCITT protocols, X.25 etc */
|
||||
#define AF_SNA 11 /* IBM SNA */
|
||||
#define AF_DECnet 12 /* DECnet */
|
||||
#define AF_DLI 13 /* Direct data link interface */
|
||||
#define AF_LAT 14 /* LAT */
|
||||
#define AF_HYLINK 15 /* NSC Hyperchannel */
|
||||
#define AF_APPLETALK 16 /* AppleTalk */
|
||||
#define AF_NETBIOS 17 /* NetBios-style addresses */
|
||||
#define AF_VOICEVIEW 18 /* VoiceView */
|
||||
#define AF_FIREFOX 19 /* Protocols from Firefox */
|
||||
#define AF_UNKNOWN1 20 /* Somebody is using this! */
|
||||
#define AF_BAN 21 /* Banyan */
|
||||
#define AF_ATM 22 /* Native ATM Services */
|
||||
#define AF_INET6 23 /* Internetwork Version 6 */
|
||||
#define AF_CLUSTER 24 /* Microsoft Wolfpack */
|
||||
#define AF_12844 25 /* IEEE 1284.4 WG AF */
|
||||
#define AF_IRDA 26 /* IrDA */
|
||||
#define AF_NETDES 28 /* Network Designers OSI & gateway
|
||||
enabled protocols */
|
||||
#define AF_TCNPROCESS 29
|
||||
#define AF_TCNMESSAGE 30
|
||||
#define AF_ICLFXBM 31
|
||||
|
||||
#define AF_MAX 32
|
||||
|
||||
extern void set_address(uint32_t *net_address, uint8_t octet1, uint8_t octet2, uint8_t octet3, uint8_t octet4);
|
||||
extern void set_address(
|
||||
uint32_t * net_address,
|
||||
uint8_t octet1,
|
||||
uint8_t octet2,
|
||||
uint8_t octet3,
|
||||
uint8_t octet4);
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user