summaryrefslogtreecommitdiff
path: root/backend/escl/escl_newjob.c
diff options
context:
space:
mode:
authorJörg Frings-Fürst <debian@jff-webhosting.net>2021-02-16 18:25:42 +0100
committerJörg Frings-Fürst <debian@jff-webhosting.net>2021-02-16 18:25:42 +0100
commit64ddd1a4dfcefd11f662f5cf4bb05b590d901a97 (patch)
tree4303648095deff998ae20c2393b56db527742ac9 /backend/escl/escl_newjob.c
parent5793a221e112fa49073de0137d6bfa750a61b0a1 (diff)
parentecaaf250cb33ff61f37a7b441f2337584a655350 (diff)
Merge branch 'feature/upstream' into develop
Diffstat (limited to 'backend/escl/escl_newjob.c')
-rw-r--r--backend/escl/escl_newjob.c156
1 files changed, 121 insertions, 35 deletions
diff --git a/backend/escl/escl_newjob.c b/backend/escl/escl_newjob.c
index ee8c03c..24bfbc9 100644
--- a/backend/escl/escl_newjob.c
+++ b/backend/escl/escl_newjob.c
@@ -16,8 +16,8 @@
for more details.
You should have received a copy of the GNU General Public License
- along with sane; see the file COPYING. If not, write to the Free
- Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ along with sane; see the file COPYING.
+ If not, see <https://www.gnu.org/licenses/>.
This file implements a SANE backend for eSCL scanners. */
@@ -29,8 +29,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-
-#include <curl/curl.h>
+#include <unistd.h>
#ifdef PATH_MAX
# undef PATH_MAX
@@ -38,12 +37,6 @@
#define PATH_MAX 4096
-struct uploading
-{
- const char *read_data;
- size_t size;
-};
-
struct downloading
{
char *memory;
@@ -71,6 +64,7 @@ static const char settings[] =
" <pwg:InputSource>%s</pwg:InputSource>" \
" <scan:InputSource>%s</scan:InputSource>" \
"%s" \
+ "%s" \
"</scan:ScanSettings>";
/**
@@ -114,6 +108,15 @@ download_callback(void *str, size_t size, size_t nmemb, void *userp)
return (realsize);
}
+static char*
+add_support_option(char *key, int val)
+{
+ int size = (strlen(key) * 3) + 10;
+ char *tmp = (char*)calloc(1, size);
+ snprintf (tmp, size, "<scan:%s>%d</scan:%s>\n", key, val, key);
+ return tmp;
+}
+
/**
* \fn char *escl_newjob (capabilities_t *scanner, const ESCL_Device *device, SANE_Status *status)
* \brief Function that, using curl, uploads the data (composed by the scanner capabilities) to the
@@ -128,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 };
@@ -138,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) {
@@ -145,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");
@@ -158,7 +162,33 @@ 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;
+ int have_png = scanner->caps[scanner->source].have_png;
+ int have_jpeg = scanner->caps[scanner->source].have_jpeg;
+ int have_tiff = scanner->caps[scanner->source].have_tiff;
+ int have_pdf = scanner->caps[scanner->source].have_pdf;
+
+ if ((scanner->source == PLATEN && have_pdf == -1) ||
+ (scanner->source > PLATEN)) {
+ if (have_tiff != -1) {
+ scanner->caps[scanner->source].default_format =
+ strdup(scanner->caps[scanner->source].DocumentFormats[have_tiff]);
+ }
+ else if (have_png != -1) {
+ scanner->caps[scanner->source].default_format =
+ strdup(scanner->caps[scanner->source].DocumentFormats[have_png]);
+ }
+ else if (have_jpeg != -1) {
+ scanner->caps[scanner->source].default_format =
+ strdup(scanner->caps[scanner->source].DocumentFormats[have_jpeg]);
+ }
+ }
+ else {
+ scanner->caps[scanner->source].default_format =
+ strdup(scanner->caps[scanner->source].DocumentFormats[have_pdf]);
+ }
if (scanner->caps[scanner->source].format_ext == 1)
{
char f_ext_tmp[1024];
@@ -179,29 +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 *source = (scanner->source == PLATEN ? "Platen" : "Feeder");
- 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);
- 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);
@@ -224,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) {
@@ -231,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);
@@ -238,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;
}
@@ -252,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);