#include "kds_s2000w_debug.h"
response* resp = NULL;
-current_state* state = NULL;
json_object* resp_config = NULL;
json_object* config = NULL;
blobdata image;
scan_status current_scan_status;
-void _get_current_metadata()
+void _get_current_metadata(void* handle)
{
debug_printf(ALL, "get_current_metadata");
+ handler* h = (handler*) handle;
resp = kds_s2000w_client_response_init();
- kds_s2000w_client_get_metadata(state->sessionid, current_scan_status.current_image_number, resp);
+ kds_s2000w_client_get_metadata(h->sessionid, current_scan_status.current_image_number, resp);
if (resp->code != 200) {
current_scan_status.mdata.valid = 0;
kds_s2000w_client_response_free(resp);
sleep(1);
}
-void _delete_current_image()
+void _delete_current_image(void* handle)
{
debug_printf(ALL, "delete_current_image");
+ handler* h = (handler*) handle;
resp = kds_s2000w_client_response_init();
- kds_s2000w_client_delete_image(state->sessionid, current_scan_status.current_image_number, resp);
+ kds_s2000w_client_delete_image(h->sessionid, current_scan_status.current_image_number, resp);
kds_s2000w_client_response_free(resp);
resp = NULL;
sleep(1);
}
-void _download_current_image()
+void _download_current_image(void* handle)
{
debug_printf(ALL, "download_current_image");
+ handler* h = (handler*) handle;
if (image.size > 0) {
free(image.data);
image.data = NULL;
image.size = 0;
}
resp = kds_s2000w_client_response_init();
- kds_s2000w_client_get_image(state->sessionid, current_scan_status.current_image_number, resp);
+ kds_s2000w_client_get_image(h->sessionid, current_scan_status.current_image_number, resp);
image.size = resp->size;
image.data = malloc(sizeof(char) * resp->size);
memcpy(image.data, resp->data, resp->size);
sleep(1);
}
-void _get_current_scan_status()
+void _get_current_scan_status(void* handle)
{
debug_printf(ALL, "get_current_scan_status");
+ handler* h = (handler*) handle;
json_object* status_resp_obj = NULL;
json_object* status_obj = NULL;
json_object* status_value_obj = NULL;
resp = kds_s2000w_client_response_init();
- kds_s2000w_client_status_session(state->sessionid, resp);
+ kds_s2000w_client_status_session(h->sessionid, resp);
status_resp_obj = json_tokener_parse(resp->data);
status_obj = json_object_object_get(status_resp_obj, "Status");
status_value_obj = json_object_object_get(status_obj, "NumImagesScanned");
sleep(1);
}
-current_state* kds_s2000w_handler_open(const char* devicename, void* handle)
+void kds_s2000w_handler_open(const char* devicename, void** handle)
{
debug_printf(ALL, "kds_s2000w_handler_open");
- if (strcmp(devicename, "kds_s2000w_net")) {
- handler* h = NULL;
- h = malloc(sizeof(handler));
- handle = h;
- }
+ if (strcmp(devicename, "kds_s2000w_net") != 0)
+ return;
+
+ handler* h = NULL;
+ h = malloc(sizeof(handler));
+ *handle = h;
init_gamma_table();
image.size = 0;
image.data = NULL;
pnm_image.size = 0;
pnm_image.data = NULL;
- state = malloc(sizeof(current_state));
- state->sessionid = 0;
- state->state = NOTCONNECTED;
+ h->sessionid = 0;
+ h->state = NOTCONNECTED;
resp = kds_s2000w_client_response_init();
int result = kds_s2000w_client_open_session("hhaalo", resp);
if (result != 0 || resp->code == 404) {
kds_s2000w_client_response_free(resp);
resp = NULL;
- return state;
+ return;
}
if (resp->code == 423) {
kds_s2000w_client_response_free(resp);
resp = NULL;
- state->state = BUSY;
+ h->state = BUSY;
}
if (resp->code == 200) {
resp_config = json_tokener_parse(resp->data);
json_object* value_object = NULL;
json_object_object_get_ex(resp_config, "SessionId", &value_object);
- state->sessionid = json_object_get_int64(value_object);
- state->state = OPENED;
+ h->sessionid = json_object_get_int64(value_object);
+ h->state = OPENED;
value_object = NULL;
}
current_scan_status.downloaded_images = 0;
current_scan_status.complete_scanned = 0;
current_scan_status.mdata.scanned_all_complete = 0;
- return state;
+ return;
}
void kds_s2000w_handler_close(void* handle)
{
debug_printf(ALL, "kds_s2000w_handler_close");
- if (state == NULL || state->sessionid == 0)
+ handler* h = (handler*) handle;
+ if (h == NULL)
return;
- kds_s2000w_client_close_session(state->sessionid);
- state->sessionid = 0;
- free(state);
- state = NULL;
+ kds_s2000w_client_close_session(h->sessionid);
+ h->sessionid = 0;
json_object_put(resp_config);
resp_config = NULL;
kds_s2000w_client_response_free(resp);
resp = NULL;
free(image.data);
image.data = NULL;
+ free(h);
+ h = NULL;
}
-current_state* kds_s2000w_handler_current_state()
-{
- return state;
-}
-
-
-void kds_s2000w_handler_start_scan()
+void kds_s2000w_handler_start_scan(void* handle)
{
debug_printf(ALL, "kds_s2000w_handler_start_scan");
+ handler* h = (handler*) handle;
resp = kds_s2000w_client_response_init();
- kds_s2000w_client_start_scan(state->sessionid, resp);
+ kds_s2000w_client_start_scan(h->sessionid, resp);
kds_s2000w_client_response_free(resp);
resp = NULL;
}
-void kds_s2000w_handler_stop_scan()
+void kds_s2000w_handler_stop_scan(void* handle)
{
debug_printf(ALL, "kds_s2000w_handler_stop_scan");
+ handler* h = (handler*) handle;
resp = kds_s2000w_client_response_init();
- kds_s2000w_client_stop_scan(state->sessionid, resp);
+ kds_s2000w_client_stop_scan(h->sessionid, resp);
kds_s2000w_client_response_free(resp);
resp = NULL;
}
-metadata kds_s2000w_handler_get_parameters()
+metadata kds_s2000w_handler_get_parameters(void* handle)
{
debug_printf(ALL, "kds_s2000w_handler_get_parameters");
+ handler* h = (handler*) handle;
if (!current_scan_status.complete_scanned
&& current_scan_status.downloaded_images == current_scan_status.available_images) {
- _get_current_scan_status();
+ _get_current_scan_status(h);
}
if (current_scan_status.complete_scanned
return current_scan_status.mdata;
}
- _download_current_image();
- _get_current_metadata();
- _delete_current_image();
+ _download_current_image(h);
+ _get_current_metadata(h);
+ _delete_current_image(h);
current_scan_status.current_image_number++;
return current_scan_status.mdata;
BUSY
} device_state;
-typedef struct {
- int64_t sessionid;
- device_state state;
-} current_state;
-
enum {
RELOAD_OPTIONS = 2,
REALOD_PARAMS = 4
typedef struct {
long sessionid;
- metadata current_metadata;
+ device_state state;
} handler;
-current_state* kds_s2000w_handler_open(const char* devicename, void* handle);
+void kds_s2000w_handler_open(const char* devicename, void** handle);
void kds_s2000w_handler_close(void* handle);
-current_state* kds_s2000w_handler_current_state();
-void kds_s2000w_handler_get_option(int option, void* value);
-void kds_s2000w_handler_set_option(int option, void* value, int* info);
+void kds_s2000w_handler_get_option(void* handle, int option, void* value);
+void kds_s2000w_handler_set_option(void* handle, int option, void* value, int* info);
void kds_s2000w_handler_set_option_auto(int option);
-void kds_s2000w_handler_start_scan();
-void kds_s2000w_handler_stop_scan();
-metadata kds_s2000w_handler_get_parameters();
+void kds_s2000w_handler_start_scan(void* handle);
+void kds_s2000w_handler_stop_scan(void* handle);
+metadata kds_s2000w_handler_get_parameters(void* handle);
#endif
\ No newline at end of file
#include "kds_s2000w_client.h"
#include "kds_s2000w_handler_opts.h"
-extern current_state* state;
extern response* resp;
extern json_object* resp_config;
extern json_object* config;
json_object_set_int(value_object, *int_value_ptr);
}
-void _load_options()
+void _load_options(void* handle)
{
+ handler* h = (handler*) handle;
json_object_put(resp_config);
resp_config = NULL;
resp = kds_s2000w_client_response_init();
- int result = kds_s2000w_client_get_option(state->sessionid, resp);
+ int result = kds_s2000w_client_get_option(h->sessionid, resp);
resp_config = json_tokener_parse(resp->data);
config = json_object_object_get(resp_config, "Configuration");
}
}
-void kds_s2000w_handler_get_option(int option, void* value)
+void kds_s2000w_handler_get_option(void* handle, int option, void* value)
{
config = json_object_object_get(resp_config, "Configuration");
+ handler* h = (handler*) handle;
json_object* value_object = NULL;
switch(option) {
case 0:
const int COUNT_CUSTOM_OPTIONS = 7;
- _load_options();
+ _load_options(h);
int* int_value_ptr = (int*) value;
*int_value_ptr = json_object_object_length(config) + COUNT_CUSTOM_OPTIONS;
break;
}
}
-void kds_s2000w_handler_set_option(int option, void* value, int* info)
+void kds_s2000w_handler_set_option(void* handle, int option, void* value, int* info)
{
config = json_object_object_get(resp_config, "Configuration");
+ handler* h = (handler*) handle;
json_object* value_object = NULL;
switch(option) {
case 2:
resp->code = 0;
resp->data = realloc(resp->data, resp->size);
resp->data = memcpy(resp->data, json_string, resp->size);
- kds_s2000w_client_set_option(state->sessionid, resp);
+ kds_s2000w_client_set_option(h->sessionid, resp);
kds_s2000w_client_response_free(resp);
resp = NULL;
json_object_put(capabilities);
capabilities = NULL;
*info = RELOAD_OPTIONS;
- _load_options();
+ _load_options(h);
return;
break;
default:
resp->code = 0;
resp->data = realloc(resp->data, resp->size);
resp->data = memcpy(resp->data, json_string, resp->size);
- kds_s2000w_client_set_option(state->sessionid, resp);
+ kds_s2000w_client_set_option(h->sessionid, resp);
kds_s2000w_client_response_free(resp);
resp = NULL;
}
#ifndef KDS_S2000W_HANDLER_OPTS_H
#define KDS_S2000W_HANDLER_OPTS_H
void init_gamma_table();
-void kds_s2000w_handler_get_option(int option, void* value);
-void kds_s2000w_handler_set_option(int option, void* value, int* info);
+void kds_s2000w_handler_get_option(void* handle, int option, void* value);
+void kds_s2000w_handler_set_option(void* handle, int option, void* value, int* info);
void kds_s2000w_handler_set_option_auto(int option);
#endif
\ No newline at end of file
}
SANE_Status _sane_kds_s2000w_net_open(SANE_String_Const devicename,
- SANE_Handle handle)
+ SANE_Handle* handle)
{
debug_printf(ALL, "sane_kds_s2000w_net_open");
if (strcmp(devicename, "kds_s2000w_net") != 0)
return SANE_STATUS_INVAL;
- current_state* state = kds_s2000w_handler_open(devicename, handle);
- if (state->state == BUSY)
+ kds_s2000w_handler_open(devicename, handle);
+ handler* h = (handler*) *handle;
+ if (h->state == BUSY)
return SANE_STATUS_DEVICE_BUSY;
- if (state->state == NOTCONNECTED)
+ if (h->state == NOTCONNECTED)
return SANE_STATUS_IO_ERROR;
- debug_printf_long(INFO, "SessionId", state->sessionid);
+ debug_printf_long(INFO, "SessionId", h->sessionid);
return SANE_STATUS_GOOD;
}
{
debug_printf(ALL, "sane_kds_s2000w_net_control_option");
if (action == SANE_ACTION_GET_VALUE)
- kds_s2000w_handler_get_option(option, value);
+ kds_s2000w_handler_get_option(handle, option, value);
if (action == SANE_ACTION_SET_VALUE)
- kds_s2000w_handler_set_option(option, value, info);
+ kds_s2000w_handler_set_option(handle, option, value, info);
if (action == SANE_ACTION_SET_AUTO)
kds_s2000w_handler_set_option_auto(option);
for (int i = 0; i < 10; i++) {
sleep(1);
- read_info.current_metadata = kds_s2000w_handler_get_parameters();
+ read_info.current_metadata = kds_s2000w_handler_get_parameters(handle);
if (read_info.current_metadata.valid)
break;
}
debug_printf(ALL, "sane_kds_s2000w_net_start");
read_info.cancel = 0;
read_info.scan_started = 1;
- kds_s2000w_handler_start_scan();
+ kds_s2000w_handler_start_scan(handle);
return SANE_STATUS_GOOD;
}
if (read_info.cancel) {
read_info.scan_started = 0;
*length = 0;
- kds_s2000w_handler_stop_scan();
+ kds_s2000w_handler_stop_scan(handle);
sleep(1);
kds_s2000w_handler_close(handle);
sleep(1);
#define kds_s2000w_handler_get_parameters mock_get_params
#define kds_s2000w_handler_stop_scan void_mock
#define kds_s2000w_handler_close void_mock
-#define kds_s2000w_handler_open state_mock
+#define kds_s2000w_handler_open void_mock
uint no_sleep(uint secondes) {}
metadata mock_get_params() {
return mdata;
}
-current_state* state_mock() { return NULL; }
void void_mock() {}
#include "../src/kds_s2000w_net.c"
#undef kds_s2000w_handler_stop_scan