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