--- /dev/null
+#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
#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();
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()