def test_dhcp_plaint_wifi(self): buf = b"method=dhcp\x00ssid=UNITTEST\x00TRASH=TRASH" real_options = { "method": "dhcp", "ssid": "UNITTEST", "scan_ssid": "0", "wifi_mode": "client", "security": None } options = NCE.parse_bytes(buf) self.assertDictEqual(real_options, options) buf2 = NCE.to_bytes(options) self.assertDictEqual(real_options, NCE.parse_bytes(buf2))
def test_static_ip_no_wifi(self): buf = b"method=static\x00ipaddr=192.168.100.200\x00mask=24\x00" \ b"ns=8.8.8.8,8.8.4.4" real_options = { "method": "static", "mask": 24, "ipaddr": "192.168.100.200", "ns": ["8.8.8.8", "8.8.4.4"], "route": None } options = NCE.parse_bytes(buf) self.assertEqual(options, real_options) buf2 = NCE.to_bytes(options) self.assertEqual(NCE.parse_bytes(buf2), real_options)
def test_dhcp_psk_wifi(self): buf = b"method=dhcp\x00ssid=UNITTEST\x00security=WPA2-PSK\x00" \ b"psk=wifipasswd12345678" real_options = { "method": "dhcp", "ssid": "UNITTEST", "wifi_mode": "client", "security": "WPA2-PSK", "psk": "wifipasswd12345678", "scan_ssid": "0" } options = NCE.parse_bytes(buf) self.assertDictEqual(real_options, options) buf2 = NCE.to_bytes(options) self.assertDictEqual(real_options, NCE.parse_bytes(buf2))
def on_config_network(self, buf): try: options = NCE.parse_bytes(buf) if "ifname" not in options: options["ifname"] = "wlan0" request_data = NCE.build_network_config_request(options) NCE.send_network_config_request(request_data) self.send_response(REQ_CONFIG_NETWORK, True, MSG_OK) except ValueError as e: self.send_response(REQ_CONFIG_NETWORK, False, b"BAD_PARAMS syntax") return except KeyError as e: self.send_response(REQ_CONFIG_NETWORK, False, ("BAD_PARAMS %s" % e.args[0]).encode()) return
def on_message(self, watcher, revent): try: buf = self.recv(4096) payloads = buf.split("\x00", 1) if len(payloads) == 2: cmd, data = payloads if cmd == "config_network": self.config_network(NCE.parse_bytes(data)) elif cmd == "power_change": self.master.on_activate_changed() else: self.master.logger.error("Unknow cmd: %s" % cmd) else: self.master.logger.error("Can not process request: %s" % buf) return except Exception: logger.exception("Unhandle error")