[mdns] add support for advertising subtypes

This commit adds support of advertising and discovering services
with subtypes.

With this change, client can publish a new service with subtypes with
code:
```
NsdServiceInfo info = new NsdServiceInfo();
info.setServiceName("My Service");
info.setServiceType("_meshcop._udp");
info.setSubtypes(Set.of("_abc"));

nsdManager.registerService(info, PROTOCOL_DNS_SD, listener);
```

Bug: 265095929
Test: atest CtsNetTestCases FrameworksNetTests
Change-Id: Ia83182e2d43002243d1ef4357d14a8056d7da14b
This commit is contained in:
Kangping Dong 2023-12-08 15:49:00 +08:00
commit 6d618d77fe

View file

@ -18,6 +18,7 @@ package android.net.nsd;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
@ -40,6 +41,7 @@ import java.net.InetAddress;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
@RunWith(DevSdkIgnoreRunner.class)
@SmallTest
@ -114,6 +116,7 @@ public class NsdServiceInfoTest {
NsdServiceInfo fullInfo = new NsdServiceInfo();
fullInfo.setServiceName("kitten");
fullInfo.setServiceType("_kitten._tcp");
fullInfo.setSubtypes(Set.of("_thread", "_matter"));
fullInfo.setPort(4242);
fullInfo.setHostAddresses(List.of(IPV4_ADDRESS));
fullInfo.setNetwork(new Network(123));
@ -149,7 +152,7 @@ public class NsdServiceInfoTest {
assertFalse(attributedInfo.getAttributes().keySet().contains("sticky"));
}
public void checkParcelable(NsdServiceInfo original) {
private static void checkParcelable(NsdServiceInfo original) {
// Write to parcel.
Parcel p = Parcel.obtain();
Bundle writer = new Bundle();
@ -179,11 +182,20 @@ public class NsdServiceInfoTest {
}
}
public void assertEmptyServiceInfo(NsdServiceInfo shouldBeEmpty) {
private static void assertEmptyServiceInfo(NsdServiceInfo shouldBeEmpty) {
byte[] txtRecord = shouldBeEmpty.getTxtRecord();
if (txtRecord == null || txtRecord.length == 0) {
return;
}
fail("NsdServiceInfo.getTxtRecord did not return null but " + Arrays.toString(txtRecord));
}
@Test
public void testSubtypesValidSubtypesSuccess() {
NsdServiceInfo info = new NsdServiceInfo();
info.setSubtypes(Set.of("_thread", "_matter"));
assertEquals(Set.of("_thread", "_matter"), info.getSubtypes());
}
}