1 /*
2  * Copyright (c) 2021-2023 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 #ifndef USB_CDCACM_TEST_H
17 #define USB_CDCACM_TEST_H
18 
19 #include <cstdio>
20 #include <cstdlib>
21 #include <cstring>
22 #include <unistd.h>
23 
24 #include "osal_atomic.h"
25 #include "osal_mutex.h"
26 #include "osal_time.h"
27 #include "usbfn_device.h"
28 #include "usbfn_interface.h"
29 #include "usbfn_request.h"
30 
31 #define DEV_USBFN_SERVICE_NAME "usbfn"
32 #define TEST_TIMES              10
33 #define BUFFER_LEN              64
34 #define WAIT_10MS               10
35 #define SYNC_5000MS             500
36 #define CDC_ACM
37 #define QUEUE_SIZE 8
38 #define PORT_RATE  9600
39 #define DATA_BIT   8
40 
41 #define SS_MAX_PACKET_SIZE 1024
42 #define MAX_PACKET_SIZE    512
43 #define EP_ADD_NOTIFY      1
44 #define EP_ADD_DATA_IN     2
45 #define EP_ADD_DATA_OUT    3
46 #define DATA_EP_NUM        2
47 #define NOTIFY_EP_NUM      1
48 #define INTF_COUNT         2
49 
50 #define ACM_NOTIFY_INTERVAL    32 /* ms */
51 #define ACM_HS_NOTIFY_INTERVAL 9  /* ms */
52 #define ACM_NOTIFY_MAXPACKET   10 /* notification + 2 bytes */
53 
54 #define ACM_CTRL_IDX 1
55 #define ACM_DATA_IDX 2
56 #define ACM_IAD_IDX  3
57 
58 struct Serial {
59     struct AcmDevice *acm;
60     struct UsbCdcLineCoding lineCoding;
61     struct OsalMutex lock;
62     struct DListHead readPool;
63     struct DListHead readQueue;
64     int32_t readStarted;
65     int32_t readAllocated;
66     struct DListHead writePool;
67     int32_t writeStarted;
68     int32_t writeAllocated;
69     bool writeBusy;
70 
71     bool suspended;
72     bool startDelayed;
73     int32_t refCount;
74 };
75 
76 struct AcmNotifyMethod {
77     void (*connect)(struct AcmDevice *acm);
78     void (*disconnect)(struct AcmDevice *acm);
79     int32_t (*sendBreak)(struct AcmDevice *acm, int32_t duration);
80 };
81 
82 struct AcmPipe {
83     uint8_t id;
84     uint16_t maxPacketSize;
85     struct UsbFnInterface *ctrlIface;
86 };
87 
88 struct AcmInterface {
89     struct UsbFnInterface *fn;
90     UsbFnInterfaceHandle handle;
91 };
92 
93 struct AcmDevice {
94     struct UsbFnDevice *fnDev;
95     struct AcmInterface ctrlIface;
96     struct AcmInterface dataIface;
97     struct AcmPipe notifyPipe;
98     struct AcmPipe dataInPipe;
99     struct AcmPipe dataOutPipe;
100     struct DListHead ctrlPool;
101     int32_t ctrlReqNum;
102     struct UsbFnRequest *notifyReq;
103     struct OsalMutex lock;
104     bool pending;
105     uint32_t enableEvtCnt;
106     char *udcName;
107     char submit;
108     char submitExit;
109     struct Serial *port;
110     struct UsbCdcLineCoding lineCoding;
111     uint16_t serialState;
112 #define SERIAL_STATE_DCD     (1 << 0)
113 #define SERIAL_STATE_DSR     (1 << 1)
114 #define SERIAL_STATE_BREAK   (1 << 2)
115 #define SERIAL_STATE_RING    (1 << 3)
116 #define SERIAL_STATE_FRAMING (1 << 4)
117 #define SERIAL_STATE_PARITY  (1 << 5)
118 #define SERIAL_STATE_OVERRUN (1 << 6)
119 
120     uint16_t handshakeBits;
121     /* notification callbacks */
122     struct AcmNotifyMethod *notify;
123 };
124 
125 struct CtrlInfo {
126     uint8_t request;
127     struct AcmDevice *acm;
128 };
129 
130 extern struct UsbFnDeviceDesc g_acmFnDevice;
131 struct AcmDevice *SetUpAcmDevice(void);
132 void ReleaseAcmDevice(struct AcmDevice *acm);
133 void AcmEventCallback(struct UsbFnEvent * const event);
134 void AcmDeviceRelease(struct AcmDevice *acmDevice);
135 
136 #endif
137