• 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 "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     USB_SERIAL_READ_SPEED,
48     USB_SERIAL_READ_GET_TEMP_SPEED,
49     USB_SERIAL_READ_SPEED_DONE,
50     USB_SERIAL_READ_GET_TEMP_SPEED_UINT32,
51 };
52 
53 static struct HdfSBuf *g_data;
54 static struct HdfSBuf *g_reply;
55 static struct HdfIoService *g_acmService;
56 static bool g_readRuning = false;
57 static sigset_t g_mask;
TestSpeed()58 static void TestSpeed()
59 {
60     HdfSbufFlush(g_reply);
61     int status = g_acmService->dispatcher->Dispatch(&g_acmService->object, USB_SERIAL_READ_SPEED, g_data, g_reply);
62     if (status) {
63         HDF_LOGE("%s: Dispatch USB_SERIAL_READ_SPEED err", __func__);
64         return;
65     }
66 }
67 
GetTempSpeed()68 static void GetTempSpeed()
69 {
70     const float calc = 10000;
71     uint32_t speed = 0;
72     HdfSbufFlush(g_reply);
73     int status = g_acmService->dispatcher->Dispatch(&g_acmService->object,
74         USB_SERIAL_READ_GET_TEMP_SPEED_UINT32, g_data, g_reply);
75     if (status) {
76         HDF_LOGE("%s: Dispatch USB_SERIAL_WRITE_GET_TEMP_SPEED failed status = %d",
77             __func__, status);
78         return;
79     }
80     if (!HdfSbufReadUint32(g_reply, &speed)) {
81         HDF_LOGE("%s: HdfSbufReadFloat failed", __func__);
82         return;
83     }
84     if (speed > 0) {
85         printf("speed : %f MB/s\n", (float)speed / calc);
86     }
87 
88 }
89 
ReadSpeedDone()90 static void ReadSpeedDone()
91 {
92     int status = g_acmService->dispatcher->Dispatch(g_acmService,
93         USB_SERIAL_READ_SPEED_DONE, g_data, g_reply);
94     if (status) {
95         HDF_LOGE("%s: Dispatch USB_SERIAL_WRITE_SPEED_DONE failed status = %d",
96             __func__, status);
97         return;
98     }
99 }
100 
StopHandler(void * arg)101 static void *StopHandler(void *arg)
102 {
103     int err, signo;
104     for (;;) {
105         err = sigwait(&g_mask, &signo);
106         if (err != 0) {
107             printf("Sigwait failed: %d\n", err);
108         }
109 
110         switch (signo) {
111         case SIGINT:
112         case SIGQUIT:
113             printf("acm_speed_read exit\n");
114             ReadSpeedDone();
115             g_readRuning = false;
116             return NULL;
117         default:
118             printf("Unexpected signal %d\n", signo);
119         }
120     }
121 }
122 
123 pthread_t g_threads;
StartStopHandler()124 static void StartStopHandler()
125 {
126     int err;
127     sigemptyset(&g_mask);
128     sigaddset(&g_mask, SIGINT);
129     sigaddset(&g_mask, SIGQUIT);
130     if ((err = 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 
main(int argc,char * argv[])140 int main(int argc, char *argv[])
141 {
142     int status;
143     g_acmService = HdfIoServiceBind("usbfn_cdcacm");
144     if (g_acmService == NULL || g_acmService->dispatcher == NULL || g_acmService->dispatcher->Dispatch == NULL) {
145         HDF_LOGE("%s: GetService err", __func__);
146         return HDF_FAILURE;
147     }
148 
149     g_data = HdfSBufObtainDefaultSize();
150     g_reply = HdfSBufObtainDefaultSize();
151     if (g_data == NULL || g_reply == NULL) {
152         HDF_LOGE("%s: GetService err", __func__);
153         return HDF_FAILURE;
154     }
155 
156     status = g_acmService->dispatcher->Dispatch(&g_acmService->object, USB_SERIAL_OPEN, g_data, g_reply);
157     if (status) {
158         HDF_LOGE("%s: Dispatch USB_SERIAL_OPEN err", __func__);
159         return HDF_FAILURE;
160     }
161 
162     StartStopHandler();
163     TestSpeed();
164     g_readRuning = true;
165     while (g_readRuning) {
166         sleep(0x2);
167         if (g_readRuning) {
168             GetTempSpeed();
169         }
170     }
171 
172     HdfSBufRecycle(g_data);
173     HdfSBufRecycle(g_reply);
174     HdfIoServiceRecycle(g_acmService);
175     return 0;
176 }
177