def try_connect(self): """ Trying to connect to the network """ logger.log("Trying to connect to the network {}".format(self.ssid)) # TODO: try to connect to the network using the given credentials cli = wpa.wpa( ["ssid {}".format(self.ssid), "psk {}".format(self.password)]) cli.execute() # give time to connect time.sleep(config.WIFI_WAIT_UNTIL_CONNECTION) return wifi.ConnectedToTheNetwork()
def getWifiData(client, clientInfo, server): """ Comminicate with the phone over bluetooth to gather the wifi data here """ if wifi.ConnectedToTheNetwork(): return logger.log("Receiving wifi credentials", logger.LOG_DEBUG) connection = None while 1: data = client.recv(size).decode("utf-8") if data: if "TYPE:" in data: type = extractData("TYPE", data) if type == "wpa2": connection = wifiConnection() elif type == "mchap": connection = wifiMCHAPConnection() else: client.send("ERROR:2 - Server doesn't recognize wifi type") if "SSID:" in data: if connection: connection.ssid = extractSSID(data) else: client.send("ERROR:1 - No connection specified") elif "PWD:" in data: if connection: connection.password = extractPassword(data) else: client.send("ERROR:1 - No connection specified") elif "USER:"******"ERROR:4 cannot set property that is not part of the connection type" ) else: client.send("ERROR:1 - No connection specified") elif "TRY:1" in data: if connection: if connection.try_connect( ): # try to connect to the network client.send("SUCCESS:1 - connected to a network") else: client.send("ERROR:3 - Network credentials are wrong") else: client.send("ERROR:1 - No connection specified") else: client.send(data) # Echo back to client
def idle(server): """ Wait until a bluetooth connection is made @server is the bluetooth connection server """ logger.log("Waiting for a bluetooth connection", logger.LOG_DEBUG) if wifi.ConnectedToTheNetwork(): return None, None try: client, clientInfo = server.accept() except: print("Closing socket") client.close() server.close() return client, clientInfo
def startup(server): """ Initialize a bluetooth server so that we can communicate with the client phone @server is the bluetooth connection server """ logger.log("Starting up the bluetooth module", logger.LOG_DEBUG) logger.log("Connected to bluetooth device: {} ".format(pair()), logger.LOG_DEBUG) if wifi.ConnectedToTheNetwork(): return server.bind((hostMACAddress, port)) server.listen(backlog) uuid = "94f39d29-7d6d-437d-973b-fba39e49d4ee" bluetooth.advertise_service( sock=server, name="Bluetooth Speaker", service_id=uuid, service_classes=[bluetooth.SERIAL_PORT_CLASS], profiles=[bluetooth.SERIAL_PORT_PROFILE], ) return server
def extractData(command, data): """ We split the payload and extract the command and value from it. If the command is equal to the expected command then we can return the value Otherwise we return a nullptr @command is a string containing the expected bluetooth command @data is the payload send over bluetooth (also a string) """ split = data.replace("\r\n", "").split(":") if len(data) < 2: logger.log("Incomming data payload is to small, {}".format(split)) return None if not split[0] == command: logger.log( "Extracted data doesn't match expected type, {} but got {} instead" .format(command, split[0])) return None logger.log( "Retreived data from bluetooth socker: {}".format("".join(split[1:])), logger.LOG_DEBUG, ) return "".join(split[1:])
def __init__(self): self.password = None self.username = None self.ssid = None logger.log("MCHAP connection created")
def __init__(self): self.password = None self.ssid = None logger.log("WPA2 connection created")
def startup(): if isConnectedTOWifi(): logger.log("Connected to the wifi") start_farm() establishConnection() start_farm()
def start_farm(): logger.log("Starting farm", logger.LOG_DEBUG)