diff --git a/backend/brother_mfp/brother_mfp-driver.cpp b/backend/brother_mfp/brother_mfp-driver.cpp index 93afc1c55..532c1e6c5 100644 --- a/backend/brother_mfp/brother_mfp-driver.cpp +++ b/backend/brother_mfp/brother_mfp-driver.cpp @@ -77,6 +77,8 @@ SANE_Status BrotherUSBDriver::Connect () * */ is_open = true; + next_frame_number = 0; + out_of_docs = false; res = sanei_usb_open (devicename, &fd); @@ -252,9 +254,9 @@ SANE_Status BrotherUSBDriver::StopSession () SANE_Status BrotherUSBDriver::ExecStartSession () { - SANE_Status res = sanei_usb_control_msg (fd, USB_DIR_IN | USB_TYPE_VENDOR, - BROTHER_USB_REQ_STARTSESSION, - 2, 0, 5, small_buffer); + SANE_Status res = sanei_usb_control_msg (fd, USB_DIR_IN | USB_TYPE_VENDOR, + BROTHER_USB_REQ_STARTSESSION, + 2, 0, 5, small_buffer); if (res != SANE_STATUS_GOOD) { @@ -272,8 +274,7 @@ SANE_Status BrotherUSBDriver::ExecStartSession () */ BrotherSessionResponse resp; - res = encoder->DecodeSessionResp (small_buffer, 5, resp); - if (res != SANE_STATUS_GOOD) + if (encoder->DecodeSessionResp (small_buffer, 5, resp) != DECODE_STATUS_GOOD) { DBG (DBG_WARN, "BrotherUSBDriver::StartSession: start session response is invalid.\n"); @@ -313,8 +314,7 @@ SANE_Status BrotherUSBDriver::ExecStopSession () */ BrotherSessionResponse resp; - res = encoder->DecodeSessionResp (small_buffer, 5, resp); - if (res != SANE_STATUS_GOOD) + if (encoder->DecodeSessionResp (small_buffer, 5, resp) != DECODE_STATUS_GOOD) { DBG (DBG_WARN, "BrotherUSBDriver::StopSession: stop session response is invalid.\n"); @@ -415,18 +415,30 @@ SANE_Status BrotherUSBDriver::ReadScanData (SANE_Byte *data, size_t max_length, */ size_t data_consumed = 0; - res = encoder->DecodeScanData (data_buffer, - data_buffer_bytes, - &data_consumed, - data, - max_length, - length); + DecodeStatus dec_ret = encoder->DecodeScanData (data_buffer, + data_buffer_bytes, + &data_consumed, + data, + max_length, + length); DBG (DBG_IMPORTANT, "BrotherUSBDriver::ReadScanData: decoder consumes %zu bytes and writes %zu bytes, returning %d\n", data_consumed, *length, - res); + dec_ret); + + res = encoder->DecodeStatusToSaneStatus(dec_ret); + + if ((dec_ret == DECODE_STATUS_ENDOFDATA) || (dec_ret == DECODE_STATUS_ENDOFFRAME_NO_MORE)) + { + next_frame_number++; + out_of_docs = true; + } + else if (dec_ret == DECODE_STATUS_ENDOFFRAME_WITH_MORE) + { + next_frame_number++; + } /* * Shift down the read buffer so that we can maximise our @@ -632,12 +644,18 @@ SANE_Status BrotherUSBDriver::CancelScan () is_scanning = false; was_cancelled = true; + next_frame_number = 0; + out_of_docs = false; /* * Send the cancel sequence. * * 0x1b52 * + * We could test to see if we have actually requested a scan + * but it looks like sending the cancel scan code is OK if we are not. + * It's more complicated to add code to check than it is to just send it. + * */ (void) memcpy (small_buffer, "\x1b" "R", 2); size_t buf_size = 2; @@ -664,7 +682,7 @@ SANE_Status BrotherUSBDriver::CancelScan () res = PollForReadFlush (TIMEOUT_SECS(1)); if (res != SANE_STATUS_GOOD) { - DBG (DBG_WARN, "BrotherUSBDriver::Init: initial flush failure: %d\n", + DBG (DBG_WARN, "BrotherUSBDriver::CancelScan: initial flush failure: %d\n", res); return res; } @@ -700,136 +718,95 @@ SANE_Status BrotherUSBDriver::StartScan () DBG (DBG_EVENT, "BrotherUSBDriver::StartScan: `%s'\n", devicename); - if (is_scanning) - { - DBG (DBG_WARN, "BrotherUSBDriver::StartScan: already scanning `%s'\n", - devicename); - return SANE_STATUS_DEVICE_BUSY; - } - - is_scanning = true; - was_cancelled = false; - /* - * Initialisation. + * This is the count of frames we have acquired so far. + * 0 means that we are at the start of a session so we need to + * so all the setup. + * + * If not, then we have come back here to get the next frame, probably + * from an ADF. * */ - res = StartSession(); - if (res != SANE_STATUS_GOOD) + if (next_frame_number == 0) { - DBG (DBG_SERIOUS, "BrotherUSBDriver::StartScan: failed to start session: %d\n", res); - (void)CancelScan(); - return res; - } + /* + * This flag indicated that we exhausted all of the frames in this session. + * Cancel is required before more scanning is possible. + * + */ + if (out_of_docs) + { + return SANE_STATUS_NO_DOCS; + } - /* - * Begin prologue. - * - */ - res = PollForReadFlush (TIMEOUT_SECS(1)); - if (res != SANE_STATUS_GOOD) - { - DBG (DBG_SERIOUS, - "BrotherUSBDriver::StartScan: failed to read flush: %d\n", res); - (void) CancelScan (); - return res; - } + if (is_scanning) + { + DBG (DBG_WARN, "BrotherUSBDriver::StartScan: already scanning `%s'\n", + devicename); + return SANE_STATUS_DEVICE_BUSY; + } - /* - * Construct the preparatory info block. - * This gets the scanner going for calibration, etc I think. - * - */ - size_t packet_len = 0; - res = encoder->EncodeBasicParameterBlock (small_buffer, sizeof(small_buffer), &packet_len); - if (res != SANE_STATUS_GOOD) - { - DBG (DBG_SERIOUS, - "BrotherUSBDriver::StartScan: failed to generate basic param block: %d\n", - res); - (void) CancelScan (); - return SANE_STATUS_INVAL; - } + is_scanning = true; + was_cancelled = false; - size_t buf_size = packet_len; - res = sanei_usb_write_bulk (fd, small_buffer, &buf_size); - if (res != SANE_STATUS_GOOD) - { - DBG (DBG_SERIOUS, - "BrotherUSBDriver::StartScan: failed to send basic parameter block: %d\n", - res); - (void) CancelScan (); - return res; - } + /* + * Initialisation. + * + */ + res = StartSession(); + if (res != SANE_STATUS_GOOD) + { + DBG (DBG_SERIOUS, "BrotherUSBDriver::StartScan: failed to start session: %d\n", res); + (void)CancelScan(); + return res; + } - if (buf_size != packet_len) - { - DBG (DBG_SERIOUS, - "BrotherUSBDriver::StartScan: failed to write basic parameter block\n"); - (void) CancelScan (); - return SANE_STATUS_IO_ERROR; - } - - /* - * Try to read the response. - * - */ - buf_size = sizeof(small_buffer); - useconds_t timeout = TIMEOUT_SECS(8); - - res = PollForRead (small_buffer, &buf_size, &timeout); - if (res != SANE_STATUS_GOOD) - { - DBG ( - DBG_SERIOUS, - "BrotherUSBDriver::StartScan: failed to read prep parameter block response: %d\n", - res); - (void) CancelScan (); - return res; - } - - BrotherBasicParamResponse basic_param_resp; - - res = encoder->DecodeBasicParameterBlockResp (small_buffer, buf_size, basic_param_resp); - if (res != SANE_STATUS_GOOD) - { - DBG (DBG_SERIOUS, - "BrotherUSBDriver::StartScan: failed to read prep parameter block response: %d\n", - res); - (void) CancelScan (); - return res; - } - - /* - * Construct the "ADF" block. - * - */ - packet_len = 0; - res = encoder->EncodeADFBlock (small_buffer, sizeof(small_buffer), &packet_len); - if (res != SANE_STATUS_UNSUPPORTED) - { + /* + * Begin prologue. + * + */ + res = PollForReadFlush (TIMEOUT_SECS(1)); if (res != SANE_STATUS_GOOD) { DBG (DBG_SERIOUS, - "BrotherUSBDriver::StartScan: failed to generate ADF block: %d\n", - res); + "BrotherUSBDriver::StartScan: failed to read flush: %d\n", res); + (void) CancelScan (); + return res; + } + + /* + * Construct the preparatory info block. + * This gets the scanner going for calibration, etc I think. + * + */ + size_t packet_len = 0; + DecodeStatus dec_ret = encoder->EncodeBasicParameterBlock (small_buffer, + sizeof(small_buffer), + &packet_len); + if (dec_ret != DECODE_STATUS_GOOD) + { + DBG (DBG_SERIOUS, + "BrotherUSBDriver::StartScan: failed to generate basic param block: %d\n", + dec_ret); (void) CancelScan (); return SANE_STATUS_INVAL; } - buf_size = packet_len; + size_t buf_size = packet_len; res = sanei_usb_write_bulk (fd, small_buffer, &buf_size); if (res != SANE_STATUS_GOOD) { - DBG (DBG_SERIOUS, "BrotherUSBDriver::StartScan: failed to send ADF block: %d\n", res); - (void)CancelScan(); + DBG (DBG_SERIOUS, + "BrotherUSBDriver::StartScan: failed to send basic parameter block: %d\n", + res); + (void) CancelScan (); return res; } if (buf_size != packet_len) { - DBG (DBG_SERIOUS, "BrotherUSBDriver::StartScan: failed to write ADF block\n"); - (void)CancelScan(); + DBG (DBG_SERIOUS, "BrotherUSBDriver::StartScan: failed to write basic parameter block\n"); + (void) CancelScan (); return SANE_STATUS_IO_ERROR; } @@ -838,85 +815,208 @@ SANE_Status BrotherUSBDriver::StartScan () * */ buf_size = sizeof(small_buffer); - timeout = TIMEOUT_SECS(8); + useconds_t timeout = TIMEOUT_SECS(8); - res = PollForRead(small_buffer, &buf_size, &timeout); + res = PollForRead (small_buffer, &buf_size, &timeout); if (res != SANE_STATUS_GOOD) { - DBG (DBG_SERIOUS, "BrotherUSBDriver::StartScan: failed to read ADF block response: %d\n", res); - (void)CancelScan(); - return res; - } - - BrotherADFResponse adf_resp; - - res = encoder->DecodeADFBlockResp (small_buffer, buf_size, adf_resp); - if (res != SANE_STATUS_GOOD) - { - DBG (DBG_SERIOUS, - "BrotherUSBDriver::StartScan: ADF block response block invalid: %d\n", - res); + DBG ( + DBG_SERIOUS, + "BrotherUSBDriver::StartScan: failed to read prep parameter block response: %d\n", res); (void) CancelScan (); return res; } - if (adf_resp.resp_code != 0xc2) + BrotherBasicParamResponse basic_param_resp; + + dec_ret = encoder->DecodeBasicParameterBlockResp (small_buffer, buf_size, basic_param_resp); + if (dec_ret != DECODE_STATUS_GOOD) { DBG (DBG_SERIOUS, - "BrotherUSBDriver::StartScan: ADF block response invalid: %u\n", - (unsigned int)adf_resp.resp_code); + "BrotherUSBDriver::StartScan: failed to read prep parameter block response: %d\n", + dec_ret); (void) CancelScan (); return res; } - } - /* - * Construct the MAIN parameter block. This will precipitate the actual scan. - * - */ - packet_len = 0; - res = encoder->EncodeParameterBlock (small_buffer, sizeof(small_buffer), &packet_len); - if (res != SANE_STATUS_GOOD) - { - DBG (DBG_SERIOUS, - "BrotherUSBDriver::StartScan: failed to generate param block: %d\n", - res); - (void) CancelScan (); - return SANE_STATUS_INVAL; - } + /* + * Construct the "ADF" block. + * + */ + packet_len = 0; + dec_ret = encoder->EncodeADFBlock (small_buffer, sizeof(small_buffer), &packet_len); + if (dec_ret != DECODE_STATUS_UNSUPPORTED) + { + if (dec_ret != DECODE_STATUS_GOOD) + { + DBG (DBG_SERIOUS, + "BrotherUSBDriver::StartScan: failed to generate ADF block: %d\n", + dec_ret); + (void) CancelScan (); + return SANE_STATUS_INVAL; + } - buf_size = packet_len; - res = sanei_usb_write_bulk (fd, small_buffer, &buf_size); - if (res != SANE_STATUS_GOOD) - { - DBG (DBG_SERIOUS, - "BrotherUSBDriver::StartScan: failed to send parameter block: %d\n", - res); - (void) CancelScan (); - return res; - } + buf_size = packet_len; + res = sanei_usb_write_bulk (fd, small_buffer, &buf_size); + if (res != SANE_STATUS_GOOD) + { + DBG (DBG_SERIOUS, "BrotherUSBDriver::StartScan: failed to send ADF block: %d\n", res); + (void) CancelScan (); + return res; + } - if (buf_size != packet_len) - { - DBG (DBG_SERIOUS, "BrotherUSBDriver::StartScan: failed to write parameter block\n"); - (void) CancelScan (); - return SANE_STATUS_IO_ERROR; - } + if (buf_size != packet_len) + { + DBG (DBG_SERIOUS, "BrotherUSBDriver::StartScan: failed to write ADF block\n"); + (void) CancelScan (); + return SANE_STATUS_IO_ERROR; + } + + /* + * Try to read the response. + * + */ + buf_size = sizeof(small_buffer); + timeout = TIMEOUT_SECS(8); + + res = PollForRead (small_buffer, &buf_size, &timeout); + if (res != SANE_STATUS_GOOD) + { + DBG (DBG_SERIOUS, + "BrotherUSBDriver::StartScan: failed to read ADF block response: %d\n", + res); + (void) CancelScan (); + return res; + } + + BrotherADFResponse adf_resp; + + dec_ret = encoder->DecodeADFBlockResp (small_buffer, buf_size, adf_resp); + if (dec_ret != DECODE_STATUS_GOOD) + { + DBG (DBG_SERIOUS, + "BrotherUSBDriver::StartScan: ADF block response block invalid: %d\n", + dec_ret); + (void) CancelScan (); + return res; + } + + if (adf_resp.resp_code != 0xc2) + { + DBG (DBG_SERIOUS, + "BrotherUSBDriver::StartScan: ADF block response invalid: %u\n", + (unsigned int) adf_resp.resp_code); + (void) CancelScan (); + return SANE_STATUS_IO_ERROR; + } + } + + /* + * Construct the MAIN parameter block. This will precipitate the actual scan. + * + */ + packet_len = 0; + dec_ret = encoder->EncodeParameterBlock (small_buffer, sizeof(small_buffer), &packet_len); + if (dec_ret != DECODE_STATUS_GOOD) + { + DBG (DBG_SERIOUS, + "BrotherUSBDriver::StartScan: failed to generate param block: %d\n", + dec_ret); + (void) CancelScan (); + return SANE_STATUS_INVAL; + } + + buf_size = packet_len; + res = sanei_usb_write_bulk (fd, small_buffer, &buf_size); + if (res != SANE_STATUS_GOOD) + { + DBG (DBG_SERIOUS, + "BrotherUSBDriver::StartScan: failed to send parameter block: %d\n", + res); + (void) CancelScan (); + return res; + } + + if (buf_size != packet_len) + { + DBG (DBG_SERIOUS, "BrotherUSBDriver::StartScan: failed to write parameter block\n"); + (void) CancelScan (); + return SANE_STATUS_IO_ERROR; + } + + /* + * Allocate a read buffer. + * + */ + data_buffer = new SANE_Byte[BROTHER_READ_BUFFER_LEN]; + if (NULL == data_buffer) + { + DBG (DBG_SERIOUS, + "BrotherUSBDriver::StartScan: failed to allocate read buffer: %zu\n", + (size_t) BROTHER_READ_BUFFER_LEN); + (void) CancelScan (); + return SANE_STATUS_NO_MEM; + } + data_buffer_bytes = 0; + } + else + { + if (!is_scanning) + { + DBG (DBG_SERIOUS, + "BrotherUSBDriver::StartScan: next image: we are no scanning!\n"); + (void) CancelScan (); + return SANE_STATUS_INVAL; + } + + /* + * Check that we have documents left. + * We will have set this flag if the device told us that there were + * no more docs to scan. + * + */ + if (out_of_docs) + { + return SANE_STATUS_NO_DOCS; + } + + /* + * If this is not the first image, then we merely need + * to ask for another image. We do this with a blank parameter block. + * + */ + size_t packet_len = 0; + DecodeStatus dec_ret = encoder->EncodeParameterBlockBlank (small_buffer, + sizeof(small_buffer), + &packet_len); + if (dec_ret != DECODE_STATUS_GOOD) + { + DBG (DBG_SERIOUS, + "BrotherUSBDriver::StartScan: failed to generate param block: %d\n", + dec_ret); + (void) CancelScan (); + return SANE_STATUS_INVAL; + } + + size_t buf_size = packet_len; + res = sanei_usb_write_bulk (fd, small_buffer, &buf_size); + if (res != SANE_STATUS_GOOD) + { + DBG (DBG_SERIOUS, + "BrotherUSBDriver::StartScan: failed to send parameter block: %d\n", + res); + (void) CancelScan (); + return res; + } + + if (buf_size != packet_len) + { + DBG (DBG_SERIOUS, "BrotherUSBDriver::StartScan: failed to write parameter block\n"); + (void) CancelScan (); + return SANE_STATUS_IO_ERROR; + } - /* - * Allocate a read buffer. - * - */ - data_buffer = new SANE_Byte[BROTHER_READ_BUFFER_LEN]; - if (NULL == data_buffer) - { - DBG (DBG_SERIOUS, - "BrotherUSBDriver::StartScan: failed to allocate read buffer: %zu\n", - (size_t) BROTHER_READ_BUFFER_LEN); - (void)CancelScan(); - return SANE_STATUS_NO_MEM; } - data_buffer_bytes = 0; /* * Reset the encoder/decoder. @@ -949,8 +1049,7 @@ SANE_Status BrotherUSBDriver::CheckSensor (BrotherSensor &status) */ BrotherButtonQueryResponse resp; - res = encoder->DecodeButtonQueryResp (small_buffer, 4, resp); - if (res != SANE_STATUS_GOOD) + if (encoder->DecodeButtonQueryResp (small_buffer, 4, resp) != DECODE_STATUS_GOOD) { DBG (DBG_WARN, "BrotherUSBDriver::CheckSensor: button state response is invalid.\n"); @@ -990,13 +1089,13 @@ if (res != SANE_STATUS_GOOD) */ BrotherButtonStateResponse state_resp; - res = encoder->DecodeButtonStateResp (small_buffer, 9, state_resp); - if (res != SANE_STATUS_GOOD) + if (encoder->DecodeButtonStateResp (small_buffer, 9, state_resp) != DECODE_STATUS_GOOD) { DBG (DBG_WARN, "BrotherUSBDriver::CheckSensor: button info response is invalid.\n"); return SANE_STATUS_IO_ERROR; } + // TODO: Move this into encoder. Seems to be the same for all devices. switch (state_resp.button_value) { case 0x05: @@ -1071,10 +1170,12 @@ BrotherUSBDriver::BrotherUSBDriver (const char *devicename, BrotherFamily family is_scanning (false), was_cancelled(false), devicename (nullptr), + next_frame_number(0), fd (0), small_buffer {0}, data_buffer (nullptr), - data_buffer_bytes (0) + data_buffer_bytes (0), + out_of_docs(false) { this->devicename = strdup(devicename); } diff --git a/backend/brother_mfp/brother_mfp-driver.h b/backend/brother_mfp/brother_mfp-driver.h index b674278dd..120bcb7c1 100644 --- a/backend/brother_mfp/brother_mfp-driver.h +++ b/backend/brother_mfp/brother_mfp-driver.h @@ -107,38 +107,36 @@ public: */ SANE_Status SetScanMode (BrotherScanMode scan_mode) { - return encoder->SetScanMode(scan_mode); + return encoder->DecodeStatusToSaneStatus (encoder->SetScanMode (scan_mode)); } SANE_Status SetRes (SANE_Int x, SANE_Int y) { - return encoder->SetRes(x, y); + return encoder->DecodeStatusToSaneStatus (encoder->SetRes (x, y)); } SANE_Status SetContrast (SANE_Int contrast) { - return encoder->SetContrast(contrast); + return encoder->DecodeStatusToSaneStatus (encoder->SetContrast (contrast)); } SANE_Status SetBrightness (SANE_Int brightness) { - return encoder->SetBrightness(brightness); + return encoder->DecodeStatusToSaneStatus (encoder->SetBrightness (brightness)); } SANE_Status SetCompression (SANE_Bool compression) { - return encoder->SetCompression(compression); + return encoder->DecodeStatusToSaneStatus (encoder->SetCompression (compression)); } - SANE_Status SetScanDimensions (SANE_Int pixel_x_offset, - SANE_Int pixel_x_width, - SANE_Int pixel_y_offset, - SANE_Int pixel_y_height) + SANE_Status SetScanDimensions (SANE_Int pixel_x_offset, SANE_Int pixel_x_width, + SANE_Int pixel_y_offset, SANE_Int pixel_y_height) { - return encoder->SetScanDimensions (pixel_x_offset, - pixel_x_width, - pixel_y_offset, - pixel_y_height); + return encoder->DecodeStatusToSaneStatus (encoder->SetScanDimensions (pixel_x_offset, + pixel_x_width, + pixel_y_offset, + pixel_y_height)); } protected: @@ -185,10 +183,13 @@ private: bool is_scanning; bool was_cancelled; char *devicename; + SANE_Int next_frame_number; SANE_Int fd; SANE_Byte small_buffer[1024]; SANE_Byte *data_buffer; size_t data_buffer_bytes; + bool out_of_docs; + }; diff --git a/backend/brother_mfp/brother_mfp-encoder.cpp b/backend/brother_mfp/brother_mfp-encoder.cpp index e7b094dac..8dc830fea 100644 --- a/backend/brother_mfp/brother_mfp-encoder.cpp +++ b/backend/brother_mfp/brother_mfp-encoder.cpp @@ -49,9 +49,9 @@ * Block header identifiers. * */ -#define BROTHER_DATA_BLOCK_NO_DATA 0x80 -#define BROTHER_DATA_BLOCK_MORE_FRAMES 0x81 -#define BROTHER_DATA_BLOCK_END_OF_FRAME 0x82 +#define BROTHER_DATA_BLOCK_SCAN_FINISHED 0x80 +#define BROTHER_DATA_BLOCK_END_OF_FRAME_MORE 0x81 +#define BROTHER_DATA_BLOCK_END_OF_FRAME_NO_MORE 0x82 #define BROTHER_DATA_BLOCK_CANCEL 0x86 #define BROTHER_DATA_BLOCK_JPEG 0x64 #define BROTHER_DATA_BLOCK_GRAY_RAW 0x40 @@ -69,7 +69,7 @@ const char* BrotherEncoder::ScanModeToText (BrotherScanMode scan_mode) } -SANE_Status BrotherEncoder::SetScanMode (BrotherScanMode scan_mode) +DecodeStatus BrotherEncoder::SetScanMode (BrotherScanMode scan_mode) { /* @@ -77,10 +77,10 @@ SANE_Status BrotherEncoder::SetScanMode (BrotherScanMode scan_mode) */ scan_params.param_scan_mode = scan_mode; - return SANE_STATUS_GOOD; + return DECODE_STATUS_GOOD; } -SANE_Status BrotherEncoder::SetRes (SANE_Int x, SANE_Int y) +DecodeStatus BrotherEncoder::SetRes (SANE_Int x, SANE_Int y) { /* @@ -89,10 +89,10 @@ SANE_Status BrotherEncoder::SetRes (SANE_Int x, SANE_Int y) scan_params.param_x_res = x; scan_params.param_y_res = y; - return SANE_STATUS_GOOD; + return DECODE_STATUS_GOOD; } -SANE_Status BrotherEncoder::SetContrast (SANE_Int contrast) +DecodeStatus BrotherEncoder::SetContrast (SANE_Int contrast) { /* @@ -100,10 +100,10 @@ SANE_Status BrotherEncoder::SetContrast (SANE_Int contrast) */ scan_params.param_contrast = contrast; - return SANE_STATUS_GOOD; + return DECODE_STATUS_GOOD; } -SANE_Status BrotherEncoder::SetBrightness (SANE_Int brightness) +DecodeStatus BrotherEncoder::SetBrightness (SANE_Int brightness) { /* @@ -111,21 +111,18 @@ SANE_Status BrotherEncoder::SetBrightness (SANE_Int brightness) */ scan_params.param_brightness = brightness; - return SANE_STATUS_GOOD; + return DECODE_STATUS_GOOD; } -SANE_Status BrotherEncoder::SetCompression (SANE_Bool compression) +DecodeStatus BrotherEncoder::SetCompression (SANE_Bool compression) { scan_params.param_compression = compression; - return SANE_STATUS_GOOD; + return DECODE_STATUS_GOOD; } -SANE_Status -BrotherEncoder::SetScanDimensions (SANE_Int pixel_x_offset, - SANE_Int pixel_x_width, - SANE_Int pixel_y_offset, - SANE_Int pixel_y_height) +DecodeStatus BrotherEncoder::SetScanDimensions (SANE_Int pixel_x_offset, SANE_Int pixel_x_width, + SANE_Int pixel_y_offset, SANE_Int pixel_y_height) { /* @@ -143,7 +140,7 @@ BrotherEncoder::SetScanDimensions (SANE_Int pixel_x_offset, pixel_y_offset, pixel_y_height); - return SANE_STATUS_GOOD; + return DECODE_STATUS_GOOD; } /* @@ -152,7 +149,7 @@ BrotherEncoder::SetScanDimensions (SANE_Int pixel_x_offset, * -------------------------------------------- * */ -SANE_Status BrotherEncoderFamily2::DecodeSessionResp (const SANE_Byte *data, size_t data_len, +DecodeStatus BrotherEncoderFamily2::DecodeSessionResp (const SANE_Byte *data, size_t data_len, BrotherSessionResponse &response) { /* @@ -169,7 +166,7 @@ SANE_Status BrotherEncoderFamily2::DecodeSessionResp (const SANE_Byte *data, siz "BrotherEncoderFamily2::DecodeSessionResp: invalid session response block: len = %zu\n", data_len); - return SANE_STATUS_IO_ERROR; + return DECODE_STATUS_ERROR; } /* @@ -183,7 +180,7 @@ SANE_Status BrotherEncoderFamily2::DecodeSessionResp (const SANE_Byte *data, siz "BrotherEncoderFamily2::DecodeSessionResp: invalid session response: data[4]=%u\n", (unsigned int) data[4]); - return SANE_STATUS_IO_ERROR; + return DECODE_STATUS_ERROR; } /* @@ -192,7 +189,7 @@ SANE_Status BrotherEncoderFamily2::DecodeSessionResp (const SANE_Byte *data, siz */ response.ready = data[4] == 0x00? SANE_TRUE: SANE_FALSE; - return SANE_STATUS_GOOD; + return DECODE_STATUS_GOOD; } /* @@ -201,8 +198,8 @@ SANE_Status BrotherEncoderFamily2::DecodeSessionResp (const SANE_Byte *data, siz * as snprintf() will require space for a terminating NUL. * */ -SANE_Status BrotherEncoderFamily2::EncodeBasicParameterBlock (SANE_Byte *data, size_t data_len, - size_t *length) +DecodeStatus BrotherEncoderFamily2::EncodeBasicParameterBlock (SANE_Byte *data, size_t data_len, + size_t *length) { const char *mode_text = ScanModeToText (scan_params.param_scan_mode); if (nullptr == mode_text) @@ -210,7 +207,7 @@ SANE_Status BrotherEncoderFamily2::EncodeBasicParameterBlock (SANE_Byte *data, s DBG (DBG_SERIOUS, "BrotherEncoderFamily2::EncodeBasicParameterBlock: failed to get scan mode text for mode %d\n", scan_params.param_scan_mode); - return SANE_STATUS_INVAL; + return DECODE_STATUS_INVAL; } *length = snprintf ((char*) data, @@ -225,15 +222,15 @@ SANE_Status BrotherEncoderFamily2::EncodeBasicParameterBlock (SANE_Byte *data, s DBG (DBG_SERIOUS, "BrotherEncoderFamily2::EncodeBasicParameterBlock: parameter block too long for buffer: %zu\n", *length); - return SANE_STATUS_INVAL; + return DECODE_STATUS_INVAL; } - return SANE_STATUS_GOOD; + return DECODE_STATUS_GOOD; } -SANE_Status BrotherEncoderFamily2::DecodeBasicParameterBlockResp (const SANE_Byte *data, size_t data_len, - BrotherBasicParamResponse &response) +DecodeStatus BrotherEncoderFamily2::DecodeBasicParameterBlockResp (const SANE_Byte *data, size_t data_len, + BrotherBasicParamResponse &response) { /* * TODO: Decode this block. @@ -244,13 +241,36 @@ SANE_Status BrotherEncoderFamily2::DecodeBasicParameterBlockResp (const SANE_Byt (void)data_len; (void)response; - return SANE_STATUS_GOOD; + return DECODE_STATUS_GOOD; } +DecodeStatus BrotherEncoderFamily2::EncodeParameterBlockBlank (SANE_Byte *data, size_t data_len, + size_t *length) +{ + const char *mode_text = ScanModeToText (scan_params.param_scan_mode); + if (nullptr == mode_text) + { + DBG (DBG_SERIOUS, + "BrotherEncoderFamily2::EncodeParameterBlockBlank: failed to get scan mode text for mode %d\n", + scan_params.param_scan_mode); + return DECODE_STATUS_INVAL; + } + *length = snprintf ((char*) data, data_len, "\x1b" "X\n\x80"); -SANE_Status BrotherEncoderFamily2::EncodeParameterBlock (SANE_Byte *data, size_t data_len, - size_t *length) + if (*length > data_len) + { + DBG (DBG_SERIOUS, + "BrotherEncoderFamily2::EncodeParameterBlockBlank: parameter block too long for buffer: %zu\n", + *length); + return DECODE_STATUS_INVAL; + } + + return DECODE_STATUS_GOOD; +} + +DecodeStatus BrotherEncoderFamily2::EncodeParameterBlock (SANE_Byte *data, size_t data_len, + size_t *length) { const char *mode_text = ScanModeToText (scan_params.param_scan_mode); if (nullptr == mode_text) @@ -258,7 +278,7 @@ SANE_Status BrotherEncoderFamily2::EncodeParameterBlock (SANE_Byte *data, size_t DBG (DBG_SERIOUS, "BrotherEncoderFamily2::EncodeBasicParameterBlock: failed to get scan mode text for mode %d\n", scan_params.param_scan_mode); - return SANE_STATUS_INVAL; + return DECODE_STATUS_INVAL; } *length = snprintf ((char*) data, @@ -289,19 +309,18 @@ SANE_Status BrotherEncoderFamily2::EncodeParameterBlock (SANE_Byte *data, size_t DBG (DBG_SERIOUS, "BrotherEncoderFamily2::EncodeBasicParameterBlock: parameter block too long for buffer: %zu\n", *length); - return SANE_STATUS_INVAL; + return DECODE_STATUS_INVAL; } - return SANE_STATUS_GOOD; + return DECODE_STATUS_GOOD; } - -SANE_Status BrotherEncoderFamily2::DecodeScanData (const SANE_Byte *src_data, size_t src_data_len, - size_t *src_data_consumed, SANE_Byte *dest_data, - size_t dest_data_len, size_t *dest_data_written) +DecodeStatus BrotherEncoderFamily2::DecodeScanData (const SANE_Byte *src_data, size_t src_data_len, + size_t *src_data_consumed, SANE_Byte *dest_data, + size_t dest_data_len, size_t *dest_data_written) { DBG (DBG_EVENT, "BrotherEncoderFamily2::DecodeScanData: \n"); - SANE_Status ret_status = SANE_STATUS_GOOD; + DecodeStatus ret_status = DECODE_STATUS_GOOD; *src_data_consumed = 0; *dest_data_written = 0; @@ -337,34 +356,35 @@ SANE_Status BrotherEncoderFamily2::DecodeScanData (const SANE_Byte *src_data, si if (current_header.block_type == 0) { size_t header_consumed = 0; - DecodeStatus res = DecodeScanDataHeader(src_data, src_data_len, &header_consumed, current_header); - if (res == DECODE_STATUS_TRUNCATED) + ret_status = DecodeScanDataHeader(src_data, src_data_len, &header_consumed, current_header); + if (ret_status == DECODE_STATUS_TRUNCATED) { /* * This means we don't have enough data to decode a header yet. * Try again next time if we have read more data. * */ + ret_status = DECODE_STATUS_GOOD; break; } - if (res == DECODE_STATUS_CANCEL) + if (ret_status == DECODE_STATUS_CANCEL) { DBG (DBG_IMPORTANT, "BrotherEncoderFamily2::DecodeScanData: end of data detected\n"); - ret_status = SANE_STATUS_CANCELLED; break; } /* * Detect special case situations. * - * TODO: We need to be able to alert the difference between these - * two things to the caller somehow. They are not the same!! - * */ - if (res == DECODE_STATUS_ENDOFDATA) + if ((ret_status == DECODE_STATUS_ENDOFDATA) + || (ret_status == DECODE_STATUS_ENDOFFRAME_WITH_MORE)) { - DBG (DBG_IMPORTANT, "BrotherEncoderFamily2::DecodeScanData: end of data detected\n"); + DBG (DBG_IMPORTANT, + "BrotherEncoderFamily2::DecodeScanData: %s detected.\n", + ret_status == DECODE_STATUS_ENDOFDATA ? "ENDOFDATA" : "ENDOFFRAME_WITH_MORE"); + current_header.block_type = 0; /* @@ -374,21 +394,22 @@ SANE_Status BrotherEncoderFamily2::DecodeScanData (const SANE_Byte *src_data, si * We don't consume this header so we will see it the next time around. * */ - ret_status = orig_dest_data_len > dest_data_len? SANE_STATUS_GOOD: SANE_STATUS_EOF; + if (orig_dest_data_len > dest_data_len) + { + ret_status = DECODE_STATUS_GOOD; + } + else + { + // Consume the header. + src_data += header_consumed; + src_data_len -= header_consumed; + } break; } - if (res == DECODE_STATUS_ENDOFFRAME) - { - DBG (DBG_IMPORTANT, "BrotherEncoderFamily2::DecodeScanData: end of frame detected\n"); - current_header.block_type = 0; - ret_status = orig_dest_data_len > dest_data_len? SANE_STATUS_GOOD: SANE_STATUS_EOF; - break; - } - if (res != DECODE_STATUS_GOOD) + if (ret_status != DECODE_STATUS_GOOD) { DBG (DBG_IMPORTANT, "BrotherEncoderFamily2::DecodeScanData: failed to decode header\n"); current_header.block_type = 0; - ret_status = SANE_STATUS_IO_ERROR; break; } @@ -409,7 +430,6 @@ SANE_Status BrotherEncoderFamily2::DecodeScanData (const SANE_Byte *src_data, si * decode it, hoping to do more next time. * */ - DecodeStatus res; size_t bytes_consumed = 0; size_t bytes_written = 0; size_t in_len = std::min(src_data_len, current_header.block_len); @@ -423,7 +443,7 @@ SANE_Status BrotherEncoderFamily2::DecodeScanData (const SANE_Byte *src_data, si colour_decoder.NewBlock (); } - res = colour_decoder.DecodeScanData (src_data, + ret_status = colour_decoder.DecodeScanData (src_data, in_len, &bytes_consumed, dest_data, @@ -437,7 +457,7 @@ SANE_Status BrotherEncoderFamily2::DecodeScanData (const SANE_Byte *src_data, si jfif_decoder.NewBlock (); } - res = jfif_decoder.DecodeScanData (src_data, + ret_status = jfif_decoder.DecodeScanData (src_data, in_len, &bytes_consumed, dest_data, @@ -451,7 +471,7 @@ SANE_Status BrotherEncoderFamily2::DecodeScanData (const SANE_Byte *src_data, si gray_decoder.NewBlock (); } - res = gray_decoder.DecodeScanData (src_data, + ret_status = gray_decoder.DecodeScanData (src_data, in_len, &bytes_consumed, dest_data, @@ -465,7 +485,7 @@ SANE_Status BrotherEncoderFamily2::DecodeScanData (const SANE_Byte *src_data, si gray_raw_decoder.NewBlock (); } - res = gray_raw_decoder.DecodeScanData (src_data, + ret_status = gray_raw_decoder.DecodeScanData (src_data, in_len, &bytes_consumed, dest_data, @@ -477,17 +497,15 @@ SANE_Status BrotherEncoderFamily2::DecodeScanData (const SANE_Byte *src_data, si DBG (DBG_IMPORTANT, "BrotherEncoderFamily2::DecodeScanData: unknown block encountered: 0x%2.2x\n", (unsigned int) current_header.block_type); - ret_status = SANE_STATUS_IO_ERROR; + ret_status = DECODE_STATUS_ERROR; break; } - DBG (DBG_DETAIL, - "BrotherEncoderFamily2::DecodeScanData: written=%zu\n", - bytes_written); - if (res != DECODE_STATUS_GOOD) + DBG (DBG_DETAIL, "BrotherEncoderFamily2::DecodeScanData: written=%zu\n", bytes_written); + + if (ret_status != DECODE_STATUS_GOOD) { current_header.block_type = 0; - ret_status = SANE_STATUS_IO_ERROR; break; } @@ -526,26 +544,15 @@ SANE_Status BrotherEncoderFamily2::DecodeScanData (const SANE_Byte *src_data, si } } - if (ret_status == SANE_STATUS_GOOD) - { +// if (ret_status == DECODE_STATUS_GOOD) +// { *src_data_consumed = orig_src_data_len - src_data_len; *dest_data_written = orig_dest_data_len - dest_data_len; - } +// } return ret_status; } -/* - * EndOfFrame (0x82) is 10 bytes long because it doesn't have the - * 2-byte packet length that the others do, so we could assume that - * the packet length is not part of the header really. - * However, it is convenient in coding terms to assume that it does. - * - * 0x80 - single byte end of data. - * 0x82 - end of frame, with 10 bytes - * Others - 12 bytes, including little endian length at the end. - * - */ DecodeStatus BrotherEncoderFamily2::DecodeScanDataHeader (const SANE_Byte *src_data, size_t src_data_len, size_t *src_data_consumed, @@ -560,29 +567,24 @@ DecodeStatus BrotherEncoderFamily2::DecodeScanDataHeader (const SANE_Byte *src_d header.block_len = 0; - if (header.block_type == BROTHER_DATA_BLOCK_NO_DATA) + if (header.block_type == BROTHER_DATA_BLOCK_SCAN_FINISHED) { *src_data_consumed = 1; return DECODE_STATUS_ENDOFDATA; } + if (header.block_type == BROTHER_DATA_BLOCK_END_OF_FRAME_MORE) + { + *src_data_consumed = 1; + return DECODE_STATUS_ENDOFFRAME_WITH_MORE; + } + if (header.block_type == BROTHER_DATA_BLOCK_CANCEL) { *src_data_consumed = 1; return DECODE_STATUS_CANCEL; } - if (header.block_type == BROTHER_DATA_BLOCK_END_OF_FRAME) - { - if (src_data_len < 10) - { - return DECODE_STATUS_TRUNCATED; - } - - *src_data_consumed = 10; - return DECODE_STATUS_ENDOFFRAME; - } - /* * Other data-carrying packets. * @@ -600,29 +602,29 @@ DecodeStatus BrotherEncoderFamily2::DecodeScanDataHeader (const SANE_Byte *src_d } -SANE_Status BrotherEncoderFamily2::DecodeButtonQueryResp (const SANE_Byte *data, size_t data_len, +DecodeStatus BrotherEncoderFamily2::DecodeButtonQueryResp (const SANE_Byte *data, size_t data_len, BrotherButtonQueryResponse &response) { if ((data_len != 4) || (data[0] != 0x04) ||(data[1] != 0x10) || (data[2] != 0x03)) { - return SANE_STATUS_IO_ERROR; + return DECODE_STATUS_ERROR; } response.has_button_press = data[3] == 0x10; - return SANE_STATUS_GOOD; + return DECODE_STATUS_GOOD; } -SANE_Status BrotherEncoderFamily2::DecodeButtonStateResp (const SANE_Byte *data, size_t data_len, - BrotherButtonStateResponse &response) +DecodeStatus BrotherEncoderFamily2::DecodeButtonStateResp (const SANE_Byte *data, size_t data_len, + BrotherButtonStateResponse &response) { if ((data_len != 9) || (memcmp(data, "\x09" "\x10" "\x03" "\x20", 4) != 0)) { - return SANE_STATUS_IO_ERROR; + return DECODE_STATUS_ERROR; } response.button_value = data[4]; - return SANE_STATUS_GOOD; + return DECODE_STATUS_GOOD; } @@ -633,8 +635,8 @@ SANE_Status BrotherEncoderFamily2::DecodeButtonStateResp (const SANE_Byte *data, * */ -SANE_Status BrotherEncoderFamily3::DecodeSessionResp (const SANE_Byte *data, size_t data_len, - BrotherSessionResponse &response) +DecodeStatus BrotherEncoderFamily3::DecodeSessionResp (const SANE_Byte *data, size_t data_len, + BrotherSessionResponse &response) { /* * Formatting and content checks. @@ -650,7 +652,7 @@ SANE_Status BrotherEncoderFamily3::DecodeSessionResp (const SANE_Byte *data, siz "BrotherEncoderFamily3::DecodeSessionResp: invalid session response block: len = %zu\n", data_len); - return SANE_STATUS_IO_ERROR; + return DECODE_STATUS_ERROR; } /* @@ -664,7 +666,7 @@ SANE_Status BrotherEncoderFamily3::DecodeSessionResp (const SANE_Byte *data, siz "BrotherEncoderFamily3::DecodeSessionResp: invalid session response: data[4]=%u\n", (unsigned int) data[4]); - return SANE_STATUS_IO_ERROR; + return DECODE_STATUS_ERROR; } /* @@ -673,7 +675,7 @@ SANE_Status BrotherEncoderFamily3::DecodeSessionResp (const SANE_Byte *data, siz */ response.ready = data[4] == 0x00? SANE_TRUE: SANE_FALSE; - return SANE_STATUS_GOOD; + return DECODE_STATUS_GOOD; } /* @@ -682,8 +684,8 @@ SANE_Status BrotherEncoderFamily3::DecodeSessionResp (const SANE_Byte *data, siz * as snprintf() will require space for a terminating NUL. * */ -SANE_Status BrotherEncoderFamily3::EncodeBasicParameterBlock (SANE_Byte *data, size_t data_len, - size_t *length) +DecodeStatus BrotherEncoderFamily3::EncodeBasicParameterBlock (SANE_Byte *data, size_t data_len, + size_t *length) { const char *mode_text = ScanModeToText (scan_params.param_scan_mode); if (nullptr == mode_text) @@ -691,7 +693,7 @@ SANE_Status BrotherEncoderFamily3::EncodeBasicParameterBlock (SANE_Byte *data, s DBG (DBG_SERIOUS, "BrotherEncoderFamily3::EncodeBasicParameterBlock: failed to get scan mode text for mode %d\n", scan_params.param_scan_mode); - return SANE_STATUS_INVAL; + return DECODE_STATUS_INVAL; } *length = snprintf ((char*) data, @@ -706,14 +708,14 @@ SANE_Status BrotherEncoderFamily3::EncodeBasicParameterBlock (SANE_Byte *data, s DBG (DBG_SERIOUS, "BrotherEncoderFamily3::EncodeBasicParameterBlock: parameter block too long for buffer: %zu\n", *length); - return SANE_STATUS_INVAL; + return DECODE_STATUS_INVAL; } - return SANE_STATUS_GOOD; + return DECODE_STATUS_GOOD; } -SANE_Status BrotherEncoderFamily3::DecodeBasicParameterBlockResp (const SANE_Byte *data, size_t data_len, +DecodeStatus BrotherEncoderFamily3::DecodeBasicParameterBlockResp (const SANE_Byte *data, size_t data_len, BrotherBasicParamResponse &response) { /* @@ -725,13 +727,36 @@ SANE_Status BrotherEncoderFamily3::DecodeBasicParameterBlockResp (const SANE_Byt (void)data_len; (void)response; - return SANE_STATUS_GOOD; + return DECODE_STATUS_GOOD; } +DecodeStatus BrotherEncoderFamily3::EncodeParameterBlockBlank (SANE_Byte *data, size_t data_len, + size_t *length) +{ + const char *mode_text = ScanModeToText (scan_params.param_scan_mode); + if (nullptr == mode_text) + { + DBG (DBG_SERIOUS, + "BrotherEncoderFamily3::EncodeParameterBlockBlank: failed to get scan mode text for mode %d\n", + scan_params.param_scan_mode); + return DECODE_STATUS_INVAL; + } + *length = snprintf ((char*) data, data_len, "\x1b" "X\n\x80"); -SANE_Status BrotherEncoderFamily3::EncodeParameterBlock (SANE_Byte *data, size_t data_len, - size_t *length) + if (*length > data_len) + { + DBG (DBG_SERIOUS, + "BrotherEncoderFamily3::EncodeParameterBlockBlank: parameter block too long for buffer: %zu\n", + *length); + return DECODE_STATUS_INVAL; + } + + return DECODE_STATUS_GOOD; +} + +DecodeStatus BrotherEncoderFamily3::EncodeParameterBlock (SANE_Byte *data, size_t data_len, + size_t *length) { const char *mode_text = ScanModeToText (scan_params.param_scan_mode); if (nullptr == mode_text) @@ -739,7 +764,7 @@ SANE_Status BrotherEncoderFamily3::EncodeParameterBlock (SANE_Byte *data, size_t DBG (DBG_SERIOUS, "BrotherEncoderFamily3::EncodeBasicParameterBlock: failed to get scan mode text for mode %d\n", scan_params.param_scan_mode); - return SANE_STATUS_INVAL; + return DECODE_STATUS_INVAL; } *length = snprintf ((char*) data, @@ -770,19 +795,18 @@ SANE_Status BrotherEncoderFamily3::EncodeParameterBlock (SANE_Byte *data, size_t DBG (DBG_SERIOUS, "BrotherEncoderFamily3::EncodeBasicParameterBlock: parameter block too long for buffer: %zu\n", *length); - return SANE_STATUS_INVAL; + return DECODE_STATUS_INVAL; } - return SANE_STATUS_GOOD; + return DECODE_STATUS_GOOD; } - -SANE_Status BrotherEncoderFamily3::DecodeScanData (const SANE_Byte *src_data, size_t src_data_len, - size_t *src_data_consumed, SANE_Byte *dest_data, - size_t dest_data_len, size_t *dest_data_written) +DecodeStatus BrotherEncoderFamily3::DecodeScanData (const SANE_Byte *src_data, size_t src_data_len, + size_t *src_data_consumed, SANE_Byte *dest_data, + size_t dest_data_len, size_t *dest_data_written) { DBG (DBG_EVENT, "BrotherEncoderFamily3::DecodeScanData: \n"); - SANE_Status ret_status = SANE_STATUS_GOOD; + DecodeStatus ret_status = DECODE_STATUS_GOOD; *src_data_consumed = 0; *dest_data_written = 0; @@ -818,34 +842,35 @@ SANE_Status BrotherEncoderFamily3::DecodeScanData (const SANE_Byte *src_data, si if (current_header.block_type == 0) { size_t header_consumed = 0; - DecodeStatus res = DecodeScanDataHeader(src_data, src_data_len, &header_consumed, current_header); - if (res == DECODE_STATUS_TRUNCATED) + ret_status = DecodeScanDataHeader(src_data, src_data_len, &header_consumed, current_header); + if (ret_status == DECODE_STATUS_TRUNCATED) { /* * This means we don't have enough data to decode a header yet. * Try again next time if we have read more data. * */ + ret_status = DECODE_STATUS_GOOD; break; } - if (res == DECODE_STATUS_CANCEL) + if (ret_status == DECODE_STATUS_CANCEL) { DBG (DBG_IMPORTANT, "BrotherEncoderFamily3::DecodeScanData: end of data detected\n"); - ret_status = SANE_STATUS_CANCELLED; break; } /* - * Detect special case situations. - * - * TODO: We need to be able to alert the difference between these - * two things to the caller somehow. They are not the same!! - * - */ - if (res == DECODE_STATUS_ENDOFDATA) + * Detect special case situations. + * + */ + if ((ret_status == DECODE_STATUS_ENDOFDATA) + || (ret_status == DECODE_STATUS_ENDOFFRAME_WITH_MORE)) { - DBG (DBG_IMPORTANT, "BrotherEncoderFamily3::DecodeScanData: end of data detected\n"); + DBG (DBG_IMPORTANT, + "BrotherEncoderFamily3::DecodeScanData: %s.\n", + ret_status == DECODE_STATUS_ENDOFDATA ? "ENDOFDATA" : "ENDOFFRAME_WITH_MORE"); + current_header.block_type = 0; /* @@ -855,21 +880,22 @@ SANE_Status BrotherEncoderFamily3::DecodeScanData (const SANE_Byte *src_data, si * We don't consume this header so we will see it the next time around. * */ - ret_status = orig_dest_data_len > dest_data_len? SANE_STATUS_GOOD: SANE_STATUS_EOF; + if (orig_dest_data_len > dest_data_len) + { + ret_status = DECODE_STATUS_GOOD; + } + else + { + // Consume the header. + src_data += header_consumed; + src_data_len -= header_consumed; + } break; } - if (res == DECODE_STATUS_ENDOFFRAME) - { - DBG (DBG_IMPORTANT, "BrotherEncoderFamily3::DecodeScanData: end of frame detected\n"); - current_header.block_type = 0; - ret_status = orig_dest_data_len > dest_data_len? SANE_STATUS_GOOD: SANE_STATUS_EOF; - break; - } - if (res != DECODE_STATUS_GOOD) + if (ret_status != DECODE_STATUS_GOOD) { DBG (DBG_IMPORTANT, "BrotherEncoderFamily3::DecodeScanData: failed to decode header\n"); current_header.block_type = 0; - ret_status = SANE_STATUS_IO_ERROR; break; } @@ -890,7 +916,6 @@ SANE_Status BrotherEncoderFamily3::DecodeScanData (const SANE_Byte *src_data, si * decode it, hoping to do more next time. * */ - DecodeStatus res; size_t bytes_consumed = 0; size_t bytes_written = 0; size_t in_len = std::min(src_data_len, current_header.block_len); @@ -904,7 +929,7 @@ SANE_Status BrotherEncoderFamily3::DecodeScanData (const SANE_Byte *src_data, si colour_decoder.NewBlock (); } - res = colour_decoder.DecodeScanData (src_data, + ret_status = colour_decoder.DecodeScanData (src_data, in_len, &bytes_consumed, dest_data, @@ -918,7 +943,7 @@ SANE_Status BrotherEncoderFamily3::DecodeScanData (const SANE_Byte *src_data, si gray_decoder.NewBlock (); } - res = gray_decoder.DecodeScanData (src_data, + ret_status = gray_decoder.DecodeScanData (src_data, in_len, &bytes_consumed, dest_data, @@ -932,7 +957,7 @@ SANE_Status BrotherEncoderFamily3::DecodeScanData (const SANE_Byte *src_data, si gray_raw_decoder.NewBlock (); } - res = gray_raw_decoder.DecodeScanData (src_data, + ret_status = gray_raw_decoder.DecodeScanData (src_data, in_len, &bytes_consumed, dest_data, @@ -946,7 +971,7 @@ SANE_Status BrotherEncoderFamily3::DecodeScanData (const SANE_Byte *src_data, si jfif_decoder.NewBlock (); } - res = jfif_decoder.DecodeScanData (src_data, + ret_status = jfif_decoder.DecodeScanData (src_data, in_len, &bytes_consumed, dest_data, @@ -958,17 +983,17 @@ SANE_Status BrotherEncoderFamily3::DecodeScanData (const SANE_Byte *src_data, si DBG (DBG_IMPORTANT, "BrotherEncoderFamily3::DecodeScanData: unknown block encountered: 0x%2.2x\n", (unsigned int) current_header.block_type); - ret_status = SANE_STATUS_IO_ERROR; + ret_status = DECODE_STATUS_ERROR; break; } DBG (DBG_DETAIL, "BrotherEncoderFamily3::DecodeScanData: written=%zu\n", bytes_written); - if (res != DECODE_STATUS_GOOD) + + if (ret_status != DECODE_STATUS_GOOD) { current_header.block_type = 0; - ret_status = SANE_STATUS_IO_ERROR; break; } @@ -1007,11 +1032,8 @@ SANE_Status BrotherEncoderFamily3::DecodeScanData (const SANE_Byte *src_data, si } } - if (ret_status == SANE_STATUS_GOOD) - { - *src_data_consumed = orig_src_data_len - src_data_len; - *dest_data_written = orig_dest_data_len - dest_data_len; - } + *src_data_consumed = orig_src_data_len - src_data_len; + *dest_data_written = orig_dest_data_len - dest_data_len; return ret_status; } @@ -1041,19 +1063,25 @@ DecodeStatus BrotherEncoderFamily3::DecodeScanDataHeader (const SANE_Byte *src_d header.block_len = 0; - if (header.block_type == BROTHER_DATA_BLOCK_NO_DATA) + if (header.block_type == BROTHER_DATA_BLOCK_SCAN_FINISHED) { *src_data_consumed = 1; return DECODE_STATUS_ENDOFDATA; } + if (header.block_type == BROTHER_DATA_BLOCK_END_OF_FRAME_MORE) + { + *src_data_consumed = 1; + return DECODE_STATUS_ENDOFFRAME_WITH_MORE; + } + if (header.block_type == BROTHER_DATA_BLOCK_CANCEL) { *src_data_consumed = 1; return DECODE_STATUS_CANCEL; } - if (header.block_type == BROTHER_DATA_BLOCK_END_OF_FRAME) + if (header.block_type == BROTHER_DATA_BLOCK_END_OF_FRAME_NO_MORE) { if (src_data_len < 10) { @@ -1061,7 +1089,7 @@ DecodeStatus BrotherEncoderFamily3::DecodeScanDataHeader (const SANE_Byte *src_d } *src_data_consumed = 10; - return DECODE_STATUS_ENDOFFRAME; + return DECODE_STATUS_ENDOFFRAME_NO_MORE; } /* @@ -1081,29 +1109,29 @@ DecodeStatus BrotherEncoderFamily3::DecodeScanDataHeader (const SANE_Byte *src_d } -SANE_Status BrotherEncoderFamily3::DecodeButtonQueryResp (const SANE_Byte *data, size_t data_len, - BrotherButtonQueryResponse &response) +DecodeStatus BrotherEncoderFamily3::DecodeButtonQueryResp (const SANE_Byte *data, size_t data_len, + BrotherButtonQueryResponse &response) { if ((data_len != 4) || (data[0] != 0x04) ||(data[1] != 0x10) || (data[2] != 0x03)) { - return SANE_STATUS_IO_ERROR; + return DECODE_STATUS_ERROR; } response.has_button_press = data[3] == 0x10; - return SANE_STATUS_GOOD; + return DECODE_STATUS_GOOD; } -SANE_Status BrotherEncoderFamily3::DecodeButtonStateResp (const SANE_Byte *data, size_t data_len, - BrotherButtonStateResponse &response) +DecodeStatus BrotherEncoderFamily3::DecodeButtonStateResp (const SANE_Byte *data, size_t data_len, + BrotherButtonStateResponse &response) { if ((data_len != 9) || (memcmp(data, "\x09" "\x10" "\x03" "\x20", 4) != 0)) { - return SANE_STATUS_IO_ERROR; + return DECODE_STATUS_ERROR; } response.button_value = data[4]; - return SANE_STATUS_GOOD; + return DECODE_STATUS_GOOD; } /* @@ -1112,8 +1140,8 @@ SANE_Status BrotherEncoderFamily3::DecodeButtonStateResp (const SANE_Byte *data, * -------------------------------------------- * */ -SANE_Status BrotherEncoderFamily4::DecodeSessionResp (const SANE_Byte *data, size_t data_len, - BrotherSessionResponse &response) +DecodeStatus BrotherEncoderFamily4::DecodeSessionResp (const SANE_Byte *data, size_t data_len, + BrotherSessionResponse &response) { /* * Formatting and content checks. @@ -1129,7 +1157,7 @@ SANE_Status BrotherEncoderFamily4::DecodeSessionResp (const SANE_Byte *data, siz "BrotherEncoderFamily4::DecodeSessionResp: invalid session response block: len = %zu\n", data_len); - return SANE_STATUS_IO_ERROR; + return DECODE_STATUS_ERROR; } /* @@ -1143,7 +1171,7 @@ SANE_Status BrotherEncoderFamily4::DecodeSessionResp (const SANE_Byte *data, siz "BrotherEncoderFamily4::DecodeSessionResp: invalid session response: data[4]=%u\n", (unsigned int) data[4]); - return SANE_STATUS_IO_ERROR; + return DECODE_STATUS_ERROR; } /* @@ -1152,7 +1180,7 @@ SANE_Status BrotherEncoderFamily4::DecodeSessionResp (const SANE_Byte *data, siz */ response.ready = data[4] == 0x00? SANE_TRUE: SANE_FALSE; - return SANE_STATUS_GOOD; + return DECODE_STATUS_GOOD; } /* @@ -1161,8 +1189,8 @@ SANE_Status BrotherEncoderFamily4::DecodeSessionResp (const SANE_Byte *data, siz * as snprintf() will require space for a terminating NUL. * */ -SANE_Status BrotherEncoderFamily4::EncodeBasicParameterBlock (SANE_Byte *data, size_t data_len, - size_t *length) +DecodeStatus BrotherEncoderFamily4::EncodeBasicParameterBlock (SANE_Byte *data, size_t data_len, + size_t *length) { const char *mode_text = ScanModeToText (scan_params.param_scan_mode); if (nullptr == mode_text) @@ -1170,7 +1198,7 @@ SANE_Status BrotherEncoderFamily4::EncodeBasicParameterBlock (SANE_Byte *data, s DBG (DBG_SERIOUS, "BrotherEncoderFamily4::EncodeBasicParameterBlock: failed to get scan mode text for mode %d\n", scan_params.param_scan_mode); - return SANE_STATUS_INVAL; + return DECODE_STATUS_INVAL; } *length = snprintf ((char*) data, @@ -1185,14 +1213,14 @@ SANE_Status BrotherEncoderFamily4::EncodeBasicParameterBlock (SANE_Byte *data, s DBG (DBG_SERIOUS, "BrotherEncoderFamily4::EncodeBasicParameterBlock: parameter block too long for buffer: %zu\n", *length); - return SANE_STATUS_INVAL; + return DECODE_STATUS_INVAL; } - return SANE_STATUS_GOOD; + return DECODE_STATUS_GOOD; } -SANE_Status BrotherEncoderFamily4::DecodeBasicParameterBlockResp (const SANE_Byte *data, size_t data_len, - BrotherBasicParamResponse &response) +DecodeStatus BrotherEncoderFamily4::DecodeBasicParameterBlockResp (const SANE_Byte *data, size_t data_len, + BrotherBasicParamResponse &response) { /* * TODO: Decode this block. @@ -1203,10 +1231,10 @@ SANE_Status BrotherEncoderFamily4::DecodeBasicParameterBlockResp (const SANE_Byt (void)data_len; (void)response; - return SANE_STATUS_GOOD; + return DECODE_STATUS_GOOD; } -SANE_Status BrotherEncoderFamily4::EncodeADFBlock (SANE_Byte *data, size_t data_len, size_t *length) +DecodeStatus BrotherEncoderFamily4::EncodeADFBlock (SANE_Byte *data, size_t data_len, size_t *length) { *length = snprintf ((char*) data, data_len, "\x1b" "D\nADF\n" "\x80"); @@ -1215,26 +1243,52 @@ SANE_Status BrotherEncoderFamily4::EncodeADFBlock (SANE_Byte *data, size_t data_ DBG (DBG_SERIOUS, "BrotherEncoderFamily4::EncodeBasicParameterBlock: parameter block too long for buffer: %zu\n", *length); - return SANE_STATUS_INVAL; + return DECODE_STATUS_INVAL; } - return SANE_STATUS_GOOD; + return DECODE_STATUS_GOOD; } -SANE_Status BrotherEncoderFamily4::DecodeADFBlockResp (const SANE_Byte *data, size_t data_len, - BrotherADFResponse &response) +DecodeStatus BrotherEncoderFamily4::DecodeADFBlockResp (const SANE_Byte *data, size_t data_len, + BrotherADFResponse &response) { if ((data_len != 1)) { - return SANE_STATUS_IO_ERROR; + return DECODE_STATUS_ERROR; } response.resp_code = data[0]; - return SANE_STATUS_GOOD; + return DECODE_STATUS_GOOD; } -SANE_Status BrotherEncoderFamily4::EncodeParameterBlock (SANE_Byte *data, size_t data_len, +DecodeStatus BrotherEncoderFamily4::EncodeParameterBlockBlank (SANE_Byte *data, size_t data_len, + size_t *length) +{ + const char *mode_text = ScanModeToText (scan_params.param_scan_mode); + if (nullptr == mode_text) + { + DBG (DBG_SERIOUS, + "BrotherEncoderFamily4::EncodeParameterBlockBlank: failed to get scan mode text for mode %d\n", + scan_params.param_scan_mode); + return DECODE_STATUS_INVAL; + } + + *length = snprintf ((char*) data, data_len, "\x1b" "X\n\x80"); + + if (*length > data_len) + { + DBG (DBG_SERIOUS, + "BrotherEncoderFamily4::EncodeParameterBlockBlank: parameter block too long for buffer: %zu\n", + *length); + return DECODE_STATUS_INVAL; + } + + return DECODE_STATUS_GOOD; +} + + +DecodeStatus BrotherEncoderFamily4::EncodeParameterBlock (SANE_Byte *data, size_t data_len, size_t *length) { const char *mode_text = ScanModeToText (scan_params.param_scan_mode); @@ -1243,7 +1297,7 @@ SANE_Status BrotherEncoderFamily4::EncodeParameterBlock (SANE_Byte *data, size_t DBG (DBG_SERIOUS, "BrotherEncoderFamily4::EncodeBasicParameterBlock: failed to get scan mode text for mode %d\n", scan_params.param_scan_mode); - return SANE_STATUS_INVAL; + return DECODE_STATUS_INVAL; } *length = snprintf ((char*) data, @@ -1274,18 +1328,18 @@ SANE_Status BrotherEncoderFamily4::EncodeParameterBlock (SANE_Byte *data, size_t DBG (DBG_SERIOUS, "BrotherEncoderFamily4::EncodeBasicParameterBlock: parameter block too long for buffer: %zu\n", *length); - return SANE_STATUS_INVAL; + return DECODE_STATUS_INVAL; } - return SANE_STATUS_GOOD; + return DECODE_STATUS_GOOD; } -SANE_Status BrotherEncoderFamily4::DecodeScanData (const SANE_Byte *src_data, size_t src_data_len, - size_t *src_data_consumed, SANE_Byte *dest_data, - size_t dest_data_len, size_t *dest_data_written) +DecodeStatus BrotherEncoderFamily4::DecodeScanData (const SANE_Byte *src_data, size_t src_data_len, + size_t *src_data_consumed, SANE_Byte *dest_data, + size_t dest_data_len, size_t *dest_data_written) { DBG (DBG_EVENT, "BrotherEncoderFamily4::DecodeScanData: \n"); - SANE_Status ret_status = SANE_STATUS_GOOD; + DecodeStatus ret_status = DECODE_STATUS_GOOD; *src_data_consumed = 0; *dest_data_written = 0; @@ -1321,34 +1375,38 @@ SANE_Status BrotherEncoderFamily4::DecodeScanData (const SANE_Byte *src_data, si if (current_header.block_type == 0) { size_t header_consumed = 0; - DecodeStatus res = DecodeScanDataHeader(src_data, src_data_len, &header_consumed, current_header); - if (res == DECODE_STATUS_TRUNCATED) + ret_status = DecodeScanDataHeader(src_data, src_data_len, &header_consumed, current_header); + if (ret_status == DECODE_STATUS_TRUNCATED) { /* * This means we don't have enough data to decode a header yet. * Try again next time if we have read more data. * */ + ret_status = DECODE_STATUS_GOOD; break; } - if (res == DECODE_STATUS_CANCEL) + if (ret_status == DECODE_STATUS_CANCEL) { - DBG (DBG_IMPORTANT, "BrotherEncoderFamily2::DecodeScanData: end of data detected\n"); - ret_status = SANE_STATUS_CANCELLED; + DBG (DBG_IMPORTANT, "BrotherEncoderFamily2::DecodeScanData: cancel detected\n"); break; } /* - * Detect special case situations. - * - * TODO: We need to be able to alert the difference between these - * two things to the caller somehow. They are not the same!! - * - */ - if (res == DECODE_STATUS_ENDOFDATA) + * Detect special case situations. + * + */ + if ((ret_status == DECODE_STATUS_ENDOFDATA) + || (ret_status == DECODE_STATUS_ENDOFFRAME_NO_MORE) + || (ret_status == DECODE_STATUS_ENDOFFRAME_WITH_MORE)) { - DBG (DBG_IMPORTANT, "BrotherEncoderFamily4::DecodeScanData: end of data detected\n"); + DBG (DBG_IMPORTANT, + "BrotherEncoderFamily4::DecodeScanData: %s.\n", + ret_status == DECODE_STATUS_ENDOFDATA ? "ENDOFDATA" : + ret_status == DECODE_STATUS_ENDOFFRAME_NO_MORE ? + "ENDOFFRAME_NO_MORE" : "ENDOFFRAME_WITH_MORE"); + current_header.block_type = 0; /* @@ -1358,21 +1416,22 @@ SANE_Status BrotherEncoderFamily4::DecodeScanData (const SANE_Byte *src_data, si * We don't consume this header so we will see it the next time around. * */ - ret_status = orig_dest_data_len > dest_data_len? SANE_STATUS_GOOD: SANE_STATUS_EOF; + if (orig_dest_data_len > dest_data_len) + { + ret_status = DECODE_STATUS_GOOD; + } + else + { + // Consume the header. + src_data += header_consumed; + src_data_len -= header_consumed; + } break; } - if (res == DECODE_STATUS_ENDOFFRAME) - { - DBG (DBG_IMPORTANT, "BrotherEncoderFamily4::DecodeScanData: end of frame detected\n"); - current_header.block_type = 0; - ret_status = orig_dest_data_len > dest_data_len? SANE_STATUS_GOOD: SANE_STATUS_EOF; - break; - } - if (res != DECODE_STATUS_GOOD) + if (ret_status != DECODE_STATUS_GOOD) { DBG (DBG_IMPORTANT, "BrotherEncoderFamily4::DecodeScanData: failed to decode header\n"); current_header.block_type = 0; - ret_status = SANE_STATUS_IO_ERROR; break; } @@ -1393,7 +1452,6 @@ SANE_Status BrotherEncoderFamily4::DecodeScanData (const SANE_Byte *src_data, si * decode it, hoping to do more next time. * */ - DecodeStatus res; size_t bytes_consumed = 0; size_t bytes_written = 0; size_t in_len = std::min(src_data_len, current_header.block_len); @@ -1405,12 +1463,12 @@ SANE_Status BrotherEncoderFamily4::DecodeScanData (const SANE_Byte *src_data, si jfif_decoder.NewBlock (); } - res = jfif_decoder.DecodeScanData (src_data, - in_len, - &bytes_consumed, - dest_data, - dest_data_len, - &bytes_written); + ret_status = jfif_decoder.DecodeScanData (src_data, + in_len, + &bytes_consumed, + dest_data, + dest_data_len, + &bytes_written); } else if (current_header.block_type == BROTHER_DATA_BLOCK_GRAY_RLENGTH) { @@ -1419,12 +1477,12 @@ SANE_Status BrotherEncoderFamily4::DecodeScanData (const SANE_Byte *src_data, si gray_decoder.NewBlock (); } - res = gray_decoder.DecodeScanData (src_data, - in_len, - &bytes_consumed, - dest_data, - dest_data_len, - &bytes_written); + ret_status = gray_decoder.DecodeScanData (src_data, + in_len, + &bytes_consumed, + dest_data, + dest_data_len, + &bytes_written); } else if (current_header.block_type == BROTHER_DATA_BLOCK_GRAY_RAW) { @@ -1433,26 +1491,24 @@ SANE_Status BrotherEncoderFamily4::DecodeScanData (const SANE_Byte *src_data, si gray_raw_decoder.NewBlock (); } - res = gray_raw_decoder.DecodeScanData (src_data, - in_len, - &bytes_consumed, - dest_data, - dest_data_len, - &bytes_written); + ret_status = gray_raw_decoder.DecodeScanData (src_data, + in_len, + &bytes_consumed, + dest_data, + dest_data_len, + &bytes_written); } else { DBG (DBG_IMPORTANT, "BrotherEncoderFamily4::DecodeScanData: unknown block encountered: 0x%2.2x\n", (unsigned int) current_header.block_type); - ret_status = SANE_STATUS_IO_ERROR; break; } - if (res != DECODE_STATUS_GOOD) + if (ret_status != DECODE_STATUS_GOOD) { current_header.block_type = 0; - ret_status = SANE_STATUS_IO_ERROR; break; } @@ -1491,11 +1547,8 @@ SANE_Status BrotherEncoderFamily4::DecodeScanData (const SANE_Byte *src_data, si } } - if (ret_status == SANE_STATUS_GOOD) - { - *src_data_consumed = orig_src_data_len - src_data_len; - *dest_data_written = orig_dest_data_len - dest_data_len; - } + *src_data_consumed = orig_src_data_len - src_data_len; + *dest_data_written = orig_dest_data_len - dest_data_len; return ret_status; } @@ -1526,7 +1579,7 @@ DecodeStatus BrotherEncoderFamily4::DecodeScanDataHeader (const SANE_Byte *src_d header.block_len = 0; - if (header.block_type == BROTHER_DATA_BLOCK_NO_DATA) + if (header.block_type == BROTHER_DATA_BLOCK_SCAN_FINISHED) { *src_data_consumed = 1; return DECODE_STATUS_ENDOFDATA; @@ -1538,7 +1591,7 @@ DecodeStatus BrotherEncoderFamily4::DecodeScanDataHeader (const SANE_Byte *src_d return DECODE_STATUS_CANCEL; } - if (header.block_type == BROTHER_DATA_BLOCK_END_OF_FRAME) + if (header.block_type == BROTHER_DATA_BLOCK_END_OF_FRAME_NO_MORE) { if (src_data_len < 10) { @@ -1546,7 +1599,7 @@ DecodeStatus BrotherEncoderFamily4::DecodeScanDataHeader (const SANE_Byte *src_d } *src_data_consumed = 10; - return DECODE_STATUS_ENDOFFRAME; + return DECODE_STATUS_ENDOFFRAME_NO_MORE; } /* @@ -1565,29 +1618,29 @@ DecodeStatus BrotherEncoderFamily4::DecodeScanDataHeader (const SANE_Byte *src_d return DECODE_STATUS_GOOD; } -SANE_Status BrotherEncoderFamily4::DecodeButtonQueryResp (const SANE_Byte *data, size_t data_len, - BrotherButtonQueryResponse &response) +DecodeStatus BrotherEncoderFamily4::DecodeButtonQueryResp (const SANE_Byte *data, size_t data_len, + BrotherButtonQueryResponse &response) { if ((data_len != 4) || (data[0] != 0x04) ||(data[1] != 0x10) || (data[2] != 0x03)) { - return SANE_STATUS_IO_ERROR; + return DECODE_STATUS_ERROR; } response.has_button_press = data[3] == 0x10; - return SANE_STATUS_GOOD; + return DECODE_STATUS_GOOD; } -SANE_Status BrotherEncoderFamily4::DecodeButtonStateResp (const SANE_Byte *data, size_t data_len, - BrotherButtonStateResponse &response) +DecodeStatus BrotherEncoderFamily4::DecodeButtonStateResp (const SANE_Byte *data, size_t data_len, + BrotherButtonStateResponse &response) { if ((data_len != 9) || (memcmp(data, "\x09" "\x10" "\x03" "\x20", 4) != 0)) { - return SANE_STATUS_IO_ERROR; + return DECODE_STATUS_ERROR; } response.button_value = data[4]; - return SANE_STATUS_GOOD; + return DECODE_STATUS_GOOD; } void BrotherGrayRLengthDecoder::NewBlock () diff --git a/backend/brother_mfp/brother_mfp-encoder.h b/backend/brother_mfp/brother_mfp-encoder.h index 98785d1f2..bf7f11a35 100644 --- a/backend/brother_mfp/brother_mfp-encoder.h +++ b/backend/brother_mfp/brother_mfp-encoder.h @@ -163,10 +163,13 @@ enum DecodeStatus DECODE_STATUS_GOOD, DECODE_STATUS_TRUNCATED, DECODE_STATUS_ENDOFDATA, - DECODE_STATUS_ENDOFFRAME, + DECODE_STATUS_ENDOFFRAME_NO_MORE, + DECODE_STATUS_ENDOFFRAME_WITH_MORE, DECODE_STATUS_CANCEL, DECODE_STATUS_ERROR, - DECODE_STATUS_MEMORY + DECODE_STATUS_MEMORY, + DECODE_STATUS_INVAL, + DECODE_STATUS_UNSUPPORTED }; struct ScanDataHeader @@ -191,46 +194,67 @@ public: { } - virtual void NewPage() = 0; + virtual void NewPage () = 0; - SANE_Status SetScanMode (BrotherScanMode scan_mode); - SANE_Status SetRes (SANE_Int x, SANE_Int y); - SANE_Status SetContrast (SANE_Int contrast); - SANE_Status SetBrightness (SANE_Int brightness); - SANE_Status SetCompression (SANE_Bool compression); + DecodeStatus SetScanMode (BrotherScanMode scan_mode); + DecodeStatus SetRes (SANE_Int x, SANE_Int y); + DecodeStatus SetContrast (SANE_Int contrast); + DecodeStatus SetBrightness (SANE_Int brightness); + DecodeStatus SetCompression (SANE_Bool compression); + + DecodeStatus SetScanDimensions (SANE_Int pixel_x_offset, SANE_Int pixel_x_width, + SANE_Int pixel_y_offset, SANE_Int pixel_y_height); + + SANE_Status DecodeStatusToSaneStatus(DecodeStatus dec_ret) + { + static SANE_Status status_lookup[] = + { + SANE_STATUS_GOOD, + SANE_STATUS_GOOD, + SANE_STATUS_EOF, + SANE_STATUS_EOF, + SANE_STATUS_EOF, + SANE_STATUS_CANCELLED, + SANE_STATUS_IO_ERROR, + SANE_STATUS_NO_MEM, + SANE_STATUS_INVAL, + SANE_STATUS_UNSUPPORTED + }; + + return status_lookup[dec_ret]; + } - SANE_Status SetScanDimensions (SANE_Int pixel_x_offset, SANE_Int pixel_x_width, SANE_Int pixel_y_offset, - SANE_Int pixel_y_height); static const char* ScanModeToText (BrotherScanMode scan_mode); - virtual SANE_Status DecodeSessionResp (const SANE_Byte *data, size_t data_len, - BrotherSessionResponse &response) = 0; + virtual DecodeStatus DecodeSessionResp (const SANE_Byte *data, size_t data_len, + BrotherSessionResponse &response) = 0; - virtual SANE_Status DecodeButtonQueryResp (const SANE_Byte *data, size_t data_len, - BrotherButtonQueryResponse &response) = 0; + virtual DecodeStatus DecodeButtonQueryResp (const SANE_Byte *data, size_t data_len, + BrotherButtonQueryResponse &response) = 0; - virtual SANE_Status DecodeButtonStateResp (const SANE_Byte *data, size_t data_len, - BrotherButtonStateResponse &response) = 0; + virtual DecodeStatus DecodeButtonStateResp (const SANE_Byte *data, size_t data_len, + BrotherButtonStateResponse &response) = 0; - virtual SANE_Status EncodeBasicParameterBlock (SANE_Byte *data, size_t data_len, - size_t *length) = 0; + virtual DecodeStatus EncodeBasicParameterBlock (SANE_Byte *data, size_t data_len, + size_t *length) = 0; - virtual SANE_Status DecodeBasicParameterBlockResp (const SANE_Byte *data, size_t data_len, - BrotherBasicParamResponse &response) = 0; + virtual DecodeStatus DecodeBasicParameterBlockResp (const SANE_Byte *data, size_t data_len, + BrotherBasicParamResponse &response) = 0; - virtual SANE_Status EncodeADFBlock (SANE_Byte *data, size_t data_len, size_t *length) = 0; + virtual DecodeStatus EncodeADFBlock (SANE_Byte *data, size_t data_len, size_t *length) = 0; - virtual SANE_Status DecodeADFBlockResp (const SANE_Byte *data, size_t data_len, - BrotherADFResponse &response) = 0; + virtual DecodeStatus DecodeADFBlockResp (const SANE_Byte *data, size_t data_len, + BrotherADFResponse &response) = 0; - virtual SANE_Status EncodeParameterBlock (SANE_Byte *data, size_t data_len, size_t *length) = 0; + virtual DecodeStatus EncodeParameterBlock (SANE_Byte *data, size_t data_len, size_t *length) = 0; - virtual SANE_Status DecodeScanData (const SANE_Byte *src_data, size_t src_data_len, - size_t *src_data_consumed, SANE_Byte *dst_data, - size_t dest_data_len, size_t *dest_data_written) = 0; + virtual DecodeStatus EncodeParameterBlockBlank (SANE_Byte *data, size_t data_len, + size_t *length) = 0; -// virtual SANE_Status CheckSensor(BrotherSensor &status) = 0; + virtual DecodeStatus DecodeScanData (const SANE_Byte *src_data, size_t src_data_len, + size_t *src_data_consumed, SANE_Byte *dst_data, + size_t dest_data_len, size_t *dest_data_written) = 0; protected: BrotherParameters scan_params; @@ -407,53 +431,57 @@ public: { } - void NewPage() override + void NewPage () override { current_header.block_type = 0; - jfif_decoder.NewPage(scan_params); - gray_decoder.NewPage(scan_params); - gray_raw_decoder.NewPage(scan_params); - colour_decoder.NewPage(scan_params); + jfif_decoder.NewPage (scan_params); + gray_decoder.NewPage (scan_params); + gray_raw_decoder.NewPage (scan_params); + colour_decoder.NewPage (scan_params); } - SANE_Status DecodeSessionResp (const SANE_Byte *data, size_t data_len, - BrotherSessionResponse &response) override; + DecodeStatus DecodeSessionResp (const SANE_Byte *data, size_t data_len, + BrotherSessionResponse &response) override; - SANE_Status EncodeBasicParameterBlock (SANE_Byte *data, size_t data_len, size_t *length) override; + DecodeStatus EncodeBasicParameterBlock (SANE_Byte *data, size_t data_len, size_t *length) + override; - SANE_Status DecodeBasicParameterBlockResp (const SANE_Byte *data, size_t data_len, - BrotherBasicParamResponse &response) override; + DecodeStatus DecodeBasicParameterBlockResp (const SANE_Byte *data, size_t data_len, + BrotherBasicParamResponse &response) override; - SANE_Status EncodeADFBlock (SANE_Byte *data, size_t data_len, size_t *length) override + DecodeStatus EncodeADFBlock (SANE_Byte *data, size_t data_len, size_t *length) override { - (void)data; - (void)data_len; - (void)length; - return SANE_STATUS_UNSUPPORTED; + (void) data; + (void) data_len; + (void) length; + return DECODE_STATUS_UNSUPPORTED; } - SANE_Status DecodeADFBlockResp (const SANE_Byte *data, size_t data_len, - BrotherADFResponse &response) override + DecodeStatus DecodeADFBlockResp (const SANE_Byte *data, size_t data_len, + BrotherADFResponse &response) override { - (void)data; - (void)data_len; - (void)response; + (void) data; + (void) data_len; + (void) response; - return SANE_STATUS_UNSUPPORTED; + return DECODE_STATUS_UNSUPPORTED; } - SANE_Status EncodeParameterBlock (SANE_Byte *data, size_t data_len, size_t *length) override; + DecodeStatus EncodeParameterBlock (SANE_Byte *data, size_t data_len, size_t *length) override; - SANE_Status DecodeScanData (const SANE_Byte *src_data, size_t src_data_len, - size_t *src_data_consumed, SANE_Byte *dst_data, - size_t dest_data_len, size_t *dest_data_written) override; + DecodeStatus EncodeParameterBlockBlank (SANE_Byte *data, size_t data_len, size_t *length) + override; - SANE_Status DecodeButtonQueryResp (const SANE_Byte *data, size_t data_len, - BrotherButtonQueryResponse &response) override; + DecodeStatus DecodeScanData (const SANE_Byte *src_data, size_t src_data_len, + size_t *src_data_consumed, SANE_Byte *dst_data, size_t dest_data_len, + size_t *dest_data_written) override; - SANE_Status DecodeButtonStateResp (const SANE_Byte *data, size_t data_len, - BrotherButtonStateResponse &response) override; + DecodeStatus DecodeButtonQueryResp (const SANE_Byte *data, size_t data_len, + BrotherButtonQueryResponse &response) override; + + DecodeStatus DecodeButtonStateResp (const SANE_Byte *data, size_t data_len, + BrotherButtonStateResponse &response) override; private: DecodeStatus DecodeScanDataHeader (const SANE_Byte *src_data, size_t src_data_len, @@ -489,43 +517,47 @@ public: jfif_decoder.NewPage(scan_params); } - SANE_Status DecodeSessionResp (const SANE_Byte *data, size_t data_len, - BrotherSessionResponse &response) override; + DecodeStatus DecodeSessionResp (const SANE_Byte *data, size_t data_len, + BrotherSessionResponse &response) override; - SANE_Status EncodeBasicParameterBlock (SANE_Byte *data, size_t data_len, size_t *length) override; + DecodeStatus EncodeBasicParameterBlock (SANE_Byte *data, size_t data_len, size_t *length) + override; - SANE_Status DecodeBasicParameterBlockResp (const SANE_Byte *data, size_t data_len, - BrotherBasicParamResponse &response) override; + DecodeStatus DecodeBasicParameterBlockResp (const SANE_Byte *data, size_t data_len, + BrotherBasicParamResponse &response) override; - SANE_Status EncodeADFBlock (SANE_Byte *data, size_t data_len, size_t *length) override + DecodeStatus EncodeADFBlock (SANE_Byte *data, size_t data_len, size_t *length) override { - (void)data; - (void)data_len; - (void)length; - return SANE_STATUS_UNSUPPORTED; + (void) data; + (void) data_len; + (void) length; + return DECODE_STATUS_UNSUPPORTED; } - SANE_Status DecodeADFBlockResp (const SANE_Byte *data, size_t data_len, - BrotherADFResponse &response) override + DecodeStatus DecodeADFBlockResp (const SANE_Byte *data, size_t data_len, + BrotherADFResponse &response) override { - (void)data; - (void)data_len; - (void)response; + (void) data; + (void) data_len; + (void) response; - return SANE_STATUS_UNSUPPORTED; + return DECODE_STATUS_UNSUPPORTED; } - SANE_Status EncodeParameterBlock (SANE_Byte *data, size_t data_len, size_t *length) override; + DecodeStatus EncodeParameterBlock (SANE_Byte *data, size_t data_len, size_t *length) override; - SANE_Status DecodeScanData (const SANE_Byte *src_data, size_t src_data_len, - size_t *src_data_consumed, SANE_Byte *dst_data, - size_t dest_data_len, size_t *dest_data_written) override; + DecodeStatus EncodeParameterBlockBlank (SANE_Byte *data, size_t data_len, size_t *length) + override; - SANE_Status DecodeButtonQueryResp (const SANE_Byte *data, size_t data_len, - BrotherButtonQueryResponse &response) override; + DecodeStatus DecodeScanData (const SANE_Byte *src_data, size_t src_data_len, + size_t *src_data_consumed, SANE_Byte *dst_data, size_t dest_data_len, + size_t *dest_data_written) override; - SANE_Status DecodeButtonStateResp (const SANE_Byte *data, size_t data_len, - BrotherButtonStateResponse &response) override; + DecodeStatus DecodeButtonQueryResp (const SANE_Byte *data, size_t data_len, + BrotherButtonQueryResponse &response) override; + + DecodeStatus DecodeButtonStateResp (const SANE_Byte *data, size_t data_len, + BrotherButtonStateResponse &response) override; private: DecodeStatus DecodeScanDataHeader (const SANE_Byte *src_data, size_t src_data_len, @@ -556,38 +588,38 @@ public: current_header.block_type = 0; jfif_decoder.NewPage(scan_params); - gray_decoder.NewPage(scan_params); - gray_raw_decoder.NewPage(scan_params); + gray_decoder.NewPage (scan_params); + gray_raw_decoder.NewPage (scan_params); } - SANE_Status DecodeSessionResp (const SANE_Byte *data, size_t data_len, - BrotherSessionResponse &response) override; + DecodeStatus DecodeSessionResp (const SANE_Byte *data, size_t data_len, + BrotherSessionResponse &response) override; - SANE_Status EncodeBasicParameterBlock (SANE_Byte *data, size_t data_len, size_t *length) override; + DecodeStatus EncodeBasicParameterBlock (SANE_Byte *data, size_t data_len, size_t *length) + override; - SANE_Status DecodeBasicParameterBlockResp (const SANE_Byte *data, size_t data_len, - BrotherBasicParamResponse &response) override; + DecodeStatus DecodeBasicParameterBlockResp (const SANE_Byte *data, size_t data_len, + BrotherBasicParamResponse &response) override; - SANE_Status EncodeADFBlock (SANE_Byte *data, size_t data_len, size_t *length) override; + DecodeStatus EncodeADFBlock (SANE_Byte *data, size_t data_len, size_t *length) override; - SANE_Status DecodeADFBlockResp (const SANE_Byte *data, size_t data_len, - BrotherADFResponse &response) override; + DecodeStatus DecodeADFBlockResp (const SANE_Byte *data, size_t data_len, + BrotherADFResponse &response) override; - SANE_Status EncodeParameterBlock (SANE_Byte *data, size_t data_len, size_t *length) override; + DecodeStatus EncodeParameterBlock (SANE_Byte *data, size_t data_len, size_t *length) override; - SANE_Status DecodeScanData (const SANE_Byte *src_data, size_t src_data_len, - size_t *src_data_consumed, SANE_Byte *dst_data, - size_t dest_data_len, size_t *dest_data_written) override; + DecodeStatus EncodeParameterBlockBlank (SANE_Byte *data, size_t data_len, size_t *length) + override; - SANE_Status DecodeButtonQueryResp (const SANE_Byte *data, size_t data_len, - BrotherButtonQueryResponse &response) override; + DecodeStatus DecodeScanData (const SANE_Byte *src_data, size_t src_data_len, + size_t *src_data_consumed, SANE_Byte *dst_data, size_t dest_data_len, + size_t *dest_data_written) override; - SANE_Status DecodeButtonStateResp (const SANE_Byte *data, size_t data_len, - BrotherButtonStateResponse &response) override; + DecodeStatus DecodeButtonQueryResp (const SANE_Byte *data, size_t data_len, + BrotherButtonQueryResponse &response) override; - - -// SANE_Status CheckSensor(BrotherSensor &status) override; + DecodeStatus DecodeButtonStateResp (const SANE_Byte *data, size_t data_len, + BrotherButtonStateResponse &response) override; private: DecodeStatus DecodeScanDataHeader (const SANE_Byte *src_data, size_t src_data_len, diff --git a/backend/brother_mfp/brother_mfp.cpp b/backend/brother_mfp/brother_mfp.cpp index 18b738065..1e511909e 100644 --- a/backend/brother_mfp/brother_mfp.cpp +++ b/backend/brother_mfp/brother_mfp.cpp @@ -1295,7 +1295,7 @@ sane_get_parameters (SANE_Handle handle, SANE_Parameters * params) * TODO: refine this. It's still not really right. * */ - pixel_x_width &= ~0x7; +// pixel_x_width += 16; SANE_Int pixel_y_height = SANE_UNFIX (device->val[OPT_BR_Y].w - device->val[OPT_TL_Y].w) / MM_IN_INCH *