Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / transport / AdminPairingTable.cpp
1 /*
2  *
3  *    Copyright (c) 2021 Project CHIP Authors
4  *
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
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17
18 /**
19  * @brief Defines a table of admins that have provisioned the device.
20  */
21
22 #include <core/CHIPEncoding.h>
23 #include <support/CHIPMem.h>
24 #include <support/SafeInt.h>
25 #include <transport/AdminPairingTable.h>
26
27 namespace chip {
28 namespace Transport {
29
30 CHIP_ERROR AdminPairingInfo::StoreIntoKVS(PersistentStorageDelegate & kvs)
31 {
32     char key[KeySize()];
33     ReturnErrorOnFailure(GenerateKey(mAdmin, key, sizeof(key)));
34
35     StorableAdminPairingInfo info;
36     info.mNodeId = Encoding::LittleEndian::HostSwap64(mNodeId);
37     info.mAdmin  = Encoding::LittleEndian::HostSwap16(mAdmin);
38
39     return kvs.SyncSetKeyValue(key, &info, sizeof(info));
40 }
41
42 CHIP_ERROR AdminPairingInfo::FetchFromKVS(PersistentStorageDelegate & kvs)
43 {
44     char key[KeySize()];
45     ReturnErrorOnFailure(GenerateKey(mAdmin, key, sizeof(key)));
46
47     StorableAdminPairingInfo info;
48
49     uint16_t size = sizeof(info);
50     ReturnErrorOnFailure(kvs.SyncGetKeyValue(key, &info, size));
51
52     mNodeId    = Encoding::LittleEndian::HostSwap64(info.mNodeId);
53     AdminId id = Encoding::LittleEndian::HostSwap16(info.mAdmin);
54     ReturnErrorCodeIf(mAdmin != id, CHIP_ERROR_INCORRECT_STATE);
55
56     return CHIP_NO_ERROR;
57 }
58
59 CHIP_ERROR AdminPairingInfo::DeleteFromKVS(PersistentStorageDelegate & kvs, AdminId id)
60 {
61     char key[KeySize()];
62     ReturnErrorOnFailure(GenerateKey(id, key, sizeof(key)));
63
64     kvs.AsyncDeleteKeyValue(key);
65     return CHIP_NO_ERROR;
66 }
67
68 constexpr size_t AdminPairingInfo::KeySize()
69 {
70     return sizeof(kAdminTableKeyPrefix) + 2 * sizeof(AdminId);
71 }
72
73 CHIP_ERROR AdminPairingInfo::GenerateKey(AdminId id, char * key, size_t len)
74 {
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);
79     return CHIP_NO_ERROR;
80 }
81
82 AdminPairingInfo * AdminPairingTable::AssignAdminId(AdminId adminId)
83 {
84     for (size_t i = 0; i < CHIP_CONFIG_MAX_DEVICE_ADMINS; i++)
85     {
86         if (!mStates[i].IsInitialized())
87         {
88             mStates[i].SetAdminId(adminId);
89
90             return &mStates[i];
91         }
92     }
93
94     return nullptr;
95 }
96
97 AdminPairingInfo * AdminPairingTable::AssignAdminId(AdminId adminId, NodeId nodeId)
98 {
99     AdminPairingInfo * admin = AssignAdminId(adminId);
100
101     if (admin != nullptr)
102     {
103         admin->SetNodeId(nodeId);
104     }
105
106     return admin;
107 }
108
109 void AdminPairingTable::ReleaseAdminId(AdminId adminId)
110 {
111     AdminPairingInfo * admin = FindAdmin(adminId);
112     if (admin != nullptr)
113     {
114         admin->Reset();
115     }
116 }
117
118 AdminPairingInfo * AdminPairingTable::FindAdmin(AdminId adminId)
119 {
120     for (size_t i = 0; i < CHIP_CONFIG_MAX_DEVICE_ADMINS; i++)
121     {
122         if (mStates[i].IsInitialized() && mStates[i].GetAdminId() == adminId)
123         {
124             return &mStates[i];
125         }
126     }
127
128     return nullptr;
129 }
130
131 void AdminPairingTable::Reset()
132 {
133     for (size_t i = 0; i < CHIP_CONFIG_MAX_DEVICE_ADMINS; i++)
134     {
135         mStates[i].Reset();
136     }
137 }
138
139 } // namespace Transport
140 } // namespace chip