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