File: hpi_cmd.c

package info (click to toggle)
openhpi 3.6.1-2.2
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 25,544 kB
  • ctags: 25,275
  • sloc: ansic: 202,885; cpp: 63,639; java: 16,472; cs: 15,161; python: 11,884; makefile: 4,872; perl: 1,529; sh: 371; xml: 36; asm: 13
file content (183 lines) | stat: -rw-r--r-- 5,096 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*      -*- linux-c -*-
 *
 * Copyright (c) 2004 by Intel 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
 * file and program are licensed under a BSD style license.  See
 * the Copying file included with the OpenHPI distribution for
 * full licensing terms.
 *
 * Authors:
 *     Racing Guo <racing.guo@intel.com>
 *
 * Changes:
 *	11.30.2004 - Kouzmich: porting to HPI-B
 *  28.10.2010 - Anton Pak: fixed -c command line argument
 *  28.10.2010 - Anton Pak: added -D command line argument
 *  10.02.2011 - Ulrich Kleber: Refactoring to use glib for option parsing and
 *                           introduce common options for all clients
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "hpi_cmd.h"
#include <oh_clients.h>

#define OH_SVN_REV "$Revision: 7428 $"

int	debug_flag = 0;
static gchar *f_cmdfile = NULL;
static gboolean f_events = FALSE;
static oHpiCommonOptionsT copt;

static GOptionEntry my_options[] =
{
  { "cmdfile", 'f', 0, G_OPTION_ARG_FILENAME, &f_cmdfile, "Execute command file",                        "filename"   },
  { "events",  'e', 0, G_OPTION_ARG_NONE,     &f_events,  "Show short events, discover after subscribe", NULL },
  { NULL }
};


int main(int argc, char **argv)
{
        GOptionContext *context;

        /* Print version strings */
        oh_prog_version(argv[0]);

        /* Parsing options */
        static char usetext[]="- Allows a user to interactively "
                              "perform a number of HPI operations\n  "
                              OH_SVN_REV; 
        OHC_PREPARE_REVISION(usetext);
        context = g_option_context_new (usetext);
        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 // not applicable
                    - OHC_VERBOSE_OPTION )) {    // no verbose mode implemented
                g_option_context_free (context);
		exit(1);
	}
        if (copt.debug) debug_flag = 1;
        g_option_context_free (context);
        if (f_cmdfile) {
                open_file ( f_cmdfile );
                g_free ( f_cmdfile );
        }

	domainlist = (GSList *)NULL;
	if (open_session(copt.domainid, f_events) == -1) 
                    //TODO For complete implementation of -N option, need to call 
                    //TODO ohAddDomain to get the domain Id.
                                                 
		return(1);
	cmd_shell();
	close_session();
	return 0;
}

ret_code_t ask_entity(SaHpiEntityPathT *ret)
{
	term_def_t	*term;
	int	res;
    SaErrorT rv;
    char buf[256];
    const char * epstr;

	term = get_next_term();
	if (term == NULL) {
        SaHpiEntityPathT root;
        root.Entry[0].EntityType = SAHPI_ENT_ROOT;
        root.Entry[0].EntityLocation = 0;

		if (read_file) return(HPI_SHELL_PARM_ERROR);
		rv = show_entity_tree(Domain, &root, 0, ui_print);
		if (rv != SA_OK) {
			printf("NO entities!\n");
			return(HPI_SHELL_CMD_ERROR);
		};
		res = get_string_param("Entity Path ==> ", buf, sizeof(buf));
		if (res != 0) {
            printf("Invalid entity path");
            return(HPI_SHELL_PARM_ERROR);
        }
        epstr = buf;
	} else {
        epstr = term->term;
	};

    rv = oh_encode_entitypath(epstr, ret); 
    if ( rv != SA_OK ) {
        printf("Invalid entity path");
        return(HPI_SHELL_PARM_ERROR);
    }

	return(HPI_SHELL_OK);
}

ret_code_t ask_rpt(SaHpiResourceIdT *ret)
{
	term_def_t	*term;
	int		i, res;

	term = get_next_term();
	if (term == NULL) {
		if (read_file) return(HPI_SHELL_PARM_ERROR);
		i = show_rpt_list(Domain, SHOW_ALL_RPT, 0, SHORT_LSRES, ui_print);
		if (i == 0) {
			printf("NO rpts!\n");
			return(HPI_SHELL_CMD_ERROR);
		};
		i = get_int_param("RPT ID ==> ", &res);
		if (i == 1) *ret = (SaHpiResourceIdT)res;
		else return(HPI_SHELL_PARM_ERROR);
	} else {
		*ret = (SaHpiResourceIdT)atoi(term->term);
	};
	return(HPI_SHELL_OK);
}

ret_code_t ask_rdr(SaHpiResourceIdT rptid, SaHpiRdrTypeT type, SaHpiInstrumentIdT *ret)
{
	term_def_t	*term;
	int		i, res;
	char            buf[64];

	strncpy(buf, oh_lookup_rdrtype(type), 64);
	buf[strlen(buf)-4] = '\0';
	strncat(buf, " NUM ==> ", 64-strlen(buf));
	term = get_next_term();
	if (term == NULL) {
		if (read_file) return(HPI_SHELL_CMD_ERROR);
		i = show_rdr_list(Domain, rptid, type, ui_print);
		if (i == 0) {
			printf("No rdrs for rpt: %d\n", rptid);
			return(HPI_SHELL_CMD_ERROR);
		};
		i = get_int_param(buf, &res);
		if (i != 1) return(HPI_SHELL_PARM_ERROR);
		*ret = (SaHpiInstrumentIdT)res;
	} else {
		*ret = (SaHpiInstrumentIdT)atoi(term->term);
	};
	return(HPI_SHELL_OK);
}

ret_code_t open_file(char *path)
{
	if (add_input_file(path) != 0) {
		printf("Can not run file: %s\n", path);
		return(HPI_SHELL_PARM_ERROR);
	};
	read_file = 1;
	read_stdin = 0;
	return(HPI_SHELL_OK);
}