~ubuntu-branches/ubuntu/karmic/openipmi/karmic

« back to all changes in this revision

Viewing changes to include/OpenIPMI/ipmi_cmdlang.h

  • Committer: Bazaar Package Importer
  • Author(s): Noèl Köthe
  • Date: 2005-07-04 21:29:17 UTC
  • Revision ID: james.westby@ubuntu.com-20050704212917-igddk5jawjmhrlay
Tags: upstream-2.0.1
Import upstream version 2.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * ipmi_cmdlang.h
 
3
 *
 
4
 * A command interpreter for OpenIPMI
 
5
 *
 
6
 * Author: MontaVista Software, Inc.
 
7
 *         Corey Minyard <minyard@mvista.com>
 
8
 *         source@mvista.com
 
9
 *
 
10
 * Copyright 2004 MontaVista Software Inc.
 
11
 *
 
12
 *  This program is free software; you can redistribute it and/or
 
13
 *  modify it under the terms of the GNU Lesser General Public License
 
14
 *  as published by the Free Software Foundation; either version 2 of
 
15
 *  the License, or (at your option) any later version.
 
16
 *
 
17
 *
 
18
 *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
 
19
 *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 
20
 *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 
21
 *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 
22
 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 
23
 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 
24
 *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 
25
 *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
 
26
 *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
 
27
 *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
28
 *
 
29
 *  You should have received a copy of the GNU Lesser General Public
 
30
 *  License along with this program; if not, write to the Free
 
31
 *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
32
 */
 
33
 
 
34
#ifndef __IPMI_CMDLANG_H
 
35
#define __IPMI_CMDLANG_H
 
36
 
 
37
#include <OpenIPMI/selector.h>
 
38
 
 
39
#ifdef __cplusplus
 
40
extern "C" {
 
41
#endif
 
42
 
 
43
/* Forward declaration */
 
44
typedef struct ipmi_cmd_info_s ipmi_cmd_info_t;
 
45
 
 
46
/*
 
47
 * A structure that must be passed into the command parser; it has
 
48
 * general information about the how the parser should handle thing
 
49
 * and generate its output.
 
50
 */
 
51
typedef struct ipmi_cmdlang_s ipmi_cmdlang_t;
 
52
 
 
53
/* Output is done in name:value pairs.  If you don't have a value,
 
54
   pass in NULL. */
 
55
typedef void (*cmd_out_cb)(ipmi_cmdlang_t *info,
 
56
                           const char     *name,
 
57
                           const char     *value);
 
58
typedef void (*cmd_out_b_cb)(ipmi_cmdlang_t *info,
 
59
                             const char     *name,
 
60
                             const char     *value,
 
61
                             unsigned int   len);
 
62
 
 
63
/* Command-specific info. */
 
64
typedef void (*cmd_info_cb)(ipmi_cmdlang_t *info);
 
65
 
 
66
/* The user provides one of these when they call the command language
 
67
   interpreter.  It is used to report errors and generate output. */
 
68
struct ipmi_cmdlang_s
 
69
{
 
70
    cmd_out_cb   out;      /* Generate output with this. */
 
71
    cmd_info_cb  down;     /* Go down a level (new indention or
 
72
                              nesting) */
 
73
    cmd_info_cb  up;       /* Go up a level (leave the current
 
74
                              indention or nesting) */
 
75
    cmd_info_cb  done;     /* Called when the command is done.  If
 
76
                              there was an error, the err value in
 
77
                              info will be non-null. */
 
78
    cmd_out_b_cb out_binary; /* Generate binary output with this. */
 
79
    cmd_out_b_cb out_unicode; /* Generate unicode output with this. */
 
80
 
 
81
    /* OS handler to use for the commands.  Note that this may be set
 
82
       to NULL if not required, don't depend on them except in certain
 
83
       circumstances. */
 
84
    os_handler_t *os_hnd;
 
85
 
 
86
    /* Tells if we are outputting help. */
 
87
    int         help;
 
88
 
 
89
    /*
 
90
     * Error reporting
 
91
     */
 
92
    int          err;      /* If non-zero, the errno code of the error
 
93
                              that occurred. */
 
94
 
 
95
    /* If non-NULL, an error occurred and this is the error info.  If
 
96
       the error string is dynamically allocated (and thus should be
 
97
       freed), errstr_dynalloc should be set to true. */
 
98
    char         *errstr;
 
99
    int          errstr_dynalloc;
 
100
 
 
101
    /* If an error occurs, this will be set to the object name that
 
102
       was dealing with the error.  It may be an empty string if
 
103
       no object is handling the error.  This must be pre-allocated
 
104
       and the length set properly. */
 
105
    char         *objstr;
 
106
    int          objstr_len;
 
107
 
 
108
    /* This is the location of an error. */
 
109
    char         *location;
 
110
 
 
111
 
 
112
    void         *user_data; /* User data for anything the user wants */
 
113
};
 
114
 
 
115
/* Parse and handle the given command string.  This always calls the
 
116
   done function when complete. */
 
117
void ipmi_cmdlang_handle(ipmi_cmdlang_t *cmdlang, char *str);
 
118
 
 
119
/* If the event info is true, then the system will output object
 
120
   information with each add or change event. */
 
121
void ipmi_cmdlang_set_evinfo(int evinfo);
 
122
int ipmi_cmdlang_get_evinfo(void);
 
123
 
 
124
/*
 
125
 * This is used to hold command information.
 
126
 */
 
127
typedef struct ipmi_cmdlang_cmd_s ipmi_cmdlang_cmd_t;
 
128
 
 
129
typedef void (*ipmi_cmdlang_handler_cb)(ipmi_cmd_info_t *cmd_info);
 
130
 
 
131
/* Register a command as a subcommand of the parent, or into the main
 
132
   command list if parent is NULL.  The command will have the given
 
133
   name and help text.  When the command is executed, the handler will
 
134
   be called with a cmd_info structure passed in.  The handler_data parm
 
135
   passed in below will be in the "handler_data" field of the cmd_info
 
136
   structure.  Note that if you are attaching subcommands to this
 
137
   command, you should pass in a NULL handler.  Returns an error value. */
 
138
int ipmi_cmdlang_reg_cmd(ipmi_cmdlang_cmd_t      *parent,
 
139
                         char                    *name,
 
140
                         char                    *help,
 
141
                         ipmi_cmdlang_handler_cb handler,
 
142
                         void                    *handler_data,
 
143
                         ipmi_cmdlang_cmd_t      **rv);
 
144
 
 
145
/* Register a table of commands. */
 
146
typedef struct ipmi_cmdlang_init_s
 
147
{
 
148
    char                    *name;
 
149
    ipmi_cmdlang_cmd_t      **parent;
 
150
    char                    *help;
 
151
    ipmi_cmdlang_handler_cb handler;
 
152
    void                    *cb_data;
 
153
    ipmi_cmdlang_cmd_t      **new_val;
 
154
} ipmi_cmdlang_init_t;
 
155
int ipmi_cmdlang_reg_table(ipmi_cmdlang_init_t *table, int len);
 
156
 
 
157
 
 
158
/* The following functions handle parsing various OpenIPMI objects
 
159
   according to the naming standard.  If you pass it into a command
 
160
   registration as the handler and pass your function as the
 
161
   handler_data, your function will be called with the specified
 
162
   object.  The specific function type is given in the individual
 
163
   functions.  The cmd_info will be passed in as the cb_data.
 
164
 
 
165
   For instance, if you have a command that take an entity argument,
 
166
   then you could write:
 
167
     void ent_cmd_hnd(ipmi_entity_t *entity, void *cb_data)
 
168
     {
 
169
         ipmi_cmd_info_t *cmd_info = cb_data;
 
170
     }
 
171
 
 
172
     rv = ipmi_cmdlang_reg_cmd(parent, "ent_cmd", "The ent command",
 
173
                               ipmi_cmdlang_entity_handler, ent_cmd_hnd,
 
174
                               &cmd);
 
175
*/
 
176
 
 
177
/* ipmi_domain_ptr_cb */
 
178
void ipmi_cmdlang_domain_handler(ipmi_cmd_info_t *cmd_info);
 
179
 
 
180
/* ipmi_entity_ptr_cb */
 
181
void ipmi_cmdlang_entity_handler(ipmi_cmd_info_t *cmd_info);
 
182
 
 
183
/* ipmi_sensor_ptr_cb */
 
184
void ipmi_cmdlang_sensor_handler(ipmi_cmd_info_t *cmd_info);
 
185
 
 
186
/* ipmi_control_ptr_cb */
 
187
void ipmi_cmdlang_control_handler(ipmi_cmd_info_t *cmd_info);
 
188
 
 
189
/* ipmi_mc_ptr_cb */
 
190
void ipmi_cmdlang_mc_handler(ipmi_cmd_info_t *cmd_info);
 
191
 
 
192
/* ipmi_connection_ptr_cb */
 
193
void ipmi_cmdlang_connection_handler(ipmi_cmd_info_t *cmd_info);
 
194
 
 
195
/* ipmi_pet_ptr_cb */
 
196
void ipmi_cmdlang_pet_handler(ipmi_cmd_info_t *cmd_info);
 
197
 
 
198
/* ipmi_lanparm_ptr_cb */
 
199
void ipmi_cmdlang_lanparm_handler(ipmi_cmd_info_t *cmd_info);
 
200
 
 
201
/* ipmi_fru_ptr_cb */
 
202
void ipmi_cmdlang_fru_handler(ipmi_cmd_info_t *cmd_info);
 
203
 
 
204
/* ipmi_pef_ptr_cb */
 
205
void ipmi_cmdlang_pef_handler(ipmi_cmd_info_t *cmd_info);
 
206
 
 
207
 
 
208
/* All output from the command language is in name/value pairs.  The
 
209
   value field may be NULL. */
 
210
void ipmi_cmdlang_out(ipmi_cmd_info_t *info,
 
211
                      const char      *name,
 
212
                      const char      *value);
 
213
void ipmi_cmdlang_out_int(ipmi_cmd_info_t *info,
 
214
                          const char      *name,
 
215
                          int             value);
 
216
void ipmi_cmdlang_out_double(ipmi_cmd_info_t *info,
 
217
                             const char      *name,
 
218
                             double          value);
 
219
void ipmi_cmdlang_out_hex(ipmi_cmd_info_t *info,
 
220
                          const char      *name,
 
221
                          int             value);
 
222
void ipmi_cmdlang_out_long(ipmi_cmd_info_t *info,
 
223
                           const char      *name,
 
224
                           long            value);
 
225
void ipmi_cmdlang_out_binary(ipmi_cmd_info_t *info,
 
226
                             const char      *name,
 
227
                             char            *value,
 
228
                             unsigned int    len);
 
229
void ipmi_cmdlang_out_unicode(ipmi_cmd_info_t *info,
 
230
                              const char      *name,
 
231
                              char            *value,
 
232
                              unsigned int    len);
 
233
void ipmi_cmdlang_out_type(ipmi_cmd_info_t      *info,
 
234
                           char                 *name,
 
235
                           enum ipmi_str_type_e type,
 
236
                           char                 *value,
 
237
                           unsigned int         len);
 
238
void ipmi_cmdlang_out_ip(ipmi_cmd_info_t *info,
 
239
                         const char      *name,
 
240
                         struct in_addr  *ip_addr);
 
241
void ipmi_cmdlang_out_mac(ipmi_cmd_info_t *info,
 
242
                          const char      *name,
 
243
                          unsigned char   mac_addr[6]);
 
244
void ipmi_cmdlang_out_bool(ipmi_cmd_info_t *info,
 
245
                           const char      *name,
 
246
                           int             value);
 
247
void ipmi_cmdlang_out_time(ipmi_cmd_info_t *info,
 
248
                           const char      *name,
 
249
                           ipmi_time_t     value);
 
250
void ipmi_cmdlang_out_timeout(ipmi_cmd_info_t *info,
 
251
                              const char      *name,
 
252
                              ipmi_timeout_t  value);
 
253
 
 
254
/* Generate info for an event. */
 
255
void ipmi_cmdlang_event_out(ipmi_event_t    *event,
 
256
                            ipmi_cmd_info_t *cmd_info);
 
257
 
 
258
/* The output from the command language is done at a nesting level.
 
259
   When you start outputting data for a new thing, you should "down"
 
260
   to create a new nesting level.  When you are done, you should
 
261
   "up". */
 
262
void ipmi_cmdlang_down(ipmi_cmd_info_t *info);
 
263
void ipmi_cmdlang_up(ipmi_cmd_info_t *info);
 
264
 
 
265
/* A cmd info structure is refcounted, if you save a pointer to it you
 
266
   must "get" it.  When you are done, you must "put" it.  It will be
 
267
   destroyed (and the done routine called) after the last user puts it. */
 
268
void ipmi_cmdlang_cmd_info_get(ipmi_cmd_info_t *info);
 
269
void ipmi_cmdlang_cmd_info_put(ipmi_cmd_info_t *info);
 
270
 
 
271
/* Helper functions */
 
272
void ipmi_cmdlang_get_int(char *str, int *val, ipmi_cmd_info_t *info);
 
273
void ipmi_cmdlang_get_double(char *str, double *val, ipmi_cmd_info_t *info);
 
274
void ipmi_cmdlang_get_uchar(char *str, unsigned char *val,
 
275
                            ipmi_cmd_info_t *info);
 
276
void ipmi_cmdlang_get_user(char *str, int *val, ipmi_cmd_info_t *info);
 
277
void ipmi_cmdlang_get_time(char *str, ipmi_time_t *val, ipmi_cmd_info_t *info);
 
278
void ipmi_cmdlang_get_timeout(char *str, ipmi_timeout_t *val,
 
279
                              ipmi_cmd_info_t *info);
 
280
void ipmi_cmdlang_get_bool(char *str, int *val, ipmi_cmd_info_t *info);
 
281
void ipmi_cmdlang_get_ip(char *str, struct in_addr *val,
 
282
                         ipmi_cmd_info_t *info);
 
283
void ipmi_cmdlang_get_mac(char *str, unsigned char val[6],
 
284
                          ipmi_cmd_info_t *info);
 
285
void ipmi_cmdlang_get_color(char *str, int *val, ipmi_cmd_info_t *info);
 
286
void ipmi_cmdlang_get_threshold_ev(char                        *str,
 
287
                                   enum ipmi_thresh_e          *rthresh,
 
288
                                   enum ipmi_event_value_dir_e *rvalue_dir,
 
289
                                   enum ipmi_event_dir_e       *rdir,
 
290
                                   ipmi_cmd_info_t             *info);
 
291
void ipmi_cmdlang_get_discrete_ev(char                  *str,
 
292
                                  int                   *roffset,
 
293
                                  enum ipmi_event_dir_e *rdir,
 
294
                                  ipmi_cmd_info_t       *info);
 
295
void ipmi_cmdlang_get_threshold(char               *str,
 
296
                                enum ipmi_thresh_e *rthresh,
 
297
                                ipmi_cmd_info_t    *info);
 
298
 
 
299
/* Call these to initialize and setup the command interpreter.  init
 
300
   should be called after the IPMI library proper is initialized, but
 
301
   before using it. */
 
302
int ipmi_cmdlang_init(os_handler_t *os_hnd);
 
303
void ipmi_cmdlang_cleanup(void);
 
304
 
 
305
 
 
306
/* Allocate a cmd info structure that can be used to generate
 
307
   information to an event.  Make sure to call the put function when
 
308
   all the data has been output.  Note that the refcounts work like
 
309
   normal, you get it at one, when it goes to zero the structure will
 
310
   be returned. */
 
311
ipmi_cmd_info_t *ipmi_cmdlang_alloc_event_info(void);
 
312
 
 
313
typedef struct ipmi_cmdlang_event_s ipmi_cmdlang_event_t;
 
314
 
 
315
/* Move to the first field. */
 
316
void ipmi_cmdlang_event_restart(ipmi_cmdlang_event_t *event);
 
317
 
 
318
enum ipmi_cmdlang_out_types {
 
319
    IPMI_CMDLANG_STRING,
 
320
    IPMI_CMDLANG_BINARY,
 
321
    IPMI_CMDLANG_UNICODE
 
322
};
 
323
 
 
324
/* Returns true if successful, false if no more fields left. */
 
325
int ipmi_cmdlang_event_next_field(ipmi_cmdlang_event_t        *event,
 
326
                                  unsigned int                *level,
 
327
                                  enum ipmi_cmdlang_out_types *type,
 
328
                                  char                        **name,
 
329
                                  unsigned int                *len,
 
330
                                  char                        **value);
 
331
 
 
332
/* Supplied by the user, used to report global errors (ones that don't
 
333
   deal with a specific command invocation).  The objstr is the name
 
334
   of the object dealing with the error (like the domain name, entity
 
335
   name, etc) or NULL if none.  The location is the file and procedure
 
336
   where the error occurred.  The errstr is a descriptive string and
 
337
   errval is an IPMI error value to be printed. */
 
338
void ipmi_cmdlang_global_err(char *objstr,
 
339
                             char *location,
 
340
                             char *errstr,
 
341
                             int  errval);
 
342
 
 
343
/* Supplied by the user to report events. */
 
344
void ipmi_cmdlang_report_event(ipmi_cmdlang_event_t *event);
 
345
 
 
346
/* In callbacks, you must use these to lock the cmd_info structure. */
 
347
void ipmi_cmdlang_lock(ipmi_cmd_info_t *info);
 
348
void ipmi_cmdlang_unlock(ipmi_cmd_info_t *info);
 
349
 
 
350
int ipmi_cmdlang_get_argc(ipmi_cmd_info_t *info);
 
351
char **ipmi_cmdlang_get_argv(ipmi_cmd_info_t *info);
 
352
int ipmi_cmdlang_get_curr_arg(ipmi_cmd_info_t *info);
 
353
ipmi_cmdlang_t *ipmi_cmdinfo_get_cmdlang(ipmi_cmd_info_t *info);
 
354
 
 
355
#ifdef __cplusplus
 
356
}
 
357
#endif
 
358
 
 
359
#endif /* __IPMI_CMDLANG_H */