# Copyright 2024 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. """HFP grpc interface.""" import math 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 hfp_grpc_aio from pandora_experimental import hfp_pb2 class HFPService(hfp_grpc_aio.HFPServicer): """Service to trigger Bluetooth HFP 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/+/main:packages/modules/Bluetooth/pandora/interfaces/pandora_experimental/hfp.proto """ def __init__(self, bluetooth: bluetooth_module.Bluetooth): self.bluetooth = bluetooth def enable_phone_for_testing(self): self.bluetooth.set_phone_ops_enabled(True) self.bluetooth.set_mps_qualification_enabled(True) async def EnableSlc(self, request: hfp_pb2.EnableSlcRequest, context: grpc.ServicerContext) -> empty_pb2.Empty: self.enable_phone_for_testing() address = utils.connection_from(request.connection).address self.bluetooth.connect_device(address) return empty_pb2.Empty() async def EnableSlcAsHandsfree(self, request: hfp_pb2.EnableSlcAsHandsfreeRequest, context: grpc.ServicerContext) -> empty_pb2.Empty: context.set_code(grpc.StatusCode.UNIMPLEMENTED) # type: ignore context.set_details("Method not implemented!") # type: ignore raise NotImplementedError("Method not implemented!") async def DisableSlc(self, request: hfp_pb2.DisableSlcRequest, context: grpc.ServicerContext) -> empty_pb2.Empty: address = utils.connection_from(request.connection).address self.bluetooth.disconnect_device(address) return empty_pb2.Empty() async def DisableSlcAsHandsfree(self, request: hfp_pb2.DisableSlcAsHandsfreeRequest, context: grpc.ServicerContext) -> empty_pb2.Empty: context.set_code(grpc.StatusCode.UNIMPLEMENTED) # type: ignore context.set_details("Method not implemented!") # type: ignore raise NotImplementedError("Method not implemented!") async def DeclineCall(self, request: hfp_pb2.DeclineCallRequest, context: grpc.ServicerContext) -> hfp_pb2.DeclineCallResponse: self.enable_phone_for_testing() self.bluetooth.hangup_call() return hfp_pb2.DeclineCallResponse() async def DeclineCallAsHandsfree(self, request: hfp_pb2.DeclineCallAsHandsfreeRequest, context: grpc.ServicerContext) -> hfp_pb2.DeclineCallAsHandsfreeResponse: context.set_code(grpc.StatusCode.UNIMPLEMENTED) # type: ignore context.set_details("Method not implemented!") # type: ignore raise NotImplementedError("Method not implemented!") async def SetBatteryLevel(self, request: hfp_pb2.SetBatteryLevelRequest, context: grpc.ServicerContext) -> (empty_pb2.Empty): self.enable_phone_for_testing() if request.battery_percentage > 100 or request.battery_percentage < 0: await context.abort(grpc.StatusCode.INVALID_ARGUMENT, 'Wrong battery percentage.') self.bluetooth.set_battery_level(math.floor((request.battery_percentage / 100) * 5)) return empty_pb2.Empty() async def ConnectToAudioAsHandsfree(self, request: hfp_pb2.ConnectToAudioAsHandsfreeRequest, context: grpc.ServicerContext) -> hfp_pb2.ConnectToAudioAsHandsfreeResponse: context.set_code(grpc.StatusCode.UNIMPLEMENTED) # type: ignore context.set_details("Method not implemented!") # type: ignore raise NotImplementedError("Method not implemented!") async def DisconnectFromAudioAsHandsfree( self, request: hfp_pb2.DisconnectFromAudioAsHandsfreeRequest, context: grpc.ServicerContext) -> hfp_pb2.DisconnectFromAudioAsHandsfreeResponse: context.set_code(grpc.StatusCode.UNIMPLEMENTED) # type: ignore context.set_details("Method not implemented!") # type: ignore raise NotImplementedError("Method not implemented!") async def SetVoiceRecognition(self, request: hfp_pb2.SetVoiceRecognitionRequest, context: grpc.ServicerContext) -> hfp_pb2.SetVoiceRecognitionResponse: context.set_code(grpc.StatusCode.UNIMPLEMENTED) # type: ignore context.set_details("Method not implemented!") # type: ignore raise NotImplementedError("Method not implemented!") async def SetVoiceRecognitionAsHandsfree( self, request: hfp_pb2.SetVoiceRecognitionAsHandsfreeRequest, context: grpc.ServicerContext) -> hfp_pb2.SetVoiceRecognitionAsHandsfreeResponse: context.set_code(grpc.StatusCode.UNIMPLEMENTED) # type: ignore context.set_details("Method not implemented!") # type: ignore raise NotImplementedError("Method not implemented!") async def MakeCall(self, request: hfp_pb2.MakeCallRequest, context: grpc.ServicerContext) -> hfp_pb2.MakeCallResponse: self.enable_phone_for_testing() number = request.number if number is None or len(number) == 0: await context.abort(grpc.StatusCode.INVALID_ARGUMENT, 'Cannot call empty number.') call_result = self.bluetooth.dial_call(number) if call_result is None or not call_result: await context.abort(grpc.StatusCode.INTERNAL, 'Failed to make a call.') return hfp_pb2.MakeCallResponse() async def MakeCallAsHandsfree(self, request: hfp_pb2.MakeCallAsHandsfreeRequest, context: grpc.ServicerContext) -> hfp_pb2.MakeCallAsHandsfreeResponse: context.set_code(grpc.StatusCode.UNIMPLEMENTED) # type: ignore context.set_details('Method not implemented!') # type: ignore raise NotImplementedError('Method not implemented!') async def AnswerCall(self, request: hfp_pb2.AnswerCallRequest, context: grpc.ServicerContext) -> hfp_pb2.AnswerCallResponse: self.enable_phone_for_testing() answer_result = self.bluetooth.answer_call() if answer_result is None or not answer_result: await context.abort(grpc.StatusCode.INTERNAL, 'Failed to answer call.') return hfp_pb2.AnswerCallResponse() async def AnswerCallAsHandsfree(self, request: hfp_pb2.AnswerCallAsHandsfreeRequest, context: grpc.ServicerContext) -> hfp_pb2.AnswerCallAsHandsfreeResponse: context.set_code(grpc.StatusCode.UNIMPLEMENTED) # type: ignore context.set_details('Method not implemented!') # type: ignore raise NotImplementedError('Method not implemented!') async def SetAudioPath(self, request: hfp_pb2.SetAudioPathRequest, context: grpc.ServicerContext) -> hfp_pb2.SetAudioPathResponse: self.enable_phone_for_testing() connected_devices = self.bluetooth.get_connected_audio_devices() if len(connected_devices) == 0: await context.abort(grpc.StatusCode.INTERNAL, 'No connected devices.') if request.audio_path == hfp_pb2.AUDIO_PATH_SPEAKERS: self.bluetooth.audio_disconnect(connected_devices[0]) elif request.audio_path == hfp_pb2.AUDIO_PATH_HANDSFREE: self.bluetooth.audio_connect(connected_devices[0]) return hfp_pb2.SetAudioPathResponse() async def SwapActiveCall(self, request: hfp_pb2.SwapActiveCallRequest, context: grpc.ServicerContext) -> hfp_pb2.SwapActiveCallResponse: self.enable_phone_for_testing() swap_result = self.bluetooth.swap_active_call() if swap_result is None or not swap_result: await context.abort(grpc.StatusCode.INTERNAL, 'Failed to swap active call.') return hfp_pb2.SwapActiveCallResponse() async def SetInBandRingtone(self, request: hfp_pb2.SetInBandRingtoneRequest, context: grpc.ServicerContext) -> hfp_pb2.SetInBandRingtoneResponse: context.set_code(grpc.StatusCode.UNIMPLEMENTED) # type: ignore context.set_details('Method not implemented!') # type: ignore raise NotImplementedError('Method not implemented!') async def ClearCallHistory(self, request: hfp_pb2.ClearCallHistoryRequest, context: grpc.ServicerContext) -> hfp_pb2.ClearCallHistoryResponse: self.enable_phone_for_testing() if not all((self.bluetooth.set_memory_call(), self.bluetooth.set_last_call())): await context.abort(grpc.StatusCode.INTERNAL, 'Failed to clear hall history.') return hfp_pb2.ClearCallHistoryResponse() async def EndCallAsHandsfree(self, request: hfp_pb2.EndCallAsHandsfreeRequest, context: grpc.ServicerContext) -> hfp_pb2.EndCallAsHandsfreeResponse: context.set_code(grpc.StatusCode.UNIMPLEMENTED) # type: ignore context.set_details('Method not implemented!') # type: ignore raise NotImplementedError('Method not implemented!') async def CallTransferAsHandsfree(self, request: hfp_pb2.CallTransferAsHandsfreeRequest, context: grpc.ServicerContext) -> hfp_pb2.CallTransferAsHandsfreeResponse: context.set_code(grpc.StatusCode.UNIMPLEMENTED) # type: ignore context.set_details('Method not implemented!') # type: ignore raise NotImplementedError('Method not implemented!') async def SendDtmfFromHandsfree(self, request: hfp_pb2.SendDtmfFromHandsfreeRequest, context: grpc.ServicerContext) -> hfp_pb2.SendDtmfFromHandsfreeResponse: context.set_code(grpc.StatusCode.UNIMPLEMENTED) # type: ignore context.set_details('Method not implemented!') # type: ignore raise NotImplementedError('Method not implemented!')