Indicate the current active profile
[connman] / test / test-connman
1 #!/usr/bin/python
2
3 import sys
4 import dbus
5
6 bus = dbus.SystemBus()
7
8 manager = dbus.Interface(bus.get_object("org.moblin.connman", "/"),
9                                         "org.moblin.connman.Manager")
10
11 if len(sys.argv) < 2:
12         print "Usage: %s <command>" % (sys.argv[0])
13         print ""
14         print "  state"
15         print "  scan [ <interface> ]"
16         print "  dev <interface>"
17         print "  dev <interface> scan"
18         print "  dev <interface> networks"
19         print "  dev <interface> connect <network>"
20         print "  dev <interface> remember <network>"
21         print "  dev <interface> disconnect [network]"
22         print "  dev <interface> policy [ignore|off|auto|manual]"
23         print "  dev <interface> powered [on|off]"
24         print "  dev <interface> priority [0-255]"
25         sys.exit(1)
26
27 def print_properties(path, properties):
28         print "[ %s ]" % (path)
29         for key in properties.keys():
30                 if key == "Networks":
31                         continue
32
33                 if key in ["Powered", "Scanning", "Connected",
34                                         "Available", "Remember", "Default"]:
35                         if properties[key] == dbus.Boolean(1):
36                                 val = "true"
37                         else:
38                                 val = "false"
39                 elif key in ["Strength", "Priority"]:
40                         val = int(properties[key])
41                 else:
42                         val = str(properties[key])
43
44                 print "     %s = %s" % (key, val)
45
46         if "Networks" in properties.keys():
47                 list = ""
48                 for path in properties["Networks"]:
49                         val = str(path)
50                         list = list + val[val.rfind("/") + 1:] + " "
51                 print "     Networks = [ %s]" % (list)
52
53 def print_networks(networks):
54         for path in networks:
55                 network = dbus.Interface(bus.get_object("org.moblin.connman", path),
56                                                 "org.moblin.connman.Network")
57
58                 properties = network.GetProperties()
59
60                 if properties["Connected"] == dbus.Boolean(1):
61                         connected = "*"
62                 else:
63                         connected = " "
64
65                 name = properties["Name"]
66                 strength = int(properties["Strength"])
67
68                 details = ""
69                 try:
70                         details += "{" + properties["WiFi.Mode"] + "} "
71                 except:
72                         pass
73                 try:
74                         details += "{" + properties["WiFi.Security"] + "} "
75                 except:
76                         pass
77                 if "WiFi.Passphrase" in properties.keys():
78                         if properties["WiFi.Passphrase"] != "":
79                                 details += "{passphrase present}"
80
81                 print "%s %-20s %3d%%  %s" % (connected,
82                                                 name, strength, details)
83
84 def select_network(networks, name):
85         for path in networks:
86                 network = dbus.Interface(bus.get_object("org.moblin.connman", path),
87                                                 "org.moblin.connman.Network")
88
89                 properties = network.GetProperties()
90
91                 if properties["Name"] != name:
92                         continue
93
94                 if properties["Connected"] == dbus.Boolean(1):
95                         print "Already connected to network %s" % (name)
96                         break
97
98                 print "Selecting network %s" % (name)
99
100                 network.Connect()
101
102 def remember_network(networks, name):
103         for path in networks:
104                 network = dbus.Interface(bus.get_object("org.moblin.connman", path),
105                                                 "org.moblin.connman.Network")
106
107                 properties = network.GetProperties()
108
109                 if properties["Name"] != name:
110                         continue
111
112                 if properties["Remember"] == dbus.Boolean(1):
113                         print "Already a known network %s" % (name)
114                         break
115
116                 print "Remembering network %s" % (name)
117
118                 network.SetProperty("Remember", dbus.Boolean(1))
119
120 def disconnect_network(networks, name):
121         for path in networks:
122                 network = dbus.Interface(bus.get_object("org.moblin.connman", path),
123                                                 "org.moblin.connman.Network")
124
125                 properties = network.GetProperties()
126
127                 if name != "" and properties["Name"] != name:
128                         continue
129
130                 if properties["Connected"] == dbus.Boolean(1):
131                         name = properties["Name"]
132                         print "Disconnecting from network %s" % (name)
133                         network.Disconnect()
134
135 if sys.argv[1] == "state":
136         properties = manager.GetProperties()
137
138         print "System is %s" % (properties["State"])
139
140 elif sys.argv[1] == "scan":
141         properties = manager.GetProperties()
142
143         interface = ""
144         found = 0
145
146         if len(sys.argv) > 2:
147                 interface = sys.argv[2]
148
149         for path in properties["Devices"]:
150                 device = dbus.Interface(bus.get_object("org.moblin.connman", path),
151                                                 "org.moblin.connman.Device")
152
153                 properties = device.GetProperties()
154
155                 if interface != "" and properties["Interface"] != interface:
156                         continue
157
158                 if properties["Type"] in ["wifi", "wimax"]:
159                         interface = properties["Interface"]
160                         print "Propose scanning for device %s" % (interface)
161                         device.ProposeScan()
162                         found = 1
163                 elif interface != "":
164                         print "No scanning support for device %s" % (interface)
165                         found = 1
166
167         if found == 0:
168                 print "No such device"
169
170 elif sys.argv[1] == "dev":
171         properties = manager.GetProperties()
172
173         interface = ""
174         command = ""
175         value = ""
176         found = 0
177
178         if len(sys.argv) > 2:
179                 interface = sys.argv[2]
180                 if len(sys.argv) > 3:
181                         command = sys.argv[3]
182                         if len(sys.argv) > 4:
183                                 value = sys.argv[4]
184
185         for path in properties["Devices"]:
186                 device = dbus.Interface(bus.get_object("org.moblin.connman", path),
187                                                 "org.moblin.connman.Device")
188
189                 properties = device.GetProperties()
190
191                 if interface != "" and properties["Interface"] != interface:
192                         continue
193
194                 if command == "scan":
195                         if properties["Type"] in ["wifi", "wimax"]:
196                                 interface = properties["Interface"]
197                                 print "Scan for device %s" % (interface)
198                                 device.ProposeScan()
199                         else:
200                                 print "No scanning for device %s" % (interface)
201                 elif command in ["networks", "net"]:
202                         if "Networks" in properties.keys():
203                                 print_networks(properties["Networks"])
204                         else:
205                                 print "Device has no networks"
206                 elif command in ["connect", "conn"] and value != "":
207                         if "Networks" in properties.keys():
208                                 select_network(properties["Networks"], value)
209                         else:
210                                 print "Device can't connect networks"
211                 elif command in ["connect", "conn"]:
212                         print "Need to specify network"
213                 elif command in ["remember", "known"] and value != "":
214                         if "Networks" in properties.keys():
215                                 remember_network(properties["Networks"], value)
216                         else:
217                                 print "Device has no networks"
218                 elif command in ["remember", "known"]:
219                         print "Need to specify network"
220                 elif command in ["disconnect", "disc"] and value != "":
221                         if "Networks" in properties.keys():
222                                 disconnect_network(properties["Networks"], value)
223                         else:
224                                 print "Device has no networks"
225                 elif command in ["discconnect", "disc"]:
226                         if "Networks" in properties.keys():
227                                 disconnect_network(properties["Networks"], "")
228                         else:
229                                 print "Device has no networks"
230                 elif command == "policy" and value != "":
231                         policy = value
232                         device.SetProperty("Policy", policy)
233                 elif command == "policy":
234                         interface = properties["Interface"]
235                         policy = properties["Policy"]
236                         print "Policy of device %s is %s" % (interface, policy)
237                 elif command == "powered" and value != "":
238                         if value == "on":
239                                 powered = dbus.Boolean(1)
240                         elif value == "off":
241                                 powered = dbus.Boolean(0)
242                         else:
243                                 powered = dbus.Boolean(value)
244                         device.SetProperty("Powered", powered)
245                 elif command == "powered":
246                         interface = properties["Interface"]
247                         if properties["Powered"] == dbus.Boolean(1):
248                                 powered = "on"
249                         else:
250                                 powered = "off"
251                         print "Device %s is powered %s" % (interface, powered)
252                 elif command == "priority" and value != "":
253                         priority = int(value)
254                         device.SetProperty("Priority", priority)
255                 elif command == "priority":
256                         interface = properties["Interface"]
257                         priority = properties["Priority"]
258                         print "Device %s has priority of %d" % (interface, priority)
259                 elif command == "list" or command == "":
260                         print_properties(path, properties)
261                 else:
262                         print "Unknown command"
263
264 else:
265         print "Unknown command"