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 "usb_common_test.h"
17 #include "usb_srv_client.h"
18
19 using namespace std;
20 using namespace OHOS;
21 using namespace OHOS::USB;
22 using namespace OHOS::USB::Common;
23
24 static constexpr int32_t DEFAULT_PORT_ID = 1;
25 static constexpr int32_t DEFAULT_ROLE_HOST = 1;
26 static constexpr int32_t DEFAULT_ROLE_DEVICE = 2;
27 static constexpr int32_t MIN_ARG_NUM = 3;
28 static constexpr uint32_t CMD_INDEX = 1;
29 static constexpr uint32_t PARAM_INDEX = 2;
30
31 static constexpr int32_t HOST_MODE = 2;
32
33 static UsbSrvClient &g_usbClient = UsbSrvClient::GetInstance();
34
PrintHelp()35 static void PrintHelp()
36 {
37 printf("2 args\n");
38 printf("-p 0: Query Port\n");
39 printf("-p 1: Switch to host\n");
40 printf("-p 2: Switch to device:\n");
41 printf("-f 0: Query function\n");
42 printf("-f 1: Switch to function:acm\n");
43 printf("-f 2: Switch to function:ecm\n");
44 printf("-f 3: Switch to function:acm&ecm\n");
45 printf("-f 4: Switch to function:hdc\n");
46 printf("-f 5: Switch to function:acm&hdc\n");
47 printf("-f 6: Switch to function:ecm&hdc\n");
48 printf("-f 8: Switch to function:mtp\n");
49 printf("-f 16: Switch to function:ptp\n");
50 printf("-f 32: Switch to function:rndis\n");
51 printf("-f 512: Switch to function:storage\n");
52 printf("-f 36: Switch to function:rndis&hdc\n");
53 printf("-f 516: Switch to function:storage&hdc\n");
54 }
55
GetCurrentFunctionInfo()56 static void GetCurrentFunctionInfo()
57 {
58 int32_t funcs = 0;
59 string strFun = "";
60 int32_t ret = g_usbClient.GetCurrentFunctions(funcs);
61 if (ret) {
62 printf("%s:%d error exit\n", __func__, __LINE__);
63 return;
64 }
65 strFun = g_usbClient.UsbFunctionsToString(funcs);
66 printf("%s:%d get current function: %s\n", __func__, __LINE__, strFun.c_str());
67 }
68
FunctionSwitch(UsbSrvClient & g_usbClient,int32_t mode)69 static void FunctionSwitch(UsbSrvClient &g_usbClient, int32_t mode)
70 {
71 switch (mode) {
72 case 0:
73 GetCurrentFunctionInfo();
74 break;
75 default:
76 int32_t ret = g_usbClient.SetCurrentFunctions(mode);
77 if (ret) {
78 printf("%s:%d error exit\n", __func__, __LINE__);
79 break;
80 }
81 GetCurrentFunctionInfo();
82 break;
83 }
84 }
85
GetPortsInfo()86 static void GetPortsInfo()
87 {
88 std::vector<UsbPort> usbports;
89 int32_t ret = g_usbClient.GetPorts(usbports);
90 if (ret) {
91 printf("%s:%d error exit\n", __func__, __LINE__);
92 return;
93 }
94
95 if (usbports[0].usbPortStatus.currentMode == HOST_MODE) {
96 printf("get current port %d: host\n", usbports[0].usbPortStatus.currentMode);
97 } else {
98 printf("get current port %d: device\n", usbports[0].usbPortStatus.currentMode);
99 }
100 }
101
PortSwitch(UsbSrvClient & g_usbClient,int32_t mode)102 static void PortSwitch(UsbSrvClient &g_usbClient, int32_t mode)
103 {
104 switch (mode) {
105 case 0:
106 GetPortsInfo();
107 break;
108 case DEFAULT_ROLE_HOST:
109 g_usbClient.SetPortRole(DEFAULT_PORT_ID, DEFAULT_ROLE_HOST, DEFAULT_ROLE_HOST);
110 GetPortsInfo();
111 break;
112 case DEFAULT_ROLE_DEVICE:
113 g_usbClient.SetPortRole(DEFAULT_PORT_ID, DEFAULT_ROLE_DEVICE, DEFAULT_ROLE_DEVICE);
114 GetPortsInfo();
115 break;
116 default:
117 printf("%s:%d port param error\n", __func__, __LINE__);
118 break;
119 }
120 }
121
isNumber(string_view strv)122 static inline bool isNumber(string_view strv)
123 {
124 return (strv.find_first_not_of("0123456789") == strv.npos);
125 }
126
main(int32_t argc,char * argv[])127 int32_t main(int32_t argc, char *argv[])
128 {
129 UsbCommonTest::GrantPermissionSysNative();
130
131 if (argc < MIN_ARG_NUM) {
132 PrintHelp();
133 return 0;
134 }
135
136 if (!isNumber(argv[PARAM_INDEX])) {
137 PrintHelp();
138 return 0;
139 }
140
141 uint32_t mode;
142 if ((!strcmp(argv[CMD_INDEX], "-f"))) {
143 mode = stoi(argv[PARAM_INDEX]);
144 FunctionSwitch(g_usbClient, mode);
145 } else if (!strcmp(argv[CMD_INDEX], "-p")) {
146 mode = stoi(argv[PARAM_INDEX]);
147 PortSwitch(g_usbClient, mode);
148 } else {
149 printf("param incorrect: please input -h for help\n");
150 }
151 return 0;
152 }
153