2 * test-gsm-receive - Play with receiving GSM data
4 * Copyright (C) 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
23 public DBus.Connection sbus;
25 //string VERSION = "0.1";
27 public class GSMReceive: Object //, zavai.Resource
29 public dynamic DBus.Object device;
30 public dynamic DBus.Object network;
31 public dynamic DBus.Object call;
35 //zavai.gsm.gsm.request("GSMReceive");
37 device = sbus.get_object(
38 "org.freesmartphone.ogsmd",
39 "/org/freesmartphone/GSM/Device",
40 "org.freesmartphone.GSM.Device");
42 network = sbus.get_object(
43 "org.freesmartphone.ogsmd",
44 "/org/freesmartphone/GSM/Device",
45 "org.freesmartphone.GSM.Network");
47 call = sbus.get_object(
48 "org.freesmartphone.ogsmd",
49 "/org/freesmartphone/GSM/Device",
50 "org.freesmartphone.GSM.Call");
52 stderr.printf("Antenna is %s\n", device.GetAntennaPower() ? "on" : "off");
53 stderr.printf("Signal strength: %d\n", network.GetSignalStrength());
54 stderr.printf("Calling identification: %s\n", network.GetCallingIdentification());
55 GLib.HashTable<string, GLib.Value?> status = network.GetStatus();
56 stderr.printf("Network status:\n");
59 network.Status += on_network_Status;
60 network.SignalStrength += on_network_SignalStrength;
61 call.CallStatus += on_call_Status;
64 public void shutdown()
66 //zavai.gsm.gsm.release("GSMReceive");
69 protected void dump_table(HashTable<string, Value?> vals)
71 vals.for_each((pk, pv) => {
72 string k = (string)pk;
73 Value? v = (Value?)pv;
74 stderr.printf("K: %s V: %s\n", k, v == null ? "(null)" : v.strdup_contents());
78 public void on_network_SignalStrength(DBus.Object sender, int strength)
80 stderr.printf("SIGNAL STRENGTH %d\n", strength);
83 public void on_network_Status(DBus.Object sender, HashTable<string, Value?> status)
85 stderr.printf("NETWORK STATUS\n");
88 // dbg("cbNetworkStatus %s" % formatDict(status))
91 public void on_call_Status(DBus.Object sender, int index, string status, HashTable<string, Value?> properties)
93 stderr.printf("CALL STATUS %d %s\n", index, status);
94 dump_table(properties);
96 dbg("cbCallStatus %d, %s, %s" % (id, status, formatDict(properties)))
98 if status == "incoming":
99 if "peer" in properties and properties["peer"] == "+358942832031":
100 self.gsm_call_iface.ReleaseAll()
101 os.system("kapula-debug-call-handler &")
103 os.system("alsactl restore -f /etc/alsa-scenarios/stereoout-maxvolume.state")
104 os.system("vibrator-start")
105 os.system("ringtone-start")
106 os.system("ledctrl --on-time 400 --off-time 200 gta02-aux:red");
107 if status == "release":
108 os.system("ringtone-stop")
109 os.system("vibrator-stop")
110 os.system("ledctrl --off gta02-aux:red");
116 def onIncomingUssd( self, mode, message ):
117 logger.info( "USSD Message: %s" % message )
118 self.main.groups["alert"].activate( "<title>Operator Message</title>%s" % message, [("OK")] )
119 def onCallStatus( self, id, status, properties ):
121 self.update_status(status)
122 if not status in ["release"]:
123 if "peer" in properties:
124 self.part_text_set( "label", self.main.groups["contacts"].tryNumberToName( properties[ "peer" ] ) )
125 self.part_text_set( "sublabel", properties[ "peer" ] )
127 self.part_text_set( "label", _("unknown number") )
128 self.part_text_set( "sublabel", "" )
130 @edje.decorators.signal_callback( "mouse,clicked,1", "button_*" )
131 def on_edje_signal_dialer_button_pressed(self, emission, source):
132 key = source.split("_", 1)[1]
133 if key in ("0", "1", "2", "3", "4", "5", "6", "7", "8", "9"):
134 dbus_object.gsm_call_iface.SendDtmf( key )
135 self.text.append(key)
137 dbus_object.gsm_call_iface.SendDtmf( "*" )
138 self.text.append("*")
140 dbus_object.gsm_call_iface.SendDtmf( "#" )
142 # The trailing whitespace is a workaround for the one char invisible
143 # bug due to some problems with scaling of text parts.
144 self.part_text_set("label", "".join(self.text)+" ")
146 @edje.decorators.signal_callback("call_button_pressed", "button_right")
147 def on_edje_signal_call_button_right_pressed(self, emission, source):
148 self.main.transition_to("call")
150 def sendMessage( self, index ):
151 logger.info( "trying to send message w/ index %d..." % index )
152 dbus_object.gsm_sim_iface.SendStoredMessage( index, reply_handler=self.cbSendReply, error_handler=self.cbSendError )
154 def cbSendReply( self, reference, timestamp ):
155 logger.info( "sent message successfully w/ reference number %d (timestamp: %s)" % ( reference, timestamp ) )
157 def cbSendError( self, e ):
158 logger.error( "could not send message. leaving in outgoing. error was: %s" % e )
160 def cbStoreReply( self, result ):
161 logger.info( "stored message lives at SIM position %d" % result )
162 self.sendMessage( result )
164 dbus_object.gsm_sim_iface.RetrieveMessagebook(
166 reply_handler=self.cbMessagebookReply,
167 error_handler=self.cbMessagebookError
171 def cbStoreError( self, e ):
172 logger.warning( "error while storing message - %s" % e.get_dbus_name() )
173 if e.get_dbus_name() == "org.freesmartphone.GSM.SIM.MemoryFull":
174 self.main.groups["alert"].activate( "<title>Failed to send message</title>SIM Memory Full", [("OK")] )
176 def cbSend1( self, selected, cb_data ):
177 self.main.groups["text_edit"].setup(
181 selected[1], # reference
185 def cbSend2( self, text, number ):
186 if dbus_object.gsm_device_obj:
187 dbus_object.gsm_sim_iface.StoreMessage(
189 reply_handler=self.cbStoreReply,
190 error_handler=self.cbStoreError
193 def cbDelete( self, result, reference ):
194 if result == "abort":
196 if dbus_object.gsm_device_obj:
197 dbus_object.gsm_sim_iface.DeleteMessage(
200 for i in range( len( self.messagebook ) ):
201 if self.messagebook[i][0] == reference:
202 del self.messagebook[i]
206 def cbForward( self, selected, text ):
207 if dbus_object.gsm_device_obj:
208 dbus_object.gsm_sim_iface.StoreMessage(
209 selected[1], text, {},
210 reply_handler=self.cbStoreReply,
211 error_handler=self.cbStoreError
214 def cbReply( self, text, number):
215 if dbus_object.gsm_device_obj:
216 dbus_object.gsm_sim_iface.StoreMessage(
218 reply_handler=self.cbStoreReply,
219 error_handler=self.cbStoreError
222 def cbMenu( self, result ):
223 if result == _("send"):
224 self.main.groups["contacts"].prepare()
225 if self.main.groups["contacts"].ready:
226 self.main.groups["list_choose"].setup(
228 [ (x[1], x[2]) for x in self.main.groups["contacts"].phonebook],
232 elif result == _("delete"):
233 self.main.groups["alert"].activate(
235 (_("abort"), _("delete")),
236 self.current[self.selected][0], # reference
239 elif result == _("forward"):
240 self.main.groups["contacts"].prepare()
241 if self.main.groups["contacts"].ready:
242 self.main.groups["list_choose"].setup(
244 [ (x[1], x[2] ) for x in self.main.groups["contacts"].phonebook],
245 self.current[self.selected][3],
248 elif result == _("reply"):
249 self.main.groups["text_edit"].setup(
252 self.current[self.selected][2], # title = number
253 self.current[self.selected][2], # reference = number
257 def cbMessagebookReply( self, result ):
258 logger.info( "retrieved messagebook: %s" % result )
260 self.messagebook = result
263 self.main.groups["main"].targets["sms"] = True
264 self.main.groups["main"].update()
265 if not self.newindex is None:
266 message = [x for x in self.messagebook if x[0] == self.newindex]
268 if message and self.main.current_group == self.main.groups["main"]:
270 if "read" in message[1]:
272 timestamp = message[4]["timestamp"]
275 timestamp = _("Unknown")
276 from_text = self.main.groups["contacts"].tryNumberToName( message[2] )
277 self.main.groups["text_show"].setup(
279 _("%s: %s<br>Date: %s<p>%s") % (
283 textblock_escape( message[3] ).replace( '\n', '<br>' )
286 if dbus_object.gsm_device_obj:
287 messagebookInfo = dbus_object.gsm_sim_iface.GetMessagebookInfo()
288 if messagebookInfo["used"] == messagebookInfo["last"]:
289 self.main.groups["alert"].activate( "<title>Warning</title>SIM Memory Full", [("OK")] )
291 def cbMessagebookError( self, e ):
292 logger.warning( "error while retrieving messagebook" )
295 def onIncomingMessage( self, index ):
296 logger.info( "new message! Retrieving messagebook..." )
297 self.newindex = index
299 dbus_object.gsm_sim_iface.RetrieveMessagebook(
301 reply_handler=self.cbMessagebookReply,
302 error_handler=self.cbMessagebookError
307 if not self.ready and not self.busy:
308 if dbus_object.gsm_device_obj:
309 logger.info( "retrieving messagebook..." )
310 dbus_object.gsm_sim_iface.RetrieveMessagebook(
312 reply_handler=self.cbMessagebookReply,
313 error_handler=self.cbMessagebookError
317 # Fake messagebook...
318 self.cbMessagebookReply( [
319 (0, "read", "+4544555", "Hello World!"),
320 (1, "read", "+456663443", "Zhone!"),
321 (2, "read", "+456663443", "Hi Guy\nGuess what, I now "+
322 "know to write multi-line SMSs.\nIsn't that "+
323 "nice?\n\nSome Buddy"),
324 (3, "read", "Flash SMS", "An SMS without digits. Strange, isn't it?"),
327 def onReadyStatus( self, status ):
328 logger.debug( "SIM is ready: %s" % status )
338 def updateList( self):
339 self.main.groups["contacts"].prepare()
340 self.pages = max( ( len( self.messagebook ) - 1 ) / 6 + 1, 1 )
341 if self.page >= self.pages:
342 self.page = self.pages - 1
345 self.current = self.messagebook[self.page*6:(self.page+1)*6]
346 text = u"".join( [u"□"]*self.page+[u"▣"]+[u"□"]*(self.pages-self.page-1) )
347 self.part_text_set( "pager", text )
348 for i in range( 0, len( self.current ) ):
349 main_text = self.main.groups["contacts"].tryNumberToName( self.current[i][2] )
350 self.part_text_set( "label_main_list_%i" % i, main_text )
351 sub_text = " ".join(self.current[i][3].splitlines())
352 self.part_text_set( "label_sub_list_%i" % i, u"(%s) %s" % ( self.current[i][1], sub_text ) )
353 for i in range( len( self.current ), 6):
354 self.part_text_set( "label_main_list_%i" % i, u"" )
355 self.part_text_set( "label_sub_list_%i" % i, u"" )
358 self.signal_emit( "deactivate_target_list_%i" % i, "" )
361 @edje.decorators.signal_callback( "mouse,clicked,1", "target_list_*" )
362 def on_edje_signal_button_list_pressed( self, emission, source ):
363 id = int( source.split( "_" )[-1] )
364 if self.selected == id:
366 if self.selected is not None:
367 self.signal_emit( "deactivate_target_list_%i" % self.selected, "" )
368 self.signal_emit( "activate_target_list_%i" % id, "" )
371 @edje.decorators.signal_callback( "mouse,clicked,1", "button_action_left" )
372 def on_edje_signal_button_action_left_pressed( self, emission, source ):
376 @edje.decorators.signal_callback( "mouse,clicked,1", "button_action_right" )
377 def on_edje_signal_button_action_right_pressed( self, emission, source ):
381 @edje.decorators.signal_callback( "mouse,clicked,1", "button_action_open" )
382 def on_edje_signal_button_action_open_pressed( self, emission, source ):
383 if self.selected is not None:
384 if "read" in self.current[self.selected][1]:
386 timestamp = self.current[self.selected][4]["timestamp"]
389 timestamp = _("Unknown")
390 from_text = self.main.groups["contacts"].tryNumberToName( self.current[self.selected][2] )
391 self.main.groups["text_show"].setup(
393 _("%s: %s<br>Date: %s<p>%s") % (
397 textblock_escape( self.current[self.selected][3] ).replace( '\n', '<br>' )
401 @edje.decorators.signal_callback( "mouse,clicked,1", "button_bottom_middle" )
402 def on_edje_signal_button_bottom_middle_pressed( self, emission, source ):
403 self.main.groups["menu"].activate( ( _("send"), _("delete"), _("forward"), _("reply") ), self.cbMenu )
404 self.main.groups["menu"].part_text_set( "target_label_cancel", _("cancel") )
408 static int main (string[] args) {
410 // Gst.init (ref args);
413 sbus = DBus.Bus.get(DBus.BusType.SYSTEM);
415 // Core infrastructure
416 // zavai.config = new zavai.Config();
417 // zavai.config.argv0 = args[0];
418 // zavai.registry = new zavai.Registry();
420 // Additional infrastructure
425 var gr = new GSMReceive();
429 // zavai.info("Shutting down")
430 // zavai.registry.shutdown();