Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions bumble/sdp.py
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,10 @@ class SDP_PDU:

@classmethod
def from_bytes(cls, pdu: bytes) -> SDP_PDU:
pdu_id, transaction_id, _parameters_length = struct.unpack_from('>BHH', pdu, 0)
pdu_id, transaction_id, parameters_length = struct.unpack_from('>BHH', pdu, 0)

if len(pdu) != 5 + parameters_length:
logger.warning("Expect %d bytes, got %d", 5 + parameters_length, len(pdu))

subclass = cls.subclasses.get(pdu_id)
if not (subclass := cls.subclasses.get(pdu_id)):
Expand All @@ -616,9 +619,11 @@ def subclass(cls, subclass: type[_PDU]) -> type[_PDU]:

def __bytes__(self):
if self._payload is None:
self._payload = struct.pack(
'>BHH', self.pdu_id, self.transaction_id, 0
) + hci.HCI_Object.dict_to_bytes(self.__dict__, self.fields)
parameters = hci.HCI_Object.dict_to_bytes(self.__dict__, self.fields)
self._payload = (
struct.pack('>BHH', self.pdu_id, self.transaction_id, len(parameters))
+ parameters
)
return self._payload

@property
Expand Down
12 changes: 12 additions & 0 deletions tests/sdp_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@
import asyncio
import logging
import os
import re

import pytest

from bumble import sdp
from bumble.core import BT_L2CAP_PROTOCOL_ID, UUID
from bumble.sdp import (
SDP_BROWSE_GROUP_LIST_ATTRIBUTE_ID,
Expand Down Expand Up @@ -206,6 +208,16 @@ def sdp_records(record_count=1):
}


# -----------------------------------------------------------------------------
def test_pdu_parameter_length(caplog) -> None:
caplog.set_level(logging.WARNING)
pdu = sdp.SDP_ErrorResponse(
transaction_id=0, error_code=sdp.ErrorCode.INVALID_SDP_VERSION
)
assert sdp.SDP_PDU.from_bytes(bytes(pdu)) == pdu
assert not re.search("Expect \d+ bytes, got \d+", caplog.text)


# -----------------------------------------------------------------------------
@pytest.mark.asyncio
async def test_service_search():
Expand Down
Loading