Remove unneeded ID systems
[gregoa/zavai.git] / src / gsm.vala
index f44f4beda68a70d4ffc4523707c5682b560c3d3c..e5545038cbcd197cafea0fb6eed035b4ea44fa19 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * gsm - gsm resource for zavai
  *
 /*
  * gsm - gsm resource for zavai
  *
- * Copyright (C) 2009  Enrico Zini <enrico@enricozini.org>
+ * Copyright (C) 2009--2010  Enrico Zini <enrico@enricozini.org>
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -23,32 +23,223 @@ using GLib;
 namespace zavai {
 namespace gsm {
 
 namespace zavai {
 namespace gsm {
 
-public class GSM: zavai.Service
+[DBus (name = "org.freesmartphone.GSM.Device")]
+public interface FSO_GSM_Device : Object {
+    public abstract async int test_int (int i, out int j) throws DBus.Error;
+    public abstract async string test_string (string s, out string t) throws DBus.Error;
+    public abstract async void set_sim_buffers_sms(bool sim_buffers_sms) throws DBus.Error;
+    public abstract async void set_r_t_c() throws DBus.Error;
+    public abstract async void cancel_command() throws DBus.Error;
+    public abstract async void set_antenna_power(bool power) throws DBus.Error;
+    public abstract async GLib.HashTable<string, GLib.Value?> get_info() throws DBus.Error;
+    public abstract async void set_microphone_muted(bool muted) throws DBus.Error;
+    public signal void keypad_event(string name, bool pressed);
+    public abstract async int get_speaker_volume() throws DBus.Error;
+    public abstract async int get_r_t_c() throws DBus.Error;
+    public abstract async void set_speaker_volume(int modem_volume) throws DBus.Error;
+    public abstract async GLib.HashTable<string, GLib.Value?> get_features() throws DBus.Error;
+    public abstract async bool get_microphone_muted() throws DBus.Error;
+    public abstract async bool get_antenna_power() throws DBus.Error;
+    public abstract async void get_power_status(out string param0, out int param1) throws DBus.Error;
+    public abstract async bool get_sim_buffers_sms() throws DBus.Error;
+}
+
+[DBus (name = "org.freesmartphone.GSM.SIM")]
+public interface FSO_GSM_SIM : GLib.Object {
+    //public abstract async SIMParam0Struct[] retrieve_phonebook(string category) throws DBus.Error;
+    public abstract async void send_auth_code(string code) throws DBus.Error;
+    public abstract async string get_issuer() throws DBus.Error;
+    public abstract async void change_auth_code(string old_pin, string new_pin) throws DBus.Error;
+    public signal void auth_status(string status);
+    public abstract async string send_generic_sim_command(string command) throws DBus.Error;
+    public abstract async string[] list_phonebooks() throws DBus.Error;
+    public abstract async void set_service_center_number(string number) throws DBus.Error;
+    public abstract async GLib.HashTable<string, string> get_provider_list() throws DBus.Error;
+    //public abstract async SIMParam0Struct2[] get_home_zones() throws DBus.Error;
+    public signal void ready_status(bool status);
+    public abstract async void retrieve_entry(string category, int index, out string param0, out string param1) throws DBus.Error;
+    public abstract async void delete_message(int index) throws DBus.Error;
+    public abstract async void send_restricted_sim_command(int command, int fileid, int p1, int p2, int p3, string data, out int param0, out int param1, out string param2) throws DBus.Error;
+    public abstract async GLib.HashTable<string, GLib.Value?> get_messagebook_info() throws DBus.Error;
+    public abstract async bool get_sim_ready() throws DBus.Error;
+    public abstract async GLib.HashTable<string, GLib.Value?> get_phonebook_info(string category) throws DBus.Error;
+    public signal void memory_full();
+    public abstract async GLib.HashTable<string, GLib.Value?> get_sim_info() throws DBus.Error;
+    public abstract async void set_auth_code_required(bool required, string pin) throws DBus.Error;
+    public abstract async string get_auth_status() throws DBus.Error;
+    public abstract async void send_stored_message(int index, out int param0, out string param1) throws DBus.Error;
+    public abstract async int store_message(string number, string contents, GLib.HashTable<string, GLib.Value?> properties) throws DBus.Error;
+    public abstract async bool get_auth_code_required() throws DBus.Error;
+    public signal void incoming_stored_message(int index);
+    public abstract async void retrieve_message(int index, out string param0, out string param1, out string param2, out GLib.HashTable<string, GLib.Value?> param3) throws DBus.Error;
+    public abstract async void store_entry(string category, int index, string name, string number) throws DBus.Error;
+    public abstract async void unlock(string puk, string new_pin) throws DBus.Error;
+    public abstract async string get_service_center_number() throws DBus.Error;
+    //public abstract async SIMParam0Struct23[] retrieve_messagebook(string category) throws DBus.Error;
+    public abstract async void delete_entry(string category, int index) throws DBus.Error;
+}
+
+[DBus (name = "org.freesmartphone.GSM.Network")]
+public interface FSO_GSM_Network : GLib.Object {
+    public signal void status(GLib.HashTable<string, GLib.Value?> status);
+    public signal void signal_strength(int strength);
+    //public abstract async NetworkParam0Struct[] list_providers() throws DBus.Error;
+    public abstract async GLib.HashTable<string, GLib.Value?> get_call_forwarding(string reason) throws DBus.Error;
+    public signal void time_zone_report(int timezone);
+    public abstract async void unregister() throws DBus.Error;
+    public abstract async void set_calling_identification(string status) throws DBus.Error;
+    public abstract async void register_() throws DBus.Error;
+    public abstract async void send_ussd_request(string request) throws DBus.Error;
+    public abstract async void disable_call_forwarding(string reason, string class_) throws DBus.Error;
+    public signal void incoming_ussd(string mode, string message_);
+    public abstract async int get_signal_strength() throws DBus.Error;
+    public abstract async void enable_call_forwarding(string reason, string class_, string number, int timeout) throws DBus.Error;
+    public abstract async string get_calling_identification() throws DBus.Error;
+    public abstract async void register_with_provider(string operator_code) throws DBus.Error;
+    public signal void cipher_status(string gsm, string gprs);
+    public abstract async GLib.HashTable<string, GLib.Value?> get_status() throws DBus.Error;
+    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
 {
 {
-    public dynamic DBus.Object network;
-    public dynamic DBus.Object call;
-    public dynamic DBus.Object device;
-    public dynamic DBus.Object sim;
-    protected Pid child_pid;
-    protected int child_watch_id;
+    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;
 
 
-    public GSM()
+    protected class CallInfo
     {
     {
-        Object(name: "gsm.gsm");
+        public int gsm_id;
+        public zavai.log.Log log;
+    }
+    protected List<CallInfo> calls;
 
 
-        network = null;
-        call = null;
-        device = null;
-        sim = null;
+    public string info_provider;
+    public string info_registration;
+    public int info_signal_strength;
+
+    public signal void status_changed(string message);
+    public signal void info_changed();
+
+    protected void dump_table(HashTable<string, Value?> vals)
+    {
+        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());
+        });
+    }
 
 
-        child_pid = 0;
-        child_watch_id = 0;
+    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;
 
 
-        dynamic DBus.Object dbus = zavai.registry.sbus.get_object(
+        var vprovider = status.lookup("provider");
+        if (vprovider != null)
+        {
+            if (info_provider != vprovider.get_string())
+            {
+                info_provider = vprovider.get_string();
+                changed = true;
+            }
+        }
+
+        var vregistration = status.lookup("registration");
+        if (vregistration != null)
+        {
+            if (info_registration != vregistration.get_string())
+            {
+                info_registration = vregistration.get_string();
+                changed = true;
+            }
+        }
+
+        if (changed)
+        {
+stderr.printf("NOTIFY CHANGED\n");
+            info_changed();
+        }
+    }
+
+    protected void acquire_new_signal_strength(int strength)
+    {
+        if (info_signal_strength != strength)
+        {
+            info_signal_strength = strength;
+stderr.printf("ACQUIRE SIG %d\n", info_signal_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.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;
                 "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
     }
 
     /// Request GPS resource
@@ -56,109 +247,154 @@ public class GSM: zavai.Service
     {
         if (started) return;
 
     {
         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/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");
-
-        // try {
-        //     device.Enable();
-        //     zavai.log.info("Started GSM");
-        //     base.start();
-        // } catch (GLib.Error e) {
-        //     zavai.log.error(e.message);
-        // }
         base.start();
     }
 
         base.start();
     }
 
-    protected void on_name_owner_changed(dynamic DBus.Object dbus, string name, string oldOwner, string newOwner)
+    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 != "")
         {
     {
         zavai.log.debug("NOC " + name + " from " + oldOwner + " to " + newOwner);
         if (name == "org.freesmartphone.ogsmd" && newOwner != "")
         {
-            // Frameworkd started
-            device.SetAntennaPower(true, on_antenna_power);
-            //                             reply_handler = self.cbAntennaPowerReply,
-            //                             error_handler = self.cbAntennaPowerError)
+            status_changed("ogpsd came online");
+            start_gsm.begin();
         }
     }
 
         }
     }
 
-    protected void on_antenna_power(Error e)
+    // Release usage of GPS
+    public override void stop()
     {
     {
-        if (e != null)
+        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)
         {
         {
-            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;
+            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");
         }
         zavai.log.warning("on_antenna_power ok");
-        // network.Register(reply_handler = self.cbRegisterReply,
-        //                  error_handler = lambda e: (dbg("cbRegisterError %s" % e), self.loop.quit()))
+        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());
     }
 
     }
 
-    // Release usage of GPS
-    public override void stop()
+    protected CallInfo? lookup_call(int gsm_id)
     {
     {
-        if (!started) return;
-
-        Posix.kill((Posix.pid_t)child_pid, Posix.SIGTERM);
+        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_child(Pid pid, int status)
+    public void on_call_status(int index, string status, HashTable<string, Value?> properties)
     {
     {
-        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;
+        stderr.printf("CALL STATUS %d %s\n", index, status);
+        dump_table(properties);
 
 
-        base.stop();
+        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 = 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());
+        });
+        info.log.add(call_info);
+
+        // Remove entry when it's the last possible status
+        if (status == "release")
+        {
+            zavai.log.log.end(info.log);
+            for (weak List<CallInfo> i = calls; i != null; i = i.next)
+                if (i.data.gsm_id == index)
+                {
+                    calls.delete_link(i);
+                    break;
+                }
+        }
+
+        /*
+        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");
+        */
     }
 }
 
     }
 }