From d2cf3699382cb5a6269d92f2f11eea56975c72c9 Mon Sep 17 00:00:00 2001 From: skarg Date: Mon, 2 May 2005 21:03:54 +0000 Subject: [PATCH] adding rtos-32 support --- bacnet-stack/ports/rtos32/ethernet.c | 66 ++++-- bacnet-stack/ports/rtos32/main.c | 299 +++++++++++++++++++++---- bacnet-stack/ports/rtos32/makefile.mak | 4 +- bacnet-stack/ports/rtos32/setvars.bat | 3 + bacnet-stack/ports/rtos32/software.cfg | 52 ++--- 5 files changed, 327 insertions(+), 97 deletions(-) create mode 100644 bacnet-stack/ports/rtos32/setvars.bat diff --git a/bacnet-stack/ports/rtos32/ethernet.c b/bacnet-stack/ports/rtos32/ethernet.c index 61dc7e29..ea5a358e 100644 --- a/bacnet-stack/ports/rtos32/ethernet.c +++ b/bacnet-stack/ports/rtos32/ethernet.c @@ -25,6 +25,16 @@ #include // for standard integer types uint8_t etc. #include // for the standard bool type. +#include // for the standard bool type. +#include // for the standard bool type. +#include +#include +#include +#include +#include +#include "ethernet.h" +#include "bacdcode.h" + // commonly used comparison address for ethernet uint8_t Ethernet_Broadcast[MAX_MAC_LEN] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; @@ -41,9 +51,12 @@ static BYTE NetMask[] = {255, 255, 255, 0}; static BYTE DefaultGateway[] = {0, 0, 0, 0}; // DNS - set to zero if not available or required static BYTE DNSServer[] = {0, 0, 0, 0}; -// the actual socket for Ethernet +// the Interface for Ethernet // SOCKET_ERROR means no open interface static int Ethernet_Interface = SOCKET_ERROR; +static SOCKET Ethernet_Socket = -1; +// used for binding 802.2 +static struct sockaddr Ethernet_Address = { 0 }; bool ethernet_valid(void) { @@ -73,19 +86,22 @@ static int get_local_hwaddr(int iface, unsigned char *mac) mac[4] = ii.my_ethernet_address[4]; mac[5] = ii.my_ethernet_address[5]; - return rv; + return 0; } -static void ethernet_error(const char * Msg) +static void ethernet_error(const char *text) { - printf("%s, error code: %s\n", Msg, xn_geterror_string(WSAGetLastError())); + fprintf(stderr,"%s, error code: %s\n", + text, xn_geterror_string(WSAGetLastError())); exit(1); } bool ethernet_init(char *interface_name) { struct _iface_info ii; // contains the hwaddr of the Ethernet interface - + int value = 1; + int Result = 0; + // FIXME: what about other drivers other than DAVICOM? (void)interface_name; RTKernelInit(0); // get the kernel going @@ -96,12 +112,16 @@ bool ethernet_init(char *interface_name) RTKDelay(1); RTCMOSSetSystemTime(); // get the right time-of-day - Ethernet_Interface = xn_rtip_init(); // Initialize the RTIP stack - if (Ethernet_Interface == SOCKET_ERROR) - ethernet_error("xn_rtip_init failed"); + Result = xn_rtip_init(); // Initialize the RTIP stack + if (Result == SOCKET_ERROR) + ethernet_error("ethernet: xn_rtip_init failed"); atexit(ethernet_cleanup); // make sure the driver is shut down properly RTCallDebugger(RT_DBG_CALLRESET, (DWORD)exit, 0); // even if we get restarted by the debugger + // tell RTIP what Ethernet driver we want + Result = xn_bind_davicom(MINOR_0); + if (Result != 0) + ethernet_error("ethernet: driver initialization failed"); // PCI device ignores the IRQ and IO parameters Ethernet_Interface = xn_interface_open_config( DAVICOM_DEVICE, MINOR_0, 0, 0, 0); @@ -110,6 +130,9 @@ bool ethernet_init(char *interface_name) fprintf(stderr,"ethernet: Davicom driver failed to initialize\r\n"); return false; } + if (xn_interface_opt(Ethernet_Interface,IO_802_2, + (const char *)&value,sizeof(value))) + fprintf(stderr,"ethernet: xn_interface_opt 802.2 failed \n"); xn_interface_info(Ethernet_Interface, &ii); printf("ethernet: MAC address: %02x-%02x-%02x-%02x-%02x-%02x\n", ii.my_ethernet_address[0], ii.my_ethernet_address[1], @@ -129,7 +152,18 @@ bool ethernet_init(char *interface_name) xn_rt_add(RT_DEFAULT, ip_ffaddr, DefaultGateway, 1, Ethernet_Interface, RT_INF); xn_set_server_list((DWORD*)DNSServer, 1); - + + // setup the socket + Ethernet_Socket = socket(AF_INET, SOCK_RAW, 0); + if (Ethernet_Socket < 0) + 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"); + setsockopt(Ethernet_Socket,SOL_SOCKET,SO_802_2,(char *)&value,sizeof(value)); + return ethernet_valid(); } @@ -148,7 +182,7 @@ int ethernet_send( int i = 0; // don't waste time if the socket is not valid - if (Ethernet_Interface < 0) + if (Ethernet_Socket < 0) { fprintf(stderr, "ethernet: 802.2 socket is invalid!\n"); return status; @@ -199,8 +233,8 @@ int ethernet_send( /* Send the packet */ bytes = - sendto(Ethernet_Interface, &mtu, mtu_len, 0, - (struct sockaddr *) ð_addr, sizeof(struct sockaddr)); + sendto(Ethernet_Socket, (const char *)&mtu, mtu_len, 0, + &Ethernet_Address, sizeof(Ethernet_Address)); /* did it get sent? */ if (bytes < 0) { fprintf(stderr,"ethernet: Error sending packet: %s\n", @@ -253,7 +287,7 @@ uint16_t ethernet_receive( struct timeval select_timeout; /* Make sure the socket is open */ - if (Ethernet_Interface <= 0) + if (Ethernet_Socket <= 0) return 0; /* we could just use a non-blocking socket, but that consumes all @@ -271,11 +305,11 @@ uint16_t ethernet_receive( select_timeout.tv_usec = 1000 * timeout; } FD_ZERO(&read_fds); - FD_SET(Ethernet_Interface, &read_fds); - max = Ethernet_Interface; + FD_SET(Ethernet_Socket, &read_fds); + max = Ethernet_Socket; if (select(max + 1, &read_fds, NULL, NULL, &select_timeout) > 0) - received_bytes = read(Ethernet_Interface, &buf[0], MAX_MPDU); + received_bytes = recv(Ethernet_Socket, (char *)&buf[0], MAX_MPDU, 0); else return 0; diff --git a/bacnet-stack/ports/rtos32/main.c b/bacnet-stack/ports/rtos32/main.c index a3ced3b9..8321e652 100644 --- a/bacnet-stack/ports/rtos32/main.c +++ b/bacnet-stack/ports/rtos32/main.c @@ -31,10 +31,13 @@ #include #include "config.h" #include "bacdef.h" +#include "bacdcode.h" #include "npdu.h" #include "apdu.h" #include "device.h" +#include "ai.h" #include "rp.h" +#include "wp.h" #include "iam.h" #include "whois.h" #include "reject.h" @@ -59,6 +62,8 @@ void UnrecognizedServiceHandler( BACNET_ADDRESS src; int pdu_len = 0; + (void)service_request; + (void)service_len; ethernet_get_my_address(&src); // encode the NPDU portion of the packet @@ -142,6 +147,29 @@ void WhoIsHandler( return; } +void IAmHandler( + uint8_t *service_request, + uint16_t service_len, + BACNET_ADDRESS *src) +{ + int len = 0; + uint32_t device_id = 0; + unsigned max_apdu = 0; + int segmentation = 0; + uint16_t vendor_id = 0; + + len = iam_decode_service_request( + service_request, + &device_id, + &max_apdu, + &segmentation, + &vendor_id); + if (len != -1) + fprintf(stderr,"Received I-Am Request from %u!\n",device_id); + + return; +} + void ReadPropertyHandler( uint8_t *service_request, uint16_t service_len, @@ -156,8 +184,8 @@ void ReadPropertyHandler( BACNET_PROPERTY_ID object_property; int32_t array_index; BACNET_ADDRESS my_address; + bool send = false; - fprintf(stderr,"Received Read-Property Request!\n"); len = rp_decode_service_request( service_request, service_len, @@ -165,6 +193,15 @@ void ReadPropertyHandler( &object_instance, &object_property, &array_index); + fprintf(stderr,"Received Read-Property Request!\n"); + if (len > 0) + fprintf(stderr,"type=%u instance=%u property=%u index=%d\n", + object_type, + object_instance, + object_property, + array_index); + else + fprintf(stderr,"Unable to decode Read-Property Request!\n"); // prepare a reply ethernet_get_my_address(&my_address); // encode the NPDU portion of the packet @@ -174,18 +211,15 @@ void ReadPropertyHandler( &my_address, false, // true for confirmed messages MESSAGE_PRIORITY_NORMAL); - // bad encoding - send an abort + // bad decoding - send an abort if (len == -1) { pdu_len += abort_encode_apdu( &Tx_Buf[pdu_len], service_data->invoke_id, ABORT_REASON_OTHER); - (void)ethernet_send_pdu( - src, // destination address - &Tx_Buf[0], - pdu_len); // number of bytes of data fprintf(stderr,"Sent Abort!\n"); + send = true; } else if (service_data->segmented_message) { @@ -193,10 +227,8 @@ void ReadPropertyHandler( &Tx_Buf[pdu_len], service_data->invoke_id, ABORT_REASON_SEGMENTATION_NOT_SUPPORTED); - (void)ethernet_send_pdu( - src, // destination address - &Tx_Buf[0], - pdu_len); // number of bytes of data + fprintf(stderr,"Sent Abort!\n"); + send = true; } else { @@ -205,29 +237,40 @@ void ReadPropertyHandler( case OBJECT_DEVICE: // FIXME: probably need a length limitation sent with encode // FIXME: might need to return error codes - len = Device_Encode_Property_APDU( - &Temp_Buf[0], - object_property, - array_index); - if (len > 0) + if (object_instance == Device_Object_Instance_Number()) { - // encode the APDU portion of the packet - rp_data.object_type = object_type; - rp_data.object_instance = object_instance; - rp_data.object_property = object_property; - rp_data.array_index = array_index; - rp_data.application_data = &Temp_Buf[0]; - rp_data.application_data_len = len; - // FIXME: probably need a length limitation sent with encode - pdu_len += rp_ack_encode_apdu( - &Tx_Buf[pdu_len], - service_data->invoke_id, - &rp_data); - (void)ethernet_send_pdu( - src, // destination address - &Tx_Buf[0], - pdu_len); // number of bytes of data - fprintf(stderr,"Sent Read Property Ack!\n"); + len = Device_Encode_Property_APDU( + &Temp_Buf[0], + object_property, + array_index); + if (len > 0) + { + // encode the APDU portion of the packet + rp_data.object_type = object_type; + rp_data.object_instance = object_instance; + rp_data.object_property = object_property; + rp_data.array_index = array_index; + rp_data.application_data = &Temp_Buf[0]; + rp_data.application_data_len = len; + // FIXME: probably need a length limitation sent with encode + pdu_len += rp_ack_encode_apdu( + &Tx_Buf[pdu_len], + service_data->invoke_id, + &rp_data); + fprintf(stderr,"Sent Read Property Ack!\n"); + send = true; + } + else + { + pdu_len += bacerror_encode_apdu( + &Tx_Buf[pdu_len], + service_data->invoke_id, + SERVICE_CONFIRMED_READ_PROPERTY, + ERROR_CLASS_PROPERTY, + ERROR_CODE_UNKNOWN_PROPERTY); + fprintf(stderr,"Sent Unknown Property Error!\n"); + send = true; + } } else { @@ -235,13 +278,59 @@ void ReadPropertyHandler( &Tx_Buf[pdu_len], service_data->invoke_id, SERVICE_CONFIRMED_READ_PROPERTY, - ERROR_CLASS_PROPERTY, - ERROR_CODE_UNKNOWN_PROPERTY); - (void)ethernet_send_pdu( - src, // destination address - &Tx_Buf[0], - pdu_len); // number of bytes of data - fprintf(stderr,"Sent Unknown Property Error!\n"); + ERROR_CLASS_OBJECT, + ERROR_CODE_UNKNOWN_OBJECT); + fprintf(stderr,"Sent Unknown Object Error!\n"); + send = true; + } + break; + case OBJECT_ANALOG_INPUT: + if (Analog_Input_Valid_Instance(object_instance)) + { + len = Analog_Input_Encode_Property_APDU( + &Temp_Buf[0], + object_instance, + object_property, + array_index); + if (len > 0) + { + // encode the APDU portion of the packet + rp_data.object_type = object_type; + rp_data.object_instance = object_instance; + rp_data.object_property = object_property; + rp_data.array_index = array_index; + rp_data.application_data = &Temp_Buf[0]; + rp_data.application_data_len = len; + // FIXME: probably need a length limitation sent with encode + pdu_len += rp_ack_encode_apdu( + &Tx_Buf[pdu_len], + service_data->invoke_id, + &rp_data); + fprintf(stderr,"Sent Read Property Ack!\n"); + send = true; + } + else + { + pdu_len += bacerror_encode_apdu( + &Tx_Buf[pdu_len], + service_data->invoke_id, + SERVICE_CONFIRMED_READ_PROPERTY, + ERROR_CLASS_PROPERTY, + ERROR_CODE_UNKNOWN_PROPERTY); + fprintf(stderr,"Sent Unknown Property Error!\n"); + send = true; + } + } + else + { + pdu_len += bacerror_encode_apdu( + &Tx_Buf[pdu_len], + service_data->invoke_id, + SERVICE_CONFIRMED_READ_PROPERTY, + ERROR_CLASS_OBJECT, + ERROR_CODE_UNKNOWN_OBJECT); + fprintf(stderr,"Sent Unknown Object Error!\n"); + send = true; } break; default: @@ -251,14 +340,133 @@ void ReadPropertyHandler( SERVICE_CONFIRMED_READ_PROPERTY, ERROR_CLASS_OBJECT, ERROR_CODE_UNKNOWN_OBJECT); - (void)ethernet_send_pdu( - src, // destination address - &Tx_Buf[0], - pdu_len); // number of bytes of data fprintf(stderr,"Sent Unknown Object Error!\n"); + send = true; break; } } + if (send) + { + (void)ethernet_send_pdu( + src, // destination address + &Tx_Buf[0], + pdu_len); // number of bytes of data + } + + return; +} + +void WritePropertyHandler( + uint8_t *service_request, + uint16_t service_len, + BACNET_ADDRESS *src, + BACNET_CONFIRMED_SERVICE_DATA *service_data) +{ + BACNET_WRITE_PROPERTY_DATA wp_data; + int len = 0; + int pdu_len = 0; + BACNET_ADDRESS my_address; + bool send = false; + BACNET_ERROR_CLASS error_class; + BACNET_ERROR_CODE error_code; + + // decode the service request only + len = wp_decode_service_request( + service_request, + service_len, + &wp_data); + fprintf(stderr,"Received Write-Property Request!\n"); + if (len > 0) + fprintf(stderr,"type=%u instance=%u property=%u index=%d\n", + wp_data.object_type, + wp_data.object_instance, + wp_data.object_property, + wp_data.array_index); + else + fprintf(stderr,"Unable to decode Write-Property Request!\n"); + // prepare a reply + ethernet_get_my_address(&my_address); + // encode the NPDU portion of the packet + pdu_len = npdu_encode_apdu( + &Tx_Buf[0], + src, + &my_address, + false, // true for confirmed messages + MESSAGE_PRIORITY_NORMAL); + // bad decoding - send an abort + if (len == -1) + { + pdu_len += abort_encode_apdu( + &Tx_Buf[pdu_len], + service_data->invoke_id, + ABORT_REASON_OTHER); + fprintf(stderr,"Sent Abort!\n"); + send = true; + } + else if (service_data->segmented_message) + { + pdu_len += abort_encode_apdu( + &Tx_Buf[pdu_len], + service_data->invoke_id, + ABORT_REASON_SEGMENTATION_NOT_SUPPORTED); + fprintf(stderr,"Sent Abort!\n"); + send = true; + } + else + { + switch (wp_data.object_type) + { + case OBJECT_DEVICE: + if (Device_Write_Property(&wp_data,&error_class,&error_code)) + { + pdu_len = encode_simple_ack( + &Tx_Buf[pdu_len], + service_data->invoke_id, + SERVICE_CONFIRMED_WRITE_PROPERTY); + fprintf(stderr,"Sent Write Property Simple Ack!\n"); + send = true; + } + else + { + pdu_len += bacerror_encode_apdu( + &Tx_Buf[pdu_len], + service_data->invoke_id, + SERVICE_CONFIRMED_WRITE_PROPERTY, + error_class, + error_code); + fprintf(stderr,"Sent Write Property Error!\n"); + send = true; + } + break; + case OBJECT_ANALOG_INPUT: + pdu_len += bacerror_encode_apdu( + &Tx_Buf[pdu_len], + service_data->invoke_id, + SERVICE_CONFIRMED_WRITE_PROPERTY, + ERROR_CLASS_PROPERTY, + ERROR_CODE_WRITE_ACCESS_DENIED); + fprintf(stderr,"Sent Write Access Error!\n"); + send = true; + break; + default: + pdu_len += bacerror_encode_apdu( + &Tx_Buf[pdu_len], + service_data->invoke_id, + SERVICE_CONFIRMED_WRITE_PROPERTY, + ERROR_CLASS_OBJECT, + ERROR_CODE_UNKNOWN_OBJECT); + fprintf(stderr,"Sent Unknown Object Error!\n"); + send = true; + break; + } + } + if (send) + { + (void)ethernet_send_pdu( + src, // destination address + &Tx_Buf[0], + pdu_len); // number of bytes of data + } return; } @@ -266,7 +474,7 @@ void ReadPropertyHandler( static void Init_Device_Parameters(void) { // configure my initial values - Device_Set_Object_Instance_Number(111); + Device_Set_Object_Instance_Number(112); Device_Set_Vendor_Name("Lithonia Lighting"); Device_Set_Vendor_Identifier(42); Device_Set_Model_Name("Simple BACnet Server"); @@ -283,6 +491,9 @@ static void Init_Service_Handlers(void) apdu_set_unconfirmed_handler( SERVICE_UNCONFIRMED_WHO_IS, WhoIsHandler); + apdu_set_unconfirmed_handler( + SERVICE_UNCONFIRMED_I_AM, + IAmHandler); // set the handler for all the services we don't implement // It is required to send the proper reject message... diff --git a/bacnet-stack/ports/rtos32/makefile.mak b/bacnet-stack/ports/rtos32/makefile.mak index 58454571..a2709db8 100644 --- a/bacnet-stack/ports/rtos32/makefile.mak +++ b/bacnet-stack/ports/rtos32/makefile.mak @@ -27,7 +27,9 @@ SRCS = init.c main.c ethernet.c \ ..\..\whois.c \ ..\..\iam.c \ ..\..\rp.c \ + ..\..\wp.c \ ..\..\device.c \ + ..\..\ai.c \ ..\..\abort.c \ ..\..\reject.c \ ..\..\bacerror.c \ @@ -48,7 +50,7 @@ LOCATE = $(RTOS32_DIR)\bin\rtloc # CC_DIR = $(BORLAND_DIR)\BIN CC_INCLDIR = $(BORLAND_DIR)\include -INCL_DIRS = -I$(BORLAND_DIR)\include;$(RTOS32_DIR)\include +INCL_DIRS = -I$(BORLAND_DIR)\include;$(RTOS32_DIR)\include;../../; DEFINES = -DDOC CFLAGS = $(INCL_DIRS) $(CS_FLAGS) $(DEFINES) diff --git a/bacnet-stack/ports/rtos32/setvars.bat b/bacnet-stack/ports/rtos32/setvars.bat new file mode 100644 index 00000000..ad9e8cf1 --- /dev/null +++ b/bacnet-stack/ports/rtos32/setvars.bat @@ -0,0 +1,3 @@ +set BORLAND_DIR=\bc5 +set RTOS32_DIR=\rtos32 + diff --git a/bacnet-stack/ports/rtos32/software.cfg b/bacnet-stack/ports/rtos32/software.cfg index 3adec06b..3f36029e 100644 --- a/bacnet-stack/ports/rtos32/software.cfg +++ b/bacnet-stack/ports/rtos32/software.cfg @@ -14,29 +14,16 @@ @HARDWARE.CFG -#ifdef DEBUGDOS - #define BOOT_DOS -#endif - -#ifdef TSYS_DOS - #define BOOT_DOS -#endif - -#ifsection .text // redefine some section names for BCB - #define CODE .text - #define DATA .data -#endif - // Either use the monitor, or create bootable code. #ifdef MONITOR - Reserve Monitor // leave room for Debug Monitor -#elifdef BOOT_DOS - Locate BootCode BIOSBOOT.EXE LowMem // boot from disk, bios, or DOS + Reserve Monitor // leave room for Debug Monitor +#elifdef DEBUGDOS + Locate BootCode BIOSBOOT.EXE LowMem // boot from disk Locate BootData BootData LowMem // must be in conventional mem Locate DiskBuffer DiskIO LowMem 16k 16k // needed by disk boot code NoFPU=0 // Check FPU CPL = 3 // normal priveleges - VideoRAM ColorText // program output sent to Graphics Card + VideoRAM ColorText // program output sent to Graphic Card #else Locate BootCode BIOSBOOT.EXE LowMem // boot from disk Locate BootData BootData LowMem 0 16 // must be in conventional mem @@ -46,34 +33,27 @@ VideoRAM ColorText // program output sent to Graphic Card #endif -FillRAM HeapMem // remap unused RAM +FillRAM HeapMem // remap unused RAM -Locate Header Application LowMem // application header +Locate Header Header LowMem // application header Locate PageTable Paging LowMem 20k // paging to use this - Locate NTSection CODE ProgMem->HighMem // code section Locate NTSection DATA ProgMem->HighMem // data section - -#ifsection .tls // the following sections are not generated by all linker versions - Locate NTSection .tls ProgMem->HighMem // TLS data section - Locate NTSection .rdata ProgMem->HighMem // TLS directory -#endif - -Locate Stack Stack StackMem->LowMem 16k // stack space for main() -Locate Heap Heap HeapMem // and the rest for the heap +Locate NTSection .tls ProgMem->HighMem // TLS data section +Locate NTSection .rdata ProgMem->HighMem // TLS directory +Locate Stack Stack StackMem->LowMem 6k // stack space for main() +Locate Heap Heap HeapMem // and the rest for the heap // Compression needed if we are short on disk space - but shortens download // Note that this is discardable, unless we use -d- option of RTLoc -Locate DecompCode Expand LowMem // include decompression stuff -Locate DecompData ExBuffer LowMem +Locate DecompCode Expand LowMem // include decompression stuff +Locate DecompData ExBuffer LowMem -Locate Copy Paging LowMem // compress Paging -Locate Copy CODE HighMem // compress CODE -Locate Copy DATA HighMem // compress DATA +Locate Copy Paging LowMem // compress Paging +Locate Copy CODE HighMem // compress CODE +Locate Copy DATA HighMem // compress DATA -#ifndef TSYS_CARD -Locate Nothing FloppyDMA MoreLowMem 18k 64k ReadWrite // floppy driver -#endif +Locate Nothing FloppyDMA MoreLowMem 18k 64k ReadWrite // floppy driver Init _Init // do some standard initializations (see init.c)