bleprph_test.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. from threading import Thread
  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("Try `export TEST_FW_PATH=$IDF_PATH/tools/tiny-test-fw` for resolving the issue")
  31. print("Try `pip install -r $IDF_PATH/tools/tiny-test-fw/requirements.txt` for resolving the issue")
  32. import IDF
  33. try:
  34. import lib_ble_client
  35. except ImportError:
  36. lib_ble_client_path = os.getenv("IDF_PATH") + "/tools/ble"
  37. if lib_ble_client_path and lib_ble_client_path not in sys.path:
  38. sys.path.insert(0, lib_ble_client_path)
  39. import lib_ble_client
  40. import Utility
  41. # When running on local machine execute the following before running this script
  42. # > make app bootloader
  43. # > make print_flash_cmd | tail -n 1 > build/download.config
  44. # > export TEST_FW_PATH=~/esp/esp-idf/tools/tiny-test-fw
  45. def bleprph_client_task(dut_addr, dut):
  46. interface = 'hci0'
  47. ble_devname = 'nimble-bleprph'
  48. srv_uuid = '2f12'
  49. # Get BLE client module
  50. ble_client_obj = lib_ble_client.BLE_Bluez_Client(interface, devname=ble_devname, devaddr=dut_addr)
  51. if not ble_client_obj:
  52. raise RuntimeError("Failed to get DBus-Bluez object")
  53. # Discover Bluetooth Adapter and power on
  54. is_adapter_set = ble_client_obj.set_adapter()
  55. if not is_adapter_set:
  56. raise RuntimeError("Adapter Power On failed !!")
  57. # Connect BLE Device
  58. is_connected = ble_client_obj.connect()
  59. if not is_connected:
  60. Utility.console_log("Connection to device ", ble_devname, "failed !!")
  61. # Call disconnect to perform cleanup operations before exiting application
  62. ble_client_obj.disconnect()
  63. return
  64. # Check dut responses
  65. dut.expect("GAP procedure initiated: advertise;", timeout=30)
  66. # Read Services
  67. services_ret = ble_client_obj.get_services(srv_uuid)
  68. if services_ret:
  69. print("\nServices\n")
  70. print(services_ret)
  71. else:
  72. print("Failure: Read Services failed")
  73. ble_client_obj.disconnect()
  74. return
  75. # Read Characteristics
  76. chars_ret = {}
  77. chars_ret = ble_client_obj.read_chars()
  78. if chars_ret:
  79. Utility.console_log("\nCharacteristics retrieved")
  80. for path, props in chars_ret.items():
  81. print("\n\tCharacteristic: ", path)
  82. print("\tCharacteristic UUID: ", props[2])
  83. print("\tValue: ", props[0])
  84. print("\tProperties: : ", props[1])
  85. else:
  86. print("Failure: Read Characteristics failed")
  87. ble_client_obj.disconnect()
  88. return
  89. '''
  90. Write Characteristics
  91. - write 'A' to characteristic with write permission
  92. '''
  93. chars_ret_on_write = {}
  94. chars_ret_on_write = ble_client_obj.write_chars('A')
  95. if chars_ret_on_write:
  96. Utility.console_log("\nCharacteristics after write operation")
  97. for path, props in chars_ret_on_write.items():
  98. print("\n\tCharacteristic:", path)
  99. print("\tCharacteristic UUID: ", props[2])
  100. print("\tValue:", props[0])
  101. print("\tProperties: : ", props[1])
  102. else:
  103. print("Failure: Write Characteristics failed")
  104. ble_client_obj.disconnect()
  105. return
  106. # Call disconnect to perform cleanup operations before exiting application
  107. ble_client_obj.disconnect()
  108. @IDF.idf_example_test(env_tag="Example_WIFI_BT")
  109. def test_example_app_ble_peripheral(env, extra_data):
  110. """
  111. Steps:
  112. 1. Discover Bluetooth Adapter and Power On
  113. 2. Connect BLE Device
  114. 3. Read Services
  115. 4. Read Characteristics
  116. 5. Write Characteristics
  117. """
  118. try:
  119. # Acquire DUT
  120. dut = env.get_dut("bleprph", "examples/bluetooth/nimble/bleprph")
  121. # Get binary file
  122. binary_file = os.path.join(dut.app.binary_path, "bleprph.bin")
  123. bin_size = os.path.getsize(binary_file)
  124. IDF.log_performance("bleprph_bin_size", "{}KB".format(bin_size // 1024))
  125. IDF.check_performance("bleprph_bin_size", bin_size // 1024)
  126. # Upload binary and start testing
  127. Utility.console_log("Starting bleprph simple example test app")
  128. dut.start_app()
  129. subprocess.check_output(['rm','-rf','/var/lib/bluetooth/*'])
  130. # Get device address from dut
  131. dut_addr = dut.expect(re.compile(r"Device Address: ([a-fA-F0-9:]+)"), timeout=30)[0]
  132. # Starting a py-client in a separate thread
  133. thread1 = Thread(target=bleprph_client_task, args=(dut_addr,dut,))
  134. thread1.start()
  135. thread1.join()
  136. # Check dut responses
  137. dut.expect("connection established; status=0", timeout=30)
  138. dut.expect("disconnect;", timeout=30)
  139. except Exception as e:
  140. sys.exit(e)
  141. if __name__ == '__main__':
  142. test_example_app_ble_peripheral()