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

Commit d6bc76c9 authored by Aritra Sen's avatar Aritra Sen Committed by Gerrit Code Review
Browse files

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

parents 2d639a46 e3bb07b9
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