3 * Copyright (c) 2021 Project CHIP Authors
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 * @brief Defines a table of admins that have provisioned the device.
22 #include <core/CHIPEncoding.h>
23 #include <support/CHIPMem.h>
24 #include <support/SafeInt.h>
25 #include <transport/AdminPairingTable.h>
30 CHIP_ERROR AdminPairingInfo::StoreIntoKVS(PersistentStorageDelegate & kvs)
33 ReturnErrorOnFailure(GenerateKey(mAdmin, key, sizeof(key)));
35 StorableAdminPairingInfo info;
36 info.mNodeId = Encoding::LittleEndian::HostSwap64(mNodeId);
37 info.mAdmin = Encoding::LittleEndian::HostSwap16(mAdmin);
39 return kvs.SyncSetKeyValue(key, &info, sizeof(info));
42 CHIP_ERROR AdminPairingInfo::FetchFromKVS(PersistentStorageDelegate & kvs)
45 ReturnErrorOnFailure(GenerateKey(mAdmin, key, sizeof(key)));
47 StorableAdminPairingInfo info;
49 uint16_t size = sizeof(info);
50 ReturnErrorOnFailure(kvs.SyncGetKeyValue(key, &info, size));
52 mNodeId = Encoding::LittleEndian::HostSwap64(info.mNodeId);
53 AdminId id = Encoding::LittleEndian::HostSwap16(info.mAdmin);
54 ReturnErrorCodeIf(mAdmin != id, CHIP_ERROR_INCORRECT_STATE);
59 CHIP_ERROR AdminPairingInfo::DeleteFromKVS(PersistentStorageDelegate & kvs, AdminId id)
62 ReturnErrorOnFailure(GenerateKey(id, key, sizeof(key)));
64 kvs.AsyncDeleteKeyValue(key);
68 constexpr size_t AdminPairingInfo::KeySize()
70 return sizeof(kAdminTableKeyPrefix) + 2 * sizeof(AdminId);
73 CHIP_ERROR AdminPairingInfo::GenerateKey(AdminId id, char * key, size_t len)
75 VerifyOrReturnError(len >= KeySize(), CHIP_ERROR_INVALID_ARGUMENT);
76 int keySize = snprintf(key, len, "%s%x", kAdminTableKeyPrefix, id);
77 VerifyOrReturnError(keySize > 0, CHIP_ERROR_INTERNAL);
78 VerifyOrReturnError(len > (size_t) keySize, CHIP_ERROR_INTERNAL);
82 AdminPairingInfo * AdminPairingTable::AssignAdminId(AdminId adminId)
84 for (size_t i = 0; i < CHIP_CONFIG_MAX_DEVICE_ADMINS; i++)
86 if (!mStates[i].IsInitialized())
88 mStates[i].SetAdminId(adminId);
97 AdminPairingInfo * AdminPairingTable::AssignAdminId(AdminId adminId, NodeId nodeId)
99 AdminPairingInfo * admin = AssignAdminId(adminId);
101 if (admin != nullptr)
103 admin->SetNodeId(nodeId);
109 void AdminPairingTable::ReleaseAdminId(AdminId adminId)
111 AdminPairingInfo * admin = FindAdmin(adminId);
112 if (admin != nullptr)
118 AdminPairingInfo * AdminPairingTable::FindAdmin(AdminId adminId)
120 for (size_t i = 0; i < CHIP_CONFIG_MAX_DEVICE_ADMINS; i++)
122 if (mStates[i].IsInitialized() && mStates[i].GetAdminId() == adminId)
131 void AdminPairingTable::Reset()
133 for (size_t i = 0; i < CHIP_CONFIG_MAX_DEVICE_ADMINS; i++)
139 } // namespace Transport