Main Page | Class List | Directories | File List | Class Members | File Members

ipmi_cmdlang.h

Go to the documentation of this file.
00001 /*
00002  * ipmi_cmdlang.h
00003  *
00004  * A command interpreter for OpenIPMI
00005  *
00006  * Author: MontaVista Software, Inc.
00007  *         Corey Minyard <minyard@mvista.com>
00008  *         source@mvista.com
00009  *
00010  * Copyright 2004 MontaVista Software Inc.
00011  *
00012  *  This program is free software; you can redistribute it and/or
00013  *  modify it under the terms of the GNU Lesser General Public License
00014  *  as published by the Free Software Foundation; either version 2 of
00015  *  the License, or (at your option) any later version.
00016  *
00017  *
00018  *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
00019  *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00020  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00021  *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00022  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00023  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
00024  *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00025  *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
00026  *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00027  *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00028  *
00029  *  You should have received a copy of the GNU Lesser General Public
00030  *  License along with this program; if not, write to the Free
00031  *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00032  */
00033 
00034 #ifndef __IPMI_CMDLANG_H
00035 #define __IPMI_CMDLANG_H
00036 
00037 #include <OpenIPMI/selector.h>
00038 
00039 #ifdef __cplusplus
00040 extern "C" {
00041 #endif
00042 
00043 /* Forward declaration */
00044 typedef struct ipmi_cmd_info_s ipmi_cmd_info_t;
00045 
00046 /*
00047  * A structure that must be passed into the command parser; it has
00048  * general information about the how the parser should handle thing
00049  * and generate its output.
00050  */
00051 typedef struct ipmi_cmdlang_s ipmi_cmdlang_t;
00052 
00053 /* Output is done in name:value pairs.  If you don't have a value,
00054    pass in NULL. */
00055 typedef void (*cmd_out_cb)(ipmi_cmdlang_t *info,
00056                            const char     *name,
00057                            const char     *value);
00058 typedef void (*cmd_out_b_cb)(ipmi_cmdlang_t *info,
00059                              const char     *name,
00060                              const char     *value,
00061                              unsigned int   len);
00062 
00063 /* Command-specific info. */
00064 typedef void (*cmd_info_cb)(ipmi_cmdlang_t *info);
00065 
00066 /* The user provides one of these when they call the command language
00067    interpreter.  It is used to report errors and generate output. */
00068 struct ipmi_cmdlang_s
00069 {
00070     cmd_out_cb   out;      /* Generate output with this. */
00071     cmd_info_cb  down;     /* Go down a level (new indention or
00072                               nesting) */
00073     cmd_info_cb  up;       /* Go up a level (leave the current
00074                               indention or nesting) */
00075     cmd_info_cb  done;     /* Called when the command is done.  If
00076                               there was an error, the err value in
00077                               info will be non-null. */
00078     cmd_out_b_cb out_binary; /* Generate binary output with this. */
00079     cmd_out_b_cb out_unicode; /* Generate unicode output with this. */
00080 
00081     /* OS handler to use for the commands.  Note that this may be set
00082        to NULL if not required, don't depend on them except in certain
00083        circumstances. */
00084     os_handler_t *os_hnd;
00085 
00086     /* Tells if we are outputting help. */
00087     int         help;
00088 
00089     /*
00090      * Error reporting
00091      */
00092     int          err;      /* If non-zero, the errno code of the error
00093                               that occurred. */
00094 
00095     /* If non-NULL, an error occurred and this is the error info.  If
00096        the error string is dynamically allocated (and thus should be
00097        freed), errstr_dynalloc should be set to true. */
00098     char         *errstr;
00099     int          errstr_dynalloc;
00100 
00101     /* If an error occurs, this will be set to the object name that
00102        was dealing with the error.  It may be an empty string if
00103        no object is handling the error.  This must be pre-allocated
00104        and the length set properly. */
00105     char         *objstr;
00106     int          objstr_len;
00107 
00108     /* This is the location of an error. */
00109     char         *location;
00110 
00111 
00112     void         *user_data; /* User data for anything the user wants */
00113 };
00114 
00115 /* Parse and handle the given command string.  This always calls the
00116    done function when complete. */
00117 void ipmi_cmdlang_handle(ipmi_cmdlang_t *cmdlang, char *str);
00118 
00119 /* If the event info is true, then the system will output object
00120    information with each add or change event. */
00121 void ipmi_cmdlang_set_evinfo(int evinfo);
00122 int ipmi_cmdlang_get_evinfo(void);
00123 
00124 /*
00125  * This is used to hold command information.
00126  */
00127 typedef struct ipmi_cmdlang_cmd_s ipmi_cmdlang_cmd_t;
00128 
00129 typedef void (*ipmi_cmdlang_handler_cb)(ipmi_cmd_info_t *cmd_info);
00130 typedef void (*ipmi_help_finisher_cb)(ipmi_cmdlang_t *cmdlang);
00131 
00132 /* Register a command as a subcommand of the parent, or into the main
00133    command list if parent is NULL.  The command will have the given
00134    name and help text.  When the command is executed, the handler will
00135    be called with a cmd_info structure passed in.  The handler_data parm
00136    passed in below will be in the "handler_data" field of the cmd_info
00137    structure.  Note that if you are attaching subcommands to this
00138    command, you should pass in a NULL handler.  Returns an error value. */
00139 int ipmi_cmdlang_reg_cmd(ipmi_cmdlang_cmd_t      *parent,
00140                          char                    *name,
00141                          char                    *help,
00142                          ipmi_cmdlang_handler_cb handler,
00143                          void                    *handler_data,
00144                          ipmi_help_finisher_cb   help_finish,
00145                          ipmi_cmdlang_cmd_t      **rv);
00146 
00147 /* Register a table of commands. */
00148 typedef struct ipmi_cmdlang_init_s
00149 {
00150     char                    *name;
00151     ipmi_cmdlang_cmd_t      **parent;
00152     char                    *help;
00153     ipmi_cmdlang_handler_cb handler;
00154     void                    *cb_data;
00155     ipmi_cmdlang_cmd_t      **new_val;
00156     ipmi_help_finisher_cb   help_finish;
00157 } ipmi_cmdlang_init_t;
00158 int ipmi_cmdlang_reg_table(ipmi_cmdlang_init_t *table, int len);
00159 
00160 
00161 /* The following functions handle parsing various OpenIPMI objects
00162    according to the naming standard.  If you pass it into a command
00163    registration as the handler and pass your function as the
00164    handler_data, your function will be called with the specified
00165    object.  The specific function type is given in the individual
00166    functions.  The cmd_info will be passed in as the cb_data.
00167 
00168    For instance, if you have a command that take an entity argument,
00169    then you could write:
00170      void ent_cmd_hnd(ipmi_entity_t *entity, void *cb_data)
00171      {
00172          ipmi_cmd_info_t *cmd_info = cb_data;
00173      }
00174 
00175      rv = ipmi_cmdlang_reg_cmd(parent, "ent_cmd", "The ent command",
00176                                ipmi_cmdlang_entity_handler, ent_cmd_hnd,
00177                                &cmd);
00178 */
00179 
00180 /* ipmi_domain_ptr_cb */
00181 void ipmi_cmdlang_domain_handler(ipmi_cmd_info_t *cmd_info);
00182 
00183 /* ipmi_entity_ptr_cb */
00184 void ipmi_cmdlang_entity_handler(ipmi_cmd_info_t *cmd_info);
00185 
00186 /* ipmi_sensor_ptr_cb */
00187 void ipmi_cmdlang_sensor_handler(ipmi_cmd_info_t *cmd_info);
00188 
00189 /* ipmi_control_ptr_cb */
00190 void ipmi_cmdlang_control_handler(ipmi_cmd_info_t *cmd_info);
00191 
00192 /* ipmi_mc_ptr_cb */
00193 void ipmi_cmdlang_mc_handler(ipmi_cmd_info_t *cmd_info);
00194 
00195 /* ipmi_connection_ptr_cb */
00196 void ipmi_cmdlang_connection_handler(ipmi_cmd_info_t *cmd_info);
00197 
00198 /* ipmi_pet_ptr_cb */
00199 void ipmi_cmdlang_pet_handler(ipmi_cmd_info_t *cmd_info);
00200 
00201 /* ipmi_lanparm_ptr_cb */
00202 void ipmi_cmdlang_lanparm_handler(ipmi_cmd_info_t *cmd_info);
00203 
00204 /* ipmi_solparm_ptr_cb */
00205 void ipmi_cmdlang_solparm_handler(ipmi_cmd_info_t *cmd_info);
00206 
00207 /* ipmi_fru_ptr_cb */
00208 void ipmi_cmdlang_fru_handler(ipmi_cmd_info_t *cmd_info);
00209 
00210 /* ipmi_pef_ptr_cb */
00211 void ipmi_cmdlang_pef_handler(ipmi_cmd_info_t *cmd_info);
00212 
00213 
00214 /* All output from the command language is in name/value pairs.  The
00215    value field may be NULL. */
00216 void ipmi_cmdlang_out(ipmi_cmd_info_t *info,
00217                       const char      *name,
00218                       const char      *value);
00219 void ipmi_cmdlang_out_int(ipmi_cmd_info_t *info,
00220                           const char      *name,
00221                           int             value);
00222 void ipmi_cmdlang_out_double(ipmi_cmd_info_t *info,
00223                              const char      *name,
00224                              double          value);
00225 void ipmi_cmdlang_out_hex(ipmi_cmd_info_t *info,
00226                           const char      *name,
00227                           int             value);
00228 void ipmi_cmdlang_out_long(ipmi_cmd_info_t *info,
00229                            const char      *name,
00230                            long            value);
00231 void ipmi_cmdlang_out_binary(ipmi_cmd_info_t *info,
00232                              const char      *name,
00233                              const char      *value,
00234                              unsigned int    len);
00235 void ipmi_cmdlang_out_unicode(ipmi_cmd_info_t *info,
00236                               const char      *name,
00237                               const char      *value,
00238                               unsigned int    len);
00239 void ipmi_cmdlang_out_type(ipmi_cmd_info_t      *info,
00240                            char                 *name,
00241                            enum ipmi_str_type_e type,
00242                            const char           *value,
00243                            unsigned int         len);
00244 void ipmi_cmdlang_out_ip(ipmi_cmd_info_t *info,
00245                          const char      *name,
00246                          struct in_addr  *ip_addr);
00247 void ipmi_cmdlang_out_mac(ipmi_cmd_info_t *info,
00248                           const char      *name,
00249                           unsigned char   mac_addr[6]);
00250 void ipmi_cmdlang_out_bool(ipmi_cmd_info_t *info,
00251                            const char      *name,
00252                            int             value);
00253 void ipmi_cmdlang_out_time(ipmi_cmd_info_t *info,
00254                            const char      *name,
00255                            ipmi_time_t     value);
00256 void ipmi_cmdlang_out_timeout(ipmi_cmd_info_t *info,
00257                               const char      *name,
00258                               ipmi_timeout_t  value);
00259 
00260 /* Generate info for an event. */
00261 void ipmi_cmdlang_event_out(ipmi_event_t    *event,
00262                             ipmi_cmd_info_t *cmd_info);
00263 
00264 /* The output from the command language is done at a nesting level.
00265    When you start outputting data for a new thing, you should "down"
00266    to create a new nesting level.  When you are done, you should
00267    "up". */
00268 void ipmi_cmdlang_down(ipmi_cmd_info_t *info);
00269 void ipmi_cmdlang_up(ipmi_cmd_info_t *info);
00270 
00271 /* A cmd info structure is refcounted, if you save a pointer to it you
00272    must "get" it.  When you are done, you must "put" it.  It will be
00273    destroyed (and the done routine called) after the last user puts it. */
00274 void ipmi_cmdlang_cmd_info_get(ipmi_cmd_info_t *info);
00275 void ipmi_cmdlang_cmd_info_put(ipmi_cmd_info_t *info);
00276 
00277 /* Helper functions */
00278 void ipmi_cmdlang_get_int(char *str, int *val, ipmi_cmd_info_t *info);
00279 void ipmi_cmdlang_get_double(char *str, double *val, ipmi_cmd_info_t *info);
00280 void ipmi_cmdlang_get_uchar(char *str, unsigned char *val,
00281                             ipmi_cmd_info_t *info);
00282 void ipmi_cmdlang_get_user(char *str, int *val, ipmi_cmd_info_t *info);
00283 void ipmi_cmdlang_get_time(char *str, ipmi_time_t *val, ipmi_cmd_info_t *info);
00284 void ipmi_cmdlang_get_timeout(char *str, ipmi_timeout_t *val,
00285                               ipmi_cmd_info_t *info);
00286 void ipmi_cmdlang_get_bool(char *str, int *val, ipmi_cmd_info_t *info);
00287 void ipmi_cmdlang_get_ip(char *str, struct in_addr *val,
00288                          ipmi_cmd_info_t *info);
00289 void ipmi_cmdlang_get_mac(char *str, unsigned char val[6],
00290                           ipmi_cmd_info_t *info);
00291 void ipmi_cmdlang_get_color(char *str, int *val, ipmi_cmd_info_t *info);
00292 void ipmi_cmdlang_get_threshold_ev(char                        *str,
00293                                    enum ipmi_thresh_e          *rthresh,
00294                                    enum ipmi_event_value_dir_e *rvalue_dir,
00295                                    enum ipmi_event_dir_e       *rdir,
00296                                    ipmi_cmd_info_t             *info);
00297 void ipmi_cmdlang_get_discrete_ev(char                  *str,
00298                                   int                   *roffset,
00299                                   enum ipmi_event_dir_e *rdir,
00300                                   ipmi_cmd_info_t       *info);
00301 void ipmi_cmdlang_get_threshold(char               *str,
00302                                 enum ipmi_thresh_e *rthresh,
00303                                 ipmi_cmd_info_t    *info);
00304 
00305 /* Call these to initialize and setup the command interpreter.  init
00306    should be called after the IPMI library proper is initialized, but
00307    before using it. */
00308 int ipmi_cmdlang_init(os_handler_t *os_hnd);
00309 void ipmi_cmdlang_cleanup(void);
00310 
00311 
00312 /* Allocate a cmd info structure that can be used to generate
00313    information to an event.  Make sure to call the put function when
00314    all the data has been output.  Note that the refcounts work like
00315    normal, you get it at one, when it goes to zero the structure will
00316    be returned. */
00317 ipmi_cmd_info_t *ipmi_cmdlang_alloc_event_info(void);
00318 
00319 typedef struct ipmi_cmdlang_event_s ipmi_cmdlang_event_t;
00320 
00321 /* Move to the first field. */
00322 void ipmi_cmdlang_event_restart(ipmi_cmdlang_event_t *event);
00323 
00324 enum ipmi_cmdlang_out_types {
00325     IPMI_CMDLANG_STRING,
00326     IPMI_CMDLANG_BINARY,
00327     IPMI_CMDLANG_UNICODE
00328 };
00329 
00330 /* Returns true if successful, false if no more fields left. */
00331 int ipmi_cmdlang_event_next_field(ipmi_cmdlang_event_t        *event,
00332                                   unsigned int                *level,
00333                                   enum ipmi_cmdlang_out_types *type,
00334                                   char                        **name,
00335                                   unsigned int                *len,
00336                                   char                        **value);
00337 
00338 /* Supplied by the user, used to report global errors (ones that don't
00339    deal with a specific command invocation).  The objstr is the name
00340    of the object dealing with the error (like the domain name, entity
00341    name, etc) or NULL if none.  The location is the file and procedure
00342    where the error occurred.  The errstr is a descriptive string and
00343    errval is an IPMI error value to be printed. */
00344 void ipmi_cmdlang_global_err(char *objstr,
00345                              char *location,
00346                              char *errstr,
00347                              int  errval);
00348 
00349 /* Supplied by the user to report events. */
00350 void ipmi_cmdlang_report_event(ipmi_cmdlang_event_t *event);
00351 
00352 /* In callbacks, you must use these to lock the cmd_info structure. */
00353 void ipmi_cmdlang_lock(ipmi_cmd_info_t *info);
00354 void ipmi_cmdlang_unlock(ipmi_cmd_info_t *info);
00355 
00356 int ipmi_cmdlang_get_argc(ipmi_cmd_info_t *info);
00357 char **ipmi_cmdlang_get_argv(ipmi_cmd_info_t *info);
00358 int ipmi_cmdlang_get_curr_arg(ipmi_cmd_info_t *info);
00359 ipmi_cmdlang_t *ipmi_cmdinfo_get_cmdlang(ipmi_cmd_info_t *info);
00360 
00361 #ifdef __cplusplus
00362 }
00363 #endif
00364 
00365 #endif /* __IPMI_CMDLANG_H */

© sourcejam.com 2005-2008