Skip to content

Commit

Permalink
Discovery updates
Browse files Browse the repository at this point in the history
  • Loading branch information
sharadb-amazon committed Sep 13, 2023
1 parent d904628 commit d9b5eb7
Show file tree
Hide file tree
Showing 14 changed files with 228 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,17 @@ public DiscoveredNodeData(NsdServiceInfo serviceInfo) {
this.numIPs = 1;
}

public DiscoveredNodeData(VideoPlayer player) {
this.connectableVideoPlayer = player;
this.hostName = player.getHostName();
this.deviceName = player.getDeviceName();
this.deviceType = player.getDeviceType();
this.vendorId = player.getVendorId();
this.productId = player.getProductId();
this.ipAddresses = player.getIpAddresses();
this.port = player.getPort();
}

void setConnectableVideoPlayer(VideoPlayer videoPlayer) {
this.connectableVideoPlayer = videoPlayer;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,17 @@
import chip.platform.PreferencesKeyValueStoreManager;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class TvCastingApp {
private static final String TAG = TvCastingApp.class.getSimpleName();
private static final String DISCOVERY_TARGET_SERVICE_TYPE = "_matterd._udp.";
private static final List<Long> DISCOVERY_TARGET_DEVICE_TYPE_FILTER =
Arrays.asList(35L); // Video player = 35;

private static final long STR_CACHE_LAST_DISCOVERED_DAYS = 60;

private static TvCastingApp sInstance;
private Context applicationContext;
private ChipAppServer chipAppServer;
Expand Down Expand Up @@ -146,13 +150,37 @@ public void discoverVideoPlayerCommissioners(
discoveryFailureCallback,
nsdManagerResolverAvailState);

Executors.newScheduledThreadPool(1)
.schedule(
() -> {
reportSleepingCommissioners(preCommissionedVideoPlayers, discoverySuccessCallback);
},
5000,
TimeUnit.MILLISECONDS);

nsdManager.discoverServices(
DISCOVERY_TARGET_SERVICE_TYPE, NsdManager.PROTOCOL_DNS_SD, nsdDiscoveryListener);
Log.d(TAG, "TvCastingApp.discoverVideoPlayerCommissioners started");
this.discoveryStarted = true;
}
}

private void reportSleepingCommissioners(
List<VideoPlayer> undiscoveredVideoPlayers,
SuccessCallback<DiscoveredNodeData> discoverySuccessCallback) {
if (undiscoveredVideoPlayers != null) {
for (VideoPlayer player : undiscoveredVideoPlayers) {
// report a player if we got its MAC address previously and it was recently discoverable
if (player.getMACAddress() != null
&& player.getLastDiscoveredMs()
> System.currentTimeMillis()
- STR_CACHE_LAST_DISCOVERED_DAYS * 24 * 60 * 60 * 1000) {
discoverySuccessCallback.handle(new DiscoveredNodeData(player));
}
}
}
}

public void stopVideoPlayerDiscovery() {
synchronized (discoveryLock) {
Log.d(TAG, "TvCastingApp trying to stop video player discovery");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,19 @@ public class VideoPlayer {
private long nodeId;
private byte fabricIndex;
private String deviceName;
private String instanceName;
private int vendorId;
private int productId;
private int deviceType;
private List<ContentApp> contentApps;
private long lastDiscoveredMs;
private String MACAddress;
private boolean isConnected = false;

private int numIPs;
private List<InetAddress> ipAddresses;
private String hostName;
private int port;

private boolean isInitialized = false;

Expand All @@ -52,6 +56,10 @@ public VideoPlayer(
int numIPs,
List<InetAddress> ipAddresses,
String hostName,
String instanceName,
int port,
long lastDiscoveredMs,
String MACAddress,
boolean isConnected) {
this.nodeId = nodeId;
this.fabricIndex = fabricIndex;
Expand All @@ -64,6 +72,10 @@ public VideoPlayer(
this.numIPs = numIPs;
this.ipAddresses = ipAddresses;
this.hostName = hostName;
this.MACAddress = MACAddress;
this.lastDiscoveredMs = lastDiscoveredMs;
this.instanceName = instanceName;
this.port = port;
this.isInitialized = true;
}

Expand Down Expand Up @@ -116,8 +128,8 @@ public int hashCode() {
return Objects.hash(super.hashCode(), nodeId, fabricIndex);
}

@java.lang.Override
public java.lang.String toString() {
@Override
public String toString() {
return "VideoPlayer{"
+ "nodeId="
+ nodeId
Expand All @@ -134,16 +146,23 @@ public java.lang.String toString() {
+ deviceType
+ ", contentApps="
+ contentApps
+ ", lastDiscoveredMs="
+ lastDiscoveredMs
+ ", MACAddress='"
+ MACAddress
+ '\''
+ ", isConnected="
+ isConnected
+ ", numIPs="
+ numIPs
+ ", ipAddresses="
+ ipAddresses
+ ", isInitialized="
+ ", hostName='"
+ hostName
+ '\''
+ ", port="
+ port
+ ", isInitialized="
+ isInitialized
+ '}';
}
Expand Down Expand Up @@ -180,6 +199,30 @@ public int getDeviceType() {
return deviceType;
}

public int getNumIPs() {
return numIPs;
}

public List<InetAddress> getIpAddresses() {
return ipAddresses;
}

public String getHostName() {
return hostName;
}

public int getPort() {
return port;
}

public long getLastDiscoveredMs() {
return lastDiscoveredMs;
}

public String getMACAddress() {
return MACAddress;
}

public boolean isInitialized() {
return isInitialized;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#include <lib/core/CHIPError.h>
#include <lib/support/JniReferences.h>
#include <lib/support/JniTypeWrappers.h>
#include <string.h>
#include <system/SystemClock.h>

CHIP_ERROR convertJAppParametersToCppAppParams(jobject appParameters, AppParams & outAppParams)
{
Expand Down Expand Up @@ -147,6 +149,9 @@ CHIP_ERROR convertJVideoPlayerToTargetVideoPlayerInfo(jobject videoPlayer, Targe
jfieldID jDeviceType = env->GetFieldID(jVideoPlayerClass, "deviceType", "I");
uint16_t deviceType = static_cast<uint16_t>(env->GetIntField(videoPlayer, jDeviceType));

jfieldID jPort = env->GetFieldID(jVideoPlayerClass, "port", "I");
uint16_t port = static_cast<uint16_t>(env->GetIntField(videoPlayer, jPort));

jfieldID getDeviceNameField = env->GetFieldID(jVideoPlayerClass, "deviceName", "Ljava/lang/String;");
jstring jDeviceName = static_cast<jstring>(env->GetObjectField(videoPlayer, getDeviceNameField));
const char * deviceName = env->GetStringUTFChars(jDeviceName, 0);
Expand All @@ -155,8 +160,25 @@ CHIP_ERROR convertJVideoPlayerToTargetVideoPlayerInfo(jobject videoPlayer, Targe
jstring jHostName = static_cast<jstring>(env->GetObjectField(videoPlayer, getHostNameField));
const char * hostName = env->GetStringUTFChars(jHostName, 0);

jfieldID getInstanceNameField = env->GetFieldID(jVideoPlayerClass, "instanceName", "Ljava/lang/String;");
jstring jInstanceName = static_cast<jstring>(env->GetObjectField(videoPlayer, getInstanceNameField));
const char * instanceName = env->GetStringUTFChars(jInstanceName, 0);

jfieldID jLastDiscoveredMs = env->GetFieldID(jVideoPlayerClass, "lastDiscoveredMs", "J");
long lastDiscoveredMs = static_cast<uint16_t>(env->GetIntField(videoPlayer, jLastDiscoveredMs));

jfieldID getMACAddressField = env->GetFieldID(jVideoPlayerClass, "MACAddress", "Ljava/lang/String;");
jstring jMACAddress = static_cast<jstring>(env->GetObjectField(videoPlayer, getMACAddressField));
const char * MACAddress = env->GetStringUTFChars(jMACAddress, 0);

outTargetVideoPlayerInfo.Initialize(nodeId, fabricIndex, nullptr, nullptr, vendorId, productId, deviceType, deviceName,
hostName);
hostName, 0, nullptr, port, instanceName, chip::System::Clock::Timestamp(lastDiscoveredMs));

if (MACAddress != nullptr)
{
chip::CharSpan MACAddressSpan(MACAddress, strlen(MACAddress));
outTargetVideoPlayerInfo.SetMACAddress(MACAddressSpan);
}

jfieldID jContentAppsField = env->GetFieldID(jVideoPlayerClass, "contentApps", "Ljava/util/List;");
jobject jContentApps = env->GetObjectField(videoPlayer, jContentAppsField);
Expand Down Expand Up @@ -218,6 +240,20 @@ CHIP_ERROR convertTargetVideoPlayerInfoToJVideoPlayer(TargetVideoPlayerInfo * ta
jstring hostName =
targetVideoPlayerInfo->GetHostName() == nullptr ? nullptr : env->NewStringUTF(targetVideoPlayerInfo->GetHostName());

jstring instanceName = targetVideoPlayerInfo->GetInstanceName() == nullptr
? nullptr
: env->NewStringUTF(targetVideoPlayerInfo->GetInstanceName());

jstring MACAddress = nullptr;
if (targetVideoPlayerInfo->GetMACAddress() != nullptr && targetVideoPlayerInfo->GetMACAddress()->data() != nullptr)
{
char MACAddressWithNil[2 * chip::DeviceLayer::ConfigurationManager::kPrimaryMACAddressLength + 1];
strncpy(MACAddressWithNil, targetVideoPlayerInfo->GetMACAddress()->data(),
targetVideoPlayerInfo->GetMACAddress()->size());
MACAddressWithNil[targetVideoPlayerInfo->GetMACAddress()->size()] = '\0';
MACAddress = env->NewStringUTF(MACAddressWithNil);
}

jobject jIPAddressList = nullptr;
const chip::Inet::IPAddress * ipAddresses = targetVideoPlayerInfo->GetIpAddresses();
if (ipAddresses != nullptr)
Expand All @@ -240,11 +276,12 @@ CHIP_ERROR convertTargetVideoPlayerInfoToJVideoPlayer(TargetVideoPlayerInfo * ta
}
}

outVideoPlayer = env->NewObject(jVideoPlayerClass, jVideoPlayerConstructor, targetVideoPlayerInfo->GetNodeId(),
targetVideoPlayerInfo->GetFabricIndex(), deviceName, targetVideoPlayerInfo->GetVendorId(),
targetVideoPlayerInfo->GetProductId(), targetVideoPlayerInfo->GetDeviceType(),
jContentAppList, targetVideoPlayerInfo->GetNumIPs(), jIPAddressList, hostName,
targetVideoPlayerInfo->GetOperationalDeviceProxy() != nullptr);
outVideoPlayer = env->NewObject(
jVideoPlayerClass, jVideoPlayerConstructor, targetVideoPlayerInfo->GetNodeId(), targetVideoPlayerInfo->GetFabricIndex(),
deviceName, targetVideoPlayerInfo->GetVendorId(), targetVideoPlayerInfo->GetProductId(),
targetVideoPlayerInfo->GetDeviceType(), jContentAppList, targetVideoPlayerInfo->GetNumIPs(), jIPAddressList, hostName,
instanceName, targetVideoPlayerInfo->GetPort(), MACAddress, targetVideoPlayerInfo->GetLastDiscovered().count(),
targetVideoPlayerInfo->GetOperationalDeviceProxy() != nullptr);
}
return CHIP_NO_ERROR;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#import "ConversionUtils.hpp"
#include <controller/DeviceDiscoveryDelegate.h>
#include <system/SystemClock.h>

class CommissionerDiscoveryDelegateImpl : public chip::Controller::DeviceDiscoveryDelegate {
public:
Expand All @@ -44,6 +45,8 @@ class CommissionerDiscoveryDelegateImpl : public chip::Controller::DeviceDiscove
if (mCachedTargetVideoPlayerInfos != nullptr) {
for (size_t i = 0; i < kMaxCachedVideoPlayers && mCachedTargetVideoPlayerInfos[i].IsInitialized(); i++) {
if (mCachedTargetVideoPlayerInfos[i].IsSameAs(&cppNodeData)) {
mCachedTargetVideoPlayerInfos[i].SetLastDiscovered(
chip::System::SystemClock().GetMonotonicMilliseconds64()); // add discovery timestamp
VideoPlayer * connectableVideoPlayer =
[ConversionUtils convertToObjCVideoPlayerFrom:&mCachedTargetVideoPlayerInfos[i]];
[objCDiscoveredNodeData setConnectableVideoPlayer:connectableVideoPlayer];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ + (CHIP_ERROR)convertToCppTargetVideoPlayerInfoFrom:(VideoPlayer * _Nonnull)objC
{
VerifyOrReturnError(objCVideoPlayer.isInitialized, CHIP_ERROR_INVALID_ARGUMENT);
ReturnErrorOnFailure(outTargetVideoPlayerInfo.Initialize(objCVideoPlayer.nodeId, objCVideoPlayer.fabricIndex, nullptr, nullptr,
objCVideoPlayer.vendorId, objCVideoPlayer.productId, objCVideoPlayer.deviceType, [objCVideoPlayer.deviceName UTF8String]));
objCVideoPlayer.vendorId, objCVideoPlayer.productId, objCVideoPlayer.deviceType, [objCVideoPlayer.deviceName UTF8String],
[objCVideoPlayer.hostName UTF8String], 0, nullptr, objCVideoPlayer.port, objCVideoPlayer.instanceName,
chip::System::Clock::Timestamp(objCVideoPlayer.lastDiscoveredMs)));
for (ContentApp * contentApp in objCVideoPlayer.contentApps) {
TargetEndpointInfo * endpoint = outTargetVideoPlayerInfo.GetOrAddEndpoint(contentApp.endpointId);
VerifyOrReturnError(endpoint != nullptr, CHIP_ERROR_INCORRECT_STATE);
Expand Down Expand Up @@ -155,6 +157,13 @@ + (VideoPlayer *)convertToObjCVideoPlayerFrom:(TargetVideoPlayerInfo * _Nonnull)
objCVideoPlayer.isConnected = (cppTargetVideoPlayerInfo->GetOperationalDeviceProxy() != nil);
objCVideoPlayer.deviceName = [NSString stringWithCString:cppTargetVideoPlayerInfo->GetDeviceName()
encoding:NSUTF8StringEncoding];
objCVideoPlayer.port = cppTargetVideoPlayerInfo->GetPort();
objCVideoPlayer.lastDiscoveredMs = cppTargetVideoPlayerInfo->GetLastDiscovered().count();
objCVideoPlayer.instanceName = [NSString stringWithCString:cppTargetVideoPlayerInfo->GetInstanceName()
encoding:NSUTF8StringEncoding];
objCVideoPlayer.hostName = [NSString stringWithCString:cppTargetVideoPlayerInfo->GetHostName()
encoding:NSUTF8StringEncoding];

objCVideoPlayer.contentApps = [NSMutableArray new];
TargetEndpointInfo * cppTargetEndpointInfos = cppTargetVideoPlayerInfo->GetEndpoints();
for (size_t i = 0; i < kMaxNumberOfEndpoints && cppTargetEndpointInfos[i].IsInitialized(); i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@

@property uint16_t deviceType;

@property NSString * hostName;

@property NSString * instanceName;

@property uint16_t port;

@property uint64_t lastDiscoveredMs;

/**
@brief true, if all the required fields are initialized, false otherwise
*/
Expand All @@ -54,7 +62,11 @@
deviceName:(NSString *)deviceName
vendorId:(uint16_t)vendorId
productId:(uint16_t)productId
deviceType:(uint16_t)deviceType;
deviceType:(uint16_t)deviceType
hostName:(NSString *)hostName
instanceName:(NSString *)instanceName
port:(uint16_t)port
lastDiscoveredMs:(uint64_t)lastDiscoveredMs;

@end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ - (instancetype)initWithNodeId:(uint64_t)nodeId
deviceName:(NSString *)deviceName
vendorId:(uint16_t)vendorId
productId:(uint16_t)productId
deviceType:(uint16_t)deviceType;
deviceType:(uint16_t)deviceType
hostName:(NSString *)hostName
instanceName:(NSString *)instanceName
port:(uint16_t)port
lastDiscoveredMs:(uint64_t)lastDiscoveredMs
{
if (self = [super init]) {
_nodeId = nodeId;
Expand All @@ -47,6 +51,10 @@ - (instancetype)initWithNodeId:(uint64_t)nodeId
_vendorId = vendorId;
_productId = productId;
_deviceType = deviceType;
_hostName = hostName;
_instanceName = instanceName;
_port = port;
_lastDiscoveredMs = lastDiscoveredMs;
_isInitialized = true;
}
return self;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -459,12 +459,14 @@ class CastingServer : public AppDelegate
chip::Dnssd::DiscoveredNodeData mStrNodeDataList[kMaxCachedVideoPlayers];
TargetVideoPlayerInfo mActiveTargetVideoPlayerInfo;
TargetVideoPlayerInfo mCachedTargetVideoPlayerInfo[kMaxCachedVideoPlayers];
uint16_t mTargetVideoPlayerVendorId = 0;
uint16_t mTargetVideoPlayerProductId = 0;
chip::DeviceTypeId mTargetVideoPlayerDeviceType = 0;
char mTargetVideoPlayerDeviceName[chip::Dnssd::kMaxDeviceNameLen + 1] = {};
char mTargetVideoPlayerHostName[chip::Dnssd::kHostNameMaxLength + 1] = {};
size_t mTargetVideoPlayerNumIPs = 0; // number of valid IP addresses
uint16_t mTargetVideoPlayerVendorId = 0;
uint16_t mTargetVideoPlayerProductId = 0;
uint16_t mTargetVideoPlayerPort = 0;
chip::DeviceTypeId mTargetVideoPlayerDeviceType = 0;
char mTargetVideoPlayerDeviceName[chip::Dnssd::kMaxDeviceNameLen + 1] = {};
char mTargetVideoPlayerHostName[chip::Dnssd::kHostNameMaxLength + 1] = {};
char mTargetVideoPlayerInstanceName[chip::Dnssd::Commission::kInstanceNameMaxLength + 1] = {};
size_t mTargetVideoPlayerNumIPs = 0; // number of valid IP addresses
chip::Inet::IPAddress mTargetVideoPlayerIpAddress[chip::Dnssd::CommonResolutionData::kMaxIPAddresses];

chip::Controller::CommissionableNodeController mCommissionableNodeController;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ class PersistenceManager : public chip::FabricTable::Delegate
kIpAddressesContainerTag,
kVideoPlayerLastDiscoveredTag,
kVideoPlayerMACAddressTag,
kVideoPlayerInstanceNameTag,
kVideoPlayerPortTag,

kContextTagMaxNum = UINT8_MAX
};
Expand Down
Loading

0 comments on commit d9b5eb7

Please sign in to comment.