Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit e3bb07b9 authored by Aritra Sen's avatar Aritra Sen
Browse files

Add support to the HFP client to extract callback response using gRPC streams.

Bug: 261221648
Test: mma -j $(nproc)
Test: ./build.py
Test: system/gd/cert/run --clean --topshim
Tag: #floss
Change-Id: Iec78b07f31348256fb75be032cb293e6b7320fbe
parent a474d898
Loading
Loading
Loading
Loading
+28 −4
Original line number Diff line number Diff line
@@ -33,10 +33,12 @@ class HfpClient(AsyncClosable):
    __task_list = []
    __channel = None
    __hfp_stub = None
    __hfp_event_stream = None

    def __init__(self, port=8999):
        self.__channel = grpc.aio.insecure_channel("localhost:%d" % port)
        self.__hfp_stub = facade_pb2_grpc.HfpServiceStub(self.__channel)
        self.__hfp_event_stream = self.__hfp_stub.FetchEvents(facade_pb2.FetchEventsRequest())

    async def close(self):
        """
@@ -64,8 +66,7 @@ class HfpClient(AsyncClosable):
        """
        """
        await self.__hfp_stub.ConnectAudio(
            facade_pb2.ConnectAudioRequest(
                connection=facade_pb2.Connection(cookie=address.encode()),
            facade_pb2.ConnectAudioRequest(connection=facade_pb2.Connection(cookie=address.encode()),
                                           is_sco_offload_enabled=is_sco_offload_enabled,
                                           force_cvsd=force_cvsd))

@@ -80,3 +81,26 @@ class HfpClient(AsyncClosable):
        """
        await self.__hfp_stub.DisconnectAudio(
            facade_pb2.DisconnectAudioRequest(connection=facade_pb2.Connection(cookie=address.encode()), volume=volume))

    async def __get_next_event(self, event, future):
        """Get the future of next event from the stream"""
        while True:
            e = await self.__hfp_event_stream.read()

            # Match event by some condition.
            if e.event_type == event:
                future.set_result(e.data)
                break
            else:
                print("Got '%s'; expecting '%s'" % (e.event_type, event))
                print(e)

    async def _listen_for_event(self, event):
        """Start fetching events"""
        future = asyncio.get_running_loop().create_future()
        self.__task_list.append(asyncio.get_running_loop().create_task(self.__get_next_event(event, future)))
        try:
            await asyncio.wait_for(future, HfpClient.DEFAULT_TIMEOUT)
        except:
            print("Failed to get event", event)
        return future