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

Commit 7f922fd6 authored by Hansong Zhang's avatar Hansong Zhang
Browse files

LeL2capTest initial commit

Test: cert/run --host
Change-Id: I7e1aea927a63fa4b84774ef213c7724bd1db2202
parent e7ae92bf
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -372,6 +372,7 @@ filegroup {
        "hci/facade/le_scanning_manager_facade.proto",
        "neighbor/facade/facade.proto",
        "l2cap/classic/facade.proto",
        "l2cap/le/facade.proto",
        "security/facade.proto",
    ],
}
@@ -407,6 +408,8 @@ genrule {
        "hci/facade/le_scanning_manager_facade.pb.h",
        "l2cap/classic/facade.grpc.pb.h",
        "l2cap/classic/facade.pb.h",
        "l2cap/le/facade.grpc.pb.h",
        "l2cap/le/facade.pb.h",
        "neighbor/facade/facade.grpc.pb.h",
        "neighbor/facade/facade.pb.h",
        "security/facade.grpc.pb.h",
@@ -445,6 +448,8 @@ genrule {
        "hci/facade/le_scanning_manager_facade.pb.cc",
        "l2cap/classic/facade.grpc.pb.cc",
        "l2cap/classic/facade.pb.cc",
        "l2cap/le/facade.grpc.pb.cc",
        "l2cap/le/facade.pb.cc",
        "neighbor/facade/facade.grpc.pb.cc",
        "neighbor/facade/facade.pb.cc",
        "security/facade.grpc.pb.cc",
@@ -466,6 +471,7 @@ genrule {
        "touch $(genDir)/packages/modules/Bluetooth/system/gd/hci/__init__.py && " +
        "touch $(genDir)/packages/modules/Bluetooth/system/gd/hci/facade/__init__.py && " +
        "touch $(genDir)/packages/modules/Bluetooth/system/gd/l2cap/classic/__init__.py && " +
        "touch $(genDir)/packages/modules/Bluetooth/system/gd/l2cap/le/__init__.py && " +
        "touch $(genDir)/packages/modules/Bluetooth/system/gd/neighbor/facade/__init__.py && " +
        "touch $(genDir)/packages/modules/Bluetooth/system/gd/security/__init__.py && " +
        "$(location soong_zip) -C $(genDir) -D $(genDir) -o $(out)",
+1 −0
Original line number Diff line number Diff line
@@ -10,3 +10,4 @@ AclManagerTest
LeAclManagerTest
StackTest
L2capTest
LeL2capTest
+25 −1
Original line number Diff line number Diff line
@@ -17,7 +17,7 @@
import bluetooth_packets_python3 as bt_packets
from bluetooth_packets_python3 import hci_packets
from bluetooth_packets_python3 import l2cap_packets
from bluetooth_packets_python3.l2cap_packets import CommandCode
from bluetooth_packets_python3.l2cap_packets import CommandCode, LeCommandCode
from cert.capture import Capture
from cert.matchers import L2capMatchers

@@ -47,6 +47,18 @@ def ConnectionCompleteCapture():
                                    list(packet.event)))))


def LeConnectionCompleteCapture():
    return Capture(lambda packet: print(packet.event[0:3]) or packet.event[0] == 0x3e
                   and (packet.event[2] == 0x01 or packet.event[2] == 0x0a),
        lambda packet: hci_packets.LeConnectionCompleteView(
            hci_packets.LeMetaEventView(
                            hci_packets.EventPacketView(
                                bt_packets.PacketViewLittleEndian(
                                    list(packet.event)
                                )
                            ))))


class L2capCaptures(object):

    @staticmethod
@@ -60,3 +72,15 @@ class L2capCaptures(object):
        frame = L2capMatchers.control_frame_with_code(
            packet, CommandCode.CONNECTION_RESPONSE)
        return l2cap_packets.ConnectionResponseView(frame)

    @staticmethod
    def CreditBasedConnectionResponse(scid):
        return Capture(
            L2capMatchers.CreditBasedConnectionResponse(scid),
            L2capCaptures._extract_credit_based_connection_response)

    @staticmethod
    def _extract_credit_based_connection_response(packet):
        frame = L2capMatchers.le_control_frame_with_code(
            packet, LeCommandCode.LE_CREDIT_BASED_CONNECTION_RESPONSE)
        return l2cap_packets.LeCreditBasedConnectionResponseView(frame)
+3 −0
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@ from hci.facade import le_advertising_manager_facade_pb2_grpc
from hci.facade import le_scanning_manager_facade_pb2_grpc
from neighbor.facade import facade_pb2_grpc as neighbor_facade_pb2_grpc
from l2cap.classic import facade_pb2_grpc as l2cap_facade_pb2_grpc
from l2cap.le import facade_pb2_grpc as l2cap_le_facade_pb2_grpc
from security import facade_pb2_grpc as security_facade_pb2_grpc
from google.protobuf import empty_pb2 as empty_proto
from cert.event_stream import EventStream
@@ -91,6 +92,8 @@ class GdDevice(GdDeviceBase):
        self.hci.send_command_with_status = self.__send_hci_command_with_status
        self.l2cap = l2cap_facade_pb2_grpc.L2capClassicModuleFacadeStub(
            self.grpc_channel)
        self.l2cap_le = l2cap_le_facade_pb2_grpc.L2capLeModuleFacadeStub(
            self.grpc_channel)
        self.hci_acl_manager = acl_manager_facade_pb2_grpc.AclManagerFacadeStub(
            self.grpc_channel)
        self.hci_le_acl_manager = le_acl_manager_facade_pb2_grpc.LeAclManagerFacadeStub(
+39 −1
Original line number Diff line number Diff line
@@ -16,9 +16,10 @@

import bluetooth_packets_python3 as bt_packets
from bluetooth_packets_python3 import l2cap_packets
from bluetooth_packets_python3.l2cap_packets import CommandCode
from bluetooth_packets_python3.l2cap_packets import CommandCode, LeCommandCode
from bluetooth_packets_python3.l2cap_packets import ConnectionResponseResult
from bluetooth_packets_python3.l2cap_packets import InformationRequestInfoType
from bluetooth_packets_python3.l2cap_packets import LeCreditBasedConnectionResponseResult
import logging


@@ -52,6 +53,14 @@ class L2capMatchers(object):
    def CommandReject():
        return lambda packet: L2capMatchers._is_control_frame_with_code(packet, CommandCode.COMMAND_REJECT)

    @staticmethod
    def CreditBasedConnectionRequest():
        return lambda packet: L2capMatchers._is_le_control_frame_with_code(packet, LeCommandCode.LE_CREDIT_BASED_CONNECTION_REQUEST)

    @staticmethod
    def CreditBasedConnectionResponse(scid):
        return lambda packet: L2capMatchers._is_matching_credit_based_connection_response(packet, scid)

    @staticmethod
    def SFrame(req_seq=None, f=None, s=None, p=None):
        return lambda packet: L2capMatchers._is_matching_supervisory_frame(packet, req_seq, f, s, p)
@@ -140,6 +149,12 @@ class L2capMatchers(object):
            return None
        return l2cap_packets.ControlView(packet.GetPayload())

    @staticmethod
    def _le_control_frame(packet):
        if packet.GetChannelId() != 5:
            return None
        return l2cap_packets.LeControlView(packet.GetPayload())

    @staticmethod
    def control_frame_with_code(packet, code):
        frame = L2capMatchers._control_frame(packet)
@@ -147,10 +162,22 @@ class L2capMatchers(object):
            return None
        return frame

    @staticmethod
    def le_control_frame_with_code(packet, code):
        frame = L2capMatchers._le_control_frame(packet)
        if frame is None or frame.GetCode() != code:
            return None
        return frame

    @staticmethod
    def _is_control_frame_with_code(packet, code):
        return L2capMatchers.control_frame_with_code(packet, code) is not None

    @staticmethod
    def _is_le_control_frame_with_code(packet, code):
        return L2capMatchers.le_control_frame_with_code(packet,
                                                        code) is not None

    @staticmethod
    def _is_matching_connection_response(packet, scid):
        frame = L2capMatchers.control_frame_with_code(
@@ -203,3 +230,14 @@ class L2capMatchers(object):
        ) != supports_fixed_channels:
            return False
        return True

    @staticmethod
    def _is_matching_credit_based_connection_response(packet, scid):
        frame = L2capMatchers.le_control_frame_with_code(
            packet, LeCommandCode.LE_CREDIT_BASED_CONNECTION_RESPONSE)
        if frame is None:
            return False
        response = l2cap_packets.LeCreditBasedConnectionResponseView(frame)
        return response.GetResult(
        ) == LeCreditBasedConnectionResponseResult.SUCCESS and response.GetDestinationCid(
        ) != 0
Loading