1 /*
2  *  Copyright (c) 2019, The OpenThread Authors.
3  *  All rights reserved.
4  *
5  *  Redistribution and use in source and binary forms, with or without
6  *  modification, are permitted provided that the following conditions are met:
7  *  1. Redistributions of source code must retain the above copyright
8  *     notice, this list of conditions and the following disclaimer.
9  *  2. Redistributions in binary form must reproduce the above copyright
10  *     notice, this list of conditions and the following disclaimer in the
11  *     documentation and/or other materials provided with the distribution.
12  *  3. Neither the name of the copyright holder nor the
13  *     names of its contributors may be used to endorse or promote products
14  *     derived from this software without specific prior written permission.
15  *
16  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  *  POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /**
30  * @file
31  *   This file implements OpenThread platform abstraction for storage of settings in RAM.
32  *
33  */
34 
35 #include "settings.h"
36 
37 #include <assert.h>
38 #include <stddef.h>
39 #include <stdlib.h>
40 #include <string.h>
41 
42 #include <openthread/instance.h>
43 #include <openthread/platform/settings.h>
44 
45 #define SETTINGS_BUFFER_SIZE 1024
46 
47 #if OPENTHREAD_SETTINGS_RAM
48 
49 static uint8_t  sSettingsBuf[SETTINGS_BUFFER_SIZE];
50 static uint16_t sSettingsBufLength;
51 
52 OT_TOOL_PACKED_BEGIN
53 struct settingsBlock
54 {
55     uint16_t key;
56     uint16_t length;
57 } OT_TOOL_PACKED_END;
58 
59 // settings API
otPlatSettingsInit(otInstance * aInstance)60 void otPlatSettingsInit(otInstance *aInstance)
61 {
62     OT_UNUSED_VARIABLE(aInstance);
63 
64     sSettingsBufLength = 0;
65 }
66 
otPlatSettingsDeinit(otInstance * aInstance)67 void otPlatSettingsDeinit(otInstance *aInstance)
68 {
69     OT_UNUSED_VARIABLE(aInstance);
70 }
71 
otPlatSettingsGet(otInstance * aInstance,uint16_t aKey,int aIndex,uint8_t * aValue,uint16_t * aValueLength)72 otError otPlatSettingsGet(otInstance *aInstance, uint16_t aKey, int aIndex, uint8_t *aValue, uint16_t *aValueLength)
73 {
74     OT_UNUSED_VARIABLE(aInstance);
75 
76     uint16_t                    i           = 0;
77     uint16_t                    valueLength = 0;
78     uint16_t                    readLength;
79     int                         currentIndex = 0;
80     const struct settingsBlock *currentBlock;
81     otError                     error = OT_ERROR_NOT_FOUND;
82 
83     while (i < sSettingsBufLength)
84     {
85         currentBlock = (struct settingsBlock *)&sSettingsBuf[i];
86 
87         if (aKey == currentBlock->key)
88         {
89             if (currentIndex == aIndex)
90             {
91                 readLength = currentBlock->length;
92 
93                 // Perform read only if an input buffer was passed in
94                 if (aValue != NULL && aValueLength != NULL)
95                 {
96                     // Adjust read length if input buffer size is smaller
97                     if (readLength > *aValueLength)
98                     {
99                         readLength = *aValueLength;
100                     }
101 
102                     memcpy(aValue, &sSettingsBuf[i + sizeof(struct settingsBlock)], readLength);
103                 }
104 
105                 valueLength = currentBlock->length;
106                 error       = OT_ERROR_NONE;
107                 break;
108             }
109 
110             currentIndex++;
111         }
112 
113         i += sizeof(struct settingsBlock) + currentBlock->length;
114     }
115 
116     if (aValueLength != NULL)
117     {
118         *aValueLength = valueLength;
119     }
120 
121     return error;
122 }
123 
otPlatSettingsSet(otInstance * aInstance,uint16_t aKey,const uint8_t * aValue,uint16_t aValueLength)124 otError otPlatSettingsSet(otInstance *aInstance, uint16_t aKey, const uint8_t *aValue, uint16_t aValueLength)
125 {
126     uint16_t                    i = 0;
127     uint16_t                    currentBlockLength;
128     uint16_t                    nextBlockStart;
129     const struct settingsBlock *currentBlock;
130 
131     // Delete all entries of aKey
132     while (i < sSettingsBufLength)
133     {
134         currentBlock       = (struct settingsBlock *)&sSettingsBuf[i];
135         currentBlockLength = sizeof(struct settingsBlock) + currentBlock->length;
136 
137         if (aKey == currentBlock->key)
138         {
139             nextBlockStart = i + currentBlockLength;
140 
141             if (nextBlockStart < sSettingsBufLength)
142             {
143                 memmove(&sSettingsBuf[i], &sSettingsBuf[nextBlockStart], sSettingsBufLength - nextBlockStart);
144             }
145 
146             assert(sSettingsBufLength >= currentBlockLength);
147             sSettingsBufLength -= currentBlockLength;
148         }
149         else
150         {
151             i += currentBlockLength;
152         }
153     }
154 
155     return otPlatSettingsAdd(aInstance, aKey, aValue, aValueLength);
156 }
157 
otPlatSettingsAdd(otInstance * aInstance,uint16_t aKey,const uint8_t * aValue,uint16_t aValueLength)158 otError otPlatSettingsAdd(otInstance *aInstance, uint16_t aKey, const uint8_t *aValue, uint16_t aValueLength)
159 {
160     OT_UNUSED_VARIABLE(aInstance);
161 
162     otError               error;
163     struct settingsBlock *currentBlock;
164     const uint16_t        newBlockLength = sizeof(struct settingsBlock) + aValueLength;
165 
166     if (sSettingsBufLength + newBlockLength <= sizeof(sSettingsBuf))
167     {
168         currentBlock         = (struct settingsBlock *)&sSettingsBuf[sSettingsBufLength];
169         currentBlock->key    = aKey;
170         currentBlock->length = aValueLength;
171 
172         memcpy(&sSettingsBuf[sSettingsBufLength + sizeof(struct settingsBlock)], aValue, aValueLength);
173         sSettingsBufLength += newBlockLength;
174 
175         error = OT_ERROR_NONE;
176     }
177     else
178     {
179         error = OT_ERROR_NO_BUFS;
180     }
181 
182     return error;
183 }
184 
otPlatSettingsDelete(otInstance * aInstance,uint16_t aKey,int aIndex)185 otError otPlatSettingsDelete(otInstance *aInstance, uint16_t aKey, int aIndex)
186 {
187     OT_UNUSED_VARIABLE(aInstance);
188 
189     uint16_t                    i            = 0;
190     int                         currentIndex = 0;
191     uint16_t                    nextBlockStart;
192     uint16_t                    currentBlockLength;
193     const struct settingsBlock *currentBlock;
194     otError                     error = OT_ERROR_NOT_FOUND;
195 
196     while (i < sSettingsBufLength)
197     {
198         currentBlock       = (struct settingsBlock *)&sSettingsBuf[i];
199         currentBlockLength = sizeof(struct settingsBlock) + currentBlock->length;
200 
201         if (aKey == currentBlock->key)
202         {
203             if (currentIndex == aIndex)
204             {
205                 nextBlockStart = i + currentBlockLength;
206 
207                 if (nextBlockStart < sSettingsBufLength)
208                 {
209                     memmove(&sSettingsBuf[i], &sSettingsBuf[nextBlockStart], sSettingsBufLength - nextBlockStart);
210                 }
211 
212                 assert(sSettingsBufLength >= currentBlockLength);
213                 sSettingsBufLength -= currentBlockLength;
214 
215                 error = OT_ERROR_NONE;
216                 break;
217             }
218             else
219             {
220                 currentIndex++;
221             }
222         }
223 
224         i += currentBlockLength;
225     }
226 
227     return error;
228 }
229 
otPlatSettingsWipe(otInstance * aInstance)230 void otPlatSettingsWipe(otInstance *aInstance)
231 {
232     otPlatSettingsInit(aInstance);
233 }
234 
235 #endif // OPENTHREAD_SETTINGS_RAM
236