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
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_WRITE_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_WRITE_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
WriteSpeedDone()64 static void WriteSpeedDone()
65 {
66 int status = g_acmService->dispatcher->Dispatch(g_acmService,
67 USB_SERIAL_WRITE_SPEED_DONE, g_data, g_reply);
68 if (status) {
69 HDF_LOGE("%s: Dispatch USB_SERIAL_WRITE_SPEED_DONE failed status = %d",
70 __func__, status);
71 return;
72 }
73 }
74
StopWriteSpeedTest(int signo)75 void StopWriteSpeedTest(int signo)
76 {
77 WriteSpeedDone();
78 g_readRuning = false;
79 printf("acm_speed_write exit.\n");
80 }
81
main(int argc,char * argv[])82 int main(int argc, char *argv[])
83 {
84 int status;
85 struct HDIServiceManager *servmgr = HDIServiceManagerGet();
86 if (servmgr == NULL) {
87 HDF_LOGE("%s: HDIServiceManagerGet err", __func__);
88 return HDF_FAILURE;
89 }
90 g_acmService = servmgr->GetService(servmgr, "usbfn_cdcacm");
91 HDIServiceManagerRelease(servmgr);
92 if (g_acmService == NULL) {
93 HDF_LOGE("%s: GetService err", __func__);
94 return HDF_FAILURE;
95 }
96
97 g_data = HdfSBufTypedObtain(SBUF_IPC);
98 g_reply = HdfSBufTypedObtain(SBUF_IPC);
99 if (g_data == NULL || g_reply == NULL) {
100 HDF_LOGE("%s: GetService err", __func__);
101 return HDF_FAILURE;
102 }
103
104 status = g_acmService->dispatcher->Dispatch(g_acmService, USB_SERIAL_OPEN, g_data, g_reply);
105 if (status) {
106 HDF_LOGE("%s: Dispatch USB_SERIAL_OPEN err", __func__);
107 return HDF_FAILURE;
108 }
109
110 signal(SIGINT, StopWriteSpeedTest);
111 TestSpeed();
112 g_readRuning = true;
113 while (g_readRuning) {
114 sleep(0x2);
115 if (g_readRuning) {
116 GetTempSpeed();
117 }
118 }
119
120 status = g_acmService->dispatcher->Dispatch(g_acmService, USB_SERIAL_CLOSE, g_data, g_reply);
121 if (status) {
122 HDF_LOGE("%s: Dispatch USB_SERIAL_CLOSE err", __func__);
123 return HDF_FAILURE;
124 }
125
126 HdfSBufRecycle(g_data);
127 HdfSBufRecycle(g_reply);
128
129 return 0;
130 }
131