summaryrefslogtreecommitdiff
path: root/common/usbx_device_classes/src/ux_device_class_video_read_task_function.c
blob: 2ef361b0249539e2420e89b5361c2f17175ed42e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/***************************************************************************
 * Copyright (c) 2024 Microsoft Corporation
 * Copyright (c) 2026-present Eclipse ThreadX contributors
 *
 * This program and the accompanying materials are made available under the
 * terms of the MIT License which is available at
 * https://opensource.org/licenses/MIT.
 *
 * SPDX-License-Identifier: MIT
 **************************************************************************/

/**************************************************************************/
/**************************************************************************/
/**                                                                       */
/** USBX Component                                                        */
/**                                                                       */
/**   Device Video Class                                                  */
/**                                                                       */
/**************************************************************************/
/**************************************************************************/

#define UX_SOURCE_CODE


/* Include necessary system files.  */

#include "ux_api.h"
#include "ux_device_class_video.h"
#include "ux_device_stack.h"


#if defined(UX_DEVICE_STANDALONE)
/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _ux_device_class_video_read_task_function           PORTABLE C      */
/*                                                           6.3.0        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Chaoqiong Xiao, Microsoft Corporation                               */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function is the background task of the video stream read.      */
/*                                                                        */
/*    It's for standalone mode.                                           */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    stream                                Pointer to video stream       */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    State machine status                                                */
/*    UX_STATE_EXIT                         Device not configured         */
/*    UX_STATE_IDLE                         No streaming transfer running */
/*    UX_STATE_WAIT                         Streaming transfer running    */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _ux_device_stack_transfer_run         Run transfer state machine    */
/*    _ux_utility_memory_copy               Copy memory                   */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    USBX Device Stack                                                   */
/*                                                                        */
/**************************************************************************/
UINT _ux_device_class_video_read_task_function(UX_DEVICE_CLASS_VIDEO_STREAM *stream)
{
UX_SLAVE_DEVICE                 *device;
UX_SLAVE_ENDPOINT               *endpoint;
UX_SLAVE_TRANSFER               *transfer;
UCHAR                           *next_pos;
UX_DEVICE_CLASS_VIDEO_PAYLOAD   *next_payload;
ULONG                           max_packet_size;
ULONG                           actual_length;
UINT                            status;


    /* Get the pointer to the device.  */
    device = stream -> ux_device_class_video_stream_video -> ux_device_class_video_device;

    /* Check if the device is configured.  */
    if (device -> ux_slave_device_state != UX_DEVICE_CONFIGURED)
    {
        stream -> ux_device_class_video_stream_task_state = UX_STATE_EXIT;
        return(UX_STATE_EXIT);
    }

    /* Get the endpoint.  */
    endpoint = stream -> ux_device_class_video_stream_endpoint;

    /* No endpoint ready, maybe it's alternate setting 0.  */
    if (endpoint == UX_NULL)
        return(UX_STATE_IDLE);

    /* Check if background transfer task is started.  */
    if (stream -> ux_device_class_video_stream_task_state == UX_DEVICE_CLASS_VIDEO_STREAM_RW_STOP)
        return(UX_STATE_IDLE);

    /* Get transfer instance.  */
    transfer = &endpoint -> ux_slave_endpoint_transfer_request;

    /* If not started yet, reset transfer and start polling.  */
    if (stream -> ux_device_class_video_stream_task_state == UX_DEVICE_CLASS_VIDEO_STREAM_RW_START)
    {

        /* Next state: transfer wait.  */
        stream -> ux_device_class_video_stream_task_state = UX_DEVICE_CLASS_VIDEO_STREAM_RW_WAIT;

#if UX_DEVICE_ENDPOINT_BUFFER_OWNER == 1

        /* Zero copy: directly use frame buffer.  */
        transfer -> ux_slave_transfer_request_data_pointer = stream ->
                ux_device_class_video_stream_transfer_pos -> ux_device_class_video_payload_data;
#endif

        /* Reset transfer state.  */
        UX_SLAVE_TRANSFER_STATE_RESET(transfer);
    }

    /* Run transfer state machine.  */
    max_packet_size = endpoint -> ux_slave_endpoint_transfer_request.
                                ux_slave_transfer_request_transfer_length;
    status = _ux_device_stack_transfer_run(transfer, max_packet_size, max_packet_size);

    /* Error case.  */
    if (status < UX_STATE_NEXT)
    {

        /* Error on background transfer task start.  */
        stream -> ux_device_class_video_stream_task_state = UX_STATE_RESET;
        stream -> ux_device_class_video_stream_task_status =
                        transfer -> ux_slave_transfer_request_completion_code;

        /* Error notification!  */
        _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_TRANSFER_ERROR);
        return(UX_STATE_EXIT);
    }

    /* Success case.  */
    if (status == UX_STATE_NEXT)
    {

        /* Next state: start.  */
        stream -> ux_device_class_video_stream_task_state = UX_DEVICE_CLASS_VIDEO_STREAM_RW_START;
        stream -> ux_device_class_video_stream_task_status =
                        transfer -> ux_slave_transfer_request_completion_code;

        /* Get actual transfer length.  */
        actual_length = transfer -> ux_slave_transfer_request_actual_length;

        /* Frame received, log it.  */
        stream -> ux_device_class_video_stream_transfer_pos -> ux_device_class_video_payload_length = actual_length;

#if UX_DEVICE_ENDPOINT_BUFFER_OWNER == 1

        /* Zero copy: data already in frame buffer.  */
#else

        /* Copy data from endpoint buffer.  */
        _ux_utility_memory_copy(stream -> ux_device_class_video_stream_transfer_pos -> ux_device_class_video_payload_data,
                        transfer -> ux_slave_transfer_request_data_pointer,
                        actual_length); /* Use case of memcpy is verified. */
#endif

        /* For simple, do not advance the transfer position if there is overflow.  */
        next_pos = (UCHAR *)stream -> ux_device_class_video_stream_transfer_pos;
        next_pos += stream -> ux_device_class_video_stream_payload_buffer_size;
        if (next_pos >= stream -> ux_device_class_video_stream_buffer + stream -> ux_device_class_video_stream_buffer_size)
            next_pos = stream -> ux_device_class_video_stream_buffer;
        next_payload = (UX_DEVICE_CLASS_VIDEO_PAYLOAD *)next_pos;

        /* Check overflow!  */
        if (next_payload -> ux_device_class_video_payload_length > 0)
        {

            /* Error notification!  */
            stream -> ux_device_class_video_stream_buffer_error_count ++;
            _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_BUFFER_OVERFLOW);
        }
        else

            /* Update transfer position.  */
            stream -> ux_device_class_video_stream_transfer_pos = next_payload;

        /* Invoke notification callback. */
        if (stream -> ux_device_class_video_stream_callbacks.ux_device_class_video_stream_payload_done != UX_NULL)
            stream -> ux_device_class_video_stream_callbacks.ux_device_class_video_stream_payload_done(stream, actual_length);
    }

    /* Keep waiting.  */
    return(UX_STATE_WAIT);
}
#endif