Skip to content

Commit

Permalink
Moving SetRotatingDeviceIdUniqueId to preInitApp so it always happens…
Browse files Browse the repository at this point in the history
… before- Matter server is initialized

iOS tv-casting-app: Changed initApp to initializeApp and added return value
  • Loading branch information
sharadb-amazon committed Jan 12, 2023
1 parent e31ac73 commit 22ebf95
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

initJni();
boolean ret = initJni();
if (!ret) {
Log.e(TAG, "Failed to initialize Matter TV casting library");
return;
}

Fragment fragment = CommissionerDiscoveryFragment.newInstance(tvCastingApp);
getSupportFragmentManager()
Expand Down Expand Up @@ -71,7 +75,7 @@ public void handleDisconnect() {
* AndroidChipPlatform to prepare platform, then start ChipAppServer, then call init on
* TvCastingApp
*/
private void initJni() {
private boolean initJni() {
tvCastingApp = new TvCastingApp();

Context applicationContext = this.getApplicationContext();
Expand All @@ -84,7 +88,7 @@ private void initJni() {
appParameters.setDacProvider(new DACProviderStub());
appParameters.setSetupPasscode(GlobalCastingConstants.SetupPasscode);
appParameters.setDiscriminator(GlobalCastingConstants.Discriminator);
tvCastingApp.initApp(applicationContext, appParameters);
return tvCastingApp.initApp(applicationContext, appParameters);
}

private void showFragment(Fragment fragment, boolean showOnBack) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,22 +65,41 @@ public boolean initApp(Context applicationContext, AppParameters appParameters)
new ChipMdnsCallbackImpl(),
new DiagnosticDataProviderImpl(applicationContext));

chipPlatform.updateCommissionableDataProviderData(
appParameters.getSpake2pVerifierBase64(),
appParameters.getSpake2pSaltBase64(),
appParameters.getSpake2pIterationCount(),
appParameters.getSetupPasscode(),
appParameters.getDiscriminator());
boolean ret =
chipPlatform.updateCommissionableDataProviderData(
appParameters.getSpake2pVerifierBase64(),
appParameters.getSpake2pSaltBase64(),
appParameters.getSpake2pIterationCount(),
appParameters.getSetupPasscode(),
appParameters.getDiscriminator());
if (!ret) {
Log.e(
TAG,
"TvCastingApp.initApp failed to updateCommissionableDataProviderData on chipPlatform");
return ret;
}

ret = preInitJni(appParameters);
if (!ret) {
Log.e(TAG, "TvCastingApp.initApp failed in preInitJni");
return ret;
}

chipAppServer = new ChipAppServer();
chipAppServer.startApp();
ret = chipAppServer.startApp();
if (!ret) {
Log.e(TAG, "TvCastingApp.initApp failed in start chipAppServer");
return ret;
}

setDACProvider(appParameters.getDacProvider());
return initJni(appParameters);
}

private native void setDACProvider(DACProvider provider);

private native boolean preInitJni(AppParameters appParameters);

private native boolean initJni(AppParameters appParameters);

public void discoverVideoPlayerCommissioners(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,37 @@ void JNI_OnUnload(JavaVM * jvm, void * reserved)
return AndroidAppServerJNI_OnUnload(jvm, reserved);
}

JNI_METHOD(jboolean, preInitJni)(JNIEnv *, jobject, jobject jAppParameters)
{
chip::DeviceLayer::StackLock lock;
ChipLogProgress(AppServer, "JNI_METHOD preInitJni called");

CHIP_ERROR err = CHIP_NO_ERROR;
if (jAppParameters == nullptr)
{
err = CastingServer::GetInstance()->PreInit();
}
else
{
AppParams appParams;
err = convertJAppParametersToCppAppParams(jAppParameters, appParams);
VerifyOrExit(err == CHIP_NO_ERROR,
ChipLogError(AppServer, "Conversion of AppParameters from jobject to Cpp type failed: %" CHIP_ERROR_FORMAT,
err.Format()));
err = CastingServer::GetInstance()->PreInit(&appParams);
}
VerifyOrExit(
err == CHIP_NO_ERROR,
ChipLogError(AppServer, "Call to CastingServer::GetInstance()->PreInit() failed: %" CHIP_ERROR_FORMAT, err.Format()));
exit:
if (err != CHIP_NO_ERROR)
{
return false;
}

return true;
}

JNI_METHOD(jboolean, initJni)(JNIEnv *, jobject, jobject jAppParameters)
{
chip::DeviceLayer::StackLock lock;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@

+ (CastingServerBridge * _Nullable)getSharedInstance;

- (void)initApp:(AppParameters * _Nullable)appParameters
clientQueue:(dispatch_queue_t _Nonnull)clientQueue
initAppStatusHandler:(nullable void (^)(bool))initAppStatusHandler;
- (MatterError * _Nonnull)initializeApp:(AppParameters * _Nullable)appParameters
clientQueue:(dispatch_queue_t _Nonnull)clientQueue
initAppStatusHandler:(nullable void (^)(bool))initAppStatusHandler;

/*!
@brief Browse for on-network commissioner TVs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ - (instancetype)init
return self;
}

- (void)initApp:(AppParameters * _Nullable)appParameters
clientQueue:(dispatch_queue_t _Nonnull)clientQueue
initAppStatusHandler:(nullable void (^)(bool))initAppStatusHandler
- (MatterError *)initializeApp:(AppParameters * _Nullable)appParameters
clientQueue:(dispatch_queue_t _Nonnull)clientQueue
initAppStatusHandler:(nullable void (^)(bool))initAppStatusHandler
{
ChipLogProgress(AppServer, "CastingServerBridge().initApp() called");

Expand All @@ -128,7 +128,7 @@ - (void)initApp:(AppParameters * _Nullable)appParameters
err = [ConversionUtils convertToCppAppParamsInfoFrom:_appParameters outAppParams:cppAppParams];
if (err != CHIP_NO_ERROR) {
ChipLogError(AppServer, "AppParameters conversion failed: %s", ErrorStr(err));
return;
return [[MatterError alloc] initWithCode:err.AsInteger() message:[NSString stringWithUTF8String:err.AsString()]];
}

// set fields in commissionableDataProvider
Expand Down Expand Up @@ -156,7 +156,7 @@ - (void)initApp:(AppParameters * _Nullable)appParameters
setupDiscriminator);
if (err != CHIP_NO_ERROR) {
ChipLogError(AppServer, "Failed to initialize CommissionableDataProvider: %s", ErrorStr(err));
return;
return [[MatterError alloc] initWithCode:err.AsInteger() message:[NSString stringWithUTF8String:err.AsString()]];
}

if (_appParameters.deviceAttestationCredentials != nil) {
Expand Down Expand Up @@ -219,13 +219,24 @@ - (void)initApp:(AppParameters * _Nullable)appParameters
err = _serverInitParams->InitializeStaticResourcesBeforeServerInit();
if (err != CHIP_NO_ERROR) {
ChipLogError(AppServer, "InitializeStaticResourcesBeforeServerInit failed: %s", ErrorStr(err));
return;
return [[MatterError alloc] initWithCode:err.AsInteger() message:[NSString stringWithUTF8String:err.AsString()]];
}

AppParams appParam;
if (appParameters == nil) {
err = CastingServer::GetInstance()->PreInit();
} else if ((err = [ConversionUtils convertToCppAppParamsInfoFrom:appParameters outAppParams:appParam]) == CHIP_NO_ERROR) {
err = CastingServer::GetInstance()->PreInit(&appParam);
}
if (err != CHIP_NO_ERROR) {
ChipLogError(AppServer, "CastingServer PreInit failed: %s", ErrorStr(err));
return [[MatterError alloc] initWithCode:err.AsInteger() message:[NSString stringWithUTF8String:err.AsString()]];
}

err = chip::Server::GetInstance().Init(*_serverInitParams);
if (err != CHIP_NO_ERROR) {
ChipLogError(AppServer, "chip::Server init failed: %s", ErrorStr(err));
return;
return [[MatterError alloc] initWithCode:err.AsInteger() message:[NSString stringWithUTF8String:err.AsString()]];
}

_chipWorkQueue = chip::DeviceLayer::PlatformMgrImpl().GetWorkQueue();
Expand All @@ -251,6 +262,9 @@ - (void)initApp:(AppParameters * _Nullable)appParameters
initAppStatusHandler(initAppStatus);
});
});

return [[MatterError alloc] initWithCode:CHIP_NO_ERROR.AsInteger()
message:[NSString stringWithUTF8String:CHIP_NO_ERROR.AsString()]];
}

- (void)discoverCommissioners:(dispatch_queue_t _Nonnull)clientQueue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ struct TvCastingApp: App {

appParameters.onboardingPayload = onboardingParameters

castingServerBridge.initApp(appParameters, clientQueue: DispatchQueue.main, initAppStatusHandler: { (result: Bool) -> () in
self.Log.info("initApp result \(result)")
var err = castingServerBridge.initializeApp(appParameters, clientQueue: DispatchQueue.main, initAppStatusHandler: { (result: Bool) -> () in
self.Log.info("initializeApp result \(result)")
})
self.Log.info("initializeApp return value \(err)")
}
})
.onReceive(NotificationCenter.default.publisher(for: UIApplication.willResignActiveNotification)) { _ in
Expand Down
2 changes: 2 additions & 0 deletions examples/tv-casting-app/linux/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ int main(int argc, char * argv[])
SetDeviceAttestationVerifier(GetDefaultDACVerifier(testingRootStore));
}

SuccessOrExit(err = CastingServer::GetInstance()->PreInit());

// Enter commissioning mode, open commissioning window
static chip::CommonCaseDeviceServerInitParams initParams;
VerifyOrDie(CHIP_NO_ERROR == initParams.InitializeStaticResourcesBeforeServerInit());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class CastingServer
void operator=(const CastingServer &) = delete;
static CastingServer * GetInstance();

CHIP_ERROR PreInit(AppParams * AppParams = nullptr);
CHIP_ERROR Init(AppParams * AppParams = nullptr);
CHIP_ERROR InitBindingHandlers();

Expand Down Expand Up @@ -419,6 +420,8 @@ class CastingServer
static CastingServer * castingServer_;
CastingServer();

CHIP_ERROR SetRotatingDeviceIdUniqueId(chip::Optional<chip::ByteSpan> rotatingDeviceIdUniqueId);

static void DeviceEventCallback(const chip::DeviceLayer::ChipDeviceEvent * event, intptr_t arg);
void ReadServerClusters(chip::EndpointId endpointId);

Expand Down
33 changes: 21 additions & 12 deletions examples/tv-casting-app/tv-casting-common/src/CastingServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,36 @@ CastingServer * CastingServer::GetInstance()
return castingServer_;
}

CHIP_ERROR CastingServer::PreInit(AppParams * AppParams)
{
return SetRotatingDeviceIdUniqueId(AppParams != nullptr ? AppParams->GetRotatingDeviceIdUniqueId() : chip::NullOptional);
}

CHIP_ERROR CastingServer::Init(AppParams * AppParams)
{
if (mInited)
{
return CHIP_NO_ERROR;
}

// Initialize binding handlers
ReturnErrorOnFailure(InitBindingHandlers());

// Add callback to send Content casting commands after commissioning completes
ReturnErrorOnFailure(DeviceLayer::PlatformMgrImpl().AddEventHandler(DeviceEventCallback, 0));

mInited = true;
return CHIP_NO_ERROR;
}

CHIP_ERROR CastingServer::SetRotatingDeviceIdUniqueId(chip::Optional<chip::ByteSpan> rotatingDeviceIdUniqueIdOptional)
{
#if CHIP_ENABLE_ROTATING_DEVICE_ID
// if this class's client provided a RotatingDeviceIdUniqueId, use that
if (AppParams != nullptr && AppParams->GetRotatingDeviceIdUniqueId().HasValue())
if (rotatingDeviceIdUniqueIdOptional.HasValue())
{
ByteSpan rotatingDeviceIdUniqueId(AppParams->GetRotatingDeviceIdUniqueId().Value());
chip::DeviceLayer::ConfigurationMgr().SetRotatingDeviceIdUniqueId(rotatingDeviceIdUniqueId);
ChipLogProgress(AppServer, "Setting unique ID (for generating rotating device ID) received from client app");
return chip::DeviceLayer::ConfigurationMgr().SetRotatingDeviceIdUniqueId(rotatingDeviceIdUniqueIdOptional.Value());
}
#ifdef CHIP_DEVICE_CONFIG_ROTATING_DEVICE_ID_UNIQUE_ID
else
Expand All @@ -61,18 +78,10 @@ CHIP_ERROR CastingServer::Init(AppParams * AppParams)
}

// ByteSpan rotatingDeviceIdUniqueIdSpan(rotatingDeviceIdUniqueId);
chip::DeviceLayer::ConfigurationMgr().SetRotatingDeviceIdUniqueId(ByteSpan(rotatingDeviceIdUniqueId));
return chip::DeviceLayer::ConfigurationMgr().SetRotatingDeviceIdUniqueId(ByteSpan(rotatingDeviceIdUniqueId));
}
#endif // CHIP_DEVICE_CONFIG_ROTATING_DEVICE_ID_UNIQUE_ID
#endif // CHIP_ENABLE_ROTATING_DEVICE_ID

// Initialize binding handlers
ReturnErrorOnFailure(InitBindingHandlers());

// Add callback to send Content casting commands after commissioning completes
ReturnErrorOnFailure(DeviceLayer::PlatformMgrImpl().AddEventHandler(DeviceEventCallback, 0));

mInited = true;
return CHIP_NO_ERROR;
}

Expand Down

0 comments on commit 22ebf95

Please sign in to comment.