softap_prov_test.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 tiny_test_fw
  21. import ttfw_idf
  22. import wifi_tools
  23. from tiny_test_fw import Utility
  24. # Have esp_prov throw exception
  25. esp_prov.config_throw_except = True
  26. @ttfw_idf.idf_example_test(env_tag='Example_WIFI_BT')
  27. def test_examples_provisioning_softap(env, extra_data):
  28. # Acquire DUT
  29. dut1 = env.get_dut('softap_prov', 'examples/provisioning/legacy/softap_prov', dut_class=ttfw_idf.ESP32DUT)
  30. # Get binary file
  31. binary_file = os.path.join(dut1.app.binary_path, 'softap_prov.bin')
  32. bin_size = os.path.getsize(binary_file)
  33. ttfw_idf.log_performance('softap_prov_bin_size', '{}KB'.format(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. try:
  46. ctrl = wifi_tools.wpa_cli(iface, reset_on_exit=True)
  47. print('Connecting to DUT SoftAP...')
  48. try:
  49. ip = ctrl.connect(ssid, password)
  50. except RuntimeError as err:
  51. Utility.console_log('error: {}'.format(err))
  52. try:
  53. got_ip = dut1.expect(re.compile(r'DHCP server assigned IP to a station, IP is: (\d+.\d+.\d+.\d+)'), timeout=60)
  54. Utility.console_log('got_ip: {}'.format(got_ip))
  55. got_ip = got_ip[0]
  56. if ip != got_ip:
  57. raise RuntimeError('SoftAP connected to another host! {} != {}'.format(ip, got_ip))
  58. except tiny_test_fw.DUT.ExpectTimeout:
  59. # print what is happening on dut side
  60. Utility.console_log('in exception tiny_test_fw.DUT.ExpectTimeout')
  61. Utility.console_log(dut1.read())
  62. raise
  63. print('Connected to DUT SoftAP')
  64. print('Starting Provisioning')
  65. verbose = False
  66. protover = 'V0.1'
  67. secver = 1
  68. pop = 'abcd1234'
  69. provmode = 'softap'
  70. ap_ssid = 'myssid'
  71. ap_password = 'mypassword'
  72. softap_endpoint = '{}.{}.{}.1:80'.format(ip.split('.')[0], ip.split('.')[1], ip.split('.')[2])
  73. print('Getting security')
  74. security = esp_prov.get_security(secver, pop, verbose)
  75. if security is None:
  76. raise RuntimeError('Failed to get security')
  77. print('Getting transport')
  78. transport = esp_prov.get_transport(provmode, softap_endpoint)
  79. if transport is None:
  80. raise RuntimeError('Failed to get transport')
  81. print('Verifying protocol version')
  82. if not esp_prov.version_match(transport, protover):
  83. raise RuntimeError('Mismatch in protocol version')
  84. print('Starting Session')
  85. if not esp_prov.establish_session(transport, security):
  86. raise RuntimeError('Failed to start session')
  87. print('Sending Wifi credential to DUT')
  88. if not esp_prov.send_wifi_config(transport, security, ap_ssid, ap_password):
  89. raise RuntimeError('Failed to send Wi-Fi config')
  90. print('Applying config')
  91. if not esp_prov.apply_wifi_config(transport, security):
  92. raise RuntimeError('Failed to send apply config')
  93. if not esp_prov.wait_wifi_connected(transport, security):
  94. raise RuntimeError('Provisioning failed')
  95. finally:
  96. ctrl.reset()
  97. if __name__ == '__main__':
  98. test_examples_provisioning_softap()