Try to log calls
[gregoa/zavai.git] / zavai / gsm.vala
index cb43956d391053ba0f2b40520bbca2c5e7dc4578..bf9abc0398cbfadb69e913ba6338b2c3d8b5b43c 100644 (file)
@@ -101,40 +101,92 @@ public interface FSO_GSM_Network : GLib.Object {
     public abstract async void get_country_code(out string param0, out string param1) throws DBus.Error;
 }
 
+[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 dynamic DBus.Object call;
+    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 GSM()
+    protected void dump_table(HashTable<string, Value?> vals)
     {
-        Object(name: "gsm");
+        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());
+        });
+    }
 
-        device = null;
-        network = null;
-        sim = null;
-        call = null;
+    protected void acquire_new_status(HashTable<string, Value?> status)
+    {
+        // 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;
 
-        dbus = zavai.registry.sbus.get_object(
-                "org.freedesktop.DBus",
-                "/org/freedesktop/DBus",
-                "org.freedesktop.DBus");
-        dbus.NameOwnerChanged += on_name_owner_changed;
+        var vprovider = status.lookup("provider");
+        if (vprovider != null)
+        {
+            if (info_provider != vprovider.get_string())
+            {
+                info_provider = vprovider.get_string();
+                changed = true;
+            }
+        }
+
+        if (changed)
+            info_changed();
     }
 
-    /// Request GPS resource
-    public override void start()
+    protected void acquire_new_signal_strength(int strength)
     {
-        if (started) return;
+        if (info_signal_strength != strength)
+        {
+            info_signal_strength = strength;
+            info_changed();
+        }
+    }
 
-        status_changed("Starting");
+    public GSM()
+    {
+        Object(name: "gsm");
 
-        script_start();
+        calls = new List<CallInfo>();
 
         device = (FSO_GSM_Device)zavai.registry.sbus.get_object(
                 "org.freesmartphone.ogsmd", 
@@ -148,10 +200,41 @@ public class GSM: zavai.ScriptMonitorService
                 "org.freesmartphone.ogsmd", 
                 "/org/freesmartphone/GSM/Device",
                 "org.freesmartphone.GSM.SIM");
-        call = zavai.registry.sbus.get_object(
+        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();
     }
@@ -174,6 +257,9 @@ public class GSM: zavai.ScriptMonitorService
         script_stop();
 
         status_changed("");
+        info_provider = "";
+        info_signal_strength = -1;
+        info_changed();
     }
 
     protected override void cleanup_after_script_stop()
@@ -183,10 +269,6 @@ public class GSM: zavai.ScriptMonitorService
 
     public async void start_gsm()
     {
-        if (device == null)
-        {
-        }
-
         status_changed("Turning on antenna");
         while (true)
         {
@@ -226,6 +308,68 @@ public class GSM: zavai.ScriptMonitorService
         status_changed("Registering with network");
         yield network.register_();
         status_changed("Registered with network");
+
+        acquire_new_status(yield network.get_status());
+        acquire_new_signal_strength(yield network.get_signal_strength());
+    }
+
+    protected CallInfo? lookup_call(int gsm_id)
+    {
+        for (weak List<CallInfo> i = calls; i != null; i = i.next)
+            if (i.data.gsm_id == gsm_id)
+                return i.data;
+        return null;
+    }
+
+    public void on_call_status(int index, string status, HashTable<string, Value?> properties)
+    {
+        stderr.printf("CALL STATUS %d %s\n", index, status);
+        dump_table(properties);
+
+        CallInfo? info = lookup_call(index);
+        if (info == null)
+        {
+            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);
+        }
+
+        // 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");
+        */
     }
 }