Exemplo n.º 1
0
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;
}
Exemplo n.º 2
0
Arquivo: bv.c Projeto: empeeoh/BACnet
/* return apdu len, or BACNET_STATUS_ERROR on error */
int Binary_Value_Read_Property(
    BACNET_READ_PROPERTY_DATA * rpdata)
{
    int len = 0;
    int apdu_len = 0;   /* return value */
    BACNET_BIT_STRING bit_string;
    BACNET_CHARACTER_STRING char_string;
    BACNET_BINARY_PV present_value = BINARY_INACTIVE;
    unsigned object_index = 0;
    unsigned i = 0;
    bool state = false;
    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], OBJECT_BINARY_VALUE,
                rpdata->object_instance);
            break;
            /* note: Name and Description don't have to be the same.
               You could make Description writable and different */
        case PROP_OBJECT_NAME:
        case PROP_DESCRIPTION:
            Binary_Value_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], OBJECT_BINARY_VALUE);
            break;
        case PROP_PRESENT_VALUE:
            present_value =
                Binary_Value_Present_Value(rpdata->object_instance);
            apdu_len = encode_application_enumerated(&apdu[0], present_value);
            break;
        case PROP_STATUS_FLAGS:
            /* note: see the details in the standard on how to use these */
            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);
            state = Binary_Value_Out_Of_Service(rpdata->object_instance);
            bitstring_set_bit(&bit_string, STATUS_FLAG_OUT_OF_SERVICE, state);
            apdu_len = encode_application_bitstring(&apdu[0], &bit_string);
            break;
        case PROP_EVENT_STATE:
            /* note: see the details in the standard on how to use this */
            apdu_len =
                encode_application_enumerated(&apdu[0], EVENT_STATE_NORMAL);
            break;
        case PROP_OUT_OF_SERVICE:
            state = Binary_Value_Out_Of_Service(rpdata->object_instance);
            apdu_len = encode_application_boolean(&apdu[0], state);
            break;
        case PROP_PRIORITY_ARRAY:
            /* Array element zero is the number of elements in the array */
            if (rpdata->array_index == 0)
                apdu_len =
                    encode_application_unsigned(&apdu[0], BACNET_MAX_PRIORITY);
            /* if no index was specified, then try to encode the entire list */
            /* into one packet. */
            else if (rpdata->array_index == BACNET_ARRAY_ALL) {
                object_index =
                    Binary_Value_Instance_To_Index(rpdata->object_instance);
                for (i = 0; i < BACNET_MAX_PRIORITY; i++) {
                    /* FIXME: check if we have room before adding it to APDU */
                    if (Binary_Value_Level[object_index][i] == BINARY_NULL)
                        len = encode_application_null(&apdu[apdu_len]);
                    else {
                        present_value = Binary_Value_Level[object_index][i];
                        len =
                            encode_application_enumerated(&apdu[apdu_len],
                            present_value);
                    }
                    /* add it if we have room */
                    if ((apdu_len + len) < MAX_APDU)
                        apdu_len += len;
                    else {
                        rpdata->error_code =
                            ERROR_CODE_ABORT_SEGMENTATION_NOT_SUPPORTED;
                        apdu_len = BACNET_STATUS_ABORT;
                        break;
                    }
                }
            } else {
                object_index =
                    Binary_Value_Instance_To_Index(rpdata->object_instance);
                if (rpdata->array_index <= BACNET_MAX_PRIORITY) {
                    if (Binary_Value_Level[object_index][rpdata->array_index]
                        == BINARY_NULL)
                        apdu_len = encode_application_null(&apdu[apdu_len]);
                    else {
                        present_value = Binary_Value_Level[object_index]
                            [rpdata->array_index];
                        apdu_len =
                            encode_application_enumerated(&apdu[apdu_len],
                            present_value);
                    }
                } else {
                    rpdata->error_class = ERROR_CLASS_PROPERTY;
                    rpdata->error_code = ERROR_CODE_INVALID_ARRAY_INDEX;
                    apdu_len = BACNET_STATUS_ERROR;
                }
            }
            break;
        case PROP_RELINQUISH_DEFAULT:
            present_value = RELINQUISH_DEFAULT;
            apdu_len = encode_application_enumerated(&apdu[0], present_value);
            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->object_property != PROP_PRIORITY_ARRAY) &&
        (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;
}