Start dbus ruckus after frameworkd comes online
[gregoa/zavai.git] / src / gsm.vala
1 /*
2  * gsm - gsm resource for zavai
3  *
4  * Copyright (C) 2009  Enrico Zini <enrico@enricozini.org>
5  *
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.
10  *
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.
15  *
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
19  */
20
21 using GLib;
22
23 namespace zavai {
24 namespace gsm {
25
26 public class GSM: zavai.Service
27 {
28     public dynamic DBus.Object network;
29     public dynamic DBus.Object call;
30     public dynamic DBus.Object device;
31     public dynamic DBus.Object sim;
32     protected Pid child_pid;
33     protected int child_watch_id;
34
35     public GSM()
36     {
37         Object(name: "gsm.gsm");
38
39         network = null;
40         call = null;
41         device = null;
42         sim = null;
43
44         child_pid = 0;
45         child_watch_id = 0;
46
47         dynamic DBus.Object dbus = zavai.registry.sbus.get_object(
48                 "org.freedesktop.DBus",
49                 "/org/freedesktop/DBus",
50                 "org.freedesktop.DBus");
51         dbus.NameOwnerChanged += on_name_owner_changed;
52     }
53
54     /// Request GPS resource
55     public override void start()
56     {
57         if (started) return;
58
59         string command = zavai.config.homedir + "/gsm run";
60         zavai.log.info("Run program: " + command);
61         string[] args = command.split(" ");
62         try {
63             Process.spawn_async(
64                 Environment.get_home_dir(),
65                 args,
66                 null,
67                 SpawnFlags.SEARCH_PATH | SpawnFlags.DO_NOT_REAP_CHILD,
68                 null,
69                 out child_pid);
70         } catch (SpawnError e) {
71             zavai.log.error("Running " + command + ": " + e.message);
72         }
73
74         // Add a child watch source to know when it ends
75         ChildWatch.add(child_pid, on_child);
76
77         network = zavai.registry.sbus.get_object(
78             "org.freesmartphone.ogpsd", 
79             "/org/freesmartphone/GSM/Device",
80             "org.freesmartphone.GSM.Network");
81         call = zavai.registry.sbus.get_object(
82             "org.freesmartphone.ogpsd", 
83             "/org/freesmartphone/GSM/Device",
84             "org.freesmartphone.GSM.Call");
85         device = zavai.registry.sbus.get_object(
86             "org.freesmartphone.ogpsd", 
87             "/org/freesmartphone/GSM/Device",
88             "org.freesmartphone.GSM.Device");
89         sim = zavai.registry.sbus.get_object(
90             "org.freesmartphone.ogpsd", 
91             "/org/freesmartphone/GSM/Device",
92             "org.freesmartphone.GSM.SIM");
93
94         // try {
95         //     device.Enable();
96         //     zavai.log.info("Started GSM");
97         //     base.start();
98         // } catch (GLib.Error e) {
99         //     zavai.log.error(e.message);
100         // }
101         base.start();
102     }
103
104     protected void on_name_owner_changed(string name, string oldOwner, string newOwner, Error e)
105     {
106         if (e != null)
107         {
108             zavai.log.warning("on_name_owner_changed: error " + e.message);
109             return;
110         }
111         if (name == "org.freesmartphone.ogsmd" && newOwner != "")
112         {
113             // Frameworkd started
114             device.SetAntennaPower(true, on_antenna_power);
115             //                             reply_handler = self.cbAntennaPowerReply,
116             //                             error_handler = self.cbAntennaPowerError)
117         }
118     }
119
120     protected void on_antenna_power(Error e)
121     {
122         if (e != null)
123         {
124             zavai.log.warning("on_antenna_power: " + e.message);
125             // if str(e).find("current status is 'enabling'") >= 0 or str(e).find("current status is 'unknown'") >= 0:
126             //     time.sleep(10)
127             //     self.cbStartAntenna(0)
128             // else:
129             //     self.gsm_sim_iface.GetAuthStatus(reply_handler = self.cbAuthStatusReply,
130             //             error_handler = lambda e: (dbg("cbAuthStatusError %s" % e), self.loop.quit()))
131             return;
132         }
133         zavai.log.warning("on_antenna_power ok");
134         // network.Register(reply_handler = self.cbRegisterReply,
135         //                  error_handler = lambda e: (dbg("cbRegisterError %s" % e), self.loop.quit()))
136     }
137
138     // Release usage of GPS
139     public override void stop()
140     {
141         if (!started) return;
142
143         Posix.kill((Posix.pid_t)child_pid, Posix.SIGTERM);
144     }
145
146     public void on_child(Pid pid, int status)
147     {
148         zavai.log.info("Exited");
149 stderr.printf("STATUS %d\n", status);
150         Process.close_pid(pid);
151
152         // try {
153         //     device.Disable();
154         //     zavai.log.info("Stopped GSM");
155         //     base.stop();
156         // } catch (GLib.Error e) {
157         //     zavai.log.error(e.message);
158         // }
159
160         network = null;
161         call = null;
162         device = null;
163         sim = null;
164
165         base.stop();
166     }
167 }
168
169 public class GPRS: zavai.Service
170 {
171     public dynamic DBus.Object device;
172
173     public GPRS()
174     {
175         Object(name: "gsm.gprs");
176
177         device = zavai.registry.sbus.get_object(
178             "org.freesmartphone.ogsmd", 
179             "/org/freesmartphone/GSM/Device",
180             "org.freesmartphone.GSM.PDP");
181     }
182
183     /// Request GPS resource
184     public override void start()
185     {
186         if (started) return;
187         try {
188             //gsm.request(name);
189             device.ActivateContext(
190                 zavai.config.gprs_apn,
191                 zavai.config.gprs_user,
192                 zavai.config.gprs_pass);
193             zavai.log.info("Started GPRS");
194             base.start();
195         } catch (GLib.Error e) {
196             zavai.log.error(e.message);
197         }
198         base.start();
199     }
200
201     // Release usage of GPS
202     public override void stop()
203     {
204         if (!started) return;
205         try {
206             //gsm.release(name);
207             device.DeactivateContext();
208             zavai.log.info("Stopped GPRS");
209             base.stop();
210         } catch (GLib.Error e) {
211             zavai.log.error(e.message);
212         }
213         base.stop();
214     }
215 }
216
217 public zavai.gsm.GSM gsm = null;
218 public zavai.gsm.GPRS gprs = null;
219
220 public void init()
221 {
222     gsm = new GSM();
223     gprs = new GPRS();
224 }
225
226 }
227 }