2 * gsm - gsm resource for zavai
4 * Copyright (C) 2009--2010 Enrico Zini <enrico@enricozini.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 [DBus (name = "org.freesmartphone.GSM.Device")]
27 public interface FSO_GSM_Device : Object {
28 public abstract async int test_int (int i, out int j) throws DBus.Error;
29 public abstract async string test_string (string s, out string t) throws DBus.Error;
30 public abstract async void set_sim_buffers_sms(bool sim_buffers_sms) throws DBus.Error;
31 public abstract async void set_r_t_c() throws DBus.Error;
32 public abstract async void cancel_command() throws DBus.Error;
33 public abstract async void set_antenna_power(bool power) throws DBus.Error;
34 public abstract async GLib.HashTable<string, GLib.Value?> get_info() throws DBus.Error;
35 public abstract async void set_microphone_muted(bool muted) throws DBus.Error;
36 public signal void keypad_event(string name, bool pressed);
37 public abstract async int get_speaker_volume() throws DBus.Error;
38 public abstract async int get_r_t_c() throws DBus.Error;
39 public abstract async void set_speaker_volume(int modem_volume) throws DBus.Error;
40 public abstract async GLib.HashTable<string, GLib.Value?> get_features() throws DBus.Error;
41 public abstract async bool get_microphone_muted() throws DBus.Error;
42 public abstract async bool get_antenna_power() throws DBus.Error;
43 public abstract async void get_power_status(out string param0, out int param1) throws DBus.Error;
44 public abstract async bool get_sim_buffers_sms() throws DBus.Error;
47 [DBus (name = "org.freesmartphone.GSM.SIM")]
48 public interface FSO_GSM_SIM : GLib.Object {
49 //public abstract async SIMParam0Struct[] retrieve_phonebook(string category) throws DBus.Error;
50 public abstract async void send_auth_code(string code) throws DBus.Error;
51 public abstract async string get_issuer() throws DBus.Error;
52 public abstract async void change_auth_code(string old_pin, string new_pin) throws DBus.Error;
53 public signal void auth_status(string status);
54 public abstract async string send_generic_sim_command(string command) throws DBus.Error;
55 public abstract async string[] list_phonebooks() throws DBus.Error;
56 public abstract async void set_service_center_number(string number) throws DBus.Error;
57 public abstract async GLib.HashTable<string, string> get_provider_list() throws DBus.Error;
58 //public abstract async SIMParam0Struct2[] get_home_zones() throws DBus.Error;
59 public signal void ready_status(bool status);
60 public abstract async void retrieve_entry(string category, int index, out string param0, out string param1) throws DBus.Error;
61 public abstract async void delete_message(int index) throws DBus.Error;
62 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;
63 public abstract async GLib.HashTable<string, GLib.Value?> get_messagebook_info() throws DBus.Error;
64 public abstract async bool get_sim_ready() throws DBus.Error;
65 public abstract async GLib.HashTable<string, GLib.Value?> get_phonebook_info(string category) throws DBus.Error;
66 public signal void memory_full();
67 public abstract async GLib.HashTable<string, GLib.Value?> get_sim_info() throws DBus.Error;
68 public abstract async void set_auth_code_required(bool required, string pin) throws DBus.Error;
69 public abstract async string get_auth_status() throws DBus.Error;
70 public abstract async void send_stored_message(int index, out int param0, out string param1) throws DBus.Error;
71 public abstract async int store_message(string number, string contents, GLib.HashTable<string, GLib.Value?> properties) throws DBus.Error;
72 public abstract async bool get_auth_code_required() throws DBus.Error;
73 public signal void incoming_stored_message(int index);
74 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;
75 public abstract async void store_entry(string category, int index, string name, string number) throws DBus.Error;
76 public abstract async void unlock(string puk, string new_pin) throws DBus.Error;
77 public abstract async string get_service_center_number() throws DBus.Error;
78 //public abstract async SIMParam0Struct23[] retrieve_messagebook(string category) throws DBus.Error;
79 public abstract async void delete_entry(string category, int index) throws DBus.Error;
82 [DBus (name = "org.freesmartphone.GSM.Network")]
83 public interface FSO_GSM_Network : GLib.Object {
84 public signal void status(GLib.HashTable<string, GLib.Value?> status);
85 public signal void signal_strength(int strength);
86 //public abstract async NetworkParam0Struct[] list_providers() throws DBus.Error;
87 public abstract async GLib.HashTable<string, GLib.Value?> get_call_forwarding(string reason) throws DBus.Error;
88 public signal void time_zone_report(int timezone);
89 public abstract async void unregister() throws DBus.Error;
90 public abstract async void set_calling_identification(string status) throws DBus.Error;
91 public abstract async void register_() throws DBus.Error;
92 public abstract async void send_ussd_request(string request) throws DBus.Error;
93 public abstract async void disable_call_forwarding(string reason, string class_) throws DBus.Error;
94 public signal void incoming_ussd(string mode, string message_);
95 public abstract async int get_signal_strength() throws DBus.Error;
96 public abstract async void enable_call_forwarding(string reason, string class_, string number, int timeout) throws DBus.Error;
97 public abstract async string get_calling_identification() throws DBus.Error;
98 public abstract async void register_with_provider(string operator_code) throws DBus.Error;
99 public signal void cipher_status(string gsm, string gprs);
100 public abstract async GLib.HashTable<string, GLib.Value?> get_status() throws DBus.Error;
101 public abstract async void get_country_code(out string param0, out string param1) throws DBus.Error;
104 [DBus (name = "org.freesmartphone.GSM.Call")]
105 public interface FSO_GSM_Call : GLib.Object {
106 public abstract async void activate(int index) throws DBus.Error;
107 public abstract async void emergency(string number) throws DBus.Error;
108 public abstract async void hold_active() throws DBus.Error;
109 public abstract async void release_held() throws DBus.Error;
110 public abstract async void send_dtmf(string tones) throws DBus.Error;
111 public abstract async void release_all() throws DBus.Error;
112 public abstract async int initiate(string number, string type_) throws DBus.Error;
113 // public abstract async CallParam0Struct[] list_calls() throws DBus.Error;
114 public abstract async void transfer(string number) throws DBus.Error;
115 public abstract async void release(int index) throws DBus.Error;
116 public signal void call_status(int index, string status, GLib.HashTable<string, GLib.Value?> properties);
117 public abstract async void activate_conference(int index) throws DBus.Error;
120 public class GSM: zavai.ScriptMonitorService
122 protected dynamic DBus.Object dbus;
123 public FSO_GSM_Device device;
124 public FSO_GSM_Network network;
125 public FSO_GSM_SIM sim;
126 public FSO_GSM_Call call;
128 protected class CallInfo
131 public zavai.log.Log log;
133 protected List<CallInfo> calls;
135 public string info_provider;
136 public string info_registration;
137 public int info_signal_strength;
139 public signal void status_changed(string message);
140 public signal void info_changed();
141 public signal void new_call(int index);
142 public signal void end_call(int index);
143 public signal void new_sms();
145 protected void dump_table(HashTable<string, Value?> vals)
147 vals.for_each((pk, pv) => {
148 string k = (string)pk;
149 Value? v = (Value?)pv;
150 stderr.printf("K: %s V: %s\n", k, v == null ? "(null)" : v.strdup_contents());
154 protected void acquire_new_status(HashTable<string, Value?> status)
157 // K: provider V: "vodafone UK"
158 // K: mode V: "automatic"
159 // K: registration V: "roaming"
163 // K: code V: "23415"
164 bool changed = false;
166 var vprovider = status.lookup("provider");
167 if (vprovider != null)
169 if (info_provider != vprovider.get_string())
171 info_provider = vprovider.get_string();
176 var vregistration = status.lookup("registration");
177 if (vregistration != null)
179 if (info_registration != vregistration.get_string())
181 info_registration = vregistration.get_string();
188 stderr.printf("NOTIFY CHANGED\n");
193 protected void acquire_new_signal_strength(int strength)
195 if (info_signal_strength != strength)
197 info_signal_strength = strength;
198 stderr.printf("ACQUIRE SIG %d\n", info_signal_strength);
207 calls = new List<CallInfo>();
209 device = (FSO_GSM_Device)zavai.registry.sbus.get_object(
210 "org.freesmartphone.ogsmd",
211 "/org/freesmartphone/GSM/Device",
212 "org.freesmartphone.GSM.Device");
213 network = (FSO_GSM_Network)zavai.registry.sbus.get_object(
214 "org.freesmartphone.ogsmd",
215 "/org/freesmartphone/GSM/Device",
216 "org.freesmartphone.GSM.Network");
217 sim = (FSO_GSM_SIM)zavai.registry.sbus.get_object(
218 "org.freesmartphone.ogsmd",
219 "/org/freesmartphone/GSM/Device",
220 "org.freesmartphone.GSM.SIM");
221 call = (FSO_GSM_Call)zavai.registry.sbus.get_object(
222 "org.freesmartphone.ogsmd",
223 "/org/freesmartphone/GSM/Device",
224 "org.freesmartphone.GSM.Call");
225 dbus = zavai.registry.sbus.get_object(
226 "org.freedesktop.DBus",
227 "/org/freedesktop/DBus",
228 "org.freedesktop.DBus");
229 dbus.NameOwnerChanged += on_name_owner_changed;
231 info_provider = "(unknown)";
232 info_signal_strength = -1;
234 network.status += (status) => {
235 stderr.printf("NETWORK STATUS\n");
237 acquire_new_status(status);
240 network.signal_strength += (strength) => {
241 stderr.printf("SIGNAL STRENGTH %d\n", strength);
242 acquire_new_signal_strength(strength);
245 call.call_status += on_call_status;
246 sim.incoming_stored_message += (idx) => {
247 stderr.printf("INCOMING STORED MESSAGE %d\n", idx);
248 sms_log_and_delete(idx);
252 /// Request GPS resource
253 public override void start()
257 status_changed("Starting");
264 protected void on_name_owner_changed(DBus.Object sender, string name, string oldOwner, string newOwner)
266 zavai.log.debug("NOC " + name + " from " + oldOwner + " to " + newOwner);
267 if (name == "org.freesmartphone.ogsmd" && newOwner != "")
269 status_changed("ogpsd came online");
274 // Release usage of GPS
275 public override void stop()
277 if (!started) return;
283 info_signal_strength = -1;
287 protected override void cleanup_after_script_stop()
292 public async void start_gsm()
294 status_changed("Turning on antenna");
298 yield device.set_antenna_power(true);
301 zavai.log.warning("SetAntennaPower: " + e.message);
302 if (e.message.str("current status is 'enabling'") != null
303 || e.message.str("current status is 'unknown'") != null)
305 status_changed("Waiting for ogsmd to settle");
306 zavai.log.info("trying again after 2 seconds");
307 Timeout.add(2 * 1000, () => {
308 start_gsm.callback();
313 status_changed("Checking if PIN is required");
314 string status = yield sim.get_auth_status();
315 zavai.log.info("on_auth_status: " + status);
316 if (status == "READY")
317 status_changed("PIN ok");
318 else if (status == "SIM PIN")
320 status_changed("Sending PIN");
321 yield sim.send_auth_code(zavai.config.sim_pin);
322 status_changed("PIN OK");
325 zavai.log.debug("Unknown status: " + status);
329 zavai.log.warning("on_antenna_power ok");
330 status_changed("Registering with network");
331 yield network.register_();
332 status_changed("Registered with network");
334 acquire_new_status(yield network.get_status());
335 acquire_new_signal_strength(yield network.get_signal_strength());
338 protected CallInfo? lookup_call(int gsm_id)
340 for (weak List<CallInfo> i = calls; i != null; i = i.next)
341 if (i.data.gsm_id == gsm_id)
346 public void on_call_status(int index, string status, HashTable<string, Value?> properties)
349 bool is_done = false;
350 stderr.printf("CALL STATUS %d %s\n", index, status);
351 dump_table(properties);
353 CallInfo? info = lookup_call(index);
357 Value? v = properties.lookup("peer");
360 title = "%s call from %s".printf(status, v.get_string());
362 title = "%s call".printf(status);
364 info = new CallInfo();
366 info.log = zavai.log.log.start("call", title);
370 // Log a summary of all info
371 string call_info = "status: %s\n".printf(status);
372 properties.for_each((pk, pv) => {
373 string k = (string)pk;
374 Value? v = (Value?)pv;
375 call_info = call_info + "%s: %s\n".printf(k, v == null ? "(null)" : v.strdup_contents());
377 info.log.add(call_info);
379 // Remove entry when it's the last possible status
380 if (status == "release")
382 zavai.log.log.end(info.log);
383 for (weak List<CallInfo> i = calls; i != null; i = i.next)
384 if (i.data.gsm_id == index)
386 calls.delete_link(i);
393 dbg("cbCallStatus %d, %s, %s" % (id, status, formatDict(properties)))
395 if status == "incoming":
396 if "peer" in properties and properties["peer"] == "+358942832031":
397 self.gsm_call_iface.ReleaseAll()
398 os.system("kapula-debug-call-handler &")
400 os.system("alsactl restore -f /etc/alsa-scenarios/stereoout-maxvolume.state")
401 os.system("vibrator-start")
402 os.system("ringtone-start")
403 os.system("ledctrl --on-time 400 --off-time 200 gta02-aux:red");
404 if status == "release":
405 os.system("ringtone-stop")
406 os.system("vibrator-stop")
407 os.system("ledctrl --off gta02-aux:red");
410 if (is_new && !is_done)
416 public async void sms_log_and_delete(int idx)
418 // Retrieve message info
419 string state, sender, msg;
420 GLib.HashTable<string, GLib.Value?> info;
421 yield sim.retrieve_message(idx, out state, out sender, out msg, out info);
424 var sms_log = zavai.log.log.start("sms", msg);
425 string sms_info = "state: %s\nsender: %s\nmsg: %s\n".printf(state, sender, msg);
426 info.for_each((pk, pv) => {
427 string k = (string)pk;
428 Value? v = (Value?)pv;
429 sms_info = sms_info + "%s: %s\n".printf(k, v == null ? "(null)" : v.strdup_contents());
431 sms_log.add(sms_info);
432 zavai.log.log.end(sms_log);
435 yield sim.delete_message(idx);
437 // Notify reception of the sms
442 public class GPRS: zavai.Service
444 public dynamic DBus.Object device;
448 Object(name: "gsm.gprs");
450 device = zavai.registry.sbus.get_object(
451 "org.freesmartphone.ogsmd",
452 "/org/freesmartphone/GSM/Device",
453 "org.freesmartphone.GSM.PDP");
456 /// Request GPS resource
457 public override void start()
462 device.ActivateContext(
463 zavai.config.gprs_apn,
464 zavai.config.gprs_user,
465 zavai.config.gprs_pass);
466 zavai.log.info("Started GPRS");
468 } catch (GLib.Error e) {
469 zavai.log.error(e.message);
474 // Release usage of GPS
475 public override void stop()
477 if (!started) return;
480 device.DeactivateContext();
481 zavai.log.info("Stopped GPRS");
483 } catch (GLib.Error e) {
484 zavai.log.error(e.message);
490 public zavai.gsm.GSM gsm = null;
491 public zavai.gsm.GPRS gprs = null;