newtx/scanner/scanner_const_opts.cpp

263 lines
11 KiB
C++

#include "scanner_const_opts.h"
#include <json/gb_json.h>
#include <sane/sane_ex.h>
#include <string.h>
#include <base/ui.h>
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// version ...
#ifndef VER_MAIN
#define VER_MAIN 2
#define VER_FAMILY 200
#define VER_DATE 20231225
#define VER_BUILD 1
#endif
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// option json:
static std::string device_opt_json[] = {
"{\"dev-vid\":{\"cat\":\"base\",\"group\":\"about\",\"title\":\"USB-VID\",\"desc\":\"\\u8bbe\\u5907\\u5236\\u9020\\u5546\\u5728USB\\u7ec4\\u7ec7\\u7684ID\",\"type\":\"string\",\"fix-id\":34898,\"ui-pos\":10,\"auth\":0,\"readonly\":true,\"size\":16,\"auto\":false,\"cur\":\"3072\",\"default\":\"3072\"},\"dev-pid\":{\"cat\":\"base\",\"group\":\"about\",\"title\":\"USB-PID\",\"desc\":\"\\u8bbe\\u5907\\u5728USB\\u7ec4\\u7ec7\\u4e2d\\u7684\\u4ea7\\u54c1ID\",\"type\":\"string\",\"fix-id\":34899,\"ui-pos\":11,\"auth\":0,\"readonly\":true,\"size\":16,\"auto\":false,\"cur\":\"0306\",\"default\":\"0306\"},\"dev-name\":{\"cat\":\"base\",\"group\":\"about\",\"title\":\"\\u8bbe\\u5907\\u540d\\u79f0\",\"desc\":\"\\u8bbe\\u5907\\u540d\\u79f0\",\"type\":\"string\",\"fix-id\":34900,\"ui-pos\":12,\"auth\":0,\"readonly\":true,\"size\":96,\"auto\":false,\"cur\":\"HUAGOSCAN G200\",\"default\":\"HUAGOSCAN G200\"},\"dev-model\":{\"cat\":\"base\",\"group\":\"about\",\"title\":\"\\u4ea7\\u54c1\\u7cfb\\u5217\",\"desc\":\"\\u8bbe\\u5907\\u6240\\u5c5e\\u4ea7\\u54c1\\u7cfb\\u5217\\u540d\\u79f0\",\"type\":\"string\",\"fix-id\":34901,\"ui-pos\":13,\"auth\":0,\"readonly\":true,\"size\":96,\"auto\":false,\"cur\":\"G200\",\"default\":\"G200\"},\"dev-sn\":{\"cat\":\"base\",\"group\":\"about\",\"title\":\"\\u5e8f\\u5217\\u53f7\",\"desc\":\"\\u8bbe\\u5907\\u5e8f\\u5217\\u53f7\",\"type\":\"string\",\"fix-id\":34902,\"ui-pos\":14,\"auth\":0,\"readonly\":true,\"size\":32,\"auto\":false,\"ownread\":true,\"cur\":\"GB20231201\",\"default\":\"GB20231201\"},\"fmw-ver\":{\"cat\":\"base\",\"group\":\"about\",\"title\":\"\\u56fa\\u4ef6\\u7248\\u672c\",\"desc\":\"\\u8bbe\\u5907\\u56fa\\u4ef6\\u7248\\u672c\\u53f7\",\"type\":\"string\",\"fix-id\":34903,\"ui-pos\":15,\"auth\":0,\"readonly\":true,\"size\":32,\"auto\":false,\"cur\":\"G2393B0500\",\"default\":\"G2393B0500\"},\"roller-life\":{\"cat\":\"base\",\"group\":\"about\",\"title\":\"\\u6eda\\u8f74\\u5bff\\u547d\",\"desc\":\"\\u8be5\\u8bbe\\u5907\\u6eda\\u8f74\\u8fc7\\u7eb8\\u7684\\u6700\\u5927\\u5f20\\u6570\",\"type\":\"int\",\"fix-id\":34907,\"ui-pos\":20,\"auth\":0,\"readonly\":true,\"size\":4,\"auto\":false,\"cur\":450000,\"default\":450000},\"roll-cnt\":{\"cat\":\"base\",\"group\":\"about\",\"title\":\"\\u6eda\\u8f74\\u5f20\\u6570\",\"desc\":\"\\u5f53\\u524d\\u6eda\\u8f74\\u626b\\u63cf\\u5f20\\u6570\",\"type\":\"int\",\"fix-id\":39170,\"ui-pos\":21,\"auth\":0,\"readonly\":true,\"size\":4,\"auto\":false,\"ownread\":true,\"cur\":0,\"default\":0},\"total-cnt\":{\"cat\":\"base\",\"group\":\"about\",\"title\":\"\\u5386\\u53f2\\u5f20\\u6570\",\"desc\":\"\\u8be5\\u8bbe\\u5907\\u5386\\u53f2\\u603b\\u8ba1\\u626b\\u63cf\\u5f20\\u6570\",\"type\":\"int\",\"fix-id\":34889,\"ui-pos\":22,\"auth\":0,\"readonly\":true,\"size\":4,\"auto\":false,\"ownread\":true,\"cur\":0,\"default\":0},\"ip-addr\":{\"cat\":\"base\",\"group\":\"about\",\"title\":\"IP\",\"desc\":\"\\u8bbe\\u5907\\u8054\\u7f51\\u65f6\\u6240\\u5206\\u914d\\u7684IP\\u5730\\u5740\",\"type\":\"string\",\"fix-id\":34904,\"ui-pos\":30,\"auth\":0,\"readonly\":true,\"size\":96,\"auto\":false,\"ownread\":true,\"cur\":\"0\",\"default\":\"0\"},\"mac-addr\":{\"cat\":\"base\",\"group\":\"about\",\"title\":\"MAC\",\"desc\":\"\\u8bbe\\u5907\\u7f51\\u5361\\u5730\\u5740\",\"type\":\"string\",\"fix-id\":34905,\"ui-pos\":31,\"auth\":0,\"readonly\":true,\"size\":96,\"auto\":false,\"ownread\":true,\"cur\":\"0\",\"default\":\"0\"}}"
};
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// scanner_const_opts
scanner_const_opts::scanner_const_opts(const char* dev_path) : root_(dev_path)
{
set_where("scanner_const_opts");
init();
}
scanner_const_opts::~scanner_const_opts()
{}
std::string scanner_const_opts::get_device_content(const char* path, bool truncate_from_lrn)
{
FILE *src = fopen((root_ + "/" + path).c_str(), "rb");
char buf[1024] = {0};
std::string ret("");
if(src)
{
int r = 0;
while((r = fread(buf, 1, sizeof(buf), src)) > 0)
ret += std::string(buf, r);
fclose(src);
if(truncate_from_lrn)
{
size_t pos = ret.find("\n");
if(pos != std::string::npos)
ret.erase(pos);
pos = ret.find("\r");
if(pos != std::string::npos)
ret.erase(pos);
}
}
return std::move(ret);
}
std::string scanner_const_opts::get_ip(void)
{
std::string val(utils::get_command_result("ifconfig | grep broadcast"));
char buf[80] = {0};
sscanf(val.c_str(), " inet %s", buf);
return buf;
}
std::string scanner_const_opts::get_mac(void)
{
std::string val(utils::get_command_result("ifconfig | grep ether"));
char buf[80] = {0};
sscanf(val.c_str(), " ether %s", buf);
return buf;
}
void scanner_const_opts::init(void)
{
std::string text("");
gb_json *jsn = new gb_json();
for(auto& v: device_opt_json)
text += v;
;
if(jsn->attach_text(&text[0]))
{
gb_json *child = nullptr;
text = "";
jsn->get_value(SANE_STD_OPT_NAME_VID, child);
if(child)
{
std::string val(get_device_content("idVendor"));
size_t pos = val.find("0x");
uint16_t id = 0;
if(pos != std::string::npos)
val.erase(0, 2);
if(utils::from_hex_string(val.c_str(), &id))
{
char str[20] = {0};
sprintf(str, "%04X", id);
child->set_value("cur", str);
child->set_value("default", str);
}
child->release();
}
jsn->get_value(SANE_STD_OPT_NAME_PID, child);
if(child)
{
std::string val(get_device_content("idProduct"));
size_t pos = val.find("0x");
uint16_t id = 0;
if(pos != std::string::npos)
val.erase(0, 2);
if(utils::from_hex_string(val.c_str(), &id))
{
char str[20] = {0};
sprintf(str, "%04X", id);
child->set_value("cur", str);
child->set_value("default", str);
}
child->release();
}
jsn->get_value(SANE_STD_OPT_NAME_DEVICE_SERIAL_NO, child);
if(child)
{
std::string val(get_device_content("strings/0x409/serialnumber", true));
child->set_value("cur", val.c_str());
child->set_value("default", val.c_str());
child->release();
}
jsn->get_value(SANE_STD_OPT_NAME_DEVICE_MAC_ADDR, child);
if(child)
{
std::string val(get_mac());
child->set_value("cur", val.c_str());
child->set_value("default", val.c_str());
child->release();
}
jsn->get_value(SANE_STD_OPT_NAME_DEVICE_IP_ADDR, child);
if(child)
{
std::string val(get_ip());
child->set_value("cur", val.c_str());
child->set_value("default", val.c_str());
child->release();
}
jsn->get_value(SANE_STD_OPT_NAME_FIRMWARE_VERSION, child);
if(child)
{
char ver[40] = {0};
sprintf(ver, "%u.%u.%u%02u", VER_MAIN, VER_FAMILY, VER_DATE, VER_BUILD);
child->set_value("cur", ver);
child->set_value("default", ver);
child->release();
utils::to_log(LOG_LEVEL_DEBUG, "version: %s\n", ver);
}
jsn->get_value(SANE_STD_OPT_NAME_DEVICE_MODEL, child);
if(child)
{
char ver[40] = {0};
sprintf(ver, "G%u", VER_FAMILY);
child->set_value("cur", ver);
child->set_value("default", ver);
child->release();
}
jsn->get_value(SANE_OPT_NAME(ROLLER_COUNT), child);
if(child)
{
child->set_value("cur", (int)roller_cnt_);
child->set_value("default", (int)roller_cnt_);
child->release();
}
jsn->get_value(SANE_OPT_NAME(HISTORY_COUNT), child);
if(child)
{
child->set_value("cur", (int)hist_cnt_);
child->set_value("default", (int)hist_cnt_);
child->release();
}
text = jsn->to_string();
set_opt_json_text(&text[0]);
}
jsn->release();
}
char* scanner_const_opts::get_value(const char* name, void* value, size_t* size, int* err)
{
char *ret = nullptr;
std::string val("");
if(err)
*err = 0;
if(strcmp(name, SANE_STD_OPT_NAME_DEVICE_IP_ADDR) == 0)
{
val = get_ip();
}
else if(strcmp(name, SANE_STD_OPT_NAME_DEVICE_MAC_ADDR) == 0)
{
val = get_mac();
}
else if(strcmp(name, SANE_STD_OPT_NAME_DEVICE_SERIAL_NO) == 0)
{
val = get_device_content("strings/0x409/serialnumber", true);
}
else if(strcmp(name, SANE_STD_OPT_NAME_FIRMWARE_VERSION) == 0)
{
char ver[40] = {0};
sprintf(ver, "%u.%u.%u%02u", VER_MAIN, VER_FAMILY, VER_DATE, VER_BUILD);
val = ver;
}
else if(strcmp(SANE_OPT_NAME(ROLLER_COUNT), name) == 0)
{
devui::send_message(devui::UI_CMD_GET_ROLLER_COUNT);
std::this_thread::sleep_for(std::chrono::milliseconds(50));
val = std::string((char*)&roller_cnt_, sizeof(roller_cnt_));
}
else if(strcmp(SANE_OPT_NAME(HISTORY_COUNT), name) == 0)
{
devui::send_message(devui::UI_CMD_GET_HISTORY_COUNT);
std::this_thread::sleep_for(std::chrono::milliseconds(50));
val = std::string((char*)&hist_cnt_, sizeof(hist_cnt_));
}
else if(err)
*err = ENOTSUP;
if(val.length())
{
ret = (char*)malloc(val.length() + 1);
memset(ret, 0, val.length() + 1);
strcpy(ret, val.c_str());
if(size)
*size = val.length();
}
return ret;
}
void scanner_const_opts::set_roller_count(uint32_t cnt)
{
roller_cnt_ = cnt;
}
void scanner_const_opts::set_history_count(uint32_t cnt)
{
hist_cnt_ = cnt;
}