[FL-1063} LF-RFID Cli (#515)

* App lfrfid: update emulator to process external data.
* App lfrfid: cleanup emulator
* App lfrfid: cli interface
* Lib: arguments parser lib

Co-authored-by: あく <alleteam@gmail.com>
This commit is contained in:
SG
2021-06-10 21:53:59 +10:00
committed by GitHub
parent ced7d6315d
commit 4ad5245969
18 changed files with 372 additions and 63 deletions

76
lib/args/args.c Normal file
View File

@@ -0,0 +1,76 @@
#include "args.h"
size_t args_get_first_word_length(string_t args) {
size_t ws = string_search_char(args, ' ');
if(ws == STRING_FAILURE) {
ws = strlen(string_get_cstr(args));
}
return ws;
}
size_t args_length(string_t args) {
return strlen(string_get_cstr(args));
}
bool args_read_string_and_trim(string_t args, string_t word) {
size_t cmd_length = args_get_first_word_length(args);
if(cmd_length == 0) {
return false;
}
string_set_n(word, args, 0, cmd_length);
string_right(args, cmd_length);
string_strim(args);
return true;
}
bool args_char_to_hex_nibble(char c, uint8_t* nibble) {
if((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f')) {
if((c >= '0' && c <= '9')) {
*nibble = c - '0';
} else if((c >= 'A' && c <= 'F')) {
*nibble = c - 'A' + 10;
} else {
*nibble = c - 'a' + 10;
}
return true;
} else {
return false;
}
}
bool args_char_to_hex(char hi_nibble, char low_nibble, uint8_t* byte) {
uint8_t hi_nibble_value = 0;
uint8_t low_nibble_value = 0;
bool result = false;
if(args_char_to_hex_nibble(hi_nibble, &hi_nibble_value)) {
if(args_char_to_hex_nibble(low_nibble, &low_nibble_value)) {
result = true;
*byte = (hi_nibble_value << 4) | low_nibble_value;
}
}
return result;
}
bool args_read_hex_bytes(string_t args, uint8_t* bytes, uint8_t bytes_count) {
bool result = true;
const char* str_pointer = string_get_cstr(args);
if(args_get_first_word_length(args) == (bytes_count * 2)) {
for(uint8_t i = 0; i < bytes_count; i++) {
if(!args_char_to_hex(str_pointer[i * 2], str_pointer[i * 2 + 1], &(bytes[i]))) {
result = false;
break;
}
}
} else {
result = false;
}
return result;
}

70
lib/args/args.h Normal file
View File

@@ -0,0 +1,70 @@
#pragma once
#include "m-string.h"
#include "stdint.h"
#include "stdbool.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Extract first word from arguments string and trim arguments string
*
* @param args arguments string
* @param word first word, output
* @return true - success
* @return false - arguments string does not contain anything
*/
bool args_read_string_and_trim(string_t args, string_t word);
/**
* @brief Convert hex ASCII values to byte array
*
* @param args arguments string
* @param bytes byte array pointer, output
* @param bytes_count needed bytes count
* @return true - success
* @return false - arguments string does not contain enough values, or contain non-hex ASCII values
*/
bool args_read_hex_bytes(string_t args, uint8_t* bytes, uint8_t bytes_count);
/************************************ HELPERS ***************************************/
/**
* @brief Get length of first word from arguments string
*
* @param args arguments string
* @return size_t length of first word
*/
size_t args_get_first_word_length(string_t args);
/**
* @brief Get length of arguments string
*
* @param args arguments string
* @return size_t length of arguments string
*/
size_t args_length(string_t args);
/**
* @brief Convert ASCII hex value to nibble
*
* @param c ASCII character
* @param nibble nibble pointer, output
* @return bool conversion status
*/
bool args_char_to_hex_nibble(char c, uint8_t* nibble);
/**
* @brief Convert ASCII hex values to byte
*
* @param hi_nibble ASCII hi nibble character
* @param low_nibble ASCII low nibble character
* @param byte byte pointer, output
* @return bool conversion status
*/
bool args_char_to_hex(char hi_nibble, char low_nibble, uint8_t* byte);
#ifdef __cplusplus
}
#endif

View File

@@ -66,15 +66,6 @@ CFLAGS += -I$(LIB_DIR)/app-template
CFLAGS += -I$(LIB_DIR)/fnv1a-hash
C_SOURCES += $(LIB_DIR)/fnv1a-hash/fnv1a-hash.c
# build onewire/cyfral library only if
# we build iButton application
ifeq ($(APP_IBUTTON), 1)
# onewire library
APP_ONEWIRE = 1
endif
APP_ONEWIRE ?= 0
ifeq ($(APP_ONEWIRE), 1)
# onewire library
ONEWIRE_DIR = $(LIB_DIR)/onewire
CFLAGS += -I$(ONEWIRE_DIR)
@@ -84,7 +75,6 @@ CPP_SOURCES += $(wildcard $(ONEWIRE_DIR)/*.cpp)
CYFRAL_DIR = $(LIB_DIR)/cyfral
CFLAGS += -I$(CYFRAL_DIR)
CPP_SOURCES += $(wildcard $(CYFRAL_DIR)/*.cpp)
endif
# common apps api
CFLAGS += -I$(LIB_DIR)/common-api
@@ -101,3 +91,7 @@ C_SOURCES += $(LIB_DIR)/version/version.c
CFLAGS += -I$(LIB_DIR)/irda
C_SOURCES += $(wildcard $(LIB_DIR)/irda/*.c)
C_SOURCES += $(wildcard $(LIB_DIR)/irda/*/*.c)
#args lib
CFLAGS += -I$(LIB_DIR)/args
C_SOURCES += $(wildcard $(LIB_DIR)/args/*.c)