1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
|
--- src/atd.c-orig 2004-02-02 16:30:39.000000000 +0100
+++ src/atd.c 2010-04-20 19:23:10.000000000 +0200
@@ -7,10 +7,12 @@
* Copyright (C) 1996, Paul Gortmaker
* Copyright (C) 2001, Russell Nelson
* Copyright (C) 2002-2004, Nils Faerber <nils@handhelds.org>
+ * Copyright (C) 2009, Łukasz Pankowski <lukpank@o2.pl>
*
* Released under the GNU General Public License, version 2,
* included herein by reference.
*
+ * Łukasz Pankowski: modified to work over org.freesmartphone.otimed
*/
#include <stdio.h>
@@ -31,6 +33,16 @@
#include <dirent.h>
#include <pwd.h>
#include <signal.h>
+#include <glib.h>
+#include <dbus/dbus-glib.h>
+#include <dbus/dbus-glib-lowlevel.h>
+#include <dbus/dbus.h>
+#include "atd-alarm.h"
+#include "atd-alarm-glue.h"
+
+#define DBUS_NAME "org.openmoko.projects.ffalarms.atd"
+#define DBUS_RECONNECT_TIMEOUT 10
+#define OTIMED "org.freesmartphone.otimed"
/* globals... */
@@ -62,33 +74,23 @@
}
-void waitfor (time_t t)
+void rtc_set_time (void)
{
-int rtcfd, tfd, retval= 0;
-unsigned long data;
+int rtcfd, retval= 0;
struct rtc_time rtc_tm;
-time_t now, then;
+time_t now;
struct tm *tm;
-struct timeval tv;
-int nfds;
-fd_set afds;
#ifdef DEBUG
- printf("waitfor %ld\n", t);
+ printf("rtc_set_time\n");
#endif
rtcfd = open ("/dev/rtc", O_RDONLY);
if (rtcfd == -1) {
+ /* treat it as warning, may be should retry? */
perror("/dev/rtc");
- exit(errno);
- }
-
- /* Read the RTC time/date */
- tfd = open ("trigger", O_RDWR);
-
- if (tfd == -1) {
- perror("trigger");
- exit(errno);
+ errno = 0;
+ return;
}
/* Set the RTC time/date */
@@ -112,112 +114,7 @@
rtc_tm.tm_hour, rtc_tm.tm_min, rtc_tm.tm_sec);
#endif
- tm = gmtime (&t);
-
-#ifdef DEBUG
- printf ("Alarm date/time is %d-%d-%d, %02d:%02d:%02d.\n",
- tm->tm_mday, tm->tm_mon + 1, tm->tm_year + 1900,
- tm->tm_hour, tm->tm_min, tm->tm_sec);
-#endif
- if (t && compare_rtc_to_tm (&rtc_tm, tm) >= 0) {
- close (rtcfd);
- close (tfd);
- return;
- }
-
- if (t) {
- /* set the alarm */
- rtc_tm.tm_mday = tm->tm_mday;
- rtc_tm.tm_mon = tm->tm_mon;
- rtc_tm.tm_year = tm->tm_year;
- rtc_tm.tm_sec = tm->tm_sec;
- rtc_tm.tm_min = tm->tm_min;
- rtc_tm.tm_hour = tm->tm_hour;
- retval = ioctl (rtcfd, RTC_ALM_SET, &rtc_tm);
- if (retval == -1) {
- perror ("ioctl");
- exit (errno);
- }
-#ifdef DEBUG
- printf ("Alarm date/time now set to %d-%d-%d, %02d:%02d:%02d.\n",
- rtc_tm.tm_mday, rtc_tm.tm_mon + 1, rtc_tm.tm_year + 1900,
- rtc_tm.tm_hour, rtc_tm.tm_min, rtc_tm.tm_sec);
-#endif
-
- /* Read the current alarm settings */
- retval = ioctl (rtcfd, RTC_ALM_READ, &rtc_tm);
- if (retval == -1) {
- perror ("ioctl");
- exit (errno);
- }
-
-#ifdef DEBUG
- printf ("Alarm date/time now in RTC: %d-%d-%d, %02d:%02d:%02d.\n",
- rtc_tm.tm_mday, rtc_tm.tm_mon + 1, rtc_tm.tm_year + 1900,
- rtc_tm.tm_hour, rtc_tm.tm_min, rtc_tm.tm_sec);
-#endif
-
- /* Enable alarm interrupts */
- retval = ioctl (rtcfd, RTC_AIE_ON, 0);
- if (retval == -1) {
- perror ("ioctl");
- exit (errno);
- }
- }
-
-#ifdef DEBUG
- printf ("Waiting for alarm...");
- fflush (stdout);
-#endif
- /* This blocks until the alarm ring causes an interrupt */
- FD_ZERO (&afds);
- if (t)
- FD_SET (rtcfd, &afds);
- FD_SET (tfd, &afds);
- nfds = rtcfd+1;
- if (tfd > rtcfd)
- nfds = tfd + 1;
- /* Wait up to ten minutes. */
- tv.tv_sec = 10 * 60;
- tv.tv_usec = 0;
- then = now;
- if (select (nfds, &afds, (fd_set *) 0, (fd_set *) 0, &tv) < 0) {
- if (errno != EINTR)
- perror ("select");
- exit (errno);
- }
- now = time (NULL);
-#ifdef DEBUG
- printf ("While we were sleeping, %d seconds elapsed, but %d alarms passed\n",
- (int)(now - then), (int)(tv.tv_sec));
-#endif
- if (FD_ISSET (rtcfd, &afds)) {
- retval = read (rtcfd, &data, sizeof (unsigned long));
- if (retval == -1) {
- perror ("read");
- exit (errno);
- }
- }
- if (FD_ISSET(tfd, &afds)) {
- retval = read (tfd, &data, 1);
- if (retval == -1) {
- perror ("read");
- exit (errno);
- }
- }
-#ifdef DEBUG
- printf ("Something happened!\n");
-#endif
-
- /* Disable alarm interrupts */
- retval = ioctl (rtcfd, RTC_AIE_OFF, 0);
- if (retval == -1) {
- perror ("ioctl");
- exit (errno);
- }
-
close (rtcfd);
- close (tfd);
}
@@ -345,11 +242,232 @@
}
+G_DEFINE_TYPE(AtdAlarm, atd_alarm, G_TYPE_OBJECT);
+
+
+static void atd_alarm_finalize (GObject* self) {
+ g_object_unref(((AtdAlarm*) self)->alarm_proxy);
+ G_OBJECT_CLASS (atd_alarm_parent_class)->finalize (self);
+}
+
+
+static void atd_alarm_class_init(AtdAlarmClass *cls)
+{
+ G_OBJECT_CLASS (cls)->finalize = atd_alarm_finalize;
+}
+
+
+static void atd_alarm_init(AtdAlarm *self)
+{
+}
+
+
+AtdAlarm* atd_alarm_new (char *spooldir)
+{
+AtdAlarm *self;
+
+ self = g_object_new(ATD_TYPE_ALARM, NULL);
+ if (self) {
+ self->bus = NULL;
+ self->alarm_proxy = NULL;
+ self->trigger = NULL;
+ }
+ return self;
+}
+
+
+gboolean input_on_trigger(GIOChannel *source, GIOCondition condition,
+ AtdAlarm *obj)
+{
+char data;
+
+ if (read (g_io_channel_unix_get_fd(source), &data, 1) == -1) {
+ perror ("read");
+ exit (errno);
+ }
+ atd_alarm_alarm(obj);
+
+ return TRUE;
+}
+
+
+void display_free_g_error(GError **error)
+{
+ if ((*error)->domain == DBUS_GERROR &&
+ (*error)->code == DBUS_GERROR_REMOTE_EXCEPTION)
+ g_printerr("Remote exception: %s: %s\n",
+ dbus_g_error_get_name(*error),
+ (*error)->message);
+ else
+ g_printerr("Error: %s\n", (*error)->message);
+ g_error_free(*error);
+ *error = NULL;
+}
+
+
+static void otimed_destroy(DBusGProxy *alarm_proxy, AtdAlarm *obj)
+{
+#ifdef DEBUG
+ printf("proxy for " OTIMED " destroyed\n");
+#endif
+ g_object_unref(obj->alarm_proxy);
+ obj->alarm_proxy = NULL;
+}
+
+
+void atd_alarm_connect_otimed(AtdAlarm *self)
+{
+GError *error;
+int tfd;
+
+ error = NULL;
+ self->alarm_proxy = dbus_g_proxy_new_for_name_owner
+ (self->bus, OTIMED, "/org/freesmartphone/Time/Alarm",
+ "org.freesmartphone.Time.Alarm", &error);
+ if (error != NULL) {
+ display_free_g_error(&error);
+ return;
+ }
+ g_signal_connect(self->alarm_proxy, "destroy",
+ G_CALLBACK(otimed_destroy), self);
+
+ /* if we are connected to otimed (so we know it is up and
+ * running) we can open the trigger
+ */
+ if (self->trigger)
+ return;
+ if (mkfifo ("trigger.new", 0777) < 0)
+ die ("cannot mkfifo trigger.new");
+ if (rename ("trigger.new","trigger"))
+ die ("cannot rename trigger.new");
+ chmod ("trigger", S_IWUSR | S_IWGRP | S_IWOTH);
+ tfd = open ("trigger", O_RDWR);
+ if (tfd == -1) {
+ perror("trigger");
+ exit(errno);
+ }
+ self->trigger = g_io_channel_unix_new(tfd);
+ g_io_add_watch(self->trigger, G_IO_IN,
+ (GIOFunc) input_on_trigger, self);
+}
+
+
+void atd_alarm_waitfor(AtdAlarm *self, time_t t)
+{
+GError *error1, *error2;
+
+ if (!t)
+ return;
+
+ rtc_set_time();
+ error1 = error2 = NULL;
+ if (self->alarm_proxy == NULL) {
+ g_printerr(OTIMED " not running, could not call"
+ " AddAlarm or SetAlarm\n");
+ } else if (dbus_g_proxy_call(self->alarm_proxy, "AddAlarm", &error1,
+ G_TYPE_STRING, DBUS_NAME,
+ G_TYPE_INT, t, G_TYPE_INVALID,
+ G_TYPE_INVALID) ||
+ dbus_g_proxy_call(self->alarm_proxy, "SetAlarm", &error2,
+ G_TYPE_STRING, DBUS_NAME,
+ G_TYPE_INT, t, G_TYPE_INVALID,
+ G_TYPE_INVALID)) {
+ if (error1 != NULL)
+ g_error_free(error1);
+ } else {
+ display_free_g_error(&error1);
+ display_free_g_error(&error2);
+ }
+#ifdef DEBUG
+ printf("DBus waitfor: %d\n", (int) t);
+#endif
+}
+
+
+void name_owner_changed(DBusGProxy *proxy, const char *name,
+ const char *prev_name, const char *new_name,
+ AtdAlarm *obj)
+{
+ if (!strcmp(name, OTIMED) && *new_name) {
+#ifdef DEBUG
+ printf("DBus NameOwnerChanged: " OTIMED "\n");
+#endif
+ atd_alarm_connect_otimed(obj);
+ atd_alarm_alarm(obj);
+ }
+}
+
+
+static void dbus_disconnected(DBusGProxy *obj, GMainLoop *ml)
+{
+ g_printerr("DBus disconnected\n");
+ g_main_loop_quit(ml);
+}
+
+
+void atd_alarm_start(AtdAlarm *self)
+//int start_atd_alarm(GIOChannel *trigger, int exit_on_error)
+{
+GMainLoop *ml;
+GError *error;
+DBusError dbus_error;
+DBusGProxy *proxy;
+int code;
+
+ error = NULL;
+ self->bus = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
+ if (!self->bus) {
+ g_printerr("failed to connect to DBUS: %s\n",
+ error->message);
+ g_error_free(error);
+ return;
+ }
+ dbus_error_init(&dbus_error);
+ code = dbus_bus_request_name
+ (dbus_g_connection_get_connection(self->bus),
+ DBUS_NAME, DBUS_NAME_FLAG_DO_NOT_QUEUE, &dbus_error);
+ if (code != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
+ if (code == -1)
+ g_printerr("Error: %s\n", dbus_error.message);
+ else
+ g_printerr("could not register name (%d): %s\n",
+ code, DBUS_NAME);
+ exit(1);
+ }
+ dbus_g_connection_register_g_object(self->bus, "/", G_OBJECT(self));
+ proxy = dbus_g_proxy_new_for_name(self->bus, "org.freedesktop.DBus",
+ "/org/freedesktop/DBus",
+ "org.freedesktop.DBus");
+ dbus_g_proxy_add_signal(proxy, "NameOwnerChanged", G_TYPE_STRING,
+ G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INVALID);
+ dbus_g_proxy_connect_signal(proxy, "NameOwnerChanged",
+ G_CALLBACK(name_owner_changed), self, NULL);
+ atd_alarm_connect_otimed(self);
+ atd_alarm_alarm(self);
+
+ ml = g_main_loop_new(NULL, FALSE);
+ g_signal_connect(proxy, "destroy", G_CALLBACK(dbus_disconnected), ml);
+ dbus_connection_set_exit_on_disconnect
+ (dbus_g_connection_get_connection(self->bus), FALSE);
+
+ g_main_loop_run(ml);
+
+ g_object_unref(proxy);
+ if (self->alarm_proxy)
+ g_object_unref(self->alarm_proxy);
+ self->alarm_proxy = NULL;
+ dbus_g_connection_unref(self->bus);
+ self->bus = NULL;
+ g_main_loop_unref(ml);
+
+ return;
+}
+
+
int main (int argc, char *argv[])
{
-struct dirent *dirent;
-unsigned long this, next, now;
char *argv0;
+AtdAlarm *obj;
argv0 = strrchr(argv[0], '/');
if (!argv0)
@@ -362,17 +480,36 @@
die("cannot chdir");
if (!strcmp (argv0, "atq"))
exit_atq();
- if (mkfifo ("trigger.new", 0777) < 0)
- die ("cannot mkfifo trigger.new");
- if (rename ("trigger.new","trigger"))
- die ("cannot rename trigger.new");
- chmod ("trigger", S_IWUSR | S_IWGRP | S_IWOTH);
/* ignore some signals we get from spawned processes */
signal (SIGCHLD, SIG_IGN);
signal (SIGPIPE, SIG_IGN);
+
+ g_type_init();
+ dbus_g_object_type_install_info(ATD_TYPE_ALARM,
+ &dbus_glib_atd_alarm_object_info);
+ obj = atd_alarm_new(argv[1]);
+ if (!obj) {
+ g_printerr("Failed to create AtdAlarm object");
+ exit(1);
+ }
while (1) {
+ atd_alarm_start(obj);
+ sleep(DBUS_RECONNECT_TIMEOUT);
+ }
+}
+
+
+gboolean atd_alarm_alarm(AtdAlarm *self)
+{
+struct dirent *dirent;
+unsigned long this, next, now;
+
+#ifdef DEBUG
+ printf("atd_alarm_alarm() run at: %d\n", (int) time(NULL));
+#endif
+
/* run all the jobs in the past */
now = time (NULL);
scan_from_top ();
@@ -402,8 +539,7 @@
#endif
if (next == ULONG_MAX)
next = 0;
- waitfor (next);
- }
+ atd_alarm_waitfor (self, next);
-return 0;
+ return TRUE;
}
--- /dev/null 2009-09-15 19:51:58.514753360 +0200
+++ src/atd-alarm.h 2009-09-15 22:45:59.000000000 +0200
@@ -0,0 +1,26 @@
+#ifndef ATD_ALARM_H
+#define ATD_ALARM_H
+
+#include <glib-object.h>
+#include <dbus/dbus-glib.h>
+
+#define ATD_TYPE_ALARM (atd_alarm_get_type())
+
+typedef struct _AtdAlarm
+{
+ GObject parent;
+ DBusGConnection *bus;
+ DBusGProxy *alarm_proxy;
+ GIOChannel *trigger;
+} AtdAlarm;
+
+typedef struct _AtdAlarmClass
+{
+ GObjectClass parent_class;
+} AtdAlarmClass;
+
+static gboolean atd_alarm_alarm(AtdAlarm *self);
+
+static void atd_alarm_waitfor(AtdAlarm *self, time_t t);
+
+#endif
--- /dev/null 2009-09-15 19:51:58.514753360 +0200
+++ src/atd-alarm.xml 2009-03-16 17:55:48.000000000 +0100
@@ -0,0 +1,8 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+
+<node name="/">
+ <interface name="org.freesmartphone.Notification">
+ <annotation name="org.freedesktop.DBus.GLib.CSymbol" value="atd_alarm" />
+ <method name="Alarm" />
+ </interface>
+</node>
|