Skip to content

Commit

Permalink
[tcat] feat: extend dataset hex command (openthread#10831)
Browse files Browse the repository at this point in the history
This commit extends the 'dataset hex' command in the bbtc.py script by
allowing dataset TLVs to be set using a hex-encoded format.

Till now the 'dataset hex' command was only printing the
'ThreadDataset' object values in hex-encoded format, there was no
functionality to set the TLVs using hex-encoded format.

The 'dataset hex' command has been modified so the user can pass
dataset TLVs in hex-encoded format as an argument to this
command. This enables the script to set desired dataset TLVs in one
command, instead of calling dataset commands individually.

Example usage: 'dataset hex <hex-encoded TLVs>'
  • Loading branch information
jaul-nsc authored Oct 14, 2024
1 parent 8b51782 commit 68c78d3
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 22 deletions.
17 changes: 14 additions & 3 deletions tools/tcat_ble_client/cli/dataset_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from cli.command import Command, CommandResultNone
from dataset.dataset import ThreadDataset, initial_dataset
from tlv.dataset_tlv import MeshcopTlvType
from copy import deepcopy


def handle_dataset_entry_command(type: MeshcopTlvType, args, context):
Expand Down Expand Up @@ -71,13 +72,23 @@ async def execute_default(self, args, context):
return CommandResultNone()


class PrintDatasetHexCommand(Command):
class DatasetHexCommand(Command):

def get_help_string(self) -> str:
return 'Print current dataset as a hexadecimal string.'
return 'Get or set dataset as hex-encoded TLVs.'

async def execute_default(self, args, context):
ds: ThreadDataset = context['dataset']
if args:
try:
ds_tmp = deepcopy(ds)
tlvs_str: str = args[0]
tlvs_bytes = bytes.fromhex(tlvs_str)
ds_tmp.set_from_bytes(tlvs_bytes)
ds.clear()
ds.set_from_bytes(tlvs_bytes)
except Exception as e:
print(e)
print(ds.to_bytes().hex())
return CommandResultNone()

Expand Down Expand Up @@ -207,7 +218,7 @@ def __init__(self):
self._subcommands = {
'clear': DatasetClearCommand(),
'help': DatasetHelpCommand(),
'hex': PrintDatasetHexCommand(),
'hex': DatasetHexCommand(),
'reload': ReloadDatasetCommand(),
'activetimestamp': ActiveTimestampCommand(),
'pendingtimestamp': PendingTimestampCommand(),
Expand Down
9 changes: 6 additions & 3 deletions tools/tcat_ble_client/dataset/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

from tlv.tlv import TLV
from tlv.dataset_tlv import MeshcopTlvType
from dataset.dataset_entries import DatasetEntry, create_dataset_entry
from dataset.dataset_entries import DatasetEntry, create_dataset_entry, ENTRY_CLASSES

initial_dataset = bytes([
0x0E, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x12, 0x35, 0x06, 0x00, 0x04,
Expand Down Expand Up @@ -58,8 +58,11 @@ def get_entry(self, type: MeshcopTlvType):
return self.entries[type]

def set_entry(self, type: MeshcopTlvType, args: List[str]):
if type in self.entries:
self.entries[type].set(args)
if type in ENTRY_CLASSES:
if type in self.entries:
self.entries[type].set(args)
else:
self.entries[type] = create_dataset_entry(type, args)
return
raise KeyError(f'Key {type} not available in the dataset.')

Expand Down
33 changes: 17 additions & 16 deletions tools/tcat_ble_client/dataset/dataset_entries.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,23 +476,24 @@ def to_tlv(self):
return TLV.from_bytes(tlv)


ENTRY_CLASSES = {
MeshcopTlvType.ACTIVETIMESTAMP: ActiveTimestamp,
MeshcopTlvType.PENDINGTIMESTAMP: PendingTimestamp,
MeshcopTlvType.NETWORKKEY: NetworkKey,
MeshcopTlvType.NETWORKNAME: NetworkName,
MeshcopTlvType.EXTPANID: ExtPanID,
MeshcopTlvType.MESHLOCALPREFIX: MeshLocalPrefix,
MeshcopTlvType.DELAYTIMER: DelayTimer,
MeshcopTlvType.PANID: PanID,
MeshcopTlvType.CHANNEL: Channel,
MeshcopTlvType.PSKC: Pskc,
MeshcopTlvType.SECURITYPOLICY: SecurityPolicy,
MeshcopTlvType.CHANNELMASK: ChannelMask
}


def create_dataset_entry(type: MeshcopTlvType, args=None):
entry_classes = {
MeshcopTlvType.ACTIVETIMESTAMP: ActiveTimestamp,
MeshcopTlvType.PENDINGTIMESTAMP: PendingTimestamp,
MeshcopTlvType.NETWORKKEY: NetworkKey,
MeshcopTlvType.NETWORKNAME: NetworkName,
MeshcopTlvType.EXTPANID: ExtPanID,
MeshcopTlvType.MESHLOCALPREFIX: MeshLocalPrefix,
MeshcopTlvType.DELAYTIMER: DelayTimer,
MeshcopTlvType.PANID: PanID,
MeshcopTlvType.CHANNEL: Channel,
MeshcopTlvType.PSKC: Pskc,
MeshcopTlvType.SECURITYPOLICY: SecurityPolicy,
MeshcopTlvType.CHANNELMASK: ChannelMask
}

entry_class = entry_classes.get(type)
entry_class = ENTRY_CLASSES.get(type)
if not entry_class:
raise ValueError(f"Invalid configuration type: {type}")

Expand Down

0 comments on commit 68c78d3

Please sign in to comment.