wifi_prov.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. # APIs for interpreting and creating protobuf packets for Wi-Fi provisioning
  16. from __future__ import print_function
  17. from future.utils import tobytes
  18. import utils
  19. import proto
  20. def print_verbose(security_ctx, data):
  21. if (security_ctx.verbose):
  22. print("++++ " + data + " ++++")
  23. def config_get_status_request(security_ctx):
  24. # Form protobuf request packet for GetStatus command
  25. cfg1 = proto.wifi_config_pb2.WiFiConfigPayload()
  26. cfg1.msg = proto.wifi_config_pb2.TypeCmdGetStatus
  27. cmd_get_status = proto.wifi_config_pb2.CmdGetStatus()
  28. cfg1.cmd_get_status.MergeFrom(cmd_get_status)
  29. encrypted_cfg = security_ctx.encrypt_data(cfg1.SerializeToString()).decode('latin-1')
  30. print_verbose(security_ctx, "Client -> Device (Encrypted CmdGetStatus) " + utils.str_to_hexstr(encrypted_cfg))
  31. return encrypted_cfg
  32. def config_get_status_response(security_ctx, response_data):
  33. # Interpret protobuf response packet from GetStatus command
  34. decrypted_message = security_ctx.decrypt_data(tobytes(response_data))
  35. cmd_resp1 = proto.wifi_config_pb2.WiFiConfigPayload()
  36. cmd_resp1.ParseFromString(decrypted_message)
  37. print_verbose(security_ctx, "Response type " + str(cmd_resp1.msg))
  38. print_verbose(security_ctx, "Response status " + str(cmd_resp1.resp_get_status.status))
  39. if cmd_resp1.resp_get_status.sta_state == 0:
  40. print("++++ WiFi state: " + "connected ++++")
  41. return "connected"
  42. elif cmd_resp1.resp_get_status.sta_state == 1:
  43. print("++++ WiFi state: " + "connecting... ++++")
  44. return "connecting"
  45. elif cmd_resp1.resp_get_status.sta_state == 2:
  46. print("++++ WiFi state: " + "disconnected ++++")
  47. return "disconnected"
  48. elif cmd_resp1.resp_get_status.sta_state == 3:
  49. print("++++ WiFi state: " + "connection failed ++++")
  50. if cmd_resp1.resp_get_status.fail_reason == 0:
  51. print("++++ Failure reason: " + "Incorrect Password ++++")
  52. elif cmd_resp1.resp_get_status.fail_reason == 1:
  53. print("++++ Failure reason: " + "Incorrect SSID ++++")
  54. return "failed"
  55. return "unknown"
  56. def config_set_config_request(security_ctx, ssid, passphrase):
  57. # Form protobuf request packet for SetConfig command
  58. cmd = proto.wifi_config_pb2.WiFiConfigPayload()
  59. cmd.msg = proto.wifi_config_pb2.TypeCmdSetConfig
  60. cmd.cmd_set_config.ssid = tobytes(ssid)
  61. cmd.cmd_set_config.passphrase = tobytes(passphrase)
  62. enc_cmd = security_ctx.encrypt_data(cmd.SerializeToString()).decode('latin-1')
  63. print_verbose(security_ctx, "Client -> Device (SetConfig cmd) " + utils.str_to_hexstr(enc_cmd))
  64. return enc_cmd
  65. def config_set_config_response(security_ctx, response_data):
  66. # Interpret protobuf response packet from SetConfig command
  67. decrypt = security_ctx.decrypt_data(tobytes(response_data))
  68. cmd_resp4 = proto.wifi_config_pb2.WiFiConfigPayload()
  69. cmd_resp4.ParseFromString(decrypt)
  70. print_verbose(security_ctx, "SetConfig status " + str(cmd_resp4.resp_set_config.status))
  71. return cmd_resp4.resp_set_config.status
  72. def config_apply_config_request(security_ctx):
  73. # Form protobuf request packet for ApplyConfig command
  74. cmd = proto.wifi_config_pb2.WiFiConfigPayload()
  75. cmd.msg = proto.wifi_config_pb2.TypeCmdApplyConfig
  76. enc_cmd = security_ctx.encrypt_data(cmd.SerializeToString()).decode('latin-1')
  77. print_verbose(security_ctx, "Client -> Device (ApplyConfig cmd) " + utils.str_to_hexstr(enc_cmd))
  78. return enc_cmd
  79. def config_apply_config_response(security_ctx, response_data):
  80. # Interpret protobuf response packet from ApplyConfig command
  81. decrypt = security_ctx.decrypt_data(tobytes(response_data))
  82. cmd_resp5 = proto.wifi_config_pb2.WiFiConfigPayload()
  83. cmd_resp5.ParseFromString(decrypt)
  84. print_verbose(security_ctx, "ApplyConfig status " + str(cmd_resp5.resp_apply_config.status))
  85. return cmd_resp5.resp_apply_config.status