blob: d5fc15f55e13b33ad4fc10c27360dba0845a922f [file]
/*
*
* Copyright (c) 2025 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <app/server/JointFabricDatastore.h>
#include <app/server/Server.h>
#include <lib/core/CHIPError.h>
#include <map>
#include <memory>
namespace chip {
class JFADatastoreSync : public app::JointFabricDatastore::Delegate
{
public:
JFADatastoreSync() {}
CHIP_ERROR Init(Server & server);
/* app::JointFabricDatastore::Delegate */
CHIP_ERROR
SyncNode(NodeId nodeId,
const app::Clusters::JointFabricDatastore::Structs::DatastoreEndpointGroupIDEntryStruct::Type & endpointGroupIDEntry,
std::function<void()> onSuccess) override;
CHIP_ERROR
SyncNode(NodeId nodeId,
const app::Clusters::JointFabricDatastore::Structs::DatastoreNodeKeySetEntryStruct::Type & nodeKeySetEntry,
std::function<void()> onSuccess) override;
CHIP_ERROR
SyncNode(NodeId nodeId,
const app::Clusters::JointFabricDatastore::Structs::DatastoreEndpointBindingEntryStruct::Type & bindingEntry,
std::function<void()> onSuccess) override;
CHIP_ERROR
SyncNode(NodeId nodeId,
std::vector<app::Clusters::JointFabricDatastore::Structs::DatastoreEndpointBindingEntryStruct::Type> & bindingEntries,
std::function<void()> onSuccess) override;
CHIP_ERROR SyncNode(NodeId nodeId, const app::Clusters::JointFabricDatastore::Structs::DatastoreACLEntryStruct::Type & aclEntry,
std::function<void()> onSuccess) override;
CHIP_ERROR SyncNode(NodeId nodeId,
const std::vector<app::Clusters::JointFabricDatastore::Structs::DatastoreACLEntryStruct::Type> & aclEntries,
std::function<void()> onSuccess) override;
CHIP_ERROR SyncNode(NodeId nodeId,
const app::Clusters::JointFabricDatastore::Structs::DatastoreGroupKeySetStruct::Type & groupKeySet,
std::function<void()> onSuccess) override;
CHIP_ERROR FetchEndpointList(
NodeId nodeId,
std::function<void(CHIP_ERROR,
const std::vector<app::Clusters::JointFabricDatastore::Structs::DatastoreEndpointEntryStruct::Type> &)>
onSuccess) override;
CHIP_ERROR FetchEndpointGroupList(
NodeId nodeId, EndpointId endpointId,
std::function<
void(CHIP_ERROR,
const std::vector<app::Clusters::JointFabricDatastore::Structs::DatastoreGroupInformationEntryStruct::Type> &)>
onSuccess) override;
CHIP_ERROR FetchEndpointBindingList(
NodeId nodeId, EndpointId endpointId,
std::function<
void(CHIP_ERROR,
const std::vector<app::Clusters::JointFabricDatastore::Structs::DatastoreEndpointBindingEntryStruct::Type> &)>
onSuccess) override;
CHIP_ERROR FetchGroupKeySetList(NodeId nodeId,
std::function<void(CHIP_ERROR, const std::vector<uint16_t> &)> onSuccess) override;
CHIP_ERROR FetchGroupKeySet(
NodeId nodeId, uint16_t groupKeySetID,
std::function<void(CHIP_ERROR, const app::Clusters::JointFabricDatastore::Structs::DatastoreGroupKeySetStruct::Type &)>
onSuccess) override;
CHIP_ERROR FetchACLList(
NodeId nodeId,
std::function<void(CHIP_ERROR,
const std::vector<app::Clusters::JointFabricDatastore::Structs::DatastoreACLEntryStruct::Type> &)>
onSuccess) override;
private:
friend JFADatastoreSync & JFADSync(void);
static JFADatastoreSync sJFDS;
Server * mServer = nullptr;
// Process-local in-flight command storage keyed by a monotonically increasing token.
// The token is an internal lifetime key only: it is not persisted, protocol-visible,
// or derived from NodeId/command contents.
// uint64_t is intentional so wraparound is practically impossible over process lifetime.
std::map<uint64_t, std::shared_ptr<void>> mInFlightCommands;
uint64_t mNextInFlightCommandToken = 1;
uint64_t AllocateInFlightCommandToken() { return mNextInFlightCommandToken++; }
// Helper to store command and ensure it stays alive until callbacks complete.
template <typename T>
void StoreInFlightCommand(uint64_t token, std::shared_ptr<T> command)
{
mInFlightCommands[token] = std::static_pointer_cast<void>(command);
}
// Helper to remove command after callbacks complete.
void RemoveInFlightCommand(uint64_t token) { mInFlightCommands.erase(token); }
};
inline JFADatastoreSync & JFADSync(void)
{
return JFADatastoreSync::sJFDS;
}
} // namespace chip