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