4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
38 #define DEFAULT_NAME "system"
40 typedef struct locator_info
{
41 int (*locator_func
)(picl_nodehdl_t
, struct locator_info
*);
42 int found
; /* Nonzero if found during walk */
43 int err
; /* Last error from picl */
44 char *name
; /* Name/LocatorName of locator node */
45 int new_state
; /* 0 = logical off, 1 = logical on */
46 char *on
; /* Logical on value for State */
47 char *off
; /* Logical off value for State */
51 usage(char *prog_name
)
53 (void) fprintf(stderr
, gettext("usage: %s [-n | -f]\n"), prog_name
);
58 change_locator_state(picl_nodehdl_t locator_node
, locator_info_t
*locator_info
)
60 picl_prophdl_t state_prop
;
61 char state
[PICL_PROPNAMELEN_MAX
];
65 err
= picl_get_prop_by_name(locator_node
, "State", &state_prop
);
66 if (err
!= PICL_SUCCESS
) {
67 (void) fprintf(stderr
,
68 gettext("picl_get_prop_by_name failed: %s\n"),
73 err
= picl_get_propval(state_prop
, state
, sizeof (state
));
74 if (err
!= PICL_SUCCESS
) {
75 (void) fprintf(stderr
,
76 gettext("picl_get_propval failed: %s\n"),
81 new_state
= (locator_info
->new_state
) ? locator_info
->on
:
84 if (strcmp(state
, new_state
) != 0) {
85 picl_propinfo_t prop_info
;
86 err
= picl_get_propinfo(state_prop
, &prop_info
);
87 if (err
!= PICL_SUCCESS
) {
88 (void) fprintf(stderr
,
89 gettext("picl_get_propinfo failed: %s\n"),
93 err
= picl_set_propval(state_prop
, new_state
, prop_info
.size
);
94 if (err
!= PICL_SUCCESS
) {
95 (void) fprintf(stderr
,
96 gettext("picl_set_propval failed: %s\n"),
105 display_locator_state(picl_nodehdl_t locator_node
,
106 locator_info_t
*locator_info
)
108 char state
[PICL_PROPNAMELEN_MAX
];
112 err
= picl_get_propval_by_name(locator_node
, "State",
113 state
, sizeof (state
));
114 if (err
!= PICL_SUCCESS
) {
115 (void) fprintf(stderr
,
116 gettext("picl_get_propval_by_name failed: %s\n"),
121 if (strcmp(state
, locator_info
->on
) == 0)
122 display_state
= gettext("on");
123 else if (strcmp(state
, locator_info
->off
) == 0)
124 display_state
= gettext("off");
126 display_state
= state
;
128 (void) printf(gettext("The '%s' locator is %s.\n"),
129 locator_info
->name
, display_state
);
134 locator_walker_func(picl_nodehdl_t nodeh
, void *arg
)
136 locator_info_t
*locator_info
= (locator_info_t
*)arg
;
138 char is_locator
[PICL_PROPNAMELEN_MAX
];
139 char name
[PICL_PROPNAMELEN_MAX
];
140 char locator_on
[PICL_PROPNAMELEN_MAX
];
141 char locator_off
[PICL_PROPNAMELEN_MAX
];
143 err
= picl_get_propval_by_name(nodeh
, "IsLocator", is_locator
,
144 sizeof (is_locator
));
146 if (err
!= PICL_SUCCESS
)
147 return (PICL_WALK_CONTINUE
);
149 if (strcmp(is_locator
, "true") != 0)
150 return (PICL_WALK_CONTINUE
);
152 err
= picl_get_propval_by_name(nodeh
, "LocatorName", name
,
155 if (err
== PICL_PROPNOTFOUND
)
156 err
= picl_get_propval_by_name(nodeh
, PICL_PROP_NAME
, name
,
159 if (err
!= PICL_SUCCESS
)
162 if (strcmp(name
, locator_info
->name
) != 0)
163 return (PICL_WALK_CONTINUE
);
165 err
= picl_get_propval_by_name(nodeh
, "LocatorOn", locator_on
,
166 sizeof (locator_on
));
168 if (err
== PICL_SUCCESS
) {
169 locator_info
->on
= locator_on
;
170 } else if (err
== PICL_PROPNOTFOUND
) {
171 locator_info
->on
= "ON";
176 err
= picl_get_propval_by_name(nodeh
, "LocatorOff", locator_off
,
177 sizeof (locator_off
));
179 if (err
== PICL_SUCCESS
) {
180 locator_info
->off
= locator_off
;
181 } else if (err
== PICL_PROPNOTFOUND
) {
182 locator_info
->off
= "OFF";
187 locator_info
->err
= (locator_info
->locator_func
)(nodeh
,
189 locator_info
->found
= 1;
191 return (PICL_WALK_TERMINATE
);
195 main(int argc
, char **argv
)
197 locator_info_t locator_info
= {0, 0, 0, 0, 0};
198 picl_nodehdl_t rooth
;
204 char *locator_name
= DEFAULT_NAME
;
206 (void) setlocale(LC_ALL
, "");
207 (void) textdomain(TEXT_DOMAIN
);
209 if ((progname
= strrchr(argv
[0], '/')) == NULL
)
214 while ((c
= getopt(argc
, argv
, "nf")) != EOF
) {
231 /* We only take one option */
232 if (on_flag
&& off_flag
)
235 err
= picl_initialize();
236 if (err
!= PICL_SUCCESS
) {
237 (void) fprintf(stderr
, gettext("picl_initialize failed: %s\n"),
242 err
= picl_get_root(&rooth
);
243 if (err
!= PICL_SUCCESS
) {
244 (void) fprintf(stderr
, gettext("picl_get_root failed: %s\n"),
251 locator_info
.locator_func
= change_locator_state
;
252 locator_info
.new_state
= 1;
253 } else if (off_flag
) {
254 locator_info
.locator_func
= change_locator_state
;
255 locator_info
.new_state
= 0;
257 locator_info
.locator_func
= display_locator_state
;
260 locator_info
.name
= locator_name
;
262 err
= picl_walk_tree_by_class(rooth
, "led", &locator_info
,
263 locator_walker_func
);
264 if (err
!= PICL_SUCCESS
) {
265 (void) fprintf(stderr
,
266 gettext("picl_walk_tree_by_class failed: %s\n"),
272 if (locator_info
.found
== 0) {
273 (void) fprintf(stderr
, gettext("'%s' locator not found\n"),
277 if (locator_info
.err
!= PICL_SUCCESS
)
280 (void) picl_shutdown();