Exemplo n.º 1
0
/** 
 * EraseInit
 *
 * @brief Register storaged with luna-service as implementer of several
 * methods.
 */
int
EraseInit(GMainLoop *loop, LSHandle* handle)
{
    LSError lserror;
    LSErrorInit(&lserror);

    if ( !LSRegisterCategory ( handle, "/erase", erase_mthds, 
                NULL, NULL, &lserror) ) 
    {
        LSREPORT( lserror );
    }
    LSErrorFree( &lserror );
    nyxSystem = GetNyxSystemDevice();

    return 0;
}
Exemplo n.º 2
0
static bool
state_shutdown_action(ShutdownEvent *event, ShutdownState *next)
{
    bool retVal =
        LSMessageReply(GetLunaServiceHandle(), shutdown_message,
            "{\"success\":true}", NULL);
    if (!retVal)
    {
        g_critical("%s: Could not send shutdown success message",
                __FUNCTION__);
    }

    if (shutdown_message)
    {
        LSMessageUnref(shutdown_message);
        shutdown_message = NULL;
    }

    nyx_system_set_alarm(GetNyxSystemDevice(),0,NULL,NULL);

    return false;  
}
Exemplo n.º 3
0
/**
* @brief  Set alarm to fire in a fixed time in the future.
*
* luna://com.palm.sleep/time/alarmAdd
*
* Set alarm to expire in T+5hrs. Response will be sent
* as a call to "luna://com.palm.X/alarm".
*
* {"key":"calendarAlarm", "serviceName":"com.palm.X",
*  "relative_time":"05:00:00"}
*
* Subscribing indicates that you want the alarm message as a response to
* the current call.
*
* {"subscribe":true, "key":"calendarAlarm", "serviceName":"com.palm.X",
*  "relative_time":"05:00:00"}
*
* Alarm is sucessfully registered.
* {"alarmId":1}
*
* Alarm failed to be registered:
*
* {"returnValue":false, ...}
* {"returnValue":false,"serivceName":"com.palm.sleep",
*  "errorText":"com.palm.sleep is not running"}
*
* @param  sh
* @param  message
* @param  ctx
*
* @retval
*/
static bool
alarmAdd(LSHandle *sh, LSMessage *message, void *ctx)
{
	time_t alarm_time = 0;
	int rel_hour, rel_min, rel_sec;

	const char *key, *serviceName, *applicationName, *rel_time;
	bool subscribe;

	struct json_object *object;

	int alarm_id;
	bool retVal = false;

	LSError lserror;
	LSErrorInit(&lserror);
	time_t rtctime = 0;

	object = json_tokener_parse(LSMessageGetPayload(message));

	if (is_error(object))
	{
		goto malformed_json;
	}

	SLEEPDLOG_DEBUG("%s", LSMessageGetPayload(message));

	serviceName = json_object_get_string(
	                  json_object_object_get(object, "serviceName"));

	applicationName = LSMessageGetApplicationID(message);

	key = json_object_get_string(json_object_object_get(object, "key"));

	rel_time = json_object_get_string(
	               json_object_object_get(object, "relative_time"));

	if (!rel_time)
	{
		goto invalid_format;
	}

	if (!ConvertJsonTime(rel_time, &rel_hour, &rel_min, &rel_sec) ||
	        (rel_hour < 0 || rel_hour > 24 || rel_min < 0 || rel_min > 59 || rel_sec < 0 ||
	         rel_sec > 59))
	{
		goto invalid_format;
	}

	nyx_system_query_rtc_time(GetNyxSystemDevice(), &rtctime);

	SLEEPDLOG_DEBUG("alarmAdd(): (%s %s %s) in %s (rtc %ld)", serviceName,
	                applicationName, key, rel_time, rtctime);
	struct json_object *subscribe_json =
	    json_object_object_get(object, "subscribe");

	subscribe = json_object_get_boolean(subscribe_json);


	alarm_time = reference_time();
	alarm_time += rel_sec;
	alarm_time += rel_min * 60;
	alarm_time += rel_hour * 60 * 60;

	retVal = alarm_queue_new(key, false, alarm_time,
	                         serviceName, applicationName, subscribe, message, &alarm_id);

	if (!retVal)
	{
		goto error;
	}

	/*****************
	 * Use new timeout API
	 */
	{
		char *timeout_key = g_strdup_printf("%s-%d", key, alarm_id);
		_AlarmTimeout timeout;
		_timeout_create(&timeout, "com.palm.sleep", timeout_key,
		                "luna://com.palm.sleep/time/internalAlarmFired",
		                "{}",
		                false /*public bus*/,
		                true /*wakeup*/,
		                "" /*activity_id*/,
		                0 /*activity_duration_ms*/,
		                false /*calendar*/,
		                alarm_time);

		retVal = _timeout_set(&timeout);

		g_free(timeout_key);

		if (!retVal)
		{
			goto error;
		}
	}
	/*****************/

	/* Send alarm id of sucessful alarm add. */
	GString *reply = g_string_sized_new(512);
	g_string_append_printf(reply, "{\"alarmId\":%d", alarm_id);

	if (subscribe_json)
	{
		g_string_append_printf(reply, ",\"subscribed\":%s",
		                       subscribe ? "true" : "false");
	}

	g_string_append_printf(reply, "}");

	retVal = LSMessageReply(sh, message, reply->str, &lserror);

	g_string_free(reply, TRUE);
	goto cleanup;
error:
	retVal = LSMessageReply(sh, message, "{\"returnValue\":false,"
	                        "\"errorText\":\"Unknown error\"}", &lserror);
	goto cleanup;
invalid_format:
	retVal = LSMessageReply(sh, message, "{\"returnValue\":false,"
	                        "\"errorText\":\"Invalid format for alarm time.\"}", &lserror);
	goto cleanup;
malformed_json:
	LSMessageReplyErrorBadJSON(sh, message);
	goto cleanup;
cleanup:

	if (!is_error(object))
	{
		json_object_put(object);
	}

	if (!retVal && LSErrorIsSet(&lserror))
	{
		LSErrorPrint(&lserror, stderr);
		LSErrorFree(&lserror);
	}

	return true;
}
Exemplo n.º 4
0
/**
* @brief Sends a "/alarm" message to the service associated with this alarm.
*
* {"alarmId":1,"fired":true,"key":"appkey"}
*
* @param  alarm
*/
static void
fire_alarm(_Alarm *alarm)
{
	bool retVal;
	char buf_alarm[STD_ASCTIME_BUF_SIZE];

	struct tm tm_alarm;
	time_t rtctime = 0;

	gmtime_r(&alarm->expiry, &tm_alarm);
	asctime_r(&tm_alarm, buf_alarm);

	nyx_system_query_rtc_time(GetNyxSystemDevice(), &rtctime);

	SLEEPDLOG_DEBUG("fire_alarm() : Alarm (%s %s %s) fired at %s (rtc %ld)",
	                alarm->serviceName,
	                alarm->applicationName, alarm->key, buf_alarm, rtctime);

	GString *payload = g_string_sized_new(255);
	g_string_append_printf(payload, "{\"alarmId\":%d,\"fired\":true", alarm->id);

	if (alarm->key)
	{
		g_string_append_printf(payload, ",\"key\":\"%s\"", alarm->key);
	}

	if (alarm->applicationName && strcmp(alarm->applicationName, "") != 0)
	{
		g_string_append_printf(payload, ",\"applicationName\":\"%s\"",
		                       alarm->applicationName);
	}

	g_string_append_printf(payload, "}");

	LSError lserror;
	LSErrorInit(&lserror);

	if (alarm->serviceName && strcmp(alarm->serviceName, "") != 0)
	{
		char *uri = g_strdup_printf("luna://%s/alarm", alarm->serviceName);
		retVal = LSCall(GetLunaServiceHandle(), uri, payload->str,
		                NULL, NULL, NULL, &lserror);

		if (!retVal)
		{
			LSErrorPrint(&lserror, stderr);
			LSErrorFree(&lserror);
		}

		g_free(uri);
	}

	if (alarm->message)
	{
		retVal = LSMessageReply(GetLunaServiceHandle(), alarm->message,
		                        payload->str, &lserror);

		if (!retVal)
		{
			LSErrorPrint(&lserror, stderr);
			LSErrorFree(&lserror);
		}
	}

	g_string_free(payload, TRUE);
}