• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "pw_sync/borrow.h"
21 #include "pw_sync/virtual_basic_lockable.h"
22 
23 namespace pw::kvs {
24 
25 namespace {
26 
27 #ifndef PW_FLASH_TEST_SECTORS
28 #define PW_FLASH_TEST_SECTORS 8U
29 #endif  // PW_FLASH_TEST_SECTORS
30 
31 #ifndef PW_FLASH_TEST_SECTOR_SIZE
32 #define PW_FLASH_TEST_SECTOR_SIZE (4 * 1024U)
33 #endif  // PW_FLASH_TEST_SECTOR_SIZE
34 
35 #ifndef PW_FLASH_TEST_ALIGNMENT
36 #define PW_FLASH_TEST_ALIGNMENT 16U
37 #endif  // PW_FLASH_TEST_ALIGNMENT
38 
39 #ifndef PW_KVS_TEST_MAX_ENTIRES
40 #define PW_KVS_TEST_MAX_ENTIRES 32U
41 #endif  // PW_KVS_TEST_MAX_ENTIRES
42 
43 #ifndef PW_KVS_TEST_REDUNDANCY
44 #define PW_KVS_TEST_REDUNDANCY 1U
45 #endif  // PW_KVS_TEST_REDUNDANCY
46 
47 constexpr size_t kFlashTestSectors = PW_FLASH_TEST_SECTORS;
48 constexpr size_t kFlashTestSectorSize = PW_FLASH_TEST_SECTOR_SIZE;
49 constexpr size_t kFlashTestAlignment = PW_FLASH_TEST_ALIGNMENT;
50 
51 constexpr size_t kKvsTestMaxEntries = PW_KVS_TEST_MAX_ENTIRES;
52 constexpr size_t kKvsTestRedundancy = PW_KVS_TEST_REDUNDANCY;
53 
54 // Default to 8 x 4k sectors, 16 byte alignment.
55 FakeFlashMemoryBuffer<kFlashTestSectorSize, kFlashTestSectors> test_flash(
56     kFlashTestAlignment);
57 FlashPartition test_partition(&test_flash);
58 
59 ChecksumCrc16 kvs_checksum;
60 
61 // For KVS magic value always use a random 32 bit integer rather than a human
62 // readable 4 bytes. See pw_kvs/format.h for more information.
63 constexpr EntryFormat kvs_format = {.magic = 0xc40fd8a8,
64                                     .checksum = &kvs_checksum};
65 
66 KeyValueStoreBuffer<kKvsTestMaxEntries, kFlashTestSectors, kKvsTestRedundancy>
67     test_kvs(&test_partition, kvs_format);
68 sync::Borrowable<KeyValueStore> borrowable_kvs(test_kvs,
69                                                sync::NoOpLock::Instance());
70 
71 }  // namespace
72 
TestKvs()73 sync::Borrowable<KeyValueStore>& TestKvs() {
74   if (!test_kvs.initialized()) {
75     test_kvs.Init()
76         .IgnoreError();  // TODO: b/242598609 - Handle Status properly
77   }
78 
79   return borrowable_kvs;
80 }
81 }  // namespace pw::kvs
82