#include "offline_opt.h" #include #include #include #include #include #include #include #include "../scanner_manager.h" // for version #include "user.h" static std::string device_opt_json[] = { "{\"company\":{\"cat\":\"base\",\"group\":\"\\u53ea\\u8bfb\\u5c5e\\u6027\",\"title\":\"\\u5236\\u9020\\u5546\",\"desc\":\"\\u8bbe\\u5907\\u5236\\u9020\\u5546\",\"ver\":0,\"pos\":0,\"fix-id\":34891,\"type\":\"string\",\"unit\":\"none\",\"affect\":0,\"readonly\":true,\"size\":128,\"auto\":false,\"cur\":\"0\",\"default\":\"0\"},\"copyright\":{\"cat\":\"base\",\"group\":\"\\u53ea\\u8bfb\\u5c5e\\u6027\",\"title\":\"\\u7248\\u672c\\u4fe1\\u606f\",\"desc\":\"\\u7248\\u6743\\u58f0\\u660e\\u7b49\\u4fe1\\u606f\",\"ver\":0,\"pos\":0,\"fix-id\":34892,\"type\":\"string\",\"unit\":\"none\",\"affect\":0,\"readonly\":true,\"size\":128,\"auto\":false,\"cur\":\"0\",\"default\":\"0\"},\"co-url\":{\"cat\":\"base\",\"group\":\"\\u53ea\\u8bfb\\u5c5e\\u6027\",\"title\":\"\\u516c\\u53f8\\u7f51\\u5740\",\"desc\":\"\\u516c\\u53f8\\u5b98\\u7f51\\u6216\\u552e\\u540e\\u670d\\u52a1\\u7f51\\u7ad9\\u5730\\u5740\",\"ver\":0,\"pos\":0,\"fix-id\":34893,\"type\":\"string\",\"unit\":\"none\",\"affect\":0,\"readonly\":true,\"size\":256,\"auto\":false,\"cur\":\"0\",\"default\":\"0\"},\"co-tel\":{\"cat\":\"base\",\"group\":\"\\u53ea\\u8bfb\\u5c5e\\u6027\",\"title\":\"\\u516c\\u53f8\\u7535\\u8bdd\",\"desc\":\"\\u516c\\u53f8\\u8054\\u7cfb\\u7535\\u8bdd\",\"ver\":0,\"pos\":0,\"fix-id\":34894,\"type\":\"string\",\"unit\":\"none\",\"affect\":0,\"readonly\":true,\"size\":129,\"auto\":false,\"cur\":\"0\",\"default\":\"0\"},\"co-addr\":{\"cat\":\"base\",\"group\":\"\\u53ea\\u8bfb\\u5c5e\\u6027\",\"title\":\"\\u516c\\u53f8\\u5730\\u5740\",\"desc\":\"\\u516c\\u53f8\\u5730\\u5740\",\"ver\":0,\"pos\":0,\"fix-id\":34895,\"type\":\"string\",\"unit\":\"none\",\"affect\":0,\"readonly\":true,\"size\":128,\"auto\":false,\"cur\":\"0\",\"default\":\"0\"},\"co-gps\":{\"cat\":\"base\",\"group\":\"\\u53ea\\u8bfb\\u5c5e\\u6027\",\"title\":\"\\u516c\\u53f8GPS\",\"desc\":\"\\u516c\\u53f8\\u5730\\u56fe\\u5730\\u5740\",\"ver\":0,\"pos\":0,\"fix-id\":34896,\"type\":\"string\",\"unit\":\"none\",\"affect\":0,\"readonly\":true,\"size\":256,\"auto\":false,\"cur\":\"0\",\"default\":\"0\"},\"drv-ver\":{\"cat\":\"base\",\"group\":\"\\u53ea\\u8bfb\\u5c5e\\u6027\",\"title\":\"\\u9a71\\u52a8\\u7248\\u672c\\u53f7\",\"desc\":\"PC\\u7aef\\u9a71\\u52a8\\u7a0b\\u5e8f\\u7248\\u672c\",\"ver\":0,\"pos\":0,\"fix-id\":34890,\"type\":\"string\",\"unit\":\"none\",\"affect\":0,\"readonly\":true,\"size\":48,\"auto\":false,\"cur\":\"0\",\"default\":\"0\"},\"login\":{\"cat\":\"advanced\",\"group\":\"\\u53ea\\u8bfb\\u5c5e\\u6027\",\"title\":\"\\u767b\\u5f55\",\"desc\":\"\\u7528\\u6237\\u767b\\u5f55\\u64cd\\u4f5c\",\"ver\":0,\"pos\":0,\"fix-id\":39168,\"type\":\"string\",\"unit\":\"none\",\"affect\":0,\"readonly\":true,\"size\":64,\"auto\":false,\"cur\":\"false\",\"default\":\"false\"},\"logout\":{\"cat\":\"advanced\",\"group\":\"\\u53ea\\u8bfb\\u5c5e\\u6027\",\"title\":\"\\u767b\\u51fa\",\"desc\":\"\\u7528\\u6237\\u767b\\u51fa\\u64cd\\u4f5c\",\"ver\":0,\"pos\":0,\"fix-id\":39169,\"type\":\"string\",\"unit\":\"none\",\"affect\":0,\"readonly\":true,\"size\":64,\"auto\":false,\"cur\":\"false\",\"default\":\"false\"},\"drv-log\":{\"cat\":\"advanced\",\"group\":\"\\u9ad8\\u7ea7\\u8bbe\\u7f6e\",\"title\":\"\\u9a71\\u52a8\\u65e5\\u5fd7\",\"desc\":\"PC\\u7aef\\u9a71\\u52a8\\u5de5\\u4f5c\\u65e5\\u5fd7\",\"ver\":0,\"pos\":0,\"fix-id\":39171,\"type\":\"string\",\"unit\":\"none\",\"affect\":0,\"size\":256,\"auto\":false,\"cur\":\"0\",\"default\":\"0\"}}" }; //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // offline_opts ... offline_opts::offline_opts(hguser* user) : user_(user) { int hh = 0, hl = 0, lh = 0, ll = 0; hg_scanner_mgr::get_version(&hh, &hl, &lh, &ll); drv_ver_ = std::to_string(hh) + "." + std::to_string(hl) + "." + std::to_string(lh) + "." + std::to_string(ll); init(); } offline_opts::~offline_opts() { } void offline_opts::init(void) { gb_json* child = nullptr, * jsn_ = new gb_json(); std::string text(""); for (auto& v : device_opt_json) text += v; jsn_->attach_text(&text[0]); #define INIT_SIMPLE_VAL(n, v) \ jsn_->get_value(SANE_STD_OPT_NAME_##n, child); \ if (child) \ { \ auto func = [this](bool read, void* buf, int* len, bool curval) -> int \ { \ if (!read) \ return SCANNER_ERR_DEVICE_NOT_SUPPORT; \ if (!len) \ return SCANNER_ERR_INVALID_PARAMETER; \ \ std::string val(from_default_language(v)); \ if (*len < val.length()) \ { \ *len = val.length() + 1; \ \ return SCANNER_ERR_INSUFFICIENT_MEMORY; \ } \ \ strcpy((char*)buf, val.c_str()); \ \ return SCANNER_ERR_OK; \ }; \ oper_[SANE_STD_OPT_NAME_##n] = func; \ \ child->release(); \ } INIT_SIMPLE_VAL(MANUFACTURER, COMPANY_NAME); INIT_SIMPLE_VAL(CO_URL, BRAND_COMPANY_URL); INIT_SIMPLE_VAL(CO_TEL, BRAND_COMPANY_TEL); INIT_SIMPLE_VAL(CO_ADDR, BRAND_COMPANY_ADDRESS); INIT_SIMPLE_VAL(CO_GPS, BRAND_URL_GPS); INIT_SIMPLE_VAL(COPYRIGHT, BRAND_COPYRIGHT); INIT_SIMPLE_VAL(DRIVER_VERSION, drv_ver_.c_str()); // driver log jsn_->get_value(SANE_STD_OPT_NAME_DRIVER_LOG, child); if (child) { auto func = [this](bool read, void* buf, int* len, bool curval) -> int { if (user_ && user_->has_privilege(USER_PRIVILEGE_LOCAL_MGR)) { if (read) { if (!buf) return SCANNER_ERR_INVALID_PARAMETER; return utils::copy_log_file_to((char*)buf); } else { return utils::clear_log_file(); } } else { return SCANNER_ERR_ACCESS_DENIED; } }; oper_[SANE_STD_OPT_NAME_DRIVER_LOG] = func; child->release(); } // login && logout jsn_->get_value(SANE_STD_OPT_NAME_LOGIN, child); if (child) { if (user_) { auto login = [this](bool read, void* buf, int* len, bool curval) -> int { if (read) return SCANNER_ERR_DEVICE_NOT_SUPPORT; if (!buf) return SCANNER_ERR_INVALID_PARAMETER; char* n = (char*)buf, * pwd = n + 32; return user_->login(n, pwd); }; auto logout = [this](bool read, void* buf, int* len, bool curval) -> int { if (read) return SCANNER_ERR_DEVICE_NOT_SUPPORT; if (!buf) return SCANNER_ERR_INVALID_PARAMETER; char* n = (char*)buf, * pwd = n + 32; return user_->logout(n, pwd); }; oper_[SANE_STD_OPT_NAME_LOGIN] = login; oper_[SANE_STD_OPT_NAME_LOGOUT] = logout; } child->release(); } jsn_->release(); } std::string offline_opts::get_json_text(void) { std::string text(""); for (auto& v : device_opt_json) text += v; return std::move(text); } int offline_opts::get_value(const char* name, void* buf, int* len, bool curval) { int ret = SCANNER_ERR_NO_DATA; if (oper_.count(name)) { ret = oper_[name](true, buf, len, curval); } return ret; } int offline_opts::set_value(const char* name, void* value) { int ret = SCANNER_ERR_NO_DATA; if (oper_.count(name)) { ret = oper_[name](false, value, nullptr, true); } return ret; } bool offline_opts::has_option(const char* name) { return oper_.count(name) > 0; }