Ken Yourek / ucmd

Dependents:   nucleo_ucmd_helloworld

Committer:
kyourek
Date:
Mon Oct 12 21:09:07 2015 +0000
Revision:
0:9e2fc73e5a12
Initial commit of the ucmd library for mbed.; https://github.com/kyourek/ucmd

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kyourek 0:9e2fc73e5a12 1 #ifndef UCMD_INTERNAL_H
kyourek 0:9e2fc73e5a12 2 #define UCMD_INTERNAL_H
kyourek 0:9e2fc73e5a12 3
kyourek 0:9e2fc73e5a12 4 #include "ucmd.h"
kyourek 0:9e2fc73e5a12 5
kyourek 0:9e2fc73e5a12 6 extern const char uc_cmd_terminator;
kyourek 0:9e2fc73e5a12 7 extern const char ucTok_separator;
kyourek 0:9e2fc73e5a12 8 extern const char* ucOpt_validation_err_invalid_argument_prefix;
kyourek 0:9e2fc73e5a12 9 extern const char* ucOpt_validation_err_invalid_switch_prefix;
kyourek 0:9e2fc73e5a12 10 extern const char* ucOpt_validation_err_invalid_switch_argument_prefix;
kyourek 0:9e2fc73e5a12 11
kyourek 0:9e2fc73e5a12 12 struct ucCmdParser {
kyourek 0:9e2fc73e5a12 13 ucCmdTok* (*parse)(ucCmdParser *p, char *cmd); };
kyourek 0:9e2fc73e5a12 14
kyourek 0:9e2fc73e5a12 15 uc_EXPORTED ucOpt* ucOpt_init(ucOpt*, const char *name, const char *desc, ucBool is_required);
kyourek 0:9e2fc73e5a12 16 struct ucOpt {
kyourek 0:9e2fc73e5a12 17 const char* name;
kyourek 0:9e2fc73e5a12 18 const char* desc;
kyourek 0:9e2fc73e5a12 19 ucBool is_required; };
kyourek 0:9e2fc73e5a12 20
kyourek 0:9e2fc73e5a12 21 uc_EXPORTED const char* ucArgOpt_format_validation_err(ucArgOpt*, ucCmdLine *cmd, ucArgTok *arg_tok, const char *switch_name);
kyourek 0:9e2fc73e5a12 22 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);
kyourek 0:9e2fc73e5a12 23 struct ucArgOpt {
kyourek 0:9e2fc73e5a12 24 ucOpt base;
kyourek 0:9e2fc73e5a12 25 ucBool is_boolean;
kyourek 0:9e2fc73e5a12 26 ucBool is_numeric;
kyourek 0:9e2fc73e5a12 27 ucArgOpt_NUMERIC_TYPE numeric_min;
kyourek 0:9e2fc73e5a12 28 ucArgOpt_NUMERIC_TYPE numeric_max;
kyourek 0:9e2fc73e5a12 29 int max_tok_count;
kyourek 0:9e2fc73e5a12 30 int min_tok_count;
kyourek 0:9e2fc73e5a12 31 ucArgOpt* next; };
kyourek 0:9e2fc73e5a12 32
kyourek 0:9e2fc73e5a12 33 uc_EXPORTED const char* ucArgOptOwner_format_validation_err(ucArgOptOwner*, ucCmdLine *cmd, ucArgTok *arg_tok, const char *switch_name);
kyourek 0:9e2fc73e5a12 34 uc_EXPORTED ucArgOptOwner* ucArgOptOwner_init(ucArgOptOwner*, const char *name, const char *desc, ucBool is_required, ucArgOpt *arg_opt);
kyourek 0:9e2fc73e5a12 35 struct ucArgOptOwner {
kyourek 0:9e2fc73e5a12 36 ucOpt base;
kyourek 0:9e2fc73e5a12 37 ucArgOpt* arg_opt; };
kyourek 0:9e2fc73e5a12 38
kyourek 0:9e2fc73e5a12 39 uc_EXPORTED const char* ucSwitchOpt_format_validation_err(ucSwitchOpt*, ucCmdLine *cmd, ucSwitchTok *switch_tok);
kyourek 0:9e2fc73e5a12 40 uc_EXPORTED ucSwitchOpt* ucSwitchOpt_init(ucSwitchOpt*, const char *name, const char *desc, ucBool is_required, ucArgOpt *arg_opt, ucSwitchOpt *next);
kyourek 0:9e2fc73e5a12 41 struct ucSwitchOpt {
kyourek 0:9e2fc73e5a12 42 ucArgOptOwner base;
kyourek 0:9e2fc73e5a12 43 ucSwitchOpt* next; };
kyourek 0:9e2fc73e5a12 44
kyourek 0:9e2fc73e5a12 45 uc_EXPORTED ucBool ucCmdLine_handle_invalid_command(ucCmdLine*, const char *invalid_command);
kyourek 0:9e2fc73e5a12 46 uc_EXPORTED ucCmdLine* ucCmdLine_init(ucCmdLine*);
kyourek 0:9e2fc73e5a12 47 struct ucCmdLine {
kyourek 0:9e2fc73e5a12 48 ucCmdTok* cmd_tok;
kyourek 0:9e2fc73e5a12 49 ucCmdLine_TransmitFunc* transmit;
kyourek 0:9e2fc73e5a12 50 ucCmdLine_IsCanceledFunc* is_canceled;
kyourek 0:9e2fc73e5a12 51 ucCmdLine_HandleInvalidCommandFunc* handle_invalid_command;
kyourek 0:9e2fc73e5a12 52 void* transmit_state;
kyourek 0:9e2fc73e5a12 53 void* is_canceled_state;
kyourek 0:9e2fc73e5a12 54 void* handle_invalid_command_state;
kyourek 0:9e2fc73e5a12 55 const char* response_terminator;
kyourek 0:9e2fc73e5a12 56 const char* command_acknowledgment;
kyourek 0:9e2fc73e5a12 57 char response[ucCmdLine_RESPONSE_SIZE];
kyourek 0:9e2fc73e5a12 58 char response_buffer[ucCmdLine_RESPONSE_SIZE];
kyourek 0:9e2fc73e5a12 59 ucBool is_quiet; };
kyourek 0:9e2fc73e5a12 60
kyourek 0:9e2fc73e5a12 61 uc_EXPORTED const char* ucCmdLineOpt_format_validation_err(ucCmdLineOpt*, ucCmdLine *cmd);
kyourek 0:9e2fc73e5a12 62 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);
kyourek 0:9e2fc73e5a12 63 struct ucCmdLineOpt {
kyourek 0:9e2fc73e5a12 64 ucArgOptOwner base;
kyourek 0:9e2fc73e5a12 65 ucCmdLineOpt_Func* func;
kyourek 0:9e2fc73e5a12 66 void* state;
kyourek 0:9e2fc73e5a12 67 ucSwitchOpt* switch_opt;
kyourek 0:9e2fc73e5a12 68 ucCmdLineOpt* next; };
kyourek 0:9e2fc73e5a12 69
kyourek 0:9e2fc73e5a12 70 uc_EXPORTED ucCmdLineApp* ucCmdLineApp_init(ucCmdLineApp*, ucCmdParser*, ucCmdLine*);
kyourek 0:9e2fc73e5a12 71 uc_EXPORTED char* ucCmdLineApp_receive(ucCmdLineApp*);
kyourek 0:9e2fc73e5a12 72 struct ucCmdLineApp {
kyourek 0:9e2fc73e5a12 73 ucCmdLine* cmd;
kyourek 0:9e2fc73e5a12 74 ucCmdParser* cmd_parser;
kyourek 0:9e2fc73e5a12 75 ucCmdLineApp_ReceiveFunc* receive;
kyourek 0:9e2fc73e5a12 76 void* receive_state;
kyourek 0:9e2fc73e5a12 77 ucErr (*run)(ucCmdLineApp *p, ucCmdLineOpt *cmd_opt);
kyourek 0:9e2fc73e5a12 78 const char* help_command;
kyourek 0:9e2fc73e5a12 79 const char* quit_command;
kyourek 0:9e2fc73e5a12 80 const char* escape_response;
kyourek 0:9e2fc73e5a12 81 char cmd_str[ucCmdLineApp_CMD_STR_SIZE + 1]; };
kyourek 0:9e2fc73e5a12 82
kyourek 0:9e2fc73e5a12 83 /*
kyourek 0:9e2fc73e5a12 84 * Summary:
kyourek 0:9e2fc73e5a12 85 * Defines a macro that is used to provide a dynamic
kyourek 0:9e2fc73e5a12 86 * feel to static-memory allocation. Calling it in a
kyourek 0:9e2fc73e5a12 87 * source file exposes functions to create and destroy
kyourek 0:9e2fc73e5a12 88 * objects of the specified type.
kyourek 0:9e2fc73e5a12 89 * Parameters:
kyourek 0:9e2fc73e5a12 90 * TYPE: The type of the object instances that are
kyourek 0:9e2fc73e5a12 91 * created and destroyed using the manager.
kyourek 0:9e2fc73e5a12 92 * COUNT: The number of object instances that is
kyourek 0:9e2fc73e5a12 93 * available to the program. Once this number
kyourek 0:9e2fc73e5a12 94 * is exceeded, NULL is returned from the
kyourek 0:9e2fc73e5a12 95 * create function until instances are released
kyourek 0:9e2fc73e5a12 96 * using the destroy function.
kyourek 0:9e2fc73e5a12 97 */
kyourek 0:9e2fc73e5a12 98 #define ucMemoryManager_INIT(TYPE, COUNT) \
kyourek 0:9e2fc73e5a12 99 typedef struct ucMemoryManager_Instance { \
kyourek 0:9e2fc73e5a12 100 TYPE inst; \
kyourek 0:9e2fc73e5a12 101 char used; \
kyourek 0:9e2fc73e5a12 102 } ucMemoryManager_Instance; \
kyourek 0:9e2fc73e5a12 103 \
kyourek 0:9e2fc73e5a12 104 static ucMemoryManager_Instance ucMemoryManager_Instances[COUNT] = { 0 }; \
kyourek 0:9e2fc73e5a12 105 \
kyourek 0:9e2fc73e5a12 106 static TYPE *ucMemoryManager_create(void) { \
kyourek 0:9e2fc73e5a12 107 int i; \
kyourek 0:9e2fc73e5a12 108 ucMemoryManager_Instance *inst; \
kyourek 0:9e2fc73e5a12 109 for (i = 0; i < COUNT; i++) { \
kyourek 0:9e2fc73e5a12 110 inst = &ucMemoryManager_Instances[i]; \
kyourek 0:9e2fc73e5a12 111 if (inst->used == 0) { \
kyourek 0:9e2fc73e5a12 112 inst->used = 1; \
kyourek 0:9e2fc73e5a12 113 return &inst->inst; \
kyourek 0:9e2fc73e5a12 114 } \
kyourek 0:9e2fc73e5a12 115 } \
kyourek 0:9e2fc73e5a12 116 return NULL; \
kyourek 0:9e2fc73e5a12 117 } \
kyourek 0:9e2fc73e5a12 118 \
kyourek 0:9e2fc73e5a12 119 static void ucMemoryManager_destroy(TYPE *p) { \
kyourek 0:9e2fc73e5a12 120 int i; \
kyourek 0:9e2fc73e5a12 121 ucMemoryManager_Instance *inst; \
kyourek 0:9e2fc73e5a12 122 for (i = 0; i < COUNT; i++) { \
kyourek 0:9e2fc73e5a12 123 inst = &ucMemoryManager_Instances[i]; \
kyourek 0:9e2fc73e5a12 124 if (p == (&inst->inst)) { \
kyourek 0:9e2fc73e5a12 125 inst->used = 0; \
kyourek 0:9e2fc73e5a12 126 } \
kyourek 0:9e2fc73e5a12 127 } \
kyourek 0:9e2fc73e5a12 128 }
kyourek 0:9e2fc73e5a12 129
kyourek 0:9e2fc73e5a12 130 #endif /* #ifndef UCMD_INTERNAL_H */
kyourek 0:9e2fc73e5a12 131