From 00c794b9c12ce670baf1b30b41b11ec3c71a72e4 Mon Sep 17 00:00:00 2001 From: zls <1026904535@qq.com> Date: Fri, 27 Dec 2024 18:00:38 +0800 Subject: [PATCH] =?UTF-8?q?sane=E6=A1=86=E6=9E=B6=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: zls <1026904535@qq.com> --- BUILD.gn | 54 +- OAT.xml | 135 ---- README.md | 120 --- README_zh.md | 120 --- backend/dll.c | 43 - bundle.json | 26 +- include/_stdint.h | 8 + include/sane/config.h | 15 +- include/sane/sanei_debug.h | 2 + install.py | 51 ++ patches/modifying_driver_search_path.patch | 113 +++ patches/patches.json | 9 - sanei/sanei_thread.c | 5 +- sanei/sanei_usb.c | 863 ++------------------- sanei/usb_manager.cpp | 671 ---------------- 15 files changed, 298 insertions(+), 1937 deletions(-) delete mode 100644 OAT.xml delete mode 100644 README.md delete mode 100644 README_zh.md create mode 100644 include/_stdint.h create mode 100644 install.py create mode 100644 patches/modifying_driver_search_path.patch delete mode 100644 patches/patches.json delete mode 100644 sanei/usb_manager.cpp diff --git a/BUILD.gn b/BUILD.gn index 73be78a03..8e71ce574 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -1,4 +1,4 @@ -#Copyright (c) 2024 Huawei Device Co., Ltd. +#Copyright (c) 2021 Huawei Device Co., Ltd. #Licensed under the Apache License, Version 2.0 (the "License"); #you may not use this file except in compliance with the License. #You may obtain a copy of the License at @@ -21,6 +21,23 @@ SANE_LIB_DIR = "/data/service/el1/public/print_service/sane/backend" SANE_V_MAJOR = 1 SANE_V_MINOR = 2 enable_hilog = true +source_dir = "//third_party/backends" +target_dir = "${target_gen_dir}/sane" + +action("backends_action") { + print("backends_action is exec") + script = "//third_party/backends/install.py" + inputs = [ "${source_dir}/patches/modifying_driver_search_path.patch" ] + outputs = [ + "${target_dir}/include/config.h", + "${target_dir}/src/dll.c", + ] + backends_source_dir = rebase_path("${source_dir}", root_build_dir) + args = [ + "--source-dir", + "$backends_source_dir", + ] +} config("backends_public_config") { include_dirs = [ @@ -99,6 +116,23 @@ sanei_names = [ "sanei_jpeg", ] +ohos_source_set("sanei_usb") { + sources = [ "./sanei/sanei_usb.c" ] + + external_deps = [ "libusb:libusb" ] + + if (enable_hilog) { + external_deps += [ "hilog:libhilog" ] + } + + public_configs = [ ":backends_public_config" ] + + configs = [ ":backends_private_config" ] + + subsystem_name = "thirdparty" + part_name = "backends" +} + foreach(name, sanei_names) { ohos_source_set("$name") { sources = [ "./sanei/$name.c" ] @@ -116,23 +150,6 @@ foreach(name, sanei_names) { } } -ohos_source_set("sanei_usb") { - sources = [ "./sanei/sanei_usb.c" ] - - external_deps = [ - "hilog:libhilog", - "libusb:libusb", - "libxml2:libxml2", - ] - - public_configs = [ ":backends_public_config" ] - - configs = [ ":backends_private_config" ] - - subsystem_name = "thirdparty" - part_name = "backends" -} - ohos_static_library("sanei") { sources = [] @@ -177,6 +194,7 @@ ohos_shared_library("sane") { defines = [ "BACKEND_NAME=dll" ] deps = [ + ":backends_action", ":lib", ":sane_strstatus", ":sanei_config", diff --git a/OAT.xml b/OAT.xml deleted file mode 100644 index bc5081b97..000000000 --- a/OAT.xml +++ /dev/null @@ -1,135 +0,0 @@ - - - - - - - LICENSE - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/README.md b/README.md deleted file mode 100644 index 305ca07e9..000000000 --- a/README.md +++ /dev/null @@ -1,120 +0,0 @@ -# SANE -## Introduction -SANE is an application programming interface (API) that provides standardized access to any raster image scanner hardware (flatbed scanner, hand-held scanner, video- and still-cameras, frame-grabbers, etc.). - -You can also learn more about the SANE project through [the official website](http://sane-project.org/) - -## Background Brief -In the process of OpenHarmony's southward ecological development, it is necessary to be compatible with printers in the stock market. The use of CUPS printing system can directly connect with most printers in the market, which also reduces the difficulty for printer manufacturers to adapt to OpenHarmony. - -## Directory structure -``` -- LICENSE Copyright File -- OAT.xml OAT.XML filtering configuration file -- README.OpenSource Project README OpenSource files -- README.md English Description -- README_zh.md Chinese Description -- backend scanning device backend source code -- include SANE API interface -- lib SANE library source code -- sanei SANE internal utility functions and tools -- doc documents and instruction files -``` - -## How to use -### 1、Header file import -```c -#include -``` -### 2、Add Compilation Dependency -Add in the bundle. json file -```json -"deps": { - "third_party": [ - "backends" - ] -} -``` -Add dependencies where needed in BUILD.gn - -```json -deps += [ "//third_party/backends:third_sane" ] -``` -### 3、Example of interface usage -```c -SANE_Status status; -SANE_Handle handle; - -// Initialize SANE -status = sane_init(NULL, NULL); -if (status != SANE_STATUS_GOOD) { - fprintf(stderr, "Failed to initialize SANE: %s\n", sane_strstatus(status)); - return 1; -} - -// Open the first scanner device -status = sane_open("your_scanner_device_name", &handle); -if (status != SANE_STATUS_GOOD) { - fprintf(stderr, "Failed to open scanner: %s\n", sane_strstatus(status)); - return 1; -} - -// Get scanner device information -const SANE_Device *device_info; -status = sane_get_devices(&device_info, SANE_FALSE); -if (status != SANE_STATUS_GOOD) { - fprintf(stderr, "Failed to get scanner device information: %s\n", sane_strstatus(status)); - return 1; -} - -// Set scan parameters -SANE_Parameters parameters; -status = sane_get_parameters(handle, ¶meters); -if (status != SANE_STATUS_GOOD) { - fprintf(stderr, "Failed to get scan parameters: %s\n", sane_strstatus(status)); - return 1; -} - -// Start scanning -SANE_Image image; -status = sane_start(handle); -if (status != SANE_STATUS_GOOD) { - fprintf(stderr, "Failed to start scanning: %s\n", sane_strstatus(status)); - return 1; -} - -// Read scan data -do { - status = sane_read(handle, &image); - if (status != SANE_STATUS_GOOD) { - fprintf(stderr, "Failed to read scan data: %s\n", sane_strstatus(status)); - break; - } - -} while (status == SANE_STATUS_GOOD); - -// Finish scanning -status = sane_cancel(handle); -if (status != SANE_STATUS_GOOD) { - fprintf(stderr, "Failed to cancel scanning: %s\n", sane_strstatus(status)); - return 1; -} - -// Close the scanner device -status = sane_close(handle); -if (status != SANE_STATUS_GOOD) { - fprintf(stderr, "Failed to close scanner: %s\n", sane_strstatus(status)); - return 1; -} - -// Exit SANE -sane_exit(); -``` - -### 相关仓 -[print_print_fwk](https://gitee.com/openharmony/print_print_fwk) - -### 参与贡献 -[How to involve](https://gitee.com/openharmony/docs/blob/HEAD/zh-cn/contribute/参与贡献.md) - -[Commit message spec](https://gitee.com/openharmony/device_qemu/wikis/Commit%20message%E8%A7%84%E8%8C%83) \ No newline at end of file diff --git a/README_zh.md b/README_zh.md deleted file mode 100644 index e7146b642..000000000 --- a/README_zh.md +++ /dev/null @@ -1,120 +0,0 @@ -# 三方开源软件sane-backends -## SANE简介 -SANE是一个应用程序编程接口(API),它提供对任何光栅图像扫描仪硬件(平板扫描仪、手持式扫描仪、视频和静态摄像机、帧处理器等)的标准化访问。 - -您也可以通过[SANE官网主页](http://sane-project.org/)了解更多关于SANE项目的信息。 - -## 引入背景简述 -OpenHarmony南向生态发展过程中,需要对存量市场的扫描仪进行兼容。使用SANE扫描系统能直接对接市场上大部分的扫描仪,也减少了扫描仪驱动适配OpenHarmony系统的难度。 - -## 目录结构 -``` -- LICENSE 版权文件 -- OAT.xml OAT.xml过滤配置文件 -- README.OpenSource 项目README.OpenSource文件 -- README.md 英文说明 -- README_zh.md 中文说明 -- backend 扫描设备后端源码 -- include SANE API接口 -- lib SANE 库源码 -- sanei SANE 内部实用函数和工具 -- doc 文档和说明文件 -``` - -## 如何使用 -### 1、头文件引入 -```c -#include -``` -### 2、添加编译依赖 -在您的 bundle.json 文件 添加 -```json -"deps": { - "third_party": [ - "backends" - ] -} -``` -在您的BUILD.gn需要的地方添加依赖 -```json -deps += [ "//third_party/backends:third_sane" ] -include_dirs += [ "//third_party/backends/include" ] -``` -```c -SANE_Status status; -SANE_Handle handle; - -// Initialize SANE -status = sane_init(NULL, NULL); -if (status != SANE_STATUS_GOOD) { - fprintf(stderr, "Failed to initialize SANE: %s\n", sane_strstatus(status)); - return 1; -} - -// Open the first scanner device -status = sane_open("your_scanner_device_name", &handle); -if (status != SANE_STATUS_GOOD) { - fprintf(stderr, "Failed to open scanner: %s\n", sane_strstatus(status)); - return 1; -} - -// Get scanner device information -const SANE_Device *device_info; -status = sane_get_devices(&device_info, SANE_FALSE); -if (status != SANE_STATUS_GOOD) { - fprintf(stderr, "Failed to get scanner device information: %s\n", sane_strstatus(status)); - return 1; -} - -// Set scan parameters -SANE_Parameters parameters; -status = sane_get_parameters(handle, ¶meters); -if (status != SANE_STATUS_GOOD) { - fprintf(stderr, "Failed to get scan parameters: %s\n", sane_strstatus(status)); - return 1; -} - -// Start scanning -SANE_Image image; -status = sane_start(handle); -if (status != SANE_STATUS_GOOD) { - fprintf(stderr, "Failed to start scanning: %s\n", sane_strstatus(status)); - return 1; -} - -// Read scan data -do { - status = sane_read(handle, &image); - if (status != SANE_STATUS_GOOD) { - fprintf(stderr, "Failed to read scan data: %s\n", sane_strstatus(status)); - break; - } - -} while (status == SANE_STATUS_GOOD); - -// Finish scanning -status = sane_cancel(handle); -if (status != SANE_STATUS_GOOD) { - fprintf(stderr, "Failed to cancel scanning: %s\n", sane_strstatus(status)); - return 1; -} - -// Close the scanner device -status = sane_close(handle); -if (status != SANE_STATUS_GOOD) { - fprintf(stderr, "Failed to close scanner: %s\n", sane_strstatus(status)); - return 1; -} - -// Exit SANE -sane_exit(); -``` - -### 相关仓 -[print_print_fwk](https://gitee.com/openharmony/print_print_fwk) - -### 参与贡献 -[如何贡献](https://gitee.com/openharmony/docs/blob/HEAD/zh-cn/contribute/参与贡献.md) - -[Commit message规范](https://gitee.com/openharmony/device_qemu/wikis/Commit%20message%E8%A7%84%E8%8C%83) - diff --git a/backend/dll.c b/backend/dll.c index 904792c19..bf34c4f6d 100644 --- a/backend/dll.c +++ b/backend/dll.c @@ -360,47 +360,6 @@ dyld_get_error_str () } #endif -#if defined(HAVE_SCAN_SERVICE) -static int has_suffix(const char *filename, const char *suffix) { - size_t len_filename = strlen(filename); - size_t len_suffix = strlen(suffix); - return len_suffix <= len_filename && strcmp(filename + len_filename - len_suffix, suffix) == 0; -} - -static void find_libname_by_drivername(char* libname, char* dir, char* drivername) -{ - DBG(1, "%s: begin", __func__); - if (libname == NULL || dir == NULL || drivername == NULL) - { - DBG(2, "%s: input parameter is a nullptr.\n", __func__); - return; - } - char driver[PATH_MAX] = {0}; - snprintf (driver, sizeof (driver), "libsane-%s.z.so", drivername); - DIR *backends_dir = opendir(dir); - if (backends_dir == NULL) { - DBG(2, "open dir %s error\n", backends_dir); - return; - } - struct dirent *entry; - char full_path[PATH_MAX] = {0}; - while ((entry = readdir(backends_dir)) != NULL) { - // ignore '.' and '..' - if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) { - memset(full_path, 0, PATH_MAX); - snprintf(full_path, PATH_MAX, "%s/" "%s", dir, entry->d_name); - DBG(2, "full_path %s\n", full_path); - if (has_suffix(full_path, driver)) { - memset(libname, 0, PATH_MAX); - strncpy(libname, full_path, PATH_MAX); - break; - } - } - } - closedir(backends_dir); -} -#endif - #ifdef __BEOS__ #include @@ -539,8 +498,6 @@ load (struct backend *be) snprintf (libname, sizeof (libname), "%s/" PREFIX "%.2s%.5s" POSTFIX, dir, be->name, strlen(be->name)>7 ? (be->name)+strlen(be->name)-5 : (be->name)+2, V_MAJOR); -#elif defined (HAVE_SCAN_SERVICE) - find_libname_by_drivername(libname, dir, be->name); #else snprintf (libname, sizeof (libname), "%s/" PREFIX "%s" POSTFIX, dir, be->name, V_MAJOR); diff --git a/bundle.json b/bundle.json index e301f0f2c..63939b74b 100644 --- a/bundle.json +++ b/bundle.json @@ -1,30 +1,34 @@ { "name": "@ohos/backends", - "description": "backends(Scanner Access Now Easy) is a framework for standardized access to any raster scanner", + "description": "SANE(Scanner Access Now Easy) is a framework for standardized access to any raster scanner", "version": "1.2.1", "license": "GPL v2", - "publishAs": "code-segment", - "segment": { - "destPath": "third_party/backends" + "publishAs":"code-segment", + "segment":{ + "destPath":"third_party/backends" }, - "dirs": {}, - "scripts": {}, + "dirs":{}, + "scripts":{}, + "licensePath": "COPYING", "component": { "name": "backends", "subsystem": "thirdparty", "syscap": [], "features": [], - "adapted_system_type": "standard", + "adapted_system_type": [ + "standard" + ], "rom": "10KB", "ram": "10KB", "deps": { "components": [ "hilog", - "usb_manager", - "bounds_checking_function", - "c_utils", - "libxml2", "libusb" + ], + "third_party": [ + "libxml2", + "libpng", + "libjpeg-turbo" ] }, "build": { diff --git a/include/_stdint.h b/include/_stdint.h new file mode 100644 index 000000000..f8a0ed15f --- /dev/null +++ b/include/_stdint.h @@ -0,0 +1,8 @@ +#ifndef _SANE_BACKENDS_INCLUDE__STDINT_H +#define _SANE_BACKENDS_INCLUDE__STDINT_H 1 +#ifndef _GENERATED_STDINT_H +#define _GENERATED_STDINT_H "sane-backends 1.2.1" +#define _STDINT_HAVE_STDINT_H 1 +#include +#endif +#endif \ No newline at end of file diff --git a/include/sane/config.h b/include/sane/config.h index c99202bba..e56704009 100644 --- a/include/sane/config.h +++ b/include/sane/config.h @@ -209,14 +209,11 @@ /* Define to 1 if you have libusb-1.0 */ #define HAVE_LIBUSB 1 -/* Define to 1 if you have usb_manager */ -// #define HAVE_USB_MANAGER 1 - /* Define to 1 if you have libusb-0.1 */ /* #undef HAVE_LIBUSB_LEGACY */ /* Define to 1 if libxml2 is available */ -#define HAVE_LIBXML2 1 +// #define HAVE_LIBXML2 1 /* Define to 1 if you have the header file. */ #define HAVE_LIMITS_H 1 @@ -257,9 +254,6 @@ /* Define to 1 if you have the header file. */ /* #undef HAVE_OS2_H */ -/* Define to 1 if the scanner driver is accessed through the scan service. */ -#define HAVE_SCAN_SERVICE 1 - /* Define to 1 if you have the `poll' function. */ #define HAVE_POLL 1 @@ -326,7 +320,7 @@ /* Define to 1 if you have the `snprintf' function. */ #define HAVE_SNPRINTF 1 -/* Define to 1 if you have the header file. */ +/* Define to 1 if you have the <_stdint.h> header file. */ #define HAVE_STDINT_H 1 /* Define to 1 if you have the header file. */ @@ -474,7 +468,7 @@ #define HAVE_U_SHORT 1 /* Define to 1 if you have the `vsyslog' function. */ -// #define HAVE_VSYSLOG 1 +//#define HAVE_VSYSLOG 1 /* Define to 1 if you have the header file. */ #define HAVE_WCHAR_H 1 @@ -655,9 +649,6 @@ /* Define to 1 if Avahi support is available */ #define WITH_AVAHI 1 -/* define if USB record replay is enabled */ -#define WITH_USB_RECORD_REPLAY 1 - /* Define WORDS_BIGENDIAN to 1 if your processor stores words with the most significant byte first (like Motorola and SPARC, unlike Intel). */ #if defined AC_APPLE_UNIVERSAL_BUILD diff --git a/include/sane/sanei_debug.h b/include/sane/sanei_debug.h index f948f7dec..030a33c10 100644 --- a/include/sane/sanei_debug.h +++ b/include/sane/sanei_debug.h @@ -9,6 +9,7 @@ #include +#include #include "hilog/log.h" #ifdef __cplusplus extern "C" { @@ -77,6 +78,7 @@ extern "C" { #ifdef NDEBUG extern void sanei_debug_ndebug (int level, const char *msg, ...); +//extern void hilog_dbg (int level, const char *msg, ...); # define DBG_LEVEL (0) # define DBG_INIT() diff --git a/install.py b/install.py new file mode 100644 index 000000000..1e12b2a27 --- /dev/null +++ b/install.py @@ -0,0 +1,51 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +# Copyright (c) 2024 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import tarfile +import argparse +import os +import subprocess +import sys +import shutil + + +def apply_patch(source_dir): + patch_list = [ + 'modifying_driver_search_path.patch' + ] + + for patch in patch_list: + patch_dir = os.path.join(source_dir, 'patches', patch) + patch_dir = os.path.abspath(patch_dir) + source_dir = os.path.abspath(source_dir) + patch_cmd = ['patch', '-p1', "--fuzz=0", "--no-backup-if-mismatch", '-i', patch_dir, '-d', source_dir] + try: + result = subprocess.run(patch_cmd, check=True, text=True, capture_output=True) + if result.returncode != 0: + raise subprocess.CalledProcessError(result.returncode, result.args) + except subprocess.CalledProcessError: + print("sane-backends: {} apply error!".format(patch)) + +def main(): + backends_path = argparse.ArgumentParser() + backends_path.add_argument('--source-dir', help='generate path of log', required=True) + args = backends_path.parse_args() + source_dir = args.source_dir + apply_patch(source_dir) + return 0 + + +if __name__ == '__main__': + sys.exit(main()) \ No newline at end of file diff --git a/patches/modifying_driver_search_path.patch b/patches/modifying_driver_search_path.patch new file mode 100644 index 000000000..8d975ed0e --- /dev/null +++ b/patches/modifying_driver_search_path.patch @@ -0,0 +1,113 @@ +diff --git a/backend/dll.c b/backend/dll.c +index bf34c4f6d..7cb7fd508 100644 +--- a/backend/dll.c ++++ b/backend/dll.c +@@ -292,6 +292,46 @@ static const char *op_name[] = { + }; + #endif /* __BEOS__ */ + ++#if defined(HAVE_SCAN_SERVICE) ++static int has_suffix(const char *filename, const char *suffix) ++{ ++ size_t len_filename = strlen(filename); ++ size_t len_suffix = strlen(suffix); ++ return len_suffix <= len_filename && strcmp(filename + len_filename - len_suffix, suffix) == 0; ++} ++ ++static void find_libname_by_drivername(char* libname, char* dir, char* drivername) ++{ ++ DBG(1, "%s: begin", __func__); ++ if (libname == NULL || dir == NULL || drivername == NULL) { ++ DBG(2, "%s: input parameter is a nullptr.\n", __func__); ++ return; ++ } ++ char driver[PATH_MAX] = {0}; ++ snprintf (driver, sizeof (driver), "libsane-%s.z.so", drivername); ++ DIR *backends_dir = opendir(dir); ++ if (backends_dir == NULL) { ++ DBG(2, "open dir %s error\n", backends_dir); ++ return; ++ } ++ struct dirent *entry; ++ char full_path[PATH_MAX] = {0}; ++ while ((entry = readdir(backends_dir)) != NULL) { ++ if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) { ++ memset(full_path, 0, PATH_MAX); ++ snprintf(full_path, PATH_MAX, "%s/" "%s", dir, entry->d_name); ++ DBG(1, "full_path %s\n", full_path); ++ if (has_suffix(full_path, driver)) { ++ memset(libname, 0, PATH_MAX); ++ strncpy(libname, full_path, PATH_MAX); ++ break; ++ } ++ } ++ } ++ closedir(backends_dir); ++} ++#endif /* HAVE_SCAN_SERVICE */ ++ + static void * + op_unsupported (void) + { +@@ -498,6 +538,8 @@ load (struct backend *be) + snprintf (libname, sizeof (libname), "%s/" PREFIX "%.2s%.5s" POSTFIX, + dir, be->name, strlen(be->name)>7 ? (be->name)+strlen(be->name)-5 : + (be->name)+2, V_MAJOR); ++#elif defined (HAVE_SCAN_SERVICE) ++ find_libname_by_drivername(libname, dir, be->name); + #else + snprintf (libname, sizeof (libname), "%s/" PREFIX "%s" POSTFIX, + dir, be->name, V_MAJOR); +@@ -807,6 +849,26 @@ read_config (const char *conffile) + fclose (fp); + } + ++#if defined(HAVE_SCAN_SERVICE) ++static void ++read_configs(const char *conffile) ++{ ++ const char* dir = "/data/service/el1/public/print_service/sane/config"; ++ DBG(1, "%s: config_dir : %s ", __func__, dir); ++ DIR *config_dir = opendir(dir); ++ struct dirent *entry; ++ while ((entry = readdir(config_dir)) != NULL) { ++ // ignore '.' and '..' ++ if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) { ++ DBG (2, "find dll_config file : %s\n ", entry->d_name); ++ if (has_suffix (entry->d_name, conffile)) { ++ read_config (entry->d_name); ++ } ++ } ++ } ++} ++#endif ++ + static void + read_dlld (void) + { +@@ -931,8 +993,11 @@ sane_init (SANE_Int * version_code, SANE_Auth_Callback authorize) + * Read dll.d first, so that the extras backends will be tried last + */ + read_dlld (); ++#ifdef HAVE_SCAN_SERVICE ++ read_configs(DLL_CONFIG_FILE); ++#else + read_config (DLL_CONFIG_FILE); +- ++#endif + fp = sanei_config_open (DLL_ALIASES_FILE); + if (!fp) + return SANE_STATUS_GOOD; /* don't insist on aliases file */ +diff --git a/include/sane/config.h b/include/sane/config.h +index 20dfa104b..206a4147e 100644 +--- a/include/sane/config.h ++++ b/include/sane/config.h +@@ -209,6 +209,9 @@ + /* Define to 1 if you have libusb-1.0 */ + #define HAVE_LIBUSB 1 + ++/* Define to 1 if you have scan_service */ ++#define HAVE_SCAN_SERVICE 1 ++ + /* Define to 1 if you have libusb-0.1 */ + /* #undef HAVE_LIBUSB_LEGACY */ diff --git a/patches/patches.json b/patches/patches.json deleted file mode 100644 index bb7e9f788..000000000 --- a/patches/patches.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "patches": [ - { - "project": "print_print_fwk", - "path": "base/print/print_fwk", - "pr_url": "https://gitee.com/openharmony/print_print_fwk/pulls/288" - } - ] -} \ No newline at end of file diff --git a/sanei/sanei_thread.c b/sanei/sanei_thread.c index a5dcae154..6ee232662 100644 --- a/sanei/sanei_thread.c +++ b/sanei/sanei_thread.c @@ -182,7 +182,8 @@ sanei_thread_kill( SANE_Pid pid ) #if defined (__APPLE__) && defined (__MACH__) return pthread_kill((pthread_t)pid, SIGUSR2); #else - return pthread_cancel((pthread_t)pid); + //return pthread_cancel((pthread_t)pid); + return pthread_kill((pthread_t)pid, 10); #endif #elif defined HAVE_OS2_H return DosKillThread(pid); @@ -344,7 +345,7 @@ local_thread( void *arg ) int old; pthread_setcancelstate( PTHREAD_CANCEL_ENABLE, &old ); - pthread_setcanceltype ( PTHREAD_CANCEL_ASYNCHRONOUS, &old ); + //pthread_setcanceltype ( PTHREAD_CANCEL_ASYNCHRONOUS, &old ); #endif DBG( 2, "thread started, calling func() now...\n" ); diff --git a/sanei/sanei_usb.c b/sanei/sanei_usb.c index 85d31e6fb..60b02fb93 100644 --- a/sanei/sanei_usb.c +++ b/sanei/sanei_usb.c @@ -65,7 +65,7 @@ #include #if WITH_USB_RECORD_REPLAY -#include +#include "libxml/tree.h" #endif #ifdef HAVE_RESMGR @@ -81,13 +81,9 @@ #endif /* HAVE_LIBUSB_LEGACY */ #ifdef HAVE_LIBUSB -#include +#include "libusb.h" #endif /* HAVE_LIBUSB */ -#ifdef HAVE_USB_MANAGER -#include "usb_manager.h" -#endif /* HAVE_USB_MANAGER */ - #ifdef HAVE_USBCALLS #include #include @@ -133,9 +129,7 @@ typedef enum (Linux, BSD) */ sanei_usb_method_libusb, - sanei_usb_method_usbcalls, - - sanei_usb_method_usb_manager + sanei_usb_method_usbcalls } sanei_usb_access_method_type; @@ -166,11 +160,6 @@ typedef struct libusb_device *lu_device; libusb_device_handle *lu_handle; #endif /* HAVE_LIBUSB */ -#ifdef HAVE_USB_MANAGER - usb_manager_device *usb_manager_device; - usb_manager_device_handle *usb_manager_handle; -#endif /* HAVE_USB_MANAGER */ - } device_list_type; @@ -223,19 +212,10 @@ static xmlNode* testing_xml_next_tx_node = NULL; static int libusb_timeout = 30 * 1000; /* 30 seconds */ #endif /* HAVE_LIBUSB_LEGACY */ -#ifdef HAVE_USB_MANAGER -static int usb_manager_timeout = 30 * 1000; /* 30 seconds */ -#endif /* HAVE_USB_MANAGER */ - - #ifdef HAVE_LIBUSB static libusb_context *sanei_usb_ctx; #endif /* HAVE_LIBUSB */ -#ifdef HAVE_USB_MANAGER -static usb_manager_context *sanei_usb_ctx; -#endif /* HAVE_USB_MANAGER */ - #if defined (__APPLE__) /* macOS won't configure several USB scanners (i.e. ScanSnap 300M) because their * descriptors are vendor specific. As a result the device will get configured @@ -319,7 +299,7 @@ print_buffer (const SANE_Byte * buffer, SANE_Int size) } } -#if !defined(HAVE_LIBUSB_LEGACY) && !defined(HAVE_LIBUSB) && !defined(HAVE_USB_MANAGER) +#if !defined(HAVE_LIBUSB_LEGACY) && !defined(HAVE_LIBUSB) static void kernel_get_vendor_product (int fd, const char *name, int *vendorID, int *productID) { @@ -393,7 +373,7 @@ kernel_get_vendor_product (int fd, const char *name, int *vendorID, int *product #endif /* defined (__linux__), defined(__BEOS__), ... */ /* put more os-dependant stuff ... */ } -#endif /* !defined(HAVE_LIBUSB_LEGACY) && !defined(HAVE_LIBUSB) && !defined(HAVE_USB_MANAGER) */ +#endif /* !defined(HAVE_LIBUSB_LEGACY) && !defined(HAVE_LIBUSB) */ /** * store the given device in device list if it isn't already @@ -425,9 +405,6 @@ store_device (device_list_type device) #ifdef HAVE_LIBUSB devices[i].lu_device = device.lu_device; #endif -#ifdef HAVE_USB_MANAGER - devices[i].usb_manager_device = device.usb_manager_device; -#endif devices[i].missing=0; DBG (3, "store_device: not storing device %s\n", device.devname); @@ -520,62 +497,6 @@ sanei_libusb_strerror (int errcode) } #endif /* HAVE_LIBUSB */ -#ifdef HAVE_USB_MANAGER -static char * -sanei_usb_manager_strerror (int errcode) -{ - /* Error codes & descriptions from the usb_manager documentation */ - - switch (errcode) - { - case USB_MANAGER_SUCCESS: - return "Success (no error)"; - - case USB_MANAGER_ERROR_IO: - return "Input/output error"; - - case USB_MANAGER_ERROR_INVALID_PARAM: - return "Invalid parameter"; - - case USB_MANAGER_ERROR_ACCESS: - return "Access denied (insufficient permissions)"; - - case USB_MANAGER_ERROR_NO_DEVICE: - return "No such device (it may have been disconnected)"; - - case USB_MANAGER_ERROR_NOT_FOUND: - return "Entity not found"; - - case USB_MANAGER_ERROR_BUSY: - return "Resource busy"; - - case USB_MANAGER_ERROR_TIMEOUT: - return "Operation timed out"; - - case USB_MANAGER_ERROR_OVERFLOW: - return "Overflow"; - - case USB_MANAGER_ERROR_PIPE: - return "Pipe error"; - - case USB_MANAGER_ERROR_INTERRUPTED: - return "System call interrupted (perhaps due to signal)"; - - case USB_MANAGER_ERROR_NO_MEM: - return "Insufficient memory"; - - case USB_MANAGER_ERROR_NOT_SUPPORTED: - return "Operation not supported or unimplemented on this platform"; - - case USB_MANAGER_ERROR_OTHER: - return "Other error"; - - default: - return "Unknown usb_manager error code"; - } -} -#endif /* HAVE_USB_MANAGER */ - #if WITH_USB_RECORD_REPLAY SANE_Status sanei_usb_testing_enable_replay(SANE_String_Const path, int development_mode) @@ -1459,9 +1380,9 @@ void sanei_usb_testing_record_message(SANE_String_Const message) void sanei_usb_init (void) { -#if defined(HAVE_LIBUSB) || defined(HAVE_USB_MANAGER) +#ifdef HAVE_LIBUSB int ret; -#endif /* HAVE_LIBUSB || HAVE_USB_MANAGER */ +#endif /* HAVE_LIBUSB */ DBG_INIT (); #ifdef DBG_LEVEL @@ -1530,22 +1451,7 @@ sanei_usb_init (void) } #endif /* HAVE_LIBUSB */ -#ifdef HAVE_USB_MANAGER - if (!sanei_usb_ctx) - { - DBG (4, "%s: initializing usb_manager\n", __func__); - ret = usb_manager_init (&sanei_usb_ctx); - if (ret < 0) - { - DBG (1, - "%s: failed to initialize usb_manager, error %d\n", __func__, - ret); - return; - } - } -#endif /* HAVE_USB_MANAGER */ - -#if !defined(HAVE_LIBUSB_LEGACY) && !defined(HAVE_LIBUSB) && !defined(HAVE_USB_MANAGER) +#if !defined(HAVE_LIBUSB_LEGACY) && !defined(HAVE_LIBUSB) DBG (4, "%s: SANE is built without support for libusb\n", __func__); #endif @@ -1592,21 +1498,14 @@ int i; devices[i].devname=NULL; } } -#if defined(HAVE_LIBUSB) +#ifdef HAVE_LIBUSB if (sanei_usb_ctx) { libusb_exit (sanei_usb_ctx); /* reset libusb-1.0 context */ sanei_usb_ctx=NULL; } -#elif defined(HAVE_USB_MANAGER) - if (sanei_usb_ctx) - { - usb_manager_exit (sanei_usb_ctx); - /* reset usb_manager context */ - sanei_usb_ctx=NULL; - } -#endif /* HAVE_LIBUSB || HAVE_USB_MANAGER */ +#endif /* reset device_number */ device_number=0; } @@ -1689,7 +1588,7 @@ static void usbcall_scan_devices(void) } #endif /* HAVE_USBCALLS */ -#if !defined(HAVE_LIBUSB_LEGACY) && !defined(HAVE_LIBUSB) && !defined(HAVE_USB_MANAGER) +#if !defined(HAVE_LIBUSB_LEGACY) && !defined(HAVE_LIBUSB) /** scan for devices using kernel device. * Check for devices using kernel device */ @@ -1788,7 +1687,7 @@ static void kernel_scan_devices(void) closedir (dir); } } -#endif /* !defined(HAVE_LIBUSB_LEGACY) && !defined(HAVE_LIBUSB) && !defined(HAVE_USB_MANAGER) */ +#endif /* !defined(HAVE_LIBUSB_LEGACY) && !defined(HAVE_LIBUSB) */ #ifdef HAVE_LIBUSB_LEGACY /** scan for devices using old libusb @@ -2081,183 +1980,6 @@ static void libusb_scan_devices(void) } #endif /* HAVE_LIBUSB */ -#ifdef HAVE_USB_MANAGER -/** scan for devices using usb_manager - * Check for devices using usb_manager - */ -static void libusb_scan_devices(void) -{ - device_list_type device; - SANE_Char devname[1024]; - usb_manager_device **devlist; - ssize_t ndev; - usb_manager_device *dev; - usb_manager_device_handle *hdl; - struct usb_manager_device_descriptor desc; - struct usb_manager_config_descriptor *config0; - unsigned short vid, pid; - unsigned char busno, address; - int config; - int interface; - int ret; - int i; - - DBG (4, "%s: Looking for usb_manager devices\n", __func__); - - ndev = usb_manager_get_device_list (sanei_usb_ctx, &devlist); - if (ndev < 0) - { - DBG (1, - "%s: failed to get usb_manager device list, error %d\n", __func__, - (int) ndev); - return; - } - - for (i = 0; i < ndev; i++) - { - SANE_Bool found = SANE_FALSE; - - dev = devlist[i]; - - busno = usb_manager_get_bus_number (dev); - address = usb_manager_get_device_address (dev); - - ret = usb_manager_get_device_descriptor (dev, &desc); - if (ret < 0) - { - DBG (1, - "%s: could not get device descriptor for device at %03d:%03d (err %d)\n", __func__, - busno, address, ret); - continue; - } - - vid = desc.idVendor; - pid = desc.idProduct; - - if ((vid == 0) || (pid == 0)) - { - DBG (5, - "%s: device 0x%04x/0x%04x at %03d:%03d looks like a root hub\n", __func__, - vid, pid, busno, address); - continue; - } - - ret = usb_manager_open (dev, &hdl); - if (ret < 0) - { - DBG (1, - "%s: skipping device 0x%04x/0x%04x at %03d:%03d: cannot open: %s\n", __func__, - vid, pid, busno, address, sanei_usb_manager_strerror (ret)); - - continue; - } - - ret = usb_manager_get_configuration (hdl, &config); - - usb_manager_close (hdl); - - if (ret < 0) - { - DBG (1, - "%s: could not get configuration for device 0x%04x/0x%04x at %03d:%03d (err %d)\n", __func__, - vid, pid, busno, address, ret); - continue; - } - -#if !defined(SANEI_ALLOW_UNCONFIGURED_DEVICES) - if (config == 0) - { - DBG (1, - "%s: device 0x%04x/0x%04x at %03d:%03d is not configured\n", __func__, - vid, pid, busno, address); - continue; - } -#endif - - ret = usb_manager_get_config_descriptor (dev, 0, &config0); - if (ret < 0) - { - DBG (1, - "%s: could not get config[0] descriptor for device 0x%04x/0x%04x at %03d:%03d (err %d)\n", __func__, - vid, pid, busno, address, ret); - continue; - } - - for (interface = 0; (interface < config0->bNumInterfaces) && !found; interface++) - { - switch (desc.bDeviceClass) - { - case USB_MANAGER_CLASS_VENDOR_SPEC: - found = SANE_TRUE; - break; - - case USB_MANAGER_CLASS_PER_INTERFACE: - if ((config0->interface[interface].num_altsetting == 0) - || !config0->interface[interface].altsetting) - { - DBG (1, "%s: device 0x%04x/0x%04x doesn't " - "have an altsetting for interface %d\n", __func__, - vid, pid, interface); - continue; - } - - switch (config0->interface[interface].altsetting[0].bInterfaceClass) - { - case USB_MANAGER_CLASS_VENDOR_SPEC: - case USB_MANAGER_CLASS_PER_INTERFACE: - case USB_MANAGER_CLASS_PTP: - case 16: /* data? */ - found = SANE_TRUE; - break; - } - break; - } - - if (!found) - DBG (5, - "%s: device 0x%04x/0x%04x, interface %d " - "doesn't look like a scanner (%d/%d)\n", __func__, - vid, pid, interface, desc.bDeviceClass, - (config0->interface[interface].num_altsetting != 0) - ? config0->interface[interface].altsetting[0].bInterfaceClass : -1); - } - - usb_manager_free_config_descriptor (config0); - - interface--; - - if (!found) - { - DBG (5, - "%s: device 0x%04x/0x%04x at %03d:%03d: no suitable interfaces\n", __func__, - vid, pid, busno, address); - continue; - } - - memset (&device, 0, sizeof (device)); - device.usb_manager_device = usb_manager_ref_device(dev); - snprintf (devname, sizeof (devname), "libusb:%03d:%03d", - busno, address); - device.devname = strdup (devname); - if (!device.devname) - return; - device.vendor = vid; - device.product = pid; - device.method = sanei_usb_method_libusb; - device.interface_nr = interface; - device.alt_setting = 0; - DBG (4, - "%s: found usb_manager device (0x%04x/0x%04x) interface " - "%d at %s\n", __func__, - vid, pid, interface, devname); - - store_device (device); - } - - usb_manager_free_device_list (devlist, 1); - -} -#endif /* HAVE_USB_MANAGER */ void sanei_usb_scan_devices (void) @@ -2287,11 +2009,11 @@ sanei_usb_scan_devices (void) } /* Check for devices using the kernel scanner driver */ -#if !defined(HAVE_LIBUSB_LEGACY) && !defined(HAVE_LIBUSB) && !defined(HAVE_USB_MANAGER) +#if !defined(HAVE_LIBUSB_LEGACY) && !defined(HAVE_LIBUSB) kernel_scan_devices(); #endif -#if defined(HAVE_LIBUSB_LEGACY) || defined(HAVE_LIBUSB) || defined(HAVE_USB_MANAGER) +#if defined(HAVE_LIBUSB_LEGACY) || defined(HAVE_LIBUSB) /* Check for devices using libusb (old or new)*/ libusb_scan_devices(); #endif @@ -2501,7 +2223,7 @@ sanei_usb_set_endpoint (SANE_Int dn, SANE_Int ep_type, SANE_Int ep) } } -#if HAVE_LIBUSB_LEGACY || HAVE_LIBUSB || HAVE_USBCALLS || WITH_USB_RECORD_REPLAY || HAVE_USB_MANAGER +#if HAVE_LIBUSB_LEGACY || HAVE_LIBUSB || HAVE_USBCALLS || WITH_USB_RECORD_REPLAY static const char* sanei_usb_transfer_type_desc(SANE_Int transfer_type) { switch (transfer_type) @@ -2570,7 +2292,7 @@ static void sanei_usb_add_endpoint(device_list_type* device, *ep_out = ep_address; } } -#endif // HAVE_LIBUSB_LEGACY || HAVE_LIBUSB || HAVE_USBCALLS || HAVE_USB_MANAGER +#endif // HAVE_LIBUSB_LEGACY || HAVE_LIBUSB || HAVE_USBCALLS SANE_Int sanei_usb_get_endpoint (SANE_Int dn, SANE_Int ep_type) @@ -3093,257 +2815,39 @@ sanei_usb_open (SANE_String_Const devname, SANE_Int * dn) libusb_free_config_descriptor (config); } -#elif defined(HAVE_USB_MANAGER) /* usb_manager */ - - int config; - usb_manager_device *dev; - struct usb_manager_device_descriptor desc; - struct usb_manager_config_descriptor *config0; - int result, num; - int c, i, a; - - dev = devices[devcount].usb_manager_device; - result = usb_manager_open (dev, &devices[devcount].usb_manager_handle); - if (result < 0) +#else /* not HAVE_LIBUSB_LEGACY && not HAVE_LIBUSB */ + DBG (1, "sanei_usb_open: can't open device `%s': " + "libusb support missing\n", devname); + return SANE_STATUS_UNSUPPORTED; +#endif /* not HAVE_LIBUSB_LEGACY && not HAVE_LIBUSB */ + } + else if (devices[devcount].method == sanei_usb_method_scanner_driver) + { +#ifdef FD_CLOEXEC + long int flag; +#endif + /* Using kernel scanner driver */ + devices[devcount].fd = -1; +#ifdef HAVE_RESMGR + devices[devcount].fd = rsm_open_device (devname, O_RDWR); +#endif + if (devices[devcount].fd == -1) + devices[devcount].fd = open (devname, O_RDWR); + if (devices[devcount].fd < 0) { SANE_Status status = SANE_STATUS_INVAL; - DBG (1, "sanei_usb_open: can't open device `%s': %s\n", - devname, sanei_usb_manager_strerror (result)); - if (result == USB_MANAGER_ERROR_ACCESS) - { - DBG (1, "Make sure you run as root or set appropriate " - "permissions\n"); - status = SANE_STATUS_ACCESS_DENIED; - } - else if (result == USB_MANAGER_ERROR_BUSY) - { - DBG (1, "Maybe the kernel scanner driver claims the " - "scanner's interface?\n"); - status = SANE_STATUS_DEVICE_BUSY; - } - else if (result == USB_MANAGER_ERROR_NO_MEM) + if (errno == EACCES) + status = SANE_STATUS_ACCESS_DENIED; + else if (errno == ENOENT) { - status = SANE_STATUS_NO_MEM; + DBG (5, "sanei_usb_open: open of `%s' failed: %s\n", + devname, strerror (errno)); + return status; } - return status; - } - - result = usb_manager_get_configuration (devices[devcount].usb_manager_handle, &config); - if (result < 0) - { - DBG (1, - "sanei_usb_open: could not get configuration for device `%s' (err %d)\n", - devname, result); - return SANE_STATUS_INVAL; - } - -#if !defined(SANEI_ALLOW_UNCONFIGURED_DEVICES) - if (config == 0) - { - DBG (1, "sanei_usb_open: device `%s' not configured?\n", devname); - return SANE_STATUS_INVAL; - } -#endif - - result = usb_manager_get_device_descriptor (dev, &desc); - if (result < 0) - { - DBG (1, - "sanei_usb_open: could not get device descriptor for device `%s' (err %d)\n", - devname, result); - return SANE_STATUS_INVAL; - } - - result = usb_manager_get_config_descriptor (dev, 0, &config0); - if (result < 0) - { - DBG (1, - "sanei_usb_open: could not get config[0] descriptor for device `%s' (err %d)\n", - devname, result); - return SANE_STATUS_INVAL; - } - - /* Set the configuration */ - if (desc.bNumConfigurations > 1) - { - DBG (3, "sanei_usb_open: more than one " - "configuration (%d), choosing first config (%d)\n", - desc.bNumConfigurations, - config0->bConfigurationValue); - - result = 0; - if (config != config0->bConfigurationValue) - result = usb_manager_set_configuration (devices[devcount].usb_manager_handle, - config0->bConfigurationValue); - - if (result < 0) - { - SANE_Status status = SANE_STATUS_INVAL; - - DBG (1, "sanei_usb_open: usb_manager complained: %s\n", - sanei_usb_manager_strerror (result)); - if (result == USB_MANAGER_ERROR_ACCESS) - { - DBG (1, "Make sure you run as root or set appropriate " - "permissions\n"); - status = SANE_STATUS_ACCESS_DENIED; - } - else if (result == USB_MANAGER_ERROR_BUSY) - { - DBG (3, "Maybe the kernel scanner driver or usblp claims " - "the interface? Ignoring this error...\n"); - status = SANE_STATUS_GOOD; - } - - if (status != SANE_STATUS_GOOD) - { - usb_manager_close (devices[devcount].usb_manager_handle); - usb_manager_free_config_descriptor (config0); - return status; - } - } - } - usb_manager_free_config_descriptor (config0); - - /* Claim the interface */ - result = usb_manager_claim_interface (devices[devcount].usb_manager_handle, - devices[devcount].interface_nr); - if (result < 0) - { - SANE_Status status = SANE_STATUS_INVAL; - - DBG (1, "sanei_usb_open: usb_manager complained: %s\n", - sanei_usb_manager_strerror (result)); - if (result == USB_MANAGER_ERROR_ACCESS) - { - DBG (1, "Make sure you run as root or set appropriate " - "permissions\n"); - status = SANE_STATUS_ACCESS_DENIED; - } - else if (result == USB_MANAGER_ERROR_BUSY) - { - DBG (1, "Maybe the kernel scanner driver claims the " - "scanner's interface?\n"); - status = SANE_STATUS_DEVICE_BUSY; - } - - usb_manager_close (devices[devcount].usb_manager_handle); - return status; - } - - /* Loop through all of the configurations */ - for (c = 0; c < desc.bNumConfigurations; c++) - { - struct usb_manager_config_descriptor *config; - - result = usb_manager_get_config_descriptor (dev, c, &config); - if (result < 0) - { - DBG (1, - "sanei_usb_open: could not get config[%d] descriptor for device `%s' (err %d)\n", - c, devname, result); - continue; - } - - /* Loop through all of the interfaces */ - for (i = 0; i < config->bNumInterfaces; i++) - { - /* Loop through all of the alternate settings */ - for (a = 0; a < config->interface[i].num_altsetting; a++) - { - const struct usb_manager_interface_descriptor *interface; - - DBG (5, "sanei_usb_open: configuration nr: %d\n", c); - DBG (5, "sanei_usb_open: interface nr: %d\n", i); - DBG (5, "sanei_usb_open: alt_setting nr: %d\n", a); - - /* Start by interfaces found in sanei_usb_init */ - if (c == 0 && i != devices[devcount].interface_nr) - { - DBG (5, "sanei_usb_open: interface %d not detected as " - "a scanner by sanei_usb_init, ignoring.\n", i); - continue; - } - - interface = &config->interface[i].altsetting[a]; - - /* Now we look for usable endpoints */ - for (num = 0; num < interface->bNumEndpoints; num++) - { - const struct usb_manager_endpoint_descriptor *endpoint; - int direction, transfer_type, transfer_type_usb_manager; - - endpoint = &interface->endpoint[num]; - DBG (5, "sanei_usb_open: endpoint nr: %d\n", num); - - transfer_type_usb_manager = - endpoint->bmAttributes & USB_MANAGER_TRANSFER_TYPE_MASK; - direction = endpoint->bEndpointAddress & USB_MANAGER_ENDPOINT_DIR_MASK; - - // don't rely on USB_MANAGER_TRANSFER_TYPE_* mapping to - // USB_ENDPOINT_TYPE_* even though they'll most likely be - // the same - switch (transfer_type_usb_manager) - { - case USB_MANAGER_TRANSFER_TYPE_INTERRUPT: - transfer_type = USB_ENDPOINT_TYPE_INTERRUPT; - break; - case USB_MANAGER_TRANSFER_TYPE_BULK: - transfer_type = USB_ENDPOINT_TYPE_BULK; - break; - case USB_MANAGER_TRANSFER_TYPE_ISOCHRONOUS: - transfer_type = USB_MANAGER_TRANSFER_TYPE_ISOCHRONOUS; - break; - case USB_MANAGER_TRANSFER_TYPE_CONTROL: - transfer_type = USB_ENDPOINT_TYPE_CONTROL; - break; - - } - - sanei_usb_add_endpoint(&devices[devcount], - transfer_type, - endpoint->bEndpointAddress, - direction); - } - } - } - - usb_manager_free_config_descriptor (config); - } -#else /* not HAVE_LIBUSB_LEGACY && not HAVE_LIBUSB && not HAVE_USB_MANAGER */ - DBG (1, "sanei_usb_open: can't open device `%s': " - "libusb support missing\n", devname); - return SANE_STATUS_UNSUPPORTED; -#endif /* not HAVE_LIBUSB_LEGACY && not HAVE_LIBUSB && not HAVE_USB_MANAGER */ - } - else if (devices[devcount].method == sanei_usb_method_scanner_driver) - { -#ifdef FD_CLOEXEC - long int flag; -#endif - /* Using kernel scanner driver */ - devices[devcount].fd = -1; -#ifdef HAVE_RESMGR - devices[devcount].fd = rsm_open_device (devname, O_RDWR); -#endif - if (devices[devcount].fd == -1) - devices[devcount].fd = open (devname, O_RDWR); - if (devices[devcount].fd < 0) - { - SANE_Status status = SANE_STATUS_INVAL; - - if (errno == EACCES) - status = SANE_STATUS_ACCESS_DENIED; - else if (errno == ENOENT) - { - DBG (5, "sanei_usb_open: open of `%s' failed: %s\n", - devname, strerror (errno)); - return status; - } - DBG (1, "sanei_usb_open: open of `%s' failed: %s\n", - devname, strerror (errno)); + DBG (1, "sanei_usb_open: open of `%s' failed: %s\n", + devname, strerror (errno)); return status; } #ifdef FD_CLOEXEC @@ -3557,22 +3061,7 @@ sanei_usb_close (SANE_Int dn) devices[dn].interface_nr); libusb_close (devices[dn].lu_handle); } -#elif defined(HAVE_USB_MANAGER) - { - /* This call seems to be required by Linux xhci driver - * even though it should be a no-op. Without it, the - * host or driver does not reset it's data toggle bit. - * We intentionally ignore the return val */ - if (workaround) - { - sanei_usb_set_altinterface (dn, devices[dn].alt_setting); - } - - usb_manager_release_interface (devices[dn].usb_manager_handle, - devices[dn].interface_nr); - usb_manager_close (devices[dn].usb_manager_handle); - } -#else /* not HAVE_LIBUSB_LEGACY && not HAVE_LIBUSB && not HAVE_USB_MANAGER */ +#else /* not HAVE_LIBUSB_LEGACY && not HAVE_LIBUSB */ DBG (1, "sanei_usb_close: libusb support missing\n"); #endif devices[dn].open = SANE_FALSE; @@ -3587,11 +3076,9 @@ sanei_usb_set_timeout (SANE_Int __sane_unused__ timeout) #if defined(HAVE_LIBUSB_LEGACY) || defined(HAVE_LIBUSB) libusb_timeout = timeout; -#elif defined(HAVE_USB_MANAGER) - usb_manager_timeout = timeout; #else DBG (1, "sanei_usb_set_timeout: libusb support missing\n"); -#endif /* HAVE_LIBUSB_LEGACY || HAVE_LIBUSB || HAVE_USB_MANAGER */ +#endif /* HAVE_LIBUSB_LEGACY || HAVE_LIBUSB */ } SANE_Status @@ -3664,32 +3151,9 @@ sanei_usb_clear_halt (SANE_Int dn) DBG (1, "sanei_usb_clear_halt: BULK_OUT ret=%d\n", ret); return SANE_STATUS_INVAL; } -#elif defined(HAVE_USB_MANAGER) - int ret; - - /* This call seems to be required by Linux xhci driver - * even though it should be a no-op. Without it, the - * host or driver does not send the clear to the device. - * We intentionally ignore the return val */ - if (workaround) - { - sanei_usb_set_altinterface (dn, devices[dn].alt_setting); - } - - ret = usb_manager_clear_halt (devices[dn].usb_manager_handle, devices[dn].bulk_in_ep); - if (ret){ - DBG (1, "sanei_usb_clear_halt: BULK_IN ret=%d\n", ret); - return SANE_STATUS_INVAL; - } - - ret = usb_manager_clear_halt (devices[dn].usb_manager_handle, devices[dn].bulk_out_ep); - if (ret){ - DBG (1, "sanei_usb_clear_halt: BULK_OUT ret=%d\n", ret); - return SANE_STATUS_INVAL; - } -#else /* not HAVE_LIBUSB_LEGACY && not HAVE_LIBUSB && not HAVE_USB_MANAGER */ +#else /* not HAVE_LIBUSB_LEGACY && not HAVE_LIBUSB */ DBG (1, "sanei_usb_clear_halt: libusb support missing\n"); -#endif /* HAVE_LIBUSB_LEGACY || HAVE_LIBUSB || HAVE_USB_MANAGER */ +#endif /* HAVE_LIBUSB_LEGACY || HAVE_LIBUSB */ return SANE_STATUS_GOOD; } @@ -3717,18 +3181,10 @@ sanei_usb_reset (SANE_Int __sane_unused__ dn) DBG (1, "sanei_usb_reset: ret=%d\n", ret); return SANE_STATUS_INVAL; } -#elif defined(HAVE_USB_MANAGER) - int ret; - - ret = usb_manager_reset_device (devices[dn].usb_manager_handle); - if (ret){ - DBG (1, "sanei_usb_reset: ret=%d\n", ret); - return SANE_STATUS_INVAL; - } -#else /* not HAVE_LIBUSB_LEGACY && not HAVE_LIBUSB && not HAVE_USB_MANAGER */ +#else /* not HAVE_LIBUSB_LEGACY && not HAVE_LIBUSB */ DBG (1, "sanei_usb_reset: libusb support missing\n"); -#endif /* HAVE_LIBUSB_LEGACY || HAVE_LIBUSB || HAVE_USB_MANAGER */ +#endif /* HAVE_LIBUSB_LEGACY || HAVE_LIBUSB */ return SANE_STATUS_GOOD; } @@ -3972,36 +3428,7 @@ sanei_usb_read_bulk (SANE_Int dn, SANE_Byte * buffer, size_t * size) return SANE_STATUS_INVAL; } } -#elif defined(HAVE_USB_MANAGER) - { - if (devices[dn].bulk_in_ep) - { - int ret, rsize; - ret = usb_manager_bulk_transfer (devices[dn].usb_manager_handle, - devices[dn].bulk_in_ep, buffer, - (int) *size, &rsize, - usb_manager_timeout); - - if (ret < 0) - { - DBG (1, "sanei_usb_read_bulk: read failed (still got %d bytes): %s\n", - rsize, sanei_usb_manager_strerror (ret)); - - read_size = -1; - } - else - { - read_size = rsize; - } - } - else - { - DBG (1, "sanei_usb_read_bulk: can't read without a bulk-in " - "endpoint\n"); - return SANE_STATUS_INVAL; - } - } -#else /* not HAVE_LIBUSB_LEGACY && not HAVE_LIBUSB && not HAVE_USB_MANAGER */ +#else /* not HAVE_LIBUSB_LEGACY && not HAVE_LIBUSB */ { DBG (1, "sanei_usb_read_bulk: libusb support missing\n"); return SANE_STATUS_UNSUPPORTED; @@ -4073,9 +3500,6 @@ sanei_usb_read_bulk (SANE_Int dn, SANE_Byte * buffer, size_t * size) #elif defined(HAVE_LIBUSB) if (devices[dn].method == sanei_usb_method_libusb) libusb_clear_halt (devices[dn].lu_handle, devices[dn].bulk_in_ep); -#elif defined(HAVE_USB_MANAGER) - if (devices[dn].method == sanei_usb_method_usb_manager) - usb_manager_clear_halt (devices[dn].usb_manager_handle, devices[dn].bulk_in_ep); #endif return SANE_STATUS_IO_ERROR; } @@ -4339,40 +3763,12 @@ sanei_usb_write_bulk (SANE_Int dn, const SANE_Byte * buffer, size_t * size) return SANE_STATUS_INVAL; } } -#elif defined(HAVE_USB_MANAGER) - { - if (devices[dn].bulk_out_ep) - { - int ret; - int trans_bytes; - ret = usb_manager_bulk_transfer (devices[dn].usb_manager_handle, - devices[dn].bulk_out_ep, - (unsigned char *) buffer, - (int) *size, &trans_bytes, - usb_manager_timeout); - if (ret < 0) - { - DBG (1, "sanei_usb_write_bulk: write failed: %s\n", - sanei_usb_manager_strerror (ret)); - - write_size = -1; - } - else - write_size = trans_bytes; - } - else - { - DBG (1, "sanei_usb_write_bulk: can't write without a bulk-out " - "endpoint\n"); - return SANE_STATUS_INVAL; - } - } -#else /* not HAVE_LIBUSB_LEGACY && not HAVE_LIBUSB && not HAVE_USB_MANAGER */ +#else /* not HAVE_LIBUSB_LEGACY && not HAVE_LIBUSB */ { DBG (1, "sanei_usb_write_bulk: libusb support missing\n"); return SANE_STATUS_UNSUPPORTED; } -#endif /* not HAVE_LIBUSB_LEGACY && not HAVE_LIBUSB && not HAVE_USB_MANAGER */ +#endif /* not HAVE_LIBUSB_LEGACY && not HAVE_LIBUSB */ else if (devices[dn].method == sanei_usb_method_usbcalls) { #ifdef HAVE_USBCALLS @@ -4440,9 +3836,6 @@ sanei_usb_write_bulk (SANE_Int dn, const SANE_Byte * buffer, size_t * size) #elif defined(HAVE_LIBUSB) if (devices[dn].method == sanei_usb_method_libusb) libusb_clear_halt (devices[dn].lu_handle, devices[dn].bulk_out_ep); -#elif defined(HAVE_USB_MANAGER) - if (devices[dn].method == sanei_usb_method_usb_manager) - usb_manager_clear_halt (devices[dn].usb_manager_handle, devices[dn].bulk_out_ep); #endif return SANE_STATUS_IO_ERROR; } @@ -4710,28 +4103,12 @@ sanei_usb_control_msg (SANE_Int dn, SANE_Int rtype, SANE_Int req, if ((rtype & 0x80) && debug_level > 10) print_buffer (data, len); } -#elif defined(HAVE_USB_MANAGER) - { - int result; - - result = usb_manager_control_transfer (devices[dn].usb_manager_handle, rtype, req, - value, index, data, len, - usb_manager_timeout); - if (result < 0) - { - DBG (1, "sanei_usb_control_msg: libusb complained: %s\n", - sanei_usb_manager_strerror (result)); - return SANE_STATUS_INVAL; - } - if ((rtype & 0x80) && debug_level > 10) - print_buffer (data, len); - } -#else /* not HAVE_LIBUSB_LEGACY && not HAVE_LIBUSB && not HAVE_USB_MANAGER*/ +#else /* not HAVE_LIBUSB_LEGACY && not HAVE_LIBUSB*/ { DBG (1, "sanei_usb_control_msg: libusb support missing\n"); return SANE_STATUS_UNSUPPORTED; } -#endif /* not HAVE_LIBUSB_LEGACY && not HAVE_LIBUSB && not HAVE_USB_MANAGER */ +#endif /* not HAVE_LIBUSB_LEGACY && not HAVE_LIBUSB */ else if (devices[dn].method == sanei_usb_method_usbcalls) { #ifdef HAVE_USBCALLS @@ -4905,7 +4282,7 @@ SANE_Status sanei_usb_read_int (SANE_Int dn, SANE_Byte * buffer, size_t * size) { ssize_t read_size = 0; -#if defined(HAVE_LIBUSB_LEGACY) || defined(HAVE_LIBUSB) || defined(HAVE_USB_MANAGER) +#if defined(HAVE_LIBUSB_LEGACY) || defined(HAVE_LIBUSB) SANE_Bool stalled = SANE_FALSE; #endif @@ -4986,37 +4363,12 @@ sanei_usb_read_int (SANE_Int dn, SANE_Byte * buffer, size_t * size) return SANE_STATUS_INVAL; } } -#elif defined(HAVE_USB_MANAGER) - { - if (devices[dn].int_in_ep) - { - int ret; - int trans_bytes; - ret = usb_manager_interrupt_transfer (devices[dn].usb_manager_handle, - devices[dn].int_in_ep, - buffer, (int) *size, - &trans_bytes, usb_manager_timeout); - - if (ret < 0) - read_size = -1; - else - read_size = trans_bytes; - - stalled = (ret == USB_MANAGER_ERROR_PIPE); - } - else - { - DBG (1, "sanei_usb_read_int: can't read without an int " - "endpoint\n"); - return SANE_STATUS_INVAL; - } - } -#else /* not HAVE_LIBUSB_LEGACY && not HAVE_LIBUSB && not HAVE_USB_MANAGER*/ +#else /* not HAVE_LIBUSB_LEGACY && not HAVE_LIBUSB */ { DBG (1, "sanei_usb_read_int: libusb support missing\n"); return SANE_STATUS_UNSUPPORTED; } -#endif /* not HAVE_LIBUSB_LEGACY && not HAVE_LIBUSB && not HAVE_USB_MANAGER */ +#endif /* not HAVE_LIBUSB_LEGACY && not HAVE_LIBUSB */ else if (devices[dn].method == sanei_usb_method_usbcalls) { #ifdef HAVE_USBCALLS @@ -5077,10 +4429,6 @@ sanei_usb_read_int (SANE_Int dn, SANE_Byte * buffer, size_t * size) if (devices[dn].method == sanei_usb_method_libusb) if (stalled) libusb_clear_halt (devices[dn].lu_handle, devices[dn].int_in_ep); -#elif defined(HAVE_USB_MANAGER) - if (devices[dn].method == sanei_usb_method_usb_manager) - if (stalled) - usb_manager_clear_halt (devices[dn].usb_manager_handle, devices[dn].int_in_ep); #endif return SANE_STATUS_IO_ERROR; } @@ -5220,25 +4568,12 @@ sanei_usb_set_configuration (SANE_Int dn, SANE_Int configuration) } return SANE_STATUS_GOOD; } -#elif defined(HAVE_USB_MANAGER) - { - int result; - - result = usb_manager_set_configuration (devices[dn].usb_manager_handle, configuration); - if (result < 0) - { - DBG (1, "sanei_usb_set_configuration: libusb complained: %s\n", - sanei_usb_manager_strerror (result)); - return SANE_STATUS_INVAL; - } - return SANE_STATUS_GOOD; - } -#else /* not HAVE_LIBUSB_LEGACY && not HAVE_LIBUSB && not HAVE_USB_MANAGER */ +#else /* not HAVE_LIBUSB_LEGACY && not HAVE_LIBUSB */ { DBG (1, "sanei_usb_set_configuration: libusb support missing\n"); return SANE_STATUS_UNSUPPORTED; } -#endif /* not HAVE_LIBUSB_LEGACY && not HAVE_LIBUSB && not HAVE_USB_MANAGER */ +#endif /* not HAVE_LIBUSB_LEGACY && not HAVE_LIBUSB */ else { DBG (1, @@ -5306,25 +4641,12 @@ sanei_usb_claim_interface (SANE_Int dn, SANE_Int interface_number) } return SANE_STATUS_GOOD; } -#elif defined(HAVE_USB_MANAGER) - { - int result; - - result = usb_manager_claim_interface (devices[dn].usb_manager_handle, interface_number); - if (result < 0) - { - DBG (1, "sanei_usb_claim_interface: libusb complained: %s\n", - sanei_usb_manager_strerror (result)); - return SANE_STATUS_INVAL; - } - return SANE_STATUS_GOOD; - } -#else /* not HAVE_LIBUSB_LEGACY && not HAVE_LIBUSB && not HAVE_USB_MANAGER */ +#else /* not HAVE_LIBUSB_LEGACY && not HAVE_LIBUSB */ { DBG (1, "sanei_usb_claim_interface: libusb support missing\n"); return SANE_STATUS_UNSUPPORTED; } -#endif /* not HAVE_LIBUSB_LEGACY && not HAVE_LIBUSB && not HAVE_USB_MANAGER*/ +#endif /* not HAVE_LIBUSB_LEGACY && not HAVE_LIBUSB */ else { DBG (1, "sanei_usb_claim_interface: access method %d not implemented\n", @@ -5390,25 +4712,12 @@ sanei_usb_release_interface (SANE_Int dn, SANE_Int interface_number) } return SANE_STATUS_GOOD; } -#elif defined(HAVE_USB_MANAGER) - { - int result; - - result = usb_manager_release_interface (devices[dn].usb_manager_handle, interface_number); - if (result < 0) - { - DBG (1, "sanei_usb_release_interface: libusb complained: %s\n", - sanei_usb_manager_strerror (result)); - return SANE_STATUS_INVAL; - } - return SANE_STATUS_GOOD; - } -#else /* not HAVE_LIBUSB_LEGACY && not HAVE_LIBUSB && not HAVE_USB_MANAGER */ +#else /* not HAVE_LIBUSB_LEGACY && not HAVE_LIBUSB */ { DBG (1, "sanei_usb_release_interface: libusb support missing\n"); return SANE_STATUS_UNSUPPORTED; } -#endif /* not HAVE_LIBUSB_LEGACY && not HAVE_LIBUSB && not HAVE_USB_MANAGER */ +#endif /* not HAVE_LIBUSB_LEGACY && not HAVE_LIBUSB */ else { DBG (1, @@ -5474,26 +4783,12 @@ sanei_usb_set_altinterface (SANE_Int dn, SANE_Int alternate) } return SANE_STATUS_GOOD; } -#elif defined(HAVE_USB_MANAGER) - { - int result; - - result = usb_manager_set_interface_alt_setting (devices[dn].usb_manager_handle, - devices[dn].interface_nr, alternate); - if (result < 0) - { - DBG (1, "sanei_usb_set_altinterface: usb_manager complained: %s\n", - sanei_usb_manager_strerror (result)); - return SANE_STATUS_INVAL; - } - return SANE_STATUS_GOOD; - } -#else /* not HAVE_LIBUSB_LEGACY && not HAVE_LIBUSB && not HAVE_USB_MANAGER*/ +#else /* not HAVE_LIBUSB_LEGACY && not HAVE_LIBUSB */ { DBG (1, "sanei_set_altinterface: libusb support missing\n"); return SANE_STATUS_UNSUPPORTED; } -#endif /* not HAVE_LIBUSB_LEGACY && not HAVE_LIBUSB && not HAVE_USB_MANAGER */ +#endif /* not HAVE_LIBUSB_LEGACY && not HAVE_LIBUSB */ else { DBG (1, @@ -5654,36 +4949,12 @@ sanei_usb_get_descriptor( SANE_Int dn, desc->dev_protocol = lu_desc.bDeviceProtocol; desc->max_packet_size = lu_desc.bMaxPacketSize0; } -#elif defined(HAVE_USB_MANAGER) - { - struct usb_manager_device_descriptor usb_manager_desc; - int ret; - - ret = usb_manager_get_device_descriptor (devices[dn].usb_manager_device, &usb_manager_desc); - if (ret < 0) - { - DBG (1, - "sanei_usb_get_descriptor: libusb error: %s\n", - sanei_usb_manager_strerror (ret)); - - return SANE_STATUS_INVAL; - } - - desc->desc_type = usb_manager_desc.bDescriptorType; - desc->bcd_usb = usb_manager_desc.bcdUSB; - desc->bcd_dev = usb_manager_desc.bcdDevice; - desc->dev_class = usb_manager_desc.bDeviceClass; - - desc->dev_sub_class = usb_manager_desc.bDeviceSubClass; - desc->dev_protocol = usb_manager_desc.bDeviceProtocol; - desc->max_packet_size = usb_manager_desc.bMaxPacketSize0; - } -#else /* not HAVE_LIBUSB_LEGACY && not HAVE_LIBUSB && not HAVE_USB_MANAGER*/ +#else /* not HAVE_LIBUSB_LEGACY && not HAVE_LIBUSB */ { DBG (1, "sanei_usb_get_descriptor: libusb support missing\n"); return SANE_STATUS_UNSUPPORTED; } -#endif /* not HAVE_LIBUSB_LEGACY && not HAVE_LIBUSB && not HAVE_USB_MANAGER */ +#endif /* not HAVE_LIBUSB_LEGACY && not HAVE_LIBUSB */ if (testing_mode == sanei_usb_testing_mode_record) { diff --git a/sanei/usb_manager.cpp b/sanei/usb_manager.cpp deleted file mode 100644 index b35b0808b..000000000 --- a/sanei/usb_manager.cpp +++ /dev/null @@ -1,671 +0,0 @@ -/* - Copyright (C) 2024 Huawei Device Co., Ltd. - - This file is part of the SANE package. - - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License as - published by the Free Software Foundation; either version 2 of the - License, or (at your option) any later version. - - This program is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . - - As a special exception, the authors of SANE give permission for - additional uses of the libraries contained in this release of SANE. - - The exception is that, if you link a SANE library with other files - to produce an executable, this does not by itself cause the - resulting executable to be covered by the GNU General Public - License. Your use of that executable is in no way restricted on - account of linking the SANE library code into it. - - This exception does not, however, invalidate any other reasons why - the executable file might be covered by the GNU General Public - License. - - If you submit changes to SANE to the maintainers to be included in - a subsequent release, you agree by submitting the changes that - those changes may be distributed with this exception intact. - - If you write modifications of your own for SANE, it is your choice - whether to permit this exception to apply to your modifications. - If you do not wish that, delete this exception notice. - - This file provides an interface to use the usb manager.*/ - -#include -#include -#include "iusb_srv.h" -#include "securec.h" -#include "cstring" -#include "usb_srv_client.h" -#include "usb_manager.h" -#include "v1_0/iusb_interface.h" -#include "hilog/log.h" - -#define SANE_LOG_TAG "sanekit" -#define SANE_HILOG_INFO(...) ((void)HiLogPrint(LOG_APP, LOG_INFO, 0, "sanekit", __VA_ARGS__)) -#define SANE_HILOG_ERROR(...) ((void)HiLogPrint(LOG_APP, LOG_ERROR, 0, "sanekit", __VA_ARGS__)) -#define SANE_HILOG_DEBUG(...) ((void)HiLogPrint(LOG_APP, LOG_DEBUG, 0, "sanekit", __VA_ARGS__)) -#define SANE_HILOG_WARN(...) ((void)HiLogPrint(LOG_APP, LOG_WARN, 0, "sanekit", __VA_ARGS__)) - -using namespace OHOS::USB; - -static std::vector g_deviceList; -static ssize_t g_deviceCount = 0; -static uint8_t g_interfaceNumber = 0; -static usb_manager_config_descriptor* g_deviceConfig = nullptr; -static std::vector g_refDevices; -constexpr int maxUsbInterfaceNum = 1000; -constexpr int maxUsbEndpointNum = 1000; - -struct usb_manager_device_handle { - UsbDevice device; - USBDevicePipe pipe; -}; - -struct usb_manager_device { - UsbDevice device; -}; - -struct UsbEndPointDes { - uint8_t bmAttributes; - uint8_t bEndpointAddress; -}; - -struct UsbInterfaceAltDes{ - uint8_t bInterfaceClass; - std::vector endpointDes; -}; - -struct UsbAltInterface { - std::vector altDes; -}; - -namespace { - void MemoryFreeDeviceList(usb_manager_device **list) - { - if (list == nullptr) { - return; - } - for (ssize_t i = 0 ; i < g_deviceCount; i++) { - if (list[i] != nullptr) { - delete list[i]; - } - } - delete[] list; - list = nullptr; - g_deviceCount = 0; - } - - void InterfaceConvert(std::vector &usbInterfaces, std::vector &usbAlt) - { - auto memCopyInter = [&](size_t &i, UsbAltInterface &altInter) { - UsbInterfaceAltDes des; - des.bInterfaceClass = static_cast(usbInterfaces[i].GetClass()); - std::vector &endPoints = usbInterfaces[i].GetEndpoints(); - for (size_t j = 0; j < endPoints.size(); j++) { - UsbEndPointDes pointDes; - pointDes.bmAttributes = static_cast(endPoints[j].GetAttributes()); - pointDes.bEndpointAddress = static_cast(endPoints[j].GetAddress()); - des.endpointDes.push_back(pointDes); - } - altInter.altDes.push_back(des); - }; - for (size_t i = 0; i < usbInterfaces.size(); i++) { - UsbInterface& inter = usbInterfaces[i]; - if (inter.GetAlternateSetting() == 0) { - UsbAltInterface altInter; - memCopyInter(i, altInter); - usbAlt.push_back(altInter); - } else { - int id = usbInterfaces[i].GetId(); - UsbAltInterface &altInter = usbAlt[id]; - memCopyInter(i, altInter); - } - } - } - - int GetRetConfigAndInterface(usb_manager_device *dev, uint8_t config_index, usb_manager_config_descriptor* &retConfig, - usb_manager_interface* &retInterface, std::vector &usbAlt) - { - std::vector &configs = dev->device.GetConfigs(); - if (config_index >= configs.size()) { - SANE_HILOG_ERROR("%s: config_index(%u) is invalid.", __func__, config_index); - return USB_MANAGER_ERROR_INVALID_PARAM; - } - USBConfig &usbConfig = configs[config_index]; - SANE_HILOG_DEBUG("%s: config[%u] = %s", __func__, config_index, usbConfig.getJsonString().c_str()); - std::vector &usbInterfaces = usbConfig.GetInterfaces(); - InterfaceConvert(usbInterfaces, usbAlt); - size_t bNumInterfaces = usbAlt.size(); - if (bNumInterfaces == 0 || bNumInterfaces > maxUsbInterfaceNum) { - SANE_HILOG_ERROR("%s: bNumInterfaces(%u) is invalid.", __func__, bNumInterfaces); - return USB_MANAGER_ERROR_OTHER; - } - retConfig = new (std::nothrow) usb_manager_config_descriptor(); - if (retConfig == nullptr) { - SANE_HILOG_ERROR("%s: Not enough memory.", __func__); - return USB_MANAGER_ERROR_NO_MEM; - } - if (memset_s(retConfig, sizeof(usb_manager_config_descriptor), 0, sizeof(usb_manager_config_descriptor)) != 0) { - SANE_HILOG_ERROR("%s: memset retConfig error.", __func__); - delete retConfig; - retConfig = nullptr; - return USB_MANAGER_ERROR_OTHER; - } - retConfig->bNumInterfaces = static_cast(bNumInterfaces); - retConfig->bConfigurationValue = static_cast(usbConfig.GetId()); - retInterface = new (std::nothrow) usb_manager_interface[bNumInterfaces]{}; - if (retInterface == nullptr) { - SANE_HILOG_ERROR("%s: Not enough memory.", __func__); - delete retConfig; - return USB_MANAGER_ERROR_NO_MEM; - } - retConfig->interface = retInterface; - return USB_MANAGER_SUCCESS; - } - - bool SetInterfaceDescriptor(std::vector &usbAlt, usb_manager_interface* retInterface) - { - if (retInterface == nullptr) { - return false; - } - for (int i = 0; i < usbAlt.size(); i++) { - size_t altSet = usbAlt[i].altDes.size(); - if (altSet > maxUsbInterfaceNum) { - return false; - } - auto retAltsetting = new (std::nothrow) usb_manager_interface_descriptor[altSet]{}; - if (retAltsetting == nullptr) { - return false; - } - retInterface[i].altsetting = retAltsetting; - retInterface[i].num_altsetting = static_cast(altSet); - for (size_t j = 0; j < altSet; j++) { - retAltsetting[j].bInterfaceClass = usbAlt[i].altDes[j].bInterfaceClass; - size_t endPointsNum = usbAlt[i].altDes[j].endpointDes.size(); - if (endPointsNum > maxUsbEndpointNum) { - return false; - } - retAltsetting[j].bNumEndpoints = endPointsNum; - retAltsetting[j].endpoint = new (std::nothrow) usb_manager_endpoint_descriptor[endPointsNum]{}; - if (retAltsetting[j].endpoint == nullptr) { - return false; - } - for (int k = 0 ; k < endPointsNum; k++) { - uint8_t bmAttr = usbAlt[i].altDes[j].endpointDes[k].bmAttributes; - uint8_t bEndpointAddress = usbAlt[i].altDes[j].endpointDes[k].bEndpointAddress; - retAltsetting[j].endpoint[k].bmAttributes = bmAttr; - retAltsetting[j].endpoint[k].bEndpointAddress = bEndpointAddress; - } - } - } - return true; - } -}; - -int usb_manager_init(usb_manager_context **ctx) -{ - SANE_HILOG_INFO("%s: begin", __func__); - if (ctx == nullptr) { - SANE_HILOG_ERROR("%s: ctx is a nullptr.", __func__); - return USB_MANAGER_ERROR_INVALID_PARAM; - } - usb_manager_context *usbmanagerContext = new (std::nothrow) usb_manager_context(); - if (usbmanagerContext == nullptr) { - SANE_HILOG_ERROR("%s: Not enough memory.", __func__); - return USB_MANAGER_ERROR_NO_MEM; - } - if (memset_s(usbmanagerContext, sizeof(usb_manager_context), 0 ,sizeof(usb_manager_context)) != 0) { - SANE_HILOG_ERROR("%s: memset_s usbmanagerContext error.", __func__); - delete usbmanagerContext; - return USB_MANAGER_ERROR_OTHER; - } - *ctx = usbmanagerContext; - SANE_HILOG_INFO("%s: end successful", __func__); - return USB_MANAGER_SUCCESS; -} - -int usb_manager_exit(usb_manager_context *ctx) -{ - SANE_HILOG_INFO("%s: begin", __func__); - if (ctx == nullptr) { - SANE_HILOG_ERROR("%s: ctx is a nullptr.", __func__); - return USB_MANAGER_ERROR_INVALID_PARAM; - } - delete ctx; - ctx = nullptr; - for (const auto& refDevice : g_refDevices) { - if (refDevice != nullptr) { - delete refDevice; - } - } - g_refDevices.clear(); - SANE_HILOG_INFO("%s: end successful", __func__); - return USB_MANAGER_SUCCESS; -} - -ssize_t usb_manager_get_device_list(usb_manager_context *ctx, usb_manager_device ***list) -{ - SANE_HILOG_INFO("%s: begin", __func__); - if (ctx == nullptr || list == nullptr) { - SANE_HILOG_ERROR("%s: ctx or list is a nullptr.", __func__); - return USB_MANAGER_ERROR_INVALID_PARAM; - } - g_deviceList.clear(); - auto ret = UsbSrvClient::GetInstance().GetDevices(g_deviceList); - if (ret != USB_MANAGER_SUCCESS) { - SANE_HILOG_ERROR("%s: GetDevices exception.", __func__); - return USB_MANAGER_ERROR_IO; - } - g_deviceCount = static_cast(g_deviceList.size()); - SANE_HILOG_DEBUG("%s: deviceCount : %d", __func__, g_deviceCount); - if (g_deviceCount == 0) { - SANE_HILOG_DEBUG("%s: not found device.", __func__); - return USB_MANAGER_SUCCESS; - } - usb_manager_device** usbManagerDeviceList = new (std::nothrow) usb_manager_device* [g_deviceCount]{}; - if (usbManagerDeviceList == nullptr) { - SANE_HILOG_ERROR("%s: Not enough memory.", __func__); - return USB_MANAGER_ERROR_NO_MEM; - } - for (ssize_t i = 0; i < g_deviceCount; i++) { - usb_manager_device* usbDevice = new (std::nothrow) usb_manager_device(); - if (usbDevice == nullptr) { - SANE_HILOG_ERROR("%s: Not enough memory.", __func__); - MemoryFreeDeviceList(usbManagerDeviceList); - return USB_MANAGER_ERROR_NO_MEM; - } - usbManagerDeviceList[i] = usbDevice; - usbDevice->device = g_deviceList[i]; - } - *list = usbManagerDeviceList; - SANE_HILOG_INFO("%s: end successful", __func__); - return g_deviceCount; -} - -int usb_manager_get_bus_number(usb_manager_device *dev) -{ - SANE_HILOG_INFO("%s: start", __func__); - if (dev == nullptr) { - SANE_HILOG_ERROR("%s: dev is a nullptr.", __func__); - return USB_MANAGER_ERROR_INVALID_PARAM; - } - SANE_HILOG_INFO("%s: end successful", __func__); - return dev->device.GetBusNum(); -} - -int usb_manager_get_device_address(usb_manager_device *dev) -{ - SANE_HILOG_INFO("%s: start", __func__); - if (dev == nullptr) { - SANE_HILOG_ERROR("%s: dev is a nullptr.", __func__); - return USB_MANAGER_ERROR_INVALID_PARAM; - } - SANE_HILOG_INFO("%s: end successful", __func__); - return dev->device.GetDevAddr(); -} - -int usb_manager_get_device_descriptor(usb_manager_device *dev, usb_manager_device_descriptor *desc) -{ - SANE_HILOG_INFO("%s: start", __func__); - if (dev == nullptr || desc == nullptr) { - SANE_HILOG_ERROR("%s: dev or desc is a nullptr.", __func__); - return USB_MANAGER_ERROR_INVALID_PARAM; - } - UsbDevice &device = dev->device; - desc->idVendor = static_cast(device.GetVendorId()); - desc->idProduct = static_cast(device.GetProductId()); - desc->bDeviceClass = static_cast(device.GetClass()); - desc->bNumConfigurations = static_cast(device.GetConfigCount()); - desc->bDeviceSubClass = static_cast(device.GetSubclass()); - desc->bDeviceProtocol = static_cast(device.GetProtocol()); - SANE_HILOG_INFO("%s: end successful", __func__); - return USB_MANAGER_SUCCESS; -} - -int usb_manager_open(usb_manager_device *dev, usb_manager_device_handle **dev_handle) -{ - SANE_HILOG_INFO("%s: start", __func__); - if (dev == nullptr || dev_handle == nullptr) { - SANE_HILOG_ERROR("%s: dev or dev_handle is a nullptr.", __func__); - return USB_MANAGER_ERROR_INVALID_PARAM; - } - auto &usbSrvClient = UsbSrvClient::GetInstance(); - USBDevicePipe pipe; - UsbDevice &device = dev->device; - auto ret = usbSrvClient.RequestRight(device.GetName()); - if (ret != USB_MANAGER_SUCCESS) { - SANE_HILOG_ERROR("request right failed ret=%{public}d", ret); - return USB_MANAGER_ERROR_ACCESS; - } - ret = usbSrvClient.OpenDevice(device, pipe); - if (ret != USB_MANAGER_SUCCESS) { - SANE_HILOG_ERROR("open device failed ret=%{public}d", ret); - return USB_MANAGER_ERROR_ACCESS; - } - auto handle = new (std::nothrow) usb_manager_device_handle(); - if (handle == nullptr) { - SANE_HILOG_ERROR("%s: Not enough memory.", __func__); - usbSrvClient.Close(pipe); - return USB_MANAGER_ERROR_NO_MEM; - } - handle->device = device; - handle->pipe = pipe; - *dev_handle = handle; - SANE_HILOG_INFO("%s: end successful", __func__); - return USB_MANAGER_SUCCESS; -} - -void usb_manager_close(usb_manager_device_handle *dev_handle) -{ - SANE_HILOG_INFO("%s: start", __func__); - if (dev_handle == nullptr) { - SANE_HILOG_ERROR("%s: dev_handle is a nullptr", __func__); - return; - } - auto &usbSrvClient = UsbSrvClient::GetInstance(); - usbSrvClient.Close(dev_handle->pipe); - delete dev_handle; - dev_handle = nullptr; - SANE_HILOG_INFO("%s: end", __func__); - return; -} - -int usb_manager_get_configuration(usb_manager_device_handle *dev, int *config) -{ - SANE_HILOG_INFO("%s: start", __func__); - if (dev == nullptr || config == nullptr) { - SANE_HILOG_ERROR("%s: dev or config is a nullptr", __func__); - return USB_MANAGER_ERROR_INVALID_PARAM; - } - UsbDevice &device = dev->device; - auto configCount = device.GetConfigCount(); - SANE_HILOG_DEBUG("%s: configCount = %d", __func__, configCount); - *config = configCount; - return USB_MANAGER_SUCCESS; -} - -int usb_manager_get_config_descriptor(usb_manager_device *dev, uint8_t config_index, - usb_manager_config_descriptor **config) -{ - SANE_HILOG_INFO("%s: begin", __func__); - if (dev == nullptr || config == nullptr || config_index < 0) { - SANE_HILOG_ERROR("%s: input param is invalid!", __func__); - return USB_MANAGER_ERROR_INVALID_PARAM; - } - usb_manager_config_descriptor* retConfig = nullptr; - usb_manager_interface* retInterface = nullptr; - std::vector usbAlt; - auto retStatus = GetRetConfigAndInterface(dev, config_index, retConfig, retInterface, usbAlt); - if (retConfig == nullptr || retInterface == nullptr || retStatus != USB_MANAGER_SUCCESS) { - SANE_HILOG_ERROR("%s: GetRetConfigAndInterface error!", __func__); - return retStatus; - } - retConfig->interface = retInterface; - if (!SetInterfaceDescriptor(usbAlt, retInterface)) { - usb_manager_free_config_descriptor(retConfig); - return USB_MANAGER_ERROR_NO_MEM; - } - *config = retConfig; - g_deviceConfig = retConfig; - SANE_HILOG_INFO("%s: end", __func__); - return USB_MANAGER_SUCCESS; -} - - -void usb_manager_free_config_descriptor(usb_manager_config_descriptor *config) -{ - SANE_HILOG_INFO("%s: start", __func__); - if (config == nullptr) { - SANE_HILOG_ERROR("%s: config is a nullptr", __func__); - return; - } - usb_manager_interface *interface = config->interface; - if (interface == nullptr) { - SANE_HILOG_ERROR("%s: interface is a nullptr", __func__); - delete config; - config = nullptr; - return; - } - // release all interface - for (int i = 0; i < config->bNumInterfaces; i++) { - // release alternate setting - size_t altSet = interface[i].num_altsetting; - auto altsetting = interface[i].altsetting; - if (altsetting == nullptr) { - continue; - } - for (size_t j = 0; j < altSet; j++) { - auto endpoints = altsetting[j].endpoint; - if (endpoints != nullptr) { - // release endpoint array - delete[] endpoints; - } - } - delete[] altsetting; - } - - delete[] interface; - delete config; - config = nullptr; - SANE_HILOG_INFO("%s: end successful", __func__); - return; -} - -void usb_manager_free_device_list(usb_manager_device **list, int unref_devices) -{ - SANE_HILOG_INFO("%s: start", __func__); - if (list == nullptr) { - SANE_HILOG_ERROR("%s: list is a nullptr", __func__); - return; - } - MemoryFreeDeviceList(list); - SANE_HILOG_INFO("%s: end successful", __func__); -} - -int usb_manager_set_configuration(usb_manager_device_handle *dev_handle, int configuration) -{ - SANE_HILOG_INFO("%s: start", __func__); - if (dev_handle == nullptr) { - SANE_HILOG_ERROR("%s: dev_handle is a nullptr", __func__); - return USB_MANAGER_ERROR_INVALID_PARAM; - } - UsbDevice &device = dev_handle->device; - USBDevicePipe pip; - pip.SetBusNum(device.GetBusNum()); - pip.SetDevAddr(device.GetDevAddr()); - USBConfig config; - config.SetId(configuration); - auto ret = UsbSrvClient::GetInstance().SetConfiguration(pip, config); - if (ret != USB_MANAGER_SUCCESS) { - SANE_HILOG_ERROR("SetConfiguration failed ret=%{public}d", ret); - return USB_MANAGER_ERROR_IO; - } - SANE_HILOG_INFO("%s: end successful", __func__); - return USB_MANAGER_SUCCESS; -} - -int usb_manager_claim_interface(usb_manager_device_handle *dev_handle, int interface_number) -{ - SANE_HILOG_INFO("%s: start", __func__); - if (dev_handle == nullptr) { - SANE_HILOG_ERROR("%s: dev_handle is a nullptr", __func__); - return USB_MANAGER_ERROR_INVALID_PARAM; - } - auto &usbSrvClient = UsbSrvClient::GetInstance(); - UsbInterface interface; - interface.SetId(interface_number); - auto ret = usbSrvClient.ClaimInterface(dev_handle->pipe, interface, true); - if (ret != USB_MANAGER_SUCCESS) { - SANE_HILOG_ERROR("ClaimInterface failed ret=%{public}d", ret); - return USB_MANAGER_ERROR_IO; - } - g_interfaceNumber = interface_number; - SANE_HILOG_INFO("%s: end successful", __func__); - return USB_MANAGER_SUCCESS; -} - -int usb_manager_release_interface(usb_manager_device_handle *dev_handle, int interface_number) -{ - SANE_HILOG_INFO("%s: start", __func__); - if (dev_handle == nullptr) { - SANE_HILOG_ERROR("%s: dev_handle is a nullptr", __func__); - return USB_MANAGER_ERROR_INVALID_PARAM; - } - auto &usbSrvClient = UsbSrvClient::GetInstance(); - USBDevicePipe &pipe = dev_handle->pipe; - SANE_HILOG_DEBUG("%s: interface_number : %d", __func__, interface_number); - UsbInterface interface; - interface.SetId(interface_number); - auto ret = usbSrvClient.ReleaseInterface(pipe, interface); - if (ret != USB_MANAGER_SUCCESS) { - SANE_HILOG_ERROR("ReleaseInterface failed ret=%{public}d", ret); - return USB_MANAGER_ERROR_IO; - } - g_interfaceNumber = 0; - SANE_HILOG_INFO("%s: end successful", __func__); - return USB_MANAGER_SUCCESS; -} - -int usb_manager_bulk_transfer(usb_manager_device_handle *dev_handle, unsigned char endpoint, - unsigned char *data, int length, int *transferred, unsigned int timeout) -{ - SANE_HILOG_INFO("%s: start", __func__); - if (dev_handle == nullptr || data == nullptr || length <= 0 || transferred == nullptr) { - SANE_HILOG_ERROR("%s: Invalid input parameter.", __func__); - return USB_MANAGER_ERROR_INVALID_PARAM; - } - USBDevicePipe &pipe = dev_handle->pipe; - USBEndpoint usbEndpoint; - usbEndpoint.SetInterfaceId(g_interfaceNumber); - usbEndpoint.SetAddr(endpoint); - auto &usbSrvClient = UsbSrvClient::GetInstance(); - if (endpoint & USB_MANAGER_ENDPOINT_IN) { - // read - std::vector bufferData(length); - auto ret = usbSrvClient.BulkTransfer(pipe, usbEndpoint, bufferData, timeout); - if (ret != USB_MANAGER_SUCCESS) { - SANE_HILOG_INFO("%s: error", __func__); - return USB_MANAGER_ERROR_IO; - } - for (size_t i = 0; i < bufferData.size(); i++) { - data[i] = bufferData[i]; - } - } else { - // write - std::vector bufferData(length); - for (int i = 0; i < length; i++) { - bufferData[i] = data[i]; - } - auto ret = usbSrvClient.BulkTransfer(pipe, usbEndpoint, bufferData, timeout); - if (ret != USB_MANAGER_SUCCESS) { - SANE_HILOG_INFO("%s: error", __func__); - return USB_MANAGER_ERROR_IO; - } - } - *transferred = length; - SANE_HILOG_INFO("%s: end successful", __func__); - return USB_MANAGER_SUCCESS; -} - -int usb_manager_control_transfer(usb_manager_device_handle *dev_handle, uint8_t request_type, - uint8_t bRequest, uint16_t wValue, uint16_t wIndex, unsigned char *data, uint16_t wLength, - unsigned int timeout) -{ - SANE_HILOG_INFO("%s: start", __func__); - if (dev_handle == nullptr || data == nullptr || wLength == 0) { - return USB_MANAGER_ERROR_INVALID_PARAM; - } - USBDevicePipe &pipe = dev_handle->pipe; - OHOS::HDI::Usb::V1_0::UsbCtrlTransfer ctrl; - ctrl.requestType = static_cast(request_type); - ctrl.requestCmd = static_cast(bRequest); - ctrl.value = static_cast(wValue); - ctrl.index = static_cast(wIndex); - ctrl.timeout = static_cast(timeout); - - auto &usbSrvClient = UsbSrvClient::GetInstance(); - std::vector bufferData(wLength); - for (uint16_t i = 0; i < wLength; i++) { - bufferData[i] = data[i]; - } - auto ret = usbSrvClient.ControlTransfer(pipe, ctrl, bufferData); - if (ret != USB_MANAGER_SUCCESS) { - SANE_HILOG_ERROR("ControlTransfer failed ret=%{public}d", ret); - return USB_MANAGER_ERROR_IO; - } - SANE_HILOG_INFO("%s: end successful", __func__); - return USB_MANAGER_SUCCESS; -} - -int usb_manager_set_interface_alt_setting(usb_manager_device_handle *dev_handle, int interface_number, - int alternate_setting) -{ - SANE_HILOG_INFO("%s: start", __func__); - if (dev_handle == nullptr) { - return USB_MANAGER_ERROR_INVALID_PARAM; - } - auto &usbSrvClient = UsbSrvClient::GetInstance(); - USBDevicePipe pipe = dev_handle->pipe; - UsbInterface interface; - interface.SetId(interface_number); - interface.SetAlternateSetting(alternate_setting); - auto ret = usbSrvClient.SetInterface(pipe, interface); - if (ret != USB_MANAGER_SUCCESS) { - SANE_HILOG_ERROR("SetInterface failed ret=%{public}d", ret); - return USB_MANAGER_ERROR_IO; - } - SANE_HILOG_INFO("%s: end successful", __func__); - return USB_MANAGER_SUCCESS; -} - -usb_manager_device* usb_manager_ref_device(usb_manager_device *dev) -{ - SANE_HILOG_INFO("%s: start", __func__); - if (dev == nullptr) { - SANE_HILOG_ERROR("%s: dev is a nullptr!", __func__); - return nullptr; - } - usb_manager_device* refDevice = new (std::nothrow) usb_manager_device(); - if (refDevice == nullptr) { - SANE_HILOG_ERROR("%s: create refDevice error!", __func__); - return nullptr; - } - refDevice->device = dev->device; - g_refDevices.push_back(refDevice); - SANE_HILOG_INFO("%s: end successful", __func__); - return refDevice; -} - -// stub -int usb_manager_interrupt_transfer(usb_manager_device_handle *dev_handle, unsigned char endpoint, - unsigned char *data, int length, int *actual_length, unsigned int timeout) -{ - SANE_HILOG_INFO("%s: start", __func__); - SANE_HILOG_INFO("%s: end successful", __func__); - return USB_MANAGER_SUCCESS; -} - -// stub -int usb_manager_clear_halt(usb_manager_device_handle *dev_handle, unsigned char endpoint) -{ - SANE_HILOG_INFO("%s: start", __func__); - SANE_HILOG_INFO("%s: end successful", __func__); - return USB_MANAGER_SUCCESS; -} - -// stub -int usb_manager_reset_device(usb_manager_device_handle *dev_handle) -{ - SANE_HILOG_INFO("%s: start", __func__); - SANE_HILOG_INFO("%s: end successful", __func__); - return USB_MANAGER_SUCCESS; -} \ No newline at end of file -- Gitee