1 /*
2 * Copyright (C) 2024 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 "host_usb.h"
16 #include "ffi_utils.h"
17
18 using namespace Hdc;
19
InitHostUsb()20 extern "C" void* InitHostUsb()
21 {
22 HostUsb* ptr = new HostUsb();
23 ptr->Initial();
24 return ptr;
25 }
26
GetReadyUsbDevice(void * ptr)27 extern "C" PersistBuffer GetReadyUsbDevice(void* ptr)
28 {
29 if (ptr == nullptr) {
30 return PersistBuffer {
31 reinterpret_cast<char *>(0),
32 static_cast<uint64_t>(0)
33 };
34 }
35 HostUsb* usbPtr = static_cast<HostUsb*>(ptr);
36 if (usbPtr == nullptr) {
37 return PersistBuffer {
38 reinterpret_cast<char *>(0),
39 static_cast<uint64_t>(0)
40 };
41 }
42 HDaemonInfo pi;
43 std::string ret = usbPtr->AdminDaemonMap(OP_GET_READY_STRLIST, "", pi);
44 char* str = new char[ret.length()];
45 if (memcpy_s(str, ret.length(), ret.c_str(), ret.length()) < 0) {
46 return PersistBuffer {
47 reinterpret_cast<char *>(0),
48 static_cast<uint64_t>(0)
49 };
50 }
51 return PersistBuffer {
52 reinterpret_cast<char *>(str),
53 static_cast<uint64_t>(ret.length())
54 };
55 }
56
OnDeviceConnected(void * ptr,char * connectKey,int len,bool success)57 extern "C" void OnDeviceConnected(void* ptr, char* connectKey, int len, bool success)
58 {
59 if (ptr == nullptr) {
60 return;
61 }
62 HostUsb* usbPtr = static_cast<HostUsb*>(ptr);
63 if (usbPtr == nullptr) {
64 return;
65 }
66 char* key = new char[len + 1];
67 memset_s(key, len + 1, 0, len + 1);
68 if (memcpy_s(key, len + 1, connectKey, len) < 0) {
69 return;
70 }
71 HUSB hUSB = usbPtr->GetUsbDevice(std::string(key));
72 delete[] key;
73 usbPtr->UpdateUSBDaemonInfo(hUSB, success ? STATUS_CONNECTED : STATUS_OFFLINE);
74 }
75
WriteUsb(void * ptr,char * connectKey,int len,SerializedBuffer buf)76 extern "C" int WriteUsb(void* ptr, char* connectKey, int len, SerializedBuffer buf)
77 {
78 if (ptr == nullptr) {
79 return -1;
80 }
81 HostUsb* usbPtr = static_cast<HostUsb*>(ptr);
82 if (usbPtr == nullptr) {
83 return -1;
84 }
85 char* key = new char[len + 1];
86 memset_s(key, len + 1, 0, len + 1);
87 if (memcpy_s(key, len + 1, connectKey, len) < 0) {
88 return -1;
89 }
90 HUSB hUSB = usbPtr->GetUsbDevice(std::string(key));
91 delete[] key;
92 return usbPtr->WriteUsbIO(hUSB, buf);
93 }
94
ReadUsb(void * ptr,char * connectKey,int len,int exceptedSize)95 extern "C" PersistBuffer ReadUsb(void* ptr, char* connectKey, int len, int exceptedSize)
96 {
97 if (ptr == nullptr) {
98 return PersistBuffer {
99 reinterpret_cast<char *>(0),
100 static_cast<uint64_t>(0)
101 };
102 }
103 HostUsb* usbPtr = static_cast<HostUsb*>(ptr);
104 if (usbPtr == nullptr) {
105 return PersistBuffer {
106 reinterpret_cast<char *>(0),
107 static_cast<uint64_t>(0)
108 };
109 }
110 char* key = new char[len + 1];
111 memset_s(key, len + 1, 0, len + 1);
112 if (memcpy_s(key, len + 1, connectKey, len) < 0) {
113 return PersistBuffer {
114 reinterpret_cast<char *>(0),
115 static_cast<uint64_t>(0)
116 };
117 }
118 HUSB hUSB = usbPtr->GetUsbDevice(std::string(key));
119 delete[] key;
120 return usbPtr->ReadUsbIO(hUSB, exceptedSize);
121 }
122
CancelUsbIo(void * ptr,char * connectKey,int len)123 extern "C" void CancelUsbIo(void* ptr, char* connectKey, int len)
124 {
125 if (ptr == nullptr) {
126 return;
127 }
128 HostUsb* usbPtr = static_cast<HostUsb*>(ptr);
129 if (usbPtr == nullptr) {
130 return;
131 }
132 char* key = new char[len + 1];
133 memset_s(key, len + 1, 0, len + 1);
134 if (memcpy_s(key, len + 1, connectKey, len) < 0) {
135 return;
136 }
137 HUSB hUSB = usbPtr->GetUsbDevice(std::string(key));
138 delete[] key;
139 usbPtr->CancelUsbIo(hUSB);
140 }
141
Stop(void * ptr)142 extern "C" bool Stop(void* ptr)
143 {
144 if (ptr == nullptr) {
145 return false;
146 }
147 HostUsb* usbPtr = static_cast<HostUsb*>(ptr);
148 if (usbPtr == nullptr) {
149 return false;
150 }
151 usbPtr->Stop();
152 delete usbPtr;
153 return true;
154 }