blehr_test.py 4.8 KB

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