summaryrefslogtreecommitdiff
path: root/common/usbx_device_classes/src/ux_device_class_printer_control_request.c
blob: 255522a888e83cf7f18c20be4b1cbb2773c53faf (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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/***************************************************************************
 * 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 Printer Class                                                */
/**                                                                       */
/**************************************************************************/
/**************************************************************************/

#define UX_SOURCE_CODE


/* Include necessary system files.  */

#include "ux_api.h"
#include "ux_device_class_printer.h"
#include "ux_device_stack.h"


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _ux_device_class_printer_control_request            PORTABLE C      */
/*                                                           6.1.12       */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Chaoqiong Xiao, Microsoft Corporation                               */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function manages the requests sent by the host on the control  */
/*    endpoint with a CLASS or VENDOR SPECIFIC type.                      */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    command                               Pointer to class command      */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    (ux_device_class_printer_soft_reset)  Notify a software reset       */
/*    _ux_device_class_printer_soft_reset   Performs software reset       */
/*    _ux_device_stack_transfer_request     Transfer request              */
/*    _ux_utility_short_get                 Get short value from buffer   */
/*    _ux_utility_short_get_big_endian      Get big endian short value    */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Device Printer Class                                                */
/*                                                                        */
/**************************************************************************/
UINT  _ux_device_class_printer_control_request(UX_SLAVE_CLASS_COMMAND *command)
{
UX_DEVICE_CLASS_PRINTER                 *printer;
UX_SLAVE_CLASS                          *printer_class;
UX_SLAVE_INTERFACE                      *printer_interface;
UX_SLAVE_TRANSFER                       *transfer_request;
UX_SLAVE_DEVICE                         *device;
ULONG                                   request;
ULONG                                   value;
ULONG                                   request_length;
ULONG                                   length;
UCHAR                                   *buffer;
UCHAR                                   index_low;
UCHAR                                   *descriptor;
ULONG                                   descriptor_length;
ULONG                                   index;
UCHAR                                   found;

    /* Get the class container.  */
    printer_class =  command -> ux_slave_class_command_class_ptr;

    /* Get the class instance in the container.  */
    printer = (UX_DEVICE_CLASS_PRINTER *) printer_class -> ux_slave_class_instance;

    /* Get the interface.  */
    printer_interface = printer_class -> ux_slave_class_interface;

    /* Get the pointer to the device.  */
    device =  &_ux_system_slave -> ux_system_slave_device;

    /* Get the pointer to the transfer request associated with the control endpoint.  */
    transfer_request =  &device -> ux_slave_device_control_endpoint.ux_slave_endpoint_transfer_request;

    /* Extract all necessary fields of the request.  */
    request =  *(transfer_request -> ux_slave_transfer_request_setup + UX_SETUP_REQUEST);

    /* Extract all necessary fields of the value.  */
    value =  _ux_utility_short_get(transfer_request -> ux_slave_transfer_request_setup + UX_SETUP_VALUE);
    index_low = transfer_request -> ux_slave_transfer_request_setup[UX_SETUP_INDEX];

    /* Pickup the request length.  */
    request_length =   _ux_utility_short_get(transfer_request -> ux_slave_transfer_request_setup + UX_SETUP_LENGTH);

    /* Get buffer to fill.  */
    buffer = transfer_request -> ux_slave_transfer_request_data_pointer;

    /* Here we proceed only the standard request we know of at the device level.  */
    switch (request)
    {
    case UX_DEVICE_CLASS_PRINTER_GET_DEVICE_ID:

        /* Sanity check
           - wValue == config index
           - wIndex_high == interface (already checked before entry, index of
                                       interface should equal to interface number)
           - wIndex_low == alternate  */
        if (index_low != (UCHAR)printer_interface -> ux_slave_interface_descriptor.bAlternateSetting)
            return(UX_ERROR);

        /* Check config index.  */
        descriptor = _ux_system_slave -> ux_system_slave_device_framework;
        length = _ux_system_slave->ux_system_slave_device_framework_length;
        index = 0;
        found = 0;
        while(length)
        {

            /* By default use bLength @ 0.  */
            descriptor_length = descriptor[0];

            /* Check bDescriptorType @ 1.  */
            switch(descriptor[1])
            {
            case UX_CONFIGURATION_DESCRIPTOR_ITEM:

                /* It's configuration requested?  */
                if (index == value)
                {

                    /* Check if bConfigurationValue @ 5 is as expected.  */
                    if ((UCHAR)printer_class -> ux_slave_class_configuration_number != descriptor[5])
                        return(UX_ERROR);

                    /* Find the configuration.  */
                    found = 1;
                    break;
                }

                /* Target to next configuration index.  */
                index ++;

                /* Use wTotalLength @ 2.  */
                descriptor_length = _ux_utility_short_get(descriptor + 2);
                break;

            default:
                break;
            }

            /* Break if found.  */
            if (found)
                break;

            /* Descriptor broken check.  */
            if (descriptor_length < 2 ||
                descriptor_length > length)
            {

                /* Error trap!  */
                _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_DESCRIPTOR_CORRUPTED);
                return(UX_DESCRIPTOR_CORRUPTED);
            }

            /* Next descriptor.  */
            descriptor += descriptor_length;
            length -= descriptor_length;
        }

        /* Request parameter not expected if not found.  */
        if (!found)
            return(UX_ERROR);

        /* Length of data (first two bytes in big endian).  */
        length = _ux_utility_short_get_big_endian(printer ->
                        ux_device_class_printer_parameter.ux_device_class_printer_device_id);

        /* Prepare device ID to send.  */
        if (length)
        {

            /* Sanity check.  */
            if (length > UX_SLAVE_REQUEST_CONTROL_MAX_LENGTH)
            {

                /* Error trap!  */
                _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_BUFFER_OVERFLOW);
                return(UX_BUFFER_OVERFLOW);
            }

            /* Copy data.  */
            _ux_utility_memory_copy(buffer, printer ->
                        ux_device_class_printer_parameter.ux_device_class_printer_device_id,
                        length); /* Use case of memcpy is verified. */
        }

        /* Set the phase of the transfer to data out.  */
        transfer_request -> ux_slave_transfer_request_phase =  UX_TRANSFER_PHASE_DATA_OUT;

        /* Transfer.  */
        _ux_device_stack_transfer_request(transfer_request, length, request_length);
        break ;

    case UX_DEVICE_CLASS_PRINTER_GET_PORT_STATUS:

        /* Sanity check.  */
        if (request_length < 1)
            return(UX_ERROR);

        /* Fill status byte.  */
        *buffer = (UCHAR)printer -> ux_device_class_printer_port_status;

        /* Set the phase of the transfer to data out.  */
        transfer_request -> ux_slave_transfer_request_phase = UX_TRANSFER_PHASE_DATA_OUT;

        /* Perform the data transfer.  */
        _ux_device_stack_transfer_request(transfer_request, 1, request_length);
        break;

    case UX_DEVICE_CLASS_PRINTER_SOFT_RESET:
        _ux_device_class_printer_soft_reset(printer);

        /* If there is a soft reset function call it.  */
        if (printer -> ux_device_class_printer_parameter.
                                ux_device_class_printer_soft_reset != UX_NULL)
        {

            /* Invoke the application callback.  */
            printer -> ux_device_class_printer_parameter.
                                ux_device_class_printer_soft_reset(printer);
        }
        break;

    default:

        /* Unknown function. It's not handled.  */
        return(UX_ERROR);
    }

    /* It's handled.  */
    return(UX_SUCCESS);
}