diff --git a/bacnet-stack/demo/handler/h_cov.c b/bacnet-stack/demo/handler/h_cov.c new file mode 100644 index 00000000..5f17b40b --- /dev/null +++ b/bacnet-stack/demo/handler/h_cov.c @@ -0,0 +1,185 @@ +/************************************************************************** +* +* Copyright (C) 2007 Steve Karg +* +* Permission is hereby granted, free of charge, to any person obtaining +* a copy of this software and associated documentation files (the +* "Software"), to deal in the Software without restriction, including +* without limitation the rights to use, copy, modify, merge, publish, +* distribute, sublicense, and/or sell copies of the Software, and to +* permit persons to whom the Software is furnished to do so, subject to +* the following conditions: +* +* The above copyright notice and this permission notice shall be included +* in all copies or substantial portions of the Software. +* +* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. +* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, +* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE +* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +* +*********************************************************************/ +#include +#include +#include +#include +#include +#include "config.h" +#include "txbuf.h" +#include "bacdef.h" +#include "bacdcode.h" +#include "apdu.h" +#include "npdu.h" +#include "abort.h" +#include "cov.h" +/* demo objects */ +#include "device.h" +#include "ai.h" +#include "ao.h" +#include "av.h" +#include "bi.h" +#include "bo.h" +#include "bv.h" +#include "lc.h" +#include "lsp.h" +#include "mso.h" +#if defined(BACFILE) + #include "bacfile.h" +#endif + +typedef struct BACnet_COV_Subscription { + bool valid; + uint32_t subscriberProcessIdentifier; + BACNET_OBJECT_ID monitoredObjectIdentifier; + bool issueConfirmedNotifications; /* optional */ + unsigned lifetime; /* optional */ + BACNET_PROPERTY_REFERENCE monitoredProperty; + bool covIncrementPresent; /* true if present */ + float covIncrement; /* optional */ +} BACNET_COV_SUBSCRIPTION; + +#define MAX_COV_SUBCRIPTIONS 32 +static BACNET_COV_SUBSCRIPTION COV_Subscriptions[MAX_COV_SUBCRIPTIONS]; + +void handler_cov_init(void) +{ + unsigned index = 0; + + for (index = 0; index < MAX_COV_SUBCRIPTIONS; index++) { + COV_Subscriptions[index].valid = false; + COV_Subscriptions[index].subscriberProcessIdentifier = 0; + COV_Subscriptions[index].monitoredObjectIdentifier.type = OBJECT_ANALOG_INPUT; + COV_Subscriptions[index].monitoredObjectIdentifier.instance = 0; + COV_Subscriptions[index].issueConfirmedNotifications = false; + COV_Subscriptions[index].lifetime = 0; + COV_Subscriptions[index].monitoredProperty.propertyIdentifier = PROP_ALL; + COV_Subscriptions[index].monitoredProperty.propertyArrayIndex = -1; + COV_Subscriptions[index].covIncrementPresent = false; + COV_Subscriptions[index].covIncrement = 0; + } +} + +void handler_cov_task(void) +{ + /* handle timeouts */ + /* handle COV notifications */ +} + +static bool cov_subscribe( + BACNET_SUBSCRIBE_COV_DATA *cov_data, + BACNET_ERROR_CLASS * error_class, + BACNET_ERROR_CODE * error_code) +{ + bool status = false; /* return value */ + + switch (cov_data->monitoredObjectIdentifier.type) { + case OBJECT_BINARY_INPUT: + status = true; + break; + default: + break; + } + + return status; +} + +void handler_cov_subscribe(uint8_t * service_request, + uint16_t service_len, + BACNET_ADDRESS * src, BACNET_CONFIRMED_SERVICE_DATA * service_data) +{ + BACNET_SUBSCRIBE_COV_DATA cov_data; + int len = 0; + int pdu_len = 0; + BACNET_NPDU_DATA npdu_data; + bool success = false; + int bytes_sent = 0; + BACNET_ERROR_CLASS error_class = ERROR_CLASS_OBJECT; + BACNET_ERROR_CODE error_code = ERROR_CODE_UNKNOWN_OBJECT; + BACNET_ADDRESS my_address; + + /* encode the NPDU portion of the packet */ + datalink_get_my_address(&my_address); + npdu_encode_npdu_data(&npdu_data, false, MESSAGE_PRIORITY_NORMAL); + pdu_len = npdu_encode_pdu(&Handler_Transmit_Buffer[0], src, + &my_address, &npdu_data); + if (service_data->segmented_message) { + /* we don't support segmentation - send an abort */ + len = abort_encode_apdu(&Handler_Transmit_Buffer[pdu_len], + service_data->invoke_id, + ABORT_REASON_SEGMENTATION_NOT_SUPPORTED, true); +#if PRINT_ENABLED + fprintf(stderr, "SubscribeCOV: Segmented message. Sending Abort!\n"); +#endif + goto COV_ABORT; + } + len = cov_subscribe_decode_service_request(service_request, + service_len, &cov_data); +#if PRINT_ENABLED + if (len <= 0) + fprintf(stderr, "SubscribeCOV: Unable to decode Request!\n"); +#endif + if (len < 0) { + /* bad decoding - send an abort */ + len = abort_encode_apdu(&Handler_Transmit_Buffer[pdu_len], + service_data->invoke_id, ABORT_REASON_OTHER, true); +#if PRINT_ENABLED + fprintf(stderr, "SubscribeCOV: Bad decoding. Sending Abort!\n"); +#endif + goto COV_ABORT; + } + success = cov_subscribe(&cov_data, &error_class, &error_code); + if (success) { + len = encode_simple_ack( + &Handler_Transmit_Buffer[pdu_len], + service_data->invoke_id, + SERVICE_CONFIRMED_SUBSCRIBE_COV); +#if PRINT_ENABLED + fprintf(stderr, + "SubscribeCOV: Sending Simple Ack!\n"); +#endif + } else { + len = bacerror_encode_apdu( + &Handler_Transmit_Buffer[pdu_len], + service_data->invoke_id, + SERVICE_CONFIRMED_SUBSCRIBE_COV, + error_class, + error_code); +#if PRINT_ENABLED + fprintf(stderr, + "SubscribeCOV: Sending Error!\n"); +#endif + } +COV_ABORT: + pdu_len += len; + bytes_sent = datalink_send_pdu(src, &npdu_data, + &Handler_Transmit_Buffer[0], pdu_len); +#if PRINT_ENABLED + if (bytes_sent <= 0) + fprintf(stderr, "SubscribeCOV: Failed to send PDU (%s)!\n", strerror(errno)); +#endif + + return; +} diff --git a/bacnet-stack/demo/handler/h_wp.c b/bacnet-stack/demo/handler/h_wp.c index b673b935..02552a61 100644 --- a/bacnet-stack/demo/handler/h_wp.c +++ b/bacnet-stack/demo/handler/h_wp.c @@ -122,8 +122,26 @@ void handler_write_property( #endif } break; - case OBJECT_ANALOG_INPUT: 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; len = diff --git a/bacnet-stack/demo/object/bi.c b/bacnet-stack/demo/object/bi.c index 8cf123ea..007c9aff 100644 --- a/bacnet-stack/demo/object/bi.c +++ b/bacnet-stack/demo/object/bi.c @@ -31,11 +31,15 @@ #include "bacdef.h" #include "bacdcode.h" #include "bacenum.h" +#include "wp.h" #include "config.h" /* the custom stuff */ #define MAX_BINARY_INPUTS 5 +/* stores the current value */ static BACNET_BINARY_PV Present_Value[MAX_BINARY_INPUTS]; +/* out of service decouples physical input from Present_Value */ +static bool Out_Of_Service[MAX_BINARY_INPUTS]; /* These three arrays are used by the ReadPropertyMultiple handler */ static const int Binary_Input_Properties_Required[] = { @@ -150,6 +154,46 @@ static BACNET_BINARY_PV Binary_Input_Present_Value( return value; } +static bool Binary_Input_Out_Of_Service( + uint32_t object_instance) +{ + bool value = false; + unsigned index = 0; + + Binary_Input_Init(); + index = Binary_Input_Instance_To_Index(object_instance); + if (index < MAX_BINARY_INPUTS) + value = Out_Of_Service[index]; + + return value; +} + +static void Binary_Input_Present_Value_Set( + uint32_t object_instance, BACNET_BINARY_PV value) +{ + unsigned index = 0; + + Binary_Input_Init(); + index = Binary_Input_Instance_To_Index(object_instance); + if (index < MAX_BINARY_INPUTS) + Present_Value[index] = value; + + return; +} + +static void Binary_Input_Out_Of_Service_Set( + uint32_t object_instance, bool value) +{ + unsigned index = 0; + + Binary_Input_Init(); + index = Binary_Input_Instance_To_Index(object_instance); + if (index < MAX_BINARY_INPUTS) + Out_Of_Service[index] = value; + + return; +} + char *Binary_Input_Name( uint32_t object_instance) { @@ -179,7 +223,6 @@ int Binary_Input_Encode_Property_APDU( BACNET_CHARACTER_STRING char_string; BACNET_POLARITY polarity = POLARITY_NORMAL; - (void) array_index; Binary_Input_Init(); switch (property) { @@ -221,7 +264,8 @@ int Binary_Input_Encode_Property_APDU( encode_application_enumerated(&apdu[0], EVENT_STATE_NORMAL); break; case PROP_OUT_OF_SERVICE: - apdu_len = encode_application_boolean(&apdu[0], false); + apdu_len = encode_application_boolean(&apdu[0], + Binary_Input_Out_Of_Service(object_instance)); break; case PROP_POLARITY: apdu_len = encode_application_enumerated(&apdu[0], polarity); @@ -236,6 +280,66 @@ int Binary_Input_Encode_Property_APDU( return apdu_len; } +/* returns true if successful */ +bool Binary_Input_Write_Property( + BACNET_WRITE_PROPERTY_DATA * wp_data, + BACNET_ERROR_CLASS * error_class, + BACNET_ERROR_CODE * error_code) +{ + bool status = false; /* return value */ + int len = 0; + BACNET_APPLICATION_DATA_VALUE value; + + Binary_Input_Init(); + if (!Binary_Input_Valid_Instance(wp_data->object_instance)) { + *error_class = ERROR_CLASS_OBJECT; + *error_code = ERROR_CODE_UNKNOWN_OBJECT; + return false; + } + /* decode the some of the request */ + len = + bacapp_decode_application_data(wp_data->application_data, + wp_data->application_data_len, &value); + /* FIXME: len < application_data_len: more data? */ + /* FIXME: len == 0: unable to decode? */ + switch (wp_data->object_property) { + case PROP_PRESENT_VALUE: + if (value.tag == BACNET_APPLICATION_TAG_ENUMERATED) { + if ((value.type.Enumerated >= MIN_BINARY_PV) && + (value.type.Enumerated <= MAX_BINARY_PV)) { + Binary_Input_Present_Value_Set( + wp_data->object_instance, + (BACNET_BINARY_PV) value.type.Enumerated); + status = true; + } else { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; + } + } else { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_INVALID_DATA_TYPE; + } + break; + case PROP_OUT_OF_SERVICE: + if (value.tag == BACNET_APPLICATION_TAG_BOOLEAN) { + Binary_Input_Out_Of_Service_Set( + wp_data->object_instance, + value.type.Boolean); + status = true; + } else { + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_INVALID_DATA_TYPE; + } + break; + default: + *error_class = ERROR_CLASS_PROPERTY; + *error_code = ERROR_CODE_WRITE_ACCESS_DENIED; + break; + } + + return status; +} + #ifdef TEST #include #include diff --git a/bacnet-stack/demo/object/device.c b/bacnet-stack/demo/object/device.c index 385f4ddd..2bf5e11b 100644 --- a/bacnet-stack/demo/object/device.c +++ b/bacnet-stack/demo/object/device.c @@ -874,6 +874,8 @@ int Device_Encode_Property_APDU( encode_application_unsigned(&apdu[0], dlmstp_max_master()); break; #endif + case PROP_ACTIVE_COV_SUBSCRIPTIONS: + break; default: *error_class = ERROR_CLASS_PROPERTY; *error_code = ERROR_CODE_UNKNOWN_PROPERTY; diff --git a/bacnet-stack/demo/server/bacserv.cbp b/bacnet-stack/demo/server/bacserv.cbp index 96864c63..acbf8024 100644 --- a/bacnet-stack/demo/server/bacserv.cbp +++ b/bacnet-stack/demo/server/bacserv.cbp @@ -44,234 +44,21 @@ - - - + - - - + + - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/bacnet-stack/include/bi.h b/bacnet-stack/include/bi.h index e29548d1..be36b36b 100644 --- a/bacnet-stack/include/bi.h +++ b/bacnet-stack/include/bi.h @@ -54,6 +54,12 @@ extern "C" { BACNET_ERROR_CLASS * error_class, BACNET_ERROR_CODE * error_code); + bool Binary_Input_Write_Property( + BACNET_WRITE_PROPERTY_DATA * wp_data, + BACNET_ERROR_CLASS * error_class, + BACNET_ERROR_CODE * error_code); + + #ifdef TEST #include "ctest.h" void testBinaryInput( diff --git a/bacnet-stack/lib/bacnet.cbp b/bacnet-stack/lib/bacnet.cbp index 88d780c4..611c424c 100644 --- a/bacnet-stack/lib/bacnet.cbp +++ b/bacnet-stack/lib/bacnet.cbp @@ -45,7 +45,6 @@ - @@ -91,7 +90,6 @@ - @@ -131,51 +129,44 @@ - - - - - - - - - - - + + + + @@ -183,32 +174,54 @@ + + + + + + + + + + + + + + + + + + + + + +