David Rogers | 17793d6 | 2021-02-05 03:29:02 -0800 | [diff] [blame] | 1 | // Copyright 2021 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 <cstring> |
| 16 | |
Wyatt Hepler | f298de4 | 2021-03-19 15:06:36 -0700 | [diff] [blame] | 17 | #include "pw_assert/check.h" |
David Rogers | 17793d6 | 2021-02-05 03:29:02 -0800 | [diff] [blame] | 18 | #include "pw_bloat/bloat_this_binary.h" |
| 19 | #include "pw_blob_store/blob_store.h" |
| 20 | #include "pw_kvs/flash_test_partition.h" |
| 21 | #include "pw_kvs/key_value_store.h" |
| 22 | #include "pw_log/log.h" |
| 23 | |
| 24 | char working_buffer[256]; |
| 25 | volatile bool is_set; |
| 26 | |
| 27 | constexpr size_t kMaxSectorCount = 64; |
| 28 | constexpr size_t kKvsMaxEntries = 32; |
| 29 | |
| 30 | // For KVS magic value always use a random 32 bit integer rather than a human |
| 31 | // readable 4 bytes. See pw_kvs/format.h for more information. |
| 32 | static constexpr pw::kvs::EntryFormat kvs_format = {.magic = 0x22d3f8a0, |
| 33 | .checksum = nullptr}; |
| 34 | |
| 35 | volatile size_t kvs_entry_count; |
| 36 | |
| 37 | pw::kvs::KeyValueStoreBuffer<kKvsMaxEntries, kMaxSectorCount> test_kvs( |
| 38 | &pw::kvs::FlashTestPartition(), kvs_format); |
| 39 | |
| 40 | int volatile* unoptimizable; |
| 41 | |
| 42 | int main() { |
| 43 | pw::bloat::BloatThisBinary(); |
| 44 | |
| 45 | // Start of base ********************** |
| 46 | // Ensure we are paying the cost for log and assert. |
| 47 | PW_CHECK_INT_GE(*unoptimizable, 0, "Ensure this CHECK logic stays"); |
| 48 | PW_LOG_INFO("We care about optimizing: %d", *unoptimizable); |
| 49 | |
| 50 | void* result = |
| 51 | std::memset((void*)working_buffer, sizeof(working_buffer), 0x55); |
| 52 | is_set = (result != nullptr); |
| 53 | |
| 54 | test_kvs.Init(); |
| 55 | |
| 56 | unsigned kvs_value = 42; |
| 57 | test_kvs.Put("example_key", kvs_value); |
| 58 | |
| 59 | kvs_entry_count = test_kvs.size(); |
| 60 | |
| 61 | unsigned read_value = 0; |
| 62 | test_kvs.Get("example_key", &read_value); |
| 63 | test_kvs.Delete("example_key"); |
| 64 | |
| 65 | auto val = pw::kvs::FlashTestPartition().PartitionAddressToMcuAddress(0); |
| 66 | PW_LOG_INFO("Use the variable. %u", unsigned(*val)); |
| 67 | |
| 68 | std::array<std::byte, 32> blob_source_buffer; |
| 69 | pw::ConstByteSpan write_data = std::span(blob_source_buffer); |
| 70 | char name[16] = "BLOB"; |
| 71 | std::array<std::byte, 32> read_buffer; |
| 72 | pw::ByteSpan read_span = read_buffer; |
| 73 | PW_LOG_INFO("Do something so variables are used. %u, %c, %u", |
| 74 | unsigned(write_data.size()), |
| 75 | name[0], |
| 76 | unsigned(read_span.size())); |
| 77 | // End of base ********************** |
| 78 | |
| 79 | // Start of deferred blob ********************** |
| 80 | constexpr size_t kBufferSize = 1; |
| 81 | |
| 82 | pw::blob_store::BlobStoreBuffer<kBufferSize> blob( |
| 83 | name, pw::kvs::FlashTestPartition(), nullptr, test_kvs, kBufferSize); |
| 84 | blob.Init(); |
| 85 | |
| 86 | // Use writer. |
| 87 | pw::blob_store::BlobStore::DeferredWriter writer(blob); |
| 88 | writer.Open(); |
| 89 | writer.Write(write_data); |
| 90 | writer.Flush(); |
| 91 | writer.Close(); |
| 92 | |
| 93 | // Use reader. |
| 94 | pw::blob_store::BlobStore::BlobReader reader(blob); |
| 95 | reader.Open(); |
| 96 | pw::Result<pw::ConstByteSpan> get_result = reader.GetMemoryMappedBlob(); |
| 97 | PW_LOG_INFO("%d", get_result.ok()); |
| 98 | auto reader_result = reader.Read(read_span); |
| 99 | reader.Close(); |
| 100 | PW_LOG_INFO("%d", reader_result.ok()); |
| 101 | |
| 102 | // End of deferred blob ********************** |
| 103 | |
| 104 | return 0; |
| 105 | } |