]> gitweb.hhaalo.de Git - sane-kds-s2000w-net.git/commitdiff
move curl call open session to client file
authorBastian Dehn <hhaalo@arcor.de>
Mon, 22 Jan 2024 19:04:29 +0000 (20:04 +0100)
committerBastian Dehn <hhaalo@arcor.de>
Mon, 22 Jan 2024 19:04:29 +0000 (20:04 +0100)
CMakeLists.txt
src/kds_s2000w_client.c [new file with mode: 0644]
src/kds_s2000w_client.h [new file with mode: 0644]
src/kds_s2000w_handler.c

index 5c67c7c064e4f44e294871c4b0404cf958591590..40bcd5284a56747ec690456d5ef9944a653a6bb0 100644 (file)
@@ -9,7 +9,8 @@ find_package(JSON-C REQUIRED)
 add_library("sane-kds_s2000w_net"
        SHARED
        "src/kds_s2000w_net.c"
-       "src/kds_s2000w_handler.c")
+       "src/kds_s2000w_handler.c"
+       "src/kds_s2000w_client.c")
 set_target_properties("sane-kds_s2000w_net" PROPERTIES VERSION 1)
 
 target_link_libraries("sane-kds_s2000w_net" json-c curl)
diff --git a/src/kds_s2000w_client.c b/src/kds_s2000w_client.c
new file mode 100644 (file)
index 0000000..d42c9f7
--- /dev/null
@@ -0,0 +1,69 @@
+#include <string.h>
+#include <stdlib.h>
+#include <curl/curl.h>
+#include "kds_s2000w_client.h"
+
+size_t write_callback(char *ptr, size_t size, size_t nmemb, void *userdata)
+{
+       response* data = (response*) userdata;
+       size_t fullsize = size * nmemb;
+
+       data->data = realloc(data->data, fullsize + 1);
+       if (!data->data)
+               return 0;
+
+       memcpy(data->data, ptr, fullsize);
+       data->size = fullsize + 1;
+
+       return fullsize;
+}
+
+response* response_init()
+{
+       response* resp;
+
+       resp = malloc(sizeof(response));
+       resp->data = malloc(sizeof(char));
+       resp->size = 0;
+       resp->code = 0L;
+
+       return resp;
+}
+
+void response_free(response* response)
+{
+       free(response->data);
+       response->data = NULL;
+       free(response);
+       response = NULL;
+}
+
+int open_session(const char* username, 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", 0);
+       char* url = NULL;
+       curl_url_get(url_handler, CURLUPART_URL, &url, 0);
+
+       char body[50] = {0};
+       sprintf(body, "{\"OCPUserName\": \"%s\"}", username);
+
+       curl_easy_setopt(curl, CURLOPT_URL, url);
+       curl_easy_setopt(curl, CURLOPT_POST, 1L);
+       curl_easy_setopt(curl, CURLOPT_POSTFIELDS, body);
+       curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
+       curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*) response);
+       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
diff --git a/src/kds_s2000w_client.h b/src/kds_s2000w_client.h
new file mode 100644 (file)
index 0000000..34905c9
--- /dev/null
@@ -0,0 +1,14 @@
+#ifndef KDS_S2000W_CLIENT_H
+typedef struct {
+       char* data;
+       size_t size;
+       long code;
+} response;
+
+// TODO: private method
+size_t write_callback(char *ptr, size_t size, size_t nmemb, void *userdata);
+
+response* response_init();
+void response_free(response* response);
+int open_session(const char* username, response* response);
+#endif
\ No newline at end of file
index fce2096f803d1e42ec63b89f696312bb44286fa5..8bf4bd537881eb694454ed4f8bd62fb83243faa7 100644 (file)
@@ -5,30 +5,10 @@
 #include <curl/curl.h>
 #include <json-c/json.h>
 #include "kds_s2000w_handler.h"
-
-typedef struct {
-       char* data;
-       size_t size;
-       long code;
-} response;
+#include "kds_s2000w_client.h"
 
 int64_t sessionid = 0;
 
-size_t write_callback(char *ptr, size_t size, size_t nmemb, void *userdata)
-{
-       response* data = (response*) userdata;
-       size_t fullsize = size * nmemb;
-
-       data->data = realloc(data->data, fullsize + 1);
-       if (!data->data)
-               return 0;
-
-       memcpy(data->data, ptr, fullsize);
-       data->size = fullsize + 1;
-
-       return fullsize;
-}
-
 void kds_s2000w_handler_get_option(int option, void* value)
 {
        CURL *curl = curl_easy_init();
@@ -83,53 +63,35 @@ void kds_s2000w_handler_set_option_auto(int option)
 
 device_state kds_s2000w_handler_open()
 {
-       device_state returnState = NOTCONNECTED;
+       device_state return_state = NOTCONNECTED;
        CURL *curl = curl_easy_init();
        if(!curl)
                return NOTCONNECTED;
 
-       response resp = {0};
-       CURLU* url_handler = curl_url();
-       curl_url_set(url_handler, CURLUPART_URL, "http://scanner.lan.hhaalo.de/api/session", 0);
-       char* url = NULL;
-       curl_url_get(url_handler, CURLUPART_URL, &url, 0);
+       response* resp = response_init();
+       int result = open_session("hhaalo", resp);
 
-       curl_easy_setopt(curl, CURLOPT_URL, url);
-       curl_easy_setopt(curl, CURLOPT_POST, 1L);
-       curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "{\"OCPUserName\":\"hhaalo\"}");
-       curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
-       curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*) &resp);
-       CURLcode result = curl_easy_perform(curl);
-       curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &resp.code);
-       
-       if (result != CURLE_OK || resp.code == 404)
-               returnState = NOTCONNECTED;
+       if (result != 0 || resp->code == 404)
+               return_state = NOTCONNECTED;
 
-       if (resp.code == 423)
-               returnState = BUSY;
+       if (resp->code == 423)
+               return_state = BUSY;
 
        json_object* resObj = NULL;
-       if (resp.code == 200) {
-               resObj = json_tokener_parse(resp.data);
+       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);
                valueObj = NULL;
-               returnState = OPENED;
+               return_state = OPENED;
        }
 
-       free(resp.data);
-       resp.data = NULL;
        json_object_put(resObj);
        resObj = NULL;
-       curl_easy_cleanup(curl);
-       curl = NULL;
-       curl_url_cleanup(url_handler);
-       url_handler = NULL;
-       curl_free(url);
-       url = NULL;
+       response_free(resp);
 
-       return returnState;
+       return return_state;
 }
 
 void kds_s2000w_handler_close()