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