补全设备属性访问;添加16进制转换

This commit is contained in:
gb 2023-12-18 09:38:39 +08:00
parent cad9d653c8
commit ae09199deb
6 changed files with 123 additions and 3 deletions

View File

@ -261,6 +261,26 @@ char* hg_scanner::get_value(const char* name, void* value, size_t* size, int* er
if (err)
*err = status_;
}
else if (scanner_)
{
uint32_t len = 0;
int result = 0;
dev_opts_->get_option_value_type(name, size);
len = *size;
ret = (char*)malloc(len);
result = scanner_->option_value_get(name, ret, &len);
*size = len;
if (result)
{
free(ret);
ret = nullptr;
}
if (err)
*err = result;
}
else if (err)
*err = SCANNER_ERR_NOT_OPEN;
return ret;
}
@ -390,6 +410,31 @@ bool hg_scanner::is_online(void)
{
return online_;
}
char* hg_scanner::get_option_value(const char* name, void* value, size_t* size, int* err)
{
std::string val(dev_opts_->get_option_value(name, SANE_ACTION_GET_VALUE, (int*)size, value));
char* ret = nullptr;
if (val.empty())
{
if (err)
*err = SCANNER_ERR_DEVICE_NOT_FOUND;
}
else
{
ret = (char*)malloc(val.length() + 1);
memcpy(ret, val.c_str(), val.length());
ret[val.length()] = 0;
if (err)
*err = SCANNER_ERR_OK;
if (size)
*size = val.length();
}
return ret;
}
device_option* hg_scanner::get_device_opt(void)
{
dev_opts_->add_ref();

View File

@ -122,8 +122,11 @@ public:
std::string status_message(void);
bool is_online(void);
char* get_option_value(const char* name, void* value, size_t* size, int* err);
// advanced functions ...
public:
device_option* get_device_opt(void);
int get_peer_config(LPPEERCFG cfg);
int file_transfer(const char* local, const char* remote, bool to_remote); // get_value("tx-prog", (double*)percent) to query progress

View File

@ -373,7 +373,7 @@ int scanner_handler::option_get_all(std::string& json_opts)
WAIT_COMMAND(call, clean, roger, &json_opts);
}
int scanner_handler::option_value_get(const char* name, void* buf, uint32_t size)
int scanner_handler::option_value_get(const char* name, void* buf, uint32_t* size)
{
auto call = [&](cmd_result* cmd) -> int
{
@ -394,6 +394,7 @@ int scanner_handler::option_value_get(const char* name, void* buf, uint32_t size
{
if (!cmd->is_over())
memcpy(cmd->get_param(), cfg->data + cfg->val_off, cfg->val_size);
*size = cfg->val_size;
*used = sizeof(*pack) + pack->payload_len;
cmd->trigger();
}

View File

@ -104,7 +104,7 @@ public:
// following methods transferred by Bulk, blocked ...
int option_get_all(std::string& json_opts);
int option_value_get(const char* name, void* buf, uint32_t size/*buffer size of 'buf'*/);
int option_value_get(const char* name, void* buf, uint32_t *size/*buffer size of 'buf' and real size of return value*/);
int option_value_set(const char* name, uint32_t type, void* buf, uint32_t size/*buffer size of 'buf'*/, uint32_t val_size, uint8_t* after);
int status_get(void);

View File

@ -857,6 +857,72 @@ namespace utils
return str.c_str();
}
bool from_hex_string(const char* hex, uint8_t* val)
{
*val = 0;
if(*hex >= '0' && *hex <= '9')
*val = *hex - '0';
else if(*hex >= 'a' && *hex <= 'f')
*val = *hex - 'a' + 10;
else if(*hex >= 'A' && *hex <= 'F')
*val = *hex - 'A' + 10;
else
return *hex == 0;
hex++;
if(*hex)
{
*val <<= 4;
if(*hex >= '0' && *hex <= '9')
*val += *hex - '0';
else if(*hex >= 'a' && *hex <= 'f')
*val += *hex - 'a' + 10;
else if(*hex >= 'A' && *hex <= 'F')
*val += *hex - 'A' + 10;
else
return false;
}
return true;
}
bool from_hex_string(const char* hex, uint16_t* val)
{
if(from_hex_string(hex, (uint8_t*)val))
{
hex += 2;
*val = swap_half(*val);
return from_hex_string(hex, (uint8_t*)val);
}
else
return false;
}
bool from_hex_string(const char* hex, uint32_t* val)
{
if(from_hex_string(hex, (uint16_t*)val))
{
hex += 4;
*val = swap_half(*val);
return from_hex_string(hex, (uint16_t*)val);
}
else
return false;
}
bool from_hex_string(const char* hex, uint64_t* val)
{
if(from_hex_string(hex, (uint32_t*)val))
{
hex += 8;
*val = swap_half(*val);
return from_hex_string(hex, (uint32_t*)val);
}
else
return false;
}
HMODULE load_dll(const char* path_file, int flag)
{
#if OS_WIN
@ -1103,7 +1169,7 @@ namespace utils
if (fn_appendix)
file += fn_appendix;
file += ".log";
printf("log file: %s\n", file.c_str());
printf("log file : %s\n", file.c_str());
}
log_cls::instance()->set_log_type(type, &file[0]);

View File

@ -52,6 +52,11 @@ namespace utils
const char* to_lower(std::string& str); // return str.c_str()
const char* trim(std::string& str, const char* sp = "\r\n\t "); // return str.c_str()
bool from_hex_string(const char* hex, uint8_t* val);
bool from_hex_string(const char* hex, uint16_t* val);
bool from_hex_string(const char* hex, uint32_t* val);
bool from_hex_string(const char* hex, uint64_t* val);
HMODULE load_dll(const char* path_file, int flag);
bool create_folder(const char* folder);
void set_ini_value(const char* seg, const char* key, const char* val, const char* cfg_file);