static bool shutdownServicesRegister(LSHandle *sh, LSMessage *message, void *user_data) { struct json_object *object = json_tokener_parse(LSMessageGetPayload(message)); if (is_error(object)) goto end; const char *clientId = LSMessageGetUniqueToken(message); const char *clientName = json_object_get_string(json_object_object_get( object, "clientName")); client_new_service(clientId, clientName); bool retVal; LSError lserror; LSErrorInit(&lserror); retVal = LSSubscriptionAdd(sh, "shutdownClient", message, &lserror); if (!retVal) { g_critical("LSSubscriptionAdd failed."); LSErrorPrint(&lserror, stderr); LSErrorFree(&lserror); } send_reply(sh, message, "{\"clientId\":\"%s\"}", clientId); json_object_put(object); end: return true; }
/** * @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; }
/** * @brief Add a new alarm to the queue. * * @param id * @param calendar_time * @param expiry * @param serviceName * @param applicationName * * @retval */ static bool alarm_queue_add(uint32_t id, const char *key, bool calendar_time, time_t expiry, const char *serviceName, const char *applicationName, bool subscribe, LSMessage *message) { _Alarm *alarm = g_new0(_Alarm, 1); alarm->key = g_strdup(key); alarm->id = id; alarm->calendar = calendar_time; alarm->expiry = expiry; alarm->serviceName = g_strdup(serviceName); alarm->applicationName = g_strdup(applicationName); if (subscribe) { LSError lserror; LSErrorInit(&lserror); bool retVal = LSSubscriptionAdd( GetLunaServiceHandle(), "alarm", message, &lserror); if (!retVal) { LSErrorPrint(&lserror, stderr); LSErrorFree(&lserror); goto error; } LSMessageRef(message); alarm->message = message; } alarm_print(alarm); if (alarm->id >= gAlarmQueue->seq_id) { gAlarmQueue->seq_id = alarm->id + 1; } g_sequence_insert_sorted(gAlarmQueue->alarms, alarm, (GCompareDataFunc)alarm_cmp_func, NULL); update_alarms(); return true; error: return false; }
MojErr MojLunaService::enableSubscriptionImpl(MojServiceMessage* msg) { LOG_TRACE("Entering function %s", __FUNCTION__); MojAssert(msg); MojAssertMutexUnlocked(m_mutex); MojString token; MojErr err = token.format(_T("%d"), msg->token()); MojErrCheck(err); MojLunaErr lserr; MojLunaMessage* lsMessage = static_cast<MojLunaMessage*>(msg); LSHandle* handle = LSMessageGetConnection(lsMessage->impl()); bool retVal = LSSubscriptionAdd(handle, token, lsMessage->impl(), lserr); MojLsErrCheck(retVal, lserr); return MojErrNone; }
static bool shutdownServicesRegister(LSHandle *sh, LSMessage *message, void *user_data) { LSMessageRef(message); LSCallOneReply(GetLunaServiceHandle(), SLEEPD_SHUTDOWN_SERVICE"shutdownServicesRegister", LSMessageGetPayload(message), shutdown_method_cb,(void *)message, NULL, NULL); bool retVal; LSError lserror; LSErrorInit(&lserror); retVal = LSSubscriptionAdd(sh, "shutdownClient", message, &lserror); if (!retVal) { g_critical("LSSubscriptionAdd failed."); LSErrorPrint(&lserror, stderr); LSErrorFree(&lserror); } return true; }
/** * @brief Register a new client. */ bool identifyCallback(LSHandle *sh, LSMessage *message, void *data) { LSMessageRef(message); LSCall(GetLunaServiceHandle(), SLEEPD_SUSPEND_SERVICE"identify", LSMessageGetPayload(message), suspend_ipc_method_cb,(void *)message, NULL, NULL); struct json_object *object = json_tokener_parse(LSMessageGetPayload(message)); if ( is_error(object) ) { goto out; } bool subscribe = json_object_get_boolean(json_object_object_get(object, "subscribe")); if (subscribe) { LSSubscriptionAdd(sh, "PowerdClients", message, NULL); } out: return true; }
/** * @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; }