13536Sjacobs /***************************************************************************
23536Sjacobs *
33536Sjacobs * probe-printer.c : Probe for prnio(7i) printer device information
43536Sjacobs *
53536Sjacobs * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
63536Sjacobs * Use is subject to license terms.
73536Sjacobs *
83536Sjacobs * Licensed under the Academic Free License version 2.1
93536Sjacobs *
103536Sjacobs **************************************************************************/
113536Sjacobs
123536Sjacobs #pragma ident "%Z%%M% %I% %E% SMI"
133536Sjacobs
143536Sjacobs #ifdef HAVE_CONFIG_H
15*5307Sjacobs #include <config.h>
163536Sjacobs #endif
173536Sjacobs
183536Sjacobs #include <errno.h>
193536Sjacobs #include <string.h>
203536Sjacobs #include <strings.h>
213536Sjacobs #include <ctype.h>
223536Sjacobs #include <stdlib.h>
233536Sjacobs #include <stdio.h>
24*5307Sjacobs #include <sys/param.h>
25*5307Sjacobs #include <sys/types.h>
26*5307Sjacobs #include <dirent.h>
273536Sjacobs #include <sys/ioctl.h>
283536Sjacobs #include <sys/prnio.h>
293536Sjacobs #include <fcntl.h>
303536Sjacobs #include <unistd.h>
313536Sjacobs #include <ctype.h>
323536Sjacobs
333536Sjacobs #include <libhal.h>
343536Sjacobs #include <logger.h>
353536Sjacobs
36*5307Sjacobs #include "printer.h"
373536Sjacobs
383536Sjacobs static int
prnio_printer_info(char * device_file,char ** manufacturer,char ** model,char ** description,char ** serial_number,char *** command_set)39*5307Sjacobs prnio_printer_info(char *device_file, char **manufacturer, char **model,
40*5307Sjacobs char **description, char **serial_number, char ***command_set)
413536Sjacobs {
423536Sjacobs struct prn_1284_device_id id;
433536Sjacobs char buf[BUFSIZ];
44*5307Sjacobs int fd = -1, rc = -1;
453536Sjacobs
463536Sjacobs memset(&id, 0, sizeof (id));
473536Sjacobs memset(&buf, 0, sizeof (buf));
483536Sjacobs id.id_data = buf;
493536Sjacobs id.id_len = sizeof (buf);
503536Sjacobs
51*5307Sjacobs if ((fd = open (device_file, O_RDONLY | O_NONBLOCK)) < 0) {
52*5307Sjacobs goto prnio_out;
53*5307Sjacobs }
54*5307Sjacobs
553536Sjacobs if (ioctl(fd, PRNIOC_GET_1284_DEVID, &id) < 0) {
56*5307Sjacobs goto prnio_out;
573536Sjacobs }
583536Sjacobs
59*5307Sjacobs HAL_DEBUG(("IEEE-1284 DeviceId = %s", buf));
603536Sjacobs
61*5307Sjacobs rc = ieee1284_devid_to_printer_info(buf, manufacturer, model,
62*5307Sjacobs description, NULL, serial_number, command_set);
633536Sjacobs
64*5307Sjacobs prnio_out:
65*5307Sjacobs if (fd != -1)
66*5307Sjacobs close(fd);
673536Sjacobs
68*5307Sjacobs return (rc);
69*5307Sjacobs }
703536Sjacobs
71*5307Sjacobs /*
72*5307Sjacobs * It is assumed that all devices that support prnio(7i), also have a link
73*5307Sjacobs * in /dev/printers.
74*5307Sjacobs */
75*5307Sjacobs static char *
prnio_device_name(void)76*5307Sjacobs prnio_device_name(void)
77*5307Sjacobs {
78*5307Sjacobs char *result = NULL;
79*5307Sjacobs char *devfs_path;
80*5307Sjacobs DIR *dp;
813536Sjacobs
82*5307Sjacobs if (((devfs_path = getenv("HAL_PROP_SOLARIS_DEVFS_PATH")) != NULL) &&
83*5307Sjacobs ((dp = opendir("/dev/printers")) != NULL)) {
84*5307Sjacobs struct dirent *ep;
85*5307Sjacobs
86*5307Sjacobs while ((ep = readdir(dp)) != NULL) {
87*5307Sjacobs char path[MAXPATHLEN], lpath[MAXPATHLEN];
88*5307Sjacobs
89*5307Sjacobs snprintf(path, sizeof (path), "/dev/printers/%s",
90*5307Sjacobs ep->d_name);
91*5307Sjacobs memset(lpath, 0, sizeof (lpath));
92*5307Sjacobs if ((readlink(path, lpath, sizeof (lpath)) > 0) &&
93*5307Sjacobs (strstr(lpath, devfs_path) != NULL)) {
94*5307Sjacobs result = strdup(path);
95*5307Sjacobs break;
963536Sjacobs }
973536Sjacobs }
98*5307Sjacobs closedir(dp);
993536Sjacobs }
1003536Sjacobs
101*5307Sjacobs return (result);
1023536Sjacobs }
1033536Sjacobs
104*5307Sjacobs int
main(int argc,char * argv[])105*5307Sjacobs main(int argc, char *argv[])
1063536Sjacobs {
1073536Sjacobs int ret = 1;
1083536Sjacobs char *udi;
1093536Sjacobs char *device_file;
110*5307Sjacobs char *manufacturer = NULL,
111*5307Sjacobs *model = NULL,
112*5307Sjacobs *serial_number = NULL,
113*5307Sjacobs *description = NULL,
114*5307Sjacobs **command_set = NULL;
1153536Sjacobs DBusError error;
1163536Sjacobs LibHalContext *ctx = NULL;
1173536Sjacobs LibHalChangeSet *cs = NULL;
1183536Sjacobs
119*5307Sjacobs if ((udi = getenv("UDI")) == NULL)
1203536Sjacobs goto out;
121*5307Sjacobs if ((device_file = getenv("HAL_PROP_PRINTER_DEVICE")) == NULL)
122*5307Sjacobs device_file = prnio_device_name();
123*5307Sjacobs
124*5307Sjacobs if (device_file == NULL)
1253536Sjacobs goto out;
1263536Sjacobs
127*5307Sjacobs setup_logger();
1283536Sjacobs
129*5307Sjacobs dbus_error_init(&error);
130*5307Sjacobs if ((ctx = libhal_ctx_init_direct(&error)) == NULL)
1313536Sjacobs goto out;
1323536Sjacobs
133*5307Sjacobs if ((cs = libhal_device_new_changeset(udi)) == NULL) {
134*5307Sjacobs HAL_DEBUG(("Cannot allocate changeset"));
1353536Sjacobs goto out;
1363536Sjacobs }
1373536Sjacobs
138*5307Sjacobs /* Probe the printer for characteristics via prnio(7i) */
139*5307Sjacobs ret = prnio_printer_info(device_file, &manufacturer, &model,
140*5307Sjacobs &description, &serial_number, &command_set);
141*5307Sjacobs if (ret < 0) {
142*5307Sjacobs HAL_DEBUG(("Cannot get prnio data for %s: %s",
143*5307Sjacobs device_file, strerror(errno)));
1443536Sjacobs goto out;
1453536Sjacobs }
1463536Sjacobs
147*5307Sjacobs /* Add printer characteristics to the HAL device tree */
148*5307Sjacobs ret = add_printer_info(cs, udi, manufacturer, model, description,
149*5307Sjacobs serial_number, command_set, device_file);
150*5307Sjacobs if (ret < 0) {
151*5307Sjacobs HAL_DEBUG(("Cannot add printer data for %s to %s: %s",
152*5307Sjacobs device_file, udi, strerror(errno)));
1533536Sjacobs goto out;
1543536Sjacobs }
1553536Sjacobs
156*5307Sjacobs libhal_device_commit_changeset(ctx, cs, &error);
1573536Sjacobs
1583536Sjacobs ret = 0;
1593536Sjacobs
1603536Sjacobs out:
1613536Sjacobs if (cs != NULL) {
162*5307Sjacobs libhal_device_free_changeset(cs);
1633536Sjacobs }
1643536Sjacobs
1653536Sjacobs if (ctx != NULL) {
1663536Sjacobs if (dbus_error_is_set(&error)) {
167*5307Sjacobs dbus_error_free(&error);
1683536Sjacobs }
169*5307Sjacobs libhal_ctx_shutdown(ctx, &error);
170*5307Sjacobs libhal_ctx_free(ctx);
1713536Sjacobs }
1723536Sjacobs
173*5307Sjacobs return (ret);
1743536Sjacobs }
175