- added support for dell1600n_net.conf

- removed tmpfile usage (including overly large buffer)
- fixed the indentation horrors inflicted by auto-formatting tools
merge-requests/1/head
Jon Chambers 2006-08-12 12:45:11 +00:00
rodzic 98339fa803
commit b7109db2eb
1 zmienionych plików z 536 dodań i 544 usunięć

Wyświetl plik

@ -42,10 +42,6 @@
Dell 1600n network scan driver for SANE. Dell 1600n network scan driver for SANE.
NOTE
- need to add support for multiple network interfaces
To debug: To debug:
SANE_DEBUG_DELL1600N_NET=255 scanimage --verbose 2>scan.errs 1>scan.png SANE_DEBUG_DELL1600N_NET=255 scanimage --verbose 2>scan.errs 1>scan.png
*/ */
@ -60,6 +56,7 @@
#define BACKEND_NAME dell1600n_net #define BACKEND_NAME dell1600n_net
#include "../include/sane/sanei_backend.h" #include "../include/sane/sanei_backend.h"
#include "../include/sane/sanei_config.h"
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -292,6 +289,10 @@ sane_get_devices (const SANE_Device *** device_list,
fd_set readFds; fd_set readFds;
struct timeval selTimeVal; struct timeval selTimeVal;
int nread, iNextDevice; int nread, iNextDevice;
FILE *fConfig;
char configBuf[ 256 ];
char *pVal;
int valLen;
/* init variables */ /* init variables */
ret = SANE_STATUS_GOOD; ret = SANE_STATUS_GOOD;
@ -302,6 +303,51 @@ sane_get_devices (const SANE_Device *** device_list,
/* clear previous results */ /* clear previous results */
ClearKnownDevices (); ClearKnownDevices ();
iNextDevice = 0;
/* look for a config file */
fConfig = sanei_config_open( "dell1600n_net.conf" );
if ( fConfig )
{
while ( ! feof( fConfig ) )
{
if ( ! sanei_config_read ( configBuf, sizeof( configBuf ), fConfig ) ) break;
/* skip whitespace */
pVal = sanei_config_skip_whitespace ( configBuf );
/* skip comments */
if ( *pVal == '#' ) continue;
/* process named_scanner */
valLen = strlen( "named_scanner:" );
if ( ! strncmp( pVal, "extra_scanner:", valLen ) ){
pVal = sanei_config_skip_whitespace ( pVal + valLen );
pDevice = malloc (sizeof (SANE_Device));
if (!pDevice)
{
DBG (1, "sane_get_devices: memory allocation failure\n");
break;
}
pDevice->name = strdup (pVal);
pDevice->vendor = "Dell";
pDevice->model = strdup( "1600n" );
pDevice->type = "multi-function peripheral";
/* add to list */
gKnownDevices[iNextDevice++] = pDevice;
continue;
} /* if */
} /* while */
/* Close the file */
fclose( fConfig );
}
/* open UDP socket */ /* open UDP socket */
sock = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP); sock = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP);
@ -346,7 +392,6 @@ sane_get_devices (const SANE_Device *** device_list,
} }
/* process replies */ /* process replies */
iNextDevice = 0;
while (select (sock + 1, &readFds, NULL, NULL, &selTimeVal)) while (select (sock + 1, &readFds, NULL, NULL, &selTimeVal))
{ {
@ -367,8 +412,7 @@ sane_get_devices (const SANE_Device *** device_list,
HexDump (10, sockBuf, nread); HexDump (10, sockBuf, nread);
/* process response (skipping bad ones) */ /* process response (skipping bad ones) */
if (!(pDevice = ProcessFindResponse (sockBuf, nread))) if (!(pDevice = ProcessFindResponse (sockBuf, nread))) continue;
continue;
/* add to list */ /* add to list */
gKnownDevices[iNextDevice++] = pDevice; gKnownDevices[iNextDevice++] = pDevice;
@ -405,8 +449,7 @@ sane_open (SANE_String_Const devicename, SANE_Handle * handle)
for (i = 0; i < MAX_SCANNERS; ++i) for (i = 0; i < MAX_SCANNERS; ++i)
{ {
if (gOpenScanners[i]) if (gOpenScanners[i]) continue;
continue;
iHandle = i; iHandle = i;
break; break;
@ -715,19 +758,13 @@ sane_start (SANE_Handle handle)
} /* while */ } /* while */
/* check whether we were cancelled */ /* check whether we were cancelled */
if ( gOpenScanners[iHandle]->m_bCancelled ) status = SANE_STATUS_CANCELLED; if ( gOpenScanners[iHandle]->m_bCancelled ) status = SANE_STATUS_CANCELLED;
cleanup: cleanup:
FreeComBuf (&buf); FreeComBuf (&buf);
return status; return status;
} /* sane_start */ } /* sane_start */
@ -857,10 +894,8 @@ ClearKnownDevices ()
if (gKnownDevices[i]) if (gKnownDevices[i])
{ {
if (gKnownDevices[i]->name) if (gKnownDevices[i]->name) free ((void*)(gKnownDevices[i]->name));
free ((void*)(gKnownDevices[i]->name)); if (gKnownDevices[i]->model) free ((void*)(gKnownDevices[i]->model));
if (gKnownDevices[i]->model)
free ((void*)(gKnownDevices[i]->model));
free (gKnownDevices[i]); free (gKnownDevices[i]);
} }
gKnownDevices[i] = NULL; gKnownDevices[i] = NULL;
@ -878,9 +913,7 @@ HexDump (int debugLevel, const unsigned char *buf, size_t bufSize)
unsigned int i, j; unsigned int i, j;
char itemBuf[16] = { 0 }, lineBuf[256] = char itemBuf[16] = { 0 }, lineBuf[256] = { 0 };
{
0};
if (DBG_LEVEL < debugLevel) if (DBG_LEVEL < debugLevel)
return; return;
@ -938,11 +971,8 @@ HexDump (int debugLevel, const unsigned char *buf, size_t bufSize)
} }
strncat (lineBuf, itemBuf, sizeof (lineBuf)); strncat (lineBuf, itemBuf, sizeof (lineBuf));
} }
DBG (debugLevel, "%s\n", lineBuf); DBG (debugLevel, "%s\n", lineBuf);
} }
} /* HexDump */ } /* HexDump */
/***********************************************************/ /***********************************************************/
@ -997,7 +1027,6 @@ AppendToComBuf (struct ComBuf *pBuf, const unsigned char *pData,
/* check we have enough space */ /* check we have enough space */
if (pBuf->m_used + datSize > pBuf->m_capacity) if (pBuf->m_used + datSize > pBuf->m_capacity)
{ {
/* nope - allocate some more */ /* nope - allocate some more */
newSize = pBuf->m_used + datSize + INITIAL_COM_BUF_SIZE; newSize = pBuf->m_used + datSize + INITIAL_COM_BUF_SIZE;
pBuf->m_pBuf = realloc (pBuf->m_pBuf, newSize); pBuf->m_pBuf = realloc (pBuf->m_pBuf, newSize);
@ -1199,8 +1228,7 @@ ProcessFindResponse (unsigned char *pData, size_t size)
} /* while pItem */ } /* while pItem */
/* just in case nothing sensible was found */ /* just in case nothing sensible was found */
if (!strlen (printerName)) if ( ! strlen( printerName ) ) return NULL;
return NULL;
pDevice = malloc (sizeof (SANE_Device)); pDevice = malloc (sizeof (SANE_Device));
if (!pDevice) if (!pDevice)
@ -1211,10 +1239,10 @@ ProcessFindResponse (unsigned char *pData, size_t size)
/* knock off "Dell " from start of model name */ /* knock off "Dell " from start of model name */
pModel = printerModel; pModel = printerModel;
if (!strncmp (pModel, "Dell ", 5)) if ( ! strncmp( pModel, "Dell ", 5 ) )
pModel += 5; pModel += 5;
pDevice->name = strdup (printerName); pDevice->name = strdup( printerName );
pDevice->vendor = "Dell"; pDevice->vendor = "Dell";
pDevice->model = strdup (pModel); pDevice->model = strdup (pModel);
pDevice->type = "multi-function peripheral"; pDevice->type = "multi-function peripheral";
@ -1543,7 +1571,6 @@ ProcessTcpResponse (struct ScannerState *pState, struct ComBuf *pTcpBuf)
} }
else if (!strncmp ("std-scan-document-start", pName, nameSize)) else if (!strncmp ("std-scan-document-start", pName, nameSize))
{ {
errorCheck |= InitPacket (&buf, 0x02); errorCheck |= InitPacket (&buf, 0x02);
uiVal = 0; uiVal = 0;
errorCheck |= errorCheck |=
@ -1552,19 +1579,15 @@ ProcessTcpResponse (struct ScannerState *pState, struct ComBuf *pTcpBuf)
&uiVal, sizeof (uiVal)); &uiVal, sizeof (uiVal));
FinalisePacket (&buf); FinalisePacket (&buf);
send (pState->m_tcpFd, buf.m_pBuf, buf.m_used, 0); send (pState->m_tcpFd, buf.m_pBuf, buf.m_used, 0);
} }
else if (!strncmp ("std-scan-document-file-type", pName, nameSize)) else if (!strncmp ("std-scan-document-file-type", pName, nameSize))
{ {
memcpy (&pState->m_fileType, pValue, sizeof (pState->m_fileType)); memcpy (&pState->m_fileType, pValue, sizeof (pState->m_fileType));
DBG (5, "File type: %x\n", ntohl (pState->m_fileType)); DBG (5, "File type: %x\n", ntohl (pState->m_fileType));
} }
else else
if (!strncmp ("std-scan-document-image-compression", pName, nameSize)) if (!strncmp ("std-scan-document-image-compression", pName, nameSize))
{ {
memcpy (&pState->m_compression, pValue, memcpy (&pState->m_compression, pValue,
sizeof (pState->m_compression)); sizeof (pState->m_compression));
DBG (5, "Compression: %x\n", ntohl (pState->m_compression)); DBG (5, "Compression: %x\n", ntohl (pState->m_compression));
@ -1572,17 +1595,13 @@ ProcessTcpResponse (struct ScannerState *pState, struct ComBuf *pTcpBuf)
} }
else if (!strncmp ("std-scan-document-xresolution", pName, nameSize)) else if (!strncmp ("std-scan-document-xresolution", pName, nameSize))
{ {
memcpy (&pState->m_xres, pValue, sizeof (pState->m_xres)); memcpy (&pState->m_xres, pValue, sizeof (pState->m_xres));
DBG (5, "X resolution: %d\n", ntohs (pState->m_xres)); DBG (5, "X resolution: %d\n", ntohs (pState->m_xres));
} }
else if (!strncmp ("std-scan-document-yresolution", pName, nameSize)) else if (!strncmp ("std-scan-document-yresolution", pName, nameSize))
{ {
memcpy (&pState->m_yres, pValue, sizeof (pState->m_yres)); memcpy (&pState->m_yres, pValue, sizeof (pState->m_yres));
DBG (5, "Y resolution: %d\n", ntohs (pState->m_yres)); DBG (5, "Y resolution: %d\n", ntohs (pState->m_yres));
} }
else if (!strncmp ("std-scan-page-widthpixel", pName, nameSize)) else if (!strncmp ("std-scan-page-widthpixel", pName, nameSize))
{ {
@ -1596,11 +1615,9 @@ ProcessTcpResponse (struct ScannerState *pState, struct ComBuf *pTcpBuf)
{ {
DBG (5, "Ignoring width (already have a value)\n"); DBG (5, "Ignoring width (already have a value)\n");
} }
} }
else if (!strncmp ("std-scan-page-heightpixel", pName, nameSize)) else if (!strncmp ("std-scan-page-heightpixel", pName, nameSize))
{ {
if (1 || !pState->m_pixelHeight) if (1 || !pState->m_pixelHeight)
{ {
memcpy (&pState->m_pixelHeight, pValue, memcpy (&pState->m_pixelHeight, pValue,
@ -1614,7 +1631,6 @@ ProcessTcpResponse (struct ScannerState *pState, struct ComBuf *pTcpBuf)
} }
else if (!strncmp ("std-scan-page-start", pName, nameSize)) else if (!strncmp ("std-scan-page-start", pName, nameSize))
{ {
errorCheck |= InitPacket (&buf, 0x02); errorCheck |= InitPacket (&buf, 0x02);
uiVal = 0; uiVal = 0;
errorCheck |= errorCheck |=
@ -1623,10 +1639,6 @@ ProcessTcpResponse (struct ScannerState *pState, struct ComBuf *pTcpBuf)
FinalisePacket (&buf); FinalisePacket (&buf);
send (pState->m_tcpFd, buf.m_pBuf, buf.m_used, 0); send (pState->m_tcpFd, buf.m_pBuf, buf.m_used, 0);
/* write out any pre-existing page data
errorCheck |= ProcessPageData (pState);
*/
/* reset the data buffer ready to store a new page */ /* reset the data buffer ready to store a new page */
pState->m_buf.m_used = 0; pState->m_buf.m_used = 0;
@ -1635,11 +1647,9 @@ ProcessTcpResponse (struct ScannerState *pState, struct ComBuf *pTcpBuf)
pState->m_pixelWidth = 0; pState->m_pixelWidth = 0;
pState->m_pixelHeight = 0; pState->m_pixelHeight = 0;
} }
else if (!strncmp ("std-scan-page-end", pName, nameSize)) else if (!strncmp ("std-scan-page-end", pName, nameSize))
{ {
bProcessImage = 1; bProcessImage = 1;
errorCheck |= InitPacket (&buf, 0x02); errorCheck |= InitPacket (&buf, 0x02);
@ -1649,11 +1659,9 @@ ProcessTcpResponse (struct ScannerState *pState, struct ComBuf *pTcpBuf)
0x05, &uiVal, sizeof (uiVal)); 0x05, &uiVal, sizeof (uiVal));
FinalisePacket (&buf); FinalisePacket (&buf);
send (pState->m_tcpFd, buf.m_pBuf, buf.m_used, 0); send (pState->m_tcpFd, buf.m_pBuf, buf.m_used, 0);
} }
else if (!strncmp ("std-scan-document-end", pName, nameSize)) else if (!strncmp ("std-scan-document-end", pName, nameSize))
{ {
errorCheck |= InitPacket (&buf, 0x02); errorCheck |= InitPacket (&buf, 0x02);
uiVal = 0; uiVal = 0;
errorCheck |= errorCheck |=
@ -1663,16 +1671,11 @@ ProcessTcpResponse (struct ScannerState *pState, struct ComBuf *pTcpBuf)
FinalisePacket (&buf); FinalisePacket (&buf);
send (pState->m_tcpFd, buf.m_pBuf, buf.m_used, 0); send (pState->m_tcpFd, buf.m_pBuf, buf.m_used, 0);
/* write out any pre-existing page data */
/* errorCheck |= ProcessPageData (pState); */
/* reset the data buffer ready to store a new page */ /* reset the data buffer ready to store a new page */
pState->m_buf.m_used = 0; pState->m_buf.m_used = 0;
} }
else if (!strncmp ("std-scan-session-end", pName, nameSize)) else if (!strncmp ("std-scan-session-end", pName, nameSize))
{ {
errorCheck |= InitPacket (&buf, 0x02); errorCheck |= InitPacket (&buf, 0x02);
uiVal = 0; uiVal = 0;
errorCheck |= errorCheck |=
@ -1684,11 +1687,9 @@ ProcessTcpResponse (struct ScannerState *pState, struct ComBuf *pTcpBuf)
/* initialise a shutodwn of the socket */ /* initialise a shutodwn of the socket */
shutdown (pState->m_tcpFd, SHUT_RDWR); shutdown (pState->m_tcpFd, SHUT_RDWR);
} }
else if (!strncmp ("std-scan-scandata-error", pName, nameSize)) else if (!strncmp ("std-scan-scandata-error", pName, nameSize))
{ {
/* determine the size of data in this chunk */ /* determine the size of data in this chunk */
dataChunkSize = (pItem[6] << 8) + pItem[7]; dataChunkSize = (pItem[6] << 8) + pItem[7];
@ -1703,12 +1704,9 @@ ProcessTcpResponse (struct ScannerState *pState, struct ComBuf *pTcpBuf)
DBG (10, "Accumulated %d bytes of scan data so far\n", DBG (10, "Accumulated %d bytes of scan data so far\n",
pState->m_buf.m_used); pState->m_buf.m_used);
} /* if */ } /* if */
} /* while */ } /* while */
/* process page data if required */ /* process page data if required */
if ( bProcessImage ) errorCheck |= ProcessPageData (pState); if ( bProcessImage ) errorCheck |= ProcessPageData (pState);
@ -1762,7 +1760,6 @@ int
ProcessPageData (struct ScannerState *pState) ProcessPageData (struct ScannerState *pState)
{ {
char tempFilename[TMP_MAX] = "scan.dat";
FILE *fTmp; FILE *fTmp;
int fdTmp; int fdTmp;
struct jpeg_source_mgr jpegSrcMgr; struct jpeg_source_mgr jpegSrcMgr;
@ -1965,17 +1962,12 @@ ProcessPageData (struct ScannerState *pState)
default: default:
/* this is not expected or very useful */ /* this is not expected or very useful */
{ {
tmpnam (tempFilename); DBG (1, "ProcessPageData: Unexpected compression flag %d\n", ntohl (pState->m_compression));
DBG (1, "ProcessPageData: Writing scan data to %s\n", tempFilename);
fTmp = fopen (tempFilename, "w");
fwrite (pState->m_buf.m_pBuf, pState->m_buf.m_used, 1, fTmp);
fclose (fTmp);
ret = SANE_STATUS_IO_ERROR; ret = SANE_STATUS_IO_ERROR;
} }
} /* switch */ } /* switch */
return ret; return ret;
} /* ProcessPageData */ } /* ProcessPageData */
/***********************************************************/ /***********************************************************/