예제 #1
0
int32 rf_evt_pack_json(json_t *result, const int32 *evt_types, rf_evt_svc_t *service)
{
	int32 i = 0;
	json_t *types_array = NULL;
	json_t *listener = NULL;
	json_t *status = NULL;

	json_object_add(result, RMM_JSON_ODATA_CONTEXT, json_string(service->links.odata_context));
	json_object_add(result, RMM_JSON_ODATA_ID, json_string(service->links.odata_id));
	add_json_string(result, RMM_JSON_ODATA_TYPE, service->fixed.odata_type);
	add_json_string(result, RMM_JSON_RF_ID, service->fixed.id);
	add_json_string(result, RMM_JSON_RF_NAME, service->fixed.name);

	status = json_object();
	if (status != NULL) {
		add_json_string(status, RMM_JSON_RF_STATUS_STATE, service->fixed.status.state);
		add_json_string(status, RMM_JSON_RF_STATUS_HEALTH, service->fixed.status.health);
	}
	json_object_add(result, RMM_JSON_RF_STATUS, status);

	if (service->fixed.service_enabled)
		json_object_add(result, RMM_JSON_RF_SERVICE_ENABLED, json_true());
	else
		json_object_add(result, RMM_JSON_RF_SERVICE_ENABLED, json_false());

	add_json_integer(result, RMM_JSON_RF_DELIVERY_RETRY, service->fixed.retry);

	add_json_integer(result, RMM_JSON_RF_DELIVERY_RETRY_INTERVAL, service->fixed.retry_interval);


	types_array = json_array();
	if (types_array == NULL)
		return -1;

	for (i = 0;  i < MAX_EVT_ACTION_NUM; i++) {
		if (evt_types[i] == 0)
			continue;

		json_array_add(types_array, json_string((int8 *)rf_evt_msg[i].type_name));
	}
	json_object_add(result, RMM_JSON_RF_SUPPORTED_EVT_TYPES, types_array);

	listener = json_object();
	if (listener == NULL) 
		return -1;

	add_json_string(listener, RMM_JSON_ODATA_ID, service->links.subscriptions);
	json_object_add(result, RMM_JSON_RF_SUBSCRIPTION, listener);

	return 0;
}
static int32 pack_fan_basic_info(const fan_member_t *fan_member, json_t *result)
{
	pack_basic_element_info(&(fan_member->be), result);
	/*
	add_json_integer(result, RMM_JSON_FAN_ID, fan_member->be.id);
	add_json_string(result, RMM_JSON_UUID, fan_member->be.uuid);
	*/
	add_json_integer(result, RMM_JSON_ENABLE_STATE, fan_member->enable_state);
	add_json_integer(result, RMM_JSON_HEALTH_STATE, fan_member->health_state);
	add_json_string(result, RMM_JSON_DESIRED_SPEED, fan_member->desired_speed);
	add_json_integer(result, RMM_JSON_TACHOMETER_READING, fan_member->tachometer.reading);
	add_json_string(result, RMM_JSON_METRE_STATE, fan_member->tachometer.current_state);

	return 0;
}
static json_t *tzone_coll_get(struct rest_uri_param *param)
{
	collections_t *tzone_collections = NULL;
	uint32 tzone_num = 0;
	result_t rs = RESULT_OK;
	json_t *result = NULL;
	json_t *tzone = NULL;
	json_t *array = NULL;
	int32 i;
	int32 zone_num = 0;

	zone_num = libwrap_get_asset_num(MC_TYPE_TZONE);
	if (zone_num == 0) {
		HTTPD_ERR("get thermal zone num fail\n");
		return NULL;
	}

	tzone_collections = (collections_t *)malloc(zone_num * sizeof(collections_t));
	if (tzone_collections == NULL)
		return NULL;

	memset(tzone_collections, 0, zone_num * sizeof(collections_t));

	rs = libwrap_get_tzone_coll(tzone_collections, &tzone_num);
	if (rs != RESULT_OK) {
		HTTPD_ERR("get cooling zone collection fail, result is %d\n", rs);
		return NULL;
	}

	result = json_object();
	if (result == NULL) {
		HTTPD_ERR("result json object request error\n");
		return NULL;
	}

	array = json_array();
	if (array == NULL) {
		HTTPD_ERR("json array request error\n");
		return NULL;
	}

	for (i = 0; i < tzone_num; i++) {
		tzone = NULL;
		tzone = json_object();
		if (tzone != NULL) {
			add_json_integer(tzone, RMM_JSON_ID, tzone_collections[i].id);
			add_json_string(tzone, RMM_JSON_UUID, tzone_collections[i].uuid);
			add_json_string(tzone, RMM_JSON_NAME, tzone_collections[i].name);
			update_href_host(tzone_collections[i].href, HREF_URL_LEN, param->host);
			add_json_string(tzone, RMM_JSON_HREF, tzone_collections[i].href);
			json_array_add(array, tzone);
		}
	}
	json_object_add(result, RMM_JSON_THERMAL_ZONES, array);

	if (tzone_collections)
		free(tzone_collections);

	return result;
}
int32 pack_basic_element_info(const base_element_t *be, json_t *result)
{
	add_json_integer(result, RMM_JSON_ID, be->id);
	add_json_string(result, RMM_JSON_UUID, be->uuid);
	add_json_string(result, RMM_JSON_NAME, be->name);
	add_json_string(result, RMM_JSON_DESC, be->desc);
	add_json_string(result, RMM_JSON_CREATED_DATE, be->create_date);
	add_json_string(result, RMM_JSON_UPDATED_DATE, be->update_date);

	return 0;
}
static int32 fan_add_property(json_t *threshold, json_t *property, int32 value, const int8 *name)
{
	property = NULL;
	property = json_object();
	if (property != NULL) {
		add_json_integer(property, name, value);
		json_array_add(threshold, property);
	}

	return 0;
}
static int32 pack_tzone_basic_info(const tzone_member_t *tzone_member, json_t *result)
{
	add_json_integer(result, RMM_JSON_TZONE_ID, tzone_member->be.id);
	add_json_string(result, RMM_JSON_UUID, tzone_member->be.uuid);
	add_json_string(result, RMM_JSON_NAME, tzone_member->be.name);
	add_json_string(result, RMM_JSON_DESC, tzone_member->be.desc);
	add_json_string(result, RMM_JSON_CREATED_DATE, tzone_member->be.create_date);
	add_json_string(result, RMM_JSON_UPDATED_DATE, tzone_member->be.update_date);

	return 0;
}
static json_t *mbp_coll_get(struct rest_uri_param *param)
{
	collections_t *mbp_collections = NULL;
	uint32 mbp_num = 0;
	int32 i;
	int32 cm_num = 0;
	result_t rs = RESULT_OK;
	json_t *result = NULL;
	json_t *array = NULL;
	json_t *sub_mbp = NULL;

	cm_num = libwrap_get_asset_num(MC_TYPE_CM);
	if (cm_num == 0) {
		HTTPD_ERR("get mbp num fail\n");
		return NULL;
	}

	mbp_collections = (collections_t *)malloc(cm_num * sizeof(collections_t));
	if (mbp_collections == NULL)
		return NULL;

	memset(mbp_collections, 0, cm_num * sizeof(collections_t));

	rs = libwrap_get_mbp_coll(mbp_collections, &mbp_num);
	if (rs != RESULT_OK) {
		HTTPD_ERR("get mbp coll info fail, result is %d\n", rs);
		return NULL;
	}

	result = json_object();
	if (result == NULL) {
		HTTPD_ERR("result json object request fail\n");
		return NULL;
	}

	array = json_array();
	if (array == NULL) {
		HTTPD_ERR("json array request fail\n");
		return NULL;
	}

	for (i = 0; i < mbp_num; i++) {
		sub_mbp = NULL;
		sub_mbp = json_object();
		if (sub_mbp == NULL) {
			HTTPD_ERR("sub mbp json object request fail\n");
			return NULL;
		}

		add_json_integer(sub_mbp, RMM_JSON_ID, mbp_collections[i].id);
		add_json_string(sub_mbp, RMM_JSON_UUID, mbp_collections[i].uuid);
		add_json_string(sub_mbp, RMM_JSON_NAME, mbp_collections[i].name);
		update_href_host(mbp_collections[i].href, HREF_URL_LEN, param->host);
		add_json_string(sub_mbp, RMM_JSON_HREF, mbp_collections[i].href);
		json_array_add(array, sub_mbp);
	}
	json_object_add(result, RMM_JSON_MBPS, array);

	if (mbp_collections)
		free(mbp_collections);

	return result;
}
static json_t *fan_get(struct rest_uri_param *param)
{
	fan_member_t fan_member = { {0} };
	result_t rs = RESULT_OK;
	json_t *result = NULL;
	json_t *threshold = NULL;
	json_t *av_action = NULL;
	json_t *fru = NULL;
	json_t *property = NULL;
	json_t *action = NULL;
	json_t *cap = NULL;
	int32 tzone_idx = 0, fan_idx = 0;
	json_t *loc = NULL;

	tzone_idx = get_asset_idx(param, "zone_id", MC_TYPE_TZONE);
	if (tzone_idx == -1) {
		HTTPD_ERR("get cooling zone index fail\n");
		return NULL;
	}

	fan_idx = get_asset_idx(param, "fan_id", MC_TYPE_FAN);
	if (fan_idx == -1) {
		HTTPD_ERR("get fan index fail\n");
		return NULL;
	}

	rs = libwrap_get_tzone_fan_by_idx(tzone_idx, fan_idx,  &fan_member);
	if (rs != RESULT_OK) {
		HTTPD_ERR("get fan fail, result is %d\n", rs);
		return NULL;
	}

	result = json_object();
	if (result == NULL) {
		HTTPD_ERR("result json object request fail\n");
		return NULL;
	}

	pack_fan_basic_info(&fan_member, result);

	loc = json_object();
	if (loc != NULL) {
		add_loc_info(loc, (int32)fan_member.fan_loc.units, RMM_JSON_RACK_UNITS);
		add_loc_info(loc, fan_member.fan_loc.u_location, RMM_JSON_ULOC);
		add_loc_info(loc, fan_member.fan_loc.u_height, RMM_JSON_UHEIGHT);
        add_loc_info(loc, fan_member.fan_loc.x_location, RMM_JSON_XLOC);
	}
	json_object_add(result, RMM_JSON_RACK_LOC, loc);

	threshold = NULL;
	threshold = json_array();
	if (threshold == NULL) {
		HTTPD_ERR("threshold array request error\n");
		return NULL;
	}

	fan_add_property(threshold, property, fan_member.tachometer.threshold.lower_non_critical, RMM_JSON_LOWER_NON_CRITICAL);
	fan_add_property(threshold, property, fan_member.tachometer.threshold.upper_non_critical, RMM_JSON_UPPER_NON_CRITICAL);
	fan_add_property(threshold, property, fan_member.tachometer.threshold.lower_critical, RMM_JSON_LOWER_CRITICAL);
	fan_add_property(threshold, property, fan_member.tachometer.threshold.upper_critical, RMM_JSON_UPPER_CRITICAL);

	json_object_add(result, RMM_JSON_THRESHOLD, threshold);
	add_json_string(result, RMM_JSON_PRESENCE, fan_member.presence);
	add_json_string(result, RMM_JSON_ASSET_TAG, fan_member.asset.asset_tag);

	add_json_integer(result, RMM_JSON_SPEED_UNIT, fan_member.speed_unit);// 0--PWM, 1---RPM

	fru = json_object();
	if (fru != NULL) {
		pack_fru_info(&(fan_member.asset.fru), fru);
		json_object_add(result, RMM_JSON_FRU_INFO, fru);
	}

	av_action = json_array();
	if (av_action == NULL) {
		HTTPD_ERR("available action array request fail\n");
		return NULL;
	}

	/* requestStateChange */
	action = json_object();
	if (0 != prepare_get_action(fan_member.av_action, action, 0)) {
		HTTPD_ERR("action prepare fail\n");
		return NULL;
	}

	cap = json_array();
	if (cap != NULL) {
		json_t *property = NULL;
		json_t *allow_values = NULL;

		allow_values = json_array();
		property = json_object();
		if ((property != NULL) && (allow_values != NULL)) {
			if (0 != strcmp((int8 *)(fan_member.av_action[0].cap[0].property), "")) {
				json_object_add(property, RMM_JSON_PROPERTY_NAME, json_string((int8 *)(fan_member.av_action[0].cap[0].property)));
				json_array_add(allow_values, json_integer(str2int((int8 *)fan_member.av_action[0].cap[0].av)));
			}
		}

		property = NULL;
		property = json_object();
		if ((property != NULL) && (allow_values != NULL)) {
			if (0 != strcmp((int8 *)(fan_member.av_action[0].cap[1].property), "")) {
				json_object_add(property, RMM_JSON_PROPERTY_NAME, json_string((int8 *)(fan_member.av_action[0].cap[1].property)));
				json_array_add(allow_values, json_integer(str2int((int8 *)fan_member.av_action[0].cap[1].av)));
			}
		}
		json_object_add(property, RMM_JSON_ALLOWABLE_VALUES, allow_values);
		json_array_add(cap, property);
		json_object_add(action, RMM_JSON_CAPABILITIES, cap);
	}
	json_array_add(av_action, action);

	/* SetDesiredSpeed */
	action = NULL;
	action = json_object();
	if (0 != prepare_get_action(fan_member.av_action, action, 1)) {
		HTTPD_ERR("action prepare fail\n");
		return NULL;
	}

	cap = NULL;
	cap = json_array();
	if (cap != NULL) {
		json_t *property = NULL;
		json_t *allow_values = NULL;

		allow_values = json_array();
		property = json_object();
		if ((property != NULL) && (allow_values != NULL)) {
			if (0 != strcmp((int8 *)(fan_member.av_action[1].cap[0].property), "")) {
				json_object_add(property, RMM_JSON_PROPERTY_NAME, json_string((int8 *)(fan_member.av_action[1].cap[0].property)));
				json_array_add(allow_values, json_string((int8 *)(fan_member.av_action[1].cap[0].av)));
			}

			if (0 != strcmp((int8 *)(fan_member.av_action[1].cap[1].property), "")) {
				json_object_add(property, RMM_JSON_PROPERTY_NAME, json_string((int8 *)(fan_member.av_action[1].cap[1].property)));
				json_array_add(allow_values, json_string((int8 *)(fan_member.av_action[1].cap[1].av)));
			}
			json_object_add(property, RMM_JSON_ALLOWABLE_VALUES, allow_values);
			json_array_add(cap, property);
		}

		property = NULL;
		property = json_object();
		if (property != NULL) {
			if (0 != strcmp((int8 *)(fan_member.av_action[1].cap[2].property), "")) {
				json_object_add(property, RMM_JSON_PROPERTY_NAME, json_string((int8 *)(fan_member.av_action[1].cap[2].property)));
				json_array_add(cap, property);
			}
		}
		json_object_add(action, RMM_JSON_CAPABILITIES, cap);
	}
	json_array_add(av_action, action);
	json_object_add(result, RMM_JSON_AVALIBLE_ACTIONS, av_action);
	return result;
}
static json_t *fan_coll_get(struct rest_uri_param *param)
{
	collections_t *tzone_fan_coll = NULL;
	uint32 fan_num = 0;
	int8 *p_tzone_id = NULL;
	result_t rs = RESULT_OK;
	json_t *result = NULL;
	json_t *fan = NULL;
	json_t *array = NULL;
	int32 tzone_idx = 0;
	int32 i;

	tzone_fan_coll = (collections_t *)malloc(MAX_PWM_NUM * sizeof(collections_t));
	if (tzone_fan_coll == NULL)
		return NULL;

	memset(tzone_fan_coll, 0, MAX_PWM_NUM * sizeof(collections_t));

	tzone_idx = get_asset_idx(param, "zone_id", MC_TYPE_TZONE);
	if (tzone_idx == -1) {
		HTTPD_ERR("get cooling zone index fail\n");
		return NULL;
	}

	rs = libwrap_get_tzone_fan_coll(tzone_idx, tzone_fan_coll, &fan_num);
	if (rs != RESULT_OK) {
		HTTPD_ERR("get fan collection fail, result is %d\n", rs);
		return NULL;
	}

	result = json_object();
	if (result == NULL) {
		HTTPD_ERR("result json object request fail\n");
		return NULL;
	}

	array = json_array();
	if (array == NULL) {
		HTTPD_ERR("json array request fail\n");
		return NULL;
	}

	for (i = 0; i < fan_num; i++) {
		fan = NULL;
		fan = json_object();
		if (NULL != fan) {
			add_json_integer(fan, RMM_JSON_ID, tzone_fan_coll[i].id);
			add_json_string(fan, RMM_JSON_UUID, tzone_fan_coll[i].uuid);
			add_json_string(fan, RMM_JSON_NAME, tzone_fan_coll[i].name);
			update_href_host(tzone_fan_coll[i].href, HREF_URL_LEN, param->host);
			add_json_string(fan, RMM_JSON_HREF, tzone_fan_coll[i].href);
			json_array_add(array, fan);
		}
	}
	json_object_add(result, RMM_JSON_FANS, array);

	if (tzone_fan_coll)
		free(tzone_fan_coll);

	return result;
}
static json_t *tzone_get(struct rest_uri_param *param)
{
	tzone_member_t tzone_member = { {0} };
	result_t rs = RESULT_OK;
	json_t *result = NULL;
	json_t *loc = NULL;
	int32 tzone_idx = 0;
	int32 presence_len = 0;

	tzone_idx = get_asset_idx(param, "zone_id", MC_TYPE_TZONE);
	if (tzone_idx == -1) {
		HTTPD_ERR("get cooling zone index fail\n");
		return NULL;
	}

	rs = libwrap_get_tzone_by_idx(tzone_idx, &tzone_member);
	if (rs != RESULT_OK) {
		HTTPD_ERR("get cooling zone fail, result is %d\n", rs);
		return NULL;
	}

	result = json_object();
	if (result == NULL) {
		HTTPD_ERR("result json object request fail\n");
		return NULL;
	}

	/*pack_tzone_basic_info(&tzone_member, result);*/
	pack_basic_element_info((const base_element_t *)&(tzone_member.be), result);

	loc = json_object();
	if (loc != NULL) {
		add_loc_info(loc, (int32)tzone_member.tzone_loc.units, RMM_JSON_RACK_UNITS);
		add_loc_info(loc, tzone_member.tzone_loc.u_location, RMM_JSON_ULOC);
		add_loc_info(loc, tzone_member.tzone_loc.u_height, RMM_JSON_UHEIGHT);
        add_loc_info(loc, (int32)tzone_member.tzone_loc.x_location, RMM_JSON_XLOC);
	}
	json_object_add(result, RMM_JSON_RACK_LOC, loc);

	add_json_string(result, RMM_JSON_PRESENCE, (uint8 *)(tzone_member.presence));
	add_json_string(result, RMM_JSON_POLICY, tzone_member.policy);

	add_json_integer(result, RMM_JSON_PRESENT_TEMP, tzone_member.pres_temp);
	add_json_integer(result, RMM_JSON_OUT_LET_TEMP, tzone_member.outlet_temp);
	add_json_integer(result, RMM_JSON_AIRFLOW, tzone_member.volumetric_airflow);
	
	//TBD: it can be saved in memdb
	add_json_integer(result, RMM_JSON_MAX_FANS_SUPPORTED, MAX_PWM_NUM);
	presence_len = strlen(tzone_member.presence);
	if ((presence_len > -1) && (presence_len < 9)) {
		int32 i = 0;
		int32 number_fan = 0;
		for(i = 0; i < presence_len; i++) {
			if (tzone_member.presence[i] == 49)//"1"
				number_fan++;
		}
		add_json_integer(result, RMM_JSON_NUM_OF_FANS_PRESENT, number_fan);
	}

	return result;
}