softap_prov_test.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 time
  20. import ttfw_idf
  21. import esp_prov
  22. import wifi_tools
  23. # Have esp_prov throw exception
  24. esp_prov.config_throw_except = True
  25. @ttfw_idf.idf_example_test(env_tag="Example_WIFI_BT")
  26. def test_examples_provisioning_softap(env, extra_data):
  27. # Acquire DUT
  28. dut1 = env.get_dut("softap_prov", "examples/provisioning/softap_prov")
  29. # Get binary file
  30. binary_file = os.path.join(dut1.app.binary_path, "softap_prov.bin")
  31. bin_size = os.path.getsize(binary_file)
  32. ttfw_idf.log_performance("softap_prov_bin_size", "{}KB".format(bin_size // 1024))
  33. ttfw_idf.check_performance("softap_prov_bin_size", bin_size // 1024)
  34. # Upload binary and start testing
  35. dut1.start_app()
  36. # Parse IP address of STA
  37. dut1.expect("Starting WiFi SoftAP provisioning", timeout=60)
  38. [ssid, password] = dut1.expect(re.compile(r"SoftAP Provisioning started with SSID '(\S+)', Password '(\S+)'"), timeout=30)
  39. iface = wifi_tools.get_wiface_name()
  40. if iface is None:
  41. raise RuntimeError("Failed to get Wi-Fi interface on host")
  42. print("Interface name : " + iface)
  43. print("SoftAP SSID : " + ssid)
  44. print("SoftAP Password : " + password)
  45. ctrl = wifi_tools.wpa_cli(iface, reset_on_exit=True)
  46. print("Connecting to DUT SoftAP...")
  47. ip = ctrl.connect(ssid, password)
  48. got_ip = dut1.expect(re.compile(r"softAP assign IP to station,IP is: (\d+.\d+.\d+.\d+)"), timeout=30)[0]
  49. if ip != got_ip:
  50. raise RuntimeError("SoftAP connected to another host! " + ip + "!=" + got_ip)
  51. print("Connected to DUT SoftAP")
  52. print("Starting Provisioning")
  53. verbose = False
  54. protover = "V0.1"
  55. secver = 1
  56. pop = "abcd1234"
  57. provmode = "softap"
  58. ap_ssid = "myssid"
  59. ap_password = "mypassword"
  60. softap_endpoint = ip.split('.')[0] + "." + ip.split('.')[1] + "." + ip.split('.')[2] + ".1:80"
  61. print("Getting security")
  62. security = esp_prov.get_security(secver, pop, verbose)
  63. if security is None:
  64. raise RuntimeError("Failed to get security")
  65. print("Getting transport")
  66. transport = esp_prov.get_transport(provmode, softap_endpoint)
  67. if transport is None:
  68. raise RuntimeError("Failed to get transport")
  69. print("Verifying protocol version")
  70. if not esp_prov.version_match(transport, protover):
  71. raise RuntimeError("Mismatch in protocol version")
  72. print("Starting Session")
  73. if not esp_prov.establish_session(transport, security):
  74. raise RuntimeError("Failed to start session")
  75. print("Sending Wifi credential to DUT")
  76. if not esp_prov.send_wifi_config(transport, security, ap_ssid, ap_password):
  77. raise RuntimeError("Failed to send Wi-Fi config")
  78. print("Applying config")
  79. if not esp_prov.apply_wifi_config(transport, security):
  80. raise RuntimeError("Failed to send apply config")
  81. success = False
  82. while True:
  83. time.sleep(5)
  84. print("Wi-Fi connection state")
  85. ret = esp_prov.get_wifi_config(transport, security)
  86. if (ret == 1):
  87. continue
  88. elif (ret == 0):
  89. print("Provisioning was successful")
  90. success = True
  91. break
  92. if not success:
  93. raise RuntimeError("Provisioning failed")
  94. if __name__ == '__main__':
  95. test_examples_provisioning_softap()