fix bug when set option

This commit is contained in:
gb 2023-12-26 13:51:22 +08:00
parent 46c617fa8b
commit f08f2ac612
9 changed files with 58 additions and 15 deletions

View File

@ -291,7 +291,7 @@ int hg_scanner::set_value(const char* name, void* val)
ret = SCANNER_ERR_OK; ret = SCANNER_ERR_OK;
size_t size = 0; size_t size = 0;
std::string t(dev_opts_->get_option_value_type(name, &size)); std::string t(dev_opts_->get_option_value_type(name, &size));
uint8_t after = 0; uint16_t after = 0;
val_size = size; val_size = size;
if (t == JSON_SANE_TYPE_BOOL) if (t == JSON_SANE_TYPE_BOOL)
@ -312,13 +312,8 @@ int hg_scanner::set_value(const char* name, void* val)
val_size = strlen((char*)val); val_size = strlen((char*)val);
} }
ret = scanner_->option_value_set(name, type, val, size, val_size, &after); ret = scanner_->option_value_set(name, type, val, size, val_size, &after);
if (ret == 0)
{
if (after)
ret = SCANNER_ERR_RELOAD_OPT_PARAM;
}
return ret; return ret ? ret : after;
} }
int hg_scanner::start(void) int hg_scanner::start(void)

View File

@ -165,7 +165,7 @@ int async_usb_host::start(libusb_device* dev)
usb_dev_ = dev; usb_dev_ = dev;
memset(&peer_cfg_, 0, sizeof(peer_cfg_)); memset(&peer_cfg_, 0, sizeof(peer_cfg_));
if (get_peer_config(&peer_cfg_)) if (hello(&peer_cfg_))
peer_cfg_.io_size = bulk_out_.max_packet; peer_cfg_.io_size = bulk_out_.max_packet;
utils::to_log(LOG_LEVEL_DEBUG, "IO size: %u\n", peer_cfg_.io_size); utils::to_log(LOG_LEVEL_DEBUG, "IO size: %u\n", peer_cfg_.io_size);
for (int i = 0; i < SIZE_MB(10) / peer_cfg_.io_size; ++i) for (int i = 0; i < SIZE_MB(10) / peer_cfg_.io_size; ++i)
@ -184,6 +184,7 @@ int async_usb_host::stop(void)
dyn_mem_ptr data = nullptr; dyn_mem_ptr data = nullptr;
data_source_ptr out = nullptr; data_source_ptr out = nullptr;
bye_bye();
run_ = false; run_ = false;
if (usb_handle_) if (usb_handle_)
@ -597,7 +598,7 @@ dyn_mem_ptr async_usb_host::handle_data_in(dyn_mem_ptr& data, uint32_t* used, pa
} }
} }
int async_usb_host::get_peer_config(LPPEERCFG cfg) int async_usb_host::hello(LPPEERCFG cfg)
{ {
SIMPLE_LOCK(io_lock_); SIMPLE_LOCK(io_lock_);
PEERCFG v = { 0 }; PEERCFG v = { 0 };
@ -623,6 +624,22 @@ int async_usb_host::get_peer_config(LPPEERCFG cfg)
return err == sizeof(v) ? 0 : EFAULT; return err == sizeof(v) ? 0 : EFAULT;
} }
int async_usb_host::bye_bye(void)
{
SIMPLE_LOCK(io_lock_);
PEERCFG v = { 0 };
int err = 0;
v.pid = GetCurrentProcessId();
v.io_size = utils::get_page_size();
v.ver = PROTOCOL_VER;
err = libusb_control_transfer(usb_handle_, LIBUSB_REQUEST_TYPE_VENDOR/* | LIBUSB_ENDPOINT_IN*/
, USB_REQ_EP0_GOOD_BYE, 0, 0
, (unsigned char*)&v, sizeof(v)
, 1000);
return err == sizeof(v) ? 0 : EFAULT;
}
uint16_t async_usb_host::get_protocol_version(void) uint16_t async_usb_host::get_protocol_version(void)
{ {
return peer_cfg_.ver; return peer_cfg_.ver;

View File

@ -90,7 +90,8 @@ public:
uint8_t& encrypt_data(void); uint8_t& encrypt_data(void);
public: public:
int get_peer_config(LPPEERCFG cfg); int hello(LPPEERCFG cfg);
int bye_bye(void);
uint16_t get_protocol_version(void); uint16_t get_protocol_version(void);
uint32_t get_io_buffer_size(void); uint32_t get_io_buffer_size(void);
int get_peer_status(LPEP0REPLYSTATUS status, bool en_dev_log); int get_peer_status(LPEP0REPLYSTATUS status, bool en_dev_log);

View File

@ -408,12 +408,12 @@ int scanner_handler::option_value_get(const char* name, void* buf, uint32_t* siz
WAIT_COMMAND(call, clean, roger, buf); WAIT_COMMAND(call, clean, roger, buf);
} }
int scanner_handler::option_value_set(const char* name, uint32_t type, void* buf, uint32_t size, uint32_t val_size, uint8_t* after) int scanner_handler::option_value_set(const char* name, uint32_t type, void* buf, uint32_t size, uint32_t val_size, uint16_t* after)
{ {
struct struct
{ {
void* buf; void* buf;
uint8_t* after; uint16_t* after;
}param; }param;
auto call = [&](cmd_result* cmd) -> int auto call = [&](cmd_result* cmd) -> int
{ {
@ -429,7 +429,7 @@ int scanner_handler::option_value_set(const char* name, uint32_t type, void* buf
struct struct
{ {
void* buf; void* buf;
uint8_t* after; uint16_t* after;
}*result = nullptr; }*result = nullptr;
LPPACK_BASE pack = (LPPACK_BASE)data->ptr(); LPPACK_BASE pack = (LPPACK_BASE)data->ptr();
LPCFGVAL cfg = (LPCFGVAL)pack->payload; LPCFGVAL cfg = (LPCFGVAL)pack->payload;

View File

@ -105,7 +105,7 @@ public:
// following methods transferred by Bulk, blocked ... // following methods transferred by Bulk, blocked ...
int option_get_all(std::string& json_opts); int option_get_all(std::string& json_opts);
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_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 option_value_set(const char* name, uint32_t type, void* buf, uint32_t size/*buffer size of 'buf'*/, uint32_t val_size, uint16_t* after);
int status_get(void); int status_get(void);
void set_image_receiver(std::function<data_holder_ptr(LPPACKIMAGE/*NULL when scanning stopped*/, uint64_t/*size in image, error in stopped*/)> img); void set_image_receiver(std::function<data_holder_ptr(LPPACKIMAGE/*NULL when scanning stopped*/, uint64_t/*size in image, error in stopped*/)> img);

View File

@ -47,6 +47,7 @@ enum cancel_io
enum ep0_req enum ep0_req
{ {
USB_REQ_EP0_HAND_SHAKE = 100, // hand-shake[in/out] with data LPPEERCFG, req = me, ind = 0, val = 0, len = sizeof(PEERCFG) USB_REQ_EP0_HAND_SHAKE = 100, // hand-shake[in/out] with data LPPEERCFG, req = me, ind = 0, val = 0, len = sizeof(PEERCFG)
USB_REQ_EP0_GOOD_BYE, // disconnect, req = me, ind = 0, val = 0, len = sizeof(PEERCFG)
USB_REQ_EP0_GET_STATUS, // 获取各工作线程状态, return EP0REPLYSTATUS. req = me, ind = 0, val = bool: whether write log, len = sizeof(EP0REPLYSTATUS) USB_REQ_EP0_GET_STATUS, // 获取各工作线程状态, return EP0REPLYSTATUS. req = me, ind = 0, val = bool: whether write log, len = sizeof(EP0REPLYSTATUS)
USB_REQ_EP0_CANCEL_IO, // 设置当前IO数据的有效性. req = me, ind = 0, val = 0, len = sizeof(uint32_t), discard IO data when data is CANCEL_IO_CANCEL USB_REQ_EP0_CANCEL_IO, // 设置当前IO数据的有效性. req = me, ind = 0, val = 0, len = sizeof(uint32_t), discard IO data when data is CANCEL_IO_CANCEL
// work-flow: write control with 'CANCEL_IO_CANCEL', write bulk with 1 byte, write control with not 'CANCEL_IO_CANCEL' to restore // work-flow: write control with 'CANCEL_IO_CANCEL', write bulk with 1 byte, write control with not 'CANCEL_IO_CANCEL' to restore
@ -294,7 +295,7 @@ typedef struct _config_val
uint8_t type; // same as SANE_Value_Type uint8_t type; // same as SANE_Value_Type
uint8_t name_off; // name offset of the option in data, end with '\0' uint8_t name_off; // name offset of the option in data, end with '\0'
uint8_t val_off; // option value offset in data uint8_t val_off; // option value offset in data
uint8_t after_do; // see SANE_INFO_xxx in sane.h uint16_t after_do; // see SCANNER_ERR_RELOAD_xxx in hgscanner_error.h
uint16_t val_size; // real size of value uint16_t val_size; // real size of value
uint16_t max_size; // max size of this option, this value has given in gb_json::size uint16_t max_size; // max size of this option, this value has given in gb_json::size
char data[0]; // contains value and name. fetch them according name_off and val_off members. char data[0]; // contains value and name. fetch them according name_off and val_off members.

View File

@ -31,6 +31,31 @@ std::string sane_opt_provider::sane_value_2_text(const char* type, void* value)
else else
return ""; return "";
} }
void sane_opt_provider::set_opt_value(gb_json* opt, void* value, const char* key)
{
std::string type("");
opt->get_value("type", type);
if (!key || *key == 0)
key = "cur";
if (type == JSON_SANE_TYPE_BOOL)
{
opt->set_value(key, *(bool*)value);
}
else if (type == JSON_SANE_TYPE_INT)
{
opt->set_value(key, *(int*)value);
}
else if (type == JSON_SANE_TYPE_FIXED)
{
opt->set_value(key, *(double*)value);
}
else if (type == JSON_SANE_TYPE_STRING)
{
opt->set_value(key, (char*)value);
}
}
bool sane_opt_provider::set_opt_json_text(char* txt) bool sane_opt_provider::set_opt_json_text(char* txt)
{ {

View File

@ -9,6 +9,8 @@
#include <map> #include <map>
#include <base/utils.h> // for refer #include <base/utils.h> // for refer
class gb_json;
class sane_opt_provider : public refer class sane_opt_provider : public refer
{ {
bool is_in_another_module_; bool is_in_another_module_;
@ -23,6 +25,7 @@ public:
sane_opt_provider(); sane_opt_provider();
static std::string sane_value_2_text(const char* type, void* value); // convert to readable text. e.g. bool to "true" or "false", ... static std::string sane_value_2_text(const char* type, void* value); // convert to readable text. e.g. bool to "true" or "false", ...
static void set_opt_value(gb_json* opt, void* value, const char* key = "cur");
protected: protected:
virtual ~sane_opt_provider(); virtual ~sane_opt_provider();

View File

@ -1213,6 +1213,7 @@ void device_option::insert_option(gb_json* opt, sane_opt_provider* from, const c
type = sane_opt_provider::sane_value_2_text(type.c_str(), &val[0]); type = sane_opt_provider::sane_value_2_text(type.c_str(), &val[0]);
utils::to_log(LOG_LEVEL_ALL, "Set option '%s' to default value: '%s'\n", opt->key().c_str(), type.c_str()); utils::to_log(LOG_LEVEL_ALL, "Set option '%s' to default value: '%s'\n", opt->key().c_str(), type.c_str());
from->set_value(opt->key().c_str(), &val[0]); from->set_value(opt->key().c_str(), &val[0]);
sane_opt_provider::set_opt_value(opt, &val[0]);
} }
} }
} }