summaryrefslogtreecommitdiff
path: root/backend/escl/escl_newjob.c
blob: ee8c03c6c8cbe0594e530bed589ffac49cc52684 (plain)
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
/* sane - Scanner Access Now Easy.

   Copyright (C) 2019 Touboul Nathane
   Copyright (C) 2019 Thierry HUCHARD <thierry@ordissimo.com>

   This file is part of the SANE package.

   SANE is free software; you can redistribute it and/or modify it under
   the terms of the GNU General Public License as published by the Free
   Software Foundation; either version 3 of the License, or (at your
   option) any later version.

   SANE is distributed in the hope that it will be useful, but WITHOUT
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
   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.

   This file implements a SANE backend for eSCL scanners.  */

#define DEBUG_DECLARE_ONLY
#include "../include/sane/config.h"

#include "escl.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <curl/curl.h>

#ifdef PATH_MAX
# undef PATH_MAX
#endif

#define PATH_MAX 4096

struct uploading
{
    const char *read_data;
    size_t size;
};

struct downloading
{
    char *memory;
    size_t size;
};

static const char settings[] =
    "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"                        \
    "<scan:ScanSettings xmlns:pwg=\"http://www.pwg.org/schemas/2010/12/sm\" xmlns:scan=\"http://schemas.hp.com/imaging/escl/2011/05/03\">" \
    "   <pwg:Version>2.0</pwg:Version>" \
    "   <pwg:ScanRegions>" \
    "      <pwg:ScanRegion>" \
    "          <pwg:ContentRegionUnits>escl:ThreeHundredthsOfInches</pwg:ContentRegionUnits>" \
    "          <pwg:Height>%d</pwg:Height>" \
    "          <pwg:Width>%d</pwg:Width>" \
    "          <pwg:XOffset>%d</pwg:XOffset>" \
    "          <pwg:YOffset>%d</pwg:YOffset>" \
    "      </pwg:ScanRegion>" \
    "   </pwg:ScanRegions>" \
    "   <pwg:DocumentFormat>%s</pwg:DocumentFormat>" \
    "%s" \
    "   <scan:ColorMode>%s</scan:ColorMode>" \
    "   <scan:XResolution>%d</scan:XResolution>" \
    "   <scan:YResolution>%d</scan:YResolution>" \
    "   <pwg:InputSource>%s</pwg:InputSource>" \
    "   <scan:InputSource>%s</scan:InputSource>" \
    "%s" \
    "</scan:ScanSettings>";

/**
 * \fn static size_t download_callback(void *str, size_t size, size_t nmemb, void *userp)
 * \brief Callback function that stocks in memory the content of the 'job'. Example below :
 *        "Trying 192.168.14.150...
 *         TCP_NODELAY set
 *         Connected to 192.168.14.150 (192.168.14.150) port 80
 *         POST /eSCL/ScanJobs HTTP/1.1
 *         Host: 192.168.14.150
 *         User-Agent: curl/7.55.1
 *         Accept: /
 *         Content-Length: 605
 *         Content-Type: application/x-www-form-urlencoded
 *         upload completely sent off: 605 out of 605 bytes
 *         < HTTP/1.1 201 Created
 *         < MIME-Version: 1.0
 *         < Location: http://192.168.14.150/eSCL/ScanJobs/22b54fd0-027b-1000-9bd0-f4a99726e2fa
 *         < Content-Length: 0
 *         < Connection: close
 *         <
 *         Closing connection 0"
 *
 * \return realsize (size of the content needed -> the 'job')
 */
static size_t
download_callback(void *str, size_t size, size_t nmemb, void *userp)
{
    struct downloading *download = (struct downloading *)userp;
    size_t realsize = size * nmemb;
    char *content = realloc(download->memory, download->size + realsize + 1);

    if (content == NULL) {
        DBG( 1, "Not enough memory (realloc returned NULL)\n");
        return (0);
    }
    download->memory = content;
    memcpy(&(download->memory[download->size]), str, realsize);
    download->size = download->size + realsize;
    download->memory[download->size] = 0;
    return (realsize);
}

/**
 * \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
 *        server to download the 'job' and recover the 'new job' (char *result), in LOCATION.
 *        This function is called in the 'sane_start' function and it's the equivalent of the
 *        following curl command : "curl -v POST -d cap.xml http(s)://'ip':'port'/eSCL/ScanJobs".
 *
 * \return result (the 'new job', situated in LOCATION)
 */
char *
escl_newjob (capabilities_t *scanner, const ESCL_Device *device, SANE_Status *status)
{
    CURL *curl_handle = NULL;
    int off_x = 0, off_y = 0;
    struct uploading *upload = NULL;
    struct downloading *download = NULL;
    const char *scan_jobs = "/eSCL/ScanJobs";
    char cap_data[PATH_MAX] = { 0 };
    char *location = NULL;
    char *result = NULL;
    char *temporary = NULL;
    char *f_ext = "";
    char *format_ext = NULL;
    char duplex_mode[1024] = { 0 };

    *status = SANE_STATUS_GOOD;
    if (device == NULL || scanner == NULL) {
        *status = SANE_STATUS_NO_MEM;
        DBG( 1, "Create NewJob : the name or the scan are invalid.\n");
        return (NULL);
    }
    upload = (struct uploading *)calloc(1, sizeof(struct uploading));
    if (upload == NULL) {
        *status = SANE_STATUS_NO_MEM;
        DBG( 1, "Create NewJob : memory allocation failure\n");
        return (NULL);
    }
    download = (struct downloading *)calloc(1, sizeof(struct downloading));
    if (download == NULL) {
        free(upload);
        DBG( 1, "Create NewJob : memory allocation failure\n");
        *status = SANE_STATUS_NO_MEM;
        return (NULL);
    }
    curl_handle = curl_easy_init();
    if (scanner->caps[scanner->source].format_ext == 1)
    {
        char f_ext_tmp[1024];
        snprintf(f_ext_tmp, sizeof(f_ext_tmp),
			"   <scan:DocumentFormatExt>%s</scan:DocumentFormatExt>",
    			scanner->caps[scanner->source].default_format);
        format_ext = f_ext_tmp;
    }
    else
      format_ext = f_ext;
    if(scanner->source > PLATEN && scanner->Sources[ADFDUPLEX]) {
       snprintf(duplex_mode, sizeof(duplex_mode),
		       "   <scan:Duplex>%s</scan:Duplex>",
		       scanner->source == ADFDUPLEX ? "true" : "false");
    }
    DBG( 1, "Create NewJob : %s\n", scanner->caps[scanner->source].default_format);
    if (scanner->caps[scanner->source].pos_x > scanner->caps[scanner->source].width)
         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;
    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_POSTFIELDSIZE, upload->size);
        curl_easy_setopt(curl_handle, CURLOPT_HEADERFUNCTION, download_callback);
        curl_easy_setopt(curl_handle, CURLOPT_HEADERDATA, (void *)download);
        CURLcode res = curl_easy_perform(curl_handle);
        if (res != CURLE_OK) {
            DBG( 1, "Create NewJob : the scanner responded incorrectly: %s\n", curl_easy_strerror(res));
            *status = SANE_STATUS_INVAL;
        }
        else {
            if (download->memory != NULL) {
                char *tmp_location = strstr(download->memory, "Location:");
                if (tmp_location) {
                    temporary = strchr(tmp_location, '\r');
                    if (temporary == NULL)
                        temporary = strchr(tmp_location, '\n');
                    if (temporary != NULL) {
                       *temporary = '\0';
                       location = strrchr(tmp_location,'/');
                       if (location) {
                          result = strdup(location);
                          DBG( 1, "Create NewJob : %s\n", result);
                          *temporary = '\n';
                       }
                    }
                    if (result == NULL) {
                        DBG( 1, "Error : Create NewJob, no location: %s\n", download->memory);
                        *status = SANE_STATUS_INVAL;
                    }
                    free(download->memory);
                }
                else {
                    DBG( 1, "Create NewJob : The creation of the failed job: %s\n", download->memory);
                    // If "409 Conflict" appear it means that there is no paper in feeder
                    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)
                        *status = SANE_STATUS_DEVICE_BUSY;
                    else
                        *status = SANE_STATUS_INVAL;
                }
            }
            else {
                *status = SANE_STATUS_NO_MEM;
                DBG( 1, "Create NewJob : The creation of the failed job\n");
                return (NULL);
            }
        }
        curl_easy_cleanup(curl_handle);
    }
    if (upload != NULL)
        free(upload);
    if (download != NULL)
        free(download);
    return (result);
}