• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 DEFAULT_PORT_ID = 1;
29 static constexpr int32_t DEFAULT_ROLE_HOST = 1;
30 static constexpr int32_t DEFAULT_ROLE_DEVICE = 2;
31 static constexpr int32_t MIN_ARG_NUM = 3;
32 static constexpr uint32_t CMD_INDEX = 1;
33 static constexpr uint32_t PARAM_INDEX = 2;
34 static constexpr int32_t HOST_MODE = 2;
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: Switch to host\n");
43     printf("-p 2: Switch to device:\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     if (usbports[0].usbPortStatus.currentMode == HOST_MODE) {
155         printf("get current port %d: host\n", usbports[0].usbPortStatus.currentMode);
156     } else {
157         printf("get current port %d: device\n", usbports[0].usbPortStatus.currentMode);
158     }
159 }
160 
PortSwitch(UsbSrvClient & g_usbClient,int32_t mode)161 static void PortSwitch(UsbSrvClient &g_usbClient, int32_t mode)
162 {
163     switch (mode) {
164         case 0:
165             GetPortsInfo();
166             break;
167         case DEFAULT_ROLE_HOST:
168             g_usbClient.SetPortRole(DEFAULT_PORT_ID, DEFAULT_ROLE_HOST, DEFAULT_ROLE_HOST);
169             GetPortsInfo();
170             break;
171         case DEFAULT_ROLE_DEVICE:
172             g_usbClient.SetPortRole(DEFAULT_PORT_ID, DEFAULT_ROLE_DEVICE, DEFAULT_ROLE_DEVICE);
173             GetPortsInfo();
174             break;
175         default:
176             printf("%s:%d port param error\n", __func__, __LINE__);
177             break;
178     }
179 }
180 
DeviceSpeed(UsbSrvClient & g_usbClient,int32_t & sp)181 static void DeviceSpeed(UsbSrvClient &g_usbClient, int32_t &sp)
182 {
183     vector<UsbDevice> devi;
184     g_usbClient.GetDevices(devi);
185     USBDevicePipe pipe;
186     UsbDevice device = devi.front();
187     g_usbClient.OpenDevice(device, pipe);
188     uint8_t speed = -1;
189     g_usbClient.GetDeviceSpeed(pipe, speed);
190     sp = speed;
191     return;
192 }
193 
InterfaceStatus(UsbSrvClient & g_usbClient,int32_t & ds)194 static void InterfaceStatus(UsbSrvClient &g_usbClient, int32_t &ds)
195 {
196     vector<UsbDevice> devi;
197     g_usbClient.GetDevices(devi);
198     USBDevicePipe pipe;
199     UsbDevice device = devi.front();
200     g_usbClient.OpenDevice(device, pipe);
201     UsbInterface interface = device.GetConfigs().front().GetInterfaces().at(0);
202     bool unactivated = false;
203     g_usbClient.GetInterfaceActiveStatus(pipe, interface, unactivated);
204     unactivated ? ds = 1 : ds = 0;
205     return;
206 }
207 
ResetProxy(UsbSrvClient & g_usbClient,int32_t & sp)208 static void ResetProxy(UsbSrvClient &g_usbClient, int32_t &sp)
209 {
210     vector<UsbDevice> devi;
211     g_usbClient.GetDevices(devi);
212     USBDevicePipe pipe;
213     UsbDevice device = devi.front();
214     g_usbClient.OpenDevice(device, pipe);
215     std::cout << "please kill service, press enter to continue" << std::endl;
216     int32_t ch = 0;
217     while (ch != EOF) {
218         if ((ch = getchar()) == '\n') {
219             break;
220         }
221     }
222     uint8_t speed = -1;
223     g_usbClient.GetDeviceSpeed(pipe, speed);
224     sp = speed;
225     return;
226 }
227 
DeviceStatus(UsbSrvClient & g_usbClient,int32_t mode)228 static void DeviceStatus(UsbSrvClient &g_usbClient, int32_t mode)
229 {
230     switch (mode) {
231         case 0:
232             int32_t sp;
233             DeviceSpeed(g_usbClient, sp);
234             printf("%s:%d device speed=%d\n", __func__, __LINE__, sp);
235             break;
236         case 1:
237             int32_t ds;
238             InterfaceStatus(g_usbClient, ds);
239             printf("%s:%d interface status=%d\n", __func__, __LINE__, ds);
240             break;
241         default:
242             printf("%s:%d port param error\n", __func__, __LINE__);
243             break;
244     }
245 }
246 
SetProxy(UsbSrvClient & g_usbClient,int32_t mode)247 static void SetProxy(UsbSrvClient &g_usbClient, int32_t mode)
248 {
249     switch (mode) {
250         case 0:
251             int32_t sp;
252             ResetProxy(g_usbClient, sp);
253             if (sp > 0) {
254                 printf("%s:%d ResetProxy Okay\n", __func__, __LINE__);
255             } else {
256                 printf("%s:%d ResetProxy failed\n", __func__, __LINE__);
257             }
258             break;
259         default:
260             printf("%s:%d port param error\n", __func__, __LINE__);
261             break;
262     }
263 }
264 
isNumber(string_view strv)265 static inline bool isNumber(string_view strv)
266 {
267     return (strv.find_first_not_of("0123456789") == strv.npos);
268 }
269 
main(int32_t argc,char * argv[])270 int32_t main(int32_t argc, char *argv[])
271 {
272     UsbCommonTest::GrantPermissionSysNative();
273 
274     if (argc < MIN_ARG_NUM) {
275         PrintHelp();
276         return 0;
277     }
278 
279     if (!isNumber(argv[PARAM_INDEX])) {
280         PrintHelp();
281         return 0;
282     }
283 
284     uint32_t mode;
285     if ((!strcmp(argv[CMD_INDEX], "-f"))) {
286         mode = stoi(argv[PARAM_INDEX]);
287         FunctionSwitch(g_usbClient, mode);
288     } else if (!strcmp(argv[CMD_INDEX], "-p")) {
289         mode = stoi(argv[PARAM_INDEX]);
290         PortSwitch(g_usbClient, mode);
291     } else if (!strcmp(argv[CMD_INDEX], "-s")) {
292         mode = stoi(argv[PARAM_INDEX]);
293         DeviceStatus(g_usbClient, mode);
294     } else if (!strcmp(argv[CMD_INDEX], "-r")) {
295         mode = stoi(argv[PARAM_INDEX]);
296         SetProxy(g_usbClient, mode);
297     } else if (!strcmp(argv[CMD_INDEX], "-c")) {
298         AddCommonEvent();
299         printf("Press input c to exit.\n");
300         char ch = getchar();
301         while (ch != 'c') {
302             ch = getchar();
303             if (ch == 'c') {
304                 StopSubscriberCommonEvent(0);
305                 break;
306             }
307             sleep(1);
308         }
309         printf("show boac exit.\n");
310     } else {
311         printf("param incorrect: please input -h for help\n");
312     }
313     return 0;
314 }
315