00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef _COMMANDS_H
00024 #define _COMMANDS_H
00025 #include <stdint.h>
00026 #include <stdarg.h>
00027 #include "types.h"
00033 #define COMMAND_OK 0x0000
00034 #define COMMAND_ERROR_TOO_MANY_CONNS 0x0001
00035 #define COMMAND_ERROR_UNKNOWN_COMMAND 0x0002
00036 #define COMMAND_ERROR_WRONG_ARGS 0x0003
00037 #define COMMAND_ERROR_AUTHENTICATION 0x0004
00038 #define COMMAND_ERROR_GENERIC 0xffff
00043 #define MAX_ERR_MSG 256
00044
00048 typedef struct Command_t
00049 {
00050 char *command;
00051 bool tokenise;
00052 int minArgs;
00053 int maxArgs;
00054 char *shortHelp;
00055 char *longHelp;
00056 void (*commandfunc)(int argc, char **argv);
00057 }Command_t;
00058
00062 typedef void (*CommandVariableGet_t)(char *name);
00066 typedef void (*CommandVariableSet_t)(char *name, int argc, char **argv);
00067
00071 typedef struct CommandVariable_t
00072 {
00073 char *name;
00076 char *description;
00077 CommandVariableGet_t get;
00079 CommandVariableSet_t set;
00081 }CommandVariable_t;
00082
00083
00087 typedef struct CommandContext_t
00088 {
00089 char *interface;
00091 bool remote;
00093 int (*printf)(struct CommandContext_t *context, const char *fmt, va_list arg_ptr);
00095 char *(*gets)(struct CommandContext_t *context,char *buffer, int len);
00097 void *privateArg;
00098 Command_t *commands;
00099 bool authenticated;
00100 uint16_t errorNumber;
00101 char errorMessage[MAX_ERR_MSG];
00102 }CommandContext_t;
00103
00107 #define CommandError(_errcode, _msgformat...) \
00108 do{\
00109 CommandContext_t *context = CommandContextGet();\
00110 context->errorNumber = _errcode;\
00111 snprintf(context->errorMessage, MAX_ERR_MSG,_msgformat);\
00112 }while(0)
00113
00118 #define CommandCheckAuthenticated() \
00119 do{\
00120 CommandContext_t *context = CommandContextGet();\
00121 if (!context->authenticated)\
00122 {\
00123 CommandError(COMMAND_ERROR_AUTHENTICATION, "Not authenticated!");\
00124 return;\
00125 }\
00126 }while(0)
00127
00132 int CommandInit(void);
00133
00137 void CommandDeInit(void);
00138
00143 void CommandRegisterCommands(Command_t *commands);
00144
00150 void CommandUnRegisterCommands(Command_t *commands);
00151
00156 void CommandLoop(void);
00157
00163 int CommandProcessFile(char *file);
00164
00170 bool CommandExecuteConsole(char *line);
00171
00178 bool CommandExecute(CommandContext_t *context, char *command);
00179
00186 int CommandPrintf(const char *fmt, ...);
00187
00194 char *CommandGets(char *buffer, int len);
00195
00200 CommandContext_t *CommandContextGet(void);
00201
00206 void CommandRegisterVariable(CommandVariable_t *handler);
00207
00212 void CommandUnRegisterVariable(CommandVariable_t *handler);
00214 #endif