blecent_test.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 uuid
  20. import subprocess
  21. from tiny_test_fw import Utility
  22. import ttfw_idf
  23. from ble import lib_ble_client
  24. # When running on local machine execute the following before running this script
  25. # > make app bootloader
  26. # > make print_flash_cmd | tail -n 1 > build/download.config
  27. @ttfw_idf.idf_example_test(env_tag="Example_WIFI_BT")
  28. def test_example_app_ble_central(env, extra_data):
  29. """
  30. Steps:
  31. 1. Discover Bluetooth Adapter and Power On
  32. """
  33. interface = 'hci0'
  34. adv_host_name = "BleCentTestApp"
  35. adv_iface_index = 0
  36. adv_type = 'peripheral'
  37. adv_uuid = '1811'
  38. subprocess.check_output(['rm','-rf','/var/lib/bluetooth/*'])
  39. subprocess.check_output(['hciconfig','hci0','reset'])
  40. # Acquire DUT
  41. dut = env.get_dut("blecent", "examples/bluetooth/nimble/blecent", dut_class=ttfw_idf.ESP32DUT)
  42. # Get binary file
  43. binary_file = os.path.join(dut.app.binary_path, "blecent.bin")
  44. bin_size = os.path.getsize(binary_file)
  45. ttfw_idf.log_performance("blecent_bin_size", "{}KB".format(bin_size // 1024))
  46. # Upload binary and start testing
  47. Utility.console_log("Starting blecent example test app")
  48. dut.start_app()
  49. dut.reset()
  50. device_addr = ':'.join(re.findall('..', '%012x' % uuid.getnode()))
  51. # Get BLE client module
  52. ble_client_obj = lib_ble_client.BLE_Bluez_Client(interface)
  53. if not ble_client_obj:
  54. raise RuntimeError("Get DBus-Bluez object failed !!")
  55. # Discover Bluetooth Adapter and power on
  56. is_adapter_set = ble_client_obj.set_adapter()
  57. if not is_adapter_set:
  58. raise RuntimeError("Adapter Power On failed !!")
  59. # Write device address to dut
  60. dut.expect("BLE Host Task Started", timeout=60)
  61. dut.write(device_addr + "\n")
  62. '''
  63. Blecent application run:
  64. Create GATT data
  65. Register GATT Application
  66. Create Advertising data
  67. Register advertisement
  68. Start advertising
  69. '''
  70. ble_client_obj.start_advertising(adv_host_name, adv_iface_index, adv_type, adv_uuid)
  71. # Call disconnect to perform cleanup operations before exiting application
  72. ble_client_obj.disconnect()
  73. # Check dut responses
  74. dut.expect("Connection established", timeout=60)
  75. dut.expect("Service discovery complete; status=0", timeout=60)
  76. print("Service discovery passed\n\tService Discovery Status: 0")
  77. dut.expect("GATT procedure initiated: read;", timeout=60)
  78. dut.expect("Read complete; status=0", timeout=60)
  79. print("Read passed\n\tSupportedNewAlertCategoryCharacteristic\n\tRead Status: 0")
  80. dut.expect("GATT procedure initiated: write;", timeout=60)
  81. dut.expect("Write complete; status=0", timeout=60)
  82. print("Write passed\n\tAlertNotificationControlPointCharacteristic\n\tWrite Status: 0")
  83. dut.expect("GATT procedure initiated: write;", timeout=60)
  84. dut.expect("Subscribe complete; status=0", timeout=60)
  85. print("Subscribe passed\n\tClientCharacteristicConfigurationDescriptor\n\tSubscribe Status: 0")
  86. if __name__ == '__main__':
  87. test_example_app_ble_central()