• 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 <stdio.h>
17 #include <sys/time.h>
18 #include <pthread.h>
19 #include <unistd.h>
20 #include "hdf_io_service_if.h"
21 #include "hdf_log.h"
22 #include "osal_mem.h"
23 #include "osal_mutex.h"
24 #include "osal_thread.h"
25 #include "osal_time.h"
26 #include "securec.h"
27 
28 #define HDF_LOG_TAG   cdc_acm_test
29 
30 enum UsbSerialCmd {
31     USB_SERIAL_OPEN = 0,
32     USB_SERIAL_CLOSE,
33     USB_SERIAL_READ,
34     USB_SERIAL_WRITE,
35     USB_SERIAL_GET_BAUDRATE,
36     USB_SERIAL_SET_BAUDRATE,
37     USB_SERIAL_SET_PROP,
38     USB_SERIAL_GET_PROP,
39     USB_SERIAL_REGIST_PROP,
40 };
41 
42 static struct HdfSBuf *g_data;
43 static struct HdfSBuf *g_reply;
44 static struct HdfIoService *g_acmService;
45 static struct OsalMutex       g_lock;
46 
TestWrite(const char * buf)47 static void TestWrite(const char *buf)
48 {
49     HdfSbufFlush(g_data);
50     (void)HdfSbufWriteString(g_data, buf);
51     int32_t status = g_acmService->dispatcher->Dispatch(&g_acmService->object, USB_SERIAL_WRITE, g_data, g_reply);
52     if (status <= 0) {
53         HDF_LOGE("%s: Dispatch USB_SERIAL_WRITE failed status = %d", __func__, status);
54     }
55 }
56 
TestRead()57 static void TestRead()
58 {
59     HdfSbufFlush(g_reply);
60     int32_t status = g_acmService->dispatcher->Dispatch(&g_acmService->object, USB_SERIAL_READ, g_data, g_reply);
61     if (status) {
62         printf("%s: Dispatch USB_SERIAL_READ failed status = %d", __func__, status);
63         return;
64     }
65     if (HdfSbufGetDataSize(g_reply)) {
66         const char *tmp = HdfSbufReadString(g_reply);
67         if (tmp && strlen(tmp) > 0) {
68             printf("%s: read : %s \n", __func__, tmp);
69         }
70     }
71 }
72 
73 static bool g_readRuning = false;
74 #define SLEEP_READ 100
ReadThread(void * arg)75 static void ReadThread(void *arg)
76 {
77     while (g_readRuning) {
78         OsalMutexLock(&g_lock);
79         TestRead();
80         OsalMutexUnlock(&g_lock);
81         OsalMDelay(SLEEP_READ);
82     }
83 }
84 
85 pthread_t g_tid;
StartPThreadRead()86 static void StartPThreadRead()
87 {
88     if ((pthread_create(&g_tid, NULL, ReadThread, NULL)) == -1) {
89         printf("create error!\n");
90     }
91 }
92 
93 #define STR_LEN 256
94 #define SLEEP_GETCHAR 100000
Test02()95 static void Test02()
96 {
97     char c;
98     char str[STR_LEN] = {0};
99     char *getStr = NULL;
100     if (OsalMutexInit(&g_lock) != HDF_SUCCESS) {
101         HDF_LOGE("%s: init lock fail!", __func__);
102         return;
103     }
104     while (1) {
105         printf("\ninput: \nr: for read acm\nw: for write acm \nq: for exit \n");
106         c = getchar();
107         if (c == 'r') {
108             printf("input: 'q' quit reading\n");
109             g_readRuning = true;
110             StartPThreadRead();
111             while (getchar() != 'q') {
112                 printf("input: 'q' quit reading\n");
113                 usleep(SLEEP_GETCHAR);
114             }
115             g_readRuning = false;
116         } else if (c == 'w') {
117             printf("input: strings and enter for send\n");
118             getchar();
119             getStr = gets_s(str, STR_LEN - 1);
120             if (getStr == NULL) {
121                 HDF_LOGE("%s: gets_s faile", __func__);
122             }
123             TestWrite(str);
124         } else if (c == 'q') {
125             return;
126         }
127     }
128 }
129 
acm_test(int32_t argc,const char * argv[])130 int32_t acm_test(int32_t argc, const char *argv[])
131 {
132     int32_t status;
133     g_acmService = HdfIoServiceBind("usbfn_cdcacm");
134     if (g_acmService == NULL || g_acmService->dispatcher == NULL || g_acmService->dispatcher->Dispatch == NULL) {
135         HDF_LOGE("%s: GetService err", __func__);
136         return HDF_FAILURE;
137     }
138 
139     g_data = HdfSbufObtainDefaultSize();
140     g_reply = HdfSbufObtainDefaultSize();
141     if (g_data == NULL || g_reply == NULL) {
142         HDF_LOGE("%s: GetService err", __func__);
143         return HDF_FAILURE;
144     }
145 
146     status = g_acmService->dispatcher->Dispatch(&g_acmService->object, USB_SERIAL_OPEN, g_data, g_reply);
147     if (status) {
148         HDF_LOGE("%s: Dispatch USB_SERIAL_OPEN err", __func__);
149         return HDF_FAILURE;
150     }
151 
152     Test02();
153 
154     status = g_acmService->dispatcher->Dispatch(&g_acmService->object, USB_SERIAL_CLOSE, g_data, g_reply);
155     if (status) {
156         HDF_LOGE("%s: Dispatch USB_SERIAL_CLOSE err", __func__);
157         return HDF_FAILURE;
158     }
159 
160     HdfSbufRecycle(g_data);
161     HdfSbufRecycle(g_reply);
162     return HDF_SUCCESS;
163 }
164