1 /***************************************************************************
3 * probe-printer.c : Probe for prnio(7i) printer device information
5 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
6 * Use is subject to license terms.
8 * Licensed under the Academic Free License version 2.1
10 **************************************************************************/
12 #pragma ident "%Z%%M% %I% %E% SMI"
24 #include <sys/param.h>
25 #include <sys/types.h>
27 #include <sys/ioctl.h>
28 #include <sys/prnio.h>
39 prnio_printer_info(char *device_file
, char **manufacturer
, char **model
,
40 char **description
, char **serial_number
, char ***command_set
)
42 struct prn_1284_device_id id
;
46 memset(&id
, 0, sizeof (id
));
47 memset(&buf
, 0, sizeof (buf
));
49 id
.id_len
= sizeof (buf
);
51 if ((fd
= open (device_file
, O_RDONLY
| O_NONBLOCK
)) < 0) {
55 if (ioctl(fd
, PRNIOC_GET_1284_DEVID
, &id
) < 0) {
59 HAL_DEBUG(("IEEE-1284 DeviceId = %s", buf
));
61 rc
= ieee1284_devid_to_printer_info(buf
, manufacturer
, model
,
62 description
, NULL
, serial_number
, command_set
);
72 * It is assumed that all devices that support prnio(7i), also have a link
76 prnio_device_name(void)
82 if (((devfs_path
= getenv("HAL_PROP_SOLARIS_DEVFS_PATH")) != NULL
) &&
83 ((dp
= opendir("/dev/printers")) != NULL
)) {
86 while ((ep
= readdir(dp
)) != NULL
) {
87 char path
[MAXPATHLEN
], lpath
[MAXPATHLEN
];
89 snprintf(path
, sizeof (path
), "/dev/printers/%s",
91 memset(lpath
, 0, sizeof (lpath
));
92 if ((readlink(path
, lpath
, sizeof (lpath
)) > 0) &&
93 (strstr(lpath
, devfs_path
) != NULL
)) {
94 result
= strdup(path
);
105 main(int argc
, char *argv
[])
110 char *manufacturer
= NULL
,
112 *serial_number
= NULL
,
114 **command_set
= NULL
;
116 LibHalContext
*ctx
= NULL
;
117 LibHalChangeSet
*cs
= NULL
;
119 if ((udi
= getenv("UDI")) == NULL
)
121 if ((device_file
= getenv("HAL_PROP_PRINTER_DEVICE")) == NULL
)
122 device_file
= prnio_device_name();
124 if (device_file
== NULL
)
129 dbus_error_init(&error
);
130 if ((ctx
= libhal_ctx_init_direct(&error
)) == NULL
)
133 if ((cs
= libhal_device_new_changeset(udi
)) == NULL
) {
134 HAL_DEBUG(("Cannot allocate changeset"));
138 /* Probe the printer for characteristics via prnio(7i) */
139 ret
= prnio_printer_info(device_file
, &manufacturer
, &model
,
140 &description
, &serial_number
, &command_set
);
142 HAL_DEBUG(("Cannot get prnio data for %s: %s",
143 device_file
, strerror(errno
)));
147 /* Add printer characteristics to the HAL device tree */
148 ret
= add_printer_info(cs
, udi
, manufacturer
, model
, description
,
149 serial_number
, command_set
, device_file
);
151 HAL_DEBUG(("Cannot add printer data for %s to %s: %s",
152 device_file
, udi
, strerror(errno
)));
156 libhal_device_commit_changeset(ctx
, cs
, &error
);
162 libhal_device_free_changeset(cs
);
166 if (dbus_error_is_set(&error
)) {
167 dbus_error_free(&error
);
169 libhal_ctx_shutdown(ctx
, &error
);
170 libhal_ctx_free(ctx
);