1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
|
/************************************************************************************\
scl.c - HP SANE backend for multi-function peripherals (libsane-hpaio)
(c) 2001-2006 Copyright Hewlett-Packard Development Company, LP
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Contributing Authors: David Paschal, Don Welch, David Suffield
\************************************************************************************/
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include "hpmud.h"
#include "io.h"
#include "common.h"
#include "scl.h"
#include "hpaio.h"
#define DEBUG_DECLARE_ONLY
#include "sanei_debug.h"
static int SclBufferIsPartialReply( void * dataptr, int datalen )
{
int i = 0, value = 0;
unsigned char * const data = dataptr;
unsigned int d;
if( i >= datalen )
{
return 0;
}
if( data[i++] != 27 )
{
return 0;
}
if( i >= datalen )
{
return 0;
}
if( data[i++] != '*' )
{
return 0;
}
if( i >= datalen )
{
return 0;
}
if( data[i++] != 's' )
{
return 0;
}
while( 42 )
{
if( i >= datalen )
{
return 0;
}
d = data[i] - '0';
if( d > 9 )
{
break;
}
i++;
}
d = data[i++];
if( d<'a' || d>'z' )
{
return 0;
}
while( 42 )
{
if( i >= datalen )
{
return 0;
}
d = data[i] - '0';
if( d > 9 )
{
break;
}
i++;
value = ( value * 10 ) + d;
}
if( i >= datalen )
{
return 0;
}
if( data[i++] != 'W' )
{
return 0;
}
value = i + value - datalen;
if( value < 0 )
{
value = 0;
}
return value;
}
static int SclChannelRead(int deviceid, int channelid, char * buffer, int countdown, int isSclResponse)
{
char * bufferStart = buffer;
int bufferLen = countdown, countup = 0, len;
enum HPMUD_RESULT stat;
if(!isSclResponse)
{
stat = hpmud_read_channel(deviceid, channelid, buffer, bufferLen, EXCEPTION_TIMEOUT, &len);
return len;
}
while(1)
{
stat = hpmud_read_channel(deviceid, channelid, buffer, countdown, EXCEPTION_TIMEOUT, &len);
if(stat != HPMUD_R_OK)
{
break;
}
countup += len;
countdown = SclBufferIsPartialReply( (unsigned char *)bufferStart, countup );
if( countup + countdown > bufferLen )
{
countdown = bufferLen - countup;
}
if( countdown <= 0 )
{
break;
}
buffer += len;
//startTimeout = continueTimeout;
}
if(!countup)
{
return len;
}
return countup;
}
SANE_Status __attribute__ ((visibility ("hidden"))) SclSendCommand(int deviceid, int channelid, int cmd, int param)
{
char buffer[LEN_SCL_BUFFER];
int datalen, len;
char punc = SCL_CMD_PUNC( cmd );
char letter1 = SCL_CMD_LETTER1( cmd),letter2 = SCL_CMD_LETTER2( cmd );
if( cmd == SCL_CMD_RESET )
{
datalen = snprintf( buffer, LEN_SCL_BUFFER, "\x1B%c", letter2 );
}
else
{
if( cmd == SCL_CMD_CLEAR_ERROR_STACK )
{
datalen = snprintf( buffer,
LEN_SCL_BUFFER,
"\x1B%c%c%c",
punc,
letter1,
letter2 );
}
else
{
datalen = snprintf( buffer,
LEN_SCL_BUFFER,
"\x1B%c%c%d%c",
punc,
letter1,
param,
letter2 );
}
}
hpmud_write_channel(deviceid, channelid, buffer, datalen, EXCEPTION_TIMEOUT, &len);
DBG(6, "SclSendCommand: size=%d bytes_wrote=%d: %s %d\n", datalen, len, __FILE__, __LINE__);
if (DBG_LEVEL >= 6)
sysdump(buffer, datalen);
if(len != datalen)
{
return SANE_STATUS_IO_ERROR;
}
return SANE_STATUS_GOOD;
}
SANE_Status __attribute__ ((visibility ("hidden"))) SclInquire(int deviceid, int channelid, int cmd, int param, int * pValue, char * buffer, int maxlen)
{
SANE_Status retcode;
int lenResponse, len, value;
char _response[LEN_SCL_BUFFER + 1], * response = _response;
char expected[LEN_SCL_BUFFER], expectedChar;
if( !pValue )
{
pValue = &value;
}
if( buffer && maxlen > 0 )
{
memset( buffer, 0, maxlen );
}
memset( _response, 0, LEN_SCL_BUFFER + 1 );
/* Send inquiry command. */
if( ( retcode = SclSendCommand( deviceid, channelid, cmd, param ) ) != SANE_STATUS_GOOD )
{
return retcode;
}
/* Figure out what format of response we expect. */
expectedChar = SCL_CMD_LETTER2( cmd ) - 'A' + 'a' - 1;
if( expectedChar == 'q' )
{
expectedChar--;
}
len = snprintf( expected,
LEN_SCL_BUFFER,
"\x1B%c%c%d%c",
SCL_CMD_PUNC( cmd ),
SCL_CMD_LETTER1( cmd ),
param,
expectedChar );
/* Read the response. */
lenResponse = SclChannelRead( deviceid, channelid, response, LEN_SCL_BUFFER, 1 );
DBG(6, "SclChannelRead: len=%d: %s %d\n", lenResponse, __FILE__, __LINE__);
if (DBG_LEVEL >= 6)
sysdump(response, lenResponse);
/* Validate the first part of the response. */
if( lenResponse <= len || memcmp( response, expected, len ) )
{
bug("invalid SclInquire(cmd=%x,param=%d) exp(len=%d)/act(len=%d): %s %d\n", cmd, param, len, lenResponse, __FILE__, __LINE__);
bug("exp:\n");
bugdump(expected, len);
bug("act:\n");
bugdump(response, lenResponse);
return SANE_STATUS_IO_ERROR;
}
response += len;
lenResponse -= len;
/* Null response? */
if( response[0] == 'N' )
{
DBG(6, "SclInquire null response. %s %d\n", __FILE__, __LINE__);
return SANE_STATUS_UNSUPPORTED;
}
/* Parse integer part of non-null response.
* If this is a binary-data response, then this value is the
* length of the binary-data portion. */
if( sscanf( response, "%d%n", pValue, &len ) != 1 )
{
bug("invalid SclInquire(cmd=%x,param=%d) integer response: %s %d\n", cmd, param, __FILE__, __LINE__);
return SANE_STATUS_IO_ERROR;
}
/* Integer response? */
if( response[len] == 'V' )
{
return SANE_STATUS_GOOD;
}
/* Binary-data response? */
if( response[len] != 'W' )
{
bug("invalid SclInquire(cmd=%x,param=%d) unexpected character '%c': %s %d\n", cmd, param, response[len], __FILE__, __LINE__);
return SANE_STATUS_IO_ERROR;
}
response += len + 1;
lenResponse -= len + 1;
/* Make sure we got the right length of binary data. */
if( lenResponse<0 || lenResponse != *pValue || lenResponse>maxlen )
{
bug("invalid SclInquire(cmd=%x,param=%d) binary data lenResponse=%d *pValue=%d maxlen=%d: %s %d\n",
cmd, param, lenResponse, *pValue, maxlen, __FILE__, __LINE__);
return SANE_STATUS_IO_ERROR;
}
/* Copy binary data into user's buffer. */
if( buffer )
{
maxlen = *pValue;
memcpy( buffer, response, maxlen );
}
return SANE_STATUS_GOOD;
}
/*
* Phase 2 partial rewrite. des 9/26/07
*/
SANE_Status __attribute__ ((visibility ("hidden"))) scl_send_cmd(HPAIO_RECORD *hpaio, const char *buf, int size)
{
int len;
hpmud_write_channel(hpaio->deviceid, hpaio->scan_channelid, buf, size, EXCEPTION_TIMEOUT, &len);
DBG(6, "scl cmd sent size=%d bytes_wrote=%d: %s %d\n", size, len, __FILE__, __LINE__);
if (DBG_LEVEL >= 6)
sysdump(buf, size);
if(len != size)
{
return SANE_STATUS_IO_ERROR;
}
return SANE_STATUS_GOOD;
}
SANE_Status __attribute__ ((visibility ("hidden"))) scl_query_int(HPAIO_RECORD *hpaio, const char *buf, int size, int *result)
{
char rbuf[256];
int len, stat;
char *tail;
*result=0;
if ((stat = scl_send_cmd(hpaio, buf, size)) != SANE_STATUS_GOOD)
{
return stat;
}
if ((stat = hpmud_read_channel(hpaio->deviceid, hpaio->scan_channelid, rbuf, sizeof(rbuf), EXCEPTION_TIMEOUT, &len)) != HPMUD_R_OK)
{
return SANE_STATUS_IO_ERROR;
}
DBG(6, "scl response size=%d: %s %d\n", len, __FILE__, __LINE__);
if (DBG_LEVEL >= 6)
sysdump(buf, size);
/* Null response? */
if(rbuf[len-1] == 'N')
{
DBG(6, "scl null response: %s %d\n", __FILE__, __LINE__);
return SANE_STATUS_UNSUPPORTED;
}
/* Integer response? */
if(rbuf[len-1] != 'V' )
{
bug("invalid scl integer response: %s %d\n", __FILE__, __LINE__);
return SANE_STATUS_IO_ERROR;
}
*result = strtol(&rbuf[size], &tail, 10);
return SANE_STATUS_GOOD;
}
|