1 // Copyright 2020 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14
15 #include "pw_kvs/crc16_checksum.h"
16 #include "pw_kvs/fake_flash_memory.h"
17 #include "pw_kvs/flash_memory.h"
18 #include "pw_kvs/key_value_store.h"
19 #include "pw_kvs/test_key_value_store.h"
20
21 namespace pw::kvs {
22
23 namespace {
24
25 #ifndef PW_FLASH_TEST_SECTORS
26 #define PW_FLASH_TEST_SECTORS 8U
27 #endif // PW_FLASH_TEST_SECTORS
28
29 #ifndef PW_FLASH_TEST_SECTOR_SIZE
30 #define PW_FLASH_TEST_SECTOR_SIZE (4 * 1024U)
31 #endif // PW_FLASH_TEST_SECTOR_SIZE
32
33 #ifndef PW_FLASH_TEST_ALIGNMENT
34 #define PW_FLASH_TEST_ALIGNMENT 16U
35 #endif // PW_FLASH_TEST_ALIGNMENT
36
37 #ifndef PW_KVS_TEST_MAX_ENTIRES
38 #define PW_KVS_TEST_MAX_ENTIRES 32U
39 #endif // PW_KVS_TEST_MAX_ENTIRES
40
41 #ifndef PW_KVS_TEST_REDUNDANCY
42 #define PW_KVS_TEST_REDUNDANCY 1U
43 #endif // PW_KVS_TEST_REDUNDANCY
44
45 constexpr size_t kFlashTestSectors = PW_FLASH_TEST_SECTORS;
46 constexpr size_t kFlashTestSectorSize = PW_FLASH_TEST_SECTOR_SIZE;
47 constexpr size_t kFlashTestAlignment = PW_FLASH_TEST_ALIGNMENT;
48
49 constexpr size_t kKvsTestMaxEntries = PW_KVS_TEST_MAX_ENTIRES;
50 constexpr size_t kKvsTestRedundancy = PW_KVS_TEST_REDUNDANCY;
51
52 // Default to 8 x 4k sectors, 16 byte alignment.
53 FakeFlashMemoryBuffer<kFlashTestSectorSize, kFlashTestSectors> test_flash(
54 kFlashTestAlignment);
55 FlashPartition test_partition(&test_flash);
56
57 ChecksumCrc16 kvs_checksum;
58
59 // For KVS magic value always use a random 32 bit integer rather than a human
60 // readable 4 bytes. See pw_kvs/format.h for more information.
61 constexpr EntryFormat kvs_format = {.magic = 0xc40fd8a8,
62 .checksum = &kvs_checksum};
63
64 KeyValueStoreBuffer<kKvsTestMaxEntries, kFlashTestSectors, kKvsTestRedundancy>
65 test_kvs(&test_partition, kvs_format);
66
67 } // namespace
68
TestKvs()69 KeyValueStore& TestKvs() {
70 if (!test_kvs.initialized()) {
71 test_kvs.Init();
72 }
73
74 return test_kvs;
75 }
76 } // namespace pw::kvs
77