blecent_test.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #!/usr/bin/env python
  2. #
  3. # Copyright 2019 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 sys
  19. import re
  20. import uuid
  21. import subprocess
  22. try:
  23. # This environment variable is expected on the host machine
  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. from IDF.IDFDUT import ESP32DUT
  29. except ImportError as e:
  30. print(e)
  31. print("\nCheck your IDF_PATH\nOR")
  32. print("Try `export TEST_FW_PATH=$IDF_PATH/tools/tiny-test-fw` for resolving the issue\nOR")
  33. print("Try `pip install -r $IDF_PATH/tools/tiny-test-fw/requirements.txt` for resolving the issue")
  34. import IDF
  35. try:
  36. import lib_ble_client
  37. except ImportError:
  38. lib_ble_client_path = os.getenv("IDF_PATH") + "/tools/ble"
  39. if lib_ble_client_path and lib_ble_client_path not in sys.path:
  40. sys.path.insert(0, lib_ble_client_path)
  41. import lib_ble_client
  42. import Utility
  43. # When running on local machine execute the following before running this script
  44. # > make app bootloader
  45. # > make print_flash_cmd | tail -n 1 > build/download.config
  46. # > export TEST_FW_PATH=~/esp/esp-idf/tools/tiny-test-fw
  47. @IDF.idf_example_test(env_tag="Example_WIFI_BT")
  48. def test_example_app_ble_central(env, extra_data):
  49. """
  50. Steps:
  51. 1. Discover Bluetooth Adapter and Power On
  52. """
  53. interface = 'hci0'
  54. adv_host_name = "BleCentTestApp"
  55. adv_iface_index = 0
  56. adv_type = 'peripheral'
  57. adv_uuid = '1811'
  58. subprocess.check_output(['rm','-rf','/var/lib/bluetooth/*'])
  59. subprocess.check_output(['hciconfig','hci0','reset'])
  60. # Acquire DUT
  61. dut = env.get_dut("blecent", "examples/bluetooth/nimble/blecent", dut_class=ESP32DUT)
  62. # Get binary file
  63. binary_file = os.path.join(dut.app.binary_path, "blecent.bin")
  64. bin_size = os.path.getsize(binary_file)
  65. IDF.log_performance("blecent_bin_size", "{}KB".format(bin_size // 1024))
  66. # Upload binary and start testing
  67. Utility.console_log("Starting blecent example test app")
  68. dut.start_app()
  69. dut.reset()
  70. device_addr = ':'.join(re.findall('..', '%012x' % uuid.getnode()))
  71. # Get BLE client module
  72. ble_client_obj = lib_ble_client.BLE_Bluez_Client(interface)
  73. if not ble_client_obj:
  74. raise RuntimeError("Get DBus-Bluez object failed !!")
  75. # Discover Bluetooth Adapter and power on
  76. is_adapter_set = ble_client_obj.set_adapter()
  77. if not is_adapter_set:
  78. raise RuntimeError("Adapter Power On failed !!")
  79. # Write device address to dut
  80. dut.expect("BLE Host Task Started", timeout=60)
  81. dut.write(device_addr + "\n")
  82. '''
  83. Blecent application run:
  84. Create GATT data
  85. Register GATT Application
  86. Create Advertising data
  87. Register advertisement
  88. Start advertising
  89. '''
  90. ble_client_obj.start_advertising(adv_host_name, adv_iface_index, adv_type, adv_uuid)
  91. # Call disconnect to perform cleanup operations before exiting application
  92. ble_client_obj.disconnect()
  93. # Check dut responses
  94. dut.expect("Connection established", timeout=30)
  95. dut.expect("Service discovery complete; status=0", timeout=30)
  96. print("Service discovery passed\n\tService Discovery Status: 0")
  97. dut.expect("GATT procedure initiated: read;", timeout=30)
  98. dut.expect("Read complete; status=0", timeout=30)
  99. print("Read passed\n\tSupportedNewAlertCategoryCharacteristic\n\tRead Status: 0")
  100. dut.expect("GATT procedure initiated: write;", timeout=30)
  101. dut.expect("Write complete; status=0", timeout=30)
  102. print("Write passed\n\tAlertNotificationControlPointCharacteristic\n\tWrite Status: 0")
  103. dut.expect("GATT procedure initiated: write;", timeout=30)
  104. dut.expect("Subscribe complete; status=0", timeout=30)
  105. print("Subscribe passed\n\tClientCharacteristicConfigurationDescriptor\n\tSubscribe Status: 0")
  106. if __name__ == '__main__':
  107. test_example_app_ble_central()