GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usbx_device_classes/src/ux_device_class_cdc_acm_bulkin_thread.c Lines: 35 35 100.0 %
Date: 2026-03-06 18:57:10 Branches: 16 16 100.0 %

Line Branch Exec Source
1
/***************************************************************************
2
 * Copyright (c) 2024 Microsoft Corporation
3
 * Copyright (c) 2026-present Eclipse ThreadX contributors
4
 *
5
 * This program and the accompanying materials are made available under the
6
 * terms of the MIT License which is available at
7
 * https://opensource.org/licenses/MIT.
8
 *
9
 * SPDX-License-Identifier: MIT
10
 **************************************************************************/
11
12
/**************************************************************************/
13
/**************************************************************************/
14
/**                                                                       */
15
/** USBX Component                                                        */
16
/**                                                                       */
17
/**   Device CDC_ACM Class                                                */
18
/**                                                                       */
19
/**************************************************************************/
20
/**************************************************************************/
21
22
#define UX_SOURCE_CODE
23
24
25
/* Include necessary system files.  */
26
27
#include "ux_api.h"
28
#include "ux_device_class_cdc_acm.h"
29
#include "ux_device_stack.h"
30
31
32
#if !defined(UX_DEVICE_CLASS_CDC_ACM_TRANSMISSION_DISABLE) && !defined(UX_DEVICE_STANDALONE)
33
/**************************************************************************/
34
/*                                                                        */
35
/*  FUNCTION                                               RELEASE        */
36
/*                                                                        */
37
/*    _ux_device_class_cdc_acm_bulkin_thread              PORTABLE C      */
38
/*                                                           6.3.0        */
39
/*  AUTHOR                                                                */
40
/*                                                                        */
41
/*    Chaoqiong Xiao, Microsoft Corporation                               */
42
/*                                                                        */
43
/*  DESCRIPTION                                                           */
44
/*                                                                        */
45
/*    This function is the thread of the cdc_acm bulkin endpoint. The bulk*/
46
/*    IN endpoint is used when the device wants to write data to be sent  */
47
/*    to the host.                                                        */
48
/*                                                                        */
49
/*    It's for RTOS mode.                                                 */
50
/*                                                                        */
51
/*  INPUT                                                                 */
52
/*                                                                        */
53
/*    cdc_acm_class                             Address of cdc_acm class  */
54
/*                                                container               */
55
/*                                                                        */
56
/*  OUTPUT                                                                */
57
/*                                                                        */
58
/*    None                                                                */
59
/*                                                                        */
60
/*  CALLS                                                                 */
61
/*                                                                        */
62
/*    _ux_device_stack_transfer_request     Request transfer              */
63
/*                                                                        */
64
/*  CALLED BY                                                             */
65
/*                                                                        */
66
/*    ThreadX                                                             */
67
/*                                                                        */
68
/**************************************************************************/
69
2
VOID  _ux_device_class_cdc_acm_bulkin_thread(ULONG cdc_acm_class)
70
{
71
72
UX_SLAVE_CLASS_CDC_ACM          *cdc_acm;
73
UX_SLAVE_DEVICE                 *device;
74
UX_SLAVE_ENDPOINT               *endpoint;
75
UX_SLAVE_INTERFACE              *interface_ptr;
76
UX_SLAVE_TRANSFER               *transfer_request;
77
UINT                            status;
78
ULONG                           actual_flags;
79
ULONG                           transfer_length;
80
ULONG                           host_length;
81
ULONG                           total_length;
82
ULONG                           sent_length;
83
84
85
    /* Get the cdc_acm instance from this class container.  */
86
2
    UX_THREAD_EXTENSION_PTR_GET(cdc_acm, UX_SLAVE_CLASS_CDC_ACM, cdc_acm_class)
87
88
    /* Get the pointer to the device.  */
89
2
    device =  &_ux_system_slave -> ux_system_slave_device;
90
91
    /* This is the first time we are activated. We need the interface to the class.  */
92
2
    interface_ptr =  cdc_acm -> ux_slave_class_cdc_acm_interface;
93
94
    /* Locate the endpoints.  */
95
2
    endpoint =  interface_ptr -> ux_slave_interface_first_endpoint;
96
97
    /* Check the endpoint direction, if IN we have the correct endpoint.  */
98
2
    if ((endpoint -> ux_slave_endpoint_descriptor.bEndpointAddress & UX_ENDPOINT_DIRECTION) != UX_ENDPOINT_IN)
99
    {
100
101
        /* So the next endpoint has to be the IN endpoint.  */
102
1
        endpoint =  endpoint -> ux_slave_endpoint_next_endpoint;
103
    }
104
105
#if (UX_DEVICE_ENDPOINT_BUFFER_OWNER == 1) && !defined(UX_DEVICE_CLASS_CDC_ACM_ZERO_COPY)
106
    endpoint -> ux_slave_endpoint_transfer_request.ux_slave_transfer_request_data_pointer =
107
                                UX_DEVICE_CLASS_CDC_ACM_WRITE_BUFFER(cdc_acm);
108
#endif
109
110
    /* This thread runs forever but can be suspended or resumed.  */
111
    while(1)
112
    {
113
114
        /* Get the transfer request for the bulk IN pipe.  */
115
5
        transfer_request =  &endpoint -> ux_slave_endpoint_transfer_request;
116
117
        /* As long as the device is in the CONFIGURED state.  */
118
17
        while (device -> ux_slave_device_state == UX_DEVICE_CONFIGURED)
119
        {
120
121
            /* Wait until we have a event sent by the application. */
122
10
            status =  _ux_utility_event_flags_get(&cdc_acm -> ux_slave_class_cdc_acm_event_flags_group, UX_DEVICE_CLASS_CDC_ACM_WRITE_EVENT,
123
                                                                                            UX_OR_CLEAR, &actual_flags, UX_WAIT_FOREVER);
124
125
            /* Check the completion code. */
126
8
            if (status == UX_SUCCESS)
127
            {
128
129
                /* Get the length of the entire buffer to send.  */
130
7
                total_length = cdc_acm -> ux_slave_class_cdc_acm_callback_total_length;
131
132
#if defined(UX_DEVICE_CLASS_CDC_ACM_ZERO_COPY)
133
134
#if !defined(UX_DEVICE_CLASS_CDC_ACM_WRITE_AUTO_ZLP)
135
136
                /* Assume host request length just equal.  */
137
                host_length = total_length;
138
#else
139
140
                /* Assume host request length larger to append ZLP automatically.  */
141
                host_length = total_length + 1;
142
#endif
143
144
                /* Pass all data to lower level transfer.  */
145
                transfer_length = total_length;
146
147
                /* Setup the data pointer.  */
148
                transfer_request -> ux_slave_transfer_request_data_pointer = cdc_acm -> ux_slave_class_cdc_acm_callback_data_pointer;
149
150
                /* Issue the transfer request.  */
151
                status = _ux_device_stack_transfer_request(transfer_request, transfer_length, host_length);
152
153
                /* Update length sent.  */
154
                sent_length = transfer_request -> ux_slave_transfer_request_actual_length;
155
#else
156
157
                /* Duplicate the data pointer to keep a current pointer.  */
158
7
                cdc_acm -> ux_slave_class_cdc_acm_callback_current_data_pointer = cdc_acm -> ux_slave_class_cdc_acm_callback_data_pointer;
159
160
                /* Reset sent length.  */
161
7
                sent_length = 0;
162
163
                /* Special ZLP case.  */
164
7
                if (total_length == 0)
165
166
                    /* Send the zlp to the host.  */
167
1
                    status =  _ux_device_stack_transfer_request(transfer_request, 0, 0);
168
169
                else
170
                {
171
172
                    /* We should send the total length.  But we may have a case of ZLP. */
173
6
                    host_length = UX_DEVICE_CLASS_CDC_ACM_WRITE_BUFFER_SIZE;
174
13
                    while (total_length)
175
                    {
176
177
                        /* Check the length remaining to send.  */
178
7
                        if (total_length > UX_DEVICE_CLASS_CDC_ACM_WRITE_BUFFER_SIZE)
179
180
                            /* We can't fit all the length.  */
181
1
                            transfer_length = UX_DEVICE_CLASS_CDC_ACM_WRITE_BUFFER_SIZE;
182
183
                        else
184
                        {
185
186
                            /* We can send everything.  */
187
6
                            transfer_length =  total_length;
188
189
#if !defined(UX_DEVICE_CLASS_CDC_ACM_WRITE_AUTO_ZLP)
190
191
                            /* Assume expected length matches length to send.  */
192
6
                            host_length = total_length;
193
#else
194
195
                            /* Assume expected more to let stack append ZLP if needed.  */
196
                            host_length = UX_DEVICE_CLASS_CDC_ACM_WRITE_BUFFER_SIZE + 1;
197
#endif
198
                        }
199
200
                        /* Copy the payload locally.  */
201
7
                        _ux_utility_memory_copy (transfer_request -> ux_slave_transfer_request_data_pointer,
202
7
                                                cdc_acm -> ux_slave_class_cdc_acm_callback_current_data_pointer,
203
                                                transfer_length); /* Use case of memcpy is verified. */
204
205
                        /* Send the acm payload to the host.  */
206
7
                        status =  _ux_device_stack_transfer_request(transfer_request, transfer_length, host_length);
207
208
                        /* Check the status.  */
209
7
                        if (status != UX_SUCCESS)
210
                        {
211
212
                            /* Reset total_length as we need to get out of loop. */
213
3
                            total_length = 0;
214
                        }
215
                        else
216
                        {
217
218
                            /* Update sent length.  */
219
4
                            sent_length += transfer_length;
220
221
                            /* Decrement length to be sent.  */
222
4
                            total_length -= transfer_length;
223
224
                            /* And update the current pointer.  */
225
4
                            cdc_acm -> ux_slave_class_cdc_acm_callback_current_data_pointer += transfer_length;
226
                        }
227
                    }
228
                }
229
#endif
230
231
                /* Schedule of transmission was completed.  */
232
7
                cdc_acm -> ux_slave_class_cdc_acm_scheduled_write = UX_FALSE;
233
234
                /* We get here when the entire user data payload has been sent or if there is an error. */
235
                /* If there is a callback defined by the application, send the transaction event to it.  */
236
7
                if (cdc_acm -> ux_device_class_cdc_acm_write_callback != UX_NULL)
237
238
                    /* Callback exists. */
239
6
                    cdc_acm -> ux_device_class_cdc_acm_write_callback(cdc_acm, status, sent_length);
240
241
                /* Now we return to wait for an event from the application or the user to stop the transmission.  */
242
            }
243
            else
244
            {
245
1
                break;
246
            }
247
        }
248
249
    /* We need to suspend ourselves. We will be resumed by the device enumeration module or when a change of alternate setting happens.  */
250
3
    _ux_device_thread_suspend(&cdc_acm -> ux_slave_class_cdc_acm_bulkin_thread);
251
    }
252
}
253
#endif