connect-p2p (4408B)
1 #!/usr/bin/python3 2 3 import sys 4 import dbus 5 import dbus.mainloop.glib 6 from gi.repository import GLib 7 import fnmatch 8 9 if len(sys.argv) not in [1, 2]: 10 print("Usage: %s [<name_mask>]" % (sys.argv[0],)) 11 sys.exit(1) 12 13 name_mask = None 14 if len(sys.argv) >= 2: 15 name_mask = sys.argv[1] 16 17 DEVICE_IF = 'net.connman.iwd.p2p.Device' 18 PEER_IF = 'net.connman.iwd.p2p.Peer' 19 WSC_IF = 'net.connman.iwd.SimpleConfiguration' 20 21 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) 22 23 bus = dbus.SystemBus() 24 manager = dbus.Interface(bus.get_object('net.connman.iwd', '/'), 25 'org.freedesktop.DBus.ObjectManager') 26 objects = manager.GetManagedObjects() 27 28 p2p_path = None 29 peers = {} 30 connecting = False 31 mainloop = None 32 33 for path in objects: 34 if DEVICE_IF in objects[path] and p2p_path is None: 35 p2p_path = path 36 37 if p2p_path is None: 38 print('No P2P interfaces') 39 sys.exit(-1) 40 41 values = objects[p2p_path][DEVICE_IF] 42 properties = dbus.Interface(bus.get_object('net.connman.iwd', p2p_path), 43 'org.freedesktop.DBus.Properties') 44 p2p = dbus.Interface(bus.get_object('net.connman.iwd', p2p_path), DEVICE_IF) 45 46 def list_peers(): 47 global peers 48 49 print('We have the following P2P peers') 50 for path in peers: 51 result = peers[path] 52 53 print(' ' + result['Name'] + ' at ' + path) 54 55 def connect(path): 56 global connecting, peers, mainloop 57 58 list_peers() 59 60 peer = dbus.Interface(bus.get_object('net.connman.iwd', path), PEER_IF) 61 peer_wsc = dbus.Interface(bus.get_object('net.connman.iwd', path), WSC_IF) 62 peer_props = peers[path] 63 64 print('Connecting to ' + peer_props['Name']) 65 connecting = True 66 try: 67 peer_wsc.PushButton(timeout=120) 68 print('New connection') 69 except Exception as e: 70 connecting = False 71 print('Connection error: ' + repr(e)) 72 73 if mainloop: 74 mainloop.quit() 75 else: 76 sys.exit(0) 77 78 def properties_changed(interface, changed, invalidated, path): 79 global peers 80 81 path = str(path) 82 if interface != PEER_IF or path not in peers: 83 return 84 85 peers[path].update(changed) 86 for k in invalidated: 87 del peers[path][k] 88 89 def interfaces_added(path, interfaces): 90 global connecting, peers 91 92 if PEER_IF not in interfaces: 93 return 94 95 path = str(path) 96 peers[path] = interfaces[PEER_IF] 97 print('Added peer ' + str(peers[path]['Name'])) 98 99 if connecting or name_mask is None or not fnmatch.fnmatch(peers[path]['Name'], name_mask): 100 return 101 102 connect(path) 103 104 def interfaces_removed(path, interfaces): 105 global peers 106 107 if PEER_IF not in interfaces: 108 return 109 110 print('Removed peer ' + str(peers[path]['Name'])) 111 del peers[path] 112 113 if not values['Enabled']: 114 properties.Set(DEVICE_IF, 'Enabled', True) 115 116 if not values['AvailableConnections']: 117 # See if we have peers we can disconnect or where we can cancel an ongoing 118 # connection that may be in the 120s GO Negotiation timeout for example. 119 # Should we have a Connecting property on the WSC interface so we can know 120 # whether a connection is in progress? 121 for path in objects: 122 if PEER_IF not in objects[path]: 123 continue 124 peer_values = objects[path][PEER_IF] 125 print('Trying to disconnect from peer ' + str(peer_values['Name'])) 126 peer = dbus.Interface(bus.get_object('net.connman.iwd', path), PEER_IF) 127 try: 128 peer.Disconnect() 129 except: 130 pass 131 132 for path in objects: 133 if PEER_IF in objects[path]: 134 interfaces_added(path, objects[path]) 135 136 p2p.RequestDiscovery() 137 138 bus.add_signal_receiver(properties_changed, 139 bus_name="net.connman.iwd", 140 dbus_interface="org.freedesktop.DBus.Properties", 141 signal_name="PropertiesChanged", 142 path_keyword="path") 143 144 bus.add_signal_receiver(interfaces_added, 145 bus_name="net.connman.iwd", 146 dbus_interface="org.freedesktop.DBus.ObjectManager", 147 signal_name="InterfacesAdded") 148 149 bus.add_signal_receiver(interfaces_removed, 150 bus_name="net.connman.iwd", 151 dbus_interface="org.freedesktop.DBus.ObjectManager", 152 signal_name="InterfacesRemoved") 153 154 mainloop = GLib.MainLoop() 155 mainloop.run()