Skip to content

Commit

Permalink
Addition of mainloop functions for Scan API
Browse files Browse the repository at this point in the history
Signed-off-by: Akarshan Kapoor <[email protected]>
  • Loading branch information
Kappuccino111 committed Nov 3, 2024
1 parent 128769a commit a34e612
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 17 deletions.
10 changes: 10 additions & 0 deletions pappl/mainloop-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
89 changes: 89 additions & 0 deletions pappl/mainloop-subcommands.c
Original file line number Diff line number Diff line change
Expand Up @@ -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, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
fprintf(post_file, "<scan:ScannerRegistration xmlns:scan=\"http://schemas.hp.com/imaging/escl/2011/05/03\">\n");
fprintf(post_file, " <scan:ScannerName>%s</scan:ScannerName>\n", scanner_name);
fprintf(post_file, " <scan:DeviceURI>%s</scan:DeviceURI>\n", device_uri);
fprintf(post_file, "</scan:ScannerRegistration>\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.
//
Expand Down
2 changes: 1 addition & 1 deletion pappl/scanner.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
30 changes: 14 additions & 16 deletions pappl/system-accessors.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
15 changes: 15 additions & 0 deletions pappl/system-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a34e612

Please sign in to comment.