1141cc406Sopenharmony_ci/* sane - Scanner Access Now Easy.
2141cc406Sopenharmony_ci
3141cc406Sopenharmony_ci   Copyright (C) 2019 Touboul Nathane
4141cc406Sopenharmony_ci   Copyright (C) 2019 Thierry HUCHARD <thierry@ordissimo.com>
5141cc406Sopenharmony_ci
6141cc406Sopenharmony_ci   This file is part of the SANE package.
7141cc406Sopenharmony_ci
8141cc406Sopenharmony_ci   SANE is free software; you can redistribute it and/or modify it under
9141cc406Sopenharmony_ci   the terms of the GNU General Public License as published by the Free
10141cc406Sopenharmony_ci   Software Foundation; either version 3 of the License, or (at your
11141cc406Sopenharmony_ci   option) any later version.
12141cc406Sopenharmony_ci
13141cc406Sopenharmony_ci   SANE is distributed in the hope that it will be useful, but WITHOUT
14141cc406Sopenharmony_ci   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15141cc406Sopenharmony_ci   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16141cc406Sopenharmony_ci   for more details.
17141cc406Sopenharmony_ci
18141cc406Sopenharmony_ci   You should have received a copy of the GNU General Public License
19141cc406Sopenharmony_ci   along with sane; see the file COPYING.
20141cc406Sopenharmony_ci   If not, see <https://www.gnu.org/licenses/>.
21141cc406Sopenharmony_ci
22141cc406Sopenharmony_ci   This file implements a SANE backend for eSCL scanners.  */
23141cc406Sopenharmony_ci
24141cc406Sopenharmony_ci#define DEBUG_DECLARE_ONLY
25141cc406Sopenharmony_ci#include "../include/sane/config.h"
26141cc406Sopenharmony_ci
27141cc406Sopenharmony_ci#include "escl.h"
28141cc406Sopenharmony_ci
29141cc406Sopenharmony_ci#include <stdlib.h>
30141cc406Sopenharmony_ci#include <string.h>
31141cc406Sopenharmony_ci
32141cc406Sopenharmony_cistatic size_t
33141cc406Sopenharmony_ciwrite_callback(void __sane_unused__*str,
34141cc406Sopenharmony_ci               size_t __sane_unused__ size,
35141cc406Sopenharmony_ci               size_t nmemb,
36141cc406Sopenharmony_ci               void __sane_unused__ *userp)
37141cc406Sopenharmony_ci{
38141cc406Sopenharmony_ci    return nmemb;
39141cc406Sopenharmony_ci}
40141cc406Sopenharmony_ci
41141cc406Sopenharmony_ci/**
42141cc406Sopenharmony_ci * \fn void escl_scanner(const ESCL_Device *device, char *result)
43141cc406Sopenharmony_ci * \brief Function that resets the scanner after each scan, using curl.
44141cc406Sopenharmony_ci *        This function is called in the 'sane_cancel' function.
45141cc406Sopenharmony_ci */
46141cc406Sopenharmony_civoid
47141cc406Sopenharmony_ciescl_scanner(const ESCL_Device *device, char *scanJob, char *result)
48141cc406Sopenharmony_ci{
49141cc406Sopenharmony_ci    CURL *curl_handle = NULL;
50141cc406Sopenharmony_ci    const char *scan_jobs = "/eSCL/";
51141cc406Sopenharmony_ci    const char *scanner_start = "/NextDocument";
52141cc406Sopenharmony_ci    char scan_cmd[PATH_MAX] = { 0 };
53141cc406Sopenharmony_ci    int i = 0;
54141cc406Sopenharmony_ci    long answer = 0;
55141cc406Sopenharmony_ci
56141cc406Sopenharmony_ci    if (device == NULL || result == NULL)
57141cc406Sopenharmony_ci        return;
58141cc406Sopenharmony_ciCURL_CALL:
59141cc406Sopenharmony_ci    curl_handle = curl_easy_init();
60141cc406Sopenharmony_ci    if (curl_handle != NULL) {
61141cc406Sopenharmony_ci        snprintf(scan_cmd, sizeof(scan_cmd), "%s%s%s%s",
62141cc406Sopenharmony_ci                 scan_jobs, scanJob, result, scanner_start);
63141cc406Sopenharmony_ci        escl_curl_url(curl_handle, device, scan_cmd);
64141cc406Sopenharmony_ci        curl_easy_setopt(curl_handle, CURLOPT_WRITEFUNCTION, write_callback);
65141cc406Sopenharmony_ci        curl_easy_setopt(curl_handle, CURLOPT_FOLLOWLOCATION, 1L);
66141cc406Sopenharmony_ci        curl_easy_setopt(curl_handle, CURLOPT_MAXREDIRS, 3L);
67141cc406Sopenharmony_ci        if (curl_easy_perform(curl_handle) == CURLE_OK) {
68141cc406Sopenharmony_ci            curl_easy_getinfo(curl_handle, CURLINFO_RESPONSE_CODE, &answer);
69141cc406Sopenharmony_ci            i++;
70141cc406Sopenharmony_ci            if (i >= 15) return;
71141cc406Sopenharmony_ci        }
72141cc406Sopenharmony_ci        curl_easy_cleanup(curl_handle);
73141cc406Sopenharmony_ci        if (SANE_STATUS_GOOD != escl_status(device,
74141cc406Sopenharmony_ci                                            PLATEN,
75141cc406Sopenharmony_ci                                            NULL,
76141cc406Sopenharmony_ci                                            NULL))
77141cc406Sopenharmony_ci            goto CURL_CALL;
78141cc406Sopenharmony_ci    }
79141cc406Sopenharmony_ci}
80