2 * gsm - gsm resource for zavai
4 * Copyright (C) 2009 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("PIN OK");
133 public class GSM: zavai.ScriptMonitorService
135 protected dynamic DBus.Object dbus;
136 public dynamic DBus.Object call;
137 protected GSMActivator activator;
139 public signal void status_changed(string message);
145 activator = new GSMActivator();
146 activator.status_changed += (msg) => { status_changed(msg); };
150 dbus = zavai.registry.sbus.get_object(
151 "org.freedesktop.DBus",
152 "/org/freedesktop/DBus",
153 "org.freedesktop.DBus");
154 dbus.NameOwnerChanged += on_name_owner_changed;
157 /// Request GPS resource
158 public override void start()
162 status_changed("Starting");
166 call = zavai.registry.sbus.get_object(
167 "org.freesmartphone.ogsmd",
168 "/org/freesmartphone/GSM/Device",
169 "org.freesmartphone.GSM.Call");
174 protected void on_name_owner_changed(DBus.Object sender, string name, string oldOwner, string newOwner)
176 zavai.log.debug("NOC " + name + " from " + oldOwner + " to " + newOwner);
177 if (name == "org.freesmartphone.ogsmd" && newOwner != "")
179 status_changed("ogpsd came online");
184 // Release usage of GPS
185 public override void stop()
187 if (!started) return;
192 protected override void cleanup_after_script_stop()
198 public class GPRS: zavai.Service
200 public dynamic DBus.Object device;
204 Object(name: "gsm.gprs");
206 device = zavai.registry.sbus.get_object(
207 "org.freesmartphone.ogsmd",
208 "/org/freesmartphone/GSM/Device",
209 "org.freesmartphone.GSM.PDP");
212 /// Request GPS resource
213 public override void start()
218 device.ActivateContext(
219 zavai.config.gprs_apn,
220 zavai.config.gprs_user,
221 zavai.config.gprs_pass);
222 zavai.log.info("Started GPRS");
224 } catch (GLib.Error e) {
225 zavai.log.error(e.message);
230 // Release usage of GPS
231 public override void stop()
233 if (!started) return;
236 device.DeactivateContext();
237 zavai.log.info("Stopped GPRS");
239 } catch (GLib.Error e) {
240 zavai.log.error(e.message);
246 public zavai.gsm.GSM gsm = null;
247 public zavai.gsm.GPRS gprs = null;