diff --git a/bacnet-stack/ports/rtos32/bip.c b/bacnet-stack/ports/rtos32/bip.c index dd4650f5..66530947 100644 --- a/bacnet-stack/ports/rtos32/bip.c +++ b/bacnet-stack/ports/rtos32/bip.c @@ -159,7 +159,7 @@ static int bip_send( else mtu[1] = 0x0A; /* Original-Unicast-NPDU */ mtu_len = 2; - mtu_len += encode_unsigned16(&mtu[mtu_len], pdu_len); + mtu_len += encode_unsigned16(&mtu[mtu_len], pdu_len + 4 /*inclusive*/); memcpy(&mtu[mtu_len], pdu, pdu_len); mtu_len += pdu_len; diff --git a/bacnet-stack/ports/win32/bacnet.ide b/bacnet-stack/ports/win32/bacnet.ide index 14318abb..4a4840bb 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 new file mode 100644 index 00000000..bd05c6be --- /dev/null +++ b/bacnet-stack/ports/win32/bacnet/bacnet.dsp @@ -0,0 +1,316 @@ +# Microsoft Developer Studio Project File - Name="bacnet" - Package Owner=<4> +# Microsoft Developer Studio Generated Build File, Format Version 6.00 +# ** DO NOT EDIT ** + +# TARGTYPE "Win32 (x86) Console Application" 0x0103 + +CFG=bacnet - Win32 Debug +!MESSAGE This is not a valid makefile. To build this project using NMAKE, +!MESSAGE use the Export Makefile command and run +!MESSAGE +!MESSAGE NMAKE /f "bacnet.mak". +!MESSAGE +!MESSAGE You can specify a configuration when running NMAKE +!MESSAGE by defining the macro CFG on the command line. For example: +!MESSAGE +!MESSAGE NMAKE /f "bacnet.mak" CFG="bacnet - Win32 Debug" +!MESSAGE +!MESSAGE Possible choices for configuration are: +!MESSAGE +!MESSAGE "bacnet - Win32 Release" (based on "Win32 (x86) Console Application") +!MESSAGE "bacnet - Win32 Debug" (based on "Win32 (x86) Console Application") +!MESSAGE + +# Begin Project +# PROP AllowPerConfigDependencies 0 +# PROP Scc_ProjName "" +# PROP Scc_LocalPath "" +CPP=cl.exe +RSC=rc.exe + +!IF "$(CFG)" == "bacnet - Win32 Release" + +# PROP BASE Use_MFC 0 +# PROP BASE Use_Debug_Libraries 0 +# PROP BASE Output_Dir "Release" +# PROP BASE Intermediate_Dir "Release" +# PROP BASE Target_Dir "" +# PROP Use_MFC 0 +# PROP Use_Debug_Libraries 0 +# PROP Output_Dir "Release" +# PROP Intermediate_Dir "Release" +# PROP Ignore_Export_Lib 0 +# PROP Target_Dir "" +# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c +# ADD CPP /nologo /W3 /GX /O2 /I "c:\code\bacnet-stack\\" /I "c:\code\bacnet-stack\ports\win32\\" /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /FD /c +# SUBTRACT CPP /YX +# ADD BASE RSC /l 0x409 /d "NDEBUG" +# ADD RSC /l 0x409 /d "NDEBUG" +BSC32=bscmake.exe +# ADD BASE BSC32 /nologo +# ADD BSC32 /nologo +LINK32=link.exe +# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386 +# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib wsock32.dll /nologo /subsystem:console /machine:I386 + +!ELSEIF "$(CFG)" == "bacnet - Win32 Debug" + +# PROP BASE Use_MFC 0 +# PROP BASE Use_Debug_Libraries 1 +# PROP BASE Output_Dir "Debug" +# PROP BASE Intermediate_Dir "Debug" +# PROP BASE Target_Dir "" +# PROP Use_MFC 0 +# PROP Use_Debug_Libraries 1 +# PROP Output_Dir "Debug" +# PROP Intermediate_Dir "Debug" +# 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 +# SUBTRACT CPP /YX +# ADD BASE RSC /l 0x409 /d "_DEBUG" +# ADD RSC /l 0x409 /d "_DEBUG" +BSC32=bscmake.exe +# ADD BASE BSC32 /nologo +# ADD BSC32 /nologo +LINK32=link.exe +# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept +# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib wsock32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept + +!ENDIF + +# Begin Target + +# Name "bacnet - Win32 Release" +# Name "bacnet - Win32 Debug" +# Begin Group "Source Files" + +# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat" +# Begin Source File + +SOURCE=..\..\..\abort.c +# End Source File +# Begin Source File + +SOURCE=..\..\..\address.c +# End Source File +# Begin Source File + +SOURCE=..\..\..\ai.c +# End Source File +# Begin Source File + +SOURCE=..\..\..\ao.c +# End Source File +# Begin Source File + +SOURCE=..\..\..\apdu.c +# End Source File +# Begin Source File + +SOURCE=..\..\..\bacdcode.c +# End Source File +# Begin Source File + +SOURCE=..\..\..\bacerror.c +# End Source File +# Begin Source File + +SOURCE=..\..\..\bigend.c +# End Source File +# Begin Source File + +SOURCE=..\bip.c +# End Source File +# Begin Source File + +SOURCE=..\..\..\crc.c +# End Source File +# Begin Source File + +SOURCE=..\..\..\datalink.c +# End Source File +# Begin Source File + +SOURCE=..\..\..\device.c +# End Source File +# Begin Source File + +SOURCE=..\..\..\handlers.c +# End Source File +# Begin Source File + +SOURCE=..\..\..\iam.c +# End Source File +# Begin Source File + +SOURCE=..\main.c +# End Source File +# Begin Source File + +SOURCE=..\..\..\npdu.c +# End Source File +# Begin Source File + +SOURCE=..\..\..\reject.c +# End Source File +# Begin Source File + +SOURCE=..\..\..\ringbuf.c +# End Source File +# Begin Source File + +SOURCE=..\..\..\rp.c +# End Source File +# Begin Source File + +SOURCE=..\..\..\tsm.c +# End Source File +# Begin Source File + +SOURCE=..\..\..\whois.c +# End Source File +# Begin Source File + +SOURCE=..\..\..\wp.c +# End Source File +# End Group +# Begin Group "Header Files" + +# PROP Default_Filter "h;hpp;hxx;hm;inl" +# Begin Source File + +SOURCE=..\..\..\abort.h +# End Source File +# Begin Source File + +SOURCE=..\..\..\address.h +# End Source File +# Begin Source File + +SOURCE=..\..\..\ai.h +# End Source File +# Begin Source File + +SOURCE=..\..\..\ao.h +# End Source File +# Begin Source File + +SOURCE=..\..\..\apdu.h +# End Source File +# Begin Source File + +SOURCE=..\..\..\arcnet.h +# End Source File +# Begin Source File + +SOURCE=..\..\..\bacdcode.h +# End Source File +# Begin Source File + +SOURCE=..\..\..\bacdef.h +# End Source File +# Begin Source File + +SOURCE=..\..\..\bacenum.h +# End Source File +# Begin Source File + +SOURCE=..\..\..\bacerror.h +# End Source File +# Begin Source File + +SOURCE=..\..\..\bigend.h +# End Source File +# Begin Source File + +SOURCE=..\..\..\bip.h +# End Source File +# Begin Source File + +SOURCE=..\..\..\bits.h +# End Source File +# Begin Source File + +SOURCE=..\..\..\bytes.h +# End Source File +# Begin Source File + +SOURCE=..\..\..\config.h +# End Source File +# Begin Source File + +SOURCE=..\..\..\crc.h +# End Source File +# Begin Source File + +SOURCE=..\..\..\datalink.h +# End Source File +# Begin Source File + +SOURCE=..\..\..\device.h +# End Source File +# Begin Source File + +SOURCE=..\..\..\ethernet.h +# End Source File +# Begin Source File + +SOURCE=..\..\..\handlers.h +# End Source File +# Begin Source File + +SOURCE=..\..\..\iam.h +# End Source File +# Begin Source File + +SOURCE=..\..\..\mstp.h +# End Source File +# Begin Source File + +SOURCE=..\..\..\npdu.h +# End Source File +# Begin Source File + +SOURCE=..\..\..\reject.h +# End Source File +# Begin Source File + +SOURCE=..\..\..\ringbuf.h +# End Source File +# Begin Source File + +SOURCE=..\..\..\rp.h +# End Source File +# Begin Source File + +SOURCE=..\..\..\rs485.h +# End Source File +# Begin Source File + +SOURCE=..\stdbool.h +# End Source File +# Begin Source File + +SOURCE=..\stdint.h +# End Source File +# Begin Source File + +SOURCE=..\..\..\tsm.h +# End Source File +# Begin Source File + +SOURCE=..\..\..\whois.h +# End Source File +# Begin Source File + +SOURCE=..\..\..\wp.h +# End Source File +# End Group +# Begin Group "Resource Files" + +# PROP Default_Filter "ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe" +# End Group +# End Target +# End Project diff --git a/bacnet-stack/ports/win32/bacnet/bacnet.dsw b/bacnet-stack/ports/win32/bacnet/bacnet.dsw new file mode 100644 index 00000000..71d4691d --- /dev/null +++ b/bacnet-stack/ports/win32/bacnet/bacnet.dsw @@ -0,0 +1,29 @@ +Microsoft Developer Studio Workspace File, Format Version 6.00 +# WARNING: DO NOT EDIT OR DELETE THIS WORKSPACE FILE! + +############################################################################### + +Project: "bacnet"=".\bacnet.dsp" - Package Owner=<4> + +Package=<5> +{{{ +}}} + +Package=<4> +{{{ +}}} + +############################################################################### + +Global: + +Package=<5> +{{{ +}}} + +Package=<3> +{{{ +}}} + +############################################################################### + diff --git a/bacnet-stack/ports/win32/bacnet/bacnet.ncb b/bacnet-stack/ports/win32/bacnet/bacnet.ncb new file mode 100644 index 00000000..02fd9db5 Binary files /dev/null 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 new file mode 100644 index 00000000..3e48cae6 Binary files /dev/null and b/bacnet-stack/ports/win32/bacnet/bacnet.opt differ diff --git a/bacnet-stack/ports/win32/bacnet/bacnet.plg b/bacnet-stack/ports/win32/bacnet/bacnet.plg new file mode 100644 index 00000000..7574dc05 --- /dev/null +++ b/bacnet-stack/ports/win32/bacnet/bacnet.plg @@ -0,0 +1,53 @@ + + +
+

Build Log

+

+--------------------Configuration: bacnet - Win32 Debug-------------------- +

+

Command Lines

+Creating temporary file "C:\DOCUME~1\stk01\LOCALS~1\Temp\RSPE11.tmp" with contents +[ +/nologo /MLd /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" /Fo"Debug/" /Fd"Debug/" /FD /GZ /c +"C:\code\bacnet-stack\ports\win32\main.c" +] +Creating command line "cl.exe @C:\DOCUME~1\stk01\LOCALS~1\Temp\RSPE11.tmp" +Creating temporary file "C:\DOCUME~1\stk01\LOCALS~1\Temp\RSPE12.tmp" with contents +[ +kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib wsock32.lib /nologo /subsystem:console /incremental:yes /pdb:"Debug/bacnet.pdb" /debug /machine:I386 /out:"Debug/bacnet.exe" /pdbtype:sept +".\Debug\bip.obj" +".\Debug\main.obj" +".\Debug\abort.obj" +".\Debug\address.obj" +".\Debug\ai.obj" +".\Debug\ao.obj" +".\Debug\apdu.obj" +".\Debug\bacdcode.obj" +".\Debug\bacerror.obj" +".\Debug\bigend.obj" +".\Debug\crc.obj" +".\Debug\datalink.obj" +".\Debug\device.obj" +".\Debug\handlers.obj" +".\Debug\iam.obj" +".\Debug\npdu.obj" +".\Debug\reject.obj" +".\Debug\ringbuf.obj" +".\Debug\rp.obj" +".\Debug\tsm.obj" +".\Debug\whois.obj" +".\Debug\wp.obj" +] +Creating command line "link.exe @C:\DOCUME~1\stk01\LOCALS~1\Temp\RSPE12.tmp" +

Output Window

+Compiling... +main.c +Linking... + + + +

Results

+bacnet.exe - 0 error(s), 0 warning(s) +
+ + diff --git a/bacnet-stack/ports/win32/bip.c b/bacnet-stack/ports/win32/bip.c index 0d0065c1..b111cad0 100644 --- a/bacnet-stack/ports/win32/bip.c +++ b/bacnet-stack/ports/win32/bip.c @@ -91,6 +91,11 @@ void bip_set_address(uint8_t octet1, uint8_t octet2, set_network_address(&BIP_Address, octet1, octet2, octet3, octet4); } +void bip_set_addr(struct in_addr *net_address) +{ + BIP_Address.s_addr = htonl(net_address->s_addr); +} + void bip_set_broadcast_address(uint8_t octet1, uint8_t octet2, uint8_t octet3, uint8_t octet4) { @@ -117,8 +122,6 @@ bool bip_init(void) int value = 1; /* local broadcast address */ - //BIP_Broadcast_Address.s_addr = BIP_Address.s_addr; - //BIP_Broadcast_Address.s_addr |= ~(BIP_Subnet_Mask.s_addr); BIP_Broadcast_Address.s_addr = INADDR_BROADCAST; /* configure standard BACnet/IP port */ bip_set_port(0xBAC0); @@ -128,6 +131,26 @@ bool bip_init(void) if (BIP_Socket < 0) return false; + // Allow us to use the same socket for sending and receiving + // This makes sure that the src port is correct when sending + rv = setsockopt(BIP_Socket, SOL_SOCKET, SO_REUSEADDR, + (char *)&value, sizeof(value)); + if (rv < 0) + { + close(BIP_Socket); + BIP_Socket = -1; + return false; + } + // allow us to send a broadcast + rv = setsockopt(BIP_Socket, SOL_SOCKET, SO_BROADCAST, + (char *)&value, sizeof(value)); + if (rv < 0) + { + close(BIP_Socket); + BIP_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); @@ -167,7 +190,7 @@ static int bip_send( else mtu[1] = 0x0A; /* Original-Unicast-NPDU */ mtu_len = 2; - mtu_len += encode_unsigned16(&mtu[mtu_len], pdu_len); + mtu_len += encode_unsigned16(&mtu[mtu_len], pdu_len + 4 /*inclusive*/); memcpy(&mtu[mtu_len], pdu, pdu_len); mtu_len += pdu_len; diff --git a/bacnet-stack/ports/win32/main.c b/bacnet-stack/ports/win32/main.c index e7ec2b09..502dcd7d 100644 --- a/bacnet-stack/ports/win32/main.c +++ b/bacnet-stack/ports/win32/main.c @@ -40,19 +40,16 @@ // buffer used for receive static uint8_t Rx_Buf[MAX_MPDU] = {0}; -static BYTE TargetIP[] = {192, 168, 0, 191}; -static BYTE NetMask[] = {255, 255, 255, 0}; - static void Init_Device_Parameters(void) { // configure my initial values - Device_Set_Object_Instance_Number(126); + Device_Set_Object_Instance_Number(124); Device_Set_Vendor_Name("Lithonia Lighting"); Device_Set_Vendor_Identifier(42); - Device_Set_Model_Name("Simple BACnet Server"); + Device_Set_Model_Name("Simple BACnet Client"); Device_Set_Firmware_Revision("1.00"); - Device_Set_Application_Software_Version("none"); - Device_Set_Description("Example of a simple BACnet server"); + Device_Set_Application_Software_Version("win32"); + Device_Set_Description("Example of a simple BACnet client/server"); return; } @@ -76,12 +73,31 @@ static void Init_Service_Handlers(void) WritePropertyHandler); } +/* To fill a need, we invent the gethostaddr() function. */ +long gethostaddr(void) +{ + struct hostent *host_ent; + char host_name[255]; + + if (gethostname(host_name, sizeof(host_name)) == 0) + return -1; + + if ((host_ent = gethostbyname(host_name)) == NULL) + return -1; + + return *(long *)host_ent->h_addr; +} + +extern void bip_set_addr(struct in_addr *net_address); + static void NetInitialize(void) // initialize the TCP/IP stack { int Result; int Code; WSADATA wd; + struct in_addr address; + Result = WSAStartup(MAKEWORD(2,2), &wd); @@ -92,6 +108,8 @@ static void NetInitialize(void) Code); exit(1); } + address.s_addr = gethostaddr(); + bip_set_addr(&address); } int main(int argc, char *argv[]) @@ -106,8 +124,6 @@ int main(int argc, char *argv[]) Init_Service_Handlers(); // init the data link layer NetInitialize(); - bip_set_address(TargetIP[0], TargetIP[1], TargetIP[2], TargetIP[3]); - bip_set_address(NetMask[0], NetMask[1], NetMask[2], NetMask[3]); if (!bip_init()) return 1;