1 /*
2 * Copyright (c) 2021-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
16 #include "cstdio"
17 #include "usb_common_test.h"
18 #include "usb_srv_client.h"
19 #include "cJSON.h"
20 #include "common_event_manager.h"
21 #include "common_event_support.h"
22
23 using namespace std;
24 using namespace OHOS;
25 using namespace OHOS::USB;
26 using namespace OHOS::USB::Common;
27 using namespace OHOS::EventFwk;
28 static constexpr int32_t MIN_ARG_NUM = 3;
29 static constexpr uint32_t CMD_INDEX = 1;
30 static constexpr uint32_t PARAM_INDEX = 2;
31 constexpr uint32_t CASE1 = 1;
32 constexpr uint32_t CASE2 = 2;
33 constexpr int32_t PORT_OK = 0;
34 constexpr int32_t PORT_FAILED = -1;
35
36 static UsbSrvClient &g_usbClient = UsbSrvClient::GetInstance();
37
PrintHelp()38 static void PrintHelp()
39 {
40 printf("2 args\n");
41 printf("-p 0: Query Port\n");
42 printf("-p 1: get port supportmode\n");
43 printf("-p 2: set port role\n");
44 printf("-f 0: Query function\n");
45 printf("-f 1: Switch to function:acm\n");
46 printf("-f 2: Switch to function:ecm\n");
47 printf("-f 3: Switch to function:acm&ecm\n");
48 printf("-f 4: Switch to function:hdc\n");
49 printf("-f 5: Switch to function:acm&hdc\n");
50 printf("-f 6: Switch to function:ecm&hdc\n");
51 printf("-f 8: Switch to function:mtp\n");
52 printf("-f 16: Switch to function:ptp\n");
53 printf("-f 32: Switch to function:rndis\n");
54 printf("-f 512: Switch to function:storage\n");
55 printf("-f 36: Switch to function:rndis&hdc\n");
56 printf("-f 516: Switch to function:storage&hdc\n");
57 printf("-c 1: Switch to recv braodcast\n");
58 printf("-s 0: Get devicespeed\n");
59 printf("-s 1: Get interface actived\n");
60 printf("-r 0: Reset proxy\n");
61 }
62
63 class UsbSubscriberTest : public CommonEventSubscriber {
64 public:
UsbSubscriberTest(const CommonEventSubscribeInfo & sp)65 explicit UsbSubscriberTest(const CommonEventSubscribeInfo &sp) : CommonEventSubscriber(sp) {}
66
OnReceiveEvent(const CommonEventData & data)67 void OnReceiveEvent(const CommonEventData &data) override
68 {
69 USB_HILOGI(MODULE_USB_SERVICE, "recv event ok");
70 eventData_ = data;
71 std::string deviceStr = eventData_.GetData();
72 USB_HILOGI(MODULE_USB_SERVICE, "recv broadcast: %{public}s", deviceStr.c_str());
73
74 cJSON* pDevice = cJSON_Parse(deviceStr.c_str());
75 UsbDevice device(pDevice);
76 std::string strConfig = "null";
77 if (device.GetConfigCount() > 0) {
78 USBConfig config;
79 device.GetConfig(0, config);
80 strConfig = config.ToString();
81 }
82 USB_HILOGI(MODULE_USB_SERVICE, "recv broadcast:Name: %{public}s, config size: %{public}d, config0: %{public}s",
83 device.GetName().c_str(), device.GetConfigCount(), strConfig.c_str());
84 }
85
86 static CommonEventData eventData_;
87 };
88
89 CommonEventData UsbSubscriberTest::eventData_ {};
90 std::shared_ptr<UsbSubscriberTest> subscriber = nullptr;
AddCommonEvent()91 static void AddCommonEvent()
92 {
93 MatchingSkills matchingSkills;
94 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USB_DEVICE_DETACHED);
95 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USB_DEVICE_ATTACHED);
96 matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_USB_STATE);
97 CommonEventSubscribeInfo subscriberInfo(matchingSkills);
98 subscriber = std::make_shared<UsbSubscriberTest>(subscriberInfo);
99 bool ret = CommonEventManager::SubscribeCommonEvent(subscriber);
100 if (!ret) {
101 USB_HILOGW(MODULE_USB_SERVICE, "subscriber event for failed: %{public}d", ret);
102 }
103 }
104
StopSubscriberCommonEvent(int32_t signo)105 static void StopSubscriberCommonEvent(int32_t signo)
106 {
107 (void) signo;
108 if (subscriber != nullptr) {
109 CommonEventManager::UnSubscribeCommonEvent(subscriber);
110 }
111 std::cout << "stop recv broadcast."<< std::endl;
112 USB_HILOGI(MODULE_USB_SERVICE, "stop recv broadcast.");
113 }
114
GetCurrentFunctionInfo()115 static void GetCurrentFunctionInfo()
116 {
117 int32_t funcs = 0;
118 string strFun = "";
119 int32_t ret = g_usbClient.GetCurrentFunctions(funcs);
120 if (ret) {
121 printf("%s:%d error exit\n", __func__, __LINE__);
122 return;
123 }
124 strFun = g_usbClient.UsbFunctionsToString(funcs);
125 printf("%s:%d get current function: %s\n", __func__, __LINE__, strFun.c_str());
126 }
127
FunctionSwitch(UsbSrvClient & g_usbClient,int32_t mode)128 static void FunctionSwitch(UsbSrvClient &g_usbClient, int32_t mode)
129 {
130 switch (mode) {
131 case 0:
132 GetCurrentFunctionInfo();
133 break;
134 default:
135 int32_t ret = g_usbClient.SetCurrentFunctions(mode);
136 if (ret) {
137 printf("%s:%d error exit\n", __func__, __LINE__);
138 break;
139 }
140 GetCurrentFunctionInfo();
141 break;
142 }
143 }
144
GetPortsInfo()145 static void GetPortsInfo()
146 {
147 std::vector<UsbPort> usbports;
148 int32_t ret = g_usbClient.GetPorts(usbports);
149 if (ret) {
150 printf("%s:%d error exit\n", __func__, __LINE__);
151 return;
152 }
153
154 for (auto it : usbports) {
155 printf("portId: %d, supportedModes: %d, currentMode: %d, currentPowerRole: %d, currentDataRole:%d\n",
156 it.id, it.supportedModes, it.usbPortStatus.currentMode,
157 it.usbPortStatus.currentPowerRole, it.usbPortStatus.currentDataRole);
158 }
159 }
160
GetSupportMode(UsbSrvClient & g_usbClient)161 int32_t GetSupportMode(UsbSrvClient &g_usbClient)
162 {
163 printf("Please enter the portid:");
164 int portId;
165 std::cin >> portId;
166 int32_t result = 0;
167 int32_t ret = g_usbClient.GetSupportedModes(portId, result);
168 if (ret != PORT_OK) {
169 printf("get SupportMode failed\n");
170 return PORT_FAILED;
171 }
172
173 printf("supportMode: %d\n", result);
174 return PORT_OK;
175 }
176
SetPortRole(UsbSrvClient & g_usbClient)177 int32_t SetPortRole(UsbSrvClient &g_usbClient)
178 {
179 int portId;
180 int powerRole;
181 int dataRole;
182
183 printf("Please enter the portid:");
184 std::cin >> portId;
185 printf("\n");
186
187 printf("Please enter the powerRole:");
188 std::cin >> powerRole;
189 printf("\n");
190
191 printf("Please enter the dataRole:");
192 std::cin >> dataRole;
193 printf("\n");
194
195 int32_t ret = g_usbClient.SetPortRole(portId, powerRole, dataRole);
196 if (ret != PORT_OK) {
197 printf("set PortRole failed\n");
198 return PORT_FAILED;
199 }
200
201 printf("set PortRole success\n");
202 return PORT_OK;
203 }
204
PortSwitch(UsbSrvClient & g_usbClient,int32_t mode)205 static void PortSwitch(UsbSrvClient &g_usbClient, int32_t mode)
206 {
207 switch (mode) {
208 case 0:
209 GetPortsInfo();
210 break;
211 case CASE1:
212 GetSupportMode(g_usbClient);
213 break;
214 case CASE2:
215 SetPortRole(g_usbClient);
216 GetPortsInfo();
217 break;
218 default:
219 printf("%s:%d port param error\n", __func__, __LINE__);
220 break;
221 }
222 }
223
DeviceSpeed(UsbSrvClient & g_usbClient,int32_t & sp)224 static void DeviceSpeed(UsbSrvClient &g_usbClient, int32_t &sp)
225 {
226 vector<UsbDevice> devi;
227 g_usbClient.GetDevices(devi);
228 USBDevicePipe pipe;
229 UsbDevice device = devi.front();
230 g_usbClient.OpenDevice(device, pipe);
231 uint8_t speed = -1;
232 g_usbClient.GetDeviceSpeed(pipe, speed);
233 sp = speed;
234 return;
235 }
236
InterfaceStatus(UsbSrvClient & g_usbClient,int32_t & ds)237 static void InterfaceStatus(UsbSrvClient &g_usbClient, int32_t &ds)
238 {
239 vector<UsbDevice> devi;
240 g_usbClient.GetDevices(devi);
241 USBDevicePipe pipe;
242 UsbDevice device = devi.front();
243 g_usbClient.OpenDevice(device, pipe);
244 UsbInterface interface = device.GetConfigs().front().GetInterfaces().at(0);
245 bool unactivated = false;
246 g_usbClient.GetInterfaceActiveStatus(pipe, interface, unactivated);
247 unactivated ? ds = 1 : ds = 0;
248 return;
249 }
250
ResetProxy(UsbSrvClient & g_usbClient,int32_t & sp)251 static void ResetProxy(UsbSrvClient &g_usbClient, int32_t &sp)
252 {
253 vector<UsbDevice> devi;
254 g_usbClient.GetDevices(devi);
255 USBDevicePipe pipe;
256 UsbDevice device = devi.front();
257 g_usbClient.OpenDevice(device, pipe);
258 std::cout << "please kill service, press enter to continue" << std::endl;
259 int32_t ch = 0;
260 while (ch != EOF) {
261 if ((ch = getchar()) == '\n') {
262 break;
263 }
264 }
265 uint8_t speed = -1;
266 g_usbClient.GetDeviceSpeed(pipe, speed);
267 sp = speed;
268 return;
269 }
270
DeviceStatus(UsbSrvClient & g_usbClient,int32_t mode)271 static void DeviceStatus(UsbSrvClient &g_usbClient, int32_t mode)
272 {
273 switch (mode) {
274 case 0:
275 int32_t sp;
276 DeviceSpeed(g_usbClient, sp);
277 printf("%s:%d device speed=%d\n", __func__, __LINE__, sp);
278 break;
279 case 1:
280 int32_t ds;
281 InterfaceStatus(g_usbClient, ds);
282 printf("%s:%d interface status=%d\n", __func__, __LINE__, ds);
283 break;
284 default:
285 printf("%s:%d port param error\n", __func__, __LINE__);
286 break;
287 }
288 }
289
SetProxy(UsbSrvClient & g_usbClient,int32_t mode)290 static void SetProxy(UsbSrvClient &g_usbClient, int32_t mode)
291 {
292 switch (mode) {
293 case 0:
294 int32_t sp;
295 ResetProxy(g_usbClient, sp);
296 if (sp > 0) {
297 printf("%s:%d ResetProxy Okay\n", __func__, __LINE__);
298 } else {
299 printf("%s:%d ResetProxy failed\n", __func__, __LINE__);
300 }
301 break;
302 default:
303 printf("%s:%d port param error\n", __func__, __LINE__);
304 break;
305 }
306 }
307
isNumber(string_view strv)308 static inline bool isNumber(string_view strv)
309 {
310 return (strv.find_first_not_of("0123456789") == strv.npos);
311 }
312
main(int32_t argc,char * argv[])313 int32_t main(int32_t argc, char *argv[])
314 {
315 UsbCommonTest::GrantPermissionSysNative();
316
317 if (argc < MIN_ARG_NUM) {
318 PrintHelp();
319 return 0;
320 }
321
322 if (!isNumber(argv[PARAM_INDEX])) {
323 PrintHelp();
324 return 0;
325 }
326
327 uint32_t mode;
328 if ((!strcmp(argv[CMD_INDEX], "-f"))) {
329 mode = stoi(argv[PARAM_INDEX]);
330 FunctionSwitch(g_usbClient, mode);
331 } else if (!strcmp(argv[CMD_INDEX], "-p")) {
332 mode = stoi(argv[PARAM_INDEX]);
333 PortSwitch(g_usbClient, mode);
334 } else if (!strcmp(argv[CMD_INDEX], "-s")) {
335 mode = stoi(argv[PARAM_INDEX]);
336 DeviceStatus(g_usbClient, mode);
337 } else if (!strcmp(argv[CMD_INDEX], "-r")) {
338 mode = stoi(argv[PARAM_INDEX]);
339 SetProxy(g_usbClient, mode);
340 } else if (!strcmp(argv[CMD_INDEX], "-c")) {
341 AddCommonEvent();
342 printf("Press input c to exit.\n");
343 char ch = getchar();
344 while (ch != 'c') {
345 ch = getchar();
346 if (ch == 'c') {
347 StopSubscriberCommonEvent(0);
348 break;
349 }
350 sleep(1);
351 }
352 printf("show boac exit.\n");
353 } else {
354 printf("param incorrect: please input -h for help\n");
355 }
356 return 0;
357 }
358