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