Bring starter back into GSM with an async method
authorEnrico Zini <enrico@enricozini.org>
Fri, 26 Mar 2010 18:33:34 +0000 (18:33 +0000)
committerEnrico Zini <enrico@enricozini.org>
Fri, 26 Mar 2010 18:33:34 +0000 (18:33 +0000)
zavai/gsm.vala

index 34d0fa9cca601bbc2d57025891c26509b1842117..cb43956d391053ba0f2b40520bbca2c5e7dc4578 100644 (file)
@@ -101,88 +101,13 @@ public interface FSO_GSM_Network : GLib.Object {
     public abstract async void get_country_code(out string param0, out string param1) throws DBus.Error;
 }
 
-// Isolate here the insane loops we need to go through to turn on the bloody
-// gsm
-protected class GSMActivator : Object
+public class GSM: zavai.ScriptMonitorService
 {
+    protected dynamic DBus.Object dbus;
     public FSO_GSM_Device device;
     public FSO_GSM_Network network;
     public FSO_GSM_SIM sim;
-
-    public signal void status_changed(string message);
-
-    public GSMActivator()
-    {
-        device = null;
-        network = null;
-        sim = null;
-    }
-
-    public async void start()
-    {
-        if (device == null)
-        {
-            device = (FSO_GSM_Device)zavai.registry.sbus.get_object(
-                    "org.freesmartphone.ogsmd", 
-                    "/org/freesmartphone/GSM/Device",
-                    "org.freesmartphone.GSM.Device");
-            network = (FSO_GSM_Network)zavai.registry.sbus.get_object(
-                "org.freesmartphone.ogsmd", 
-                "/org/freesmartphone/GSM/Device",
-                "org.freesmartphone.GSM.Network");
-            sim = (FSO_GSM_SIM)zavai.registry.sbus.get_object(
-                    "org.freesmartphone.ogsmd", 
-                    "/org/freesmartphone/GSM/Device",
-                    "org.freesmartphone.GSM.SIM");
-        }
-
-        status_changed("Turning on antenna");
-        while (true)
-        {
-            try {
-                yield device.set_antenna_power(true);
-                break;
-            } catch (Error e) {
-                zavai.log.warning("SetAntennaPower: " + 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, () => {
-                        start.callback();
-                        return false;
-                    });
-                    yield;
-                } else {
-                    status_changed("Checking if PIN is required");
-                    string status = yield sim.get_auth_status();
-                    zavai.log.info("on_auth_status: " + status);
-                    if (status == "READY")
-                        status_changed("PIN ok");
-                    else if (status == "SIM PIN")
-                    {
-                        status_changed("Sending PIN");
-                        yield sim.send_auth_code(zavai.config.sim_pin);
-                        status_changed("PIN OK");
-                    }
-                    else
-                        zavai.log.debug("Unknown status: " + status);
-                }
-            }
-        }
-        zavai.log.warning("on_antenna_power ok");
-        status_changed("Registering with network");
-        yield network.register_();
-        status_changed("Registered with network");
-    }
-}
-
-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);
 
@@ -190,9 +115,9 @@ public class GSM: zavai.ScriptMonitorService
     {
         Object(name: "gsm");
 
-        activator = new GSMActivator();
-        activator.status_changed += (msg) => { status_changed(msg); };
-
+        device = null;
+        network = null;
+        sim = null;
         call = null;
 
         dbus = zavai.registry.sbus.get_object(
@@ -211,6 +136,18 @@ public class GSM: zavai.ScriptMonitorService
 
         script_start();
 
+        device = (FSO_GSM_Device)zavai.registry.sbus.get_object(
+                "org.freesmartphone.ogsmd", 
+                "/org/freesmartphone/GSM/Device",
+                "org.freesmartphone.GSM.Device");
+        network = (FSO_GSM_Network)zavai.registry.sbus.get_object(
+            "org.freesmartphone.ogsmd", 
+            "/org/freesmartphone/GSM/Device",
+            "org.freesmartphone.GSM.Network");
+        sim = (FSO_GSM_SIM)zavai.registry.sbus.get_object(
+                "org.freesmartphone.ogsmd", 
+                "/org/freesmartphone/GSM/Device",
+                "org.freesmartphone.GSM.SIM");
         call = zavai.registry.sbus.get_object(
             "org.freesmartphone.ogsmd", 
             "/org/freesmartphone/GSM/Device",
@@ -225,7 +162,7 @@ public class GSM: zavai.ScriptMonitorService
         if (name == "org.freesmartphone.ogsmd" && newOwner != "")
         {
             status_changed("ogpsd came online");
-            activator.start.begin();
+            start_gsm.begin();
         }
     }
 
@@ -235,12 +172,61 @@ public class GSM: zavai.ScriptMonitorService
         if (!started) return;
 
         script_stop();
+
+        status_changed("");
     }
 
     protected override void cleanup_after_script_stop()
     {
         call = null;
     }
+
+    public async void start_gsm()
+    {
+        if (device == null)
+        {
+        }
+
+        status_changed("Turning on antenna");
+        while (true)
+        {
+            try {
+                yield device.set_antenna_power(true);
+                break;
+            } catch (Error e) {
+                zavai.log.warning("SetAntennaPower: " + 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, () => {
+                        start_gsm.callback();
+                        return false;
+                    });
+                    yield;
+                } else {
+                    status_changed("Checking if PIN is required");
+                    string status = yield sim.get_auth_status();
+                    zavai.log.info("on_auth_status: " + status);
+                    if (status == "READY")
+                        status_changed("PIN ok");
+                    else if (status == "SIM PIN")
+                    {
+                        status_changed("Sending PIN");
+                        yield sim.send_auth_code(zavai.config.sim_pin);
+                        status_changed("PIN OK");
+                    }
+                    else
+                        zavai.log.debug("Unknown status: " + status);
+                }
+            }
+        }
+        zavai.log.warning("on_antenna_power ok");
+        status_changed("Registering with network");
+        yield network.register_();
+        status_changed("Registered with network");
+    }
 }
 
 public class GPRS: zavai.Service