wifi_prov_mgr_test.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. # Have esp_prov throw exception
  36. esp_prov.config_throw_except = True
  37. @IDF.idf_example_test(env_tag="Example_WIFI_BT")
  38. def test_examples_wifi_prov_mgr(env, extra_data):
  39. # Acquire DUT
  40. dut1 = env.get_dut("wifi_prov_mgr", "examples/provisioning/manager")
  41. # Get binary file
  42. binary_file = os.path.join(dut1.app.binary_path, "wifi_prov_mgr.bin")
  43. bin_size = os.path.getsize(binary_file)
  44. IDF.log_performance("wifi_prov_mgr_bin_size", "{}KB".format(bin_size // 1024))
  45. IDF.check_performance("wifi_prov_mgr_bin_size", bin_size // 1024)
  46. # Upload binary and start testing
  47. dut1.start_app()
  48. # Check if BT memory is released before provisioning starts
  49. dut1.expect("wifi_prov_scheme_ble: BT memory released", timeout=60)
  50. # Parse BLE devname
  51. devname = dut1.expect(re.compile(r"Provisioning started with service name : (PROV_\S\S\S\S\S\S)"), timeout=30)[0]
  52. print("BLE Device Alias for DUT :", devname)
  53. print("Starting Provisioning")
  54. verbose = False
  55. protover = "v1.1"
  56. secver = 1
  57. pop = "abcd1234"
  58. provmode = "ble"
  59. ap_ssid = "myssid"
  60. ap_password = "mypassword"
  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, devname)
  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("Verifying scan list capability")
  73. if not esp_prov.has_capability(transport, 'wifi_scan'):
  74. raise RuntimeError("Capability not present")
  75. print("Starting Session")
  76. if not esp_prov.establish_session(transport, security):
  77. raise RuntimeError("Failed to start session")
  78. print("Sending Wifi credential to DUT")
  79. if not esp_prov.send_wifi_config(transport, security, ap_ssid, ap_password):
  80. raise RuntimeError("Failed to send Wi-Fi config")
  81. print("Applying config")
  82. if not esp_prov.apply_wifi_config(transport, security):
  83. raise RuntimeError("Failed to send apply config")
  84. success = False
  85. retry = 0
  86. while True:
  87. time.sleep(5)
  88. print("Wi-Fi connection state")
  89. ret = esp_prov.get_wifi_config(transport, security)
  90. if (ret == 1):
  91. continue
  92. elif (ret == 0):
  93. print("Provisioning was successful")
  94. success = True
  95. elif (ret == 3 and retry < 3):
  96. retry = retry + 1
  97. print("Connection failed.. retry again...: ", ret)
  98. continue
  99. break
  100. if not success:
  101. raise RuntimeError("Provisioning failed")
  102. # Check if BTDM memory is released after provisioning finishes
  103. dut1.expect("wifi_prov_scheme_ble: BTDM memory released", timeout=30)
  104. if __name__ == '__main__':
  105. test_examples_wifi_prov_mgr()