1/*
2   Copyright (C) 2024 Huawei Device Co., Ltd.
3
4   This file is part of the SANE package.
5
6   This program is free software; you can redistribute it and/or
7   modify it under the terms of the GNU General Public License as
8   published by the Free Software Foundation; either version 2 of the
9   License, or (at your option) any later version.
10
11   This program is distributed in the hope that it will be useful, but
12   WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with this program.  If not, see <https://www.gnu.org/licenses/>.
18
19   As a special exception, the authors of SANE give permission for
20   additional uses of the libraries contained in this release of SANE.
21
22   The exception is that, if you link a SANE library with other files
23   to produce an executable, this does not by itself cause the
24   resulting executable to be covered by the GNU General Public
25   License.  Your use of that executable is in no way restricted on
26   account of linking the SANE library code into it.
27
28   This exception does not, however, invalidate any other reasons why
29   the executable file might be covered by the GNU General Public
30   License.
31
32   If you submit changes to SANE to the maintainers to be included in
33   a subsequent release, you agree by submitting the changes that
34   those changes may be distributed with this exception intact.
35
36   If you write modifications of your own for SANE, it is your choice
37   whether to permit this exception to apply to your modifications.
38   If you do not wish that, delete this exception notice.
39
40   This file provides an interface to use the usb manager.*/
41
42#include <cstring>
43#include <string>
44#include "iusb_srv.h"
45#include "securec.h"
46#include "cstring"
47#include "usb_srv_client.h"
48#include "usb_manager.h"
49#include "v1_0/iusb_interface.h"
50#include "hilog/log.h"
51
52#define SANE_LOG_TAG "sanekit"
53#define SANE_HILOG_INFO(...) ((void)HiLogPrint(LOG_APP, LOG_INFO, 0, "sanekit", __VA_ARGS__))
54#define SANE_HILOG_ERROR(...) ((void)HiLogPrint(LOG_APP, LOG_ERROR, 0, "sanekit", __VA_ARGS__))
55#define SANE_HILOG_DEBUG(...) ((void)HiLogPrint(LOG_APP, LOG_DEBUG, 0, "sanekit", __VA_ARGS__))
56#define SANE_HILOG_WARN(...) ((void)HiLogPrint(LOG_APP, LOG_WARN, 0, "sanekit", __VA_ARGS__))
57
58using namespace OHOS::USB;
59
60static std::vector<UsbDevice> g_deviceList;
61static ssize_t g_deviceCount = 0;
62static uint8_t g_interfaceNumber = 0;
63static usb_manager_config_descriptor* g_deviceConfig = nullptr;
64static std::vector<usb_manager_device*> g_refDevices;
65constexpr int maxUsbInterfaceNum = 1000;
66constexpr int maxUsbEndpointNum = 1000;
67
68struct usb_manager_device_handle {
69    UsbDevice device;
70    USBDevicePipe pipe;
71};
72
73struct usb_manager_device {
74    UsbDevice device;
75};
76
77struct UsbEndPointDes {
78    uint8_t bmAttributes;
79    uint8_t  bEndpointAddress;
80};
81
82struct UsbInterfaceAltDes{
83    uint8_t bInterfaceClass;
84    std::vector<UsbEndPointDes> endpointDes;
85};
86
87struct UsbAltInterface {
88    std::vector<UsbInterfaceAltDes> altDes;
89};
90
91namespace {
92    void MemoryFreeDeviceList(usb_manager_device **list)
93    {
94        if (list == nullptr) {
95            return;
96        }
97        for (ssize_t i = 0 ; i < g_deviceCount; i++) {
98            if (list[i] != nullptr) {
99                delete list[i];
100            }
101        }
102        delete[] list;
103        list = nullptr;
104        g_deviceCount = 0;
105    }
106
107    void InterfaceConvert(std::vector<UsbInterface> &usbInterfaces, std::vector<UsbAltInterface> &usbAlt)
108    {
109        auto memCopyInter = [&](size_t &i, UsbAltInterface &altInter) {
110            UsbInterfaceAltDes des;
111            des.bInterfaceClass = static_cast<uint8_t>(usbInterfaces[i].GetClass());
112            std::vector<USBEndpoint> &endPoints = usbInterfaces[i].GetEndpoints();
113            for (size_t j = 0; j < endPoints.size(); j++) {
114                UsbEndPointDes pointDes;
115                pointDes.bmAttributes = static_cast<uint8_t>(endPoints[j].GetAttributes());
116                pointDes.bEndpointAddress = static_cast<uint8_t>(endPoints[j].GetAddress());
117                des.endpointDes.push_back(pointDes);
118            }
119            altInter.altDes.push_back(des);
120        };
121        for (size_t i = 0; i < usbInterfaces.size(); i++) {
122            UsbInterface& inter = usbInterfaces[i];
123            if (inter.GetAlternateSetting() == 0) {
124                UsbAltInterface altInter;
125                memCopyInter(i, altInter);
126                usbAlt.push_back(altInter);
127            } else {
128                int id = usbInterfaces[i].GetId();
129                UsbAltInterface &altInter = usbAlt[id];
130                memCopyInter(i, altInter);
131            }
132        }
133    }
134
135    int GetRetConfigAndInterface(usb_manager_device *dev, uint8_t config_index, usb_manager_config_descriptor* &retConfig,
136        usb_manager_interface* &retInterface, std::vector<UsbAltInterface> &usbAlt)
137    {
138        std::vector<USBConfig> &configs = dev->device.GetConfigs();
139        if (config_index >= configs.size()) {
140            SANE_HILOG_ERROR("%s: config_index(%u) is invalid.", __func__, config_index);
141            return USB_MANAGER_ERROR_INVALID_PARAM;
142        }
143        USBConfig &usbConfig = configs[config_index];
144        SANE_HILOG_DEBUG("%s: config[%u] = %s", __func__, config_index, usbConfig.getJsonString().c_str());
145        std::vector<UsbInterface> &usbInterfaces = usbConfig.GetInterfaces();
146        InterfaceConvert(usbInterfaces, usbAlt);
147        size_t bNumInterfaces = usbAlt.size();
148        if (bNumInterfaces == 0 || bNumInterfaces > maxUsbInterfaceNum) {
149            SANE_HILOG_ERROR("%s: bNumInterfaces(%u) is invalid.", __func__, bNumInterfaces);
150            return USB_MANAGER_ERROR_OTHER;
151        }
152        retConfig = new (std::nothrow) usb_manager_config_descriptor();
153        if (retConfig == nullptr) {
154            SANE_HILOG_ERROR("%s: Not enough memory.", __func__);
155            return USB_MANAGER_ERROR_NO_MEM;
156        }
157        if (memset_s(retConfig, sizeof(usb_manager_config_descriptor), 0, sizeof(usb_manager_config_descriptor)) != 0) {
158            SANE_HILOG_ERROR("%s: memset retConfig error.", __func__);
159            delete retConfig;
160            retConfig = nullptr;
161            return USB_MANAGER_ERROR_OTHER;
162        }
163        retConfig->bNumInterfaces = static_cast<uint8_t>(bNumInterfaces);
164        retConfig->bConfigurationValue = static_cast<uint8_t>(usbConfig.GetId());
165        retInterface = new (std::nothrow) usb_manager_interface[bNumInterfaces]{};
166        if (retInterface == nullptr) {
167            SANE_HILOG_ERROR("%s: Not enough memory.", __func__);
168            delete retConfig;
169            return USB_MANAGER_ERROR_NO_MEM;
170        }
171        retConfig->interface = retInterface;
172        return USB_MANAGER_SUCCESS;
173    }
174
175    bool SetInterfaceDescriptor(std::vector<UsbAltInterface> &usbAlt, usb_manager_interface* retInterface)
176    {
177        if (retInterface == nullptr) {
178            return false;
179        }
180        for (int i = 0; i < usbAlt.size(); i++) {
181            size_t altSet = usbAlt[i].altDes.size();
182            if (altSet > maxUsbInterfaceNum) {
183                return false;
184            }
185            auto retAltsetting = new (std::nothrow) usb_manager_interface_descriptor[altSet]{};
186            if (retAltsetting == nullptr) {
187                return false;
188            }
189            retInterface[i].altsetting = retAltsetting;
190            retInterface[i].num_altsetting = static_cast<int>(altSet);
191            for (size_t j = 0; j < altSet; j++) {
192                retAltsetting[j].bInterfaceClass = usbAlt[i].altDes[j].bInterfaceClass;
193                size_t endPointsNum = usbAlt[i].altDes[j].endpointDes.size();
194                if (endPointsNum > maxUsbEndpointNum) {
195                    return false;
196                }
197                retAltsetting[j].bNumEndpoints = endPointsNum;
198                retAltsetting[j].endpoint = new (std::nothrow) usb_manager_endpoint_descriptor[endPointsNum]{};
199                if (retAltsetting[j].endpoint == nullptr) {
200                    return false;
201                }
202                for (int k = 0 ; k < endPointsNum; k++) {
203                    uint8_t bmAttr = usbAlt[i].altDes[j].endpointDes[k].bmAttributes;
204                    uint8_t bEndpointAddress = usbAlt[i].altDes[j].endpointDes[k].bEndpointAddress;
205                    retAltsetting[j].endpoint[k].bmAttributes = bmAttr;
206                    retAltsetting[j].endpoint[k].bEndpointAddress = bEndpointAddress;
207                }
208            }
209        }
210        return true;
211    }
212};
213
214int usb_manager_init(usb_manager_context **ctx)
215{
216    SANE_HILOG_INFO("%s: begin", __func__);
217    if (ctx == nullptr) {
218        SANE_HILOG_ERROR("%s: ctx is a nullptr.", __func__);
219        return USB_MANAGER_ERROR_INVALID_PARAM;
220    }
221    usb_manager_context *usbmanagerContext = new (std::nothrow) usb_manager_context();
222    if (usbmanagerContext == nullptr) {
223        SANE_HILOG_ERROR("%s: Not enough memory.", __func__);
224        return USB_MANAGER_ERROR_NO_MEM;
225    }
226    if (memset_s(usbmanagerContext, sizeof(usb_manager_context), 0 ,sizeof(usb_manager_context)) != 0) {
227        SANE_HILOG_ERROR("%s: memset_s usbmanagerContext error.", __func__);
228        delete usbmanagerContext;
229        return USB_MANAGER_ERROR_OTHER;
230    }
231    *ctx = usbmanagerContext;
232    SANE_HILOG_INFO("%s: end successful", __func__);
233    return USB_MANAGER_SUCCESS;
234}
235
236int usb_manager_exit(usb_manager_context *ctx)
237{
238    SANE_HILOG_INFO("%s: begin", __func__);
239    if (ctx == nullptr) {
240        SANE_HILOG_ERROR("%s: ctx is a nullptr.", __func__);
241        return USB_MANAGER_ERROR_INVALID_PARAM;
242    }
243    delete ctx;
244    ctx = nullptr;
245    for (const auto& refDevice : g_refDevices) {
246        if (refDevice != nullptr) {
247            delete refDevice;
248        }
249    }
250    g_refDevices.clear();
251    SANE_HILOG_INFO("%s: end successful", __func__);
252    return USB_MANAGER_SUCCESS;
253}
254
255ssize_t usb_manager_get_device_list(usb_manager_context *ctx, usb_manager_device ***list)
256{
257    SANE_HILOG_INFO("%s: begin", __func__);
258    if (ctx == nullptr || list == nullptr) {
259        SANE_HILOG_ERROR("%s: ctx or list is a nullptr.", __func__);
260        return USB_MANAGER_ERROR_INVALID_PARAM;
261    }
262    g_deviceList.clear();
263    auto ret = UsbSrvClient::GetInstance().GetDevices(g_deviceList);
264    if (ret != USB_MANAGER_SUCCESS) {
265        SANE_HILOG_ERROR("%s: GetDevices exception.", __func__);
266        return USB_MANAGER_ERROR_IO;
267    }
268    g_deviceCount = static_cast<ssize_t>(g_deviceList.size());
269    SANE_HILOG_DEBUG("%s: deviceCount : %d", __func__, g_deviceCount);
270    if (g_deviceCount == 0) {
271        SANE_HILOG_DEBUG("%s: not found device.", __func__);
272        return USB_MANAGER_SUCCESS;
273    }
274    usb_manager_device** usbManagerDeviceList = new (std::nothrow) usb_manager_device* [g_deviceCount]{};
275    if (usbManagerDeviceList == nullptr) {
276        SANE_HILOG_ERROR("%s: Not enough memory.", __func__);
277        return USB_MANAGER_ERROR_NO_MEM;
278    }
279    for (ssize_t i = 0; i < g_deviceCount; i++) {
280        usb_manager_device* usbDevice = new (std::nothrow) usb_manager_device();
281        if (usbDevice == nullptr) {
282            SANE_HILOG_ERROR("%s: Not enough memory.", __func__);
283            MemoryFreeDeviceList(usbManagerDeviceList);
284            return USB_MANAGER_ERROR_NO_MEM;
285        }
286        usbManagerDeviceList[i] = usbDevice;
287        usbDevice->device = g_deviceList[i];
288    }
289    *list = usbManagerDeviceList;
290    SANE_HILOG_INFO("%s: end successful", __func__);
291    return g_deviceCount;
292}
293
294int usb_manager_get_bus_number(usb_manager_device *dev)
295{
296    SANE_HILOG_INFO("%s: start", __func__);
297    if (dev == nullptr) {
298        SANE_HILOG_ERROR("%s: dev is a nullptr.", __func__);
299        return USB_MANAGER_ERROR_INVALID_PARAM;
300    }
301    SANE_HILOG_INFO("%s: end successful", __func__);
302    return dev->device.GetBusNum();
303}
304
305int usb_manager_get_device_address(usb_manager_device *dev)
306{
307    SANE_HILOG_INFO("%s: start", __func__);
308    if (dev == nullptr) {
309        SANE_HILOG_ERROR("%s: dev is a nullptr.", __func__);
310        return USB_MANAGER_ERROR_INVALID_PARAM;
311    }
312    SANE_HILOG_INFO("%s: end successful", __func__);
313    return dev->device.GetDevAddr();
314}
315
316int usb_manager_get_device_descriptor(usb_manager_device *dev, usb_manager_device_descriptor *desc)
317{
318    SANE_HILOG_INFO("%s: start", __func__);
319    if (dev == nullptr || desc == nullptr) {
320        SANE_HILOG_ERROR("%s: dev or desc is a nullptr.", __func__);
321        return USB_MANAGER_ERROR_INVALID_PARAM;
322    }
323    UsbDevice &device = dev->device;
324    desc->idVendor = static_cast<uint16_t>(device.GetVendorId());
325    desc->idProduct = static_cast<uint16_t>(device.GetProductId());
326    desc->bDeviceClass = static_cast<uint8_t>(device.GetClass());
327    desc->bNumConfigurations = static_cast<uint8_t>(device.GetConfigCount());
328    desc->bDeviceSubClass = static_cast<uint8_t>(device.GetSubclass());
329    desc->bDeviceProtocol = static_cast<uint8_t>(device.GetProtocol());
330    SANE_HILOG_INFO("%s: end successful", __func__);
331    return USB_MANAGER_SUCCESS;
332}
333
334int usb_manager_open(usb_manager_device *dev, usb_manager_device_handle **dev_handle)
335{
336    SANE_HILOG_INFO("%s: start", __func__);
337    if (dev == nullptr || dev_handle == nullptr) {
338        SANE_HILOG_ERROR("%s: dev or dev_handle is a nullptr.", __func__);
339        return USB_MANAGER_ERROR_INVALID_PARAM;
340    }
341    auto &usbSrvClient = UsbSrvClient::GetInstance();
342    USBDevicePipe pipe;
343    UsbDevice &device = dev->device;
344    auto ret = usbSrvClient.RequestRight(device.GetName());
345    if (ret != USB_MANAGER_SUCCESS) {
346        SANE_HILOG_ERROR("request right failed ret=%{public}d", ret);
347        return USB_MANAGER_ERROR_ACCESS;
348    }
349    ret = usbSrvClient.OpenDevice(device, pipe);
350    if (ret != USB_MANAGER_SUCCESS) {
351        SANE_HILOG_ERROR("open device failed ret=%{public}d", ret);
352        return USB_MANAGER_ERROR_ACCESS;
353    }
354    auto handle = new (std::nothrow) usb_manager_device_handle();
355    if (handle == nullptr) {
356        SANE_HILOG_ERROR("%s: Not enough memory.", __func__);
357        usbSrvClient.Close(pipe);
358        return USB_MANAGER_ERROR_NO_MEM;
359    }
360    handle->device = device;
361    handle->pipe = pipe;
362    *dev_handle = handle;
363    SANE_HILOG_INFO("%s: end successful", __func__);
364    return USB_MANAGER_SUCCESS;
365}
366
367void usb_manager_close(usb_manager_device_handle *dev_handle)
368{
369    SANE_HILOG_INFO("%s: start", __func__);
370    if (dev_handle == nullptr) {
371        SANE_HILOG_ERROR("%s: dev_handle is a nullptr", __func__);
372        return;
373    }
374    auto &usbSrvClient = UsbSrvClient::GetInstance();
375    usbSrvClient.Close(dev_handle->pipe);
376    delete dev_handle;
377    dev_handle = nullptr;
378    SANE_HILOG_INFO("%s: end", __func__);
379    return;
380}
381
382int usb_manager_get_configuration(usb_manager_device_handle *dev, int *config)
383{
384    SANE_HILOG_INFO("%s: start", __func__);
385    if (dev == nullptr || config == nullptr) {
386        SANE_HILOG_ERROR("%s: dev or config is a nullptr", __func__);
387        return USB_MANAGER_ERROR_INVALID_PARAM;
388    }
389    UsbDevice &device = dev->device;
390    auto configCount = device.GetConfigCount();
391    SANE_HILOG_DEBUG("%s: configCount = %d", __func__, configCount);
392    *config = configCount;
393    return USB_MANAGER_SUCCESS;
394}
395
396int usb_manager_get_config_descriptor(usb_manager_device *dev, uint8_t config_index,
397    usb_manager_config_descriptor **config)
398{
399    SANE_HILOG_INFO("%s: begin", __func__);
400    if (dev == nullptr || config == nullptr || config_index < 0) {
401        SANE_HILOG_ERROR("%s: input param is invalid!", __func__);
402        return USB_MANAGER_ERROR_INVALID_PARAM;
403    }
404    usb_manager_config_descriptor* retConfig = nullptr;
405    usb_manager_interface* retInterface = nullptr;
406    std::vector<UsbAltInterface> usbAlt;
407    auto retStatus = GetRetConfigAndInterface(dev, config_index, retConfig, retInterface, usbAlt);
408    if (retConfig == nullptr || retInterface == nullptr || retStatus != USB_MANAGER_SUCCESS) {
409        SANE_HILOG_ERROR("%s: GetRetConfigAndInterface error!", __func__);
410        return retStatus;
411    }
412    retConfig->interface = retInterface;
413    if (!SetInterfaceDescriptor(usbAlt, retInterface)) {
414        usb_manager_free_config_descriptor(retConfig);
415        return USB_MANAGER_ERROR_NO_MEM;
416    }
417    *config = retConfig;
418    g_deviceConfig = retConfig;
419    SANE_HILOG_INFO("%s: end", __func__);
420    return USB_MANAGER_SUCCESS;
421}
422
423
424void usb_manager_free_config_descriptor(usb_manager_config_descriptor *config)
425{
426    SANE_HILOG_INFO("%s: start", __func__);
427    if (config == nullptr) {
428        SANE_HILOG_ERROR("%s: config is a nullptr", __func__);
429        return;
430    }
431    usb_manager_interface *interface = config->interface;
432    if (interface == nullptr) {
433        SANE_HILOG_ERROR("%s: interface is a nullptr", __func__);
434        delete config;
435        config = nullptr;
436        return;
437    }
438    // release all interface
439    for (int i = 0; i < config->bNumInterfaces; i++) {
440        // release alternate setting
441        size_t altSet = interface[i].num_altsetting;
442        auto altsetting = interface[i].altsetting;
443        if (altsetting == nullptr) {
444            continue;
445        }
446        for (size_t j = 0; j < altSet; j++) {
447            auto endpoints = altsetting[j].endpoint;
448            if (endpoints != nullptr) {
449                // release endpoint array
450                delete[] endpoints;
451            }
452        }
453        delete[] altsetting;
454    }
455
456    delete[] interface;
457    delete config;
458    config = nullptr;
459    SANE_HILOG_INFO("%s: end successful", __func__);
460    return;
461}
462
463void usb_manager_free_device_list(usb_manager_device **list, int unref_devices)
464{
465    SANE_HILOG_INFO("%s: start", __func__);
466    if (list == nullptr) {
467        SANE_HILOG_ERROR("%s: list is a nullptr", __func__);
468        return;
469    }
470    MemoryFreeDeviceList(list);
471    SANE_HILOG_INFO("%s: end successful", __func__);
472}
473
474int usb_manager_set_configuration(usb_manager_device_handle *dev_handle, int configuration)
475{
476    SANE_HILOG_INFO("%s: start", __func__);
477    if (dev_handle == nullptr) {
478        SANE_HILOG_ERROR("%s: dev_handle is a nullptr", __func__);
479        return USB_MANAGER_ERROR_INVALID_PARAM;
480    }
481    UsbDevice &device = dev_handle->device;
482    USBDevicePipe pip;
483    pip.SetBusNum(device.GetBusNum());
484    pip.SetDevAddr(device.GetDevAddr());
485    USBConfig config;
486    config.SetId(configuration);
487    auto ret = UsbSrvClient::GetInstance().SetConfiguration(pip, config);
488    if (ret != USB_MANAGER_SUCCESS) {
489        SANE_HILOG_ERROR("SetConfiguration failed ret=%{public}d", ret);
490        return USB_MANAGER_ERROR_IO;
491    }
492    SANE_HILOG_INFO("%s: end successful", __func__);
493    return USB_MANAGER_SUCCESS;
494}
495
496int usb_manager_claim_interface(usb_manager_device_handle *dev_handle, int interface_number)
497{
498    SANE_HILOG_INFO("%s: start", __func__);
499    if (dev_handle == nullptr) {
500        SANE_HILOG_ERROR("%s: dev_handle is a nullptr", __func__);
501        return USB_MANAGER_ERROR_INVALID_PARAM;
502    }
503    auto &usbSrvClient = UsbSrvClient::GetInstance();
504    UsbInterface interface;
505    interface.SetId(interface_number);
506    auto ret = usbSrvClient.ClaimInterface(dev_handle->pipe, interface, true);
507    if (ret != USB_MANAGER_SUCCESS) {
508        SANE_HILOG_ERROR("ClaimInterface failed ret=%{public}d", ret);
509        return USB_MANAGER_ERROR_IO;
510    }
511    g_interfaceNumber = interface_number;
512    SANE_HILOG_INFO("%s: end successful", __func__);
513    return USB_MANAGER_SUCCESS;
514}
515
516int usb_manager_release_interface(usb_manager_device_handle *dev_handle, int interface_number)
517{
518    SANE_HILOG_INFO("%s: start", __func__);
519    if (dev_handle == nullptr) {
520        SANE_HILOG_ERROR("%s: dev_handle is a nullptr", __func__);
521        return USB_MANAGER_ERROR_INVALID_PARAM;
522    }
523    auto &usbSrvClient = UsbSrvClient::GetInstance();
524    USBDevicePipe &pipe = dev_handle->pipe;
525    SANE_HILOG_DEBUG("%s: interface_number : %d", __func__, interface_number);
526    UsbInterface interface;
527    interface.SetId(interface_number);
528    auto ret = usbSrvClient.ReleaseInterface(pipe, interface);
529    if (ret != USB_MANAGER_SUCCESS) {
530        SANE_HILOG_ERROR("ReleaseInterface failed ret=%{public}d", ret);
531        return USB_MANAGER_ERROR_IO;
532    }
533    g_interfaceNumber = 0;
534    SANE_HILOG_INFO("%s: end successful", __func__);
535    return USB_MANAGER_SUCCESS;
536}
537
538int usb_manager_bulk_transfer(usb_manager_device_handle *dev_handle, unsigned char endpoint,
539    unsigned char *data, int length, int *transferred, unsigned int timeout)
540{
541    SANE_HILOG_INFO("%s: start", __func__);
542    if (dev_handle == nullptr || data == nullptr || length <= 0 || transferred == nullptr) {
543        SANE_HILOG_ERROR("%s: Invalid input parameter.", __func__);
544        return USB_MANAGER_ERROR_INVALID_PARAM;
545    }
546    USBDevicePipe &pipe = dev_handle->pipe;
547    USBEndpoint usbEndpoint;
548    usbEndpoint.SetInterfaceId(g_interfaceNumber);
549    usbEndpoint.SetAddr(endpoint);
550    auto &usbSrvClient = UsbSrvClient::GetInstance();
551    if (endpoint & USB_MANAGER_ENDPOINT_IN) {
552        // read
553        std::vector<uint8_t> bufferData(length);
554        auto ret = usbSrvClient.BulkTransfer(pipe, usbEndpoint, bufferData, timeout);
555        if (ret != USB_MANAGER_SUCCESS) {
556            SANE_HILOG_INFO("%s: error", __func__);
557            return USB_MANAGER_ERROR_IO;
558        }
559        for (size_t i = 0; i < bufferData.size(); i++) {
560            data[i] = bufferData[i];
561        }
562    } else {
563        // write
564        std::vector<uint8_t> bufferData(length);
565        for (int i = 0; i < length; i++) {
566            bufferData[i] = data[i];
567        }
568        auto ret = usbSrvClient.BulkTransfer(pipe, usbEndpoint, bufferData, timeout);
569        if (ret != USB_MANAGER_SUCCESS) {
570            SANE_HILOG_INFO("%s: error", __func__);
571            return USB_MANAGER_ERROR_IO;
572        }
573    }
574    *transferred = length;
575    SANE_HILOG_INFO("%s: end successful", __func__);
576    return USB_MANAGER_SUCCESS;
577}
578
579int usb_manager_control_transfer(usb_manager_device_handle *dev_handle, uint8_t request_type,
580    uint8_t bRequest, uint16_t wValue, uint16_t wIndex, unsigned char *data, uint16_t wLength,
581    unsigned int timeout)
582{
583    SANE_HILOG_INFO("%s: start", __func__);
584    if (dev_handle == nullptr || data == nullptr || wLength == 0) {
585        return USB_MANAGER_ERROR_INVALID_PARAM;
586    }
587    USBDevicePipe &pipe = dev_handle->pipe;
588    OHOS::HDI::Usb::V1_0::UsbCtrlTransfer ctrl;
589    ctrl.requestType = static_cast<int32_t>(request_type);
590    ctrl.requestCmd = static_cast<int32_t>(bRequest);
591    ctrl.value = static_cast<int32_t>(wValue);
592    ctrl.index = static_cast<int32_t>(wIndex);
593    ctrl.timeout = static_cast<int32_t>(timeout);
594
595    auto &usbSrvClient = UsbSrvClient::GetInstance();
596    std::vector<uint8_t> bufferData(wLength);
597    for (uint16_t i = 0; i < wLength; i++) {
598        bufferData[i] = data[i];
599    }
600    auto ret = usbSrvClient.ControlTransfer(pipe, ctrl, bufferData);
601    if (ret != USB_MANAGER_SUCCESS) {
602        SANE_HILOG_ERROR("ControlTransfer failed ret=%{public}d", ret);
603        return USB_MANAGER_ERROR_IO;
604    }
605    SANE_HILOG_INFO("%s: end successful", __func__);
606    return USB_MANAGER_SUCCESS;
607}
608
609int usb_manager_set_interface_alt_setting(usb_manager_device_handle *dev_handle, int interface_number,
610    int alternate_setting)
611{
612    SANE_HILOG_INFO("%s: start", __func__);
613    if (dev_handle == nullptr) {
614        return USB_MANAGER_ERROR_INVALID_PARAM;
615    }
616    auto &usbSrvClient = UsbSrvClient::GetInstance();
617    USBDevicePipe pipe = dev_handle->pipe;
618    UsbInterface interface;
619    interface.SetId(interface_number);
620    interface.SetAlternateSetting(alternate_setting);
621    auto ret = usbSrvClient.SetInterface(pipe, interface);
622    if (ret != USB_MANAGER_SUCCESS) {
623        SANE_HILOG_ERROR("SetInterface failed ret=%{public}d", ret);
624        return USB_MANAGER_ERROR_IO;
625    }
626    SANE_HILOG_INFO("%s: end successful", __func__);
627    return USB_MANAGER_SUCCESS;
628}
629
630usb_manager_device* usb_manager_ref_device(usb_manager_device *dev)
631{
632    SANE_HILOG_INFO("%s: start", __func__);
633    if (dev == nullptr) {
634        SANE_HILOG_ERROR("%s: dev is a nullptr!", __func__);
635        return nullptr;
636    }
637    usb_manager_device* refDevice = new (std::nothrow) usb_manager_device();
638    if (refDevice == nullptr) {
639        SANE_HILOG_ERROR("%s: create refDevice error!", __func__);
640        return nullptr;
641    }
642    refDevice->device = dev->device;
643    g_refDevices.push_back(refDevice);
644    SANE_HILOG_INFO("%s: end successful", __func__);
645    return refDevice;
646}
647
648// stub
649int usb_manager_interrupt_transfer(usb_manager_device_handle *dev_handle, unsigned char endpoint,
650    unsigned char *data, int length, int *actual_length, unsigned int timeout)
651{
652    SANE_HILOG_INFO("%s: start", __func__);
653    SANE_HILOG_INFO("%s: end successful", __func__);
654    return USB_MANAGER_SUCCESS;
655}
656
657// stub
658int usb_manager_clear_halt(usb_manager_device_handle *dev_handle, unsigned char endpoint)
659{
660    SANE_HILOG_INFO("%s: start", __func__);
661    SANE_HILOG_INFO("%s: end successful", __func__);
662    return USB_MANAGER_SUCCESS;
663}
664
665// stub
666int usb_manager_reset_device(usb_manager_device_handle *dev_handle)
667{
668    SANE_HILOG_INFO("%s: start", __func__);
669    SANE_HILOG_INFO("%s: end successful", __func__);
670    return USB_MANAGER_SUCCESS;
671}