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 "usb_host_serial_loopback.h"
17 #include <cstdio>
18 #include <cstring>
19 #include <gtest/gtest.h>
20 #include <unistd.h>
21 #include "securec.h"
22 #include "usbhost_ddk_test.h"
23 
24 using namespace std;
25 using namespace testing::ext;
26 
27 namespace {
28 class UsbHostSerialLoopback : public testing::Test {
29 public:
30     static void SetUpTestCase();
31     static void TearDownTestCase();
32 };
33 
SetUpTestCase()34 void UsbHostSerialLoopback::SetUpTestCase()
35 {
36     const char *apiType = "-SDK";
37     UsbHostDdkTestInit(const_cast<char*>(apiType));
38 }
39 
TearDownTestCase()40 void UsbHostSerialLoopback::TearDownTestCase()
41 {
42     TestExit();
43 }
44 
45 HWTEST_F(UsbHostSerialLoopback, HostSerialLoopback, TestSize.Level1)
46 {
47     printf("------start HostSerialLoopback------\n");
48     char data[512] = {0};
49     while (true) {
50         UsbHostDdkTestOpen(HOST_ACM_SYNC_READ);
51         UsbHostDdkTestSyncRead(data);
52         UsbHostDdkTestClose(HOST_ACM_SYNC_READ);
53         if (strlen(data) > 0) {
54             if (strcmp(data, "q") == 0) {
55                 break;
56             }
57             UsbHostDdkTestOpen(HOST_ACM_SYNC_WRITE);
58             UsbHostDdkTestSyncWrite(data);
59             UsbHostDdkTestClose(HOST_ACM_SYNC_WRITE);
60             memset_s(data, sizeof(data), 0, sizeof(data));
61         }
62     }
63     printf("------end HostSerialLoopback------\n");
64 }
65 }