running the indent on the files.
This commit is contained in:
@@ -37,174 +37,180 @@
|
||||
#include "bip.h"
|
||||
|
||||
#if (defined(BACDL_ETHERNET) || defined(BACDL_BIP))
|
||||
static int interface = SOCKET_ERROR; // SOCKET_ERROR means no open interface
|
||||
static int interface = SOCKET_ERROR; // SOCKET_ERROR means no open interface
|
||||
#endif
|
||||
|
||||
void bip_set_interface(char *ifname)
|
||||
{
|
||||
/*dummy function - to make the demos compile easier */
|
||||
/*dummy function - to make the demos compile easier */
|
||||
}
|
||||
|
||||
#if (defined(BACDL_ETHERNET) || defined(BACDL_BIP))
|
||||
/*-----------------------------------*/
|
||||
static void Error(const char * Msg)
|
||||
static void Error(const char *Msg)
|
||||
{
|
||||
int Code = WSAGetLastError();
|
||||
int Code = WSAGetLastError();
|
||||
#ifdef HOST
|
||||
printf("%s, error code: %i\n", Msg, Code);
|
||||
printf("%s, error code: %i\n", Msg, Code);
|
||||
#else
|
||||
printf("%s, error code: %s\n", Msg, xn_geterror_string(Code));
|
||||
printf("%s, error code: %s\n", Msg, xn_geterror_string(Code));
|
||||
#endif
|
||||
exit(1);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
#ifndef HOST
|
||||
/*-----------------------------------*/
|
||||
void InterfaceCleanup(void)
|
||||
{
|
||||
if (interface != SOCKET_ERROR)
|
||||
{
|
||||
xn_interface_close(interface);
|
||||
interface = SOCKET_ERROR;
|
||||
if (interface != SOCKET_ERROR) {
|
||||
xn_interface_close(interface);
|
||||
interface = SOCKET_ERROR;
|
||||
#if DEVICE_ID == PRISM_PCMCIA_DEVICE
|
||||
RTPCShutDown();
|
||||
RTPCShutDown();
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
static void NetInitialize(void)
|
||||
// initialize the TCP/IP stack
|
||||
{
|
||||
int Result;
|
||||
int Result;
|
||||
|
||||
#ifndef HOST
|
||||
|
||||
RTKernelInit(0); // get the kernel going
|
||||
RTKernelInit(0); // get the kernel going
|
||||
|
||||
if (!RTKDebugVersion()) // switch of all diagnostics and error messages of RTIP-32
|
||||
xn_callbacks()->cb_wr_screen_string_fnc = NULL;
|
||||
if (!RTKDebugVersion()) // switch of all diagnostics and error messages of RTIP-32
|
||||
xn_callbacks()->cb_wr_screen_string_fnc = NULL;
|
||||
|
||||
CLKSetTimerIntVal(10*1000); // 10 millisecond tick
|
||||
RTKDelay(1);
|
||||
RTCMOSSetSystemTime(); // get the right time-of-day
|
||||
CLKSetTimerIntVal(10 * 1000); // 10 millisecond tick
|
||||
RTKDelay(1);
|
||||
RTCMOSSetSystemTime(); // get the right time-of-day
|
||||
|
||||
#ifdef RTUSB_VER
|
||||
RTURegisterCallback(USBAX172); // ax172 and ax772 drivers
|
||||
RTURegisterCallback(USBAX772);
|
||||
RTURegisterCallback(USBKeyboard); // support USB keyboards
|
||||
FindUSBControllers(); // install USB host controllers
|
||||
Sleep(2000); // give the USB stack time to enumerate devices
|
||||
RTURegisterCallback(USBAX172); // ax172 and ax772 drivers
|
||||
RTURegisterCallback(USBAX772);
|
||||
RTURegisterCallback(USBKeyboard); // support USB keyboards
|
||||
FindUSBControllers(); // install USB host controllers
|
||||
Sleep(2000); // give the USB stack time to enumerate devices
|
||||
#endif
|
||||
|
||||
#ifdef DHCP
|
||||
XN_REGISTER_DHCP_CLI() // and optionally the DHCP client
|
||||
XN_REGISTER_DHCP_CLI() // and optionally the DHCP client
|
||||
#endif
|
||||
Result = xn_rtip_init(); // Initialize the RTIP stack
|
||||
if (Result != 0)
|
||||
Error("xn_rtip_init failed");
|
||||
|
||||
Result = xn_rtip_init(); // Initialize the RTIP stack
|
||||
if (Result != 0)
|
||||
Error("xn_rtip_init failed");
|
||||
atexit(InterfaceCleanup); // make sure the driver is shut down properly
|
||||
RTCallDebugger(RT_DBG_CALLRESET, (DWORD) exit, 0); // even if we get restarted by the debugger
|
||||
|
||||
atexit(InterfaceCleanup); // make sure the driver is shut down properly
|
||||
RTCallDebugger(RT_DBG_CALLRESET, (DWORD)exit, 0); // even if we get restarted by the debugger
|
||||
|
||||
Result = BIND_DRIVER(MINOR_0); // tell RTIP what Ethernet driver we want (see netcfg.h)
|
||||
if (Result != 0)
|
||||
Error("driver initialization failed");
|
||||
Result = BIND_DRIVER(MINOR_0); // tell RTIP what Ethernet driver we want (see netcfg.h)
|
||||
if (Result != 0)
|
||||
Error("driver initialization failed");
|
||||
|
||||
#if DEVICE_ID == PRISM_PCMCIA_DEVICE
|
||||
// if this is a PCMCIA device, start the PCMCIA driver
|
||||
if (RTPCInit(-1, 0, 2, NULL) == 0)
|
||||
Error("No PCMCIA controller found");
|
||||
// if this is a PCMCIA device, start the PCMCIA driver
|
||||
if (RTPCInit(-1, 0, 2, NULL) == 0)
|
||||
Error("No PCMCIA controller found");
|
||||
#endif
|
||||
|
||||
// Open the interface
|
||||
interface = xn_interface_open_config(DEVICE_ID, MINOR_0, ED_IO_ADD, ED_IRQ, ED_MEM_ADD);
|
||||
if (interface == SOCKET_ERROR)
|
||||
Error("xn_interface_open_config failed");
|
||||
else
|
||||
{
|
||||
struct _iface_info ii;
|
||||
// Open the interface
|
||||
interface =
|
||||
xn_interface_open_config(DEVICE_ID, MINOR_0, ED_IO_ADD, ED_IRQ,
|
||||
ED_MEM_ADD);
|
||||
if (interface == SOCKET_ERROR)
|
||||
Error("xn_interface_open_config failed");
|
||||
else {
|
||||
struct _iface_info ii;
|
||||
#ifdef BACDL_ETHERNET
|
||||
BACNET_ADDRESS my_address;
|
||||
unsigned i;
|
||||
BACNET_ADDRESS my_address;
|
||||
unsigned i;
|
||||
#endif
|
||||
xn_interface_info(interface, &ii);
|
||||
printf("Interface opened, MAC address: %02x-%02x-%02x-%02x-%02x-%02x\n",
|
||||
ii.my_ethernet_address[0], ii.my_ethernet_address[1], ii.my_ethernet_address[2],
|
||||
ii.my_ethernet_address[3], ii.my_ethernet_address[4], ii.my_ethernet_address[5]);
|
||||
xn_interface_info(interface, &ii);
|
||||
printf
|
||||
("Interface opened, MAC address: %02x-%02x-%02x-%02x-%02x-%02x\n",
|
||||
ii.my_ethernet_address[0], ii.my_ethernet_address[1],
|
||||
ii.my_ethernet_address[2], ii.my_ethernet_address[3],
|
||||
ii.my_ethernet_address[4], ii.my_ethernet_address[5]);
|
||||
#ifdef BACDL_ETHERNET
|
||||
for (i = 0; i < 6; i++)
|
||||
{
|
||||
my_address.mac[i] = ii.my_ethernet_address[i];
|
||||
}
|
||||
ethernet_set_my_address(&my_address);
|
||||
for (i = 0; i < 6; i++) {
|
||||
my_address.mac[i] = ii.my_ethernet_address[i];
|
||||
}
|
||||
ethernet_set_my_address(&my_address);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
#if DEVICE_ID == PRISM_PCMCIA_DEVICE || DEVICE_ID == PRISM_DEVICE
|
||||
xn_wlan_setup(interface, // iface_no: value returned by xn_interface_open_config()
|
||||
"network name", // SSID : network name set in the access point
|
||||
"station name", // Name : name of this node
|
||||
0, // Channel : 0 for access points, 1..14 for ad-hoc
|
||||
0, // KeyIndex: 0 .. 3
|
||||
"12345", // WEP Key : key to use (5 or 13 bytes)
|
||||
0); // Flags : see manual and Wlanapi.h for details
|
||||
Sleep(1000); // wireless devices need a little time before they can be used
|
||||
#endif // WLAN device
|
||||
xn_wlan_setup(interface, // iface_no: value returned by xn_interface_open_config()
|
||||
"network name", // SSID : network name set in the access point
|
||||
"station name", // Name : name of this node
|
||||
0, // Channel : 0 for access points, 1..14 for ad-hoc
|
||||
0, // KeyIndex: 0 .. 3
|
||||
"12345", // WEP Key : key to use (5 or 13 bytes)
|
||||
0); // Flags : see manual and Wlanapi.h for details
|
||||
Sleep(1000); // wireless devices need a little time before they can be used
|
||||
#endif // WLAN device
|
||||
|
||||
#if defined(AUTO_IP) // use xn_autoip() to get an IP address
|
||||
Result = xn_autoip(interface, MinIP, MaxIP, NetMask, TargetIP);
|
||||
if (Result == SOCKET_ERROR)
|
||||
Error("xn_autoip failed");
|
||||
else
|
||||
{
|
||||
printf("Auto-assigned IP address %i.%i.%i.%i\n", TargetIP[0], TargetIP[1], TargetIP[2], TargetIP[3]);
|
||||
// define default gateway and DNS server
|
||||
xn_rt_add(RT_DEFAULT, ip_ffaddr, DefaultGateway, 1, interface, RT_INF);
|
||||
xn_set_server_list((DWORD*)DNSServer, 1);
|
||||
}
|
||||
#elif defined(DHCP) // use DHCP
|
||||
{
|
||||
DHCP_param param[] = {{SUBNET_MASK, 1}, {DNS_OP, 1}, {ROUTER_OPTION, 1}};
|
||||
DHCP_session DS;
|
||||
DHCP_conf DC;
|
||||
#if defined(AUTO_IP) // use xn_autoip() to get an IP address
|
||||
Result = xn_autoip(interface, MinIP, MaxIP, NetMask, TargetIP);
|
||||
if (Result == SOCKET_ERROR)
|
||||
Error("xn_autoip failed");
|
||||
else {
|
||||
printf("Auto-assigned IP address %i.%i.%i.%i\n", TargetIP[0],
|
||||
TargetIP[1], TargetIP[2], TargetIP[3]);
|
||||
// define default gateway and DNS server
|
||||
xn_rt_add(RT_DEFAULT, ip_ffaddr, DefaultGateway, 1, interface,
|
||||
RT_INF);
|
||||
xn_set_server_list((DWORD *) DNSServer, 1);
|
||||
}
|
||||
#elif defined(DHCP) // use DHCP
|
||||
{
|
||||
DHCP_param param[] = { {SUBNET_MASK, 1}
|
||||
, {DNS_OP, 1}
|
||||
, {ROUTER_OPTION, 1}
|
||||
};
|
||||
DHCP_session DS;
|
||||
DHCP_conf DC;
|
||||
|
||||
xn_init_dhcp_conf(&DC); // load default DHCP options
|
||||
DC.plist = param; // add MASK, DNS, and gateway options
|
||||
DC.plist_entries = sizeof(param) / sizeof(param[0]);
|
||||
printf("Contacting DHCP server, please wait...\n");
|
||||
Result = xn_dhcp(interface, &DS, &DC); // contact DHCP server
|
||||
if (Result == SOCKET_ERROR)
|
||||
Error("xn_dhcp failed");
|
||||
memcpy(TargetIP, DS.client_ip, 4);
|
||||
printf("My IP address is: %i.%i.%i.%i\n", TargetIP[0], TargetIP[1], TargetIP[2], TargetIP[3]);
|
||||
}
|
||||
xn_init_dhcp_conf(&DC); // load default DHCP options
|
||||
DC.plist = param; // add MASK, DNS, and gateway options
|
||||
DC.plist_entries = sizeof(param) / sizeof(param[0]);
|
||||
printf("Contacting DHCP server, please wait...\n");
|
||||
Result = xn_dhcp(interface, &DS, &DC); // contact DHCP server
|
||||
if (Result == SOCKET_ERROR)
|
||||
Error("xn_dhcp failed");
|
||||
memcpy(TargetIP, DS.client_ip, 4);
|
||||
printf("My IP address is: %i.%i.%i.%i\n", TargetIP[0], TargetIP[1],
|
||||
TargetIP[2], TargetIP[3]);
|
||||
}
|
||||
#else
|
||||
// Set the IP address and interface
|
||||
printf("Using static IP address %i.%i.%i.%i\n", TargetIP[0], TargetIP[1], TargetIP[2], TargetIP[3]);
|
||||
Result = xn_set_ip(interface, TargetIP, NetMask);
|
||||
// define default gateway and DNS server
|
||||
xn_rt_add(RT_DEFAULT, ip_ffaddr, DefaultGateway, 1, interface, RT_INF);
|
||||
xn_set_server_list((DWORD*)DNSServer, 1);
|
||||
// Set the IP address and interface
|
||||
printf("Using static IP address %i.%i.%i.%i\n", TargetIP[0],
|
||||
TargetIP[1], TargetIP[2], TargetIP[3]);
|
||||
Result = xn_set_ip(interface, TargetIP, NetMask);
|
||||
// define default gateway and DNS server
|
||||
xn_rt_add(RT_DEFAULT, ip_ffaddr, DefaultGateway, 1, interface, RT_INF);
|
||||
xn_set_server_list((DWORD *) DNSServer, 1);
|
||||
#endif
|
||||
|
||||
#else // HOST defined, run on Windows
|
||||
#else // HOST defined, run on Windows
|
||||
|
||||
WSADATA wd;
|
||||
Result = WSAStartup(0x0101, &wd);
|
||||
WSADATA wd;
|
||||
Result = WSAStartup(0x0101, &wd);
|
||||
|
||||
#endif
|
||||
|
||||
if (Result != 0)
|
||||
Error("TCP/IP stack initialization failed");
|
||||
if (Result != 0)
|
||||
Error("TCP/IP stack initialization failed");
|
||||
}
|
||||
#endif
|
||||
|
||||
bool bip_init(void)
|
||||
{
|
||||
int rv = 0; // return from socket lib calls
|
||||
struct sockaddr_in sin = {-1};
|
||||
int rv = 0; // return from socket lib calls
|
||||
struct sockaddr_in sin = { -1 };
|
||||
int value = 1;
|
||||
int sock_fd = -1;
|
||||
|
||||
@@ -212,7 +218,7 @@ bool bip_init(void)
|
||||
|
||||
bip_set_address(TargetIP[0], TargetIP[1], TargetIP[2], TargetIP[3]);
|
||||
|
||||
// FIXME:
|
||||
// FIXME:
|
||||
#if 0
|
||||
bip_set_address(NetMask[0], NetMask[1], NetMask[2], NetMask[3]);
|
||||
extern unsigned long bip_get_addr(void);
|
||||
@@ -237,14 +243,13 @@ bool bip_init(void)
|
||||
sin.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||
sin.sin_port = htons(bip_get_port());
|
||||
memset(&(sin.sin_zero), '\0', 8);
|
||||
rv = bind(sock_fd,
|
||||
(const struct sockaddr*)&sin, sizeof(struct sockaddr));
|
||||
if (rv < 0)
|
||||
{
|
||||
rv = bind(sock_fd,
|
||||
(const struct sockaddr *) &sin, sizeof(struct sockaddr));
|
||||
if (rv < 0) {
|
||||
close(sock_fd);
|
||||
bip_set_socket(-1);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -25,8 +25,8 @@
|
||||
|
||||
#include <stdint.h> // for standard integer types uint8_t etc.
|
||||
#include <stdbool.h> // for the standard bool type.
|
||||
#include <stdio.h> // for the standard bool type.
|
||||
#include <stdlib.h> // for the standard bool type.
|
||||
#include <stdio.h> // for the standard bool type.
|
||||
#include <stdlib.h> // for the standard bool type.
|
||||
#include <rttarget.h>
|
||||
#include <rtk32.h>
|
||||
#include <clock.h>
|
||||
@@ -40,6 +40,7 @@ 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 };
|
||||
|
||||
// my local device data - MAC address
|
||||
uint8_t Ethernet_MAC_Address[MAX_MAC_LEN] = { 0, 0, 0, 0, 0, 0 };
|
||||
|
||||
@@ -49,45 +50,44 @@ static struct sockaddr Ethernet_Address = { 0 };
|
||||
|
||||
bool ethernet_valid(void)
|
||||
{
|
||||
return (Ethernet_Socket != -1);
|
||||
return (Ethernet_Socket != -1);
|
||||
}
|
||||
|
||||
void ethernet_cleanup(void)
|
||||
{
|
||||
if (ethernet_valid())
|
||||
closesocket(Ethernet_Socket);
|
||||
Ethernet_Socket = -1;
|
||||
if (ethernet_valid())
|
||||
closesocket(Ethernet_Socket);
|
||||
Ethernet_Socket = -1;
|
||||
|
||||
return;
|
||||
return;
|
||||
}
|
||||
|
||||
bool ethernet_init(char *interface_name)
|
||||
{
|
||||
int value = 1;
|
||||
|
||||
(void)interface_name;
|
||||
int value = 1;
|
||||
|
||||
(void) interface_name;
|
||||
// setup the socket
|
||||
Ethernet_Socket = socket(AF_INET, SOCK_RAW, 0);
|
||||
//Ethernet_Socket = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (Ethernet_Socket < 0)
|
||||
fprintf(stderr,"ethernet: failed to bind to socket!\r\n");
|
||||
fprintf(stderr, "ethernet: failed to bind to socket!\r\n");
|
||||
Ethernet_Address.sa_family = AF_INET;
|
||||
memset(Ethernet_Address.sa_data,0,sizeof(Ethernet_Address.sa_data));
|
||||
if (bind(Ethernet_Socket,
|
||||
&Ethernet_Address, sizeof(Ethernet_Address)) == SOCKET_ERROR)
|
||||
fprintf(stderr,"ethernet: failed to bind to socket!\r\n");
|
||||
memset(Ethernet_Address.sa_data, 0, sizeof(Ethernet_Address.sa_data));
|
||||
if (bind(Ethernet_Socket,
|
||||
&Ethernet_Address, sizeof(Ethernet_Address)) == SOCKET_ERROR)
|
||||
fprintf(stderr, "ethernet: failed to bind to socket!\r\n");
|
||||
//setsockopt(Ethernet_Socket,SOL_SOCKET,SO_802_2,(char *)&value,sizeof(value));
|
||||
|
||||
return ethernet_valid();
|
||||
return ethernet_valid();
|
||||
}
|
||||
|
||||
/* function to send a packet out the 802.2 socket */
|
||||
/* returns bytes sent on success, negative number on failure */
|
||||
int ethernet_send(
|
||||
BACNET_ADDRESS *dest, // destination address
|
||||
BACNET_ADDRESS *src, // source address
|
||||
uint8_t *pdu, // any data to be sent - may be null
|
||||
unsigned pdu_len) // number of bytes of data
|
||||
int ethernet_send(BACNET_ADDRESS * dest, // destination address
|
||||
BACNET_ADDRESS * src, // source address
|
||||
uint8_t * pdu, // any data to be sent - may be null
|
||||
unsigned pdu_len) // number of bytes of data
|
||||
{
|
||||
int bytes = 0;
|
||||
uint8_t mtu[MAX_MPDU] = { 0 };
|
||||
@@ -95,100 +95,86 @@ int ethernet_send(
|
||||
int i = 0;
|
||||
|
||||
// don't waste time if the socket is not valid
|
||||
if (Ethernet_Socket < 0)
|
||||
{
|
||||
if (Ethernet_Socket < 0) {
|
||||
fprintf(stderr, "ethernet: 802.2 socket is invalid!\n");
|
||||
return -1;
|
||||
}
|
||||
/* load destination ethernet MAC address */
|
||||
if (dest->mac_len == 6)
|
||||
{
|
||||
for (i = 0; i < 6; i++)
|
||||
{
|
||||
mtu[mtu_len] = dest->mac[i];
|
||||
mtu_len++;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (dest->mac_len == 6) {
|
||||
for (i = 0; i < 6; i++) {
|
||||
mtu[mtu_len] = dest->mac[i];
|
||||
mtu_len++;
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "ethernet: invalid destination MAC address!\n");
|
||||
return -2;
|
||||
}
|
||||
|
||||
/* load source ethernet MAC address */
|
||||
if (src->mac_len == 6)
|
||||
{
|
||||
for (i = 0; i < 6; i++)
|
||||
{
|
||||
mtu[mtu_len] = src->mac[i];
|
||||
mtu_len++;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (src->mac_len == 6) {
|
||||
for (i = 0; i < 6; i++) {
|
||||
mtu[mtu_len] = src->mac[i];
|
||||
mtu_len++;
|
||||
}
|
||||
} else {
|
||||
fprintf(stderr, "ethernet: invalid source MAC address!\n");
|
||||
return -3;
|
||||
}
|
||||
if ((14 + 3 + pdu_len) > MAX_MPDU)
|
||||
{
|
||||
if ((14 + 3 + pdu_len) > MAX_MPDU) {
|
||||
fprintf(stderr, "ethernet: PDU is too big to send!\n");
|
||||
return -4;
|
||||
}
|
||||
/* packet length */
|
||||
mtu_len += encode_unsigned16(&mtu[12],
|
||||
3 /*DSAP,SSAP,LLC*/ + pdu_len);
|
||||
mtu_len += encode_unsigned16(&mtu[12],
|
||||
3 /*DSAP,SSAP,LLC */ + pdu_len);
|
||||
// Logical PDU portion
|
||||
mtu[mtu_len++] = 0x82; /* DSAP for BACnet */
|
||||
mtu[mtu_len++] = 0x82; /* SSAP for BACnet */
|
||||
mtu[mtu_len++] = 0x03; /* Control byte in header */
|
||||
mtu[mtu_len++] = 0x82; /* DSAP for BACnet */
|
||||
mtu[mtu_len++] = 0x82; /* SSAP for BACnet */
|
||||
mtu[mtu_len++] = 0x03; /* Control byte in header */
|
||||
memcpy(&mtu[mtu_len], pdu, pdu_len);
|
||||
mtu_len += pdu_len;
|
||||
|
||||
|
||||
/* Send the packet */
|
||||
bytes =
|
||||
send(Ethernet_Socket, (const char *)&mtu, mtu_len, 0);
|
||||
bytes = send(Ethernet_Socket, (const char *) &mtu, mtu_len, 0);
|
||||
/* did it get sent? */
|
||||
if (bytes < 0)
|
||||
fprintf(stderr,"ethernet: Error sending packet: %s\n",
|
||||
strerror(errno));
|
||||
|
||||
fprintf(stderr, "ethernet: Error sending packet: %s\n",
|
||||
strerror(errno));
|
||||
|
||||
return bytes;
|
||||
}
|
||||
|
||||
/* function to send a packet out the 802.2 socket */
|
||||
/* returns bytes sent on success, negative number on failure */
|
||||
int ethernet_send_pdu(
|
||||
BACNET_ADDRESS *dest, // destination address
|
||||
uint8_t *pdu, // any data to be sent - may be null
|
||||
unsigned pdu_len) // number of bytes of data
|
||||
int ethernet_send_pdu(BACNET_ADDRESS * dest, // destination address
|
||||
uint8_t * pdu, // any data to be sent - may be null
|
||||
unsigned pdu_len) // number of bytes of data
|
||||
{
|
||||
int i = 0; // counter
|
||||
BACNET_ADDRESS src = {0}; // source address
|
||||
|
||||
for (i = 0; i < 6; i++)
|
||||
{
|
||||
src.mac[i] = Ethernet_MAC_Address[i];
|
||||
src.mac_len++;
|
||||
}
|
||||
/* function to send a packet out the 802.2 socket */
|
||||
/* returns 1 on success, 0 on failure */
|
||||
return ethernet_send(dest, // destination address
|
||||
&src, // source address
|
||||
pdu, // any data to be sent - may be null
|
||||
pdu_len); // number of bytes of data
|
||||
int i = 0; // counter
|
||||
BACNET_ADDRESS src = { 0 }; // source address
|
||||
|
||||
for (i = 0; i < 6; i++) {
|
||||
src.mac[i] = Ethernet_MAC_Address[i];
|
||||
src.mac_len++;
|
||||
}
|
||||
/* function to send a packet out the 802.2 socket */
|
||||
/* returns 1 on success, 0 on failure */
|
||||
return ethernet_send(dest, // destination address
|
||||
&src, // source address
|
||||
pdu, // any data to be sent - may be null
|
||||
pdu_len); // number of bytes of data
|
||||
}
|
||||
|
||||
// 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
|
||||
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
|
||||
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) // number of milliseconds to wait for a packet
|
||||
{
|
||||
int received_bytes;
|
||||
uint8_t buf[MAX_MPDU] = {0}; // data
|
||||
uint16_t pdu_len = 0; // return value
|
||||
uint8_t buf[MAX_MPDU] = { 0 }; // data
|
||||
uint16_t pdu_len = 0; // return value
|
||||
fd_set read_fds;
|
||||
int max;
|
||||
struct timeval select_timeout;
|
||||
@@ -200,14 +186,11 @@ uint16_t ethernet_receive(
|
||||
/* 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
|
||||
a select. */
|
||||
if (timeout >= 1000)
|
||||
{
|
||||
if (timeout >= 1000) {
|
||||
select_timeout.tv_sec = timeout / 1000;
|
||||
select_timeout.tv_usec =
|
||||
1000 * (timeout - select_timeout.tv_sec * 1000);
|
||||
}
|
||||
else
|
||||
{
|
||||
select_timeout.tv_usec =
|
||||
1000 * (timeout - select_timeout.tv_sec * 1000);
|
||||
} else {
|
||||
select_timeout.tv_sec = 0;
|
||||
select_timeout.tv_usec = 1000 * timeout;
|
||||
}
|
||||
@@ -216,7 +199,8 @@ uint16_t ethernet_receive(
|
||||
max = Ethernet_Socket;
|
||||
|
||||
if (select(max + 1, &read_fds, NULL, NULL, &select_timeout) > 0)
|
||||
received_bytes = recv(Ethernet_Socket, (char *)&buf[0], MAX_MPDU, 0);
|
||||
received_bytes =
|
||||
recv(Ethernet_Socket, (char *) &buf[0], MAX_MPDU, 0);
|
||||
else
|
||||
return 0;
|
||||
|
||||
@@ -226,14 +210,15 @@ uint16_t ethernet_receive(
|
||||
// using O_NONBLOCK and no data
|
||||
// was immediately available for reading.
|
||||
if (errno != EAGAIN)
|
||||
fprintf(stderr,"ethernet: Read error in receiving packet: %s\n",
|
||||
fprintf(stderr,
|
||||
"ethernet: Read error in receiving packet: %s\n",
|
||||
strerror(errno));
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (received_bytes == 0)
|
||||
return 0;
|
||||
|
||||
return 0;
|
||||
|
||||
/* the signature of an 802.2 BACnet packet */
|
||||
if ((buf[14] != 0x82) && (buf[15] != 0x82)) {
|
||||
//fprintf(stderr,"ethernet: Non-BACnet packet\n");
|
||||
@@ -245,108 +230,95 @@ uint16_t ethernet_receive(
|
||||
|
||||
// check destination address for when
|
||||
// the Ethernet card is in promiscious mode
|
||||
if ((memcmp(&buf[0], Ethernet_MAC_Address,6) != 0)
|
||||
&& (memcmp(&buf[0], Ethernet_Broadcast, 6) != 0))
|
||||
{
|
||||
if ((memcmp(&buf[0], Ethernet_MAC_Address, 6) != 0)
|
||||
&& (memcmp(&buf[0], Ethernet_Broadcast, 6) != 0)) {
|
||||
//fprintf(stderr, "ethernet: This packet isn't for us\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
(void)decode_unsigned16(&buf[12],&pdu_len);
|
||||
(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)
|
||||
memmove(&pdu[0],&buf[17],pdu_len);
|
||||
memmove(&pdu[0], &buf[17], pdu_len);
|
||||
// ignore packets that are too large
|
||||
// client should check my max apdu first
|
||||
else
|
||||
pdu_len = 0;
|
||||
pdu_len = 0;
|
||||
|
||||
return pdu_len;
|
||||
}
|
||||
|
||||
void ethernet_get_my_address(BACNET_ADDRESS *my_address)
|
||||
void ethernet_get_my_address(BACNET_ADDRESS * my_address)
|
||||
{
|
||||
int i = 0;
|
||||
|
||||
my_address->mac_len = 0;
|
||||
for (i = 0; i < 6; i++)
|
||||
{
|
||||
my_address->mac[i] = Ethernet_MAC_Address[i];
|
||||
my_address->mac_len++;
|
||||
}
|
||||
my_address->net = 0; // local only, no routing
|
||||
my_address->len = 0;
|
||||
for (i = 0; i < MAX_MAC_LEN; i++)
|
||||
{
|
||||
my_address->adr[i] = 0;
|
||||
}
|
||||
int i = 0;
|
||||
|
||||
return;
|
||||
my_address->mac_len = 0;
|
||||
for (i = 0; i < 6; i++) {
|
||||
my_address->mac[i] = Ethernet_MAC_Address[i];
|
||||
my_address->mac_len++;
|
||||
}
|
||||
my_address->net = 0; // local only, no routing
|
||||
my_address->len = 0;
|
||||
for (i = 0; i < MAX_MAC_LEN; i++) {
|
||||
my_address->adr[i] = 0;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void ethernet_set_my_address(BACNET_ADDRESS *my_address)
|
||||
void ethernet_set_my_address(BACNET_ADDRESS * my_address)
|
||||
{
|
||||
int i = 0;
|
||||
|
||||
for (i = 0; i < 6; i++)
|
||||
{
|
||||
Ethernet_MAC_Address[i] = my_address->mac[i];
|
||||
}
|
||||
int i = 0;
|
||||
|
||||
return;
|
||||
for (i = 0; i < 6; i++) {
|
||||
Ethernet_MAC_Address[i] = my_address->mac[i];
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void ethernet_get_broadcast_address(
|
||||
BACNET_ADDRESS *dest) // destination address
|
||||
void ethernet_get_broadcast_address(BACNET_ADDRESS * dest) // destination address
|
||||
{
|
||||
int i = 0; // counter
|
||||
|
||||
if (dest)
|
||||
{
|
||||
for (i = 0; i < 6; i++)
|
||||
{
|
||||
dest->mac[i] = Ethernet_Broadcast[i];
|
||||
}
|
||||
dest->mac_len = 6;
|
||||
dest->net = BACNET_BROADCAST_NETWORK;
|
||||
dest->len = 0; // denotes broadcast address
|
||||
for (i = 0; i < MAX_MAC_LEN; i++)
|
||||
{
|
||||
dest->adr[i] = 0;
|
||||
}
|
||||
}
|
||||
int i = 0; // counter
|
||||
|
||||
return;
|
||||
if (dest) {
|
||||
for (i = 0; i < 6; i++) {
|
||||
dest->mac[i] = Ethernet_Broadcast[i];
|
||||
}
|
||||
dest->mac_len = 6;
|
||||
dest->net = BACNET_BROADCAST_NETWORK;
|
||||
dest->len = 0; // denotes broadcast address
|
||||
for (i = 0; i < MAX_MAC_LEN; i++) {
|
||||
dest->adr[i] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
void ethernet_debug_address(
|
||||
const char *info,
|
||||
BACNET_ADDRESS *dest)
|
||||
void ethernet_debug_address(const char *info, BACNET_ADDRESS * dest)
|
||||
{
|
||||
int i = 0; // counter
|
||||
int i = 0; // counter
|
||||
|
||||
if (info)
|
||||
fprintf(stderr,"%s",info);
|
||||
if (dest)
|
||||
{
|
||||
fprintf(stderr,"Address:\n");
|
||||
fprintf(stderr," MAC Length=%d\n",dest->mac_len);
|
||||
fprintf(stderr," MAC Address=");
|
||||
for (i = 0; i < MAX_MAC_LEN; i++)
|
||||
{
|
||||
fprintf(stderr,"%02X ",(unsigned)dest->mac[i]);
|
||||
if (info)
|
||||
fprintf(stderr, "%s", info);
|
||||
if (dest) {
|
||||
fprintf(stderr, "Address:\n");
|
||||
fprintf(stderr, " MAC Length=%d\n", dest->mac_len);
|
||||
fprintf(stderr, " MAC Address=");
|
||||
for (i = 0; i < MAX_MAC_LEN; i++) {
|
||||
fprintf(stderr, "%02X ", (unsigned) dest->mac[i]);
|
||||
}
|
||||
fprintf(stderr, "\n");
|
||||
fprintf(stderr, " Net=%hu\n", dest->net);
|
||||
fprintf(stderr, " Len=%d\n", dest->len);
|
||||
fprintf(stderr, " Adr=");
|
||||
for (i = 0; i < MAX_MAC_LEN; i++) {
|
||||
fprintf(stderr, "%02X ", (unsigned) dest->adr[i]);
|
||||
}
|
||||
fprintf(stderr, "\n");
|
||||
}
|
||||
fprintf(stderr,"\n");
|
||||
fprintf(stderr," Net=%hu\n",dest->net);
|
||||
fprintf(stderr," Len=%d\n",dest->len);
|
||||
fprintf(stderr," Adr=");
|
||||
for (i = 0; i < MAX_MAC_LEN; i++)
|
||||
{
|
||||
fprintf(stderr,"%02X ",(unsigned)dest->adr[i]);
|
||||
}
|
||||
fprintf(stderr,"\n");
|
||||
}
|
||||
|
||||
return;
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -30,34 +30,33 @@
|
||||
extern void RTEmuInit(void);
|
||||
|
||||
#ifdef _MSC_VER
|
||||
#define VOIDEXPORT _declspec(dllexport) void __cdecl
|
||||
#define VOIDEXPORT _declspec(dllexport) void __cdecl
|
||||
#else
|
||||
#define VOIDEXPORT void __export __cdecl
|
||||
#define VOIDEXPORT void __export __cdecl
|
||||
#endif
|
||||
|
||||
/* DISK SYSTEM */
|
||||
#ifdef DOC // include DiskOnChip driver
|
||||
#include <rtfiles.h>
|
||||
#define RTF_MAX_FILES 16 // support for more open files (default is 8)
|
||||
#define RTF_BUFFERS_IN_BSS // we do not need file I/O before the run-time
|
||||
#include <rtfdata.c> // system is initialized
|
||||
#ifdef DOC // include DiskOnChip driver
|
||||
#include <rtfiles.h>
|
||||
#define RTF_MAX_FILES 16 // support for more open files (default is 8)
|
||||
#define RTF_BUFFERS_IN_BSS // we do not need file I/O before the run-time
|
||||
#include <rtfdata.c> // system is initialized
|
||||
|
||||
//#define READ_HEAD_BUFFER_SIZE 2048+4
|
||||
|
||||
//static BYTE ReadAheadBuffer[READ_HEAD_BUFFER_SIZE];
|
||||
|
||||
static RTFDrvFLPYData FLPYDriveAData = {0};
|
||||
static RTFDrvDOCData DOCDriveData = {0};
|
||||
static RTFDrvIDEData IDEDriveData = {0};
|
||||
static RTFDrvFLPYData FLPYDriveAData = { 0 };
|
||||
static RTFDrvDOCData DOCDriveData = { 0 };
|
||||
static RTFDrvIDEData IDEDriveData = { 0 };
|
||||
|
||||
RTFDevice RTFDeviceList[] =
|
||||
{
|
||||
RTFDevice RTFDeviceList[] = {
|
||||
/* type,number,flags,driver,driverdata */
|
||||
{ RTF_DEVICE_FLOPPY, 0, 0, &RTFDrvFloppy, &FLPYDriveAData},
|
||||
{ RTF_DEVICE_FDISK , 0, 0, &RTFDrvDOC, &DOCDriveData},
|
||||
{ RTF_DEVICE_FDISK , 0, 0, &RTFDrvIDE, &IDEDriveData},
|
||||
{ 0 , 0, 0, NULL, NULL}
|
||||
};
|
||||
{RTF_DEVICE_FLOPPY, 0, 0, &RTFDrvFloppy, &FLPYDriveAData},
|
||||
{RTF_DEVICE_FDISK, 0, 0, &RTFDrvDOC, &DOCDriveData},
|
||||
{RTF_DEVICE_FDISK, 0, 0, &RTFDrvIDE, &IDEDriveData},
|
||||
{0, 0, 0, NULL, NULL}
|
||||
};
|
||||
#endif
|
||||
/* END OF DISK SYSTEM */
|
||||
|
||||
@@ -67,14 +66,14 @@ extern void RTEmuInit(void);
|
||||
#define MAXOBJECTS 1024
|
||||
#define MAXTYPES 32
|
||||
|
||||
RTW32Handle RTHandleTable[MAXHANDLES] = {{0}};
|
||||
int RTHandleCount = MAXHANDLES;
|
||||
RTW32Handle RTHandleTable[MAXHANDLES] = { {0} };
|
||||
int RTHandleCount = MAXHANDLES;
|
||||
|
||||
RTW32Object RTObjectTable[MAXOBJECTS] = {{0}};
|
||||
int RTObjectCount = MAXOBJECTS;
|
||||
RTW32Object RTObjectTable[MAXOBJECTS] = { {0} };
|
||||
int RTObjectCount = MAXOBJECTS;
|
||||
|
||||
RTW32Types RTTypeTable[MAXTYPES] = {{0}};
|
||||
int RTTypeCount = MAXTYPES;
|
||||
RTW32Types RTTypeTable[MAXTYPES] = { {0} };
|
||||
int RTTypeCount = MAXTYPES;
|
||||
|
||||
#if 0
|
||||
/* We can embed some files in the RTB file, like a binary
|
||||
@@ -85,44 +84,41 @@ int RTTypeCount = MAXTYPES;
|
||||
that here, as well as the LPT, console, and FAT.
|
||||
From RTFiles-32 manual, ch. 7, "Using RTFiles-32 with
|
||||
RTTarget-32" */
|
||||
RTFileSystem Console =
|
||||
{ RT_FS_CONSOLE, 0, 0, &RTConsoleFileSystem };
|
||||
RTFileSystem Console = { RT_FS_CONSOLE, 0, 0, &RTConsoleFileSystem };
|
||||
|
||||
RTFileSystem LPTFiles =
|
||||
{ RT_FS_LPT_DEVICE, 0, 0, &RTLPTFileSystem };
|
||||
RTFileSystem LPTFiles = { RT_FS_LPT_DEVICE, 0, 0, &RTLPTFileSystem };
|
||||
|
||||
/* logical drive Z: can be used to access the RAM drive */
|
||||
RTFileSystem RAMFiles =
|
||||
{ RT_FS_FILE,1 << ('Z'-'A'), 0, &RTRAMFileSystem };
|
||||
{ RT_FS_FILE, 1 << ('Z' - 'A'), 0, &RTRAMFileSystem };
|
||||
|
||||
/* logical drive A: through D: are reserved for FAT */
|
||||
RTFileSystem FATFiles =
|
||||
{ RT_FS_FILE | RT_FS_IS_DEFAULT, 0x0F, 0x03, &RTFilesFileSystem };
|
||||
{ RT_FS_FILE | RT_FS_IS_DEFAULT, 0x0F, 0x03, &RTFilesFileSystem };
|
||||
|
||||
RTFileSystem *RTFileSystemList[] =
|
||||
{
|
||||
&Console,
|
||||
&LPTFiles,
|
||||
&RAMFiles,
|
||||
&FATFiles,
|
||||
NULL,
|
||||
RTFileSystem *RTFileSystemList[] = {
|
||||
&Console,
|
||||
&LPTFiles,
|
||||
&RAMFiles,
|
||||
&FATFiles,
|
||||
NULL,
|
||||
};
|
||||
#endif
|
||||
|
||||
/*-----------------------------------*/
|
||||
VOIDEXPORT Init(void)
|
||||
{
|
||||
(void)RTSetFlags(RT_MM_VIRTUAL, 1); // this is the better method
|
||||
(void)RTCMOSExtendHeap(); // get as much memory as we can
|
||||
RTCMOSSetSystemTime(); // get the right date and time
|
||||
RTEmuInit(); // set up floating point emulation
|
||||
// pizza - RTHaltCPL3 appears to cause problems with file handling
|
||||
//RTIdleHandler = (void RTTAPI *)RTHaltCPL3; // low power when idle
|
||||
// not needed with pre-emptive
|
||||
//RTKTimeSlice(2); // allow same priority task switch
|
||||
RTKConfig.Flags |= RF_PREEMPTIVE; // preemptive multitasking
|
||||
RTKConfig.Flags |= RF_WIN32MUTEX_MUTEX; // Win32 mutexes are RTK32 mutexes
|
||||
RTKConfig.Flags |= RF_FPCONTEXT; // saves floating point context for tasks
|
||||
RTKConfig.HookedIRQs |= 1 << 1; // hook the keyboard IRQ
|
||||
RTKConfig.DefaultTaskStackSize = 1024*8; // for Win32 task stacks req = 0
|
||||
(void) RTSetFlags(RT_MM_VIRTUAL, 1); // this is the better method
|
||||
(void) RTCMOSExtendHeap(); // get as much memory as we can
|
||||
RTCMOSSetSystemTime(); // get the right date and time
|
||||
RTEmuInit(); // set up floating point emulation
|
||||
// pizza - RTHaltCPL3 appears to cause problems with file handling
|
||||
//RTIdleHandler = (void RTTAPI *)RTHaltCPL3; // low power when idle
|
||||
// not needed with pre-emptive
|
||||
//RTKTimeSlice(2); // allow same priority task switch
|
||||
RTKConfig.Flags |= RF_PREEMPTIVE; // preemptive multitasking
|
||||
RTKConfig.Flags |= RF_WIN32MUTEX_MUTEX; // Win32 mutexes are RTK32 mutexes
|
||||
RTKConfig.Flags |= RF_FPCONTEXT; // saves floating point context for tasks
|
||||
RTKConfig.HookedIRQs |= 1 << 1; // hook the keyboard IRQ
|
||||
RTKConfig.DefaultTaskStackSize = 1024 * 8; // for Win32 task stacks req = 0
|
||||
}
|
||||
|
||||
@@ -37,95 +37,82 @@
|
||||
#include "net.h"
|
||||
|
||||
// buffers used for transmit and receive
|
||||
static uint8_t Rx_Buf[MAX_MPDU] = {0};
|
||||
static uint8_t Rx_Buf[MAX_MPDU] = { 0 };
|
||||
|
||||
#ifdef BACDL_MSTP
|
||||
volatile struct mstp_port_struct_t MSTP_Port; // port data
|
||||
volatile struct mstp_port_struct_t MSTP_Port; // port data
|
||||
static uint8_t MSTP_MAC_Address = 0x05; // local MAC address
|
||||
#endif
|
||||
|
||||
static void Init_Service_Handlers(void)
|
||||
{
|
||||
// we need to handle who-is to support dynamic device binding
|
||||
apdu_set_unconfirmed_handler(
|
||||
SERVICE_UNCONFIRMED_WHO_IS,
|
||||
WhoIsHandler);
|
||||
// set the handler for all the services we don't implement
|
||||
// It is required to send the proper reject message...
|
||||
apdu_set_unrecognized_service_handler_handler(
|
||||
UnrecognizedServiceHandler);
|
||||
// we must implement read property - it's required!
|
||||
apdu_set_confirmed_handler(
|
||||
SERVICE_CONFIRMED_READ_PROPERTY,
|
||||
ReadPropertyHandler);
|
||||
apdu_set_confirmed_handler(
|
||||
SERVICE_CONFIRMED_WRITE_PROPERTY,
|
||||
WritePropertyHandler);
|
||||
// we need to handle who-is to support dynamic device binding
|
||||
apdu_set_unconfirmed_handler(SERVICE_UNCONFIRMED_WHO_IS, WhoIsHandler);
|
||||
// set the handler for all the services we don't implement
|
||||
// It is required to send the proper reject message...
|
||||
apdu_set_unrecognized_service_handler_handler
|
||||
(UnrecognizedServiceHandler);
|
||||
// we must implement read property - it's required!
|
||||
apdu_set_confirmed_handler(SERVICE_CONFIRMED_READ_PROPERTY,
|
||||
ReadPropertyHandler);
|
||||
apdu_set_confirmed_handler(SERVICE_CONFIRMED_WRITE_PROPERTY,
|
||||
WritePropertyHandler);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
BACNET_ADDRESS src = {0}; // address where message came from
|
||||
uint16_t pdu_len = 0;
|
||||
unsigned timeout = 100; // milliseconds
|
||||
BACNET_ADDRESS src = { 0 }; // address where message came from
|
||||
uint16_t pdu_len = 0;
|
||||
unsigned timeout = 100; // milliseconds
|
||||
|
||||
(void)argc;
|
||||
(void)argv;
|
||||
Device_Set_Object_Instance_Number(126);
|
||||
Init_Service_Handlers();
|
||||
// init the physical layer
|
||||
#ifdef BACDL_BIP
|
||||
if (!bip_init())
|
||||
return 1;
|
||||
#endif
|
||||
#ifdef BACDL_ETHERNET
|
||||
if (!ethernet_init(NULL))
|
||||
return 1;
|
||||
#endif
|
||||
#ifdef BACDL_MSTP
|
||||
RS485_Initialize();
|
||||
MSTP_Init(&MSTP_Port,MSTP_MAC_Address);
|
||||
#endif
|
||||
(void) argc;
|
||||
(void) argv;
|
||||
Device_Set_Object_Instance_Number(126);
|
||||
Init_Service_Handlers();
|
||||
// init the physical layer
|
||||
#ifdef BACDL_BIP
|
||||
if (!bip_init())
|
||||
return 1;
|
||||
#endif
|
||||
#ifdef BACDL_ETHERNET
|
||||
if (!ethernet_init(NULL))
|
||||
return 1;
|
||||
#endif
|
||||
#ifdef BACDL_MSTP
|
||||
RS485_Initialize();
|
||||
MSTP_Init(&MSTP_Port, MSTP_MAC_Address);
|
||||
#endif
|
||||
|
||||
|
||||
// loop forever
|
||||
for (;;)
|
||||
{
|
||||
// input
|
||||
#ifdef BACDL_MSTP
|
||||
MSTP_Millisecond_Timer(&MSTP_Port);
|
||||
// note: also called by RS-485 Receive ISR
|
||||
RS485_Check_UART_Data(&MSTP_Port);
|
||||
MSTP_Receive_Frame_FSM(&MSTP_Port);
|
||||
#endif
|
||||
|
||||
#if (defined(BACDL_ETHERNET) || defined(BACDL_BIP))
|
||||
// returns 0 bytes on timeout
|
||||
pdu_len = bacdl_receive(
|
||||
&src,
|
||||
&Rx_Buf[0],
|
||||
MAX_MPDU,
|
||||
timeout);
|
||||
#endif
|
||||
// loop forever
|
||||
for (;;) {
|
||||
// input
|
||||
#ifdef BACDL_MSTP
|
||||
MSTP_Millisecond_Timer(&MSTP_Port);
|
||||
// note: also called by RS-485 Receive ISR
|
||||
RS485_Check_UART_Data(&MSTP_Port);
|
||||
MSTP_Receive_Frame_FSM(&MSTP_Port);
|
||||
#endif
|
||||
|
||||
// process
|
||||
#if (defined(BACDL_ETHERNET) || defined(BACDL_BIP))
|
||||
// returns 0 bytes on timeout
|
||||
pdu_len = bacdl_receive(&src, &Rx_Buf[0], MAX_MPDU, timeout);
|
||||
#endif
|
||||
|
||||
if (pdu_len)
|
||||
{
|
||||
npdu_handler(
|
||||
&src,
|
||||
&Rx_Buf[0],
|
||||
pdu_len);
|
||||
// process
|
||||
|
||||
if (pdu_len) {
|
||||
npdu_handler(&src, &Rx_Buf[0], pdu_len);
|
||||
}
|
||||
if (I_Am_Request) {
|
||||
I_Am_Request = false;
|
||||
Send_IAm();
|
||||
}
|
||||
// output
|
||||
#ifdef BACDL_MSTP
|
||||
MSTP_Master_Node_FSM(&MSTP_Port);
|
||||
#endif
|
||||
|
||||
// blink LEDs, Turn on or off outputs, etc
|
||||
}
|
||||
if (I_Am_Request)
|
||||
{
|
||||
I_Am_Request = false;
|
||||
Send_IAm();
|
||||
}
|
||||
// output
|
||||
#ifdef BACDL_MSTP
|
||||
MSTP_Master_Node_FSM(&MSTP_Port);
|
||||
#endif
|
||||
|
||||
// blink LEDs, Turn on or off outputs, etc
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,17 +35,17 @@
|
||||
#include <process.h>
|
||||
|
||||
#ifdef BACDL_BIP
|
||||
#include "bip.h"
|
||||
#ifndef HOST
|
||||
#include "netcfg.h"
|
||||
#include <rttarget.h>
|
||||
#include <rtk32.h>
|
||||
#include <clock.h>
|
||||
#include <socket.h>
|
||||
#else
|
||||
#include <winsock.h>
|
||||
#endif
|
||||
#define close closesocket
|
||||
#include "bip.h"
|
||||
#ifndef HOST
|
||||
#include "netcfg.h"
|
||||
#include <rttarget.h>
|
||||
#include <rtk32.h>
|
||||
#include <clock.h>
|
||||
#include <socket.h>
|
||||
#else
|
||||
#include <winsock.h>
|
||||
#endif
|
||||
#define close closesocket
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
@@ -31,74 +31,74 @@
|
||||
// #define AUTO_IP // use xn_autoip() to get an IP address
|
||||
// #define DHCP // if you enable this, you must also link library dhcpc.lib
|
||||
|
||||
#if defined(AUTO_IP) // use xn_autoip() to get an IP address
|
||||
static BYTE TargetIP[] = { 0, 0, 0, 0}; // will be filled at run-time
|
||||
static BYTE NetMask[] = {255, 255, 255, 0};
|
||||
static BYTE MinIP[] = {192, 168, 1, 128};
|
||||
static BYTE MaxIP[] = {192, 168, 1, 255};
|
||||
static BYTE DefaultGateway[] = {192, 168, 1, 1}; // set to zero if not available or required
|
||||
static BYTE DNSServer[] = {192, 168, 1, 1}; // ditto
|
||||
#elif defined(DHCP) // use DHCP
|
||||
#include <dhcpcapi.h>
|
||||
static BYTE TargetIP[] = { 0, 0, 0, 0}; // will be filled at run-time
|
||||
#else // static IP address assignment (default)
|
||||
static BYTE TargetIP[] = {192, 168, 0, 50};
|
||||
static BYTE NetMask[] = {255, 255, 255, 0};
|
||||
static BYTE DefaultGateway[] = {192, 168, 0, 1}; // set to zero if not available or required
|
||||
static BYTE DNSServer[] = {192, 168, 0, 1}; // ditto
|
||||
#if defined(AUTO_IP) // use xn_autoip() to get an IP address
|
||||
static BYTE TargetIP[] = { 0, 0, 0, 0 }; // will be filled at run-time
|
||||
static BYTE NetMask[] = { 255, 255, 255, 0 };
|
||||
static BYTE MinIP[] = { 192, 168, 1, 128 };
|
||||
static BYTE MaxIP[] = { 192, 168, 1, 255 };
|
||||
static BYTE DefaultGateway[] = { 192, 168, 1, 1 }; // set to zero if not available or required
|
||||
static BYTE DNSServer[] = { 192, 168, 1, 1 }; // ditto
|
||||
#elif defined(DHCP) // use DHCP
|
||||
#include <dhcpcapi.h>
|
||||
static BYTE TargetIP[] = { 0, 0, 0, 0 }; // will be filled at run-time
|
||||
#else // static IP address assignment (default)
|
||||
static BYTE TargetIP[] = { 192, 168, 0, 50 };
|
||||
static BYTE NetMask[] = { 255, 255, 255, 0 };
|
||||
static BYTE DefaultGateway[] = { 192, 168, 0, 1 }; // set to zero if not available or required
|
||||
static BYTE DNSServer[] = { 192, 168, 0, 1 }; // ditto
|
||||
#endif
|
||||
|
||||
#define DEVICE_ID DAVICOM_DEVICE // define your device type here
|
||||
#define DEVICE_ID DAVICOM_DEVICE // define your device type here
|
||||
|
||||
#ifndef DEVICE_ID
|
||||
#error You must define Ethernet driver/resources and IP address/net mask here
|
||||
#error You must define Ethernet driver/resources and IP address/net mask here
|
||||
#endif
|
||||
|
||||
// The following values are ignored for PCI devices (the BIOS supplies
|
||||
// them), but they must be set correctly for ISA/PCMCIA systems and for
|
||||
// PCI devices if you do not have a BIOS
|
||||
|
||||
#define ED_IO_ADD 0x300 // I/O address of the device
|
||||
#define ED_IRQ 5 // IRQ of the device
|
||||
#define ED_MEM_ADD 0 // Memory Window (only some devices)
|
||||
#define ED_IO_ADD 0x300 // I/O address of the device
|
||||
#define ED_IRQ 5 // IRQ of the device
|
||||
#define ED_MEM_ADD 0 // Memory Window (only some devices)
|
||||
|
||||
// Define function to pull in the required driver
|
||||
|
||||
#if DEVICE_ID == NE2000_DEVICE
|
||||
#define BIND_DRIVER xn_bind_ne2000
|
||||
#define BIND_DRIVER xn_bind_ne2000
|
||||
#elif DEVICE_ID == N83815_DEVICE
|
||||
#define BIND_DRIVER xn_bind_n83815
|
||||
#define BIND_DRIVER xn_bind_n83815
|
||||
#elif DEVICE_ID == TC90X_DEVICE
|
||||
#define BIND_DRIVER xn_bind_tc90x
|
||||
#define BIND_DRIVER xn_bind_tc90x
|
||||
#elif DEVICE_ID == SMC91C9X_DEVICE
|
||||
#define BIND_DRIVER xn_bind_smc91c9x
|
||||
#define BIND_DRIVER xn_bind_smc91c9x
|
||||
#elif DEVICE_ID == LANCE_DEVICE
|
||||
#define BIND_DRIVER xn_bind_rtlance
|
||||
#define BIND_DRIVER xn_bind_rtlance
|
||||
#elif DEVICE_ID == LANCE_ISA_DEVICE
|
||||
#define BIND_DRIVER xn_bind_lance_isa
|
||||
#define BIND_DRIVER xn_bind_lance_isa
|
||||
#elif DEVICE_ID == LAN_CS89X0_DEVICE
|
||||
#define BIND_DRIVER xn_bind_cs
|
||||
#define BIND_DRIVER xn_bind_cs
|
||||
#elif DEVICE_ID == I82559_DEVICE
|
||||
#define BIND_DRIVER xn_bind_i82559
|
||||
#define BIND_DRIVER xn_bind_i82559
|
||||
#elif DEVICE_ID == R8139_DEVICE
|
||||
#define BIND_DRIVER xn_bind_r8139
|
||||
#define BIND_DRIVER xn_bind_r8139
|
||||
#elif DEVICE_ID == DAVICOM_DEVICE
|
||||
#define BIND_DRIVER xn_bind_davicom
|
||||
#define BIND_DRIVER xn_bind_davicom
|
||||
#elif DEVICE_ID == RHINE_DEVICE
|
||||
#define BIND_DRIVER xn_bind_rhine
|
||||
#define BIND_DRIVER xn_bind_rhine
|
||||
#elif DEVICE_ID == AX172_DEVICE
|
||||
#include <rtusb.h> // must also link Rtusb.lib and UsbInit.cpp
|
||||
#define BIND_DRIVER xn_bind_ax172
|
||||
#include <rtusb.h> // must also link Rtusb.lib and UsbInit.cpp
|
||||
#define BIND_DRIVER xn_bind_ax172
|
||||
#elif DEVICE_ID == AX772_DEVICE
|
||||
#include <rtusb.h> // must also link Rtusb.lib and UsbInit.cpp
|
||||
#define BIND_DRIVER xn_bind_ax772
|
||||
#include <rtusb.h> // must also link Rtusb.lib and UsbInit.cpp
|
||||
#define BIND_DRIVER xn_bind_ax772
|
||||
#elif DEVICE_ID == PRISM_DEVICE
|
||||
#include <wlanapi.h> // must also link Wlan.lib
|
||||
#define BIND_DRIVER xn_bind_prism
|
||||
#include <wlanapi.h> // must also link Wlan.lib
|
||||
#define BIND_DRIVER xn_bind_prism
|
||||
#elif DEVICE_ID == PRISM_PCMCIA_DEVICE
|
||||
#include <rtpcmcia.h>
|
||||
#include <wlanapi.h> // must also link Wlan.lib
|
||||
#define BIND_DRIVER xn_bind_prism_pcmcia
|
||||
#include <rtpcmcia.h>
|
||||
#include <wlanapi.h> // must also link Wlan.lib
|
||||
#define BIND_DRIVER xn_bind_prism_pcmcia
|
||||
#else
|
||||
#error Invalid DEVICE_ID value
|
||||
#error Invalid DEVICE_ID value
|
||||
#endif
|
||||
|
||||
@@ -40,122 +40,115 @@ static int RS485_Port = COM2;
|
||||
/* baud rate */
|
||||
static long RS485_Baud = 38400;
|
||||
/* io base address */
|
||||
static long RS485_Base = 0;
|
||||
static long RS485_Base = 0;
|
||||
/* hardware IRQ number */
|
||||
static long RS485_IRQ_Number = 0;
|
||||
static long RS485_IRQ_Number = 0;
|
||||
|
||||
static void RS485_Standard_Port_Settings(long port, long *pIRQ, long *pBase)
|
||||
static void RS485_Standard_Port_Settings(long port, long *pIRQ,
|
||||
long *pBase)
|
||||
{
|
||||
switch (port)
|
||||
{
|
||||
switch (port) {
|
||||
case COM1:
|
||||
*pBase = (long)0x3F8;
|
||||
*pIRQ = 4L;
|
||||
break;
|
||||
*pBase = (long) 0x3F8;
|
||||
*pIRQ = 4L;
|
||||
break;
|
||||
case COM2:
|
||||
*pBase = (long)0x2F8;
|
||||
*pIRQ = 3L;
|
||||
break;
|
||||
*pBase = (long) 0x2F8;
|
||||
*pIRQ = 3L;
|
||||
break;
|
||||
case COM3:
|
||||
*pBase = (long)0x3E8;
|
||||
*pIRQ = 4L;
|
||||
break;
|
||||
*pBase = (long) 0x3E8;
|
||||
*pIRQ = 4L;
|
||||
break;
|
||||
case COM4:
|
||||
*pBase = (long)0x2E8;
|
||||
*pIRQ = 3L;
|
||||
break;
|
||||
*pBase = (long) 0x2E8;
|
||||
*pIRQ = 3L;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static RS485_Open_Port(
|
||||
int port, /* COM port number - COM1 = 0 */
|
||||
long baud, /* baud rate */
|
||||
unsigned base, /* io base address */
|
||||
int irq) /* hardware IRQ number */
|
||||
{
|
||||
/* setup the COM IO */
|
||||
SetIOBase(port, base);
|
||||
SetIRQ(port, irq);
|
||||
static RS485_Open_Port(int port, /* COM port number - COM1 = 0 */
|
||||
long baud, /* baud rate */
|
||||
unsigned base, /* io base address */
|
||||
int irq)
|
||||
{ /* hardware IRQ number */
|
||||
/* setup the COM IO */
|
||||
SetIOBase(port, base);
|
||||
SetIRQ(port, irq);
|
||||
|
||||
if (irq < 8)
|
||||
RTKIRQTopPriority(irq,9);
|
||||
if (irq < 8)
|
||||
RTKIRQTopPriority(irq, 9);
|
||||
|
||||
InitPort(port, baud, PARITY_NONE, 1, 8);
|
||||
InitPort(port, baud, PARITY_NONE, 1, 8);
|
||||
|
||||
if (HasFIFO(port))
|
||||
EnableFIFO(port,8);
|
||||
EnableCOMInterrupt(port, 1024*4);
|
||||
if (HasFIFO(port))
|
||||
EnableFIFO(port, 8);
|
||||
EnableCOMInterrupt(port, 1024 * 4);
|
||||
|
||||
/* enable the 485 via the DTR pin */
|
||||
RS485_IO_ENABLE(port);
|
||||
RS485_RECEIVE_ENABLE(port);
|
||||
/* enable the 485 via the DTR pin */
|
||||
RS485_IO_ENABLE(port);
|
||||
RS485_RECEIVE_ENABLE(port);
|
||||
|
||||
return;
|
||||
return;
|
||||
}
|
||||
|
||||
void RS485_Initialize(void)
|
||||
{
|
||||
RS485_Standard_Port_Settings(RS485_Port, &RS485_IRQ_Number, &RS485_Base);
|
||||
RS485_Open_Port(RS485_Port, RS485_Baud, RS485_Base, RS485_IRQ_Number);
|
||||
RS485_Standard_Port_Settings(RS485_Port, &RS485_IRQ_Number,
|
||||
&RS485_Base);
|
||||
RS485_Open_Port(RS485_Port, RS485_Baud, RS485_Base, RS485_IRQ_Number);
|
||||
}
|
||||
|
||||
void RS485_Send_Frame(
|
||||
volatile struct mstp_port_struct_t *mstp_port, // port specific data
|
||||
uint8_t *buffer, // frame to send (up to 501 bytes of data)
|
||||
uint16_t nbytes) // number of bytes of data (up to 501)
|
||||
void RS485_Send_Frame(volatile struct mstp_port_struct_t *mstp_port, // port specific data
|
||||
uint8_t * buffer, // frame to send (up to 501 bytes of data)
|
||||
uint16_t nbytes) // number of bytes of data (up to 501)
|
||||
{
|
||||
bool status = true; // return value
|
||||
bool status = true; // return value
|
||||
|
||||
(void)mstp_port;
|
||||
RS485_TRANSMIT_ENABLE(RS485_Port);
|
||||
SendBlock(RS485_Port, (char *)buffer, nbytes);
|
||||
/* need to wait at least 9600 baud * 512 bytes = 54mS */
|
||||
(void)WaitSendBufferEmpty(RS485_Port,MilliSecsToTicks(200));
|
||||
while (!(LineStatus(RS485_Port) & TX_SHIFT_EMPTY))
|
||||
RTKScheduler();
|
||||
RS485_RECEIVE_ENABLE(RS485_Port);
|
||||
(void) mstp_port;
|
||||
RS485_TRANSMIT_ENABLE(RS485_Port);
|
||||
SendBlock(RS485_Port, (char *) buffer, nbytes);
|
||||
/* need to wait at least 9600 baud * 512 bytes = 54mS */
|
||||
(void) WaitSendBufferEmpty(RS485_Port, MilliSecsToTicks(200));
|
||||
while (!(LineStatus(RS485_Port) & TX_SHIFT_EMPTY))
|
||||
RTKScheduler();
|
||||
RS485_RECEIVE_ENABLE(RS485_Port);
|
||||
|
||||
return;
|
||||
return;
|
||||
}
|
||||
|
||||
void RS485_Check_UART_Data(
|
||||
volatile struct mstp_port_struct_t *mstp_port) // port specific data
|
||||
void RS485_Check_UART_Data(volatile struct mstp_port_struct_t *mstp_port) // port specific data
|
||||
{
|
||||
COMData com_data = 0; /* byte from COM driver */
|
||||
unsigned timeout = 10; // milliseconds to wait for a character
|
||||
Duration ticks; /* duration to wait for data */
|
||||
COMData com_data = 0; /* byte from COM driver */
|
||||
unsigned timeout = 10; // milliseconds to wait for a character
|
||||
Duration ticks; /* duration to wait for data */
|
||||
|
||||
if (mstp_port->ReceiveError)
|
||||
{
|
||||
// wait for state machine to clear this
|
||||
}
|
||||
// wait for state machine to read from the DataRegister
|
||||
else if (!mstp_port->DataAvailable)
|
||||
{
|
||||
// check for data
|
||||
ticks = MilliSecsToTicks(timeout);
|
||||
if (!ticks)
|
||||
ticks = 1;
|
||||
if (RTKGetTimed(ReceiveBuffer[RS485_Port],&com_data,ticks))
|
||||
{
|
||||
// if error,
|
||||
if (com_data & (COM_OVERRUN << 8))
|
||||
mstp_port->ReceiveError = true;
|
||||
else if (com_data & (COM_FRAME << 8))
|
||||
mstp_port->ReceiveError = true;
|
||||
else
|
||||
{
|
||||
mstp_port->DataRegister = com_data & 0x00FF;
|
||||
mstp_port->DataAvailable = true;
|
||||
}
|
||||
if (mstp_port->ReceiveError) {
|
||||
// wait for state machine to clear this
|
||||
}
|
||||
// wait for state machine to read from the DataRegister
|
||||
else if (!mstp_port->DataAvailable) {
|
||||
// check for data
|
||||
ticks = MilliSecsToTicks(timeout);
|
||||
if (!ticks)
|
||||
ticks = 1;
|
||||
if (RTKGetTimed(ReceiveBuffer[RS485_Port], &com_data, ticks)) {
|
||||
// if error,
|
||||
if (com_data & (COM_OVERRUN << 8))
|
||||
mstp_port->ReceiveError = true;
|
||||
else if (com_data & (COM_FRAME << 8))
|
||||
mstp_port->ReceiveError = true;
|
||||
else {
|
||||
mstp_port->DataRegister = com_data & 0x00FF;
|
||||
mstp_port->DataAvailable = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void RS485_Process_Tx_Message(void)
|
||||
{
|
||||
// nothing to do
|
||||
}
|
||||
|
||||
|
||||
@@ -4,25 +4,25 @@
|
||||
// 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
|
||||
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
|
||||
#define FALSE 0
|
||||
#endif
|
||||
|
||||
#ifndef TRUE
|
||||
#define TRUE 1
|
||||
#define TRUE 1
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
@@ -6,14 +6,14 @@
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
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 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 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
|
||||
#endif // STDINT_H
|
||||
|
||||
Reference in New Issue
Block a user