From: Bastian Dehn Date: Sun, 21 Jan 2024 11:46:19 +0000 (+0100) Subject: add close session with session id X-Git-Tag: v1.0.0^2~499 X-Git-Url: https://gitweb.hhaalo.de/?a=commitdiff_plain;h=fb9a218ae7325ec2c2dd12f938952d7c391f0e13;p=sane-kds-s2000w-net.git add close session with session id --- diff --git a/src/kds_s2000w_handler.c b/src/kds_s2000w_handler.c index 4d5959b..2b4a18d 100644 --- a/src/kds_s2000w_handler.c +++ b/src/kds_s2000w_handler.c @@ -11,6 +11,8 @@ typedef struct { size_t size; } response; +int64_t sessionid = 0; + size_t write_callback(char *ptr, size_t size, size_t nmemb, void *userdata) { response* data = (response*) userdata; @@ -26,35 +28,47 @@ size_t write_callback(char *ptr, size_t size, size_t nmemb, void *userdata) return fullsize; } -void* kds_s2000w_handler_open() +void kds_s2000w_handler_open() { - printf("open\n"); CURL *curl = curl_easy_init(); - if(curl) { - printf("curl\n"); - CURLcode res; - response resp = {0}; - printf("open data %x\n", &resp); - curl_easy_setopt(curl, CURLOPT_URL, "http://scanner.lan.hhaalo.de/api/session"); - 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); - res = curl_easy_perform(curl); - - json_object* resObj = json_tokener_parse(resp.data); - json_object* valueObj = NULL; - json_object_object_get_ex(resObj, "SessionId", &valueObj); - int64_t sessionid = json_object_get_int64(valueObj); - - printf("sessionid: %li\n", sessionid); - printf("data address open: %x\n", resp.data); - printf("data: %s\n", resp.data); - printf("curlcode: %li\n", res); - free(resp.data); - resp.data = NULL; - json_object_put(resObj); - curl_easy_cleanup(curl); - } - return NULL; + if(!curl) + return; + + response resp = {0}; + curl_easy_setopt(curl, CURLOPT_URL, "http://scanner.lan.hhaalo.de/api/session"); + 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); + curl_easy_perform(curl); + + json_object* resObj = json_tokener_parse(resp.data); + json_object* valueObj = NULL; + json_object_object_get_ex(resObj, "SessionId", &valueObj); + sessionid = json_object_get_int64(valueObj); + + free(resp.data); + resp.data = NULL; + json_object_put(resObj); + curl_easy_cleanup(curl); +} + +void kds_s2000w_handler_close() +{ + CURL *curl = curl_easy_init(); + if(!curl) + return; + + + 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_URL, "http://scanner.lan.hhaalo.de/api/session"); + curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "DELETE"); + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); + curl_easy_perform(curl); + + curl_slist_free_all(headers); + curl_easy_cleanup(curl); } \ No newline at end of file diff --git a/src/kds_s2000w_handler.h b/src/kds_s2000w_handler.h index a12477c..33c8b5c 100644 --- a/src/kds_s2000w_handler.h +++ b/src/kds_s2000w_handler.h @@ -1,3 +1,4 @@ #ifndef KDS_S2000W_HANDLER_H -void* kds_s2000w_handler_open(); +void kds_s2000w_handler_open(); +void kds_s2000w_handler_close(); #endif \ No newline at end of file diff --git a/src/kds_s2000w_net.c b/src/kds_s2000w_net.c index 224c079..34a89e1 100644 --- a/src/kds_s2000w_net.c +++ b/src/kds_s2000w_net.c @@ -13,6 +13,7 @@ SANE_Status _sane_kds_s2000w_net_init(SANE_Int* version_code, SANE_Auth_Callback void _sane_kds_s2000w_net_exit(void) { printf("kds_s2000w_net exit\n"); + kds_s2000w_handler_close(); } SANE_Status _sane_kds_s2000w_net_get_devices(SANE_Device*** device_list,