272 lines
7.4 KiB
C
272 lines
7.4 KiB
C
|
/*
|
||
|
* console.c
|
||
|
*
|
||
|
* Created on: Nov 6, 2024
|
||
|
* Author: true
|
||
|
*/
|
||
|
|
||
|
#include "console.h"
|
||
|
|
||
|
#include <stdint.h>
|
||
|
#include <stdio.h>
|
||
|
|
||
|
#include "version.h"
|
||
|
|
||
|
|
||
|
|
||
|
// console
|
||
|
ntshell_t ntshell;
|
||
|
text_history_t ntshell_history;
|
||
|
char ntshell_prompt_old[32];
|
||
|
static uint16_t shell_flags;
|
||
|
|
||
|
static int (*console_puts)(char *buf, int cnt);
|
||
|
static int (*console_gets)(char *buf, int cnt);
|
||
|
|
||
|
|
||
|
// console commands
|
||
|
#include "commands/about.h"
|
||
|
#include "commands/cls.h"
|
||
|
|
||
|
void cons_help(int argc, char **argv);
|
||
|
|
||
|
|
||
|
// console command table
|
||
|
static const command_table_t console_cmdlist[] = {
|
||
|
{"-", 0, "General Commands", 0x00, 0},
|
||
|
{"help", "<cmd-name>", "prints this text. for command help, try help <cmd-name>", 0x00, cons_help},
|
||
|
{"?", 0, 0, 0x00, cons_help},
|
||
|
{"save", 0, "commits data to nvram", 0x00, 0},
|
||
|
{"clear", 0, "clears the screen", 0x00, cons_cls},
|
||
|
{"ver", 0, "prints firmware version number", 0x00, 0},
|
||
|
{"about", 0, "prints info about this product", 0x00, cons_about},
|
||
|
|
||
|
{"-", 0, "I2C Commands", 0x00, 0},
|
||
|
{"scan", 0, "scans i2c bus for responding slaves", 0x00, 0},
|
||
|
{"read", "<addr> <len>", "reads from I2C slave", 0x00, 0},
|
||
|
{"write", "<addr> <len> <hexdata>", "writes to I2C slave", 0x00, 0},
|
||
|
|
||
|
{"-", 0, "Clock Commands", 0x00, 0},
|
||
|
{"time", "[hhmmss]", "prints current time. if time is provided, sets the time.", 0x00, 0},
|
||
|
{"auto", "[on/off", "enables or disables all automatic schedules.", 0x00, 0},
|
||
|
{"calendar", "[id] [on/off] [hhmmss]", "prints an auto schedule. if values set, sets a schedule.", 0x00, 0},
|
||
|
|
||
|
{"-", 0, "Light Level Commands", 0x00, 0},
|
||
|
{"light", 0, "prints the detected light level", 0x00, 0},
|
||
|
{"day", "[new-day-thresh]", "prints bright (on) threshold. if value set, sets threshold.", 0x00, 0},
|
||
|
{"night", "[new-day-thresh]", "prints dark (off) threshold. if value set, sets threshold.", 0x00, 0},
|
||
|
};
|
||
|
command_table_t *ctbl;
|
||
|
command_table_t *ctbl_prev;
|
||
|
|
||
|
|
||
|
// help needs to reference the command table
|
||
|
#include "commands/help.h"
|
||
|
|
||
|
|
||
|
// additional
|
||
|
char console_line[256];
|
||
|
|
||
|
// hijack serial for custom text parsing
|
||
|
// todo: is this required anymore? wtf does it do?
|
||
|
ntshell_t hijack;
|
||
|
|
||
|
|
||
|
// ntshell handlers
|
||
|
static int console_read(char *buf, int cnt, void *extobj)
|
||
|
{
|
||
|
if (console_gets != 0)
|
||
|
return console_gets(buf, cnt);
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
int console_write(const char *buf, int cnt, void *extobj)
|
||
|
{
|
||
|
if (console_puts != 0) {
|
||
|
console_puts((char *)buf, cnt);
|
||
|
return cnt;
|
||
|
}
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
static int console_ntopt_cb(int argc, char **argv, void *extobj)
|
||
|
{
|
||
|
// ntshell_t *ntshell = (ntshell_t *)extobj;
|
||
|
char cmd[2][4];
|
||
|
command_table_t *p;
|
||
|
|
||
|
if (argc == 0) {
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
cmd[0][2] = cmd[1][2] = 0;
|
||
|
strncpy(cmd[0], argv[0], 2);
|
||
|
|
||
|
for (p = ctbl; p->cmd != NULL; p++) {
|
||
|
strncpy(cmd[1], p->cmd, 2);
|
||
|
if ((strcmp(cmd[0], cmd[1]) == 0) && (strlen(argv[0]) <= strlen(p->cmd))) {
|
||
|
p->go(argc, argv);
|
||
|
return 0;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
sprintf(console_line, "* '%s': ", argv[0]);
|
||
|
CONS_PRINT(console_line);
|
||
|
CONS_PRINT("command not found\r\n");
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
static int console_cb(const char *text, void *extobj)
|
||
|
{
|
||
|
// ntshell_t *ntshell = (ntshell_t *)extobj;
|
||
|
|
||
|
if ((shell_flags & 0x01) == 0) {
|
||
|
sprintf(console_line, "trueControl GAT Stand, version %s\r\n", FW_VERSION);
|
||
|
CONS_PRINT(console_line);
|
||
|
CONS_PRINT("Use 'help' or '?' for the command list.\r\n");
|
||
|
shell_flags |= 0x01;
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
if (strlen(text) > 0) {
|
||
|
return ntopt_parse(text, console_ntopt_cb, extobj);
|
||
|
}
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
|
||
|
// setup
|
||
|
void console_init()
|
||
|
{
|
||
|
// set up command table pointer
|
||
|
ctbl = (command_table_t *)console_cmdlist;
|
||
|
|
||
|
// set up the shell
|
||
|
ntshell_init(
|
||
|
&ntshell,
|
||
|
console_read,
|
||
|
console_write,
|
||
|
console_cb,
|
||
|
&ntshell_history,
|
||
|
(void *)&hijack);
|
||
|
|
||
|
ntshell_set_prompt(&ntshell, CONS_PROMPT_DEFAULT);
|
||
|
// ntshell_execute(&ntshell);
|
||
|
|
||
|
// set up the hijacker
|
||
|
ntshell_init(
|
||
|
&hijack,
|
||
|
console_read,
|
||
|
console_write,
|
||
|
NULL,
|
||
|
0,
|
||
|
(void *)&ntshell);
|
||
|
ntshell_set_prompt(&hijack, "");
|
||
|
}
|
||
|
|
||
|
void console_set_puts(int (*puts)(char *buf, int cnt))
|
||
|
{
|
||
|
console_puts = puts;
|
||
|
}
|
||
|
|
||
|
void console_set_gets(int (*gets)(char *buf, int cnt))
|
||
|
{
|
||
|
console_gets = gets;
|
||
|
}
|
||
|
|
||
|
void console_start()
|
||
|
{
|
||
|
ntshell_execute_start(&ntshell);
|
||
|
}
|
||
|
|
||
|
void console_stop()
|
||
|
{
|
||
|
shell_flags = 0;
|
||
|
}
|
||
|
|
||
|
void console_process()
|
||
|
{
|
||
|
ntshell_execute_process(hijack.func_callback ? &hijack : &ntshell);
|
||
|
}
|
||
|
|
||
|
void console_hijack(int (*cb)(const char *text, void *extobj))
|
||
|
{
|
||
|
if (cb) {
|
||
|
// we are in the middle of processing our old shell, and we don't
|
||
|
// want to show the prompt. so back it up and unset it
|
||
|
// before enabling the hijack
|
||
|
strncpy(ntshell_prompt_old, ntshell.prompt, 15);
|
||
|
strcpy(ntshell.prompt, "");
|
||
|
hijack.func_callback = cb;
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
// we're done, so recover our shell prompt and print it
|
||
|
hijack.func_callback = 0;
|
||
|
strncpy(ntshell.prompt, ntshell_prompt_old, 15);
|
||
|
CONS_PRINT("\r\n");
|
||
|
CONS_PRINT(ntshell.prompt);
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
void console_cmd_credits(int argc, char **argv)
|
||
|
{
|
||
|
if (!argc) {
|
||
|
CONS_PRINT("Just run the command.");
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
console_cmd_clear_screen(1, 0);
|
||
|
CONS_PRINT(" It seems you have a badge with our name on it. We are the");
|
||
|
CONS_PRINT("\x1B[3;20H\x1B[01mWHISKEY PIRATE CREW\x1b[00m");
|
||
|
CONS_PRINT("\x1B[5;18HBooze. Badges. Bullshit.\r\n");
|
||
|
}
|
||
|
|
||
|
void console_cmd_clear_screen(int argc, char **argv)
|
||
|
{
|
||
|
if (!argc) {
|
||
|
CONS_PRINT("...");
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
CONS_PRINT("\x1B[2J\x1B[0;0H");
|
||
|
}
|
||
|
|
||
|
void console_settings_save()
|
||
|
{
|
||
|
CONS_PRINT("Saving settings...");
|
||
|
CONS_PRINT("done.\r\n");
|
||
|
}
|
||
|
|
||
|
void console_cmd_save(int argc, char **argv)
|
||
|
{
|
||
|
if (!argc) {
|
||
|
CONS_PRINT("Saves settings to flash. Run the 'save' command for arguments.");
|
||
|
}
|
||
|
|
||
|
if (argc == 1) {
|
||
|
CONS_PRINT("Usage: save <all/setup or settings/alarm/led)\r\n");
|
||
|
}
|
||
|
|
||
|
if (argc == 2) {
|
||
|
strtolower((unsigned char *)argv[1]);
|
||
|
if (strcmp(argv[1], "all") == 0) {
|
||
|
console_settings_save();
|
||
|
console_time_alarm_save();
|
||
|
}
|
||
|
if (strcmp(argv[1], "alarm") == 0) {
|
||
|
console_time_alarm_save();
|
||
|
}
|
||
|
if (strcmp(argv[1], "led") == 0) {
|
||
|
|
||
|
}
|
||
|
if (argv[1][0] == 's' && argv[1][1] == 'e') {
|
||
|
console_settings_save();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
*/
|