ble-wifi-example-tests: Add fixes and cleanups to ble and wifi tests

This commit is contained in:
Shivani Tipnis
2021-04-07 17:13:02 +05:30
parent b9911e01b3
commit 2d22374460
10 changed files with 953 additions and 982 deletions

View File

@@ -17,9 +17,14 @@
from __future__ import print_function
import os
import re
import subprocess
import uuid
import threading
import traceback
try:
import Queue
except ImportError:
import queue as Queue
import ttfw_idf
from ble import lib_ble_client
@@ -30,21 +35,75 @@ from tiny_test_fw import Utility
# > make print_flash_cmd | tail -n 1 > build/download.config
def blecent_client_task(dut):
interface = 'hci0'
adv_host_name = 'BleCentTestApp'
adv_type = 'peripheral'
adv_uuid = '1811'
# Get BLE client module
ble_client_obj = lib_ble_client.BLE_Bluez_Client(iface=interface)
# Discover Bluetooth Adapter and power on
is_adapter_set = ble_client_obj.set_adapter()
if not is_adapter_set:
return
'''
Blecent application run:
Create GATT data
Register GATT Application
Create Advertising data
Register advertisement
Start advertising
'''
# Create Gatt Application
# Register GATT Application
ble_client_obj.register_gatt_app()
# Register Advertisement
# Check read/write/subscribe is received from device
ble_client_obj.register_adv(adv_host_name, adv_type, adv_uuid)
# Check dut responses
dut.expect('Connection established', timeout=30)
dut.expect('Service discovery complete; status=0', timeout=30)
dut.expect('GATT procedure initiated: read;', timeout=30)
dut.expect('Read complete; status=0', timeout=30)
dut.expect('GATT procedure initiated: write;', timeout=30)
dut.expect('Write complete; status=0', timeout=30)
dut.expect('GATT procedure initiated: write;', timeout=30)
dut.expect('Subscribe complete; status=0', timeout=30)
dut.expect('received notification;', timeout=30)
class BleCentThread(threading.Thread):
def __init__(self, dut, exceptions_queue):
threading.Thread.__init__(self)
self.dut = dut
self.exceptions_queue = exceptions_queue
def run(self):
try:
blecent_client_task(self.dut)
except RuntimeError:
self.exceptions_queue.put(traceback.format_exc(), block=False)
@ttfw_idf.idf_example_test(env_tag='Example_WIFI_BT')
def test_example_app_ble_central(env, extra_data):
"""
Steps:
1. Discover Bluetooth Adapter and Power On
2. Connect BLE Device
3. Start Notifications
4. Updated value is retrieved
5. Stop Notifications
"""
# Remove cached bluetooth devices of any previous connections
subprocess.check_output(['rm', '-rf', '/var/lib/bluetooth/*'])
subprocess.check_output(['hciconfig', 'hci0', 'reset'])
interface = 'hci0'
adv_host_name = 'BleCentTestApp'
adv_iface_index = 0
adv_type = 'peripheral'
adv_uuid = '1811'
subprocess.check_output(['rm','-rf','/var/lib/bluetooth/*'])
subprocess.check_output(['hciconfig','hci0','reset'])
# Acquire DUT
dut = env.get_dut('blecent', 'examples/bluetooth/nimble/blecent', dut_class=ttfw_idf.ESP32DUT)
@@ -58,52 +117,23 @@ def test_example_app_ble_central(env, extra_data):
dut.start_app()
dut.reset()
device_addr = ':'.join(re.findall('..', '%012x' % uuid.getnode()))
exceptions_queue = Queue.Queue()
# Starting a py-client in a separate thread
blehr_thread_obj = BleCentThread(dut, exceptions_queue)
blehr_thread_obj.start()
blehr_thread_obj.join()
# Get BLE client module
ble_client_obj = lib_ble_client.BLE_Bluez_Client(interface)
if not ble_client_obj:
raise RuntimeError('Get DBus-Bluez object failed !!')
exception_msg = None
while True:
try:
exception_msg = exceptions_queue.get(block=False)
except Queue.Empty:
break
else:
Utility.console_log('\n' + exception_msg)
# Discover Bluetooth Adapter and power on
is_adapter_set = ble_client_obj.set_adapter()
if not is_adapter_set:
raise RuntimeError('Adapter Power On failed !!')
# Write device address to dut
dut.expect('BLE Host Task Started', timeout=60)
dut.write(device_addr + '\n')
'''
Blecent application run:
Create GATT data
Register GATT Application
Create Advertising data
Register advertisement
Start advertising
'''
ble_client_obj.start_advertising(adv_host_name, adv_iface_index, adv_type, adv_uuid)
# Call disconnect to perform cleanup operations before exiting application
ble_client_obj.disconnect()
# Check dut responses
dut.expect('Connection established', timeout=60)
dut.expect('Service discovery complete; status=0', timeout=60)
print('Service discovery passed\n\tService Discovery Status: 0')
dut.expect('GATT procedure initiated: read;', timeout=60)
dut.expect('Read complete; status=0', timeout=60)
print('Read passed\n\tSupportedNewAlertCategoryCharacteristic\n\tRead Status: 0')
dut.expect('GATT procedure initiated: write;', timeout=60)
dut.expect('Write complete; status=0', timeout=60)
print('Write passed\n\tAlertNotificationControlPointCharacteristic\n\tWrite Status: 0')
dut.expect('GATT procedure initiated: write;', timeout=60)
dut.expect('Subscribe complete; status=0', timeout=60)
print('Subscribe passed\n\tClientCharacteristicConfigurationDescriptor\n\tSubscribe Status: 0')
if exception_msg:
raise Exception('Blecent thread did not run successfully')
if __name__ == '__main__':

View File

@@ -36,37 +36,35 @@ from tiny_test_fw import Utility
# > make print_flash_cmd | tail -n 1 > build/download.config
def blehr_client_task(hr_obj, dut_addr):
def blehr_client_task(hr_obj, dut, dut_addr):
interface = 'hci0'
ble_devname = 'blehr_sensor_1.0'
hr_srv_uuid = '180d'
hr_char_uuid = '2a37'
# Get BLE client module
ble_client_obj = lib_ble_client.BLE_Bluez_Client(interface, devname=ble_devname, devaddr=dut_addr)
if not ble_client_obj:
raise RuntimeError('Failed to get DBus-Bluez object')
ble_client_obj = lib_ble_client.BLE_Bluez_Client(iface=interface)
# Discover Bluetooth Adapter and power on
is_adapter_set = ble_client_obj.set_adapter()
if not is_adapter_set:
raise RuntimeError('Adapter Power On failed !!')
return
# Connect BLE Device
is_connected = ble_client_obj.connect()
is_connected = ble_client_obj.connect(
devname=ble_devname,
devaddr=dut_addr)
if not is_connected:
# Call disconnect to perform cleanup operations before exiting application
ble_client_obj.disconnect()
raise RuntimeError('Connection to device ' + str(ble_devname) + ' failed !!')
return
# Read Services
services_ret = ble_client_obj.get_services()
if services_ret:
Utility.console_log('\nServices\n')
Utility.console_log(str(services_ret))
else:
# Get services of the connected device
services = ble_client_obj.get_services()
if not services:
ble_client_obj.disconnect()
raise RuntimeError('Failure: Read Services failed')
return
# Get characteristics of the connected device
ble_client_obj.get_chars()
'''
Blehr application run:
@@ -74,26 +72,48 @@ def blehr_client_task(hr_obj, dut_addr):
Retrieve updated value
Stop Notifications
'''
blehr_ret = ble_client_obj.hr_update_simulation(hr_srv_uuid, hr_char_uuid)
if blehr_ret:
Utility.console_log('Success: blehr example test passed')
# Get service if exists
service = ble_client_obj.get_service_if_exists(hr_srv_uuid)
if service:
# Get characteristic if exists
char = ble_client_obj.get_char_if_exists(hr_char_uuid)
if not char:
ble_client_obj.disconnect()
return
else:
raise RuntimeError('Failure: blehr example test failed')
ble_client_obj.disconnect()
return
# Start Notify
# Read updated value
# Stop Notify
notify = ble_client_obj.start_notify(char)
if not notify:
ble_client_obj.disconnect()
return
# Check dut responses
dut.expect('subscribe event; cur_notify=1', timeout=30)
dut.expect('subscribe event; cur_notify=0', timeout=30)
# Call disconnect to perform cleanup operations before exiting application
ble_client_obj.disconnect()
# Check dut responses
dut.expect('disconnect;', timeout=30)
class BleHRThread(threading.Thread):
def __init__(self, dut_addr, exceptions_queue):
def __init__(self, dut, dut_addr, exceptions_queue):
threading.Thread.__init__(self)
self.dut = dut
self.dut_addr = dut_addr
self.exceptions_queue = exceptions_queue
def run(self):
try:
blehr_client_task(self, self.dut_addr)
except Exception:
blehr_client_task(self, self.dut, self.dut_addr)
except RuntimeError:
self.exceptions_queue.put(traceback.format_exc(), block=False)
@@ -107,8 +127,9 @@ def test_example_app_ble_hr(env, extra_data):
4. Updated value is retrieved
5. Stop Notifications
"""
subprocess.check_output(['rm','-rf','/var/lib/bluetooth/*'])
subprocess.check_output(['hciconfig','hci0','reset'])
# Remove cached bluetooth devices of any previous connections
subprocess.check_output(['rm', '-rf', '/var/lib/bluetooth/*'])
subprocess.check_output(['hciconfig', 'hci0', 'reset'])
# Acquire DUT
dut = env.get_dut('blehr', 'examples/bluetooth/nimble/blehr', dut_class=ttfw_idf.ESP32DUT)
@@ -127,7 +148,7 @@ def test_example_app_ble_hr(env, extra_data):
dut_addr = dut.expect(re.compile(r'Device Address: ([a-fA-F0-9:]+)'), timeout=30)[0]
exceptions_queue = Queue.Queue()
# Starting a py-client in a separate thread
blehr_thread_obj = BleHRThread(dut_addr, exceptions_queue)
blehr_thread_obj = BleHRThread(dut, dut_addr, exceptions_queue)
blehr_thread_obj.start()
blehr_thread_obj.join()
@@ -141,12 +162,7 @@ def test_example_app_ble_hr(env, extra_data):
Utility.console_log('\n' + exception_msg)
if exception_msg:
raise Exception('Thread did not run successfully')
# Check dut responses
dut.expect('subscribe event; cur_notify=1', timeout=30)
dut.expect('subscribe event; cur_notify=0', timeout=30)
dut.expect('disconnect;', timeout=30)
raise Exception('Blehr thread did not run successfully')
if __name__ == '__main__':

View File

@@ -37,73 +37,57 @@ from tiny_test_fw import Utility
# > export TEST_FW_PATH=~/esp/esp-idf/tools/tiny-test-fw
def bleprph_client_task(prph_obj, dut, dut_addr):
def bleprph_client_task(dut, dut_addr):
interface = 'hci0'
ble_devname = 'nimble-bleprph'
srv_uuid = '2f12'
write_value = b'A'
# Get BLE client module
ble_client_obj = lib_ble_client.BLE_Bluez_Client(interface, devname=ble_devname, devaddr=dut_addr)
if not ble_client_obj:
raise RuntimeError('Failed to get DBus-Bluez object')
ble_client_obj = lib_ble_client.BLE_Bluez_Client(iface=interface)
# Discover Bluetooth Adapter and power on
is_adapter_set = ble_client_obj.set_adapter()
if not is_adapter_set:
raise RuntimeError('Adapter Power On failed !!')
return
# Connect BLE Device
is_connected = ble_client_obj.connect()
is_connected = ble_client_obj.connect(
devname=ble_devname,
devaddr=dut_addr)
if not is_connected:
# Call disconnect to perform cleanup operations before exiting application
return
# Get services of the connected device
services = ble_client_obj.get_services()
if not services:
ble_client_obj.disconnect()
raise RuntimeError('Connection to device ' + ble_devname + ' failed !!')
return
# Verify service uuid exists
service_exists = ble_client_obj.get_service_if_exists(srv_uuid)
if not service_exists:
ble_client_obj.disconnect()
return
# Get characteristics of the connected device
ble_client_obj.get_chars()
# Read properties of characteristics (uuid, value and permissions)
ble_client_obj.read_chars()
# Write new value to characteristic having read and write permission
# and display updated value
write_char = ble_client_obj.write_chars(write_value)
if not write_char:
ble_client_obj.disconnect()
return
# Disconnect device
ble_client_obj.disconnect()
# Check dut responses
dut.expect('GAP procedure initiated: advertise;', timeout=30)
# Read Services
services_ret = ble_client_obj.get_services(srv_uuid)
if services_ret:
Utility.console_log('\nServices\n')
Utility.console_log(str(services_ret))
else:
ble_client_obj.disconnect()
raise RuntimeError('Failure: Read Services failed')
# Read Characteristics
chars_ret = {}
chars_ret = ble_client_obj.read_chars()
if chars_ret:
Utility.console_log('\nCharacteristics retrieved')
for path, props in chars_ret.items():
Utility.console_log('\n\tCharacteristic: ' + str(path))
Utility.console_log('\tCharacteristic UUID: ' + str(props[2]))
Utility.console_log('\tValue: ' + str(props[0]))
Utility.console_log('\tProperties: : ' + str(props[1]))
else:
ble_client_obj.disconnect()
raise RuntimeError('Failure: Read Characteristics failed')
'''
Write Characteristics
- write 'A' to characteristic with write permission
'''
chars_ret_on_write = {}
chars_ret_on_write = ble_client_obj.write_chars(b'A')
if chars_ret_on_write:
Utility.console_log('\nCharacteristics after write operation')
for path, props in chars_ret_on_write.items():
Utility.console_log('\n\tCharacteristic:' + str(path))
Utility.console_log('\tCharacteristic UUID: ' + str(props[2]))
Utility.console_log('\tValue:' + str(props[0]))
Utility.console_log('\tProperties: : ' + str(props[1]))
else:
ble_client_obj.disconnect()
raise RuntimeError('Failure: Write Characteristics failed')
# Call disconnect to perform cleanup operations before exiting application
ble_client_obj.disconnect()
dut.expect('connection established; status=0', timeout=30)
dut.expect('disconnect;', timeout=30)
class BlePrphThread(threading.Thread):
@@ -115,8 +99,8 @@ class BlePrphThread(threading.Thread):
def run(self):
try:
bleprph_client_task(self, self.dut, self.dut_addr)
except Exception:
bleprph_client_task(self.dut, self.dut_addr)
except RuntimeError:
self.exceptions_queue.put(traceback.format_exc(), block=False)
@@ -130,8 +114,9 @@ def test_example_app_ble_peripheral(env, extra_data):
4. Read Characteristics
5. Write Characteristics
"""
subprocess.check_output(['rm','-rf','/var/lib/bluetooth/*'])
subprocess.check_output(['hciconfig','hci0','reset'])
# Remove cached bluetooth devices of any previous connections
subprocess.check_output(['rm', '-rf', '/var/lib/bluetooth/*'])
subprocess.check_output(['hciconfig', 'hci0', 'reset'])
# Acquire DUT
dut = env.get_dut('bleprph', 'examples/bluetooth/nimble/bleprph', dut_class=ttfw_idf.ESP32DUT)
@@ -149,8 +134,11 @@ def test_example_app_ble_peripheral(env, extra_data):
# Get device address from dut
dut_addr = dut.expect(re.compile(r'Device Address: ([a-fA-F0-9:]+)'), timeout=30)[0]
exceptions_queue = Queue.Queue()
# Check dut responses
dut.expect('GAP procedure initiated: advertise;', timeout=30)
# Starting a py-client in a separate thread
exceptions_queue = Queue.Queue()
bleprph_thread_obj = BlePrphThread(dut, dut_addr, exceptions_queue)
bleprph_thread_obj.start()
bleprph_thread_obj.join()
@@ -165,11 +153,7 @@ def test_example_app_ble_peripheral(env, extra_data):
Utility.console_log('\n' + exception_msg)
if exception_msg:
raise Exception('Thread did not run successfully')
# Check dut responses
dut.expect('connection established; status=0', timeout=30)
dut.expect('disconnect;', timeout=30)
raise Exception('BlePrph thread did not run successfully')
if __name__ == '__main__':