-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
FabricSync example changed to use ScopedNodeId in some locations #35805
Changes from 4 commits
e8a7224
899ebb1
ed52460
737dedd
cbb6c6e
856218e
66c36ff
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* | ||
* Copyright (c) 2024 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. | ||
*/ | ||
|
||
#include "BridgeAdminDeviceMapper.h" | ||
|
||
#include <limits> | ||
|
||
#include <lib/support/CodeUtils.h> | ||
|
||
std::optional<uint64_t> BridgeAdminDeviceMapper::AddScopedNodeId(const chip::ScopedNodeId & scopedNodeId) | ||
{ | ||
// We are assuming that we will never run out of HandleIds | ||
VerifyOrDie(mNextHandleId != std::numeric_limits<uint64_t>::max()); | ||
VerifyOrReturnValue(mScopedNodeIdToHandleId.find(scopedNodeId) == mScopedNodeIdToHandleId.end(), std::nullopt); | ||
|
||
uint64_t handleId = mNextHandleId; | ||
mHandleIdToScopedNodeId[handleId] = scopedNodeId; | ||
mScopedNodeIdToHandleId[scopedNodeId] = handleId; | ||
mNextHandleId++; | ||
return handleId; | ||
} | ||
|
||
void BridgeAdminDeviceMapper::RemoveScopedNodeIdByHandleId(uint64_t handleId) | ||
{ | ||
auto it = mHandleIdToScopedNodeId.find(handleId); | ||
VerifyOrReturn(it != mHandleIdToScopedNodeId.end()); | ||
mScopedNodeIdToHandleId.erase(it->second); | ||
mHandleIdToScopedNodeId.erase(handleId); | ||
} | ||
|
||
std::optional<uint64_t> BridgeAdminDeviceMapper::GetHandleId(const chip::ScopedNodeId & scopedNodeId) | ||
{ | ||
auto scopedNodeIterator = mScopedNodeIdToHandleId.find(scopedNodeId); | ||
VerifyOrReturnValue(scopedNodeIterator != mScopedNodeIdToHandleId.end(), std::nullopt); | ||
return scopedNodeIterator->second; | ||
} | ||
|
||
std::optional<chip::ScopedNodeId> BridgeAdminDeviceMapper::GetScopedNodeId(uint64_t handleId) | ||
{ | ||
auto it = mHandleIdToScopedNodeId.find(handleId); | ||
VerifyOrReturnValue(it != mHandleIdToScopedNodeId.end(), std::nullopt); | ||
return it->second; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
* | ||
* Copyright (c) 2024 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 <memory> | ||
#include <optional> | ||
#include <set> | ||
#include <unordered_map> | ||
|
||
#include <lib/core/DataModelTypes.h> | ||
#include <lib/core/ScopedNodeId.h> | ||
|
||
struct ScopedNodeIdHasher | ||
{ | ||
std::size_t operator()(const chip::ScopedNodeId & scopedNodeId) const | ||
{ | ||
std::size_t h1 = std::hash<uint64_t>{}(scopedNodeId.GetFabricIndex()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The NodeId within ScopedNodeId is 64-bit, so is it possible for different ScopedNodeId instances to map to the same hash value, even if the chances are minimized? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Collisions are fine. Overall a hash table is best case O(1), when there isn't a collision and worst case of O(n) when there are collisions all the time https://en.wikipedia.org/wiki/Hash_table#Collision_resolution unordered_map's default hashing funtion already maps to size_t right so already there is always a risk of a collision with anything you have ever used in the past |
||
std::size_t h2 = std::hash<uint64_t>{}(scopedNodeId.GetNodeId()); | ||
// Bitshifting h2 reduces collisions where fabricIndex == nodeId resulting | ||
// in hash return of 0. | ||
return h1 ^ (h2 << 1); | ||
} | ||
}; | ||
|
||
// Bi-directional translation between handle for aggregator and information about the | ||
// the device required for fabric admin to communicate with local device. | ||
class BridgeAdminDeviceMapper | ||
{ | ||
public: | ||
std::optional<uint64_t> AddScopedNodeId(const chip::ScopedNodeId & scopedNodeId); | ||
void RemoveScopedNodeIdByHandleId(uint64_t handleId); | ||
|
||
std::optional<uint64_t> GetHandleId(const chip::ScopedNodeId & scopedNodeId); | ||
std::optional<chip::ScopedNodeId> GetScopedNodeId(uint64_t handleId); | ||
|
||
private: | ||
uint64_t mNextHandleId = 0; | ||
// If we ever need more data other than ScopedNodeId we can change | ||
// mHandleIdToScopedNodeId value from ScopedNodeId to AggregatorDeviceInfo. | ||
std::unordered_map<uint64_t, chip::ScopedNodeId> mHandleIdToScopedNodeId; | ||
std::unordered_map<chip::ScopedNodeId, uint64_t, ScopedNodeIdHasher> mScopedNodeIdToHandleId; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am open to alternative naming suggestion
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ScopedNodeIdHandleMapper? Reflects the role of mapping between ScopedNodeId and handles. But I am not clear why can't we just use ScopedNodeId directly.