1 /************************************************************************************\
3 scl.c - HP SANE backend for multi-function peripherals (libsane-hpaio)
5 (c) 2001-2006 Copyright Hewlett-Packard Development Company, LP
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
11 of the Software, and to permit persons to whom the Software is furnished to do
12 so, subject to the following conditions:
14 The above copyright notice and this permission notice shall be included in all
15 copies or substantial portions of the Software.
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19 FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20 COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24 Contributing Authors: David Paschal, Don Welch, David Suffield
26 \************************************************************************************/
37 #define DEBUG_DECLARE_ONLY
38 #include "sanei_debug.h"
40 static int SclBufferIsPartialReply( unsigned char * data, int datalen )
57 if( data[i++] != '*' )
65 if( data[i++] != 's' )
99 value = ( value * 10 ) + d;
105 if( data[i++] != 'W' )
109 value = i + value - datalen;
118 static int SclChannelRead(int deviceid, int channelid, char * buffer, int countdown, int isSclResponse)
120 char * bufferStart = buffer;
121 int bufferLen = countdown, countup = 0, len;
122 enum HPMUD_RESULT stat;
126 stat = hpmud_read_channel(deviceid, channelid, buffer, bufferLen, EXCEPTION_TIMEOUT, &len);
132 stat = hpmud_read_channel(deviceid, channelid, buffer, countdown, EXCEPTION_TIMEOUT, &len);
134 if(stat != HPMUD_R_OK)
140 countdown = SclBufferIsPartialReply( (unsigned char *)bufferStart, countup );
142 if( countup + countdown > bufferLen )
144 countdown = bufferLen - countup;
152 //startTimeout = continueTimeout;
163 SANE_Status __attribute__ ((visibility ("hidden"))) SclSendCommand(int deviceid, int channelid, int cmd, int param)
165 char buffer[LEN_SCL_BUFFER];
167 char punc = SCL_CMD_PUNC( cmd );
168 char letter1 = SCL_CMD_LETTER1( cmd),letter2 = SCL_CMD_LETTER2( cmd );
170 if( cmd == SCL_CMD_RESET )
172 datalen = snprintf( buffer, LEN_SCL_BUFFER, "\x1B%c", letter2 );
176 if( cmd == SCL_CMD_CLEAR_ERROR_STACK )
178 datalen = snprintf( buffer,
187 datalen = snprintf( buffer,
197 hpmud_write_channel(deviceid, channelid, buffer, datalen, EXCEPTION_TIMEOUT, &len);
199 DBG(6, "SclSendCommand: size=%d bytes_wrote=%d: %s %d\n", datalen, len, __FILE__, __LINE__);
201 sysdump(buffer, datalen);
205 return SANE_STATUS_IO_ERROR;
208 return SANE_STATUS_GOOD;
211 SANE_Status __attribute__ ((visibility ("hidden"))) SclInquire(int deviceid, int channelid, int cmd, int param, int * pValue, char * buffer, int maxlen)
214 int lenResponse, len, value;
215 char _response[LEN_SCL_BUFFER + 1], * response = _response;
216 char expected[LEN_SCL_BUFFER], expectedChar;
222 if( buffer && maxlen > 0 )
224 memset( buffer, 0, maxlen );
226 memset( _response, 0, LEN_SCL_BUFFER + 1 );
228 /* Send inquiry command. */
229 if( ( retcode = SclSendCommand( deviceid, channelid, cmd, param ) ) != SANE_STATUS_GOOD )
234 /* Figure out what format of response we expect. */
235 expectedChar = SCL_CMD_LETTER2( cmd ) - 'A' + 'a' - 1;
236 if( expectedChar == 'q' )
240 len = snprintf( expected,
244 SCL_CMD_LETTER1( cmd ),
248 /* Read the response. */
249 lenResponse = SclChannelRead( deviceid, channelid, response, LEN_SCL_BUFFER, 1 );
251 DBG(6, "SclChannelRead: len=%d: %s %d\n", lenResponse, __FILE__, __LINE__);
253 sysdump(response, lenResponse);
255 /* Validate the first part of the response. */
256 if( lenResponse <= len || memcmp( response, expected, len ) )
258 bug("invalid SclInquire(cmd=%x,param=%d) exp(len=%d)/act(len=%d): %s %d\n", cmd, param, len, lenResponse, __FILE__, __LINE__);
260 bugdump(expected, len);
262 bugdump(response, lenResponse);
263 return SANE_STATUS_IO_ERROR;
269 if( response[0] == 'N' )
271 DBG(6, "SclInquire null response. %s %d\n", __FILE__, __LINE__);
272 return SANE_STATUS_UNSUPPORTED;
275 /* Parse integer part of non-null response.
276 * If this is a binary-data response, then this value is the
277 * length of the binary-data portion. */
278 if( sscanf( response, "%d%n", pValue, &len ) != 1 )
280 bug("invalid SclInquire(cmd=%x,param=%d) integer response: %s %d\n", cmd, param, __FILE__, __LINE__);
281 return SANE_STATUS_IO_ERROR;
284 /* Integer response? */
285 if( response[len] == 'V' )
287 return SANE_STATUS_GOOD;
290 /* Binary-data response? */
291 if( response[len] != 'W' )
293 bug("invalid SclInquire(cmd=%x,param=%d) unexpected character '%c': %s %d\n", cmd, param, response[len], __FILE__, __LINE__);
294 return SANE_STATUS_IO_ERROR;
297 lenResponse -= len + 1;
299 /* Make sure we got the right length of binary data. */
300 if( lenResponse<0 || lenResponse != *pValue || lenResponse>maxlen )
302 bug("invalid SclInquire(cmd=%x,param=%d) binary data lenResponse=%d *pValue=%d maxlen=%d: %s %d\n",
303 cmd, param, lenResponse, *pValue, maxlen, __FILE__, __LINE__);
304 return SANE_STATUS_IO_ERROR;
307 /* Copy binary data into user's buffer. */
311 memcpy( buffer, response, maxlen );
314 return SANE_STATUS_GOOD;
318 * Phase 2 partial rewrite. des 9/26/07
321 SANE_Status __attribute__ ((visibility ("hidden"))) scl_send_cmd(HPAIO_RECORD *hpaio, const char *buf, int size)
325 hpmud_write_channel(hpaio->deviceid, hpaio->scan_channelid, buf, size, EXCEPTION_TIMEOUT, &len);
327 DBG(6, "scl cmd sent size=%d bytes_wrote=%d: %s %d\n", size, len, __FILE__, __LINE__);
333 return SANE_STATUS_IO_ERROR;
336 return SANE_STATUS_GOOD;
339 SANE_Status __attribute__ ((visibility ("hidden"))) scl_query_int(HPAIO_RECORD *hpaio, const char *buf, int size, int *result)
347 if ((stat = scl_send_cmd(hpaio, buf, size)) != SANE_STATUS_GOOD)
352 if ((stat = hpmud_read_channel(hpaio->deviceid, hpaio->scan_channelid, rbuf, sizeof(rbuf), EXCEPTION_TIMEOUT, &len)) != HPMUD_R_OK)
354 return SANE_STATUS_IO_ERROR;
357 DBG(6, "scl response size=%d: %s %d\n", len, __FILE__, __LINE__);
362 if(rbuf[len-1] == 'N')
364 DBG(6, "scl null response: %s %d\n", __FILE__, __LINE__);
365 return SANE_STATUS_UNSUPPORTED;
368 /* Integer response? */
369 if(rbuf[len-1] != 'V' )
371 bug("invalid scl integer response: %s %d\n", __FILE__, __LINE__);
372 return SANE_STATUS_IO_ERROR;
375 *result = strtol(&rbuf[size], &tail, 10);
377 return SANE_STATUS_GOOD;