From 58c2754b3e85b6e5981c6fbec03d23fb62235c59 Mon Sep 17 00:00:00 2001 From: j8xixo12 Date: Mon, 28 Nov 2022 19:13:04 +0800 Subject: [PATCH 1/4] [genio] correct dnssd api usage --- examples/platform/mt793x/link_wrapper.c | 7 + src/platform/mt793x/DnssdContexts.cpp | 80 +++++- src/platform/mt793x/DnssdImpl.cpp | 317 ++++++++++++++++-------- src/platform/mt793x/DnssdImpl.h | 26 +- 4 files changed, 327 insertions(+), 103 deletions(-) diff --git a/examples/platform/mt793x/link_wrapper.c b/examples/platform/mt793x/link_wrapper.c index 2b741940828196..3ef6f068974d72 100644 --- a/examples/platform/mt793x/link_wrapper.c +++ b/examples/platform/mt793x/link_wrapper.c @@ -58,6 +58,13 @@ void __assert_func(const char * file, int line, const char * func, const char * #include #include +void * __wrap__calloc_r(size_t nmemb, size_t size) { + void * p = pvPortCalloc(nmemb, size); + while(!p) + ; + return p; +} + void * __wrap__malloc_r(void * REENT, size_t size) { void * p = pvPortMalloc(size); diff --git a/src/platform/mt793x/DnssdContexts.cpp b/src/platform/mt793x/DnssdContexts.cpp index 2a1dec2de1176b..4a97fcf4238da2 100644 --- a/src/platform/mt793x/DnssdContexts.cpp +++ b/src/platform/mt793x/DnssdContexts.cpp @@ -33,6 +33,7 @@ namespace { constexpr uint8_t kDnssdKeyMaxSize = 32; constexpr uint8_t kDnssdTxtRecordMaxEntries = 20; + std::string GetHostNameWithoutDomain(const char * hostnameWithDomain) { std::string hostname(hostnameWithDomain); @@ -45,6 +46,7 @@ std::string GetHostNameWithoutDomain(const char * hostnameWithDomain) return hostname; } + std::string GetFullTypeWithoutSubTypes(std::string fullType) { size_t position = fullType.find(","); @@ -56,6 +58,7 @@ std::string GetFullTypeWithoutSubTypes(std::string fullType) return fullType; } + void GetTextEntries(DnssdService & service, const unsigned char * data, uint16_t len) { uint16_t recordCount = TXTRecordGetCount(len, data); @@ -93,6 +96,7 @@ void GetTextEntries(DnssdService & service, const unsigned char * data, uint16_t } } + DNSServiceProtocol GetProtocol(const chip::Inet::IPAddressType & addressType) { #if INET_CONFIG_ENABLE_IPV4 @@ -112,12 +116,11 @@ DNSServiceProtocol GetProtocol(const chip::Inet::IPAddressType & addressType) return kDNSServiceProtocol_IPv6; #endif } - } // namespace + namespace chip { namespace Dnssd { - CHIP_ERROR GenericContext::Finalize(DNSServiceErrorType err) { if (MdnsContexts::GetInstance().Has(this) == CHIP_NO_ERROR) @@ -139,6 +142,56 @@ CHIP_ERROR GenericContext::Finalize(DNSServiceErrorType err) return Error::ToChipError(err); } + +RegisterContext::RegisterContext(const char * sType, const char * instanceName, DnssdPublishCallback cb, void * cbContext) +{ + type = ContextType::Register; + context = cbContext; + callback = cb; + + mType = sType; + mInstanceName = instanceName; +} + + +void RegisterContext::DispatchFailure(DNSServiceErrorType err) +{ + ChipLogError(Discovery, "Mdns: Register failure (%s)", Error::ToString(err)); + callback(context, nullptr, nullptr, Error::ToChipError(err)); + MdnsContexts::GetInstance().Remove(this); +} + + +void RegisterContext::DispatchSuccess() +{ + std::string typeWithoutSubTypes = GetFullTypeWithoutSubTypes(mType); + callback(context, typeWithoutSubTypes.c_str(), mInstanceName.c_str(), CHIP_NO_ERROR); + + // Once a service has been properly published it is normally unreachable because the hostname has not yet been + // registered against the dns daemon. Register the records mapping the hostname to our IP. + // mHostNameRegistrar.Register(); +} + + +CHIP_ERROR MdnsContexts::GetRegisterContextOfType(const char * type, RegisterContext ** context) +{ + bool found = false; + std::vector::iterator iter; + + for (iter = mContexts.begin(); iter != mContexts.end(); iter++) + { + if ((*iter)->type == ContextType::Register && (static_cast(*iter))->matches(type)) + { + *context = static_cast(*iter); + found = true; + break; + } + } + + return found ? CHIP_NO_ERROR : CHIP_ERROR_KEY_NOT_FOUND; +} + + MdnsContexts::~MdnsContexts() { std::vector::const_iterator iter = mContexts.cbegin(); @@ -149,6 +202,7 @@ MdnsContexts::~MdnsContexts() } } + CHIP_ERROR MdnsContexts::Add(GenericContext * context, DNSServiceRef sdRef) { VerifyOrReturnError(context != nullptr, CHIP_ERROR_INVALID_ARGUMENT); @@ -165,6 +219,7 @@ CHIP_ERROR MdnsContexts::Add(GenericContext * context, DNSServiceRef sdRef) return CHIP_NO_ERROR; } + CHIP_ERROR MdnsContexts::Remove(GenericContext * context) { bool found = false; @@ -187,6 +242,7 @@ CHIP_ERROR MdnsContexts::Remove(GenericContext * context) return found ? CHIP_NO_ERROR : CHIP_ERROR_KEY_NOT_FOUND; } + CHIP_ERROR MdnsContexts::RemoveAllOfType(ContextType type) { bool found = false; @@ -208,6 +264,7 @@ CHIP_ERROR MdnsContexts::RemoveAllOfType(ContextType type) return found ? CHIP_NO_ERROR : CHIP_ERROR_KEY_NOT_FOUND; } + void MdnsContexts::Delete(GenericContext * context) { if (context->serviceRef != nullptr) @@ -217,6 +274,7 @@ void MdnsContexts::Delete(GenericContext * context) chip::Platform::Delete(context); } + CHIP_ERROR MdnsContexts::Has(GenericContext * context) { std::vector::iterator iter; @@ -232,6 +290,7 @@ CHIP_ERROR MdnsContexts::Has(GenericContext * context) return CHIP_ERROR_KEY_NOT_FOUND; } + GenericContext * MdnsContexts::GetBySockFd(int fd) { std::vector::iterator iter; @@ -247,6 +306,7 @@ GenericContext * MdnsContexts::GetBySockFd(int fd) return NULL; } + int MdnsContexts::GetSelectFd(fd_set * pSelectFd) { int maxFd = 0; @@ -266,6 +326,7 @@ int MdnsContexts::GetSelectFd(fd_set * pSelectFd) return maxFd; } + BrowseContext::BrowseContext(void * cbContext, DnssdBrowseCallback cb, DnssdServiceProtocol cbContextProtocol) { type = ContextType::Browse; @@ -275,6 +336,7 @@ BrowseContext::BrowseContext(void * cbContext, DnssdBrowseCallback cb, DnssdServ mSelectCount = 0; } + void BrowseContext::DispatchFailure(DNSServiceErrorType err) { ChipLogError(Discovery, "Mdns: Browse failure (%s)", Error::ToString(err)); @@ -282,12 +344,14 @@ void BrowseContext::DispatchFailure(DNSServiceErrorType err) MdnsContexts::GetInstance().Remove(this); } + void BrowseContext::DispatchSuccess() { callback(context, services.data(), services.size(), true, CHIP_NO_ERROR); MdnsContexts::GetInstance().Remove(this); } + ResolveContext::ResolveContext(void * cbContext, DnssdResolveCallback cb, chip::Inet::IPAddressType cbAddressType) { type = ContextType::Resolve; @@ -297,8 +361,10 @@ ResolveContext::ResolveContext(void * cbContext, DnssdResolveCallback cb, chip:: mSelectCount = 0; } + ResolveContext::~ResolveContext() {} + void ResolveContext::DispatchFailure(DNSServiceErrorType err) { ChipLogError(Discovery, "Mdns: Resolve failure (%s)", Error::ToString(err)); @@ -306,6 +372,7 @@ void ResolveContext::DispatchFailure(DNSServiceErrorType err) MdnsContexts::GetInstance().Remove(this); } + void ResolveContext::DispatchSuccess() { for (auto & interface : interfaces) @@ -326,6 +393,7 @@ void ResolveContext::DispatchSuccess() MdnsContexts::GetInstance().Remove(this); } + CHIP_ERROR ResolveContext::OnNewAddress(uint32_t interfaceId, const struct sockaddr * address) { CHIP_ERROR err = CHIP_NO_ERROR; @@ -367,6 +435,7 @@ CHIP_ERROR ResolveContext::OnNewAddress(uint32_t interfaceId, const struct socka return err; } + CHIP_ERROR ResolveContext::OnNewLocalOnlyAddress() { struct sockaddr_in sockaddr; @@ -381,6 +450,7 @@ CHIP_ERROR ResolveContext::OnNewLocalOnlyAddress() return OnNewAddress(kDNSServiceInterfaceIndexLocalOnly, reinterpret_cast(&sockaddr)); } + bool ResolveContext::HasAddress() { for (auto & interface : interfaces) @@ -394,6 +464,7 @@ bool ResolveContext::HasAddress() return false; } + void ResolveContext::OnNewInterface(uint32_t interfaceId, const char * fullname, const char * hostnameWithDomain, uint16_t port, uint16_t txtLen, const unsigned char * txtRecord) { @@ -463,17 +534,20 @@ void ResolveContext::OnNewInterface(uint32_t interfaceId, const char * fullname, interfaces.insert(std::make_pair(interfaceId, std::move(interface))); } + bool ResolveContext::HasInterface() { return interfaces.size(); } + InterfaceInfo::InterfaceInfo() { service.mTextEntrySize = 0; service.mTextEntries = nullptr; } + InterfaceInfo::InterfaceInfo(InterfaceInfo && other) : service(std::move(other.service)), addresses(std::move(other.addresses)), fullyQualifiedDomainName(std::move(other.fullyQualifiedDomainName)) @@ -484,6 +558,7 @@ InterfaceInfo::InterfaceInfo(InterfaceInfo && other) : other.service.mTextEntries = nullptr; } + InterfaceInfo::~InterfaceInfo() { if (service.mTextEntries == nullptr) @@ -500,6 +575,5 @@ InterfaceInfo::~InterfaceInfo() } Platform::MemoryFree(const_cast(service.mTextEntries)); } - } // namespace Dnssd } // namespace chip diff --git a/src/platform/mt793x/DnssdImpl.cpp b/src/platform/mt793x/DnssdImpl.cpp index efcd82c1016a3e..165198bd4bfdae 100644 --- a/src/platform/mt793x/DnssdImpl.cpp +++ b/src/platform/mt793x/DnssdImpl.cpp @@ -28,6 +28,12 @@ #include "FreeRTOS.h" #include "event_groups.h" #include "lwip/sockets.h" +#undef write +#undef read +#include +#include +#include + #include "mDNSDebug.h" #include "task.h" #include "task_def.h" @@ -37,46 +43,224 @@ #include #include #include +#include + +using namespace chip::Dnssd; + extern "C" { extern void mDNSPlatformWriteLogRedirect(void (*)(const char *, const char *)); } -namespace { +namespace { +constexpr const char * kLocalDot = "local."; +constexpr const char * kProtocolTcp = "._tcp"; +constexpr const char * kProtocolUdp = "._udp"; static constexpr uint32_t kTimeoutMilli = 3000; static constexpr size_t kMaxResults = 20; +constexpr DNSServiceFlags kRegisterFlags = kDNSServiceFlagsNoAutoRename; +constexpr DNSServiceFlags kBrowseFlags = 0; +constexpr DNSServiceFlags kGetAddrInfoFlags = kDNSServiceFlagsTimeout | kDNSServiceFlagsShareConnection; +constexpr DNSServiceFlags kResolveFlags = kDNSServiceFlagsShareConnection; +constexpr DNSServiceFlags kReconfirmRecordFlags = 0; + + +bool IsSupportedProtocol(DnssdServiceProtocol protocol) +{ + return (protocol == DnssdServiceProtocol::kDnssdProtocolUdp) || (protocol == DnssdServiceProtocol::kDnssdProtocolTcp); +} + + +uint32_t GetInterfaceId(chip::Inet::InterfaceId interfaceId) +{ + return interfaceId.IsPresent() ? (uint32_t)(void *)interfaceId.GetPlatformInterface() : kDNSServiceInterfaceIndexAny; +} + + +std::string GetFullType(const char * type, DnssdServiceProtocol protocol) +{ + std::ostringstream typeBuilder; + typeBuilder << type; + typeBuilder << (protocol == DnssdServiceProtocol::kDnssdProtocolUdp ? kProtocolUdp : kProtocolTcp); + return typeBuilder.str(); +} + + +std::string GetFullType(const DnssdService * service) +{ + return GetFullType(service->mType, service->mProtocol); +} + + +std::string GetFullTypeWithSubTypes(const char * type, DnssdServiceProtocol protocol, const char * subTypes[], size_t subTypeSize) +{ + std::ostringstream typeBuilder; + typeBuilder << type; + typeBuilder << (protocol == DnssdServiceProtocol::kDnssdProtocolUdp ? kProtocolUdp : kProtocolTcp); + for (int i = 0; i < (int) subTypeSize; i++) + { + typeBuilder << ","; + typeBuilder << subTypes[i]; + } + return typeBuilder.str(); +} + + +std::string GetFullTypeWithSubTypes(const char * type, DnssdServiceProtocol protocol) +{ + auto fullType = GetFullType(type, protocol); + + std::string subtypeDelimiter = "._sub."; + size_t position = fullType.find(subtypeDelimiter); + if (position != std::string::npos) + { + fullType = fullType.substr(position + subtypeDelimiter.size()) + "," + fullType.substr(0, position); + } + + return fullType; +} + + +std::string GetFullTypeWithSubTypes(const DnssdService * service) +{ + return GetFullTypeWithSubTypes(service->mType, service->mProtocol, service->mSubTypes, service->mSubTypeSize); +} + + +std::string GetHostNameWithDomain(const char * hostname) +{ + return std::string(hostname) + '.' + kLocalDot; +} + + +void LogOnFailure(const char * name, DNSServiceErrorType err) +{ + if (kDNSServiceErr_NoError != err) + { + ChipLogError(Discovery, "%s (%s)", name, Error::ToString(err)); + } +} + + +class ScopedTXTRecord +{ +public: + ScopedTXTRecord() {} + + ~ScopedTXTRecord() + { + if (mDataSize != 0) + { + TXTRecordDeallocate(&mRecordRef); + } + } + + CHIP_ERROR Init(TextEntry * textEntries, size_t textEntrySize) + { + VerifyOrReturnError(textEntrySize <= kDnssdTextMaxSize, CHIP_ERROR_INVALID_ARGUMENT); + + TXTRecordCreate(&mRecordRef, sizeof(mRecordBuffer), mRecordBuffer); + + for (size_t i = 0; i < textEntrySize; i++) + { + TextEntry entry = textEntries[i]; + VerifyOrReturnError(chip::CanCastTo(entry.mDataSize), CHIP_ERROR_INVALID_ARGUMENT); + + auto err = TXTRecordSetValue(&mRecordRef, entry.mKey, static_cast(entry.mDataSize), entry.mData); + VerifyOrReturnError(err == kDNSServiceErr_NoError, CHIP_ERROR_INVALID_ARGUMENT); + } + + mDataSize = TXTRecordGetLength(&mRecordRef); + if (mDataSize == 0) + { + TXTRecordDeallocate(&mRecordRef); + } + + mData = TXTRecordGetBytesPtr(&mRecordRef); + return CHIP_NO_ERROR; + } + + uint16_t size() { return mDataSize; } + const void * data() { return mData; } + +private: + uint16_t mDataSize = 0; + const void * mData = nullptr; + + TXTRecordRef mRecordRef; + char mRecordBuffer[kDnssdTextMaxSize]; +}; } // namespace + namespace chip { namespace Dnssd { #define SERVICE_DOMAIN ("local") MdnsContexts MdnsContexts::sInstance; -static DNSServiceRef client = NULL; +static std::vector clients; static TXTRecordRef PublishTxtRecord; +static DNSServiceRef BrowseClient = NULL; +static TaskHandle_t gResolveTask = NULL; +static EventGroupHandle_t gResolveTaskWakeEvent; + void ChipDnssdMdnsLog(const char * level, const char * msg) { ChipLogProgress(ServiceProvisioning, "%s %s", level, msg); } -/** - * @brief mDNS Daemon Task entry - * @param[in] void *not_used:Not used - * @return None - */ + +static void OnRegister(DNSServiceRef sdRef, DNSServiceFlags flags, DNSServiceErrorType err, const char * name, const char * type, + const char * domain, void * context) +{ + ChipLogDetail(Discovery, "Mdns: %s name: %s, type: %s, domain: %s, flags: %ld", __func__, name, type, domain, flags); + + auto sdCtx = reinterpret_cast(context); + sdCtx->Finalize(err); +}; + + +CHIP_ERROR Register(void * context, DnssdPublishCallback callback, uint32_t interfaceId, const char * type, const char * name, + uint16_t port, ScopedTXTRecord & record, Inet::IPAddressType addressType, const char * hostname) +{ + ChipLogDetail(Discovery, "Registering service %s on host %s with port %u and type: %s on interface id: %" PRIu32, name, + hostname, port, type, interfaceId); + + RegisterContext * sdCtx = nullptr; + if (CHIP_NO_ERROR == MdnsContexts::GetInstance().GetRegisterContextOfType(type, &sdCtx)) + { + auto err = DNSServiceUpdateRecord(sdCtx->serviceRef, nullptr, kRegisterFlags, record.size(), record.data(), 0 /* ttl */); + VerifyOrReturnError(kDNSServiceErr_NoError == err, sdCtx->Finalize(err)); + return CHIP_NO_ERROR; + } + + sdCtx = chip::Platform::New(type, name, callback, context); + VerifyOrReturnError(nullptr != sdCtx, CHIP_ERROR_NO_MEMORY); + + // sdCtx->mHostNameRegistrar.Init(hostname, addressType, interfaceId); + + DNSServiceRef sdRef; + auto err = DNSServiceRegister(&sdRef, kRegisterFlags, interfaceId, name, type, kLocalDot, hostname, ntohs(port), record.size(), + record.data(), OnRegister, sdCtx); + VerifyOrReturnError(kDNSServiceErr_NoError == err, sdCtx->Finalize(err)); + + return MdnsContexts::GetInstance().Add(sdCtx, sdRef); +} + + static void mdnsd_entry(void * not_used) { ChipLogProgress(ServiceProvisioning, "mdnsd_entry start"); mdnsd_start(); ChipLogProgress(ServiceProvisioning, "mdnsd_entry return"); - client = NULL; vTaskDelete(NULL); } + CHIP_ERROR ChipDnssdInit(DnssdAsyncReturnCallback initCallback, DnssdAsyncReturnCallback errorCallback, void * context) { CHIP_ERROR error = CHIP_NO_ERROR; @@ -95,23 +279,8 @@ CHIP_ERROR ChipDnssdInit(DnssdAsyncReturnCallback initCallback, DnssdAsyncReturn mDNSPlatformWriteLogRedirect(ChipDnssdMdnsLog); -#if 0 -#define MDNS_STACK_SIZE ((64 * 1024) / sizeof(portSTACK_TYPE)) - static StackType_t xMDnsStack[ MDNS_STACK_SIZE ]; - static StaticTask_t xMDnsTask; - - // xTaskHandle create mDNS daemon task - if ( NULL != xTaskCreateStatic( mdnsd_entry, - "mdnsd", - MDNS_STACK_SIZE, - NULL, - TASK_PRIORITY_NORMAL, - &xMDnsStack[0], - &xMDnsTask ) ) -#else // xTaskHandle create mDNS daemon task if (pdPASS != xTaskCreate(mdnsd_entry, "mdnsd", (15 * 1024) / sizeof(portSTACK_TYPE), NULL, TASK_PRIORITY_NORMAL, NULL)) -#endif { ChipLogProgress(ServiceProvisioning, "Cannot create mdnsd_task"); error = CHIP_ERROR_INTERNAL; @@ -122,102 +291,54 @@ CHIP_ERROR ChipDnssdInit(DnssdAsyncReturnCallback initCallback, DnssdAsyncReturn return error; } + void ChipDnssdShutdown(void) { ChipLogProgress(ServiceProvisioning, "shutdown mdnsd_task not implemented"); } + static const char * GetProtocolString(DnssdServiceProtocol protocol) { return protocol == DnssdServiceProtocol::kDnssdProtocolTcp ? "_tcp" : "_udp"; } + CHIP_ERROR ChipDnssdPublishService(const DnssdService * service, DnssdPublishCallback callback, void * context) { - CHIP_ERROR error = CHIP_NO_ERROR; - DNSServiceErrorType err; - DNSServiceFlags flags = 0; - char ServiceType[kDnssdTypeMaxSize + 10] = { 0 }; + VerifyOrReturnError(service != nullptr, CHIP_ERROR_INVALID_ARGUMENT); + VerifyOrReturnError(IsSupportedProtocol(service->mProtocol), CHIP_ERROR_INVALID_ARGUMENT); + VerifyOrReturnError(callback != nullptr, CHIP_ERROR_INVALID_ARGUMENT); + VerifyOrReturnError(strcmp(service->mHostName, "") != 0, CHIP_ERROR_INVALID_ARGUMENT); - (void) callback; - (void) context; + ScopedTXTRecord record; + ReturnErrorOnFailure(record.Init(service->mTextEntries, service->mTextEntrySize)); - ChipLogProgress(ServiceProvisioning, "ChipDnssdPublishService"); + auto regtype = GetFullTypeWithSubTypes(service); + auto interfaceId = GetInterfaceId(service->mInterface); + auto hostname = GetHostNameWithDomain(service->mHostName); - strcpy(ServiceType, service->mType); - strcat(ServiceType, "."); - strcat(ServiceType, GetProtocolString(service->mProtocol)); - - ChipLogProgress(ServiceProvisioning, "ServiceName: %s", service->mName); - ChipLogProgress(ServiceProvisioning, "ServiceType: %s", ServiceType); - ChipLogProgress(ServiceProvisioning, "ServiceDomain: %s", SERVICE_DOMAIN); - ChipLogProgress(ServiceProvisioning, "Hostname: %s", service->mHostName); - ChipLogProgress(ServiceProvisioning, "ServicePort: %d", (int) service->mPort); - - VerifyOrExit(service->mTextEntrySize <= UINT8_MAX, error = CHIP_ERROR_INVALID_ARGUMENT); - - if (service->mTextEntries) - { - // Create TXT Record - TXTRecordCreate(&PublishTxtRecord, 0, NULL); - for (size_t i = 0; i < service->mTextEntrySize; i++) - { - ChipLogProgress(ServiceProvisioning, "service: key %s size %d data %s", service->mTextEntries[i].mKey, - service->mTextEntries[i].mDataSize, service->mTextEntries[i].mData); + return Register(context, callback, interfaceId, regtype.c_str(), service->mName, service->mPort, record, service->mAddressType, + hostname.c_str()); +} - err = TXTRecordSetValue(&PublishTxtRecord, service->mTextEntries[i].mKey, service->mTextEntries[i].mDataSize, - service->mTextEntries[i].mData); - VerifyOrExit(err == 0, error = CHIP_ERROR_INTERNAL); - } - } - if (client != NULL) +CHIP_ERROR ChipDnssdRemoveServices() +{ + auto err = MdnsContexts::GetInstance().RemoveAllOfType(ContextType::Register); + if (CHIP_ERROR_KEY_NOT_FOUND == err) { - // ChipLogProgress(ServiceProvisioning, "ChipDnssdPublishService - DNSServiceRefDeallocate"); - // DNSServiceRefDeallocate(client); - // client = NULL; + err = CHIP_NO_ERROR; } - - ChipLogProgress(ServiceProvisioning, "ChipDnssdPublishService - client %p", client); - - ChipLogProgress(ServiceProvisioning, "ChipDnssdPublishService - DNSServiceRegister"); - - // Register Bonjour Service - err = DNSServiceRegister(&client, // DNSServiceRef - flags, // DNSServiceFlags - kDNSServiceInterfaceIndexAny, // interface index - service->mName, // service name - ServiceType, // service type - SERVICE_DOMAIN, // domain - NULL, // host - // service->mHostName, // host - htons(service->mPort), // port - TXTRecordGetLength(&PublishTxtRecord), // txt record length - TXTRecordGetBytesPtr(&PublishTxtRecord), // txt record pointer - NULL, // callback - NULL); // context - VerifyOrExit(err == 0, error = CHIP_ERROR_INTERNAL); - -exit: - // PublishTxtRecord is static global, no need to free - - return error; + return err; } -CHIP_ERROR ChipDnssdRemoveServices() -{ - ChipLogProgress(ServiceProvisioning, "ChipDnssdRemoveServices"); - TXTRecordDeallocate(&PublishTxtRecord); - DNSServiceRefDeallocate(client); - return CHIP_NO_ERROR; -} CHIP_ERROR ChipDnssdFinalizeServiceUpdate() { return CHIP_NO_ERROR; } -static DNSServiceRef BrowseClient = NULL; void ChipDNSServiceBrowseReply(DNSServiceRef sdRef, DNSServiceFlags flags, uint32_t interfaceIndex, DNSServiceErrorType errorCode, const char * serviceName, const char * regtype, const char * replyDomain, void * context) @@ -231,6 +352,7 @@ void ChipDNSServiceBrowseReply(DNSServiceRef sdRef, DNSServiceFlags flags, uint3 ChipBrowseHandler(NULL, &service, 1, true, CHIP_NO_ERROR); } + CHIP_ERROR ChipDnssdBrowse(const char * type, DnssdServiceProtocol protocol, chip::Inet::IPAddressType addressType, chip::Inet::InterfaceId interface, DnssdBrowseCallback callback, void * context, intptr_t * browseIdentifier) @@ -257,13 +379,12 @@ CHIP_ERROR ChipDnssdBrowse(const char * type, DnssdServiceProtocol protocol, chi return error; } + CHIP_ERROR ChipDnssdStopBrowse(intptr_t browseIdentifier) { return CHIP_ERROR_NOT_IMPLEMENTED; } -static TaskHandle_t gResolveTask = NULL; -static EventGroupHandle_t gResolveTaskWakeEvent; static void resolve_client_task(void * parameter) { @@ -328,6 +449,7 @@ static void resolve_client_task(void * parameter) vTaskDelete(NULL); } + static void OnGetAddrInfo(DNSServiceRef sdRef, DNSServiceFlags flags, uint32_t interfaceId, DNSServiceErrorType err, const char * hostname, const struct sockaddr * address, uint32_t ttl, void * context) { @@ -348,6 +470,7 @@ static void OnGetAddrInfo(DNSServiceRef sdRef, DNSServiceFlags flags, uint32_t i } } + static void GetAddrInfo(ResolveContext * sdCtx) { ChipLogProgress(ServiceProvisioning, "ChipDNSServiceResolveReply: GetAddrInfo"); @@ -369,6 +492,7 @@ static void GetAddrInfo(ResolveContext * sdCtx) } } + void ChipDNSServiceResolveReply(DNSServiceRef sdRef, DNSServiceFlags flags, uint32_t interfaceIndex, DNSServiceErrorType errorCode, const char * fullname, const char * hosttarget, uint16_t port, /* In network byte order */ uint16_t txtLen, const unsigned char * txtRecord, void * context) @@ -393,10 +517,6 @@ void ChipDNSServiceResolveReply(DNSServiceRef sdRef, DNSServiceFlags flags, uint } } -uint32_t GetInterfaceId(chip::Inet::InterfaceId interfaceId) -{ - return interfaceId.IsPresent() ? (uint32_t)(void *) interfaceId.GetPlatformInterface() : kDNSServiceInterfaceIndexAny; -} CHIP_ERROR ChipDnssdResolve(DnssdService * service, chip::Inet::InterfaceId interface, DnssdResolveCallback callback, void * context) @@ -455,6 +575,7 @@ CHIP_ERROR ChipDnssdResolve(DnssdService * service, chip::Inet::InterfaceId inte return error; } + CHIP_ERROR ChipDnssdReconfirmRecord(const char * hostname, chip::Inet::IPAddress address, chip::Inet::InterfaceId interface) { return CHIP_ERROR_NOT_IMPLEMENTED; diff --git a/src/platform/mt793x/DnssdImpl.h b/src/platform/mt793x/DnssdImpl.h index ba4d213a6618a2..5adc75e21f98b5 100644 --- a/src/platform/mt793x/DnssdImpl.h +++ b/src/platform/mt793x/DnssdImpl.h @@ -26,7 +26,6 @@ namespace chip { namespace Dnssd { - enum class ContextType { Register, @@ -34,6 +33,7 @@ enum class ContextType Resolve, }; + struct GenericContext { ContextType type; @@ -48,8 +48,10 @@ struct GenericContext virtual void DispatchSuccess() = 0; }; + struct RegisterContext; + class MdnsContexts { public: @@ -91,6 +93,25 @@ class MdnsContexts std::vector mContexts; }; + +struct RegisterContext : public GenericContext +{ + DnssdPublishCallback callback; + std::string mType; + std::string mInstanceName; + // HostNameRegistrar mHostNameRegistrar; + + RegisterContext(const char * sType, const char * instanceName, DnssdPublishCallback cb, void * cbContext); + // virtual ~RegisterContext() { mHostNameRegistrar.Unregister(); } + virtual ~RegisterContext() {} + + void DispatchFailure(DNSServiceErrorType err) override; + void DispatchSuccess() override; + + bool matches(const char * sType) { return mType.compare(sType) == 0; } +}; + + struct BrowseContext : public GenericContext { DnssdBrowseCallback callback; @@ -104,6 +125,7 @@ struct BrowseContext : public GenericContext void DispatchSuccess() override; }; + struct InterfaceInfo { InterfaceInfo(); @@ -118,6 +140,7 @@ struct InterfaceInfo std::string fullyQualifiedDomainName; }; + struct ResolveContext : public GenericContext { DnssdResolveCallback callback; @@ -138,6 +161,5 @@ struct ResolveContext : public GenericContext const unsigned char * txtRecord); bool HasInterface(); }; - } // namespace Dnssd } // namespace chip From cf1b628d7ecccf25e3036c973d13c1f5cf53cee1 Mon Sep 17 00:00:00 2001 From: j8xixo12 Date: Thu, 1 Dec 2022 09:17:00 +0800 Subject: [PATCH 2/4] [genio] remove unuse variables --- src/platform/mt793x/DnssdImpl.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/platform/mt793x/DnssdImpl.cpp b/src/platform/mt793x/DnssdImpl.cpp index 165198bd4bfdae..971160ca89cd88 100644 --- a/src/platform/mt793x/DnssdImpl.cpp +++ b/src/platform/mt793x/DnssdImpl.cpp @@ -201,8 +201,6 @@ namespace Dnssd { #define SERVICE_DOMAIN ("local") MdnsContexts MdnsContexts::sInstance; -static std::vector clients; -static TXTRecordRef PublishTxtRecord; static DNSServiceRef BrowseClient = NULL; static TaskHandle_t gResolveTask = NULL; static EventGroupHandle_t gResolveTaskWakeEvent; From 6ad3e57638c6e7e3ad7edd73ea308fe77da65f3a Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Thu, 1 Dec 2022 03:47:55 +0000 Subject: [PATCH 3/4] Restyled by clang-format --- examples/platform/mt793x/link_wrapper.c | 5 +-- src/platform/mt793x/DnssdContexts.cpp | 32 ------------------ src/platform/mt793x/DnssdImpl.cpp | 44 ++++--------------------- src/platform/mt793x/DnssdImpl.h | 7 ---- 4 files changed, 9 insertions(+), 79 deletions(-) diff --git a/examples/platform/mt793x/link_wrapper.c b/examples/platform/mt793x/link_wrapper.c index 3ef6f068974d72..134a372a7cacaa 100644 --- a/examples/platform/mt793x/link_wrapper.c +++ b/examples/platform/mt793x/link_wrapper.c @@ -58,9 +58,10 @@ void __assert_func(const char * file, int line, const char * func, const char * #include #include -void * __wrap__calloc_r(size_t nmemb, size_t size) { +void * __wrap__calloc_r(size_t nmemb, size_t size) +{ void * p = pvPortCalloc(nmemb, size); - while(!p) + while (!p) ; return p; } diff --git a/src/platform/mt793x/DnssdContexts.cpp b/src/platform/mt793x/DnssdContexts.cpp index 4a97fcf4238da2..f8af3b450e9d87 100644 --- a/src/platform/mt793x/DnssdContexts.cpp +++ b/src/platform/mt793x/DnssdContexts.cpp @@ -33,7 +33,6 @@ namespace { constexpr uint8_t kDnssdKeyMaxSize = 32; constexpr uint8_t kDnssdTxtRecordMaxEntries = 20; - std::string GetHostNameWithoutDomain(const char * hostnameWithDomain) { std::string hostname(hostnameWithDomain); @@ -46,7 +45,6 @@ std::string GetHostNameWithoutDomain(const char * hostnameWithDomain) return hostname; } - std::string GetFullTypeWithoutSubTypes(std::string fullType) { size_t position = fullType.find(","); @@ -58,7 +56,6 @@ std::string GetFullTypeWithoutSubTypes(std::string fullType) return fullType; } - void GetTextEntries(DnssdService & service, const unsigned char * data, uint16_t len) { uint16_t recordCount = TXTRecordGetCount(len, data); @@ -96,7 +93,6 @@ void GetTextEntries(DnssdService & service, const unsigned char * data, uint16_t } } - DNSServiceProtocol GetProtocol(const chip::Inet::IPAddressType & addressType) { #if INET_CONFIG_ENABLE_IPV4 @@ -118,7 +114,6 @@ DNSServiceProtocol GetProtocol(const chip::Inet::IPAddressType & addressType) } } // namespace - namespace chip { namespace Dnssd { CHIP_ERROR GenericContext::Finalize(DNSServiceErrorType err) @@ -142,7 +137,6 @@ CHIP_ERROR GenericContext::Finalize(DNSServiceErrorType err) return Error::ToChipError(err); } - RegisterContext::RegisterContext(const char * sType, const char * instanceName, DnssdPublishCallback cb, void * cbContext) { type = ContextType::Register; @@ -153,7 +147,6 @@ RegisterContext::RegisterContext(const char * sType, const char * instanceName, mInstanceName = instanceName; } - void RegisterContext::DispatchFailure(DNSServiceErrorType err) { ChipLogError(Discovery, "Mdns: Register failure (%s)", Error::ToString(err)); @@ -161,7 +154,6 @@ void RegisterContext::DispatchFailure(DNSServiceErrorType err) MdnsContexts::GetInstance().Remove(this); } - void RegisterContext::DispatchSuccess() { std::string typeWithoutSubTypes = GetFullTypeWithoutSubTypes(mType); @@ -172,7 +164,6 @@ void RegisterContext::DispatchSuccess() // mHostNameRegistrar.Register(); } - CHIP_ERROR MdnsContexts::GetRegisterContextOfType(const char * type, RegisterContext ** context) { bool found = false; @@ -191,7 +182,6 @@ CHIP_ERROR MdnsContexts::GetRegisterContextOfType(const char * type, RegisterCon return found ? CHIP_NO_ERROR : CHIP_ERROR_KEY_NOT_FOUND; } - MdnsContexts::~MdnsContexts() { std::vector::const_iterator iter = mContexts.cbegin(); @@ -202,7 +192,6 @@ MdnsContexts::~MdnsContexts() } } - CHIP_ERROR MdnsContexts::Add(GenericContext * context, DNSServiceRef sdRef) { VerifyOrReturnError(context != nullptr, CHIP_ERROR_INVALID_ARGUMENT); @@ -219,7 +208,6 @@ CHIP_ERROR MdnsContexts::Add(GenericContext * context, DNSServiceRef sdRef) return CHIP_NO_ERROR; } - CHIP_ERROR MdnsContexts::Remove(GenericContext * context) { bool found = false; @@ -242,7 +230,6 @@ CHIP_ERROR MdnsContexts::Remove(GenericContext * context) return found ? CHIP_NO_ERROR : CHIP_ERROR_KEY_NOT_FOUND; } - CHIP_ERROR MdnsContexts::RemoveAllOfType(ContextType type) { bool found = false; @@ -264,7 +251,6 @@ CHIP_ERROR MdnsContexts::RemoveAllOfType(ContextType type) return found ? CHIP_NO_ERROR : CHIP_ERROR_KEY_NOT_FOUND; } - void MdnsContexts::Delete(GenericContext * context) { if (context->serviceRef != nullptr) @@ -274,7 +260,6 @@ void MdnsContexts::Delete(GenericContext * context) chip::Platform::Delete(context); } - CHIP_ERROR MdnsContexts::Has(GenericContext * context) { std::vector::iterator iter; @@ -290,7 +275,6 @@ CHIP_ERROR MdnsContexts::Has(GenericContext * context) return CHIP_ERROR_KEY_NOT_FOUND; } - GenericContext * MdnsContexts::GetBySockFd(int fd) { std::vector::iterator iter; @@ -306,7 +290,6 @@ GenericContext * MdnsContexts::GetBySockFd(int fd) return NULL; } - int MdnsContexts::GetSelectFd(fd_set * pSelectFd) { int maxFd = 0; @@ -326,7 +309,6 @@ int MdnsContexts::GetSelectFd(fd_set * pSelectFd) return maxFd; } - BrowseContext::BrowseContext(void * cbContext, DnssdBrowseCallback cb, DnssdServiceProtocol cbContextProtocol) { type = ContextType::Browse; @@ -336,7 +318,6 @@ BrowseContext::BrowseContext(void * cbContext, DnssdBrowseCallback cb, DnssdServ mSelectCount = 0; } - void BrowseContext::DispatchFailure(DNSServiceErrorType err) { ChipLogError(Discovery, "Mdns: Browse failure (%s)", Error::ToString(err)); @@ -344,14 +325,12 @@ void BrowseContext::DispatchFailure(DNSServiceErrorType err) MdnsContexts::GetInstance().Remove(this); } - void BrowseContext::DispatchSuccess() { callback(context, services.data(), services.size(), true, CHIP_NO_ERROR); MdnsContexts::GetInstance().Remove(this); } - ResolveContext::ResolveContext(void * cbContext, DnssdResolveCallback cb, chip::Inet::IPAddressType cbAddressType) { type = ContextType::Resolve; @@ -361,10 +340,8 @@ ResolveContext::ResolveContext(void * cbContext, DnssdResolveCallback cb, chip:: mSelectCount = 0; } - ResolveContext::~ResolveContext() {} - void ResolveContext::DispatchFailure(DNSServiceErrorType err) { ChipLogError(Discovery, "Mdns: Resolve failure (%s)", Error::ToString(err)); @@ -372,7 +349,6 @@ void ResolveContext::DispatchFailure(DNSServiceErrorType err) MdnsContexts::GetInstance().Remove(this); } - void ResolveContext::DispatchSuccess() { for (auto & interface : interfaces) @@ -393,7 +369,6 @@ void ResolveContext::DispatchSuccess() MdnsContexts::GetInstance().Remove(this); } - CHIP_ERROR ResolveContext::OnNewAddress(uint32_t interfaceId, const struct sockaddr * address) { CHIP_ERROR err = CHIP_NO_ERROR; @@ -435,7 +410,6 @@ CHIP_ERROR ResolveContext::OnNewAddress(uint32_t interfaceId, const struct socka return err; } - CHIP_ERROR ResolveContext::OnNewLocalOnlyAddress() { struct sockaddr_in sockaddr; @@ -450,7 +424,6 @@ CHIP_ERROR ResolveContext::OnNewLocalOnlyAddress() return OnNewAddress(kDNSServiceInterfaceIndexLocalOnly, reinterpret_cast(&sockaddr)); } - bool ResolveContext::HasAddress() { for (auto & interface : interfaces) @@ -464,7 +437,6 @@ bool ResolveContext::HasAddress() return false; } - void ResolveContext::OnNewInterface(uint32_t interfaceId, const char * fullname, const char * hostnameWithDomain, uint16_t port, uint16_t txtLen, const unsigned char * txtRecord) { @@ -534,20 +506,17 @@ void ResolveContext::OnNewInterface(uint32_t interfaceId, const char * fullname, interfaces.insert(std::make_pair(interfaceId, std::move(interface))); } - bool ResolveContext::HasInterface() { return interfaces.size(); } - InterfaceInfo::InterfaceInfo() { service.mTextEntrySize = 0; service.mTextEntries = nullptr; } - InterfaceInfo::InterfaceInfo(InterfaceInfo && other) : service(std::move(other.service)), addresses(std::move(other.addresses)), fullyQualifiedDomainName(std::move(other.fullyQualifiedDomainName)) @@ -558,7 +527,6 @@ InterfaceInfo::InterfaceInfo(InterfaceInfo && other) : other.service.mTextEntries = nullptr; } - InterfaceInfo::~InterfaceInfo() { if (service.mTextEntries == nullptr) diff --git a/src/platform/mt793x/DnssdImpl.cpp b/src/platform/mt793x/DnssdImpl.cpp index 9406576c7f7d01..5778c4996cd05c 100644 --- a/src/platform/mt793x/DnssdImpl.cpp +++ b/src/platform/mt793x/DnssdImpl.cpp @@ -42,21 +42,19 @@ #include "platform/CHIPDeviceLayer.h" #include #include -#include #include +#include using namespace chip::Dnssd; - extern "C" { extern void mDNSPlatformWriteLogRedirect(void (*)(const char *, const char *)); } - namespace { -constexpr const char * kLocalDot = "local."; -constexpr const char * kProtocolTcp = "._tcp"; -constexpr const char * kProtocolUdp = "._udp"; +constexpr const char * kLocalDot = "local."; +constexpr const char * kProtocolTcp = "._tcp"; +constexpr const char * kProtocolUdp = "._udp"; static constexpr uint32_t kTimeoutMilli = 3000; static constexpr size_t kMaxResults = 20; @@ -66,19 +64,16 @@ constexpr DNSServiceFlags kGetAddrInfoFlags = kDNSServiceFlagsTimeout | kDNS constexpr DNSServiceFlags kResolveFlags = kDNSServiceFlagsShareConnection; constexpr DNSServiceFlags kReconfirmRecordFlags = 0; - bool IsSupportedProtocol(DnssdServiceProtocol protocol) { return (protocol == DnssdServiceProtocol::kDnssdProtocolUdp) || (protocol == DnssdServiceProtocol::kDnssdProtocolTcp); } - uint32_t GetInterfaceId(chip::Inet::InterfaceId interfaceId) { - return interfaceId.IsPresent() ? (uint32_t)(void *)interfaceId.GetPlatformInterface() : kDNSServiceInterfaceIndexAny; + return interfaceId.IsPresent() ? (uint32_t)(void *) interfaceId.GetPlatformInterface() : kDNSServiceInterfaceIndexAny; } - std::string GetFullType(const char * type, DnssdServiceProtocol protocol) { std::ostringstream typeBuilder; @@ -87,13 +82,11 @@ std::string GetFullType(const char * type, DnssdServiceProtocol protocol) return typeBuilder.str(); } - std::string GetFullType(const DnssdService * service) { return GetFullType(service->mType, service->mProtocol); } - std::string GetFullTypeWithSubTypes(const char * type, DnssdServiceProtocol protocol, const char * subTypes[], size_t subTypeSize) { std::ostringstream typeBuilder; @@ -107,7 +100,6 @@ std::string GetFullTypeWithSubTypes(const char * type, DnssdServiceProtocol prot return typeBuilder.str(); } - std::string GetFullTypeWithSubTypes(const char * type, DnssdServiceProtocol protocol) { auto fullType = GetFullType(type, protocol); @@ -122,19 +114,16 @@ std::string GetFullTypeWithSubTypes(const char * type, DnssdServiceProtocol prot return fullType; } - std::string GetFullTypeWithSubTypes(const DnssdService * service) { return GetFullTypeWithSubTypes(service->mType, service->mProtocol, service->mSubTypes, service->mSubTypeSize); } - std::string GetHostNameWithDomain(const char * hostname) { return std::string(hostname) + '.' + kLocalDot; } - void LogOnFailure(const char * name, DNSServiceErrorType err) { if (kDNSServiceErr_NoError != err) @@ -143,7 +132,6 @@ void LogOnFailure(const char * name, DNSServiceErrorType err) } } - class ScopedTXTRecord { public: @@ -194,7 +182,6 @@ class ScopedTXTRecord }; } // namespace - namespace chip { namespace Dnssd { @@ -202,16 +189,14 @@ namespace Dnssd { MdnsContexts MdnsContexts::sInstance; static DNSServiceRef BrowseClient = NULL; -static TaskHandle_t gResolveTask = NULL; +static TaskHandle_t gResolveTask = NULL; static EventGroupHandle_t gResolveTaskWakeEvent; - void ChipDnssdMdnsLog(const char * level, const char * msg) { ChipLogProgress(ServiceProvisioning, "%s %s", StringOrNullMarker(level), StringOrNullMarker(msg)); } - static void OnRegister(DNSServiceRef sdRef, DNSServiceFlags flags, DNSServiceErrorType err, const char * name, const char * type, const char * domain, void * context) { @@ -221,7 +206,6 @@ static void OnRegister(DNSServiceRef sdRef, DNSServiceFlags flags, DNSServiceErr sdCtx->Finalize(err); }; - CHIP_ERROR Register(void * context, DnssdPublishCallback callback, uint32_t interfaceId, const char * type, const char * name, uint16_t port, ScopedTXTRecord & record, Inet::IPAddressType addressType, const char * hostname) { @@ -249,7 +233,6 @@ CHIP_ERROR Register(void * context, DnssdPublishCallback callback, uint32_t inte return MdnsContexts::GetInstance().Add(sdCtx, sdRef); } - static void mdnsd_entry(void * not_used) { ChipLogProgress(ServiceProvisioning, "mdnsd_entry start"); @@ -258,7 +241,6 @@ static void mdnsd_entry(void * not_used) vTaskDelete(NULL); } - CHIP_ERROR ChipDnssdInit(DnssdAsyncReturnCallback initCallback, DnssdAsyncReturnCallback errorCallback, void * context) { CHIP_ERROR error = CHIP_NO_ERROR; @@ -289,19 +271,16 @@ CHIP_ERROR ChipDnssdInit(DnssdAsyncReturnCallback initCallback, DnssdAsyncReturn return error; } - void ChipDnssdShutdown(void) { ChipLogProgress(ServiceProvisioning, "shutdown mdnsd_task not implemented"); } - static const char * GetProtocolString(DnssdServiceProtocol protocol) { return protocol == DnssdServiceProtocol::kDnssdProtocolTcp ? "_tcp" : "_udp"; } - CHIP_ERROR ChipDnssdPublishService(const DnssdService * service, DnssdPublishCallback callback, void * context) { VerifyOrReturnError(service != nullptr, CHIP_ERROR_INVALID_ARGUMENT); @@ -320,7 +299,6 @@ CHIP_ERROR ChipDnssdPublishService(const DnssdService * service, DnssdPublishCal hostname.c_str()); } - CHIP_ERROR ChipDnssdRemoveServices() { auto err = MdnsContexts::GetInstance().RemoveAllOfType(ContextType::Register); @@ -331,13 +309,11 @@ CHIP_ERROR ChipDnssdRemoveServices() return err; } - CHIP_ERROR ChipDnssdFinalizeServiceUpdate() { return CHIP_NO_ERROR; } - void ChipDNSServiceBrowseReply(DNSServiceRef sdRef, DNSServiceFlags flags, uint32_t interfaceIndex, DNSServiceErrorType errorCode, const char * serviceName, const char * regtype, const char * replyDomain, void * context) { @@ -350,7 +326,6 @@ void ChipDNSServiceBrowseReply(DNSServiceRef sdRef, DNSServiceFlags flags, uint3 ChipBrowseHandler(NULL, &service, 1, true, CHIP_NO_ERROR); } - CHIP_ERROR ChipDnssdBrowse(const char * type, DnssdServiceProtocol protocol, chip::Inet::IPAddressType addressType, chip::Inet::InterfaceId interface, DnssdBrowseCallback callback, void * context, intptr_t * browseIdentifier) @@ -377,13 +352,11 @@ CHIP_ERROR ChipDnssdBrowse(const char * type, DnssdServiceProtocol protocol, chi return error; } - CHIP_ERROR ChipDnssdStopBrowse(intptr_t browseIdentifier) { return CHIP_ERROR_NOT_IMPLEMENTED; } - static void resolve_client_task(void * parameter) { EventBits_t uxBits; @@ -447,7 +420,6 @@ static void resolve_client_task(void * parameter) vTaskDelete(NULL); } - static void OnGetAddrInfo(DNSServiceRef sdRef, DNSServiceFlags flags, uint32_t interfaceId, DNSServiceErrorType err, const char * hostname, const struct sockaddr * address, uint32_t ttl, void * context) { @@ -468,7 +440,6 @@ static void OnGetAddrInfo(DNSServiceRef sdRef, DNSServiceFlags flags, uint32_t i } } - static void GetAddrInfo(ResolveContext * sdCtx) { ChipLogProgress(ServiceProvisioning, "ChipDNSServiceResolveReply: GetAddrInfo"); @@ -490,7 +461,6 @@ static void GetAddrInfo(ResolveContext * sdCtx) } } - void ChipDNSServiceResolveReply(DNSServiceRef sdRef, DNSServiceFlags flags, uint32_t interfaceIndex, DNSServiceErrorType errorCode, const char * fullname, const char * hosttarget, uint16_t port, /* In network byte order */ uint16_t txtLen, const unsigned char * txtRecord, void * context) @@ -515,7 +485,6 @@ void ChipDNSServiceResolveReply(DNSServiceRef sdRef, DNSServiceFlags flags, uint } } - CHIP_ERROR ChipDnssdResolve(DnssdService * service, chip::Inet::InterfaceId interface, DnssdResolveCallback callback, void * context) { @@ -573,7 +542,6 @@ CHIP_ERROR ChipDnssdResolve(DnssdService * service, chip::Inet::InterfaceId inte return error; } - CHIP_ERROR ChipDnssdReconfirmRecord(const char * hostname, chip::Inet::IPAddress address, chip::Inet::InterfaceId interface) { return CHIP_ERROR_NOT_IMPLEMENTED; diff --git a/src/platform/mt793x/DnssdImpl.h b/src/platform/mt793x/DnssdImpl.h index 5adc75e21f98b5..7eec33f71eccd4 100644 --- a/src/platform/mt793x/DnssdImpl.h +++ b/src/platform/mt793x/DnssdImpl.h @@ -33,7 +33,6 @@ enum class ContextType Resolve, }; - struct GenericContext { ContextType type; @@ -48,10 +47,8 @@ struct GenericContext virtual void DispatchSuccess() = 0; }; - struct RegisterContext; - class MdnsContexts { public: @@ -93,7 +90,6 @@ class MdnsContexts std::vector mContexts; }; - struct RegisterContext : public GenericContext { DnssdPublishCallback callback; @@ -111,7 +107,6 @@ struct RegisterContext : public GenericContext bool matches(const char * sType) { return mType.compare(sType) == 0; } }; - struct BrowseContext : public GenericContext { DnssdBrowseCallback callback; @@ -125,7 +120,6 @@ struct BrowseContext : public GenericContext void DispatchSuccess() override; }; - struct InterfaceInfo { InterfaceInfo(); @@ -140,7 +134,6 @@ struct InterfaceInfo std::string fullyQualifiedDomainName; }; - struct ResolveContext : public GenericContext { DnssdResolveCallback callback; From 043695213352032f9bea7b4272f9037db9490861 Mon Sep 17 00:00:00 2001 From: j8xixo12 Date: Mon, 5 Dec 2022 10:36:39 +0800 Subject: [PATCH 4/4] [genio] clean up comment-out code and correct ntohs to htons --- src/platform/mt793x/DnssdImpl.cpp | 4 +--- src/platform/mt793x/DnssdImpl.h | 2 -- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/src/platform/mt793x/DnssdImpl.cpp b/src/platform/mt793x/DnssdImpl.cpp index 5778c4996cd05c..b1c8748437892f 100644 --- a/src/platform/mt793x/DnssdImpl.cpp +++ b/src/platform/mt793x/DnssdImpl.cpp @@ -223,10 +223,8 @@ CHIP_ERROR Register(void * context, DnssdPublishCallback callback, uint32_t inte sdCtx = chip::Platform::New(type, name, callback, context); VerifyOrReturnError(nullptr != sdCtx, CHIP_ERROR_NO_MEMORY); - // sdCtx->mHostNameRegistrar.Init(hostname, addressType, interfaceId); - DNSServiceRef sdRef; - auto err = DNSServiceRegister(&sdRef, kRegisterFlags, interfaceId, name, type, kLocalDot, hostname, ntohs(port), record.size(), + auto err = DNSServiceRegister(&sdRef, kRegisterFlags, interfaceId, name, type, kLocalDot, hostname, htons(port), record.size(), record.data(), OnRegister, sdCtx); VerifyOrReturnError(kDNSServiceErr_NoError == err, sdCtx->Finalize(err)); diff --git a/src/platform/mt793x/DnssdImpl.h b/src/platform/mt793x/DnssdImpl.h index 7eec33f71eccd4..d1499b6b9a9be8 100644 --- a/src/platform/mt793x/DnssdImpl.h +++ b/src/platform/mt793x/DnssdImpl.h @@ -95,10 +95,8 @@ struct RegisterContext : public GenericContext DnssdPublishCallback callback; std::string mType; std::string mInstanceName; - // HostNameRegistrar mHostNameRegistrar; RegisterContext(const char * sType, const char * instanceName, DnssdPublishCallback cb, void * cbContext); - // virtual ~RegisterContext() { mHostNameRegistrar.Unregister(); } virtual ~RegisterContext() {} void DispatchFailure(DNSServiceErrorType err) override;