ble_prov_test.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 os
  18. import re
  19. import esp_prov
  20. import ttfw_idf
  21. # Have esp_prov throw exception
  22. esp_prov.config_throw_except = True
  23. @ttfw_idf.idf_example_test(env_tag='Example_WIFI_BT')
  24. def test_examples_provisioning_ble(env, extra_data):
  25. # Acquire DUT
  26. dut1 = env.get_dut('ble_prov', 'examples/provisioning/legacy/ble_prov', dut_class=ttfw_idf.ESP32DUT)
  27. # Get binary file
  28. binary_file = os.path.join(dut1.app.binary_path, 'ble_prov.bin')
  29. bin_size = os.path.getsize(binary_file)
  30. ttfw_idf.log_performance('ble_prov_bin_size', '{}KB'.format(bin_size // 1024))
  31. # Upload binary and start testing
  32. dut1.start_app()
  33. # Parse BLE devname
  34. devname = dut1.expect(re.compile(r"Provisioning started with BLE devname : '(PROV_\S\S\S\S\S\S)'"), timeout=60)[0]
  35. print('BLE Device Alias for DUT :', devname)
  36. # Match additional headers sent in the request
  37. dut1.expect('BLE Provisioning started', timeout=30)
  38. print('Starting Provisioning')
  39. verbose = False
  40. protover = 'V0.1'
  41. secver = 1
  42. pop = 'abcd1234'
  43. provmode = 'ble'
  44. ap_ssid = 'myssid'
  45. ap_password = 'mypassword'
  46. print('Getting security')
  47. security = esp_prov.get_security(secver, pop, verbose)
  48. if security is None:
  49. raise RuntimeError('Failed to get security')
  50. print('Getting transport')
  51. transport = esp_prov.get_transport(provmode, devname)
  52. if transport is None:
  53. raise RuntimeError('Failed to get transport')
  54. print('Verifying protocol version')
  55. if not esp_prov.version_match(transport, protover):
  56. raise RuntimeError('Mismatch in protocol version')
  57. print('Starting Session')
  58. if not esp_prov.establish_session(transport, security):
  59. raise RuntimeError('Failed to start session')
  60. print('Sending Wifi credential to DUT')
  61. if not esp_prov.send_wifi_config(transport, security, ap_ssid, ap_password):
  62. raise RuntimeError('Failed to send Wi-Fi config')
  63. print('Applying config')
  64. if not esp_prov.apply_wifi_config(transport, security):
  65. raise RuntimeError('Failed to send apply config')
  66. if not esp_prov.wait_wifi_connected(transport, security):
  67. raise RuntimeError('Provisioning failed')
  68. if __name__ == '__main__':
  69. test_examples_provisioning_ble()