ble_prov_test.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. from IDF.IDFDUT import ESP32DUT
  24. except ImportError:
  25. test_fw_path = os.getenv("TEST_FW_PATH")
  26. if test_fw_path and test_fw_path not in sys.path:
  27. sys.path.insert(0, test_fw_path)
  28. import IDF
  29. try:
  30. import esp_prov
  31. except ImportError:
  32. esp_prov_path = os.getenv("IDF_PATH") + "/tools/esp_prov"
  33. if esp_prov_path and esp_prov_path not in sys.path:
  34. sys.path.insert(0, esp_prov_path)
  35. import esp_prov
  36. # Have esp_prov throw exception
  37. esp_prov.config_throw_except = True
  38. @IDF.idf_example_test(env_tag="Example_WIFI_BT")
  39. def test_examples_provisioning_ble(env, extra_data):
  40. # Acquire DUT
  41. dut1 = env.get_dut("ble_prov", "examples/provisioning/ble_prov", dut_class=ESP32DUT)
  42. # Get binary file
  43. binary_file = os.path.join(dut1.app.binary_path, "ble_prov.bin")
  44. bin_size = os.path.getsize(binary_file)
  45. IDF.log_performance("ble_prov_bin_size", "{}KB".format(bin_size // 1024))
  46. IDF.check_performance("ble_prov_bin_size", bin_size // 1024)
  47. # Upload binary and start testing
  48. dut1.start_app()
  49. # Parse BLE devname
  50. devname = dut1.expect(re.compile(r"Provisioning started with BLE devname : '(PROV_\S\S\S\S\S\S)'"), timeout=60)[0]
  51. print("BLE Device Alias for DUT :", devname)
  52. # Match additional headers sent in the request
  53. dut1.expect("BLE Provisioning started", timeout=30)
  54. print("Starting Provisioning")
  55. verbose = False
  56. protover = "V0.1"
  57. secver = 1
  58. pop = "abcd1234"
  59. provmode = "ble"
  60. ap_ssid = "myssid"
  61. ap_password = "mypassword"
  62. print("Getting security")
  63. security = esp_prov.get_security(secver, pop, verbose)
  64. if security is None:
  65. raise RuntimeError("Failed to get security")
  66. print("Getting transport")
  67. transport = esp_prov.get_transport(provmode, devname)
  68. if transport is None:
  69. raise RuntimeError("Failed to get transport")
  70. print("Verifying protocol version")
  71. if not esp_prov.version_match(transport, protover):
  72. raise RuntimeError("Mismatch in protocol version")
  73. print("Starting Session")
  74. if not esp_prov.establish_session(transport, security):
  75. raise RuntimeError("Failed to start session")
  76. print("Sending Wifi credential to DUT")
  77. if not esp_prov.send_wifi_config(transport, security, ap_ssid, ap_password):
  78. raise RuntimeError("Failed to send Wi-Fi config")
  79. print("Applying config")
  80. if not esp_prov.apply_wifi_config(transport, security):
  81. raise RuntimeError("Failed to send apply config")
  82. success = False
  83. while True:
  84. time.sleep(5)
  85. print("Wi-Fi connection state")
  86. ret = esp_prov.get_wifi_config(transport, security)
  87. if (ret == 1):
  88. continue
  89. elif (ret == 0):
  90. print("Provisioning was successful")
  91. success = True
  92. break
  93. if not success:
  94. raise RuntimeError("Provisioning failed")
  95. if __name__ == '__main__':
  96. test_examples_provisioning_ble()