Skip to content

Commit

Permalink
add zapper-enabled bt test (#419)
Browse files Browse the repository at this point in the history
Add Zapper-enabled BT A2DP test
  • Loading branch information
kissiel authored Apr 9, 2023
1 parent 3fdef1f commit e8f32ac
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 0 deletions.
68 changes: 68 additions & 0 deletions providers/base/bin/bt_a2dp.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#!/usr/bin/env python3
# Copyright 2023 Canonical Ltd.
# All rights reserved.
#
# Written by:
# Authors: Maciej Kisielewski <maciej.kisielewski@canonical.com>
"""
This program checks if the host can connect to a Zapper using Bluetooth.
"""
import sys
import time

from contextlib import contextmanager

import bluetooth

from checkbox_support.scripts.zapper_proxy import zapper_run # noqa: E402


@contextmanager
def zapper_as_a_speaker(host):
"""Ensure that the service is turned off after using it."""
addr = zapper_run(host, "bluetooth_start", "a2dp")
try:
yield addr
finally:
zapper_run(host, "bluetooth_stop")


def main():
"""Entry point to the test."""
if len(sys.argv) != 2:
raise SystemExit("Usage: {} ZAPPER_HOST".format(sys.argv[0]))
print("Asking Zapper to become a Bluetooth speaker")
with zapper_as_a_speaker(sys.argv[1]) as zapper_addr:
print("Zapper Bluetooth address is {}".format(zapper_addr))
retry_count = 5
for i in range(1, retry_count + 1):
print("Discovering Bluetooth devices (try {}/{})".format(
i, retry_count))
devices = bluetooth.discover_devices()
print("Devices found: {}".format(devices))
if zapper_addr in devices:
break
print("Zapper not found in the device list... Retrying")
else:
raise SystemExit("Zapper not found")

for i in range(1, retry_count + 1):
print("Trying to connect to Zapper (try {}/{})".format(
i, retry_count))
socket = bluetooth.BluetoothSocket(bluetooth.L2CAP)
try:
socket.connect((zapper_addr, 25)) # 25 - A2DP port
# the sleep below is there to be able to see the icon change
# when observing the test with human eyes
# change it to a longer delay to enable easy debugging
time.sleep(1)
socket.close()
break
except bluetooth.btcommon.BluetoothError as exc:
print("Failed to connect. {}".format(exc))
else:
raise SystemExit("Failed to connect to Zapper via BT")


if __name__ == "__main__":
raise SystemExit(main())
10 changes: 10 additions & 0 deletions providers/base/units/zapper/jobs.pxu
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,13 @@ command:
removable_storage_watcher.py --unmounted -m 500000000 --zapper-usb-address={{ port_alias }} remove usb
{% endif -%}
estimated_duration: 20

id: bluetooth/zapper-a2dp
requires: zapper_capabilities.capability == 'bluetooth' and 'a2dp' in zapper_capabilities.profiles
category_id: com.canonical.plainbox::bluetooth
plugin: shell
estimated_duration: 60
_summary: Check if the system can connect to a Bluetooth speaker using A2DP profile
environ: ZAPPER_HOST
command: bt_a2dp.py "$ZAPPER_HOST"
depends: bluetooth/detect-output
5 changes: 5 additions & 0 deletions providers/base/units/zapper/packaging.pxu
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# This is for Zapper-enabled Bluetooth tests
unit: packaging meta-data
os-id: debian
Depends: python3-bluez

1 change: 1 addition & 0 deletions providers/base/units/zapper/test-plan.pxu
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ unit: test plan
_name: Tests using Zapper
_description: Tests using Zapper
include:
bluetooth/zapper-a2dp
monitor/zapper-edid
monitor/1_zapper_hdmi_.*
usb/zapper-usb-insert-.*
Expand Down

0 comments on commit e8f32ac

Please sign in to comment.