#include "backend-private.h"
#include <cups/cups-private.h>
int
backendGetDeviceID(
int fd,
char *device_id,
int device_id_size,
char *make_model,
int make_model_size,
const char *scheme,
char *uri,
int uri_size)
{
#ifdef __APPLE__
(void)fd;
(void)device_id;
(void)device_id_size;
(void)make_model;
(void)make_model_size;
(void)scheme;
(void)uri;
(void)uri_size;
return (-1);
#else
# ifdef __linux
int length;
int got_id = 0;
# endif
# if defined(__sun) && defined(ECPPIOC_GETDEVID)
struct ecpp_device_id did;
# endif
char *ptr;
DEBUG_printf(("backendGetDeviceID(fd=%d, device_id=%p, device_id_size=%d, "
"make_model=%p, make_model_size=%d, scheme=\"%s\", "
"uri=%p, uri_size=%d)\n", fd, device_id, device_id_size,
make_model, make_model_size, scheme ? scheme : "(null)",
uri, uri_size));
if (!device_id || device_id_size < 32)
{
DEBUG_puts("backendGetDeviceID: Bad args!");
return (-1);
}
if (make_model)
*make_model = '\0';
if (fd >= 0)
{
*device_id = '\0';
# ifdef __linux
if (ioctl(fd, LPIOC_GET_DEVICE_ID(device_id_size), device_id))
{
if (uri && !strncmp(uri, "parallel:/dev/", 14))
{
char devparport[16];
int devparportfd,
mode;
snprintf(devparport, sizeof(devparport), "/dev/parport%s",
uri + strlen(uri) - 1);
if ((devparportfd = open(devparport, O_RDWR | O_NOCTTY)) != -1)
{
if (!ioctl(devparportfd, PPCLAIM))
{
fcntl(devparportfd, F_SETFL, fcntl(devparportfd, F_GETFL) | O_NONBLOCK);
mode = IEEE1284_MODE_COMPAT;
if (!ioctl(devparportfd, PPNEGOT, &mode))
{
mode = IEEE1284_MODE_NIBBLE | IEEE1284_DEVICEID;
if (!ioctl(devparportfd, PPNEGOT, &mode))
{
if ((length = read(devparportfd, device_id,
device_id_size - 1)) >= 2)
{
device_id[length] = '\0';
got_id = 1;
}
}
}
ioctl(devparportfd, PPRELEASE);
}
close(devparportfd);
}
}
}
else
got_id = 1;
if (got_id)
{
length = (((unsigned)device_id[0] & 255) << 8) +
((unsigned)device_id[1] & 255);
if (length > device_id_size || length < 14)
length = (((unsigned)device_id[1] & 255) << 8) +
((unsigned)device_id[0] & 255);
if (length > device_id_size)
length = device_id_size;
if (length < 14)
{
device_id[0] = '\0';
got_id = 0;
}
else
{
length -= 2;
memmove(device_id, device_id + 2, length);
device_id[length] = '\0';
}
}
else
{
DEBUG_printf(("backendGetDeviceID: ioctl failed - %s\n",
strerror(errno)));
*device_id = '\0';
}
# endif
# if defined(__sun) && defined(ECPPIOC_GETDEVID)
did.mode = ECPP_CENTRONICS;
did.len = device_id_size - 1;
did.rlen = 0;
did.addr = device_id;
if (!ioctl(fd, ECPPIOC_GETDEVID, &did))
{
if (did.rlen < (device_id_size - 1))
device_id[did.rlen] = '\0';
else
device_id[device_id_size - 1] = '\0';
}
# ifdef DEBUG
else
DEBUG_printf(("backendGetDeviceID: ioctl failed - %s\n",
strerror(errno)));
# endif
# endif
}
for (ptr = device_id; *ptr; ptr ++)
if (_cups_isspace(*ptr))
*ptr = ' ';
else if ((*ptr & 255) < ' ' || *ptr == 127)
{
DEBUG_printf(("backendGetDeviceID: Bad device_id character %d.",
*ptr & 255));
*device_id = '\0';
break;
}
DEBUG_printf(("backendGetDeviceID: device_id=\"%s\"\n", device_id));
if (scheme && uri)
*uri = '\0';
if (!*device_id)
return (-1);
if (make_model)
backendGetMakeModel(device_id, make_model, make_model_size);
if (scheme && uri && uri_size > 32)
{
int num_values;
cups_option_t *values;
const char *mfg,
*mdl,
*sern;
char temp[256],
*tempptr;
num_values = _cupsGet1284Values(device_id, &values);
if ((sern = cupsGetOption("SERIALNUMBER", num_values, values)) == NULL)
if ((sern = cupsGetOption("SERN", num_values, values)) == NULL)
sern = cupsGetOption("SN", num_values, values);
if ((mfg = cupsGetOption("MANUFACTURER", num_values, values)) == NULL)
mfg = cupsGetOption("MFG", num_values, values);
if ((mdl = cupsGetOption("MODEL", num_values, values)) == NULL)
mdl = cupsGetOption("MDL", num_values, values);
if (mfg)
{
if (!_cups_strcasecmp(mfg, "Hewlett-Packard"))
mfg = "HP";
else if (!_cups_strcasecmp(mfg, "Lexmark International"))
mfg = "Lexmark";
}
else
{
strlcpy(temp, make_model, sizeof(temp));
if ((tempptr = strchr(temp, ' ')) != NULL)
*tempptr = '\0';
mfg = temp;
}
if (!mdl)
mdl = "";
if (!_cups_strncasecmp(mdl, mfg, strlen(mfg)))
{
mdl += strlen(mfg);
while (isspace(*mdl & 255))
mdl ++;
}
httpAssembleURIf(HTTP_URI_CODING_ALL, uri, uri_size, scheme, NULL, mfg, 0,
"/%s%s%s", mdl, sern ? "?serial=" : "", sern ? sern : "");
cupsFreeOptions(num_values, values);
}
return (0);
#endif
}
int
backendGetMakeModel(
const char *device_id,
char *make_model,
int make_model_size)
{
int num_values;
cups_option_t *values;
const char *mfg,
*mdl,
*des;
DEBUG_printf(("backendGetMakeModel(device_id=\"%s\", "
"make_model=%p, make_model_size=%d)\n", device_id,
make_model, make_model_size));
if (!device_id || !*device_id || !make_model || make_model_size < 32)
{
DEBUG_puts("backendGetMakeModel: Bad args!");
return (-1);
}
*make_model = '\0';
num_values = _cupsGet1284Values(device_id, &values);
if ((mdl = cupsGetOption("MODEL", num_values, values)) == NULL)
mdl = cupsGetOption("MDL", num_values, values);
if (mdl)
{
if ((mfg = cupsGetOption("MANUFACTURER", num_values, values)) == NULL)
mfg = cupsGetOption("MFG", num_values, values);
if (!mfg || !_cups_strncasecmp(mdl, mfg, strlen(mfg)))
{
_ppdNormalizeMakeAndModel(mdl, make_model, make_model_size);
}
else
{
char temp[1024];
snprintf(temp, sizeof(temp), "%s %s", mfg, mdl);
_ppdNormalizeMakeAndModel(temp, make_model, make_model_size);
}
}
else if ((des = cupsGetOption("DESCRIPTION", num_values, values)) != NULL ||
(des = cupsGetOption("DES", num_values, values)) != NULL)
{
if (strlen(des) >= 8)
{
const char *ptr;
int letters,
spaces;
for (ptr = des, letters = 0, spaces = 0; *ptr; ptr ++)
{
if (isspace(*ptr & 255))
spaces ++;
else if (isalpha(*ptr & 255))
letters ++;
if (spaces && letters)
break;
}
if (spaces && letters)
_ppdNormalizeMakeAndModel(des, make_model, make_model_size);
}
}
if (!make_model[0])
{
strlcpy(make_model, "Unknown", make_model_size);
}
cupsFreeOptions(num_values, values);
return (0);
}