]> gitweb.hhaalo.de Git - sane-kds-s2000w-net.git/commitdiff
refactor open handler
authorBastian Dehn <hhaalo@arcor.de>
Sat, 4 Jan 2025 10:25:21 +0000 (11:25 +0100)
committerBastian Dehn <hhaalo@arcor.de>
Sat, 4 Jan 2025 10:27:08 +0000 (11:27 +0100)
src/kds_s2000w_handler.c
src/kds_s2000w_net.c
tests/kds_s2000w_net_tests.c

index ad6fe6f316cf39d660dc846ce506ee2e3cf62fb0..f6cf7ecd827920ee56bc2bbc964005f479d9331a 100644 (file)
@@ -283,40 +283,40 @@ void kds_s2000w_handler_open(const char* devicename, void** handle)
        kds_s2000w_client_init();
 
        response* resp = kds_s2000w_client_response_init();
-       int result = kds_s2000w_client_open_session(resp);
-
-       if (resp->code == 408) {
-               kds_s2000w_client_response_free(resp);
-               resp = NULL;
-               sleep(1);
-               resp = kds_s2000w_client_response_init();
+       int result = 0;
+       for (int i = 0; i < 30; i++) {
                result = kds_s2000w_client_open_session(resp);
-       }
 
-       if (result != 0 || resp->code == 404) {
+               if (result != 0) {
+                       h->state = NOTCONNECTED;
+                       break;
+               }
+
+               if (resp->code == 200) {
+                       h->state = OPENED;
+                       break;
+               }
+
+               if (resp->code == 423) {
+                       h->state = BUSY;
+                       break;
+               }
+
                kds_s2000w_client_response_free(resp);
                resp = NULL;
-               return;
+               sleep(1);
        }
 
-       if (resp->code == 423)
-               h->state = BUSY;
-
-       if (resp->code != 200) {
+       if (result != 0 || resp->code != 200) {
                kds_s2000w_client_response_free(resp);
                resp = NULL;
-
                return;
        }
 
-       h->image->size = 0;
-       h->image->data = NULL;
-
        h->current_scanner_config = json_tokener_parse(resp->data);
        json_object* value_object = NULL;
        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;
 
index 00ad2fb6bdd83548dc2c5fcf04f1f0f88507bfbe..dd57a81e976b9447e0daa9e36675d18bccca9c5d 100644 (file)
@@ -52,15 +52,16 @@ SANE_Status _sane_kds_s2000w_net_open(SANE_String_Const devicename,
        if (strcmp(devicename, "kds_s2000w_net") != 0)
                return SANE_STATUS_INVAL;
 
-       kds_s2000w_option_descriptor_init_option_descriptors();
        kds_s2000w_handler_open(devicename, handle);
        handler* h = (handler*) *handle;
-       if (h->state == BUSY)
-               return SANE_STATUS_DEVICE_BUSY;
 
        if (h->state == NOTCONNECTED)
                return SANE_STATUS_IO_ERROR;
 
+       if (h->state == BUSY)
+               return SANE_STATUS_DEVICE_BUSY;
+
+       kds_s2000w_option_descriptor_init_option_descriptors();
        return SANE_STATUS_GOOD;
 }
 
index d1620bb8c7f487ea1bc6c251f9e54354850dda30..e099c855d15f097f230d9335a51c133e7c6ba947 100644 (file)
@@ -576,7 +576,6 @@ void sane_kds_s2000w_net_open()
        assert_int_equal(h->sessionid, 1251877821);
        assert_int_equal(status, SANE_STATUS_GOOD);
 
-       kds_s2000w_option_descriptor_free_option_descriptors();
        free_handler(h);
        h = NULL;
        kds_s2000w_client_response_free(resp);
@@ -601,7 +600,6 @@ void sane_kds_s2000w_net_open_busy_device()
        assert_int_equal(h->state, BUSY);
        assert_int_equal(status, SANE_STATUS_DEVICE_BUSY);
 
-       kds_s2000w_option_descriptor_free_option_descriptors();
        free_handler(h);
        h = NULL;
        kds_s2000w_client_response_free(resp);
@@ -627,7 +625,7 @@ void sane_kds_s2000w_net_open_not_connected()
        response* resp = kds_s2000w_client_response_init();
        resp->code = 0;
        will_return(mock_response, resp);
-       will_return(__wrap_kds_s2000w_client_open_session, 0);
+       will_return(__wrap_kds_s2000w_client_open_session, 1);
        expect_function_call(__wrap_kds_s2000w_client_open_session);
        void** hlist = malloc(sizeof(void*));