Updates with structure

This commit is contained in:
Tilen Majerle 2021-04-06 23:30:34 +02:00
parent 5390eb68f4
commit a8d60f1159
3 changed files with 31 additions and 39 deletions

View File

@ -1,4 +1,4 @@
/**
/**
* \file lwshell.h
* \brief Lightweight shell
*/
@ -35,7 +35,6 @@
#define LWSHELL_HDR_H
#include <stdint.h>
#include <limits.h>
#include "lwshell/lwshell_opt.h"
#ifdef __cplusplus
@ -62,6 +61,11 @@ typedef enum {
lwshellOK = 0x00, /*!< Everything OK */
} lwshellr_t;
/**
*
*/
typedef int32_t(*lwshell_cmd_fn)(int32_t argc, const char** argv);
/**
* \brief LwSHELL main structure
*/
@ -69,8 +73,6 @@ typedef struct lwshell {
uint8_t res;
} lwshell_t;
/**
* \}
*/

View File

@ -53,54 +53,38 @@ extern "C" {
*/
/**
* \brief Enables `1` or disables `0` operating system support in the library
* \brief Maximum number of different commands to be registered
*
* \note When `LWSHELL_CFG_OS` is enabled, user must implement functions in \ref LWSHELL_SYS group.
*/
#ifndef LWSHELL_CFG_OS
#define LWSHELL_CFG_OS 0
#ifndef LWSHELL_CFG_MAX_CMDS
#define LWSHELL_CFG_MAX_CMDS 8
#endif
/**
* \brief Mutex handle type
* \brief Maximum characters for command line length.
*
* \note This value must be set in case \ref LWSHELL_CFG_OS is set to `1`.
* If data type is not known to compiler, include header file with
* definition before you define handle type
* This includes new line character and trailing zero.
* Commands longer than this are automatically discarded
*/
#ifndef LWSHELL_CFG_OS_MUTEX_HANDLE
#define LWSHELL_CFG_OS_MUTEX_HANDLE void *
#ifndef LWSHELL_CFG_MAX_CMD_LEN
#define LWSHELL_CFG_MAX_CMD_LEN 128
#endif
/**
* \brief Number of bits to align memory address and memory size
* \brief Maximum characters for command name
*
* Some CPUs do not offer unaligned memory access (Cortex-M0 as an example)
* therefore it is important to have alignment of data addresses and potentialy length of data
*
* \note This value must be a power of `2` for number of bytes.
* Usually alignment of `4` bytes fits to all processors.
*/
#ifndef LWSHELL_CFG_ALIGN_NUM
#define LWSHELL_CFG_ALIGN_NUM ((size_t)4)
#ifndef LWSHELL_CFG_MAX_CMD_NAME_LEN
#define LWSHELL_CFG_MAX_CMD_NAME_LEN 16
#endif
/**
* \brief Enables `1` or disables `0` memory cleanup on free operation (or realloc).
* \brief Maximum number of parameters accepted by command.
*
* It resets unused memory to `0x00` and prevents other applications seeing old data.
* It is disabled by default since it has performance penalties.
* Number includes command name itself
*/
#ifndef LWSHELL_CFG_CLEAN_MEMORY
#define LWSHELL_CFG_CLEAN_MEMORY 0
#endif
/**
* \brief Enables `1` or disables `0` statistics in the library
*
*/
#ifndef LWSHELL_CFG_ENABLE_STATS
#define LWSHELL_CFG_ENABLE_STATS 0
#ifndef LWSHELL_CFG_MAX_CMD_ARGS
#define LWSHELL_CFG_MAX_CMD_ARGS 8
#endif
/**

View File

@ -31,11 +31,17 @@
* Author: Tilen MAJERLE <tilen@majerle.eu>
* Version: $_version_$
*/
#include <limits.h>
#include <string.h>
#include "lwshell/lwshell.h"
#define LWSHELL_MEMSET memset
#define LWSHELL_MEMCPY memcpy
#define LWSHELL_MEMMOVE memmove
/**
* \brief Shell command structure
*/
typedef struct {
lwshell_cmd_fn cmd_fn; /*!< Command function to call on match */
char cmd_name[LWSHELL_CFG_MAX_CMD_NAME_LEN + 1]; /*!< Command name to search for match */
} lwshell_cmd_t;
/* Array of all commands */
static lwshell_cmd_t cmds[LWSHELL_CFG_MAX_CMDS];
static size_t cmds_cnt;