blehr_test.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 threading
  21. import traceback
  22. import subprocess
  23. try:
  24. import Queue
  25. except ImportError:
  26. import queue as Queue
  27. try:
  28. # This environment variable is expected on the host machine
  29. test_fw_path = os.getenv("TEST_FW_PATH")
  30. if test_fw_path and test_fw_path not in sys.path:
  31. sys.path.insert(0, test_fw_path)
  32. import IDF
  33. except ImportError as e:
  34. print(e)
  35. print("\nCheck your IDF_PATH\nOR")
  36. print("Try `export TEST_FW_PATH=$IDF_PATH/tools/tiny-test-fw` for resolving the issue\nOR")
  37. print("Try `pip install -r $IDF_PATH/tools/tiny-test-fw/requirements.txt` for resolving the issue\n")
  38. import IDF
  39. try:
  40. import lib_ble_client
  41. except ImportError:
  42. lib_ble_client_path = os.getenv("IDF_PATH") + "/tools/ble"
  43. if lib_ble_client_path and lib_ble_client_path not in sys.path:
  44. sys.path.insert(0, lib_ble_client_path)
  45. import lib_ble_client
  46. import Utility
  47. # When running on local machine execute the following before running this script
  48. # > make app bootloader
  49. # > make print_flash_cmd | tail -n 1 > build/download.config
  50. # > export TEST_FW_PATH=~/esp/esp-idf/tools/tiny-test-fw
  51. def blehr_client_task(hr_obj, dut_addr):
  52. interface = 'hci0'
  53. ble_devname = 'blehr_sensor_1.0'
  54. hr_srv_uuid = '180d'
  55. hr_char_uuid = '2a37'
  56. # Get BLE client module
  57. ble_client_obj = lib_ble_client.BLE_Bluez_Client(interface, devname=ble_devname, devaddr=dut_addr)
  58. if not ble_client_obj:
  59. raise RuntimeError("Failed to get DBus-Bluez object")
  60. # Discover Bluetooth Adapter and power on
  61. is_adapter_set = ble_client_obj.set_adapter()
  62. if not is_adapter_set:
  63. raise RuntimeError("Adapter Power On failed !!")
  64. # Connect BLE Device
  65. is_connected = ble_client_obj.connect()
  66. if not is_connected:
  67. # Call disconnect to perform cleanup operations before exiting application
  68. ble_client_obj.disconnect()
  69. raise RuntimeError("Connection to device " + str(ble_devname) + " failed !!")
  70. # Read Services
  71. services_ret = ble_client_obj.get_services()
  72. if services_ret:
  73. Utility.console_log("\nServices\n")
  74. Utility.console_log(str(services_ret))
  75. else:
  76. ble_client_obj.disconnect()
  77. raise RuntimeError("Failure: Read Services failed")
  78. '''
  79. Blehr application run:
  80. Start Notifications
  81. Retrieve updated value
  82. Stop Notifications
  83. '''
  84. blehr_ret = ble_client_obj.hr_update_simulation(hr_srv_uuid, hr_char_uuid)
  85. if blehr_ret:
  86. Utility.console_log("Success: blehr example test passed")
  87. else:
  88. raise RuntimeError("Failure: blehr example test failed")
  89. # Call disconnect to perform cleanup operations before exiting application
  90. ble_client_obj.disconnect()
  91. class BleHRThread(threading.Thread):
  92. def __init__(self, dut_addr, exceptions_queue):
  93. threading.Thread.__init__(self)
  94. self.dut_addr = dut_addr
  95. self.exceptions_queue = exceptions_queue
  96. def run(self):
  97. try:
  98. blehr_client_task(self, self.dut_addr)
  99. except Exception:
  100. self.exceptions_queue.put(traceback.format_exc(), block=False)
  101. @IDF.idf_example_test(env_tag="Example_WIFI_BT")
  102. def test_example_app_ble_hr(env, extra_data):
  103. """
  104. Steps:
  105. 1. Discover Bluetooth Adapter and Power On
  106. 2. Connect BLE Device
  107. 3. Start Notifications
  108. 4. Updated value is retrieved
  109. 5. Stop Notifications
  110. """
  111. subprocess.check_output(['rm','-rf','/var/lib/bluetooth/*'])
  112. subprocess.check_output(['hciconfig','hci0','reset'])
  113. # Acquire DUT
  114. dut = env.get_dut("blehr", "examples/bluetooth/nimble/blehr")
  115. # Get binary file
  116. binary_file = os.path.join(dut.app.binary_path, "blehr.bin")
  117. bin_size = os.path.getsize(binary_file)
  118. IDF.log_performance("blehr_bin_size", "{}KB".format(bin_size // 1024))
  119. IDF.check_performance("blehr_bin_size", bin_size // 1024)
  120. # Upload binary and start testing
  121. Utility.console_log("Starting blehr simple example test app")
  122. dut.start_app()
  123. dut.reset()
  124. # Get device address from dut
  125. dut_addr = dut.expect(re.compile(r"Device Address: ([a-fA-F0-9:]+)"), timeout=30)[0]
  126. exceptions_queue = Queue.Queue()
  127. # Starting a py-client in a separate thread
  128. blehr_thread_obj = BleHRThread(dut_addr, exceptions_queue)
  129. blehr_thread_obj.start()
  130. blehr_thread_obj.join()
  131. exception_msg = None
  132. while True:
  133. try:
  134. exception_msg = exceptions_queue.get(block=False)
  135. except Queue.Empty:
  136. break
  137. else:
  138. Utility.console_log("\n" + exception_msg)
  139. if exception_msg:
  140. raise Exception("Thread did not run successfully")
  141. # Check dut responses
  142. dut.expect("subscribe event; cur_notify=1", timeout=30)
  143. dut.expect("subscribe event; cur_notify=0", timeout=30)
  144. dut.expect("disconnect;", timeout=30)
  145. if __name__ == '__main__':
  146. test_example_app_ble_hr()