Try to log calls
[gregoa/zavai.git] / zavai / gsm.vala
index 34d0fa9cca601bbc2d57025891c26509b1842117..bf9abc0398cbfadb69e913ba6338b2c3d8b5b43c 100644 (file)
@@ -101,41 +101,174 @@ 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
+[DBus (name = "org.freesmartphone.GSM.Call")]
+public interface FSO_GSM_Call : GLib.Object {
+    public abstract async void activate(int index) throws DBus.Error;
+    public abstract async void emergency(string number) throws DBus.Error;
+    public abstract async void hold_active() throws DBus.Error;
+    public abstract async void release_held() throws DBus.Error;
+    public abstract async void send_dtmf(string tones) throws DBus.Error;
+    public abstract async void release_all() throws DBus.Error;
+    public abstract async int initiate(string number, string type_) throws DBus.Error;
+    // public abstract async CallParam0Struct[] list_calls() throws DBus.Error;
+    public abstract async void transfer(string number) throws DBus.Error;
+    public abstract async void release(int index) throws DBus.Error;
+    public signal void call_status(int index, string status, GLib.HashTable<string, GLib.Value?> properties);
+    public abstract async void activate_conference(int index) throws DBus.Error;
+}
+
+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 FSO_GSM_Call call;
+
+    protected class CallInfo
+    {
+        public int gsm_id;
+        public uint log_id;
+    }
+    protected List<CallInfo> calls;
+
+    public string info_provider;
+    public int info_signal_strength;
 
     public signal void status_changed(string message);
+    public signal void info_changed();
 
-    public GSMActivator()
+    protected void dump_table(HashTable<string, Value?> vals)
     {
-        device = null;
-        network = null;
-        sim = null;
+        vals.for_each((pk, pv) => {
+            string k = (string)pk;
+            Value? v = (Value?)pv;
+            stderr.printf("K: %s V: %s\n", k, v == null ? "(null)" : v.strdup_contents());
+        });
     }
 
-    public async void start()
+    protected void acquire_new_status(HashTable<string, Value?> status)
     {
-        if (device == null)
+        // NETWORK STATUS
+        // K: provider V: "vodafone UK"
+        // K: mode V: "automatic"
+        // K: registration V: "roaming"
+        // K: cid V: "157F"
+        // K: lac V: "0031"
+        // K: act V: "GSM"
+        // K: code V: "23415"
+        bool changed = false;
+
+        var vprovider = status.lookup("provider");
+        if (vprovider != 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(
+            if (info_provider != vprovider.get_string())
+            {
+                info_provider = vprovider.get_string();
+                changed = true;
+            }
+        }
+
+        if (changed)
+            info_changed();
+    }
+
+    protected void acquire_new_signal_strength(int strength)
+    {
+        if (info_signal_strength != strength)
+        {
+            info_signal_strength = strength;
+            info_changed();
+        }
+    }
+
+    public GSM()
+    {
+        Object(name: "gsm");
+
+        calls = new List<CallInfo>();
+
+        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.Network");
-            sim = (FSO_GSM_SIM)zavai.registry.sbus.get_object(
-                    "org.freesmartphone.ogsmd", 
-                    "/org/freesmartphone/GSM/Device",
-                    "org.freesmartphone.GSM.SIM");
+                "org.freesmartphone.GSM.SIM");
+        call = (FSO_GSM_Call)zavai.registry.sbus.get_object(
+            "org.freesmartphone.ogsmd", 
+            "/org/freesmartphone/GSM/Device",
+            "org.freesmartphone.GSM.Call");
+        dbus = zavai.registry.sbus.get_object(
+                "org.freedesktop.DBus",
+                "/org/freedesktop/DBus",
+                "org.freedesktop.DBus");
+        dbus.NameOwnerChanged += on_name_owner_changed;
+
+        info_provider = "(unknown)";
+        info_signal_strength = -1;
+
+        network.status += (status) => {
+            stderr.printf("NETWORK STATUS\n");
+            dump_table(status);
+            acquire_new_status(status);
+        };
+
+        network.signal_strength += (strength) => {
+            stderr.printf("SIGNAL STRENGTH %d\n", strength);
+            acquire_new_signal_strength(strength);
+        };
+
+        call.call_status += on_call_status;
+    }
+
+    /// Request GPS resource
+    public override void start()
+    {
+        if (started) return;
+
+        status_changed("Starting");
+
+        script_start();
+
+        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");
+            start_gsm.begin();
         }
+    }
 
+    // Release usage of GPS
+    public override void stop()
+    {
+        if (!started) return;
+
+        script_stop();
+
+        status_changed("");
+        info_provider = "";
+        info_signal_strength = -1;
+        info_changed();
+    }
+
+    protected override void cleanup_after_script_stop()
+    {
+        call = null;
+    }
+
+    public async void start_gsm()
+    {
         status_changed("Turning on antenna");
         while (true)
         {
@@ -150,7 +283,7 @@ protected class GSMActivator : Object
                     status_changed("Waiting for ogsmd to settle");
                     zavai.log.info("trying again after 2 seconds");
                     Timeout.add(2 * 1000, () => {
-                        start.callback();
+                        start_gsm.callback();
                         return false;
                     });
                     yield;
@@ -175,71 +308,68 @@ protected class GSMActivator : Object
         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);
-
-    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;
+        acquire_new_status(yield network.get_status());
+        acquire_new_signal_strength(yield network.get_signal_strength());
     }
 
-    /// Request GPS resource
-    public override void start()
+    protected CallInfo? lookup_call(int gsm_id)
     {
-        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();
+        for (weak List<CallInfo> i = calls; i != null; i = i.next)
+            if (i.data.gsm_id == gsm_id)
+                return i.data;
+        return null;
     }
 
-    protected void on_name_owner_changed(DBus.Object sender, string name, string oldOwner, string newOwner)
+    public void on_call_status(int index, string status, HashTable<string, Value?> properties)
     {
-        zavai.log.debug("NOC " + name + " from " + oldOwner + " to " + newOwner);
-        if (name == "org.freesmartphone.ogsmd" && newOwner != "")
+        stderr.printf("CALL STATUS %d %s\n", index, status);
+        dump_table(properties);
+
+        CallInfo? info = lookup_call(index);
+        if (info == null)
         {
-            status_changed("ogpsd came online");
-            activator.start.begin();
+            Value? v = properties.lookup("peer");
+            string title;
+            if (v != null)
+                title = "%s call from %s".printf(status, v.get_string());
+            else
+                title = "%s call".printf(status);
+
+            info = new CallInfo();
+            info.gsm_id = index;
+            info.log_id = zavai.log.log.start("call", title);
+            calls.append(info);
         }
-    }
 
-    // Release usage of GPS
-    public override void stop()
-    {
-        if (!started) return;
-
-        script_stop();
-    }
-
-    protected override void cleanup_after_script_stop()
-    {
-        call = null;
+        // Log a summary of all info
+        string call_info = "status: %s\n".printf(status);
+        properties.for_each((pk, pv) => {
+            string k = (string)pk;
+            Value? v = (Value?)pv;
+            call_info = call_info + "%s: %s\n".printf(k, v == null ? "(null)" : v.strdup_contents());
+        });
+        zavai.log.log.add(info.log_id, call_info);
+
+        // TODO: remove entry when it's the last possible status
+
+        /*
+        dbg("cbCallStatus %d, %s, %s" % (id, status, formatDict(properties)))
+        self.status = status
+        if status == "incoming":
+            if "peer" in properties and properties["peer"] == "+358942832031":
+                self.gsm_call_iface.ReleaseAll()
+                os.system("kapula-debug-call-handler &")
+            else:
+                os.system("alsactl restore -f /etc/alsa-scenarios/stereoout-maxvolume.state")
+                os.system("vibrator-start")
+                os.system("ringtone-start")
+                os.system("ledctrl --on-time 400 --off-time 200 gta02-aux:red");
+        if status == "release":
+            os.system("ringtone-stop")
+            os.system("vibrator-stop")
+            os.system("ledctrl --off gta02-aux:red");
+        */
     }
 }