From 1373c18f1c86c9c375d7dc3606bc2e5df2f72f62 Mon Sep 17 00:00:00 2001 From: Bastian Dehn Date: Thu, 21 Mar 2024 17:33:19 +0100 Subject: [PATCH] change response config into handler --- src/kds_s2000w_handler.c | 22 +++++++++++----------- src/kds_s2000w_handler.h | 3 +++ src/kds_s2000w_handler_opts.c | 22 ++++++++++------------ 3 files changed, 24 insertions(+), 23 deletions(-) diff --git a/src/kds_s2000w_handler.c b/src/kds_s2000w_handler.c index 95bb70d..64a2ca2 100644 --- a/src/kds_s2000w_handler.c +++ b/src/kds_s2000w_handler.c @@ -1,7 +1,6 @@ #include #include #include -#include #include #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; diff --git a/src/kds_s2000w_handler.h b/src/kds_s2000w_handler.h index 1fd9ca7..aba0ca5 100644 --- a/src/kds_s2000w_handler.h +++ b/src/kds_s2000w_handler.h @@ -1,5 +1,7 @@ #ifndef KDS_S2000W_HANDLER_H #define KDS_S2000W_HANDLER_H +#include + 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; diff --git a/src/kds_s2000w_handler_opts.c b/src/kds_s2000w_handler_opts.c index cc47e53..8ff8c26 100644 --- a/src/kds_s2000w_handler_opts.c +++ b/src/kds_s2000w_handler_opts.c @@ -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; -- 2.39.5