a2176f2bc63b80a8149a71c033769e2d0d25d90c
[platform/upstream/connectedhomeip.git] / src / controller / CHIPPersistentStorageDelegate.h
1 /*
2  *
3  *    Copyright (c) 2020 Project CHIP Authors
4  *    All rights reserved.
5  *
6  *    Licensed under the Apache License, Version 2.0 (the "License");
7  *    you may not use this file except in compliance with the License.
8  *    You may obtain a copy of the License at
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *    Unless required by applicable law or agreed to in writing, software
13  *    distributed under the License is distributed on an "AS IS" BASIS,
14  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *    See the License for the specific language governing permissions and
16  *    limitations under the License.
17  */
18
19 #pragma once
20
21 #include <core/CHIPCore.h>
22 #include <support/DLLUtil.h>
23
24 namespace chip {
25 namespace Controller {
26
27 class DLL_EXPORT PersistentStorageResultDelegate
28 {
29 public:
30     enum class Operation : uint8_t
31     {
32         kGET = 0,
33         kSET,
34         kDELETE,
35     };
36
37     virtual ~PersistentStorageResultDelegate() {}
38
39     /**
40      * @brief
41      *   Called when a value is returned from the storage.
42      *   This is useful for GetKeyValue() API call.
43      *
44      * @param[in] key Key for which the value is being returned
45      * @param[in] value Value or nullptr if not found.
46      */
47     virtual void OnValue(const char * key, const char * value) = 0;
48
49     /**
50      * @brief
51      *   Called on completion of an operation in PersistentStorageDelegate API
52      *
53      * @param[in] key Key for which the status is being returned
54      * @param[in] op Operation that was being performed on the key
55      * @param[in] result CHIP_NO_ERROR or corresponding error code
56      */
57     virtual void OnStatus(const char * key, Operation op, CHIP_ERROR result) = 0;
58 };
59
60 class DLL_EXPORT PersistentStorageDelegate
61 {
62 public:
63     virtual ~PersistentStorageDelegate() {}
64
65     /**
66      * @brief
67      *   Set the callback object with methods that are called on completion
68      *   of the operation.
69      *
70      * @param[in] delegate The callback object
71      */
72     virtual void SetDelegate(PersistentStorageResultDelegate * delegate) = 0;
73
74     /**
75      * @brief
76      *   Lookup the key and call delegate object with it's stringified value
77      *
78      * @param[in] key Key to lookup
79      */
80     virtual void GetKeyValue(const char * key) = 0;
81
82     /**
83      * @brief
84      *   This is a synchronous Get API, where the value is returned via the output
85      *   buffer. This API should be used sparingly, since it may block for
86      *   some duration.
87      *
88      * @param[in]      key Key to lookup
89      * @param[out]     value Value for the key
90      * @param[in, out] size Input value buffer size, output length of value.
91      *                 The output length could be larger than input value. In
92      *                 such cases, the user should allocate the buffer large
93      *                 enough (>= output length), and call the API again.
94      */
95     virtual CHIP_ERROR GetKeyValue(const char * key, char * value, uint16_t & size) { return CHIP_ERROR_NOT_IMPLEMENTED; }
96
97     /**
98      * @brief
99      *   Set the value for the key
100      *
101      * @param[in] key Key to be set
102      * @param[in] value Value to be set
103      */
104     virtual void SetKeyValue(const char * key, const char * value) = 0;
105
106     /**
107      * @brief
108      *   Deletes the value for the key
109      *
110      * @param[in] key Key to be deleted
111      */
112     virtual void DeleteKeyValue(const char * key) = 0;
113 };
114
115 } // namespace Controller
116 } // namespace chip