1 /*
2  * Copyright (c) 2021 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 <stdio.h>
17 #include <sys/time.h>
18 #include <hdf_log.h>
19 #include <hdf_remote_service.h>
20 #include <hdf_sbuf.h>
21 #include <osal_time.h>
22 #include <servmgr_hdi.h>
23 #include <signal.h>
24 #include <unistd.h>
25 #include "cdcacm.h"
26 
27 #include "usb_dev_test.h"
28 
29 #define HDF_LOG_TAG   cdc_acm_speed
30 
31 static struct HdfSBuf *g_data;
32 static struct HdfSBuf *g_reply;
33 static struct HdfRemoteService *g_acmService;
34 static bool g_readRuning = false;
TestSpeed(void)35 static void TestSpeed(void)
36 {
37     HdfSbufFlush(g_reply);
38     int32_t status = g_acmService->dispatcher->Dispatch(g_acmService,
39         USB_SERIAL_WRITE_SPEED, g_data, g_reply);
40     if (status) {
41         HDF_LOGE("%{public}s: Dispatch USB_SERIAL_WRITE_SPEED failed status = %{public}d",
42             __func__, status);
43         return;
44     }
45 }
46 
GetTempSpeed(void)47 static void GetTempSpeed(void)
48 {
49     float speed;
50     HdfSbufFlush(g_reply);
51     int32_t status = g_acmService->dispatcher->Dispatch(g_acmService,
52         USB_SERIAL_WRITE_GET_TEMP_SPEED, g_data, g_reply);
53     if (status) {
54         HDF_LOGE("%{public}s: Dispatch USB_SERIAL_WRITE_GET_TEMP_SPEED failed status = %{public}d",
55             __func__, status);
56         return;
57     }
58     if (!HdfSbufReadFloat(g_reply, &speed)) {
59         HDF_LOGE("%{public}s: HdfSbufReadFloat failed", __func__);
60         return;
61     }
62     if (speed > 0) {
63         printf("speed : %f MB/s\n", speed);
64     }
65 }
66 
WriteSpeedDone(void)67 static void WriteSpeedDone(void)
68 {
69     int32_t status = g_acmService->dispatcher->Dispatch(g_acmService,
70         USB_SERIAL_WRITE_SPEED_DONE, g_data, g_reply);
71     if (status) {
72         HDF_LOGE("%{public}s: Dispatch USB_SERIAL_WRITE_SPEED_DONE failed status = %{public}d",
73             __func__, status);
74         return;
75     }
76 }
77 
StopWriteSpeedTest(int32_t signo)78 static void StopWriteSpeedTest(int32_t signo)
79 {
80     (void)signo;
81     WriteSpeedDone();
82     g_readRuning = false;
83     printf("AcmSpeedWrite exit.\n");
84 }
85 
AcmSpeedWrite(int32_t argc,const char * argv[])86 int32_t AcmSpeedWrite(int32_t argc, const char *argv[])
87 {
88     (void)argc;
89     (void)argv;
90     int32_t status;
91     struct HDIServiceManager *servmgr = HDIServiceManagerGet();
92     if (servmgr == NULL) {
93         HDF_LOGE("%{public}s: HDIServiceManagerGet err", __func__);
94         return HDF_FAILURE;
95     }
96     g_acmService = servmgr->GetService(servmgr, "usbfn_cdcacm");
97     HDIServiceManagerRelease(servmgr);
98     if (g_acmService == NULL) {
99         HDF_LOGE("%{public}s: GetService err", __func__);
100         return HDF_FAILURE;
101     }
102 
103     g_data = HdfSbufTypedObtain(SBUF_IPC);
104     g_reply = HdfSbufTypedObtain(SBUF_IPC);
105     if (g_data == NULL || g_reply == NULL) {
106         HDF_LOGE("%{public}s: GetService err", __func__);
107         return HDF_FAILURE;
108     }
109 
110     status = g_acmService->dispatcher->Dispatch(g_acmService, USB_SERIAL_OPEN, g_data, g_reply);
111     if (status) {
112         HDF_LOGE("%{public}s: Dispatch USB_SERIAL_OPEN err", __func__);
113         return HDF_FAILURE;
114     }
115 
116     (void)signal(SIGINT, StopWriteSpeedTest);
117     TestSpeed();
118     g_readRuning = true;
119     while (g_readRuning) {
120         sleep(0x2);
121         if (g_readRuning) {
122             GetTempSpeed();
123         }
124     }
125 
126     status = g_acmService->dispatcher->Dispatch(g_acmService, USB_SERIAL_CLOSE, g_data, g_reply);
127     if (status) {
128         HDF_LOGE("%{public}s: Dispatch USB_SERIAL_CLOSE err", __func__);
129         return HDF_FAILURE;
130     }
131 
132     HdfSbufRecycle(g_data);
133     HdfSbufRecycle(g_reply);
134 
135     return 0;
136 }
137