dpp-nfc: Add color and details for interactive operations

Make the debug output easier to read when performing interactive NFC
operations on a device that has a terminal showing the log.

Signed-off-by: Jouni Malinen <jouni@codeaurora.org>
This commit is contained in:
Jouni Malinen 2020-07-23 12:10:26 +03:00 committed by Jouni Malinen
parent 09c22bb782
commit 7e2edfbc1a

View file

@ -41,9 +41,19 @@ hs_sent = False
netrole = None netrole = None
mutex = threading.Lock() mutex = threading.Lock()
def summary(txt): C_NORMAL = '\033[0m'
C_RED = '\033[91m'
C_GREEN = '\033[92m'
C_BLUE = '\033[94m'
C_MAGENTA = '\033[95m'
C_CYAN = '\033[96m'
def summary(txt, color=None):
with mutex: with mutex:
print(txt) if color:
print(color + txt + C_NORMAL)
else:
print(txt)
if summary_file: if summary_file:
with open(summary_file, 'a') as f: with open(summary_file, 'a') as f:
f.write(txt + "\n") f.write(txt + "\n")
@ -85,7 +95,7 @@ def dpp_nfc_uri_process(uri):
return False return False
peer_id = wpas.request("DPP_NFC_URI " + uri) peer_id = wpas.request("DPP_NFC_URI " + uri)
if "FAIL" in peer_id: if "FAIL" in peer_id:
summary("Could not parse DPP URI from NFC URI record") summary("Could not parse DPP URI from NFC URI record", color=C_RED)
return False return False
peer_id = int(peer_id) peer_id = int(peer_id)
summary("peer_id=%d for URI from NFC Tag: %s" % (peer_id, uri)) summary("peer_id=%d for URI from NFC Tag: %s" % (peer_id, uri))
@ -100,7 +110,7 @@ def dpp_nfc_uri_process(uri):
summary("Initiate DPP authentication: " + cmd) summary("Initiate DPP authentication: " + cmd)
res = wpas.request(cmd) res = wpas.request(cmd)
if "OK" not in res: if "OK" not in res:
summary("Failed to initiate DPP Authentication") summary("Failed to initiate DPP Authentication", color=C_RED)
return False return False
summary("DPP Authentication initiated") summary("DPP Authentication initiated")
return True return True
@ -111,20 +121,20 @@ def dpp_hs_tag_read(record):
return False return False
summary(record) summary(record)
if len(record.data) < 5: if len(record.data) < 5:
summary("Too short DPP HS") summary("Too short DPP HS", color=C_RED)
return False return False
if record.data[0] != 0: if record.data[0] != 0:
summary("Unexpected URI Identifier Code") summary("Unexpected URI Identifier Code", color=C_RED)
return False return False
uribuf = record.data[1:] uribuf = record.data[1:]
try: try:
uri = uribuf.decode() uri = uribuf.decode()
except: except:
summary("Invalid URI payload") summary("Invalid URI payload", color=C_RED)
return False return False
summary("URI: " + uri) summary("URI: " + uri)
if not uri.startswith("DPP:"): if not uri.startswith("DPP:"):
summary("Not a DPP URI") summary("Not a DPP URI", color=C_RED)
return False return False
return dpp_nfc_uri_process(uri) return dpp_nfc_uri_process(uri)
@ -239,7 +249,8 @@ def dpp_handover_client(llc, alt=False):
chan_override = altchanlist chan_override = altchanlist
uri = wpas_get_nfc_uri(start_listen=False, chan_override=chan_override) uri = wpas_get_nfc_uri(start_listen=False, chan_override=chan_override)
if uri is None: if uri is None:
summary("Cannot start handover client - no bootstrap URI available") summary("Cannot start handover client - no bootstrap URI available",
color=C_RED)
return return
uri = ndef.UriRecord(uri) uri = ndef.UriRecord(uri)
summary("NFC URI record for DPP: " + str(uri)) summary("NFC URI record for DPP: " + str(uri))
@ -280,7 +291,7 @@ def dpp_handover_client(llc, alt=False):
if not client.send_records(message): if not client.send_records(message):
my_crn_ready = False my_crn_ready = False
summary("Failed to send handover request") summary("Failed to send handover request", color=C_RED)
client.close() client.close()
return return
@ -294,13 +305,13 @@ def dpp_handover_client(llc, alt=False):
if hs_sent: if hs_sent:
summary("Client receive failed as expected since I'm the handover server: %s" % str(e)) summary("Client receive failed as expected since I'm the handover server: %s" % str(e))
else: else:
summary("Client receive failed: %s" % str(e)) summary("Client receive failed: %s" % str(e), color=C_RED)
message = None message = None
if message is None: if message is None:
if hs_sent: if hs_sent:
summary("No response received as expected since I'm the handover server") summary("No response received as expected since I'm the handover server")
else: else:
summary("No response received") summary("No response received", color=C_RED)
client.close() client.close()
return return
summary("Received message: " + str(message)) summary("Received message: " + str(message))
@ -320,7 +331,7 @@ def dpp_handover_client(llc, alt=False):
summary("Remote carrier type: " + carrier.type) summary("Remote carrier type: " + carrier.type)
if carrier.type == "application/vnd.wfa.dpp": if carrier.type == "application/vnd.wfa.dpp":
if len(carrier.data) == 0 or carrier.data[0] != 0: if len(carrier.data) == 0 or carrier.data[0] != 0:
summary("URI Identifier Code 'None' not seen") summary("URI Identifier Code 'None' not seen", color=C_RED)
continue continue
summary("DPP carrier type match - send to wpa_supplicant") summary("DPP carrier type match - send to wpa_supplicant")
dpp_found = True dpp_found = True
@ -328,7 +339,7 @@ def dpp_handover_client(llc, alt=False):
summary("DPP URI: " + uri) summary("DPP URI: " + uri)
res = wpas_report_handover_sel(uri) res = wpas_report_handover_sel(uri)
if res is None or "FAIL" in res: if res is None or "FAIL" in res:
summary("DPP handover report rejected") summary("DPP handover report rejected", color=C_RED)
break break
success_report("DPP handover reported successfully (initiator)") success_report("DPP handover reported successfully (initiator)")
@ -348,7 +359,7 @@ def dpp_handover_client(llc, alt=False):
# TODO: Single Configurator instance # TODO: Single Configurator instance
res = wpas.request("DPP_CONFIGURATOR_ADD") res = wpas.request("DPP_CONFIGURATOR_ADD")
if "FAIL" in res: if "FAIL" in res:
summary("Failed to initiate Configurator") summary("Failed to initiate Configurator", color=C_RED)
break break
conf_id = int(res) conf_id = int(res)
extra = " conf=sta-dpp configurator=%d" % conf_id extra = " conf=sta-dpp configurator=%d" % conf_id
@ -358,7 +369,7 @@ def dpp_handover_client(llc, alt=False):
cmd += extra cmd += extra
res = wpas.request(cmd) res = wpas.request(cmd)
if "FAIL" in res: if "FAIL" in res:
summary("Failed to initiate DPP authentication") summary("Failed to initiate DPP authentication", color=C_RED)
break break
if not dpp_found: if not dpp_found:
@ -450,7 +461,7 @@ class HandoverServer(nfc.handover.HandoverServer):
if carrier.type == "application/vnd.wfa.dpp": if carrier.type == "application/vnd.wfa.dpp":
summary("DPP carrier type match - add DPP carrier record") summary("DPP carrier type match - add DPP carrier record")
if len(carrier.data) == 0 or carrier.data[0] != 0: if len(carrier.data) == 0 or carrier.data[0] != 0:
summary("URI Identifier Code 'None' not seen") summary("URI Identifier Code 'None' not seen", color=C_RED)
continue continue
uri = carrier.data[1:].decode("utf-8") uri = carrier.data[1:].decode("utf-8")
summary("Received DPP URI: " + uri) summary("Received DPP URI: " + uri)
@ -460,7 +471,8 @@ class HandoverServer(nfc.handover.HandoverServer):
res = wpas_report_handover_req(uri) res = wpas_report_handover_req(uri)
if res is None or "FAIL" in res: if res is None or "FAIL" in res:
summary("DPP handover request processing failed") summary("DPP handover request processing failed",
color=C_RED)
global altchanlist global altchanlist
if altchanlist: if altchanlist:
data = wpas_get_nfc_uri(start_listen=False, data = wpas_get_nfc_uri(start_listen=False,
@ -508,7 +520,7 @@ class HandoverServer(nfc.handover.HandoverServer):
summary(cmd) summary(cmd)
res = wpas.request(cmd) res = wpas.request(cmd)
if "OK" not in res: if "OK" not in res:
summary("Failed to start DPP listen") summary("Failed to start DPP listen", color=C_RED)
break break
carrier = ndef.Record('application/vnd.wfa.dpp', 'A', uri.data) carrier = ndef.Record('application/vnd.wfa.dpp', 'A', uri.data)
@ -585,18 +597,22 @@ def dpp_tag_read(tag):
def rdwr_connected_write_tag(tag): def rdwr_connected_write_tag(tag):
summary("Tag found - writing - " + str(tag)) summary("Tag found - writing - " + str(tag))
if not tag.ndef: if not tag.ndef:
summary("Not a formatted NDEF tag") summary("Not a formatted NDEF tag", color=C_RED)
return return
if not tag.ndef.is_writeable: if not tag.ndef.is_writeable:
summary("Not a writable tag") summary("Not a writable tag", color=C_RED)
return return
global dpp_tag_data global dpp_tag_data
if tag.ndef.capacity < len(dpp_tag_data): if tag.ndef.capacity < len(dpp_tag_data):
summary("Not enough room for the message") summary("Not enough room for the message")
return return
tag.ndef.records = dpp_tag_data try:
tag.ndef.records = dpp_tag_data
except ValueError as e:
summary("Writing the tag failed: %s" % str(e), color=C_RED)
return
success_report("Tag write succeeded") success_report("Tag write succeeded")
summary("Done - remove tag") summary("Tag writing completed - remove tag", color=C_GREEN)
global only_one global only_one
if only_one: if only_one:
global continue_loop global continue_loop
@ -608,7 +624,7 @@ def write_nfc_uri(clf, wait_remove=True):
summary("Write NFC URI record") summary("Write NFC URI record")
data = wpas_get_nfc_uri() data = wpas_get_nfc_uri()
if data is None: if data is None:
summary("Could not get NFC URI from wpa_supplicant") summary("Could not get NFC URI from wpa_supplicant", color=C_RED)
return return
global dpp_sel_wait_remove global dpp_sel_wait_remove
@ -617,7 +633,7 @@ def write_nfc_uri(clf, wait_remove=True):
uri = ndef.UriRecord(data) uri = ndef.UriRecord(data)
summary(uri) summary(uri)
summary("Touch an NFC tag") summary("Touch an NFC tag to write URI record", color=C_CYAN)
global dpp_tag_data global dpp_tag_data
dpp_tag_data = [uri] dpp_tag_data = [uri]
clf.connect(rdwr={'on-connect': rdwr_connected_write_tag}) clf.connect(rdwr={'on-connect': rdwr_connected_write_tag})
@ -626,7 +642,7 @@ def write_nfc_hs(clf, wait_remove=True):
summary("Write NFC Handover Select record on a tag") summary("Write NFC Handover Select record on a tag")
data = wpas_get_nfc_uri() data = wpas_get_nfc_uri()
if data is None: if data is None:
summary("Could not get NFC URI from wpa_supplicant") summary("Could not get NFC URI from wpa_supplicant", color=C_RED)
return return
global dpp_sel_wait_remove global dpp_sel_wait_remove
@ -640,7 +656,7 @@ def write_nfc_hs(clf, wait_remove=True):
summary(hs) summary(hs)
summary(carrier) summary(carrier)
summary("Touch an NFC tag") summary("Touch an NFC tag to write HS record", color=C_CYAN)
global dpp_tag_data global dpp_tag_data
dpp_tag_data = [hs, carrier] dpp_tag_data = [hs, carrier]
summary(dpp_tag_data) summary(dpp_tag_data)
@ -658,7 +674,7 @@ def rdwr_connected(tag):
global continue_loop global continue_loop
continue_loop = False continue_loop = False
else: else:
summary("Not an NDEF tag - remove tag") summary("Not an NDEF tag - remove tag", color=C_RED)
return True return True
return not no_wait return not no_wait
@ -841,7 +857,7 @@ def main():
try: try:
if not clf.open(args.device): if not clf.open(args.device):
summary("Could not open connection with an NFC device") summary("Could not open connection with an NFC device", color=C_RED)
raise SystemExit raise SystemExit
if args.command == "write-nfc-uri": if args.command == "write-nfc-uri":
@ -859,10 +875,13 @@ def main():
clear_raw_mode() clear_raw_mode()
if was_in_raw_mode: if was_in_raw_mode:
print("\r") print("\r")
if args.tag_read_only: if args.handover_only:
summary("Waiting for a tag to be touched") summary("Waiting a peer to be touched", color=C_MAGENTA)
elif args.tag_read_only:
summary("Waiting for a tag to be touched", color=C_BLUE)
else: else:
summary("Waiting for a tag or peer to be touched") summary("Waiting for a tag or peer to be touched",
color=C_GREEN)
wait_connection = True wait_connection = True
try: try:
if args.tag_read_only: if args.tag_read_only: