Add configuration file for DBus access
[platform/upstream/connman.git] / 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("net.connman", "/"),
9                                         "net.connman.Manager")
10
11 if len(sys.argv) < 2:
12         print "Usage: %s <command>" % (sys.argv[0])
13         print ""
14         print "  state"
15         print "  services"
16         print "  autoconnect <service> [autoconnect]"
17         print "  connect <service>"
18         print "  disconnect <service>"
19         print "  remove <service>"
20         print ""
21         print "  scan <type>"
22         print "  enable <type>"
23         print "  disable <type>"
24         print "  offlinemode [on|off]"
25         sys.exit(1)
26
27 def print_services(services):
28         for path, properties in services:
29                 identifier = path[path.rfind("/") + 1:]
30                 state = " "
31                 autoconnect = "  "
32
33                 if properties["Favorite"] == dbus.Boolean(1):
34                         favorite = "*"
35
36                         if properties["AutoConnect"] == dbus.Boolean(1):
37                                 autoconnect = " A"
38                         else:
39                                 autoconnect = "  "
40
41                         if properties["State"] == "ready":
42                                 state = "R"
43                         elif properties["State"] == "online":
44                                 state = "O"
45                 else:
46                         favorite = " "
47
48                 if "Name" in properties.keys():
49                         name = properties["Name"]
50                 else:
51                         name = "{" + properties["Type"] + "}"
52
53                 print "%s%s%s %-26s { %s }" % (favorite, autoconnect, state,
54                                                name, identifier)
55
56 if sys.argv[1] == "state":
57         properties = manager.GetProperties()
58
59         print "System is %s" % (properties["State"])
60
61 elif sys.argv[1] in ["services", "list", "show"]:
62         print_services(manager.GetServices())
63
64 elif sys.argv[1] in ["autoconnect", "autoconn"]:
65         if (len(sys.argv) < 3):
66                 print "Need at least service parameter"
67                 sys.exit(1)
68
69         path = "/net/connman/service/" + sys.argv[2]
70
71         service = dbus.Interface(bus.get_object("net.connman", path),
72                                                 "net.connman.Service")
73
74         if (len(sys.argv) > 3):
75                 flag = sys.argv[3].strip().lower()
76                 autoconnect = dbus.Boolean(flag not in ['false', 'f', 'n', '0'])
77
78                 service.SetProperty("AutoConnect", autoconnect);
79
80                 print "Auto connect %s for %s" % (autoconnect, sys.argv[2])
81         else:
82                 properties = service.GetProperties()
83
84                 if "Name" in properties.keys():
85                         name = properties["Name"]
86                 else:
87                         name = "{" + properties["Type"] + "}"
88
89                 if "AutoConnect" in properties.keys():
90                         autoconnect = properties["AutoConnect"]
91                 else:
92                         autoconnect = dbus.Boolean(0)
93
94                 print "Auto connect %s for %s" % (autoconnect, name)
95
96 elif sys.argv[1] in ["connect", "conn"]:
97         if (len(sys.argv) < 3):
98                 print "Need at least service parameter"
99                 sys.exit(1)
100
101         path = "/net/connman/service/" + sys.argv[2]
102
103         service = dbus.Interface(bus.get_object("net.connman", path),
104                                                 "net.connman.Service")
105
106         try:
107                 service.Connect(timeout=60000)
108         except dbus.DBusException, error:
109                 print "%s: %s" % (error._dbus_error_name, error.message)
110
111 elif sys.argv[1] in ["disconnect", "disc"]:
112         if (len(sys.argv) < 3):
113                 print "Need at least service parameter"
114                 sys.exit(1)
115
116         path = "/net/connman/service/" + sys.argv[2]
117
118         service = dbus.Interface(bus.get_object("net.connman", path),
119                                                 "net.connman.Service")
120
121         try:
122                 service.Disconnect()
123         except dbus.DBusException, error:
124                 print "%s: %s" % (error._dbus_error_name, error.message)
125
126 elif sys.argv[1] in ["remove"]:
127         if (len(sys.argv) < 3):
128                 print "Need at least service parameter"
129                 sys.exit(1)
130
131         path = "/net/connman/service/" + sys.argv[2]
132
133         service = dbus.Interface(bus.get_object("net.connman", path),
134                                                 "net.connman.Service")
135
136         properties = service.GetProperties()
137
138         if properties["Favorite"] == dbus.Boolean(0):
139                 print "Only favorite services can be removed"
140                 sys.exit(1)
141
142         try:
143                 service.Remove()
144         except dbus.DBusException, error:
145                 print "%s: %s" % (error._dbus_error_name, error.message)
146
147 elif sys.argv[1] == "scan":
148         if len(sys.argv) == 3:
149                 path = "/net/connman/technology/" + sys.argv[2]
150                 technology = dbus.Interface(bus.get_object("net.connman", path),
151                                                 "net.connman.Technology")
152                 technology.Scan()
153         else:
154                 print "'%s' takes two arguments" % sys.argv[1]
155
156 elif sys.argv[1] == "enable":
157         if len(sys.argv) == 3:
158                 path = "/net/connman/technology/" + sys.argv[2]
159                 technology = dbus.Interface(bus.get_object("net.connman", path),
160                                             "net.connman.Technology")
161                 technology.SetProperty("Powered", True)
162         else:
163                 print "'%s' takes two arguments" % sys.argv[1]
164
165 elif sys.argv[1] == "disable":
166         if len(sys.argv) == 3:
167                 path = "/net/connman/technology/" + sys.argv[2]
168                 technology = dbus.Interface(bus.get_object("net.connman", path),
169                                             "net.connman.Technology")
170                 technology.SetProperty("Powered", False)
171         else:
172                 print "'%s' takes two arguments" % sys.argv[1]
173
174
175 elif sys.argv[1] in ["offlinemode", "flightmode"]:
176         if len(sys.argv) == 3:
177                 if sys.argv[2] == "on" or sys.argv[2] == "1" or sys.argv[2] == "yes":
178                         active = dbus.Boolean(1)
179                 elif sys.argv[2] == "off" or sys.argv[2] == "0" or sys.argv[2] == "no":
180                         active = dbus.Boolean(0)
181                 else:
182                         print "Use either 'on', '1', 'yes', 'off', '0' or 'no'"
183                         exit()
184
185                 manager.SetProperty("OfflineMode", active)
186         elif len(sys.argv) == 2:
187                 properties = manager.GetProperties()
188                 print "Offline mode is %s" % (properties["OfflineMode"])
189         else:
190                 print "'%s' takes max. two arguments" % sys.argv[1]
191
192 else:
193         print "Unknown command"