|
楼主 |
发表于 2015-10-24 20:53:09
|
显示全部楼层
- def list_existing_networks(iface, control_socket_dir):
- output = shell_execute('%s -p %s -i %s list_network' % (P2P_CLI_PATH, control_socket_dir, iface))
- LOGGER.info('existing networks: %s' % output)
- existing_networks = {}
- for line in output.splitlines(False)[1:]:
- index, ssid, bssid, status = line.split('\t')
- existing_networks[int(index)] = {
- 'ssid': ssid,
- 'bssid': bssid,
- 'status': status
- }
- return existing_networks
- def get_p2p_supplicant_control_socket_dir():
- try:
- return get_wpa_supplicant_control_socket_dir(P2P_SUPPLICANT_CONF_PATH)
- except:
- LOGGER.exception('failed to get p2p supplicant control socket dir')
- return get_wpa_supplicant_control_socket_dir()
- def get_wpa_supplicant_control_socket_dir(conf_path=WPA_SUPPLICANT_CONF_PATH):
- if shell.USE_SU:
- content = shell_execute('/data/data/fq.router2/busybox cat %s' % conf_path)
- return parse_wpa_supplicant_conf(content)
- try:
- if not os.path.exists(conf_path):
- raise Exception('can not find wpa_supplicant.conf')
- with open(conf_path) as f:
- content = f.read()
- control_socket_dir = parse_wpa_supplicant_conf(content)
- if WPA_SUPPLICANT_CONF_PATH == conf_path:
- control_socket_dir = fix_wrong_control_socket_dir(control_socket_dir)
- if control_socket_dir:
- return control_socket_dir
- else:
- raise Exception('can not find ctrl_interface dir from wpa_supplicant.conf')
- except:
- LOGGER.exception('failed to get wpa_supplicant control socket dir')
- return '/data/misc/wifi/wpa_supplicant'
- def fix_wrong_control_socket_dir(control_socket_dir):
- if os.path.exists('/data/system/wpa_supplicant/%s' % WIFI_INTERFACE):
- return '/data/system/wpa_supplicant'
- if os.path.exists('/data/misc/wifi/wpa_supplicant/%s' % WIFI_INTERFACE):
- return '/data/misc/wifi/wpa_supplicant'
- if control_socket_dir == WIFI_INTERFACE:
- return WIFI_INTERFACE
- if control_socket_dir:
- control_socket_not_exists = not os.path.exists(
- os.path.join(control_socket_dir, WIFI_INTERFACE))
- else:
- control_socket_not_exists = True
- dev_socket_exists = os.path.exists('/dev/socket/wpa_%s' % WIFI_INTERFACE)
- if dev_socket_exists and control_socket_not_exists:
- return WIFI_INTERFACE # any valid dir will cause wpa_cli fail
- return control_socket_dir
- def parse_wpa_supplicant_conf(content):
- for line in content.splitlines():
- line = line.strip()
- if not line:
- continue
- if line.startswith('ctrl_interface='):
- line = line.replace('ctrl_interface=', '')
- parts = line.split(' ')
- for part in parts:
- if part.startswith('DIR='): # if there is DIR=
- return part.replace('DIR=', '')
- return line # otherwise just return the ctrl_interface=
- return None
- def netd_execute(command):
- if shell.USE_SU:
- return shell.launch_python('main', ['netd-execute', '"%s"' % command])
- global netd_sequence_number
- netd_socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
- try:
- netd_socket.connect('/dev/socket/netd')
- if netd_sequence_number:
- netd_sequence_number += 1
- LOGGER.info('send: %s %s' % (netd_sequence_number, command))
- netd_socket.send('%s %s\0' % (netd_sequence_number, command))
- else:
- LOGGER.info('send: %s' % command)
- netd_socket.send('%s\0' % command)
- output = netd_socket.recv(1024)
- LOGGER.info('received: %s' % output)
- if not netd_sequence_number and 'Invalid sequence number' in output:
- LOGGER.info('resend command to netd with sequence number')
- netd_sequence_number = 1
- netd_execute(command)
- finally:
- netd_socket.close()
- def shell_execute(command):
- LOGGER.info('execute: %s' % command)
- try:
- output = shell.check_output(shlex.split(command) if isinstance(command, basestring) else command)
- LOGGER.info('succeed, output: %s' % output)
- except subprocess.CalledProcessError, e:
- LOGGER.error('failed, output: %s' % e.output)
- raise
- return output
复制代码
|
|