Example #1
0
/**
* @brief If message contains subscribe:true, add the message
         to subscription list using the default key '/category/method'.
*
*        This is equivalent to LSSubscriptionAdd(sh, key, message, lserror)
*        where the key is LSMessageGetKind(message).
*
* @param  sh
* @param  message
* @param  subscribed
* @param  lserror
*
* @retval
*/
bool
LSSubscriptionProcess (LSHandle *sh, LSMessage *message, bool *subscribed,
                       LSError *lserror)
{
    JSchemaInfo schemaInfo;
    jschema_info_init(&schemaInfo, jschema_all(), NULL, NULL);

    bool retVal = false;
    bool subscribePayload = false;
    jvalue_ref subObj = NULL;

    const char *payload = LSMessageGetPayload(message);
    jvalue_ref object = jdom_parse(j_cstr_to_buffer(payload), DOMOPT_NOOPT,
                                   &schemaInfo);

    if (jis_null(object))
    {
        _LSErrorSet(lserror, MSGID_LS_INVALID_JSON, -1, "Unable to parse JSON: %s", payload);
        goto exit;
    }

    if (!jobject_get_exists(object, J_CSTR_TO_BUF("subscribe"), &subObj) ||
            subObj == NULL || !jis_boolean(subObj))
    {
        subscribePayload = false;
        /* FIXME: I think retVal should be false, but I don't know if anyone
         * is relying on this behavior. If set to false, make sure to set
         * LSError */
        retVal = true;
    }
    else
    {
        (void)jboolean_get(subObj, &subscribePayload);/* TODO: handle appropriately */
        retVal = true;
    }

    if (subscribePayload)
    {
        const char *key = LSMessageGetKind(message);
        retVal = LSSubscriptionAdd(sh, key, message, lserror);
    }

    if (retVal && subscribePayload)
    {
        *subscribed = true;
    }
    else
    {
        *subscribed = false;
    }

exit:
    j_release(&object);

    return retVal;
}
Example #2
0
const char * jvalue_tostring (jvalue_ref val, const jschema_ref schema)
{
	JSchemaInfo schemainfo;
	JSchemaResolverRef resolver = NULL;
	JErrorCallbacksRef errors = NULL;

	jschema_info_init(&schemainfo, schema, resolver, errors);

	return jvalue_tostring_internal_layer1(val, &schemainfo, true);
}
Example #3
0
const char *jvalue_tostring_simple(jvalue_ref val)
{
	JSchemaInfo schemainfo;
	JSchemaResolverRef resolver = NULL;
	JErrorCallbacksRef errors = NULL;

	jschema_info_init(&schemainfo, jschema_all(), resolver, errors);

	return jvalue_tostring_internal_layer1(val, &schemainfo, false);
}
Example #4
0
JSchemaInfo JParser::prepare(const JSchema& schema, JSchemaResolver& resolver, JErrorCallbacks& cErrCbs, JErrorHandler *errors)
{
	JSchemaInfo schemaInfo;

	jschema_info_init(&schemaInfo,
	                  schema.peek(),
	                  &resolver,
	                  &cErrCbs);

	setErrorHandlers(errors);

	return schemaInfo;
}
Example #5
0
jvalue_ref luna_service_message_parse_and_validate(const char *payload)
{
	jschema_ref input_schema = NULL;
	jvalue_ref parsed_obj = NULL;
	JSchemaInfo schema_info;

	input_schema = jschema_parse(j_cstr_to_buffer("{}"), DOMOPT_NOOPT, NULL);
	jschema_info_init(&schema_info, input_schema, NULL, NULL);

	parsed_obj = jdom_parse(j_cstr_to_buffer(payload), DOMOPT_NOOPT, &schema_info);

	jschema_release(&input_schema);
	if (jis_null(parsed_obj))
		return NULL;

	return parsed_obj;
}
Example #6
0
bool
_CatalogHandleCancel(_Catalog *catalog, LSMessage *cancelMsg,
                     LSError *lserror)
{
    JSchemaInfo schemaInfo;
    jschema_info_init(&schemaInfo, jschema_all(), NULL, NULL);

    const char *sender;
    int token;
    jvalue_ref tokenObj = NULL;

    const char *payload = LSMessageGetPayload(cancelMsg);

    jvalue_ref object = jdom_parse(j_cstr_to_buffer(payload), DOMOPT_NOOPT,
                                   &schemaInfo);
    if (jis_null(object))
    {
        _LSErrorSet(lserror, MSGID_LS_INVALID_JSON, -EINVAL, "Invalid json");
        goto error;
    }

    sender = LSMessageGetSender(cancelMsg);

    if (!jobject_get_exists(object, J_CSTR_TO_BUF("token"), &tokenObj) ||
            tokenObj == NULL || !jis_number(tokenObj))
    {
        _LSErrorSet(lserror, MSGID_LS_INVALID_JSON, -EINVAL, "Invalid json");
        goto error;
    }

    (void)jnumber_get_i32(tokenObj, &token);/* TODO: handle appropriately */

    char *uniqueToken = g_strdup_printf("%s.%d", sender, token);

    _CatalogCallCancelNotifications(catalog, uniqueToken);
    _CatalogRemoveToken(catalog, uniqueToken, true);

    g_free(uniqueToken);
    j_release(&object);
    return true;

error:
    j_release(&object);
    return false;
}
/**
 *******************************************************************************
 * @brief Get the service name from a "ServceStatus" message. The name is
 * allocated and should be freed.
 *
 * @param  message  IN  message
 *
 * @retval name string on success
 * @retval NULL on error
 *******************************************************************************
 */
char*
LSTransportServiceStatusSignalGetServiceName(_LSTransportMessage *message)
{
    JSchemaInfo schemaInfo;
    jschema_info_init(&schemaInfo, jschema_all(), NULL, NULL);

    LS_ASSERT(_LSTransportMessageGetType(message) == _LSTransportMessageTypeServiceDownSignal
              || _LSTransportMessageGetType(message) == _LSTransportMessageTypeServiceUpSignal);

    char *service_name = NULL;
    jvalue_ref service_name_obj = NULL;
    const char *payload = _LSTransportMessageGetPayload(message);

    if (!payload)
    {
        LOG_LS_ERROR(MSGID_LS_INVALID_JSON, 0, "Unable to get payload from message");
        return NULL;
    }

    /* get the serviceName part of the JSON object */
    jvalue_ref payload_json = jdom_parse(j_cstr_to_buffer(payload),
                                         DOMOPT_NOOPT, &schemaInfo);

    bool ret = jobject_get_exists(payload_json,
                                  J_CSTR_TO_BUF(SERVICE_STATUS_SERVICE_NAME),
                                  &service_name_obj);

    if (ret)
    {
        raw_buffer service_name_buf = jstring_get_fast(service_name_obj);
        service_name = g_strndup(service_name_buf.m_str, service_name_buf.m_len);
    }
    else
    {
        LOG_LS_ERROR(MSGID_LS_INVALID_JSON, 0, "Unable to get service name string from payload: %s", payload);
    }

    j_release(&payload_json);

    return service_name;
}
Example #8
0
void TestSchema::validateInput()
{
	QFETCH(raw_buffer, testInput);
	QFETCH(bool, testDOM);

	JSchemaResolver resolver;
	resolver.m_resolve = simpleResolver;
	resolver.m_userCtxt = this;	// in this case, not necessary, but good style

	JSchemaInfo schemaInfo;
	jschema_info_init(&schemaInfo, m_schemaDOM,
			&resolver,
			NULL /* use the default error handler - no recovery possible */);

	const char *dataName = QTest::currentDataTag();
	QByteArray failureInfo(dataName);
	failureInfo += " (must validate... ";
	failureInfo += (m_mustPass ? "yes" : "no");
	failureInfo += "): '" + QByteArray::fromRawData(testInput.m_str, testInput.m_len);

#if BYPASS_SCHEMA
	if (!m_mustPass) {
		QEXPECT_FAIL("", "Schemas disabled - verifying schema rejects invalid input not available", Continue);
	}
#endif

	if (!testDOM) {
		QVERIFY2(jsax_parse_ex(NULL, testInput, &schemaInfo, NULL, true) == m_mustPass, failureInfo.constData());
	} else {
		jvalue_ref parsed = jdom_parse(testInput, DOMOPT_NOOPT, &schemaInfo);
		bool parseOk = !jis_null(parsed);
		j_release(&parsed);
		if (m_mustPass)
			QVERIFY2(parseOk, failureInfo.constData());
		else
			QVERIFY2(!parseOk, failureInfo.constData());
	}
}
static bool handle_set_state_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 wifiObj = {0}, wiredObj = {0};
	gboolean enable_wifi = FALSE, enable_wired = FALSE;
	gboolean invalidArg = TRUE;

	if(jobject_get_exists(parsedObj, J_CSTR_TO_BUF("wifi"), &wifiObj))
	{
		if (jstring_equal2(wifiObj, J_CSTR_TO_BUF("enabled")))
		{
			enable_wifi = TRUE;
		}
		else if (jstring_equal2(wifiObj, J_CSTR_TO_BUF("disabled")))
		{
			enable_wifi = FALSE;
		}
		else
		{
			goto invalid_params;
		}
		/*
		 *  Check if we are enabling an already enabled service,
		 *  or disabling an already disabled service
		 */

		if((enable_wifi && is_wifi_powered()) || (!enable_wifi && !is_wifi_powered()))
		{
			WCA_LOG_DEBUG("Wifi technology already enabled/disabled");
		}
		else
		{
			set_wifi_state(enable_wifi);
		}
		invalidArg = FALSE;
	}

	if(jobject_get_exists(parsedObj, J_CSTR_TO_BUF("wired"), &wiredObj))
	{
		if (jstring_equal2(wiredObj, J_CSTR_TO_BUF("enabled")))
		{
			enable_wired = TRUE;
		}
		else if (jstring_equal2(wiredObj, J_CSTR_TO_BUF("disabled")))
		{
			enable_wired = FALSE;
		}
		else
		{
			goto invalid_params;
		}
		/*
		 *  Check if we are enabling an already enabled service,
		 *  or disabling an already disabled service
		 */
		if((enable_wired && is_ethernet_powered()) || (!enable_wired && !is_ethernet_powered()))
		{
			WCA_LOG_DEBUG("Wired technology already enabled/disabled");
		}
		else
		{
			set_ethernet_state(enable_wired);
		}
		invalidArg = FALSE;
	}
	if(invalidArg == TRUE)
	{
		goto invalid_params;
	}

	LSMessageReplySuccess(sh,message);
	goto cleanup;

invalid_params:
	LSMessageReplyErrorInvalidParams(sh, message);
cleanup:
	j_release(&parsedObj);
	return true;

}
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;
}
static bool handle_set_ipv4_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}, methodObj = {0}, addressObj = {0}, netmaskObj = {0}, gatewayObj = {0};
	ipv4info_t ipv4 = {0};
	gchar *ssid = NULL;
	gboolean invalidArg = TRUE;

	if(jobject_get_exists(parsedObj, J_CSTR_TO_BUF("method"), &methodObj))
	{
		raw_buffer method_buf = jstring_get(methodObj);
		ipv4.method = g_strdup(method_buf.m_str);
		jstring_free_buffer(method_buf);
		invalidArg = FALSE;
	}
	if(jobject_get_exists(parsedObj, J_CSTR_TO_BUF("address"), &addressObj))
	{
		raw_buffer address_buf = jstring_get(addressObj);
		ipv4.address = g_strdup(address_buf.m_str);
		jstring_free_buffer(address_buf);
		invalidArg = FALSE;
	}
	if(jobject_get_exists(parsedObj, J_CSTR_TO_BUF("netmask"), &netmaskObj))
	{
		raw_buffer netmask_buf = jstring_get(netmaskObj);
		ipv4.netmask = g_strdup(netmask_buf.m_str);
		jstring_free_buffer(netmask_buf);
		invalidArg = FALSE;
	}
	if(jobject_get_exists(parsedObj, J_CSTR_TO_BUF("gateway"), &gatewayObj))
	{
		raw_buffer gateway_buf = jstring_get(gatewayObj);
		ipv4.gateway = g_strdup(gateway_buf.m_str);
		jstring_free_buffer(gateway_buf);
		invalidArg = FALSE;
	}
	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);
		invalidArg = FALSE;
	}
	if(invalidArg == TRUE)
	{
		LSMessageReplyErrorInvalidParams(sh, message);
		goto Exit;
	}

	connman_service_t *service = get_connman_service(ssid);
	if(NULL != service)
	{
		if(connman_service_set_ipv4(service, &ipv4))
			LSMessageReplySuccess(sh, message);
		else
			LSMessageReplyErrorUnknown(sh, message);
		goto Exit;
	}
	else
	{
		LSMessageReplyCustomError(sh, message, "Network not found");
	}

Exit:
	g_free(ipv4.method);
	g_free(ipv4.address);
	g_free(ipv4.netmask);
	g_free(ipv4.gateway);
	g_free(ssid);
	j_release(&parsedObj);
	return true;
}
Example #12
0
void TestSchema::testSimpleSchema()
{
	JSchemaInfo schemaInfo;
	jschema_info_init(&schemaInfo, jschema_all(), NULL, NULL);
	QVERIFY(jsax_parse(NULL, J_CSTR_TO_BUF("{}"), &schemaInfo));
}