Skip to content

Commit

Permalink
[tcat] Implement new tcat General commands.
Browse files Browse the repository at this point in the history
New General TLV's implemented:
	- Get network name
	- Get device id
	- Get ext pan ID
	- get provisioning URL
  • Loading branch information
canisLupus1313 committed Aug 1, 2024
1 parent 41cbe82 commit a056114
Show file tree
Hide file tree
Showing 8 changed files with 269 additions and 57 deletions.
18 changes: 18 additions & 0 deletions src/cli/cli_tcat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,19 @@ otTcatDeviceId sVendorDeviceIds[OT_TCAT_DEVICE_ID_MAX];
const char kPskdVendor[] = "JJJJJJ";
const char kUrl[] = "dummy_url";

static bool IsDeviceIdSet(void)
{
bool ret = false;

for (otTcatDeviceId &vendorDeviceId : sVendorDeviceIds)
{
VerifyOrExit(vendorDeviceId.mDeviceIdType == OT_TCAT_DEVICE_ID_EMPTY, ret = true);
}

exit:
return ret;
}

static void HandleBleSecureReceive(otInstance *aInstance,
const otMessage *aMessage,
int32_t aOffset,
Expand Down Expand Up @@ -210,6 +223,11 @@ template <> otError Tcat::Process<Cmd("start")>(Arg aArgs[])
mVendorInfo.mPskdString = kPskdVendor;
mVendorInfo.mProvisioningUrl = kUrl;

if (IsDeviceIdSet())
{
mVendorInfo.mDeviceIds = sVendorDeviceIds;
}

otBleSecureSetCertificate(GetInstancePtr(), reinterpret_cast<const uint8_t *>(OT_CLI_TCAT_X509_CERT),
sizeof(OT_CLI_TCAT_X509_CERT), reinterpret_cast<const uint8_t *>(OT_CLI_TCAT_PRIV_KEY),
sizeof(OT_CLI_TCAT_PRIV_KEY));
Expand Down
117 changes: 109 additions & 8 deletions src/core/meshcop/tcat_agent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

#include "tcat_agent.hpp"
#include <openthread/tcat.h>
#include "meshcop/network_name.hpp"

#if OPENTHREAD_CONFIG_BLE_TCAT_ENABLE

Expand All @@ -55,7 +56,10 @@ RegisterLogModule("TcatAgent");

bool TcatAgent::VendorInfo::IsValid(void) const
{
return mProvisioningUrl == nullptr || IsValidUtf8String(mProvisioningUrl) || mPskdString != nullptr;
return (mProvisioningUrl == nullptr ||
(IsValidUtf8String(mProvisioningUrl) &&
(static_cast<uint8_t>(strlen(mProvisioningUrl)) < Tlv::kBaseTlvMaxLength))) ||
mPskdString != nullptr;
}

TcatAgent::TcatAgent(Instance &aInstance)
Expand Down Expand Up @@ -414,17 +418,24 @@ Error TcatAgent::HandleSingleTlv(const Message &aIncomingMessage, Message &aOutg
error = HandleDecomission();
break;
case kTlvPing:
error = HandlePing(aIncomingMessage, aOutgoingMessage, offset, length);
if (error == kErrorNone)
{
response = true;
}
error = HandlePing(aIncomingMessage, aOutgoingMessage, offset, length, response);
break;
case kTlvGetNetworkName:
error = HandleGetNetworkName(aOutgoingMessage, response);
break;
case kTlvGetDeviceId:
error = HandleGetDeviceId(aOutgoingMessage, response);
break;
case kTlvGetExtendedPanID:
error = HandleGetExtPanId(aOutgoingMessage, response);
break;
case kTlvGetProvisioningURL:
error = HandleGetProvisioningUrl(aOutgoingMessage, response);
break;
default:
error = kErrorInvalidCommand;
}
}

if (!response)
{
StatusCode statusCode;
Expand Down Expand Up @@ -514,7 +525,8 @@ Error TcatAgent::HandleDecomission(void)
Error TcatAgent::HandlePing(const Message &aIncomingMessage,
Message &aOutgoingMessage,
uint16_t aOffset,
uint16_t aLength)
uint16_t aLength,
bool &response)
{
Error error = kErrorNone;
ot::ExtendedTlv extTlv;
Expand All @@ -535,6 +547,95 @@ Error TcatAgent::HandlePing(const Message &aIncomingMessage,
}

SuccessOrExit(error = aOutgoingMessage.AppendBytesFromMessage(aIncomingMessage, aOffset, aLength));
response = true;

exit:
return error;
}

Error TcatAgent::HandleGetNetworkName(Message &aOutgoingMessage, bool &response)
{
Error error = kErrorNone;
MeshCoP::NameData nameData = Get<MeshCoP::NetworkNameManager>().GetNetworkName().GetAsData();

VerifyOrExit(Get<ActiveDatasetManager>().IsCommissioned(), error = kErrorInvalidState);
#if !OPENTHREAD_CONFIG_ALLOW_EMPTY_NETWORK_NAME
VerifyOrExit(nameData.GetLength() > 0, error = kErrorInvalidState);
#endif

SuccessOrExit(
error = Tlv::AppendTlv(aOutgoingMessage, kTlvResponseWithPayload, nameData.GetBuffer(), nameData.GetLength()));
response = true;

exit:
return error;
}

Error TcatAgent::HandleGetDeviceId(Message &aOutgoingMessage, bool &response)
{
uint8_t deviceId[sizeof(Mac::ExtAddress)];
uint16_t length = 0;
ot::Tlv tlv;
Error error = kErrorNone;

static_assert(OT_EXT_ADDRESS_SIZE > OT_TCAT_MAX_VENDORID_SIZE, "Size of TCAT vendor id is greater than extaddr.");

if (mVendorInfo->mDeviceIds != nullptr)
{
// If array is not null return first set device ID.
for (uint8_t i = 0; mVendorInfo->mDeviceIds[i].mDeviceIdType != OT_TCAT_DEVICE_ID_EMPTY; i++)
{
length = mVendorInfo->mDeviceIds->mDeviceIdLen;
memcpy(deviceId, mVendorInfo->mDeviceIds[i].mDeviceId, length);
break;
}
}

if (length == 0)
{
// If array is null or contains only empty entries return EUI64.
otPlatRadioGetIeeeEui64(&GetInstance(), deviceId);
length = OT_EXT_ADDRESS_SIZE;
}

tlv.SetType(kTlvResponseWithPayload);
tlv.SetLength(static_cast<uint8_t>(length));
SuccessOrExit(error = aOutgoingMessage.Append(tlv));
SuccessOrExit(error = aOutgoingMessage.AppendBytes(deviceId, length));

response = true;

exit:
return error;
}

Error TcatAgent::HandleGetExtPanId(Message &aOutgoingMessage, bool &response)
{
Error error;

VerifyOrExit(Get<ActiveDatasetManager>().IsCommissioned(), error = kErrorInvalidState);

SuccessOrExit(error = Tlv::AppendTlv(aOutgoingMessage, kTlvResponseWithPayload,
&Get<MeshCoP::ExtendedPanIdManager>().GetExtPanId(), sizeof(ExtendedPanId)));
response = true;

exit:
return error;
}

Error TcatAgent::HandleGetProvisioningUrl(Message &aOutgoingMessage, bool &response)
{
Error error = kErrorNone;
uint16_t length;

VerifyOrExit(mVendorInfo->mProvisioningUrl != nullptr, error = kErrorInvalidState);

length = StringLength(mVendorInfo->mProvisioningUrl, Tlv::kBaseTlvMaxLength + 1);
VerifyOrExit(length > 0 && length <= Tlv::kBaseTlvMaxLength, error = kErrorInvalidState);

error = Tlv::AppendTlv(aOutgoingMessage, kTlvResponseWithPayload, mVendorInfo->mProvisioningUrl,
static_cast<uint8_t>(length));
response = true;

exit:
return error;
Expand Down
11 changes: 10 additions & 1 deletion src/core/meshcop/tcat_agent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ class TcatAgent : public InstanceLocator, private NonCopyable
kTlvPing = 10, ///< TCAT ping request TLV
kTlvGetDeviceId = 11, ///< TCAT device ID query TLV
kTlvGetExtendedPanID = 12, ///< TCAT extended PAN ID query TLV
kTlvGetProvisioningURL = 13, ///< TCAT provisioning URL query TLV
kTlvPresentPskdHash = 16, ///< TCAT commissioner rights elevation request TLV using PSKd hash
kTlvPresentPskcHash = 17, ///< TCAT commissioner rights elevation request TLV using PSKc hash
kTlvPresentInstallCodeHash = 18, ///< TCAT commissioner rights elevation request TLV using install code
Expand Down Expand Up @@ -353,7 +354,15 @@ class TcatAgent : public InstanceLocator, private NonCopyable
Error HandleSingleTlv(const Message &aIncomingMessage, Message &aOutgoingMessage);
Error HandleSetActiveOperationalDataset(const Message &aIncomingMessage, uint16_t aOffset, uint16_t aLength);
Error HandleDecomission(void);
Error HandlePing(const Message &aIncomingMessage, Message &aOutgoingMessage, uint16_t aOffset, uint16_t aLength);
Error HandlePing(const Message &aIncomingMessage,
Message &aOutgoingMessage,
uint16_t aOffset,
uint16_t aLength,
bool &response);
Error HandleGetNetworkName(Message &aOutgoingMessage, bool &response);
Error HandleGetDeviceId(Message &aOutgoingMessage, bool &response);
Error HandleGetExtPanId(Message &aOutgoingMessage, bool &response);
Error HandleGetProvisioningUrl(Message &aOutgoingMessage, bool &response);
Error HandleStartThreadInterface(void);

bool CheckCommandClassAuthorizationFlags(CommandClassFlags aCommissionerCommandClassFlags,
Expand Down
25 changes: 25 additions & 0 deletions tests/scripts/expect/cli-tcat.exp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ expect_line "Done"
spawn python "tools/tcat_ble_client/bbtc.py" --simulation 1 --cert_path "tools/tcat_ble_client/auth"
set py_client "$spawn_id"
expect_line "Done"

send "network_name\n"
expect_line "\tTYPE:\tRESPONSE_W_STATUS"
expect_line "\tVALUE:\t0x06"

send "commission\n"
expect_line "\tTYPE:\tRESPONSE_W_STATUS"
expect_line "\tVALUE:\t0x00"
Expand All @@ -58,6 +63,26 @@ send "ping 512\n"
expect_line "\tTYPE:\tRESPONSE_W_PAYLOAD"
expect_line "\tLEN:\t512"

send "network_name\n"
expect_line "\tTYPE:\tRESPONSE_W_PAYLOAD"
expect_line "\tLEN:\t15"
expect_line "\tVALUE:\t0x4f70656e5468726561642d63363465"

send "device_id\n"
expect_line "\tTYPE:\tRESPONSE_W_PAYLOAD"
expect_line "\tLEN:\t8"
expect_line "\tVALUE:\t0x18b4300000000001"

send "ext_panid\n"
expect_line "\tTYPE:\tRESPONSE_W_PAYLOAD"
expect_line "\tLEN:\t8"
expect_line "\tVALUE:\t0xef1398c2fd504b67"

send "provisioning_url\n"
expect_line "\tTYPE:\tRESPONSE_W_PAYLOAD"
expect_line "\tLEN:\t9"
expect_line "\tVALUE:\t0x64756d6d795f75726c"

send "exit\n"
expect eof

Expand Down
Loading

0 comments on commit a056114

Please sign in to comment.