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 "securec.h"
17 #include "hdf_log.h"
18 #include "osal_mem.h"
19 #include "hdf_io_service_if.h"
20 #include <unistd.h>
21 #include <sys/time.h>
22 #include <stdio.h>
23 #include <fcntl.h>
24 #include "osal_thread.h"
25 #include "osal_mutex.h"
26 #include "osal_time.h"
27 #include "osal_file.h"
28 #include "securec.h"
29 #include "signal.h"
30 #include <pthread.h>
31 #define HDF_LOG_TAG cdc_acm_speed
32
33 enum UsbSerialCmd {
34 USB_SERIAL_OPEN = 0,
35 USB_SERIAL_CLOSE,
36 USB_SERIAL_READ,
37 USB_SERIAL_WRITE,
38 USB_SERIAL_GET_BAUDRATE,
39 USB_SERIAL_SET_BAUDRATE,
40 USB_SERIAL_SET_PROP,
41 USB_SERIAL_GET_PROP,
42 USB_SERIAL_REGIST_PROP,
43 USB_SERIAL_WRITE_SPEED,
44 USB_SERIAL_WRITE_GET_TEMP_SPEED,
45 USB_SERIAL_WRITE_SPEED_DONE,
46 USB_SERIAL_WRITE_GET_TEMP_SPEED_UINT32,
47 };
48
49 static struct HdfSBuf *g_data;
50 static struct HdfSBuf *g_reply;
51 static struct HdfIoService *g_acmService;
52 static bool g_readRuning = false;
53 static sigset_t g_mask;
TestSpeed()54 static void TestSpeed()
55 {
56 HdfSbufFlush(g_reply);
57 int status = g_acmService->dispatcher->Dispatch(&g_acmService->object,
58 USB_SERIAL_WRITE_SPEED, g_data, g_reply);
59 if (status) {
60 HDF_LOGE("%s: Dispatch USB_SERIAL_WRITE_SPEED failed status = %d",
61 __func__, status);
62 return;
63 }
64 }
65
GetTempSpeed()66 static void GetTempSpeed()
67 {
68 const float calc = 10000;
69 uint32_t speed = 0;
70 HdfSbufFlush(g_reply);
71 int status = g_acmService->dispatcher->Dispatch(&g_acmService->object,
72 USB_SERIAL_WRITE_GET_TEMP_SPEED_UINT32, g_data, g_reply);
73 if (status) {
74 HDF_LOGE("%s: Dispatch USB_SERIAL_WRITE_GET_TEMP_SPEED failed status = %d",
75 __func__, status);
76 return;
77 }
78 if (!HdfSbufReadUint32(g_reply, &speed)) {
79 HDF_LOGE("%s: HdfSbufReadFloat failed", __func__);
80 return;
81 }
82 if (speed > 0) {
83 printf("speed : %f MB/s\n", (float)speed / calc);
84 }
85
86 }
87
WriteSpeedDone()88 static void WriteSpeedDone()
89 {
90 int status = g_acmService->dispatcher->Dispatch(g_acmService,
91 USB_SERIAL_WRITE_SPEED_DONE, g_data, g_reply);
92 if (status) {
93 HDF_LOGE("%s: Dispatch USB_SERIAL_WRITE_SPEED_DONE failed status = %d",
94 __func__, status);
95 return;
96 }
97 }
98
StopHandler(void * arg)99 static void *StopHandler(void *arg)
100 {
101 int err, signo;
102 for (;;) {
103 err = sigwait(&g_mask, &signo);
104 if (err != 0) {
105 printf("Sigwait failed: %d\n", err);
106 }
107
108 switch (signo) {
109 case SIGINT:
110 case SIGQUIT:
111 printf("acm_speed_write exit\n");
112 WriteSpeedDone();
113 g_readRuning = false;
114 return NULL;
115 default:
116 printf("Unexpected signal %d\n", signo);
117 }
118 }
119 }
120
121 pthread_t g_threads;
StartStopHandler()122 static void StartStopHandler()
123 {
124 int err;
125 sigemptyset(&g_mask);
126 sigaddset(&g_mask, SIGINT);
127 sigaddset(&g_mask, SIGQUIT);
128 if ((err = pthread_sigmask(SIG_BLOCK, &g_mask, NULL)) != 0) {
129 printf("SIG_BLOCK error\n");
130 return;
131 }
132 if (pthread_create(&g_threads, NULL, StopHandler, NULL) != 0) {
133 printf("Could not create core thread\n");
134 return;
135 }
136 }
137
main(int argc,char * argv[])138 int main(int argc, char *argv[])
139 {
140 int status;
141
142 g_acmService = HdfIoServiceBind("usbfn_cdcacm");
143 if (g_acmService == NULL || g_acmService->dispatcher == NULL || g_acmService->dispatcher->Dispatch == NULL) {
144 HDF_LOGE("%s: GetService err", __func__);
145 return HDF_FAILURE;
146 }
147
148 g_data = HdfSBufObtainDefaultSize();
149 g_reply = HdfSBufObtainDefaultSize();
150 if (g_data == NULL || g_reply == NULL) {
151 HDF_LOGE("%s: GetService err", __func__);
152 return HDF_FAILURE;
153 }
154
155 status = g_acmService->dispatcher->Dispatch(&g_acmService->object, USB_SERIAL_OPEN, g_data, g_reply);
156 if (status) {
157 HDF_LOGE("%s: Dispatch USB_SERIAL_OPEN err", __func__);
158 return HDF_FAILURE;
159 }
160 StartStopHandler();
161 TestSpeed();
162 g_readRuning = true;
163 while (g_readRuning) {
164 sleep(0x2);
165 if (g_readRuning) {
166 GetTempSpeed();
167 }
168 }
169
170 status = g_acmService->dispatcher->Dispatch(&g_acmService->object, USB_SERIAL_CLOSE, g_data, g_reply);
171 if (status) {
172 HDF_LOGE("%s: Dispatch USB_SERIAL_CLOSE err", __func__);
173 return HDF_FAILURE;
174 }
175
176 HdfSBufRecycle(g_data);
177 HdfSBufRecycle(g_reply);
178 HdfIoServiceRecycle(g_acmService);
179 return 0;
180 }
181