Skip to content

Commit 4778163

Browse files
chrisdecenzorestyled-io[bot]restyled-commitslazarkov
authored andcommitted
Add text entries to mDNS service discovery response (#19027)
* Add text entries to mDNS service discovery response * fix android tv-app build * remove hack * remove hack * address feedback, formatting * fix darwin build * Restyle Add text entries to mDNS service discovery response (#19028) * Restyled by whitespace * Restyled by google-java-format Co-authored-by: Restyled.io <[email protected]> * address comments * address comments * address comments * address comments * Restyle merge Co-authored-by: restyled-io[bot] <32688539+restyled-io[bot]@users.noreply.github.com> Co-authored-by: Restyled.io <[email protected]> Co-authored-by: Lazar Kovacic <[email protected]>
1 parent 9432f63 commit 4778163

File tree

8 files changed

+124
-22
lines changed

8 files changed

+124
-22
lines changed

examples/tv-app/android/App/platform-app/src/main/java/com/matter/tv/server/MatterCommissioningPrompter.java

+1-8
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,7 @@ public void promptCommissioningSucceeded(int vendorId, int productId, String com
112112
}
113113

114114
public void promptCommissioningFailed(String commissioneeName, String error) {
115-
Log.d(
116-
TAG,
117-
"Received prompt for failure vendor id:"
118-
+ vendorId
119-
+ " productId:"
120-
+ productId
121-
+ ". Commissionee: "
122-
+ commissioneeName);
115+
Log.d(TAG, "Received prompt for failure Commissionee: " + commissioneeName);
123116
NotificationCompat.Builder builder =
124117
new NotificationCompat.Builder(activity, CHANNEL_ID)
125118
.setSmallIcon(R.drawable.ic_baseline_clear_24)

examples/tv-app/android/java/src/com/tcl/chip/tvapp/UserPrompterResolver.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
public class UserPrompterResolver {
2121

22-
private static final String TAG = "UserPrompterResolver";
22+
protected static final String TAG = "UserPrompterResolver";
2323

2424
public native void OnPinCodeEntered(int pinCode);
2525

src/platform/android/AndroidChipPlatform-JNI.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -238,11 +238,11 @@ JNI_METHOD(void, nativeSetServiceResolver)(JNIEnv * env, jclass self, jobject re
238238
}
239239

240240
JNI_MDNSCALLBACK_METHOD(void, handleServiceResolve)
241-
(JNIEnv * env, jclass self, jstring instanceName, jstring serviceType, jstring address, jint port, jlong callbackHandle,
242-
jlong contextHandle)
241+
(JNIEnv * env, jclass self, jstring instanceName, jstring serviceType, jstring address, jint port, jobject attributes,
242+
jlong callbackHandle, jlong contextHandle)
243243
{
244244
using ::chip::Dnssd::HandleResolve;
245-
HandleResolve(instanceName, serviceType, address, port, callbackHandle, contextHandle);
245+
HandleResolve(instanceName, serviceType, address, port, attributes, callbackHandle, contextHandle);
246246
}
247247

248248
#if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE

src/platform/android/DnssdImpl.cpp

+94-7
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
#include <cstddef>
3131
#include <jni.h>
32+
#include <memory>
3233
#include <string>
3334

3435
namespace chip {
@@ -37,11 +38,14 @@ namespace Dnssd {
3738
using namespace chip::Platform;
3839

3940
namespace {
40-
jobject sResolverObject = nullptr;
41-
jobject sMdnsCallbackObject = nullptr;
42-
jmethodID sResolveMethod = nullptr;
43-
jmethodID sPublishMethod = nullptr;
44-
jmethodID sRemoveServicesMethod = nullptr;
41+
jobject sResolverObject = nullptr;
42+
jobject sMdnsCallbackObject = nullptr;
43+
jmethodID sResolveMethod = nullptr;
44+
jmethodID sGetTextEntryKeysMethod = nullptr;
45+
jmethodID sGetTextEntryDataMethod = nullptr;
46+
jclass sMdnsCallbackClass = nullptr;
47+
jmethodID sPublishMethod = nullptr;
48+
jmethodID sRemoveServicesMethod = nullptr;
4549
} // namespace
4650

4751
// Implementation of functions declared in lib/dnssd/platform/Dnssd.h
@@ -185,9 +189,14 @@ void InitializeWithObjects(jobject resolverObject, jobject mdnsCallbackObject)
185189
sResolverObject = env->NewGlobalRef(resolverObject);
186190
sMdnsCallbackObject = env->NewGlobalRef(mdnsCallbackObject);
187191
jclass resolverClass = env->GetObjectClass(sResolverObject);
192+
sMdnsCallbackClass = env->GetObjectClass(sMdnsCallbackObject);
188193

189194
VerifyOrReturn(resolverClass != nullptr, ChipLogError(Discovery, "Failed to get Resolver Java class"));
190195

196+
sGetTextEntryKeysMethod = env->GetMethodID(sMdnsCallbackClass, "getTextEntryKeys", "(Ljava/util/Map;)[Ljava/lang/String;");
197+
198+
sGetTextEntryDataMethod = env->GetMethodID(sMdnsCallbackClass, "getTextEntryData", "(Ljava/util/Map;Ljava/lang/String;)[B");
199+
191200
sResolveMethod =
192201
env->GetMethodID(resolverClass, "resolve", "(Ljava/lang/String;Ljava/lang/String;JJLchip/platform/ChipMdnsCallback;)V");
193202
if (sResolveMethod == nullptr)
@@ -196,6 +205,18 @@ void InitializeWithObjects(jobject resolverObject, jobject mdnsCallbackObject)
196205
env->ExceptionClear();
197206
}
198207

208+
if (sGetTextEntryKeysMethod == nullptr)
209+
{
210+
ChipLogError(Discovery, "Failed to access MdnsCallback 'getTextEntryKeys' method");
211+
env->ExceptionClear();
212+
}
213+
214+
if (sGetTextEntryDataMethod == nullptr)
215+
{
216+
ChipLogError(Discovery, "Failed to access MdnsCallback 'getTextEntryData' method");
217+
env->ExceptionClear();
218+
}
219+
199220
sPublishMethod =
200221
env->GetMethodID(resolverClass, "publish",
201222
"(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;I[Ljava/lang/String;[[B[Ljava/lang/String;)V");
@@ -213,7 +234,8 @@ void InitializeWithObjects(jobject resolverObject, jobject mdnsCallbackObject)
213234
}
214235
}
215236

216-
void HandleResolve(jstring instanceName, jstring serviceType, jstring address, jint port, jlong callbackHandle, jlong contextHandle)
237+
void HandleResolve(jstring instanceName, jstring serviceType, jstring address, jint port, jobject textEntries, jlong callbackHandle,
238+
jlong contextHandle)
217239
{
218240
VerifyOrReturn(callbackHandle != 0, ChipLogError(Discovery, "HandleResolve called with callback equal to nullptr"));
219241

@@ -241,9 +263,74 @@ void HandleResolve(jstring instanceName, jstring serviceType, jstring address, j
241263
DnssdService service = {};
242264
CopyString(service.mName, jniInstanceName.c_str());
243265
CopyString(service.mType, jniServiceType.c_str());
244-
service.mPort = static_cast<uint16_t>(port);
266+
service.mPort = static_cast<uint16_t>(port);
267+
service.mTextEntrySize = 0;
268+
service.mTextEntries = nullptr;
269+
270+
// Note on alloc/free memory use
271+
// We are only allocating the entries list and the data field of each entry
272+
// so we free these in the exit section
273+
if (textEntries != nullptr)
274+
{
275+
jobjectArray keys = (jobjectArray) env->CallObjectMethod(sMdnsCallbackObject, sGetTextEntryKeysMethod, textEntries);
276+
size_t size = env->GetArrayLength(keys);
277+
TextEntry * entries = new (std::nothrow) TextEntry[size];
278+
VerifyOrExit(entries != nullptr, ChipLogError(Discovery, "entries alloc failure"));
279+
memset(entries, 0, sizeof(entries[0]) * size);
280+
281+
service.mTextEntries = entries;
282+
for (size_t i = 0; i < size; i++)
283+
{
284+
jstring jniKeyObject = (jstring) env->GetObjectArrayElement(keys, i);
285+
JniUtfString key(env, jniKeyObject);
286+
entries[i].mKey = strdup(key.c_str());
287+
288+
jbyteArray datas =
289+
(jbyteArray) env->CallObjectMethod(sMdnsCallbackObject, sGetTextEntryDataMethod, textEntries, jniKeyObject);
290+
if (datas != nullptr)
291+
{
292+
size_t dataSize = env->GetArrayLength(datas);
293+
uint8_t * data = new (std::nothrow) uint8_t[dataSize];
294+
VerifyOrExit(data != nullptr, ChipLogError(Discovery, "data alloc failure"));
295+
296+
jbyte * jnidata = env->GetByteArrayElements(datas, nullptr);
297+
for (size_t j = 0; j < dataSize; j++)
298+
{
299+
data[j] = static_cast<uint8_t>(jnidata[j]);
300+
}
301+
entries[i].mDataSize = dataSize;
302+
entries[i].mData = data;
303+
304+
ChipLogProgress(Discovery, " ----- entry [%u] : %s %s\n", static_cast<unsigned int>(i), entries[i].mKey,
305+
std::string(reinterpret_cast<char *>(data), dataSize).c_str());
306+
}
307+
else
308+
{
309+
ChipLogProgress(Discovery, " ----- entry [%u] : %s NULL\n", static_cast<unsigned int>(i), entries[i].mKey);
310+
311+
entries[i].mDataSize = 0;
312+
entries[i].mData = nullptr;
313+
}
314+
service.mTextEntrySize = size;
315+
}
316+
}
245317

318+
exit:
246319
dispatch(CHIP_NO_ERROR, &service, &ipAddress);
320+
321+
if (service.mTextEntries != nullptr)
322+
{
323+
size_t size = service.mTextEntrySize;
324+
for (size_t i = 0; i < size; i++)
325+
{
326+
delete[] service.mTextEntries[i].mKey;
327+
if (service.mTextEntries[i].mData != nullptr)
328+
{
329+
delete[] service.mTextEntries[i].mData;
330+
}
331+
}
332+
delete[] service.mTextEntries;
333+
}
247334
}
248335

249336
} // namespace Dnssd

src/platform/android/DnssdImpl.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ void InitializeWithObjects(jobject resolverObject, jobject chipMdnsCallbackObjec
3232
/**
3333
* Pass results of the service resolution to the CHIP stack.
3434
*/
35-
void HandleResolve(jstring instanceName, jstring serviceType, jstring address, jint port, jlong callbackHandle,
35+
void HandleResolve(jstring instanceName, jstring serviceType, jstring address, jint port, jobject textEntries, jlong callbackHandle,
3636
jlong contextHandle);
3737

3838
} // namespace Dnssd

src/platform/android/java/chip/platform/ChipMdnsCallback.java

+3
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,16 @@
1717
*/
1818
package chip.platform;
1919

20+
import java.util.Map;
21+
2022
/** Interface for communicating with the CHIP mDNS stack. */
2123
public interface ChipMdnsCallback {
2224
void handleServiceResolve(
2325
String instanceName,
2426
String serviceType,
2527
String address,
2628
int port,
29+
Map<String, byte[]> textEntries,
2730
long callbackHandle,
2831
long contextHandle);
2932
}

src/platform/android/java/chip/platform/ChipMdnsCallbackImpl.java

+11
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,23 @@
1717
*/
1818
package chip.platform;
1919

20+
import java.util.Map;
21+
2022
public class ChipMdnsCallbackImpl implements ChipMdnsCallback {
2123
public native void handleServiceResolve(
2224
String instanceName,
2325
String serviceType,
2426
String address,
2527
int port,
28+
Map<String, byte[]> textEntries,
2629
long callbackHandle,
2730
long contextHandle);
31+
32+
public String[] getTextEntryKeys(Map<String, byte[]> textEntries) {
33+
return textEntries.keySet().toArray(new String[textEntries.size()]);
34+
}
35+
36+
public byte[] getTextEntryData(Map<String, byte[]> textEntries, String key) {
37+
return textEntries.get(key);
38+
}
2839
}

src/platform/android/java/chip/platform/NsdManagerServiceResolver.java

+10-2
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,13 @@ public void resolve(
5858
NsdServiceInfo serviceInfo = new NsdServiceInfo();
5959
serviceInfo.setServiceName(instanceName);
6060
serviceInfo.setServiceType(serviceType);
61-
Log.d(TAG, "Starting service resolution for '" + instanceName + "'");
61+
Log.d(
62+
TAG,
63+
"resolve: Starting service resolution for '"
64+
+ instanceName
65+
+ "' type '"
66+
+ serviceType
67+
+ "'");
6268

6369
Runnable timeoutRunnable =
6470
new Runnable() {
@@ -67,6 +73,7 @@ public void run() {
6773
// Ensure we always release the multicast lock. It's possible that we release the
6874
// multicast lock here before ResolveListener returns, but since NsdManager has no API
6975
// to cancel service resolution, there's not much we can do here.
76+
Log.d(TAG, "resolve: Timing out");
7077
if (multicastLock.isHeld()) {
7178
multicastLock.release();
7279
}
@@ -82,7 +89,7 @@ public void onResolveFailed(NsdServiceInfo serviceInfo, int errorCode) {
8289
TAG,
8390
"Failed to resolve service '" + serviceInfo.getServiceName() + "': " + errorCode);
8491
chipMdnsCallback.handleServiceResolve(
85-
instanceName, serviceType, null, 0, callbackHandle, contextHandle);
92+
instanceName, serviceType, null, 0, null, callbackHandle, contextHandle);
8693

8794
if (multicastLock.isHeld()) {
8895
multicastLock.release();
@@ -104,6 +111,7 @@ public void onServiceResolved(NsdServiceInfo serviceInfo) {
104111
serviceType,
105112
serviceInfo.getHost().getHostAddress(),
106113
serviceInfo.getPort(),
114+
serviceInfo.getAttributes(),
107115
callbackHandle,
108116
contextHandle);
109117

0 commit comments

Comments
 (0)