From: <uli...@us...> - 2011-02-02 09:41:26
|
Revision: 7265 http://openhpi.svn.sourceforge.net/openhpi/?rev=7265&view=rev Author: ulikleber Date: 2011-02-02 09:41:18 +0000 (Wed, 02 Feb 2011) Log Message: ----------- more on 1493787 Update clients to use long options and uniform format clients hpiel, hpievents, hpifan, hpigensimdata, hpireset, hpipower, hpionIBMblade, hpiinv and some small fixes. Modified Paths: -------------- openhpi/trunk/clients/hpialarms.c openhpi/trunk/clients/hpiel.c openhpi/trunk/clients/hpievents.c openhpi/trunk/clients/hpifan.c openhpi/trunk/clients/hpigensimdata.c openhpi/trunk/clients/hpiinv.c openhpi/trunk/clients/hpionIBMblade.c openhpi/trunk/clients/hpipower.c openhpi/trunk/clients/hpireset.c openhpi/trunk/clients/hpitree.c openhpi/trunk/clients/oh_clients.h openhpi/trunk/docs/man/hpialarms.pod.1 openhpi/trunk/docs/man/hpidomain.pod.1 openhpi/trunk/docs/man/hpiel.pod.1 openhpi/trunk/docs/man/hpievents.pod.1 openhpi/trunk/docs/man/hpifan.pod.1 openhpi/trunk/docs/man/hpigensimdata.pod.1 openhpi/trunk/docs/man/hpiinv.pod.1 openhpi/trunk/docs/man/hpionIBMblade.pod.1 openhpi/trunk/docs/man/hpipower.pod.1 openhpi/trunk/docs/man/hpireset.pod.1 openhpi/trunk/docs/man/hpitop.pod.1 openhpi/trunk/docs/man/hpitree.pod.1 Modified: openhpi/trunk/clients/hpialarms.c =================================================================== --- openhpi/trunk/clients/hpialarms.c 2011-02-01 16:12:21 UTC (rev 7264) +++ openhpi/trunk/clients/hpialarms.c 2011-02-02 09:41:18 UTC (rev 7265) @@ -14,7 +14,7 @@ * 10/13/04 Andy Cress - v1.2 add ifdefs for HPI_A & HPI_B, added -d/raw * < ...for more changes look at svn logs... > * 09/06/2010 ulikleber New option -D to select domain - * 20/01/2011 ulikleber Refactoring to use glib for option parsing and + * 01/02/2011 ulikleber Refactoring to use glib for option parsing and * introduce common options for all clients * * Author(s): Modified: openhpi/trunk/clients/hpiel.c =================================================================== --- openhpi/trunk/clients/hpiel.c 2011-02-01 16:12:21 UTC (rev 7264) +++ openhpi/trunk/clients/hpiel.c 2011-02-02 09:41:18 UTC (rev 7265) @@ -2,6 +2,7 @@ * * (C) Copyright IBM Corp 2006 * (C) Copyright Nokia Siemens Networks 2010 + * (C) Copyright Ulrich Kleber 2011 * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of @@ -19,24 +20,17 @@ * * Changes: * 09/06/2010 ulikleber New option -D to select domain + * 01/02/2011 ulikleber Refactoring to use glib for option parsing and + * introduce common options for all clients */ -#include <stdlib.h> -#include <string.h> -#include <getopt.h> - - -#include <SaHpi.h> -#include <oHpi.h> -#include <oh_utils.h> - #include "oh_clients.h" #define OH_SVN_REV "$Revision$" #define err(format, ...) \ do { \ - if (opts.dbg) { \ + if (copt.debug) { \ fprintf(stderr, format "\n", ## __VA_ARGS__); \ } \ } while(0) @@ -44,7 +38,7 @@ #define show_error_quit(msg) \ do { \ if (error) { \ - opts.dbg = 1; \ + copt.debug = TRUE; \ err(msg, oh_lookup_error(error)); \ exit(-1); \ } \ @@ -52,19 +46,28 @@ /* Globals */ static struct hpiel_opts { - int del; /* Domain Event Log option. */ - char *ep; /* Resource Event Log option. */ - int clear; /* Clear the event log before traversing it. */ - int resource; /* Get resource along with event log entry. */ - int rdr; /* Get RDR along with event log entry. */ - int dbg; /* Display debug messages. */ -} opts = { 0, NULL, 0, 0, 0, 0 }; + gboolean del; /* Domain Event Log option. */ + gboolean clear; /* Clear the event log before traversing it. */ + gboolean resource; /* Get resource along with event log entry. */ + gboolean rdr; /* Get RDR along with event log entry. */ +} opts = { FALSE, FALSE, FALSE, FALSE }; +static oHpiCommonOptionsT copt; +static GOptionEntry my_options[] = +{ + { "del", 'd', 0, G_OPTION_ARG_NONE, &opts.del, "Display domain event log entries", NULL }, + { "clear", 'c', 0, G_OPTION_ARG_NONE, &opts.clear, "Clear log before reading event log entries", NULL }, + { "resource", 'p', 0, G_OPTION_ARG_NONE, &opts.resource, "Pull resource info along with log entry", NULL }, + { "rdr", 'r', 0, G_OPTION_ARG_NONE, &opts.rdr, "Pull RDR info along with log entry", NULL }, + { NULL } +}; + + SaHpiDomainIdT domainid = SAHPI_UNSPECIFIED_DOMAIN_ID; /* Prototypes */ -SaErrorT parse_options(int argc, char ***argv, struct hpiel_opts *opts); -SaErrorT harvest_sels(SaHpiSessionIdT sid, SaHpiDomainInfoT *dinfo, char *ep); +SaErrorT parse_options(int argc, char ***argv); +SaErrorT harvest_sels(SaHpiSessionIdT sid, SaHpiDomainInfoT *dinfo); SaErrorT display_el(SaHpiSessionIdT sid, SaHpiResourceIdT rid, SaHpiTextBufferT *tag); int main(int argc, char **argv) @@ -72,18 +75,32 @@ SaErrorT error = SA_OK; SaHpiSessionIdT sid; SaHpiDomainInfoT dinfo; + GError *gerror = NULL; + GOptionContext *context; /* Print version strings */ oh_prog_version(argv[0], OH_SVN_REV); /* Parsing options */ - if (parse_options(argc, &argv, &opts)) { - fprintf(stderr, "There was an error parsing the options. Exiting.\n"); - exit(-1); - } + context = g_option_context_new ("- Displays HPI event log entries\n" + "Option E (entity-path) displays resource event log entries.\n" + "If neither -d or -E \"<arg>\" are specified, " + "event log entries will be shown\n" + "for all supporting resources by default."); + g_option_context_add_main_entries (context, my_options, NULL); + if (!ohc_option_parse(&argc, argv, + context, &copt, + OHC_ALL_OPTIONS + - OHC_VERBOSE_OPTION, // no verbose mode implemented + gerror)) { + g_print ("option parsing failed: %s\n", gerror->message); + exit(1); + } + /* Program really begins here - all options parsed at this point */ - error = saHpiSessionOpen(domainid, &sid, NULL); + error = ohc_session_open_by_option ( &copt, &sid); + if (error != SA_OK) exit(-1); show_error_quit("saHpiSessionOpen() returned %s. Exiting.\n"); error = saHpiDiscover(sid); @@ -92,18 +109,15 @@ error = saHpiDomainInfoGet(sid, &dinfo); show_error_quit("saHpiDomainInfoGet() returned %s. Exiting.\n"); - if (domainid==SAHPI_UNSPECIFIED_DOMAIN_ID) - printf("Domain Info: UpdateCount = %u, UpdateTime = %lx\n", - dinfo.RptUpdateCount, (unsigned long)dinfo.RptUpdateTimestamp); - else printf("Domain Info: DomainId = %u, UpdateCount = %u, UpdateTime = %lx\n", - domainid, dinfo.RptUpdateCount, (unsigned long)dinfo.RptUpdateTimestamp); + printf("Domain Info: UpdateCount = %u, UpdateTime = %lx\n", + dinfo.RptUpdateCount, (unsigned long)dinfo.RptUpdateTimestamp); - if (opts.ep) { /* Entity path specified */ - error = harvest_sels(sid, &dinfo, opts.ep); + if (copt.withentitypath) { /* Entity path specified */ + error = harvest_sels(sid, &dinfo); } else if (opts.del) { /* Domain event log specified */ error = display_el(sid, SAHPI_UNSPECIFIED_RESOURCE_ID, &dinfo.DomainTag); } else { /* Else, show SELs of all supporting resources */ - error = harvest_sels(sid, &dinfo, NULL); + error = harvest_sels(sid, &dinfo); } if (error) err("An error happened. Gathering event log entries returned %s", @@ -116,97 +130,13 @@ return error; } -#define print_usage_quit() \ - do { \ - printf("\nUsage:\n" \ - " hpiel - Displays HPI event log entries.\n\n" \ - " --Domain=\"<arg>\", -D \"<arg>\" select domain id\n" \ - " --del, -d display domain event log entries\n" \ - " --entitypath=\"<arg>\", -e \"<arg>\" display resource event log entries\n" \ - " (e.g. -e \"{SYSTEM_CHASSIS,2}{SBC_BLADE,5}\")\n" \ - " --clear, -c clear log before reading event log entries\n" \ - " --resource, -p pull resource info along with log entry\n" \ - " --rdr, -r pull RDR info along with log entry\n" \ - " --xml, -x print output in xml format (not implemented)\n" \ - " --verbose, -v print debug messages\n" \ - " --help, -h print this usage message\n" \ - "\n If neither -d or -e \"<arg>\" are specified, event log entries will be shown\n" \ - " for all supporting resources by default.\n\n"); \ - exit(0); \ - } while(0) - -SaErrorT parse_options(int argc, char ***argv, struct hpiel_opts *opts) +SaErrorT harvest_sels(SaHpiSessionIdT sid, SaHpiDomainInfoT *dinfo) { - static struct option long_options[] = { - {"Domain", required_argument, 0, 'D'}, - {"del", no_argument, 0, 'd'}, - {"entitypath", required_argument, 0, 'e'}, - {"clear", no_argument, 0, 'c'}, - {"resource", no_argument, 0, 'p'}, - {"rdr", no_argument, 0, 'r'}, - {"verbose", no_argument, 0, 'v'}, - {"help", no_argument, 0, 'h'}, - {0, 0, 0, 0} - }; - int c, option_index = 0; - - while ((c = getopt_long(argc, *argv, "D:de:cprvh", long_options, &option_index)) != -1) { - switch(c) { - case 'D': - if (optarg) domainid = atoi(optarg); - else { - printf("hpiel: option requires an argument -- D"); - print_usage_quit(); - } - break; - - case 'd': - opts->del = 1; - break; - case 'e': - if (optarg) opts->ep = strdup(optarg); - else { - printf("hpiel: option requires an argument -- e"); - print_usage_quit(); - } - break; - case 'c': - opts->clear = 1; - break; - case 'p': - opts->resource = 1; - break; - case 'r': - opts->rdr = 1; - break; - case 'v': - opts->dbg = 1; - break; - case 'h': - print_usage_quit(); - break; - case '?': - printf("extraneous option found, %d\n", optopt); - print_usage_quit(); - break; - default: - printf("Found bad option %d.\n", c); - print_usage_quit(); - break; - } - } - - return 0; -} - -SaErrorT harvest_sels(SaHpiSessionIdT sid, SaHpiDomainInfoT *dinfo, char *ep) -{ SaErrorT error = SA_OK; SaHpiRptEntryT rptentry; SaHpiEntryIdT entryid, nextentryid; SaHpiResourceIdT rid; oh_big_textbuffer bigbuf; - SaHpiEntityPathT entitypath; SaHpiBoolT found_entry = SAHPI_FALSE; if (!sid || !dinfo) { @@ -214,23 +144,14 @@ return SA_ERR_HPI_INVALID_PARAMS; } - if (opts.ep && ep) { - error = oh_encode_entitypath(ep, &entitypath); - if (error) { - err("oh_encode_entitypath() returned %s from %s\n", - oh_lookup_error(error), ep); - return error; - } - } - entryid = SAHPI_FIRST_ENTRY; while (error == SA_OK && entryid != SAHPI_LAST_ENTRY) { error = saHpiRptEntryGet(sid, entryid, &nextentryid, &rptentry); err("saHpiRptEntryGet() returned %s\n", oh_lookup_error(error)); if (error == SA_OK) { - if (opts.ep && ep) { - if (!oh_cmp_ep(&entitypath, &rptentry.ResourceEntity)) { + if (copt.withentitypath) { + if (!oh_cmp_ep(&copt.entitypath, &rptentry.ResourceEntity)) { entryid = nextentryid; continue; } @@ -255,15 +176,18 @@ error = display_el(sid, rid, &rptentry.ResourceTag); - if (opts.ep && ep) return SA_OK; + if (copt.withentitypath) return SA_OK; } entryid = nextentryid; } if (!found_entry) { - if (opts.ep && ep) { - err("Could not find resource matching %s\n", ep); + if (copt.withentitypath) { + fprintf(stderr, //cannot use err macro here + "Could not find resource matching "); + oh_fprint_ep(stderr, &copt.entitypath, 0); + fprintf(stderr,"\n"); } else { err("No resources supporting event logs were found.\n"); } Modified: openhpi/trunk/clients/hpievents.c =================================================================== --- openhpi/trunk/clients/hpievents.c 2011-02-01 16:12:21 UTC (rev 7264) +++ openhpi/trunk/clients/hpievents.c 2011-02-02 09:41:18 UTC (rev 7265) @@ -2,6 +2,7 @@ * * (C) Copyright IBM Corp. 2003, 2004, 2007 * (C) Copyright Nokia Siemens Networks 2010 + * (C) Copyright Ulrich Kleber 2011 * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of @@ -20,40 +21,33 @@ * added -d option for call saHpiDiscover after saHpiSubscribe * 11/17/2004 kouzmich linux style and checking timeout error * 09/06/2010 ulikleber New option -D to select domain + * 01/02/2011 ulikleber Refactoring to use glib for option parsing and + * introduce common options for all clients */ -#include <stdio.h> -#include <stdlib.h> -#include <getopt.h> -#include <string.h> - -#include <SaHpi.h> -#include <oh_utils.h> - #include "oh_clients.h" #define OH_SVN_REV "$Revision$" #define HPI_NSEC_PER_SEC 1000000000LL -int fdebug = 0; -SaHpiDomainIdT domainid = SAHPI_UNSPECIFIED_DOMAIN_ID; +static gchar *timeout_str = NULL; +static gboolean do_discover_after_subscribe = FALSE; +static oHpiCommonOptionsT copt; -static void Usage(char **argv) +static GOptionEntry my_options[] = { - printf("Usage %s [-D <value>] [-t <value> | SAHPI_TIMEOUT_BLOCK | BLOCK] [-d] [-x]\n",argv[0]); - printf(" where: -D <value> - select domain id\n"); - printf(" -t <value> - wait <value> seconds for event;\n"); - printf(" -t SAHPI_TIMEOUT_BLOCK or BLOCK - infinite wait\n"); - printf(" -d - call saHpiDiscover() after saHpiSubscribe()\n"); - printf(" -x - displays eXtra debug messages\n"); -} + { "timeout", 't', 0, G_OPTION_ARG_STRING, &timeout_str, "Wait n seconds for event or infinite wait", + "n|BLOCK|SAHPI_TIMEOUT_BLOCK" }, + { "discover", 'd', 0, G_OPTION_ARG_NONE, &do_discover_after_subscribe, "Call saHpiDiscover() after saHpiSubscribe()", + NULL }, + { NULL } +}; + int main(int argc, char **argv) { - int c, test_fail = 0, wait = 0; - char *timeout_str= (char *)NULL; - int do_discover_after_subscribe = 0; + int test_fail = 0, wait = 0; SaErrorT rv; SaHpiSessionIdT sessionid; SaHpiDomainInfoT domainInfo; @@ -65,42 +59,32 @@ SaHpiRdrT rdr; SaHpiTimeoutT timeout; SaHpiEventT event; + GError *error = NULL; + GOptionContext *context; memset(&rptentry, 0, sizeof(rptentry)); oh_prog_version(argv[0], OH_SVN_REV); - while ( (c = getopt( argc, argv,"D:t:xd?")) != EOF ) { - switch(c) { - case 'D': - if (optarg) domainid = atoi(optarg); - else { - printf("hpievents: option requires an argument -- D"); - Usage(argv); - exit(1); - } - break; - case 't': - if (optarg) timeout_str = optarg; - else { - printf("hpievents: option requires an argument -- t"); - Usage(argv); - exit(1); - } - break; - case 'd': - do_discover_after_subscribe = 1; - break; - case 'x': - fdebug = 1; - break; - default: - Usage(argv); - exit(1); - } - } + context = g_option_context_new ("- Poll for HPI events"); + g_option_context_add_main_entries (context, my_options, NULL); - if (timeout_str != (char *)NULL) { + if (!ohc_option_parse(&argc, argv, + context, &copt, + OHC_ALL_OPTIONS + - OHC_ENTITY_PATH_OPTION //TODO: Feature 880127 + - OHC_VERBOSE_OPTION, // no verbose mode implemented + error)) { + g_print ("option parsing failed: %s\n", error->message); + exit(1); + } + + if (timeout_str) { + int i = 0; + while (i < 20 && timeout_str[i] != '\0') { + timeout_str[i] = toupper((char) timeout_str[i]); + i++; + } if ((strcmp(timeout_str, "SAHPI_TIMEOUT_BLOCK") == 0) || (strcmp(timeout_str, "BLOCK") == 0)) { timeout = SAHPI_TIMEOUT_BLOCK; @@ -114,22 +98,11 @@ printf("************** timeout:[%" PRId64 "] ****************\n", (uint64_t) timeout); - if (fdebug) { - if (domainid==SAHPI_UNSPECIFIED_DOMAIN_ID) printf("saHpiSessionOpen\n"); - else printf("saHpiSessionOpen to domain %u\n",domainid); - } - - rv = saHpiSessionOpen(domainid, &sessionid, NULL); - if (rv != SA_OK) { - if (rv == SA_ERR_HPI_ERROR) - printf("saHpiSessionOpen: error %d, SpiLibd not running\n", rv); - else - printf("saHpiSessionOpen: %s\n", oh_lookup_error(rv)); - exit(-1); - } + rv = ohc_session_open_by_option ( &copt, &sessionid); + if (rv != SA_OK) exit(-1); if (!do_discover_after_subscribe) { - if (fdebug) printf("saHpiDiscover\n"); + if (copt.debug) printf("saHpiDiscover\n"); rv = saHpiDiscover(sessionid); if (rv != SA_OK) { printf("saHpiDiscover: %s\n", oh_lookup_error(rv)); @@ -137,7 +110,7 @@ } } - if (fdebug) printf( "Subscribe to events\n"); + if (copt.debug) printf( "Subscribe to events\n"); rv = saHpiSubscribe( sessionid ); if (rv != SA_OK) { printf("saHpiSubscribe: %s\n", oh_lookup_error(rv)); @@ -145,7 +118,7 @@ } if (do_discover_after_subscribe) { - if (fdebug) printf("saHpiDiscover after saHpiSubscribe\n"); + if (copt.debug) printf("saHpiDiscover after saHpiSubscribe\n"); rv = saHpiDiscover(sessionid); if (rv != SA_OK) { printf("saHpiDiscover after saHpiSubscribe: %s\n", oh_lookup_error(rv)); @@ -155,7 +128,7 @@ rv = saHpiDomainInfoGet(sessionid, &domainInfo); - if (fdebug) printf("saHpiDomainInfoGet %s\n", oh_lookup_error(rv)); + if (copt.debug) printf("saHpiDomainInfoGet %s\n", oh_lookup_error(rv)); printf("DomainInfo: UpdateCount = %u, UpdateTime = %lx\n", domainInfo.RptUpdateCount, (unsigned long)domainInfo.RptUpdateTimestamp); @@ -165,23 +138,23 @@ printf("**********************************************\n"); rv = saHpiRptEntryGet(sessionid, rptentryid, &nextrptentryid, &rptentry); - if (fdebug) printf("saHpiRptEntryGet %s\n", oh_lookup_error(rv)); + if (copt.debug) printf("saHpiRptEntryGet %s\n", oh_lookup_error(rv)); if (rv == SA_OK) { resourceid = rptentry.ResourceId; - if (fdebug) + if (copt.debug) printf("RPT %x capabilities = %x\n", resourceid, rptentry.ResourceCapabilities); if ( (rptentry.ResourceCapabilities & SAHPI_CAPABILITY_EVENT_LOG)) { /* Using EventLogInfo to build up event queue - for now */ rv = saHpiEventLogInfoGet(sessionid, resourceid, &info); - if (fdebug) + if (copt.debug) printf("saHpiEventLogInfoGet %s\n", oh_lookup_error(rv)); if (rv == SA_OK) oh_print_eventloginfo(&info, 4); } else { - if (fdebug) + if (copt.debug) printf("RPT doesn't have SEL\n"); } @@ -238,7 +211,7 @@ printf(" Test FAILED.\n"); /* Unsubscribe to future events */ - if (fdebug) printf( "Unsubscribe\n"); + if (copt.debug) printf( "Unsubscribe\n"); rv = saHpiUnsubscribe( sessionid ); rv = saHpiSessionClose(sessionid); Modified: openhpi/trunk/clients/hpifan.c =================================================================== --- openhpi/trunk/clients/hpifan.c 2011-02-01 16:12:21 UTC (rev 7264) +++ openhpi/trunk/clients/hpifan.c 2011-02-02 09:41:18 UTC (rev 7265) @@ -3,6 +3,7 @@ * * Copyright (c) 2003,2004 by FORCE Computers * (C) Copyright Nokia Siemens Networks 2010 + * (C) Copyright Ulrich Kleber 2011 * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of @@ -18,17 +19,11 @@ * Changes: * 10/13/2004 kouzmich porting to HPI B * 09/06/2010 ulikleber New option -D to select domain + * 01/02/2011 ulikleber Refactoring to use glib for option parsing and + * introduce common options for all clients * */ -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <getopt.h> - -#include <SaHpi.h> -#include <oh_utils.h> - #include "oh_clients.h" #define OH_SVN_REV "$Revision$" @@ -36,53 +31,16 @@ static SaHpiBoolT set_new = SAHPI_FALSE; static SaHpiCtrlModeT new_mode = SAHPI_CTRL_MODE_MANUAL; static int new_speed = -1; -SaHpiDomainIdT domainid = SAHPI_UNSPECIFIED_DOMAIN_ID; +static gchar *f_speed = NULL; +static oHpiCommonOptionsT copt; - -static int -usage( char *progname ) +static GOptionEntry my_options[] = { - fprintf( stderr, "usage: %s [-D domainid] [-h] [-s fan_speed_level]\n", progname ); - fprintf( stderr, "\t\t -D domainid select the domain to work on\n" ); - fprintf( stderr, "\t\t -h help\n" ); - fprintf( stderr, "\t\t -s speed set fan speed for ALL fans in domain\n" ); - fprintf( stderr, "\t\t speed is a number or \"auto\" for setting fan in auto mode\n" ); + { "speed", 's', 0, G_OPTION_ARG_STRING, &f_speed, "Set fan speed for ALL fans in domain\n" + " speed is a number or \"auto\" for setting fan in auto mode", "auto|nn" }, + { NULL } +}; - return 1; -} - - -static void -display_textbuffer( SaHpiTextBufferT string ) -{ - int i; - switch( string.DataType ) { - case SAHPI_TL_TYPE_BINARY: - for( i = 0; i < string.DataLength; i++ ) - printf( "%x", string.Data[i] ); - break; - case SAHPI_TL_TYPE_BCDPLUS: - for( i = 0; i < string.DataLength; i++ ) - printf( "%c", string.Data[i] ); - break; - case SAHPI_TL_TYPE_ASCII6: - for( i = 0; i < string.DataLength; i++ ) - printf( "%c", string.Data[i] ); - break; - case SAHPI_TL_TYPE_UNICODE: - for( i = 0; i < string.DataLength; i++ ) - printf( "%c", string.Data[i] ); - break; - case SAHPI_TL_TYPE_TEXT: - for( i = 0; i < string.DataLength; i++ ) - printf( "%c", string.Data[i] ); - break; - default: - printf("Invalid string data type=%u", string.DataType ); - } -} - - static SaErrorT get_fan_speed( SaHpiSessionIdT session_id, SaHpiResourceIdT resource_id, @@ -92,6 +50,8 @@ { SaHpiCtrlStateT state; + if (copt.debug) printf("get fan speed for resource %u, control %u\n", resource_id, ctrl_num); + SaErrorT rv = saHpiControlGet( session_id, resource_id, ctrl_num, mode, &state ); if ( rv != SA_OK ) { @@ -122,6 +82,7 @@ state.Type = SAHPI_CTRL_TYPE_ANALOG; state.StateUnion.Analog = speed; + if (copt.debug) printf("set fan speed for resource %u, control %u\n", resource_id, ctrl_num); rv = saHpiControlSet( session_id, resource_id, ctrl_num, mode, &state ); if ( rv != SA_OK ) { @@ -142,7 +103,7 @@ SaHpiCtrlModeT ctrl_mode; printf( "\tfan: num %u, id ", ctrl_rec->Num ); - display_textbuffer( rdr->IdString ); + oh_print_text ( &rdr->IdString ); printf( "\n" ); if ( ctrl_rec->Type != SAHPI_CTRL_TYPE_ANALOG ) { @@ -265,69 +226,58 @@ int main( int argc, char *argv[] ) { - int c; - int help = 0; SaErrorT rv; + SaHpiSessionIdT sessionid; + GError *error = NULL; + GOptionContext *context; oh_prog_version(argv[0], OH_SVN_REV); - while( (c = getopt( argc, argv,"D:hs:") ) != -1 ) - switch( c ) { - case 'D': - if (optarg) domainid = atoi(optarg); - else { - printf("hpifan: option requires an argument -- D"); - help = 1; - } - break; - case 'h': - help = 1; - break; + context = g_option_context_new ("- Show \"Fan Control\" management instruments"); + g_option_context_add_main_entries (context, my_options, NULL); - case 's': - if (optarg) { - set_new = SAHPI_TRUE; - if ( strcmp( optarg, "auto" ) == 0 ) { - new_mode = SAHPI_CTRL_MODE_AUTO; - } else { - new_speed = atoi( optarg ); - } - } else { - printf("hpifan: option requires an argument -- s"); - help = 1; - } - break; - - default: - fprintf( stderr, "unknown option %s !\n", - argv[optind] ); - help = 1; - } - - if ( help ) - return usage(argv[0]); - - SaHpiSessionIdT sessionid; - rv = saHpiSessionOpen(domainid, &sessionid, NULL); - if ( rv != SA_OK ) { - printf( "saHpiSessionOpen: %s\n", oh_lookup_error( rv ) ); - return 1; + if (!ohc_option_parse(&argc, argv, + context, &copt, + OHC_ALL_OPTIONS + - OHC_ENTITY_PATH_OPTION //TODO: Feature 880127 + - OHC_VERBOSE_OPTION, // no verbose mode implemented + error)) { + g_print ("option parsing failed: %s\n", error->message); + exit(1); + } + + if (f_speed) { + set_new = SAHPI_TRUE; + if ( strcmp( f_speed, "auto" ) == 0 ) { + new_mode = SAHPI_CTRL_MODE_AUTO; + } else if ( strcmp( f_speed, "0" ) == 0 ) { + new_speed = 0; + } else { + new_speed = atoi( f_speed ); + if (new_speed == 0) { + g_print ("please enter a valid speed: \"auto\" or a number.\n"); + exit(1); + } + } } - if (domainid!=SAHPI_UNSPECIFIED_DOMAIN_ID) - printf("HPI Session to domain %u\n",domainid); + rv = ohc_session_open_by_option ( &copt, &sessionid); + if (rv != SA_OK) exit(-1); - rv = saHpiDiscover( sessionid ); - + /* + * Resource discovery + */ + if (copt.debug) printf("saHpiDiscover\n"); + rv = saHpiDiscover(sessionid); if ( rv != SA_OK ) { printf( "saHpiDiscover: %s\n", oh_lookup_error( rv ) ); return 1; } + if (copt.debug) printf("Discovery done\n"); int rc = discover_domain( sessionid ); rv = saHpiSessionClose( sessionid ); - if ( rv != SA_OK ) printf( "saHpiSessionClose: %s\n", oh_lookup_error( rv ) ); Modified: openhpi/trunk/clients/hpigensimdata.c =================================================================== --- openhpi/trunk/clients/hpigensimdata.c 2011-02-01 16:12:21 UTC (rev 7264) +++ openhpi/trunk/clients/hpigensimdata.c 2011-02-02 09:41:18 UTC (rev 7265) @@ -2,6 +2,7 @@ * hpigensimdata.c * * Copyright (c) 2010 by Lars Wetzel + * (C) Copyright Ulrich Kleber 2011 * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of @@ -12,6 +13,7 @@ * * Authors: * Lars Wetzel (klw) <lar...@us...> + * Ulrich Kleber <uli...@us...> * * Changes: * 10/02/09 (klw) Release 0.9 @@ -24,20 +26,15 @@ * - maybe more selection criteria at the command line (eg. only sensor) * depends on user which needs this client for some other reasons * + * Changes: + * 01/02/2011 ulikleber Refactoring to use glib for option parsing and + * introduce common options for all clients */ -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <getopt.h> -#include <ctype.h> -#include <SaHpi.h> -#include <oh_utils.h> - #include "oh_clients.h" #define OH_SVN_REV "$Revision: 6571 $" -#define GEN_SIM_DATA_VERSION 0.901 +#define GEN_SIM_DATA_VERSION "0.901000" #define OFFSET_STEP 3 #define OFFSET_FILLER ' ' @@ -83,9 +80,19 @@ /* * Globals */ -int g_resource = FALSE; -int g_file = FALSE; +static gint g_resourceid = (gint) SAHPI_UNSPECIFIED_RESOURCE_ID; +static gchar *g_file = NULL; +static gchar *g_mode = NULL; +static oHpiCommonOptionsT copt; +static GOptionEntry my_options[] = +{ + { "resource", 'r', 0, G_OPTION_ARG_INT, &g_resourceid, "Select particular resource id for an update file", "res_id" }, + { "file", 'f', 0, G_OPTION_ARG_FILENAME, &g_file, "Name of the file to be generated", "filename" }, + { "mode", 'm', 0, G_OPTION_ARG_STRING, &g_mode, "Write update or initial file", "UPD|INIT" }, + { NULL } +}; + char offSet[MAX_OFFSETS][MAX_CHAR]; @@ -282,82 +289,53 @@ */ int main(int argc, char **argv) { SaErrorT rv = SA_OK; - SaHpiDomainIdT domainid = SAHPI_UNSPECIFIED_DOMAIN_ID; SaHpiSessionIdT sessionid; - SaHpiResourceIdT resourceid = 0; - char filename[MAX_CHAR+1]; FILE *outfile = stdout; - int c; ConfigurationDataT confdata; - SaHpiBoolT printusage = FALSE; + GError *error = NULL; + GOptionContext *context; confdata.mode = MODE_INIT; oh_prog_version(argv[0], OH_SVN_REV); - while ( (c = getopt( argc, argv,"xD:r:f:m:?")) != EOF ) { - switch(c) { - case 'D': - if (optarg) domainid = atoi(optarg); - else printusage = TRUE; - break; - case 'r': - if (optarg) { - resourceid = atoi(optarg); - g_resource = TRUE; - } else { - fprintf(stderr, "\nResourceid is missing\n"); - printusage = TRUE; - } - break; - case 'm': - if (optarg) { - char tmpmode[MAX_SHORT_CHAR+1]; - int i = 0; - while (i < MAX_SHORT_CHAR || optarg[i] != '\0') { - tmpmode[i] = toupper((char) optarg[i]); - i++; - } - tmpmode[i] = '\0'; - if (!strcmp(tmpmode, "UPD")) { - confdata.mode = MODE_UPD; - } else if (!strcmp(tmpmode, "INIT")) { - confdata.mode = MODE_INIT; - } else { - fprintf(stderr, "\nUnknown mode %s.\n", tmpmode); - printusage = TRUE; - } - } - break; - case 'f': - g_file = 1; - if (optarg) { - strncpy(filename, optarg, MAX_CHAR); - filename[MAX_CHAR] = '\0'; - outfile = fopen(filename, "w"); - if (outfile == NULL) { - fprintf(stderr, "\n%s couldn't be opened for writing.\n", filename); - exit(1); - } - break; - } - default: printusage = TRUE; + context = g_option_context_new ("- Version " GEN_SIM_DATA_VERSION + "\nThe client will print all HPI information in a format that can be parsed by Dynamic" + "\nSimulator plugin."); + g_option_context_add_main_entries (context, my_options, NULL); + + if (!ohc_option_parse(&argc, argv, + context, &copt, + OHC_ALL_OPTIONS + - OHC_ENTITY_PATH_OPTION //TODO: Feature 880127 + - OHC_VERBOSE_OPTION // no verbose mode + - OHC_DEBUG_OPTION, // no debug mode + error)) { + g_print ("option parsing failed: %s\n", error->message); + exit(1); + } + + if (g_file) { + outfile = fopen(g_file, "w"); + if (outfile == NULL) { + fprintf(stderr, "\n%s couldn't be opened for writing.\n", g_file); + exit(1); } } - if (printusage == TRUE) - { - printf("\n %s - Version %.3f\n", argv[0], GEN_SIM_DATA_VERSION); - printf("\nThe client will print all HPI information. This information can be parsed by NewSimulator plugin."); - printf("\nTo spare some encoding/decoding function, values of enums are printed as int values instead of" - "encode them to ASCII values and decode them afterwards."); - printf("\nSince this client is primarily for the NewSimulator, the output functions don't depend on the" - "oh_.. - output function. Therefore they can be easily changed without influence on other clients."); - printf("\n\tUsage: %s [-option]\n\n", argv[0]); - printf("\t -D domain_id Select particular Domain\n"); - printf("\t -r res_id Select particular resource id for an update file\n"); - printf("\t -f filename Name of the file to be generated\n"); - printf("\t -m (UPD|INIT) Write update or initial file\n"); - printf("\n\n\n\n"); - exit(1); + + if (g_mode) { + int i = 0; + while (i < MAX_SHORT_CHAR && g_mode[i] != '\0') { + g_mode[i] = toupper((char) g_mode[i]); + i++; + } + if (!strcmp(g_mode, "UPD")) { + confdata.mode = MODE_UPD; + } else if (!strcmp(g_mode, "INIT")) { + confdata.mode = MODE_INIT; + } else { + fprintf(stderr, "\nUnknown mode %s.\n", g_mode); + exit (1); + } } /** @@ -373,11 +351,8 @@ print_header(outfile, 0, confdata); - rv = saHpiSessionOpen(domainid,&sessionid,NULL); - if (rv != SA_OK) { - printf("saHpiSessionOpen returns %s\n",oh_lookup_error(rv)); - exit(-1); - } + rv = ohc_session_open_by_option ( &copt, &sessionid); + if (rv != SA_OK) exit(-1); /* * Resource discovery @@ -387,7 +362,7 @@ printf("saHpiDiscover returns %s\n",oh_lookup_error(rv)); exit(-1); } - print_resources(outfile, 0, sessionid, resourceid); + print_resources(outfile, 0, sessionid, (SaHpiResourceIdT) g_resourceid); rv = saHpiSessionClose(sessionid); if (g_file) fclose(outfile); @@ -411,7 +386,7 @@ fprintf(out, "%s%s {\n", offSet[myoffset++], CONF_SECTION); fprintf(out, "%sMODE=%s\n", offSet[myoffset], strMode); - fprintf(out, "%sVERSION=%f\n", offSet[myoffset], GEN_SIM_DATA_VERSION); + fprintf(out, "%sVERSION=%s\n", offSet[myoffset], GEN_SIM_DATA_VERSION); fprintf(out, "%s}\n", offSet[--myoffset]); } @@ -440,7 +415,8 @@ return rvRptGet; } - if ((g_resource == FALSE) || (resourceid == rptentry.ResourceId)) { + if ((resourceid == SAHPI_UNSPECIFIED_RESOURCE_ID) + || (resourceid == rptentry.ResourceId)) { fprintf(out, "%s%s {\n", offSet[myoffset], RPT_SECTION); myoffset++; Modified: openhpi/trunk/clients/hpiinv.c =================================================================== --- openhpi/trunk/clients/hpiinv.c 2011-02-01 16:12:21 UTC (rev 7264) +++ openhpi/trunk/clients/hpiinv.c 2011-02-02 09:41:18 UTC (rev 7265) @@ -5,6 +5,7 @@ * Copyright (c) 2003-2005 by Intel Corp. * (C) Copyright IBM Corp. 2004 * (C) Copyright Nokia Siemens Networks 2010 + * (C) Copyright Ulrich Kleber 2011 * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of @@ -27,19 +28,12 @@ * 2/09/05 ARCress re-merged with hpifru.c, adding IdrFieldSet, * generalized for other HPI libraris also. * 09/06/10 ulikleber New option -D to select domain + * 20/01/11 ulikleber Refactoring to use glib for option parsing and + * introduce common options for all clients */ #define OPENHPI_USED 1 -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <getopt.h> -#include <SaHpi.h> - #include "oh_clients.h" -#ifdef OPENHPI_USED -#include <oh_utils.h> -#endif #define OH_SVN_REV "$Revision$" @@ -54,16 +48,24 @@ "Buss Expansion Chassis", "Peripheral Chassis", "RAID Chassis", "Rack-Mount Chassis", "New" }; -int fasset = 0; -int fverbose = 0; -int foundasset = 0; -int fdebug = 0; -int fzdebug = 0; +static gchar *fatag = NULL; +static oHpiCommonOptionsT copt; + +static GOptionEntry my_options[] = +{ + { "asset-tag", 'a', 0, G_OPTION_ARG_STRING, &fatag, "Set the asset tag to the specified string", "tag" }, + { NULL } +}; + + + + int i,j,k = 0; SaHpiUint32T buffersize; SaHpiUint32T actualsize; char inbuff[2048]; char outbuff[1024]; +int foundasset = 0; SaHpiIdrAreaHeaderT *inv; SaHpiIdrFieldTypeT chasstype; SaHpiTextBufferT *strptr; @@ -175,7 +177,7 @@ if (datalen != 0) { strncpy ((char *)outstr, (char *)strptr->Data, datalen); outstr[datalen] = 0; - if (fdebug) { + if (copt.debug) { printf("TextBuffer len=%u, dtype=%x lang=%u: %s\n", strptr->DataLength,strptr->DataType,strptr->Language, strptr->Data ); @@ -293,7 +295,7 @@ char *pstr; #ifdef OPENHPI_USED - if (fverbose) { + if (copt.verbose) { oh_print_ep(epath, len); } else #endif @@ -320,11 +322,11 @@ static void print_idrinfo(SaHpiIdrInfoT *idrInfo,int len) { #ifdef OPENHPI_USED - if (fverbose) { + if (copt.verbose) { oh_print_idrinfo(idrInfo, len); } else #endif - { /* don't need to show this if not fverbose */ + { /* don't need to show this if not verbose */ } } @@ -333,7 +335,7 @@ { int i; #ifdef OPENHPI_USED - if (fverbose) { + if (copt.verbose) { oh_print_idrareaheader(areaHeader, len); } else #endif @@ -354,7 +356,7 @@ int i; #ifdef OPENHPI_USED - if (fverbose) { + if (copt.verbose) { oh_print_idrfield(field, len); } else #endif @@ -418,7 +420,7 @@ areaHeader.AreaId, fieldType, fieldId, &nextFieldId, &thisField); - if (fdebug) + if (copt.debug) printf("saHpiIdrFieldGet[%x] rv = %d type=%u\n", idrInfo->IdrId,rvField, thisField.Type); @@ -457,9 +459,7 @@ int main(int argc, char **argv) { - int c; SaErrorT rv,rv_rdr; - SaHpiDomainIdT domainid = SAHPI_UNSPECIFIED_DOMAIN_ID; SaHpiSessionIdT sessionid; SaHpiDomainInfoT rptinfo; SaHpiRptEntryT rptentry; @@ -473,58 +473,38 @@ SaHpiIdrIdT idrid; int invfound = 0; int nloops = 0; - SaHpiBoolT printusage = FALSE; + GError *error = NULL; + GOptionContext *context; oh_prog_version(argv[0], OH_SVN_REV); atag.tlen = 0; - while ( (c = getopt( argc, argv,"D:a:vxz?")) != EOF ) - switch(c) { - case 'D': - if (optarg) domainid = atoi(optarg); - else printusage = TRUE; - break; - case 'z': fzdebug = 1; /* fall thru to include next setting */ - case 'x': fdebug = 1; break; - case 'v': fverbose = 1; break; - case 'a': - fasset = 1; - if (optarg) { - atag.tag = (char *)strdup(optarg); - atag.tlen = strlen(optarg); - } - else printusage = TRUE; - break; - default: printusage = TRUE; + context = g_option_context_new ("- Shows resources' inventory records"); + g_option_context_add_main_entries (context, my_options, NULL); + + if (!ohc_option_parse(&argc, argv, + context, &copt, + OHC_ALL_OPTIONS + - OHC_ENTITY_PATH_OPTION, //TODO: Feature 880127? + error)) { + g_print ("option parsing failed: %s\n", error->message); + exit(1); } - if (printusage) { - printf("Usage: %s [-x] [-a asset_tag]\n", argv[0]); - printf(" -D domainid Selects the domain\n"); - printf(" -a tag Sets the asset tag\n"); - printf(" -v Use verbose output mode\n"); - printf(" -x Display debug messages\n"); - printf(" -z Display extra debug messages\n"); - exit(1); + if (fatag) { + atag.tag = (char *)strdup(fatag); + atag.tlen = strlen(fatag); } /* compile error */ // inv = (SaHpiIdrAreaHeaderT *)&inbuff[0]; inv = (SaHpiIdrAreaHeaderT *)(void *)&inbuff[0]; - if (fdebug) { - if (domainid==SAHPI_UNSPECIFIED_DOMAIN_ID) printf("saHpiSessionOpen\n"); - else printf("saHpiSessionOpen to domain %u\n",domainid); - } - rv = saHpiSessionOpen(domainid,&sessionid,NULL); - if (fdebug) printf("saHpiSessionOpen rv = %d sessionid = %u\n",rv,sessionid); - if (rv != SA_OK) { - printf("saHpiSessionOpen error %d\n",rv); - exit(-1); - } - + rv = ohc_session_open_by_option ( &copt, &sessionid); + if (rv != SA_OK) exit(-1); + rv = saHpiDomainInfoGet(sessionid,&rptinfo); - if (fdebug) printf("saHpiDomainInfoGet rv = %d\n",rv); - // if (fdebug) printf("RptInfo: UpdateCount = %x, UpdateTime = %lx\n", + if (copt.debug) printf("saHpiDomainInfoGet rv = %d\n",rv); + // if (copt.debug) printf("RptInfo: UpdateCount = %x, UpdateTime = %lx\n", // rptinfo.UpdateCount, (unsigned long)rptinfo.UpdateTimestamp); while (!invfound && (nloops < 7)) @@ -535,9 +515,9 @@ * This should not apply to other well-behaved plugins. */ nloops++; - if (fdebug) printf("Starting Discovery, pass %u ...\n",nloops); + if (copt.debug) printf("Starting Discovery, pass %u ...\n",nloops); rv = saHpiDiscover(sessionid); - if (fdebug) printf("saHpiDiscover rv = %d\n",rv); + if (copt.debug) printf("saHpiDiscover rv = %d\n",rv); if (rv != SA_OK) { printf("saHpiDiscover error %d\n",rv); break; @@ -558,7 +538,7 @@ /* walk the RDR list for this RPT entry */ entryid = SAHPI_FIRST_ENTRY; resourceid = rptentry.ResourceId; - if (fdebug) printf("rptentry[%u] resourceid=%d\n", rptentryid,resourceid); + if (copt.debug) printf("rptentry[%u] resourceid=%d\n", rptentryid,resourceid); if (rptentry.ResourceCapabilities & SAHPI_CAPABILITY_INVENTORY_DATA) { printf("Resource[%u] Tag: %s \thas inventory capability\n", rptentryid,tagstr); @@ -566,10 +546,10 @@ while ((rv_rdr == SA_OK) && (entryid != SAHPI_LAST_ENTRY)) { rv_rdr = saHpiRdrGet(sessionid,resourceid, entryid,&nextentryid, &rdr); - if (fdebug) printf("saHpiRdrGet[%u] rv = %d\n",entryid,rv_rdr); + if (copt.debug) printf("saHpiRdrGet[%u] rv = %d\n",entryid,rv_rdr); if (rv_rdr == SA_OK) { - if (fdebug) printf("Rdr[%u] type = %u tag = %s\n",entryid, + if (copt.debug) printf("Rdr[%u] type = %u tag = %s\n",entryid, rdr.RdrType,rdr.IdString.Data); if (rdr.RdrType == SAHPI_INVENTORY_RDR) { @@ -578,7 +558,7 @@ rdr.IdString.Data[rdr.IdString.DataLength] = 0; idrid = rdr.RdrTypeUnion.InventoryRec.IdrId; buffersize = sizeof(inbuff); - if (fdebug) { + if (copt.debug) { printf("Rdr[%x] is inventory, IdrId=%x\n",rdr.RecordId,idrid); printf("Inv BufferSize=%u\n", buffersize); } @@ -590,18 +570,18 @@ if (rv_rdr != SA_OK) { printf("IDR Info error: rv_rdr = %d\n",rv_rdr); } else { /* idrInfo is ok */ - if (fdebug) printf("IDR Info: ok \n"); + if (copt.debug) printf("IDR Info: ok \n"); print_epath(&rptentry.ResourceEntity, 1); printf("RDR[%x]: Inventory, IdrId=%x %s\n",rdr.RecordId, idrid,rdr.IdString.Data); print_idrinfo(&idrInfo,4); rv_rdr = walkInventory(sessionid, resourceid, &idrInfo); - if (fdebug) printf("walkInventory rv_rdr = %d\n",rv_rdr); + if (copt.debug) printf("walkInventory rv_rdr = %d\n",rv_rdr); } if (!ent_writable(&rptentry.ResourceEntity,&idrInfo)) foundasset = 0; - if ((fasset == 1) && (foundasset == 1)) { + if ((fatag) && (foundasset == 1)) { SaHpiIdrFieldT atagField; atagField.Type = SAHPI_IDR_FIELDTYPE_ASSET_TAG; atagField.ReadOnly = SAHPI_FALSE; @@ -620,22 +600,22 @@ if (rv_rdr == SA_OK) { printf ("Good write - re-reading!\n"); rv_rdr = walkInventory(sessionid, resourceid, &idrInfo); - if (fdebug) printf("walkInventory rv_rdr = %d\n",rv_rdr); + if (copt.debug) printf("walkInventory rv_rdr = %d\n",rv_rdr); } /* Good write - re-read */ - } /*endif fasset*/ + } /*endif foundasset*/ } /*endif RDR tag ok*/ } /* Inventory Data Records - Type 3 */ - else if (fdebug) printf("rdr type = %u\n", rdr.RdrType); + else if (copt.debug) printf("rdr type = %u\n", rdr.RdrType); } /*endif RdrGet ok*/ entryid = nextentryid; } /*end while rdr*/ } /*endif rpt invent capab*/ else - if (fdebug) printf("Resource[%u] Tag: %s\n", rptentryid,tagstr); + if (copt.debug) printf("Resource[%u] Tag: %s\n", rptentryid,tagstr); } /*endif rpt ok*/ rptentryid = nextrptentryid; } /*end rpt loop */ - if (fdebug) printf("loop %u inventory found = %d\n",nloops,invfound); + if (copt.debug) printf("loop %u inventory found = %d\n",nloops,invfound); } /*end while no inv */ rv = saHpiSessionClose(sessionid); exit(0); Modified: openhpi/trunk/clients/hpionIBMblade.c =================================================================== --- openhpi/trunk/clients/hpionIBMblade.c 2011-02-01 16:12:21 UTC (rev 7264) +++ openhpi/trunk/clients/hpionIBMblade.c 2011-02-02 09:41:18 UTC (rev 7265) @@ -2,6 +2,8 @@ * * Copyright (c) 2003 by Intel Corp. * 2006 by IBM Corp. + * (C) Copyright Ulrich Kleber 2011 + * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. This @@ -57,19 +59,11 @@ * 10/13/2006 pdphan Copy from kouzmich's hpithres.c to hpionIBMblade.c * Add functions specifically for * an IBM blade with Baseboard Management Controller (BMC) + * 20/01/2011 ulikleber Refactoring to use glib for option parsing and + * introduce common options for all clients, + * e.g. --domain and --host */ -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <getopt.h> - -#include <glib.h> - -#include <SaHpi.h> -#include <oh_utils.h> -#include <oHpi.h> - #include "oh_clients.h" #define OH_SVN_REV "$Revision: 6412 $" @@ -138,8 +132,7 @@ int nrpts = 0; long int blade_slot = 0; -int fdebug = 0; - +static oHpiCommonOptionsT copt; SaHpiSessionIdT sessionid; static void *resize_array(void *Ar, int item_size, int *last_num, int add_num) @@ -232,13 +225,13 @@ Rpt->Rpt.ResourceId, &this_handler_id); if (rv) { - if (fdebug) printf("oHpiHandlerFind returns %s\n", oh_lookup_error(rv)); + if (copt.debug) printf("oHpiHandlerFind returns %s\n", oh_lookup_error(rv)); return(0); } rv = oHpiHandlerInfo(sessionid, this_handler_id, &handler_info, config); if (rv) { - if (fdebug) printf("oHpiHandlerInfo returns %s\n", oh_lookup_error(rv)); + if (copt.debug) printf("oHpiHandlerInfo returns %s\n", oh_lookup_error(rv)); return(0); } @@ -588,7 +581,7 @@ NULL, NULL, NULL); if (rv == SA_OK) break; - if (fdebug) printf("sleep before saHpiEventGet\n"); + if (copt.debug) printf("sleep before saHpiEventGet\n"); g_usleep(G_USEC_PER_SEC); }; saHpiSensorThresholdsGet(sessionid, Rpt->Rpt.ResourceId, @@ -898,39 +891,41 @@ int main(int argc, char **argv) { - int c, i; + int i; SaErrorT rv; char buf[READ_BUF_SIZE]; char *S; + GError *error = NULL; + GOptionContext *context; oh_prog_version(argv[0], OH_SVN_REV); - while ( (c = getopt( argc, argv,"x?")) != EOF ) - switch(c) { - case 'x': - fdebug = 1; - break; - default: - printf("Usage: %s [-x]\n", argv[0]); - printf(" -x Display debug messages\n"); - return(1); - } + context = g_option_context_new ("- Shows how to use two (2) openhpi " + "plugins to display and manage resources of an IBM Blade " + "with Basedboard Management Controller (BMC)"); + + if (!ohc_option_parse(&argc, argv, + context, &copt, + OHC_ALL_OPTIONS + - OHC_ENTITY_PATH_OPTION // not implemented + - OHC_VERBOSE_OPTION, // no verbose mode implemented + error)) { + g_print ("option parsing failed: %s\n", error->message); + exit(1); + } + rv = hpiIBMspecial_find_blade_slot(); if (rv != SA_OK) { printf("ipmitool can not find slot number in which this blade resides.\n"); return(-1); } - rv = saHpiSessionOpen(SAHPI_UNSPECIFIED_DOMAIN_ID, &sessionid, NULL); + rv = ohc_session_open_by_option ( &copt, &sessionid); + if (rv != SA_OK) exit(-1); - if (rv != SA_OK) { - printf("saHpiSessionOpen: %s\n", oh_lookup_error(rv)); - return(-1); - } - rv = saHpiDiscover(sessionid); - if (fdebug) printf("saHpiDiscover: %s\n", oh_lookup_error(rv)); + if (copt.debug) printf("saHpiDiscover: %s\n", oh_lookup_error(rv)); rv = saHpiSubscribe(sessionid); if (rv != SA_OK) { @@ -954,4 +949,4 @@ rv = saHpiSessionClose(sessionid); return(0); } - /* end hpthres.c */ + /* end hpionIBMblade.c */ Modified: openhpi/trunk/clients/hpipower.c =================================================================== --- openhpi/trunk/clients/hpipower.c 2011-02-01 16:12:21 UTC (rev 7264) +++ openhpi/trunk/clients/hpipower.c 2011-02-02 09:41:18 UTC (rev 7265) @@ -2,6 +2,7 @@ * * (C) Copyright IBM Corp. 2004-2006 * (C) Copyright Nokia Siemens Networks 2010 + * (C) Copyright Ulrich Kleber 2011 * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of @@ -16,22 +17,16 @@ * * Changes: * 09/06/2010 ulikleber New option -D to select domain + * 20/01/2011 ulikleber Refactoring to use glib for option parsing and + * introduce common options for all clients */ -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <glib.h> -#include <getopt.h> -#include <SaHpi.h> -#include <oh_utils.h> - #include "oh_clients.h" #define OH_SVN_REV "$Revision$" #define MAX_MANAGED_SYSTEMS 80 -#define HPI_POWER_DEBUG_PRINT(a) if(DebugPrints==TRUE)printf(a) +#define HPI_POWER_DEBUG_PRINT(a) if(copt.debug==TRUE)printf(a) typedef struct COMPUTER_DATA_ { @@ -42,7 +37,24 @@ char NameStr[80];//Text Name for the computer or plade } COMPUTER_DATA; +static gboolean f_down = FALSE; +static gboolean f_up = FALSE; +static gboolean f_reset = FALSE; +static gboolean f_unattended = FALSE; +static gint f_blade = 0; +static oHpiCommonOptionsT copt; +static GOptionEntry my_options[] = +{ + { "power-down", 'd', 0, G_OPTION_ARG_NONE, &f_down, "Power down target object", NULL }, + { "power-up", 'p', 0, G_OPTION_ARG_NONE, &f_up, "Power on target object", NULL }, + { "reset", 'r', 0, G_OPTION_ARG_NONE, &f_reset, "Reset target object", NULL }, + { "unattended", 'u', 0, G_OPTION_ARG_NONE, &f_unattended, "Unattended", NULL }, + { "blade", 'b', 0, G_OPTION_ARG_INT, &f_blade, "Specify blade n (1...n)", "n" }, + { NULL } +}; + + /* Utility function to work with SaHpiTextBufferT */ static void HpiTextBuffer2CString( const SaHpiTextBufferT * tb, char * cstr ) { @@ -61,126 +73,76 @@ * main * * Hpi Power Utility Entry point routine - * - * Parameters: - * int - argc, - * char pointer pointer - argv - * Program Command line Arguments: - * 'd' power down - * 'p' power up - * 'r' hard reset - * 'u' unattended - * 'b:n' Blade number - * '?' useage - help - * 'x' debug messages - * Reserved Platform Specific for future use: - * 'c' 'o' 'n' 's' ************************************************/ int main(int argc, char **argv) { - SaHpiInt32T ComputerNumber; //0..n-1 - SaHpiInt32T SelectedSystem; //0..n-1 - SaHpiPowerStateT Action; + SaHpiInt32T ComputerNumber = 0; //0..n-1 + SaHpiInt32T SelectedSystem = 0; //0..n-1 + SaHpiPowerStateT Action = 255; //set it out of range to stand for status; COMPUTER_DATA *ComputerPtr; - SaHpiBoolT BladeSelected; - SaHpiBoolT MultipleBlades; - SaHpiBoolT ActionSelected; - SaHpiBoolT PrintUsage; - SaHpiBoolT DebugPrints; + SaHpiBoolT BladeSelected = FALSE; + SaHpiBoolT MultipleBlades = FALSE; + SaHpiBoolT ActionSelected = FALSE; GSList* Computer; GSList* ComputerListHead; - int option; - SaHpiDomainIdT domainid = SAHPI_UNSPECIFIED_DOMAIN_ID; SaHpiSessionIdT SessionId; SaErrorT Status; - SaHpiEntryIdT RptEntry, RptNextEntry; + SaHpiEntryIdT RptEntry = SAHPI_FIRST_ENTRY; + SaHpiEntryIdT RptNextEntry; SaHpiRptEntryT Report; SaHpiInt32T Index, EntityElement; - SaHpiPowerStateT PowerState; + SaHpiPowerStateT PowerState; char PowerStateString[3][7]={"off\0","on\0","cycled\0"}; + GError *error = NULL; + GOptionContext *context; /* // Print out the Program name and Version */ oh_prog_version(argv[0], OH_SVN_REV); - /* Set Program Defaults */ - ComputerNumber = 0; - SelectedSystem = 0; - Action = 255; //set it out of range to stand for status - BladeSelected = FALSE; - MultipleBlades = FALSE; - ActionSelected = FALSE; - PrintUsage = FALSE; - DebugPrints = FALSE; - RptEntry = SAHPI_FIRST_ENTRY; /* Parse out option instructions */ - while (1) - { - option = getopt(argc, argv, "D:dpruxb:"); - if ((option == EOF) || (PrintUsage == TRUE)) - { - break; //break out of the while loop - } + context = g_option_context_new ("- Exercise HPI Power Management APIs"); + g_option_context_add_main_entries (context, my_options, NULL); - switch (option) - { - case 'D': - if (optarg) domainid = atoi(optarg); - else { - printf("hpipower: option requires an argument -- D"); - PrintUsage = TRUE; - } - break; - case 'd': - Action = SAHPI_POWER_OFF; - ActionSelected = TRUE; - break; - case 'p': - Action = SAHPI_POWER_ON; - ActionSelected = TRUE; - break; - case 'r': - Action = SAHPI_POWER_CYCLE; - ActionSelected = TRUE; - break; - case 'u': - BladeSelected = TRUE; - ActionSelected = TRUE; - break; - case 'x': - DebugPrints = TRUE; - break; - case 'b': - if (*optarg == 0) - { - printf("hpipower: option requires an argument -- b"); - PrintUsage = TRUE; - break; //no argument - } - SelectedSystem = atoi(optarg) - 1; //Normalizing to 0...n-1 - if ((SelectedSystem > MAX_MANAGED_SYSTEMS) || - (SelectedSystem < 0)) - { - //Argument is out of Range - PrintUsage = TRUE; - } - BladeSelected = TRUE; - break; - default: - PrintUsage = TRUE; - break; - } //end of switch statement - } //end of argument parsing while loop - - if (PrintUsage == TRUE) - { - UsageMessage(argv[0]); - exit(1); //When we exit here, there is nothing to clean up + if (!ohc_option_parse(&argc, argv, + context, &copt, + OHC_ALL_OPTIONS + - OHC_ENTITY_PATH_OPTION //TODO: Feature 880127 + - OHC_VERBOSE_OPTION, // no verbose mode implemented + error)) { + g_print ("option parsing failed: %s\n", error->message); + exit(1); + } + if (f_down) { + Action = SAHPI_POWER_OFF; + ActionSelected = TRUE; + } + if (f_up) { + Action = SAHPI_POWER_ON; + ActionSelected = TRUE; + } + if (f_reset) { + Action = SAHPI_POWER_CYCLE; + ActionSelected = TRUE; + } + if (f_unattended) { + BladeSelected = TRUE; + ActionSelected = TRUE; + } + if (f_blade > 0) { + BladeSelected = TRUE; + SelectedSystem = f_blade - 1; //Normalizing to 0...n-1 + if ((SelectedSystem > MAX_MANAGED_SYSTEMS) || + (SelectedSystem < 0)) { + printf("hpipower: blade number out of r... [truncated message content] |