2022-07-18 02:54:41 +00:00
# pragma once
2022-05-03 03:56:07 +00:00
# include <signal.h>
# include <string.h>
# include <stdlib.h>
# include <vector>
# include <algorithm>
# include <string>
# include <stdio.h>
2022-07-18 08:56:03 +00:00
# if defined(WIN32) || defined(_WIN64)
2022-05-03 03:56:07 +00:00
# define bzero(b, s) memset(b, 0, s)
# else
# include <iconv.h>
# endif
# include "../sdk/hginclude/huagaoxxx_warraper_ex.h"
# include "sane/sane_ex.h"
# define ENABLE_SLAVE_OPTION_CONTROL
# define ALIGN_INT(n) ((((n) + sizeof(int) - 1) / sizeof(int)) * sizeof(int))
2022-05-17 07:04:55 +00:00
# ifdef OEM_HANWANG
# define hg_sane_middleware hw_sane_middleware
# elif defined(OEM_LISICHENG)
# define hg_sane_middleware lsc_sane_middleware
# endif
2022-05-03 03:56:07 +00:00
class json ;
2022-05-23 03:52:53 +00:00
class sane_std_opts ;
2022-05-03 03:56:07 +00:00
class hg_sane_middleware
{
typedef struct _device_option
{
std : : string dev_name ;
int option_no ;
2022-11-10 02:02:52 +00:00
std : : string opt_name ;
2022-05-03 03:56:07 +00:00
SANE_Option_Descriptor * desc ;
} DEVOPT ;
std : : vector < DEVOPT > opts_ ;
2022-05-20 06:46:25 +00:00
SANE_Option_Descriptor * opt_0_ ;
2022-05-23 03:52:53 +00:00
sane_std_opts * std_opt_ ;
2022-05-03 03:56:07 +00:00
typedef struct _openning_scanner_option
{
std : : string dev_name ;
scanner_handle handle ;
int scan_count ;
//struct _openning_scanner_option()
//{
// dev_name = "";
// handle = NULL;
// option_no = 0;
// desc = NULL;
//}
bool operator = = ( const char * name )
{
return strcmp ( dev_name . c_str ( ) , name ) = = 0 ;
}
bool operator = = ( scanner_handle h )
{
return handle = = h ;
}
} OPENDEV ;
std : : vector < OPENDEV > openning_ ;
static hg_sane_middleware * inst_ ;
static const SANE_Device * * dev_list_ ;
2022-05-17 07:04:55 +00:00
static const SANE_Device * * to_sane_device ( ScannerInfo * hgscanner , int count ) ; // 将驱动层传回的设备列表数据, 转换为标准SANE协议的设备列表
2022-05-03 03:56:07 +00:00
static void free_sane_device ( SANE_Device * * dev ) ; // 释放由to_sane_device返回的指针
static void device_pnp ( int sig ) ; // 热插拔事件监控
static SANE_Fixed double_2_sane_fixed ( double v ) ;
static double sane_fixed_2_double ( SANE_Fixed v ) ;
2022-05-18 09:41:05 +00:00
void on_device_closed ( scanner_handle h ) ;
2022-05-03 03:56:07 +00:00
SANE_Status open ( SANE_String_Const devicename , SANE_Handle * handle , const char * name , const char * pwd , const char * method , char * rsc ) ;
2022-11-10 02:02:52 +00:00
SANE_Option_Descriptor * from_json ( scanner_handle h , const std : : string & name , json * jsn , int opt_no ) ;
2022-05-03 03:56:07 +00:00
scanner_handle find_openning_device ( SANE_Handle h , bool rmv = false , OPENDEV * dev = NULL ) ;
2022-11-11 09:59:08 +00:00
std : : string get_option_json ( scanner_handle handle , void * opt , std : : string * key = nullptr ) ;
2022-11-16 10:20:10 +00:00
SANE_Option_Descriptor * find_stored_descriptor ( const char * name , unsigned long long option ) ;
SANE_Option_Descriptor * find_stored_descriptor ( SANE_Handle handle , unsigned long long option ) ;
2022-05-03 03:56:07 +00:00
2022-11-10 02:02:52 +00:00
void reload_current_value ( scanner_handle handle , std : : vector < std : : string > * changed = NULL ) ;
2022-06-15 03:05:36 +00:00
bool get_current_value ( scanner_handle handle , int option , void * value , SANE_Value_Type * type = NULL ) ;
2022-11-10 03:09:16 +00:00
void * get_default_value ( scanner_handle handle , int option , int * bytes = nullptr , bool log = false ) ; // caller should call local_utility::free_memory to free the returned value
2022-05-03 03:56:07 +00:00
/// <summary>
/// 关联项处理
// 添加对多依赖项的支持 - 2022-03-10
typedef struct _cur_val
{
2022-11-10 02:02:52 +00:00
std : : string name ;
2022-05-03 03:56:07 +00:00
std : : string type ;
std : : string val ; /*参数全部字符串化*/
2022-11-10 02:02:52 +00:00
bool operator = = ( const std : : string & n )
2022-05-03 03:56:07 +00:00
{
2022-11-10 02:02:52 +00:00
return name = = n ;
2022-05-03 03:56:07 +00:00
}
} CURVAL ;
std : : vector < CURVAL > cur_vals_ ;
2022-11-10 02:02:52 +00:00
bool refresh_current_value ( const char * name , json * jsn ) ; // return whether changed old value
bool refresh_current_value ( const char * name , const char * val ) ;
2022-05-03 03:56:07 +00:00
static bool compare_val_equal ( const char * cur_val , const char * limit_l , const char * limit_r ) ;
static bool compare_val_not_equal ( const char * cur_val , const char * limit_l , const char * limit_r ) ;
static bool compare_val_great ( const char * cur_val , const char * limit_l , const char * limit_r ) ;
static bool compare_val_not_less ( const char * cur_val , const char * limit_l , const char * limit_r ) ;
static bool compare_val_less ( const char * cur_val , const char * limit_l , const char * limit_r ) ;
static bool compare_val_not_great ( const char * cur_val , const char * limit_l , const char * limit_r ) ;
static bool compare_val_between ( const char * cur_val , const char * limit_l , const char * limit_r ) ;
static bool compare_val_not_between ( const char * cur_val , const char * limit_l , const char * limit_r ) ;
typedef struct _master_option
{
2022-11-10 02:02:52 +00:00
std : : string name ;
2022-05-03 03:56:07 +00:00
SANE_Value_Type type ;
std : : string limit_l ;
std : : string limit_r ;
bool ( * compare_val ) ( const char * cur_val , const char * limit_l , const char * limit_r ) ; // ==, !=, >, >=, <, <=, [l, r], ![l, r]
} MASTEROP ;
static bool is_enable_and ( const std : : vector < MASTEROP > & master , std : : vector < CURVAL > & curvals ) ;
static bool is_enable_or ( const std : : vector < MASTEROP > & master , std : : vector < CURVAL > & curvals ) ;
typedef struct _slave_option
{
2022-11-10 02:02:52 +00:00
std : : string name ;
2022-05-03 03:56:07 +00:00
bool enable_now ;
std : : vector < MASTEROP > master ;
bool ( * is_enable ) ( const std : : vector < MASTEROP > & master
, std : : vector < CURVAL > & curvals /*参数全部字符串化*/ ) ; // logic 'and', 'or' opertions
} SLAVEOP ;
2022-11-10 02:02:52 +00:00
std : : vector < SLAVEOP > slave_options_ ;
std : : vector < std : : string > master_options_ ;
2022-05-03 03:56:07 +00:00
bool parse_master_option ( const char * depend_str , MASTEROP & mo ) ;
bool parse_depends ( json * jsn , SLAVEOP & so ) ;
typedef struct _opt_enabled
{
2022-11-10 02:02:52 +00:00
std : : string name ;
bool enable ;
2022-05-03 03:56:07 +00:00
2022-11-10 02:02:52 +00:00
bool operator = = ( const std : : string & opt )
2022-05-03 03:56:07 +00:00
{
2022-11-10 02:02:52 +00:00
return name = = opt ;
2022-05-03 03:56:07 +00:00
}
} OPTENABLE ;
2022-11-10 02:02:52 +00:00
bool is_associatived ( const SLAVEOP & slave , const char * master_name ) ;
2022-11-16 10:20:10 +00:00
bool set_stored_option_enabled ( const char * dev_name , unsigned long long option , bool enable , int * size = NULL ) ;
2022-11-10 02:02:52 +00:00
int something_after_do ( scanner_handle h , const char * dev_name , const char * master_name , const char * cur_val ) ;
2022-05-03 03:56:07 +00:00
OPTEN * get_control_enalbe_data ( const SLAVEOP & slave ) ;
void free_control_enable_data ( OPTEN * opt ) ;
2022-05-17 07:04:55 +00:00
void on_SCANNER_ERR_CONFIGURATION_CHANGED ( scanner_handle handle , const char * dev_name ) ;
2022-05-03 03:56:07 +00:00
/// 关联项处理结束
protected :
hg_sane_middleware ( void ) ;
~ hg_sane_middleware ( ) ;
public :
2022-06-16 08:03:55 +00:00
static std : : string sane_path ( void ) ;
2022-05-03 03:56:07 +00:00
static hg_sane_middleware * instance ( void ) ;
2022-06-18 00:55:15 +00:00
static void set_callback ( sane_callback cb , void * param ) ;
2022-05-03 03:56:07 +00:00
static void clear ( void ) ;
2022-11-19 00:48:01 +00:00
static std : : string option_value_2_string ( SANE_Value_Type type , void * val ) ;
2022-05-03 03:56:07 +00:00
static scanner_handle sane_handle_to_scanner ( SANE_Handle h ) ;
static SANE_Handle scanner_handle_to_sane ( scanner_handle h ) ;
static SANE_Option_Descriptor * string_option_to_SANE_descriptor ( const char * name , const char * title , const char * desc
, const std : : vector < std : : string > & values ) ;
static SANE_Option_Descriptor * number_option_to_SANE_descriptor ( const char * name , const char * title , const char * desc
, bool double_val , double * lower , double * upper ) ; // NO constraint if lower or upper were NULL
static SANE_Option_Descriptor * number_option_to_SANE_descriptor ( const char * name , const char * title , const char * desc
, const std : : vector < int > & values ) ; // NO constraint if values was empty
static SANE_Option_Descriptor * number_option_to_SANE_descriptor ( const char * name , const char * title , const char * desc
, const std : : vector < double > & values ) ; // NO constraint if values was empty
// methods ...
public :
SANE_Status get_devices ( const SANE_Device * * * device_list , SANE_Bool local_only ) ;
SANE_Status open_device ( SANE_String_Const devicename , SANE_Handle * handle ) ;
SANE_Status close_device ( SANE_Handle h ) ;
SANE_Status get_image_parameters ( SANE_Handle handle , SANE_Parameters * params ) ;
SANE_Status start ( SANE_Handle h , void * async_event ) ;
SANE_Status read ( SANE_Handle h , void * buf , int * bytes ) ;
SANE_Status stop ( SANE_Handle h ) ;
SANE_Option_Descriptor * get_option_descriptor ( SANE_Handle h , SANE_Int option ) ;
SANE_Status set_option ( SANE_Handle h , SANE_Int option , SANE_Action action , void * value , SANE_Int * after_do ) ;
2022-09-26 09:30:44 +00:00
bool get_cur_value ( SANE_Handle handle , int option , void * value , SANE_Value_Type * type = NULL ) ; // SANE_type
2022-11-10 03:09:16 +00:00
void * get_def_value ( SANE_Handle handle , int option , int * bytes = nullptr , bool log = false ) ; // caller should call local_utility::free_memory to free the returned value, SANE_type
2022-05-03 03:56:07 +00:00
// extension ...
2022-11-11 09:59:08 +00:00
SANE_Status io_control ( SANE_Handle h , unsigned long code , void * data , unsigned * len ) ;
2022-05-03 03:56:07 +00:00
public :
} ;
2022-06-15 03:05:36 +00:00
namespace local_utility
{
void free_memory ( void * m ) ;
int sane_statu_2_scanner_err ( int statu ) ;
}