bacnet/runtime/av.c
changeset 2020 6dddf3070806
equal deleted inserted replaced
2019:92f02bb17c7e 2020:6dddf3070806
       
     1 /**************************************************************************
       
     2 *
       
     3 * Copyright (C) 2006 Steve Karg <skarg@users.sourceforge.net>
       
     4 * Copyright (C) 2011 Krzysztof Malorny <malornykrzysztof@gmail.com>
       
     5 * Copyright (C) 2017 Mario de Sousa <msousa@fe.up.pt>
       
     6 *
       
     7 * Permission is hereby granted, free of charge, to any person obtaining
       
     8 * a copy of this software and associated documentation files (the
       
     9 * "Software"), to deal in the Software without restriction, including
       
    10 * without limitation the rights to use, copy, modify, merge, publish,
       
    11 * distribute, sublicense, and/or sell copies of the Software, and to
       
    12 * permit persons to whom the Software is furnished to do so, subject to
       
    13 * the following conditions:
       
    14 *
       
    15 * The above copyright notice and this permission notice shall be included
       
    16 * in all copies or substantial portions of the Software.
       
    17 *
       
    18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
       
    19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
       
    20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
       
    21 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
       
    22 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
       
    23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
       
    24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
       
    25 *
       
    26 *********************************************************************/
       
    27 
       
    28 /* Analog Value Objects - customize for your use */
       
    29 
       
    30 #include <stdbool.h>
       
    31 #include <stdint.h>
       
    32 #include <stdio.h>
       
    33 #include <string.h>
       
    34 #include <math.h>     /* NAN maro */
       
    35 
       
    36 #include "config_bacnet_for_beremiz_%(locstr)s.h"     /* the custom configuration for beremiz plugin */
       
    37 #include "bacdef.h"
       
    38 #include "bacdcode.h"
       
    39 #include "bacenum.h"
       
    40 #include "bacapp.h"
       
    41 #include "bactext.h"
       
    42 #include "device_%(locstr)s.h"
       
    43 #include "handlers.h"
       
    44 #include "av_%(locstr)s.h"
       
    45 
       
    46 /* we choose to have a NULL level in our system represented by */
       
    47 /* a particular value.  When the priorities are not in use, they */
       
    48 /* will be relinquished (i.e. set to the NULL level). */
       
    49 /* Since the values are floats, we use NAN (Not A Number) as our NULL value. */
       
    50 /* WARNING: Never use comparisons like 'if (value == AO_LEVEL_NULL)'
       
    51  *           as it will always return false, even if both values are NAN.
       
    52  *          Use instead the negated version 'if (value != AO_LEVEL_NULL)'
       
    53  *           and add an 'else' to the 'if' condition if necessary.
       
    54  *         However, some compilers may screw this up if they do not
       
    55  *         implement IEEE 754 properly. It is probably best to stick with
       
    56  *         the isnan() macro if available.
       
    57  */
       
    58 #define AV_VALUE_NULL NAN
       
    59 #define AV_VALUE_IS_NULL(x)  (isnan(x))
       
    60 /* When all the priorities are level null, the present value returns */
       
    61 /* the Relinquish Default value */
       
    62 #define AV_VALUE_RELINQUISH_DEFAULT (0.0)
       
    63 
       
    64 
       
    65 /* The IEC 61131-3 located variables mapped onto the Analog Value objects of BACnet protocol */
       
    66 %(AV_lvars)s
       
    67 
       
    68 
       
    69 /* The array where we keep all the state related to the Analog Value Objects */
       
    70 #define MAX_ANALOG_VALUES %(AV_count)s
       
    71 static ANALOG_VALUE_DESCR AV_Descr[MAX_ANALOG_VALUES] = {
       
    72 %(AV_param)s
       
    73 };
       
    74 
       
    75 
       
    76 /* These three arrays are used by the ReadPropertyMultiple handler,
       
    77  * as well as to initialize the XXX_Property_List used by the 
       
    78  * Property List (PROP_PROPERTY_LIST) property.
       
    79  */
       
    80 static const int Analog_Value_Properties_Required[] = {
       
    81  /* (1) Currently Supported                  */
       
    82  /* (2) Required by standard ASHRAE 135-2016 */
       
    83                               /*(1)(2)      */
       
    84     PROP_OBJECT_IDENTIFIER,   /* R  R ( 75) */
       
    85     PROP_OBJECT_NAME,         /* R  R ( 77) */
       
    86     PROP_OBJECT_TYPE,         /* R  R ( 79) */
       
    87     PROP_PRESENT_VALUE,       /* W  R ( 85) */
       
    88     PROP_STATUS_FLAGS,        /* R  R (111) */
       
    89     PROP_EVENT_STATE,         /* R  R ( 36) */
       
    90     PROP_OUT_OF_SERVICE,      /* W  R ( 81) */
       
    91     PROP_UNITS,               /* W  R (117) */
       
    92 //  PROP_PROPERTY_LIST,       /* R  R (371) */
       
    93     -1
       
    94 };
       
    95 
       
    96 static const int Analog_Value_Properties_Optional[] = {
       
    97  /* (1) Currently Supported                  */
       
    98  /* (2) Required by standard ASHRAE 135-2016 */
       
    99                                   /*(1)(2)      */
       
   100     PROP_DESCRIPTION,             /* R  O ( 28) */
       
   101     /* required if Present_Value is writable (which is true in our case!) */
       
   102     PROP_PRIORITY_ARRAY,          /* R  O ( 87) */
       
   103     PROP_RELINQUISH_DEFAULT,      /* R  O (104) */
       
   104 //  PROP_CURRENT_COMMAND_PRIORITY,/* R  O (431) */   
       
   105     -1
       
   106 };
       
   107 
       
   108 static const int Analog_Value_Properties_Proprietary[] = {
       
   109     -1
       
   110 };
       
   111 
       
   112 /* This array stores the PROPERTY_LIST which may be read by clients.
       
   113  * End of list is marked by following the last element with the value '-1'
       
   114  * 
       
   115  * It is initialized by Analog_Values_Init() based off the values
       
   116  * stored in Analog_Value_Properties_Required 
       
   117  *           Analog_Value_Properties_Optional
       
   118  *           Analog_Value_Properties_Proprietary
       
   119  */
       
   120 /* TODO: Allocate memory for this array with malloc() at startup */
       
   121 static int Analog_Value_Properties_List[64];
       
   122 
       
   123 
       
   124 
       
   125 /********************************************************/
       
   126 /**                  Callback functions.               **/
       
   127 /** Functions required by BACnet devie implementation. **/
       
   128 /********************************************************/
       
   129 
       
   130 
       
   131 /* This is a callback function. Callback set in My_Object_Table[] array declared in device.c,  */
       
   132 void Analog_Value_Property_Lists(
       
   133     const int **pRequired,
       
   134     const int **pOptional,
       
   135     const int **pProprietary)
       
   136 {
       
   137     if (pRequired)
       
   138         *pRequired = Analog_Value_Properties_Required;
       
   139     if (pOptional)
       
   140         *pOptional = Analog_Value_Properties_Optional;
       
   141     if (pProprietary)
       
   142         *pProprietary = Analog_Value_Properties_Proprietary;
       
   143 
       
   144     return;
       
   145 }
       
   146 
       
   147 
       
   148 /* This is a callback function. Callback set in My_Object_Table[] array declared in device.c,  */
       
   149 void Analog_Value_Init(
       
   150     void)
       
   151 {
       
   152     unsigned i, j;
       
   153     static bool initialized = false;
       
   154 
       
   155     if (!initialized) {
       
   156         initialized = true;
       
   157 
       
   158         /* initialize the Analog_Value_Properties_List array */
       
   159         int len = 0;
       
   160         len += BACnet_Init_Properties_List(Analog_Value_Properties_List + len,
       
   161                                            Analog_Value_Properties_Required);
       
   162         len += BACnet_Init_Properties_List(Analog_Value_Properties_List + len,
       
   163                                            Analog_Value_Properties_Optional);
       
   164         len += BACnet_Init_Properties_List(Analog_Value_Properties_List + len,
       
   165                                            Analog_Value_Properties_Proprietary);
       
   166 
       
   167         for (i = 0; i < MAX_ANALOG_VALUES; i++) {
       
   168             // MJS: the following line in th original demo code was commented out so we do not
       
   169             //      overwrite the initial values configured by the user in beremiz IDE
       
   170             // memset(&AV_Descr[i], 0x00, sizeof(ANALOG_VALUE_DESCR));
       
   171             for (j = 0; j < BACNET_MAX_PRIORITY; j++) {
       
   172                 AV_Descr[i].Present_Value[j]  = AV_VALUE_NULL;
       
   173             }
       
   174             AV_Descr[i].Out_Of_Service = 0;
       
   175             AV_Descr[i].Event_State    = 0;
       
   176 //          AV_Descr[i].Units = UNITS_NO_UNITS;
       
   177         }
       
   178     }
       
   179 }
       
   180 
       
   181 
       
   182 
       
   183 
       
   184 /* validate that the given instance exists */
       
   185 /* This is a callback function. Callback set in My_Object_Table[] array declared in device.c,  */
       
   186 bool Analog_Value_Valid_Instance(
       
   187     uint32_t object_instance)
       
   188 {
       
   189     return (Analog_Value_Instance_To_Index(object_instance) < MAX_ANALOG_VALUES);
       
   190 }
       
   191 
       
   192 /* the number of Analog Value Objects */
       
   193 /* This is a callback function. Callback set in My_Object_Table[] array declared in device.c,  */
       
   194 unsigned Analog_Value_Count(void) {return MAX_ANALOG_VALUES;}
       
   195 
       
   196 
       
   197 /* returns the instance (i.e. Object Identifier) that correlates to the correct index */
       
   198 /* This is a callback function. Callback set in My_Object_Table[] array declared in device.c,  */
       
   199 uint32_t Analog_Value_Index_To_Instance(unsigned index) {return AV_Descr[index].Object_Identifier;}
       
   200 
       
   201 
       
   202 
       
   203 /* returns the index that correlates to the correct instance number (Object Identifier) */
       
   204 unsigned Analog_Value_Instance_To_Index(
       
   205     uint32_t object_instance)
       
   206 {
       
   207     unsigned index = 0;
       
   208   
       
   209     for (index = 0; index < MAX_ANALOG_VALUES; index++)
       
   210         if (object_instance == AV_Descr[index].Object_Identifier)
       
   211             return index;
       
   212 
       
   213     /* error, this object ID is not in our list! */
       
   214     return MAX_ANALOG_VALUES;
       
   215 }
       
   216 
       
   217 
       
   218 
       
   219 
       
   220 float Analog_Value_Present_Value(
       
   221     uint32_t object_instance)
       
   222 {
       
   223     float value = AV_VALUE_RELINQUISH_DEFAULT;
       
   224     unsigned index = 0;
       
   225     unsigned i = 0;
       
   226 
       
   227     index = Analog_Value_Instance_To_Index(object_instance);
       
   228     if (index < MAX_ANALOG_VALUES) {
       
   229         for (i = 0; i < BACNET_MAX_PRIORITY; i++) {
       
   230             if (!AV_VALUE_IS_NULL(AV_Descr[index].Present_Value[i])) {
       
   231                 value = AV_Descr[index].Present_Value[i];
       
   232                 break;
       
   233             }
       
   234         }
       
   235     }
       
   236 
       
   237     return value;
       
   238 }
       
   239 
       
   240 
       
   241 
       
   242 /* returns command priority (1..16), or 0 if all priority values are at NULL */
       
   243 int Analog_Value_Current_Command_Priority(
       
   244     uint32_t object_instance)
       
   245 {
       
   246     unsigned index = 0;
       
   247     unsigned i = 0;
       
   248 
       
   249     index = Analog_Value_Instance_To_Index(object_instance);
       
   250     if (index < MAX_ANALOG_VALUES) {
       
   251         for (i = 0; i < BACNET_MAX_PRIORITY; i++) {
       
   252             if (!AV_VALUE_IS_NULL(AV_Descr[index].Present_Value[i])) {
       
   253                 return i+1; // +1 since priority is 1..16, and not 0..15
       
   254             }
       
   255         }
       
   256     }
       
   257     // command values in all priorities are set to NULL
       
   258     return 0;
       
   259 }
       
   260 
       
   261 
       
   262 
       
   263 /* For a given object instance-number, sets the present-value at a given
       
   264  * priority 1..16 (except 6, see ASHRAE 135-2016, section 19.2.2)
       
   265  */
       
   266 bool Analog_Value_Present_Value_Set(
       
   267     uint32_t object_instance,
       
   268     float value,
       
   269     uint8_t priority)
       
   270 {
       
   271     unsigned index = 0;
       
   272 
       
   273     index = Analog_Value_Instance_To_Index(object_instance);
       
   274     if (index >= MAX_ANALOG_VALUES) 
       
   275       return false;
       
   276     if ((priority == 0) || (priority > BACNET_MAX_PRIORITY) ||
       
   277         (priority == 6 /* reserved, ASHRAE 135-2016, section 19.2.2 */))
       
   278       return false;
       
   279     
       
   280     priority--;
       
   281     AV_Descr[index].Present_Value[priority] = value;
       
   282     return true;
       
   283 }
       
   284 
       
   285 
       
   286 
       
   287 bool Analog_Value_Present_Value_Relinquish(
       
   288     uint32_t object_instance,
       
   289     unsigned priority)
       
   290 {
       
   291     unsigned index = 0;
       
   292 
       
   293     index = Analog_Value_Instance_To_Index(object_instance);
       
   294     if (index >= MAX_ANALOG_VALUES)
       
   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     AV_Descr[index].Present_Value[priority] = AV_VALUE_NULL;
       
   303     return true;
       
   304 }
       
   305 
       
   306 
       
   307 
       
   308 /* note: the object name must be unique within this device */
       
   309 /* This is a callback function. Callback set in My_Object_Table[] array declared in device.c,  */
       
   310 bool Analog_Value_Object_Name(
       
   311     uint32_t object_instance,
       
   312     BACNET_CHARACTER_STRING * object_name)
       
   313 {
       
   314     bool    status = false;
       
   315     unsigned index = Analog_Value_Instance_To_Index(object_instance);
       
   316 
       
   317     if (index < MAX_ANALOG_VALUES)
       
   318         status = characterstring_init_ansi(object_name, AV_Descr[index].Object_Name);
       
   319     
       
   320     return status;
       
   321 }
       
   322 
       
   323 
       
   324 
       
   325 bool Analog_Value_Object_Description(
       
   326     uint32_t object_instance,
       
   327     BACNET_CHARACTER_STRING * object_name)
       
   328 {
       
   329     bool    status = false;
       
   330     unsigned index = Analog_Value_Instance_To_Index(object_instance);
       
   331 
       
   332     if (index < MAX_ANALOG_VALUES)
       
   333         status = characterstring_init_ansi(object_name, AV_Descr[index].Description);
       
   334     
       
   335     return status;    
       
   336 }
       
   337 
       
   338 
       
   339 
       
   340 /* return apdu len, or BACNET_STATUS_ERROR on error */
       
   341 /* This is a callback function. Callback set in My_Object_Table[] array declared in device.c,  */
       
   342 int Analog_Value_Read_Property(
       
   343     BACNET_READ_PROPERTY_DATA * rpdata)
       
   344 {
       
   345     int apdu_len = 0;   /* return value */
       
   346     BACNET_BIT_STRING bit_string;
       
   347     BACNET_CHARACTER_STRING char_string;
       
   348     float real_value = (float) 1.414;
       
   349     unsigned object_index = 0;
       
   350     bool state = false;
       
   351     uint8_t *apdu = NULL;
       
   352     ANALOG_VALUE_DESCR *CurrentAV;
       
   353 
       
   354     if ((rpdata == NULL) || (rpdata->application_data == NULL) ||
       
   355         (rpdata->application_data_len == 0)) {
       
   356         return 0;
       
   357     }
       
   358 
       
   359     apdu = rpdata->application_data;
       
   360 
       
   361     object_index = Analog_Value_Instance_To_Index(rpdata->object_instance);
       
   362     if (object_index >= MAX_ANALOG_VALUES) {
       
   363         rpdata->error_class = ERROR_CLASS_OBJECT;
       
   364         rpdata->error_code  = ERROR_CODE_UNKNOWN_OBJECT;
       
   365         return BACNET_STATUS_ERROR;
       
   366     }
       
   367 
       
   368     switch (rpdata->object_property) {
       
   369         case PROP_OBJECT_IDENTIFIER:
       
   370             apdu_len =
       
   371                 encode_application_object_id(&apdu[0], OBJECT_ANALOG_VALUE,
       
   372                 rpdata->object_instance);
       
   373             break;
       
   374 
       
   375         case PROP_OBJECT_NAME:
       
   376             Analog_Value_Object_Name(rpdata->object_instance, &char_string);
       
   377             apdu_len =
       
   378                 encode_application_character_string(&apdu[0], &char_string);
       
   379             break;
       
   380 
       
   381         case PROP_DESCRIPTION:
       
   382             Analog_Value_Object_Description(rpdata->object_instance, &char_string);
       
   383             apdu_len =
       
   384                 encode_application_character_string(&apdu[0], &char_string);
       
   385             break;
       
   386 
       
   387         case PROP_OBJECT_TYPE:
       
   388             apdu_len =
       
   389                 encode_application_enumerated(&apdu[0], OBJECT_ANALOG_VALUE);
       
   390             break;
       
   391 
       
   392         case PROP_PRESENT_VALUE:
       
   393             real_value = Analog_Value_Present_Value(rpdata->object_instance);
       
   394             apdu_len = encode_application_real(&apdu[0], real_value);
       
   395             break;
       
   396 
       
   397         case PROP_STATUS_FLAGS:
       
   398             bitstring_init(&bit_string);
       
   399             bitstring_set_bit(&bit_string, STATUS_FLAG_IN_ALARM, false);
       
   400             bitstring_set_bit(&bit_string, STATUS_FLAG_FAULT, false);
       
   401             bitstring_set_bit(&bit_string, STATUS_FLAG_OVERRIDDEN, false);
       
   402             bitstring_set_bit(&bit_string, STATUS_FLAG_OUT_OF_SERVICE,
       
   403                 AV_Descr[object_index].Out_Of_Service);
       
   404 
       
   405             apdu_len = encode_application_bitstring(&apdu[0], &bit_string);
       
   406             break;
       
   407 
       
   408         case PROP_EVENT_STATE:
       
   409             apdu_len =
       
   410                 encode_application_enumerated(&apdu[0], EVENT_STATE_NORMAL);
       
   411             break;
       
   412 
       
   413         case PROP_OUT_OF_SERVICE:
       
   414             state = AV_Descr[object_index].Out_Of_Service;
       
   415             apdu_len = encode_application_boolean(&apdu[0], state);
       
   416             break;
       
   417 
       
   418         case PROP_PRIORITY_ARRAY:
       
   419             BACnet_encode_array(AV_Descr[object_index].Present_Value,
       
   420                                 BACNET_MAX_PRIORITY,
       
   421                                 AV_VALUE_IS_NULL,
       
   422                                 encode_application_real)
       
   423             break;
       
   424 
       
   425 //      case PROP_CURRENT_COMMAND_PRIORITY: {
       
   426 //          unsigned i = Analog_Value_Current_Command_Priority(rpdata->object_instance);
       
   427 //          if (i == 0)  apdu_len = encode_application_null    (&apdu[0]);
       
   428 //          else         apdu_len = encode_application_unsigned(&apdu[0], i);
       
   429 //          break;
       
   430 //      }
       
   431 
       
   432         case PROP_RELINQUISH_DEFAULT:
       
   433             real_value = AV_VALUE_RELINQUISH_DEFAULT;
       
   434             apdu_len = encode_application_real(&apdu[0], real_value);
       
   435             break;
       
   436 
       
   437         case PROP_UNITS:
       
   438             apdu_len =
       
   439                 encode_application_enumerated(&apdu[0], AV_Descr[object_index].Units);
       
   440             break;
       
   441 
       
   442 //      case PROP_PROPERTY_LIST:
       
   443 //          BACnet_encode_array(Analog_Value_Properties_List,
       
   444 //                              property_list_count(Analog_Value_Properties_List),
       
   445 //                              retfalse, encode_application_enumerated);
       
   446 //          break;
       
   447         default:
       
   448             rpdata->error_class = ERROR_CLASS_PROPERTY;
       
   449             rpdata->error_code = ERROR_CODE_UNKNOWN_PROPERTY;
       
   450             apdu_len = BACNET_STATUS_ERROR;
       
   451             break;
       
   452     }
       
   453     /*  only array properties can have array options */
       
   454     if ((apdu_len >= 0) && (rpdata->object_property != PROP_PRIORITY_ARRAY) &&
       
   455         (rpdata->object_property != PROP_EVENT_TIME_STAMPS) &&
       
   456 //      (rpdata->object_property != PROP_PROPERTY_LIST) &&
       
   457         (rpdata->array_index != BACNET_ARRAY_ALL)) {
       
   458         rpdata->error_class = ERROR_CLASS_PROPERTY;
       
   459         rpdata->error_code = ERROR_CODE_PROPERTY_IS_NOT_AN_ARRAY;
       
   460         apdu_len = BACNET_STATUS_ERROR;
       
   461     }
       
   462 
       
   463     return apdu_len;
       
   464 }
       
   465 
       
   466 
       
   467 
       
   468 
       
   469 /* returns true if successful */
       
   470 /* This is a callback function. Callback set in My_Object_Table[] array declared in device.c,  */
       
   471 bool Analog_Value_Write_Property(
       
   472     BACNET_WRITE_PROPERTY_DATA * wp_data)
       
   473 {
       
   474     bool status = false;        /* return value */
       
   475     unsigned int object_index = 0;
       
   476     int len = 0;
       
   477     BACNET_APPLICATION_DATA_VALUE value;
       
   478     ANALOG_VALUE_DESCR *CurrentAV;
       
   479 
       
   480     /* decode the some of the request */
       
   481     len =
       
   482         bacapp_decode_application_data(wp_data->application_data,
       
   483         wp_data->application_data_len, &value);
       
   484     /* FIXME: len < application_data_len: more data? */
       
   485     if (len < 0) {
       
   486         /* error while decoding - a value larger than we can handle */
       
   487         wp_data->error_class = ERROR_CLASS_PROPERTY;
       
   488         wp_data->error_code = ERROR_CODE_VALUE_OUT_OF_RANGE;
       
   489         return false;
       
   490     }
       
   491     if ((wp_data->object_property != PROP_PRIORITY_ARRAY) &&
       
   492         (wp_data->object_property != PROP_EVENT_TIME_STAMPS) &&
       
   493         (wp_data->array_index != BACNET_ARRAY_ALL)) {
       
   494         /*  only array properties can have array options */
       
   495         wp_data->error_class = ERROR_CLASS_PROPERTY;
       
   496         wp_data->error_code = ERROR_CODE_PROPERTY_IS_NOT_AN_ARRAY;
       
   497         return false;
       
   498     }
       
   499     object_index = Analog_Value_Instance_To_Index(wp_data->object_instance);
       
   500     if (object_index < MAX_ANALOG_VALUES)
       
   501         CurrentAV = &AV_Descr[object_index];
       
   502     else
       
   503         return false;
       
   504 
       
   505     switch (wp_data->object_property) {
       
   506         case PROP_PRESENT_VALUE:
       
   507             if (value.tag == BACNET_APPLICATION_TAG_REAL) {
       
   508                 if (Analog_Value_Present_Value_Set(wp_data->object_instance,
       
   509                         value.type.Real, wp_data->priority)) {
       
   510                     status = true;
       
   511                 } else if (wp_data->priority == 6) {
       
   512                     /* Command priority 6 is reserved for use by Minimum On/Off
       
   513                        algorithm and may not be used for other purposes in any
       
   514                        object. */
       
   515                     wp_data->error_class = ERROR_CLASS_PROPERTY;
       
   516                     wp_data->error_code = ERROR_CODE_WRITE_ACCESS_DENIED;
       
   517                 } else {
       
   518                     wp_data->error_class = ERROR_CLASS_PROPERTY;
       
   519                     wp_data->error_code = ERROR_CODE_VALUE_OUT_OF_RANGE;
       
   520                 }
       
   521             } else {
       
   522                 status =
       
   523                     WPValidateArgType(&value, BACNET_APPLICATION_TAG_NULL,
       
   524                     &wp_data->error_class, &wp_data->error_code);
       
   525                 if (status) {
       
   526                     status =
       
   527                         Analog_Value_Present_Value_Relinquish
       
   528                         (wp_data->object_instance, wp_data->priority);
       
   529                 }
       
   530                 if (!status) {
       
   531                     wp_data->error_class = ERROR_CLASS_PROPERTY;
       
   532                     wp_data->error_code = ERROR_CODE_VALUE_OUT_OF_RANGE;
       
   533                 }
       
   534             }
       
   535             break;
       
   536 
       
   537         case PROP_OUT_OF_SERVICE:
       
   538         {
       
   539             bool Previous_Out_Of_Service = CurrentAV->Out_Of_Service;
       
   540             status =
       
   541                 WPValidateArgType(&value, BACNET_APPLICATION_TAG_BOOLEAN,
       
   542                 &wp_data->error_class, &wp_data->error_code);
       
   543             if (status) {
       
   544                 CurrentAV->Out_Of_Service = value.type.Boolean;
       
   545                 if (Previous_Out_Of_Service && !CurrentAV->Out_Of_Service)
       
   546                     /* We have just changed from Out_of_Service -> In Service */
       
   547                     /* We need to update the Present_Value to the value
       
   548                      * currently in the PLC...
       
   549                      */
       
   550                     CurrentAV->Present_Value[BACNET_MAX_PRIORITY-1] =
       
   551                                              *(CurrentAV->Located_Var_ptr);
       
   552             }
       
   553             break;
       
   554         }
       
   555 
       
   556         case PROP_UNITS:
       
   557             status =
       
   558                 WPValidateArgType(&value, BACNET_APPLICATION_TAG_ENUMERATED,
       
   559                 &wp_data->error_class, &wp_data->error_code);
       
   560             if (status) {
       
   561                 CurrentAV->Units = value.type.Enumerated;
       
   562             }
       
   563             break;
       
   564 
       
   565         case PROP_OBJECT_IDENTIFIER:
       
   566         case PROP_OBJECT_NAME:
       
   567         case PROP_OBJECT_TYPE:
       
   568         case PROP_STATUS_FLAGS:
       
   569         case PROP_EVENT_STATE:
       
   570         case PROP_DESCRIPTION:
       
   571         case PROP_RELINQUISH_DEFAULT:
       
   572         case PROP_PRIORITY_ARRAY:
       
   573 //      case PROP_PROPERTY_LIST:
       
   574 //      case PROP_CURRENT_COMMAND_PRIORITY:
       
   575             wp_data->error_class = ERROR_CLASS_PROPERTY;
       
   576             wp_data->error_code = ERROR_CODE_WRITE_ACCESS_DENIED;
       
   577             break;
       
   578         default:
       
   579             wp_data->error_class = ERROR_CLASS_PROPERTY;
       
   580             wp_data->error_code = ERROR_CODE_UNKNOWN_PROPERTY;
       
   581             break;
       
   582     }
       
   583 
       
   584     return status;
       
   585 }
       
   586 
       
   587 
       
   588 
       
   589 
       
   590 
       
   591 
       
   592 
       
   593 /********************************************/
       
   594 /** Functions required for Beremiz plugin  **/
       
   595 /********************************************/
       
   596 
       
   597 void  Analog_Value_Copy_Present_Value_to_Located_Var(void) {
       
   598     unsigned i;
       
   599     for (i = 0; i < MAX_ANALOG_VALUES; i++) {
       
   600         // decouple PLC's Located Variable from Bacnet Object's Present Value if Out_Of_Service is true
       
   601         if (AV_Descr[i].Out_Of_Service)
       
   602             continue;
       
   603 
       
   604         // copy the value
       
   605         *(AV_Descr[i].Located_Var_ptr) = Analog_Value_Present_Value(AV_Descr[i].Object_Identifier);
       
   606     }
       
   607 }
       
   608 
       
   609 
       
   610 
       
   611 void  Analog_Value_Copy_Located_Var_to_Present_Value(void) {
       
   612     unsigned i;
       
   613     for (i = 0; i < MAX_ANALOG_VALUES; i++) {
       
   614         // decouple PLC's Located Variable from Bacnet Object's Present Value if Out_Of_Service is true
       
   615         if (AV_Descr[i].Out_Of_Service)
       
   616             continue;
       
   617 
       
   618         // copy the value
       
   619         AV_Descr[i].Present_Value[BACNET_MAX_PRIORITY-1] = *(AV_Descr[i].Located_Var_ptr);
       
   620     }
       
   621 }
       
   622