1 /*
2  * Copyright (C) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include "ffi_utils.h"
16 #include "oh_usb.h"
17 #include "usb_util.h"
18 #include "log.h"
19 
20 #include <string>
21 
22 namespace Hdc {
ConfigEpPointEx(const char* path)23 extern "C" int32_t ConfigEpPointEx(const char* path)
24 {
25     int ep;
26     if (ConfigEpPoint(ep, std::string(path)) != 0) {
27         WRITE_LOG(LOG_WARN, "open ep failed");
28         return -1;
29     }
30     return static_cast<int32_t>(ep);
31 }
32 
OpenEpPointEx(const char* path)33 extern "C" int32_t OpenEpPointEx(const char* path)
34 {
35     int fd = -1;
36     if (OpenEpPoint(fd, std::string(path)) != 0) {
37         WRITE_LOG(LOG_WARN, "open ep failed");
38         return -1;
39     }
40     return static_cast<int32_t>(fd);
41 }
42 
CloseUsbFdEx(int32_t fd)43 extern "C" int32_t CloseUsbFdEx(int32_t fd)
44 {
45     return static_cast<int32_t>(CloseUsbFd(fd));
46 }
47 
CloseEndPointEx(int32_t bulkInFd, int32_t bulkOutFd, int32_t ctrlEp, uint8_t closeCtrlEp)48 extern "C" void CloseEndPointEx(int32_t bulkInFd, int32_t bulkOutFd, int32_t ctrlEp,
49                                 uint8_t closeCtrlEp)
50 {
51     CloseEndpoint(bulkInFd, bulkOutFd, ctrlEp, closeCtrlEp);
52 }
53 
WriteUsbDevEx(int32_t bulkOut, SerializedBuffer buf)54 extern "C" int32_t WriteUsbDevEx(int32_t bulkOut, SerializedBuffer buf)
55 {
56     return static_cast<int32_t>(WriteData(bulkOut, reinterpret_cast<uint8_t *>(buf.ptr),
57                                           static_cast<size_t>(buf.size)));
58 }
59 
60 uint8_t *g_bufRet = nullptr;
61 
62 struct PersistBuffer {
63     uint8_t *ptr;
64     uint64_t size;
65 };
66 
ReadUsbDevEx(int32_t bulkIn, uint8_t *buf, const size_t size)67 extern "C" size_t ReadUsbDevEx(int32_t bulkIn, uint8_t *buf, const size_t size)
68 {
69     return ReadData(bulkIn, buf, size);
70 }
71 
GetDevPathEx(const char *path)72 extern "C" char *GetDevPathEx(const char *path)
73 {
74     std::string basePath = GetDevPath(std::string(path));
75     size_t buf_size = basePath.length() + 1;
76     char *bufRet = new char[buf_size];
77     (void)memset_s(bufRet, buf_size, 0, buf_size);
78     (void)memcpy_s(bufRet, buf_size, basePath.c_str(), buf_size);
79     return bufRet;
80 }
81 
82 }
83