mbed_controller / Mbed 2 deprecated mbed_controller_demo

Dependencies:   CMDB EthernetInterface HTTPD dconfig mbed-rpc mbed-rtos mbed storage_on_flash

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers console.cpp Source File

console.cpp

Go to the documentation of this file.
00001 /**
00002  * @file console.cpp
00003  *
00004  * @brief console implementation using CMDB library.
00005  *
00006  * After boot, it prompts "CMD>" in console. Type "help" command to get help.
00007  */
00008 
00009 #include <vector>
00010 #include "mbed.h"
00011 #include "cmdb.h"
00012 #include "util.h"
00013 #include "main.h"
00014 
00015 #define CID_TEST        1
00016 #define CID_FREE        2
00017 #define CID_CFG_SET     3
00018 #define CID_CFG_RESET   4
00019 #define CID_CFG_SAVE    5
00020 #define CID_CFG_PRINT   6
00021 #define CID_IF_UP       7
00022 #define CID_IF_DOWN     8
00023 #define CID_IF_STAT     9
00024 
00025 
00026 /** Sample User Command Dispatcher.
00027  *
00028  * @parm cmdb the command interpreter object.
00029  * @parm cid the command id.
00030  */
00031 void my_dispatcher(Cmdb& cmdb, int cid)
00032 {
00033     //cmdb.printf("my_dispatcher: cid=%d\r\n", cid);
00034 
00035     switch (cid) {
00036         case CID_FREE :
00037             //cmdb.printf("my_dispatcher: parm 0=%d\r\n",cmdb.INTPARM(0));
00038             print_memstat();
00039             break;
00040         case CID_CFG_SET :
00041             _config.lock_config();
00042             if (!_config.value_replace(cmdb.STRINGPARM(0), cmdb.STRINGPARM(1))) {
00043                 cmdb.printf("invalid key='%s'\r\n", cmdb.STRINGPARM(0));
00044             }
00045             _config.unlock_config();
00046             break;
00047         case CID_CFG_RESET :
00048            _config.lock_config();
00049            _config.reset_default();
00050             _config.unlock_config();
00051             break;
00052         case CID_CFG_SAVE :
00053             _config.lock_config();
00054             if (!_config.save_config()) {
00055                 cmdb.printf("save fail\r\n");
00056             }
00057             _config.unlock_config();
00058             break;
00059         case CID_CFG_PRINT :
00060             _config.lock_config();
00061             _config.print_all();
00062             _config.unlock_config();
00063             break;
00064         case CID_IF_UP :
00065             send_main_message(MSG_IFUP, 0, 0);
00066             break;
00067         case CID_IF_DOWN:
00068             send_main_message(MSG_IFDOWN, 0, 0);
00069             break;
00070         case CID_IF_STAT :
00071             send_main_message(MSG_IFSTAT, 0, 0);
00072             break;
00073         default:
00074             printf("unknown CID=%u\r\n", cid);
00075             break;
00076     }
00077 }
00078 
00079 static const cmd user_cmd[] = {
00080     {"Test",SUBSYSTEM,CID_TEST,""  ,"* Test Subsystem"},
00081     {"free",CID_TEST,CID_FREE,""    ,"show amount of free memory", ""},
00082     {"cfgset",CID_TEST,CID_CFG_SET,"%s %s"  ,"config set", "config_key value"},
00083     {"cfgreset",CID_TEST,CID_CFG_RESET,""  ,"config reset", ""},
00084     {"cfgsave",CID_TEST,CID_CFG_SAVE,""  ,"config save to flash"},
00085     {"cfgprint",CID_TEST,CID_CFG_PRINT,""  ,"print all config"},
00086     {"ifup",CID_TEST,CID_IF_UP,""  ,"bring a network interface up"},
00087     {"ifdown",CID_TEST,CID_IF_DOWN,""  ,"bring a network interface down"},
00088     {"ifstat",CID_TEST,CID_IF_STAT,""  ,"print network info"},
00089 };
00090 
00091 
00092 void console_thread(void const *args)
00093 {
00094     Serial &serial = *((Serial *)args);
00095 
00096     // Test the serial connection by
00097     serial.printf("\r\n\r\nCmdb Command Interpreter Demo Version %0.2f.\r\n\r\n", Cmdb::version());
00098 
00099     //Create a Command Table Vector.
00100     std::vector<cmd> cmds(&user_cmd[0], &user_cmd[sizeof(user_cmd)/sizeof(user_cmd[0])]);
00101 
00102     //Add some of our own first...
00103     //Add some predefined...
00104     cmds.push_back(COMMANDS); //Handled by Cmdb internally.
00105     cmds.push_back(BOOT); //Handled by Cmdb internally.
00106 
00107     cmds.push_back(ECHO); //Handled by Cmdb internally.
00108     cmds.push_back(BOLD); //Handled by Cmdb internally.
00109     cmds.push_back(CLS);  //Handled by Cmdb internally.
00110 
00111     cmds.push_back(MACRO);  //Handled by Cmdb internally.
00112     cmds.push_back(RUN);    //Handled by Cmdb internally.
00113     cmds.push_back(MACROS); //Handled by Cmdb internally.
00114 
00115     //Add some predefined and mandatory...
00116     cmds.push_back(IDLE); //Handled by Cmdb internally.
00117     cmds.push_back(HELP); //Handled by Cmdb internally.
00118 
00119     //Create and initialize the Command Interpreter.
00120     Cmdb cmdb(serial, cmds, &my_dispatcher);
00121 
00122     //cmdb.printf("%d=%d\r\n",cmds[0].subs,cmds[0].cid);
00123     //cmdb.printf("%d=%d\r\n",cmds[1].subs,cmds[1].cid);
00124 
00125     while (1) {
00126         //Check for input...
00127         if (cmdb.hasnext()==true) {
00128 
00129             //Supply input to Command Interpreter
00130             if (cmdb.scan(cmdb.next())) {
00131             }
00132         }
00133 
00134         //For Macro Support we basically do the same but take characters from the macro buffer.
00135         //Example Macro: Test|Int_42|Idle
00136         while (cmdb.macro_hasnext()) {
00137             //Get and process next character.
00138             cmdb.scan(cmdb.macro_next());
00139 
00140             //After the last character we need to add a cr to force execution.
00141             if (!cmdb.macro_peek()) {
00142                 cmdb.scan(cr);
00143             }
00144         }
00145     }
00146 }
00147 
00148