]> gitweb.hhaalo.de Git - sane-kds-s2000w-net.git/commitdiff
send response for set option
authorBastian Dehn <hhaalo@arcor.de>
Sun, 28 Jan 2024 11:46:59 +0000 (12:46 +0100)
committerBastian Dehn <hhaalo@arcor.de>
Sun, 28 Jan 2024 11:46:59 +0000 (12:46 +0100)
src/kds_s2000w_client.c
src/kds_s2000w_client.h
src/kds_s2000w_handler.c

index 304b0dfa29a277ad0fe1f8be28da60fd622fe43e..07d21a1b2340c594a2f1f2cac97f797aa46286dc 100644 (file)
@@ -124,5 +124,36 @@ int kds_s2000w_client_get_option(int64_t sessionid, response* response)
        curl_free(url);
        url = NULL;
 
+       return result;
+}
+
+int kds_s2000w_client_set_option(int64_t sessionid, response* response)
+{
+       CURL *curl = curl_easy_init();
+       if(!curl)
+               return CURLE_FAILED_INIT;
+
+       CURLU* url_handler = curl_url();
+       curl_url_set(url_handler, CURLUPART_URL, "http://scanner.lan.hhaalo.de/api/session/configuration", 0);
+       char* url = NULL;
+       curl_url_get(url_handler, CURLUPART_URL, &url, 0);
+
+       struct curl_slist* headers = NULL;
+       char header_str[80];
+       sprintf(header_str, "SessionId: %li", sessionid);
+       headers = curl_slist_append(headers, header_str);
+       curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
+       curl_easy_setopt(curl, CURLOPT_URL, url);
+       curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "PUT");
+       curl_easy_setopt(curl, CURLOPT_POSTFIELDS, response->data);
+       curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, response->size);
+       CURLcode result = curl_easy_perform(curl);
+       curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response->code);
+
+       curl_url_cleanup(url_handler);
+       url_handler = NULL;
+       curl_free(url);
+       url = NULL;
+
        return result;
 }
\ No newline at end of file
index ca0b123f73ecf76f476e0cafde1574a994ad40f9..8c0e5bf1c6d4aa626af2da9f35dd744f0438e6a2 100644 (file)
@@ -10,4 +10,5 @@ void kds_s2000w_client_response_free(response* response);
 int kds_s2000w_client_open_session(const char* username, response* response);
 void kds_s2000w_client_close_session(int64_t sessionid);
 int kds_s2000w_client_get_option(int64_t sessionid, response* response);
+int kds_s2000w_client_set_option(int64_t sessionid, response* response);
 #endif
\ No newline at end of file
index 464128e8a1a2a2eecf4bd9e0db03e3bf89c7b57c..47293fafe6e849a3c2c1f2eee93d4868d31a1668 100644 (file)
@@ -7,6 +7,7 @@
 #include "kds_s2000w_handler.h"
 #include "kds_s2000w_client.h"
 
+response* resp = NULL;
 current_state* state = NULL;
 json_object* resObj = NULL;
 json_object* config = NULL;
@@ -48,7 +49,7 @@ current_state* kds_s2000w_handler_open()
        if(!curl)
                return state;
 
-       response* resp = kds_s2000w_client_response_init();
+       resp = kds_s2000w_client_response_init();
        int result = kds_s2000w_client_open_session("hhaalo", resp);
 
        if (result != 0 || resp->code == 404)
@@ -69,8 +70,8 @@ current_state* kds_s2000w_handler_open()
 
        json_object_put(resObj);
        resObj = NULL;
-       kds_s2000w_client_response_free(resp);
 
+       printf("sessionid: %li\n", state->sessionid);
        return state;
 }
 
@@ -85,6 +86,7 @@ void kds_s2000w_handler_close()
        state = NULL;
        json_object_put(resObj);
        resObj = NULL;
+       kds_s2000w_client_response_free(resp);
 }
 
 current_state* kds_s2000w_handler_current_state()
@@ -97,7 +99,7 @@ void kds_s2000w_handler_get_option(int option, void* value)
        json_object* value_object = NULL;
        switch(option) {
                case 0:
-                       response* resp = kds_s2000w_client_response_init();
+                       resp = kds_s2000w_client_response_init();
                        int result = kds_s2000w_client_get_option(state->sessionid, resp);
                        resObj = json_tokener_parse(resp->data);
                        config = json_object_object_get(resObj, "Configuration");
@@ -110,9 +112,6 @@ void kds_s2000w_handler_get_option(int option, void* value)
                                return;
                        }
 
-                       kds_s2000w_client_response_free(resp);
-                       resp = NULL;
-
                        int* int_value_ptr = (int*) value;
                        *int_value_ptr = json_object_object_length(config) + 1;
                        break;
@@ -305,6 +304,12 @@ void kds_s2000w_handler_set_option(int option, void* value)
                default:
                        break;
        }
+
+       const char* json_string = json_object_to_json_string_ext(resObj, JSON_C_TO_STRING_PLAIN);
+       resp->data = (char*) json_string;
+       resp->size = sizeof(char) * strlen(json_string);
+       resp->code = 0;
+       kds_s2000w_client_set_option(state->sessionid, resp);
 }
 
 void kds_s2000w_handler_set_option_auto(int option)