Edouard@2020: /**************************************************************************
Edouard@2020: *
Edouard@2020: * Copyright (C) 2006 Steve Karg <skarg@users.sourceforge.net>
Edouard@2020: * Copyright (C) 2017 Mario de Sousa <msousa@fe.up.pt>
Edouard@2020: *
Edouard@2020: * Permission is hereby granted, free of charge, to any person obtaining
Edouard@2020: * a copy of this software and associated documentation files (the
Edouard@2020: * "Software"), to deal in the Software without restriction, including
Edouard@2020: * without limitation the rights to use, copy, modify, merge, publish,
Edouard@2020: * distribute, sublicense, and/or sell copies of the Software, and to
Edouard@2020: * permit persons to whom the Software is furnished to do so, subject to
Edouard@2020: * the following conditions:
Edouard@2020: *
Edouard@2020: * The above copyright notice and this permission notice shall be included
Edouard@2020: * in all copies or substantial portions of the Software.
Edouard@2020: *
Edouard@2020: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
Edouard@2020: * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
Edouard@2020: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
Edouard@2020: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
Edouard@2020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
Edouard@2020: * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
Edouard@2020: * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Edouard@2020: *
Edouard@2020: *********************************************************************/
Edouard@2020: 
Edouard@2020: /* Binary Value Objects - customize for your use */
Edouard@2020: 
Edouard@2020: #include <stdbool.h>
Edouard@2020: #include <stdint.h>
Edouard@2020: #include <stdio.h>
Edouard@2020: 
Edouard@2020: #include "config_bacnet_for_beremiz_%(locstr)s.h"     /* the custom configuration for beremiz plugin */
Edouard@2020: #include "bacdef.h"
Edouard@2020: #include "bacdcode.h"
Edouard@2020: #include "bacenum.h"
Edouard@2020: #include "bacapp.h"
Edouard@2020: #include "wp.h"
Edouard@2020: #include "rp.h"
Edouard@2020: #include "bv_%(locstr)s.h"
Edouard@2020: #include "handlers.h"
Edouard@2020: 
Edouard@2020: /* we choose to have a NULL level in our system represented by */
Edouard@2020: /* a particular value.  When the priorities are not in use, they */
Edouard@2020: /* will be relinquished (i.e. set to the NULL level). */
Edouard@2020: // BINARY_NULL
Edouard@2020: /* test whether value is NULL */
Edouard@2020: #define BINARY_VALUE_IS_NULL(x) ((x) == BINARY_NULL)
Edouard@2020: 
Edouard@2020: /* When all the priorities are level null, the present value returns
Edouard@2020:  * the Relinquish Default value 
Edouard@2020:  */
Edouard@2020: #define BV_VALUE_RELINQUISH_DEFAULT BINARY_INACTIVE
Edouard@2020: 
Edouard@2020: /* The IEC 61131-3 located variables mapped onto the Binary Value objects of BACnet protocol */
Edouard@2020: %(BV_lvars)s
Edouard@2020: 
Edouard@2020: 
Edouard@2020: /* The array where we keep all the state related to the Binary Value Objects */
Edouard@2020: #define MAX_BINARY_VALUES %(BV_count)s
Edouard@2020: static BINARY_VALUE_DESCR BV_Descr[MAX_BINARY_VALUES] = {
Edouard@2020: %(BV_param)s
Edouard@2020: };
Edouard@2020: 
Edouard@2020: 
Edouard@2020: 
Edouard@2020: 
Edouard@2020: /* These three arrays are used by the ReadPropertyMultiple handler,
Edouard@2020:  * as well as to initialize the XXX_Property_List used by the 
Edouard@2020:  * Property List (PROP_PROPERTY_LIST) property.
Edouard@2020:  */
Edouard@2020: static const int Binary_Value_Properties_Required[] = {
Edouard@2020:  /* (1) Currently Supported                  */
Edouard@2020:  /* (2) Required by standard ASHRAE 135-2016 */
Edouard@2020:                               /*(1)(2)      */
Edouard@2020:     PROP_OBJECT_IDENTIFIER,   /* R  R ( 75) */
Edouard@2020:     PROP_OBJECT_NAME,         /* R  R ( 77) */
Edouard@2020:     PROP_OBJECT_TYPE,         /* R  R ( 79) */
Edouard@2020:     PROP_PRESENT_VALUE,       /* W  R ( 85) */
Edouard@2020:     PROP_STATUS_FLAGS,        /* R  R (111) */
Edouard@2020:     PROP_EVENT_STATE,         /* R  R ( 36) */
Edouard@2020:     PROP_OUT_OF_SERVICE,      /* W  R ( 81) */
Edouard@2020: //  PROP_PROPERTY_LIST,       /* R  R (371) */
Edouard@2020:     -1
Edouard@2020: };
Edouard@2020: 
Edouard@2020: static const int Binary_Value_Properties_Optional[] = {
Edouard@2020:  /* (1) Currently Supported                  */
Edouard@2020:  /* (2) Required by standard ASHRAE 135-2016 */
Edouard@2020:                                   /*(1)(2)      */
Edouard@2020:     PROP_DESCRIPTION,             /* R  O ( 28) */
Edouard@2020:     /* required if Present_Value is writable (which is true in our case!) */
Edouard@2020:     PROP_PRIORITY_ARRAY,          /* R  O ( 87) */
Edouard@2020:     PROP_RELINQUISH_DEFAULT,      /* R  O (104) */
Edouard@2020: //  PROP_CURRENT_COMMAND_PRIORITY,/* R  O (431) */   
Edouard@2020:     -1
Edouard@2020: };
Edouard@2020: 
Edouard@2020: static const int Binary_Value_Properties_Proprietary[] = {
Edouard@2020:     -1
Edouard@2020: };
Edouard@2020: 
Edouard@2020: 
Edouard@2020: /* This array stores the PROPERTY_LIST which may be read by clients.
Edouard@2020:  * End of list is marked by following the last element with the value '-1'
Edouard@2020:  * 
Edouard@2020:  * It is initialized by Binary_Value_Init() based off the values
Edouard@2020:  * stored in Binary_Value_Properties_Required 
Edouard@2020:  *           Binary_Value_Properties_Optional
Edouard@2020:  *           Binary_Value_Properties_Proprietary
Edouard@2020:  */
Edouard@2020: /* TODO: Allocate memory for this array with malloc() at startup */
Edouard@2020: static int Binary_Value_Properties_List[64];
Edouard@2020: 
Edouard@2020: 
Edouard@2020: 
Edouard@2020: 
Edouard@2020: 
Edouard@2020: /********************************************************/
Edouard@2020: /**                  Callback functions.               **/
Edouard@2020: /** Functions required by BACnet devie implementation. **/
Edouard@2020: /********************************************************/
Edouard@2020: 
Edouard@2020: 
Edouard@2020: /* This is a callback function. Callback set in My_Object_Table[] array declared in device.c,  */
Edouard@2020: void Binary_Value_Property_Lists(
Edouard@2020:     const int **pRequired,
Edouard@2020:     const int **pOptional,
Edouard@2020:     const int **pProprietary)
Edouard@2020: {
Edouard@2020:     if (pRequired)
Edouard@2020:         *pRequired = Binary_Value_Properties_Required;
Edouard@2020:     if (pOptional)
Edouard@2020:         *pOptional = Binary_Value_Properties_Optional;
Edouard@2020:     if (pProprietary)
Edouard@2020:         *pProprietary = Binary_Value_Properties_Proprietary;
Edouard@2020: 
Edouard@2020:     return;
Edouard@2020: }
Edouard@2020: 
Edouard@2020: 
Edouard@2020: /* This is a callback function. Callback set in My_Object_Table[] array declared in device.c,  */
Edouard@2020: void Binary_Value_Init(
Edouard@2020:     void)
Edouard@2020: {
Edouard@2020:     unsigned i, j;
Edouard@2020:     static bool initialized = false;
Edouard@2020: 
Edouard@2020:     // fprintf(stderr, "BACnet plugin: Binary_Value_Init() called!\n");
Edouard@2020:     
Edouard@2020:     if (!initialized) {
Edouard@2020:         initialized = true;
Edouard@2020: 
Edouard@2020:         /* initialize the Binary_Value_Properties_List array */
Edouard@2020:         int len = 0;
Edouard@2020:         len += BACnet_Init_Properties_List(Binary_Value_Properties_List + len,
Edouard@2020:                                            Binary_Value_Properties_Required);
Edouard@2020:         len += BACnet_Init_Properties_List(Binary_Value_Properties_List + len,
Edouard@2020:                                            Binary_Value_Properties_Optional);
Edouard@2020:         len += BACnet_Init_Properties_List(Binary_Value_Properties_List + len,
Edouard@2020:                                            Binary_Value_Properties_Proprietary);
Edouard@2020: 
Edouard@2020:         /* initialize all the binary values priority arrays to NULL */
Edouard@2020:         for (i = 0; i < MAX_BINARY_VALUES; i++) {
Edouard@2020:             for (j = 0; j < BACNET_MAX_PRIORITY; j++) {
Edouard@2020:                 BV_Descr[i].Present_Value[j] = BINARY_NULL;
Edouard@2020:             }
Edouard@2020:         }
Edouard@2020:     }
Edouard@2020: 
Edouard@2020:     return;
Edouard@2020: }
Edouard@2020: 
Edouard@2020: 
Edouard@2020: 
Edouard@2020: /* validate that the given instance (Object ID) exists */
Edouard@2020: /* This is a callback function. Callback set in My_Object_Table[] array declared in device.c,  */
Edouard@2020: bool Binary_Value_Valid_Instance(
Edouard@2020:     uint32_t object_instance)
Edouard@2020: {
Edouard@2020:     // fprintf(stderr, "BACnet plugin: Binary_Value_Valid_Instance(obj_ID=%%u) called!\n", object _instance);
Edouard@2020:     return (Binary_Value_Instance_To_Index(object_instance) < MAX_BINARY_VALUES);
Edouard@2020: }
Edouard@2020: 
Edouard@2020: 
Edouard@2020: /* the number of Binary Value Objects */
Edouard@2020: /* This is a callback function. Callback set in My_Object_Table[] array declared in device.c,  */
Edouard@2020: unsigned Binary_Value_Count(void)   {return MAX_BINARY_VALUES;}
Edouard@2020: 
Edouard@2020: 
Edouard@2020: /* returns the instance (i.e. Object Identifier) that correlates to the correct index */
Edouard@2020: /* This is a callback function. Callback set in My_Object_Table[] array declared in device.c,  */
Edouard@2020: uint32_t Binary_Value_Index_To_Instance(unsigned index) {return BV_Descr[index].Object_Identifier;}
Edouard@2020: 
Edouard@2020: 
Edouard@2020: /* returns the index that correlates to the correct instance number (Object Identifier) */
Edouard@2020: unsigned Binary_Value_Instance_To_Index(
Edouard@2020:     uint32_t object_instance)
Edouard@2020: {
Edouard@2020:     unsigned index = 0;
Edouard@2020:   
Edouard@2020:     for (index = 0; index < MAX_BINARY_VALUES; index++)
Edouard@2020:         if (object_instance == BV_Descr[index].Object_Identifier)
Edouard@2020:             return index;
Edouard@2020: 
Edouard@2020:     /* error, this object ID is not in our list! */
Edouard@2020:     return MAX_BINARY_VALUES;
Edouard@2020: }
Edouard@2020: 
Edouard@2020: 
Edouard@2020: 
Edouard@2020: BACNET_BINARY_PV Binary_Value_Present_Value(
Edouard@2020:     uint32_t object_instance)
Edouard@2020: {
Edouard@2020:     BACNET_BINARY_PV value = BV_VALUE_RELINQUISH_DEFAULT;
Edouard@2020:     unsigned index = 0;
Edouard@2020:     unsigned i = 0;
Edouard@2020: 
Edouard@2020:     // fprintf(stderr, "BACnet plugin: Binary_Value_Present_Value(obj_ID=%%u) called!\n", object_instance);
Edouard@2020: 
Edouard@2020:     index = Binary_Value_Instance_To_Index(object_instance);
Edouard@2020:     if (index < MAX_BINARY_VALUES) {
Edouard@2020:         for (i = 0; i < BACNET_MAX_PRIORITY; i++) {
Edouard@2020:             if (!BINARY_VALUE_IS_NULL(BV_Descr[index].Present_Value[i])) {
Edouard@2020:                 value = BV_Descr[index].Present_Value[i];
Edouard@2020:                 break;
Edouard@2020:             }
Edouard@2020:         }
Edouard@2020:     }
Edouard@2020: 
Edouard@2020:     return value;
Edouard@2020: }
Edouard@2020: 
Edouard@2020: 
Edouard@2020: 
Edouard@2020: /* returns command priority (1..16), or 0 if all priority values are at NULL */
Edouard@2020: int Binary_Value_Current_Command_Priority(
Edouard@2020:     uint32_t object_instance)
Edouard@2020: {
Edouard@2020:     unsigned index = 0;
Edouard@2020:     unsigned i = 0;
Edouard@2020: 
Edouard@2020:     index = Binary_Value_Instance_To_Index(object_instance);
Edouard@2020:     if (index < MAX_BINARY_VALUES) {
Edouard@2020:         for (i = 0; i < BACNET_MAX_PRIORITY; i++) {
Edouard@2020:             if (!BINARY_VALUE_IS_NULL(BV_Descr[index].Present_Value[i])) {
Edouard@2020:                 return i+1; // +1 since priority is 1..16, and not 0..15
Edouard@2020:             }
Edouard@2020:         }
Edouard@2020:     }
Edouard@2020:     // command values in all priorities are set to NULL
Edouard@2020:     return 0;
Edouard@2020: }
Edouard@2020: 
Edouard@2020: 
Edouard@2020: 
Edouard@2020: /* note: the object name must be unique within this device */
Edouard@2020: /* This is a callback function. Callback set in My_Object_Table[] array declared in device.c,  */
Edouard@2020: bool Binary_Value_Object_Name(
Edouard@2020:     uint32_t object_instance,
Edouard@2020:     BACNET_CHARACTER_STRING * object_name)
Edouard@2020: {
Edouard@2020:     bool    status = false;
Edouard@2020:     unsigned index = Binary_Value_Instance_To_Index(object_instance);
Edouard@2020: 
Edouard@2020:     if (index < MAX_BINARY_VALUES)
Edouard@2020:         status = characterstring_init_ansi(object_name, BV_Descr[index].Object_Name);
Edouard@2020:     
Edouard@2020:     return status;
Edouard@2020: }
Edouard@2020: 
Edouard@2020: 
Edouard@2020: 
Edouard@2020: bool Binary_Value_Object_Description(
Edouard@2020:     uint32_t object_instance,
Edouard@2020:     BACNET_CHARACTER_STRING * object_name)
Edouard@2020: {
Edouard@2020:     bool    status = false;
Edouard@2020:     unsigned index = Binary_Value_Instance_To_Index(object_instance);
Edouard@2020: 
Edouard@2020:     if (index < MAX_BINARY_VALUES)
Edouard@2020:         status = characterstring_init_ansi(object_name, BV_Descr[index].Description);
Edouard@2020:     
Edouard@2020:     return status;    
Edouard@2020: }
Edouard@2020: 
Edouard@2020: 
Edouard@2020: 
Edouard@2020: /* return apdu len, or BACNET_STATUS_ERROR on error */
Edouard@2020: /* This is a callback function. Callback set in My_Object_Table[] array declared in device.c,  */
Edouard@2020: int Binary_Value_Read_Property(
Edouard@2020:     BACNET_READ_PROPERTY_DATA * rpdata)
Edouard@2020: {
Edouard@2020:     int len = 0;
Edouard@2020:     int apdu_len = 0;   /* return value */
Edouard@2020:     BACNET_BIT_STRING bit_string;
Edouard@2020:     BACNET_CHARACTER_STRING char_string;
Edouard@2020:     BACNET_BINARY_PV present_value = BINARY_INACTIVE;
Edouard@2020:     unsigned object_index = 0;
Edouard@2020:     unsigned i = 0;
Edouard@2020:     bool state = false;
Edouard@2020:     uint8_t *apdu = NULL;
Edouard@2020: 
Edouard@2020:     // fprintf(stderr, "BACnet plugin: Binary_Value_Read_Property() called!\n");
Edouard@2020: 
Edouard@2020:     if ((rpdata == NULL) || (rpdata->application_data == NULL) ||
Edouard@2020:         (rpdata->application_data_len == 0)) {
Edouard@2020:         return 0;
Edouard@2020:     }
Edouard@2020: 
Edouard@2020:     object_index = Binary_Value_Instance_To_Index(rpdata->object_instance);
Edouard@2020:     if (object_index >= MAX_BINARY_VALUES) {
Edouard@2020:         rpdata->error_class = ERROR_CLASS_OBJECT;
Edouard@2020:         rpdata->error_code  = ERROR_CODE_UNKNOWN_OBJECT;
Edouard@2020:         return BACNET_STATUS_ERROR;
Edouard@2020:     }
Edouard@2020:     
Edouard@2020:     apdu = rpdata->application_data;
Edouard@2020:     switch (rpdata->object_property) {
Edouard@2020:         case PROP_OBJECT_IDENTIFIER:
Edouard@2020:             apdu_len =
Edouard@2020:                 encode_application_object_id(&apdu[0], OBJECT_BINARY_VALUE,
Edouard@2020:                 rpdata->object_instance);
Edouard@2020:             break;
Edouard@2020:         case PROP_OBJECT_NAME:
Edouard@2020:             Binary_Value_Object_Name(rpdata->object_instance, &char_string);
Edouard@2020:             apdu_len =
Edouard@2020:                 encode_application_character_string(&apdu[0], &char_string);
Edouard@2020:             break;
Edouard@2020:         case PROP_DESCRIPTION:
Edouard@2020:             Binary_Value_Object_Description(rpdata->object_instance, &char_string);
Edouard@2020:             apdu_len =
Edouard@2020:                 encode_application_character_string(&apdu[0], &char_string);
Edouard@2020:             break;
Edouard@2020:         case PROP_OBJECT_TYPE:
Edouard@2020:             apdu_len =
Edouard@2020:                 encode_application_enumerated(&apdu[0], OBJECT_BINARY_VALUE);
Edouard@2020:             break;
Edouard@2020:         case PROP_PRESENT_VALUE:
Edouard@2020:             present_value =
Edouard@2020:                 Binary_Value_Present_Value(rpdata->object_instance);
Edouard@2020:             apdu_len = encode_application_enumerated(&apdu[0], present_value);
Edouard@2020:             break;
Edouard@2020:         case PROP_STATUS_FLAGS:
Edouard@2020:             /* note: see the details in the standard on how to use these */
Edouard@2020:             bitstring_init(&bit_string);
Edouard@2020:             bitstring_set_bit(&bit_string, STATUS_FLAG_IN_ALARM, false);
Edouard@2020:             bitstring_set_bit(&bit_string, STATUS_FLAG_FAULT, false);
Edouard@2020:             bitstring_set_bit(&bit_string, STATUS_FLAG_OVERRIDDEN, false);
Edouard@2020:             state = BV_Descr[object_index].Out_Of_Service;
Edouard@2020:             bitstring_set_bit(&bit_string, STATUS_FLAG_OUT_OF_SERVICE, state);
Edouard@2020:             apdu_len = encode_application_bitstring(&apdu[0], &bit_string);
Edouard@2020:             break;
Edouard@2020:         case PROP_EVENT_STATE:
Edouard@2020:             /* note: see the details in the standard on how to use this */
Edouard@2020:             apdu_len =
Edouard@2020:                 encode_application_enumerated(&apdu[0], EVENT_STATE_NORMAL);
Edouard@2020:             break;
Edouard@2020:         case PROP_OUT_OF_SERVICE:
Edouard@2020:             state = BV_Descr[object_index].Out_Of_Service;
Edouard@2020:             apdu_len = encode_application_boolean(&apdu[0], state);
Edouard@2020:             break;
Edouard@2020:         case PROP_PRIORITY_ARRAY:
Edouard@2020:             BACnet_encode_array(BV_Descr[object_index].Present_Value,
Edouard@2020:                                 BACNET_MAX_PRIORITY,
Edouard@2020:                                 BINARY_VALUE_IS_NULL,
Edouard@2020:                                 encode_application_enumerated)
Edouard@2020:             break;
Edouard@2020: //      case PROP_CURRENT_COMMAND_PRIORITY: {
Edouard@2020: //          unsigned i = Binary_Value_Current_Command_Priority(rpdata->object_instance);
Edouard@2020: //          if (i == 0)  apdu_len = encode_application_null    (&apdu[0]);
Edouard@2020: //          else         apdu_len = encode_application_unsigned(&apdu[0], i);
Edouard@2020: //          break;
Edouard@2020: //      }
Edouard@2020: 
Edouard@2020:         case PROP_RELINQUISH_DEFAULT:
Edouard@2020:             present_value = BV_VALUE_RELINQUISH_DEFAULT;
Edouard@2020:             apdu_len = encode_application_enumerated(&apdu[0], present_value);
Edouard@2020:             break;
Edouard@2020: //      case PROP_PROPERTY_LIST:
Edouard@2020: //          BACnet_encode_array(Binary_Value_Properties_List,
Edouard@2020: //                              property_list_count(Binary_Value_Properties_List),
Edouard@2020: //                              retfalse, encode_application_enumerated);
Edouard@2020: //          break;
Edouard@2020:         default:
Edouard@2020:             rpdata->error_class = ERROR_CLASS_PROPERTY;
Edouard@2020:             rpdata->error_code = ERROR_CODE_UNKNOWN_PROPERTY;
Edouard@2020:             apdu_len = BACNET_STATUS_ERROR;
Edouard@2020:             break;
Edouard@2020:     }
Edouard@2020:     /*  only array properties can have array options */
Edouard@2020:     if ((apdu_len >= 0) && (rpdata->object_property != PROP_PRIORITY_ARRAY) &&
Edouard@2020: //      (rpdata->object_property != PROP_PROPERTY_LIST) &&
Edouard@2020:         (rpdata->array_index != BACNET_ARRAY_ALL)) {
Edouard@2020:         rpdata->error_class = ERROR_CLASS_PROPERTY;
Edouard@2020:         rpdata->error_code = ERROR_CODE_PROPERTY_IS_NOT_AN_ARRAY;
Edouard@2020:         apdu_len = BACNET_STATUS_ERROR;
Edouard@2020:     }
Edouard@2020: 
Edouard@2020:     return apdu_len;
Edouard@2020: }
Edouard@2020: 
Edouard@2020: 
Edouard@2020: 
Edouard@2020: 
Edouard@2020: /* returns true if successful */
Edouard@2020: /* This is a callback function. Callback set in My_Object_Table[] array declared in device.c,  */
Edouard@2020: bool Binary_Value_Write_Property(
Edouard@2020:     BACNET_WRITE_PROPERTY_DATA * wp_data)
Edouard@2020: {
Edouard@2020:     bool status = false;        /* return value */
Edouard@2020:     unsigned int object_index = 0;
Edouard@2020:     unsigned int priority = 0;
Edouard@2020:     BACNET_BINARY_PV level = BINARY_NULL;
Edouard@2020:     int len = 0;
Edouard@2020:     BACNET_APPLICATION_DATA_VALUE value;
Edouard@2020: 
Edouard@2020:     /* decode the some of the request */
Edouard@2020:     len =
Edouard@2020:         bacapp_decode_application_data(wp_data->application_data,
Edouard@2020:         wp_data->application_data_len, &value);
Edouard@2020:     /* FIXME: len < application_data_len: more data? */
Edouard@2020:     if (len < 0) {
Edouard@2020:         /* error while decoding - a value larger than we can handle */
Edouard@2020:         wp_data->error_class = ERROR_CLASS_PROPERTY;
Edouard@2020:         wp_data->error_code = ERROR_CODE_VALUE_OUT_OF_RANGE;
Edouard@2020:         return false;
Edouard@2020:     }
Edouard@2020:     /*  only array properties can have array options */
Edouard@2020:     if ((wp_data->object_property != PROP_PRIORITY_ARRAY) &&
Edouard@2020:         (wp_data->array_index != BACNET_ARRAY_ALL)) {
Edouard@2020:         wp_data->error_class = ERROR_CLASS_PROPERTY;
Edouard@2020:         wp_data->error_code = ERROR_CODE_PROPERTY_IS_NOT_AN_ARRAY;
Edouard@2020:         return false;
Edouard@2020:     }
Edouard@2020:     /* No need to check whether object_index is within bounds.
Edouard@2020:      * Has already been checked before Binary_Value_Write_Property() is called
Edouard@2020:      */
Edouard@2020:     object_index = Binary_Value_Instance_To_Index(wp_data->object_instance);
Edouard@2020: 
Edouard@2020:     switch (wp_data->object_property) {
Edouard@2020:         case PROP_PRESENT_VALUE:
Edouard@2020:             if (value.tag == BACNET_APPLICATION_TAG_ENUMERATED) {
Edouard@2020:                 priority = wp_data->priority;
Edouard@2020:                 if (priority && (priority <= BACNET_MAX_PRIORITY) &&
Edouard@2020:                     (priority != 6 /* reserved */ ) &&
Edouard@2020:                     (value.type.Enumerated <= MAX_BINARY_PV)) {
Edouard@2020:                     level = (BACNET_BINARY_PV) value.type.Enumerated;
Edouard@2020:                     priority--;
Edouard@2020:                     BV_Descr[object_index].Present_Value[priority] = level;
Edouard@2020:                     status = true;
Edouard@2020:                 } else if (priority == 6) {
Edouard@2020:                     /* Command priority 6 is reserved for use by Minimum On/Off
Edouard@2020:                        algorithm and may not be used for other purposes in any
Edouard@2020:                        object. */
Edouard@2020:                     wp_data->error_class = ERROR_CLASS_PROPERTY;
Edouard@2020:                     wp_data->error_code = ERROR_CODE_WRITE_ACCESS_DENIED;
Edouard@2020:                 } else {
Edouard@2020:                     wp_data->error_class = ERROR_CLASS_PROPERTY;
Edouard@2020:                     wp_data->error_code = ERROR_CODE_VALUE_OUT_OF_RANGE;
Edouard@2020:                 }
Edouard@2020:             } else {
Edouard@2020:                 status =
Edouard@2020:                     WPValidateArgType(&value, BACNET_APPLICATION_TAG_NULL,
Edouard@2020:                     &wp_data->error_class, &wp_data->error_code);
Edouard@2020:                 if (status) {
Edouard@2020:                     level = BINARY_NULL;
Edouard@2020:                     priority = wp_data->priority;
Edouard@2020:                     if (priority && (priority <= BACNET_MAX_PRIORITY)) {
Edouard@2020:                         priority--;
Edouard@2020:                         BV_Descr[object_index].Present_Value[priority] = level;
Edouard@2020:                     } else {
Edouard@2020:                         status = false;
Edouard@2020:                         wp_data->error_class = ERROR_CLASS_PROPERTY;
Edouard@2020:                         wp_data->error_code = ERROR_CODE_VALUE_OUT_OF_RANGE;
Edouard@2020:                     }
Edouard@2020:                 }
Edouard@2020:             }
Edouard@2020:             break;
Edouard@2020:         case PROP_OUT_OF_SERVICE:
Edouard@2020:         {
Edouard@2020:             bool Previous_Out_Of_Service = BV_Descr[object_index].Out_Of_Service;
Edouard@2020:             status =
Edouard@2020:                 WPValidateArgType(&value, BACNET_APPLICATION_TAG_BOOLEAN,
Edouard@2020:                 &wp_data->error_class, &wp_data->error_code);
Edouard@2020:             if (status) {
Edouard@2020:                 BV_Descr[object_index].Out_Of_Service = value.type.Boolean;
Edouard@2020:                 if (Previous_Out_Of_Service && !BV_Descr[object_index].Out_Of_Service)
Edouard@2020:                     /* We have just changed from Out_of_Service -> In Service */
Edouard@2020:                     /* We need to update the Present_Value to the value
Edouard@2020:                      * currently in the PLC...
Edouard@2020:                      */
Edouard@2020:                     BV_Descr[object_index].Present_Value[BACNET_MAX_PRIORITY-1] =
Edouard@2020:                                            *(BV_Descr[object_index].Located_Var_ptr);
Edouard@2020:             }
Edouard@2020:             break;
Edouard@2020:         }
Edouard@2020:         case PROP_OBJECT_IDENTIFIER:
Edouard@2020:         case PROP_OBJECT_NAME:
Edouard@2020:         case PROP_DESCRIPTION:
Edouard@2020:         case PROP_OBJECT_TYPE:
Edouard@2020:         case PROP_STATUS_FLAGS:
Edouard@2020:         case PROP_EVENT_STATE:
Edouard@2020:         case PROP_PRIORITY_ARRAY:
Edouard@2020: //      case PROP_CURRENT_COMMAND_PRIORITY:
Edouard@2020:         case PROP_RELINQUISH_DEFAULT:
Edouard@2020: //      case PROP_PROPERTY_LIST:
Edouard@2020:             wp_data->error_class = ERROR_CLASS_PROPERTY;
Edouard@2020:             wp_data->error_code = ERROR_CODE_WRITE_ACCESS_DENIED;
Edouard@2020:             break;
Edouard@2020:         default:
Edouard@2020:             wp_data->error_class = ERROR_CLASS_PROPERTY;
Edouard@2020:             wp_data->error_code = ERROR_CODE_UNKNOWN_PROPERTY;
Edouard@2020:             break;
Edouard@2020:     }
Edouard@2020: 
Edouard@2020:     return status;
Edouard@2020: }
Edouard@2020: 
Edouard@2020: 
Edouard@2020: 
Edouard@2020: 
Edouard@2020: 
Edouard@2020: /********************************************/
Edouard@2020: /** Functions required for Beremiz plugin  **/
Edouard@2020: /********************************************/
Edouard@2020: 
Edouard@2020: 
Edouard@2020: 
Edouard@2020: void  Binary_Value_Copy_Present_Value_to_Located_Var(void) {
Edouard@2020:     unsigned i;
Edouard@2020:     for (i = 0; i < MAX_BINARY_VALUES; i++) {
Edouard@2020:         // decouple PLC's Located Variable from Bacnet Object's Present Value if Out_Of_Service is true
Edouard@2020:         if (BV_Descr[i].Out_Of_Service)
Edouard@2020:             continue;
Edouard@2020: 
Edouard@2020:         // copy the value
Edouard@2020:         *(BV_Descr[i].Located_Var_ptr) = Binary_Value_Present_Value(BV_Descr[i].Object_Identifier);
Edouard@2020:     }
Edouard@2020: }
Edouard@2020:   
Edouard@2020: void  Binary_Value_Copy_Located_Var_to_Present_Value(void) {
Edouard@2020:     unsigned i;
Edouard@2020:     for (i = 0; i < MAX_BINARY_VALUES; i++) {
Edouard@2020:         // decouple PLC's Located Variable from Bacnet Object's Present Value if Out_Of_Service is true
Edouard@2020:         if (BV_Descr[i].Out_Of_Service)
Edouard@2020:             continue;
Edouard@2020: 
Edouard@2020:         // copy the value
Edouard@2020:         BV_Descr[i].Present_Value[BACNET_MAX_PRIORITY-1] = *(BV_Descr[i].Located_Var_ptr);
Edouard@2020: 
Edouard@2020:         // If the Present_Value was set to an invalid value (i.e. > 1, and < BINARY_NULL)
Edouard@2020:         //   then we set it to BINARY_ACTIVE 
Edouard@2020:         //   (i.e. we assume 0 is FALSE, all other non NULL values are TRUE)
Edouard@2020:         if ((BV_Descr[i].Present_Value[BACNET_MAX_PRIORITY-1] != BINARY_INACTIVE) &&
Edouard@2020:             (BV_Descr[i].Present_Value[BACNET_MAX_PRIORITY-1] != BINARY_ACTIVE  ) &&
Edouard@2020:             (BV_Descr[i].Present_Value[BACNET_MAX_PRIORITY-1] != BINARY_NULL    ))
Edouard@2020:              BV_Descr[i].Present_Value[BACNET_MAX_PRIORITY-1]  = BINARY_ACTIVE;          
Edouard@2020:     }
Edouard@2020: }
Edouard@2020: 
Edouard@2020: