blob: 77c25e747fa9503b92cbaa04c460e2ee6c3e314e [file] [log] [blame]
David Rogers17793d62021-02-05 03:29:02 -08001// 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 Heplerf298de42021-03-19 15:06:36 -070017#include "pw_assert/check.h"
David Rogers17793d62021-02-05 03:29:02 -080018#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
24char working_buffer[256];
25volatile bool is_set;
26
27constexpr size_t kMaxSectorCount = 64;
28constexpr 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.
32static constexpr pw::kvs::EntryFormat kvs_format = {.magic = 0x22d3f8a0,
33 .checksum = nullptr};
34
35volatile size_t kvs_entry_count;
36
37pw::kvs::KeyValueStoreBuffer<kKvsMaxEntries, kMaxSectorCount> test_kvs(
38 &pw::kvs::FlashTestPartition(), kvs_format);
39
40int volatile* unoptimizable;
41
42int 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 basic 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::BlobWriter writer(blob);
88 writer.Open();
89 writer.Write(write_data);
90 writer.Close();
91
92 // Use reader.
93 pw::blob_store::BlobStore::BlobReader reader(blob);
94 reader.Open();
95 pw::Result<pw::ConstByteSpan> get_result = reader.GetMemoryMappedBlob();
96 PW_LOG_INFO("%d", get_result.ok());
97 auto reader_result = reader.Read(read_span);
98 reader.Close();
99 PW_LOG_INFO("%d", reader_result.ok());
100
101 // End of basic blob **********************
102
103 return 0;
104}