From 3dc3da57354150084a2bfc03e17a65e627bae95b Mon Sep 17 00:00:00 2001 From: skarg Date: Sat, 22 Aug 2009 19:27:18 +0000 Subject: [PATCH] Changing handlers to register objects so that handlers can remain unchanged. In Progress! --- bacnet-stack/demo/handler/h_rp.c | 14 ++ bacnet-stack/demo/handler/h_wp.c | 234 ++++---------------------- bacnet-stack/demo/object/device.c | 10 ++ bacnet-stack/include/handlers.h | 8 + bacnet-stack/include/rp.h | 7 + bacnet-stack/include/wp.h | 12 +- bacnet-stack/ports/at91sam7s/Makefile | 4 +- 7 files changed, 82 insertions(+), 207 deletions(-) diff --git a/bacnet-stack/demo/handler/h_rp.c b/bacnet-stack/demo/handler/h_rp.c index a2872603..9ceeb895 100644 --- a/bacnet-stack/demo/handler/h_rp.c +++ b/bacnet-stack/demo/handler/h_rp.c @@ -52,6 +52,20 @@ static uint8_t Temp_Buf[MAX_APDU] = { 0 }; + +static read_property_function + Read_Property[MAX_BACNET_OBJECT_TYPE]; + +void handler_read_property_object_set( + BACNET_OBJECT_TYPE object_type, + read_property_function pFunction) +{ + if (object_type < MAX_BACNET_OBJECT_TYPE) { + Read_Property[object_type] = pFunction; + } +} + + /* Encodes the property APDU and returns the length, or sets the error, and returns -1 */ int Encode_Property_APDU( diff --git a/bacnet-stack/demo/handler/h_wp.c b/bacnet-stack/demo/handler/h_wp.c index 02552a61..7386d2a2 100644 --- a/bacnet-stack/demo/handler/h_wp.c +++ b/bacnet-stack/demo/handler/h_wp.c @@ -50,6 +50,18 @@ #include "bacfile.h" #endif +static write_property_function + Write_Property[MAX_BACNET_OBJECT_TYPE]; + +void handler_write_property_object_set( + BACNET_OBJECT_TYPE object_type, + write_property_function pFunction) +{ + if (object_type < MAX_BACNET_OBJECT_TYPE) { + Write_Property[object_type] = pFunction; + } +} + void handler_write_property( uint8_t * service_request, uint16_t service_len, @@ -64,6 +76,7 @@ void handler_write_property( BACNET_ERROR_CODE error_code = ERROR_CODE_UNKNOWN_OBJECT; int bytes_sent = 0; BACNET_ADDRESS my_address; + write_property_function wp_function = NULL; /* encode the NPDU portion of the packet */ datalink_get_my_address(&my_address); @@ -103,219 +116,34 @@ void handler_write_property( #endif goto WP_ABORT; } - switch (wp_data.object_type) { - case OBJECT_DEVICE: - if (Device_Write_Property(&wp_data, &error_class, &error_code)) { - len = - encode_simple_ack(&Handler_Transmit_Buffer[pdu_len], - service_data->invoke_id, SERVICE_CONFIRMED_WRITE_PROPERTY); + if (wp_data.object_type < MAX_BACNET_OBJECT_TYPE) { + wp_function = Write_Property[wp_data.object_type]; + } + if (wp_function) { + if (wp_function(&wp_data, &error_class, &error_code)) { + len = + encode_simple_ack(&Handler_Transmit_Buffer[pdu_len], + service_data->invoke_id, SERVICE_CONFIRMED_WRITE_PROPERTY); #if PRINT_ENABLED - fprintf(stderr, "WP: Sending Simple Ack for Device!\n"); + fprintf(stderr, "WP: Sending Simple Ack!\n"); #endif - } else { - len = - bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len], - service_data->invoke_id, SERVICE_CONFIRMED_WRITE_PROPERTY, - error_class, error_code); -#if PRINT_ENABLED - fprintf(stderr, "WP: Sending Error for Device!\n"); -#endif - } - break; - case OBJECT_BINARY_INPUT: - if (Binary_Input_Write_Property(&wp_data, &error_class, - &error_code)) { - len = - encode_simple_ack(&Handler_Transmit_Buffer[pdu_len], - service_data->invoke_id, SERVICE_CONFIRMED_WRITE_PROPERTY); -#if PRINT_ENABLED - fprintf(stderr, "WP: Sending Simple Ack for BI!\n"); -#endif - } else { - len = - bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len], - service_data->invoke_id, SERVICE_CONFIRMED_WRITE_PROPERTY, - error_class, error_code); -#if PRINT_ENABLED - fprintf(stderr, "WP: Sending Write Access Error for BI!\n"); -#endif - } - break; - case OBJECT_ANALOG_INPUT: - error_class = ERROR_CLASS_PROPERTY; - error_code = ERROR_CODE_WRITE_ACCESS_DENIED; + } else { len = bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len], service_data->invoke_id, SERVICE_CONFIRMED_WRITE_PROPERTY, error_class, error_code); #if PRINT_ENABLED - fprintf(stderr, "WP: Sending Write Access Error!\n"); + fprintf(stderr, "WP: Sending Error!\n"); #endif - break; - case OBJECT_BINARY_OUTPUT: - if (Binary_Output_Write_Property(&wp_data, &error_class, - &error_code)) { - len = - encode_simple_ack(&Handler_Transmit_Buffer[pdu_len], - service_data->invoke_id, SERVICE_CONFIRMED_WRITE_PROPERTY); + } + } else { + len = + bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len], + service_data->invoke_id, SERVICE_CONFIRMED_WRITE_PROPERTY, + error_class, error_code); #if PRINT_ENABLED - fprintf(stderr, "WP: Sending Simple Ack for BO!\n"); + fprintf(stderr, "WP: Sending Unknown Object Error!\n"); #endif - } else { - len = - bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len], - service_data->invoke_id, SERVICE_CONFIRMED_WRITE_PROPERTY, - error_class, error_code); -#if PRINT_ENABLED - fprintf(stderr, "WP: Sending Write Access Error for BO!\n"); -#endif - } - break; - case OBJECT_BINARY_VALUE: - if (Binary_Value_Write_Property(&wp_data, &error_class, - &error_code)) { - len = - encode_simple_ack(&Handler_Transmit_Buffer[pdu_len], - service_data->invoke_id, SERVICE_CONFIRMED_WRITE_PROPERTY); -#if PRINT_ENABLED - fprintf(stderr, "WP: Sending Simple Ack for BV!\n"); -#endif - } else { - len = - bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len], - service_data->invoke_id, SERVICE_CONFIRMED_WRITE_PROPERTY, - error_class, error_code); -#if PRINT_ENABLED - fprintf(stderr, "WP: Sending Write Access Error for BV!\n"); -#endif - } - break; - case OBJECT_ANALOG_OUTPUT: - if (Analog_Output_Write_Property(&wp_data, &error_class, - &error_code)) { - len = - encode_simple_ack(&Handler_Transmit_Buffer[pdu_len], - service_data->invoke_id, SERVICE_CONFIRMED_WRITE_PROPERTY); -#if PRINT_ENABLED - fprintf(stderr, "WP: Sending Simple Ack for AO!\n"); -#endif - } else { - len = - bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len], - service_data->invoke_id, SERVICE_CONFIRMED_WRITE_PROPERTY, - error_class, error_code); -#if PRINT_ENABLED - fprintf(stderr, "WP: Sending Write Access Error for AO!\n"); -#endif - } - break; - case OBJECT_ANALOG_VALUE: - if (Analog_Value_Write_Property(&wp_data, &error_class, - &error_code)) { - len = - encode_simple_ack(&Handler_Transmit_Buffer[pdu_len], - service_data->invoke_id, SERVICE_CONFIRMED_WRITE_PROPERTY); -#if PRINT_ENABLED - fprintf(stderr, "WP: Sending Simple Ack for AV!\n"); -#endif - } else { - len = - bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len], - service_data->invoke_id, SERVICE_CONFIRMED_WRITE_PROPERTY, - error_class, error_code); -#if PRINT_ENABLED - fprintf(stderr, "WP: Sending Write Access Error for AV!\n"); -#endif - } - break; - case OBJECT_LIFE_SAFETY_POINT: - if (Life_Safety_Point_Write_Property(&wp_data, &error_class, - &error_code)) { - len = - encode_simple_ack(&Handler_Transmit_Buffer[pdu_len], - service_data->invoke_id, SERVICE_CONFIRMED_WRITE_PROPERTY); -#if PRINT_ENABLED - fprintf(stderr, "WP: Sending Simple Ack for LSP!\n"); -#endif - } else { - len = - bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len], - service_data->invoke_id, SERVICE_CONFIRMED_WRITE_PROPERTY, - error_class, error_code); -#if PRINT_ENABLED - fprintf(stderr, "WP: Sending Write Access Error for LSP!\n"); -#endif - } - break; - case OBJECT_LOAD_CONTROL: - if (Load_Control_Write_Property(&wp_data, &error_class, - &error_code)) { - len = - encode_simple_ack(&Handler_Transmit_Buffer[pdu_len], - service_data->invoke_id, SERVICE_CONFIRMED_WRITE_PROPERTY); -#if PRINT_ENABLED - fprintf(stderr, "WP: Sending Simple Ack for Load Control!\n"); -#endif - } else { - len = - bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len], - service_data->invoke_id, SERVICE_CONFIRMED_WRITE_PROPERTY, - error_class, error_code); -#if PRINT_ENABLED - fprintf(stderr, - "WP: Sending Write Access Error for Load Control!\n"); -#endif - } - break; - case OBJECT_MULTI_STATE_OUTPUT: - if (Multistate_Output_Write_Property(&wp_data, &error_class, - &error_code)) { - len = - encode_simple_ack(&Handler_Transmit_Buffer[pdu_len], - service_data->invoke_id, SERVICE_CONFIRMED_WRITE_PROPERTY); -#if PRINT_ENABLED - fprintf(stderr, - "WP: Sending Write Property Simple Ack for MSO!\n"); -#endif - } else { - len = - bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len], - service_data->invoke_id, SERVICE_CONFIRMED_WRITE_PROPERTY, - error_class, error_code); -#if PRINT_ENABLED - fprintf(stderr, "WP: Sending Write Access Error for MSO!\n"); -#endif - } - break; -#if defined(BACFILE) - case OBJECT_FILE: - if (bacfile_write_property(&wp_data, &error_class, &error_code)) { - len = - encode_simple_ack(&Handler_Transmit_Buffer[pdu_len], - service_data->invoke_id, SERVICE_CONFIRMED_WRITE_PROPERTY); -#if PRINT_ENABLED - fprintf(stderr, "WP: Sending Simple Ack for File!\n"); -#endif - } else { - len = - bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len], - service_data->invoke_id, SERVICE_CONFIRMED_WRITE_PROPERTY, - error_class, error_code); -#if PRINT_ENABLED - fprintf(stderr, "WP: Sending Write Access Error for File!\n"); -#endif - } - break; -#endif /* BACFILE */ - default: - len = - bacerror_encode_apdu(&Handler_Transmit_Buffer[pdu_len], - service_data->invoke_id, SERVICE_CONFIRMED_WRITE_PROPERTY, - error_class, error_code); -#if PRINT_ENABLED - fprintf(stderr, "WP: Sending Unknown Object Error!\n"); -#endif - break; } WP_ABORT: pdu_len += len; diff --git a/bacnet-stack/demo/object/device.c b/bacnet-stack/demo/object/device.c index 1f26936b..3b26d9a5 100644 --- a/bacnet-stack/demo/object/device.c +++ b/bacnet-stack/demo/object/device.c @@ -1002,6 +1002,16 @@ bool Device_Write_Property( return status; } +void Device_Init(void) +{ + handler_write_property_object_set( + OBJECT_DEVICE, + Device_Write_Property); + handler_read_property_object_set( + OBJECT_DEVICE, + Device_Encode_Property_APDU); +} + #ifdef TEST #include #include diff --git a/bacnet-stack/include/handlers.h b/bacnet-stack/include/handlers.h index e37bbcf7..600745ae 100644 --- a/bacnet-stack/include/handlers.h +++ b/bacnet-stack/include/handlers.h @@ -73,6 +73,10 @@ extern "C" { BACNET_ADDRESS * src, BACNET_CONFIRMED_SERVICE_DATA * service_data); + void handler_read_property_object_set( + BACNET_OBJECT_TYPE object_type, + read_property_function pFunction); + void handler_read_property_ack( uint8_t * service_request, uint16_t service_len, @@ -84,6 +88,10 @@ extern "C" { uint16_t service_len, BACNET_ADDRESS * src, BACNET_CONFIRMED_SERVICE_DATA * service_data); + + void handler_write_property_object_set( + BACNET_OBJECT_TYPE object_type, + write_property_function pFunction); void handler_atomic_read_file( uint8_t * service_request, diff --git a/bacnet-stack/include/rp.h b/bacnet-stack/include/rp.h index 687c0ea5..c5eebc02 100644 --- a/bacnet-stack/include/rp.h +++ b/bacnet-stack/include/rp.h @@ -46,6 +46,13 @@ typedef struct BACnet_Read_Property_Data { int application_data_len; } BACNET_READ_PROPERTY_DATA; +typedef int (*read_property_function) ( + uint8_t * apdu, + BACNET_PROPERTY_ID property, + int32_t array_index, + BACNET_ERROR_CLASS * error_class, + BACNET_ERROR_CODE * error_code); + #ifdef __cplusplus extern "C" { #endif /* __cplusplus */ diff --git a/bacnet-stack/include/wp.h b/bacnet-stack/include/wp.h index 3d19523c..77176c1c 100644 --- a/bacnet-stack/include/wp.h +++ b/bacnet-stack/include/wp.h @@ -53,17 +53,23 @@ typedef struct BACnet_Write_Property_Data { uint8_t priority; /* use BACNET_NO_PRIORITY if no priority */ } BACNET_WRITE_PROPERTY_DATA; +typedef bool (*write_property_function) ( + BACNET_WRITE_PROPERTY_DATA * wp_data, + uint8_t * service_request, + BACNET_ERROR_CLASS * error_class, + BACNET_ERROR_CODE * error_code); + #ifdef __cplusplus extern "C" { #endif /* __cplusplus */ - -/* encode service */ + + /* encode service */ int wp_encode_apdu( uint8_t * apdu, uint8_t invoke_id, BACNET_WRITE_PROPERTY_DATA * wp_data); -/* decode the service request only */ + /* decode the service request only */ int wp_decode_service_request( uint8_t * apdu, unsigned apdu_len, diff --git a/bacnet-stack/ports/at91sam7s/Makefile b/bacnet-stack/ports/at91sam7s/Makefile index e45711b7..d7fefb3f 100644 --- a/bacnet-stack/ports/at91sam7s/Makefile +++ b/bacnet-stack/ports/at91sam7s/Makefile @@ -27,6 +27,7 @@ INCLUDES += -I$(BACNET_INCLUDE) #OPTIMIZATION = -O0 OPTIMIZATION = -Os CFLAGS = -fno-common $(INCLUDES) $(BACNET_FLAGS) -Wall -g +CFLAGS += -mno-thumb-interwork # dead code removal CFLAGS += -fdata-sections -ffunction-sections LIBRARY = lib$(TARGET).a @@ -34,7 +35,8 @@ LIBRARY = lib$(TARGET).a AFLAGS = -Wa,-ahls,-mapcs-32,-adhlns=$(<:.s=.lst) # -Wl, Pass comma-separated on to the linker LIBRARIES=-lc,-lgcc,-lm,-L=.,-l$(TARGET) -LDFLAGS = -nostartfiles -Wl,-nostdlib,-Map=$(TARGET).map,$(LIBRARIES),-T$(LDSCRIPT) +LDFLAGS = -nostartfiles +LDFLAGS += -Wl,-nostdlib,-Map=$(TARGET).map,$(LIBRARIES),-T$(LDSCRIPT) # dead code removal LDFLAGS += -Wl,--gc-sections,-static CPFLAGS = --output-target=binary