blob: 11519adc626101635238b58df1a368fc22be7cbf [file] [log] [blame]
Wyatt Heplerd6216822020-02-04 16:39:15 -08001// 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 <iostream>
16#include <sstream>
17#include <string>
18
19#include "pw_kvs/crc16_checksum.h"
20#include "pw_kvs/in_memory_fake_flash.h"
21#include "pw_kvs/key_value_store.h"
22
23namespace pw::kvs {
24namespace {
25
26using std::byte;
27
28ChecksumCrc16 checksum;
Wyatt Hepler88adfe82020-02-20 19:33:27 -080029constexpr EntryFormat format{.magic = 0xBAD'C0D3, .checksum = &checksum};
Wyatt Heplerd6216822020-02-04 16:39:15 -080030
Alexei Frolov972b5f42020-02-20 14:04:26 -080031constexpr char kHelpText[] = R"(
32pw_kvs debug CLI
33
34Commands:
35
36 init Initializes the KVS
37 put KEY VALUE Sets a key to a specified value
38 get KEY Looks up the value for a key
39 delete KEY Deletes a key from the KVS
40 contents Prints the contents of the KVS
41 quit Exits the CLI
42)";
43
Wyatt Heplerd6216822020-02-04 16:39:15 -080044void Run() {
45 // 4 x 4k sectors, 16 byte alignment
Wyatt Heplercdd6dfc2020-02-18 12:04:04 -080046 FakeFlashBuffer<4 * 1024, 4> test_flash(16);
Wyatt Heplerd6216822020-02-04 16:39:15 -080047
48 FlashPartition test_partition(&test_flash, 0, test_flash.sector_count());
49 test_partition.Erase(0, test_partition.sector_count());
50
Wyatt Hepler38ce30f2020-02-19 11:48:31 -080051 KeyValueStoreBuffer<256, 256> kvs(&test_partition, format);
Wyatt Heplerd6216822020-02-04 16:39:15 -080052 kvs.Init();
53
Alexei Frolov972b5f42020-02-20 14:04:26 -080054 while (true) {
55 printf("\n> ");
56 fflush(stdout);
57
58 std::string line;
59 if (!std::getline(std::cin, line)) {
60 putchar('\n');
61 break;
62 }
63
Wyatt Heplerd6216822020-02-04 16:39:15 -080064 std::istringstream data(line);
Wyatt Hepler8e5d3132020-02-05 15:49:28 -080065 std::string cmd, key, value;
66 data >> cmd >> key >> value;
Wyatt Heplerd6216822020-02-04 16:39:15 -080067
Wyatt Hepler8e5d3132020-02-05 15:49:28 -080068 if (cmd == "init") {
69 printf("Init() -> %s\n", kvs.Init().str());
Alexei Frolov972b5f42020-02-20 14:04:26 -080070 } else if (cmd == "delete" || cmd == "d") {
Wyatt Hepler8e5d3132020-02-05 15:49:28 -080071 printf("Delete(\"%s\") -> %s\n", key.c_str(), kvs.Delete(key).str());
Alexei Frolov972b5f42020-02-20 14:04:26 -080072 } else if (cmd == "put" || cmd == "p") {
Wyatt Hepler8e5d3132020-02-05 15:49:28 -080073 printf("Put(\"%s\", \"%s\") -> %s\n",
74 key.c_str(),
75 value.c_str(),
76 kvs.Put(key, as_bytes(span(value))).str());
Alexei Frolov972b5f42020-02-20 14:04:26 -080077 } else if (cmd == "get" || cmd == "g") {
Wyatt Hepler8e5d3132020-02-05 15:49:28 -080078 byte buffer[128] = {};
79 Status status = kvs.Get(key, buffer).status();
80 printf("Get(\"%s\") -> %s\n", key.c_str(), status.str());
81 if (status.ok()) {
82 printf(" Key: \"%s\"\n", key.c_str());
83 printf("Value: \"%s\"\n", reinterpret_cast<const char*>(buffer));
Wyatt Heplerd6216822020-02-04 16:39:15 -080084 }
Alexei Frolov972b5f42020-02-20 14:04:26 -080085 } else if (cmd == "contents" || cmd == "c") {
Wyatt Hepler8e5d3132020-02-05 15:49:28 -080086 int i = 0;
87 printf("KVS CONTENTS ----------------------------------------------\n");
88 for (auto& entry : kvs) {
89 char value[64] = {};
Wyatt Hepler50f70772020-02-13 11:25:10 -080090 if (StatusWithSize result = entry.Get(as_writable_bytes(span(value)));
91 result.ok()) {
Wyatt Heplera00d1ef2020-02-14 14:31:26 -080092 printf("%2d: %s='%s'\n", ++i, entry.key(), value);
Wyatt Hepler8e5d3132020-02-05 15:49:28 -080093 } else {
Wyatt Heplera00d1ef2020-02-14 14:31:26 -080094 printf(
95 "FAILED to Get key %s: %s\n", entry.key(), result.status().str());
Wyatt Hepler8e5d3132020-02-05 15:49:28 -080096 }
Wyatt Heplerd6216822020-02-04 16:39:15 -080097 }
Wyatt Hepler8e5d3132020-02-05 15:49:28 -080098 printf("---------------------------------------------- END CONTENTS\n");
Alexei Frolov972b5f42020-02-20 14:04:26 -080099 } else if (cmd == "help" || cmd == "h") {
100 printf("%s", kHelpText);
101 } else if (cmd == "quit" || cmd == "q") {
102 break;
103 } else {
104 printf("Unrecognized command: %s\n", cmd.c_str());
105 printf("Type 'help' for options\n");
Wyatt Heplerd6216822020-02-04 16:39:15 -0800106 }
Wyatt Heplerd6216822020-02-04 16:39:15 -0800107 }
108}
109
110} // namespace
111} // namespace pw::kvs
112
113int main() {
114 pw::kvs::Run();
115 return 0;
116}