Erick / Mbed 2 deprecated ICE_BLE_TEST

Dependencies:   NaturalTinyShell_ice libmDot-12Sept mbed-rtos mbed

Fork of ICE by Erick

src/CommandParser/cmd.cpp

Committer:
jmarkel44
Date:
2016-09-07
Revision:
13:c80c283f9db2
Parent:
12:ea87887ca7ad
Child:
14:cc916fa8dd11

File content as of revision 13:c80c283f9db2:

/*
 * ===============================================================
 *  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 "ntshell.h"
#include "ntopt.h"
#include "global.h"
#include "ConfigurationHandler.h"
#include "mDot.h"

Serial serial(USBTX, USBRX);
ntshell_t ntshell;

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         },
    {"heap",                "show heap statistics",                             cmd_heap            },
    {"help",                "help command",                                     cmd_help            },
    {"modify",              "modify a control",                                 cmd_modify          },
    {"reset",               "reset the controller",                             cmd_reset           },
    {"showControls",        "display active controls",                          cmd_ShowControls    },
    {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 *******************************/
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");
}


void cmd_ShowControls(int argc, char **argv)
{
    UNUSED(argc);
    UNUSED(argv);
    ConfigurationHandler_showControls();
}

void cmd_reset(int argc, char **argv)
{
    UNUSED(argc);
    UNUSED(argv);
    //NVIC_SystemReset();
    GLOBAL_mdot->resetCpu(); 
}

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\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);
    return;
}

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\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);
    return;
}

void cmd_heap(int argc, char **argv)
{
    UNUSED(argc), UNUSED(argv);
    __heapstats((__heapprt)fprintf,stderr);   // print initial free heap size
}

void cmd_modify(int argc, char **argv)
{
    // stubbed
    printf("\rNot yet implemented.\n");
    return;
}