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