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