[ncp] implement ncp dnssd update service and key record (#10909)

This commit is contained in:
Li Cao
2024-11-14 18:34:17 -08:00
committed by GitHub
parent 0551005887
commit 3702c88a76
6 changed files with 206 additions and 15 deletions
+2
View File
@@ -1416,6 +1416,8 @@ const char *spinel_prop_key_to_cstr(spinel_prop_key_t prop_key)
{SPINEL_PROP_DNSSD_STATE, "DNSSD_STATE"},
{SPINEL_PROP_DNSSD_REQUEST_RESULT, "DNSSD_REQUEST_RESULT"},
{SPINEL_PROP_DNSSD_HOST, "DNSSD_HOST"},
{SPINEL_PROP_DNSSD_SERVICE, "DNSSD_SERVICE"},
{SPINEL_PROP_DNSSD_KEY_RECORD, "DNSSD_KEY_RECORD"},
{SPINEL_PROP_PARENT_RESPONSE_INFO, "PARENT_RESPONSE_INFO"},
{SPINEL_PROP_SLAAC_ENABLED, "SLAAC_ENABLED"},
{SPINEL_PROP_SUPPORTED_RADIO_LINKS, "SUPPORTED_RADIO_LINKS"},
+36
View File
@@ -4836,6 +4836,42 @@ enum
*/
SPINEL_PROP_DNSSD_HOST = SPINEL_PROP_DNSSD__BEGIN + 3,
/// DNS-SD Service
/**
* Format `UUUt(A(U))dSSSSLD`: Inserted/Removed
*
* `U` : The host name (does not include domain name).
* `U` : The service instance name label (not the full name).
* `U` : The service type (e.g., "_mt._udp", does not include domain name).
* `t(A(U))` : Array of sub-type labels (can be empty array if no label).
* `d` : Encoded TXT data bytes.
* `S` : The service port number.
* `S` : The service priority.
* `S` : The service weight.
* `S` : The service TTL in seconds.
* `L` : The Dnssd Request ID.
* `D` : The context of the request. (A pointer to the callback for the request)
*
* NCP uses this property to register/unregister a DNS-SD service.
*/
SPINEL_PROP_DNSSD_SERVICE = SPINEL_PROP_DNSSD__BEGIN + 4,
/// DNS-SD Key Record
/**
* Format `Ut(U)dSSLD`: Inserted/Removed
*
* `U` : A host or a service instance name (does not include domain name).
* `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 Dnssd Request ID.
* `D` : The context of the request. (A pointer to the callback for the request)
*
* NCP uses this property to register/unregister a DNS-SD key record.
*/
SPINEL_PROP_DNSSD_KEY_RECORD = SPINEL_PROP_DNSSD__BEGIN + 5,
SPINEL_PROP_DNSSD__END = 0x950,
SPINEL_PROP_NEST__BEGIN = 0x3BC0,
+44
View File
@@ -268,6 +268,50 @@ public:
otPlatDnssdRequestId aRequestId,
otPlatDnssdRegisterCallback aCallback);
/**
* Registers or updates a service on the infrastructure network's DNS-SD module (on host).
*
* @param[in] aService Information about the service to register.
* @param[in] aRequestId The ID associated with this request.
* @param[in] aCallback The callback function pointer to report the outcome (may be NULL if no callback needed).
*/
void DnssdRegisterService(const otPlatDnssdService *aService,
otPlatDnssdRequestId aRequestId,
otPlatDnssdRegisterCallback aCallback);
/**
* Unregisters a service on the infrastructure network's DNS-SD module (on host).
*
* @param[in] aService Information about the service to unregister.
* @param[in] aRequestId The ID associated with this request.
* @param[in] aCallback The callback function pointer to report the outcome (may be NULL if no callback needed).
*/
void DnssdUnregisterService(const otPlatDnssdService *aService,
otPlatDnssdRequestId aRequestId,
otPlatDnssdRegisterCallback aCallback);
/**
* Registers or updates a key record on the infrastructure network's DNS-SD module (on host).
*
* @param[in] aKey Information about the key record to register.
* @param[in] aRequestId The ID associated with this request.
* @param[in] aCallback The callback function pointer to report the outcome (may be NULL if no callback needed).
*/
void DnssdRegisterKey(const otPlatDnssdKey *aKey,
otPlatDnssdRequestId aRequestId,
otPlatDnssdRegisterCallback aCallback);
/**
* Unregisters a key record on the infrastructure network's DNS-SD module (on host).
*
* @param[in] aKey Information about the key record to register.
* @param[in] aRequestId The ID associated with this request.
* @param[in] aCallback The callback function pointer to report the outcome (may be NULL if no callback needed).
*/
void DnssdUnregisterKey(const otPlatDnssdKey *aKey,
otPlatDnssdRequestId aRequestId,
otPlatDnssdRegisterCallback aCallback);
/**
* Gets the Dnssd state.
*
+72
View File
@@ -1582,6 +1582,50 @@ 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)
@@ -1596,6 +1640,34 @@ void NcpBase::DnssdUnregisterHost(const otPlatDnssdHost *aHost,
DnssdUpdate(aHost, aRequestId, aCallback, /* aRegister */ false);
}
void NcpBase::DnssdRegisterService(const otPlatDnssdService *aService,
otPlatDnssdRequestId aRequestId,
otPlatDnssdRegisterCallback aCallback)
{
DnssdUpdate(aService, aRequestId, aCallback, /* aRegister */ true);
}
void NcpBase::DnssdUnregisterService(const otPlatDnssdService *aService,
otPlatDnssdRequestId aRequestId,
otPlatDnssdRegisterCallback aCallback)
{
DnssdUpdate(aService, aRequestId, aCallback, /* aRegister */ false);
}
void NcpBase::DnssdRegisterKey(const otPlatDnssdKey *aKey,
otPlatDnssdRequestId aRequestId,
otPlatDnssdRegisterCallback aCallback)
{
DnssdUpdate(aKey, aRequestId, aCallback, /* aRegister */ true);
}
void NcpBase::DnssdUnregisterKey(const otPlatDnssdKey *aKey,
otPlatDnssdRequestId aRequestId,
otPlatDnssdRegisterCallback aCallback)
{
DnssdUpdate(aKey, aRequestId, aCallback, /* aRegister */ false);
}
otPlatDnssdState NcpBase::DnssdGetState(void) { return mDnssdState; }
template <> otError NcpBase::HandlePropertySet<SPINEL_PROP_DNSSD_STATE>(void)
+12 -12
View File
@@ -45,9 +45,9 @@ void otPlatDnssdRegisterService(otInstance *aInstance,
otPlatDnssdRegisterCallback aCallback)
{
OT_UNUSED_VARIABLE(aInstance);
OT_UNUSED_VARIABLE(aService);
OT_UNUSED_VARIABLE(aRequestId);
OT_UNUSED_VARIABLE(aCallback);
ot::Ncp::NcpBase *ncp = ot::Ncp::NcpBase::GetNcpInstance();
ncp->DnssdRegisterService(aService, aRequestId, aCallback);
}
void otPlatDnssdUnregisterService(otInstance *aInstance,
@@ -56,9 +56,9 @@ void otPlatDnssdUnregisterService(otInstance *aInstance,
otPlatDnssdRegisterCallback aCallback)
{
OT_UNUSED_VARIABLE(aInstance);
OT_UNUSED_VARIABLE(aService);
OT_UNUSED_VARIABLE(aRequestId);
OT_UNUSED_VARIABLE(aCallback);
ot::Ncp::NcpBase *ncp = ot::Ncp::NcpBase::GetNcpInstance();
ncp->DnssdUnregisterService(aService, aRequestId, aCallback);
}
void otPlatDnssdRegisterHost(otInstance *aInstance,
@@ -89,9 +89,9 @@ void otPlatDnssdRegisterKey(otInstance *aInstance,
otPlatDnssdRegisterCallback aCallback)
{
OT_UNUSED_VARIABLE(aInstance);
OT_UNUSED_VARIABLE(aKey);
OT_UNUSED_VARIABLE(aRequestId);
OT_UNUSED_VARIABLE(aCallback);
ot::Ncp::NcpBase *ncp = ot::Ncp::NcpBase::GetNcpInstance();
ncp->DnssdRegisterKey(aKey, aRequestId, aCallback);
}
void otPlatDnssdUnregisterKey(otInstance *aInstance,
@@ -100,9 +100,9 @@ void otPlatDnssdUnregisterKey(otInstance *aInstance,
otPlatDnssdRegisterCallback aCallback)
{
OT_UNUSED_VARIABLE(aInstance);
OT_UNUSED_VARIABLE(aKey);
OT_UNUSED_VARIABLE(aRequestId);
OT_UNUSED_VARIABLE(aCallback);
ot::Ncp::NcpBase *ncp = ot::Ncp::NcpBase::GetNcpInstance();
ncp->DnssdUnregisterKey(aKey, aRequestId, aCallback);
}
void otPlatDnssdStartBrowser(otInstance *aInstance, const otPlatDnssdBrowser *aBrowser)
+40 -3
View File
@@ -117,9 +117,11 @@ void TestNcpDnssdRegistrations(void)
Instance *instance = static_cast<Instance *>(testInitInstance());
Ncp::NcpBase ncpBase(instance);
uint8_t recvBuf[kMaxSpinelBufferSize];
uint16_t recvLen;
otPlatDnssdHost dnssdHost;
uint8_t recvBuf[kMaxSpinelBufferSize];
uint16_t recvLen;
otPlatDnssdHost dnssdHost;
otPlatDnssdService dnssdService;
otPlatDnssdKey dnssdKey;
sRequestId = 0; // Use 0 to indicate `sRequestId` hasn't been set.
@@ -139,6 +141,41 @@ void TestNcpDnssdRegistrations(void)
ncpBase.HandleReceive(recvBuf, recvLen);
VerifyOrQuit(sRequestId == 2);
VerifyOrQuit(sError == OT_ERROR_NOT_FOUND);
dnssdService.mHostName = "test-service";
dnssdService.mServiceInstance = nullptr;
dnssdService.mServiceType = nullptr;
dnssdService.mSubTypeLabelsLength = 0;
dnssdService.mPort = 1234;
dnssdService.mTxtDataLength = 0;
otPlatDnssdRegisterService(instance, &dnssdService, 3 /* aRequestId */, TestPlatDnssdRegisterCallback);
SuccessOrQuit(GenerateSpinelDnssdRequestResultFrame(3 /* aRequestId */, OT_ERROR_NONE, recvBuf, recvLen));
ncpBase.HandleReceive(recvBuf, recvLen);
VerifyOrQuit(sRequestId == 3);
VerifyOrQuit(sError == OT_ERROR_NONE);
sRequestId = 0;
sError = OT_ERROR_FAILED;
otPlatDnssdUnregisterService(instance, &dnssdService, 3 /* aRequestId */, TestPlatDnssdRegisterCallback);
ncpBase.HandleReceive(recvBuf, recvLen);
VerifyOrQuit(sRequestId == 3);
VerifyOrQuit(sError == OT_ERROR_NONE);
dnssdKey.mName = "test-key";
dnssdKey.mServiceType = "someType";
dnssdKey.mKeyDataLength = 0;
otPlatDnssdRegisterKey(instance, &dnssdKey, 4 /* aRequestId */, TestPlatDnssdRegisterCallback);
SuccessOrQuit(GenerateSpinelDnssdRequestResultFrame(4 /* aRequestId */, OT_ERROR_NONE, recvBuf, recvLen));
ncpBase.HandleReceive(recvBuf, recvLen);
VerifyOrQuit(sRequestId == 4);
VerifyOrQuit(sError == OT_ERROR_NONE);
sRequestId = 0;
sError = OT_ERROR_FAILED;
otPlatDnssdUnregisterKey(instance, &dnssdKey, 4 /* aRequestId */, TestPlatDnssdRegisterCallback);
ncpBase.HandleReceive(recvBuf, recvLen);
VerifyOrQuit(sRequestId == 4);
VerifyOrQuit(sError == OT_ERROR_NONE);
}
} // namespace ot