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;
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