2022-06-15 07:20:01 -04:00
|
|
|
# SPDX-FileCopyrightText: 2018-2022 Espressif Systems (Shanghai) CO LTD
|
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
2019-04-23 02:48:28 -04:00
|
|
|
#
|
|
|
|
|
|
|
|
# APIs for interpreting and creating protobuf packets for Wi-Fi Scanning
|
|
|
|
import proto
|
2022-06-22 05:44:19 -04:00
|
|
|
from utils import str_to_bytes
|
2019-04-23 02:48:28 -04:00
|
|
|
|
|
|
|
|
|
|
|
def print_verbose(security_ctx, data):
|
|
|
|
if (security_ctx.verbose):
|
2022-06-22 05:44:19 -04:00
|
|
|
print(f'\x1b[32;20m++++ {data} ++++\x1b[0m')
|
2019-04-23 02:48:28 -04:00
|
|
|
|
|
|
|
|
|
|
|
def scan_start_request(security_ctx, blocking=True, passive=False, group_channels=5, period_ms=120):
|
|
|
|
# Form protobuf request packet for ScanStart command
|
|
|
|
cmd = proto.wifi_scan_pb2.WiFiScanPayload()
|
|
|
|
cmd.msg = proto.wifi_scan_pb2.TypeCmdScanStart
|
|
|
|
cmd.cmd_scan_start.blocking = blocking
|
|
|
|
cmd.cmd_scan_start.passive = passive
|
|
|
|
cmd.cmd_scan_start.group_channels = group_channels
|
|
|
|
cmd.cmd_scan_start.period_ms = period_ms
|
2022-06-22 05:44:19 -04:00
|
|
|
enc_cmd = security_ctx.encrypt_data(cmd.SerializeToString())
|
|
|
|
print_verbose(security_ctx, f'Client -> Device (Encrypted CmdScanStart): 0x{enc_cmd.hex()}')
|
|
|
|
return enc_cmd.decode('latin-1')
|
2019-04-23 02:48:28 -04:00
|
|
|
|
|
|
|
|
|
|
|
def scan_start_response(security_ctx, response_data):
|
|
|
|
# Interpret protobuf response packet from ScanStart command
|
2022-06-22 05:44:19 -04:00
|
|
|
dec_resp = security_ctx.decrypt_data(str_to_bytes(response_data))
|
2019-04-23 02:48:28 -04:00
|
|
|
resp = proto.wifi_scan_pb2.WiFiScanPayload()
|
|
|
|
resp.ParseFromString(dec_resp)
|
2022-06-22 05:44:19 -04:00
|
|
|
print_verbose(security_ctx, f'ScanStart status: 0x{str(resp.status)}')
|
2019-04-23 02:48:28 -04:00
|
|
|
if resp.status != 0:
|
|
|
|
raise RuntimeError
|
|
|
|
|
|
|
|
|
|
|
|
def scan_status_request(security_ctx):
|
|
|
|
# Form protobuf request packet for ScanStatus command
|
|
|
|
cmd = proto.wifi_scan_pb2.WiFiScanPayload()
|
|
|
|
cmd.msg = proto.wifi_scan_pb2.TypeCmdScanStatus
|
2022-06-22 05:44:19 -04:00
|
|
|
enc_cmd = security_ctx.encrypt_data(cmd.SerializeToString())
|
|
|
|
print_verbose(security_ctx, f'Client -> Device (Encrypted CmdScanStatus): 0x{enc_cmd.hex()}')
|
|
|
|
return enc_cmd.decode('latin-1')
|
2019-04-23 02:48:28 -04:00
|
|
|
|
|
|
|
|
|
|
|
def scan_status_response(security_ctx, response_data):
|
|
|
|
# Interpret protobuf response packet from ScanStatus command
|
2022-06-22 05:44:19 -04:00
|
|
|
dec_resp = security_ctx.decrypt_data(str_to_bytes(response_data))
|
2019-04-23 02:48:28 -04:00
|
|
|
resp = proto.wifi_scan_pb2.WiFiScanPayload()
|
|
|
|
resp.ParseFromString(dec_resp)
|
2022-06-22 05:44:19 -04:00
|
|
|
print_verbose(security_ctx, f'ScanStatus status: 0x{str(resp.status)}')
|
2019-04-23 02:48:28 -04:00
|
|
|
if resp.status != 0:
|
|
|
|
raise RuntimeError
|
2021-01-25 21:49:01 -05:00
|
|
|
return {'finished': resp.resp_scan_status.scan_finished, 'count': resp.resp_scan_status.result_count}
|
2019-04-23 02:48:28 -04:00
|
|
|
|
|
|
|
|
|
|
|
def scan_result_request(security_ctx, index, count):
|
|
|
|
# Form protobuf request packet for ScanResult command
|
|
|
|
cmd = proto.wifi_scan_pb2.WiFiScanPayload()
|
|
|
|
cmd.msg = proto.wifi_scan_pb2.TypeCmdScanResult
|
|
|
|
cmd.cmd_scan_result.start_index = index
|
|
|
|
cmd.cmd_scan_result.count = count
|
2022-06-22 05:44:19 -04:00
|
|
|
enc_cmd = security_ctx.encrypt_data(cmd.SerializeToString())
|
|
|
|
print_verbose(security_ctx, f'Client -> Device (Encrypted CmdScanResult): 0x{enc_cmd.hex()}')
|
|
|
|
return enc_cmd.decode('latin-1')
|
2019-04-23 02:48:28 -04:00
|
|
|
|
|
|
|
|
|
|
|
def scan_result_response(security_ctx, response_data):
|
|
|
|
# Interpret protobuf response packet from ScanResult command
|
2022-06-22 05:44:19 -04:00
|
|
|
dec_resp = security_ctx.decrypt_data(str_to_bytes(response_data))
|
2019-04-23 02:48:28 -04:00
|
|
|
resp = proto.wifi_scan_pb2.WiFiScanPayload()
|
|
|
|
resp.ParseFromString(dec_resp)
|
2022-06-22 05:44:19 -04:00
|
|
|
print_verbose(security_ctx, f'ScanResult status: 0x{str(resp.status)}')
|
2019-04-23 02:48:28 -04:00
|
|
|
if resp.status != 0:
|
|
|
|
raise RuntimeError
|
2022-06-15 07:20:01 -04:00
|
|
|
authmode_str = ['Open', 'WEP', 'WPA_PSK', 'WPA2_PSK', 'WPA_WPA2_PSK',
|
|
|
|
'WPA2_ENTERPRISE', 'WPA3_PSK', 'WPA2_WPA3_PSK']
|
2019-04-23 02:48:28 -04:00
|
|
|
results = []
|
|
|
|
for entry in resp.resp_scan_result.entries:
|
2021-01-25 21:49:01 -05:00
|
|
|
results += [{'ssid': entry.ssid.decode('latin-1').rstrip('\x00'),
|
2022-06-22 05:44:19 -04:00
|
|
|
'bssid': entry.bssid.hex(),
|
2021-01-25 21:49:01 -05:00
|
|
|
'channel': entry.channel,
|
|
|
|
'rssi': entry.rssi,
|
|
|
|
'auth': authmode_str[entry.auth]}]
|
2022-06-22 05:44:19 -04:00
|
|
|
print_verbose(security_ctx, f"ScanResult SSID : {str(results[-1]['ssid'])}")
|
|
|
|
print_verbose(security_ctx, f"ScanResult BSSID : {str(results[-1]['bssid'])}")
|
|
|
|
print_verbose(security_ctx, f"ScanResult Channel : {str(results[-1]['channel'])}")
|
|
|
|
print_verbose(security_ctx, f"ScanResult RSSI : {str(results[-1]['rssi'])}")
|
|
|
|
print_verbose(security_ctx, f"ScanResult AUTH : {str(results[-1]['auth'])}")
|
2019-04-23 02:48:28 -04:00
|
|
|
return results
|