softap_prov_test.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #!/usr/bin/env python
  2. #
  3. # Copyright 2018 Espressif Systems (Shanghai) PTE LTD
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. from __future__ import print_function
  17. import re
  18. import os
  19. import sys
  20. import time
  21. try:
  22. import IDF
  23. except ImportError:
  24. test_fw_path = os.getenv("TEST_FW_PATH")
  25. if test_fw_path and test_fw_path not in sys.path:
  26. sys.path.insert(0, test_fw_path)
  27. import IDF
  28. try:
  29. import esp_prov
  30. except ImportError:
  31. esp_prov_path = os.getenv("IDF_PATH") + "/tools/esp_prov"
  32. if esp_prov_path and esp_prov_path not in sys.path:
  33. sys.path.insert(0, esp_prov_path)
  34. import esp_prov
  35. try:
  36. import wifi_tools
  37. except ImportError:
  38. wifi_tools_path = os.getenv("IDF_PATH") + "/examples/provisioning/softap_prov/utils"
  39. if wifi_tools_path and wifi_tools_path not in sys.path:
  40. sys.path.insert(0, wifi_tools_path)
  41. import wifi_tools
  42. # Have esp_prov throw exception
  43. esp_prov.config_throw_except = True
  44. @IDF.idf_example_test(env_tag="Example_WIFI_BT")
  45. def test_examples_provisioning_softap(env, extra_data):
  46. # Acquire DUT
  47. dut1 = env.get_dut("softap_prov", "examples/provisioning/softap_prov")
  48. # Get binary file
  49. binary_file = os.path.join(dut1.app.binary_path, "softap_prov.bin")
  50. bin_size = os.path.getsize(binary_file)
  51. IDF.log_performance("softap_prov_bin_size", "{}KB".format(bin_size // 1024))
  52. IDF.check_performance("softap_prov_bin_size", bin_size // 1024)
  53. # Upload binary and start testing
  54. dut1.start_app()
  55. # Parse IP address of STA
  56. dut1.expect("Starting WiFi SoftAP provisioning", timeout=60)
  57. dut1.expect("SoftAP started", timeout=30)
  58. [ssid, password] = dut1.expect(re.compile(r"SoftAP Provisioning started with SSID '(\S+)', Password '(\S+)'"), timeout=30)
  59. iface = wifi_tools.get_wiface_name()
  60. if iface is None:
  61. raise RuntimeError("Failed to get Wi-Fi interface on host")
  62. print("Interface name : " + iface)
  63. print("SoftAP SSID : " + ssid)
  64. print("SoftAP Password : " + password)
  65. ctrl = wifi_tools.wpa_cli(iface, reset_on_exit=True)
  66. print("Connecting to DUT SoftAP...")
  67. ip = ctrl.connect(ssid, password)
  68. got_ip = dut1.expect(re.compile(r"softAP assign IP to station,IP is: (\d+.\d+.\d+.\d+)"), timeout=30)[0]
  69. if ip != got_ip:
  70. raise RuntimeError("SoftAP connected to another host! " + ip + "!=" + got_ip)
  71. print("Connected to DUT SoftAP")
  72. print("Starting Provisioning")
  73. verbose = False
  74. protover = "V0.1"
  75. secver = 1
  76. pop = "abcd1234"
  77. provmode = "softap"
  78. ap_ssid = "myssid"
  79. ap_password = "mypassword"
  80. softap_endpoint = ip.split('.')[0] + "." + ip.split('.')[1] + "." + ip.split('.')[2] + ".1:80"
  81. print("Getting security")
  82. security = esp_prov.get_security(secver, pop, verbose)
  83. if security is None:
  84. raise RuntimeError("Failed to get security")
  85. print("Getting transport")
  86. transport = esp_prov.get_transport(provmode, softap_endpoint)
  87. if transport is None:
  88. raise RuntimeError("Failed to get transport")
  89. print("Verifying protocol version")
  90. if not esp_prov.version_match(transport, protover):
  91. raise RuntimeError("Mismatch in protocol version")
  92. print("Starting Session")
  93. if not esp_prov.establish_session(transport, security):
  94. raise RuntimeError("Failed to start session")
  95. print("Sending Wifi credential to DUT")
  96. if not esp_prov.send_wifi_config(transport, security, ap_ssid, ap_password):
  97. raise RuntimeError("Failed to send Wi-Fi config")
  98. print("Applying config")
  99. if not esp_prov.apply_wifi_config(transport, security):
  100. raise RuntimeError("Failed to send apply config")
  101. success = False
  102. while True:
  103. time.sleep(5)
  104. print("Wi-Fi connection state")
  105. ret = esp_prov.get_wifi_config(transport, security)
  106. if (ret == 1):
  107. continue
  108. elif (ret == 0):
  109. print("Provisioning was successful")
  110. success = True
  111. break
  112. if not success:
  113. raise RuntimeError("Provisioning failed")
  114. if __name__ == '__main__':
  115. test_examples_provisioning_softap()