blecent_test.py 4.4 KB

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