diff --git a/bacnet-stack/ports/linux/bip-init.c b/bacnet-stack/ports/linux/bip-init.c index 1237467f..ef79a231 100644 --- a/bacnet-stack/ports/linux/bip-init.c +++ b/bacnet-stack/ports/linux/bip-init.c @@ -123,7 +123,7 @@ bool bip_init(void) sin.sin_family = AF_INET; sin.sin_addr.s_addr = htonl(INADDR_ANY); sin.sin_port = htons(bip_get_port()); - memset(&(sin.sin_zero), '\0', 8); + memset(&(sin.sin_zero), '\0', sizeof(sin.sin_zero)); status = bind(sock_fd, (const struct sockaddr*)&sin, sizeof(struct sockaddr)); if (status < 0) diff --git a/bacnet-stack/ports/win32/bacnet.ide b/bacnet-stack/ports/win32/bacnet.ide index 08db2149..b90772d3 100644 Binary files a/bacnet-stack/ports/win32/bacnet.ide and b/bacnet-stack/ports/win32/bacnet.ide differ diff --git a/bacnet-stack/ports/win32/bacnet/bacnet.dsp b/bacnet-stack/ports/win32/bacnet/bacnet.dsp index 6bab091a..6ced4a8e 100644 --- a/bacnet-stack/ports/win32/bacnet/bacnet.dsp +++ b/bacnet-stack/ports/win32/bacnet/bacnet.dsp @@ -67,7 +67,7 @@ LINK32=link.exe # PROP Ignore_Export_Lib 0 # PROP Target_Dir "" # ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c -# ADD CPP /nologo /W3 /Gm /GX /ZI /Od /I "c:\code\bacnet-stack\\" /I "c:\code\bacnet-stack\ports\win32\\" /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /D "BACDL_BIP" /FD /GZ /c +# ADD CPP /nologo /W3 /Gm /GX /ZI /Od /I "c:\code\bacnet-stack\\" /I "c:\code\bacnet-stack\ports\win32\\" /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /D "BACDL_BIP" /FR /FD /GZ /c # SUBTRACT CPP /YX # ADD BASE RSC /l 0x409 /d "_DEBUG" # ADD RSC /l 0x409 /d "_DEBUG" @@ -125,11 +125,19 @@ SOURCE=..\..\..\bacfile.c # End Source File # Begin Source File +SOURCE=..\..\..\bactext.c +# End Source File +# Begin Source File + SOURCE=..\..\..\bigend.c # End Source File # Begin Source File -SOURCE=..\bip.c +SOURCE="..\bip-init.c" +# End Source File +# Begin Source File + +SOURCE=..\..\..\bip.c # End Source File # Begin Source File @@ -153,6 +161,10 @@ SOURCE=..\..\..\iam.c # End Source File # Begin Source File +SOURCE=..\..\..\indtext.c +# End Source File +# Begin Source File + SOURCE=..\main.c # End Source File # Begin Source File diff --git a/bacnet-stack/ports/win32/bacnet/bacnet.ncb b/bacnet-stack/ports/win32/bacnet/bacnet.ncb index 608816c1..43f68495 100644 Binary files a/bacnet-stack/ports/win32/bacnet/bacnet.ncb and b/bacnet-stack/ports/win32/bacnet/bacnet.ncb differ diff --git a/bacnet-stack/ports/win32/bacnet/bacnet.opt b/bacnet-stack/ports/win32/bacnet/bacnet.opt index 962bdcfb..0a49b61e 100644 Binary files a/bacnet-stack/ports/win32/bacnet/bacnet.opt and b/bacnet-stack/ports/win32/bacnet/bacnet.opt differ diff --git a/bacnet-stack/ports/win32/bip-init.c b/bacnet-stack/ports/win32/bip-init.c index a2ae9250..5aeb083f 100644 --- a/bacnet-stack/ports/win32/bip-init.c +++ b/bacnet-stack/ports/win32/bip-init.c @@ -80,6 +80,11 @@ static void set_broadcast_address(uint32_t net_address) #endif } +static void cleanup(void) +{ + WSACleanup(); +} + bool bip_init(void) { int rv = 0; // return from socket lib calls @@ -92,7 +97,6 @@ bool bip_init(void) struct in_addr address; Result = WSAStartup(MAKEWORD(2,2), &wd); - if (Result != 0) { Code = WSAGetLastError(); @@ -100,6 +104,8 @@ bool bip_init(void) Code); exit(1); } + atexit(cleanup); + address.s_addr = gethostaddr(); if (address.s_addr == (unsigned)-1) { @@ -116,7 +122,7 @@ bool bip_init(void) sock_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); bip_set_socket(sock_fd); if (sock_fd < 0) - return false; + return false; // Allow us to use the same socket for sending and receiving // This makes sure that the src port is correct when sending @@ -124,25 +130,44 @@ bool bip_init(void) (char *)&value, sizeof(value)); if (rv < 0) { - close(sock_fd); - bip_set_socket(-1); - return false; + close(sock_fd); + bip_set_socket(-1); + return false; } // allow us to send a broadcast rv = setsockopt(sock_fd, SOL_SOCKET, SO_BROADCAST, - (char *)&value, sizeof(value)); + (char *)&value, sizeof(value)); if (rv < 0) { - close(sock_fd); - bip_set_socket(-1); - return false; + close(sock_fd); + bip_set_socket(-1); + return false; } // bind the socket to the local port number and IP address sin.sin_family = AF_INET; - sin.sin_addr.s_addr = htonl(INADDR_ANY); + /* by setting sin.sin_addr.s_addr to INADDR_ANY, + I am telling the IP stack to automatically fill + in the IP address of the machine the process + is running on. + + Some server computers have multiple IP addresses. + A socket bound to one of these will not accept + connections to another address. Frequently you prefer + to allow any one of the computer's IP addresses + to be used for connections. Use INADDR_ANY (0L) to + allow clients to connect using any one of the host's + IP addresses. + + Note: sometimes INADDR_ANY does not let me get + any unicast messages. Not sure why... + */ + /* sin.sin_addr.s_addr = htonl(INADDR_ANY); */ + /* or we could use the specific adapter address + note: already in network byte order */ + sin.sin_addr.s_addr = address.s_addr; sin.sin_port = htons(bip_get_port()); - memset(&(sin.sin_zero), '\0', 8); + memset(&(sin.sin_zero), '\0', sizeof(sin.sin_zero)); rv = bind(sock_fd, (const struct sockaddr*)&sin, sizeof(struct sockaddr)); if (rv < 0) diff --git a/bacnet-stack/ports/win32/main.c b/bacnet-stack/ports/win32/main.c index 54978d02..5b817679 100644 --- a/bacnet-stack/ports/win32/main.c +++ b/bacnet-stack/ports/win32/main.c @@ -29,6 +29,7 @@ #include #include #include +#include /* for kbhit and getch */ #include "iam.h" #include "address.h" #include "config.h" @@ -171,7 +172,7 @@ static void print_address( BACNET_ADDRESS *dest) // destination address { int i = 0; // counter - + if (dest) { printf("%s: ",name); @@ -183,6 +184,30 @@ static void print_address( } } +static void print_address_cache(void) +{ + unsigned i,j; + BACNET_ADDRESS address; + uint32_t device_id = 0; + unsigned max_apdu = 0; + + fprintf(stderr,"Device\tMAC\tMaxAPDU\tNet\n"); + for (i = 0; i < MAX_ADDRESS_CACHE; i++) + { + if (address_get_by_index(i,&device_id, &max_apdu, &address)) + { + fprintf(stderr,"%u\t",device_id); + for (j = 0; j < address.mac_len; j++) + { + fprintf(stderr,"%02X",address.mac[j]); + } + fprintf(stderr,"\t"); + fprintf(stderr,"%hu\t",max_apdu); + fprintf(stderr,"%hu\n",address.net); + } + } +} + int main(int argc, char *argv[]) { BACNET_ADDRESS src = {0}; // address where message came from @@ -200,12 +225,12 @@ int main(int argc, char *argv[]) if (!bip_init()) return 1; - datalink_get_broadcast_address(&broadcast_address); + datalink_get_broadcast_address(&broadcast_address); print_address("Broadcast",&broadcast_address); datalink_get_my_address(&my_address); print_address("Address",&my_address); - - printf("BACnet stack running...\n"); + + printf("BACnet stack running...\n"); // loop forever for (;;) { @@ -243,5 +268,13 @@ int main(int argc, char *argv[]) // output // blink LEDs, Turn on or off outputs, etc + + /* wait for ESC from keyboard before quitting */ + if (kbhit() && (getch() == 0x1B)) + break; } + + print_address_cache(); + + return 0; }