From a2e9098dd7298b256f743e6ba483d46d5627b3de Mon Sep 17 00:00:00 2001 From: Bastian Dehn Date: Sat, 28 Dec 2024 13:58:02 +0100 Subject: [PATCH] set autostart with one method in handler only one load config --- src/kds_s2000w_handler_opts.c | 46 ++++------ tests/kds_s2000w_net_get_opt_tests.c | 130 ++++++++++++--------------- 2 files changed, 76 insertions(+), 100 deletions(-) diff --git a/src/kds_s2000w_handler_opts.c b/src/kds_s2000w_handler_opts.c index c82ba75..a5be246 100644 --- a/src/kds_s2000w_handler_opts.c +++ b/src/kds_s2000w_handler_opts.c @@ -5,8 +5,7 @@ #include "kds_s2000w_handler_opts.h" #include "kds_s2000w_debug.h" -#define AUTOSTART_ALWAYS_ON 1 -#define COUNT_CUSTOM_OPTIONS 0 +#define AUTOSTART_ON 1 void _write_string_value(json_object* value_object, void* value) { @@ -56,34 +55,30 @@ void _load_options(handler* h) resp = NULL; } +void _set_autostart_on(handler* h) +{ + int auto_start_value = AUTOSTART_ON; + json_object* config = json_object_object_get(h->current_scanner_config, "Configuration"); + json_object* value_object = json_object_object_get(config, "AutoStart"); + _write_int_value_to_json(value_object, &auto_start_value); +} + 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; + json_object_put(h->current_scanner_config); + h->current_scanner_config = 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); - if (resp->code != 200) - debug_printf_int(ERROR, "set options response code", resp->code); + h->current_scanner_config = default_config; kds_s2000w_client_response_free(resp); resp = NULL; - json_object_put(capabilities); - capabilities = NULL; } void _set_options(handler* h) @@ -107,9 +102,11 @@ void kds_s2000w_handler_get_option(handler* h, int option, void* value, int* inf json_object* config = NULL; json_object* value_object = NULL; - if (h->current_scan_status->load_options == 0 && option > 0) { + if (h->current_scan_status->load_options < 1) { h->current_scan_status->load_options = 1; _load_options(h); + _set_autostart_on(h); + _set_options(h); } if (h->current_scanner_config != NULL) @@ -117,10 +114,9 @@ void kds_s2000w_handler_get_option(handler* h, int option, void* value, int* inf switch(option) { case 0: - _load_options(h); 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; + *int_value_ptr = json_object_object_length(config); debug_printf_int(DEBUG, "option numbers", *int_value_ptr); break; case 2: @@ -218,14 +214,11 @@ void kds_s2000w_handler_set_option(handler* h, int option, void* value, int* inf if (h->current_scan_status->load_options == 0 && option > 0) { h->current_scan_status->load_options = 1; _load_options(h); + _set_autostart_on(h); } json_object* config = json_object_object_get(h->current_scanner_config, "Configuration"); - - int autostart_value = AUTOSTART_ALWAYS_ON; - json_object* value_object = json_object_object_get(config, "AutoStart"); - _write_int_value_to_json(value_object, &autostart_value); - value_object = NULL; + json_object* value_object = NULL; switch(option) { case 2: @@ -318,11 +311,10 @@ void kds_s2000w_handler_set_option(handler* h, int option, void* value, int* inf break; case 21: _set_option_to_default(h); + _set_autostart_on(h); if (info != NULL) *info = RELOAD_OPTIONS | RELOAD_PARAMS; - - _load_options(h); return; break; default: diff --git a/tests/kds_s2000w_net_get_opt_tests.c b/tests/kds_s2000w_net_get_opt_tests.c index 2cc3599..61dc299 100644 --- a/tests/kds_s2000w_net_get_opt_tests.c +++ b/tests/kds_s2000w_net_get_opt_tests.c @@ -29,7 +29,10 @@ void sane_kds_s2000w_net_control_get_option_zero(void** state) response* resp = (response*) *state; will_return(mock_response, resp); will_return(__wrap_kds_s2000w_client_get_option, 0); + will_return(mock_response, resp); + will_return(__wrap_kds_s2000w_client_set_option, 0); expect_function_call(__wrap_kds_s2000w_client_get_option); + expect_function_call(__wrap_kds_s2000w_client_set_option); handler* h = init_handler(); int value = 0; @@ -43,11 +46,10 @@ void sane_kds_s2000w_net_control_get_option_zero(void** state) void sane_kds_s2000w_net_control_get_option_two(void** state) { - response* resp = (response*) *state; - will_return(mock_response, resp); - will_return(__wrap_kds_s2000w_client_get_option, 0); - expect_function_call(__wrap_kds_s2000w_client_get_option); handler* h = init_handler(); + h->current_scan_status->load_options = 1; + response* resp = (response*) *state; + h->current_scanner_config = json_tokener_parse(resp->data); char value[50] = {0}; _sane_kds_s2000w_net_control_option(h, 2, SANE_ACTION_GET_VALUE, &value, NULL); @@ -61,11 +63,10 @@ void sane_kds_s2000w_net_control_get_option_two(void** state) void sane_kds_s2000w_net_control_get_option_three(void** state) { - response* resp = (response*) *state; - will_return(mock_response, resp); - will_return(__wrap_kds_s2000w_client_get_option, 0); - expect_function_call(__wrap_kds_s2000w_client_get_option); handler* h = init_handler(); + h->current_scan_status->load_options = 1; + response* resp = (response*) *state; + h->current_scanner_config = json_tokener_parse(resp->data); char value[50] = {0}; _sane_kds_s2000w_net_control_option(h, 3, SANE_ACTION_GET_VALUE, &value, NULL); @@ -78,11 +79,10 @@ void sane_kds_s2000w_net_control_get_option_three(void** state) void sane_kds_s2000w_net_control_get_option_four(void** state) { - response* resp = (response*) *state; - will_return(mock_response, resp); - will_return(__wrap_kds_s2000w_client_get_option, 0); - expect_function_call(__wrap_kds_s2000w_client_get_option); handler* h = init_handler(); + h->current_scan_status->load_options = 1; + response* resp = (response*) *state; + h->current_scanner_config = json_tokener_parse(resp->data); int value = 0; _sane_kds_s2000w_net_control_option(h, 4, SANE_ACTION_GET_VALUE, &value, NULL); @@ -95,12 +95,10 @@ void sane_kds_s2000w_net_control_get_option_four(void** state) void sane_kds_s2000w_net_control_get_option_five(void** state) { - response* resp = (response*) *state; - will_return(mock_response, resp); - will_return(__wrap_kds_s2000w_client_get_option, 0); - expect_function_call(__wrap_kds_s2000w_client_get_option); handler* h = init_handler(); - + h->current_scan_status->load_options = 1; + response* resp = (response*) *state; + h->current_scanner_config = json_tokener_parse(resp->data); char value[50] = {0}; _sane_kds_s2000w_net_control_option(h, 5, SANE_ACTION_GET_VALUE, &value, NULL); @@ -113,11 +111,10 @@ void sane_kds_s2000w_net_control_get_option_five(void** state) void sane_kds_s2000w_net_control_get_option_seven(void** state) { - response* resp = (response*) *state; - will_return(mock_response, resp); - will_return(__wrap_kds_s2000w_client_get_option, 0); - expect_function_call(__wrap_kds_s2000w_client_get_option); handler* h = init_handler(); + h->current_scan_status->load_options = 1; + response* resp = (response*) *state; + h->current_scanner_config = json_tokener_parse(resp->data); int value = -1; _sane_kds_s2000w_net_control_option(h, 7, SANE_ACTION_GET_VALUE, &value, NULL); @@ -130,11 +127,10 @@ void sane_kds_s2000w_net_control_get_option_seven(void** state) void sane_kds_s2000w_net_control_get_option_eight(void** state) { - response* resp = (response*) *state; - will_return(mock_response, resp); - will_return(__wrap_kds_s2000w_client_get_option, 0); - expect_function_call(__wrap_kds_s2000w_client_get_option); handler* h = init_handler(); + h->current_scan_status->load_options = 1; + response* resp = (response*) *state; + h->current_scanner_config = json_tokener_parse(resp->data); char value[50] = {0}; _sane_kds_s2000w_net_control_option(h, 8, SANE_ACTION_GET_VALUE, &value, NULL); @@ -147,11 +143,10 @@ void sane_kds_s2000w_net_control_get_option_eight(void** state) void sane_kds_s2000w_net_control_get_option_nine(void** state) { - response* resp = (response*) *state; - will_return(mock_response, resp); - will_return(__wrap_kds_s2000w_client_get_option, 0); - expect_function_call(__wrap_kds_s2000w_client_get_option); handler* h = init_handler(); + h->current_scan_status->load_options = 1; + response* resp = (response*) *state; + h->current_scanner_config = json_tokener_parse(resp->data); int value = -1; _sane_kds_s2000w_net_control_option(h, 9, SANE_ACTION_GET_VALUE, &value, NULL); @@ -164,11 +159,10 @@ void sane_kds_s2000w_net_control_get_option_nine(void** state) void sane_kds_s2000w_net_control_get_option_ten(void** state) { - response* resp = (response*) *state; - will_return(mock_response, resp); - will_return(__wrap_kds_s2000w_client_get_option, 0); - expect_function_call(__wrap_kds_s2000w_client_get_option); handler* h = init_handler(); + h->current_scan_status->load_options = 1; + response* resp = (response*) *state; + h->current_scanner_config = json_tokener_parse(resp->data); char value[50] = {0}; _sane_kds_s2000w_net_control_option(h, 10, SANE_ACTION_GET_VALUE, &value, NULL); @@ -181,11 +175,10 @@ void sane_kds_s2000w_net_control_get_option_ten(void** state) void sane_kds_s2000w_net_control_get_option_eleven(void** state) { - response* resp = (response*) *state; - will_return(mock_response, resp); - will_return(__wrap_kds_s2000w_client_get_option, 0); - expect_function_call(__wrap_kds_s2000w_client_get_option); handler* h = init_handler(); + h->current_scan_status->load_options = 1; + response* resp = (response*) *state; + h->current_scanner_config = json_tokener_parse(resp->data); char value[50] = {0}; _sane_kds_s2000w_net_control_option(h, 11, SANE_ACTION_GET_VALUE, &value, NULL); @@ -198,11 +191,10 @@ void sane_kds_s2000w_net_control_get_option_eleven(void** state) void sane_kds_s2000w_net_control_get_option_twelve(void** state) { - response* resp = (response*) *state; - will_return(mock_response, resp); - will_return(__wrap_kds_s2000w_client_get_option, 0); - expect_function_call(__wrap_kds_s2000w_client_get_option); handler* h = init_handler(); + h->current_scan_status->load_options = 1; + response* resp = (response*) *state; + h->current_scanner_config = json_tokener_parse(resp->data); int value = -1; _sane_kds_s2000w_net_control_option(h, 12, SANE_ACTION_GET_VALUE, &value, NULL); @@ -215,11 +207,10 @@ void sane_kds_s2000w_net_control_get_option_twelve(void** state) void sane_kds_s2000w_net_control_get_option_thirdteen(void** state) { - response* resp = (response*) *state; - will_return(mock_response, resp); - will_return(__wrap_kds_s2000w_client_get_option, 0); - expect_function_call(__wrap_kds_s2000w_client_get_option); handler* h = init_handler(); + h->current_scan_status->load_options = 1; + response* resp = (response*) *state; + h->current_scanner_config = json_tokener_parse(resp->data); int value = -1; _sane_kds_s2000w_net_control_option(h, 13, SANE_ACTION_GET_VALUE, &value, NULL); @@ -232,11 +223,10 @@ void sane_kds_s2000w_net_control_get_option_thirdteen(void** state) void sane_kds_s2000w_net_control_get_option_fourteen(void** state) { - response* resp = (response*) *state; - will_return(mock_response, resp); - will_return(__wrap_kds_s2000w_client_get_option, 0); - expect_function_call(__wrap_kds_s2000w_client_get_option); handler* h = init_handler(); + h->current_scan_status->load_options = 1; + response* resp = (response*) *state; + h->current_scanner_config = json_tokener_parse(resp->data); int value = -1; _sane_kds_s2000w_net_control_option(h, 14, SANE_ACTION_GET_VALUE, &value, NULL); @@ -249,11 +239,10 @@ void sane_kds_s2000w_net_control_get_option_fourteen(void** state) void sane_kds_s2000w_net_control_get_option_fifteen(void** state) { - response* resp = (response*) *state; - will_return(mock_response, resp); - will_return(__wrap_kds_s2000w_client_get_option, 0); - expect_function_call(__wrap_kds_s2000w_client_get_option); handler* h = init_handler(); + h->current_scan_status->load_options = 1; + response* resp = (response*) *state; + h->current_scanner_config = json_tokener_parse(resp->data); int value = -1; _sane_kds_s2000w_net_control_option(h, 15, SANE_ACTION_GET_VALUE, &value, NULL); @@ -266,11 +255,10 @@ void sane_kds_s2000w_net_control_get_option_fifteen(void** state) void sane_kds_s2000w_net_control_get_option_sixteen(void** state) { - response* resp = (response*) *state; - will_return(mock_response, resp); - will_return(__wrap_kds_s2000w_client_get_option, 0); - expect_function_call(__wrap_kds_s2000w_client_get_option); handler* h = init_handler(); + h->current_scan_status->load_options = 1; + response* resp = (response*) *state; + h->current_scanner_config = json_tokener_parse(resp->data); char value[50] = {0}; _sane_kds_s2000w_net_control_option(h, 16, SANE_ACTION_GET_VALUE, &value, NULL); @@ -283,11 +271,10 @@ void sane_kds_s2000w_net_control_get_option_sixteen(void** state) void sane_kds_s2000w_net_control_get_option_seventeen(void** state) { - response* resp = (response*) *state; - will_return(mock_response, resp); - will_return(__wrap_kds_s2000w_client_get_option, 0); - expect_function_call(__wrap_kds_s2000w_client_get_option); handler* h = init_handler(); + h->current_scan_status->load_options = 1; + response* resp = (response*) *state; + h->current_scanner_config = json_tokener_parse(resp->data); int value = -1; _sane_kds_s2000w_net_control_option(h, 17, SANE_ACTION_GET_VALUE, &value, NULL); @@ -300,11 +287,10 @@ void sane_kds_s2000w_net_control_get_option_seventeen(void** state) void sane_kds_s2000w_net_control_get_option_eightteen(void** state) { - response* resp = (response*) *state; - will_return(mock_response, resp); - will_return(__wrap_kds_s2000w_client_get_option, 0); - expect_function_call(__wrap_kds_s2000w_client_get_option); handler* h = init_handler(); + h->current_scan_status->load_options = 1; + response* resp = (response*) *state; + h->current_scanner_config = json_tokener_parse(resp->data); char value[50] = {0}; _sane_kds_s2000w_net_control_option(h, 18, SANE_ACTION_GET_VALUE, &value, NULL); @@ -317,11 +303,10 @@ void sane_kds_s2000w_net_control_get_option_eightteen(void** state) void sane_kds_s2000w_net_control_get_option_nineteen(void** state) { - response* resp = (response*) *state; - will_return(mock_response, resp); - will_return(__wrap_kds_s2000w_client_get_option, 0); - expect_function_call(__wrap_kds_s2000w_client_get_option); handler* h = init_handler(); + h->current_scan_status->load_options = 1; + response* resp = (response*) *state; + h->current_scanner_config = json_tokener_parse(resp->data); int value = -1; _sane_kds_s2000w_net_control_option(h, 19, SANE_ACTION_GET_VALUE, &value, NULL); @@ -334,11 +319,10 @@ void sane_kds_s2000w_net_control_get_option_nineteen(void** state) void sane_kds_s2000w_net_control_get_option_twenty(void** state) { - response* resp = (response*) *state; - will_return(mock_response, resp); - will_return(__wrap_kds_s2000w_client_get_option, 0); - expect_function_call(__wrap_kds_s2000w_client_get_option); handler* h = init_handler(); + h->current_scan_status->load_options = 1; + response* resp = (response*) *state; + h->current_scanner_config = json_tokener_parse(resp->data); int value = -1; _sane_kds_s2000w_net_control_option(h, 20, SANE_ACTION_GET_VALUE, &value, NULL); -- 2.39.5