Example #1
0
static bool jarray_to_string_append (jvalue_ref jref, JStreamRef generating)
{
	ssize_t i;
	assert(jis_array(jref));

	if (UNLIKELY(!generating)) {
		PJ_LOG_ERR("Cannot append string value to NULL output stream");
		return false;
	}

	SANITY_CHECK_POINTER(jref);
	if (UNLIKELY(jis_null(jref))) {
		PJ_LOG_ERR("INTERNAL ERROR!!!!!!!!!! - used internal API for array --> string for JSON null");
		generating->null_value (generating);
		return false;
	}

	if (UNLIKELY(!generating->a_begin (generating))) {
		PJ_LOG_ERR("Schema validation error, arrays are not allowed");
		return false;
	}
	for (i = 0; i < jarray_size (jref); i++) {
		jvalue_ref toAppend = jarray_get (jref, i);
		SANITY_CHECK_POINTER(toAppend);
		if (UNLIKELY(!jvalue_to_string_append (toAppend, generating))) {
			return false;
		}
	}
	if (UNLIKELY(!generating->a_end (generating))) {
		PJ_LOG_ERR("Schema validation error, array did not validate against schema");
		return false;
	}

	return true;
}
Example #2
0
JValueArrayElement JValue::operator[](int index) const
{
	return JValue(jvalue_copy(jarray_get(m_jval, index)));
}
static bool handle_set_dns_command(LSHandle *sh, LSMessage *message, void* context)
{
	if(!connman_status_check(manager, sh, message))
		return true;

	jvalue_ref parsedObj = {0};
	jschema_ref input_schema = jschema_parse (j_cstr_to_buffer("{}"), DOMOPT_NOOPT, NULL);
	if(!input_schema)
		return false;

	JSchemaInfo schemaInfo;
	jschema_info_init(&schemaInfo, input_schema, NULL, NULL); // no external refs & no error handlers
	parsedObj = jdom_parse(j_cstr_to_buffer(LSMessageGetPayload(message)), DOMOPT_NOOPT, &schemaInfo);
	jschema_release(&input_schema);

	if (jis_null(parsedObj))
	{
		LSMessageReplyErrorBadJSON(sh, message);
		return true;
	}

	jvalue_ref ssidObj = {0}, dnsObj = {0};
	GStrv dns = NULL;
	gchar *ssid = NULL;

	if(jobject_get_exists(parsedObj, J_CSTR_TO_BUF("dns"), &dnsObj))
	{
		int i, dns_arrsize = jarray_size(dnsObj);
		dns = (GStrv) g_new0(GStrv, 1);
		for(i = 0; i < dns_arrsize; i++)
		{
			raw_buffer dns_buf = jstring_get(jarray_get(dnsObj, i));
			dns[i] = g_strdup(dns_buf.m_str);
			jstring_free_buffer(dns_buf);
		}
	}
	else
	{
		LSMessageReplyErrorInvalidParams(sh, message);
		goto Exit;
	}

	if(jobject_get_exists(parsedObj, J_CSTR_TO_BUF("ssid"), &ssidObj))
	{
		raw_buffer ssid_buf = jstring_get(ssidObj);
		ssid = g_strdup(ssid_buf.m_str);
		jstring_free_buffer(ssid_buf);
	}

	connman_service_t *service = get_connman_service(ssid);
	if(NULL != service)
	{
		if(connman_service_set_nameservers(service, dns))
			LSMessageReplySuccess(sh, message);
		else
			LSMessageReplyErrorUnknown(sh, message);
		goto Exit;
	}
	else
		LSMessageReplyCustomError(sh, message, "No connected network");
Exit:
	g_strfreev(dns);
	g_free(ssid);
	j_release(&parsedObj);
	return true;
}
bool get_property_cb(LSHandle *handle, LSMessage *message, void *user_data)
{
	jvalue_ref parsed_obj = NULL;
	jvalue_ref keys_obj = NULL;
	jvalue_ref reply_obj = NULL;
	jvalue_ref props_obj = NULL;
	jvalue_ref prop_obj = NULL;
	char *payload, value[PROP_VALUE_MAX];
	int n;
	raw_buffer key_buf;

	payload = LSMessageGetPayload(message);
	parsed_obj = luna_service_message_parse_and_validate(payload);
	if (jis_null(parsed_obj)) {
		luna_service_message_reply_error_bad_json(handle, message);
		goto cleanup;
	}

	if (!jobject_get_exists(parsed_obj, J_CSTR_TO_BUF("keys"), &keys_obj) ||
		!jis_array(keys_obj)) {
		luna_service_message_reply_error_bad_json(handle, message);
		goto cleanup;
	}

	reply_obj = jobject_create();
	props_obj = jarray_create(NULL);

	for (n = 0; n < jarray_size(keys_obj); n++) {
		jvalue_ref key_obj = jarray_get(keys_obj, n);

		if (!jis_string(key_obj))
			continue;

		key_buf = jstring_get(key_obj);

		if (strlen(key_buf.m_str) == 0)
			continue;

		property_get(key_buf.m_str, value, "");

		prop_obj = jobject_create();
		jobject_put(prop_obj, jstring_create(key_buf.m_str), jstring_create(value));

		jarray_append(props_obj, prop_obj);
	}

	jobject_put(reply_obj, J_CSTR_TO_JVAL("properties"), props_obj);
	jobject_put(reply_obj, J_CSTR_TO_JVAL("returnValue"), jboolean_create(true));

	if (!luna_service_message_validate_and_send(handle, message, reply_obj))
		goto cleanup;

cleanup:
	if (!jis_null(parsed_obj))
		j_release(&parsed_obj);

	if (!jis_null(reply_obj))
		j_release(&reply_obj);

	return true;
}
Example #5
0
static inline bool jsax_parse_inject_internal(JSAXContextRef ctxt, jvalue_ref key, jvalue_ref value)
{
	assert (ctxt != NULL);
	assert (value != NULL);

	yajl_callbacks *cbs = ctxt->m_handlers;
	raw_buffer str;

	if (key) {
		str = jstring_get_fast(key);
		if (UNLIKELY(!cbs->yajl_map_key(ctxt, (const unsigned char *)str.m_str, str.m_len)))
			return false;
	}

	switch (value->m_type) {
		case JV_OBJECT:
		{
			jobject_key_value keyval;
			if (UNLIKELY(!cbs->yajl_start_map(ctxt)))
				return false;

			for (jobject_iter i = jobj_iter_init(value); jobj_iter_is_valid(i); i = jobj_iter_next(i)) {
				jobj_iter_deref(i, &keyval);
				if (UNLIKELY(!jsax_parse_inject(ctxt, keyval.key, keyval.value)))
					return false;
			}

			if (UNLIKELY(!cbs->yajl_end_map(ctxt)))
				return false;
			break;
		}
		case JV_ARRAY:
		{
			jvalue_ref item;

			if (UNLIKELY(!cbs->yajl_start_array(ctxt)))
				return false;

			for (ssize_t i = jarray_size(value) - 1; i >= 0; i--) {
				item = jarray_get(value, i);
				if (UNLIKELY(!jsax_parse_inject(ctxt, NULL, item)))
					return false;
			}

			if (UNLIKELY(!cbs->yajl_end_array(ctxt)))
				return false;
			break;
		}
		case JV_STR:
		{
			str = jstring_get_fast(value);
			if (UNLIKELY(!cbs->yajl_string(ctxt, (const unsigned char *)str.m_str, str.m_len)))
				return false;

			break;
		}
		case JV_NUM:
		{
			assert (value->value.val_num.m_type == NUM_RAW);
			// this numeric string should have come directly from a schema - we don't do any conversion internally.
			// how did this state occur?
			CHECK_CONDITION_RETURN_VALUE(value->value.val_num.m_type != NUM_RAW, false, "Some internal problem parsing schema");

			str = jnumber_deref_raw(value);
			if (UNLIKELY(!cbs->yajl_number(ctxt, str.m_str, str.m_len)))
				return false;

			break;
		}
		case JV_BOOL:
		{
			if (UNLIKELY(!cbs->yajl_boolean(ctxt, jboolean_deref(value))))
				return false;
			break;
		}
		case JV_NULL:
		{
			if (UNLIKELY(!cbs->yajl_null(ctxt)))
				return false;
			break;
		}
		default:
		{
			// how can this occur? - memory corruption?
			PJ_SCHEMA_ERR("Internal error - schema is corrupt");
			return false;
		}
	}
	return true;
}