diff --git a/examples/tests/kv_interactive/Makefile b/examples/tests/kv_interactive/Makefile new file mode 100644 index 000000000..54d6a7969 --- /dev/null +++ b/examples/tests/kv_interactive/Makefile @@ -0,0 +1,11 @@ +# Makefile for user application + +# Specify this directory relative to the current application. +TOCK_USERLAND_BASE_DIR = ../../.. + +# Which files to compile. +C_SRCS := $(wildcard *.c) + +# Include userland master makefile. Contains rules and flags for actually +# building the application. +include $(TOCK_USERLAND_BASE_DIR)/AppMakefile.mk diff --git a/examples/tests/kv_interactive/README.md b/examples/tests/kv_interactive/README.md new file mode 100644 index 000000000..3947f0e8a --- /dev/null +++ b/examples/tests/kv_interactive/README.md @@ -0,0 +1,21 @@ +KV Interactive +========= + +Uses stdin to allow the user to get/set/delete key-value objects. + +Commands +-------- + +Use commands like: + +``` +get key1 +``` + +``` +set key2 mykeyvalue +``` + +``` +delete key1 +``` diff --git a/examples/tests/kv_interactive/main.c b/examples/tests/kv_interactive/main.c new file mode 100644 index 000000000..06adfbc32 --- /dev/null +++ b/examples/tests/kv_interactive/main.c @@ -0,0 +1,145 @@ +#include +#include +#include + +#include +#include + +#define KEY_LEN 64 +#define DATA_LEN 64 + +uint8_t key_buf[KEY_LEN]; +uint8_t data_buf[DATA_LEN]; +uint8_t value_buf[DATA_LEN]; + +char read_buf[64]; + +int get_command(void); +int get_command(void) { + int idx = 0; + memset(read_buf, 0, 64); + + // Read in characters until a new line + while (1) { + int c = getch(); + + if (c == RETURNCODE_FAIL) { + printf("\ngetch() failed!\n"); + return 0; + + } else { + char in = c; + + if (in == 10 || in == 13) { + return 0; + + } else { + // If this is a valid number record it + read_buf[idx] = in; + idx += 1; + + // If our buffer is full, quit + if (idx >= 64) { + return 0; + } + } + } + } +} + +int find_end(int start); +int find_end(int start) { + for (int i = start; i < 64; i++) { + if (read_buf[i] == ' ' || read_buf[i] == '\n' || read_buf[i] == '\0') { + return i; + } + } + return 64; +} + +int main(void) { + int ret; + + printf("[KV Interactive] Text interface to KV store\n"); + printf("\n"); + printf("Please enter a command: get, set, or delete\n"); + printf(" get \n"); + printf(" set \n"); + printf(" delete \n"); + printf("\n"); + + while (1) { + ret = get_command(); + if (ret < 0) { + printf("Error reading stdin\n"); + exit(-1); + } + + if (strncmp(read_buf, "get", 3) == 0) { + int key_start = 4; + int key_end = find_end(key_start); + int key_len = key_end - key_start; + + memcpy(key_buf, read_buf + key_start, key_len); + key_buf[key_len] = '\0'; + + printf("Getting %s\n", key_buf); + + ret = kv_system_get_sync(key_buf, key_len, data_buf, DATA_LEN); + if (ret < 0) { + printf("Could not get key: %i\n", ret); + } else { + printf("Got value: "); + for (int i = 0; i < DATA_LEN; i++) { + printf("%c", data_buf[i]); + } + printf("\n"); + } + + } else if (strncmp(read_buf, "set", 3) == 0) { + int key_start = 4; + int key_end = find_end(key_start); + int key_len = key_end - key_start; + int value_start = key_end + 1; + int value_end = find_end(value_start); + int value_len = value_end - value_start; + + memcpy(key_buf, read_buf + key_start, key_len); + key_buf[key_len] = '\0'; + + memcpy(value_buf, read_buf + value_start, value_len); + value_buf[value_len] = '\0'; + + printf("Setting %s=%s\n", key_buf, value_buf); + + ret = kv_system_set_sync(key_buf, key_len, value_buf, value_len); + if (ret < 0) { + printf("Error setting key %i\n", ret); + } else { + printf("Set key-value\n"); + } + + } else if (strncmp(read_buf, "delete", 6) == 0) { + int key_start = 7; + int key_end = find_end(key_start); + int key_len = key_end - key_start; + + memcpy(key_buf, read_buf + key_start, key_len); + key_buf[key_len] = '\0'; + + printf("Deleting %s\n", key_buf); + + ret = kv_system_delete_sync(key_buf, key_len); + if (ret < 0) { + printf("Could not delete key: %i\n", ret); + } else { + printf("Deleted key\n"); + } + + } else { + printf("KV: unknown command\n"); + } + } + + return 0; +}