bleprph_test.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 subprocess
  20. import threading
  21. import traceback
  22. try:
  23. import Queue
  24. except ImportError:
  25. import queue as Queue
  26. import ttfw_idf
  27. from ble import lib_ble_client
  28. from tiny_test_fw import Utility
  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. # > export TEST_FW_PATH=~/esp/esp-idf/tools/tiny-test-fw
  33. def bleprph_client_task(dut, dut_addr):
  34. interface = 'hci0'
  35. ble_devname = 'nimble-bleprph'
  36. srv_uuid = '2f12'
  37. write_value = b'A'
  38. # Get BLE client module
  39. ble_client_obj = lib_ble_client.BLE_Bluez_Client(iface=interface)
  40. # Discover Bluetooth Adapter and power on
  41. is_adapter_set = ble_client_obj.set_adapter()
  42. if not is_adapter_set:
  43. return
  44. # Connect BLE Device
  45. is_connected = ble_client_obj.connect(
  46. devname=ble_devname,
  47. devaddr=dut_addr)
  48. if not is_connected:
  49. return
  50. # Get services of the connected device
  51. services = ble_client_obj.get_services()
  52. if not services:
  53. ble_client_obj.disconnect()
  54. return
  55. # Verify service uuid exists
  56. service_exists = ble_client_obj.get_service_if_exists(srv_uuid)
  57. if not service_exists:
  58. ble_client_obj.disconnect()
  59. return
  60. # Get characteristics of the connected device
  61. ble_client_obj.get_chars()
  62. # Read properties of characteristics (uuid, value and permissions)
  63. ble_client_obj.read_chars()
  64. # Write new value to characteristic having read and write permission
  65. # and display updated value
  66. write_char = ble_client_obj.write_chars(write_value)
  67. if not write_char:
  68. ble_client_obj.disconnect()
  69. return
  70. # Disconnect device
  71. ble_client_obj.disconnect()
  72. # Check dut responses
  73. dut.expect('connection established; status=0', timeout=30)
  74. dut.expect('disconnect;', timeout=30)
  75. class BlePrphThread(threading.Thread):
  76. def __init__(self, dut, dut_addr, exceptions_queue):
  77. threading.Thread.__init__(self)
  78. self.dut = dut
  79. self.dut_addr = dut_addr
  80. self.exceptions_queue = exceptions_queue
  81. def run(self):
  82. try:
  83. bleprph_client_task(self.dut, self.dut_addr)
  84. except RuntimeError:
  85. self.exceptions_queue.put(traceback.format_exc(), block=False)
  86. @ttfw_idf.idf_example_test(env_tag='Example_WIFI_BT')
  87. def test_example_app_ble_peripheral(env, extra_data):
  88. """
  89. Steps:
  90. 1. Discover Bluetooth Adapter and Power On
  91. 2. Connect BLE Device
  92. 3. Read Services
  93. 4. Read Characteristics
  94. 5. Write Characteristics
  95. """
  96. # Remove cached bluetooth devices of any previous connections
  97. subprocess.check_output(['rm', '-rf', '/var/lib/bluetooth/*'])
  98. subprocess.check_output(['hciconfig', 'hci0', 'reset'])
  99. # Acquire DUT
  100. dut = env.get_dut('bleprph', 'examples/bluetooth/nimble/bleprph', dut_class=ttfw_idf.ESP32DUT)
  101. # Get binary file
  102. binary_file = os.path.join(dut.app.binary_path, 'bleprph.bin')
  103. bin_size = os.path.getsize(binary_file)
  104. ttfw_idf.log_performance('bleprph_bin_size', '{}KB'.format(bin_size // 1024))
  105. # Upload binary and start testing
  106. Utility.console_log('Starting bleprph simple example test app')
  107. dut.start_app()
  108. dut.reset()
  109. # Get device address from dut
  110. dut_addr = dut.expect(re.compile(r'Device Address: ([a-fA-F0-9:]+)'), timeout=30)[0]
  111. # Check dut responses
  112. dut.expect('GAP procedure initiated: advertise;', timeout=30)
  113. # Starting a py-client in a separate thread
  114. exceptions_queue = Queue.Queue()
  115. bleprph_thread_obj = BlePrphThread(dut, dut_addr, exceptions_queue)
  116. bleprph_thread_obj.start()
  117. bleprph_thread_obj.join()
  118. exception_msg = None
  119. while True:
  120. try:
  121. exception_msg = exceptions_queue.get(block=False)
  122. except Queue.Empty:
  123. break
  124. else:
  125. Utility.console_log('\n' + exception_msg)
  126. if exception_msg:
  127. raise Exception('BlePrph thread did not run successfully')
  128. if __name__ == '__main__':
  129. test_example_app_ble_peripheral()