1#!/usr/bin/python
2#
3# Copyright (C) 2013 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9#      http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17
18# usage:
19# list-active-service [[service-type [[service-prop1] ... [service-propN]]]]
20#     service-type: shill service type: wifi, ethernet, etc
21#     service-propX: shill service property: Connectable, Name, etc
22#
23# Queries the currently active services from shill, optionally filtering
24# for specific service types and properties
25
26import dbus, flimflam, sys
27
28def main():
29    if len(sys.argv) > 1 and str(sys.argv[1]) in ['-h','-?','--help','-help']:
30        print('usage: %s [[service-type [[service-prop1] ... [service-propN]]]]'
31            % str(sys.argv[0]))
32        return
33
34    flim = flimflam.FlimFlam(dbus.SystemBus())
35
36    for service in flim.GetObjectList('Service'):
37        properties = service.GetProperties(utf8_strings = True)
38        if not bool(properties['IsActive']):
39            continue
40
41        if len(sys.argv) > 1 and str(properties['Type']) != sys.argv[1]:
42            continue
43
44        if len(sys.argv) > 2:
45            requested_keys = sys.argv[2:]
46        else:
47            print('[ %s ]' % service.object_path)
48            requested_keys = properties.keys()
49
50        for key in requested_keys:
51            print('    %s = %s' % (
52                    key, flimflam.convert_dbus_value(properties[key], 4)))
53
54if __name__ == '__main__':
55    main()
56