# 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. """HID grpc interface.""" import asyncio import logging from floss.pandora.floss import floss_enums from floss.pandora.floss import qa_client from floss.pandora.floss import utils from floss.pandora.server import bluetooth as bluetooth_module from google.protobuf import empty_pb2 import grpc from pandora_experimental import hid_grpc_aio from pandora_experimental import hid_pb2 class HIDService(hid_grpc_aio.HIDServicer): """Service to trigger Bluetooth HID procedures. This class implements the Pandora bluetooth test interfaces, where the meta class definition is automatically generated by the protobuf. The interface definition can be found in: https://cs.android.com/android/platform/superproject/+/main:packages/modules/Bluetooth/pandora/interfaces/pandora_experimental/hid.proto """ def __init__(self, bluetooth: bluetooth_module.Bluetooth): self.bluetooth = bluetooth async def SendHostReport(self, request: hid_pb2.SendHostReportRequest, context: grpc.ServicerContext) -> hid_pb2.SendHostReportResponse: class HIDReportObserver(qa_client.BluetoothQACallbacks): """Observer to observe the set HID report state.""" def __init__(self, task): self.task = task @utils.glib_callback() def on_set_hid_report_completed(self, status): if floss_enums.BtStatus(status) != floss_enums.BtStatus.SUCCESS: logging.error('Failed to set HID report. Status: %s', status) future = self.task['set_hid_report'] future.get_loop().call_soon_threadsafe(future.set_result, None) if not request.address: await context.abort(grpc.StatusCode.INVALID_ARGUMENT, 'Request address field must be set.') address = utils.address_from(request.address) try: set_hid_report = asyncio.get_running_loop().create_future() observer = HIDReportObserver({'set_hid_report': set_hid_report}) name = utils.create_observer_name(observer) self.bluetooth.qa_client.register_callback_observer(name, observer) if request.report_type not in iter(floss_enums.BthhReportType): await context.abort(grpc.StatusCode.INVALID_ARGUMENT, 'Invalid report type.') if not self.bluetooth.set_hid_report(address, request.report_type, request.report): await context.abort(grpc.StatusCode.UNKNOWN, 'Failed to call set_hid_report.') await asyncio.wait_for(set_hid_report, timeout=5) finally: self.bluetooth.qa_client.unregister_callback_observer(name, observer) return empty_pb2.Empty()