bacnet/runtime/msi.c
changeset 2020 6dddf3070806
equal deleted inserted replaced
2019:92f02bb17c7e 2020:6dddf3070806
       
     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 Input 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 "msi_%(locstr)s.h"
       
    41 #include "handlers.h"
       
    42 
       
    43 
       
    44 /* initial value for present_value property of each object */ 
       
    45 #define MSI_VALUE_INIT (1)
       
    46 
       
    47 /* The IEC 61131-3 located variables mapped onto the Multi-state Input objects of BACnet protocol */
       
    48 %(MSI_lvars)s
       
    49 
       
    50 
       
    51 /* The array where we keep all the state related to the Multi-state Input Objects */
       
    52 #define MAX_MULTISTATE_INPUTS %(MSI_count)s
       
    53 static MULTISTATE_INPUT_DESCR MSI_Descr[MAX_MULTISTATE_INPUTS] = {
       
    54 %(MSI_param)s
       
    55 };
       
    56 
       
    57 
       
    58 
       
    59 /* These three arrays are used by the ReadPropertyMultiple handler,
       
    60  * as well as to initialize the XXX_Property_List used by the 
       
    61  * Property List (PROP_PROPERTY_LIST) property.
       
    62  */
       
    63 static const int Multistate_Input_Properties_Required[] = {
       
    64  /* (1) Currently Supported                  */
       
    65  /* (2) Required by standard ASHRAE 135-2016 */
       
    66                               /*(1)(2)      */
       
    67     PROP_OBJECT_IDENTIFIER,   /* R  R ( 75) */
       
    68     PROP_OBJECT_NAME,         /* R  R ( 77) */
       
    69     PROP_OBJECT_TYPE,         /* R  R ( 79) */
       
    70     PROP_PRESENT_VALUE,       /* W  R ( 85) */
       
    71     PROP_STATUS_FLAGS,        /* R  R (111) */
       
    72     PROP_EVENT_STATE,         /* R  R ( 36) */
       
    73     PROP_OUT_OF_SERVICE,      /* W  R ( 81) */
       
    74     PROP_NUMBER_OF_STATES,    /* R  R ( 74) */
       
    75 //  PROP_PROPERTY_LIST,       /* R  R (371) */
       
    76     -1
       
    77 };
       
    78 
       
    79 static const int Multistate_Input_Properties_Optional[] = {
       
    80  /* (1) Currently Supported                  */
       
    81  /* (2) Required by standard ASHRAE 135-2016 */
       
    82                               /*(1)(2)      */
       
    83     PROP_DESCRIPTION,         /* R  O ( 28) */
       
    84     PROP_STATE_TEXT,          /* R  O (110) */
       
    85     -1
       
    86 };
       
    87 
       
    88 static const int Multistate_Input_Properties_Proprietary[] = {
       
    89     -1
       
    90 };
       
    91 
       
    92 
       
    93 /* This array stores the PROPERTY_LIST which may be read by clients.
       
    94  * End of list is marked by following the last element with the value '-1'
       
    95  * 
       
    96  * It is initialized by Multistate_Input_Init() based off the values
       
    97  * stored in Multistate_Input_Properties_Required 
       
    98  *           Multistate_Input_Properties_Optional
       
    99  *           Multistate_Input_Properties_Proprietary
       
   100  */
       
   101 /* TODO: Allocate memory for this array with malloc() at startup */
       
   102 static int Multistate_Input_Properties_List[64];
       
   103 
       
   104 
       
   105 
       
   106 
       
   107 
       
   108 /********************************************************/
       
   109 /**                  Callback functions.               **/
       
   110 /** Functions required by BACnet devie implementation. **/
       
   111 /********************************************************/
       
   112 
       
   113 
       
   114 /* This is a callback function. Callback set in My_Object_Table[] array declared in device.c,  */
       
   115 void Multistate_Input_Property_Lists(
       
   116     const int **pRequired,
       
   117     const int **pOptional,
       
   118     const int **pProprietary)
       
   119 {
       
   120     if (pRequired)
       
   121         *pRequired = Multistate_Input_Properties_Required;
       
   122     if (pOptional)
       
   123         *pOptional = Multistate_Input_Properties_Optional;
       
   124     if (pProprietary)
       
   125         *pProprietary = Multistate_Input_Properties_Proprietary;
       
   126 
       
   127     return;
       
   128 }
       
   129 
       
   130 
       
   131 
       
   132 /* This is a callback function. Callback set in My_Object_Table[] array declared in device.c,  */
       
   133 void Multistate_Input_Init(
       
   134     void)
       
   135 {
       
   136     unsigned int i, j;
       
   137 
       
   138     /* initialize the Multistate_Input_Properties_List array */
       
   139     int len = 0;
       
   140     len += BACnet_Init_Properties_List(Multistate_Input_Properties_List + len,
       
   141                                        Multistate_Input_Properties_Required);
       
   142     len += BACnet_Init_Properties_List(Multistate_Input_Properties_List + len,
       
   143                                        Multistate_Input_Properties_Optional);
       
   144     len += BACnet_Init_Properties_List(Multistate_Input_Properties_List + len,
       
   145                                        Multistate_Input_Properties_Proprietary);
       
   146 
       
   147     /* initialize all the analog output priority arrays to NULL */
       
   148     for (i = 0; i < MAX_MULTISTATE_INPUTS; i++) {
       
   149         MSI_Descr[i].Present_Value = MSI_VALUE_INIT;
       
   150         for (j = 0; j < MSI_Descr[i].Number_Of_States; j++) {
       
   151             sprintf(MSI_Descr[i].State_Text[j], "State %%d", j+1);
       
   152         }
       
   153     }
       
   154     return;
       
   155 }
       
   156 
       
   157 
       
   158 
       
   159 /* validate that the given instance exists */
       
   160 /* This is a callback function. Callback set in My_Object_Table[] array declared in device.c,  */
       
   161 bool Multistate_Input_Valid_Instance(
       
   162     uint32_t object_instance)
       
   163 {
       
   164     // fprintf(stderr, "BACnet plugin: Multistate_Input_Valid_Instance(obj_ID=%%u) called!\n", object _instance);
       
   165     return (Multistate_Input_Instance_To_Index(object_instance) < MAX_MULTISTATE_INPUTS);
       
   166 }
       
   167 
       
   168 
       
   169 /* the number of Multistate Value Objects */
       
   170 /* This is a callback function. Callback set in My_Object_Table[] array declared in device.c,  */
       
   171 unsigned Multistate_Input_Count(void) {return MAX_MULTISTATE_INPUTS;}
       
   172 
       
   173 
       
   174 /* returns the instance (i.e. Object Identifier) that correlates to the correct index */
       
   175 /* This is a callback function. Callback set in My_Object_Table[] array declared in device.c,  */
       
   176 uint32_t Multistate_Input_Index_To_Instance(unsigned index) {return MSI_Descr[index].Object_Identifier;}
       
   177 
       
   178 
       
   179 
       
   180 
       
   181 
       
   182 
       
   183 /* returns the index that correlates to the correct instance number (Object Identifier) */
       
   184 unsigned Multistate_Input_Instance_To_Index(
       
   185     uint32_t object_instance)
       
   186 {
       
   187     unsigned index = 0;
       
   188   
       
   189     for (index = 0; index < MAX_MULTISTATE_INPUTS; index++)
       
   190         if (object_instance == MSI_Descr[index].Object_Identifier)
       
   191             return index;
       
   192 
       
   193     /* error, this object ID is not in our list! */
       
   194     return MAX_MULTISTATE_INPUTS;
       
   195 }
       
   196 
       
   197 
       
   198 
       
   199 
       
   200 
       
   201 uint32_t Multistate_Input_Present_Value(
       
   202     uint32_t object_instance)
       
   203 {
       
   204     uint32_t value = MSI_VALUE_INIT;
       
   205     unsigned index = 0; /* offset from instance lookup */
       
   206 
       
   207     index = Multistate_Input_Instance_To_Index(object_instance);
       
   208     if (index < MAX_MULTISTATE_INPUTS)
       
   209         value = MSI_Descr[index].Present_Value;
       
   210 
       
   211     return value;
       
   212 }
       
   213 
       
   214 
       
   215 
       
   216 bool Multistate_Input_Present_Value_Set(
       
   217     uint32_t object_instance,
       
   218     uint32_t value)
       
   219 {
       
   220     unsigned index = 0; /* offset from instance lookup */
       
   221 
       
   222     index = Multistate_Input_Instance_To_Index(object_instance);
       
   223     if (index >= MAX_MULTISTATE_INPUTS)
       
   224       return false;
       
   225 
       
   226     if ((value == 0) || (value > MSI_Descr[index].Number_Of_States))
       
   227       return false;
       
   228       
       
   229     MSI_Descr[index].Present_Value = (uint8_t) value;
       
   230     return true;
       
   231 }
       
   232 
       
   233 
       
   234 
       
   235 
       
   236 bool Multistate_Input_Out_Of_Service(
       
   237     uint32_t object_instance)
       
   238 {
       
   239     bool value = false;
       
   240     unsigned index = 0;
       
   241 
       
   242     index = Multistate_Input_Instance_To_Index(object_instance);
       
   243     if (index < MAX_MULTISTATE_INPUTS) {
       
   244         value = MSI_Descr[index].Out_Of_Service;
       
   245     }
       
   246 
       
   247     return value;
       
   248 }
       
   249 
       
   250 
       
   251 
       
   252 void Multistate_Input_Out_Of_Service_Set(
       
   253     uint32_t object_instance,
       
   254     bool value)
       
   255 {
       
   256     unsigned index = 0;
       
   257 
       
   258     index = Multistate_Input_Instance_To_Index(object_instance);
       
   259     if (index < MAX_MULTISTATE_INPUTS) {
       
   260         MSI_Descr[index].Out_Of_Service = value;
       
   261     }
       
   262 
       
   263     return;
       
   264 }
       
   265 
       
   266 
       
   267 
       
   268 static char *Multistate_Input_Description(
       
   269     uint32_t object_instance)
       
   270 {
       
   271     unsigned index = 0; /* offset from instance lookup */
       
   272     char *pName = NULL; /* return value */
       
   273 
       
   274     index = Multistate_Input_Instance_To_Index(object_instance);
       
   275     if (index < MAX_MULTISTATE_INPUTS) {
       
   276         pName = MSI_Descr[index].Description;
       
   277     }
       
   278 
       
   279     return pName;
       
   280 }
       
   281 
       
   282 
       
   283 
       
   284 /* This is a callback function. Callback set in My_Object_Table[] array declared in device.c,  */
       
   285 bool Multistate_Input_Object_Name(
       
   286     uint32_t object_instance,
       
   287     BACNET_CHARACTER_STRING * object_name)
       
   288 {
       
   289     unsigned index = 0; /* offset from instance lookup */
       
   290     bool status = false;
       
   291 
       
   292     index = Multistate_Input_Instance_To_Index(object_instance);
       
   293     if (index < MAX_MULTISTATE_INPUTS) {
       
   294         status = characterstring_init_ansi(object_name, MSI_Descr[index].Object_Name);
       
   295     }
       
   296 
       
   297     return status;
       
   298 }
       
   299 
       
   300 
       
   301 
       
   302 /* return apdu len, or BACNET_STATUS_ERROR on error */
       
   303 /* This is a callback function. Callback set in My_Object_Table[] array declared in device.c,  */
       
   304 int Multistate_Input_Read_Property(
       
   305     BACNET_READ_PROPERTY_DATA * rpdata)
       
   306 {
       
   307     int len = 0;
       
   308     int apdu_len = 0;   /* return value */
       
   309     BACNET_BIT_STRING bit_string;
       
   310     BACNET_CHARACTER_STRING char_string;
       
   311     uint32_t present_value = 0;
       
   312     unsigned object_index = 0;
       
   313     unsigned i = 0;
       
   314     bool state = false;
       
   315     uint8_t *apdu = NULL;
       
   316 
       
   317     if ((rpdata == NULL) || (rpdata->application_data == NULL) ||
       
   318         (rpdata->application_data_len == 0)) {
       
   319         return 0;
       
   320     }
       
   321 
       
   322     object_index = Multistate_Input_Instance_To_Index(rpdata->object_instance);
       
   323     if (object_index >= MAX_MULTISTATE_INPUTS) {
       
   324         rpdata->error_class = ERROR_CLASS_OBJECT;
       
   325         rpdata->error_code  = ERROR_CODE_UNKNOWN_OBJECT;
       
   326         return BACNET_STATUS_ERROR;
       
   327     }
       
   328 
       
   329     apdu = rpdata->application_data;
       
   330     switch (rpdata->object_property) {
       
   331         case PROP_OBJECT_IDENTIFIER:
       
   332             apdu_len =
       
   333                 encode_application_object_id(&apdu[0],
       
   334                 OBJECT_MULTI_STATE_INPUT, rpdata->object_instance);
       
   335             break;
       
   336         case PROP_OBJECT_NAME:
       
   337             Multistate_Input_Object_Name(rpdata->object_instance,
       
   338                 &char_string);
       
   339             apdu_len =
       
   340                 encode_application_character_string(&apdu[0], &char_string);
       
   341             break;
       
   342         case PROP_DESCRIPTION:
       
   343             characterstring_init_ansi(&char_string,
       
   344                 Multistate_Input_Description(rpdata->object_instance));
       
   345             apdu_len =
       
   346                 encode_application_character_string(&apdu[0], &char_string);
       
   347             break;
       
   348         case PROP_OBJECT_TYPE:
       
   349             apdu_len =
       
   350                 encode_application_enumerated(&apdu[0],
       
   351                 OBJECT_MULTI_STATE_INPUT);
       
   352             break;
       
   353         case PROP_PRESENT_VALUE:
       
   354             present_value =
       
   355                 Multistate_Input_Present_Value(rpdata->object_instance);
       
   356             apdu_len = encode_application_unsigned(&apdu[0], present_value);
       
   357             break;
       
   358         case PROP_STATUS_FLAGS:
       
   359             /* note: see the details in the standard on how to use these */
       
   360             bitstring_init(&bit_string);
       
   361             bitstring_set_bit(&bit_string, STATUS_FLAG_IN_ALARM, false);
       
   362             bitstring_set_bit(&bit_string, STATUS_FLAG_FAULT, false);
       
   363             bitstring_set_bit(&bit_string, STATUS_FLAG_OVERRIDDEN, false);
       
   364             if (Multistate_Input_Out_Of_Service(rpdata->object_instance)) {
       
   365                 bitstring_set_bit(&bit_string, STATUS_FLAG_OUT_OF_SERVICE,
       
   366                     true);
       
   367             } else {
       
   368                 bitstring_set_bit(&bit_string, STATUS_FLAG_OUT_OF_SERVICE,
       
   369                     false);
       
   370             }
       
   371             apdu_len = encode_application_bitstring(&apdu[0], &bit_string);
       
   372             break;
       
   373         case PROP_EVENT_STATE:
       
   374             /* note: see the details in the standard on how to use this */
       
   375             apdu_len =
       
   376                 encode_application_enumerated(&apdu[0], EVENT_STATE_NORMAL);
       
   377             break;
       
   378         case PROP_OUT_OF_SERVICE:
       
   379             state = MSI_Descr[object_index].Out_Of_Service;
       
   380             apdu_len = encode_application_boolean(&apdu[0], state);
       
   381             break;
       
   382         case PROP_NUMBER_OF_STATES:
       
   383             apdu_len =
       
   384                 encode_application_unsigned(&apdu[apdu_len],
       
   385                 MSI_Descr[object_index].Number_Of_States);
       
   386             break;
       
   387         case PROP_STATE_TEXT:
       
   388             BACnet_encode_array(MSI_Descr[object_index].State_Text,
       
   389                                 MSI_Descr[object_index].Number_Of_States,
       
   390                                 retfalse, BACnet_encode_character_string);
       
   391             break;
       
   392 //      case PROP_PROPERTY_LIST:
       
   393 //          BACnet_encode_array(Multistate_Input_Properties_List,
       
   394 //                              property_list_count(Multistate_Input_Properties_List),
       
   395 //                              retfalse, encode_application_enumerated);
       
   396 //          break;
       
   397         default:
       
   398             rpdata->error_class = ERROR_CLASS_PROPERTY;
       
   399             rpdata->error_code = ERROR_CODE_UNKNOWN_PROPERTY;
       
   400             apdu_len = BACNET_STATUS_ERROR;
       
   401             break;
       
   402     }
       
   403     /*  only array properties can have array options */
       
   404     if ((apdu_len >= 0) && (rpdata->object_property != PROP_STATE_TEXT) &&
       
   405 //      (rpdata->object_property != PROP_PROPERTY_LIST) &&
       
   406         (rpdata->array_index != BACNET_ARRAY_ALL)) {
       
   407         rpdata->error_class = ERROR_CLASS_PROPERTY;
       
   408         rpdata->error_code = ERROR_CODE_PROPERTY_IS_NOT_AN_ARRAY;
       
   409         apdu_len = BACNET_STATUS_ERROR;
       
   410     }
       
   411 
       
   412     return apdu_len;
       
   413 }
       
   414 
       
   415 /* returns true if successful */
       
   416 /* This is a callback function. Callback set in My_Object_Table[] array declared in device.c,  */
       
   417 bool Multistate_Input_Write_Property(
       
   418     BACNET_WRITE_PROPERTY_DATA * wp_data)
       
   419 {
       
   420     bool status = false;        /* return value */
       
   421     int len = 0;
       
   422     unsigned object_index = 0;
       
   423     BACNET_APPLICATION_DATA_VALUE value;
       
   424 
       
   425     /* decode the some of the request */
       
   426     len =
       
   427         bacapp_decode_application_data(wp_data->application_data,
       
   428         wp_data->application_data_len, &value);
       
   429     /* FIXME: len < application_data_len: more data? */
       
   430     if (len < 0) {
       
   431         /* error while decoding - a value larger than we can handle */
       
   432         wp_data->error_class = ERROR_CLASS_PROPERTY;
       
   433         wp_data->error_code = ERROR_CODE_VALUE_OUT_OF_RANGE;
       
   434         return false;
       
   435     }
       
   436     if ((wp_data->object_property != PROP_STATE_TEXT) &&
       
   437         (wp_data->array_index != BACNET_ARRAY_ALL)) {
       
   438         /*  only array properties can have array options */
       
   439         wp_data->error_class = ERROR_CLASS_PROPERTY;
       
   440         wp_data->error_code = ERROR_CODE_PROPERTY_IS_NOT_AN_ARRAY;
       
   441         return false;
       
   442     }
       
   443     /* No need to check whether object_index is within bounds.
       
   444      * Has already been checked before Multistate_Input_Write_Property() is called
       
   445      */
       
   446     object_index = Multistate_Input_Instance_To_Index(wp_data->object_instance);
       
   447 
       
   448     switch (wp_data->object_property) {
       
   449         case PROP_PRESENT_VALUE:
       
   450             status =
       
   451                 WPValidateArgType(&value, BACNET_APPLICATION_TAG_UNSIGNED_INT,
       
   452                 &wp_data->error_class, &wp_data->error_code);
       
   453             if (!status) {
       
   454                 wp_data->error_class = ERROR_CLASS_PROPERTY;
       
   455                 wp_data->error_code  = ERROR_CODE_INVALID_DATA_TYPE;
       
   456                 status = false; // not really necessary here.
       
   457             } else {
       
   458                 if (!MSI_Descr[object_index].Out_Of_Service) {
       
   459                     /* input objects can only be written to when Out_Of_Service is true! */
       
   460                     wp_data->error_class = ERROR_CLASS_PROPERTY;
       
   461                     wp_data->error_code  = ERROR_CODE_WRITE_ACCESS_DENIED;
       
   462                     status = false;
       
   463                 } else {
       
   464                     status =
       
   465                         Multistate_Input_Present_Value_Set
       
   466                         (wp_data->object_instance, value.type.Unsigned_Int);
       
   467                     if (!status) {
       
   468                         wp_data->error_class = ERROR_CLASS_PROPERTY;
       
   469                         wp_data->error_code  = ERROR_CODE_VALUE_OUT_OF_RANGE;
       
   470                         status = false; // not really necessary here.
       
   471                     }
       
   472                 }
       
   473             }
       
   474             break;
       
   475         case PROP_OUT_OF_SERVICE:
       
   476         {
       
   477             bool Previous_Out_Of_Service = MSI_Descr[object_index].Out_Of_Service;
       
   478             status =
       
   479                 WPValidateArgType(&value, BACNET_APPLICATION_TAG_BOOLEAN,
       
   480                 &wp_data->error_class, &wp_data->error_code);
       
   481             if (status) {
       
   482                 MSI_Descr[object_index].Out_Of_Service = value.type.Boolean;
       
   483                 if (Previous_Out_Of_Service && !MSI_Descr[object_index].Out_Of_Service)
       
   484                     /* We have just changed from Out_of_Service -> In Service */
       
   485                     /* We need to update the Present_Value to the value
       
   486                      * currently in the PLC...
       
   487                      */
       
   488                     MSI_Descr[object_index].Present_Value =
       
   489                                            *(MSI_Descr[object_index].Located_Var_ptr);
       
   490             }
       
   491             break;
       
   492         }
       
   493         case PROP_OBJECT_IDENTIFIER:
       
   494         case PROP_OBJECT_NAME:
       
   495         case PROP_OBJECT_TYPE:
       
   496         case PROP_STATUS_FLAGS:
       
   497         case PROP_EVENT_STATE:
       
   498         case PROP_NUMBER_OF_STATES:
       
   499         case PROP_DESCRIPTION:
       
   500         case PROP_STATE_TEXT:
       
   501 //      case PROP_PROPERTY_LIST:
       
   502             wp_data->error_class = ERROR_CLASS_PROPERTY;
       
   503             wp_data->error_code = ERROR_CODE_WRITE_ACCESS_DENIED;
       
   504             break;
       
   505         default:
       
   506             wp_data->error_class = ERROR_CLASS_PROPERTY;
       
   507             wp_data->error_code = ERROR_CODE_UNKNOWN_PROPERTY;
       
   508             break;
       
   509     }
       
   510 
       
   511     return status;
       
   512 }
       
   513 
       
   514 
       
   515 
       
   516 
       
   517 
       
   518 /********************************************/
       
   519 /** Functions required for Beremiz plugin  **/
       
   520 /********************************************/
       
   521 
       
   522 
       
   523 
       
   524 void  Multistate_Input_Copy_Present_Value_to_Located_Var(void) {
       
   525     unsigned i;
       
   526     for (i = 0; i < MAX_MULTISTATE_INPUTS; i++) {
       
   527         // decouple PLC's Located Variable from Bacnet Object's Present Value if Out_Of_Service is true
       
   528         if (MSI_Descr[i].Out_Of_Service)
       
   529             continue;
       
   530 
       
   531         // copy the value
       
   532         *(MSI_Descr[i].Located_Var_ptr) = Multistate_Input_Present_Value(MSI_Descr[i].Object_Identifier);
       
   533     }
       
   534 }
       
   535 
       
   536 
       
   537 
       
   538 void  Multistate_Input_Copy_Located_Var_to_Present_Value(void) {
       
   539     unsigned i;
       
   540     for (i = 0; i < MAX_MULTISTATE_INPUTS; i++) {
       
   541         // decouple PLC's Located Variable from Bacnet Object's Present Value if Out_Of_Service is true
       
   542         if (MSI_Descr[i].Out_Of_Service)
       
   543             continue;
       
   544 
       
   545         // Make sure local device does not set Present_Value to 0, nor higher than Number_Of_States
       
   546         if ((*(MSI_Descr[i].Located_Var_ptr) >  MSI_Descr[i].Number_Of_States) ||
       
   547             (*(MSI_Descr[i].Located_Var_ptr) == 0))
       
   548             continue;
       
   549 
       
   550         // Everything seems to be OK. Copy the value
       
   551         MSI_Descr[i].Present_Value = *(MSI_Descr[i].Located_Var_ptr);
       
   552     }
       
   553 }
       
   554 
       
   555 
       
   556