forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[android] Use common DNS-SD code (project-chip#8543)
* [android] Use common DNS-SD code Currently, Android contains temporary code for resolving a node ID to an IP address. Rewrite this piece to use common CHIP DNS-SD layer by providing an adapter between Discovery_ImplPlatform and NsdManager from the Android library. * Fix weird doxygen error * Address code-review comments * Support node IDs > 2^63 * Add TODO about interface ID
- Loading branch information
1 parent
f46bf40
commit da1f75d
Showing
11 changed files
with
344 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
/* | ||
* | ||
* Copyright (c) 2021 Project CHIP Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include "MdnsImpl.h" | ||
|
||
#include "CHIPJNIError.h" | ||
#include "JniReferences.h" | ||
#include "JniTypeWrappers.h" | ||
|
||
#include <lib/mdns/platform/Mdns.h> | ||
#include <support/CHIPMemString.h> | ||
#include <support/CodeUtils.h> | ||
#include <support/SafeInt.h> | ||
#include <support/logging/CHIPLogging.h> | ||
|
||
#include <string> | ||
|
||
namespace chip { | ||
namespace Mdns { | ||
|
||
using namespace chip::Controller; | ||
using namespace chip::Platform; | ||
|
||
namespace { | ||
jobject sResolverObject = nullptr; | ||
jmethodID sResolveMethod = nullptr; | ||
} // namespace | ||
|
||
// Implemention of functions declared in lib/mdns/platform/Mdns.h | ||
|
||
CHIP_ERROR ChipMdnsInit(MdnsAsyncReturnCallback initCallback, MdnsAsyncReturnCallback errorCallback, void * context) | ||
{ | ||
return CHIP_NO_ERROR; | ||
} | ||
|
||
CHIP_ERROR ChipMdnsPublishService(const MdnsService * service) | ||
{ | ||
return CHIP_ERROR_NOT_IMPLEMENTED; | ||
} | ||
|
||
CHIP_ERROR ChipMdnsStopPublish() | ||
{ | ||
return CHIP_NO_ERROR; | ||
} | ||
|
||
CHIP_ERROR ChipMdnsStopPublishService(const MdnsService * service) | ||
{ | ||
return CHIP_ERROR_NOT_IMPLEMENTED; | ||
} | ||
|
||
CHIP_ERROR ChipMdnsBrowse(const char * type, MdnsServiceProtocol protocol, Inet::IPAddressType addressType, | ||
Inet::InterfaceId interface, MdnsBrowseCallback callback, void * context) | ||
{ | ||
// TODO: Implement DNS-SD browse for Android | ||
return CHIP_ERROR_NOT_IMPLEMENTED; | ||
} | ||
|
||
CHIP_ERROR ChipMdnsResolve(MdnsService * service, Inet::InterfaceId interface, MdnsResolveCallback callback, void * context) | ||
{ | ||
VerifyOrReturnError(service != nullptr && callback != nullptr, CHIP_ERROR_INVALID_ARGUMENT); | ||
VerifyOrReturnError(sResolverObject != nullptr && sResolveMethod != nullptr, CHIP_ERROR_INCORRECT_STATE); | ||
|
||
std::string serviceType = service->mType; | ||
serviceType += '.'; | ||
serviceType += (service->mProtocol == MdnsServiceProtocol::kMdnsProtocolUdp ? "_udp" : "_tcp"); | ||
|
||
JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread(); | ||
UtfString jniInstanceName(env, service->mName); | ||
UtfString jniServiceType(env, serviceType.c_str()); | ||
|
||
env->CallVoidMethod(sResolverObject, sResolveMethod, jniInstanceName.jniValue(), jniServiceType.jniValue(), | ||
reinterpret_cast<jlong>(callback), reinterpret_cast<jlong>(context)); | ||
|
||
if (env->ExceptionCheck()) | ||
{ | ||
ChipLogError(Discovery, "Java exception in ChipMdnsResolve"); | ||
env->ExceptionDescribe(); | ||
env->ExceptionClear(); | ||
return CHIP_JNI_ERROR_EXCEPTION_THROWN; | ||
} | ||
|
||
return CHIP_NO_ERROR; | ||
} | ||
|
||
// Implementation of other methods required by the CHIP stack | ||
|
||
void GetMdnsTimeout(timeval & timeout) {} | ||
void HandleMdnsTimeout() {} | ||
|
||
// Implemention of Java-specific functions | ||
|
||
void InitializeWithObject(jobject resolverObject) | ||
{ | ||
JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread(); | ||
sResolverObject = env->NewGlobalRef(resolverObject); | ||
jclass resolverClass = env->GetObjectClass(sResolverObject); | ||
|
||
VerifyOrReturn(resolverClass != nullptr, ChipLogError(Discovery, "Failed to get Resolver Java class")); | ||
|
||
sResolveMethod = env->GetMethodID(resolverClass, "resolve", "(Ljava/lang/String;Ljava/lang/String;JJ)V"); | ||
|
||
if (sResolveMethod == nullptr) | ||
{ | ||
ChipLogError(Discovery, "Failed to access Resolver 'resolve' method"); | ||
env->ExceptionClear(); | ||
} | ||
} | ||
|
||
void HandleResolve(jstring instanceName, jstring serviceType, jstring address, jint port, jlong callbackHandle, jlong contextHandle) | ||
{ | ||
VerifyOrReturn(callbackHandle != 0, ChipLogError(Discovery, "HandleResolve called with callback equal to nullptr")); | ||
|
||
const auto dispatch = [callbackHandle, contextHandle](CHIP_ERROR error, MdnsService * service = nullptr) { | ||
MdnsResolveCallback callback = reinterpret_cast<MdnsResolveCallback>(callbackHandle); | ||
callback(reinterpret_cast<void *>(contextHandle), service, error); | ||
}; | ||
|
||
VerifyOrReturn(address != nullptr && port != 0, dispatch(CHIP_ERROR_UNKNOWN_RESOURCE_ID)); | ||
|
||
JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread(); | ||
JniUtfString jniInstanceName(env, instanceName); | ||
JniUtfString jniServiceType(env, serviceType); | ||
JniUtfString jniAddress(env, address); | ||
Inet::IPAddress ipAddress; | ||
|
||
VerifyOrReturn(strlen(jniInstanceName.c_str()) <= kMdnsInstanceNameMaxSize, dispatch(CHIP_ERROR_INVALID_ARGUMENT)); | ||
VerifyOrReturn(strlen(jniServiceType.c_str()) <= kMdnsTypeAndProtocolMaxSize, dispatch(CHIP_ERROR_INVALID_ARGUMENT)); | ||
VerifyOrReturn(CanCastTo<uint16_t>(port), dispatch(CHIP_ERROR_INVALID_ARGUMENT)); | ||
VerifyOrReturn(Inet::IPAddress::FromString(jniAddress.c_str(), ipAddress), dispatch(CHIP_ERROR_INVALID_ARGUMENT)); | ||
|
||
MdnsService service = {}; | ||
CopyString(service.mName, jniInstanceName.c_str()); | ||
CopyString(service.mType, jniServiceType.c_str()); | ||
service.mAddress.SetValue(ipAddress); | ||
service.mPort = static_cast<uint16_t>(port); | ||
|
||
dispatch(CHIP_NO_ERROR, &service); | ||
} | ||
|
||
} // namespace Mdns | ||
} // namespace chip |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* | ||
* Copyright (c) 2021 Project CHIP Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <jni.h> | ||
|
||
namespace chip { | ||
namespace Mdns { | ||
|
||
/** | ||
* Initialize DNS-SD implementation for Android with an object of a class | ||
* that implements chip.devicecontroller.ServiceResolver interface. | ||
*/ | ||
void InitializeWithObject(jobject resolverObject); | ||
|
||
/** | ||
* Pass results of the service resolution to the CHIP stack. | ||
*/ | ||
void HandleResolve(jstring instanceName, jstring serviceType, jstring address, jint port, jlong callbackHandle, | ||
jlong contextHandle); | ||
|
||
} // namespace Mdns | ||
} // namespace chip |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.