Edouard@2020: /**************************************************************************
Edouard@2020: *
Edouard@2020: * Copyright (C) 2012 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: /* Multi-state Output Objects */
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 pluginh  */
Edouard@2020: #include "bacdef.h"
Edouard@2020: #include "bacdcode.h"
Edouard@2020: #include "bacenum.h"
Edouard@2020: #include "bacapp.h"
Edouard@2020: #include "rp.h"
Edouard@2020: #include "wp.h"
Edouard@2020: #include "mso_%(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: /* Since multi state value objects can never take the value 0, we 
Edouard@2020:  * use 0 as our NULL value.
Edouard@2020:  * See ASHRAE 135-2016, section 12.20.4
Edouard@2020:  */
Edouard@2020: #define MSO_VALUE_NULL (0)
Edouard@2020: #define MSO_VALUE_IS_NULL(x)  ((x) == MSO_VALUE_NULL)
Edouard@2020: /* When all the priorities are level null, the present value returns */
Edouard@2020: /* the Relinquish Default value */
Edouard@2020: #define MSO_VALUE_RELINQUISH_DEFAULT 1
Edouard@2020: 
Edouard@2020: 
Edouard@2020: 
Edouard@2020: /* The IEC 61131-3 located variables mapped onto the Multi-state Output objects of BACnet protocol */
Edouard@2020: %(MSO_lvars)s
Edouard@2020: 
Edouard@2020: 
Edouard@2020: /* The array where we keep all the state related to the Multi-state Output Objects */
Edouard@2020: #define MAX_MULTISTATE_OUTPUTS %(MSO_count)s
Edouard@2020: static MULTISTATE_OUTPUT_DESCR MSO_Descr[MAX_MULTISTATE_OUTPUTS] = {
Edouard@2020: %(MSO_param)s
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 Multistate_Output_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  W ( 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_NUMBER_OF_STATES,        /* R  R ( 74) */
Edouard@2020:     PROP_PRIORITY_ARRAY,          /* R  R ( 87) */
Edouard@2020:     PROP_RELINQUISH_DEFAULT,      /* R  R (104) */
Edouard@2020: //  PROP_PROPERTY_LIST,           /* R  R (371) */
Edouard@2020: //  PROP_CURRENT_COMMAND_PRIORITY,/* R  R (431) */   
Edouard@2020:     -1
Edouard@2020: };
Edouard@2020: 
Edouard@2020: static const int Multistate_Output_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:     PROP_STATE_TEXT,              /* R  O (110) */
Edouard@2020:     -1
Edouard@2020: };
Edouard@2020: 
Edouard@2020: static const int Multistate_Output_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 Multistate_Output_Init() based off the values
Edouard@2020:  * stored in Multistate_Output_Properties_Required 
Edouard@2020:  *           Multistate_Output_Properties_Optional
Edouard@2020:  *           Multistate_Output_Properties_Proprietary
Edouard@2020:  */
Edouard@2020: /* TODO: Allocate memory for this array with malloc() at startup */
Edouard@2020: static int Multistate_Output_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 Multistate_Output_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 = Multistate_Output_Properties_Required;
Edouard@2020:     if (pOptional)
Edouard@2020:         *pOptional = Multistate_Output_Properties_Optional;
Edouard@2020:     if (pProprietary)
Edouard@2020:         *pProprietary = Multistate_Output_Properties_Proprietary;
Edouard@2020: 
Edouard@2020:     return;
Edouard@2020: }
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 Multistate_Output_Init(
Edouard@2020:     void)
Edouard@2020: {
Edouard@2020:     unsigned int i, j;
Edouard@2020: 
Edouard@2020:     /* initialize the Multistate_Output_Properties_List array */
Edouard@2020:     int len = 0;
Edouard@2020:     len += BACnet_Init_Properties_List(Multistate_Output_Properties_List + len,
Edouard@2020:                                        Multistate_Output_Properties_Required);
Edouard@2020:     len += BACnet_Init_Properties_List(Multistate_Output_Properties_List + len,
Edouard@2020:                                        Multistate_Output_Properties_Optional);
Edouard@2020:     len += BACnet_Init_Properties_List(Multistate_Output_Properties_List + len,
Edouard@2020:                                        Multistate_Output_Properties_Proprietary);
Edouard@2020: 
Edouard@2020:     /* initialize all the analog output priority arrays to NULL */
Edouard@2020:     for (i = 0; i < MAX_MULTISTATE_OUTPUTS; i++) {
Edouard@2020:         for (j = 0; j < BACNET_MAX_PRIORITY; j++) {
Edouard@2020:             MSO_Descr[i].Present_Value[j] = MSO_VALUE_NULL;
Edouard@2020:         }
Edouard@2020:         for (j = 0; j < MSO_Descr[i].Number_Of_States; j++) {
Edouard@2020:             sprintf(MSO_Descr[i].State_Text[j], "State %%d", j+1);
Edouard@2020:         }
Edouard@2020:     }
Edouard@2020:     return;
Edouard@2020: }
Edouard@2020: 
Edouard@2020: 
Edouard@2020: 
Edouard@2020: /* validate that the given instance exists */
Edouard@2020: /* This is a callback function. Callback set in My_Object_Table[] array declared in device.c,  */
Edouard@2020: bool Multistate_Output_Valid_Instance(
Edouard@2020:     uint32_t object_instance)
Edouard@2020: {
Edouard@2020:     // fprintf(stderr, "BACnet plugin: Multistate_Output_Valid_Instance(obj_ID=%%u) called!\n", object _instance);
Edouard@2020:     return (Multistate_Output_Instance_To_Index(object_instance) < MAX_MULTISTATE_OUTPUTS);
Edouard@2020: }
Edouard@2020: 
Edouard@2020: 
Edouard@2020: /* the number of Multistate Value Objects */
Edouard@2020: /* This is a callback function. Callback set in My_Object_Table[] array declared in device.c,  */
Edouard@2020: unsigned Multistate_Output_Count(void) {return MAX_MULTISTATE_OUTPUTS;}
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 Multistate_Output_Index_To_Instance(unsigned index) {return MSO_Descr[index].Object_Identifier;}
Edouard@2020: 
Edouard@2020: 
Edouard@2020: 
Edouard@2020: 
Edouard@2020: 
Edouard@2020: 
Edouard@2020: /* returns the index that correlates to the correct instance number (Object Identifier) */
Edouard@2020: unsigned Multistate_Output_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_MULTISTATE_OUTPUTS; index++)
Edouard@2020:         if (object_instance == MSO_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_MULTISTATE_OUTPUTS;
Edouard@2020: }
Edouard@2020: 
Edouard@2020: 
Edouard@2020: 
Edouard@2020: 
Edouard@2020: 
Edouard@2020: uint32_t Multistate_Output_Present_Value(
Edouard@2020:     uint32_t object_instance)
Edouard@2020: {
Edouard@2020:     uint32_t value = MSO_VALUE_RELINQUISH_DEFAULT;
Edouard@2020:     unsigned index = 0; /* offset from instance lookup */
Edouard@2020:     unsigned i = 0;
Edouard@2020: 
Edouard@2020:     index = Multistate_Output_Instance_To_Index(object_instance);
Edouard@2020:     if (index < MAX_MULTISTATE_OUTPUTS) {
Edouard@2020:         for (i = 0; i < BACNET_MAX_PRIORITY; i++) {
Edouard@2020:             if (!MSO_VALUE_IS_NULL(MSO_Descr[index].Present_Value[i])) {
Edouard@2020:                 value = MSO_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: bool Multistate_Output_Present_Value_Set(
Edouard@2020:     uint32_t object_instance,
Edouard@2020:     uint32_t value,
Edouard@2020:     uint8_t  priority)
Edouard@2020: {
Edouard@2020:     unsigned index = 0; /* offset from instance lookup */
Edouard@2020: 
Edouard@2020:     index = Multistate_Output_Instance_To_Index(object_instance);
Edouard@2020:     if (index >= MAX_MULTISTATE_OUTPUTS)
Edouard@2020:       return false;
Edouard@2020: 
Edouard@2020:     if ((priority == 0) || (priority > BACNET_MAX_PRIORITY) ||
Edouard@2020:         (priority == 6 /* reserved, ASHRAE 135-2016, section 19.2.2 */))
Edouard@2020:       return false;
Edouard@2020:     
Edouard@2020:     if ((value == 0) || (value > MSO_Descr[index].Number_Of_States))
Edouard@2020:       return false;
Edouard@2020:       
Edouard@2020:     priority--;
Edouard@2020:     MSO_Descr[index].Present_Value[priority] = (uint8_t) value;
Edouard@2020: 
Edouard@2020:     return true;
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 Multistate_Output_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 = Multistate_Output_Instance_To_Index(object_instance);
Edouard@2020:     if (index < MAX_MULTISTATE_OUTPUTS) {
Edouard@2020:         for (i = 0; i < BACNET_MAX_PRIORITY; i++) {
Edouard@2020:             if (!MSO_VALUE_IS_NULL(MSO_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: 
Edouard@2020: bool Multistate_Output_Present_Value_Relinquish(
Edouard@2020:     uint32_t object_instance,
Edouard@2020:     uint8_t  priority)
Edouard@2020: {
Edouard@2020:     unsigned index = 0; /* offset from instance lookup */
Edouard@2020: 
Edouard@2020:     index = Multistate_Output_Instance_To_Index(object_instance);
Edouard@2020:     if (index >= MAX_MULTISTATE_OUTPUTS)
Edouard@2020:       return false;
Edouard@2020:     
Edouard@2020:     if ((priority == 0) || (priority > BACNET_MAX_PRIORITY) ||
Edouard@2020:         (priority == 6 /* reserved, ASHRAE 135-2016, section 19.2.2 */))
Edouard@2020:       return false;
Edouard@2020:     
Edouard@2020:     priority--;
Edouard@2020:     MSO_Descr[index].Present_Value[priority] = MSO_VALUE_NULL;
Edouard@2020: 
Edouard@2020:     return true;
Edouard@2020: }
Edouard@2020: 
Edouard@2020: 
Edouard@2020: 
Edouard@2020: bool Multistate_Output_Out_Of_Service(
Edouard@2020:     uint32_t object_instance)
Edouard@2020: {
Edouard@2020:     bool value = false;
Edouard@2020:     unsigned index = 0;
Edouard@2020: 
Edouard@2020:     index = Multistate_Output_Instance_To_Index(object_instance);
Edouard@2020:     if (index < MAX_MULTISTATE_OUTPUTS) {
Edouard@2020:         value = MSO_Descr[index].Out_Of_Service;
Edouard@2020:     }
Edouard@2020: 
Edouard@2020:     return value;
Edouard@2020: }
Edouard@2020: 
Edouard@2020: 
Edouard@2020: 
Edouard@2020: void Multistate_Output_Out_Of_Service_Set(
Edouard@2020:     uint32_t object_instance,
Edouard@2020:     bool value)
Edouard@2020: {
Edouard@2020:     unsigned index = 0;
Edouard@2020: 
Edouard@2020:     index = Multistate_Output_Instance_To_Index(object_instance);
Edouard@2020:     if (index < MAX_MULTISTATE_OUTPUTS) {
Edouard@2020:         MSO_Descr[index].Out_Of_Service = value;
Edouard@2020:     }
Edouard@2020: 
Edouard@2020:     return;
Edouard@2020: }
Edouard@2020: 
Edouard@2020: 
Edouard@2020: 
Edouard@2020: static char *Multistate_Output_Description(
Edouard@2020:     uint32_t object_instance)
Edouard@2020: {
Edouard@2020:     unsigned index = 0; /* offset from instance lookup */
Edouard@2020:     char *pName = NULL; /* return value */
Edouard@2020: 
Edouard@2020:     index = Multistate_Output_Instance_To_Index(object_instance);
Edouard@2020:     if (index < MAX_MULTISTATE_OUTPUTS) {
Edouard@2020:         pName = MSO_Descr[index].Description;
Edouard@2020:     }
Edouard@2020: 
Edouard@2020:     return pName;
Edouard@2020: }
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: bool Multistate_Output_Object_Name(
Edouard@2020:     uint32_t object_instance,
Edouard@2020:     BACNET_CHARACTER_STRING * object_name)
Edouard@2020: {
Edouard@2020:     unsigned index = 0; /* offset from instance lookup */
Edouard@2020:     bool status = false;
Edouard@2020: 
Edouard@2020:     index = Multistate_Output_Instance_To_Index(object_instance);
Edouard@2020:     if (index < MAX_MULTISTATE_OUTPUTS) {
Edouard@2020:         status = characterstring_init_ansi(object_name, MSO_Descr[index].Object_Name);
Edouard@2020:     }
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 Multistate_Output_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:     uint32_t present_value = 0;
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:     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 = Multistate_Output_Instance_To_Index(rpdata->object_instance);
Edouard@2020:     if (object_index >= MAX_MULTISTATE_OUTPUTS) {
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],
Edouard@2020:                 OBJECT_MULTI_STATE_OUTPUT, rpdata->object_instance);
Edouard@2020:             break;
Edouard@2020:         case PROP_OBJECT_NAME:
Edouard@2020:             Multistate_Output_Object_Name(rpdata->object_instance,
Edouard@2020:                 &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:             characterstring_init_ansi(&char_string,
Edouard@2020:                 Multistate_Output_Description(rpdata->object_instance));
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],
Edouard@2020:                 OBJECT_MULTI_STATE_OUTPUT);
Edouard@2020:             break;
Edouard@2020:         case PROP_PRESENT_VALUE:
Edouard@2020:             present_value =
Edouard@2020:                 Multistate_Output_Present_Value(rpdata->object_instance);
Edouard@2020:             apdu_len = encode_application_unsigned(&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:             if (Multistate_Output_Out_Of_Service(rpdata->object_instance)) {
Edouard@2020:                 bitstring_set_bit(&bit_string, STATUS_FLAG_OUT_OF_SERVICE,
Edouard@2020:                     true);
Edouard@2020:             } else {
Edouard@2020:                 bitstring_set_bit(&bit_string, STATUS_FLAG_OUT_OF_SERVICE,
Edouard@2020:                     false);
Edouard@2020:             }
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 = MSO_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(MSO_Descr[object_index].Present_Value,
Edouard@2020:                                 BACNET_MAX_PRIORITY,
Edouard@2020:                                 MSO_VALUE_IS_NULL,
Edouard@2020:                                 encode_application_unsigned);
Edouard@2020:             break;
Edouard@2020: //      case PROP_CURRENT_COMMAND_PRIORITY: {
Edouard@2020: //          unsigned i = Multistate_Output_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 = MSO_VALUE_RELINQUISH_DEFAULT;
Edouard@2020:             apdu_len = encode_application_unsigned(&apdu[0], present_value);
Edouard@2020:             break;
Edouard@2020:         case PROP_NUMBER_OF_STATES:
Edouard@2020:             apdu_len =
Edouard@2020:                 encode_application_unsigned(&apdu[apdu_len],
Edouard@2020:                 MSO_Descr[object_index].Number_Of_States);
Edouard@2020:             break;
Edouard@2020:         case PROP_STATE_TEXT:
Edouard@2020:             BACnet_encode_array(MSO_Descr[object_index].State_Text,
Edouard@2020:                                 MSO_Descr[object_index].Number_Of_States,
Edouard@2020:                                 retfalse, BACnet_encode_character_string);
Edouard@2020:             break;
Edouard@2020: //      case PROP_PROPERTY_LIST:
Edouard@2020: //          BACnet_encode_array(Multistate_Output_Properties_List,
Edouard@2020: //                              property_list_count(Multistate_Output_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_STATE_TEXT) &&
Edouard@2020:         (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: /* 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 Multistate_Output_Write_Property(
Edouard@2020:     BACNET_WRITE_PROPERTY_DATA * wp_data)
Edouard@2020: {
Edouard@2020:     bool status = false;        /* return value */
Edouard@2020:     int len = 0;
Edouard@2020:     unsigned object_index = 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:     if ((wp_data->object_property != PROP_STATE_TEXT) &&
Edouard@2020:         (wp_data->object_property != PROP_PRIORITY_ARRAY) &&
Edouard@2020:         (wp_data->array_index != BACNET_ARRAY_ALL)) {
Edouard@2020:         /*  only array properties can have array options */
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 Multistate_Output_Write_Property() is called
Edouard@2020:      */
Edouard@2020:     object_index = Multistate_Output_Instance_To_Index(wp_data->object_instance);
Edouard@2020: 
Edouard@2020:     switch (wp_data->object_property) {
Edouard@2020:         case PROP_PRESENT_VALUE:
Edouard@2020:             status =
Edouard@2020:                 WPValidateArgType(&value, BACNET_APPLICATION_TAG_UNSIGNED_INT,
Edouard@2020:                 &wp_data->error_class, &wp_data->error_code);
Edouard@2020:             if (status) {
Edouard@2020:                 status =
Edouard@2020:                     Multistate_Output_Present_Value_Set
Edouard@2020:                     (wp_data->object_instance, value.type.Unsigned_Int, wp_data->priority);
Edouard@2020:                 if (!status) {
Edouard@2020:                     if (wp_data->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:                 }
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:                     status =
Edouard@2020:                         Multistate_Output_Present_Value_Relinquish
Edouard@2020:                         (wp_data->object_instance, wp_data->priority);
Edouard@2020:                 }
Edouard@2020:                 if (!status) {
Edouard@2020:                     wp_data->error_class = ERROR_CLASS_PROPERTY;
Edouard@2020:                     wp_data->error_code = ERROR_CODE_INVALID_DATA_TYPE;
Edouard@2020:                 }
Edouard@2020:             }
Edouard@2020:             break;
Edouard@2020:         case PROP_OUT_OF_SERVICE:
Edouard@2020:         {
Edouard@2020:             bool Previous_Out_Of_Service = MSO_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:                 MSO_Descr[object_index].Out_Of_Service = value.type.Boolean;
Edouard@2020:                 if (Previous_Out_Of_Service && !MSO_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:                     MSO_Descr[object_index].Present_Value[BACNET_MAX_PRIORITY-1] =
Edouard@2020:                                            *(MSO_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_OBJECT_TYPE:
Edouard@2020:         case PROP_STATUS_FLAGS:
Edouard@2020:         case PROP_EVENT_STATE:
Edouard@2020:         case PROP_NUMBER_OF_STATES:
Edouard@2020:         case PROP_PRIORITY_ARRAY:
Edouard@2020: //      case PROP_CURRENT_COMMAND_PRIORITY:
Edouard@2020:         case PROP_RELINQUISH_DEFAULT:
Edouard@2020:         case PROP_DESCRIPTION:
Edouard@2020:         case PROP_STATE_TEXT:
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  Multistate_Output_Copy_Present_Value_to_Located_Var(void) {
Edouard@2020:     unsigned i;
Edouard@2020:     for (i = 0; i < MAX_MULTISTATE_OUTPUTS; i++) {
Edouard@2020:         // decouple PLC's Located Variable from Bacnet Object's Present Value if Out_Of_Service is true
Edouard@2020:         if (MSO_Descr[i].Out_Of_Service)
Edouard@2020:             continue;
Edouard@2020: 
Edouard@2020:         // copy the value
Edouard@2020:         *(MSO_Descr[i].Located_Var_ptr) = Multistate_Output_Present_Value(MSO_Descr[i].Object_Identifier);
Edouard@2020:     }
Edouard@2020: }
Edouard@2020: 
Edouard@2020: 
Edouard@2020: 
Edouard@2020: void  Multistate_Output_Copy_Located_Var_to_Present_Value(void) {
Edouard@2020:     unsigned i;
Edouard@2020:     for (i = 0; i < MAX_MULTISTATE_OUTPUTS; i++) {
Edouard@2020:         // decouple PLC's Located Variable from Bacnet Object's Present Value if Out_Of_Service is true
Edouard@2020:         if (MSO_Descr[i].Out_Of_Service)
Edouard@2020:             continue;
Edouard@2020: 
Edouard@2020:         // Make sure local device does not set Present_Value to a value higher than Number_Of_States
Edouard@2020:         /* NOTE: The following comparison (Present_Value > Number_Of_States) is OK as the
Edouard@2020:          *       MSO_VALUE_NULL is currently set to 0. This means that the local controller
Edouard@2020:          *       can safely relinquish control by setting the Present_Value to 0.
Edouard@2020:          *       The comparison would not be safe if for some reason we later change
Edouard@2020:          *       MSO_VALUE_NULL to a value that may be higher than Number_Of_States.
Edouard@2020:          */
Edouard@2020:         #if MSO_VALUE_NULL != 0
Edouard@2020:         #error Implementation assumes that MSO_VALUE_NULL is set to 0, which is currently not the case.
Edouard@2020:         #endif
Edouard@2020:         if (*(MSO_Descr[i].Located_Var_ptr) > MSO_Descr[i].Number_Of_States)
Edouard@2020:             continue;
Edouard@2020: 
Edouard@2020:         // Everything seems to be OK. Copy the value
Edouard@2020:         MSO_Descr[i].Present_Value[BACNET_MAX_PRIORITY-1] = *(MSO_Descr[i].Located_Var_ptr);
Edouard@2020:     }
Edouard@2020: }
Edouard@2020: 
Edouard@2020: 
Edouard@2020: