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