Esempio n. 1
0
def on_upgrade_message(topic, payload_in):
    global file_count, manifest_checksum, manifest_size, manifest_url, version

    if payload_in.startswith("(upgrade "):
        tokens = payload_in[9:-1].split()
        if tokens[0] > common.AIKO_VERSION:
            version = tokens[0]
            manifest_url = tokens[1]
            manifest_checksum = tokens[2]
            manifest_size = int(tokens[3])
            file_count = int(tokens[4])
            common.log("Firmware upgrade available: " + version)
        return True
Esempio n. 2
0
def wifi_connect(wifi):
    global connected
    sta_if = network.WLAN(network.STA_IF)
    sta_if.active(True)
    oled.set_annunciator(common.ANNUNCIATOR_WIFI, "s", True)
    common.log("WiFi scan")
    aps = sta_if.scan()

    for ap in aps:  # Note 1
        for ssid in wifi:
            if ssid[0].encode() in ap:
                print(W + "Connecting: " + ssid[0])
                oled.set_annunciator(common.ANNUNCIATOR_WIFI, "c", True)
                common.log("WiFi connecting:" + ssid[0])
                sta_if.connect(ssid[0], ssid[1])
                for retry in range(WIFI_CONNECTING_RETRY_LIMIT):
                    if sta_if.isconnected():
                        print(W + "Connected: " + sta_if.ifconfig()[0])
                        common.log("WiFi connected: " + sta_if.ifconfig()[0])
                        connected = True
                        wifi_configuration_update(wifi)
                        break  # for retry


#         print(W + "Waiting")
                    sleep_ms(WIFI_CONNECTING_CLIENT_PERIOD)
                if sta_if.isconnected(): break  # for ssid
                print(W + "Timeout: Bad password ?")
                common.log("Timeout:        Bad password ?")
        if sta_if.isconnected(): break  # for ap
    return sta_if
Esempio n. 3
0
def wifi_configuration_update(wifi):
  global wifi_configuration_updated

  if wifi_configuration_updated:
    file = open("configuration/net.py", "w")
    file.write("wifi = [\n")
    for ssid_password in wifi:
      record = '  ("' + ssid_password[0] + '", "' + ssid_password[1] + '"),\n'
      file.write(record)
    file.write("]")
    file.close()
    print(W + "Configuration updated")
    common.log("WiFi configuration updated")
  wifi_configuration_updated = False
Esempio n. 4
0
def on_upgrade_message(topic, payload_in):
  global file_count, manifest_checksum, manifest_size, manifest_url, version

  if payload_in.startswith("(upgrade "):
    tokens = payload_in[9:-1].split()
    if tokens[0] > common.AIKO_VERSION:
      version = tokens[0]
      manifest_url = tokens[1]
      manifest_checksum = tokens[2]
      manifest_size = int(tokens[3])
      file_count = int(tokens[4])
      common.annunicator_log_symbol = "F"
      oled.set_annunciator(common.ANNUNCIATOR_LOG, common.annunicator_log_symbol, True)
      common.log("Firmware upgrade available: " + version)
    return True
Esempio n. 5
0
def wifi_configure(wifi):
    print(W + "WiFi configuration using SSID: " + WIFI_AP_SSID)
    common.log("Configure WiFi: " + WIFI_AP_SSID)

    ap_if = network.WLAN(network.AP_IF)
    ap_if.active(True)
    ap_if.config(essid=WIFI_AP_SSID)
    ap_if.config(max_clients=1)
    ip_address = ap_if.ifconfig()[0]
    aiko.net.set_status(led.yellow)
    oled.set_annunciator(common.ANNUNCIATOR_WIFI, "A", True)
    common.log("Try http://" + ip_address)
    ssid_password = web_server()
    ap_if.active(False)
    return ssid_password
Esempio n. 6
0
def connect(settings=configuration.mqtt.settings):
    global client, connected, keepalive, topic_path

    client_id = common.hostname()
    client = MQTTClient(client_id,
                        settings["host"],
                        settings["port"],
                        keepalive=keepalive)

    client.set_callback(on_message)
    client.set_last_will(topic_path + "/state", "nil")
    try:
        client.connect()
        aiko.event.add_timer_handler(mqtt_ping_handler, keepalive * 1000)

        for topic in settings["topic_subscribe"]:
            if topic.startswith("$all/"): topic = "+/+/+" + topic[4:]
            if topic.startswith("$me/"): topic = topic_path + topic[3:]
            client.subscribe(topic)

        connected = True
        print(M + "Connected to %s: %s" % (settings["host"], topic_path))
        common.log("MQTT connected ...")
        common.log("  " + settings["host"])
        common.log("  " + topic_path)
        payload_out = "(boot %s %s)" % (common.AIKO_VERSION, "swagbadge")
        client.publish(topic_path + "/out", payload_out)
    except Exception:
        disconnect("connect")
Esempio n. 7
0
def upgrade_thread():
  global in_progress
  global file_count, manifest_checksum, manifest_size, manifest_url, version
  try:
    common.log("Firmware upgrade start")
    gc.collect()

    manifest_pathname = "manifest"
    shutil.path_remove(manifest_pathname)
# TODO: Remove all "*_new" and "*_old"

    aiko.web_client.http_get_file(manifest_url, manifest_pathname)
# TODO: Verify "manifest_pathname" actual file size versus "manifest_size"
# TODO: Verify "manifest_pathname" checksum

    top_level_files = []
    url_prefix = manifest_url.rpartition("/")[0]
    with open(manifest_pathname, "r") as manifest_file:
      file_index = 0
      for line in manifest_file.readlines():
        file_index += 1
        file_checksum, file_size, filepath = line.split()
        url_suffix = filepath.partition("/")[-1]
        file_url = "/".join([url_prefix, url_suffix])

        pathname = url_suffix.partition("/")
        if not pathname[0] in top_level_files:
          top_level_files.append(pathname[0])
        pathname = "".join([pathname[0] + "_new"] + list(pathname[1:]))

        print(file_url + " --> " + pathname)
        common.log("Firmware get ... %d of %d" % (file_index, file_count))
        aiko.web_client.http_get_file(file_url, pathname)
# TODO: Verify actual file size versus size stated in the "manifest"
# TODO: Verify actual file checksum

    shutil.path_remove(manifest_pathname)
    shutil.file_copy("configuration/net.py",  "configuration_new/net.py")
    shutil.file_copy("configuration/keys.db", "configuration_new/keys.db")

    common.log("Firmware install")
    for file in top_level_files:
      try:
        print("Rename %s to %s" % (file + "_new", file))
        shutil.path_remove(file)
        os.rename(file + "_new", file)
      except OSError:
        print("OSError")

    common.log("Firmware upgrade success !")
    common.log("Please reboot :)")
  except Exception as exception:
    common.log("Firmware upgrade failed :(")
    import sys
    sys.print_exception(exception)
  finally:
    in_progress = False
    version = None
Esempio n. 8
0
def log(message):
    common.log(message)
    print("(pass log)")