1# Copyright 2018 Espressif Systems (Shanghai) PTE LTD 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14# 15 16# APIs for interpreting and creating protobuf packets for Wi-Fi provisioning 17 18from __future__ import print_function 19 20import proto 21import utils 22from future.utils import tobytes 23 24 25def print_verbose(security_ctx, data): 26 if (security_ctx.verbose): 27 print('++++ ' + data + ' ++++') 28 29 30def config_get_status_request(security_ctx): 31 # Form protobuf request packet for GetStatus command 32 cfg1 = proto.wifi_config_pb2.WiFiConfigPayload() 33 cfg1.msg = proto.wifi_config_pb2.TypeCmdGetStatus 34 cmd_get_status = proto.wifi_config_pb2.CmdGetStatus() 35 cfg1.cmd_get_status.MergeFrom(cmd_get_status) 36 encrypted_cfg = security_ctx.encrypt_data(cfg1.SerializeToString()).decode('latin-1') 37 print_verbose(security_ctx, 'Client -> Device (Encrypted CmdGetStatus) ' + utils.str_to_hexstr(encrypted_cfg)) 38 return encrypted_cfg 39 40 41def config_get_status_response(security_ctx, response_data): 42 # Interpret protobuf response packet from GetStatus command 43 decrypted_message = security_ctx.decrypt_data(tobytes(response_data)) 44 cmd_resp1 = proto.wifi_config_pb2.WiFiConfigPayload() 45 cmd_resp1.ParseFromString(decrypted_message) 46 print_verbose(security_ctx, 'Response type ' + str(cmd_resp1.msg)) 47 print_verbose(security_ctx, 'Response status ' + str(cmd_resp1.resp_get_status.status)) 48 49 if cmd_resp1.resp_get_status.sta_state == 0: 50 print('++++ WiFi state: ' + 'connected ++++') 51 return 'connected' 52 elif cmd_resp1.resp_get_status.sta_state == 1: 53 print('++++ WiFi state: ' + 'connecting... ++++') 54 return 'connecting' 55 elif cmd_resp1.resp_get_status.sta_state == 2: 56 print('++++ WiFi state: ' + 'disconnected ++++') 57 return 'disconnected' 58 elif cmd_resp1.resp_get_status.sta_state == 3: 59 print('++++ WiFi state: ' + 'connection failed ++++') 60 if cmd_resp1.resp_get_status.fail_reason == 0: 61 print('++++ Failure reason: ' + 'Incorrect Password ++++') 62 elif cmd_resp1.resp_get_status.fail_reason == 1: 63 print('++++ Failure reason: ' + 'Incorrect SSID ++++') 64 return 'failed' 65 return 'unknown' 66 67 68def config_set_config_request(security_ctx, ssid, passphrase): 69 # Form protobuf request packet for SetConfig command 70 cmd = proto.wifi_config_pb2.WiFiConfigPayload() 71 cmd.msg = proto.wifi_config_pb2.TypeCmdSetConfig 72 cmd.cmd_set_config.ssid = tobytes(ssid) 73 cmd.cmd_set_config.passphrase = tobytes(passphrase) 74 enc_cmd = security_ctx.encrypt_data(cmd.SerializeToString()).decode('latin-1') 75 print_verbose(security_ctx, 'Client -> Device (SetConfig cmd) ' + utils.str_to_hexstr(enc_cmd)) 76 return enc_cmd 77 78 79def config_set_config_response(security_ctx, response_data): 80 # Interpret protobuf response packet from SetConfig command 81 decrypt = security_ctx.decrypt_data(tobytes(response_data)) 82 cmd_resp4 = proto.wifi_config_pb2.WiFiConfigPayload() 83 cmd_resp4.ParseFromString(decrypt) 84 print_verbose(security_ctx, 'SetConfig status ' + str(cmd_resp4.resp_set_config.status)) 85 return cmd_resp4.resp_set_config.status 86 87 88def config_apply_config_request(security_ctx): 89 # Form protobuf request packet for ApplyConfig command 90 cmd = proto.wifi_config_pb2.WiFiConfigPayload() 91 cmd.msg = proto.wifi_config_pb2.TypeCmdApplyConfig 92 enc_cmd = security_ctx.encrypt_data(cmd.SerializeToString()).decode('latin-1') 93 print_verbose(security_ctx, 'Client -> Device (ApplyConfig cmd) ' + utils.str_to_hexstr(enc_cmd)) 94 return enc_cmd 95 96 97def config_apply_config_response(security_ctx, response_data): 98 # Interpret protobuf response packet from ApplyConfig command 99 decrypt = security_ctx.decrypt_data(tobytes(response_data)) 100 cmd_resp5 = proto.wifi_config_pb2.WiFiConfigPayload() 101 cmd_resp5.ParseFromString(decrypt) 102 print_verbose(security_ctx, 'ApplyConfig status ' + str(cmd_resp5.resp_apply_config.status)) 103 return cmd_resp5.resp_apply_config.status 104