bacnet/runtime/msv.c
changeset 2189 49a6738b7c63
parent 2020 6dddf3070806
equal deleted inserted replaced
2186:2ec02f0f9fa9 2189:49a6738b7c63
       
     1 /**************************************************************************
       
     2 *
       
     3 * Copyright (C) 2012 Steve Karg <skarg@users.sourceforge.net>
       
     4 * Copyright (C) 2017 Mario de Sousa <msousa@fe.up.pt>
       
     5 *
       
     6 * Permission is hereby granted, free of charge, to any person obtaining
       
     7 * a copy of this software and associated documentation files (the
       
     8 * "Software"), to deal in the Software without restriction, including
       
     9 * without limitation the rights to use, copy, modify, merge, publish,
       
    10 * distribute, sublicense, and/or sell copies of the Software, and to
       
    11 * permit persons to whom the Software is furnished to do so, subject to
       
    12 * the following conditions:
       
    13 *
       
    14 * The above copyright notice and this permission notice shall be included
       
    15 * in all copies or substantial portions of the Software.
       
    16 *
       
    17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
       
    18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
       
    19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
       
    20 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
       
    21 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
       
    22 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
       
    23 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
       
    24 *
       
    25 *********************************************************************/
       
    26 
       
    27 /* Multi-state Value Objects */
       
    28 
       
    29 #include <stdbool.h>
       
    30 #include <stdint.h>
       
    31 #include <stdio.h>
       
    32 
       
    33 #include "config_bacnet_for_beremiz_%(locstr)s.h"     /* the custom configuration for beremiz pluginh  */
       
    34 #include "bacdef.h"
       
    35 #include "bacdcode.h"
       
    36 #include "bacenum.h"
       
    37 #include "bacapp.h"
       
    38 #include "rp.h"
       
    39 #include "wp.h"
       
    40 #include "msv_%(locstr)s.h"
       
    41 #include "handlers.h"
       
    42 
       
    43 /* we choose to have a NULL level in our system represented by */
       
    44 /* a particular value.  When the priorities are not in use, they */
       
    45 /* will be relinquished (i.e. set to the NULL level). */
       
    46 /* Since multi state value objects can never take the value 0, we 
       
    47  * use 0 as our NULL value.
       
    48  * See ASHRAE 135-2016, section 12.20.4
       
    49  */
       
    50 #define MSV_VALUE_NULL (0)
       
    51 #define MSV_VALUE_IS_NULL(x)  ((x) == MSV_VALUE_NULL)
       
    52 /* When all the priorities are level null, the present value returns */
       
    53 /* the Relinquish Default value */
       
    54 #define MSV_VALUE_RELINQUISH_DEFAULT 1
       
    55 
       
    56 
       
    57 
       
    58 /* The IEC 61131-3 located variables mapped onto the Multi-state Value objects of BACnet protocol */
       
    59 %(MSV_lvars)s
       
    60 
       
    61 
       
    62 /* The array where we keep all the state related to the Multi-state Value Objects */
       
    63 #define MAX_MULTISTATE_VALUES %(MSV_count)s
       
    64 static MULTISTATE_VALUE_DESCR MSV_Descr[MAX_MULTISTATE_VALUES] = {
       
    65 %(MSV_param)s
       
    66 };
       
    67 
       
    68 
       
    69 
       
    70 /* These three arrays are used by the ReadPropertyMultiple handler,
       
    71  * as well as to initialize the XXX_Property_List used by the 
       
    72  * Property List (PROP_PROPERTY_LIST) property.
       
    73  */
       
    74 static const int Multistate_Value_Properties_Required[] = {
       
    75  /* (1) Currently Supported                  */
       
    76  /* (2) Required by standard ASHRAE 135-2016 */
       
    77                                   /*(1)(2)      */
       
    78     PROP_OBJECT_IDENTIFIER,       /* R  R ( 75) */
       
    79     PROP_OBJECT_NAME,             /* R  R ( 77) */
       
    80     PROP_OBJECT_TYPE,             /* R  R ( 79) */
       
    81     PROP_PRESENT_VALUE,           /* W  R ( 85) */
       
    82     PROP_STATUS_FLAGS,            /* R  R (111) */
       
    83     PROP_EVENT_STATE,             /* R  R ( 36) */
       
    84     PROP_OUT_OF_SERVICE,          /* W  R ( 81) */
       
    85     PROP_NUMBER_OF_STATES,        /* R  R ( 74) */
       
    86 //  PROP_PROPERTY_LIST,           /* R  R (371) */
       
    87     -1
       
    88 };
       
    89 
       
    90 static const int Multistate_Value_Properties_Optional[] = {
       
    91     PROP_DESCRIPTION,             /* R  O ( 28) */
       
    92     PROP_STATE_TEXT,              /* R  O (110) */
       
    93     /* required if Present_Value is writable (which is true in our case!) */
       
    94     PROP_PRIORITY_ARRAY,          /* R  O ( 87) */
       
    95     PROP_RELINQUISH_DEFAULT,      /* R  O (104) */
       
    96 //  PROP_CURRENT_COMMAND_PRIORITY,/* R  O (431) */   
       
    97     -1
       
    98 };
       
    99 
       
   100 static const int Multistate_Value_Properties_Proprietary[] = {
       
   101     -1
       
   102 };
       
   103 
       
   104 
       
   105 /* This array stores the PROPERTY_LIST which may be read by clients.
       
   106  * End of list is marked by following the last element with the value '-1'
       
   107  * 
       
   108  * It is initialized by Multistate_Value_Init() based off the values
       
   109  * stored in Multistate_Value_Properties_Required 
       
   110  *           Multistate_Value_Properties_Optional
       
   111  *           Multistate_Value_Properties_Proprietary
       
   112  */
       
   113 /* TODO: Allocate memory for this array with malloc() at startup */
       
   114 static int Multistate_Value_Properties_List[64];
       
   115 
       
   116 
       
   117 
       
   118 
       
   119 
       
   120 /********************************************************/
       
   121 /**                  Callback functions.               **/
       
   122 /** Functions required by BACnet devie implementation. **/
       
   123 /********************************************************/
       
   124 
       
   125 
       
   126 /* This is a callback function. Callback set in My_Object_Table[] array declared in device.c,  */
       
   127 void Multistate_Value_Property_Lists(
       
   128     const int **pRequired,
       
   129     const int **pOptional,
       
   130     const int **pProprietary)
       
   131 {
       
   132     if (pRequired)
       
   133         *pRequired = Multistate_Value_Properties_Required;
       
   134     if (pOptional)
       
   135         *pOptional = Multistate_Value_Properties_Optional;
       
   136     if (pProprietary)
       
   137         *pProprietary = Multistate_Value_Properties_Proprietary;
       
   138 
       
   139     return;
       
   140 }
       
   141 
       
   142 
       
   143 
       
   144 /* This is a callback function. Callback set in My_Object_Table[] array declared in device.c,  */
       
   145 void Multistate_Value_Init(
       
   146     void)
       
   147 {
       
   148     unsigned int i, j;
       
   149 
       
   150     /* initialize the Multistate_Value_Properties_List array */
       
   151     int len = 0;
       
   152     len += BACnet_Init_Properties_List(Multistate_Value_Properties_List + len,
       
   153                                        Multistate_Value_Properties_Required);
       
   154     len += BACnet_Init_Properties_List(Multistate_Value_Properties_List + len,
       
   155                                        Multistate_Value_Properties_Optional);
       
   156     len += BACnet_Init_Properties_List(Multistate_Value_Properties_List + len,
       
   157                                        Multistate_Value_Properties_Proprietary);
       
   158 
       
   159     /* initialize all the analog output priority arrays to NULL */
       
   160     for (i = 0; i < MAX_MULTISTATE_VALUES; i++) {
       
   161         for (j = 0; j < BACNET_MAX_PRIORITY; j++) {
       
   162             MSV_Descr[i].Present_Value[j] = MSV_VALUE_NULL;
       
   163         }
       
   164         for (j = 0; j < MSV_Descr[i].Number_Of_States; j++) {
       
   165             sprintf(MSV_Descr[i].State_Text[j], "State %%d", j+1);
       
   166         }
       
   167     }
       
   168     return;
       
   169 }
       
   170 
       
   171 
       
   172 
       
   173 /* validate that the given instance exists */
       
   174 /* This is a callback function. Callback set in My_Object_Table[] array declared in device.c,  */
       
   175 bool Multistate_Value_Valid_Instance(
       
   176     uint32_t object_instance)
       
   177 {
       
   178     // fprintf(stderr, "BACnet plugin: Multistate_Value_Valid_Instance(obj_ID=%%u) called!\n", object _instance);
       
   179     return (Multistate_Value_Instance_To_Index(object_instance) < MAX_MULTISTATE_VALUES);
       
   180 }
       
   181 
       
   182 
       
   183 /* the number of Multistate Value Objects */
       
   184 /* This is a callback function. Callback set in My_Object_Table[] array declared in device.c,  */
       
   185 unsigned Multistate_Value_Count(void) {return MAX_MULTISTATE_VALUES;}
       
   186 
       
   187 
       
   188 /* returns the instance (i.e. Object Identifier) that correlates to the correct index */
       
   189 /* This is a callback function. Callback set in My_Object_Table[] array declared in device.c,  */
       
   190 uint32_t Multistate_Value_Index_To_Instance(unsigned index) {return MSV_Descr[index].Object_Identifier;}
       
   191 
       
   192 
       
   193 
       
   194 
       
   195 
       
   196 
       
   197 /* returns the index that correlates to the correct instance number (Object Identifier) */
       
   198 unsigned Multistate_Value_Instance_To_Index(
       
   199     uint32_t object_instance)
       
   200 {
       
   201     unsigned index = 0;
       
   202   
       
   203     for (index = 0; index < MAX_MULTISTATE_VALUES; index++)
       
   204         if (object_instance == MSV_Descr[index].Object_Identifier)
       
   205             return index;
       
   206 
       
   207     /* error, this object ID is not in our list! */
       
   208     return MAX_MULTISTATE_VALUES;
       
   209 }
       
   210 
       
   211 
       
   212 
       
   213 
       
   214 
       
   215 uint32_t Multistate_Value_Present_Value(
       
   216     uint32_t object_instance)
       
   217 {
       
   218     uint32_t value = MSV_VALUE_RELINQUISH_DEFAULT;
       
   219     unsigned index = 0; /* offset from instance lookup */
       
   220     unsigned i = 0;
       
   221 
       
   222     index = Multistate_Value_Instance_To_Index(object_instance);
       
   223     if (index < MAX_MULTISTATE_VALUES) {
       
   224         for (i = 0; i < BACNET_MAX_PRIORITY; i++) {
       
   225             if (!MSV_VALUE_IS_NULL(MSV_Descr[index].Present_Value[i])) {
       
   226                 value = MSV_Descr[index].Present_Value[i];
       
   227                 break;
       
   228             }
       
   229         }
       
   230     }
       
   231 
       
   232     return value;
       
   233 }
       
   234 
       
   235 
       
   236 
       
   237 bool Multistate_Value_Present_Value_Set(
       
   238     uint32_t object_instance,
       
   239     uint32_t value,
       
   240     uint8_t  priority)
       
   241 {
       
   242     unsigned index = 0; /* offset from instance lookup */
       
   243 
       
   244     index = Multistate_Value_Instance_To_Index(object_instance);
       
   245     if (index >= MAX_MULTISTATE_VALUES)
       
   246       return false;
       
   247 
       
   248     if ((priority == 0) || (priority > BACNET_MAX_PRIORITY) ||
       
   249         (priority == 6 /* reserved, ASHRAE 135-2016, section 19.2.2 */))
       
   250       return false;
       
   251     
       
   252     if ((value == 0) || (value > MSV_Descr[index].Number_Of_States))
       
   253       return false;
       
   254       
       
   255     priority--;
       
   256     MSV_Descr[index].Present_Value[priority] = (uint8_t) value;
       
   257 
       
   258     return true;
       
   259 }
       
   260 
       
   261 
       
   262 
       
   263 /* returns command priority (1..16), or 0 if all priority values are at NULL */
       
   264 int Multistate_Value_Current_Command_Priority(
       
   265     uint32_t object_instance)
       
   266 {
       
   267     unsigned index = 0;
       
   268     unsigned i = 0;
       
   269 
       
   270     index = Multistate_Value_Instance_To_Index(object_instance);
       
   271     if (index < MAX_MULTISTATE_VALUES) {
       
   272         for (i = 0; i < BACNET_MAX_PRIORITY; i++) {
       
   273             if (!MSV_VALUE_IS_NULL(MSV_Descr[index].Present_Value[i])) {
       
   274                 return i+1; // +1 since priority is 1..16, and not 0..15
       
   275             }
       
   276         }
       
   277     }
       
   278     // command values in all priorities are set to NULL
       
   279     return 0;
       
   280 }
       
   281 
       
   282 
       
   283 
       
   284 
       
   285 bool Multistate_Value_Present_Value_Relinquish(
       
   286     uint32_t object_instance,
       
   287     uint8_t  priority)
       
   288 {
       
   289     unsigned index = 0; /* offset from instance lookup */
       
   290 
       
   291     index = Multistate_Value_Instance_To_Index(object_instance);
       
   292     if (index >= MAX_MULTISTATE_VALUES)
       
   293       return false;
       
   294     
       
   295     if ((priority == 0) || (priority > BACNET_MAX_PRIORITY) ||
       
   296         (priority == 6 /* reserved, ASHRAE 135-2016, section 19.2.2 */))
       
   297       return false;
       
   298     
       
   299     priority--;
       
   300     MSV_Descr[index].Present_Value[priority] = MSV_VALUE_NULL;
       
   301 
       
   302     return true;
       
   303 }
       
   304 
       
   305 
       
   306 
       
   307 bool Multistate_Value_Out_Of_Service(
       
   308     uint32_t object_instance)
       
   309 {
       
   310     bool value = false;
       
   311     unsigned index = 0;
       
   312 
       
   313     index = Multistate_Value_Instance_To_Index(object_instance);
       
   314     if (index < MAX_MULTISTATE_VALUES) {
       
   315         value = MSV_Descr[index].Out_Of_Service;
       
   316     }
       
   317 
       
   318     return value;
       
   319 }
       
   320 
       
   321 
       
   322 
       
   323 void Multistate_Value_Out_Of_Service_Set(
       
   324     uint32_t object_instance,
       
   325     bool value)
       
   326 {
       
   327     unsigned index = 0;
       
   328 
       
   329     index = Multistate_Value_Instance_To_Index(object_instance);
       
   330     if (index < MAX_MULTISTATE_VALUES) {
       
   331         MSV_Descr[index].Out_Of_Service = value;
       
   332     }
       
   333 
       
   334     return;
       
   335 }
       
   336 
       
   337 
       
   338 
       
   339 static char *Multistate_Value_Description(
       
   340     uint32_t object_instance)
       
   341 {
       
   342     unsigned index = 0; /* offset from instance lookup */
       
   343     char *pName = NULL; /* return value */
       
   344 
       
   345     index = Multistate_Value_Instance_To_Index(object_instance);
       
   346     if (index < MAX_MULTISTATE_VALUES) {
       
   347         pName = MSV_Descr[index].Description;
       
   348     }
       
   349 
       
   350     return pName;
       
   351 }
       
   352 
       
   353 
       
   354 
       
   355 /* This is a callback function. Callback set in My_Object_Table[] array declared in device.c,  */
       
   356 bool Multistate_Value_Object_Name(
       
   357     uint32_t object_instance,
       
   358     BACNET_CHARACTER_STRING * object_name)
       
   359 {
       
   360     unsigned index = 0; /* offset from instance lookup */
       
   361     bool status = false;
       
   362 
       
   363     index = Multistate_Value_Instance_To_Index(object_instance);
       
   364     if (index < MAX_MULTISTATE_VALUES) {
       
   365         status = characterstring_init_ansi(object_name, MSV_Descr[index].Object_Name);
       
   366     }
       
   367 
       
   368     return status;
       
   369 }
       
   370 
       
   371 
       
   372 
       
   373 /* return apdu len, or BACNET_STATUS_ERROR on error */
       
   374 /* This is a callback function. Callback set in My_Object_Table[] array declared in device.c,  */
       
   375 int Multistate_Value_Read_Property(
       
   376     BACNET_READ_PROPERTY_DATA * rpdata)
       
   377 {
       
   378     int len = 0;
       
   379     int apdu_len = 0;   /* return value */
       
   380     BACNET_BIT_STRING bit_string;
       
   381     BACNET_CHARACTER_STRING char_string;
       
   382     uint32_t present_value = 0;
       
   383     unsigned object_index = 0;
       
   384     unsigned i = 0;
       
   385     bool state = false;
       
   386     uint8_t *apdu = NULL;
       
   387 
       
   388     if ((rpdata == NULL) || (rpdata->application_data == NULL) ||
       
   389         (rpdata->application_data_len == 0)) {
       
   390         return 0;
       
   391     }
       
   392 
       
   393     object_index = Multistate_Value_Instance_To_Index(rpdata->object_instance);
       
   394     if (object_index >= MAX_MULTISTATE_VALUES) {
       
   395         rpdata->error_class = ERROR_CLASS_OBJECT;
       
   396         rpdata->error_code  = ERROR_CODE_UNKNOWN_OBJECT;
       
   397         return BACNET_STATUS_ERROR;
       
   398     }
       
   399 
       
   400     apdu = rpdata->application_data;
       
   401     switch (rpdata->object_property) {
       
   402         case PROP_OBJECT_IDENTIFIER:
       
   403             apdu_len =
       
   404                 encode_application_object_id(&apdu[0],
       
   405                 OBJECT_MULTI_STATE_VALUE, rpdata->object_instance);
       
   406             break;
       
   407         case PROP_OBJECT_NAME:
       
   408             Multistate_Value_Object_Name(rpdata->object_instance,
       
   409                 &char_string);
       
   410             apdu_len =
       
   411                 encode_application_character_string(&apdu[0], &char_string);
       
   412             break;
       
   413         case PROP_DESCRIPTION:
       
   414             characterstring_init_ansi(&char_string,
       
   415                 Multistate_Value_Description(rpdata->object_instance));
       
   416             apdu_len =
       
   417                 encode_application_character_string(&apdu[0], &char_string);
       
   418             break;
       
   419         case PROP_OBJECT_TYPE:
       
   420             apdu_len =
       
   421                 encode_application_enumerated(&apdu[0],
       
   422                 OBJECT_MULTI_STATE_VALUE);
       
   423             break;
       
   424         case PROP_PRESENT_VALUE:
       
   425             present_value =
       
   426                 Multistate_Value_Present_Value(rpdata->object_instance);
       
   427             apdu_len = encode_application_unsigned(&apdu[0], present_value);
       
   428             break;
       
   429         case PROP_STATUS_FLAGS:
       
   430             /* note: see the details in the standard on how to use these */
       
   431             bitstring_init(&bit_string);
       
   432             bitstring_set_bit(&bit_string, STATUS_FLAG_IN_ALARM, false);
       
   433             bitstring_set_bit(&bit_string, STATUS_FLAG_FAULT, false);
       
   434             bitstring_set_bit(&bit_string, STATUS_FLAG_OVERRIDDEN, false);
       
   435             if (Multistate_Value_Out_Of_Service(rpdata->object_instance)) {
       
   436                 bitstring_set_bit(&bit_string, STATUS_FLAG_OUT_OF_SERVICE,
       
   437                     true);
       
   438             } else {
       
   439                 bitstring_set_bit(&bit_string, STATUS_FLAG_OUT_OF_SERVICE,
       
   440                     false);
       
   441             }
       
   442             apdu_len = encode_application_bitstring(&apdu[0], &bit_string);
       
   443             break;
       
   444         case PROP_EVENT_STATE:
       
   445             /* note: see the details in the standard on how to use this */
       
   446             apdu_len =
       
   447                 encode_application_enumerated(&apdu[0], EVENT_STATE_NORMAL);
       
   448             break;
       
   449         case PROP_OUT_OF_SERVICE:
       
   450             state = MSV_Descr[object_index].Out_Of_Service;
       
   451             apdu_len = encode_application_boolean(&apdu[0], state);
       
   452             break;
       
   453         case PROP_PRIORITY_ARRAY:
       
   454             BACnet_encode_array(MSV_Descr[object_index].Present_Value,
       
   455                                 BACNET_MAX_PRIORITY,
       
   456                                 MSV_VALUE_IS_NULL,
       
   457                                 encode_application_unsigned);
       
   458             break;
       
   459 //      case PROP_CURRENT_COMMAND_PRIORITY: {
       
   460 //          unsigned i = Multistate_Value_Current_Command_Priority(rpdata->object_instance);
       
   461 //          if (i == 0)  apdu_len = encode_application_null    (&apdu[0]);
       
   462 //          else         apdu_len = encode_application_unsigned(&apdu[0], i);
       
   463 //          break;
       
   464 //      }
       
   465         case PROP_RELINQUISH_DEFAULT:
       
   466             present_value = MSV_VALUE_RELINQUISH_DEFAULT;
       
   467             apdu_len = encode_application_unsigned(&apdu[0], present_value);
       
   468             break;
       
   469         case PROP_NUMBER_OF_STATES:
       
   470             apdu_len =
       
   471                 encode_application_unsigned(&apdu[apdu_len],
       
   472                 MSV_Descr[object_index].Number_Of_States);
       
   473             break;
       
   474         case PROP_STATE_TEXT:
       
   475             BACnet_encode_array(MSV_Descr[object_index].State_Text,
       
   476                                 MSV_Descr[object_index].Number_Of_States,
       
   477                                 retfalse, BACnet_encode_character_string);
       
   478             break;
       
   479 //      case PROP_PROPERTY_LIST:
       
   480 //          BACnet_encode_array(Multistate_Value_Properties_List,
       
   481 //                              property_list_count(Multistate_Value_Properties_List),
       
   482 //                              retfalse, encode_application_enumerated);
       
   483 //          break;
       
   484         default:
       
   485             rpdata->error_class = ERROR_CLASS_PROPERTY;
       
   486             rpdata->error_code = ERROR_CODE_UNKNOWN_PROPERTY;
       
   487             apdu_len = BACNET_STATUS_ERROR;
       
   488             break;
       
   489     }
       
   490     /*  only array properties can have array options */
       
   491     if ((apdu_len >= 0) && (rpdata->object_property != PROP_STATE_TEXT) &&
       
   492         (rpdata->object_property != PROP_PRIORITY_ARRAY) &&
       
   493 //      (rpdata->object_property != PROP_PROPERTY_LIST) &&
       
   494         (rpdata->array_index != BACNET_ARRAY_ALL)) {
       
   495         rpdata->error_class = ERROR_CLASS_PROPERTY;
       
   496         rpdata->error_code = ERROR_CODE_PROPERTY_IS_NOT_AN_ARRAY;
       
   497         apdu_len = BACNET_STATUS_ERROR;
       
   498     }
       
   499 
       
   500     return apdu_len;
       
   501 }
       
   502 
       
   503 /* returns true if successful */
       
   504 /* This is a callback function. Callback set in My_Object_Table[] array declared in device.c,  */
       
   505 bool Multistate_Value_Write_Property(
       
   506     BACNET_WRITE_PROPERTY_DATA * wp_data)
       
   507 {
       
   508     bool status = false;        /* return value */
       
   509     int len = 0;
       
   510     unsigned object_index = 0;
       
   511     BACNET_APPLICATION_DATA_VALUE value;
       
   512 
       
   513     /* decode the some of the request */
       
   514     len =
       
   515         bacapp_decode_application_data(wp_data->application_data,
       
   516         wp_data->application_data_len, &value);
       
   517     /* FIXME: len < application_data_len: more data? */
       
   518     if (len < 0) {
       
   519         /* error while decoding - a value larger than we can handle */
       
   520         wp_data->error_class = ERROR_CLASS_PROPERTY;
       
   521         wp_data->error_code = ERROR_CODE_VALUE_OUT_OF_RANGE;
       
   522         return false;
       
   523     }
       
   524     if ((wp_data->object_property != PROP_STATE_TEXT) &&
       
   525         (wp_data->object_property != PROP_PRIORITY_ARRAY) &&
       
   526         (wp_data->array_index != BACNET_ARRAY_ALL)) {
       
   527         /*  only array properties can have array options */
       
   528         wp_data->error_class = ERROR_CLASS_PROPERTY;
       
   529         wp_data->error_code = ERROR_CODE_PROPERTY_IS_NOT_AN_ARRAY;
       
   530         return false;
       
   531     }
       
   532     /* No need to check whether object_index is within bounds.
       
   533      * Has already been checked before Multistate_Output_Write_Property() is called
       
   534      */
       
   535     object_index = Multistate_Value_Instance_To_Index(wp_data->object_instance);
       
   536 
       
   537     switch (wp_data->object_property) {
       
   538         case PROP_PRESENT_VALUE:
       
   539             status =
       
   540                 WPValidateArgType(&value, BACNET_APPLICATION_TAG_UNSIGNED_INT,
       
   541                 &wp_data->error_class, &wp_data->error_code);
       
   542             if (status) {
       
   543                 status =
       
   544                     Multistate_Value_Present_Value_Set
       
   545                     (wp_data->object_instance, value.type.Unsigned_Int, wp_data->priority);
       
   546                 if (!status) {
       
   547                     if (wp_data->priority == 6) {
       
   548                         /* Command priority 6 is reserved for use by Minimum On/Off
       
   549                            algorithm and may not be used for other purposes in any
       
   550                            object. */
       
   551                         wp_data->error_class = ERROR_CLASS_PROPERTY;
       
   552                         wp_data->error_code = ERROR_CODE_WRITE_ACCESS_DENIED;
       
   553                     } else {
       
   554                         wp_data->error_class = ERROR_CLASS_PROPERTY;
       
   555                         wp_data->error_code = ERROR_CODE_VALUE_OUT_OF_RANGE;
       
   556                     }
       
   557                 }
       
   558             } else {
       
   559                 status =
       
   560                     WPValidateArgType(&value, BACNET_APPLICATION_TAG_NULL,
       
   561                     &wp_data->error_class, &wp_data->error_code);
       
   562                 if (status) {
       
   563                     status =
       
   564                         Multistate_Value_Present_Value_Relinquish
       
   565                         (wp_data->object_instance, wp_data->priority);
       
   566                 }
       
   567                 if (!status) {
       
   568                     wp_data->error_class = ERROR_CLASS_PROPERTY;
       
   569                     wp_data->error_code = ERROR_CODE_INVALID_DATA_TYPE;
       
   570                 }
       
   571             }
       
   572             break;
       
   573         case PROP_OUT_OF_SERVICE:
       
   574         {
       
   575             bool Previous_Out_Of_Service = MSV_Descr[object_index].Out_Of_Service;
       
   576             status =
       
   577                 WPValidateArgType(&value, BACNET_APPLICATION_TAG_BOOLEAN,
       
   578                 &wp_data->error_class, &wp_data->error_code);
       
   579             if (status) {
       
   580                 MSV_Descr[object_index].Out_Of_Service = value.type.Boolean;
       
   581                 if (Previous_Out_Of_Service && !MSV_Descr[object_index].Out_Of_Service)
       
   582                     /* We have just changed from Out_of_Service -> In Service */
       
   583                     /* We need to update the Present_Value to the value
       
   584                      * currently in the PLC...
       
   585                      */
       
   586                     MSV_Descr[object_index].Present_Value[BACNET_MAX_PRIORITY-1] =
       
   587                                            *(MSV_Descr[object_index].Located_Var_ptr);
       
   588             }
       
   589             break;
       
   590         }
       
   591         case PROP_OBJECT_IDENTIFIER:
       
   592         case PROP_OBJECT_NAME:
       
   593         case PROP_OBJECT_TYPE:
       
   594         case PROP_STATUS_FLAGS:
       
   595         case PROP_EVENT_STATE:
       
   596         case PROP_NUMBER_OF_STATES:
       
   597         case PROP_PRIORITY_ARRAY:
       
   598 //      case PROP_CURRENT_COMMAND_PRIORITY:
       
   599         case PROP_RELINQUISH_DEFAULT:
       
   600         case PROP_DESCRIPTION:
       
   601         case PROP_STATE_TEXT:
       
   602 //      case PROP_PROPERTY_LIST:
       
   603             wp_data->error_class = ERROR_CLASS_PROPERTY;
       
   604             wp_data->error_code = ERROR_CODE_WRITE_ACCESS_DENIED;
       
   605             break;
       
   606         default:
       
   607             wp_data->error_class = ERROR_CLASS_PROPERTY;
       
   608             wp_data->error_code = ERROR_CODE_UNKNOWN_PROPERTY;
       
   609             break;
       
   610     }
       
   611 
       
   612     return status;
       
   613 }
       
   614 
       
   615 
       
   616 
       
   617 
       
   618 
       
   619 /********************************************/
       
   620 /** Functions required for Beremiz plugin  **/
       
   621 /********************************************/
       
   622 
       
   623 
       
   624 
       
   625 void  Multistate_Value_Copy_Present_Value_to_Located_Var(void) {
       
   626     unsigned i;
       
   627     for (i = 0; i < MAX_MULTISTATE_VALUES; i++) {
       
   628         // decouple PLC's Located Variable from Bacnet Object's Present Value if Out_Of_Service is true
       
   629         if (MSV_Descr[i].Out_Of_Service)
       
   630             continue;
       
   631 
       
   632         // copy the value
       
   633         *(MSV_Descr[i].Located_Var_ptr) = Multistate_Value_Present_Value(MSV_Descr[i].Object_Identifier);
       
   634     }
       
   635 }
       
   636 
       
   637 
       
   638 
       
   639 void  Multistate_Value_Copy_Located_Var_to_Present_Value(void) {
       
   640     unsigned i;
       
   641     for (i = 0; i < MAX_MULTISTATE_VALUES; i++) {
       
   642         // decouple PLC's Located Variable from Bacnet Object's Present Value if Out_Of_Service is true
       
   643         if (MSV_Descr[i].Out_Of_Service)
       
   644             continue;
       
   645 
       
   646         // copy the value
       
   647         // Make sure local device does not set Present_Value to a value higher than Number_Of_States
       
   648         /* NOTE: The following comparison (Present_Value > Number_Of_States) is OK as the
       
   649          *       MSV_VALUE_NULL is currently set to 0. This means that the local controller
       
   650          *       can safely relinquish control by setting the Present_Value to 0.
       
   651          *       The comparison would not be safe if for some reason we later change
       
   652          *       MSV_VALUE_NULL to a value that may be higher than Number_Of_States.
       
   653          */
       
   654         #if MSV_VALUE_NULL != 0
       
   655         #error Implementation assumes that MSV_VALUE_NULL is set to 0, which is currently not the case.
       
   656         #endif
       
   657         if (*(MSV_Descr[i].Located_Var_ptr) > MSV_Descr[i].Number_Of_States)
       
   658             continue;
       
   659 
       
   660         // Everything seems to be OK. Copy the value
       
   661         MSV_Descr[i].Present_Value[BACNET_MAX_PRIORITY-1] = *(MSV_Descr[i].Located_Var_ptr);
       
   662     }
       
   663 }
       
   664 
       
   665 
       
   666