Wyatt Hepler | d621682 | 2020-02-04 16:39:15 -0800 | [diff] [blame] | 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 <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 | |
| 23 | namespace pw::kvs { |
| 24 | namespace { |
| 25 | |
| 26 | using std::byte; |
| 27 | |
| 28 | ChecksumCrc16 checksum; |
Wyatt Hepler | 88adfe8 | 2020-02-20 19:33:27 -0800 | [diff] [blame] | 29 | constexpr EntryFormat format{.magic = 0xBAD'C0D3, .checksum = &checksum}; |
Wyatt Hepler | d621682 | 2020-02-04 16:39:15 -0800 | [diff] [blame] | 30 | |
Alexei Frolov | 972b5f4 | 2020-02-20 14:04:26 -0800 | [diff] [blame] | 31 | constexpr char kHelpText[] = R"( |
| 32 | pw_kvs debug CLI |
| 33 | |
| 34 | Commands: |
| 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 Hepler | d621682 | 2020-02-04 16:39:15 -0800 | [diff] [blame] | 44 | void Run() { |
| 45 | // 4 x 4k sectors, 16 byte alignment |
Wyatt Hepler | cdd6dfc | 2020-02-18 12:04:04 -0800 | [diff] [blame] | 46 | FakeFlashBuffer<4 * 1024, 4> test_flash(16); |
Wyatt Hepler | d621682 | 2020-02-04 16:39:15 -0800 | [diff] [blame] | 47 | |
| 48 | FlashPartition test_partition(&test_flash, 0, test_flash.sector_count()); |
| 49 | test_partition.Erase(0, test_partition.sector_count()); |
| 50 | |
Wyatt Hepler | 38ce30f | 2020-02-19 11:48:31 -0800 | [diff] [blame] | 51 | KeyValueStoreBuffer<256, 256> kvs(&test_partition, format); |
Wyatt Hepler | d621682 | 2020-02-04 16:39:15 -0800 | [diff] [blame] | 52 | kvs.Init(); |
| 53 | |
Alexei Frolov | 972b5f4 | 2020-02-20 14:04:26 -0800 | [diff] [blame] | 54 | 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 Hepler | d621682 | 2020-02-04 16:39:15 -0800 | [diff] [blame] | 64 | std::istringstream data(line); |
Wyatt Hepler | 8e5d313 | 2020-02-05 15:49:28 -0800 | [diff] [blame] | 65 | std::string cmd, key, value; |
| 66 | data >> cmd >> key >> value; |
Wyatt Hepler | d621682 | 2020-02-04 16:39:15 -0800 | [diff] [blame] | 67 | |
Wyatt Hepler | 8e5d313 | 2020-02-05 15:49:28 -0800 | [diff] [blame] | 68 | if (cmd == "init") { |
| 69 | printf("Init() -> %s\n", kvs.Init().str()); |
Alexei Frolov | 972b5f4 | 2020-02-20 14:04:26 -0800 | [diff] [blame] | 70 | } else if (cmd == "delete" || cmd == "d") { |
Wyatt Hepler | 8e5d313 | 2020-02-05 15:49:28 -0800 | [diff] [blame] | 71 | printf("Delete(\"%s\") -> %s\n", key.c_str(), kvs.Delete(key).str()); |
Alexei Frolov | 972b5f4 | 2020-02-20 14:04:26 -0800 | [diff] [blame] | 72 | } else if (cmd == "put" || cmd == "p") { |
Wyatt Hepler | 8e5d313 | 2020-02-05 15:49:28 -0800 | [diff] [blame] | 73 | printf("Put(\"%s\", \"%s\") -> %s\n", |
| 74 | key.c_str(), |
| 75 | value.c_str(), |
| 76 | kvs.Put(key, as_bytes(span(value))).str()); |
Alexei Frolov | 972b5f4 | 2020-02-20 14:04:26 -0800 | [diff] [blame] | 77 | } else if (cmd == "get" || cmd == "g") { |
Wyatt Hepler | 8e5d313 | 2020-02-05 15:49:28 -0800 | [diff] [blame] | 78 | 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 Hepler | d621682 | 2020-02-04 16:39:15 -0800 | [diff] [blame] | 84 | } |
Alexei Frolov | 972b5f4 | 2020-02-20 14:04:26 -0800 | [diff] [blame] | 85 | } else if (cmd == "contents" || cmd == "c") { |
Wyatt Hepler | 8e5d313 | 2020-02-05 15:49:28 -0800 | [diff] [blame] | 86 | int i = 0; |
| 87 | printf("KVS CONTENTS ----------------------------------------------\n"); |
| 88 | for (auto& entry : kvs) { |
| 89 | char value[64] = {}; |
Wyatt Hepler | 50f7077 | 2020-02-13 11:25:10 -0800 | [diff] [blame] | 90 | if (StatusWithSize result = entry.Get(as_writable_bytes(span(value))); |
| 91 | result.ok()) { |
Wyatt Hepler | a00d1ef | 2020-02-14 14:31:26 -0800 | [diff] [blame] | 92 | printf("%2d: %s='%s'\n", ++i, entry.key(), value); |
Wyatt Hepler | 8e5d313 | 2020-02-05 15:49:28 -0800 | [diff] [blame] | 93 | } else { |
Wyatt Hepler | a00d1ef | 2020-02-14 14:31:26 -0800 | [diff] [blame] | 94 | printf( |
| 95 | "FAILED to Get key %s: %s\n", entry.key(), result.status().str()); |
Wyatt Hepler | 8e5d313 | 2020-02-05 15:49:28 -0800 | [diff] [blame] | 96 | } |
Wyatt Hepler | d621682 | 2020-02-04 16:39:15 -0800 | [diff] [blame] | 97 | } |
Wyatt Hepler | 8e5d313 | 2020-02-05 15:49:28 -0800 | [diff] [blame] | 98 | printf("---------------------------------------------- END CONTENTS\n"); |
Alexei Frolov | 972b5f4 | 2020-02-20 14:04:26 -0800 | [diff] [blame] | 99 | } 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 Hepler | d621682 | 2020-02-04 16:39:15 -0800 | [diff] [blame] | 106 | } |
Wyatt Hepler | d621682 | 2020-02-04 16:39:15 -0800 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
| 110 | } // namespace |
| 111 | } // namespace pw::kvs |
| 112 | |
| 113 | int main() { |
| 114 | pw::kvs::Run(); |
| 115 | return 0; |
| 116 | } |