Skip to content

Commit 4fe39f8

Browse files
committed
Add code to update ACL after app is installed
1 parent 28430f1 commit 4fe39f8

File tree

6 files changed

+121
-2
lines changed

6 files changed

+121
-2
lines changed

examples/tv-app/android/App/content-app/src/main/AndroidManifest.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
<meta-data android:name="com.matter.tv.app.api.clusters" android:resource="@raw/static_matter_clusters" />
2020
<meta-data android:name="com.matter.tv.app.api.vendor_name" android:value="CSA" />
2121
<meta-data android:name="com.matter.tv.app.api.vendor_id" android:value="65521" />
22-
<meta-data android:name="com.matter.tv.app.api.product_id" android:value="5678" />
22+
<meta-data android:name="com.matter.tv.app.api.product_id" android:value="32769" />
2323
<activity
2424
android:name="com.example.contentapp.MainActivity"
2525
android:exported="true">

examples/tv-app/android/java/AppImpl.cpp

+76
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,59 @@ ContentApp * ContentAppFactoryImpl::LoadContentApp(const CatalogVendorApp & vend
355355
return nullptr;
356356
}
357357

358+
class DevicePairedCommand : public Controller::DevicePairingDelegate
359+
{
360+
public:
361+
struct CallbackContext
362+
{
363+
uint16_t vendorId;
364+
uint16_t productId;
365+
chip::NodeId nodeId;
366+
367+
CallbackContext(uint16_t vId, uint16_t pId, chip::NodeId nId)
368+
: vendorId(vId), productId(pId), nodeId(nId) {}
369+
};
370+
DevicePairedCommand(uint16_t vendorId, uint16_t productId, chip::NodeId nodeId) :
371+
mOnDeviceConnectedCallback(OnDeviceConnectedFn, this),
372+
mOnDeviceConnectionFailureCallback(OnDeviceConnectionFailureFn, this)
373+
{
374+
mContext = std::make_shared<CallbackContext>(vendorId, productId, nodeId);
375+
}
376+
377+
static void OnDeviceConnectedFn(void * context, chip::Messaging::ExchangeManager & exchangeMgr,
378+
const chip::SessionHandle & sessionHandle)
379+
{
380+
auto * pairingCommand = static_cast<DevicePairedCommand *>(context);
381+
auto cbContext = pairingCommand->mContext;
382+
383+
if (pairingCommand)
384+
{
385+
ChipLogProgress(DeviceLayer, "OnDeviceConnectedFn - Updating ACL for node id: %llu and vendor id: %d and product id: %d", cbContext->nodeId, cbContext->vendorId, cbContext->productId);
386+
387+
GetCommissionerDiscoveryController()->CommissioningSucceeded(cbContext->vendorId, cbContext->productId, cbContext->nodeId, exchangeMgr, sessionHandle);
388+
}
389+
}
390+
391+
392+
static void OnDeviceConnectionFailureFn(void * context, const ScopedNodeId & peerId, CHIP_ERROR error)
393+
{
394+
auto * pairingCommand = static_cast<DevicePairedCommand *>(context);
395+
auto cbContext = pairingCommand->mContext;
396+
397+
if (pairingCommand)
398+
{
399+
ChipLogProgress(DeviceLayer, "OnDeviceConnectionFailureFn - Not updating ACL for node id: %llu and vendor id: %d and product id: %d", cbContext->nodeId, cbContext->vendorId, cbContext->productId);
400+
// TODO: Remove Node Id
401+
}
402+
}
403+
404+
chip::Callback::Callback<chip::OnDeviceConnected> mOnDeviceConnectedCallback;
405+
chip::Callback::Callback<chip::OnDeviceConnectionFailure> mOnDeviceConnectionFailureCallback;
406+
std::shared_ptr<CallbackContext> mContext;
407+
};
408+
409+
410+
358411
EndpointId ContentAppFactoryImpl::AddContentApp(const char * szVendorName, uint16_t vendorId, const char * szApplicationName,
359412
uint16_t productId, const char * szApplicationVersion,
360413
std::vector<SupportedCluster> supportedClusters, jobject manager)
@@ -369,6 +422,17 @@ EndpointId ContentAppFactoryImpl::AddContentApp(const char * szVendorName, uint1
369422
app->GetEndpointId());
370423
mContentApps.push_back(app);
371424
mDataVersions.push_back(dataVersionBuf);
425+
426+
std::set<NodeId> nodeIds = ContentAppPlatform::GetInstance().GetNodeIdsForContentApp(vendorId, productId);
427+
428+
for (const auto& nodeId : nodeIds) {
429+
430+
ChipLogProgress(DeviceLayer, "Creating Pairing Command with node id: %llu and vendor id: %d and product id: %d", nodeId, vendorId, productId);
431+
432+
std::shared_ptr<DevicePairedCommand> pairingCommand = std::make_shared<DevicePairedCommand>(vendorId, productId, nodeId);
433+
434+
GetDeviceCommissioner()->GetConnectedDevice(nodeId, &pairingCommand->mOnDeviceConnectedCallback, &pairingCommand->mOnDeviceConnectionFailureCallback);
435+
}
372436
return epId;
373437
}
374438

@@ -387,6 +451,18 @@ EndpointId ContentAppFactoryImpl::AddContentApp(const char * szVendorName, uint1
387451
app->GetEndpointId());
388452
mContentApps.push_back(app);
389453
mDataVersions.push_back(dataVersionBuf);
454+
455+
std::set<NodeId> nodeIds = ContentAppPlatform::GetInstance().GetNodeIdsForContentApp(vendorId, productId);
456+
457+
for (const auto& nodeId : nodeIds) {
458+
459+
ChipLogProgress(DeviceLayer, "Creating Pairing Command with node id: %llu and vendor id: %d and product id: %d", nodeId, vendorId, productId);
460+
461+
std::shared_ptr<DevicePairedCommand> pairingCommand = std::make_shared<DevicePairedCommand>(vendorId, productId, nodeId);
462+
463+
GetDeviceCommissioner()->GetConnectedDevice(nodeId, &pairingCommand->mOnDeviceConnectedCallback, &pairingCommand->mOnDeviceConnectionFailureCallback);
464+
}
465+
390466
return epId;
391467
}
392468

examples/tv-app/android/java/TVApp-JNI.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,8 @@ class MyPostCommissioningListener : public PostCommissioningListener
258258
// read current binding list
259259
chip::Controller::ClusterBase cluster(exchangeMgr, sessionHandle, kTargetBindingClusterEndpointId);
260260

261+
ContentAppPlatform::GetInstance().StoreNodeIdForContentApp(vendorId, productId, nodeId);
262+
261263
cacheContext(vendorId, productId, nodeId, exchangeMgr, sessionHandle);
262264

263265
CHIP_ERROR err =

src/app/app-platform/ContentAppPlatform.cpp

+31
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,37 @@ ContentApp * ContentAppPlatform::GetContentApp(EndpointId id)
387387
return nullptr;
388388
}
389389

390+
// create a string key from vendorId and productId
391+
std::string createKey(uint16_t vendorId, uint16_t productId) {
392+
return std::to_string(vendorId) + ":" + std::to_string(productId);
393+
}
394+
395+
void ContentAppPlatform::StoreNodeIdForContentApp(uint16_t vendorId, uint16_t productId, NodeId nodeId)
396+
{
397+
std::string key = createKey(vendorId, productId);
398+
399+
ChipLogProgress(DeviceLayer, "Stored node id: %llu for key: %s", nodeId, key.c_str());
400+
401+
mConnectedContentAppNodeIds[key].insert(nodeId);
402+
}
403+
404+
std::set<NodeId> ContentAppPlatform::GetNodeIdsForContentApp(uint16_t vendorId, uint16_t productId)
405+
{
406+
std::string key = createKey(vendorId, productId);
407+
408+
ChipLogProgress(DeviceLayer, "Retrieving node id for key: %s", key.c_str());
409+
410+
auto it = mConnectedContentAppNodeIds.find(key);
411+
if (it != mConnectedContentAppNodeIds.end()) {
412+
ChipLogProgress(DeviceLayer, "Found node id");
413+
return it->second;
414+
}
415+
416+
ChipLogProgress(DeviceLayer, "Didn't find node id");
417+
// If key not found, return an empty set
418+
return {};
419+
}
420+
390421
void ContentAppPlatform::SetCurrentApp(ContentApp * app)
391422
{
392423
if (!HasCurrentApp())

src/app/app-platform/ContentAppPlatform.h

+10
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,14 @@ class DLL_EXPORT ContentAppPlatform
161161
bool HasTargetContentApp(uint16_t vendorId, uint16_t productId, CharSpan rotatingId,
162162
Protocols::UserDirectedCommissioning::TargetAppInfo & info, uint32_t & passcode);
163163

164+
// returns set of connected nodes for a given content app
165+
std::set<NodeId> GetNodeIdsForContentApp(uint16_t vendorId, uint16_t productId);
166+
167+
// store node id for content app after commissioning
168+
// node id can be used later on to update ACL
169+
// in case app is not installed
170+
void StoreNodeIdForContentApp(uint16_t vendorId, uint16_t productId, NodeId nodeId);
171+
164172
/**
165173
* @brief
166174
* Add ACLs on this device for the given client,
@@ -201,6 +209,8 @@ class DLL_EXPORT ContentAppPlatform
201209
EndpointId mCurrentEndpointId;
202210
EndpointId mFirstDynamicEndpointId;
203211
ContentApp * mContentApps[CHIP_DEVICE_CONFIG_DYNAMIC_ENDPOINT_COUNT];
212+
// key is string -> vendorId:producTid
213+
std::map<std::string, std::set<NodeId>> mConnectedContentAppNodeIds;
204214

205215
private:
206216
void IncrementCurrentEndpointID();

0 commit comments

Comments
 (0)