static bool set_mic_mute_cb(LSHandle *handle, LSMessage *message, void *user_data) { struct audio_service *service = user_data; const char *payload; jvalue_ref parsed_obj = NULL; struct luna_service_req_data *req; pa_operation *op; if (!service->context_initialized) { luna_service_message_reply_custom_error(handle, message, "Not yet initialized"); return true; } 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; } service->mic_mute = luna_service_message_get_boolean(parsed_obj, "micMute", service->mic_mute); req = luna_service_req_data_new(handle, message); req->user_data = service; op = pa_context_get_source_info_list(service->context, mm_sourceinfo_cb, req); pa_operation_unref(op); cleanup: if (!jis_null(parsed_obj)) j_release(&parsed_obj); return true; }
bool get_all_properties_cb(LSHandle *handle, LSMessage *message, void *user_data) { jvalue_ref reply_obj = NULL; jvalue_ref props_obj = NULL; struct property_list list; reply_obj = jobject_create(); props_obj = jarray_create(NULL); memset(&list, 0, sizeof(struct property_list)); if (property_list(record_prop, props_obj) < 0) { luna_service_message_reply_error_internal(handle, message); goto cleanup; } 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(reply_obj)) j_release(&reply_obj); if (!jis_null(props_obj)) j_release(&props_obj); return true; }
/** * @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; }
static bool play_feedback_cb(LSHandle *handle, LSMessage *message, void *user_data) { struct audio_service *service = user_data; struct play_feedback_data *pfd; const char *payload; jvalue_ref parsed_obj; char *name, *sink; bool play; if (!service->context_initialized) { luna_service_message_reply_custom_error(handle, message, "Not yet initialized"); return true; } 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; } name = luna_service_message_get_string(parsed_obj, "name", NULL); if (!name) { luna_service_message_reply_custom_error(handle, message, "Invalid parameters: name parameter is required"); goto cleanup; } play = luna_service_message_get_boolean(parsed_obj, "play", true); sink = luna_service_message_get_string(parsed_obj, "sink", NULL); pfd = g_new0(struct play_feedback_data, 1); pfd->service = service; pfd->handle = handle; pfd->message = message; pfd->name = name; pfd->sink = sink; pfd->play = play; LSMessageRef(message); if (!preload_sample(service, pfd)) { luna_service_message_reply_custom_error(handle, message, "Could not preload sample"); LSMessageUnref(message); g_free(pfd); g_free(name); g_free(sink); goto cleanup; } cleanup: if (!jis_null(parsed_obj)) j_release(&parsed_obj); return true; }
/** * @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 set_volume_cb(LSHandle *handle, LSMessage *message, void *user_data) { struct audio_service *service = user_data; const char *payload; jvalue_ref parsed_obj = NULL; jvalue_ref volume_obj = NULL; struct luna_service_req_data *req; int new_volume = 0; if (!service->context_initialized) { luna_service_message_reply_custom_error(handle, message, "Not yet initialized"); return true; } 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("volume"), &volume_obj) || !jis_number(volume_obj)) { luna_service_message_reply_error_bad_json(handle, message); goto cleanup; } jnumber_get_i32(volume_obj, &new_volume); if (new_volume < 0 || new_volume > 100) { luna_service_message_reply_custom_error(handle, message, "Volume out of range. Must be in [0;100]"); goto cleanup; } if (service->new_volume == service->volume) { luna_service_message_reply_custom_error(handle, message, "Provided volume doesn't differ from current one"); goto cleanup; } req = luna_service_req_data_new(handle, message); req->user_data = service; set_volume(service, service->new_volume, req); cleanup: if (!jis_null(parsed_obj)) j_release(&parsed_obj); return true; }
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; }
static bool get_status_cb(LSHandle *handle, LSMessage *message, void *user_data) { struct audio_service *service = user_data; jvalue_ref reply_obj = NULL; bool subscribed = false; if (!service->context_initialized) { luna_service_message_reply_custom_error(handle, message, "Not yet initialized"); return true; } subscribed = luna_service_check_for_subscription_and_process(handle, message); reply_obj = jobject_create(); jobject_put(reply_obj, J_CSTR_TO_JVAL("volume"), jnumber_create_f64(service->volume)); jobject_put(reply_obj, J_CSTR_TO_JVAL("mute"), jboolean_create(service->mute)); jobject_put(reply_obj, J_CSTR_TO_JVAL("inCall"), jboolean_create(service->in_call)); jobject_put(reply_obj, J_CSTR_TO_JVAL("speakerMode"), jboolean_create(service->speaker_mode)); jobject_put(reply_obj, J_CSTR_TO_JVAL("micMute"), jboolean_create(service->mic_mute)); if (subscribed) jobject_put(reply_obj, J_CSTR_TO_JVAL("subscribed"), jboolean_create(true)); 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(reply_obj)) j_release(&reply_obj); return true; }
static int dom_object_start(JSAXContextRef ctxt) { DomInfo *data = getDOMContext(ctxt); jvalue_ref newParent; DomInfo *newChild; CHECK_CONDITION_RETURN_VALUE(data == NULL, 0, "object encountered without any context"); newParent = jobject_create(); newChild = calloc(1, sizeof(DomInfo)); if (UNLIKELY(newChild == NULL || jis_null(newParent))) { PJ_LOG_ERR("Failed to allocate space for new object"); j_release(&newParent); free(newChild); return 0; } newChild->m_prev = data; newChild->m_optInformation = data->m_optInformation; changeDOMContext(ctxt, newChild); if (data->m_prev != NULL) { if (jis_array(data->m_prev->m_value)) { assert(data->m_value == NULL); jarray_append(data->m_prev->m_value, newParent); } else { assert(jis_object(data->m_prev->m_value)); CHECK_CONDITION_RETURN_VALUE(!jis_string(data->m_value), 0, "improper place for a child object"); jobject_put(data->m_prev->m_value, data->m_value, newParent); } } // not using reference counting here on purpose data->m_value = newParent; return 1; }
/** * @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; }
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; }
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; }
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()); } }
bool JValue::isNull() const { return jis_null(m_jval); }
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; }
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; }
jvalue_ref jdom_parse_file(const char *file, JSchemaInfoRef schemaInfo, JFileOptimizationFlags flags) { CHECK_POINTER_RETURN_NULL(file); CHECK_POINTER_RETURN_NULL(schemaInfo); int fd; off_t fileSize; raw_buffer input = { 0 }; jvalue_ref result; char *err_msg; fd = open(file, O_RDONLY); if (fd == -1) { goto errno_parse_failure; } if (!file_size(fd, &fileSize)) { goto errno_parse_failure; } input.m_len = fileSize; if (input.m_len != fileSize) { PJ_LOG_ERR("File too big - currently unsupported by this API"); close(fd); } if (flags & JFileOptMMap) { input.m_str = (char *)mmap(NULL, input.m_len, PROT_READ, MAP_PRIVATE | MAP_NORESERVE, fd, 0); if (input.m_str == NULL || input.m_str == MAP_FAILED) { goto errno_parse_failure; } } else { input.m_str = (char *)malloc(input.m_len + 1); if (input.m_len != read(fd, (char *)input.m_str, input.m_len)) { goto errno_parse_failure; } ((char *)input.m_str)[input.m_len] = 0; } result = jdom_parse(input, DOMOPT_INPUT_OUTLIVES_WITH_NOCHANGE, schemaInfo); return_result: close(fd); if (UNLIKELY(jis_null(result))) { if (input.m_str) { if (flags & JFileOptMMap) { munmap((void *)input.m_str, input.m_len); } else { free((void *)input.m_str); } } } else { result->m_backingBuffer = input; result->m_backingBufferMMap = flags & JFileOptMMap; } return result; errno_parse_failure: err_msg = strdup(strerror(errno)); PJ_LOG_WARN("Attempt to parse json document '%s' failed (%d) : %s", file, errno, err_msg); free(err_msg); result = jnull(); goto return_result; }
/** * @brief Subscribe for a specific group of events * * JSON format: * {"events":"<type>"} **/ bool _service_subscribe_cb(LSHandle *handle, LSMessage *message, void *user_data) { jvalue_ref parsed_obj = NULL; jvalue_ref events_obj = NULL; jvalue_ref reply_obj = NULL; bool result = false; LSError lserror; const char *payload; bool subscribed = false; 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("events"), &events_obj)) { luna_service_message_reply_error_invalid_params(handle, message); goto cleanup; } if (jstring_equal2(events_obj, J_CSTR_TO_BUF("network"))) { result = LSSubscriptionAdd(handle, "/networkStatusQuery", message, &lserror); if (!result) { LSErrorPrint(&lserror, stderr); LSErrorFree(&lserror); luna_service_message_reply_error_internal(handle, message); goto cleanup; } subscribed = true; } else if (jstring_equal2(events_obj, J_CSTR_TO_BUF("signal"))) { result = LSSubscriptionAdd(handle, "/signalStrengthQuery", message, &lserror); if (!result) { LSErrorPrint(&lserror, stderr); LSErrorFree(&lserror); luna_service_message_reply_error_internal(handle, message); goto cleanup; } subscribed = true; } reply_obj = jobject_create(); jobject_put(reply_obj, J_CSTR_TO_JVAL("returnValue"), jboolean_create(true)); jobject_put(reply_obj, J_CSTR_TO_JVAL("errorCode"), jnumber_create_i32(0)); jobject_put(reply_obj, J_CSTR_TO_JVAL("errorText"), jstring_create("success")); jobject_put(reply_obj, J_CSTR_TO_JVAL("subscribed"), jboolean_create(subscribed)); if (!luna_service_message_validate_and_send(handle, message, reply_obj)) { luna_service_message_reply_error_internal(handle, message); goto cleanup; } cleanup: j_release(&parsed_obj); return true; }
bool _service_power_set_cb(LSHandle *handle, LSMessage *message, void *user_data) { struct telephony_service *service = user_data; struct luna_service_req_data *req_data = NULL; bool power = false; jvalue_ref parsed_obj = NULL; jvalue_ref state_obj = NULL; jvalue_ref save_obj = NULL; const char *payload; bool should_save = false; if (!service->initialized) { luna_service_message_reply_custom_error(handle, message, "Backend not initialized"); return true; } if (!service->driver || !service->driver->power_set) { g_warning("No implementation available for service powerSet 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("state"), &state_obj)) { luna_service_message_reply_error_bad_json(handle, message); goto cleanup; } if (jstring_equal2(state_obj, J_CSTR_TO_BUF("on"))) power = true; else if (jstring_equal2(state_obj, J_CSTR_TO_BUF("off"))) power = false; else if (jstring_equal2(state_obj, J_CSTR_TO_BUF("default"))) { power = true; } else { luna_service_message_reply_error_bad_json(handle, message); goto cleanup; } if (jobject_get_exists(parsed_obj, J_CSTR_TO_BUF("save"), &save_obj)) { jboolean_get(save_obj, &should_save); if (should_save) telephony_settings_store(TELEPHONY_SETTINGS_TYPE_POWER_STATE, power ? "{\"state\":true}" : "{\"state\":false}"); } service->power_off_pending = !power; req_data = luna_service_req_data_new(handle, message); req_data->user_data = service; service->driver->power_set(service, power, _service_power_set_finish, req_data); cleanup: if (!jis_null(parsed_obj)) j_release(&parsed_obj); return true; }