Fixed signature
[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(dynamic DBus.Object dbus, string name, string oldOwner, string newOwner)
105     {
106         zavai.log.debug("NOC " + name + " from " + oldOwner + " to " + newOwner);
107         if (name == "org.freesmartphone.ogsmd" && newOwner != "")
108         {
109             // Frameworkd started
110             device.SetAntennaPower(true, on_antenna_power);
111             //                             reply_handler = self.cbAntennaPowerReply,
112             //                             error_handler = self.cbAntennaPowerError)
113         }
114     }
115
116     protected void on_antenna_power(Error e)
117     {
118         if (e != null)
119         {
120             zavai.log.warning("on_antenna_power: " + e.message);
121             // if str(e).find("current status is 'enabling'") >= 0 or str(e).find("current status is 'unknown'") >= 0:
122             //     time.sleep(10)
123             //     self.cbStartAntenna(0)
124             // else:
125             //     self.gsm_sim_iface.GetAuthStatus(reply_handler = self.cbAuthStatusReply,
126             //             error_handler = lambda e: (dbg("cbAuthStatusError %s" % e), self.loop.quit()))
127             return;
128         }
129         zavai.log.warning("on_antenna_power ok");
130         // network.Register(reply_handler = self.cbRegisterReply,
131         //                  error_handler = lambda e: (dbg("cbRegisterError %s" % e), self.loop.quit()))
132     }
133
134     // Release usage of GPS
135     public override void stop()
136     {
137         if (!started) return;
138
139         Posix.kill((Posix.pid_t)child_pid, Posix.SIGTERM);
140     }
141
142     public void on_child(Pid pid, int status)
143     {
144         zavai.log.info("Exited");
145 stderr.printf("STATUS %d\n", status);
146         Process.close_pid(pid);
147
148         // try {
149         //     device.Disable();
150         //     zavai.log.info("Stopped GSM");
151         //     base.stop();
152         // } catch (GLib.Error e) {
153         //     zavai.log.error(e.message);
154         // }
155
156         network = null;
157         call = null;
158         device = null;
159         sim = null;
160
161         base.stop();
162     }
163 }
164
165 public class GPRS: zavai.Service
166 {
167     public dynamic DBus.Object device;
168
169     public GPRS()
170     {
171         Object(name: "gsm.gprs");
172
173         device = zavai.registry.sbus.get_object(
174             "org.freesmartphone.ogsmd", 
175             "/org/freesmartphone/GSM/Device",
176             "org.freesmartphone.GSM.PDP");
177     }
178
179     /// Request GPS resource
180     public override void start()
181     {
182         if (started) return;
183         try {
184             //gsm.request(name);
185             device.ActivateContext(
186                 zavai.config.gprs_apn,
187                 zavai.config.gprs_user,
188                 zavai.config.gprs_pass);
189             zavai.log.info("Started GPRS");
190             base.start();
191         } catch (GLib.Error e) {
192             zavai.log.error(e.message);
193         }
194         base.start();
195     }
196
197     // Release usage of GPS
198     public override void stop()
199     {
200         if (!started) return;
201         try {
202             //gsm.release(name);
203             device.DeactivateContext();
204             zavai.log.info("Stopped GPRS");
205             base.stop();
206         } catch (GLib.Error e) {
207             zavai.log.error(e.message);
208         }
209         base.stop();
210     }
211 }
212
213 public zavai.gsm.GSM gsm = null;
214 public zavai.gsm.GPRS gprs = null;
215
216 public void init()
217 {
218     gsm = new GSM();
219     gprs = new GPRS();
220 }
221
222 }
223 }