Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: NaturalTinyShell_ice libmDot-12Sept mbed-rtos mbed
Fork of ICE by
src/CommandParser/cmd.cpp
- Committer:
- jmarkel44
- Date:
- 2016-09-09
- Revision:
- 37:7e6986b77f01
- Parent:
- 36:f240f5a6d0ed
- Child:
- 41:e8946fc01ea4
File content as of revision 37:7e6986b77f01:
/*
* ===============================================================
* Natural Tiny Shell (NT-Shell) Application example.
* Version 0.0.6
* ===============================================================
* Copyright (c) 2010-2011 Shinichiro Nakamura
*
* 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.
* ===============================================================
*/
#include "cmd.h"
#include <mbed.h>
#include <utility>
#include "ntshell.h"
#include "ntopt.h"
#include "global.h"
#include "ConfigurationHandler.h"
#include "mDot.h"
#include "rtos.h"
Serial serial(USBTX, USBRX);
ntshell_t ntshell;
extern mDot *GLOBAL_mdot;
typedef struct {
char *command; // command (from shell)
char *description; // descrption
void (*func)(int argc, char **argv); // callback function
} command_table_t;
// see cmd.h
const command_table_t cmdlist[] = {
{"?", "help command", cmd_help },
{"create", "create a control", cmd_create },
{"destroy", "destroy a control", cmd_destroy },
{"ccf", "create a test control file", cmd_ccf },
{"cif", "create a test input file", cmd_cif },
{"cof", "create a test output file", cmd_cof },
{"heap", "show heap statistics", cmd_heap },
{"help", "help command", cmd_help },
{"log-level", "get/set mDot log level", cmd_logLevel },
{"ls", "list user files", cmd_ls },
{"modify", "modify a control", cmd_modify },
{"reset", "reset the controller", cmd_reset },
{"reset-stats", "reset current mDot statistics", cmd_resetStats },
{"rm", "remove a user file", cmd_rm },
{"rssi-stats", "get current rssi stats", cmd_rssiStats },
{"show-controls", "display active controls", cmd_ShowControls },
{"snr-stats", "get current SNR stats", cmd_snrStats },
{"stack", "get thread stack usage stats", cmd_stack },
{"stats", "get current mDot statistics", cmd_stats },
{NULL, NULL, NULL}
};
int func_read(char *buf, int cnt);
int func_write(const char *buf, int cnt);
int func_cb_ntshell(const char *text);
void func_cb_ntopt(int argc, char **argv);
/**
* Serial read function.
*/
int func_read(char *buf, int cnt)
{
for (int i = 0; i < cnt; i++) {
buf[i] = serial.getc();
}
return 0;
}
/**
* Serial write function.
*/
int func_write(const char *buf, int cnt)
{
for (int i = 0; i < cnt; i++) {
serial.putc(buf[i]);
}
return 0;
}
/**
* Callback function for ntshell module.
*/
int func_cb_ntshell(const char *text)
{
return ntopt_parse(text, func_cb_ntopt);
}
/**
* Callback function for ntopt module.
*/
void func_cb_ntopt(int argc, char **argv)
{
if (argc == 0) {
return;
}
int execnt = 0;
const command_table_t *p = &cmdlist[0];
while (p->command != NULL) {
if (strcmp(argv[0], p->command) == 0) {
p->func(argc, argv);
execnt++;
}
p++;
}
if (execnt == 0) {
printf("Command not found.\r\n");
}
wait_ms(250);
}
/************************* callback functions *******************************/
/*****************************************************************************
* Function: cmd_help
* Description: displays the list of commands available
*
* @param argc (not used)
* @param argv (not used)
* @return none
*****************************************************************************/
void cmd_help(int argc, char **argv)
{
UNUSED(argc);
UNUSED(argv);
const command_table_t *tblPtr = cmdlist;
while (tblPtr->command) {
printf("\r%-32s:\t%s\n", tblPtr->command, tblPtr->description);
tblPtr++;
}
printf("\r\n");
}
/*****************************************************************************
* Function: cmd_logLevel
* Description: get or set the current log-level
*
* @param argc (not used)
* @param argv (not used)
* @return none
*****************************************************************************/
void cmd_logLevel(int argc, char **argv)
{
uint8_t logLevel = 0;
const char *mapper[] = { "NONE",
"FATAL",
"ERROR",
"WARNING",
"INFO",
"DEBUG",
"TRACE" };
if ( argc == 1 ) {
printf("\r current log-level set to %s\r\n",
mapper[GLOBAL_mdot->getLogLevel()]);
goto usage;
}
if ( argc != 2 ) {
usage:
printf("\rusage: log-level [0-6]\n");
printf("\r 0 = NONE\n");
printf("\r 1 = FATAL\n");
printf("\r 2 = ERROR\n");
printf("\r 3 = WARNING\n");
printf("\r 4 = INFO\n");
printf("\r 5 = DEBUG\n");
printf("\r 6 = TRACE\r\n");
return;
}
logLevel = atoi(argv[1]);
if ( logLevel > 6 )
goto usage;
// reassign the log level
printf("...setting log-level to %s\r\n", mapper[logLevel]);
GLOBAL_mdot->setLogLevel(logLevel);
printf("\r\n");
}
/*****************************************************************************
* Function: cmd_ls
* Description: list the user files on flash
*
* @param argc (not used)
* @param argv (not used)
* @return none
*****************************************************************************/
void cmd_ls(int argc, char **argv)
{
UNUSED(argc);
UNUSED(argv);
vector<mDot::mdot_file> userFiles;
userFiles = GLOBAL_mdot->listUserFiles();
vector<mDot::mdot_file>::iterator pos;
for ( pos = userFiles.begin(); pos != userFiles.end(); ++pos ) {
printf("\r %-33s %d\n", pos->name, pos->size);
}
printf("\r\n");
}
/*****************************************************************************
* Function: cmd_ShowControls
* Description: show active controls
*
* @param argc (not used)
* @param argv (not used)
* @return none
*****************************************************************************/
void cmd_ShowControls(int argc, char **argv)
{
UNUSED(argc);
UNUSED(argv);
ConfigurationHandler_showControls();
printf("\r\n");
}
/*****************************************************************************
* Function: cmd_reset
* Description: reset the cpu
*
* @param argc (not used)
* @param argv (not used)
* @return none
*****************************************************************************/
void cmd_reset(int argc, char **argv)
{
UNUSED(argc);
UNUSED(argv);
GLOBAL_mdot->resetCpu();
}
/*****************************************************************************
* Function: cmd_rm
* Description: removes a user file from flash
*
* @param argc-> number of args
* @param argv-> filename
* @return none
*****************************************************************************/
void cmd_rm(int argc, char **argv)
{
UNUSED(argc);
UNUSED(argv);
if ( argc != 2 ) {
printf("\rusage: rm <filename>\n");
return;
}
// attempt to remove a user file
GLOBAL_mdot->deleteUserFile(argv[1]);
}
/*****************************************************************************
* Function: cmd_create
* Description: create a control
*
* @param argc-> number of args
* @param argv-> control name, control type
* @return none
*****************************************************************************/
void cmd_create(int argc, char **argv)
{
if ( argc != 3 ) {
printf("\r\nusage: create [controlName] [controlType]\n");
printf("\rcontrolType-> 0=timer, 1=PID, 2=setpoint, 3=composite, 4=manual\r\n");
return;
}
// send a message to the configuration handler to create the control
Message_t *msg = MailBox.alloc();
memset(msg, 0, sizeof(Message_t));
msg->action = ACTION_CREATE;
msg->control = (Control_t) atoi(argv[2]);
strncpy(msg->controlFile, argv[1], sizeof(msg->controlFile)-1);
printf("%s: Sending a create request for control %s type = %u\r\n",
__func__, msg->controlFile, msg->control);
MailBox.put(msg);
printf("\r\n");
return;
}
/*****************************************************************************
* Function: cmd_destroy
* Description: reset the cpu
*
* @param argc-> number of arguments
* @param argv-> control name, control type
* @return none
*****************************************************************************/
void cmd_destroy(int argc, char **argv)
{
if ( argc != 3 ) {
printf("\r\nusage: destroy [controlName] [controlType]\n");
printf("\rcontrolType-> 0=timer, 1=PID, 2=setpoint, 3=composite, 4=manual\r\n");
return;
}
// send a message to the configuration handler to destroy the control
Message_t *msg = MailBox.alloc();
memset(msg, 0, sizeof(Message_t));
msg->action = ACTION_DESTROY;
msg->control = (Control_t) atoi(argv[2]);
strncpy(msg->controlFile, argv[1], sizeof(msg->controlFile)-1);
printf("%s: Sending a destroy request for control %s\r\n",
__func__, msg->controlFile);
MailBox.put(msg);
printf("\r\n");
return;
}
/*****************************************************************************
* Function: cmd_ccf
* Description: create control file
*
* @param argc-> number of args
* @param argv-> filename
* @return none
*****************************************************************************/
void cmd_ccf(int argc, char **argv)
{
if ( argc != 2 ) {
printf("\rusage: ccf <filename>\r\n");
return;
}
char data_buf[1024];
snprintf(data_buf, sizeof(data_buf),
"{ "
"\"id\": \"SP\", "
"\"name\": \"TRASAR\", "
"\"priority\": \"800\", "
"\"input\": \"i_tra01\", "
"\"output\": \"o_r05\", "
"\"prodfact\": \"100\", "
"\"halert\": \"115\", "
"\"lalert\": \"85\", "
"\"hfs\": \"130\", "
"\"lfs\": \"70\", "
"\"tol\": \"5\" } "
);
bool status = GLOBAL_mdot->saveUserFile(argv[1], (void *)data_buf, 1024);
if( status != true ) {
logInfo("(%d)save file failed, status=%d", __LINE__, status);
}
}
/*****************************************************************************
* Function: cmd_cif
* Description: create input file
*
* @param argc-> number of args
* @param argv-> filename
* @return none
*****************************************************************************/
void cmd_cif(int argc, char **argv)
{
if ( argc != 2 ) {
printf("\rusage: cif <filename>\r\n");
return;
}
char data_buf[1024];
snprintf(data_buf, sizeof(data_buf),
"{ "
"\"id\": \"i_tra1\", "
"\"name\": \"TRASAR\", "
"\"units\": \"PPM\", "
"\"min\": \"0\", "
"\"max\": \"300\", "
"\"node\": \"20\", "
"\"reg\": \"21\", "
"\"rtype\": \"1\", "
"\"type\": \"16\", "
"\"fmt\": \"%%.2f\" } "
);
bool status = GLOBAL_mdot->saveUserFile(argv[1], (void *)data_buf, 1024);
if( status != true ) {
logInfo("(%d)save file failed, status=%d", __LINE__, status);
}
}
/*****************************************************************************
* Function: cmd_cof
* Description: create output file
*
* @param argc-> number of args
* @param argv-> filename
* @return none
*****************************************************************************/
void cmd_cof(int argc, char **argv)
{
if ( argc != 2 ) {
printf("\rusage: cof <filename>\r\n");
return;
}
char data_buf[1024];
snprintf(data_buf, sizeof(data_buf),
"{ "
"\"id\": \"i_tra1\", "
"\"name\": \"TRASAR\", "
"\"units\": \"PPM\", "
"\"min\": \"0\", "
"\"max\": \"300\", "
"\"node\": \"20\", "
"\"reg\": \"21\", "
"\"rtype\": \"1\", "
"\"type\": \"16\", "
"\"fmt\": \"%%.2f\", "
"\"toperiod\": \"0\", "
"\"scalelo\": \"0\", "
"\"scalehi\": \"100\" }"
);
bool status = GLOBAL_mdot->saveUserFile(argv[1], (void *)data_buf, 1024);
if( status != true ) {
logInfo("(%d)save file failed, status=%d", __LINE__, status);
}
}
/*****************************************************************************
* Function: cmd_heap
* Description: display heap statistics
*
* @param argc (not used)
* @param argv (not used)
* @return none
*****************************************************************************/
void cmd_heap(int argc, char **argv)
{
UNUSED(argc), UNUSED(argv);
__heapstats((__heapprt)fprintf,stderr); // print initial free heap size
}
/*****************************************************************************
* Function: cmd_modify
* Description: modify an active control
*
* @param argc (not used)
* @param argv (not used)
* @return none
*****************************************************************************/
void cmd_modify(int argc, char **argv)
{
// stubbed
printf("\rNot yet implemented.\n");
return;
}
/*****************************************************************************
* Function: cmd_stats
* Description: display mDot stats
*
* @param argc (not used)
* @param argv (not used)
* @return none
*****************************************************************************/
void cmd_stats(int argc, char **argv)
{
UNUSED(argc);
UNUSED(argv);
mDot::mdot_stats stats = GLOBAL_mdot->getStats();
printf("\r Up: %u\n", stats.Up);
printf("\r Down: %u\n", stats.Down);
printf("\r Joins: %u\n", stats.Joins);
printf("\r JoinFails: %u\n", stats.JoinFails);
printf("\r MissedAcks: %u\n", stats.MissedAcks);
printf("\r CRCErrors: %u\n", stats.CRCErrors);
printf("\r\n");
}
/*****************************************************************************
* Function: cmd_resetStats
* Description: resets the mDot stats
*
* @param argc (not used)
* @param argv (not used)
* @return none
*****************************************************************************/
void cmd_resetStats(int argc, char **argv)
{
UNUSED(argc);
UNUSED(argv);
GLOBAL_mdot->resetStats();
}
/*****************************************************************************
* Function: cmd_rssiStats
* Description: displays mDot RSSI statistics
*
* @param argc (not used)
* @param argv (not used)
* @return none
*****************************************************************************/
void cmd_rssiStats(int argc, char **argv)
{
UNUSED(argc);
UNUSED(argv);
mDot::rssi_stats s = GLOBAL_mdot->getRssiStats();
printf("\r Last: %d dB\n", s.last);
printf("\r Min: %d dB\n", s.min);
printf("\r Max: %d dB\n", s.max);
printf("\r Avg: %d dB\n", s.avg);
printf("\r\n");
}
/*****************************************************************************
* Function: cmd_snrStats
* Description: displays signal-to-noise ratio stats
*
* @param argc (not used)
* @param argv (not used)
* @return none
*****************************************************************************/
void cmd_snrStats(int argc, char **argv)
{
mDot::snr_stats s = GLOBAL_mdot->getSnrStats();
printf("\r Last: %d cB\n", s.last);
printf("\r Min: %d cB\n", s.min);
printf("\r Max: %d cB\n", s.max);
printf("\r Avg: %d cB\n", s.avg);
printf("\r\n");
}
/*****************************************************************************
* Function: cmd_stack
* Description: display thread stack statisics
*
* @param argc (not used)
* @param argv (not used)
* @return none
*****************************************************************************/
void cmd_stack(int argc, char **argv)
{
vector<pair<string, Thread*> > taskList;
//simply add your task to the list...
taskList.push_back(make_pair((string)"AnalyticsLogger", GLOBAL_analyticsLogger_thread));
taskList.push_back(make_pair((string)"BLEHandler", GLOBAL_BLE_thread));
taskList.push_back(make_pair((string)"CloudDataHandler", GLOBAL_CDH_thread));
taskList.push_back(make_pair((string)"ConfigHandler", GLOBAL_configHandler_thread));
taskList.push_back(make_pair((string)"ControlTask", GLOBAL_controlTask_thread));
taskList.push_back(make_pair((string)"ModbusMaster", GLOBAL_modbusMaster_thread));
for ( vector<pair<string, Thread*> >::iterator pos = taskList.begin();
pos != taskList.end(); ++ pos) {
printf("\r %-32s size/free/used/max = %u/%u/%u/%u\n",
pos->first.c_str(),
pos->second->stack_size(),
pos->second->free_stack(),
pos->second->used_stack(),
pos->second->max_stack());
}
printf("\r\n");
}
