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