2019-07-04 20:14:04 -04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# Copyright 2019 Espressif Systems (Shanghai) PTE LTD
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
from __future__ import print_function
|
2021-01-25 21:49:01 -05:00
|
|
|
|
2019-07-04 20:14:04 -04:00
|
|
|
import os
|
|
|
|
import re
|
2021-01-25 21:49:01 -05:00
|
|
|
import subprocess
|
2019-07-24 05:27:22 -04:00
|
|
|
import threading
|
|
|
|
import traceback
|
2019-07-04 20:14:04 -04:00
|
|
|
|
2020-09-07 00:01:27 -04:00
|
|
|
try:
|
|
|
|
import Queue
|
|
|
|
except ImportError:
|
|
|
|
import queue as Queue
|
|
|
|
|
2019-11-26 22:58:07 -05:00
|
|
|
import ttfw_idf
|
|
|
|
from ble import lib_ble_client
|
2021-01-25 21:49:01 -05:00
|
|
|
from tiny_test_fw import Utility
|
2019-07-04 20:14:04 -04:00
|
|
|
|
|
|
|
# When running on local machine execute the following before running this script
|
|
|
|
# > make app bootloader
|
|
|
|
# > make print_flash_cmd | tail -n 1 > build/download.config
|
|
|
|
|
|
|
|
|
2021-04-07 07:43:02 -04:00
|
|
|
def blehr_client_task(hr_obj, dut, dut_addr):
|
2019-07-04 20:14:04 -04:00
|
|
|
interface = 'hci0'
|
|
|
|
ble_devname = 'blehr_sensor_1.0'
|
|
|
|
hr_srv_uuid = '180d'
|
|
|
|
hr_char_uuid = '2a37'
|
|
|
|
|
|
|
|
# Get BLE client module
|
2021-04-07 07:43:02 -04:00
|
|
|
ble_client_obj = lib_ble_client.BLE_Bluez_Client(iface=interface)
|
2019-07-04 20:14:04 -04:00
|
|
|
|
|
|
|
# Discover Bluetooth Adapter and power on
|
|
|
|
is_adapter_set = ble_client_obj.set_adapter()
|
|
|
|
if not is_adapter_set:
|
2021-04-07 07:43:02 -04:00
|
|
|
return
|
2019-07-04 20:14:04 -04:00
|
|
|
|
|
|
|
# Connect BLE Device
|
2021-04-07 07:43:02 -04:00
|
|
|
is_connected = ble_client_obj.connect(
|
|
|
|
devname=ble_devname,
|
|
|
|
devaddr=dut_addr)
|
2019-07-04 20:14:04 -04:00
|
|
|
if not is_connected:
|
2021-04-07 07:43:02 -04:00
|
|
|
return
|
2019-07-04 20:14:04 -04:00
|
|
|
|
2021-04-07 07:43:02 -04:00
|
|
|
# Get services of the connected device
|
|
|
|
services = ble_client_obj.get_services()
|
|
|
|
if not services:
|
2019-07-04 20:14:04 -04:00
|
|
|
ble_client_obj.disconnect()
|
2021-04-07 07:43:02 -04:00
|
|
|
return
|
|
|
|
|
|
|
|
# Get characteristics of the connected device
|
|
|
|
ble_client_obj.get_chars()
|
2019-07-04 20:14:04 -04:00
|
|
|
|
|
|
|
'''
|
|
|
|
Blehr application run:
|
|
|
|
Start Notifications
|
|
|
|
Retrieve updated value
|
|
|
|
Stop Notifications
|
|
|
|
'''
|
2021-04-07 07:43:02 -04:00
|
|
|
# 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
|
2019-07-04 20:14:04 -04:00
|
|
|
else:
|
2021-04-07 07:43:02 -04:00
|
|
|
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)
|
2019-07-04 20:14:04 -04:00
|
|
|
|
|
|
|
# Call disconnect to perform cleanup operations before exiting application
|
|
|
|
ble_client_obj.disconnect()
|
|
|
|
|
2021-04-07 07:43:02 -04:00
|
|
|
# Check dut responses
|
|
|
|
dut.expect('disconnect;', timeout=30)
|
|
|
|
|
2019-07-04 20:14:04 -04:00
|
|
|
|
2019-07-24 05:27:22 -04:00
|
|
|
class BleHRThread(threading.Thread):
|
2021-04-07 07:43:02 -04:00
|
|
|
def __init__(self, dut, dut_addr, exceptions_queue):
|
2019-07-24 05:27:22 -04:00
|
|
|
threading.Thread.__init__(self)
|
2021-04-07 07:43:02 -04:00
|
|
|
self.dut = dut
|
2019-07-24 05:27:22 -04:00
|
|
|
self.dut_addr = dut_addr
|
|
|
|
self.exceptions_queue = exceptions_queue
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
try:
|
2021-04-07 07:43:02 -04:00
|
|
|
blehr_client_task(self, self.dut, self.dut_addr)
|
|
|
|
except RuntimeError:
|
2019-07-24 05:27:22 -04:00
|
|
|
self.exceptions_queue.put(traceback.format_exc(), block=False)
|
|
|
|
|
|
|
|
|
2021-01-25 21:49:01 -05:00
|
|
|
@ttfw_idf.idf_example_test(env_tag='Example_WIFI_BT')
|
2019-07-04 20:14:04 -04:00
|
|
|
def test_example_app_ble_hr(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
|
|
|
|
"""
|
2021-04-07 07:43:02 -04:00
|
|
|
# Remove cached bluetooth devices of any previous connections
|
|
|
|
subprocess.check_output(['rm', '-rf', '/var/lib/bluetooth/*'])
|
|
|
|
subprocess.check_output(['hciconfig', 'hci0', 'reset'])
|
2019-07-24 05:27:22 -04:00
|
|
|
|
|
|
|
# Acquire DUT
|
2021-01-25 21:49:01 -05:00
|
|
|
dut = env.get_dut('blehr', 'examples/bluetooth/nimble/blehr', dut_class=ttfw_idf.ESP32DUT)
|
2019-07-24 05:27:22 -04:00
|
|
|
|
|
|
|
# Get binary file
|
2021-01-25 21:49:01 -05:00
|
|
|
binary_file = os.path.join(dut.app.binary_path, 'blehr.bin')
|
2019-07-24 05:27:22 -04:00
|
|
|
bin_size = os.path.getsize(binary_file)
|
2021-01-25 21:49:01 -05:00
|
|
|
ttfw_idf.log_performance('blehr_bin_size', '{}KB'.format(bin_size // 1024))
|
2019-07-24 05:27:22 -04:00
|
|
|
|
|
|
|
# Upload binary and start testing
|
2021-01-25 21:49:01 -05:00
|
|
|
Utility.console_log('Starting blehr simple example test app')
|
2019-07-24 05:27:22 -04:00
|
|
|
dut.start_app()
|
|
|
|
dut.reset()
|
|
|
|
|
|
|
|
# Get device address from dut
|
2021-01-25 21:49:01 -05:00
|
|
|
dut_addr = dut.expect(re.compile(r'Device Address: ([a-fA-F0-9:]+)'), timeout=30)[0]
|
2019-07-24 05:27:22 -04:00
|
|
|
exceptions_queue = Queue.Queue()
|
|
|
|
# Starting a py-client in a separate thread
|
2021-04-07 07:43:02 -04:00
|
|
|
blehr_thread_obj = BleHRThread(dut, dut_addr, exceptions_queue)
|
2019-07-24 05:27:22 -04:00
|
|
|
blehr_thread_obj.start()
|
|
|
|
blehr_thread_obj.join()
|
|
|
|
|
|
|
|
exception_msg = None
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
exception_msg = exceptions_queue.get(block=False)
|
|
|
|
except Queue.Empty:
|
|
|
|
break
|
|
|
|
else:
|
2021-01-25 21:49:01 -05:00
|
|
|
Utility.console_log('\n' + exception_msg)
|
2019-07-24 05:27:22 -04:00
|
|
|
|
|
|
|
if exception_msg:
|
2021-04-07 07:43:02 -04:00
|
|
|
raise Exception('Blehr thread did not run successfully')
|
2019-07-04 20:14:04 -04:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
test_example_app_ble_hr()
|