summaryrefslogtreecommitdiff
path: root/common/usbx_pictbridge/src/ux_pictbridge_dpshost_object_get.c
blob: e44c4e2c63a267cdd37a022d45d3b7ae61c50992 (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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/***************************************************************************
 * 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                                                        */
/**                                                                       */
/**   Pictbridge Application                                              */
/**                                                                       */
/**************************************************************************/
/**************************************************************************/


/* Include necessary system files.  */

#define UX_SOURCE_CODE

#include "ux_api.h"
#include "ux_pictbridge.h"
#include "ux_host_class_pima.h"


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _ux_pictbridge_dpshost_object_get                   PORTABLE C      */
/*                                                           6.1.12       */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Chaoqiong Xiao, Microsoft Corporation                               */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function obtains an object and execute its script.             */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    pictbridge                             Pictbridge instance          */
/*    object_handle                          Object handle                */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    Completion Status                                                   */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    _ux_pictbridge_dpshost_thread                                       */
/*                                                                        */
/**************************************************************************/
UINT  _ux_pictbridge_dpshost_object_get(UX_PICTBRIDGE *pictbridge, ULONG object_handle)
{
UINT                                status;
UX_HOST_CLASS_PIMA                  *pima;
UX_HOST_CLASS_PIMA_SESSION          *pima_session;
UX_HOST_CLASS_PIMA_OBJECT           *pima_object;
ULONG                               object_length;
ULONG                               requested_length;
ULONG                               actual_length;
UCHAR                               *object_buffer;
UCHAR                               object_file_name[UX_PICTBRIDGE_MAX_FILE_NAME_SIZE + 1]; /* +1 for null-terminator */
UINT                                length, length1;

    /* Get the pima instance from the pictbridge container.  */
    pima = (UX_HOST_CLASS_PIMA *) pictbridge -> ux_pictbridge_pima;

    /* And now the session since we use it.  */
    pima_session =  (UX_HOST_CLASS_PIMA_SESSION *) pictbridge -> ux_pictbridge_session;

    /* Get the address of the object container.  */
    pima_object =  (UX_HOST_CLASS_PIMA_OBJECT *) pictbridge -> ux_pictbridge_object_host;

    /* Get the object info structure.  */
    status = _ux_host_class_pima_object_info_get(pima, pima_session,
                                             object_handle, pima_object);
    if (status != UX_SUCCESS)
    {

        /* Return an error.  */
        return(UX_PICTBRIDGE_ERROR_INVALID_OBJECT_HANDLE);
    }

    /* Check if this is a script.  If the object is not a script or does not contain the
       File Name we expect, we ignore it.  */
    if (pima_object -> ux_host_class_pima_object_format == UX_HOST_CLASS_PIMA_OFC_SCRIPT)
    {

        /* Yes this is a script. We need to search for the DREQUEST.DPS file name.
           Get the file name length (with null-terminator).  */
        length1 = (UINT)(*pima_object -> ux_host_class_pima_object_filename);

        /* Check the file name from this script. It should be in the form DREQUEST.DPS.  */
        if (length1 <= UX_PICTBRIDGE_MAX_FILE_NAME_SIZE)
        {

            /* Invalidate length, on error it's untouched.  */
            length = UX_PICTBRIDGE_MAX_FILE_NAME_SIZE + 1;
            _ux_utility_string_length_check(_ux_pictbridge_drequest_name, &length, UX_PICTBRIDGE_MAX_FILE_NAME_SIZE);
            if ((length + 1) == length1)
            {

                /* Get the file name in a ascii format (with null-terminator). */
                _ux_utility_unicode_to_string(pima_object -> ux_host_class_pima_object_filename, object_file_name);

                /* So far, the length of name of the files are the same.
                Compare names now (since length is same just compare without null-terminator). */
                if (_ux_utility_memory_compare(_ux_pictbridge_drequest_name, object_file_name,
                                            length) ==  UX_SUCCESS)
                {

                    /* Yes this is a script. Get the entire object in memory.  */
                    /* Get script length from the object info.  */
                    object_length = pima_object -> ux_host_class_pima_object_compressed_size;

                    /* Check for overflow.  */
                    if (object_length > UX_PICTBRIDGE_MAX_PIMA_OBJECT_BUFFER)
                        return(UX_PICTBRIDGE_ERROR_SCRIPT_BUFFER_OVERFLOW);

                    /* Open the object.  */
                    status = _ux_host_class_pima_object_open(pima, pima_session, object_handle, pima_object);

                    /* Check status.  */
                    if (status != UX_SUCCESS)
                        return(status);

                    /* Set the object buffer pointer.  */
                    object_buffer = pima_object -> ux_host_class_pima_object_buffer;

                    /* Obtain all the object data.  */
                    while(object_length != 0)
                    {

                        /* Calculate what length to request.  */
                        if (object_length > UX_PICTBRIDGE_MAX_PIMA_OBJECT_BUFFER)

                            /* Request maximum length.  */
                            requested_length = UX_PICTBRIDGE_MAX_PIMA_OBJECT_BUFFER;

                        else

                            /* Request remaining length.  */
                            requested_length = object_length;

                        /* Get the object data.  */
                        status = _ux_host_class_pima_object_get(pima, pima_session, object_handle, pima_object,
                                                                object_buffer, requested_length, &actual_length);
                        if (status != UX_SUCCESS)
                        {

                            /* We had a problem, abort the transfer.  */
                            _ux_host_class_pima_object_transfer_abort(pima, pima_session, object_handle, pima_object);

                            /* And close the object.  */
                            _ux_host_class_pima_object_close(pima, pima_session, object_handle, pima_object);

                            return(status);
                        }

                        /* We have received some data, update the length remaining.  */
                        object_length -= actual_length;

                        /* Update the buffer address.  */
                        object_buffer += actual_length;
                    }

                    /* Close the object.  */
                    status = _ux_host_class_pima_object_close(pima, pima_session, object_handle, pima_object);

                    /* Check the error.  */
                    if (status != UX_SUCCESS)
                        return(UX_PICTBRIDGE_ERROR_INVALID_OBJECT_HANDLE);

                    /* We now have the entire script into memory. Parse the object and execute the script.  */
                    status = _ux_pictbridge_object_parse(pictbridge, pima_object -> ux_host_class_pima_object_buffer,
                                                pima_object -> ux_host_class_pima_object_compressed_size);

                    /* Analyze the status from the parsing and determine the result code to be sent back.  */
                    switch (status)
                    {
                        case UX_SUCCESS                                     :

                            /* Set the result code to OK.  */
                            pictbridge -> ux_pictbridge_operation_result =  UX_PICTBRIDGE_ACTION_RESULT_OK;
                            break;

                        case UX_PICTBRIDGE_ERROR_SCRIPT_SYNTAX_ERROR        :

                            /* Set the result code to Error.  */
                            pictbridge -> ux_pictbridge_operation_result =  UX_PICTBRIDGE_ACTION_RESULT_NOT_SUPPORTED_IP;
                            break;

                        case UX_PICTBRIDGE_ERROR_PARAMETER_UNKNOWN          :

                            /* Set the result code to Error.  */
                            pictbridge -> ux_pictbridge_operation_result =  UX_PICTBRIDGE_ACTION_RESULT_NOT_SUPPORTED_UP;
                            break;

                        case UX_PICTBRIDGE_ERROR_PARAMETER_MISSING          :

                            /* Set the result code to Error.  */
                            pictbridge -> ux_pictbridge_operation_result =  UX_PICTBRIDGE_ACTION_RESULT_NOT_SUPPORTED_MP;
                            break;

                        default :

                            /* Set the result code to Error.  */
                            pictbridge -> ux_pictbridge_operation_result =  UX_PICTBRIDGE_ACTION_RESULT_NOT_SUPPORTED_DEFAULT;
                            break;
                    }

                    /* We have completed the parsing of the XML object.  We will send an object back to the client.
                    Generate the output report.  */
                    status = _ux_pictbridge_dpshost_output_object_create(pictbridge);

                    /* Get the length of the object and store it into the object data set.  */
                    pima_object -> ux_host_class_pima_object_compressed_size = pima_object -> ux_host_class_pima_object_length;

                    /* We need to change the file name to DRSPONSE.DPS. Encode the file name from ascii format into Unicode.  */
                    _ux_utility_string_to_unicode(_ux_pictbridge_hrsponse_name, pima_object -> ux_host_class_pima_object_filename);

                    /* Send a script info.  */
                    status = _ux_host_class_pima_object_info_send(pima, pima_session, 0, 0, pima_object);
                    if (status != UX_SUCCESS)
                        return(status);

                    /* Open the object.  */
                    status = _ux_host_class_pima_object_open(pima, pima_session,
                                                            object_handle,
                                                            pima_object);
                    if (status != UX_SUCCESS)
                        return(status);

                    /* Get the object length.  */
                    object_length =  pima_object -> ux_host_class_pima_object_compressed_size;

                    /* Recall the object buffer address.  */
                    object_buffer = pima_object -> ux_host_class_pima_object_buffer;

                    /* Send all the object data.  */
                    while(object_length != 0)
                    {

                        /* Calculate what length to request.  */
                        if (object_length > UX_PICTBRIDGE_MAX_PIMA_OBJECT_BUFFER)

                            /* Request maximum length.  */
                            requested_length = UX_PICTBRIDGE_MAX_PIMA_OBJECT_BUFFER;

                        else

                            /* Request remaining length.  */
                            requested_length = object_length;


                        /* Send the object data.  */
                        status = _ux_host_class_pima_object_send(pima, pima_session, pima_object,
                                                                    object_buffer, requested_length);
                        if (status != UX_SUCCESS)
                        {

                            /* Abort the transfer.  */
                            _ux_host_class_pima_object_transfer_abort(pima, pima_session, object_handle, pima_object);

                            return(status);
                        }

                        /* We have sent some data, update the length remaining.  */
                    object_length -= requested_length;
                    }

                    /* Close the object.  */
                    status = _ux_host_class_pima_object_close(pima, pima_session, object_handle, pima_object);

                    /* Check the status.  */
                    if (status != UX_SUCCESS)
                        return(status);
                }
            }
        }
    }

    return(UX_SUCCESS);
}