]> ToastFreeware Gitweb - gregoa/zavai.git/blobdiff - src/gsm.vala
Notes about remotising devices
[gregoa/zavai.git] / src / gsm.vala
index 25ed65d091a9f035cc9eb9bdc497812f5f5340aa..b11065d0a79511493c222874ff28187847c92aeb 100644 (file)
@@ -23,60 +23,233 @@ using GLib;
 namespace zavai {
 namespace gsm {
 
-// For a list of dbus services, look in /etc/dbus-1/system.d/
+// 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 signal void status_changed(string message);
+
+    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");
+        }
+
+        status_changed("Turning on antenna");
+        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)
+            {
+                status_changed("Waiting for ogsmd to settle");
+                zavai.log.info("trying again after 2 seconds");
+                Timeout.add(2 * 1000, () => {
+                    device.SetAntennaPower(true, on_antenna_power);
+                    return false;
+                });
+            } else {
+                status_changed("Checking if PIN is required");
+                sim.GetAuthStatus(on_auth_status);
+            }
+            return;
+        }
+        zavai.log.warning("on_antenna_power ok");
+        status_changed("Registering with network");
+        network.Register(on_network_register);
+    }
+
+    protected void on_network_register(Error e)
+    {
+        if (e != null)
+        {
+            zavai.log.warning("on_network_register: " + e.message);
+            return;
+        }
+        status_changed("Registered with network");
+    }
+
+    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")
+        {
+            status_changed("PIN ok");
+            device.SetAntennaPower(true, on_antenna_power);
+        }
+        else if (status == "SIM PIN")
+        {
+            status_changed("Sending 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;
+        }
+        status_changed("PIN OK");
+    }
+}
+
+public class GSM: zavai.ScriptMonitorService
+{
+    protected dynamic DBus.Object dbus;
+    public dynamic DBus.Object call;
+    protected GSMActivator activator;
+
+    public signal void status_changed(string message);
+
+    public GSM()
+    {
+        Object(name: "gsm");
+
+        activator = new GSMActivator();
+        activator.status_changed += (msg) => { status_changed(msg); };
+
+        call = null;
+
+        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;
+
+        status_changed("Starting");
+
+        script_start();
+
+        call = zavai.registry.sbus.get_object(
+            "org.freesmartphone.ogsmd", 
+            "/org/freesmartphone/GSM/Device",
+            "org.freesmartphone.GSM.Call");
+
+        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 != "")
+        {
+            status_changed("ogpsd came online");
+            activator.begin();
+        }
+    }
+
+    // Release usage of GPS
+    public override void stop()
+    {
+        if (!started) return;
+
+        script_stop();
+    }
+
+    protected override void cleanup_after_script_stop()
+    {
+        call = null;
+    }
+}
+
 public class GPRS: zavai.Service
 {
-       public dynamic DBus.Object device;
-
-       public GPRS()
-       {
-               name = "gsm.gprs";
-
-               device = zavai.registry.sbus.get_object(
-                       "org.freesmartphone.ogpsd", 
-                       "/org/freesmartphone/GSM/Device",
-                       "org.freesmartphone.GSM.PDP");
-       }
-
-       /// Request GPS resource
-       public override void start()
-       {
-               if (started) return;
-               try {
-                       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 {
-                       device.DeactivateContext();
-                       zavai.log.info("Stopped GPRS");
-                       base.stop();
-               } catch (GLib.Error e) {
-                       zavai.log.error(e.message);
-               }
-               base.stop();
-       }
+    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()
 {
-       gprs = new GPRS();
-
-       zavai.registry.register_service(gprs);
+    gsm = new GSM();
+    gprs = new GPRS();
 }
 
 }