Skip to content

Commit

Permalink
tv-casting-app: Synchronizing on the use of NsdManager.resolveService…
Browse files Browse the repository at this point in the history
…() (#23549)

* tv-casting-app: Synchronizing on the use of NsdManager.resolveService()

* Making AppParameters.discriminator private
  • Loading branch information
sharadb-amazon authored Nov 10, 2022
1 parent 2075b45 commit 599ad3b
Show file tree
Hide file tree
Showing 12 changed files with 157 additions and 86 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.chip.casting.app;

import android.content.Context;
import android.net.nsd.NsdManager;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
Expand Down Expand Up @@ -69,8 +67,6 @@ public void onClick(View v) {
manualCommissioningButton.setOnClickListener(manualCommissioningButtonOnClickListener);

Context context = this.getContext();
Context applicationContext = this.getContext().getApplicationContext();

SuccessCallback<DiscoveredNodeData> successCallback =
new SuccessCallback<DiscoveredNodeData>() {
@Override
Expand Down Expand Up @@ -112,11 +108,7 @@ public void handle(MatterError matterError) {
};

tvCastingApp.discoverVideoPlayerCommissioners(
(WifiManager) context.getSystemService(Context.WIFI_SERVICE),
(NsdManager) applicationContext.getSystemService(Context.NSD_SERVICE),
DISCOVERY_DURATION_SECS,
successCallback,
failureCallback);
DISCOVERY_DURATION_SECS, successCallback, failureCallback);
}

@VisibleForTesting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,6 @@
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import chip.appserver.ChipAppServer;
import chip.platform.AndroidBleManager;
import chip.platform.AndroidChipPlatform;
import chip.platform.ChipMdnsCallbackImpl;
import chip.platform.DiagnosticDataProviderImpl;
import chip.platform.NsdManagerServiceBrowser;
import chip.platform.NsdManagerServiceResolver;
import chip.platform.PreferencesConfigurationManager;
import chip.platform.PreferencesKeyValueStoreManager;
import com.chip.casting.AppParameters;
import com.chip.casting.DACProviderStub;
import com.chip.casting.DiscoveredNodeData;
Expand All @@ -29,7 +20,6 @@ public class MainActivity extends AppCompatActivity

private static final String TAG = MainActivity.class.getSimpleName();

private ChipAppServer chipAppServer;
private TvCastingApp tvCastingApp;

@Override
Expand Down Expand Up @@ -79,30 +69,17 @@ public void handleDisconnect() {
private void initJni() {
tvCastingApp = new TvCastingApp();

tvCastingApp.setDACProvider(new DACProviderStub());
Context applicationContext = this.getApplicationContext();
AndroidChipPlatform chipPlatform =
new AndroidChipPlatform(
new AndroidBleManager(),
new PreferencesKeyValueStoreManager(applicationContext),
new PreferencesConfigurationManager(applicationContext),
new NsdManagerServiceResolver(applicationContext),
new NsdManagerServiceBrowser(applicationContext),
new ChipMdnsCallbackImpl(),
new DiagnosticDataProviderImpl(applicationContext));

chipPlatform.updateCommissionableDataProviderData(
null, null, 0, GlobalCastingConstants.SetupPasscode, GlobalCastingConstants.Discriminator);

chipAppServer = new ChipAppServer();
chipAppServer.startApp();

AppParameters appParameters = new AppParameters();
byte[] rotatingDeviceIdUniqueId =
new byte[AppParameters.MIN_ROTATING_DEVICE_ID_UNIQUE_ID_LENGTH];
new Random().nextBytes(rotatingDeviceIdUniqueId);
appParameters.setRotatingDeviceIdUniqueId(rotatingDeviceIdUniqueId);
tvCastingApp.init(appParameters);
appParameters.setDacProvider(new DACProviderStub());
appParameters.setSetupPasscode(GlobalCastingConstants.SetupPasscode);
appParameters.setDiscriminator(GlobalCastingConstants.Discriminator);
tvCastingApp.initApp(applicationContext, appParameters);
}

private void showFragment(Fragment fragment, boolean showOnBack) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package com.chip.casting.util;

import java.util.Arrays;
import java.util.List;

public class GlobalCastingConstants {
public static final String CommissionerServiceType = "_matterd._udp.";
public static final int CommissioningWindowDurationSecs = 3 * 60;
public static int SetupPasscode = 20202021;
public static int Discriminator = 0xF00;
public static List<Long> CommissionerDeviceTypeFilter = Arrays.asList(35L); // Video player = 35
public static final int SetupPasscode = 20202021;
public static final int Discriminator = 0xF00;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,14 @@

public class AppParameters {
public static final int MIN_ROTATING_DEVICE_ID_UNIQUE_ID_LENGTH = 16;
private static final int TEST_SETUP_PASSCODE = 20202021;
private static final int TEST_DISCRIMINATOR = 0xF00;
private DACProvider TEST_DAC_PROVIDER = new DACProviderStub();

private byte[] rotatingDeviceIdUniqueId;
private DACProvider dacProvider = TEST_DAC_PROVIDER;
private int setupPasscode = TEST_SETUP_PASSCODE;
private int discriminator = TEST_DISCRIMINATOR;

public void setRotatingDeviceIdUniqueId(byte[] rotatingDeviceIdUniqueId) {
this.rotatingDeviceIdUniqueId = rotatingDeviceIdUniqueId;
Expand All @@ -29,4 +35,28 @@ public void setRotatingDeviceIdUniqueId(byte[] rotatingDeviceIdUniqueId) {
public byte[] getRotatingDeviceIdUniqueId() {
return rotatingDeviceIdUniqueId;
}

public DACProvider getDacProvider() {
return dacProvider;
}

public void setDacProvider(DACProvider dacProvider) {
this.dacProvider = dacProvider;
}

public int getSetupPasscode() {
return setupPasscode;
}

public void setSetupPasscode(int setupPasscode) {
this.setupPasscode = setupPasscode;
}

public int getDiscriminator() {
return discriminator;
}

public void setDiscriminator(int discriminator) {
this.discriminator = discriminator;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import android.net.nsd.NsdManager;
import android.net.nsd.NsdServiceInfo;
import android.util.Log;
import chip.platform.NsdManagerServiceResolver;
import java.util.List;

public class NsdDiscoveryListener implements NsdManager.DiscoveryListener {
Expand All @@ -31,20 +32,23 @@ public class NsdDiscoveryListener implements NsdManager.DiscoveryListener {
private final List<VideoPlayer> preCommissionedVideoPlayers;
private final SuccessCallback<DiscoveredNodeData> successCallback;
private final FailureCallback failureCallback;
private final NsdManagerServiceResolver.NsdManagerResolverAvailState nsdManagerResolverAvailState;

public NsdDiscoveryListener(
NsdManager nsdManager,
String targetServiceType,
List<Long> deviceTypeFilter,
List<VideoPlayer> preCommissionedVideoPlayers,
SuccessCallback<DiscoveredNodeData> successCallback,
FailureCallback failureCallback) {
FailureCallback failureCallback,
NsdManagerServiceResolver.NsdManagerResolverAvailState nsdManagerResolverAvailState) {
this.nsdManager = nsdManager;
this.targetServiceType = targetServiceType;
this.deviceTypeFilter = deviceTypeFilter;
this.preCommissionedVideoPlayers = preCommissionedVideoPlayers;
this.successCallback = successCallback;
this.failureCallback = failureCallback;
this.nsdManagerResolverAvailState = nsdManagerResolverAvailState;
}

@Override
Expand All @@ -56,14 +60,18 @@ public void onDiscoveryStarted(String regType) {
public void onServiceFound(NsdServiceInfo service) {
Log.d(TAG, "Service discovery success. " + service);
if (service.getServiceType().equals(targetServiceType)) {
if (nsdManagerResolverAvailState != null) {
nsdManagerResolverAvailState.acquireResolver();
}
nsdManager.resolveService(
service,
new NsdResolveListener(
nsdManager,
deviceTypeFilter,
preCommissionedVideoPlayers,
successCallback,
failureCallback));
failureCallback,
nsdManagerResolverAvailState));
} else {
Log.d(TAG, "Ignoring discovered service: " + service.toString());
}
Expand All @@ -79,6 +87,9 @@ public void onServiceLost(NsdServiceInfo service) {
@Override
public void onDiscoveryStopped(String serviceType) {
Log.i(TAG, "Discovery stopped: " + serviceType);
if (nsdManagerResolverAvailState != null) {
nsdManagerResolverAvailState.signalFree();
}
}

@Override
Expand All @@ -93,6 +104,9 @@ public void onStartDiscoveryFailed(String serviceType, int errorCode) {
@Override
public void onStopDiscoveryFailed(String serviceType, int errorCode) {
Log.e(TAG, "Discovery failed to stop: Error code:" + errorCode);
if (nsdManagerResolverAvailState != null) {
nsdManagerResolverAvailState.signalFree();
}
failureCallback.handle(
new MatterError(
3, "NsdDiscoveryListener Discovery failed to stop: Nsd Error code:" + errorCode));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import android.net.nsd.NsdManager;
import android.net.nsd.NsdServiceInfo;
import android.util.Log;
import chip.platform.NsdManagerServiceResolver;
import java.util.List;

public class NsdResolveListener implements NsdManager.ResolveListener {
Expand All @@ -31,13 +32,15 @@ public class NsdResolveListener implements NsdManager.ResolveListener {
private final List<VideoPlayer> preCommissionedVideoPlayers;
private final SuccessCallback<DiscoveredNodeData> successCallback;
private final FailureCallback failureCallback;
private final NsdManagerServiceResolver.NsdManagerResolverAvailState nsdManagerResolverAvailState;

public NsdResolveListener(
NsdManager nsdManager,
List<Long> deviceTypeFilter,
List<VideoPlayer> preCommissionedVideoPlayers,
SuccessCallback<DiscoveredNodeData> successCallback,
FailureCallback failureCallback) {
FailureCallback failureCallback,
NsdManagerServiceResolver.NsdManagerResolverAvailState nsdManagerResolverAvailState) {
this.nsdManager = nsdManager;
this.deviceTypeFilter = deviceTypeFilter;
this.preCommissionedVideoPlayers = preCommissionedVideoPlayers;
Expand All @@ -48,13 +51,18 @@ public NsdResolveListener(
}
this.successCallback = successCallback;
this.failureCallback = failureCallback;
this.nsdManagerResolverAvailState = nsdManagerResolverAvailState;
}

@Override
public void onServiceResolved(NsdServiceInfo serviceInfo) {
DiscoveredNodeData discoveredNodeData = new DiscoveredNodeData(serviceInfo);
Log.d(TAG, "DiscoveredNodeData resolved: " + discoveredNodeData);

if (nsdManagerResolverAvailState != null) {
nsdManagerResolverAvailState.signalFree();
}

if (isPassingDeviceTypeFilter(discoveredNodeData)) {
addCommissioningInfo(discoveredNodeData);
successCallback.handle(discoveredNodeData);
Expand All @@ -68,6 +76,10 @@ public void onServiceResolved(NsdServiceInfo serviceInfo) {

@Override
public void onResolveFailed(NsdServiceInfo serviceInfo, int errorCode) {
if (nsdManagerResolverAvailState != null) {
nsdManagerResolverAvailState.signalFree();
}

switch (errorCode) {
case NsdManager.FAILURE_ALREADY_ACTIVE:
Log.e(TAG, "NsdResolveListener FAILURE_ALREADY_ACTIVE - Service: " + serviceInfo);
Expand Down
Loading

0 comments on commit 599ad3b

Please sign in to comment.