/**
 * @brief Set the network the modem should connect to.
 *
 * JSON format:
 *  request:
 *    {
 *        "automatic": <boolean>,
 *        "id": <string>,
 *    }
 *  response:
 *    {
 *       "returnValue": <boolean>,
 *       "errorCode": <integer>,
 *       "errorString": <string>,
 *    }
 **/
bool _service_network_set_cb(LSHandle *handle, LSMessage *message, void *user_data)
{
	struct telephony_service *service = user_data;
	struct luna_service_req_data *req_data = NULL;
	jvalue_ref parsed_obj = NULL;
	jvalue_ref automatic_obj = NULL;
	jvalue_ref id_obj = NULL;
	const char *payload;
	raw_buffer id_buf;
	const char *id = NULL;
	bool automatic = false;

	if (!service->initialized) {
		luna_service_message_reply_custom_error(handle, message, "Backend not initialized");
		return true;
	}

	if (!service->driver || !service->driver->network_set) {
		g_warning("No implementation available for service networkSet API method");
		luna_service_message_reply_error_not_implemented(handle, message);
		goto cleanup;
	}

	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("automatic"), &automatic_obj)) {
		luna_service_message_reply_error_bad_json(handle, message);
		goto cleanup;
	}

	jboolean_get(automatic_obj, &automatic);

	if (!automatic) {
		if (!jobject_get_exists(parsed_obj, J_CSTR_TO_BUF("id"), &id_obj)) {
			luna_service_message_reply_error_bad_json(handle, message);
			goto cleanup;
		}

		id_buf = jstring_get(id_obj);
		id = id_buf.m_str;
	}

	req_data = luna_service_req_data_new(handle, message);

	service->driver->network_set(service, automatic, id, telephonyservice_common_finish, req_data);

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

	return true;
}
Ejemplo n.º 2
0
char* luna_service_message_get_string(jvalue_ref parsed_obj, const char *name, const char *default_value)
{
	jvalue_ref string_obj = NULL;
	raw_buffer string_buf;

	if (!jobject_get_exists(parsed_obj, j_str_to_buffer(name, strlen(name)), &string_obj) ||
		!jis_string(string_obj))
		return g_strdup(default_value);

	string_buf = jstring_get(string_obj);

	return g_strdup(string_buf.m_str);
}
/**
 * @brief Set the radio access technology mode
 *
 * JSON format:
 *  request:
 *    {
 *        "mode": <string>,
 *    }
 *  response:
 *    {
 *       "returnValue": <boolean>,
 *       "errorCode": <integer>,
 *       "errorString": <string>,
 *    }
 **/
bool _service_rat_set_cb(LSHandle *handle, LSMessage *message, void *user_data)
{
	struct telephony_service *service = user_data;
	struct luna_service_req_data *req_data = NULL;
	jvalue_ref parsed_obj = NULL;
	jvalue_ref mode_obj = NULL;
	const char *payload;
	raw_buffer mode_buf;
	enum telephony_radio_access_mode mode;

	if (!service->initialized) {
		luna_service_message_reply_custom_error(handle, message, "Backend not initialized");
		return true;
	}

	if (!service->driver || !service->driver->rat_set) {
		g_warning("No implementation available for service ratSet API method");
		luna_service_message_reply_error_not_implemented(handle, message);
		goto cleanup;
	}

	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("mode"), &mode_obj)) {
		luna_service_message_reply_error_bad_json(handle, message);
		goto cleanup;
	}

	mode_buf = jstring_get(mode_obj);
	mode = telephony_radio_access_mode_from_string(mode_buf.m_str);

	if (mode < 0) {
		luna_service_message_reply_error_invalid_params(handle, message);
		goto cleanup;
	}

	req_data = luna_service_req_data_new(handle, message);

	service->driver->rat_set(service, mode, telephonyservice_common_finish, req_data);

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

	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;
}
bool service_retrieve_package_info_cb(LSHandle *handle, LSMessage *message, void *user_data)
{
	jvalue_ref reply_obj;
	jvalue_ref parsed_obj;
	jvalue_ref name_obj;
	jvalue_ref depends_obj;
	jvalue_ref recommends_obj;
	jvalue_ref conflicts_obj;
	pkg_t *package = NULL;
	const char *payload = NULL;
	const char *pkgname = NULL;
	raw_buffer name_buf;
	int n;

	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);
		return true;
	}

	if (!jobject_get_exists(parsed_obj, J_CSTR_TO_BUF("name"), &name_obj)) {
		luna_service_message_reply_error_bad_json(handle, message);
		return true;
	}

	if (opkg_new()) {
		luna_service_message_reply_error_internal(handle, message);
		return true;
	}

	name_buf = jstring_get(name_obj);
	package = opkg_find_package(name_buf.m_str, NULL, NULL, NULL);

	if (!package) {
		luna_service_message_reply_custom_error(handle, message, "Package does not exist");
		goto cleanup;
	}

	set_flags_from_control(package);

	reply_obj = jobject_create();
	jobject_put(reply_obj, J_CSTR_TO_JVAL("returnValue"), jboolean_create(true));
	jobject_put(reply_obj, J_CSTR_TO_JVAL("version"), jstring_create(package->version ? package->version : ""));
	jobject_put(reply_obj, J_CSTR_TO_JVAL("revision"), jstring_create(package->revision ? package->revision : ""));
	jobject_put(reply_obj, J_CSTR_TO_JVAL("architecture"), jstring_create(package->architecture ? package->architecture : ""));
	jobject_put(reply_obj, J_CSTR_TO_JVAL("section"), jstring_create(package->section ? package->section : ""));
	jobject_put(reply_obj, J_CSTR_TO_JVAL("maintainer"), jstring_create(package->maintainer ? package->maintainer : ""));
	jobject_put(reply_obj, J_CSTR_TO_JVAL("description"), jstring_create(package->description ? package->description : ""));
	jobject_put(reply_obj, J_CSTR_TO_JVAL("tags"), jstring_create(package->tags ? package->tags : ""));
	jobject_put(reply_obj, J_CSTR_TO_JVAL("size"), jnumber_create_i32(package->size));
	jobject_put(reply_obj, J_CSTR_TO_JVAL("installed_size"), jnumber_create_i32(package->installed_size));
	jobject_put(reply_obj, J_CSTR_TO_JVAL("auto_installed"), jboolean_create(package->auto_installed > 0 ? true : false));

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

cleanup:
	opkg_free();

	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;
}