~ubuntu-branches/ubuntu/intrepid/openipmi/intrepid

« back to all changes in this revision

Viewing changes to lanserv/emu_cmd.c

  • 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
#include <string.h>
 
3
#include <stdio.h>
 
4
#include <errno.h>
 
5
#include <stdlib.h>
 
6
#include "emu.h"
 
7
 
 
8
typedef int (*ipmi_emu_cmd_handler)(emu_data_t *emu,
 
9
                                    lmc_data_t *mc,
 
10
                                    char       **toks);
 
11
 
 
12
static int
 
13
get_uchar(char **toks, unsigned char *val, char *errstr, int empty_ok)
 
14
{
 
15
    char *str, *tmpstr;
 
16
 
 
17
    str = strtok_r(NULL, " \t\n", toks);
 
18
    if (!str) {
 
19
        if (empty_ok)
 
20
            return ENOSPC;
 
21
        if (errstr)
 
22
            printf("**No %s given\n", errstr);
 
23
        return EINVAL;
 
24
    }
 
25
    if (str[0] == '\'') {
 
26
        *val = str[1];
 
27
        return 0;
 
28
    }
 
29
    *val = strtoul(str, &tmpstr, 16);
 
30
    if (*tmpstr != '\0') {
 
31
        if (errstr)
 
32
            printf("**Invalid %s given\n", errstr);
 
33
        return EINVAL;
 
34
    }
 
35
 
 
36
    return 0;
 
37
}
 
38
 
 
39
static int
 
40
get_bitmask(char **toks, unsigned char *val, char *errstr, int size,
 
41
            int empty_ok)
 
42
{
 
43
    char *str;
 
44
    int  i, j;
 
45
 
 
46
    str = strtok_r(NULL, " \t\n", toks);
 
47
    if (!str) {
 
48
        if (empty_ok)
 
49
            return ENOSPC;
 
50
        if (errstr)
 
51
            printf("**No %s given\n", errstr);
 
52
        return EINVAL;
 
53
    }
 
54
    if (strlen(str) != size) {
 
55
        if (errstr)
 
56
            printf("**invalid number of bits in %s\n", errstr);
 
57
        return EINVAL;
 
58
    }
 
59
    for (i=size-1, j=0; i>=0; i--, j++) {
 
60
        if (str[j] == '0') {
 
61
            val[i] = 0;
 
62
        } else if (str[j] == '1') {
 
63
            val[i] = 1;
 
64
        } else {
 
65
            if (errstr)
 
66
                printf("**Invalid bit value '%c' in %s\n", str[j], errstr);
 
67
            return EINVAL;
 
68
        }
 
69
    }
 
70
 
 
71
    return 0;
 
72
}
 
73
 
 
74
static int
 
75
get_uint(char **toks, unsigned int *val, char *errstr)
 
76
{
 
77
    char *str, *tmpstr;
 
78
 
 
79
    str = strtok_r(NULL, " \t\n", toks);
 
80
    if (!str) {
 
81
        if (errstr)
 
82
            printf("**No %s given\n", errstr);
 
83
        return EINVAL;
 
84
    }
 
85
    *val = strtoul(str, &tmpstr, 16);
 
86
    if (*tmpstr != '\0') {
 
87
        if (errstr)
 
88
            printf("**Invalid %s given\n", errstr);
 
89
        return EINVAL;
 
90
    }
 
91
 
 
92
    return 0;
 
93
}
 
94
 
 
95
#define INPUT_BUFFER_SIZE 65536
 
96
int
 
97
read_command_file(emu_data_t *emu, char *command_file)
 
98
{
 
99
    FILE *f = fopen(command_file, "r");
 
100
    int  rv;
 
101
 
 
102
    if (!f) {
 
103
        fprintf(stderr, "Unable to open command file '%s'\n",
 
104
                command_file);
 
105
    } else {
 
106
        char *buffer;
 
107
        int  pos = 0;
 
108
 
 
109
        buffer = malloc(INPUT_BUFFER_SIZE);
 
110
        if (!buffer) {
 
111
            fprintf(stderr, "Could not allocate buffer memory\n");
 
112
            goto out;
 
113
        }
 
114
        while (fgets(buffer+pos, INPUT_BUFFER_SIZE-pos, f)) {
 
115
            printf("%s", buffer+pos);
 
116
            if (buffer[pos] == '#')
 
117
                continue;
 
118
            pos = strlen(buffer);
 
119
            if (pos == 0)
 
120
                continue;
 
121
            pos--;
 
122
            while ((pos > 0) && (buffer[pos] == '\n'))
 
123
                pos--;
 
124
            if (pos == 0)
 
125
                continue;
 
126
            if ((pos > 0) && (buffer[pos] == '\\')) {
 
127
                /* Continue the line. */
 
128
                pos--;
 
129
                continue;
 
130
            }
 
131
            
 
132
            rv = ipmi_emu_cmd(emu, buffer);
 
133
            if (rv)
 
134
                return rv;
 
135
            pos = 0;
 
136
        }
 
137
 out:
 
138
        if (buffer)
 
139
            free(buffer);
 
140
        fclose(f);
 
141
    }
 
142
 
 
143
    return 0;
 
144
}
 
145
 
 
146
static int
 
147
sel_enable(emu_data_t *emu, lmc_data_t *mc, char **toks)
 
148
{
 
149
    int           rv;
 
150
    unsigned int  max_records;
 
151
    unsigned char flags;
 
152
 
 
153
    rv = get_uint(toks, &max_records, "max records");
 
154
    if (rv)
 
155
        return rv;
 
156
 
 
157
    rv = get_uchar(toks, &flags, "flags", 0);
 
158
    if (rv)
 
159
        return rv;
 
160
 
 
161
    rv = ipmi_mc_enable_sel(mc, max_records, flags);
 
162
    if (rv)
 
163
        printf("**Unable to enable sel, error 0x%x\n", rv);
 
164
    return rv;
 
165
}
 
166
 
 
167
static int
 
168
sel_add(emu_data_t *emu, lmc_data_t *mc, char **toks)
 
169
{
 
170
    int           i;
 
171
    int           rv;
 
172
    unsigned char record_type;
 
173
    unsigned char data[13];
 
174
    unsigned int  r;
 
175
 
 
176
    rv = get_uchar(toks, &record_type, "record type", 0);
 
177
    if (rv)
 
178
        return rv;
 
179
 
 
180
    for (i=0; i<13; i++) {
 
181
        rv = get_uchar(toks, &data[i], "data byte", 0);
 
182
        if (rv)
 
183
            return rv;
 
184
    }
 
185
 
 
186
    rv = ipmi_mc_add_to_sel(mc, record_type, data, &r);
 
187
    if (rv)
 
188
        printf("**Unable to add to sel, error 0x%x\n", rv);
 
189
    else
 
190
        printf("Added record %d\n", r);
 
191
    return rv;
 
192
}
 
193
 
 
194
static int
 
195
main_sdr_add(emu_data_t *emu, lmc_data_t *mc, char **toks)
 
196
{
 
197
    int           i;
 
198
    int           rv;
 
199
    unsigned char data[256];
 
200
 
 
201
    for (i=0; i<256; i++) {
 
202
        rv = get_uchar(toks, &data[i], "data byte", 1);
 
203
        if (rv == ENOSPC)
 
204
            break;
 
205
        if (rv) {
 
206
            printf("**Error 0x%x in data byte %d\n", rv, i);
 
207
            return rv;
 
208
        }
 
209
    }
 
210
 
 
211
    rv = ipmi_mc_add_main_sdr(mc, data, i);
 
212
    if (rv)
 
213
        printf("**Unable to add to sdr, error 0x%x\n", rv);
 
214
    return rv;
 
215
}
 
216
 
 
217
static int
 
218
device_sdr_add(emu_data_t *emu, lmc_data_t *mc, char **toks)
 
219
{
 
220
    int           i;
 
221
    int           rv;
 
222
    unsigned char data[256];
 
223
    unsigned char lun;
 
224
 
 
225
    rv = get_uchar(toks, &lun, "LUN", 0);
 
226
    if (rv)
 
227
        return rv;
 
228
 
 
229
    for (i=0; i<256; i++) {
 
230
        rv = get_uchar(toks, &data[i], "data byte", 1);
 
231
        if (rv == ENOSPC)
 
232
            break;
 
233
        if (rv) {
 
234
            printf("**Error 0x%x in data byte %d\n", rv, i);
 
235
            return rv;
 
236
        }
 
237
    }
 
238
 
 
239
    rv = ipmi_mc_add_device_sdr(mc, lun, data, i);
 
240
    if (rv)
 
241
        printf("**Unable to add to sdr, error 0x%x\n", rv);
 
242
    return rv;
 
243
}
 
244
 
 
245
static int
 
246
sensor_add(emu_data_t *emu, lmc_data_t *mc, char **toks)
 
247
{
 
248
    int           rv;
 
249
    unsigned char lun;
 
250
    unsigned char num;
 
251
    unsigned char type;
 
252
    unsigned char code;
 
253
 
 
254
    rv = get_uchar(toks, &lun, "LUN", 0);
 
255
    if (rv)
 
256
        return rv;
 
257
 
 
258
    rv = get_uchar(toks, &num, "sensor num", 0);
 
259
    if (rv)
 
260
        return rv;
 
261
 
 
262
    rv = get_uchar(toks, &type, "sensor type", 0);
 
263
    if (rv)
 
264
        return rv;
 
265
 
 
266
    rv = get_uchar(toks, &code, "event reading code", 0);
 
267
    if (rv)
 
268
        return rv;
 
269
 
 
270
    rv = ipmi_mc_add_sensor(mc, lun, num, type, code);
 
271
    if (rv)
 
272
        printf("**Unable to add to sensor, error 0x%x\n", rv);
 
273
    return rv;
 
274
}
 
275
 
 
276
static int
 
277
sensor_set_bit(emu_data_t *emu, lmc_data_t *mc, char **toks)
 
278
{
 
279
    int           rv;
 
280
    unsigned char lun;
 
281
    unsigned char num;
 
282
    unsigned char bit;
 
283
    unsigned char value;
 
284
    unsigned char gen_event;
 
285
 
 
286
    rv = get_uchar(toks, &lun, "LUN", 0);
 
287
    if (rv)
 
288
        return rv;
 
289
 
 
290
    rv = get_uchar(toks, &num, "sensor num", 0);
 
291
    if (rv)
 
292
        return rv;
 
293
 
 
294
    rv = get_uchar(toks, &bit, "bit to set", 0);
 
295
    if (rv)
 
296
        return rv;
 
297
 
 
298
    rv = get_uchar(toks, &value, "bit value", 0);
 
299
    if (rv)
 
300
        return rv;
 
301
 
 
302
    rv = get_uchar(toks, &gen_event, "generate event", 0);
 
303
    if (rv)
 
304
        return rv;
 
305
 
 
306
    rv = ipmi_mc_sensor_set_bit(mc, lun, num, bit, value, gen_event);
 
307
    if (rv)
 
308
        printf("**Unable to set sensor bit, error 0x%x\n", rv);
 
309
    return rv;
 
310
}
 
311
 
 
312
static int
 
313
sensor_set_bit_clr_rest(emu_data_t *emu, lmc_data_t *mc, char **toks)
 
314
{
 
315
    int           rv;
 
316
    unsigned char lun;
 
317
    unsigned char num;
 
318
    unsigned char bit;
 
319
    unsigned char gen_event;
 
320
 
 
321
    rv = get_uchar(toks, &lun, "LUN", 0);
 
322
    if (rv)
 
323
        return rv;
 
324
 
 
325
    rv = get_uchar(toks, &num, "sensor num", 0);
 
326
    if (rv)
 
327
        return rv;
 
328
 
 
329
    rv = get_uchar(toks, &bit, "bit to set", 0);
 
330
    if (rv)
 
331
        return rv;
 
332
 
 
333
    rv = get_uchar(toks, &gen_event, "generate event", 0);
 
334
    if (rv)
 
335
        return rv;
 
336
 
 
337
    rv = ipmi_mc_sensor_set_bit_clr_rest(mc, lun, num, bit, gen_event);
 
338
    if (rv)
 
339
        printf("**Unable to set sensor bit, error 0x%x\n", rv);
 
340
    return rv;
 
341
}
 
342
 
 
343
static int
 
344
sensor_set_value(emu_data_t *emu, lmc_data_t *mc, char **toks)
 
345
{
 
346
    int           rv;
 
347
    unsigned char lun;
 
348
    unsigned char num;
 
349
    unsigned char value;
 
350
    unsigned char gen_event;
 
351
 
 
352
    rv = get_uchar(toks, &lun, "LUN", 0);
 
353
    if (rv)
 
354
        return rv;
 
355
 
 
356
    rv = get_uchar(toks, &num, "sensor num", 0);
 
357
    if (rv)
 
358
        return rv;
 
359
 
 
360
    rv = get_uchar(toks, &value, "value", 0);
 
361
    if (rv)
 
362
        return rv;
 
363
 
 
364
    rv = get_uchar(toks, &gen_event, "generate event", 0);
 
365
    if (rv)
 
366
        return rv;
 
367
 
 
368
    rv = ipmi_mc_sensor_set_value(mc, lun, num, value, gen_event);
 
369
    if (rv)
 
370
        printf("**Unable to set sensor value, error 0x%x\n", rv);
 
371
    return rv;
 
372
}
 
373
 
 
374
static int
 
375
sensor_set_hysteresis(emu_data_t *emu, lmc_data_t *mc, char **toks)
 
376
{
 
377
    int           rv;
 
378
    unsigned char lun;
 
379
    unsigned char num;
 
380
    unsigned char support;
 
381
    unsigned char positive, negative;
 
382
 
 
383
    rv = get_uchar(toks, &lun, "LUN", 0);
 
384
    if (rv)
 
385
        return rv;
 
386
 
 
387
    rv = get_uchar(toks, &num, "sensor num", 0);
 
388
    if (rv)
 
389
        return rv;
 
390
 
 
391
    rv = get_uchar(toks, &support, "hysteresis support", 0);
 
392
    if (rv)
 
393
        return rv;
 
394
 
 
395
    rv = get_uchar(toks, &positive, "positive hysteresis", 0);
 
396
    if (rv)
 
397
        return rv;
 
398
 
 
399
    rv = get_uchar(toks, &negative, "negative hysteresis", 0);
 
400
    if (rv)
 
401
        return rv;
 
402
 
 
403
    rv = ipmi_mc_sensor_set_hysteresis(mc, lun, num, support, positive,
 
404
                                       negative);
 
405
    if (rv)
 
406
        printf("**Unable to set sensor hysteresis, error 0x%x\n", rv);
 
407
    return rv;
 
408
}
 
409
 
 
410
static int
 
411
sensor_set_threshold(emu_data_t *emu, lmc_data_t *mc, char **toks)
 
412
{
 
413
    int           rv;
 
414
    unsigned char lun;
 
415
    unsigned char num;
 
416
    unsigned char support;
 
417
    unsigned char enabled[6];
 
418
    unsigned char thresholds[6];
 
419
    int           i;
 
420
 
 
421
    rv = get_uchar(toks, &lun, "LUN", 0);
 
422
    if (rv)
 
423
        return rv;
 
424
 
 
425
    rv = get_uchar(toks, &num, "sensor num", 0);
 
426
    if (rv)
 
427
        return rv;
 
428
 
 
429
    rv = get_uchar(toks, &support, "threshold support", 0);
 
430
    if (rv)
 
431
        return rv;
 
432
 
 
433
    rv = get_bitmask(toks, enabled, "threshold enabled", 6, 0);
 
434
    if (rv)
 
435
        return rv;
 
436
 
 
437
    for (i=5; i>=0; i--) {
 
438
        rv = get_uchar(toks, &thresholds[i], "threshold value", 0);
 
439
        if (rv)
 
440
            return rv;
 
441
    }
 
442
 
 
443
    rv = ipmi_mc_sensor_set_threshold(mc, lun, num, support,
 
444
                                      enabled, thresholds);
 
445
    if (rv)
 
446
        printf("**Unable to set sensor thresholds, error 0x%x\n", rv);
 
447
    return rv;
 
448
}
 
449
 
 
450
static int
 
451
sensor_set_event_support(emu_data_t *emu, lmc_data_t *mc, char **toks)
 
452
{
 
453
    int           rv;
 
454
    unsigned char lun;
 
455
    unsigned char num;
 
456
    unsigned char support;
 
457
    unsigned char events_enable;
 
458
    unsigned char scanning;
 
459
    unsigned char assert_support[15];
 
460
    unsigned char deassert_support[15];
 
461
    unsigned char assert_enabled[15];
 
462
    unsigned char deassert_enabled[15];
 
463
 
 
464
    rv = get_uchar(toks, &lun, "LUN", 0);
 
465
    if (rv)
 
466
        return rv;
 
467
 
 
468
    rv = get_uchar(toks, &num, "sensor num", 0);
 
469
    if (rv)
 
470
        return rv;
 
471
 
 
472
    rv = get_uchar(toks, &events_enable, "events enable", 0);
 
473
    if (rv)
 
474
        return rv;
 
475
 
 
476
    rv = get_uchar(toks, &scanning, "scanning", 0);
 
477
    if (rv)
 
478
        return rv;
 
479
 
 
480
    rv = get_uchar(toks, &support, "event support", 0);
 
481
    if (rv)
 
482
        return rv;
 
483
 
 
484
    rv = get_bitmask(toks, assert_support, "assert support", 15, 0);
 
485
    if (rv)
 
486
        return rv;
 
487
 
 
488
    rv = get_bitmask(toks, deassert_support, "deassert support", 15, 0);
 
489
    if (rv)
 
490
        return rv;
 
491
 
 
492
    rv = get_bitmask(toks, assert_enabled, "assert enabled", 15, 0);
 
493
    if (rv)
 
494
        return rv;
 
495
 
 
496
    rv = get_bitmask(toks, deassert_enabled, "deassert enabled", 15, 0);
 
497
    if (rv)
 
498
        return rv;
 
499
 
 
500
    rv = ipmi_mc_sensor_set_event_support(mc, lun, num,
 
501
                                          events_enable, scanning,
 
502
                                          support,
 
503
                                          assert_support, deassert_support,
 
504
                                          assert_enabled, deassert_enabled);
 
505
    if (rv)
 
506
        printf("**Unable to set sensor thresholds, error 0x%x\n", rv);
 
507
    return rv;
 
508
}
 
509
 
 
510
static int
 
511
mc_add(emu_data_t *emu, lmc_data_t *mc, char **toks)
 
512
{
 
513
    unsigned char ipmb;
 
514
    unsigned char device_id;
 
515
    unsigned char has_device_sdrs;
 
516
    unsigned char device_revision;
 
517
    unsigned char major_fw_rev;
 
518
    unsigned char minor_fw_rev;
 
519
    unsigned char device_support;
 
520
    unsigned char mfg_id[3];
 
521
    unsigned int  mfg_id_i;
 
522
    unsigned char product_id[2];
 
523
    unsigned int  product_id_i;
 
524
    unsigned char dyn_sens = 0;
 
525
    int           rv;
 
526
    
 
527
    rv = get_uchar(toks, &ipmb, "IPMB address", 0);
 
528
    if (rv)
 
529
        return rv;
 
530
    rv = get_uchar(toks, &device_id, "Device ID", 0);
 
531
    if (rv)
 
532
        return rv;
 
533
    rv = get_uchar(toks, &has_device_sdrs, "Has Device SDRs", 0);
 
534
    if (rv)
 
535
        return rv;
 
536
    rv = get_uchar(toks, &device_revision, "Device Revision", 0);
 
537
    if (rv)
 
538
        return rv;
 
539
    rv = get_uchar(toks, &major_fw_rev, "Major FW Rev", 0);
 
540
    if (rv)
 
541
        return rv;
 
542
    rv = get_uchar(toks, &minor_fw_rev, "Minor FW Rev", 0);
 
543
    if (rv)
 
544
        return rv;
 
545
    rv = get_uchar(toks, &device_support, "Device Support", 0);
 
546
    if (rv)
 
547
        return rv;
 
548
    rv = get_uint(toks, &mfg_id_i, "Manufacturer ID");
 
549
    if (rv)
 
550
        return rv;
 
551
    rv = get_uint(toks, &product_id_i, "Product ID");
 
552
    if (rv)
 
553
        return rv;
 
554
    rv = get_uchar(toks, &dyn_sens, "Dynamic Sensor Population", 1);
 
555
    if (rv)
 
556
        goto next;
 
557
 next:
 
558
    mfg_id[0] = mfg_id_i & 0xff;
 
559
    mfg_id[1] = (mfg_id_i >> 8) & 0xff;
 
560
    mfg_id[2] = (mfg_id_i >> 16) & 0xff;
 
561
    product_id[0] = product_id_i & 0xff;
 
562
    product_id[1] = (product_id_i >> 8) & 0xff;
 
563
    rv = ipmi_emu_add_mc(emu, ipmb, device_id, has_device_sdrs,
 
564
                         device_revision, major_fw_rev, minor_fw_rev,
 
565
                         device_support, mfg_id, product_id, dyn_sens);
 
566
    if (rv)
 
567
        printf("**Unable to add the MC, error 0x%x\n", rv);
 
568
    return rv;
 
569
}
 
570
 
 
571
static int
 
572
mc_setchan(emu_data_t *emu, lmc_data_t *mc, char **toks)
 
573
{
 
574
    unsigned char channel;
 
575
    unsigned char medium_type;
 
576
    unsigned char protocol_type;
 
577
    unsigned char session_support;
 
578
    int           rv;
 
579
 
 
580
    rv = get_uchar(toks, &channel, "Channel Number", 0);
 
581
    if (rv)
 
582
        return rv;
 
583
    rv = get_uchar(toks, &medium_type, "Medium Type", 0);
 
584
    if (rv)
 
585
        return rv;
 
586
    rv = get_uchar(toks, &protocol_type, "Protocol Type", 0);
 
587
    if (rv)
 
588
        return rv;
 
589
    rv = get_uchar(toks, &session_support, "Session Support", 0);
 
590
    if (rv)
 
591
        return rv;
 
592
    rv = ipmi_emu_set_mc_channel(mc, channel, medium_type, protocol_type,
 
593
                                 session_support);
 
594
    if (rv)
 
595
        printf("**Unable to set up channel, error 0x%x\n", rv);
 
596
    return rv;
 
597
}
 
598
 
 
599
static int
 
600
mc_delete(emu_data_t *emu, lmc_data_t *mc, char **toks)
 
601
{
 
602
    ipmi_mc_destroy(mc);
 
603
    return 0;
 
604
}
 
605
 
 
606
static int
 
607
mc_disable(emu_data_t *emu, lmc_data_t *mc, char **toks)
 
608
{
 
609
    ipmi_mc_disable(mc);
 
610
    return 0;
 
611
}
 
612
 
 
613
static int
 
614
mc_enable(emu_data_t *emu, lmc_data_t *mc, char **toks)
 
615
{
 
616
    ipmi_mc_enable(mc);
 
617
    return 0;
 
618
}
 
619
 
 
620
static int
 
621
mc_set_power(emu_data_t *emu, lmc_data_t *mc, char **toks)
 
622
{
 
623
    unsigned char power;
 
624
    unsigned char gen_int;
 
625
    int           rv;
 
626
 
 
627
    rv = get_uchar(toks, &power, "Power", 0);
 
628
    if (rv)
 
629
        return rv;
 
630
 
 
631
    rv = get_uchar(toks, &gen_int, "Gen int", 0);
 
632
    if (rv)
 
633
        return rv;
 
634
 
 
635
    rv = ipmi_mc_set_power(mc, power, gen_int);
 
636
    if (rv)
 
637
        printf("**Unable to set power, error 0x%x\n", rv);
 
638
    return rv;
 
639
}
 
640
 
 
641
#define MAX_FRU_SIZE 8192
 
642
static int
 
643
mc_add_fru_data(emu_data_t *emu, lmc_data_t *mc, char **toks)
 
644
{
 
645
    unsigned char data[MAX_FRU_SIZE];
 
646
    unsigned char devid;
 
647
    unsigned int  length;
 
648
    int           i;
 
649
    int           rv;
 
650
 
 
651
    rv = get_uchar(toks, &devid, "Device ID", 0);
 
652
    if (rv)
 
653
        return rv;
 
654
 
 
655
    rv = get_uint(toks, &length, "FRU physical size");
 
656
    if (rv)
 
657
        return rv;
 
658
 
 
659
    for (i=0; i<MAX_FRU_SIZE; i++) {
 
660
        rv = get_uchar(toks, &data[i], "data byte", 1);
 
661
        if (rv == ENOSPC)
 
662
            break;
 
663
        if (rv) {
 
664
            printf("**Error 0x%x in data byte %d\n", rv, i);
 
665
            return rv;
 
666
        }
 
667
    }
 
668
 
 
669
    rv = ipmi_mc_add_fru_data(mc, devid, length, data, i);
 
670
    if (rv)
 
671
        printf("**Unable to add FRU data, error 0x%x\n", rv);
 
672
    return rv;
 
673
}
 
674
 
 
675
static int
 
676
mc_dump_fru_data(emu_data_t *emu, lmc_data_t *mc, char **toks)
 
677
{
 
678
    unsigned char *data;
 
679
    unsigned char devid;
 
680
    unsigned int  length;
 
681
    int           i;
 
682
    int           rv;
 
683
 
 
684
    rv = get_uchar(toks, &devid, "Device ID", 0);
 
685
    if (rv)
 
686
        return rv;
 
687
 
 
688
    rv = ipmi_mc_get_fru_data(mc, devid, &length, &data);
 
689
    if (rv) {
 
690
        printf("**Unable to dump FRU data, error 0x%x\n", rv);
 
691
        goto out;
 
692
    }
 
693
 
 
694
    for (i=0; i<length; i++) {
 
695
        if ((i > 0) && ((i % 8) == 0))
 
696
            printf("\n");
 
697
        printf(" 0x%2.2x", data[i]);
 
698
    }
 
699
    printf("\n");
 
700
 
 
701
 out:
 
702
    return rv;
 
703
}
 
704
 
 
705
static int
 
706
mc_setbmc(emu_data_t *emu, lmc_data_t *mc, char **toks)
 
707
{
 
708
    unsigned char ipmb;
 
709
    int           rv;
 
710
 
 
711
    rv = get_uchar(toks, &ipmb, "IPMB address of BMC", 0);
 
712
    if (rv)
 
713
        return rv;
 
714
    rv = ipmi_emu_set_bmc_mc(emu, ipmb);
 
715
    if (rv)
 
716
        printf("**Invalid IPMB address\n");
 
717
    return rv;
 
718
}
 
719
 
 
720
static int
 
721
atca_enable(emu_data_t *emu, lmc_data_t *mc, char **toks)
 
722
{
 
723
    int rv;
 
724
 
 
725
    rv = ipmi_emu_atca_enable(emu);
 
726
    if (rv)
 
727
        printf("**Unable to enable ATCA mode, error 0x%x\n", rv);
 
728
    return rv;
 
729
}
 
730
 
 
731
static int
 
732
atca_set_site(emu_data_t *emu, lmc_data_t *mc, char **toks)
 
733
{
 
734
    int           rv;
 
735
    unsigned char hw_address;
 
736
    unsigned char site_type;
 
737
    unsigned char site_number;
 
738
    
 
739
    rv = get_uchar(toks, &hw_address, "hardware address", 0);
 
740
    if (rv)
 
741
        return rv;
 
742
    rv = get_uchar(toks, &site_type, "site type", 0);
 
743
    if (rv)
 
744
        return rv;
 
745
    rv = get_uchar(toks, &site_number, "site number", 0);
 
746
    if (rv)
 
747
        return rv;
 
748
 
 
749
    rv = ipmi_emu_atca_set_site(emu, hw_address, site_type, site_number);
 
750
    if (rv)
 
751
        printf("**Unable to set site type, error 0x%x\n", rv);
 
752
    return rv;
 
753
}
 
754
 
 
755
static int
 
756
mc_set_num_leds(emu_data_t *emu, lmc_data_t *mc, char **toks)
 
757
{
 
758
    int           rv;
 
759
    unsigned char count;
 
760
 
 
761
    rv = get_uchar(toks, &count, "number of LEDs", 0);
 
762
    if (rv)
 
763
        return rv;
 
764
 
 
765
    rv = ipmi_mc_set_num_leds(mc, count);
 
766
    if (rv)
 
767
        printf("**Unable to set number of LEDs, error 0x%x\n", rv);
 
768
    return rv;
 
769
}
 
770
 
 
771
static int
 
772
read_cmds(emu_data_t *emu, lmc_data_t *mc, char **toks)
 
773
{
 
774
    char *filename = strtok_r(NULL, " \t\n", toks);
 
775
 
 
776
    if (!filename) {
 
777
        printf("**No filename specified\n");
 
778
        return EINVAL;
 
779
    }
 
780
 
 
781
    return read_command_file(emu, filename);
 
782
}
 
783
 
 
784
static int
 
785
sleep_cmd(emu_data_t *emu, lmc_data_t *mc, char **toks)
 
786
{
 
787
    unsigned int   time;
 
788
    struct timeval tv;
 
789
    int            rv;
 
790
 
 
791
    rv = get_uint(toks, &time, "timeout");
 
792
    if (rv)
 
793
        return rv;
 
794
 
 
795
    tv.tv_sec = time / 1000;
 
796
    tv.tv_usec = (time % 1000) * 1000;
 
797
    ipmi_emu_sleep(emu, &tv);
 
798
    return 0;
 
799
}
 
800
 
 
801
static int
 
802
quit(emu_data_t *emu, lmc_data_t *mc, char **toks)
 
803
{
 
804
    ipmi_emu_shutdown();
 
805
    return 0;
 
806
}
 
807
 
 
808
#define MC      1
 
809
#define NOMC    0
 
810
static struct {
 
811
    char                 *name;
 
812
    int                  flags;
 
813
    ipmi_emu_cmd_handler handler;
 
814
} cmds[] =
 
815
{
 
816
    { "quit",           NOMC,           quit },
 
817
    { "sel_enable",     MC,             sel_enable },
 
818
    { "sel_add",        MC,             sel_add },
 
819
    { "main_sdr_add",   MC,             main_sdr_add },
 
820
    { "device_sdr_add", MC,             device_sdr_add },
 
821
    { "sensor_add",     MC,             sensor_add },
 
822
    { "sensor_set_bit", MC,             sensor_set_bit },
 
823
    { "sensor_set_bit_clr_rest", MC,        sensor_set_bit_clr_rest },
 
824
    { "sensor_set_value", MC,           sensor_set_value },
 
825
    { "sensor_set_hysteresis", MC,      sensor_set_hysteresis },
 
826
    { "sensor_set_threshold", MC,       sensor_set_threshold },
 
827
    { "sensor_set_event_support", MC,   sensor_set_event_support },
 
828
    { "mc_set_power",   MC,             mc_set_power },
 
829
    { "mc_add_fru_data",MC,             mc_add_fru_data },
 
830
    { "mc_dump_fru_data",MC,            mc_dump_fru_data },
 
831
    { "mc_set_num_leds",MC,             mc_set_num_leds },
 
832
    { "mc_add",         NOMC,           mc_add },
 
833
    { "mc_delete",      MC,             mc_delete },
 
834
    { "mc_disable",     MC,             mc_disable },
 
835
    { "mc_enable",      MC,             mc_enable },
 
836
    { "mc_setbmc",      NOMC,           mc_setbmc },
 
837
    { "mc_setchan",     MC,             mc_setchan },
 
838
    { "atca_enable",    NOMC,           atca_enable },
 
839
    { "atca_set_site",  NOMC,           atca_set_site },
 
840
    { "read_cmds",      NOMC,           read_cmds },
 
841
    { "sleep",          NOMC,           sleep_cmd },
 
842
    { NULL }
 
843
};
 
844
 
 
845
int
 
846
ipmi_emu_cmd(emu_data_t *emu, char *cmd_str)
 
847
{
 
848
    char       *toks;
 
849
    char       *cmd;
 
850
    int        i;
 
851
    int        rv = EINVAL;
 
852
    lmc_data_t *mc = NULL;
 
853
 
 
854
    cmd = strtok_r(cmd_str, " \t\n", &toks);
 
855
    if (!cmd)
 
856
        return 0;
 
857
    if (cmd[0] == '#')
 
858
        return 0;
 
859
 
 
860
    for (i=0; cmds[i].name; i++) {
 
861
        if (strcmp(cmd, cmds[i].name) == 0) {
 
862
            if (cmds[i].flags & MC) {
 
863
                unsigned char ipmb;
 
864
                rv = get_uchar(&toks, &ipmb, "MC address", 0);
 
865
                if (rv)
 
866
                    return rv;
 
867
                rv = ipmi_emu_get_mc_by_addr(emu, ipmb, &mc);
 
868
                if (rv) {
 
869
                    printf("**Invalid MC address\n");
 
870
                    return rv;
 
871
                }
 
872
            }
 
873
            rv = cmds[i].handler(emu, mc, &toks);
 
874
            if (rv)
 
875
                return rv;
 
876
            goto out;
 
877
        }
 
878
    }
 
879
 
 
880
    printf("**Unknown command: %s\n", cmd);
 
881
 
 
882
 out:
 
883
    return rv;
 
884
}