Ken Yourek / ucmd

Dependents:   nucleo_ucmd_helloworld

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ucmd_internal.h Source File

ucmd_internal.h

00001 #ifndef     UCMD_INTERNAL_H
00002 #define     UCMD_INTERNAL_H
00003 
00004 #include    "ucmd.h"
00005 
00006 extern      const char                          uc_cmd_terminator;
00007 extern      const char                          ucTok_separator;
00008 extern      const char*                         ucOpt_validation_err_invalid_argument_prefix;
00009 extern      const char*                         ucOpt_validation_err_invalid_switch_prefix;
00010 extern      const char*                         ucOpt_validation_err_invalid_switch_argument_prefix;
00011 
00012             struct                              ucCmdParser {
00013             ucCmdTok*                           (*parse)(ucCmdParser *p, char *cmd); };
00014 
00015 uc_EXPORTED ucOpt*                              ucOpt_init(ucOpt*, const char *name, const char *desc, ucBool is_required);
00016             struct                              ucOpt {
00017             const char*                         name;
00018             const char*                         desc;
00019             ucBool                              is_required; };
00020 
00021 uc_EXPORTED const char*                         ucArgOpt_format_validation_err(ucArgOpt*, ucCmdLine *cmd, ucArgTok *arg_tok, const char *switch_name);
00022 uc_EXPORTED ucArgOpt*                           ucArgOpt_init(ucArgOpt*, const char *name, const char *desc, ucBool is_required, int min_tok_count, int max_tok_count, ucBool is_boolean, ucBool is_numeric, ucArgOpt_NUMERIC_TYPE numeric_min, ucArgOpt_NUMERIC_TYPE numeric_max, ucArgOpt *next);
00023             struct                              ucArgOpt {
00024             ucOpt                               base;
00025             ucBool                              is_boolean;
00026             ucBool                              is_numeric;
00027             ucArgOpt_NUMERIC_TYPE               numeric_min;
00028             ucArgOpt_NUMERIC_TYPE               numeric_max;
00029             int                                 max_tok_count;
00030             int                                 min_tok_count;
00031             ucArgOpt*                           next; };            
00032 
00033 uc_EXPORTED const char*                         ucArgOptOwner_format_validation_err(ucArgOptOwner*, ucCmdLine *cmd, ucArgTok *arg_tok, const char *switch_name);
00034 uc_EXPORTED ucArgOptOwner*                      ucArgOptOwner_init(ucArgOptOwner*, const char *name, const char *desc, ucBool is_required, ucArgOpt *arg_opt);
00035             struct                              ucArgOptOwner {
00036             ucOpt                               base;
00037             ucArgOpt*                           arg_opt; };
00038 
00039 uc_EXPORTED const char*                         ucSwitchOpt_format_validation_err(ucSwitchOpt*, ucCmdLine *cmd, ucSwitchTok *switch_tok);
00040 uc_EXPORTED ucSwitchOpt*                        ucSwitchOpt_init(ucSwitchOpt*, const char *name, const char *desc, ucBool is_required, ucArgOpt *arg_opt, ucSwitchOpt *next);
00041             struct                              ucSwitchOpt {
00042             ucArgOptOwner                       base;
00043             ucSwitchOpt*                        next; };
00044 
00045 uc_EXPORTED ucBool                              ucCmdLine_handle_invalid_command(ucCmdLine*, const char *invalid_command);
00046 uc_EXPORTED ucCmdLine*                          ucCmdLine_init(ucCmdLine*);
00047             struct                              ucCmdLine {
00048             ucCmdTok*                           cmd_tok;
00049             ucCmdLine_TransmitFunc*             transmit;
00050             ucCmdLine_IsCanceledFunc*           is_canceled;
00051             ucCmdLine_HandleInvalidCommandFunc* handle_invalid_command;
00052             void*                               transmit_state;
00053             void*                               is_canceled_state;
00054             void*                               handle_invalid_command_state;
00055             const char*                         response_terminator;
00056             const char*                         command_acknowledgment;
00057             char                                response[ucCmdLine_RESPONSE_SIZE];
00058             char                                response_buffer[ucCmdLine_RESPONSE_SIZE];
00059             ucBool                              is_quiet; };
00060 
00061 uc_EXPORTED const char*                         ucCmdLineOpt_format_validation_err(ucCmdLineOpt*, ucCmdLine *cmd);
00062 uc_EXPORTED ucCmdLineOpt*                       ucCmdLineOpt_init(ucCmdLineOpt*, ucCmdLineOpt_Func *func, void* state, const char *name, const char *desc, ucArgOpt* arg_opt, ucSwitchOpt *switch_opt, ucCmdLineOpt *next);
00063             struct                              ucCmdLineOpt {
00064             ucArgOptOwner                       base;
00065             ucCmdLineOpt_Func*                  func;
00066             void*                               state;
00067             ucSwitchOpt*                        switch_opt;
00068             ucCmdLineOpt*                       next; };
00069 
00070 uc_EXPORTED ucCmdLineApp*                       ucCmdLineApp_init(ucCmdLineApp*, ucCmdParser*, ucCmdLine*);
00071 uc_EXPORTED char*                               ucCmdLineApp_receive(ucCmdLineApp*);
00072             struct                              ucCmdLineApp {
00073             ucCmdLine*                          cmd;
00074             ucCmdParser*                        cmd_parser;
00075             ucCmdLineApp_ReceiveFunc*           receive;
00076             void*                               receive_state;
00077             ucErr                               (*run)(ucCmdLineApp *p, ucCmdLineOpt *cmd_opt);
00078             const char*                         help_command;
00079             const char*                         quit_command;
00080             const char*                         escape_response;            
00081             char                                cmd_str[ucCmdLineApp_CMD_STR_SIZE + 1]; };
00082 
00083 /* 
00084  * Summary:
00085  *   Defines a macro that is used to provide a dynamic
00086  *   feel to static-memory allocation. Calling it in a
00087  *   source file exposes functions to create and destroy
00088  *   objects of the specified type.
00089  * Parameters:
00090  *   TYPE: The type of the object instances that are
00091  *         created and destroyed using the manager.
00092  *   COUNT: The number of object instances that is
00093  *          available to the program. Once this number
00094  *          is exceeded, NULL is returned from the
00095  *          create function until instances are released
00096  *          using the destroy function.
00097  */
00098 #define ucMemoryManager_INIT(TYPE, COUNT)                                           \
00099     typedef struct ucMemoryManager_Instance {                                       \
00100         TYPE inst;                                                                  \
00101         char used;                                                                  \
00102     } ucMemoryManager_Instance;                                                     \
00103                                                                                     \
00104     static ucMemoryManager_Instance ucMemoryManager_Instances[COUNT] = { 0 };       \
00105                                                                                     \
00106     static TYPE *ucMemoryManager_create(void) {                                     \
00107         int i;                                                                      \
00108         ucMemoryManager_Instance *inst;                                             \
00109         for (i = 0; i < COUNT; i++) {                                               \
00110             inst = &ucMemoryManager_Instances[i];                                   \
00111             if (inst->used == 0) {                                                  \
00112                 inst->used = 1;                                                     \
00113                 return &inst->inst;                                                 \
00114             }                                                                       \
00115         }                                                                           \
00116         return NULL;                                                                \
00117     }                                                                               \
00118                                                                                     \
00119     static void ucMemoryManager_destroy(TYPE *p) {                                  \
00120         int i;                                                                      \
00121         ucMemoryManager_Instance *inst;                                             \
00122         for (i = 0; i < COUNT; i++) {                                               \
00123             inst = &ucMemoryManager_Instances[i];                                   \
00124             if (p == (&inst->inst)) {                                               \
00125                 inst->used = 0;                                                     \
00126             }                                                                       \
00127         }                                                                           \
00128     }
00129 
00130 #endif /* #ifndef UCMD_INTERNAL_H */
00131