aboutsummaryrefslogtreecommitdiff
path: root/avatar
diff options
context:
space:
mode:
authoruael <uael@google.com>2023-09-27 18:26:02 +0000
committerLucas Abel <22837557+uael@users.noreply.github.com>2023-09-29 18:04:22 +0200
commit9942a06cdd15c558b668419e0d5be57cb6aa4f5b (patch)
treec4baf8c40619c7afe9946410f72249839d14d764 /avatar
parentc12fb2bd67f19169b29d102c3fa750229b001300 (diff)
downloadavatar-9942a06cdd15c558b668419e0d5be57cb6aa4f5b.tar.gz
cases: move common test routines to avatar
Diffstat (limited to 'avatar')
-rw-r--r--avatar/pandora.py67
1 files changed, 67 insertions, 0 deletions
diff --git a/avatar/pandora.py b/avatar/pandora.py
new file mode 100644
index 0000000..31695ee
--- /dev/null
+++ b/avatar/pandora.py
@@ -0,0 +1,67 @@
+# Copyright 2023 Google LLC
+#
+# 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
+#
+# https://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.
+
+import asyncio
+
+from avatar import BumblePandoraDevice
+from avatar import PandoraDevice
+from bumble.device import Connection as BumbleConnection
+from mobly.asserts import assert_equal # type: ignore
+from mobly.asserts import assert_is_not_none # type: ignore
+from pandora._utils import AioStream
+from pandora.host_pb2 import AdvertiseResponse
+from pandora.host_pb2 import Connection
+from pandora.host_pb2 import OwnAddressType
+from pandora.host_pb2 import ScanningResponse
+from typing import Optional, Tuple
+
+
+def get_raw_connection_handle(device: PandoraDevice, connection: Connection) -> int:
+ assert isinstance(device, BumblePandoraDevice)
+ return int.from_bytes(connection.cookie.value, 'big')
+
+
+def get_raw_connection(device: PandoraDevice, connection: Connection) -> Optional[BumbleConnection]:
+ assert isinstance(device, BumblePandoraDevice)
+ return device.device.lookup_connection(get_raw_connection_handle(device, connection))
+
+
+async def connect(initiator: PandoraDevice, acceptor: PandoraDevice) -> Tuple[Connection, Connection]:
+ init_res, wait_res = await asyncio.gather(
+ initiator.aio.host.Connect(address=acceptor.address),
+ acceptor.aio.host.WaitConnection(address=initiator.address),
+ )
+ assert_equal(init_res.result_variant(), 'connection')
+ assert_equal(wait_res.result_variant(), 'connection')
+ assert init_res.connection is not None and wait_res.connection is not None
+ return init_res.connection, wait_res.connection
+
+
+async def connect_le(
+ initiator: PandoraDevice,
+ acceptor: AioStream[AdvertiseResponse],
+ scan: ScanningResponse,
+ own_address_type: OwnAddressType,
+ cancel_advertisement: bool = True,
+) -> Tuple[Connection, Connection]:
+ (init_res, wait_res) = await asyncio.gather(
+ initiator.aio.host.ConnectLE(own_address_type=own_address_type, **scan.address_asdict()),
+ anext(aiter(acceptor)), # pytype: disable=name-error
+ )
+ if cancel_advertisement:
+ acceptor.cancel()
+ assert_equal(init_res.result_variant(), 'connection')
+ assert_is_not_none(init_res.connection)
+ assert init_res.connection
+ return init_res.connection, wait_res.connection