]> gitweb.hhaalo.de Git - sane-kds-s2000w-net.git/commitdiff
add current state struct
authorBastian Dehn <hhaalo@arcor.de>
Wed, 24 Jan 2024 19:23:33 +0000 (20:23 +0100)
committerBastian Dehn <hhaalo@arcor.de>
Wed, 24 Jan 2024 19:24:03 +0000 (20:24 +0100)
src/kds_s2000w_handler.c
src/kds_s2000w_handler.h
src/kds_s2000w_net.c

index bd69b8ed43cc219fd0805a9863f78988f0542788..c9636307bc6458da2df7ba9e5d2cb832a597e83b 100644 (file)
@@ -7,8 +7,8 @@
 #include "kds_s2000w_handler.h"
 #include "kds_s2000w_client.h"
 
-int64_t _sessionid = 0;
 json_object* _all_options = NULL;
+current_state* state;
 
 void _get_all_options()
 {
@@ -23,52 +23,58 @@ void _get_all_options()
        resp = NULL;
 }
 
-device_state kds_s2000w_handler_open()
+current_state* kds_s2000w_handler_open()
 {
-       device_state return_state = NOTCONNECTED;
+       state = malloc(sizeof(current_state));
+       state->sessionid = 0;
+       state->state = NOTCONNECTED;
+
+       state->state = NOTCONNECTED;
        CURL *curl = curl_easy_init();
        if(!curl)
-               return NOTCONNECTED;
+               return state;
 
        response* resp = kds_s2000w_client_response_init();
        int result = kds_s2000w_client_open_session("hhaalo", resp);
 
        if (result != 0 || resp->code == 404)
-               return_state = NOTCONNECTED;
+               return state;
 
        if (resp->code == 423)
-               return_state = BUSY;
+               state->state = BUSY;
 
        json_object* resObj = NULL;
        if (resp->code == 200) {
                resObj = json_tokener_parse(resp->data);
                json_object* valueObj = NULL;
                json_object_object_get_ex(resObj, "SessionId", &valueObj);
-               _sessionid = json_object_get_int64(valueObj);
+               state->sessionid = json_object_get_int64(valueObj);
                valueObj = NULL;
-               return_state = OPENED;
+               state->state = OPENED;
        }
 
        json_object_put(resObj);
        resObj = NULL;
        kds_s2000w_client_response_free(resp);
 
-       return return_state;
+       return state;
 }
 
 void kds_s2000w_handler_close()
 {
-       if (_sessionid == 0)
+       if (state == NULL || state->sessionid == 0)
                return;
 
        CURL *curl = curl_easy_init();
        if(!curl)
                return;
 
-       kds_s2000w_client_close_session(_sessionid);
-       _sessionid = 0;
+       kds_s2000w_client_close_session(state->sessionid);
+       state->sessionid = 0;
        json_object_put(_all_options);
        _all_options = NULL;
+       free(state);
+       state = NULL;
 }
 
 void kds_s2000w_handler_get_option(int option, void* value)
index bed32eaeb4a2d376cabff02d904751909057e710..292f49d5a03d6f017751493e0dc928f6e8692be2 100644 (file)
@@ -5,7 +5,12 @@ typedef enum {
        BUSY
 } device_state;
 
-device_state kds_s2000w_handler_open();
+typedef struct {
+       int64_t sessionid;
+       device_state state;
+} current_state;
+
+current_state* kds_s2000w_handler_open();
 void kds_s2000w_handler_close();
 void kds_s2000w_handler_get_option(int option, void* value);
 void kds_s2000w_handler_set_option(int option, void* value);
index 002c0670c0ec94d03a5f64c554a1969ec1ca3998..c275de87a5f3007d57c496f49429ade067727740 100644 (file)
@@ -53,11 +53,11 @@ SANE_Status _sane_kds_s2000w_net_open(SANE_String_Const devicename,
        if (strcmp(devicename, "kds_s2000w_net") != 0)
                return SANE_STATUS_INVAL;
 
-       device_state state = kds_s2000w_handler_open();
-       if (state == BUSY)
+       current_state* state = kds_s2000w_handler_open();
+       if (state->state == BUSY)
                return SANE_STATUS_DEVICE_BUSY;
 
-       if (state == NOTCONNECTED)
+       if (state->state == NOTCONNECTED)
                return SANE_STATUS_IO_ERROR;
 
        return SANE_STATUS_GOOD;