1#!/usr/bin/env python3
2#
3#   Copyright 2020 - The Android Open Source Project
4#
5#   Licensed under the Apache License, Version 2.0 (the "License");
6#   you may not use this file except in compliance with the License.
7#   You may obtain a copy of the License at
8#
9#       http://www.apache.org/licenses/LICENSE-2.0
10#
11#   Unless required by applicable law or agreed to in writing, software
12#   distributed under the License is distributed on an "AS IS" BASIS,
13#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14#   See the License for the specific language governing permissions and
15#   limitations under the License.
16
17from google.protobuf import empty_pb2 as empty_proto
18from cert.event_stream import EventStream
19from cert.closable import Closable
20from cert.closable import safeClose
21from hal import facade_pb2 as hal_facade
22
23
24class PyHal(Closable):
25
26    def __init__(self, device):
27        self.device = device
28
29        self.hci_event_stream = EventStream(self.device.hal.FetchHciEvent(empty_proto.Empty()))
30        self.acl_stream = EventStream(self.device.hal.FetchHciAcl(empty_proto.Empty()))
31
32        # We don't deal with SCO for now
33
34    def close(self):
35        safeClose(self.hci_event_stream)
36        safeClose(self.acl_stream)
37
38    def get_hci_event_stream(self):
39        return self.hci_event_stream
40
41    def get_acl_stream(self):
42        return self.acl_stream
43
44    def send_hci_command(self, command):
45        self.device.hal.SendHciCommand(hal_facade.HciCommandPacket(payload=bytes(command)))
46
47    def send_acl(self, acl):
48        self.device.hal.SendHciAcl(hal_facade.HciAclPacket(payload=bytes(acl)))
49