blecent_test.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 subprocess
  19. import threading
  20. import traceback
  21. try:
  22. import Queue
  23. except ImportError:
  24. import queue as Queue
  25. import ttfw_idf
  26. from ble import lib_ble_client
  27. from tiny_test_fw import Utility
  28. # When running on local machine execute the following before running this script
  29. # > make app bootloader
  30. # > make print_flash_cmd | tail -n 1 > build/download.config
  31. def blecent_client_task(dut):
  32. interface = 'hci0'
  33. adv_host_name = 'BleCentTestApp'
  34. adv_type = 'peripheral'
  35. adv_uuid = '1811'
  36. # Get BLE client module
  37. ble_client_obj = lib_ble_client.BLE_Bluez_Client(iface=interface)
  38. # Discover Bluetooth Adapter and power on
  39. is_adapter_set = ble_client_obj.set_adapter()
  40. if not is_adapter_set:
  41. return
  42. '''
  43. Blecent application run:
  44. Create GATT data
  45. Register GATT Application
  46. Create Advertising data
  47. Register advertisement
  48. Start advertising
  49. '''
  50. # Create Gatt Application
  51. # Register GATT Application
  52. ble_client_obj.register_gatt_app()
  53. # Register Advertisement
  54. # Check read/write/subscribe is received from device
  55. ble_client_obj.register_adv(adv_host_name, adv_type, adv_uuid)
  56. # Check dut responses
  57. dut.expect('Connection established', timeout=30)
  58. dut.expect('Service discovery complete; status=0', timeout=30)
  59. dut.expect('GATT procedure initiated: read;', timeout=30)
  60. dut.expect('Read complete; status=0', timeout=30)
  61. dut.expect('GATT procedure initiated: write;', timeout=30)
  62. dut.expect('Write complete; status=0', timeout=30)
  63. dut.expect('GATT procedure initiated: write;', timeout=30)
  64. dut.expect('Subscribe complete; status=0', timeout=30)
  65. dut.expect('received notification;', timeout=30)
  66. class BleCentThread(threading.Thread):
  67. def __init__(self, dut, exceptions_queue):
  68. threading.Thread.__init__(self)
  69. self.dut = dut
  70. self.exceptions_queue = exceptions_queue
  71. def run(self):
  72. try:
  73. blecent_client_task(self.dut)
  74. except RuntimeError:
  75. self.exceptions_queue.put(traceback.format_exc(), block=False)
  76. @ttfw_idf.idf_example_test(env_tag='Example_WIFI_BT')
  77. def test_example_app_ble_central(env, extra_data):
  78. """
  79. Steps:
  80. 1. Discover Bluetooth Adapter and Power On
  81. 2. Connect BLE Device
  82. 3. Start Notifications
  83. 4. Updated value is retrieved
  84. 5. Stop Notifications
  85. """
  86. # Remove cached bluetooth devices of any previous connections
  87. subprocess.check_output(['rm', '-rf', '/var/lib/bluetooth/*'])
  88. subprocess.check_output(['hciconfig', 'hci0', 'reset'])
  89. # Acquire DUT
  90. dut = env.get_dut('blecent', 'examples/bluetooth/nimble/blecent', dut_class=ttfw_idf.ESP32DUT)
  91. # Get binary file
  92. binary_file = os.path.join(dut.app.binary_path, 'blecent.bin')
  93. bin_size = os.path.getsize(binary_file)
  94. ttfw_idf.log_performance('blecent_bin_size', '{}KB'.format(bin_size // 1024))
  95. # Upload binary and start testing
  96. Utility.console_log('Starting blecent example test app')
  97. dut.start_app()
  98. dut.reset()
  99. exceptions_queue = Queue.Queue()
  100. # Starting a py-client in a separate thread
  101. blehr_thread_obj = BleCentThread(dut, exceptions_queue)
  102. blehr_thread_obj.start()
  103. blehr_thread_obj.join()
  104. exception_msg = None
  105. while True:
  106. try:
  107. exception_msg = exceptions_queue.get(block=False)
  108. except Queue.Empty:
  109. break
  110. else:
  111. Utility.console_log('\n' + exception_msg)
  112. if exception_msg:
  113. raise Exception('Blecent thread did not run successfully')
  114. if __name__ == '__main__':
  115. test_example_app_ble_central()