bacnet/runtime/mso.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 Output 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 "mso_%(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 MSO_VALUE_NULL (0)
       
    51 #define MSO_VALUE_IS_NULL(x)  ((x) == MSO_VALUE_NULL)
       
    52 /* When all the priorities are level null, the present value returns */
       
    53 /* the Relinquish Default value */
       
    54 #define MSO_VALUE_RELINQUISH_DEFAULT 1
       
    55 
       
    56 
       
    57 
       
    58 /* The IEC 61131-3 located variables mapped onto the Multi-state Output objects of BACnet protocol */
       
    59 %(MSO_lvars)s
       
    60 
       
    61 
       
    62 /* The array where we keep all the state related to the Multi-state Output Objects */
       
    63 #define MAX_MULTISTATE_OUTPUTS %(MSO_count)s
       
    64 static MULTISTATE_OUTPUT_DESCR MSO_Descr[MAX_MULTISTATE_OUTPUTS] = {
       
    65 %(MSO_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_Output_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  W ( 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_PRIORITY_ARRAY,          /* R  R ( 87) */
       
    87     PROP_RELINQUISH_DEFAULT,      /* R  R (104) */
       
    88 //  PROP_PROPERTY_LIST,           /* R  R (371) */
       
    89 //  PROP_CURRENT_COMMAND_PRIORITY,/* R  R (431) */   
       
    90     -1
       
    91 };
       
    92 
       
    93 static const int Multistate_Output_Properties_Optional[] = {
       
    94  /* (1) Currently Supported                  */
       
    95  /* (2) Required by standard ASHRAE 135-2016 */
       
    96                                   /*(1)(2)      */
       
    97     PROP_DESCRIPTION,             /* R  O ( 28) */
       
    98     PROP_STATE_TEXT,              /* R  O (110) */
       
    99     -1
       
   100 };
       
   101 
       
   102 static const int Multistate_Output_Properties_Proprietary[] = {
       
   103     -1
       
   104 };
       
   105 
       
   106 
       
   107 /* This array stores the PROPERTY_LIST which may be read by clients.
       
   108  * End of list is marked by following the last element with the value '-1'
       
   109  * 
       
   110  * It is initialized by Multistate_Output_Init() based off the values
       
   111  * stored in Multistate_Output_Properties_Required 
       
   112  *           Multistate_Output_Properties_Optional
       
   113  *           Multistate_Output_Properties_Proprietary
       
   114  */
       
   115 /* TODO: Allocate memory for this array with malloc() at startup */
       
   116 static int Multistate_Output_Properties_List[64];
       
   117 
       
   118 
       
   119 
       
   120 
       
   121 
       
   122 /********************************************************/
       
   123 /**                  Callback functions.               **/
       
   124 /** Functions required by BACnet devie implementation. **/
       
   125 /********************************************************/
       
   126 
       
   127 
       
   128 /* This is a callback function. Callback set in My_Object_Table[] array declared in device.c,  */
       
   129 void Multistate_Output_Property_Lists(
       
   130     const int **pRequired,
       
   131     const int **pOptional,
       
   132     const int **pProprietary)
       
   133 {
       
   134     if (pRequired)
       
   135         *pRequired = Multistate_Output_Properties_Required;
       
   136     if (pOptional)
       
   137         *pOptional = Multistate_Output_Properties_Optional;
       
   138     if (pProprietary)
       
   139         *pProprietary = Multistate_Output_Properties_Proprietary;
       
   140 
       
   141     return;
       
   142 }
       
   143 
       
   144 
       
   145 
       
   146 /* This is a callback function. Callback set in My_Object_Table[] array declared in device.c,  */
       
   147 void Multistate_Output_Init(
       
   148     void)
       
   149 {
       
   150     unsigned int i, j;
       
   151 
       
   152     /* initialize the Multistate_Output_Properties_List array */
       
   153     int len = 0;
       
   154     len += BACnet_Init_Properties_List(Multistate_Output_Properties_List + len,
       
   155                                        Multistate_Output_Properties_Required);
       
   156     len += BACnet_Init_Properties_List(Multistate_Output_Properties_List + len,
       
   157                                        Multistate_Output_Properties_Optional);
       
   158     len += BACnet_Init_Properties_List(Multistate_Output_Properties_List + len,
       
   159                                        Multistate_Output_Properties_Proprietary);
       
   160 
       
   161     /* initialize all the analog output priority arrays to NULL */
       
   162     for (i = 0; i < MAX_MULTISTATE_OUTPUTS; i++) {
       
   163         for (j = 0; j < BACNET_MAX_PRIORITY; j++) {
       
   164             MSO_Descr[i].Present_Value[j] = MSO_VALUE_NULL;
       
   165         }
       
   166         for (j = 0; j < MSO_Descr[i].Number_Of_States; j++) {
       
   167             sprintf(MSO_Descr[i].State_Text[j], "State %%d", j+1);
       
   168         }
       
   169     }
       
   170     return;
       
   171 }
       
   172 
       
   173 
       
   174 
       
   175 /* validate that the given instance exists */
       
   176 /* This is a callback function. Callback set in My_Object_Table[] array declared in device.c,  */
       
   177 bool Multistate_Output_Valid_Instance(
       
   178     uint32_t object_instance)
       
   179 {
       
   180     // fprintf(stderr, "BACnet plugin: Multistate_Output_Valid_Instance(obj_ID=%%u) called!\n", object _instance);
       
   181     return (Multistate_Output_Instance_To_Index(object_instance) < MAX_MULTISTATE_OUTPUTS);
       
   182 }
       
   183 
       
   184 
       
   185 /* the number of Multistate Value Objects */
       
   186 /* This is a callback function. Callback set in My_Object_Table[] array declared in device.c,  */
       
   187 unsigned Multistate_Output_Count(void) {return MAX_MULTISTATE_OUTPUTS;}
       
   188 
       
   189 
       
   190 /* returns the instance (i.e. Object Identifier) that correlates to the correct index */
       
   191 /* This is a callback function. Callback set in My_Object_Table[] array declared in device.c,  */
       
   192 uint32_t Multistate_Output_Index_To_Instance(unsigned index) {return MSO_Descr[index].Object_Identifier;}
       
   193 
       
   194 
       
   195 
       
   196 
       
   197 
       
   198 
       
   199 /* returns the index that correlates to the correct instance number (Object Identifier) */
       
   200 unsigned Multistate_Output_Instance_To_Index(
       
   201     uint32_t object_instance)
       
   202 {
       
   203     unsigned index = 0;
       
   204   
       
   205     for (index = 0; index < MAX_MULTISTATE_OUTPUTS; index++)
       
   206         if (object_instance == MSO_Descr[index].Object_Identifier)
       
   207             return index;
       
   208 
       
   209     /* error, this object ID is not in our list! */
       
   210     return MAX_MULTISTATE_OUTPUTS;
       
   211 }
       
   212 
       
   213 
       
   214 
       
   215 
       
   216 
       
   217 uint32_t Multistate_Output_Present_Value(
       
   218     uint32_t object_instance)
       
   219 {
       
   220     uint32_t value = MSO_VALUE_RELINQUISH_DEFAULT;
       
   221     unsigned index = 0; /* offset from instance lookup */
       
   222     unsigned i = 0;
       
   223 
       
   224     index = Multistate_Output_Instance_To_Index(object_instance);
       
   225     if (index < MAX_MULTISTATE_OUTPUTS) {
       
   226         for (i = 0; i < BACNET_MAX_PRIORITY; i++) {
       
   227             if (!MSO_VALUE_IS_NULL(MSO_Descr[index].Present_Value[i])) {
       
   228                 value = MSO_Descr[index].Present_Value[i];
       
   229                 break;
       
   230             }
       
   231         }
       
   232     }
       
   233 
       
   234     return value;
       
   235 }
       
   236 
       
   237 
       
   238 
       
   239 bool Multistate_Output_Present_Value_Set(
       
   240     uint32_t object_instance,
       
   241     uint32_t value,
       
   242     uint8_t  priority)
       
   243 {
       
   244     unsigned index = 0; /* offset from instance lookup */
       
   245 
       
   246     index = Multistate_Output_Instance_To_Index(object_instance);
       
   247     if (index >= MAX_MULTISTATE_OUTPUTS)
       
   248       return false;
       
   249 
       
   250     if ((priority == 0) || (priority > BACNET_MAX_PRIORITY) ||
       
   251         (priority == 6 /* reserved, ASHRAE 135-2016, section 19.2.2 */))
       
   252       return false;
       
   253     
       
   254     if ((value == 0) || (value > MSO_Descr[index].Number_Of_States))
       
   255       return false;
       
   256       
       
   257     priority--;
       
   258     MSO_Descr[index].Present_Value[priority] = (uint8_t) value;
       
   259 
       
   260     return true;
       
   261 }
       
   262 
       
   263 
       
   264 
       
   265 /* returns command priority (1..16), or 0 if all priority values are at NULL */
       
   266 int Multistate_Output_Current_Command_Priority(
       
   267     uint32_t object_instance)
       
   268 {
       
   269     unsigned index = 0;
       
   270     unsigned i = 0;
       
   271 
       
   272     index = Multistate_Output_Instance_To_Index(object_instance);
       
   273     if (index < MAX_MULTISTATE_OUTPUTS) {
       
   274         for (i = 0; i < BACNET_MAX_PRIORITY; i++) {
       
   275             if (!MSO_VALUE_IS_NULL(MSO_Descr[index].Present_Value[i])) {
       
   276                 return i+1; // +1 since priority is 1..16, and not 0..15
       
   277             }
       
   278         }
       
   279     }
       
   280     // command values in all priorities are set to NULL
       
   281     return 0;
       
   282 }
       
   283 
       
   284 
       
   285 
       
   286 
       
   287 bool Multistate_Output_Present_Value_Relinquish(
       
   288     uint32_t object_instance,
       
   289     uint8_t  priority)
       
   290 {
       
   291     unsigned index = 0; /* offset from instance lookup */
       
   292 
       
   293     index = Multistate_Output_Instance_To_Index(object_instance);
       
   294     if (index >= MAX_MULTISTATE_OUTPUTS)
       
   295       return false;
       
   296     
       
   297     if ((priority == 0) || (priority > BACNET_MAX_PRIORITY) ||
       
   298         (priority == 6 /* reserved, ASHRAE 135-2016, section 19.2.2 */))
       
   299       return false;
       
   300     
       
   301     priority--;
       
   302     MSO_Descr[index].Present_Value[priority] = MSO_VALUE_NULL;
       
   303 
       
   304     return true;
       
   305 }
       
   306 
       
   307 
       
   308 
       
   309 bool Multistate_Output_Out_Of_Service(
       
   310     uint32_t object_instance)
       
   311 {
       
   312     bool value = false;
       
   313     unsigned index = 0;
       
   314 
       
   315     index = Multistate_Output_Instance_To_Index(object_instance);
       
   316     if (index < MAX_MULTISTATE_OUTPUTS) {
       
   317         value = MSO_Descr[index].Out_Of_Service;
       
   318     }
       
   319 
       
   320     return value;
       
   321 }
       
   322 
       
   323 
       
   324 
       
   325 void Multistate_Output_Out_Of_Service_Set(
       
   326     uint32_t object_instance,
       
   327     bool value)
       
   328 {
       
   329     unsigned index = 0;
       
   330 
       
   331     index = Multistate_Output_Instance_To_Index(object_instance);
       
   332     if (index < MAX_MULTISTATE_OUTPUTS) {
       
   333         MSO_Descr[index].Out_Of_Service = value;
       
   334     }
       
   335 
       
   336     return;
       
   337 }
       
   338 
       
   339 
       
   340 
       
   341 static char *Multistate_Output_Description(
       
   342     uint32_t object_instance)
       
   343 {
       
   344     unsigned index = 0; /* offset from instance lookup */
       
   345     char *pName = NULL; /* return value */
       
   346 
       
   347     index = Multistate_Output_Instance_To_Index(object_instance);
       
   348     if (index < MAX_MULTISTATE_OUTPUTS) {
       
   349         pName = MSO_Descr[index].Description;
       
   350     }
       
   351 
       
   352     return pName;
       
   353 }
       
   354 
       
   355 
       
   356 
       
   357 /* This is a callback function. Callback set in My_Object_Table[] array declared in device.c,  */
       
   358 bool Multistate_Output_Object_Name(
       
   359     uint32_t object_instance,
       
   360     BACNET_CHARACTER_STRING * object_name)
       
   361 {
       
   362     unsigned index = 0; /* offset from instance lookup */
       
   363     bool status = false;
       
   364 
       
   365     index = Multistate_Output_Instance_To_Index(object_instance);
       
   366     if (index < MAX_MULTISTATE_OUTPUTS) {
       
   367         status = characterstring_init_ansi(object_name, MSO_Descr[index].Object_Name);
       
   368     }
       
   369 
       
   370     return status;
       
   371 }
       
   372 
       
   373 
       
   374 
       
   375 /* return apdu len, or BACNET_STATUS_ERROR on error */
       
   376 /* This is a callback function. Callback set in My_Object_Table[] array declared in device.c,  */
       
   377 int Multistate_Output_Read_Property(
       
   378     BACNET_READ_PROPERTY_DATA * rpdata)
       
   379 {
       
   380     int len = 0;
       
   381     int apdu_len = 0;   /* return value */
       
   382     BACNET_BIT_STRING bit_string;
       
   383     BACNET_CHARACTER_STRING char_string;
       
   384     uint32_t present_value = 0;
       
   385     unsigned object_index = 0;
       
   386     unsigned i = 0;
       
   387     bool state = false;
       
   388     uint8_t *apdu = NULL;
       
   389 
       
   390     if ((rpdata == NULL) || (rpdata->application_data == NULL) ||
       
   391         (rpdata->application_data_len == 0)) {
       
   392         return 0;
       
   393     }
       
   394 
       
   395     object_index = Multistate_Output_Instance_To_Index(rpdata->object_instance);
       
   396     if (object_index >= MAX_MULTISTATE_OUTPUTS) {
       
   397         rpdata->error_class = ERROR_CLASS_OBJECT;
       
   398         rpdata->error_code  = ERROR_CODE_UNKNOWN_OBJECT;
       
   399         return BACNET_STATUS_ERROR;
       
   400     }
       
   401 
       
   402     apdu = rpdata->application_data;
       
   403     switch (rpdata->object_property) {
       
   404         case PROP_OBJECT_IDENTIFIER:
       
   405             apdu_len =
       
   406                 encode_application_object_id(&apdu[0],
       
   407                 OBJECT_MULTI_STATE_OUTPUT, rpdata->object_instance);
       
   408             break;
       
   409         case PROP_OBJECT_NAME:
       
   410             Multistate_Output_Object_Name(rpdata->object_instance,
       
   411                 &char_string);
       
   412             apdu_len =
       
   413                 encode_application_character_string(&apdu[0], &char_string);
       
   414             break;
       
   415         case PROP_DESCRIPTION:
       
   416             characterstring_init_ansi(&char_string,
       
   417                 Multistate_Output_Description(rpdata->object_instance));
       
   418             apdu_len =
       
   419                 encode_application_character_string(&apdu[0], &char_string);
       
   420             break;
       
   421         case PROP_OBJECT_TYPE:
       
   422             apdu_len =
       
   423                 encode_application_enumerated(&apdu[0],
       
   424                 OBJECT_MULTI_STATE_OUTPUT);
       
   425             break;
       
   426         case PROP_PRESENT_VALUE:
       
   427             present_value =
       
   428                 Multistate_Output_Present_Value(rpdata->object_instance);
       
   429             apdu_len = encode_application_unsigned(&apdu[0], present_value);
       
   430             break;
       
   431         case PROP_STATUS_FLAGS:
       
   432             /* note: see the details in the standard on how to use these */
       
   433             bitstring_init(&bit_string);
       
   434             bitstring_set_bit(&bit_string, STATUS_FLAG_IN_ALARM, false);
       
   435             bitstring_set_bit(&bit_string, STATUS_FLAG_FAULT, false);
       
   436             bitstring_set_bit(&bit_string, STATUS_FLAG_OVERRIDDEN, false);
       
   437             if (Multistate_Output_Out_Of_Service(rpdata->object_instance)) {
       
   438                 bitstring_set_bit(&bit_string, STATUS_FLAG_OUT_OF_SERVICE,
       
   439                     true);
       
   440             } else {
       
   441                 bitstring_set_bit(&bit_string, STATUS_FLAG_OUT_OF_SERVICE,
       
   442                     false);
       
   443             }
       
   444             apdu_len = encode_application_bitstring(&apdu[0], &bit_string);
       
   445             break;
       
   446         case PROP_EVENT_STATE:
       
   447             /* note: see the details in the standard on how to use this */
       
   448             apdu_len =
       
   449                 encode_application_enumerated(&apdu[0], EVENT_STATE_NORMAL);
       
   450             break;
       
   451         case PROP_OUT_OF_SERVICE:
       
   452             state = MSO_Descr[object_index].Out_Of_Service;
       
   453             apdu_len = encode_application_boolean(&apdu[0], state);
       
   454             break;
       
   455         case PROP_PRIORITY_ARRAY:
       
   456             BACnet_encode_array(MSO_Descr[object_index].Present_Value,
       
   457                                 BACNET_MAX_PRIORITY,
       
   458                                 MSO_VALUE_IS_NULL,
       
   459                                 encode_application_unsigned);
       
   460             break;
       
   461 //      case PROP_CURRENT_COMMAND_PRIORITY: {
       
   462 //          unsigned i = Multistate_Output_Current_Command_Priority(rpdata->object_instance);
       
   463 //          if (i == 0)  apdu_len = encode_application_null    (&apdu[0]);
       
   464 //          else         apdu_len = encode_application_unsigned(&apdu[0], i);
       
   465 //          break;
       
   466 //      }
       
   467 
       
   468         case PROP_RELINQUISH_DEFAULT:
       
   469             present_value = MSO_VALUE_RELINQUISH_DEFAULT;
       
   470             apdu_len = encode_application_unsigned(&apdu[0], present_value);
       
   471             break;
       
   472         case PROP_NUMBER_OF_STATES:
       
   473             apdu_len =
       
   474                 encode_application_unsigned(&apdu[apdu_len],
       
   475                 MSO_Descr[object_index].Number_Of_States);
       
   476             break;
       
   477         case PROP_STATE_TEXT:
       
   478             BACnet_encode_array(MSO_Descr[object_index].State_Text,
       
   479                                 MSO_Descr[object_index].Number_Of_States,
       
   480                                 retfalse, BACnet_encode_character_string);
       
   481             break;
       
   482 //      case PROP_PROPERTY_LIST:
       
   483 //          BACnet_encode_array(Multistate_Output_Properties_List,
       
   484 //                              property_list_count(Multistate_Output_Properties_List),
       
   485 //                              retfalse, encode_application_enumerated);
       
   486 //          break;
       
   487         default:
       
   488             rpdata->error_class = ERROR_CLASS_PROPERTY;
       
   489             rpdata->error_code = ERROR_CODE_UNKNOWN_PROPERTY;
       
   490             apdu_len = BACNET_STATUS_ERROR;
       
   491             break;
       
   492     }
       
   493     /*  only array properties can have array options */
       
   494     if ((apdu_len >= 0) && (rpdata->object_property != PROP_STATE_TEXT) &&
       
   495         (rpdata->object_property != PROP_PRIORITY_ARRAY) &&
       
   496 //      (rpdata->object_property != PROP_PROPERTY_LIST) &&
       
   497         (rpdata->array_index != BACNET_ARRAY_ALL)) {
       
   498         rpdata->error_class = ERROR_CLASS_PROPERTY;
       
   499         rpdata->error_code = ERROR_CODE_PROPERTY_IS_NOT_AN_ARRAY;
       
   500         apdu_len = BACNET_STATUS_ERROR;
       
   501     }
       
   502 
       
   503     return apdu_len;
       
   504 }
       
   505 
       
   506 /* returns true if successful */
       
   507 /* This is a callback function. Callback set in My_Object_Table[] array declared in device.c,  */
       
   508 bool Multistate_Output_Write_Property(
       
   509     BACNET_WRITE_PROPERTY_DATA * wp_data)
       
   510 {
       
   511     bool status = false;        /* return value */
       
   512     int len = 0;
       
   513     unsigned object_index = 0;
       
   514     BACNET_APPLICATION_DATA_VALUE value;
       
   515 
       
   516     /* decode the some of the request */
       
   517     len =
       
   518         bacapp_decode_application_data(wp_data->application_data,
       
   519         wp_data->application_data_len, &value);
       
   520     /* FIXME: len < application_data_len: more data? */
       
   521     if (len < 0) {
       
   522         /* error while decoding - a value larger than we can handle */
       
   523         wp_data->error_class = ERROR_CLASS_PROPERTY;
       
   524         wp_data->error_code = ERROR_CODE_VALUE_OUT_OF_RANGE;
       
   525         return false;
       
   526     }
       
   527     if ((wp_data->object_property != PROP_STATE_TEXT) &&
       
   528         (wp_data->object_property != PROP_PRIORITY_ARRAY) &&
       
   529         (wp_data->array_index != BACNET_ARRAY_ALL)) {
       
   530         /*  only array properties can have array options */
       
   531         wp_data->error_class = ERROR_CLASS_PROPERTY;
       
   532         wp_data->error_code = ERROR_CODE_PROPERTY_IS_NOT_AN_ARRAY;
       
   533         return false;
       
   534     }
       
   535     /* No need to check whether object_index is within bounds.
       
   536      * Has already been checked before Multistate_Output_Write_Property() is called
       
   537      */
       
   538     object_index = Multistate_Output_Instance_To_Index(wp_data->object_instance);
       
   539 
       
   540     switch (wp_data->object_property) {
       
   541         case PROP_PRESENT_VALUE:
       
   542             status =
       
   543                 WPValidateArgType(&value, BACNET_APPLICATION_TAG_UNSIGNED_INT,
       
   544                 &wp_data->error_class, &wp_data->error_code);
       
   545             if (status) {
       
   546                 status =
       
   547                     Multistate_Output_Present_Value_Set
       
   548                     (wp_data->object_instance, value.type.Unsigned_Int, wp_data->priority);
       
   549                 if (!status) {
       
   550                     if (wp_data->priority == 6) {
       
   551                         /* Command priority 6 is reserved for use by Minimum On/Off
       
   552                            algorithm and may not be used for other purposes in any
       
   553                            object. */
       
   554                         wp_data->error_class = ERROR_CLASS_PROPERTY;
       
   555                         wp_data->error_code = ERROR_CODE_WRITE_ACCESS_DENIED;
       
   556                     } else {
       
   557                         wp_data->error_class = ERROR_CLASS_PROPERTY;
       
   558                         wp_data->error_code = ERROR_CODE_VALUE_OUT_OF_RANGE;
       
   559                     }
       
   560                 }
       
   561             } else {
       
   562                 status =
       
   563                     WPValidateArgType(&value, BACNET_APPLICATION_TAG_NULL,
       
   564                     &wp_data->error_class, &wp_data->error_code);
       
   565                 if (status) {
       
   566                     status =
       
   567                         Multistate_Output_Present_Value_Relinquish
       
   568                         (wp_data->object_instance, wp_data->priority);
       
   569                 }
       
   570                 if (!status) {
       
   571                     wp_data->error_class = ERROR_CLASS_PROPERTY;
       
   572                     wp_data->error_code = ERROR_CODE_INVALID_DATA_TYPE;
       
   573                 }
       
   574             }
       
   575             break;
       
   576         case PROP_OUT_OF_SERVICE:
       
   577         {
       
   578             bool Previous_Out_Of_Service = MSO_Descr[object_index].Out_Of_Service;
       
   579             status =
       
   580                 WPValidateArgType(&value, BACNET_APPLICATION_TAG_BOOLEAN,
       
   581                 &wp_data->error_class, &wp_data->error_code);
       
   582             if (status) {
       
   583                 MSO_Descr[object_index].Out_Of_Service = value.type.Boolean;
       
   584                 if (Previous_Out_Of_Service && !MSO_Descr[object_index].Out_Of_Service)
       
   585                     /* We have just changed from Out_of_Service -> In Service */
       
   586                     /* We need to update the Present_Value to the value
       
   587                      * currently in the PLC...
       
   588                      */
       
   589                     MSO_Descr[object_index].Present_Value[BACNET_MAX_PRIORITY-1] =
       
   590                                            *(MSO_Descr[object_index].Located_Var_ptr);
       
   591             }
       
   592             break;
       
   593         }
       
   594         case PROP_OBJECT_IDENTIFIER:
       
   595         case PROP_OBJECT_NAME:
       
   596         case PROP_OBJECT_TYPE:
       
   597         case PROP_STATUS_FLAGS:
       
   598         case PROP_EVENT_STATE:
       
   599         case PROP_NUMBER_OF_STATES:
       
   600         case PROP_PRIORITY_ARRAY:
       
   601 //      case PROP_CURRENT_COMMAND_PRIORITY:
       
   602         case PROP_RELINQUISH_DEFAULT:
       
   603         case PROP_DESCRIPTION:
       
   604         case PROP_STATE_TEXT:
       
   605 //      case PROP_PROPERTY_LIST:
       
   606             wp_data->error_class = ERROR_CLASS_PROPERTY;
       
   607             wp_data->error_code = ERROR_CODE_WRITE_ACCESS_DENIED;
       
   608             break;
       
   609         default:
       
   610             wp_data->error_class = ERROR_CLASS_PROPERTY;
       
   611             wp_data->error_code = ERROR_CODE_UNKNOWN_PROPERTY;
       
   612             break;
       
   613     }
       
   614 
       
   615     return status;
       
   616 }
       
   617 
       
   618 
       
   619 
       
   620 
       
   621 
       
   622 /********************************************/
       
   623 /** Functions required for Beremiz plugin  **/
       
   624 /********************************************/
       
   625 
       
   626 
       
   627 
       
   628 void  Multistate_Output_Copy_Present_Value_to_Located_Var(void) {
       
   629     unsigned i;
       
   630     for (i = 0; i < MAX_MULTISTATE_OUTPUTS; i++) {
       
   631         // decouple PLC's Located Variable from Bacnet Object's Present Value if Out_Of_Service is true
       
   632         if (MSO_Descr[i].Out_Of_Service)
       
   633             continue;
       
   634 
       
   635         // copy the value
       
   636         *(MSO_Descr[i].Located_Var_ptr) = Multistate_Output_Present_Value(MSO_Descr[i].Object_Identifier);
       
   637     }
       
   638 }
       
   639 
       
   640 
       
   641 
       
   642 void  Multistate_Output_Copy_Located_Var_to_Present_Value(void) {
       
   643     unsigned i;
       
   644     for (i = 0; i < MAX_MULTISTATE_OUTPUTS; i++) {
       
   645         // decouple PLC's Located Variable from Bacnet Object's Present Value if Out_Of_Service is true
       
   646         if (MSO_Descr[i].Out_Of_Service)
       
   647             continue;
       
   648 
       
   649         // Make sure local device does not set Present_Value to a value higher than Number_Of_States
       
   650         /* NOTE: The following comparison (Present_Value > Number_Of_States) is OK as the
       
   651          *       MSO_VALUE_NULL is currently set to 0. This means that the local controller
       
   652          *       can safely relinquish control by setting the Present_Value to 0.
       
   653          *       The comparison would not be safe if for some reason we later change
       
   654          *       MSO_VALUE_NULL to a value that may be higher than Number_Of_States.
       
   655          */
       
   656         #if MSO_VALUE_NULL != 0
       
   657         #error Implementation assumes that MSO_VALUE_NULL is set to 0, which is currently not the case.
       
   658         #endif
       
   659         if (*(MSO_Descr[i].Located_Var_ptr) > MSO_Descr[i].Number_Of_States)
       
   660             continue;
       
   661 
       
   662         // Everything seems to be OK. Copy the value
       
   663         MSO_Descr[i].Present_Value[BACNET_MAX_PRIORITY-1] = *(MSO_Descr[i].Located_Var_ptr);
       
   664     }
       
   665 }
       
   666 
       
   667 
       
   668