From: Bastian Dehn Date: Thu, 1 Aug 2024 07:57:40 +0000 (+0200) Subject: refactor set options handler X-Git-Tag: v1.0.4^2~2 X-Git-Url: https://gitweb.hhaalo.de/?a=commitdiff_plain;h=73320e3a518689c28675e17860c303beadb5f5e1;p=sane-kds-s2000w-net.git refactor set options handler --- diff --git a/src/kds_s2000w_handler_opts.c b/src/kds_s2000w_handler_opts.c index 264fe91..934ffa2 100644 --- a/src/kds_s2000w_handler_opts.c +++ b/src/kds_s2000w_handler_opts.c @@ -41,9 +41,8 @@ void _load_options(handler* h) { json_object_put(h->current_scanner_config); h->current_scanner_config = NULL; - response* resp = NULL; - resp = kds_s2000w_client_response_init(); + response* resp = kds_s2000w_client_response_init(); int result = kds_s2000w_client_get_option(h->sessionid, resp); h->current_scanner_config = json_tokener_parse(resp->data); @@ -62,10 +61,65 @@ void _load_options(handler* h) resp = NULL; } +void _set_option_to_default(handler* h) +{ + response* resp = kds_s2000w_client_response_init(); + kds_s2000w_client_get_capabilities(resp); + json_object* capabilities = json_tokener_parse(resp->data); + json_object* default_values = json_object_object_get(capabilities, "Defaults"); + json_object* autostart = json_object_object_get(default_values, "AutoStart"); + json_object_set_int(autostart, AUTOSTART_ALWAYS_ON); + json_object* default_config = json_object_new_object(); + json_object_object_add(default_config, "Configuration", default_values); + + const char* json_string = json_object_to_json_string_ext(default_config, JSON_C_TO_STRING_PLAIN); + kds_s2000w_client_response_free(resp); + resp = NULL; + + resp = kds_s2000w_client_response_init(); + resp->size = sizeof(char) * strlen(json_string); + resp->code = 0; + resp->data = realloc(resp->data, resp->size); + resp->data = memcpy(resp->data, json_string, resp->size); + kds_s2000w_client_set_option(h->sessionid, resp); +#ifndef NODEBUG + if (resp->code != 200) + debug_printf_int(ERROR, "set options response code", resp->code); +#endif + + kds_s2000w_client_response_free(resp); + resp = NULL; + json_object_put(capabilities); + capabilities = NULL; + + h->current_metadata->format = 1; + h->current_metadata->channels = 3; + h->current_metadata->depth = 8; +} + +void _set_options(handler* h) +{ + const char* json_string = json_object_to_json_string_ext(h->current_scanner_config, JSON_C_TO_STRING_PLAIN); + response* resp = kds_s2000w_client_response_init(); + resp->size = sizeof(char) * strlen(json_string); + resp->code = 0; + resp->data = realloc(resp->data, resp->size); + resp->data = memcpy(resp->data, json_string, resp->size); + kds_s2000w_client_set_option(h->sessionid, resp); +#ifndef NODEBUG + if (resp->code != 200) + debug_printf_int(ERROR, "set options response code", resp->code); +#endif + + kds_s2000w_client_response_free(resp); + resp = NULL; +} + void kds_s2000w_handler_get_option(handler* h, int option, void* value, int* info) { json_object* config = NULL; json_object* value_object = NULL; + if (h->current_scan_status->load_options == 0 && option > 0) { h->current_scan_status->load_options = 1; _load_options(h); @@ -176,23 +230,15 @@ void kds_s2000w_handler_set_option(handler* h, int option, void* value, int* inf if (h->read_info->scan_started) return; - response* resp = NULL; - json_object* value_object = NULL; - json_object* capabilities = NULL; - json_object* default_values = NULL; - json_object* autostart = NULL; - json_object* default_config = NULL; - json_object* config = NULL; - if (h->current_scan_status->load_options == 0 && option > 0) { h->current_scan_status->load_options = 1; _load_options(h); } - config = json_object_object_get(h->current_scanner_config, "Configuration"); + json_object* config = json_object_object_get(h->current_scanner_config, "Configuration"); int autostart_value = AUTOSTART_ALWAYS_ON; - value_object = json_object_object_get(config, "AutoStart"); + json_object* value_object = json_object_object_get(config, "AutoStart"); _write_int_value_to_json(value_object, &autostart_value); value_object = NULL; @@ -302,38 +348,7 @@ void kds_s2000w_handler_set_option(handler* h, int option, void* value, int* inf _write_int_value_to_json(value_object, value); break; case 21: - resp = kds_s2000w_client_response_init(); - kds_s2000w_client_get_capabilities(resp); - capabilities = json_tokener_parse(resp->data); - default_values = json_object_object_get(capabilities, "Defaults"); - autostart = json_object_object_get(default_values, "AutoStart"); - json_object_set_int(autostart, AUTOSTART_ALWAYS_ON); - default_config = json_object_new_object(); - json_object_object_add(default_config, "Configuration", default_values); - - const char* json_string = json_object_to_json_string_ext(default_config, JSON_C_TO_STRING_PLAIN); - kds_s2000w_client_response_free(resp); - resp = NULL; - - resp = kds_s2000w_client_response_init(); - resp->size = sizeof(char) * strlen(json_string); - resp->code = 0; - resp->data = realloc(resp->data, resp->size); - resp->data = memcpy(resp->data, json_string, resp->size); - kds_s2000w_client_set_option(h->sessionid, resp); -#ifndef NODEBUG - if (resp->code != 200) - debug_printf_int(ERROR, "set options response code", resp->code); -#endif - - kds_s2000w_client_response_free(resp); - resp = NULL; - json_object_put(capabilities); - capabilities = NULL; - - h->current_metadata->format = 1; - h->current_metadata->channels = 3; - h->current_metadata->depth = 8; + _set_option_to_default(h); if (info != NULL) *info = RELOAD_OPTIONS | RELOAD_PARAMS; @@ -345,18 +360,5 @@ 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(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; - resp->data = realloc(resp->data, resp->size); - resp->data = memcpy(resp->data, json_string, resp->size); - kds_s2000w_client_set_option(h->sessionid, resp); -#ifndef NODEBUG - if (resp->code != 200) - debug_printf_int(ERROR, "set options response code", resp->code); -#endif - - kds_s2000w_client_response_free(resp); - resp = NULL; + _set_options(h); } \ No newline at end of file