diff --git a/bacnet-stack/ports/arm7/_stdint.h b/bacnet-stack/ports/arm7/_stdint.h new file mode 100644 index 00000000..3bd54ae8 --- /dev/null +++ b/bacnet-stack/ports/arm7/_stdint.h @@ -0,0 +1,19 @@ +/* Defines the standard integer types that are used in code */ +/* for the x86 processor and Borland Compiler */ + +#ifndef STDINT_H +#define STDINT_H + +#include + +typedef unsigned char uint8_t; /* 1 byte 0 to 255 */ +typedef signed char int8_t; /* 1 byte -127 to 127 */ +typedef unsigned short uint16_t; /* 2 bytes 0 to 65535 */ +typedef signed short int16_t; /* 2 bytes -32767 to 32767 */ +/*typedef unsigned short long uint24_t; // 3 bytes 0 to 16777215 */ +typedef unsigned long uint32_t; /* 4 bytes 0 to 4294967295 */ +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 */ diff --git a/bacnet-stack/ports/arm7/bip.c b/bacnet-stack/ports/arm7/bip.c new file mode 100644 index 00000000..2f603f2d --- /dev/null +++ b/bacnet-stack/ports/arm7/bip.c @@ -0,0 +1,240 @@ +/*####COPYRIGHTBEGIN#### + ------------------------------------------- + Copyright (C) 2005 Steve Karg + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License + as published by the Free Software Foundation; either version 2 + of the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to: + The Free Software Foundation, Inc. + 59 Temple Place - Suite 330 + Boston, MA 02111-1307, USA. + + As a special exception, if other files instantiate templates or + use macros or inline functions from this file, or you compile + this file and link it with other works to produce a work based + on this file, this file does not by itself cause the resulting + work to be covered by the GNU General Public License. However + the source code for this file must still be made available in + accordance with section (3) of the GNU General Public License. + + This exception does not invalidate any other reasons why a work + based on this file might be covered by the GNU General Public + License. + ------------------------------------------- +####COPYRIGHTEND####*/ + +#include /* for standard integer types uint8_t etc. */ +#include /* for the standard bool type. */ +#include "bacdcode.h" +#include "bip.h" +#include "eth.h" +#include "net.h" /* custom per port */ + +static int BIP_Socket = -1; +/* port to use - stored in host byte order */ +static uint16_t BIP_Port = 0xBAC0; +/* IP Address - stored in host byte order */ +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) +{ + BIP_Socket = sock_fd; +} + +int bip_socket(void) +{ + return BIP_Socket; +} + +bool bip_valid(void) +{ + return (BIP_Socket != -1); +} + +void bip_cleanup(void) +{ +// if (bip_valid()) +// close(BIP_Socket); + BIP_Socket = -1; + + return; +} + +/* set using network byte order */ +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) +{ + return BIP_Address.s_addr; +} + +/* set using network byte order */ +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) +{ + return BIP_Broadcast_Address.s_addr; +} + +/* set using host byte order */ +void bip_set_port(uint16_t port) +{ + BIP_Port = port; +} + +/* returns host byte order */ +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 */ + BACNET_NPDU_DATA * npdu_data, /* network information */ + uint8_t * pdu, /* any data to be sent - may be null */ + unsigned pdu_len) +{ /* 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]; + + (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); + + 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++; + +} + +/* 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 */ + unsigned timeout) +{ /* number of milliseconds to wait for a packet */ + return 0; +} + +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_unsigned16(&my_address->mac[4], htons(BIP_Port)); + my_address->net = 0; /* local only, no routing */ + my_address->len = 0; /* no SLEN */ + for (i = 0; i < MAX_MAC_LEN; i++) { + /* no SADR */ + my_address->adr[i] = 0; + } + + return; +} + +void bip_get_broadcast_address(BACNET_ADDRESS * dest) +{ /* destination address */ + int i = 0; /* counter */ + + if (dest) { + dest->mac_len = 6; + (void) encode_unsigned32(&dest->mac[0], + 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 */ + for (i = 0; i < MAX_MAC_LEN; i++) { + /* no SADR */ + dest->adr[i] = 0; + } + } + + return; +} diff --git a/bacnet-stack/ports/arm7/net.h b/bacnet-stack/ports/arm7/net.h new file mode 100644 index 00000000..f45fec7a --- /dev/null +++ b/bacnet-stack/ports/arm7/net.h @@ -0,0 +1,115 @@ +/************************************************************************** +* +* Copyright (C) 2005 Steve Karg +* +* 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. +* +*********************************************************************/ + +#ifndef NET_H +#define NET_H + +#include "stdint.h" + +struct sockaddr { + uint16_t sa_family; + char sa_data[14]; +}; + + +struct in_addr { + 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 +}; + + +typedef int socklen_t; + +/** + * Convert 16-bit quantity from host byte order to network byte order. + * + * This macro is primarily used for converting constants from host + * byte order to network byte order. For converting variables to + * network byte order, use the htons() function instead. + * + * \hideinitializer + */ +#ifndef htons +#define htons(n) ((((uint16_t)((n) & 0xff)) << 8) | (((n) & 0xff00) >> 8)) +#endif /* HTONS */ + +#define ntohs(n) (((((uint16_t)(n) & 0xFF)) << 8) | (((uint16_t)(n) & 0xFF00) >> 8)) +#define htonl(n) (((((uint32_t)(n) & 0xFF)) << 24) | \ + ((((uint32_t)(n) & 0xFF00)) << 8) | \ + ((((uint32_t)(n) & 0xFF0000)) >> 8) | \ + ((((uint32_t)(n) & 0xFF000000)) >> 24)) + +#define ntohl(n) (((((uint32_t)(n) & 0xFF)) << 24) | \ + ((((uint32_t)(n) & 0xFF00)) << 8) | \ + ((((uint32_t)(n) & 0xFF0000)) >> 8) | \ + ((((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_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); + +#endif diff --git a/bacnet-stack/ports/arm7/stdbool.h b/bacnet-stack/ports/arm7/stdbool.h new file mode 100644 index 00000000..4240bfe0 --- /dev/null +++ b/bacnet-stack/ports/arm7/stdbool.h @@ -0,0 +1,29 @@ +#ifndef STDBOOL_H +#define STDBOOL_H + +/* C99 Boolean types for compilers without C99 support */ + +#ifndef __cplusplus + +//typedef int _Bool; +#ifndef bool +#define bool _Bool +#endif +#ifndef true +#define true 1 +#endif +#ifndef false +#define false 0 +#endif +#define __bool_true_false_are_defined 1 +#endif + +#ifndef FALSE +#define FALSE 0 +#endif + +#ifndef TRUE +#define TRUE 1 +#endif + +#endif