merge-requests/541/merge
Ordissimo 2021-01-24 07:17:36 +00:00
rodzic fc74de7c5e
commit 6f90442641
1 zmienionych plików z 82 dodań i 70 usunięć

Wyświetl plik

@ -29,6 +29,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#ifdef PATH_MAX
# undef PATH_MAX
@ -36,12 +37,6 @@
#define PATH_MAX 4096
struct uploading
{
const char *read_data;
size_t size;
};
struct downloading
{
char *memory;
@ -136,7 +131,7 @@ escl_newjob (capabilities_t *scanner, const ESCL_Device *device, SANE_Status *st
{
CURL *curl_handle = NULL;
int off_x = 0, off_y = 0;
struct uploading *upload = NULL;
struct downloading *upload = NULL;
struct downloading *download = NULL;
const char *scan_jobs = "/eSCL/ScanJobs";
char cap_data[PATH_MAX] = { 0 };
@ -146,6 +141,7 @@ escl_newjob (capabilities_t *scanner, const ESCL_Device *device, SANE_Status *st
char *f_ext = "";
char *format_ext = NULL;
char duplex_mode[1024] = { 0 };
int wakup_count = 0;
*status = SANE_STATUS_GOOD;
if (device == NULL || scanner == NULL) {
@ -153,7 +149,7 @@ escl_newjob (capabilities_t *scanner, const ESCL_Device *device, SANE_Status *st
DBG( 1, "Create NewJob : the name or the scan are invalid.\n");
return (NULL);
}
upload = (struct uploading *)calloc(1, sizeof(struct uploading));
upload = (struct downloading *)calloc(1, sizeof(struct downloading));
if (upload == NULL) {
*status = SANE_STATUS_NO_MEM;
DBG( 1, "Create NewJob : memory allocation failure\n");
@ -166,7 +162,6 @@ escl_newjob (capabilities_t *scanner, const ESCL_Device *device, SANE_Status *st
*status = SANE_STATUS_NO_MEM;
return (NULL);
}
curl_handle = curl_easy_init();
if (scanner->caps[scanner->source].default_format)
free(scanner->caps[scanner->source].default_format);
scanner->caps[scanner->source].default_format = NULL;
@ -214,68 +209,71 @@ escl_newjob (capabilities_t *scanner, const ESCL_Device *device, SANE_Status *st
off_x = (scanner->caps[scanner->source].pos_x > scanner->caps[scanner->source].width) / 2;
if (scanner->caps[scanner->source].pos_y > scanner->caps[scanner->source].height)
off_y = (scanner->caps[scanner->source].pos_y > scanner->caps[scanner->source].height) / 2;
char support_options[1024];
memset(support_options, 0, 1024);
char *source = (scanner->source == PLATEN ? "Platen" : "Feeder");
if (scanner->use_threshold)
{
char *tmp = add_support_option("ThresholdSupport", scanner->val_threshold);
if (support_options[0])
strcat(support_options, tmp);
else
strcpy(support_options, tmp);
free(tmp);
}
if (scanner->use_sharpen)
{
char *tmp = add_support_option("SharpenSupport", scanner->val_sharpen);
if (support_options[0])
strcat(support_options, tmp);
else
strcpy(support_options, tmp);
free(tmp);
}
if (scanner->use_contrast)
{
char *tmp = add_support_option("ContrastSupport", scanner->val_contrast);
if (support_options[0])
strcat(support_options, tmp);
else
strcpy(support_options, tmp);
free(tmp);
}
if (scanner->use_brightness)
{
char *tmp = add_support_option("BrightnessSupport", scanner->val_brightness);
if (support_options[0])
strcat(support_options, tmp);
else
strcpy(support_options, tmp);
free(tmp);
}
snprintf(cap_data, sizeof(cap_data), settings,
scanner->caps[scanner->source].height,
scanner->caps[scanner->source].width,
off_x,
off_y,
scanner->caps[scanner->source].default_format,
format_ext,
scanner->caps[scanner->source].default_color,
scanner->caps[scanner->source].default_resolution,
scanner->caps[scanner->source].default_resolution,
source,
source,
duplex_mode[0] == 0 ? " " : duplex_mode,
support_options[0] == 0 ? " " : support_options);
upload->memory = strdup(cap_data);
upload->size = strlen(cap_data);
wake_up_device:
DBG( 1, "Create NewJob : %s\n", cap_data);
download->memory = malloc(1);
download->size = 0;
curl_handle = curl_easy_init();
if (curl_handle != NULL) {
char support_options[1024];
memset(support_options, 0, 1024);
char *source = (scanner->source == PLATEN ? "Platen" : "Feeder");
if (scanner->use_threshold)
{
char *tmp = add_support_option("ThresholdSupport", scanner->val_threshold);
if (support_options[0])
strcat(support_options, tmp);
else
strcpy(support_options, tmp);
free(tmp);
}
if (scanner->use_sharpen)
{
char *tmp = add_support_option("SharpenSupport", scanner->val_sharpen);
if (support_options[0])
strcat(support_options, tmp);
else
strcpy(support_options, tmp);
free(tmp);
}
if (scanner->use_contrast)
{
char *tmp = add_support_option("ContrastSupport", scanner->val_contrast);
if (support_options[0])
strcat(support_options, tmp);
else
strcpy(support_options, tmp);
free(tmp);
}
if (scanner->use_brightness)
{
char *tmp = add_support_option("BrightnessSupport", scanner->val_brightness);
if (support_options[0])
strcat(support_options, tmp);
else
strcpy(support_options, tmp);
free(tmp);
}
snprintf(cap_data, sizeof(cap_data), settings,
scanner->caps[scanner->source].height,
scanner->caps[scanner->source].width,
off_x,
off_y,
scanner->caps[scanner->source].default_format,
format_ext,
scanner->caps[scanner->source].default_color,
scanner->caps[scanner->source].default_resolution,
scanner->caps[scanner->source].default_resolution,
source,
source,
duplex_mode[0] == 0 ? " " : duplex_mode,
support_options[0] == 0 ? " " : support_options);
DBG( 1, "Create NewJob : %s\n", cap_data);
upload->read_data = strdup(cap_data);
upload->size = strlen(cap_data);
download->memory = malloc(1);
download->size = 0;
escl_curl_url(curl_handle, device, scan_jobs);
curl_easy_setopt(curl_handle, CURLOPT_POST, 1L);
curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDS, upload->read_data);
curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDS, (const char*)upload->memory);
curl_easy_setopt(curl_handle, CURLOPT_POSTFIELDSIZE, upload->size);
curl_easy_setopt(curl_handle, CURLOPT_HEADERFUNCTION, download_callback);
curl_easy_setopt(curl_handle, CURLOPT_HEADERDATA, (void *)download);
@ -298,6 +296,7 @@ escl_newjob (capabilities_t *scanner, const ESCL_Device *device, SANE_Status *st
result = strdup(location);
DBG( 1, "Create NewJob : %s\n", result);
*temporary = '\n';
wakup_count = 0;
}
}
if (result == NULL) {
@ -305,6 +304,7 @@ escl_newjob (capabilities_t *scanner, const ESCL_Device *device, SANE_Status *st
*status = SANE_STATUS_INVAL;
}
free(download->memory);
download->memory = NULL;
}
else {
DBG( 1, "Create NewJob : The creation of the failed job: %s\n", download->memory);
@ -312,8 +312,10 @@ escl_newjob (capabilities_t *scanner, const ESCL_Device *device, SANE_Status *st
if (strstr(download->memory, "409 Conflict") != NULL)
*status = SANE_STATUS_NO_DOCS;
// If "503 Service Unavailable" appear, it means that device is busy (scanning in progress)
else if (strstr(download->memory, "503 Service Unavailable") != NULL)
else if (strstr(download->memory, "503 Service Unavailable") != NULL) {
wakup_count += 1;
*status = SANE_STATUS_DEVICE_BUSY;
}
else
*status = SANE_STATUS_INVAL;
}
@ -326,8 +328,18 @@ escl_newjob (capabilities_t *scanner, const ESCL_Device *device, SANE_Status *st
}
curl_easy_cleanup(curl_handle);
}
if (upload != NULL)
if (wakup_count > 0 && wakup_count < 4) {
free(download->memory);
download->memory = NULL;
download->size = 0;
*status = SANE_STATUS_GOOD;
usleep(250);
goto wake_up_device;
}
if (upload != NULL) {
free(upload->memory);
free(upload);
}
if (download != NULL)
free(download);
return (result);