]> gitweb.hhaalo.de Git - sane-kds-s2000w-net.git/commitdiff
change response config into handler
authorBastian Dehn <hhaalo@arcor.de>
Thu, 21 Mar 2024 16:33:19 +0000 (17:33 +0100)
committerBastian Dehn <hhaalo@arcor.de>
Thu, 21 Mar 2024 16:33:19 +0000 (17:33 +0100)
src/kds_s2000w_handler.c
src/kds_s2000w_handler.h
src/kds_s2000w_handler_opts.c

index 95bb70d89c2299ba268d57257ba8c0f82242902a..64a2ca295fabd05ce0731f29950bddb0c0a1209c 100644 (file)
@@ -1,7 +1,6 @@
 #include <stdio.h>
 #include <string.h>
 #include <unistd.h>
-#include <json-c/json.h>
 #include <math.h>
 #include "kds_s2000w_handler.h"
 #include "kds_s2000w_handler_opts.h"
@@ -9,8 +8,6 @@
 #include "kds_s2000w_image_converter.h"
 #include "kds_s2000w_debug.h"
 
-json_object* resp_config = NULL;
-
 void _get_current_metadata(handler* h)
 {
        debug_printf(ALL, "get_current_metadata");
@@ -55,7 +52,7 @@ void _get_current_metadata(handler* h)
 
        json_object_put(metadataResp);
        metadata = NULL;
-       config = json_object_object_get(resp_config, "Configuration");
+       config = json_object_object_get(h->current_scanner_config, "Configuration");
        metadata = json_object_object_get(config, "ColorMode");
        const char* color_value = json_object_get_string(metadata);
        mdata_value = NULL;
@@ -169,6 +166,7 @@ handler* init_handler()
 {
        debug_printf(ALL, "init handler");
        handler* h = malloc(sizeof(handler));
+       h->current_scanner_config = NULL;
        h->current_scan_status = malloc(sizeof(scanstatus));
        h->current_metadata = malloc(sizeof(metadata));
        h->read_info = malloc(sizeof(readinfo));
@@ -206,6 +204,8 @@ handler* init_handler()
 void free_handler(handler* h)
 {
        debug_printf(ALL, "free handler");
+       json_object_put(h->current_scanner_config);
+       h->current_scanner_config = NULL;
        free(h->current_scan_status);
        h->current_scan_status = NULL;
        free(h->current_metadata);
@@ -272,7 +272,7 @@ void kds_s2000w_handler_recreate_session(handler* h)
                // set old config parameters
                kds_s2000w_client_response_free(resp);
                resp = NULL;
-               const char* json_string = json_object_to_json_string_ext(resp_config, JSON_C_TO_STRING_PLAIN);
+               const char* json_string = json_object_to_json_string_ext(h->current_scanner_config, JSON_C_TO_STRING_PLAIN);
                resp = kds_s2000w_client_response_init();
                resp->size = sizeof(char) * strlen(json_string);
                resp->code = 0;
@@ -328,16 +328,16 @@ void kds_s2000w_handler_open(const char* devicename, void** handle)
        }
 
        if (resp->code == 200) {
-               resp_config = json_tokener_parse(resp->data);
-               json_object_object_get_ex(resp_config, "SessionId", &value_object);
+               h->current_scanner_config = json_tokener_parse(resp->data);
+               json_object_object_get_ex(h->current_scanner_config, "SessionId", &value_object);
                h->sessionid = json_object_get_int64(value_object);
                h->state = OPENED;
                debug_printf_long(INFO, "SessionId", h->sessionid);
                value_object = NULL;
        }
 
-       json_object_put(resp_config);
-       resp_config = NULL;
+       json_object_put(h->current_scanner_config);
+       h->current_scanner_config = NULL;
        kds_s2000w_client_response_free(resp);
        resp = NULL;
 }
@@ -345,8 +345,8 @@ void kds_s2000w_handler_open(const char* devicename, void** handle)
 void kds_s2000w_handler_close(handler* h)
 {
        debug_printf(ALL, "kds_s2000w_handler_close");
-       json_object_put(resp_config);
-       resp_config = NULL;
+       json_object_put(h->current_scanner_config);
+       h->current_scanner_config = NULL;
 
        if (h == NULL)
                return;
index 1fd9ca7846ba15864d04997dec2f5e4754940ee7..aba0ca5a3c37e4e6fef4fe50ca452b0c0aad9ad4 100644 (file)
@@ -1,5 +1,7 @@
 #ifndef KDS_S2000W_HANDLER_H
 #define KDS_S2000W_HANDLER_H
+#include <json-c/json.h>
+
 enum {
        NOTCONNECTED,
        OPENED,
@@ -46,6 +48,7 @@ typedef struct {
 typedef struct {
        long sessionid;
        int state;
+       json_object* current_scanner_config;
        scanstatus* current_scan_status;
        metadata* current_metadata;
        readinfo* read_info;
index cc47e5398eb5fb72a0aaa9fd7efe12e957230a98..8ff8c26895b93413e604c3b8248919c4489c9975 100644 (file)
@@ -8,8 +8,6 @@
 #define AUTOSTART_ALWAYS_ON 1
 #define COUNT_CUSTOM_OPTIONS 2
 
-extern json_object* resp_config;
-
 void _write_string_value(json_object* value_object, void* value)
 {
        const char* string_value = json_object_get_string(value_object);
@@ -38,20 +36,20 @@ void _write_int_value_to_json(json_object* value_object, void* value)
 
 void _load_options(handler* h)
 {
-       json_object_put(resp_config);
-       resp_config = NULL;
+       json_object_put(h->current_scanner_config);
+       h->current_scanner_config = NULL;
        response* resp = NULL;
 
        resp = kds_s2000w_client_response_init();
        int result = kds_s2000w_client_get_option(h->sessionid, resp);
-       resp_config = json_tokener_parse(resp->data);
+       h->current_scanner_config = json_tokener_parse(resp->data);
 
        if (result != 0 || resp->code != 200) {
                debug_printf_int(ERROR, "load options response code", resp->code);
                kds_s2000w_client_response_free(resp);
                resp = NULL;
-               json_object_put(resp_config);
-               resp_config = NULL;
+               json_object_put(h->current_scanner_config);
+               h->current_scanner_config = NULL;
                return;
        }
 
@@ -63,13 +61,13 @@ void kds_s2000w_handler_get_option(handler* h, int option, void* value, int* inf
 {
        json_object* config = NULL;
        json_object* value_object = NULL;
-       if (resp_config != NULL)
-               config = json_object_object_get(resp_config, "Configuration");
+       if (h->current_scanner_config != NULL)
+               config = json_object_object_get(h->current_scanner_config, "Configuration");
 
        switch(option) {
                case 0:
                        _load_options(h);
-                       config = json_object_object_get(resp_config, "Configuration");
+                       config = json_object_object_get(h->current_scanner_config, "Configuration");
                        int* int_value_ptr = (int*) value;
                        *int_value_ptr = json_object_object_length(config) + COUNT_CUSTOM_OPTIONS;
                        debug_printf_int(DEBUG, "option numbers", *int_value_ptr);
@@ -173,7 +171,7 @@ void kds_s2000w_handler_set_option(handler* h, int option, void* value, int* inf
        json_object* default_config = NULL;
        json_object* config = NULL;
 
-       config = json_object_object_get(resp_config, "Configuration");
+       config = json_object_object_get(h->current_scanner_config, "Configuration");
 
        int autostart_value = AUTOSTART_ALWAYS_ON;
        value_object = json_object_object_get(config, "AutoStart");
@@ -319,7 +317,7 @@ void kds_s2000w_handler_set_option(handler* h, int option, void* value, int* inf
                        break;
        }
 
-       const char* json_string = json_object_to_json_string_ext(resp_config, JSON_C_TO_STRING_PLAIN);
+       const char* json_string = json_object_to_json_string_ext(h->current_scanner_config, JSON_C_TO_STRING_PLAIN);
        resp = kds_s2000w_client_response_init();
        resp->size = sizeof(char) * strlen(json_string);
        resp->code = 0;