设为首页收藏本站

 找回密码
 注册

QQ登录

只需一步,快速开始

楼主: Test

庖丁解牛fqrouter - 整体构成

[复制链接]
 楼主| 发表于 2015-10-24 20:53:09 | 显示全部楼层
  1.      def list_existing_networks(iface, control_socket_dir):
  2.     output = shell_execute('%s -p %s -i %s list_network' % (P2P_CLI_PATH, control_socket_dir, iface))
  3.     LOGGER.info('existing networks: %s' % output)
  4.     existing_networks = {}
  5.     for line in output.splitlines(False)[1:]:
  6.     index, ssid, bssid, status = line.split('\t')
  7.     existing_networks[int(index)] = {
  8.     'ssid': ssid,
  9.     'bssid': bssid,
  10.     'status': status
  11.     }
  12.     return existing_networks


  13.     def get_p2p_supplicant_control_socket_dir():
  14.     try:
  15.     return get_wpa_supplicant_control_socket_dir(P2P_SUPPLICANT_CONF_PATH)
  16.     except:
  17.     LOGGER.exception('failed to get p2p supplicant control socket dir')
  18.     return get_wpa_supplicant_control_socket_dir()


  19.     def get_wpa_supplicant_control_socket_dir(conf_path=WPA_SUPPLICANT_CONF_PATH):
  20.     if shell.USE_SU:
  21.     content = shell_execute('/data/data/fq.router2/busybox cat %s' % conf_path)
  22.     return parse_wpa_supplicant_conf(content)
  23.     try:
  24.     if not os.path.exists(conf_path):
  25.     raise Exception('can not find wpa_supplicant.conf')
  26.     with open(conf_path) as f:
  27.     content = f.read()
  28.     control_socket_dir = parse_wpa_supplicant_conf(content)
  29.     if WPA_SUPPLICANT_CONF_PATH == conf_path:
  30.     control_socket_dir = fix_wrong_control_socket_dir(control_socket_dir)
  31.     if control_socket_dir:
  32.     return control_socket_dir
  33.     else:
  34.     raise Exception('can not find ctrl_interface dir from wpa_supplicant.conf')
  35.     except:
  36.     LOGGER.exception('failed to get wpa_supplicant control socket dir')
  37.     return '/data/misc/wifi/wpa_supplicant'


  38.     def fix_wrong_control_socket_dir(control_socket_dir):
  39.     if os.path.exists('/data/system/wpa_supplicant/%s' % WIFI_INTERFACE):
  40.     return '/data/system/wpa_supplicant'
  41.     if os.path.exists('/data/misc/wifi/wpa_supplicant/%s' % WIFI_INTERFACE):
  42.     return '/data/misc/wifi/wpa_supplicant'
  43.     if control_socket_dir == WIFI_INTERFACE:
  44.     return WIFI_INTERFACE
  45.     if control_socket_dir:
  46.     control_socket_not_exists = not os.path.exists(
  47.     os.path.join(control_socket_dir, WIFI_INTERFACE))
  48.     else:
  49.     control_socket_not_exists = True
  50.     dev_socket_exists = os.path.exists('/dev/socket/wpa_%s' % WIFI_INTERFACE)
  51.     if dev_socket_exists and control_socket_not_exists:
  52.     return WIFI_INTERFACE # any valid dir will cause wpa_cli fail
  53.     return control_socket_dir


  54.     def parse_wpa_supplicant_conf(content):
  55.     for line in content.splitlines():
  56.     line = line.strip()
  57.     if not line:
  58.     continue
  59.     if line.startswith('ctrl_interface='):
  60.     line = line.replace('ctrl_interface=', '')
  61.     parts = line.split(' ')
  62.     for part in parts:
  63.     if part.startswith('DIR='): # if there is DIR=
  64.     return part.replace('DIR=', '')
  65.     return line # otherwise just return the ctrl_interface=
  66.     return None


  67.     def netd_execute(command):
  68.     if shell.USE_SU:
  69.     return shell.launch_python('main', ['netd-execute', '"%s"' % command])
  70.     global netd_sequence_number
  71.     netd_socket = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
  72.     try:
  73.     netd_socket.connect('/dev/socket/netd')
  74.     if netd_sequence_number:
  75.     netd_sequence_number += 1
  76.     LOGGER.info('send: %s %s' % (netd_sequence_number, command))
  77.     netd_socket.send('%s %s\0' % (netd_sequence_number, command))
  78.     else:
  79.     LOGGER.info('send: %s' % command)
  80.     netd_socket.send('%s\0' % command)
  81.     output = netd_socket.recv(1024)
  82.     LOGGER.info('received: %s' % output)
  83.     if not netd_sequence_number and 'Invalid sequence number' in output:
  84.     LOGGER.info('resend command to netd with sequence number')
  85.     netd_sequence_number = 1
  86.     netd_execute(command)
  87.     finally:
  88.     netd_socket.close()


  89.     def shell_execute(command):
  90.     LOGGER.info('execute: %s' % command)
  91.     try:
  92.     output = shell.check_output(shlex.split(command) if isinstance(command, basestring) else command)
  93.     LOGGER.info('succeed, output: %s' % output)
  94.     except subprocess.CalledProcessError, e:
  95.     LOGGER.error('failed, output: %s' % e.output)
  96.     raise
  97.     return output
复制代码


 楼主| 发表于 2015-10-24 20:55:50 | 显示全部楼层
Pick & Play:

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|小黑屋|BC Morning Website ( Best Deal Inc. 001 )  

GMT-8, 2025-8-26 09:37 , Processed in 0.017556 second(s), 16 queries .

Supported by Best Deal Online X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表