• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     char *ptr;
64     uint64_t size;
65 };
66 
ReadUsbDevEx(int32_t bulkIn)67 extern "C" PersistBuffer ReadUsbDevEx(int32_t bulkIn)
68 {
69     if (g_bufRet == nullptr) {
70         WRITE_LOG(LOG_WARN, "remalloc g_bufRet\n");
71         g_bufRet = new uint8_t[MAX_SIZE_IOBUF];
72     }
73 
74     while (true) {
75         int length = ReadData(bulkIn, g_bufRet, MAX_SIZE_IOBUF);
76         if (length > 0) {
77             return PersistBuffer{reinterpret_cast<char *>(g_bufRet), static_cast<uint64_t>(length)};
78         } else if (length < 0) {
79             return PersistBuffer{reinterpret_cast<char *>(g_bufRet), static_cast<uint64_t>(0)};
80         }
81     }
82 }
83 
GetDevPathEx(const char * path)84 extern "C" char *GetDevPathEx(const char *path)
85 {
86     std::string basePath = GetDevPath(std::string(path));
87     size_t buf_size = basePath.length() + 1;
88     char *bufRet = new char[buf_size];
89     (void)memset_s(bufRet, buf_size, 0, buf_size);
90     (void)memcpy_s(bufRet, buf_size, basePath.c_str(), buf_size);
91     return bufRet;
92 }
93 
94 }
95