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