From a34e61220c0315d800df049c03da937e0837ea03 Mon Sep 17 00:00:00 2001 From: Akarshan Kapoor Date: Sun, 3 Nov 2024 17:43:37 +0000 Subject: [PATCH] Addition of mainloop functions for Scan API Signed-off-by: Akarshan Kapoor --- pappl/mainloop-private.h | 10 ++++ pappl/mainloop-subcommands.c | 89 ++++++++++++++++++++++++++++++++++++ pappl/scanner.h | 2 +- pappl/system-accessors.c | 30 ++++++------ pappl/system-private.h | 15 ++++++ 5 files changed, 129 insertions(+), 17 deletions(-) diff --git a/pappl/mainloop-private.h b/pappl/mainloop-private.h index 414b7933..28c1c74a 100644 --- a/pappl/mainloop-private.h +++ b/pappl/mainloop-private.h @@ -28,28 +28,38 @@ extern char *_papplMainloopPath _PAPPL_PRIVATE; // extern int _papplMainloopAddPrinter(const char *base_name, cups_len_t num_options, cups_option_t *options) _PAPPL_PRIVATE; +extern int _papplMainloopAddScanner(const char *base_name, cups_len_t num_options, cups_option_t *options) _PAPPL_PRIVATE; extern int _papplMainloopAutoAddPrinters(const char *base_name, cups_len_t num_options, cups_option_t *options) _PAPPL_PRIVATE; extern int _papplMainloopCancelJob(const char *base_name, cups_len_t num_options, cups_option_t *options) _PAPPL_PRIVATE; extern int _papplMainloopDeletePrinter(const char *base_name, cups_len_t num_options, cups_option_t *options) _PAPPL_PRIVATE; +extern int _papplMainloopDeleteScanner(const char *base_name, cups_len_t num_options, cups_option_t *options) _PAPPL_PRIVATE; extern int _papplMainloopGetSetDefaultPrinter(const char *base_name, cups_len_t num_options, cups_option_t *options) _PAPPL_PRIVATE; +extern int _papplMainloopGetSetDefaultScanner(const char *base_name, cups_len_t num_options, cups_option_t *options) _PAPPL_PRIVATE; extern int _papplMainloopModifyPrinter(const char *base_name, cups_len_t num_options, cups_option_t *options) _PAPPL_PRIVATE; extern int _papplMainloopPausePrinter(const char *base_name, cups_len_t num_options, cups_option_t *options) _PAPPL_PRIVATE; extern int _papplMainloopResumePrinter(const char *base_name, cups_len_t num_options, cups_option_t *options) _PAPPL_PRIVATE; + +// TODO : Introduce functionality for scanner callbacks and scanner drivers. extern int _papplMainloopRunServer(const char *base_name, const char *version, const char *footer_html, cups_len_t num_drivers, pappl_pr_driver_t *drivers, pappl_pr_autoadd_cb_t autoadd_cb, pappl_pr_driver_cb_t driver_cb, cups_len_t num_options, cups_option_t **options, pappl_ml_system_cb_t system_cb, void *data) _PAPPL_PRIVATE; extern int _papplMainloopShowDevices(const char *base_name, cups_len_t num_options, cups_option_t *options) _PAPPL_PRIVATE; + +// TODO : Introduce functionality for scanner callbacks and scanner drivers. extern int _papplMainloopShowDrivers(const char *base_name, cups_len_t num_drivers, pappl_pr_driver_t *drivers, pappl_pr_autoadd_cb_t autoadd_cb, pappl_pr_driver_cb_t driver_cb, cups_len_t num_options, cups_option_t *options, pappl_ml_system_cb_t system_cb, void *data) _PAPPL_PRIVATE; extern int _papplMainloopShowJobs(const char *base_name, cups_len_t num_options, cups_option_t *options) _PAPPL_PRIVATE; extern int _papplMainloopShowOptions(const char *base_name, cups_len_t num_options, cups_option_t *options) _PAPPL_PRIVATE; extern int _papplMainloopShowPrinters(const char *base_name, cups_len_t num_options, cups_option_t *options) _PAPPL_PRIVATE; +extern int _papplMainloopShowScanners(const char *base_name, cups_len_t num_options, cups_option_t *options) _PAPPL_PRIVATE; extern int _papplMainloopShowStatus(const char *base_name, cups_len_t num_options, cups_option_t *options) _PAPPL_PRIVATE; extern int _papplMainloopShutdownServer(const char *base_name, cups_len_t num_options, cups_option_t *options) _PAPPL_PRIVATE; extern int _papplMainloopSubmitJob(const char *base_name, cups_len_t num_options, cups_option_t *options, cups_len_t num_files, char **files) _PAPPL_PRIVATE; extern void _papplMainloopAddOptions(ipp_t *request, cups_len_t num_options, cups_option_t *options, ipp_t *supported) _PAPPL_PRIVATE; extern void _papplMainloopAddPrinterURI(ipp_t *request, const char *printer_name, char *resource,size_t rsize) _PAPPL_PRIVATE; +extern void _papplMainloopAddScannerURI(ipp_t *request, const char *scanner_name, char *resource, size_t rsize) _PAPPL_PRIVATE; // Request type ipp ? extern http_t *_papplMainloopConnect(const char *base_name, bool auto_start) _PAPPL_PRIVATE; extern http_t *_papplMainloopConnectURI(const char *base_name, const char *printer_uri, char *resource, size_t rsize) _PAPPL_PRIVATE; extern char *_papplMainloopGetDefaultPrinter(http_t *http, char *buffer, size_t bufsize) _PAPPL_PRIVATE; +extern char *_papplMainloopGetDefaultScanner(http_t *http, char *buffer, size_t bufsize) _PAPPL_PRIVATE; extern char *_papplMainloopGetServerPath(const char *base_name, uid_t uid, char *buffer, size_t bufsize) _PAPPL_PRIVATE; extern int _papplMainloopGetServerPort(const char *base_name) _PAPPL_PRIVATE; diff --git a/pappl/mainloop-subcommands.c b/pappl/mainloop-subcommands.c index 62f8c874..39e9b5e8 100644 --- a/pappl/mainloop-subcommands.c +++ b/pappl/mainloop-subcommands.c @@ -111,6 +111,95 @@ _papplMainloopAddPrinter( } + +// +// '_papplMainloopAddScanner()' - Add a scanner using eSCL. +// +int +_papplMainloopAddScanner( + const char *base_name, // I - Base name + cups_len_t num_options, // I - Number of options + cups_option_t *options) // I - Options +{ + http_t *http = NULL; // Connection to server + const char *device_uri, // Device URI + *scanner_name, // Name of scanner + *scanner_uri, // Scanner URI + *escl_path; // eSCL resource path + char resource[1024]; // Resource path for connection + bool status = false; // Status of scanner addition + + // Get required values... + device_uri = cupsGetOption("device-uri", (int)num_options, options); + scanner_name = cupsGetOption("scanner-name", (int)num_options, options); + escl_path = cupsGetOption("escl", (int)num_options, options); + + // Rest of the implementation remains the same... + if (!device_uri || !scanner_name) + { + if (!scanner_name) + _papplLocPrintf(stderr, _PAPPL_LOC("%s: Missing '-d SCANNER'."), base_name); + if (!device_uri) + _papplLocPrintf(stderr, _PAPPL_LOC("%s: Missing '-v DEVICE-URI'."), base_name); + return (1); + } + + if ((scanner_uri = cupsGetOption("scanner-uri", (int)num_options, options)) != NULL) + { + if ((http = _papplMainloopConnectURI(base_name, scanner_uri, resource, + sizeof(resource))) == NULL) + return (1); + } + + // Set up eSCL connection and registration + if (!escl_path) + escl_path = "/eSCL/"; // Default eSCL path if not specified + + // Create scanner registration request + char *post_data = NULL; + size_t post_size = 0; + FILE *post_file = open_memstream(&post_data, &post_size); + + if (post_file) + { + // Format eSCL scanner registration XML + fprintf(post_file, "\n"); + fprintf(post_file, "\n"); + fprintf(post_file, " %s\n", scanner_name); + fprintf(post_file, " %s\n", device_uri); + fprintf(post_file, "\n"); + fclose(post_file); + + // Set up HTTP POST request + httpClearFields(http); + httpSetField(http, HTTP_FIELD_CONTENT_TYPE, "application/xml"); + httpSetLength(http, post_size); + + // Send the registration request + if (httpPost(http, escl_path) == HTTP_STATUS_OK) + { + http_status_t response = httpUpdate(http); + if (response == HTTP_STATUS_OK || response == HTTP_STATUS_CREATED) + status = true; + } + + free(post_data); + } + + httpClose(http); + + if (!status) + { + _papplLocPrintf(stderr, _PAPPL_LOC("%s: Unable to add scanner: %s"), + base_name, cupsGetErrorString()); + return (1); + } + + return (0); +} + + + // // '_papplMainloopAutoAddPrinters()' - Automatically add printers. // diff --git a/pappl/scanner.h b/pappl/scanner.h index 8f0577c3..b39c99cb 100644 --- a/pappl/scanner.h +++ b/pappl/scanner.h @@ -125,7 +125,7 @@ typedef struct pappl_sc_driver_data_s // Initially polling the scanner driver f pappl_identify_sc_actions_t identify_default; // "identify-actions-default" values pappl_identify_sc_actions_t identify_supported; // "identify-actions-supported" values - pappl_icon_sc_t icons[3]; // "printer-icons" values + pappl_icon_sc_t icons[3]; // "scanner-icons" values char make_and_model[128]; // Make and model of the scanner const char *document_formats_supported[PAPPL_MAX_FORMATS]; // Supported document formats (JPEG, PDF, TIFF, PNG, BMP) diff --git a/pappl/system-accessors.c b/pappl/system-accessors.c index 00c08cd3..2821169b 100644 --- a/pappl/system-accessors.c +++ b/pappl/system-accessors.c @@ -2529,22 +2529,20 @@ papplSystemSetScannerDrivers( _papplRWLockWrite(system); // Set the system's scanner-related fields - system->num_sc_drivers = (cups_len_t)num_drivers; - system->sc_drivers = drivers; - system->sc_identify_cb = identify_cb; - system->sc_create_cb = create_cb; - system->sc_driver_cb = driver_cb; - system->sc_delete_cb = sc_delete_cb; - system->sc_capabilities_cb = capabilities_cb; - system->sc_job_create_cb = job_create_cb; - system->sc_job_delete_cb = job_delete_cb; - system->sc_data_cb = data_cb; - system->sc_status_cb = status_cb; - system->sc_job_complete_cb = job_complete_cb; - system->sc_job_cancel_cb = job_cancel_cb; - system->sc_buffer_info_cb = buffer_info_cb; - system->sc_image_info_cb = image_info_cb; - system->sc_cbdata = data; + system->num_scanner_drivers = (cups_len_t)num_drivers; + system->scanner_drivers = drivers; + system->identify_cb = identify_cb; + system->sc_delete_cb = sc_delete_cb; + system->capabilities_cb = capabilities_cb; + system->job_create_cb = job_create_cb; + system->job_delete_cb = job_delete_cb; + system->data_cb = data_cb; + system->status_cb = status_cb; + system->job_complete_cb = job_complete_cb; + system->job_cancel_cb = job_cancel_cb; + system->buffer_info_cb = buffer_info_cb; + system->image_info_cb = image_info_cb; + system->sc_driver_cbdata = data; _papplRWUnlock(system); } diff --git a/pappl/system-private.h b/pappl/system-private.h index 61de2a15..17df61cb 100644 --- a/pappl/system-private.h +++ b/pappl/system-private.h @@ -112,6 +112,21 @@ struct _pappl_system_s // System data pappl_pr_driver_t *drivers; // Printer drivers pappl_sc_driver_t *scanner_drivers; // Scanner drivers + + cups_len_t num_scanner_drivers; // Number of scanner drivers + pappl_sc_identify_cb_t identify_cb; // Callback for identifying the scanner + pappl_sc_delete_cb_t sc_delete_cb; // Scanner deletion callback + pappl_sc_capabilities_cb_t capabilities_cb; // Callback for getting scanner capabilities + pappl_sc_job_create_cb_t job_create_cb; // Callback for creating a scan job + pappl_sc_job_delete_cb_t job_delete_cb; // Callback for deleting a scan job + pappl_sc_data_cb_t data_cb; // Callback for getting scan data + pappl_sc_status_cb_t status_cb; // Callback for getting scanner status + pappl_sc_job_complete_cb_t job_complete_cb; // Callback for completing a scan job + pappl_sc_job_cancel_cb_t job_cancel_cb; // Callback for cancelling a scan job + pappl_sc_buffer_info_cb_t buffer_info_cb; // Callback for getting buffer information + pappl_sc_image_info_cb_t image_info_cb; // Callback for getting image information + pappl_identify_sc_actions_t identify_default; // "identify-actions-default" values + pappl_identify_sc_actions_t identify_supported; // "identify-actions-supported" values pappl_sc_autoadd_cb_t autoadd_sc_cb; // Scanner driver auto-add callback pappl_sc_create_cb_t create_sc_cb; // Scanner driver creation callback pappl_sc_driver_cb_t driver_sc_cb; // Scanner driver initialization callback