Exemple #1
0
void box_cb(struct evhttp_request *req, struct evbuffer *evb, void *ctx)
{
    double lat, lng, miles, ulat, ulng, llat, llng;
    char *uri, *json;
    struct evkeyvalq args;
    struct json_object *jsobj;
    
    uri = evhttp_decode_uri(req->uri);
    evhttp_parse_query(uri, &args);
    free(uri);
    
    argtof(&args, "lat", &lat, 0);
    argtof(&args, "lng", &lng, 0);
    argtof(&args, "miles", &miles, 0);

    geo_box(lat, lng, miles, &ulat, &ulng, &llat, &llng);
    
    jsobj = json_object_new_object();
    json_object_object_add(jsobj, "ulat", json_object_new_double(ulat));
    json_object_object_add(jsobj, "ulng", json_object_new_double(ulng));
    json_object_object_add(jsobj, "llat", json_object_new_double(llat));
    json_object_object_add(jsobj, "llng", json_object_new_double(llng));

    finalize_json(req, evb, &args, jsobj);
}
//------------------------------------------------------------------------------
// GeoJSON feature builder
//------------------------------------------------------------------------------
void geoJsonFeatBuilder( float lat, float lon, int id, char notlast_feat ) {

    json_object *jgeom = json_object_new_object();
    json_object *jpointobj = json_object_new_object();
    json_object *jprops = json_object_new_object();

    json_object *jpttype = json_object_new_string( "Point" );
    json_object *jfttype = json_object_new_string( "Feature" );
    json_object *jcoords = json_object_new_array();
    json_object *jlat = json_object_new_double( lat );
    json_object *jlon = json_object_new_double( lon );
    json_object *jid = json_object_new_int( id );

    json_object_array_add( jcoords, jlon );
    json_object_array_add( jcoords, jlat);

    json_object_object_add( jpointobj,"coordinates", jcoords );
    json_object_object_add( jpointobj,"type", jpttype );
    json_object_object_add( jgeom,"type", jfttype );
    json_object_object_add( jgeom,"properties", jprops );
    json_object_object_add( jgeom,"geometry", jpointobj );
    json_object_object_add( jprops,"id", jid );

    fprintf(fgeojsn, "%s%c\n", json_object_to_json_string( jgeom ), notlast_feat);

}
Exemple #3
0
static
double processor_setup_time(processor_state_t *self, json_object *request, const char *time_name, const char *duplicate)
{
    // TODO: might be better to drop requests without total_time
    double total_time;
    json_object *total_time_obj = NULL;
    if (json_object_object_get_ex(request, time_name, &total_time_obj)) {
        total_time = json_object_get_double(total_time_obj);
        if (total_time == 0.0) {
            total_time = 1.0;
            total_time_obj = json_object_new_double(total_time);
            json_object_object_add(request, time_name, total_time_obj);
        }
    } else {
        total_time = 1.0;
        total_time_obj = json_object_new_double(total_time);
        json_object_object_add(request, time_name, total_time_obj);
    }
    // printf("[D] %s: %f\n", time_name, total_time);
    if (duplicate) {
        // TODO: check whether we could simply share the object
        json_object_object_add(request, duplicate, json_object_new_double(total_time));
    }
    return total_time;
}
Exemple #4
0
json_object * api_json_tuples(Buffer::Ptr buf) {

	json_object *json_tuples = json_object_new_array();
	Buffer::iterator it;

	print(log_debug, "==> number of tuples: %d", "api", buf->size());

	for (it = buf->begin(); it != buf->end(); it++) {
		struct json_object *json_tuple = json_object_new_array();

		buf->lock();

		// TODO use long int of new json-c version
		// API requires milliseconds => * 1000
		double timestamp = it->tvtod() * 1000; 
		double value = it->value();
		buf->unlock();

		json_object_array_add(json_tuple, json_object_new_double(timestamp));
		json_object_array_add(json_tuple, json_object_new_double(value));

		json_object_array_add(json_tuples, json_tuple);
	}

	return json_tuples;
}
Exemple #5
0
json_object *ipc_json_describe_input(struct libinput_device *device) {
	char* identifier = libinput_dev_unique_id(device);
	int vendor = libinput_device_get_id_vendor(device);
	int product = libinput_device_get_id_product(device);
	const char *name = libinput_device_get_name(device);
	double width = -1, height = -1;
	int has_size = libinput_device_get_size(device, &width, &height);

	json_object *device_object = json_object_new_object();
	json_object_object_add(device_object,"identifier",
			identifier ? json_object_new_string(identifier) : NULL);
	json_object_object_add(device_object,
			"vendor", json_object_new_int(vendor));
	json_object_object_add(device_object,
			"product", json_object_new_int(product));
	json_object_object_add(device_object,
			"name", json_object_new_string(name));
	if (has_size == 0) {
		json_object *size_object = json_object_new_object();
		json_object_object_add(size_object,
				"width", json_object_new_double(width));
		json_object_object_add(size_object,
				"height", json_object_new_double(height));
	} else {
		json_object_object_add(device_object, "size", NULL);
	}

	struct {
		enum libinput_device_capability cap;
		const char *name;
		// If anyone feels like implementing device-specific IPC output,
		// be my guest
		json_object *(*describe)(struct libinput_device *);
	} caps[] = {
		{ LIBINPUT_DEVICE_CAP_KEYBOARD, "keyboard", NULL },
		{ LIBINPUT_DEVICE_CAP_POINTER, "pointer", NULL },
		{ LIBINPUT_DEVICE_CAP_TOUCH, "touch", NULL },
		{ LIBINPUT_DEVICE_CAP_TABLET_TOOL, "tablet_tool", NULL },
		{ LIBINPUT_DEVICE_CAP_TABLET_PAD, "tablet_pad", NULL },
		{ LIBINPUT_DEVICE_CAP_GESTURE, "gesture", NULL },
#ifdef LIBINPUT_DEVICE_CAP_SWITCH // libinput 1.7.0+
		{ LIBINPUT_DEVICE_CAP_SWITCH, "switch", NULL },
#endif		
	};

	json_object *_caps = json_object_new_array();
	for (size_t i = 0; i < sizeof(caps) / sizeof(caps[0]); ++i) {
		if (libinput_device_has_capability(device, caps[i].cap)) {
			json_object_array_add(_caps, json_object_new_string(caps[i].name));
			if (caps[i].describe) {
				json_object *desc = caps[i].describe(device);
				json_object_object_add(device_object, caps[i].name, desc);
			}
		}
	}
	json_object_object_add(device_object, "capabilities", _caps);

	free(identifier);
	return device_object;
}
const char* dwipe_get_info_json( void )
{
	char* tmp;
	json_object* jdwipe = json_object_new_object();
	json_object* jinfo  = json_object_new_object();

	json_object_object_add( jinfo, "entropy", json_object_new_string( "Linux Kernel (urandom)" ) );
	json_object_object_add( jinfo, "prng", json_object_new_string( dwipe_options.prng->label ) );
	json_object_object_add( jinfo, "method", json_object_new_string( dwipe_method_label( dwipe_options.method ) ) );
	json_object_object_add( jinfo, "verify", json_object_new_int( dwipe_options.verify ) );
	json_object_object_add( jinfo, "rounds", json_object_new_int( dwipe_options.rounds ) );
	asprintf(&tmp, "%s", dwipe_runtime ? dwipe_runtime : "" );
	json_object_object_add( jinfo, "runtime", json_object_new_string( tmp ) );
	asprintf(&tmp, "%s", dwipe_remaining ? dwipe_remaining : "" );
	json_object_object_add( jinfo, "remaining", json_object_new_string( tmp ) );
	asprintf(&tmp, "%s", dwipe_loadavg ? dwipe_loadavg : "" );
	json_object_object_add( jinfo, "load_avg", json_object_new_string( tmp ) );
	json_object_object_add( jinfo, "throughput", json_object_new_double( dwipe_throughput ) );
	json_object_object_add( jinfo, "errors", json_object_new_double( dwipe_errors ) );
	json_object_object_add( jinfo, "total_disks", json_object_new_int( dwipe_enumerated ) );
	json_object_object_add( jinfo, "wiping_disks", json_object_new_int( dwipe_selected ) );

	json_object_object_add( jdwipe, "info", jinfo );
	free( tmp );

        return json_object_to_json_string( jdwipe );
}
Exemple #7
0
int CXGooseCap::GSE_SendJson(int cid, int nid, int type, const u_char *data)
{
	//printf("cid = %d nid = %d type = %d\n", cid, nid, type);
	//return 0;
    json_object *alm_body = json_object_new_object();
    json_object_object_add(alm_body, (char *)"cmd", json_object_new_int(103));
    json_object *alm_json = json_object_new_object();
    json_object_object_add(alm_json, (char *)"cid", json_object_new_int(cid));
    json_object_object_add(alm_json, (char *)"node", json_object_new_int(nid));
	json_object_object_add(alm_json, (char *)"type", json_object_new_int(11));
	json_object_object_add(alm_json, (char *)"ext", json_object_new_int(0));
	json_object *alm_values = json_object_new_object();
    switch (type)
    {
        case GSE_TAG_BOOL:
			json_object_object_add(alm_values, (char *)"val1", json_object_new_double(2.0));
			json_object_object_add(alm_values, (char *)"val2", json_object_new_double((double)data[0]));
            break;
		case GSE_TAG_DBPOS:
			json_object_object_add(alm_values, (char *)"val1", json_object_new_double(1.0));
			json_object_object_add(alm_values, (char *)"val2", json_object_new_double((double)(data[0] >> 6)));
			break;
        default:
            //assert(false);
            break;
    }
    json_object_object_add(alm_body, (char *)"parm", alm_json);
	json_object_object_add(alm_json, (char *)"values", alm_values);
	if (cid == 1338 && (nid == 1 || nid == 23))
		printf("%s\n", json_object_to_json_string(alm_body));
    
	m_httpHelper.SetUri(GetMCUrl());
	//printf("%s\n", GetMCUrl());
	m_httpHelper.SetType(x_http_type_post);
	m_httpHelper.SetBody(json_object_to_json_string(alm_body), strlen(json_object_to_json_string(alm_body)));
	m_httpHelper.Prepare();
	if (m_httpHelper.Process() != J_OK)
	{
	    //continue;
		printf("%s\n", "error");
	}

    int i_state = m_httpHelper.GetStatusCode();
	switch(i_state)
	{
	case 200:
        break;
	default:
        //assert(false);
		break;
	}
    json_object_put(alm_body);
    return J_OK;
}
Exemple #8
0
void GeoHttpTask::ProcessGeoPoint ( UriSplit& urisplit,
                                    CHttpServerResponse& response, std::string& rescontent )
{
    if ( strcasecmp ( "save", urisplit.GetPath() [1].c_str() ) == 0 ) {
        if ( urisplit.ParamExist ( "id" ) && urisplit.ParamExist ( "lat" )
                && urisplit.ParamExist ( "lng" ) ) {
            int id = atoi ( urisplit.GetParam ( "id" ).c_str() );
            float lat = atof ( urisplit.GetParam ( "lat" ).c_str() );
            float lng = atof ( urisplit.GetParam ( "lng" ).c_str() );
            CBackDb::Instanse().AddPoint ( id, lat, lng );
            response.Message ( 200 );
            rescontent = "insert ok";
        } else {
            response.Message ( 500 );
            rescontent = "not enough param";
        }
    } else if ( strcasecmp ( "area", urisplit.GetPath() [1].c_str() ) == 0 ) {
        float lat1 = atof ( urisplit.GetParam ( "lat1" ).c_str() );
        float lng1 = atof ( urisplit.GetParam ( "lng1" ).c_str() );
        float lat2 = atof ( urisplit.GetParam ( "lat2" ).c_str() );
        float lng2 = atof ( urisplit.GetParam ( "lng2" ).c_str() );
        std::vector<GeoPoint> list;

        CBackDb::Instanse().ReadAreaPoint ( MIN ( lat1,lat2 ), MAX ( lat1,lat2 ),
                                            MIN ( lng1,lng2 ), MAX ( lng1,lng2 ), list );

        json_object* root = json_object_new_object();
        json_object* data = json_object_new_array();
        json_object_object_add ( root, "data", data );
        for ( std::vector<GeoPoint>::iterator i = list.begin();
                i != list.end(); i++ ) {
            json_object* ptvalue = json_object_new_object();
            json_object_array_add ( data, ptvalue );

            json_object* tempvalue;
            tempvalue = json_object_new_int ( i->id );
            json_object_object_add ( ptvalue, "id", tempvalue );
            tempvalue = json_object_new_double ( i->lat );
            json_object_object_add ( ptvalue, "lat", tempvalue );
            tempvalue = json_object_new_double ( i->lng );
            json_object_object_add ( ptvalue, "lng", tempvalue );
        }
        rescontent = json_object_to_json_string_ext ( root,JSON_C_TO_STRING_PLAIN );
        json_object_put ( root );

    } else {
        response.Message ( 404 );
    }
}
Exemple #9
0
/**
 * @ingroup VuoPoint2d
 * Encodes @c value as a JSON object.
 */
json_object * VuoPoint3d_getJson(const VuoPoint3d value)
{
	json_object *js = json_object_new_object();

	json_object *xObject = json_object_new_double(value.x);
	json_object_object_add(js, "x", xObject);

	json_object *yObject = json_object_new_double(value.y);
	json_object_object_add(js, "y", yObject);

	json_object *zObject = json_object_new_double(value.z);
	json_object_object_add(js, "z", zObject);

	return js;
}
Exemple #10
0
int cli_jsondouble(json_object *obj, const char* key, double d)
{
    json_type objty;
    json_object *fpobj;
    if (NULL == obj) {
        cli_dbgmsg("json: no parent object specified to cli_jsondouble\n");
        return CL_ENULLARG;
    }
    objty = json_object_get_type(obj);

    if (objty == json_type_object) {
        if (NULL == key) {
            cli_dbgmsg("json: null string specified as key to cli_jsondouble\n");
            return CL_ENULLARG;
        }
    }
    else if (objty != json_type_array) {
        return CL_EARG;
    }

    fpobj = json_object_new_double(d);
    if (NULL == fpobj) {
        cli_errmsg("json: no memory for json double object.\n");
        return CL_EMEM;
    }

    if (objty == json_type_object)
        json_object_object_add(obj, key, fpobj);
    else if (objty == json_type_array)
        json_object_array_add(obj, fpobj);

    return CL_SUCCESS;
}
Exemple #11
0
json_object* json_object_new_gme_double(double dfVal)

{
    json_object* pjoD = json_object_new_double(dfVal);
    json_object_set_serializer(pjoD, json_gme_double_to_string, NULL, NULL );

    return pjoD;
}
json_object* json_object_new_double_with_precision(double dfVal,
                                                   int nCoordPrecision)
{
    json_object* jso = json_object_new_double(dfVal);
    json_object_set_serializer(jso, OGR_json_double_with_precision_to_string,
                               (void*)(size_t)nCoordPrecision, NULL );
    return jso;
}
Exemple #13
0
static struct json_object * get_rootfs_usage(void) {
	struct statfs s;
	if (statfs("/", &s))
		return NULL;

	struct json_object *jso = json_object_new_double(1 - (double)s.f_bfree / s.f_blocks);
	json_object_set_serializer(jso, json_object_double_to_json_string, "%.4f", NULL);
	return jso;
}
Exemple #14
0
static void add_uptime(struct json_object *obj) {
	FILE *f = fopen("/proc/uptime", "r");
	struct json_object* jso;
	if (!f)
		return;

	double uptime, idletime;
	if (fscanf(f, "%lf %lf", &uptime, &idletime) == 2) {
		jso = json_object_new_double(uptime);
		json_object_set_serializer(jso, json_object_double_to_json_string, "%.2f", NULL);
		json_object_object_add(obj, "uptime", jso);
		jso = json_object_new_double(idletime);
		json_object_set_serializer(jso, json_object_double_to_json_string, "%.2f", NULL);
		json_object_object_add(obj, "idletime", jso);
	}

	fclose(f);
}
Exemple #15
0
json_object * vz::api::Volkszaehler::api_json_tuples(Buffer::Ptr buf) {

	json_object *json_tuples = json_object_new_array();
	Buffer::iterator it;

	print(log_debug, "==> number of tuples: %d", channel()->name(), buf->size());
	uint64_t timestamp = 1;

	// copy all values to local buffer queue
	buf->lock();
	for (it = buf->begin(); it != buf->end(); it++) {
		timestamp = round(it->tvtod() * 1000);
		print(log_debug, "compare: %llu %llu %f", channel()->name(), _last_timestamp, timestamp, it->tvtod() * 1000);
		if (_last_timestamp < timestamp ) {
			_values.push_back(*it);
			_last_timestamp = timestamp;
		}
		it->mark_delete();
	}
	buf->unlock();
	buf->clean();

	if (_values.size() < 1 ) {
		return NULL;
	}

	for (it = _values.begin(); it != _values.end(); it++) {
		struct json_object *json_tuple = json_object_new_array();

		// TODO use long int of new json-c version
		// API requires milliseconds => * 1000
		double timestamp = it->tvtod() * 1000;
		double value = it->value();

		json_object_array_add(json_tuple, json_object_new_double(timestamp));
		json_object_array_add(json_tuple, json_object_new_double(value));

		json_object_array_add(json_tuples, json_tuple);
	}

	return json_tuples;
}
Exemple #16
0
static void
json_spit_jobq(eventer_jobq_t *jobq, void *closure) {
  struct json_object *doc = closure;
  struct json_object *jo = json_object_new_object();
  json_object_object_add(jo, "concurrency", json_object_new_int(jobq->concurrency));
  json_object_object_add(jo, "desired_concurrency", json_object_new_int(jobq->desired_concurrency));
  struct json_object *li = json_object_new_int(0);
  json_object_set_int_overflow(li, json_overflow_int64);
  json_object_set_int64(li, (long long int)jobq->total_jobs);
  json_object_object_add(jo, "total_jobs", li);
  json_object_object_add(jo, "backlog", json_object_new_int(jobq->backlog));
  json_object_object_add(jo, "inflight", json_object_new_int(jobq->inflight));
  li = json_object_new_int(0);
  json_object_set_int_overflow(li, json_overflow_int64);
  json_object_set_int64(li, (long long int)jobq->timeouts);
  json_object_object_add(jo, "timeouts", li);
  json_object_object_add(jo, "avg_wait_ms", json_object_new_double((double)jobq->avg_wait_ns/1000000.0));
  json_object_object_add(jo, "avg_run_ms", json_object_new_double((double)jobq->avg_run_ns/1000000.0));
  json_object_object_add(doc, jobq->queue_name, jo);
}
struct json_object* dbus_basic_json(DBusMessageIter *iter)
{
	int arg_type, i;
	dbus_bool_t b;
	double d;
        char *str;
        struct json_object *res;
        DBusMessageIter entry;

	arg_type = dbus_message_iter_get_arg_type(iter);

	switch (arg_type) {
	case DBUS_TYPE_STRING:
	case DBUS_TYPE_OBJECT_PATH:
		dbus_message_iter_get_basic(iter, &str);
                res = json_object_new_string(str);
		break;

	case DBUS_TYPE_VARIANT:
		dbus_message_iter_recurse(iter, &entry);
		res = dbus_to_json(&entry);
		break;

	case DBUS_TYPE_BOOLEAN:
		dbus_message_iter_get_basic(iter, &b);
		if (!b)
                        res = json_object_new_boolean(0);
		else
                        res = json_object_new_boolean(1);
		break;

	case DBUS_TYPE_BYTE:
	case DBUS_TYPE_INT32:
	case DBUS_TYPE_UINT16:
	case DBUS_TYPE_UINT32:
		dbus_message_iter_get_basic(iter, &i);
                res = json_object_new_int((int32_t) i);
		break;

	case DBUS_TYPE_DOUBLE:
		dbus_message_iter_get_basic(iter, &d);
		res = json_object_new_double((double) d);
		break;

	default:
                fprintf(stderr, "Error on type %d(%c) in"
                "__connman_dbus_basic_json\n", arg_type, (char)arg_type);
                res = NULL;
		break;
	}

        return res;
}
Exemple #18
0
json_object
*influxdb_serie_to_json(s_influxdb_series *series)
{
    json_object *jo = json_object_new_object();

    /* Name */
    json_object_object_add(jo, "name", json_object_new_string(series->name));

    /* Columns */
    {
        size_t i;
        json_object *cols = json_object_new_array();

        for (i = 0; i < series->columns_length; i++)
            json_object_array_add(cols,
                                  json_object_new_string(series->columns[i]));

        json_object_object_add(jo, "columns", cols);
    }

    /* Points */
    {
        size_t i, j;
        struct json_object * json_obj;
        json_object *cols = json_object_new_array();

        for (i = 0; i < series->points_length; i++)
        {
            json_object *row = json_object_new_array();

            for (j = 0; j < series->columns_length; j++){
            	switch(series->column_types[j]){
            	case COLUMN_TYPE_INT:
            		json_obj = json_object_new_int(atoi(series->points[i][j]));
            		break;
            	case COLUMN_TYPE_DOUBLE:
            		json_obj = json_object_new_double(atof(series->points[i][j]));
            		break;
            	case COLUMN_TYPE_STRING:
            		json_obj = json_object_new_string(series->points[i][j]);
            	}
            	json_object_array_add(row,json_obj);
            }
            json_object_array_add(cols, row);
        }

        json_object_object_add(jo, "points", cols);
    }

    return jo;
}
Exemple #19
0
static void ipc_json_describe_view(swayc_t *c, json_object *object) {
	json_object *props = json_object_new_object();
	float percent = ipc_json_child_percentage(c);
	const char *layout = (c->parent->type == C_CONTAINER) ?
		ipc_json_layout_description(c->parent->layout) : "none";
	const char *last_layout = (c->parent->type == C_CONTAINER) ?
		ipc_json_layout_description(c->parent->prev_layout) : "none";
	wlc_handle parent = wlc_view_get_parent(c->handle);

	json_object_object_add(object, "type", json_object_new_string((c->is_floating) ? "floating_con" : "con"));

	json_object_object_add(object, "scratchpad_state",
		json_object_new_string(ipc_json_get_scratchpad_state(c)));
	json_object_object_add(object, "percent", (percent > 0) ? json_object_new_double(percent) : NULL);
	// TODO: make urgency actually work once Sway supports it
	json_object_object_add(object, "urgent", json_object_new_boolean(false));
	json_object_object_add(object, "layout",
		(strcmp(layout, "null") == 0) ? NULL : json_object_new_string(layout));
	json_object_object_add(object, "last_split_layout",
		(strcmp(last_layout, "null") == 0) ? NULL : json_object_new_string(last_layout));
	json_object_object_add(object, "workspace_layout",
		json_object_new_string(ipc_json_layout_description(swayc_parent_by_type(c, C_WORKSPACE)->workspace_layout)));

	json_object_object_add(object, "border", json_object_new_string(ipc_json_border_description(c)));
	json_object_object_add(object, "current_border_width", json_object_new_int(c->border_thickness));

	json_object_object_add(object, "rect", ipc_json_create_rect(c));
	json_object_object_add(object, "deco_rect", ipc_json_create_rect_from_geometry(c->title_bar_geometry));
	json_object_object_add(object, "geometry", ipc_json_create_rect_from_geometry(c->cached_geometry));
	json_object_object_add(object, "window_rect", ipc_json_create_rect_from_geometry(c->actual_geometry));

	json_object_object_add(object, "name", (c->name) ? json_object_new_string(c->name) : NULL);

	json_object_object_add(object, "window", json_object_new_int(c->handle)); // for the sake of i3 compat
	json_object_object_add(props, "class", c->class ? json_object_new_string(c->class) :
		c->app_id ? json_object_new_string(c->app_id) : NULL);
	json_object_object_add(props, "instance", c->instance ? json_object_new_string(c->instance) :
		c->app_id ? json_object_new_string(c->app_id) : NULL);
	json_object_object_add(props, "title", (c->name) ? json_object_new_string(c->name) : NULL);
	json_object_object_add(props, "transient_for", parent ? json_object_new_int(parent) : NULL);
	json_object_object_add(object, "window_properties", props);

	json_object_object_add(object, "fullscreen_mode",
		json_object_new_int(swayc_is_fullscreen(c) ? 1 : 0));
	json_object_object_add(object, "sticky", json_object_new_boolean(c->sticky));
	json_object_object_add(object, "floating", json_object_new_string(
		c->is_floating ? "auto_on" : "auto_off")); // we can't state the cause

	json_object_object_add(object, "app_id", c->app_id ? json_object_new_string(c->app_id) : NULL);
}
Exemple #20
0
static
void processor_setup_other_time(processor_state_t *self, json_object *request, double total_time)
{
    double other_time = total_time;
    for (size_t i = 0; i <= last_other_time_resource_index; i++) {
        json_object *time_val;
        if (json_object_object_get_ex(request, other_time_resources[i], &time_val)) {
            double v = json_object_get_double(time_val);
            other_time -= v;
        }
    }
    json_object_object_add(request, "other_time", json_object_new_double(other_time));
    // printf("[D] other_time: %f\n", other_time);
}
/* FIXME error handling */
static struct json_object *respondd_provider_statistics(void) {
	struct ffffm_airtime *a = NULL;
	struct json_object *ret = NULL, *wireless = NULL;

	wireless = json_object_new_object();
	if (!wireless)
		goto end;

	ret = json_object_new_object();
	if (!ret)
		goto end;

        a = ffffm_get_airtime();
        if (!a)
                return NULL;

        struct json_object *v;

	if (a->a24 != FFFFM_INVALID_AIRTIME) {
		v = json_object_new_double(a->a24);
		if (!v)
			goto end;
		json_object_object_add(wireless, "airtime2", v);
	}
	if (a->a50 != FFFFM_INVALID_AIRTIME) {
		v = json_object_new_double(a->a50);
		if (!v)
			goto end;
		json_object_object_add(wireless, "airtime5", v);
	}

	json_object_object_add(ret, "wireless", wireless);

end:
        free(a);
        return ret;
}
Exemple #22
0
iot_json_t *iot_json_add_member(iot_json_t *o, const char *key,
                                iot_json_type_t type, ...)
{
    iot_json_t *m;
    const char *s;
    bool        b;
    int         i, l;
    double      d;
    va_list     ap;

    va_start(ap, type);
    switch (type) {
    case IOT_JSON_STRING:
        s = va_arg(ap, const char *);
        l = va_arg(ap, int);
        if (l < 0)
            m = json_object_new_string(s);
        else
            m = json_object_new_string_len(s, l);
        break;
    case IOT_JSON_BOOLEAN:
        b = va_arg(ap, int);
        m = json_object_new_boolean(b);
        break;
    case IOT_JSON_INTEGER:
        i = va_arg(ap, int);
        m = json_object_new_int(i);
        break;
    case IOT_JSON_DOUBLE:
        d = va_arg(ap, double);
        m = json_object_new_double(d);
        break;
    case IOT_JSON_OBJECT:
        m = json_object_new_object();
        break;
    case IOT_JSON_ARRAY:
        m = json_object_new_array();
        break;
    default:
        m = NULL;
        errno = EINVAL;
    }
    va_end(ap);

    if (m != NULL)
        json_object_object_add(o, key, m);

    return m;
}
Exemple #23
0
char *cws_query2json (char *dbfile, char *stmt)
{
    int i;
    const char *aux;
    char *json;
    
    json_object *root;
    json_object *array;
    sqlite3 *db;
    sqlite3_stmt *pp;
    
    sqlite3_open (dbfile, &db);
    sqlite3_prepare (db, stmt, strlen (stmt), &pp, NULL); 
    array = json_object_new_array ();
    
    while (sqlite3_step (pp) == SQLITE_ROW)
    {
        int ncol = sqlite3_column_count (pp);
        json_object *row = json_object_new_object (); 
        for (i = 0; i < ncol; i++)
        {
            json_object *col = NULL;
            switch (sqlite3_column_type (pp, i))
            {
                case SQLITE_FLOAT :
                    col = json_object_new_double (sqlite3_column_double (pp, i));
                    break;
                case SQLITE_INTEGER : 
                    col = json_object_new_int (sqlite3_column_int (pp, i));
                    break;
                case SQLITE_TEXT : 
                    col = json_object_new_string ((char *) sqlite3_column_text (pp, i));
                    break;
            }
            json_object_object_add (row, sqlite3_column_name (pp, i), col);
        }
        json_object_array_add (array, row);
    }
    sqlite3_finalize (pp);
    sqlite3_close (db);
    root = json_object_new_object ();
    json_object_object_add (root, "records", array);
    aux = json_object_to_json_string (root);
    json = malloc (sizeof (char) * (strlen (aux) + 1));
    strcpy (json, aux);
    json_object_put (root);
    return json;
}
Exemple #24
0
void numeric(double dbl, json_object **jo){

    int intgr = round(dbl);

    if(mxIsNaN(dbl) || mxIsInf(dbl)){
        *jo = NULL;
    }
    else{
        if(fabs(dbl-(double)intgr) > 1e-18){
            *jo = json_object_new_double(dbl);
        }
        else{
            *jo = json_object_new_int(intgr);
        }
    }
}
Exemple #25
0
int main(int argc, char **argv)
{
	json_object *tmp=json_object_new_int(123);
	assert (json_object_get_int(tmp)==123);
	json_object_set_int(tmp,321);
	assert (json_object_get_int(tmp)==321); 
	printf("INT PASSED\n");
	json_object_set_int64(tmp,(int64_t)321321321);
	assert (json_object_get_int64(tmp)==321321321); 
	json_object_put(tmp);
	printf("INT64 PASSED\n");
	tmp=json_object_new_boolean(TRUE);
	assert (json_object_get_boolean(tmp)==TRUE); 
	json_object_set_boolean(tmp,FALSE);
	assert (json_object_get_boolean(tmp)==FALSE); 
	json_object_set_boolean(tmp,TRUE);
	assert (json_object_get_boolean(tmp)==TRUE); 
	json_object_put(tmp);
	printf("BOOL PASSED\n");
	tmp=json_object_new_double(12.34);
	assert (json_object_get_double(tmp)==12.34); 
	json_object_set_double(tmp,34.56);
	assert (json_object_get_double(tmp)==34.56); 
	json_object_set_double(tmp,6435.34);
	assert (json_object_get_double(tmp)==6435.34); 
	json_object_put(tmp);
	printf("DOUBLE PASSED\n");
	#define SHORT "SHORT"
	#define MID   "A MID STRING"
	//             12345678901234567890123456789012....
	#define HUGE  "A string longer than 32 chars as to check non local buf codepath"
	tmp=json_object_new_string(SHORT);
	assert (strcmp(json_object_get_string(tmp),SHORT)==0); 
	json_object_set_string(tmp,MID);
	assert (strcmp(json_object_get_string(tmp),MID)==0); 
	json_object_set_string(tmp,HUGE);
	assert (strcmp(json_object_get_string(tmp),HUGE)==0); 
	json_object_set_string(tmp,SHORT);
	assert (strcmp(json_object_get_string(tmp),SHORT)==0); 
	json_object_put(tmp);
	printf("STRING PASSED\n");
	
	
	printf("PASSED\n");
	return 0;
}
JO calib_result2json(const calib_result&res) {
	// Build json object
	struct json_object *new_obj;
	new_obj = json_object_new_object();		
	
	double est_E_d 	= res.radius_r / res.radius_l; 
	/** XXX ??? */
	double est_E_b 	= res.axle / 0.09;
	json_object_object_add(new_obj, "axle", json_object_new_double(res.axle) );
	json_object_object_add(new_obj, "l_diam", json_object_new_double(res.radius_l*2) );
	json_object_object_add(new_obj, "r_diam", json_object_new_double(res.radius_r*2) );
	json_object_object_add(new_obj, "l_x", json_object_new_double(res.l[0]) );
	json_object_object_add(new_obj, "l_y", json_object_new_double(res.l[1]) );
	json_object_object_add(new_obj, "l_theta", json_object_new_double(res.l[2]) );
	json_object_object_add(new_obj, "E_d", json_object_new_double(est_E_d) );
	json_object_object_add(new_obj, "E_b", json_object_new_double(est_E_b) );
	
	return new_obj;
}
Exemple #27
0
/* begin implementations */
static json_object *jenc_create_object(struct _v *value) {
	json_object *object;
	/* if stored value is string is starting with { and ending with }, 
	   then create a json object from it. */
	if (value->slen == 0){
		object = json_object_new_double(value->val.f);
	}
	else if (value->val.s[0] == '{' && value->val.s[strlen(value->val.s) - 1] == '}') {
		char *parsed_string;
		size_t memsize = 0;
		parsed_string = string_remove_backslashes(value->val.s, &memsize);
		object = json_tokener_parse(parsed_string);
		string_free(parsed_string, &memsize);
	} else {
		object = json_object_new_string(value->val.s);
	}
	return object;
}
Exemple #28
0
iot_json_t *iot_json_create(iot_json_type_t type, ...)
{
    iot_json_t *o;
    const char *s;
    bool        b;
    int         i, l;
    double      d;
    va_list     ap;

    va_start(ap, type);
    switch (type) {
    case IOT_JSON_STRING:
        s = va_arg(ap, const char *);
        l = va_arg(ap, int);
        if (l < 0)
            o = json_object_new_string(s);
        else
            o = json_object_new_string_len(s, l);
        break;
    case IOT_JSON_BOOLEAN:
        b = va_arg(ap, int);
        o = json_object_new_boolean(b);
        break;
    case IOT_JSON_INTEGER:
        i = va_arg(ap, int);
        o = json_object_new_int(i);
        break;
    case IOT_JSON_DOUBLE:
        d = va_arg(ap, double);
        o = json_object_new_double(d);
        break;
    case IOT_JSON_OBJECT:
        o = json_object_new_object();
        break;
    case IOT_JSON_ARRAY:
        o = json_object_new_array();
        break;
    default:
        o = NULL;
    }
    va_end(ap);

    return o;
}
DLL_PUBLIC int result(judgm_manage_info *info,line_result_data *res_data){
    manage_result_info *res_info;
    json_object *jso_item;
    char tpath[PATH_MAX + 1];

    res_info = (manage_result_info*)info->private_data;
    res_info->count++;

    if(res_data->status > res_info->result){
	res_info->result = res_data->status;
    }
    res_info->totalscore += res_data->score;
    res_info->totalruntime += res_data->runtime;
    if(res_data->memory > res_info->maxmemory){
	res_info->maxmemory = res_data->memory;
    }

    jso_item = json_object_new_object();
    json_object_object_add(jso_item,"status",json_object_new_int(res_data->status));
    json_object_object_add(jso_item,"score",json_object_new_double(res_data->score));
    json_object_object_add(jso_item,"runtime",json_object_new_int64(res_data->runtime));
    json_object_object_add(jso_item,"memory",json_object_new_int64(res_data->memory / 1024UL));
    if(strlen(res_data->err_msg) > 0){
	printf("  strlen %d\n",strlen(res_data->err_msg));
	json_object_object_add(jso_item,"errmsg",json_object_new_string(res_data->err_msg));
    }
    json_object_array_put_idx(res_info->jso_resarray,res_data->id - 1,jso_item);
    
    printf("jmod count %d %d\n",res_info->count,res_info->allcount);

    if(res_info->count == res_info->allcount){
	snprintf(tpath,sizeof(tpath),"%s/result",info->res_path);
	json_object_to_file_ext(tpath,res_info->jso_res,JSON_C_TO_STRING_PLAIN);

	info->result = res_info->result;
	info->score = res_info->totalscore;
	info->runtime = res_info->totalruntime;
	info->memory = res_info->maxmemory;
	
	delete res_info;
	return 1;
    }
    return 0;
}
Exemple #30
0
static void add_loadavg(struct json_object *obj) {
	FILE *f = fopen("/proc/loadavg", "r");
	if (!f)
		return;

	double loadavg;
	unsigned proc_running, proc_total;
	if (fscanf(f, "%lf %*f %*f %u/%u", &loadavg, &proc_running, &proc_total) == 3) {
		struct json_object *jso = json_object_new_double(loadavg);
		json_object_set_serializer(jso, json_object_double_to_json_string, "%.2f", NULL);
		json_object_object_add(obj, "loadavg", jso);

		struct json_object *processes = json_object_new_object();
		json_object_object_add(processes, "running", json_object_new_int(proc_running));
		json_object_object_add(processes, "total", json_object_new_int(proc_total));
		json_object_object_add(obj, "processes", processes);
	}

	fclose(f);
}