2 * gsm - gsm resource for zavai
4 * Copyright (C) 2009--2010 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 // Isolate here the insane loops we need to go through to turn on the bloody
28 protected class GSMActivator : Object
30 public dynamic DBus.Object device;
31 public dynamic DBus.Object network;
32 public dynamic DBus.Object sim;
34 public signal void status_changed(string message);
47 device = zavai.registry.sbus.get_object(
48 "org.freesmartphone.ogsmd",
49 "/org/freesmartphone/GSM/Device",
50 "org.freesmartphone.GSM.Device");
51 network = zavai.registry.sbus.get_object(
52 "org.freesmartphone.ogsmd",
53 "/org/freesmartphone/GSM/Device",
54 "org.freesmartphone.GSM.Network");
55 sim = zavai.registry.sbus.get_object(
56 "org.freesmartphone.ogsmd",
57 "/org/freesmartphone/GSM/Device",
58 "org.freesmartphone.GSM.SIM");
61 status_changed("Turning on antenna");
62 device.SetAntennaPower(true, on_antenna_power);
65 protected void on_antenna_power(Error e)
69 zavai.log.warning("on_antenna_power: " + e.message);
70 if (e.message.str("current status is 'enabling'") != null
71 || e.message.str("current status is 'unknown'") != null)
73 status_changed("Waiting for ogsmd to settle");
74 zavai.log.info("trying again after 2 seconds");
75 Timeout.add(2 * 1000, () => {
76 device.SetAntennaPower(true, on_antenna_power);
80 status_changed("Checking if PIN is required");
81 sim.GetAuthStatus(on_auth_status);
85 zavai.log.warning("on_antenna_power ok");
86 status_changed("Registering with network");
87 network.Register(on_network_register);
90 protected void on_network_register(Error e)
94 zavai.log.warning("on_network_register: " + e.message);
97 status_changed("Registered with network");
100 protected void on_auth_status(string status, Error e)
104 zavai.log.warning("on_auth_status: " + e.message);
107 zavai.log.info("on_auth_status: " + status);
108 if (status == "READY")
110 status_changed("PIN ok");
111 device.SetAntennaPower(true, on_antenna_power);
113 else if (status == "SIM PIN")
115 status_changed("Sending PIN");
116 sim.SendAuthCode(zavai.config.sim_pin, on_auth_code);
119 zavai.log.debug("Unknown status: " + status);
122 protected void on_auth_code(Error e)
126 zavai.log.warning("on_auth_code: " + e.message);
129 status_changed("Registering with network");
130 network.Register(on_network_register);
134 public class GSM: zavai.ScriptMonitorService
136 protected dynamic DBus.Object dbus;
137 public dynamic DBus.Object call;
138 protected GSMActivator activator;
140 public signal void status_changed(string message);
146 activator = new GSMActivator();
147 activator.status_changed += (msg) => { status_changed(msg); };
151 dbus = zavai.registry.sbus.get_object(
152 "org.freedesktop.DBus",
153 "/org/freedesktop/DBus",
154 "org.freedesktop.DBus");
155 dbus.NameOwnerChanged += on_name_owner_changed;
158 /// Request GPS resource
159 public override void start()
163 status_changed("Starting");
167 call = zavai.registry.sbus.get_object(
168 "org.freesmartphone.ogsmd",
169 "/org/freesmartphone/GSM/Device",
170 "org.freesmartphone.GSM.Call");
175 protected void on_name_owner_changed(DBus.Object sender, string name, string oldOwner, string newOwner)
177 zavai.log.debug("NOC " + name + " from " + oldOwner + " to " + newOwner);
178 if (name == "org.freesmartphone.ogsmd" && newOwner != "")
180 status_changed("ogpsd came online");
185 // Release usage of GPS
186 public override void stop()
188 if (!started) return;
193 protected override void cleanup_after_script_stop()
199 public class GPRS: zavai.Service
201 public dynamic DBus.Object device;
205 Object(name: "gsm.gprs");
207 device = zavai.registry.sbus.get_object(
208 "org.freesmartphone.ogsmd",
209 "/org/freesmartphone/GSM/Device",
210 "org.freesmartphone.GSM.PDP");
213 /// Request GPS resource
214 public override void start()
219 device.ActivateContext(
220 zavai.config.gprs_apn,
221 zavai.config.gprs_user,
222 zavai.config.gprs_pass);
223 zavai.log.info("Started GPRS");
225 } catch (GLib.Error e) {
226 zavai.log.error(e.message);
231 // Release usage of GPS
232 public override void stop()
234 if (!started) return;
237 device.DeactivateContext();
238 zavai.log.info("Stopped GPRS");
240 } catch (GLib.Error e) {
241 zavai.log.error(e.message);
247 public zavai.gsm.GSM gsm = null;
248 public zavai.gsm.GPRS gprs = null;