Upgrade ofono to 1.2
[profile/ivi/ofono.git] / test / test-cbs
1 #!/usr/bin/python
2
3 import dbus
4 import dbus.mainloop.glib
5 import sys
6 import gobject
7 import os
8
9 def print_menu():
10         print "Select test case"
11         print "----------------------------------------------------------------"
12         print "[0] Activate cbs"
13         print "[1] Deactivate cbs"
14         print "[2] Get cbs properties"
15         print "[3] Set/Register topics"
16         print "        If several - give topics separated with comma. \
17                 \n        E.g. 20,50-51,60"
18         print "[4] Clear/Unregister topics"
19         print "[5] NetReg Base Station - Get current serving cell"
20         print "[x] Exit"
21         print "----------------------------------------------------------------"
22
23 def property_changed(property, value):
24         if value == "" and property == "Topics":
25                 print "User selected Topics have been cleared. \
26                         \nRegistered for emergency topics only."
27         else:
28                 print "Cell Broadcast property %s is changed to %s" % (property, value)
29         print "\nPress ENTER to continue"
30
31 def incoming_broadcast(text, topic):
32         print "Broadcast msg: %s \n Topic channel: %s" % (text, topic)
33         print "\nPress ENTER to continue"
34
35 def emergency_broadcast(text, properties):
36         emergType = properties["EmergencyType"]
37         emergAlert = properties["EmergencyAlert"]
38
39         print "Broadcast msg: %s \n\t Type: %s \n\t Alert: %s \n\t Popup: %s" \
40                 % (text, emergType, emergAlert, popup)
41
42         if properties["Popup"] == True:
43                 print "Popup required."
44
45         print "\nPress ENTER to continue"
46
47 def set_cbs_state(cbs, state):
48         if state == True:
49                 print "Activating cell broadcast..."
50                 cbs.SetProperty("Powered", dbus.Boolean(1))
51         else:
52                 print "Deactivating cell broadcast..."
53                 cbs.SetProperty("Powered", dbus.Boolean(0))
54         print "-----------------------------------------------------------"
55
56 def print_cbs_properties(cbs):
57         properties = cbs.GetProperties()
58         print "---------------------PROPERTIES----------------------------"
59         for p in properties:
60                 if len(properties[p].__str__()) > 0:
61                         if p == "Powered":
62                                 if properties[p] == True:
63                                         print "Cell Broadcast is Activated."
64                                 else:
65                                         print "Cell Broadcast is Deactivated."
66                         elif p == "Topics":
67                                 print "Currently set CBS %s are: %s" \
68                                         % (p, properties[p])
69                                 topics_available = True
70                 else:
71                         print "Cell Broadcast %s value empty" % (p)
72         print "-----------------------------------------------------------"
73
74 def set_topics(cbs):
75         print_cbs_properties(cbs)
76
77         topicTemp = ""
78         invalidData = False;
79         index = 0
80
81         topics = raw_input('Enter the topic ID(s) you want to register to: ')
82
83         while index < len(topics):
84                 if topics[index] == ',' or topics[index] == '-':
85                         topicTemp = ""
86                 elif topics[index] >= '0' and topics[index] <= '9':
87                         topicTemp = topicTemp + topics[index]
88                 else:
89                         print "Invalid char. \"%s\" entered. Topic not set." \
90                                 % (topics[index])
91                         invalidData = True
92                         break
93
94                 if topicTemp:
95                         if int(topicTemp) > 999:
96                                 invalidData = True
97                                 print "Invalid Topic ID %s (range 0-999). \
98                                         \nCould not register." % topicTemp
99
100                 index = index + 1
101
102         if invalidData == False:
103                 try:
104                         print "Setting Cell Broadcast topics..."
105                         cbs.SetProperty("Topics", topics);
106                 except dbus.DBusException, e:
107                         print "Unable to set topic: ", e
108
109         print "-----------------------------------------------------------"
110
111 def get_serving_cell_name(netReg):
112         wasFound = False;
113         properties = netReg.GetProperties()
114
115         for p in properties:
116                 if p == "BaseStation":
117                         if len(properties[p].__str__()) > 0:
118                                 print "Current serving cell name: %s" \
119                                         % (properties["BaseStation"])
120                                 wasFound = True;
121                         else:
122                                 print "Current Serving cell name empty. \
123                                         Base Station CBS not available."
124
125         if wasFound == False:
126                 print "Base Station parameter not found. \
127                         \nBase Station CBS not available."
128         print "-----------------------------------------------------------"
129
130 def stdin_handler(fd, condition, cbs, netReg):
131         in_key = os.read(fd.fileno(), 160).rstrip()
132
133         if in_key == '0':
134                 set_cbs_state(cbs, True)
135
136         elif in_key == '1':
137                 set_cbs_state(cbs, False)
138
139         elif in_key == '2':
140                 print_cbs_properties(cbs)
141
142         elif in_key == '3':
143                 set_topics(cbs)
144
145         elif in_key == '4':
146                 cbs.SetProperty("Topics", "")
147
148         elif in_key == '5':
149                 get_serving_cell_name(netReg)
150
151         elif in_key == 'x':
152                 sys.exit(1)
153
154         print '\n' * 2
155         print_menu()
156
157         return True
158
159 if __name__ == "__main__":
160
161         dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
162         bus = dbus.SystemBus()
163
164         manager = dbus.Interface(bus.get_object('org.ofono', '/'),
165                                                 'org.ofono.Manager')
166
167         modems = manager.GetModems()
168         path = modems[0][0]
169
170         cbs = dbus.Interface(bus.get_object('org.ofono', path),
171                                 'org.ofono.CellBroadcast')
172
173         netReg = dbus.Interface(bus.get_object('org.ofono', path),
174                                 'org.ofono.NetworkRegistration')
175
176         cbs.connect_to_signal("PropertyChanged", property_changed)
177         cbs.connect_to_signal("IncomingBroadcast", incoming_broadcast)
178         cbs.connect_to_signal("EmergencyBroadcast", emergency_broadcast)
179
180         print '\n' * 2
181
182         print_menu()
183
184         gobject.io_add_watch(sys.stdin, gobject.IO_IN, stdin_handler, cbs, \
185                                 netReg)
186
187         mainloop = gobject.MainLoop()
188         mainloop.run()