[ncp] implement ncp version of otPlatDnssdRegisterHost (#10893)

This commit implements ncp version of otPlatDnssdRegisterHost.
1. move the position of `SPINEL_PROP_DNSSD_STATE` in `spinel.h`
   because it was by mistake put within the range of
   `SPINEL_PROP_SRP_SERVER`. The value is unchanged.
2. A template method `DnssdUpdate` is added because the same process
   works also for DNSSD service and key record which will be added in
   following PRs.
This commit is contained in:
Li Cao
2024-11-07 09:46:59 +01:00
committed by GitHub
parent e43120d903
commit d0998beca4
7 changed files with 206 additions and 17 deletions
+61
View File
@@ -42,6 +42,8 @@
namespace ot {
constexpr uint16_t kMaxSpinelBufferSize = 2048;
static uint32_t sRequestId;
static uint8_t sError;
static otError GenerateSpinelDnssdSetStateFrame(otPlatDnssdState aState, uint8_t *aBuf, uint16_t &aLen)
{
@@ -63,6 +65,35 @@ exit:
return error;
}
static void TestPlatDnssdRegisterCallback(otInstance *aInstance, otPlatDnssdRequestId aRequestId, otError aError)
{
sRequestId = aRequestId;
sError = aError;
}
static otError GenerateSpinelDnssdRequestResultFrame(uint32_t aRequestId, otError aError, uint8_t *aBuf, uint16_t &aLen)
{
otError error = OT_ERROR_NONE;
uint8_t buf[kMaxSpinelBufferSize];
Spinel::Buffer ncpBuffer(buf, kMaxSpinelBufferSize);
Spinel::Encoder encoder(ncpBuffer);
otPlatDnssdRegisterCallback callback = &TestPlatDnssdRegisterCallback;
uint8_t header = SPINEL_HEADER_FLAG | 0 /* Iid */ | 1 /* Tid */;
SuccessOrExit(error = encoder.BeginFrame(header, SPINEL_CMD_PROP_VALUE_SET, SPINEL_PROP_DNSSD_REQUEST_RESULT));
SuccessOrExit(error = encoder.WriteUint8(aError));
SuccessOrExit(error = encoder.WriteUint32(aRequestId));
SuccessOrExit(error = encoder.WriteData(reinterpret_cast<const uint8_t *>(&callback), sizeof(callback)));
SuccessOrExit(error = encoder.EndFrame());
SuccessOrExit(ncpBuffer.OutFrameBegin());
aLen = ncpBuffer.OutFrameGetLength();
VerifyOrExit(ncpBuffer.OutFrameRead(aLen, aBuf) == aLen, error = OT_ERROR_FAILED);
exit:
return error;
}
void TestNcpDnssdGetState(void)
{
Instance *instance = static_cast<Instance *>(testInitInstance());
@@ -81,6 +112,35 @@ void TestNcpDnssdGetState(void)
VerifyOrQuit(dnssdState == OT_PLAT_DNSSD_READY);
}
void TestNcpDnssdRegistrations(void)
{
Instance *instance = static_cast<Instance *>(testInitInstance());
Ncp::NcpBase ncpBase(instance);
uint8_t recvBuf[kMaxSpinelBufferSize];
uint16_t recvLen;
otPlatDnssdHost dnssdHost;
sRequestId = 0; // Use 0 to indicate `sRequestId` hasn't been set.
// Register a dnssd host when the dnssd state is DISABLED
dnssdHost.mHostName = "ot-test";
dnssdHost.mAddressesLength = 0;
otPlatDnssdRegisterHost(instance, &dnssdHost, 1 /* aRequestId */, TestPlatDnssdRegisterCallback);
VerifyOrQuit(sRequestId == 1);
VerifyOrQuit(sError == OT_ERROR_INVALID_STATE);
// Set the dnssd state to READY
SuccessOrQuit(GenerateSpinelDnssdSetStateFrame(OT_PLAT_DNSSD_READY, recvBuf, recvLen));
ncpBase.HandleReceive(recvBuf, recvLen);
otPlatDnssdUnregisterHost(instance, &dnssdHost, 2 /* aRequestId */, TestPlatDnssdRegisterCallback);
SuccessOrQuit(GenerateSpinelDnssdRequestResultFrame(2 /* aRequestId */, OT_ERROR_NOT_FOUND, recvBuf, recvLen));
ncpBase.HandleReceive(recvBuf, recvLen);
VerifyOrQuit(sRequestId == 2);
VerifyOrQuit(sError == OT_ERROR_NOT_FOUND);
}
} // namespace ot
#endif // OPENTHREAD_CONFIG_NCP_DNSSD_ENABLE && OPENTHREAD_CONFIG_PLATFORM_DNSSD_ENABLE
@@ -89,6 +149,7 @@ int main(void)
{
#if OPENTHREAD_CONFIG_NCP_DNSSD_ENABLE && OPENTHREAD_CONFIG_PLATFORM_DNSSD_ENABLE
ot::TestNcpDnssdGetState();
ot::TestNcpDnssdRegistrations();
#endif
printf("All tests passed\n");
return 0;