2 * clock - clock resource for zavai
4 * Copyright (C) 2009 Enrico Zini <enrico@enricozini.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 public enum SourceType
32 public class Alarm : Object
34 public signal void trigger(Alarm a);
36 public time_t deadline;
39 public Alarm(time_t deadline, string label)
41 this.deadline = deadline;
46 private int alarm_compare(void* a, void* b)
48 return (int)(((Alarm*)a)->deadline - ((Alarm*)b)->deadline);
51 [DBus (name = "org.freesmartphone.Notification")]
52 public class AlarmNotification : Object {
53 public void Alarm () {
58 public class Clock: zavai.Service
60 protected time_t last_gps_time;
61 protected time_t last_gps_time_system_time;
62 protected time_t last_system_time;
63 protected dynamic DBus.Object gps_time;
64 protected uint system_time_timeout;
65 protected time_t last_minute;
66 protected time_t chosen_time;
67 protected SourceType chosen_type;
68 protected AlarmNotification listener;
70 protected dynamic DBus.Object otimed_alarm;
71 protected dynamic DBus.Object rtc;
72 protected SList<Alarm> alarms;
74 // Ticks once a minute
75 public signal void minute_changed(long time, SourceType source);
76 public signal void schedule_changed();
82 listener = new AlarmNotification();
85 last_gps_time_system_time = 0;
86 last_system_time = time_t();
87 chosen_time = last_system_time;
89 gps_time = zavai.registry.sbus.get_object(
90 "org.freesmartphone.ogpsd",
91 "/org/freedesktop/Gypsy",
92 "org.freedesktop.Gypsy.Time");
95 otimed_alarm = zavai.registry.sbus.get_object(
96 "org.freesmartphone.otimed",
97 "/org/freesmartphone/Time/Alarm",
98 "org.freesmartphone.Time.Alarm");
100 rtc = zavai.registry.sbus.get_object(
101 "org.freesmartphone.odeviced",
102 "/org/freesmartphone/Device/RealTimeClock/0",
103 "org.freesmartphone.RealTimeClock");
105 zavai.registry.sbus.register_object("/", listener);
109 public Alarm? next_alarm()
116 public void schedule(Alarm a)
118 alarms.insert_sorted(a, alarm_compare);
122 private void otimed_reschedule()
126 zavai.log.info("Scheduling next alarm: " + alarms.data.label + " at " + Time.local(alarms.data.deadline).to_string());
129 otimed_alarm.ClearAlarm(zavai.registry.bus_name);
131 zavai.log.error("Cannot clear alarms: " + e.message);
134 otimed_alarm.SetAlarm(zavai.registry.bus_name, (int)alarms.data.deadline);
136 zavai.log.error("Cannot reschedule alarms: " + e.message);
139 stderr.printf("Current time: %d, RTC time: %d\n", (int)time_t(), (int)rtc.GetCurrentTime());
140 stderr.printf("Scheduled alarm: %d, RTC wakeup time: %d\n", (int)alarms.data.deadline, (int)rtc.GetWakeupTime());
142 zavai.log.info("No alarms left to reschedule");
146 public void check_alarms()
148 last_system_time = time_t();
150 while (alarms != null && alarms.data.deadline <= chosen_time)
152 Alarm a = alarms.data;
154 zavai.log.info("Triggering " + a.label);
161 private void on_gps_time(dynamic DBus.Object pos, int t)
165 last_gps_time_system_time = 0;
168 last_gps_time = (time_t)t;
169 last_gps_time_system_time = time_t();
174 private bool on_system_time()
176 last_system_time = time_t();
181 private void update_time()
183 if (last_gps_time_system_time + 10 > last_system_time)
185 chosen_time = last_gps_time;
186 chosen_type = SourceType.GPS;
190 chosen_time = last_system_time;
191 chosen_type = SourceType.SYSTEM;
193 if (chosen_time / 60 != last_minute)
195 last_minute = chosen_time / 60;
196 minute_changed(chosen_time, chosen_type);
200 /// Request GPS resource
201 public override void start()
205 system_time_timeout = Timeout.add(5000, on_system_time);
206 gps_time.TimeChanged += on_gps_time;
207 last_system_time = time_t();
213 public override void stop()
215 if (!started) return;
217 Source.remove(system_time_timeout);
218 gps_time.TimeChanged -= on_gps_time;
224 public Clock clock = null;
230 zavai.registry.register_service(clock);