Skip to content

Commit 0b0d68b

Browse files
koffessjanc
authored andcommitted
Added svc data set function
Added function in gap.py to set service data. Tests added to verify functionality.
1 parent 9e23435 commit 0b0d68b

3 files changed

Lines changed: 87 additions & 0 deletions

File tree

autopts/pybtp/btp/gap.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1700,3 +1700,35 @@ def gap_set_broadcast_code(broadcast_code):
17001700
logging.debug("%s %r", gap_set_broadcast_code.__name__, broadcast_code)
17011701
stack = get_stack()
17021702
stack.gap.big_broadcast_code = broadcast_code
1703+
1704+
1705+
def gap_set_uuid16_svc_data(advData, uuid, service_data=None):
1706+
"""Update service data. Will overwrite existing UUID16 service data if present"""
1707+
# Convert uuid to integer if it's a string
1708+
if not isinstance(uuid, int):
1709+
try:
1710+
uuid = int(uuid, 16)
1711+
except ValueError as err:
1712+
raise ValueError("Invalid UUID string format") from err
1713+
1714+
if (uuid < 0) or (uuid > 0xFFFF):
1715+
raise ValueError("Invalid UUID16 value")
1716+
1717+
if service_data is not None:
1718+
# Check if service_data is bytes (result of struct.pack)
1719+
if not isinstance(service_data, bytes):
1720+
raise TypeError(f"service_data must be bytes, got {type(service_data).__name__}")
1721+
1722+
# Initialize uuid16_svc_data if it doesn't exist
1723+
if AdType.uuid16_svc_data not in advData:
1724+
advData[AdType.uuid16_svc_data] = []
1725+
1726+
for entry in advData[AdType.uuid16_svc_data]:
1727+
existing_uuid = struct.unpack_from('<H', entry[:2])[0]
1728+
if existing_uuid == uuid:
1729+
advData[AdType.uuid16_svc_data].remove(entry)
1730+
break
1731+
if service_data is None:
1732+
advData[AdType.uuid16_svc_data] += [struct.pack('<H', uuid)]
1733+
else:
1734+
advData[AdType.uuid16_svc_data] += [struct.pack('<H', uuid) + service_data]

autopts/pybtp/types.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ class UUID:
117117
BASS = '184F'
118118
PACS = '1850'
119119
CAS = '1853'
120+
TMAP = '1855'
120121
ASE_CP = '2BC6'
121122
CEP = '2900'
122123
CUD = '2901'
@@ -502,3 +503,22 @@ class BIGEncryption:
502503
BROADCAST_CODE_REQUIRED = 0x01
503504
DECRYPTING = 0x02
504505
BAD_CODE = 0x03
506+
507+
508+
class TMAPRole(IntFlag):
509+
CALL_GATEWAY = defs.BIT(0)
510+
CALL_TERMINAL = defs.BIT(1)
511+
UNICAST_MEDIA_SENDER = defs.BIT(2)
512+
UNICAST_MEDIA_RECEIVER = defs.BIT(3)
513+
BROADCAST_MEDIA_SENDER = defs.BIT(4)
514+
BROADCAST_MEDIA_RECEIVER = defs.BIT(5)
515+
516+
517+
class BAPAnnouncement:
518+
GENERAL = 0x0
519+
TARGETED = 0x1
520+
521+
522+
class CAPAnnouncement:
523+
GENERAL = 0x0
524+
TARGETED = 0x1

test/unittests.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
import os
22
import shutil
3+
import struct
34
import sys
45
import unittest
56
from os.path import abspath, dirname
67
from pathlib import Path
78
from unittest.mock import patch
89

10+
import pytest
11+
912
from autopts.bot.common_features import report
1013
from autopts.client import FakeProxy, TestCaseRunStats
1114
from autopts.config import FILE_PATHS
1215
from autopts.ptsprojects.testcase_db import TestCaseTable
16+
from autopts.pybtp.btp.gap import gap_set_uuid16_svc_data
17+
from autopts.pybtp.types import AdType
1318
from autoptsclient_bot import import_bot_module, import_bot_projects
1419
from test.mocks.mocked_test_cases import mock_workspace_test_cases, test_case_list_generation_samples
1520

@@ -250,6 +255,36 @@ def test_generate_stats(self):
250255
results, regressions, progresses, new_cases)
251256
assert os.path.exists(FILE_PATHS['REPORT_DIFF_TXT_FILE'])
252257

258+
def test_gap_set_uuid16_svc_data(self):
259+
advData = {}
260+
# Test invalid inputs
261+
with pytest.raises(ValueError, match="Invalid UUID16 value"):
262+
gap_set_uuid16_svc_data(advData, 0xFFFF1, None)
263+
with pytest.raises(ValueError, match="Invalid UUID16 value"):
264+
gap_set_uuid16_svc_data(advData, -1, None)
265+
with pytest.raises(TypeError):
266+
gap_set_uuid16_svc_data(advData, 0xAAAA, 4)
267+
268+
# Test valid input
269+
try:
270+
gap_set_uuid16_svc_data(advData, 0x0000, None)
271+
gap_set_uuid16_svc_data(advData, 0xFFFF, None)
272+
gap_set_uuid16_svc_data(advData, 0xAABB, struct.pack('<B', 1))
273+
274+
except Exception as e:
275+
self.fail(f"Function raised unexpected exception: {e}")
276+
277+
assert advData[AdType.uuid16_svc_data][0] == struct.pack('<H', 0x0000)
278+
assert advData[AdType.uuid16_svc_data][1] == struct.pack('<H', 0xFFFF)
279+
assert advData[AdType.uuid16_svc_data][2] == struct.pack('<HB', 0xAABB, 1)
280+
281+
# Update a value in advData
282+
gap_set_uuid16_svc_data(advData, 0xFFFF, struct.pack('<H', 0xABCD))
283+
284+
assert advData[AdType.uuid16_svc_data][0] == struct.pack('<H', 0x0000)
285+
assert advData[AdType.uuid16_svc_data][1] == struct.pack('<HB', 0xAABB, 1)
286+
assert advData[AdType.uuid16_svc_data][2] == struct.pack('<HH', 0xFFFF, 0xABCD)
287+
253288

254289
if __name__ == '__main__':
255290
unittest.main()

0 commit comments

Comments
 (0)