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