/************************************************************************** * Description: handles recurring task * Returns: none * Notes: none **************************************************************************/ static void bacnet_test_task(void) { static unsigned index = 0; uint32_t instance; float float_value; uint16_t adc_value; instance = Analog_Input_Index_To_Instance(index); if (!Analog_Input_Out_Of_Service(instance)) { adc_value = adc_result_12bit(index); float_value = adc_value; float_value /= 4095; Analog_Input_Present_Value_Set(instance, float_value); } index++; if (index >= MAX_ANALOG_INPUTS) { index = 0; } }
/* assumption - object already exists */ int Analog_Input_Read_Property( BACNET_READ_PROPERTY_DATA * rpdata) { int apdu_len = 0; /* return value */ BACNET_CHARACTER_STRING char_string = { 0 }; BACNET_BIT_STRING bit_string = { 0 }; uint8_t *apdu = NULL; if ((rpdata == NULL) || (rpdata->application_data == NULL) || (rpdata->application_data_len == 0)) { return 0; } apdu = rpdata->application_data; switch (rpdata->object_property) { case PROP_OBJECT_IDENTIFIER: apdu_len = encode_application_object_id(&apdu[0], rpdata->object_type, rpdata->object_instance); break; case PROP_OBJECT_NAME: Analog_Input_Object_Name(rpdata->object_instance, &char_string); apdu_len = encode_application_character_string(&apdu[0], &char_string); break; case PROP_OBJECT_TYPE: apdu_len = encode_application_enumerated(&apdu[0], rpdata->object_type); break; case PROP_PRESENT_VALUE: apdu_len = encode_application_real(&apdu[0], Analog_Input_Present_Value(rpdata->object_instance)); break; case PROP_STATUS_FLAGS: bitstring_init(&bit_string); bitstring_set_bit(&bit_string, STATUS_FLAG_IN_ALARM, false); bitstring_set_bit(&bit_string, STATUS_FLAG_FAULT, false); bitstring_set_bit(&bit_string, STATUS_FLAG_OVERRIDDEN, false); bitstring_set_bit(&bit_string, STATUS_FLAG_OUT_OF_SERVICE, Analog_Input_Out_Of_Service(rpdata->object_instance)); apdu_len = encode_application_bitstring(&apdu[0], &bit_string); break; case PROP_EVENT_STATE: apdu_len = encode_application_enumerated(&apdu[0], EVENT_STATE_NORMAL); break; case PROP_OUT_OF_SERVICE: apdu_len = encode_application_boolean(&apdu[0], Analog_Input_Out_Of_Service(rpdata->object_instance)); break; case PROP_UNITS: apdu_len = encode_application_enumerated(&apdu[0], Analog_Input_Units(rpdata->object_instance)); break; default: rpdata->error_class = ERROR_CLASS_PROPERTY; rpdata->error_code = ERROR_CODE_UNKNOWN_PROPERTY; apdu_len = BACNET_STATUS_ERROR; break; } /* only array properties can have array options */ if ((apdu_len >= 0) && (rpdata->array_index != BACNET_ARRAY_ALL)) { rpdata->error_class = ERROR_CLASS_PROPERTY; rpdata->error_code = ERROR_CODE_PROPERTY_IS_NOT_AN_ARRAY; apdu_len = BACNET_STATUS_ERROR; } return apdu_len; }
/* returns true if successful */ bool Analog_Input_Write_Property( BACNET_WRITE_PROPERTY_DATA * wp_data) { bool status = false; /* return value */ int len = 0; BACNET_APPLICATION_DATA_VALUE value; /* decode the some of the request */ len = bacapp_decode_application_data(wp_data->application_data, wp_data->application_data_len, &value); /* FIXME: len < application_data_len: more data? */ if (len < 0) { /* error while decoding - a value larger than we can handle */ wp_data->error_class = ERROR_CLASS_PROPERTY; wp_data->error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; return false; } /* only array properties can have array options */ if ((wp_data->object_property != PROP_EVENT_TIME_STAMPS) && (wp_data->array_index != BACNET_ARRAY_ALL)) { wp_data->error_class = ERROR_CLASS_PROPERTY; wp_data->error_code = ERROR_CODE_PROPERTY_IS_NOT_AN_ARRAY; return false; } switch ((int) wp_data->object_property) { case PROP_PRESENT_VALUE: status = WPValidateArgType(&value, BACNET_APPLICATION_TAG_REAL, &wp_data->error_class, &wp_data->error_code); if (status) { if (Analog_Input_Out_Of_Service(wp_data->object_instance)) { Analog_Input_Present_Value_Set(wp_data->object_instance, value.type.Real); } else { wp_data->error_class = ERROR_CLASS_PROPERTY; wp_data->error_code = ERROR_CODE_WRITE_ACCESS_DENIED; status = false; } } break; case PROP_OUT_OF_SERVICE: status = WPValidateArgType(&value, BACNET_APPLICATION_TAG_BOOLEAN, &wp_data->error_class, &wp_data->error_code); if (status) { Analog_Input_Out_Of_Service_Set( wp_data->object_instance, value.type.Boolean); } break; case PROP_UNITS: status = WPValidateArgType(&value, BACNET_APPLICATION_TAG_ENUMERATED, &wp_data->error_class, &wp_data->error_code); if (status) { Analog_Input_Out_Of_Service_Set( wp_data->object_instance, value.type.Enumerated); } break; case PROP_OBJECT_IDENTIFIER: case PROP_OBJECT_NAME: case PROP_OBJECT_TYPE: case PROP_STATUS_FLAGS: case PROP_EVENT_STATE: wp_data->error_class = ERROR_CLASS_PROPERTY; wp_data->error_code = ERROR_CODE_WRITE_ACCESS_DENIED; break; default: wp_data->error_class = ERROR_CLASS_PROPERTY; wp_data->error_code = ERROR_CODE_UNKNOWN_PROPERTY; break; } return status; }
static time_t uci_Update( time_t ucimodtime, BACNET_OBJECT_TYPE update_object_type, int ucirewrite ) { char *section; char *type; time_t chk_mtime = 0; struct uci_context *ctx; int uci_idx; /* update Value from uci */ section = NULL; type = NULL; if (false) { section = NULL; type = NULL; #if defined(AI) } else if (update_object_type == OBJECT_ANALOG_INPUT) { section = "bacnet_ai"; type = "ai"; #endif #if defined(AO) } else if (update_object_type == OBJECT_ANALOG_OUTPUT) { section = "bacnet_ao"; type = "ao"; #endif #if defined(AV) } else if (update_object_type == OBJECT_ANALOG_VALUE) { section = "bacnet_av"; type = "av"; #endif #if defined(BI) } else if (update_object_type == OBJECT_BINARY_INPUT) { section = "bacnet_bi"; type = "bi"; #endif #if defined(BO) } else if (update_object_type == OBJECT_BINARY_OUTPUT) { section = "bacnet_bo"; type = "bo"; #endif #if defined(BV) } else if (update_object_type == OBJECT_BINARY_VALUE) { section = "bacnet_bv"; type = "bv"; #endif #if defined(MI) } else if (update_object_type == OBJECT_MULTI_STATE_INPUT) { section = "bacnet_mi"; type = "mi"; #endif #if defined(MO) } else if (update_object_type == OBJECT_MULTI_STATE_OUTPUT) { section = "bacnet_mo"; type = "mo"; #endif #if defined(MSV) } else if (update_object_type == OBJECT_MULTI_STATE_VALUE) { section = "bacnet_mv"; type = "mv"; #endif } else { return 0; } if ( ucirewrite == 0) { chk_mtime = ucimodtime; #if PRINT_ENABLED printf("rewrite type: %s\n", type); #endif } else { chk_mtime = check_uci_update(section, ucimodtime); } if(chk_mtime != 0) { sleep(1); ucimodtime = chk_mtime; #if PRINT_ENABLED printf("Config changed, reloading %s\n",section); #endif ctx = ucix_init(section); struct uci_itr_ctx itr; value_tuple_t *cur; itr.list = NULL; itr.section = section; itr.ctx = ctx; ucix_for_each_section_type(ctx, section, type, (void *)load_value, &itr); for( cur = itr.list; cur; cur = cur->next ) { uci_idx = atoi(cur->idx); #if PRINT_ENABLED printf("section %s idx %i \n", section, uci_idx); #endif if (false) { } /* update Analog Input from uci */ #if defined(AI) else if (update_object_type == OBJECT_ANALOG_INPUT) { float ai_val, ai_pval; ai_val = strtof(cur->value,NULL); ai_pval = Analog_Input_Present_Value(uci_idx); if ( ai_val != ai_pval ) { Analog_Input_Present_Value_Set(uci_idx,ai_val,16); } if (cur->Out_Of_Service == 0) { if (Analog_Input_Out_Of_Service(uci_idx)) Analog_Input_Out_Of_Service_Set(uci_idx,0); if (Analog_Input_Reliability(uci_idx)) Analog_Input_Reliability_Set(uci_idx, RELIABILITY_NO_FAULT_DETECTED); } else { #if PRINT_ENABLED printf("idx %s ",cur->idx); printf("Out_Of_Service\n"); #endif Analog_Input_Out_Of_Service_Set(uci_idx,1); Analog_Input_Reliability_Set(uci_idx, RELIABILITY_COMMUNICATION_FAILURE); } } #endif /* update Analog Output from uci */ #if defined(AO) else if (update_object_type == OBJECT_ANALOG_OUTPUT) { float ao_val, ao_pval; ao_val = strtof(cur->value,NULL); ao_pval = Analog_Output_Present_Value(uci_idx); if ( ao_val != ao_pval ) { Analog_Output_Present_Value_Set(uci_idx,ao_val,16); } if (cur->Out_Of_Service == 0) { if (Analog_Output_Out_Of_Service(uci_idx)) Analog_Output_Out_Of_Service_Set(uci_idx,0); if (Analog_Output_Reliability(uci_idx)) Analog_Output_Reliability_Set(uci_idx, RELIABILITY_NO_FAULT_DETECTED); } else { #if PRINT_ENABLED printf("idx %s ",cur->idx); printf("Out_Of_Service\n"); #endif Analog_Output_Out_Of_Service_Set(uci_idx,1); Analog_Output_Reliability_Set(uci_idx, RELIABILITY_COMMUNICATION_FAILURE); } } #endif /* update Analog Value from uci */ #if defined(AV) else if (update_object_type == OBJECT_ANALOG_VALUE) { float av_val, av_pval; av_val = strtof(cur->value,NULL); av_pval = Analog_Value_Present_Value(uci_idx); if ( av_val != av_pval ) { Analog_Value_Present_Value_Set(uci_idx,av_val,16); } if (cur->Out_Of_Service == 0) { if (Analog_Value_Out_Of_Service(uci_idx)) Analog_Value_Out_Of_Service_Set(uci_idx,0); if (Analog_Value_Reliability(uci_idx)) Analog_Value_Reliability_Set(uci_idx, RELIABILITY_NO_FAULT_DETECTED); } else { #if PRINT_ENABLED printf("idx %s ",cur->idx); printf("Out_Of_Service\n"); #endif Analog_Value_Out_Of_Service_Set(uci_idx,1); Analog_Value_Reliability_Set(uci_idx, RELIABILITY_COMMUNICATION_FAILURE); } } #endif /* update Binary Input from uci */ #if defined(BI) else if (update_object_type == OBJECT_BINARY_INPUT) { int bi_val, bi_pval; bi_val = atoi(cur->value); bi_pval = Binary_Input_Present_Value(uci_idx); if ( bi_val != bi_pval ) { Binary_Input_Present_Value_Set(uci_idx,bi_val,16); } if (cur->Out_Of_Service == 0) { if (Binary_Input_Out_Of_Service(uci_idx)) Binary_Input_Out_Of_Service_Set(uci_idx,0); if (Binary_Input_Reliability(uci_idx)) Binary_Input_Reliability_Set(uci_idx, RELIABILITY_NO_FAULT_DETECTED); } else { #if PRINT_ENABLED printf("idx %s ",cur->idx); printf("Out_Of_Service\n"); #endif Binary_Input_Out_Of_Service_Set(uci_idx,1); Binary_Input_Reliability_Set(uci_idx, RELIABILITY_COMMUNICATION_FAILURE); } } #endif /* update Binary Output from uci */ #if defined(BO) else if (update_object_type == OBJECT_BINARY_OUTPUT) { int bo_val, bo_pval; bo_val = atoi(cur->value); bo_pval = Binary_Output_Present_Value(uci_idx); if ( bo_val != bo_pval ) { Binary_Output_Present_Value_Set(uci_idx,bo_val,16); } if (cur->Out_Of_Service == 0) { if (Binary_Output_Out_Of_Service(uci_idx)) Binary_Output_Out_Of_Service_Set(uci_idx,0); if (Binary_Output_Reliability(uci_idx)) Binary_Output_Reliability_Set(uci_idx, RELIABILITY_NO_FAULT_DETECTED); } else { #if PRINT_ENABLED printf("idx %s ",cur->idx); printf("Out_Of_Service\n"); #endif Binary_Output_Out_Of_Service_Set(uci_idx,1); Binary_Output_Reliability_Set(uci_idx, RELIABILITY_COMMUNICATION_FAILURE); } } #endif /* update Binary Value from uci */ #if defined(BV) else if (update_object_type == OBJECT_BINARY_VALUE) { int bv_val, bv_pval; bv_val = atoi(cur->value); bv_pval = Binary_Value_Present_Value(uci_idx); if ( bv_val != bv_pval ) { Binary_Value_Present_Value_Set(uci_idx,bv_val,16); } if (cur->Out_Of_Service == 0) { if (Binary_Value_Out_Of_Service(uci_idx)) Binary_Value_Out_Of_Service_Set(uci_idx,0); if (Binary_Value_Reliability(uci_idx)) Binary_Value_Reliability_Set(uci_idx, RELIABILITY_NO_FAULT_DETECTED); } else { #if PRINT_ENABLED printf("idx %s ",cur->idx); printf("Out_Of_Service\n"); #endif Binary_Value_Out_Of_Service_Set(uci_idx,1); Binary_Value_Reliability_Set(uci_idx, RELIABILITY_COMMUNICATION_FAILURE); } } #endif /* update Multistate Input from uci */ #if defined(MSI) else if (update_object_type == OBJECT_MULTI_STATE_INPUT) { int msi_val, msi_pval; msi_val = atoi(cur->value); msi_pval = Multistate_Input_Present_Value(uci_idx); if ( msi_val != msi_pval ) { Multistate_Input_Present_Value_Set(uci_idx,msi_val,16); } if (cur->Out_Of_Service == 0) { if (Multistate_Input_Out_Of_Service(uci_idx)) Multistate_Input_Out_Of_Service_Set(uci_idx,0); if (Multistate_Input_Reliability(uci_idx)) Multistate_Input_Reliability_Set(uci_idx, RELIABILITY_NO_FAULT_DETECTED); } else { #if PRINT_ENABLED printf("idx %s ",cur->idx); printf("Out_Of_Service\n"); #endif Multistate_Input_Out_Of_Service_Set(uci_idx,1); Multistate_Input_Reliability_Set(uci_idx, RELIABILITY_COMMUNICATION_FAILURE); } } #endif /* update Multistate Output from uci */ #if defined(MSO) else if (update_object_type == OBJECT_MULTI_STATE_OUTPUT) { int mso_val, mso_pval; mso_val = atoi(cur->value); mso_pval = Multistate_Output_Present_Value(uci_idx); if ( mso_val != mso_pval ) { Multistate_Output_Present_Value_Set(uci_idx,mso_val,16); } if (cur->Out_Of_Service == 0) { if (Multistate_Output_Out_Of_Service(uci_idx)) Multistate_Output_Out_Of_Service_Set(uci_idx,0); if (Multistate_Output_Reliability(uci_idx)) Multistate_Output_Reliability_Set(uci_idx, RELIABILITY_NO_FAULT_DETECTED); } else { #if PRINT_ENABLED printf("idx %s ",cur->idx); printf("Out_Of_Service\n"); #endif Multistate_Output_Out_Of_Service_Set(uci_idx,1); Multistate_Output_Reliability_Set(uci_idx, RELIABILITY_COMMUNICATION_FAILURE); } } #endif /* update Multistate Value from uci */ #if defined(MSV) else if (update_object_type == OBJECT_MULTI_STATE_VALUE) { int msv_val, msv_pval; msv_val = atoi(cur->value); msv_pval = Multistate_Value_Present_Value(uci_idx); if ( msv_val != msv_pval ) { Multistate_Value_Present_Value_Set(uci_idx,msv_val,16); } if (cur->Out_Of_Service == 0) { if (Multistate_Value_Out_Of_Service(uci_idx)) Multistate_Value_Out_Of_Service_Set(uci_idx,0); if (Multistate_Value_Reliability(uci_idx)) Multistate_Value_Reliability_Set(uci_idx, RELIABILITY_NO_FAULT_DETECTED); } else { #if PRINT_ENABLED printf("idx %s ",cur->idx); printf("Out_Of_Service\n"); #endif Multistate_Value_Out_Of_Service_Set(uci_idx,1); Multistate_Value_Reliability_Set(uci_idx, RELIABILITY_COMMUNICATION_FAILURE); } } #endif } ucix_cleanup(ctx); } /* update end */ return ucimodtime; }