char * slayer_server_stats_tojson(slayer_server_stats_t *istats,apr_pool_t *mpool) {
	slayer_server_stats_t stats;
	slayer_server_stats_get(istats,&stats);

	json_value *container = json_object_create(mpool);
	json_object_add(container,"total_requests",json_long_create(mpool,stats.total_requests));
	json_value *hits = json_array_create(mpool,stats.nslice);
	json_value *slices = json_array_create(mpool,stats.nslice);
	int i;
	for ( i  = stats.offset+1; i < stats.nslice; i++) {
		if (stats.slices[i] != 0) {
			json_array_append(hits,json_long_create(mpool,stats.hits[i]));
			json_array_append(slices,json_long_create(mpool,stats.slices[i]));
		}
	}
	for ( i = 0; i < stats.offset; i++) {
		if (stats.slices[i] != 0) {
			json_array_append(hits,json_long_create(mpool,stats.hits[i]));
			json_array_append(slices,json_long_create(mpool,stats.slices[i]));
		}
	}
	json_object_add(container,"hits",hits);
	json_object_add(container,"slices",slices);
	json_object_add(container,"start_time",json_long_create(mpool,stats.start_time));
	json_object_add(container,"current_time",json_long_create(mpool,apr_time_now() / (1000*1000)));
	return json_serialize(mpool,container);
}
json_t *get_json_listener(listener_dest_info_t *header)
{
	json_t *result = NULL;
	json_t *jitem = NULL;
	json_t *evt_type_item = NULL;
	int32 i = 0;
	int8 evt_type[128];
	listener_dest_info_t *tmp;

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

	while (header) {
		jitem = json_object();
		json_object_add(jitem, RMM_JSON_RF_LINKS, json_string(header->dest));

		evt_type_item = json_array();
		for (i = 0; i < header->evt_index; i++) {
			memset(evt_type, 0, 128);
			get_event_type_by_nodeid(header->event_types[i], evt_type, sizeof(evt_type));
			json_array_add(evt_type_item, json_string(evt_type));
		}

		json_object_add(jitem, RMM_JSON_RF_EVT_TYPES, evt_type_item);
		json_array_add(result, jitem);
		tmp = header->pnext;
		free(header);
		header = tmp;
	}

	return result;
}
void pack_rf_evt_links(json_t *result, rf_link_t *link)
{
	int32 i = 0;
	json_t *array = NULL;
	json_t *listener = NULL;

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

	for (i = 0; i < RF_EVENT_MAX_LISTENER_NUM; i++) {
		listener = NULL;
		listener = json_object();
		if (listener != NULL) {
			if (strlen(link->subscription[i]) != 0) {
				add_json_string(listener, RMM_JSON_RF_LISTENER, (uint8 *)(link->subscription[i]));
				json_array_add(array, listener);
			}
		}
	}
	json_object_add(result, RMM_JSON_RF_SUBSCRIPTION, array);

	add_json_string(result, RMM_JSON_RF_SELF, (uint8 *)link->self);
}
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;
}
void add_loc_info(json_t *loc, int32 input, const int8 *name)
{
	json_t *rs_json = NULL;

	rs_json = json_integer((int64)input);
	if (rs_json)
		json_object_add(loc, name, rs_json);
}
void slayer_server_log_add_error(slayer_server_log_manager_t *manager, apr_pool_t *mpool,
                                  const char *client_ip,apr_int64_t rtime,
                                  const char *request_line, const char *error_msg ) {

	json_value *container = json_object_create(mpool);
	json_object_add(container,"client_ip",json_string_create(mpool,client_ip));
	json_object_add(container,"request_time",json_long_create(mpool,rtime / (1000*1000)));
	json_object_add(container,"request",json_string_create(mpool,request_line));
	json_object_add(container,"error",json_string_create(mpool,error_msg));
	char *json_entry = strdup(json_serialize(mpool,container)); //we want our own copy of this data
	//smallest chunk in the mutex
	apr_thread_mutex_lock(manager->list_mutex);
	manager->offset++;
	if (manager->offset == manager->nentries)  manager->offset = 0;
	free(manager->entries[manager->offset].json_view);
	manager->entries[manager->offset].json_view = json_entry;
	apr_thread_mutex_unlock(manager->list_mutex);
}
int32 evt_listeners_pack_json(json_t *result, evt_listeners_t *listeners)
{
	json_t *listener_array = NULL;
	json_t * listener = NULL;
	int32 i = 0;

	json_object_add(result, RMM_JSON_ODATA_CONTEXT, json_string(listeners->odata_context));
	json_object_add(result, RMM_JSON_ODATA_TYPE, json_string(listeners->odata_type));
	json_object_add(result, RMM_JSON_NAME, json_string(listeners->name));
	json_object_add(result, RMM_JSON_MEMBERS_ODATA_COUNT, json_integer(listeners->num));

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

	for (i = 0; i < listeners->num; i++) {
		listener = json_object();
		if (listener == NULL) {
			HTTPD_ERR("json object request fail\n");
			return -1;
		}
		json_object_add(listener, RMM_JSON_ODATA_ID, json_string(listeners->url[i]));
		json_array_add(listener_array, listener);
	}

	json_object_add(result, RMM_JSON_MEMBERS, listener_array);

	return 0;
}
int32 evt_listener_pack_json(json_t *result, evt_listener_t *listener)
{
	int8 evt_type[128] = {};
	int32 i = 0;
	json_t *evt_type_item = NULL;

	if (strnlen_s(listener->url, sizeof(listener->url)-1) == 0) {
		HTTPD_ERR("invalid subscribe destination\n");
		return -1;
	}

	json_object_add(result, RMM_JSON_ODATA_CONTEXT, json_string(listener->odata_context));
	json_object_add(result, RMM_JSON_ODATA_ID, json_string(listener->odata_id));
	json_object_add(result, RMM_JSON_ODATA_TYPE, json_string(listener->odata_type));
	json_object_add(result, RMM_JSON_RF_ID, json_string(listener->id));
	json_object_add(result, RMM_JSON_RF_NAME, json_string(listener->name));
	json_object_add(result, RMM_JSON_RF_DESC, json_string(listener->description));
	json_object_add(result, RMM_JSON_RF_DEST_URL, json_string(listener->url));

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

	for (i = 0; i < listener->evt_index; i++) {
		memset(evt_type, 0, sizeof(evt_type));
		get_event_type_by_node_type(listener->event_types[i], evt_type, sizeof(evt_type));
		if (strnlen_s(evt_type, sizeof(evt_type)-1) != 0)
			json_array_add(evt_type_item, json_string(evt_type));
	}
	json_object_add(result, RMM_JSON_RF_EVT_TYPES, evt_type_item);

	json_object_add(result, RMM_JSON_RF_CONTEXT, json_string(listener->context));
	json_object_add(result, RMM_JSON_RF_PROTOCOL, json_string(listener->protocol));

	return 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 json_t *mbp_process_update(json_t *req, int32 idx, struct rest_uri_param *param)
{
	json_t *result = NULL;
	int8 *image_data = NULL;
	json_t *image_obj = NULL;
	int32 rc = -1;
	int32 cm_lid = 0;
	int8 cm_dev[64] = {0};

	/* check update capability */
	if(libwrap_check_update_capability() != RESULT_OK) {
		HTTPD_ERR("firmware update is not supported.\n");
		return NULL;
	}

	if (!libwrap_get_firmware_update_status()) {
		HTTPD_ERR("get firmware update status fail\n");
		return NULL;
	}

	libwrap_set_firmware_update_status(0);

	result = json_object();
	if (result == NULL) {
		update_response_info(param, HTTP_APPLICATION_ERROR);
		libwrap_set_firmware_update_status(1);
		return NULL;
	}
	rc = libwrap_check_tftp_service();
	if (rc == -1) {
		json_free(req);
		json_object_add(result, RMM_JSON_FRU_RESULT, json_string("tftp server not ready"));
		update_response_info(param, HTTP_NOT_ACCEPTABLE);
		libwrap_set_firmware_update_status(1);
		return result;
	}

	image_obj = json_object_get(req, RMM_JSON_IMAGE);
	if (image_obj == NULL) {
		json_free(req);
		json_object_add(result, RMM_JSON_FRU_RESULT, json_string("invalid image"));
		update_response_info(param, HTTP_NOT_ACCEPTABLE);
		libwrap_set_firmware_update_status(1);
		return result;
	}

	image_data = json_string_value(image_obj);
	if (image_data == NULL) {
		json_free(req);
		json_object_add(result, RMM_JSON_FRU_RESULT, json_string("invalid image"));
		update_response_info(param, HTTP_NOT_ACCEPTABLE);
		libwrap_set_firmware_update_status(1);
		return result;
	}

	cm_lid = idx;
	if ((cm_lid == 1) || (cm_lid == 2)) {
		snprintf(cm_dev, sizeof(cm_dev), "/dev/ttyCm%dIPMI", cm_lid);
		rc = process_firmware_update(cm_lid, cm_dev, image_data, strlen(image_data));
	} else {
		HTTPD_ERR("invalid cm loc id\n");
		json_free(req);
		libwrap_set_firmware_update_status(1);
		return NULL;
	}

	if (rc == 0) {
		json_free(req);
		json_object_add(result, RMM_JSON_FRU_RESULT, json_string(RMM_JSON_UPDATING));
		update_response_info(param, HTTP_ACCEPTED);
		return result;
	} else {
		libwrap_set_firmware_update_status(1);
	}

	json_free(req);

	int8 buff[128] = {};
	snprintf(buff, sizeof(buff), "%d", idx);
	rf_snmp_evt(INFO, MSGMbpChange, buff, RMM_JSON_UPDATE_BIG);
	return NULL;
}
static json_t *mbp_get(struct rest_uri_param *param)
{
	int32 idx = 0;
	result_t rs = RESULT_OK;
	json_t *result = NULL;
	json_t *fru = NULL;
	json_t *av_action = NULL;
	json_t *rs_json = NULL;
	json_t *cap = NULL;
	mbp_member_t mbp_member = { {0} };
	json_t *loc = NULL;

	idx = get_asset_idx(param, "mbp_id", MC_TYPE_CM);
	if (idx == -1) {
		HTTPD_ERR("get mbp index fail\n");
		return NULL;
	}

	rs = libwrap_get_mbp_by_idx(idx, &mbp_member);
	if (rs != RESULT_OK) {
		HTTPD_ERR("get mbp 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;
	}

	pack_mbp_basic_info(&mbp_member, result);

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

    add_loc_info(loc, (int32)mbp_member.loc.units, RMM_JSON_RACK_UNITS);
	add_loc_info(loc, mbp_member.loc.u_location, RMM_JSON_ULOC);
	add_loc_info(loc, mbp_member.loc.u_height, RMM_JSON_UHEIGHT);
	add_loc_info(loc, mbp_member.loc.x_location, RMM_JSON_XLOC);
	json_object_add(result, RMM_JSON_RACK_LOC, loc);

	add_json_string(result, RMM_JSON_ASSET_TAG, mbp_member.asset.asset_tag);

	fru = json_object();
	if (fru != NULL) {
		pack_fru_info(&(mbp_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;
	}

	/* action reset */
	json_t *action = NULL;

	action = json_object();
	if (0 != prepare_get_action(mbp_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();
		/* soft reset */
		property = json_object();
		if ((property != NULL) && (allow_values != NULL)) {
			if (0 != strcmp((int8 *)(mbp_member.av_action[0].cap[0].property), "")) {
				json_object_add(property, RMM_JSON_PROPERTY_NAME, json_string((int8 *)(mbp_member.av_action[0].cap[0].property)));
				json_array_add(allow_values, json_string((int8 *)(mbp_member.av_action[0].cap[0].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);


	/* action SetUartTarget */
	action = NULL;
	action = json_object();
	if (0 != prepare_get_action(mbp_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;

		/* targetIndex */
		property = json_object();
		if (property != NULL) {
			if (0 != strcmp((int8 *)(mbp_member.av_action[1].cap[0].property), ""))
				json_object_add(property, RMM_JSON_PROPERTY_NAME, json_string((int8 *)(mbp_member.av_action[1].cap[0].property)));
			json_array_add(cap, property);
		}
		json_object_add(action, RMM_JSON_CAPABILITIES, cap);
	}
	json_array_add(av_action, action);

	/* action Update */
	action = NULL;
	action = json_object();
	if (prepare_get_action(mbp_member.av_action, action, 2)) {
		HTTPD_ERR("action prepare fail\n");
		return NULL;
	}

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

		property = json_object();
		if (property != NULL) {
			if (0 != strcmp((int8 *)(mbp_member.av_action[2].cap[0].property), ""))
				json_object_add(property, RMM_JSON_PROPERTY_NAME, json_string((int8 *)(mbp_member.av_action[2].cap[0].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 *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;
}