]> ToastFreeware Gitweb - gregoa/zavai.git/blobdiff - src/gsm.vala
Notes about remotising devices
[gregoa/zavai.git] / src / gsm.vala
index 835337371da967cd55e8dfed4153ccabb15b8e64..b11065d0a79511493c222874ff28187847c92aeb 100644 (file)
@@ -23,26 +23,135 @@ using GLib;
 namespace zavai {
 namespace gsm {
 
-public class GSM: zavai.Service
+// Isolate here the insane loops we need to go through to turn on the bloody
+// gsm
+protected class GSMActivator : Object
 {
-    public dynamic DBus.Object network;
-    public dynamic DBus.Object call;
     public dynamic DBus.Object device;
+    public dynamic DBus.Object network;
     public dynamic DBus.Object sim;
-    protected Pid child_pid;
-    protected int child_watch_id;
+
+    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.gsm");
+        Object(name: "gsm");
+
+        activator = new GSMActivator();
+        activator.status_changed += (msg) => { status_changed(msg); };
 
-        network = null;
         call = null;
-        device = null;
-        sim = 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
@@ -50,71 +159,26 @@ public class GSM: zavai.Service
     {
         if (started) return;
 
-        string 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);
-        }
+        status_changed("Starting");
 
-        // Add a child watch source to know when it ends
-        ChildWatch.add(child_pid, on_child);
+        script_start();
 
-        network = zavai.registry.sbus.get_object(
-            "org.freesmartphone.ogpsd", 
-            "/org/freesmartphone/GSM/Device",
-            "org.freesmartphone.GSM.Network");
         call = zavai.registry.sbus.get_object(
-            "org.freesmartphone.ogpsd", 
+            "org.freesmartphone.ogsmd", 
             "/org/freesmartphone/GSM/Device",
             "org.freesmartphone.GSM.Call");
-        device = zavai.registry.sbus.get_object(
-            "org.freesmartphone.ogpsd", 
-            "/org/freesmartphone/GSM/Device",
-            "org.freesmartphone.GSM.Device");
-        sim = zavai.registry.sbus.get_object(
-            "org.freesmartphone.ogpsd", 
-            "/org/freesmartphone/GSM/Device",
-            "org.freesmartphone.GSM.SIM");
 
-        device.SetAntennaPower(true, on_antenna_power);
-        //                             reply_handler = self.cbAntennaPowerReply,
-        //                             error_handler = self.cbAntennaPowerError)
-
-        // try {
-        //     device.Enable();
-        //     zavai.log.info("Started GSM");
-        //     base.start();
-        // } catch (GLib.Error e) {
-        //     zavai.log.error(e.message);
-        // }
         base.start();
     }
 
-    protected void on_antenna_power(Error e)
+    protected void on_name_owner_changed(DBus.Object sender, string name, string oldOwner, string newOwner)
     {
-        if (e != null)
+        zavai.log.debug("NOC " + name + " from " + oldOwner + " to " + newOwner);
+        if (name == "org.freesmartphone.ogsmd" && newOwner != "")
         {
-            zavai.log.warning("on_antenna_power: " + e.message);
-            // if str(e).find("current status is 'enabling'") >= 0 or str(e).find("current status is 'unknown'") >= 0:
-            //     time.sleep(10)
-            //     self.cbStartAntenna(0)
-            // else:
-            //     self.gsm_sim_iface.GetAuthStatus(reply_handler = self.cbAuthStatusReply,
-            //             error_handler = lambda e: (dbg("cbAuthStatusError %s" % e), self.loop.quit()))
-            return;
+            status_changed("ogpsd came online");
+            activator.begin();
         }
-        zavai.log.warning("on_antenna_power ok");
-        // network.Register(reply_handler = self.cbRegisterReply,
-        //                  error_handler = lambda e: (dbg("cbRegisterError %s" % e), self.loop.quit()))
     }
 
     // Release usage of GPS
@@ -122,29 +186,12 @@ public class GSM: zavai.Service
     {
         if (!started) return;
 
-        Posix.kill((Posix.pid_t)child_pid, Posix.SIGTERM);
+        script_stop();
     }
 
-    public void on_child(Pid pid, int status)
+    protected override void cleanup_after_script_stop()
     {
-        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);
-        // }
-
-        network = null;
         call = null;
-        device = null;
-        sim = null;
-
-        base.stop();
     }
 }