sane access utilities by huagao_wrapper

This commit is contained in:
gb 2023-07-11 17:44:33 +08:00
parent fbff9f073f
commit 30f5bdfdbc
3 changed files with 99 additions and 34 deletions

View File

@ -346,9 +346,9 @@ extern "C"
{
return level >= utils::get_log_level() && utils::get_log_type() != LOG_TYPE_NONE;
}
void hg_scanner_log(const char* info)
void hg_scanner_log(const char* info, int level)
{
utils::log_info(info, LOG_LEVEL_DEBUG);
utils::log_info(info, level);
}
const char* hg_scanner_image_statu_name(int img_statu)
@ -365,6 +365,27 @@ extern "C"
return g_unk_statu;
}
int hg_get_full_path_of_module(char* buf, size_t* size, const char* part_name)
{
if(!size)
return EINVAL;
std::string fullp(utils::get_module_full_path(part_name));
if(fullp.empty())
return ENOENT;
if(*size <= fullp.length())
{
*size = fullp.length() + 4;
return ENOMEM;
}
strcpy(buf, fullp.c_str());
*size = fullp.length();
return 0;
}
}

View File

@ -256,6 +256,36 @@ namespace local_utility
}
template<typename ... Args>
void to_log(int level, const char* fmt, Args ... args)
{
size_t size = snprintf(nullptr, 0, fmt, args ...) + 2;
std::unique_ptr<char[]> buf(new char[size]);
snprintf(buf.get(), size, fmt, args ...);
hg_scanner_log(buf.get(), level);
}
std::string get_module_full_path(const char* name)
{
char buf[260] = {0};
size_t size = _countof(buf) - 1;
int err = hg_get_full_path_of_module(buf, &size, name);
std::string ret("");
if(err == ENOMEM)
{
char* mem = new char[size + 4];
err = hg_get_full_path_of_module(mem, &size, name);
if(err == 0)
ret = mem;
delete[] mem;
}
else if(err == 0)
ret = buf;
return std::move(ret);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// fixed id map
@ -454,7 +484,7 @@ namespace local_utility
SANE_Handle h = hg_sane_middleware::scanner_handle_to_sane(dev);
// utils::to_log(LOG_LEVEL_ALL, "sane callback invoked of event %s\n", sane_event((SANE_Event)code).c_str());
// local_utility::to_log(LOG_LEVEL_ALL, "sane callback invoked of event %s\n", sane_event((SANE_Event)code).c_str());
if (cb_ui)
{
@ -542,7 +572,7 @@ hg_sane_middleware::hg_sane_middleware(void) : opt_0_(nullptr), init_ok_(false)
char path[512] = { 0 };
size_t pos = 0;
g_sane_path = utils::get_module_full_path((std::string(GET_BACKEND_NAME) + ".so").c_str());
g_sane_path = local_utility::get_module_full_path((std::string(GET_BACKEND_NAME) + ".so").c_str());
pos = g_sane_path.rfind('/');
if (pos++ != std::string::npos)
g_sane_path.erase(pos);
@ -616,7 +646,7 @@ const SANE_Device** hg_sane_middleware::to_sane_device(ScannerInfo* hgscanner, i
COPY_DEVICE_MEMBER(type);
}
//utils::to_log(LOG_LEVEL_ALL, "Memory usage: %u / %u\n", val - (char*)ret, total);
//local_utility::to_log(LOG_LEVEL_ALL, "Memory usage: %u / %u\n", val - (char*)ret, total);
return (const SANE_Device**)ret;
}
@ -631,7 +661,7 @@ void hg_sane_middleware::free_sane_device(SANE_Device** dev)
}
void hg_sane_middleware::device_pnp(int sig)
{
utils::to_log(LOG_LEVEL_DEBUG, "Device list changed (%d)...", sig);
local_utility::to_log(LOG_LEVEL_DEBUG, "Device list changed (%d)...", sig);
}
SANE_Fixed hg_sane_middleware::double_2_sane_fixed(double v)
{
@ -848,7 +878,7 @@ SANE_Option_Descriptor* hg_sane_middleware::number_option_to_SANE_descriptor(con
for (size_t i = 0; i < values.size(); ++i)
val[i] = values[i];
}
//utils::to_log(LOG_LEVEL_ALL, "Memory usage: %u/%u\n", str - (char*)sod, bytes);
//local_utility::to_log(LOG_LEVEL_ALL, "Memory usage: %u/%u\n", str - (char*)sod, bytes);
return sod;
}
@ -1080,7 +1110,7 @@ SANE_Option_Descriptor* hg_sane_middleware::from_json(scanner_handle h, const st
lower = l;
upper = u;
step = s;
utils::to_log(LOG_LEVEL_DEBUG, "%s range: [%d, +%d, %d]\n", name.c_str(), l, s, u);
local_utility::to_log(LOG_LEVEL_DEBUG, "%s range: [%d, +%d, %d]\n", name.c_str(), l, s, u);
ret = hg_sane_middleware::number_option_to_SANE_descriptor(name.c_str(), title.c_str(), desc.c_str()
, false, &lower, &upper, &step);
}
@ -1106,7 +1136,7 @@ SANE_Option_Descriptor* hg_sane_middleware::from_json(scanner_handle h, const st
range->get_value("max", upper);
step = (upper - lower) / 10.0f;
range->get_value("step", step);
utils::to_log(LOG_LEVEL_DEBUG, "%s range: (%f, +%f, %f)\n", name.c_str(), lower, step, upper);
local_utility::to_log(LOG_LEVEL_DEBUG, "%s range: (%f, +%f, %f)\n", name.c_str(), lower, step, upper);
ret = hg_sane_middleware::number_option_to_SANE_descriptor(name.c_str(), title.c_str(), desc.c_str()
, true, &lower, &upper, &step);
}
@ -1483,7 +1513,7 @@ bool hg_sane_middleware::get_current_value(scanner_handle handle, const void* op
if (setv == &hg_sane_middleware::set_value_to_new)
value = *(void**)value;
utils::to_log(LOG_LEVEL_ALL, "<--Get option(%d - %s) value: %s\n", option, val.c_str(), hg_sane_middleware::option_value_2_string(t, value).c_str());
local_utility::to_log(LOG_LEVEL_ALL, "<--Get option(%d - %s) value: %s\n", option, val.c_str(), hg_sane_middleware::option_value_2_string(t, value).c_str());
}
delete jsn;
@ -1511,7 +1541,7 @@ void* hg_sane_middleware::get_default_value(scanner_handle handle, const void* o
*bytes = sizeof(SANE_Bool);
if (log)
{
utils::to_log(LOG_LEVEL_DEBUG, "option %d(%s) default value is: %s\n", option, title.c_str(), v ? "true" : "false");
local_utility::to_log(LOG_LEVEL_DEBUG, "option %d(%s) default value is: %s\n", option, title.c_str(), v ? "true" : "false");
}
}
else if (val == "int")
@ -1525,7 +1555,7 @@ void* hg_sane_middleware::get_default_value(scanner_handle handle, const void* o
*bytes = sizeof(v);
if (log)
{
utils::to_log(LOG_LEVEL_DEBUG, "option %d(%s) default value is: %d\n", option, title.c_str(), v);
local_utility::to_log(LOG_LEVEL_DEBUG, "option %d(%s) default value is: %d\n", option, title.c_str(), v);
}
}
else if (val == "float")
@ -1541,7 +1571,7 @@ void* hg_sane_middleware::get_default_value(scanner_handle handle, const void* o
memcpy(data, &sd, sizeof(sd));
if (log)
{
utils::to_log(LOG_LEVEL_DEBUG, "option %d(%s) default value is: %f\n", option, title.c_str(), v);
local_utility::to_log(LOG_LEVEL_DEBUG, "option %d(%s) default value is: %f\n", option, title.c_str(), v);
}
}
else if (val == "string")
@ -1559,12 +1589,12 @@ void* hg_sane_middleware::get_default_value(scanner_handle handle, const void* o
*bytes = val.length() + 1;
if (log)
{
utils::to_log(LOG_LEVEL_DEBUG, "option %d(%s) default value is: %s\n", option, title.c_str(), (char*)data);
local_utility::to_log(LOG_LEVEL_DEBUG, "option %d(%s) default value is: %s\n", option, title.c_str(), (char*)data);
}
}
else
{
utils::to_log(LOG_LEVEL_DEBUG, "option %d(%s) is '%s' and no value action.\n", option, title.c_str(), val.c_str());
local_utility::to_log(LOG_LEVEL_DEBUG, "option %d(%s) is '%s' and no value action.\n", option, title.c_str(), val.c_str());
}
}
delete jsn;
@ -1724,7 +1754,7 @@ SANE_Option_Descriptor* hg_sane_middleware::get_option_descriptor(SANE_Handle h,
opt_0_->type = SANE_TYPE_INT;
opt_0_->size = sizeof(SANE_TYPE_INT);
}
utils::log_info("get_option_descriptor(0)\n", LOG_LEVEL_DEBUG);
local_utility::to_log(LOG_LEVEL_DEBUG, "get_option_descriptor(0)\n");
return opt_0_;
}
@ -1791,7 +1821,7 @@ SANE_Status hg_sane_middleware::set_option(SANE_Handle h, const void* option, SA
hg_scanner_get_parameter(handle, (const char*)option, NULL, &count);
*((SANE_Int*)value) = count;
ret = SANE_STATUS_GOOD;
utils::to_log(LOG_LEVEL_WARNING, "get option count = %d.\n", count);
local_utility::to_log(LOG_LEVEL_WARNING, "get option count = %d.\n", count);
}
else
{
@ -1861,7 +1891,7 @@ SANE_Status hg_sane_middleware::set_option(SANE_Handle h, const void* option, SA
if (action == SANE_ACTION_SET_AUTO && desc && desc->type != SANE_TYPE_BUTTON && desc->type != SANE_TYPE_GROUP) // we assume the driver can set the option properbly, and no work to do
{
utils::to_log(LOG_LEVEL_WARNING, "Option %d(%s) call SANE_ACTION_SET_AUTO, we set default value.\n", option, desc->title);
local_utility::to_log(LOG_LEVEL_WARNING, "Option %d(%s) call SANE_ACTION_SET_AUTO, we set default value.\n", option, desc->title);
int len = 0;
void* val = get_default_value(handle, option, &len);
@ -1886,7 +1916,7 @@ SANE_Status hg_sane_middleware::set_option(SANE_Handle h, const void* option, SA
SANE_Option_Descriptor* known = dev->std_opt->get_option(id);
unsigned char* cont = (unsigned char*)value;
prev = hg_sane_middleware::option_value_2_string(known->type, value);
utils::to_log(LOG_LEVEL_DEBUG, "$First 4-bytes of origin value for option %d is: %02X%02X%02X%02X\n", option, cont[0], cont[1], cont[2], cont[3]);
local_utility::to_log(LOG_LEVEL_DEBUG, "$First 4-bytes of origin value for option %d is: %02X%02X%02X%02X\n", option, cont[0], cont[1], cont[2], cont[3]);
err = dev->std_opt->set_value(handle, id, value);
v = hg_sane_middleware::option_value_2_string(known->type, value);
}
@ -1903,14 +1933,14 @@ SANE_Status hg_sane_middleware::set_option(SANE_Handle h, const void* option, SA
return local_utility::scanner_err_2_sane_statu(hg_scanner_set_parameter(handle, name.c_str(), value, 0));
}
utils::to_log(LOG_LEVEL_FATAL, "Option descriptor %d not found.\n", option);
local_utility::to_log(LOG_LEVEL_FATAL, "Option descriptor %d not found.\n", option);
return SANE_STATUS_UNSUPPORTED;
}
else if (!value && desc->type != SANE_TYPE_BUTTON)
{
//if (action == SANE_ACTION_SET_AUTO) // we assume the driver can set the option properbly, and no work to do
//{
// utils::to_log(LOG_LEVEL_WARNING, "Option %d(%s) call SANE_ACTION_SET_AUTO, we set default value.\n", option, desc->title);
// local_utility::to_log(LOG_LEVEL_WARNING, "Option %d(%s) call SANE_ACTION_SET_AUTO, we set default value.\n", option, desc->title);
//
// value = get_default_value(handle, option);
// if (!value)
@ -1919,7 +1949,7 @@ SANE_Status hg_sane_middleware::set_option(SANE_Handle h, const void* option, SA
//}
//else
{
utils::to_log(LOG_LEVEL_WARNING, "Option descriptor %d(%s) need a value!.\n", option, desc->title);
local_utility::to_log(LOG_LEVEL_WARNING, "Option descriptor %d(%s) need a value!.\n", option, desc->title);
return SANE_STATUS_INVAL;
}
@ -1959,11 +1989,11 @@ SANE_Status hg_sane_middleware::set_option(SANE_Handle h, const void* option, SA
if (prev == v)
{
utils::to_log(LOG_LEVEL_ALL, "-->Set option(%d - %s) value: %s\n", option, desc_title.c_str(), v.c_str());
local_utility::to_log(LOG_LEVEL_ALL, "-->Set option(%d - %s) value: %s\n", option, desc_title.c_str(), v.c_str());
}
else
{
utils::to_log(LOG_LEVEL_ALL, "-->Set option(%d - %s) value: %s(Applied: %s)\n", option, desc_title.c_str(), prev.c_str(), v.c_str());
local_utility::to_log(LOG_LEVEL_ALL, "-->Set option(%d - %s) value: %s(Applied: %s)\n", option, desc_title.c_str(), prev.c_str(), v.c_str());
}
if (err == SCANNER_ERR_OK)
@ -1976,7 +2006,7 @@ SANE_Status hg_sane_middleware::set_option(SANE_Handle h, const void* option, SA
}
else if (err == SCANNER_ERR_CONFIGURATION_CHANGED)
{
utils::to_log(LOG_LEVEL_DEBUG, "the setting '%s' affects other options value, RELOAD ...\n", desc_title.c_str());
local_utility::to_log(LOG_LEVEL_DEBUG, "the setting '%s' affects other options value, RELOAD ...\n", desc_title.c_str());
//on_SCANNER_ERR_CONFIGURATION_CHANGED(dev);
if(dev->fixed_id.count(SANE_OPT_ID_LANGUAGE) == 0 || dev->fixed_id[SANE_OPT_ID_LANGUAGE] != id) // language reload by callback already
reload_options(handle);
@ -1984,12 +2014,12 @@ SANE_Status hg_sane_middleware::set_option(SANE_Handle h, const void* option, SA
}
else if(err == SCANNER_ERR_RELOAD_IMAGE_PARAM)
{
utils::to_log(LOG_LEVEL_DEBUG, "the setting '%s' affects image parameter, APP should re-get ...\n", desc_title.c_str());
local_utility::to_log(LOG_LEVEL_DEBUG, "the setting '%s' affects image parameter, APP should re-get ...\n", desc_title.c_str());
err = (scanner_err)SANE_INFO_RELOAD_PARAMS;
}
else if(err == SCANNER_ERR_RELOAD_OPT_PARAM)
{
utils::to_log(LOG_LEVEL_DEBUG, "the setting '%s' affects image parameter and options, APP should re-get image info and reload options...\n", desc_title.c_str());
local_utility::to_log(LOG_LEVEL_DEBUG, "the setting '%s' affects image parameter and options, APP should re-get image info and reload options...\n", desc_title.c_str());
//on_SCANNER_ERR_CONFIGURATION_CHANGED(dev);
if (dev->fixed_id.count(SANE_OPT_ID_LANGUAGE) == 0 || dev->fixed_id[SANE_OPT_ID_LANGUAGE] != id) // language reload by callback already
reload_options(handle);
@ -2060,7 +2090,7 @@ SANE_Status hg_sane_middleware::io_control(SANE_Handle h, unsigned long code, vo
if (ret == SCANNER_ERR_CONFIGURATION_CHANGED)
{
int nc = code;
utils::to_log(LOG_LEVEL_DEBUG, "the setting '0x%08x' affects other options value, RELOAD ...\n", nc);
local_utility::to_log(LOG_LEVEL_DEBUG, "the setting '0x%08x' affects other options value, RELOAD ...\n", nc);
on_SCANNER_ERR_CONFIGURATION_CHANGED(dev);
}
@ -2116,7 +2146,7 @@ bool hg_sane_middleware::is_enable_and(scanner_handle hdev, const std::vector<MA
std::vector<CURVAL>::iterator it = std::find(curvals.begin(), curvals.end(), master[i].name);
if (it == curvals.end())
{
utils::to_log(LOG_LEVEL_WARNING, "option %s's current value is not found, other options depend it maybe in wrong status.\n", master[i].name.c_str());
local_utility::to_log(LOG_LEVEL_WARNING, "option %s's current value is not found, other options depend it maybe in wrong status.\n", master[i].name.c_str());
continue;
}
@ -2138,7 +2168,7 @@ bool hg_sane_middleware::is_enable_or(scanner_handle hdev, const std::vector<MAS
std::vector<CURVAL>::iterator it = std::find(curvals.begin(), curvals.end(), master[i].name);
if (it == curvals.end())
{
utils::to_log(LOG_LEVEL_WARNING, "option %s's current value is not found, other options depend it maybe in wrong status.\n", master[i].name.c_str());
local_utility::to_log(LOG_LEVEL_WARNING, "option %s's current value is not found, other options depend it maybe in wrong status.\n", master[i].name.c_str());
continue;
}
@ -2553,7 +2583,7 @@ extern "C" { // avoid compiler exporting name in C++ style !!!
}
SANE_Status inner_sane_start(SANE_Handle handle)
{
utils::log_info("sane_start\n", LOG_LEVEL_ALL);
local_utility::to_log(LOG_LEVEL_ALL, "sane_start\n");
return hg_sane_middleware::instance()->start(handle, NULL);
}
@ -2568,13 +2598,13 @@ extern "C" { // avoid compiler exporting name in C++ style !!!
}
void inner_sane_cancel(SANE_Handle handle)
{
utils::log_info("sane_cancel\n", LOG_LEVEL_ALL);
local_utility::to_log(LOG_LEVEL_ALL, "sane_cancel\n");
hg_sane_middleware::instance()->stop(handle);
}
SANE_Status inner_sane_set_io_mode(SANE_Handle handle, SANE_Bool non_blocking)
{
utils::log_info("sane_set_io_mode\n", LOG_LEVEL_ALL);
local_utility::to_log(LOG_LEVEL_ALL, "sane_set_io_mode\n");
return non_blocking ? SANE_STATUS_UNSUPPORTED : SANE_STATUS_GOOD;
}

View File

@ -441,11 +441,25 @@ extern "C"{
const char* hg_scanner_err_description(int err);
bool hg_scanner_log_is_enable(int level);
void hg_scanner_log(const char* info);
void hg_scanner_log(const char* info, int level);
// Function: 图像状态名称
const char* hg_scanner_image_statu_name(int img_statu);
// Function: 获取模块全路径
//
// Parameter: buf - buffer to receive the full path
//
// size - [in] capacity of 'buf'; [out] real length of the full path or minimum size should needed
//
// part_name - part of the module name, nullptr for the first module (often be the PE)
//
// Return: 0 - OK
// ENOMEM - buffer is too small, and the desired size is stored in size
// EINVAL - parameter 'size' is nullptr
// ENOENT - not found the module
int hg_get_full_path_of_module(char* buf, size_t* size, const char* part_name = nullptr);
#ifdef __cplusplus
}
#endif