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 blueberry.tests.gd.cert.capture import Capture
18from blueberry.tests.gd.cert.matchers import HciMatchers
19from blueberry.tests.gd.cert.matchers import SecurityMatchers
20import hci_packets as hci
21
22
23class HalCaptures(object):
24
25    @staticmethod
26    def ReadBdAddrCompleteCapture():
27        return Capture(lambda packet: packet.payload[0:5] == b'\x0e\x0a\x01\x09\x10',
28                       lambda packet: hci.Event.parse_all(packet.payload))
29
30    @staticmethod
31    def ConnectionRequestCapture():
32        return Capture(lambda packet: packet.payload[0:2] == b'\x04\x0a',
33                       lambda packet: hci.Event.parse_all(packet.payload))
34
35    @staticmethod
36    def ConnectionCompleteCapture():
37        return Capture(lambda packet: packet.payload[0:3] == b'\x03\x0b\x00',
38                       lambda packet: hci.Event.parse_all(packet.payload))
39
40    @staticmethod
41    def DisconnectionCompleteCapture():
42        return Capture(lambda packet: packet.payload[0:2] == b'\x05\x04',
43                       lambda packet: hci.Event.parse_all(packet.payload))
44
45    @staticmethod
46    def LeConnectionCompleteCapture():
47        return Capture(
48            lambda packet: packet.payload[0] == 0x3e and (packet.payload[2] == 0x01 or packet.payload[2] == 0x0a),
49            lambda packet: hci.Event.parse_all(packet.payload))
50
51
52class HciCaptures(object):
53
54    @staticmethod
55    def ReadLocalOobDataCompleteCapture():
56        return Capture(
57            HciMatchers.CommandComplete(hci.OpCode.READ_LOCAL_OOB_DATA),
58            lambda packet: HciMatchers.ExtractMatchingCommandComplete(packet.payload, hci.OpCode.READ_LOCAL_OOB_DATA))
59
60    @staticmethod
61    def ReadLocalOobExtendedDataCompleteCapture():
62        return Capture(
63            HciMatchers.CommandComplete(hci.OpCode.READ_LOCAL_OOB_EXTENDED_DATA), lambda packet: HciMatchers.
64            ExtractMatchingCommandComplete(packet.payload, hci.OpCode.READ_LOCAL_OOB_EXTENDED_DATA))
65
66    @staticmethod
67    def ReadBdAddrCompleteCapture():
68        return Capture(HciMatchers.CommandComplete(hci.OpCode.READ_BD_ADDR),
69                       lambda packet: hci.Event.parse_all(packet.payload))
70
71    @staticmethod
72    def ConnectionRequestCapture():
73        return Capture(HciMatchers.EventWithCode(hci.EventCode.CONNECTION_REQUEST),
74                       lambda packet: hci.Event.parse_all(packet.payload))
75
76    @staticmethod
77    def ConnectionCompleteCapture():
78        return Capture(HciMatchers.EventWithCode(hci.EventCode.CONNECTION_COMPLETE),
79                       lambda packet: hci.Event.parse_all(packet.payload))
80
81    @staticmethod
82    def DisconnectionCompleteCapture():
83        return Capture(HciMatchers.EventWithCode(hci.EventCode.DISCONNECTION_COMPLETE),
84                       lambda packet: hci.Event.parse_all(packet.payload))
85
86    @staticmethod
87    def LeConnectionCompleteCapture():
88        return Capture(HciMatchers.LeConnectionComplete(),
89                       lambda packet: HciMatchers.ExtractLeConnectionComplete(packet.payload))
90
91    @staticmethod
92    def SimplePairingCompleteCapture():
93        return Capture(HciMatchers.EventWithCode(hci.EventCode.SIMPLE_PAIRING_COMPLETE),
94                       lambda packet: hci.Event.parse_all(packet.payload))
95