-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathcli.c
More file actions
159 lines (134 loc) · 4.16 KB
/
cli.c
File metadata and controls
159 lines (134 loc) · 4.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
* MIT License
*
* Copyright (c) 2019 Sean Farrelly
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* File cli.c
* Created by Sean Farrelly
* Version 1.0
*
*/
/*! @file cli.c
* @brief Implementation of command-line interface.
*/
#include "cli.h"
#include <stdint.h>
#include <string.h>
static volatile uint8_t buf[MAX_BUF_SIZE]; /* CLI Rx byte-buffer */
static volatile uint8_t *buf_ptr; /* Pointer to Rx byte-buffer */
static uint8_t cmd_buf[MAX_BUF_SIZE]; /* CLI command buffer */
static volatile uint8_t cmd_pending;
const char cli_prompt[] = ">> "; /* CLI prompt displayed to the user */
const char cli_unrecog[] = "CMD: Command not recognised\r\n";
/*!
* @brief This internal API prints a message to the user on the CLI.
*/
static void cli_print(cli_t *cli, const char *msg);
/*!
* @brief This API initialises the command-line interface.
*/
cli_status_t cli_init(cli_t *cli)
{
/* Set buffer ptr to beginning of buf */
buf_ptr = buf;
cmd_pending = 0;
/* Print the CLI prompt. */
cli_print(cli, cli_prompt);
return CLI_OK;
}
/*!
* @brief This API deinitialises the command-line interface.
*/
cli_status_t cli_deinit(cli_t *cli)
{
return CLI_OK;
}
/*! @brief This API must be periodically called by the user to process and
* execute any commands received.
*/
cli_status_t cli_process(cli_t *cli)
{
if(!cmd_pending)
return CLI_IDLE;
uint8_t argc = 0;
char *argv[30];
/* Get the first token (cmd name) */
argv[argc] = strtok(cmd_buf, " ");
/* Walk through the other tokens (parameters) */
while((argv[argc] != NULL) && (argc < 30)) {
argv[++argc] = strtok(NULL, " ");
}
/* Search the command table for a matching command, using argv[0]
* which is the command name. */
for(size_t i = 0; i < cli->cmd_cnt; i++) {
if(strcmp(argv[0], cli->cmd_tbl[i].cmd) == 0) {
/* Found a match, execute the associated function. */
cli_status_t return_value = cli->cmd_tbl[i].func(argc, argv);
cli_print(cli, cli_prompt); /* Print the CLI prompt to the user. */
cmd_pending = 0;
return return_value;
}
}
/* Command not found */
cli_print(cli, cli_unrecog);
cli_print(cli, cli_prompt); /* Print the CLI prompt to the user. */
cmd_pending = 0;
return CLI_E_CMD_NOT_FOUND;
}
/*!
* @brief This API should be called from the devices interrupt handler whenever
* a character is received over the input stream.
*/
cli_status_t cli_put(cli_t *cli, char c)
{
switch(c) {
case CMD_TERMINATOR:
if(!cmd_pending) {
*buf_ptr = '\0'; /* Terminate the msg and reset the msg ptr. */
strcpy(cmd_buf, buf); /* Copy string to command buffer for processing. */
cmd_pending = 1;
buf_ptr = buf; /* Reset buf_ptr to beginning. */
}
break;
case '\b':
/* Backspace. Delete character. */
if(buf_ptr > buf)
buf_ptr--;
break;
default:
/* Normal character received, add to buffer. */
if((buf_ptr - buf) < MAX_BUF_SIZE)
*buf_ptr++ = c;
else
return CLI_E_BUF_FULL;
break;
}
}
/*!
* @brief Print a message on the command-line interface.
*/
static void cli_print(cli_t *cli, const char *msg)
{
/* Temp buffer to store text in ram first */
char buf[50];
strcpy(buf, msg);
cli->println(buf);
}