|
| 1 | +#!/usr/bin/python |
| 2 | + |
| 3 | +import socket |
| 4 | +import subprocess |
| 5 | +import shutil |
| 6 | +import sys |
| 7 | +import os |
| 8 | +import urllib2 |
| 9 | +import fcntl |
| 10 | +import struct |
| 11 | +import json |
| 12 | + |
| 13 | +class WiFi(): |
| 14 | + |
| 15 | + CONFIG_FILE = "/etc/coderbot_wifi.conf" |
| 16 | + adapters = ["RT5370", "RTL8188CUS"] |
| 17 | + hostapds = {"RT5370": "hostapd.RT5370", "RTL8188CUS": "hostapd.RTL8188"} |
| 18 | + web_url = "http://coderbotsrv.appspot.com/register_ip" |
| 19 | + wifi_client_conf_file = "/etc/wpa_supplicant/wpa_supplicant.conf" |
| 20 | + _config = {} |
| 21 | + |
| 22 | + @classmethod |
| 23 | + def load_config(cls): |
| 24 | + f = open(cls.CONFIG_FILE) |
| 25 | + cls._config = json.load(f) |
| 26 | + return cls._config |
| 27 | + |
| 28 | + @classmethod |
| 29 | + def save_config(cls): |
| 30 | + f = open(cls.CONFIG_FILE, 'w') |
| 31 | + json.dump(cls._config, f) |
| 32 | + return cls._config |
| 33 | + |
| 34 | + @classmethod |
| 35 | + def get_config(cls): |
| 36 | + return cls._config |
| 37 | + |
| 38 | + @classmethod |
| 39 | + def get_adapter_type(cls): |
| 40 | + lsusb_out = subprocess.check_output("lsusb") |
| 41 | + for a in cls.adapters: |
| 42 | + if a in lsusb_out: |
| 43 | + return a |
| 44 | + return None |
| 45 | + |
| 46 | + @classmethod |
| 47 | + def start_hostapd(cls): |
| 48 | + adapter = cls.get_adapter_type() |
| 49 | + hostapd_type = cls.hostapds.get(adapter) |
| 50 | + try: |
| 51 | + print "starting hostapd..." |
| 52 | + os.system("start-stop-daemon --start --oknodo --quiet --exec /usr/sbin/" + hostapd_type + " -- /etc/hostapd/" + hostapd_type + " &") |
| 53 | + |
| 54 | + except subprocess.CalledProcessError as e: |
| 55 | + print e.output |
| 56 | + |
| 57 | + @classmethod |
| 58 | + def stop_hostapd(cls): |
| 59 | + try: |
| 60 | + out = subprocess.check_output(["pkill", "-9", "hostapd"]) |
| 61 | + print out |
| 62 | + except subprocess.CalledProcessError as e: |
| 63 | + print e.output |
| 64 | + |
| 65 | + @classmethod |
| 66 | + def get_ipaddr(cls, ifname): |
| 67 | + s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
| 68 | + return socket.inet_ntoa(fcntl.ioctl( |
| 69 | + s.fileno(), |
| 70 | + 0x8915, # SIOCGIFADDR |
| 71 | + struct.pack('256s', ifname[:15]) |
| 72 | + )[20:24]) |
| 73 | + ipaddr = socket.gethostbyname(socket.gethostname()) |
| 74 | + return ipaddr |
| 75 | + |
| 76 | + @classmethod |
| 77 | + def register_ipaddr(cls, ipaddr, botname): |
| 78 | + try: |
| 79 | + ret = urllib2.urlopen(cls.web_url + "?name=" + botname + "&ipaddr=" + ipaddr) |
| 80 | + if ret.getcode() != 200: |
| 81 | + raise Exception() |
| 82 | + except URLError as e: |
| 83 | + print e |
| 84 | + raise |
| 85 | + print botname, ": ", ipaddr |
| 86 | + |
| 87 | + @classmethod |
| 88 | + def get_wlans(cls): |
| 89 | + out = subprocess.check_output(["iwlist", "wlan0", "scan"]) |
| 90 | + |
| 91 | + @classmethod |
| 92 | + def set_client_params(cls, wssid, wpsk): |
| 93 | + f = open (cls.wifi_client_conf_file, "w+") |
| 94 | + f.write("""ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev |
| 95 | +update_config=1 |
| 96 | +network={\n""") |
| 97 | + f.write(" ssid=\""+wssid+"\"\n") |
| 98 | + f.write(" psk=\""+wpsk+"\"\n") |
| 99 | + f.write("}") |
| 100 | + |
| 101 | + @classmethod |
| 102 | + def set_start_as_client(cls): |
| 103 | + shutil.copy("/etc/network/interfaces_cli", "/etc/network/interfaces") |
| 104 | + cls._config["wifi_mode"] = "client" |
| 105 | + cls.save_config() |
| 106 | + |
| 107 | + @classmethod |
| 108 | + def start_as_client(cls): |
| 109 | + cls.stop_hostapd() |
| 110 | + try: |
| 111 | + out = subprocess.check_output(["ifdown", "wlan0"]) |
| 112 | + out = subprocess.check_output(["ifup", "wlan0"]) |
| 113 | + except subprocess.CalledProcessError as e: |
| 114 | + print e.output |
| 115 | + raise |
| 116 | + |
| 117 | + @classmethod |
| 118 | + def set_start_as_ap(cls): |
| 119 | + shutil.copy("/etc/network/interfaces_ap", "/etc/network/interfaces") |
| 120 | + cls._config["wifi_mode"] = "ap" |
| 121 | + cls.save_config() |
| 122 | + |
| 123 | + @classmethod |
| 124 | + def start_as_ap(cls): |
| 125 | + out = subprocess.check_output(["ifdown", "wlan0"]) |
| 126 | + out = subprocess.check_output(["ifup", "wlan0"]) |
| 127 | + cls.start_hostapd() |
| 128 | + |
| 129 | + |
| 130 | +def main(): |
| 131 | + print "starting wifi service..." |
| 132 | + w = WiFi() |
| 133 | + config = w.load_config() |
| 134 | + |
| 135 | + if config["wifi_mode"] == "ap": |
| 136 | + print "starting as ap..." |
| 137 | + w.start_as_ap() |
| 138 | + elif config["wifi_mode"] == "client": |
| 139 | + print "starting as client..." |
| 140 | + w.start_as_client() |
| 141 | + try: |
| 142 | + print "registering ip..." |
| 143 | + w.register_ipaddr(w.get_ipaddr("wlan0"), "CoderBot") |
| 144 | + except: |
| 145 | + print "Unable to register ip, revert to ap mode" |
| 146 | + w.start_as_ap() |
| 147 | + |
| 148 | +if __name__ == "__main__": |
| 149 | + main() |
| 150 | + |
0 commit comments