mirror of
https://github.com/espressif/openthread.git
synced 2026-08-09 20:27:47 +00:00
[spinel] add spinel property codec (#10934)
This commit adds spinel_prop_codec module to lib/spinel to provide encoding & decoding functions for complex spinel properties. This commit moves the encoding of `SPINEL_PROP_DNSSD_HOST`, `SPINEL_PROP_DNSSD_SERVICE` and `SPINEL_PROP_DNSSD_KEY_RECORD` from NcpBase to the lib and adds the decoding functions. The background is that I found the encoding & decoding of complex spinel properties are error-prone. However the encoding and decoding of one property are usually put in different places. For example, encoding is in openthread ncp while decoding is in ot-br-posix ncp spinel. It's difficult to debug the decoding in ot-br-posix and there is no unit tests for the encoding & decoding. This commit puts the encoding & decoding together and adds unit tests to ensure they are correct.
This commit is contained in:
@@ -73,6 +73,7 @@ set(COMMON_SOURCES
|
||||
spinel_decoder.cpp
|
||||
spinel_encoder.cpp
|
||||
spinel_helper.cpp
|
||||
spinel_prop_codec.cpp
|
||||
)
|
||||
|
||||
set(OT_SPINEL_VENDOR_HOOK_SOURCE "" CACHE STRING "set vendor hook source file for Spinel")
|
||||
|
||||
@@ -4859,7 +4859,7 @@ enum
|
||||
* `S` : The service port number.
|
||||
* `S` : The service priority.
|
||||
* `S` : The service weight.
|
||||
* `S` : The service TTL in seconds.
|
||||
* `L` : The service TTL in seconds.
|
||||
* `L` : The Dnssd Request ID.
|
||||
* `D` : The context of the request. (A pointer to the callback for the request)
|
||||
*
|
||||
@@ -4875,7 +4875,7 @@ enum
|
||||
* `t(U)` : The service type if key is for a service (does not include domain name).
|
||||
* `d` : Byte array containing the key record data.
|
||||
* `S` : The resource record class.
|
||||
* `S` : The TTL in seconds.
|
||||
* `L` : The TTL in seconds.
|
||||
* `L` : The Dnssd Request ID.
|
||||
* `D` : The context of the request. (A pointer to the callback for the request)
|
||||
*
|
||||
|
||||
@@ -0,0 +1,200 @@
|
||||
/*
|
||||
* Copyright (c) 2024, The OpenThread Authors.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the copyright holder nor the
|
||||
* names of its contributors may be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file This file implements spinel property encoding and decoding functions.
|
||||
*/
|
||||
|
||||
#include "lib/spinel/spinel_prop_codec.hpp"
|
||||
#include "lib/utils/utils.hpp"
|
||||
|
||||
namespace ot {
|
||||
namespace Spinel {
|
||||
|
||||
template <>
|
||||
otError EncodeDnssd<otPlatDnssdHost>(Encoder &aEncoder,
|
||||
const otPlatDnssdHost &aObj,
|
||||
otPlatDnssdRequestId aRequestId,
|
||||
otPlatDnssdRegisterCallback aCallback)
|
||||
{
|
||||
otError error = OT_ERROR_NONE;
|
||||
|
||||
EXPECT_NO_ERROR(error = aEncoder.WriteUintPacked(SPINEL_PROP_DNSSD_HOST));
|
||||
EXPECT_NO_ERROR(error = aEncoder.WriteUtf8(aObj.mHostName == nullptr ? "" : aObj.mHostName));
|
||||
EXPECT_NO_ERROR(error = aEncoder.WriteUint16(aObj.mAddressesLength));
|
||||
for (uint16_t i = 0; i < aObj.mAddressesLength; i++)
|
||||
{
|
||||
EXPECT_NO_ERROR(error = aEncoder.WriteIp6Address(aObj.mAddresses[i]));
|
||||
}
|
||||
EXPECT_NO_ERROR(error = aEncoder.WriteUint32(aRequestId));
|
||||
EXPECT_NO_ERROR(error = aEncoder.WriteData(reinterpret_cast<const uint8_t *>(&aCallback), sizeof(aCallback)));
|
||||
|
||||
exit:
|
||||
return error;
|
||||
}
|
||||
|
||||
template <>
|
||||
otError EncodeDnssd<otPlatDnssdService>(Encoder &aEncoder,
|
||||
const otPlatDnssdService &aObj,
|
||||
otPlatDnssdRequestId aRequestId,
|
||||
otPlatDnssdRegisterCallback aCallback)
|
||||
{
|
||||
otError error = OT_ERROR_NONE;
|
||||
|
||||
EXPECT_NO_ERROR(error = aEncoder.WriteUintPacked(SPINEL_PROP_DNSSD_SERVICE));
|
||||
EXPECT_NO_ERROR(error = aEncoder.WriteUtf8(aObj.mHostName == nullptr ? "" : aObj.mHostName));
|
||||
EXPECT_NO_ERROR(error = aEncoder.WriteUtf8(aObj.mServiceInstance == nullptr ? "" : aObj.mServiceInstance));
|
||||
EXPECT_NO_ERROR(error = aEncoder.WriteUtf8(aObj.mServiceType == nullptr ? "" : aObj.mServiceType));
|
||||
EXPECT_NO_ERROR(error = aEncoder.OpenStruct());
|
||||
for (uint16_t i = 0; i < aObj.mSubTypeLabelsLength; i++)
|
||||
{
|
||||
EXPECT_NO_ERROR(error = aEncoder.WriteUtf8(aObj.mSubTypeLabels[i]));
|
||||
}
|
||||
EXPECT_NO_ERROR(error = aEncoder.CloseStruct());
|
||||
EXPECT_NO_ERROR(error = aEncoder.WriteDataWithLen(aObj.mTxtData, aObj.mTxtDataLength));
|
||||
EXPECT_NO_ERROR(error = aEncoder.WriteUint16(aObj.mPort));
|
||||
EXPECT_NO_ERROR(error = aEncoder.WriteUint16(aObj.mPriority));
|
||||
EXPECT_NO_ERROR(error = aEncoder.WriteUint16(aObj.mWeight));
|
||||
EXPECT_NO_ERROR(error = aEncoder.WriteUint32(aObj.mTtl));
|
||||
EXPECT_NO_ERROR(error = aEncoder.WriteUint32(aRequestId));
|
||||
EXPECT_NO_ERROR(error = aEncoder.WriteData(reinterpret_cast<const uint8_t *>(&aCallback), sizeof(aCallback)));
|
||||
|
||||
exit:
|
||||
return error;
|
||||
}
|
||||
|
||||
template <>
|
||||
otError EncodeDnssd<otPlatDnssdKey>(Encoder &aEncoder,
|
||||
const otPlatDnssdKey &aObj,
|
||||
otPlatDnssdRequestId aRequestId,
|
||||
otPlatDnssdRegisterCallback aCallback)
|
||||
{
|
||||
otError error = OT_ERROR_NONE;
|
||||
|
||||
EXPECT_NO_ERROR(error = aEncoder.WriteUintPacked(SPINEL_PROP_DNSSD_KEY_RECORD));
|
||||
EXPECT_NO_ERROR(error = aEncoder.WriteUtf8(aObj.mName == nullptr ? "" : aObj.mName));
|
||||
EXPECT_NO_ERROR(error = aEncoder.OpenStruct());
|
||||
if (aObj.mServiceType != nullptr)
|
||||
{
|
||||
EXPECT_NO_ERROR(error = aEncoder.WriteUtf8(aObj.mServiceType));
|
||||
}
|
||||
EXPECT_NO_ERROR(error = aEncoder.CloseStruct());
|
||||
EXPECT_NO_ERROR(error = aEncoder.WriteDataWithLen(aObj.mKeyData, aObj.mKeyDataLength));
|
||||
EXPECT_NO_ERROR(error = aEncoder.WriteUint16(aObj.mClass));
|
||||
EXPECT_NO_ERROR(error = aEncoder.WriteUint32(aObj.mTtl));
|
||||
EXPECT_NO_ERROR(error = aEncoder.WriteUint32(aRequestId));
|
||||
EXPECT_NO_ERROR(error = aEncoder.WriteData(reinterpret_cast<const uint8_t *>(&aCallback), sizeof(aCallback)));
|
||||
|
||||
exit:
|
||||
return error;
|
||||
}
|
||||
|
||||
otError DecodeDnssdHost(Decoder &aDecoder,
|
||||
otPlatDnssdHost &aHost,
|
||||
otPlatDnssdRequestId &aRequestId,
|
||||
const uint8_t *&aCallbackData,
|
||||
uint16_t &aCallbackDataLen)
|
||||
{
|
||||
otError error = OT_ERROR_NONE;
|
||||
|
||||
EXPECT_NO_ERROR(error = aDecoder.ReadUtf8(aHost.mHostName));
|
||||
EXPECT_NO_ERROR(error = aDecoder.ReadUint16(aHost.mAddressesLength));
|
||||
EXPECT_NO_ERROR(error = aDecoder.ReadIp6Address(aHost.mAddresses));
|
||||
EXPECT_NO_ERROR(error = aDecoder.ReadUint32(aRequestId));
|
||||
EXPECT_NO_ERROR(error = aDecoder.ReadData(aCallbackData, aCallbackDataLen));
|
||||
|
||||
exit:
|
||||
return error;
|
||||
}
|
||||
|
||||
otError DecodeDnssdService(Decoder &aDecoder,
|
||||
otPlatDnssdService &aService,
|
||||
const char **aSubTypeLabels,
|
||||
uint16_t &aSubTypeLabelsCount,
|
||||
otPlatDnssdRequestId &aRequestId,
|
||||
const uint8_t *&aCallbackData,
|
||||
uint16_t &aCallbackDataLen)
|
||||
{
|
||||
otError error = OT_ERROR_NONE;
|
||||
uint8_t index = 0;
|
||||
|
||||
EXPECT_NO_ERROR(error = aDecoder.ReadUtf8(aService.mHostName));
|
||||
EXPECT_NO_ERROR(error = aDecoder.ReadUtf8(aService.mServiceInstance));
|
||||
EXPECT_NO_ERROR(error = aDecoder.ReadUtf8(aService.mServiceType));
|
||||
EXPECT_NO_ERROR(error = aDecoder.OpenStruct());
|
||||
while (!aDecoder.IsAllReadInStruct())
|
||||
{
|
||||
EXPECT(index < aSubTypeLabelsCount, error = OT_ERROR_NO_BUFS);
|
||||
EXPECT_NO_ERROR(error = aDecoder.ReadUtf8(aSubTypeLabels[index]));
|
||||
index++;
|
||||
}
|
||||
aSubTypeLabelsCount = index;
|
||||
EXPECT_NO_ERROR(error = aDecoder.CloseStruct());
|
||||
EXPECT_NO_ERROR(error = aDecoder.ReadDataWithLen(aService.mTxtData, aService.mTxtDataLength));
|
||||
EXPECT_NO_ERROR(error = aDecoder.ReadUint16(aService.mPort));
|
||||
EXPECT_NO_ERROR(error = aDecoder.ReadUint16(aService.mPriority));
|
||||
EXPECT_NO_ERROR(error = aDecoder.ReadUint16(aService.mWeight));
|
||||
EXPECT_NO_ERROR(error = aDecoder.ReadUint32(aService.mTtl));
|
||||
EXPECT_NO_ERROR(error = aDecoder.ReadUint32(aRequestId));
|
||||
EXPECT_NO_ERROR(error = aDecoder.ReadData(aCallbackData, aCallbackDataLen));
|
||||
|
||||
exit:
|
||||
return error;
|
||||
}
|
||||
|
||||
otError DecodeDnssdKey(Decoder &aDecoder,
|
||||
otPlatDnssdKey &aKey,
|
||||
otPlatDnssdRequestId &aRequestId,
|
||||
const uint8_t *&aCallbackData,
|
||||
uint16_t &aCallbackDataLen)
|
||||
{
|
||||
otError error = OT_ERROR_NONE;
|
||||
|
||||
EXPECT_NO_ERROR(error = aDecoder.ReadUtf8(aKey.mName));
|
||||
EXPECT_NO_ERROR(error = aDecoder.OpenStruct());
|
||||
if (!aDecoder.IsAllReadInStruct())
|
||||
{
|
||||
EXPECT_NO_ERROR(error = aDecoder.ReadUtf8(aKey.mServiceType));
|
||||
}
|
||||
else
|
||||
{
|
||||
aKey.mServiceType = nullptr;
|
||||
}
|
||||
EXPECT_NO_ERROR(error = aDecoder.CloseStruct());
|
||||
EXPECT_NO_ERROR(error = aDecoder.ReadDataWithLen(aKey.mKeyData, aKey.mKeyDataLength));
|
||||
EXPECT_NO_ERROR(error = aDecoder.ReadUint16(aKey.mClass));
|
||||
EXPECT_NO_ERROR(error = aDecoder.ReadUint32(aKey.mTtl));
|
||||
EXPECT_NO_ERROR(error = aDecoder.ReadUint32(aRequestId));
|
||||
EXPECT_NO_ERROR(error = aDecoder.ReadData(aCallbackData, aCallbackDataLen));
|
||||
|
||||
exit:
|
||||
return error;
|
||||
}
|
||||
|
||||
} // namespace Spinel
|
||||
} // namespace ot
|
||||
@@ -0,0 +1,119 @@
|
||||
/*
|
||||
* Copyright (c) 2024, The OpenThread Authors.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the copyright holder nor the
|
||||
* names of its contributors may be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file This file includes definitions for spinel property encoding and decoding functions.
|
||||
*/
|
||||
|
||||
#ifndef SPINEL_PROP_CODEC_HPP_
|
||||
#define SPINEL_PROP_CODEC_HPP_
|
||||
|
||||
#include <openthread/platform/dnssd.h>
|
||||
|
||||
#include "lib/spinel/spinel.h"
|
||||
#include "lib/spinel/spinel_decoder.hpp"
|
||||
#include "lib/spinel/spinel_encoder.hpp"
|
||||
|
||||
namespace ot {
|
||||
namespace Spinel {
|
||||
|
||||
/**
|
||||
* Use Spinel::Encoder to encode a Dnssd object.
|
||||
*
|
||||
* A Spinel header and command MUST have been encoded by the encoder.
|
||||
*
|
||||
* @param[in] aEncoder A reference to the encoder object.
|
||||
* @param[in] aObj A reference to the dnssd object. (otPlatDnssdHost, otPlatDnssdService or otPlatDnssdKey).
|
||||
* @param[in] aRequestId The request id.
|
||||
* @param[in] aCallback A callback to receive the dnssd update result.
|
||||
*/
|
||||
template <typename DnssdObjType>
|
||||
otError EncodeDnssd(Encoder &aEncoder,
|
||||
const DnssdObjType &aObj,
|
||||
otPlatDnssdRequestId aRequestId,
|
||||
otPlatDnssdRegisterCallback aCallback);
|
||||
|
||||
/**
|
||||
* Use Spinel::Decoder to decode a SPINEL_PROP_DNSSD_HOST message to a otPlatDnssdHost.
|
||||
*
|
||||
* The decoder MUST have read the header, command and property key of the frame.
|
||||
*
|
||||
* @param[in] aDecoder A reference to the decoder object.
|
||||
* @param[out] aHost A reference to the dnssd host.
|
||||
* @param[out] aRequestId A reference to the request id.
|
||||
* @param[out] aCallback A reference to the pointer to the callback data.
|
||||
* @param[out] aCallbackLen A reference to the callback data length.
|
||||
*/
|
||||
otError DecodeDnssdHost(Decoder &aDecoder,
|
||||
otPlatDnssdHost &aHost,
|
||||
otPlatDnssdRequestId &aRequestId,
|
||||
const uint8_t *&aCallbackData,
|
||||
uint16_t &aCallbackDataLen);
|
||||
|
||||
/**
|
||||
* Use Spinel::Decoder to decode a SPINEL_PROP_DNSSD_SERVICE message to a otPlatDnssdService.
|
||||
*
|
||||
* The decoder MUST have read the header, command and property key of the frame.
|
||||
*
|
||||
* @param[in] aDecoder A reference to the decoder object.
|
||||
* @param[out] aService A reference to the dnssd service.
|
||||
* @param[out] aRequestId A reference to the request id.
|
||||
* @param[out] aSubTypeLabels A pointer to the array of sub-type labels.
|
||||
* @param[out] aSubTypeCount The number of sub-type labels.
|
||||
* @param[out] aCallback A reference to the pointer to the callback data.
|
||||
* @param[out] aCallbackLen A reference to the callback data length.
|
||||
*/
|
||||
otError DecodeDnssdService(Decoder &aDecoder,
|
||||
otPlatDnssdService &aService,
|
||||
const char **aSubTypeLabels,
|
||||
uint16_t &aSubTypeLabelsCount,
|
||||
otPlatDnssdRequestId &aRequestId,
|
||||
const uint8_t *&aCallbackData,
|
||||
uint16_t &aCallbackDataLen);
|
||||
|
||||
/**
|
||||
* Use Spinel::Decoder to decode a SPINEL_PROP_DNSSD_KEY_RECORD message to a otPlatDnssdKey.
|
||||
*
|
||||
* The decoder MUST have read the header, command and property key of the frame.
|
||||
*
|
||||
* @param[in] aDecoder A reference to the decoder object.
|
||||
* @param[out] aKey A reference to the dnssd key.
|
||||
* @param[out] aRequestId A reference to the request id.
|
||||
* @param[out] aCallback A reference to the pointer to the callback data.
|
||||
* @param[out] aCallbackLen A reference to the callback data length.
|
||||
*/
|
||||
otError DecodeDnssdKey(Decoder &aDecoder,
|
||||
otPlatDnssdKey &aKey,
|
||||
otPlatDnssdRequestId &aRequestId,
|
||||
const uint8_t *&aCallbackData,
|
||||
uint16_t &aCallbackDataLen);
|
||||
|
||||
} // namespace Spinel
|
||||
} // namespace ot
|
||||
|
||||
#endif // SPINEL_PROP_CODEC_HPP_
|
||||
@@ -62,6 +62,7 @@
|
||||
#include "lib/spinel/spinel_buffer.hpp"
|
||||
#include "lib/spinel/spinel_decoder.hpp"
|
||||
#include "lib/spinel/spinel_encoder.hpp"
|
||||
#include "lib/spinel/spinel_prop_codec.hpp"
|
||||
|
||||
#if OPENTHREAD_CONFIG_MULTIPAN_RCP_ENABLE
|
||||
#define SPINEL_HEADER_IID_BROADCAST OPENTHREAD_SPINEL_CONFIG_BROADCAST_IID
|
||||
@@ -849,7 +850,7 @@ protected:
|
||||
#if OPENTHREAD_CONFIG_NCP_DNSSD_ENABLE && OPENTHREAD_CONFIG_PLATFORM_DNSSD_ENABLE
|
||||
|
||||
template <typename DnssdObjType>
|
||||
void DnssdUpdate(const DnssdObjType *Obj,
|
||||
void DnssdUpdate(const DnssdObjType *aObj,
|
||||
otPlatDnssdRequestId aRequestId,
|
||||
otPlatDnssdRegisterCallback aCallback,
|
||||
bool aRegister)
|
||||
@@ -858,12 +859,11 @@ protected:
|
||||
uint8_t header = SPINEL_HEADER_FLAG | SPINEL_HEADER_TX_NOTIFICATION_IID;
|
||||
spinel_command_t cmd = aRegister ? SPINEL_CMD_PROP_VALUE_INSERTED : SPINEL_CMD_PROP_VALUE_REMOVED;
|
||||
|
||||
VerifyOrExit(aObj != nullptr, error = OT_ERROR_INVALID_ARGS);
|
||||
VerifyOrExit(mDnssdState == OT_PLAT_DNSSD_READY, error = OT_ERROR_INVALID_STATE);
|
||||
|
||||
SuccessOrExit(error = mEncoder.BeginFrame(header, cmd));
|
||||
SuccessOrExit(error = EncodeDnssd(Obj));
|
||||
SuccessOrExit(error = mEncoder.WriteUint32(aRequestId));
|
||||
SuccessOrExit(error = mEncoder.WriteData(reinterpret_cast<const uint8_t *>(&aCallback), sizeof(aCallback)));
|
||||
SuccessOrExit(error = Spinel::EncodeDnssd(mEncoder, *aObj, aRequestId, aCallback));
|
||||
SuccessOrExit(error = mEncoder.EndFrame());
|
||||
|
||||
exit:
|
||||
@@ -873,11 +873,9 @@ protected:
|
||||
}
|
||||
}
|
||||
|
||||
template <typename DnssdObjType> otError EncodeDnssd(const DnssdObjType *aObj);
|
||||
|
||||
otPlatDnssdState mDnssdState;
|
||||
#endif
|
||||
#endif
|
||||
#endif // OPENTHREAD_CONFIG_NCP_DNSSD_ENABLE && OPENTHREAD_CONFIG_PLATFORM_DNSSD_ENABLE
|
||||
#endif // OPENTHREAD_FTD
|
||||
|
||||
#if OPENTHREAD_CONFIG_DIAG_ENABLE
|
||||
char *mDiagOutput;
|
||||
|
||||
@@ -1566,66 +1566,6 @@ exit:
|
||||
|
||||
#if OPENTHREAD_CONFIG_NCP_DNSSD_ENABLE && OPENTHREAD_CONFIG_PLATFORM_DNSSD_ENABLE
|
||||
|
||||
template <> otError NcpBase::EncodeDnssd<otPlatDnssdHost>(const otPlatDnssdHost *aHost)
|
||||
{
|
||||
otError error = OT_ERROR_NONE;
|
||||
|
||||
SuccessOrExit(error = mEncoder.WriteUintPacked(SPINEL_PROP_DNSSD_HOST));
|
||||
SuccessOrExit(error = mEncoder.WriteUtf8(aHost->mHostName == nullptr ? "" : aHost->mHostName));
|
||||
SuccessOrExit(error = mEncoder.WriteUint16(aHost->mAddressesLength));
|
||||
for (uint8_t i = 0; i < aHost->mAddressesLength; i++)
|
||||
{
|
||||
SuccessOrExit(error = mEncoder.WriteIp6Address(aHost->mAddresses[i]));
|
||||
}
|
||||
|
||||
exit:
|
||||
return error;
|
||||
}
|
||||
|
||||
template <> otError NcpBase::EncodeDnssd<otPlatDnssdService>(const otPlatDnssdService *aService)
|
||||
{
|
||||
otError error = OT_ERROR_NONE;
|
||||
|
||||
SuccessOrExit(error = mEncoder.WriteUintPacked(SPINEL_PROP_DNSSD_SERVICE));
|
||||
SuccessOrExit(error = mEncoder.WriteUtf8(aService->mHostName == nullptr ? "" : aService->mHostName));
|
||||
SuccessOrExit(error = mEncoder.WriteUtf8(aService->mServiceInstance == nullptr ? "" : aService->mServiceInstance));
|
||||
SuccessOrExit(error = mEncoder.WriteUtf8(aService->mServiceType == nullptr ? "" : aService->mServiceType));
|
||||
SuccessOrExit(error = mEncoder.OpenStruct());
|
||||
for (uint8_t i = 0; i < aService->mSubTypeLabelsLength; i++)
|
||||
{
|
||||
SuccessOrExit(error = mEncoder.WriteUtf8(aService->mSubTypeLabels[i]));
|
||||
}
|
||||
SuccessOrExit(error = mEncoder.CloseStruct());
|
||||
SuccessOrExit(error = mEncoder.WriteDataWithLen(aService->mTxtData, aService->mTxtDataLength));
|
||||
SuccessOrExit(error = mEncoder.WriteUint16(aService->mPort));
|
||||
SuccessOrExit(error = mEncoder.WriteUint16(aService->mPriority));
|
||||
SuccessOrExit(error = mEncoder.WriteUint16(aService->mWeight));
|
||||
SuccessOrExit(error = mEncoder.WriteUint16(aService->mTtl));
|
||||
|
||||
exit:
|
||||
return error;
|
||||
}
|
||||
|
||||
template <> otError NcpBase::EncodeDnssd<otPlatDnssdKey>(const otPlatDnssdKey *aKey)
|
||||
{
|
||||
otError error = OT_ERROR_NONE;
|
||||
|
||||
SuccessOrExit(error = mEncoder.WriteUintPacked(SPINEL_PROP_DNSSD_KEY_RECORD));
|
||||
SuccessOrExit(error = mEncoder.WriteUtf8(aKey->mName == nullptr ? "" : aKey->mName));
|
||||
SuccessOrExit(error = mEncoder.OpenStruct());
|
||||
if (aKey->mServiceType != nullptr)
|
||||
{
|
||||
mEncoder.WriteUtf8(aKey->mServiceType);
|
||||
}
|
||||
SuccessOrExit(error = mEncoder.CloseStruct());
|
||||
SuccessOrExit(error = mEncoder.WriteDataWithLen(aKey->mKeyData, aKey->mKeyDataLength));
|
||||
SuccessOrExit(error = mEncoder.WriteUint16(aKey->mClass));
|
||||
SuccessOrExit(error = mEncoder.WriteUint16(aKey->mTtl));
|
||||
|
||||
exit:
|
||||
return error;
|
||||
}
|
||||
|
||||
void NcpBase::DnssdRegisterHost(const otPlatDnssdHost *aHost,
|
||||
otPlatDnssdRequestId aRequestId,
|
||||
otPlatDnssdRegisterCallback aCallback)
|
||||
|
||||
@@ -251,6 +251,7 @@ ot_unit_test(smart_ptrs)
|
||||
ot_unit_test(spinel_buffer)
|
||||
ot_unit_test(spinel_decoder)
|
||||
ot_unit_test(spinel_encoder)
|
||||
ot_unit_test(spinel_prop_codec)
|
||||
ot_unit_test(srp_adv_proxy)
|
||||
ot_unit_test(srp_server)
|
||||
ot_unit_test(string)
|
||||
|
||||
@@ -0,0 +1,191 @@
|
||||
/*
|
||||
* Copyright (c) 2024, The OpenThread Authors.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the copyright holder nor the
|
||||
* names of its contributors may be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
|
||||
#include "test_util.hpp"
|
||||
#include "lib/spinel/spinel_prop_codec.hpp"
|
||||
|
||||
namespace ot {
|
||||
namespace Spinel {
|
||||
|
||||
static void DnssdFakeCallback(otInstance *aInstance, otPlatDnssdRequestId aRequestId, otError aError)
|
||||
{
|
||||
OT_UNUSED_VARIABLE(aInstance);
|
||||
OT_UNUSED_VARIABLE(aRequestId);
|
||||
OT_UNUSED_VARIABLE(aError);
|
||||
}
|
||||
|
||||
void TestDnssd(void)
|
||||
{
|
||||
constexpr uint16_t kMaxSpinelBufferSize = 2048;
|
||||
uint8_t buf[kMaxSpinelBufferSize];
|
||||
uint16_t len;
|
||||
Spinel::Buffer ncpBuffer(buf, kMaxSpinelBufferSize);
|
||||
Spinel::Encoder encoder(ncpBuffer);
|
||||
Spinel::Decoder decoder;
|
||||
uint8_t header;
|
||||
unsigned int command;
|
||||
spinel_prop_key_t propKey;
|
||||
otError error = OT_ERROR_NONE;
|
||||
|
||||
// Test DnssdHost encoding and decoding
|
||||
otPlatDnssdHost dnssdHostEncode;
|
||||
otPlatDnssdHost dnssdHostDecode;
|
||||
otIp6Address dnssdHostAddrs[] = {
|
||||
{0xfd, 0x2a, 0xc3, 0x0c, 0x87, 0xd3, 0x00, 0x01, 0xed, 0x1c, 0x0c, 0x91, 0xcc, 0xb6, 0x57, 0x8b},
|
||||
};
|
||||
otPlatDnssdRequestId requestId;
|
||||
const uint8_t *callbackData;
|
||||
uint16_t callbackDataLen;
|
||||
dnssdHostEncode.mHostName = "ot-host1";
|
||||
dnssdHostEncode.mAddresses = dnssdHostAddrs;
|
||||
dnssdHostEncode.mAddressesLength = 1;
|
||||
|
||||
SuccessOrQuit(error = encoder.BeginFrame(SPINEL_HEADER_FLAG, SPINEL_CMD_PROP_VALUE_INSERTED));
|
||||
SuccessOrQuit(error = EncodeDnssd(encoder, dnssdHostEncode, 1 /* aRequestId */, DnssdFakeCallback));
|
||||
SuccessOrQuit(error = encoder.EndFrame());
|
||||
SuccessOrQuit(ncpBuffer.OutFrameBegin());
|
||||
len = ncpBuffer.OutFrameGetLength();
|
||||
VerifyOrQuit(ncpBuffer.OutFrameRead(len, buf) == len);
|
||||
|
||||
decoder.Init(buf, len);
|
||||
SuccessOrQuit(error = decoder.ReadUint8(header));
|
||||
VerifyOrQuit(header == SPINEL_HEADER_FLAG);
|
||||
SuccessOrQuit(error = decoder.ReadUintPacked(command));
|
||||
VerifyOrQuit(command == SPINEL_CMD_PROP_VALUE_INSERTED);
|
||||
SuccessOrQuit(error = decoder.ReadUintPacked(propKey));
|
||||
VerifyOrQuit(propKey == SPINEL_PROP_DNSSD_HOST);
|
||||
SuccessOrQuit(error = DecodeDnssdHost(decoder, dnssdHostDecode, requestId, callbackData, callbackDataLen));
|
||||
VerifyOrQuit(strcmp(dnssdHostDecode.mHostName, dnssdHostEncode.mHostName) == 0);
|
||||
VerifyOrQuit(dnssdHostDecode.mAddressesLength == dnssdHostEncode.mAddressesLength);
|
||||
VerifyOrQuit(memcmp(dnssdHostDecode.mAddresses, dnssdHostEncode.mAddresses, sizeof(dnssdHostEncode.mAddresses)) ==
|
||||
0);
|
||||
VerifyOrQuit(requestId == 1);
|
||||
VerifyOrQuit(callbackDataLen == sizeof(otPlatDnssdRegisterCallback));
|
||||
VerifyOrQuit(*reinterpret_cast<const otPlatDnssdRegisterCallback *>(callbackData) == DnssdFakeCallback);
|
||||
|
||||
// Test DnssdService encoding and decoding
|
||||
otPlatDnssdService dnssdServiceEncode;
|
||||
otPlatDnssdService dnssdServiceDecode;
|
||||
const char *dnssdSubType[] = {"cat", "dog", "fish"};
|
||||
const uint8_t txtData[] = {0x01, 0x02, 0x03, 0x04};
|
||||
dnssdServiceEncode.mHostName = "ot-host2";
|
||||
dnssdServiceEncode.mServiceInstance = "ot-service";
|
||||
dnssdServiceEncode.mServiceType = "";
|
||||
dnssdServiceEncode.mSubTypeLabels = dnssdSubType;
|
||||
dnssdServiceEncode.mSubTypeLabelsLength = sizeof(dnssdSubType) / sizeof(dnssdSubType[0]);
|
||||
dnssdServiceEncode.mTxtData = txtData;
|
||||
dnssdServiceEncode.mTxtDataLength = sizeof(txtData);
|
||||
dnssdServiceEncode.mPort = 1234;
|
||||
dnssdServiceEncode.mPriority = 567;
|
||||
dnssdServiceEncode.mWeight = 890;
|
||||
dnssdServiceEncode.mTtl = 9999;
|
||||
|
||||
ncpBuffer.Clear();
|
||||
SuccessOrQuit(error = encoder.BeginFrame(SPINEL_HEADER_FLAG, SPINEL_CMD_PROP_VALUE_INSERTED));
|
||||
SuccessOrQuit(error = EncodeDnssd(encoder, dnssdServiceEncode, 2 /* aRequestId */, DnssdFakeCallback));
|
||||
SuccessOrQuit(error = encoder.EndFrame());
|
||||
SuccessOrQuit(ncpBuffer.OutFrameBegin());
|
||||
len = ncpBuffer.OutFrameGetLength();
|
||||
VerifyOrQuit(ncpBuffer.OutFrameRead(len, buf) == len);
|
||||
|
||||
decoder.Init(buf, len);
|
||||
SuccessOrQuit(error = decoder.ReadUint8(header));
|
||||
VerifyOrQuit(header == SPINEL_HEADER_FLAG);
|
||||
SuccessOrQuit(error = decoder.ReadUintPacked(command));
|
||||
VerifyOrQuit(command == SPINEL_CMD_PROP_VALUE_INSERTED);
|
||||
SuccessOrQuit(error = decoder.ReadUintPacked(propKey));
|
||||
VerifyOrQuit(propKey == SPINEL_PROP_DNSSD_SERVICE);
|
||||
const char *dnssdSubTypeDecode[] = {nullptr, nullptr, nullptr};
|
||||
uint16_t dnssdSubTypeCount = sizeof(dnssdSubTypeDecode) / sizeof(dnssdSubTypeDecode[0]);
|
||||
SuccessOrQuit(error = DecodeDnssdService(decoder, dnssdServiceDecode, dnssdSubTypeDecode, dnssdSubTypeCount,
|
||||
requestId, callbackData, callbackDataLen));
|
||||
VerifyOrQuit(strcmp(dnssdServiceDecode.mHostName, dnssdServiceEncode.mHostName) == 0);
|
||||
VerifyOrQuit(strcmp(dnssdServiceDecode.mServiceInstance, dnssdServiceEncode.mServiceInstance) == 0);
|
||||
VerifyOrQuit(strcmp(dnssdServiceDecode.mServiceType, dnssdServiceEncode.mServiceType) == 0);
|
||||
VerifyOrQuit(dnssdServiceDecode.mPort == dnssdServiceEncode.mPort);
|
||||
VerifyOrQuit(dnssdServiceDecode.mPriority == dnssdServiceEncode.mPriority);
|
||||
VerifyOrQuit(dnssdServiceDecode.mWeight == dnssdServiceEncode.mWeight);
|
||||
VerifyOrQuit(dnssdServiceDecode.mTtl == dnssdServiceEncode.mTtl);
|
||||
VerifyOrQuit(dnssdSubTypeCount == sizeof(dnssdSubType) / sizeof(dnssdSubType[0]));
|
||||
for (uint16_t i = 0; i < dnssdSubTypeCount; i++)
|
||||
{
|
||||
VerifyOrQuit(strcmp(dnssdSubTypeDecode[i], dnssdSubType[i]) == 0);
|
||||
}
|
||||
VerifyOrQuit(requestId == 2);
|
||||
VerifyOrQuit(callbackDataLen == sizeof(otPlatDnssdRegisterCallback));
|
||||
VerifyOrQuit(*reinterpret_cast<const otPlatDnssdRegisterCallback *>(callbackData) == DnssdFakeCallback);
|
||||
|
||||
// Test DnssdKey encoding and decoding
|
||||
otPlatDnssdKey dnssdKeyEncode;
|
||||
otPlatDnssdKey dnssdKeyDecode;
|
||||
const uint8_t keyData[] = {0x05, 0x06, 0x07, 0x08};
|
||||
dnssdKeyEncode.mName = "ot-key";
|
||||
dnssdKeyEncode.mServiceType = nullptr;
|
||||
dnssdKeyEncode.mKeyData = keyData;
|
||||
dnssdKeyEncode.mKeyDataLength = sizeof(keyData);
|
||||
dnssdKeyEncode.mClass = 123;
|
||||
dnssdKeyEncode.mTtl = 888;
|
||||
|
||||
ncpBuffer.Clear();
|
||||
SuccessOrQuit(error = encoder.BeginFrame(SPINEL_HEADER_FLAG, SPINEL_CMD_PROP_VALUE_INSERTED));
|
||||
SuccessOrQuit(error = EncodeDnssd(encoder, dnssdKeyEncode, 3 /* aRequestId */, DnssdFakeCallback));
|
||||
SuccessOrQuit(error = encoder.EndFrame());
|
||||
SuccessOrQuit(ncpBuffer.OutFrameBegin());
|
||||
len = ncpBuffer.OutFrameGetLength();
|
||||
VerifyOrQuit(ncpBuffer.OutFrameRead(len, buf) == len);
|
||||
|
||||
decoder.Init(buf, len);
|
||||
SuccessOrQuit(error = decoder.ReadUint8(header));
|
||||
VerifyOrQuit(header == SPINEL_HEADER_FLAG);
|
||||
SuccessOrQuit(error = decoder.ReadUintPacked(command));
|
||||
VerifyOrQuit(command == SPINEL_CMD_PROP_VALUE_INSERTED);
|
||||
SuccessOrQuit(error = decoder.ReadUintPacked(propKey));
|
||||
VerifyOrQuit(propKey == SPINEL_PROP_DNSSD_KEY_RECORD);
|
||||
SuccessOrQuit(error = DecodeDnssdKey(decoder, dnssdKeyDecode, requestId, callbackData, callbackDataLen));
|
||||
VerifyOrQuit(strcmp(dnssdKeyDecode.mName, dnssdKeyEncode.mName) == 0);
|
||||
VerifyOrQuit(dnssdKeyDecode.mServiceType == dnssdKeyEncode.mServiceType);
|
||||
VerifyOrQuit(dnssdKeyDecode.mKeyDataLength == dnssdKeyEncode.mKeyDataLength);
|
||||
VerifyOrQuit(memcmp(dnssdKeyDecode.mKeyData, dnssdKeyEncode.mKeyData, dnssdKeyDecode.mKeyDataLength) == 0);
|
||||
VerifyOrQuit(dnssdKeyDecode.mClass == dnssdKeyEncode.mClass);
|
||||
VerifyOrQuit(dnssdKeyDecode.mTtl == dnssdKeyEncode.mTtl);
|
||||
VerifyOrQuit(requestId == 3);
|
||||
VerifyOrQuit(callbackDataLen == sizeof(otPlatDnssdRegisterCallback));
|
||||
VerifyOrQuit(*reinterpret_cast<const otPlatDnssdRegisterCallback *>(callbackData) == DnssdFakeCallback);
|
||||
}
|
||||
|
||||
} // namespace Spinel
|
||||
} // namespace ot
|
||||
|
||||
int main(void)
|
||||
{
|
||||
ot::Spinel::TestDnssd();
|
||||
printf("\nAll tests passed.\n");
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user