/* * gsm - gsm resource for zavai * * Copyright (C) 2009 Enrico Zini * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ using GLib; namespace zavai { namespace gsm { // Isolate here the insane loops we need to go through to turn on the bloody // gsm protected class GSMActivator : Object { public dynamic DBus.Object device; public dynamic DBus.Object network; public dynamic DBus.Object sim; public GSMActivator() { device = null; network = null; sim = null; } public void begin() { if (device == null) { device = zavai.registry.sbus.get_object( "org.freesmartphone.ogsmd", "/org/freesmartphone/GSM/Device", "org.freesmartphone.GSM.Device"); network = zavai.registry.sbus.get_object( "org.freesmartphone.ogsmd", "/org/freesmartphone/GSM/Device", "org.freesmartphone.GSM.Network"); sim = zavai.registry.sbus.get_object( "org.freesmartphone.ogsmd", "/org/freesmartphone/GSM/Device", "org.freesmartphone.GSM.SIM"); } device.SetAntennaPower(true, on_antenna_power); } protected void on_antenna_power(Error e) { if (e != null) { zavai.log.warning("on_antenna_power: " + e.message); if (e.message.str("current status is 'enabling'") != null || e.message.str("current status is 'unknown'") != null) { zavai.log.info("trying again after 2 seconds"); Timeout.add(2 * 1000, () => { device.SetAntennaPower(true, on_antenna_power); return false; }); } else { sim.GetAuthStatus(on_auth_status); } return; } zavai.log.warning("on_antenna_power ok"); network.Register(on_network_register); } protected void on_network_register(Error e) { if (e != null) { zavai.log.warning("on_network_register: " + e.message); return; } zavai.log.info("on_network_register: registered"); } protected void on_auth_status(string status, Error e) { if (e != null) { zavai.log.warning("on_auth_status: " + e.message); return; } zavai.log.info("on_auth_status: " + status); if (status == "READY") device.SetAntennaPower(true, on_antenna_power); else if (status == "SIM PIN") sim.SendAuthCode(zavai.config.sim_pin, on_auth_code); else zavai.log.debug("Unknown status: " + status); } protected void on_auth_code(Error e) { if (e != null) { zavai.log.warning("on_auth_code: " + e.message); return; } zavai.log.info("PIN OK"); } } public class GSM: zavai.Service { protected dynamic DBus.Object dbus; public dynamic DBus.Object call; protected Pid child_pid; protected int child_watch_id; protected GSMActivator activator; public GSM() { Object(name: "gsm.gsm"); activator = new GSMActivator(); call = null; child_pid = 0; child_watch_id = 0; dbus = zavai.registry.sbus.get_object( "org.freedesktop.DBus", "/org/freedesktop/DBus", "org.freedesktop.DBus"); dbus.NameOwnerChanged += on_name_owner_changed; } /// Request GPS resource public override void start() { if (started) return; string command = zavai.config.homedir + "/gsm pre"; try { // Then run our own script zavai.app.run_script(command); } catch (Error e) { zavai.log.error("Running " + command + ": " + e.message); return; } command = zavai.config.homedir + "/gsm run"; zavai.log.info("Run program: " + command); string[] args = command.split(" "); try { Process.spawn_async( Environment.get_home_dir(), args, null, SpawnFlags.SEARCH_PATH | SpawnFlags.DO_NOT_REAP_CHILD, null, out child_pid); } catch (SpawnError e) { zavai.log.error("Running " + command + ": " + e.message); } // Add a child watch source to know when it ends ChildWatch.add(child_pid, on_child); call = zavai.registry.sbus.get_object( "org.freesmartphone.ogsmd", "/org/freesmartphone/GSM/Device", "org.freesmartphone.GSM.Call"); // try { // device.Enable(); // zavai.log.info("Started GSM"); // base.start(); // } catch (GLib.Error e) { // zavai.log.error(e.message); // } base.start(); } protected void on_name_owner_changed(DBus.Object sender, string name, string oldOwner, string newOwner) { zavai.log.debug("NOC " + name + " from " + oldOwner + " to " + newOwner); if (name == "org.freesmartphone.ogsmd" && newOwner != "") activator.begin(); } // Release usage of GPS public override void stop() { if (!started) return; Posix.kill((Posix.pid_t)child_pid, Posix.SIGTERM); } public void on_child(Pid pid, int status) { zavai.log.info("Exited"); stderr.printf("STATUS %d\n", status); Process.close_pid(pid); // try { // device.Disable(); // zavai.log.info("Stopped GSM"); // base.stop(); // } catch (GLib.Error e) { // zavai.log.error(e.message); // } string command = zavai.config.homedir + "/gsm post"; try { // Then run our own script zavai.app.run_script(command); } catch (Error e) { zavai.log.error("Running " + command + ": " + e.message); return; } call = null; base.stop(); } } public class GPRS: zavai.Service { public dynamic DBus.Object device; public GPRS() { Object(name: "gsm.gprs"); device = zavai.registry.sbus.get_object( "org.freesmartphone.ogsmd", "/org/freesmartphone/GSM/Device", "org.freesmartphone.GSM.PDP"); } /// Request GPS resource public override void start() { if (started) return; try { //gsm.request(name); device.ActivateContext( zavai.config.gprs_apn, zavai.config.gprs_user, zavai.config.gprs_pass); zavai.log.info("Started GPRS"); base.start(); } catch (GLib.Error e) { zavai.log.error(e.message); } base.start(); } // Release usage of GPS public override void stop() { if (!started) return; try { //gsm.release(name); device.DeactivateContext(); zavai.log.info("Stopped GPRS"); base.stop(); } catch (GLib.Error e) { zavai.log.error(e.message); } base.stop(); } } public zavai.gsm.GSM gsm = null; public zavai.gsm.GPRS gprs = null; public void init() { gsm = new GSM(); gprs = new GPRS(); } } }