1 /*
2 * sample.c
3 *
4 * OpenIPMI sample code
5 *
6 * Author: MontaVista Software
7 * Corey Minyard <cminyard@mvista.com>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2 of
12 * the License, or (at your option) any later version.
13 *
14 *
15 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
16 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
17 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
21 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
23 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
24 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 *
26 * You should have received a copy of the GNU Lesser General Public
27 * License along with this program; if not, write to the Free
28 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 */
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <unistd.h>
35 #include <signal.h>
36
37 #include <OpenIPMI/ipmiif.h>
38 #include <OpenIPMI/ipmi_err.h>
39 #include <OpenIPMI/ipmi_posix.h>
40
41 /* This sample application demostrates a very simple method to use
42 OpenIPMI. It takes a entity id, entity instance, and control name,
43 looks for that sensor to be created, then prints out the value
44 every 5 seconds. */
45
46 static os_handler_t *os_hnd;
47
48 static const char *progname;
49
con_usage(const char * name,const char * help,void * cb_data)50 static void con_usage(const char *name, const char *help, void *cb_data)
51 {
52 printf("\n%s%s", name, help);
53 }
54
55 static void
usage(void)56 usage(void)
57 {
58 printf("Usage:\n");
59 printf(" %s <entity id> <entity instance> <control name> <con_parms>\n",
60 progname);
61 printf(" Where the entity id, instance, and control name identify a\n");
62 printf(" control to monitor, and ");
63 printf(" where <con_parms> is one of:");
64 ipmi_parse_args_iter_help(con_usage, NULL);
65 }
66
67 int control_set = 0;
68 ipmi_control_id_t control_id;
69
70 struct timeval check_timeout;
71 os_hnd_timer_id_t *check_timer;
72
73 int entity_id;
74 int entity_instance;
75 const char *control_name;
76
77 ipmi_domain_id_t domain_id;
78 int leaving = 0;
79
80 void
leave_done(void * cb_data)81 leave_done(void *cb_data)
82 {
83 exit(0);
84 }
85
86 void
leave2(ipmi_domain_t * domain,void * cb_data)87 leave2(ipmi_domain_t *domain, void *cb_data)
88 {
89 ipmi_domain_close(domain, leave_done, NULL);
90 }
91
92 void
leave(void)93 leave(void)
94 {
95 leaving = 1;
96 ipmi_domain_pointer_cb(domain_id, leave2, NULL);
97 }
98
got_val(ipmi_control_t * control,int err,int * val,void * cb_data)99 void got_val(ipmi_control_t *control,
100 int err,
101 int *val,
102 void *cb_data)
103 {
104 int i;
105 int count;
106
107 if (err) {
108 printf("Error reading values\n");
109 exit(1);
110 }
111
112 count = ipmi_control_get_num_vals(control);
113 printf("Value:");
114 for (i=0; i<count; i++) {
115 printf(" %d", val[i]);
116 }
117 printf("\n");
118 }
119
got_id(ipmi_control_t * control,int err,unsigned char * val,int length,void * cb_data)120 void got_id(ipmi_control_t *control,
121 int err,
122 unsigned char *val,
123 int length,
124 void *cb_data)
125 {
126 int i;
127
128 if (err) {
129 printf("Error reading values\n");
130 leave();
131 }
132
133 printf("Value:");
134 for (i=0; i<length; i++) {
135 printf(" %2.2x", val[i]);
136 }
137 printf("\n");
138 }
139
140
141 void
check_control(ipmi_control_t * control,void * cb_data)142 check_control(ipmi_control_t *control, void *cb_data)
143 {
144 int rv = 0;
145
146 switch(ipmi_control_get_type(control))
147 {
148 case IPMI_CONTROL_RELAY:
149 case IPMI_CONTROL_ALARM:
150 case IPMI_CONTROL_RESET:
151 case IPMI_CONTROL_POWER:
152 case IPMI_CONTROL_FAN_SPEED:
153 case IPMI_CONTROL_OUTPUT:
154 case IPMI_CONTROL_LIGHT:
155 rv = ipmi_control_get_val(control, got_val, NULL);
156 break;
157
158 case IPMI_CONTROL_IDENTIFIER:
159 rv = ipmi_control_identifier_get_val(control, got_id, NULL);
160 break;
161
162 default:
163 printf("Invalid control type\n");
164 leave();
165 }
166 if (rv) {
167 printf("Unable to get control val: %x\n", rv);
168 leave();
169 }
170 }
171
172 void
control_check_timeout(void * cb_data,os_hnd_timer_id_t * id)173 control_check_timeout(void *cb_data, os_hnd_timer_id_t *id)
174 {
175 int rv;
176
177 if (control_set) {
178 rv = ipmi_control_pointer_cb(control_id, check_control, NULL);
179 if (rv) {
180 printf("Unable to convert control to pointer: %d\n", rv);
181 leave();
182 }
183 }
184
185 os_hnd->start_timer(os_hnd,
186 check_timer, &check_timeout,
187 control_check_timeout, NULL);
188 }
189
190 /* Whenever the status of a control changes, the function is called
191 We display the information of the control if we find a new control
192 */
193 static void
control_change(enum ipmi_update_e op,ipmi_entity_t * ent,ipmi_control_t * control,void * cb_data)194 control_change(enum ipmi_update_e op,
195 ipmi_entity_t *ent,
196 ipmi_control_t *control,
197 void *cb_data)
198 {
199 int id, instance;
200 char name[33];
201
202 id = ipmi_entity_get_entity_id(ent);
203 instance = ipmi_entity_get_entity_instance(ent);
204 ipmi_control_get_id(control, name, 32);
205 if (op == IPMI_ADDED) {
206 if ((id == entity_id)
207 && (instance == entity_instance)
208 && (strcmp(control_name, name) == 0))
209 {
210 control_id = ipmi_control_convert_to_id(control);
211 control_set = 1;
212 }
213 }
214 }
215
216 /* Whenever the status of an entity changes, the function is called
217 When a new entity is created, we search all controls that belong
218 to the entity */
219 static void
entity_change(enum ipmi_update_e op,ipmi_domain_t * domain,ipmi_entity_t * entity,void * cb_data)220 entity_change(enum ipmi_update_e op,
221 ipmi_domain_t *domain,
222 ipmi_entity_t *entity,
223 void *cb_data)
224 {
225 int rv;
226
227 if (op == IPMI_ADDED) {
228 /* Register callback so that when the status of a
229 control changes, control_change is called */
230 rv = ipmi_entity_add_control_update_handler(entity,
231 control_change,
232 entity);
233 if (rv) {
234 printf("ipmi_entity_add_control_update_handler: 0x%x", rv);
235 leave();
236 }
237 }
238 }
239
240 /* After we have established connection to domain, this function get called
241 At this time, we can do whatever things we want to do. Herr we want to
242 search all entities in the system */
243 void
setup_done(ipmi_domain_t * domain,int err,unsigned int conn_num,unsigned int port_num,int still_connected,void * user_data)244 setup_done(ipmi_domain_t *domain,
245 int err,
246 unsigned int conn_num,
247 unsigned int port_num,
248 int still_connected,
249 void *user_data)
250 {
251 int rv;
252
253 domain_id = ipmi_domain_convert_to_id(domain);
254
255 /* Register a callback functin entity_change. When a new entity
256 is created, entity_change is called */
257 rv = ipmi_domain_add_entity_update_handler(domain, entity_change, domain);
258 if (rv) {
259 printf("ipmi_domain_add_entity_update_handler return error: %d\n", rv);
260 return;
261 }
262 }
263
264 static void
thread(void * data)265 thread(void *data)
266 {
267 os_handler_t *os_hnd = data;
268
269 /* This is the main loop of the event-driven program.
270 Try <CTRL-C> to exit the program */
271 os_hnd->operation_loop(os_hnd);
272 }
273
274 int
main(int argc,char * argv[])275 main(int argc, char *argv[])
276 {
277 int rv;
278 int curr_arg = 4;
279 ipmi_args_t *args;
280 ipmi_con_t *con;
281
282 progname = argv[0];
283
284 /* OS handler allocated first. */
285 os_hnd = ipmi_posix_thread_setup_os_handler(SIGUSR1);
286 if (!os_hnd) {
287 printf("ipmi_smi_setup_con: Unable to allocate os handler\n");
288 exit(1);
289 }
290
291 /* Use the default log handler. */
292
293 /* Initialize the OpenIPMI library. */
294 ipmi_init(os_hnd);
295
296 if (argc < 4) {
297 usage();
298 exit(1);
299 }
300
301 entity_id = strtoul(argv[1], NULL, 10);
302 entity_instance = strtoul(argv[2], NULL, 10);
303 control_name = argv[3];
304
305 rv = ipmi_parse_args2(&curr_arg, argc, argv, &args);
306 if (rv) {
307 fprintf(stderr, "Error parsing command arguments, argument %d: %s\n",
308 curr_arg, strerror(rv));
309 usage();
310 exit(1);
311 }
312
313 rv = ipmi_args_setup_con(args, os_hnd, NULL, &con);
314 if (rv) {
315 fprintf(stderr, "ipmi_ip_setup_con: %s", strerror(rv));
316 exit(1);
317 }
318
319 rv = ipmi_open_domain("", &con, 1, setup_done, NULL, NULL, NULL,
320 NULL, 0, NULL);
321 if (rv) {
322 fprintf(stderr, "ipmi_init_domain: %s\n", strerror(rv));
323 exit(1);
324 }
325
326 rv = os_hnd->alloc_timer(os_hnd, &check_timer);
327 if (rv) {
328 fprintf(stderr, "alloc_timer: %x\n", rv);
329 leave();
330 }
331
332 check_timeout.tv_sec = 5;
333 check_timeout.tv_usec = 0;
334 os_hnd->start_timer(os_hnd,
335 check_timer, &check_timeout,
336 control_check_timeout, NULL);
337
338 rv = os_hnd->create_thread(os_hnd, 0, thread, os_hnd);
339 if (rv) {
340 fprintf(stderr, "create_thread: %x\n", rv);
341 exit(1);
342 }
343
344 rv = os_hnd->create_thread(os_hnd, 0, thread, os_hnd);
345 if (rv) {
346 fprintf(stderr, "create_thread 2: %x\n", rv);
347 exit(1);
348 }
349
350 /* Let the other threads take over. */
351
352 for (;;)
353 sleep(100);
354 /* Note that at cleanup time, you should call
355 os_hnd->free_os_handler(os_hnd);
356 but this doesn't have any cleanup.
357 */
358 return 0;
359 }
360