xref: /illumos-gate/usr/src/cmd/hal/probing/network-printer/probe-network-printer.c (revision 2a8bcb4efb45d99ac41c94a75c396b362c414f7f)
1 /***************************************************************************
2  *
3  * probe-network-printer.c : Probe for snmp printer device information
4  *
5  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
6  * Use is subject to license terms.
7  *
8  * Licensed under the Academic Free License version 2.1
9  *
10  **************************************************************************/
11 
12 #ifdef HAVE_CONFIG_H
13 #  include <config.h>
14 #endif
15 
16 #include <errno.h>
17 #include <string.h>
18 #include <strings.h>
19 #include <ctype.h>
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <sys/ioctl.h>
23 #include <sys/prnio.h>
24 #include <fcntl.h>
25 #include <unistd.h>
26 #include <ctype.h>
27 
28 #include <libhal.h>
29 #include <logger.h>
30 
31 #include "printer.h"
32 
33 int
main(int argc,char * argv[])34 main(int argc, char *argv[])
35 {
36 	int ret = 1;
37 	char *udi;
38 	char *printer_address,
39 	     *community;
40 	DBusError error;
41 	LibHalContext *ctx = NULL;
42 	LibHalChangeSet *cs = NULL;
43 	char *manufacturer = NULL,
44 	     *model = NULL,
45 	     *serial_number = NULL,
46 	     *description = NULL,
47 	     **command_set = NULL,
48 	     *device_uri = NULL;
49 	extern int snmp_printer_info(char *hostname, char *community,
50 			char **manufacturer, char **model, char **description,
51 			char **serial_number, char ***command_set,
52 			char **device_uri);
53 
54 	dbus_error_init(&error);
55 
56 	if ((udi = getenv("UDI")) == NULL)
57 		goto out;
58 
59 	printer_address = getenv("HAL_PROP_NETWORK_DEVICE_ADDRESS");
60 	if (printer_address == NULL)
61 		goto out;
62 
63 	community = getenv("HAL_PROP_NETWORK_DEVICE_SNMP_COMMUNITY");
64 	if (community == NULL)
65 		community = "public";
66 
67 	setup_logger();
68 
69 	dbus_error_init(&error);
70 
71 	if ((ctx = libhal_ctx_init_direct(&error)) == NULL)
72 		goto out;
73 
74 	if ((cs = libhal_device_new_changeset(udi)) == NULL) {
75 		HAL_DEBUG(("Cannot allocate changeset"));
76 		goto out;
77 	}
78 
79 	/* Probe the printer for characteristics via SNMP */
80 	ret = snmp_printer_info(printer_address, community, &manufacturer,
81 			&model, &description, &serial_number, &command_set,
82 			&device_uri);
83 	if (ret < 0) {
84 		HAL_DEBUG(("Cannot get snmp data for %s: %s",
85 				printer_address, strerror(errno)));
86 		goto out;
87 	}
88 
89 	/* Add printer characteristics to the HAL device tree */
90 	ret = add_printer_info(cs, udi, manufacturer, model, description,
91 			serial_number, command_set, device_uri);
92 	if (ret < 0) {
93 		HAL_DEBUG(("Cannot add printer data for %s to %s: %s",
94 				printer_address, udi, strerror(errno)));
95 		goto out;
96 	}
97 
98 	libhal_device_commit_changeset(ctx, cs, &error);
99 
100 	ret = 0;
101 
102 out:
103 	if (cs != NULL) {
104 		libhal_device_free_changeset(cs);
105 	}
106 
107 	if (ctx != NULL) {
108 		if (dbus_error_is_set(&error)) {
109 			dbus_error_free(&error);
110 		}
111 		libhal_ctx_shutdown(ctx, &error);
112 		libhal_ctx_free(ctx);
113 	}
114 
115 	return (ret);
116 }
117