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