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 device;
-
- public GSM()
- {
- Object(name: "gsm.gsm");
-
- device = zavai.registry.sbus.get_object(
- "org.freesmartphone.ogpsd",
- "/org/freesmartphone/GSM/Device",
- "org.freesmartphone.Resource");
- }
-
- /// Request GPS resource
- public override void start()
- {
- if (started) return;
- try {
- device.Enable();
- zavai.log.info("Started GSM");
- base.start();
- } catch (GLib.Error e) {
- zavai.log.error(e.message);
- }
- base.start();
- }
-
- // Release usage of GPS
- public override void stop()
- {
- if (!started) return;
- try {
- device.Disable();
- zavai.log.info("Stopped GSM");
- base.stop();
- } catch (GLib.Error e) {
- zavai.log.error(e.message);
- }
- base.stop();
- }
+ public dynamic DBus.Object device;
+ public dynamic DBus.Object network;
+ public dynamic DBus.Object sim;
+
+ 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");
+
+ 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;
+ }
+
+ /// Request GPS resource
+ public override void start()
+ {
+ 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();
+ }
+
+ 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");
+ activator.begin();
+ }
+ }
+
+ // Release usage of GPS
+ public override void stop()
+ {
+ if (!started) return;
+
+ script_stop();
+ }
+
+ protected override void cleanup_after_script_stop()
+ {
+ call = null;
+ }
}
public class GPRS: zavai.Service
{
- public dynamic DBus.Object device;
-
- public GPRS()
- {
- Object(name: "gsm.gprs");
-
- device = zavai.registry.sbus.get_object(
- "org.freesmartphone.ogsmd",
- "/org/freesmartphone/GSM/Device",
- "org.freesmartphone.GSM.PDP");
- }
-
- /// Request GPS resource
- public override void start()
- {
- if (started) return;
- try {
- //gsm.request(name);
- device.ActivateContext(
- zavai.config.gprs_apn,
- zavai.config.gprs_user,
- zavai.config.gprs_pass);
- zavai.log.info("Started GPRS");
- base.start();
- } catch (GLib.Error e) {
- zavai.log.error(e.message);
- }
- base.start();
- }
-
- // Release usage of GPS
- public override void stop()
- {
- if (!started) return;
- try {
- //gsm.release(name);
- device.DeactivateContext();
- zavai.log.info("Stopped GPRS");
- base.stop();
- } catch (GLib.Error e) {
- zavai.log.error(e.message);
- }
- base.stop();
- }
+ public dynamic DBus.Object device;
+
+ public GPRS()
+ {
+ Object(name: "gsm.gprs");
+
+ device = zavai.registry.sbus.get_object(
+ "org.freesmartphone.ogsmd",
+ "/org/freesmartphone/GSM/Device",
+ "org.freesmartphone.GSM.PDP");
+ }
+
+ /// Request GPS resource
+ public override void start()
+ {
+ if (started) return;
+ try {
+ //gsm.request(name);
+ device.ActivateContext(
+ zavai.config.gprs_apn,
+ zavai.config.gprs_user,
+ zavai.config.gprs_pass);
+ zavai.log.info("Started GPRS");
+ base.start();
+ } catch (GLib.Error e) {
+ zavai.log.error(e.message);
+ }
+ base.start();
+ }
+
+ // Release usage of GPS
+ public override void stop()
+ {
+ if (!started) return;
+ try {
+ //gsm.release(name);
+ device.DeactivateContext();
+ zavai.log.info("Stopped GPRS");
+ base.stop();
+ } catch (GLib.Error e) {
+ zavai.log.error(e.message);
+ }
+ base.stop();
+ }
}
public zavai.gsm.GSM gsm = null;
public void init()
{
- gsm = new GSM();
- gprs = new GPRS();
-
- zavai.registry.register_service(gsm);
- zavai.registry.register_service(gprs);
+ gsm = new GSM();
+ gprs = new GPRS();
}
}