193 lines
8.3 KiB
C++
193 lines
8.3 KiB
C++
#include "scanner_const_opts.h"
|
|
|
|
#include <json/gb_json.h>
|
|
#include <sane/sane_ex.h>
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
// version ...
|
|
#ifndef VER_MAIN
|
|
#define VER_MAIN 2
|
|
#define VER_MINOR 0
|
|
#define VER_DATE 20231219
|
|
#define VER_BUILD 1
|
|
#endif
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
// option json:
|
|
static std::string device_opt_json[] = {
|
|
"{\"dev-vid\":{\"cat\":\"base\",\"group\":\"\\u5173\\u4e8e\",\"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\":\"\\u5173\\u4e8e\",\"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\":\"\\u5173\\u4e8e\",\"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\":\"300NewTx\",\"default\":\"300NewTx\"},\"dev-model\":{\"cat\":\"base\",\"group\":\"\\u5173\\u4e8e\",\"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\":\"G300\",\"default\":\"G300\"},\"dev-sn\":{\"cat\":\"base\",\"group\":\"\\u5173\\u4e8e\",\"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\":\"\\u5173\\u4e8e\",\"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,\"ownread\":true,\"cur\":\"G2393B0500\",\"default\":\"G2393B0500\"},\"roller-life\":{\"cat\":\"base\",\"group\":\"\\u5173\\u4e8e\",\"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\":16,\"auth\":0,\"readonly\":true,\"size\":4,\"auto\":false,\"cur\":450000,\"default\":450000},\"ip-addr\":{\"cat\":\"base\",\"group\":\"\\u5173\\u4e8e\",\"title\":\"IP\",\"desc\":\"\\u8bbe\\u5907\\u8054\\u7f51\\u65f6\\u6240\\u5206\\u914d\\u7684IP\\u5730\\u5740\",\"type\":\"string\",\"fix-id\":34904,\"ui-pos\":20,\"auth\":0,\"readonly\":true,\"size\":96,\"auto\":false,\"ownread\":true,\"cur\":\"0\",\"default\":\"0\"},\"mac-addr\":{\"cat\":\"base\",\"group\":\"\\u5173\\u4e8e\",\"title\":\"MAC\",\"desc\":\"\\u8bbe\\u5907\\u7f51\\u5361\\u5730\\u5740\",\"type\":\"string\",\"fix-id\":34905,\"ui-pos\":21,\"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)
|
|
{
|
|
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);
|
|
}
|
|
|
|
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"));
|
|
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();
|
|
}
|
|
|
|
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");
|
|
}
|
|
else if(strcmp(name, SANE_STD_OPT_NAME_FIRMWARE_VERSION) == 0)
|
|
{
|
|
char ver[40] = {0};
|
|
sprintf(ver, "%u.%u.%u%02u", VER_MAIN, VER_MINOR, VER_DATE, VER_BUILD);
|
|
val = ver;
|
|
}
|
|
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;
|
|
} |