Clean up code. Add comments. Init module with callback to register all
[bertos.git] / bertos / net / protocol.c
index e2ef94d65cc9b28680d2d36caae12923c5631354..8f12d065dbb28d76435caa9e05b7f73f4e14c525 100644 (file)
  * invalidate any other reasons why the executable file might be covered by
  * the GNU General Public License.
  *
- * Copyright 2003, 2004, 2006 Develer S.r.l. (http://www.develer.com/)
+ * Copyright 2003, 2004, 2006, 2012 Develer S.r.l. (http://www.develer.com/)
  * Copyright 2000 Bernie Innocenti <bernie@codewiz.org>
  *
  * -->
  *
- * \brief Implementation of the command protocol between the board and the host
+ * \brief Protocol module.
  *
+ * This module supply a simple ascii protocol to send commands to
+ * the device like pc "terminal". To use it we need to define all command
+ * that we want supply, and then we should register they using a user defined
+ * function. All commands can take arguments or/and return a value.
+ *
+ * \code
+ * #include "verstag.h"
+ * #include <mware/parser.h>
+ *
+ * //Define a function ver, that return 3 int.
+ * //This macro will expand into a fuction named "ver" that not take
+ * //an input and return 3 int (ddd).
+ * MAKE_CMD(ver, "", "ddd",
+ * ({
+ *     args[1].l = VERS_MAJOR;
+ *     args[2].l = VERS_MINOR;
+ *     args[3].l = VERS_REV;
+ *     0;
+ * }), 0);
+ *
+ *
+ * //Define the function to pass at protocol_init, to register
+ * //all defined protocol function.
+ * static void protocol_registerCmds(void)
+ * {
+ *       REGISTER_CMD(ver);
+ * }
+ *
+ *
+ * //Init the protocol module whit comunication channel and
+ * //the callback to register the defined protocol functions.
+ * protocol_init(&socket.fd, protocol_registerCmds);
+ * \endcode
  *
  * \author Giovanni Bajo <rasky@develer.com>
  * \author Marco Benelli <marco@develer.com>
  */
 
 #include "protocol.h"
-#include "verstag.h"
 
 #include "cfg/cfg_parser.h"
+
 #include <cfg/compiler.h>
 #include <cfg/debug.h>
 
 #include <drv/timer.h>
-#include <drv/ser.h>
 
 #include <mware/readline.h>
 #include <mware/parser.h>
 #include <stdlib.h>
 #include <string.h>
 
-// Define the format string for ADC
-#define ADC_FORMAT_STR "dddd"
-#define ADC_CHANNEL_NUM    4
-
 // DEBUG: set to 1 to force interactive mode
 #define FORCE_INTERACTIVE         1
 
 /**
  * True if we are in interactive mode, false if we are in protocol mode.
  * In interactive mode, commands are read through readline() (prompt,
- * completion, history) without IDs, and replies/errors are sent to the serial
- * output.
+ * completion, history) and replies/errors are sent to the output channel.
  * In protocol mode, we implement the default protocol
  */
 static bool interactive;
@@ -77,7 +104,6 @@ static bool interactive;
 /// Readline context, used for interactive mode.
 static struct RLContext rl_ctx;
 
-uint8_t reg_status_dout;
 /**
  * Send a NAK asking the host to send the current message again.
  *
@@ -102,8 +128,7 @@ static void protocol_prompt(KFile *fd)
  * Print args on s, with format specified in t->result_fmt.
  * Return number of valid arguments or -1 in case of error.
  */
-static bool protocol_reply(KFile *fd, const struct CmdTemplate *t,
-                         const parms *args)
+static bool protocol_reply(KFile *fd, const struct CmdTemplate *t, const parms *args)
 {
        unsigned short offset = strlen(t->arg_fmt) + 1;
        unsigned short nres = strlen(t->result_fmt);
@@ -121,7 +146,8 @@ static bool protocol_reply(KFile *fd, const struct CmdTemplate *t,
 
                else
                {
-                       abort();
+                       //abort();
+                       kprintf("errore\n");
                }
        }
        kfile_printf(fd, "\r\n");
@@ -167,20 +193,16 @@ static void protocol_parse(KFile *fd, const char *buf)
 
 void protocol_run(KFile *fd)
 {
-       /**
-        * \todo to be removed, we could probably access the serial FIFO
-        * directly
-        */
        static char linebuf[80];
 
        if (!interactive)
        {
                kfile_gets(fd, linebuf, sizeof(linebuf));
 
-               // reset serial port error anyway
+               /* Clear errors on channel */
                kfile_clearerr(fd);
 
-               // check message minimum length
+               /* check message minimum length */
                if (linebuf[0])
                {
                        /* If we enter lines beginning with sharp(#)
@@ -204,7 +226,7 @@ void protocol_run(KFile *fd)
                const char *buf;
 
                /*
-                * Read a line from serial. We use a temporary buffer
+                * Read a line from channel. We use a temporary buffer
                 * because otherwise we would have to extract a message
                 * from the port immediately: there might not be any
                 * available, and one might get free while we read
@@ -241,30 +263,9 @@ void protocol_run(KFile *fd)
        }
 }
 
-/*
- * Commands.
- * TODO: Command declarations and definitions should be in another file(s).
- * Maybe we should use CMD_HUNK_TEMPLATE.
- *
- */
-
-MAKE_CMD(ver, "", "ddd",
-({
-       args[1].l = VERS_MAJOR;
-       args[2].l = VERS_MINOR;
-       args[3].l = VERS_REV;
-       0;
-}), 0);
-
-
-/* Register commands.  */
-static void protocol_registerCmds(void)
-{
-       REGISTER_CMD(ver);
-}
 
 /* Initialization: readline context, parser and register commands.  */
-void protocol_init(KFile *fd)
+void protocol_init(KFile *fd, protocol_t cmds_register)
 {
        interactive = FORCE_INTERACTIVE;
 
@@ -276,8 +277,8 @@ void protocol_init(KFile *fd)
        rl_sethook_clear(&rl_ctx, (clear_hook)kfile_clearerr,fd);
 
        parser_init();
-
-       protocol_registerCmds();
+       ASSERT(cmds_register);
+       cmds_register();
 
        protocol_prompt(fd);
 }