Commit f1867995 authored by Tomasz Wlostowski's avatar Tomasz Wlostowski

initial support for command line shell

parent d6ba0125
/* Command: gui
Arguments: none
Description: launches the WR Core monitor GUI */
#include "shell.h"
extern int wrc_gui_mode;
int cmd_gui(const char *args[])
{
wrc_gui_mode = 1;
return 0;
}
\ No newline at end of file
/* Command: gui
Arguments: none
Description: launches the WR Core monitor GUI */
#include "shell.h"
extern int measure_t24p(int *value);
int cmd_measure_t24p(const char *args[])
{
return measure_t24p(NULL);
}
\ No newline at end of file
/* Command: mode
Arguments: PTP mode: gm = grandmaster, master = free-running master, slave = slave
Description: (re)starts/stops the PTP session. */
#include <errno.h>
#include <string.h>
#include "shell.h"
#include "wrc_ptp.h"
int cmd_mode(const char *args[])
{
int mode;
if(!strcasecmp(args[0], "gm"))
mode = WRC_MODE_GM;
else if(!strcasecmp(args[0], "master"))
mode = WRC_MODE_MASTER;
else if(!strcasecmp(args[0], "slave"))
mode = WRC_MODE_SLAVE;
else
return -EINVAL;
return wrc_ptp_set_mode(mode);
}
\ No newline at end of file
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "softpll_ng.h"
#include "shell.h"
int cmd_pll(const char *args[])
{
int cur, tgt;
if(!strcasecmp(args[0], "init"))
{
if(!args[3])
return -EINVAL;
spll_init(atoi(args[1]), atoi(args[2]), atoi(args[3]));
} else if (!strcasecmp(args[0], "cl"))
{
if(!args[1])
return -EINVAL;
mprintf("%d\n", spll_check_lock(atoi(args[1])));
} else if (!strcasecmp(args[0], "sps"))
{
if(!args[2])
return -EINVAL;
spll_set_phase_shift(atoi(args[1]), atoi(args[2]));
} else if (!strcasecmp(args[0], "gps"))
{
if(!args[1])
return -EINVAL;
spll_get_phase_shift(atoi(args[1]), &cur, &tgt);
printf("%d %d\n", cur, tgt);
} else if (!strcasecmp(args[0], "start"))
{
if(!args[1])
return -EINVAL;
spll_start_channel(atoi(args[1]));
} else if (!strcasecmp(args[0], "stop"))
{
if(!args[1])
return -EINVAL;
spll_stop_channel(atoi(args[1]));
} else if (!strcasecmp(args[0], "sdac"))
{
if(!args[2])
return -EINVAL;
spll_set_dac(atoi(args[1]), atoi(args[2]));
} else if (!strcasecmp(args[0], "gdac"))
{
if(!args[1])
return -EINVAL;
mprintf("%d\n", spll_get_dac(atoi(args[1])));
} else return -EINVAL;
return 0;
}
/* Command: ptp
Arguments: one [start/stop]
Description: (re)starts/stops the PTP session. */
#include <errno.h>
#include <string.h>
#include "shell.h"
int cmd_ptp(const char *args[])
{
if(!strcasecmp(args[0], "start"))
return wrc_ptp_start();
else if(!strcasecmp(args[0], "stop"))
return wrc_ptp_stop();
else
return -EINVAL;
return 0;
}
\ No newline at end of file
/* Command: sfp
Arguments: subcommand [subcommand-specific args]
Description: SFP detection/database manipulation.
Subcommands:
add vendor_type delta_tx delta_rx alpha - adds an SFP to the database, with given alpha/delta_rx/delta_rx values
show - shows the SFP database
detect - detects the transceiver type
*/
#include "shell.h"
int cmd_sfp(const char *args[])
{
return 0;
}
\ No newline at end of file
#include "shell.h"
int cmd_stat(const char *args[])
{
wrc_log_stats();
return 0;
}
\ No newline at end of file
#include "shell.h"
#include "syscon.h"
extern const char *build_revision, *build_date;
int cmd_version(const char *args[])
{
mprintf("WR Core build: %s, compiled on: %s (memory size: %d kB)\n", build_revision, build_date, sysc_get_memsize());
return 0;
}
\ No newline at end of file
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include "shell.h"
#define SH_MAX_LINE_LEN 80
#define SH_MAX_ARGS 8
/* interactive shell state definitions */
#define SH_PROMPT 0
#define SH_INPUT 1
#define SH_EXEC 2
#define ESCAPE_FLAG 0x10000
#define KEY_LEFT (ESCAPE_FLAG | 68)
#define KEY_RIGHT (ESCAPE_FLAG | 67)
#define KEY_ENTER (13)
#define KEY_ESCAPE (27)
#define KEY_BACKSPACE (127)
#define KEY_DELETE (126)
struct shell_cmd {
char *name;
int (*exec)(const char *args[]);
};
extern int uart_read_byte();
static const struct shell_cmd cmds_list[] = {
{ "pll", cmd_pll },
{ "gui", cmd_gui },
{ "ver", cmd_version },
{ "stat", cmd_stat },
{ "ptp", cmd_ptp },
{ "mode", cmd_mode },
{ "measure_t24p", cmd_measure_t24p },
{ NULL, NULL }
};
static char cmd_buf[SH_MAX_LINE_LEN + 1];
static int cmd_pos = 0, cmd_len = 0;
static int state = SH_PROMPT;
static int current_key = 0;
static int insert(char c)
{
if(cmd_len >= SH_MAX_LINE_LEN)
return 0;
if(cmd_pos != cmd_len)
memmove(&cmd_buf[cmd_pos+1], &cmd_buf[cmd_pos], cmd_len - cmd_pos);
cmd_buf[cmd_pos] = c;
cmd_pos++;
cmd_len++;
return 1;
}
static void delete(int where)
{
memmove(&cmd_buf[where], &cmd_buf[where+1], cmd_len - where);
cmd_len--;
}
static void esc(char code)
{
mprintf("\033[1%c", code);
}
static int _shell_exec()
{
char *tokptr[SH_MAX_ARGS+1];
int n = 0, i = 0;
memset(tokptr, 0, sizeof(tokptr));
while(1)
{
if(n >= SH_MAX_ARGS)
break;
while(cmd_buf[i] == ' ' && cmd_buf[i]) cmd_buf[i++] = 0;
if(!cmd_buf[i])
break;
tokptr [n++] = &cmd_buf[i];
while(cmd_buf[i] != ' ' && cmd_buf[i]) i++;
if(!cmd_buf[i])
break;
}
if(!n)
return 0;
if(*tokptr[0] == '#')
return 0;
for(i=0; cmds_list[i].name; i++)
if(!strcasecmp(cmds_list[i].name, tokptr[0]))
{
int rv = cmds_list[i].exec(tokptr+1);
if(rv<0)
mprintf("Err %d\n", rv);
return rv;
}
mprintf("Unrecognized command.\n");
return -EINVAL;
}
int shell_exec(const char *cmd)
{
strncpy(cmd_buf, cmd, SH_MAX_LINE_LEN);
cmd_len = strlen(cmd_buf);
return _shell_exec();
}
void shell_init()
{
cmd_len = cmd_pos = 0;
state = SH_PROMPT;
mprintf("\033[2J\033[1;1H");
}
void shell_interactive()
{
int c;
switch(state)
{
case SH_PROMPT:
mprintf("wrc# ");
cmd_pos = 0;
cmd_len = 0;
state = SH_INPUT;
break;
case SH_INPUT:
c = uart_read_byte();
if(c < 0)
return;
if(c == 27 || ((current_key & ESCAPE_FLAG) && c == 91))
current_key = ESCAPE_FLAG;
else
current_key |= c;
if(current_key & 0xff)
{
switch(current_key)
{
case KEY_LEFT:
if(cmd_pos > 0)
{
cmd_pos--;
esc('D');
}
break;
case KEY_RIGHT:
if(cmd_pos < cmd_len)
{
cmd_pos++;
esc('C');
}
break;
case KEY_ENTER:
mprintf("\n");
state = SH_EXEC;
break;
case KEY_DELETE:
if(cmd_pos != cmd_len)
{
delete(cmd_pos);
esc('P');
}
break;
case KEY_BACKSPACE:
if(cmd_pos > 0)
{
esc('D');
esc('P');
delete(cmd_pos-1);
cmd_pos--;
}
break;
case '\t':
break;
default:
if(!(current_key & ESCAPE_FLAG) && insert(current_key))
{
esc('@');
mprintf("%c", current_key);
}
break;
}
current_key = 0;
}
break;
case SH_EXEC:
cmd_buf[cmd_len] = 0;
_shell_exec();
state = SH_PROMPT;
break;
}
}
#ifndef __SHELL_H
#define __SHELL_H
int cmd_gui(const char *args[]);
int cmd_pll(const char *args[]);
int cmd_version(const char *args[]);
int cmd_stat(const char *args[]);
int cmd_ptp(const char *args[]);
int cmd_sfp(const char *args[]);
int cmd_mode(const char *args[]);
int cmd_measure_t24p(const char *args[]);
int shell_exec(const char *buf);
void shell_interactive();
#endif
OBJS_SHELL = shell/shell.o \
shell/cmd_version.o \
shell/cmd_pll.o \
shell/cmd_stat.o \
shell/cmd_ptp.o \
shell/cmd_mode.o \
shell/cmd_measure_t24p.o \
shell/cmd_gui.o
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment