summaryrefslogtreecommitdiff
path: root/common/usbx_pictbridge/src/ux_pictbridge_dpsclient_thread.c
blob: 5f4521eac75802ba44943f5efb8bf5e7f033d69d (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
/***************************************************************************
 * 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_device_class_pima.h"


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _ux_pictbridge_dpsclient_thread                     PORTABLE C      */
/*                                                           6.1.12       */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Chaoqiong Xiao, Microsoft Corporation                               */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This is the Pictbridge dpsclient thread that receives and execute   */
/*    commands from the dpshost.                                          */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    pictbridge                             Pictbridge instance          */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    Completion Status                                                   */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    user application                                                    */
/*                                                                        */
/**************************************************************************/
VOID  _ux_pictbridge_dpsclient_thread(ULONG parameter)
{

UX_PICTBRIDGE                       *pictbridge;
ULONG                               actual_flags;
UINT                                status = UX_SUCCESS;
ULONG                               object_length;
UCHAR                               *pima_object_buffer;
UX_SLAVE_CLASS_PIMA                 *pima;
UX_SLAVE_CLASS_PIMA_OBJECT          *pima_object;

    /* Cast the parameter passed in the thread into the pictbridge pointer.  */
    UX_THREAD_EXTENSION_PTR_GET(pictbridge, UX_PICTBRIDGE, parameter)

    /* Loop forever waiting for changes signaled through the semaphore. */
    while (1)
    {

        /* Report errors via error handler.  */
        if (status != UX_SUCCESS)
            _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, status);

        /* We should wait for the host to send a script with a command.  */
        status =  _ux_system_event_flags_get(&pictbridge -> ux_pictbridge_event_flags_group, (UX_PICTBRIDGE_EVENT_FLAG_NOTIFY_JOB_STATUS |
                                                                                                UX_PICTBRIDGE_EVENT_FLAG_NOTIFY_DEVICE_STATUS),
                                                                                                UX_OR_CLEAR, &actual_flags, UX_PICTBRIDGE_EVENT_TIMEOUT);

        /* Check the status.  */
        if (status == UX_SUCCESS)
        {

            /* The event flag is meaningful. What did we get ? */
            if ((actual_flags & UX_PICTBRIDGE_EVENT_FLAG_NOTIFY_JOB_STATUS) || (actual_flags & UX_PICTBRIDGE_EVENT_FLAG_NOTIFY_DEVICE_STATUS))
            {

                /* We need to send a response now.  */
                /* Get the pima instance from the pictbridge container.  */
                pima = (UX_SLAVE_CLASS_PIMA *) pictbridge -> ux_pictbridge_pima;

                /* Get the address of the object container.  We need the host container here, as it is a response for the host.  */
                pima_object =  (UX_SLAVE_CLASS_PIMA_OBJECT *) pictbridge -> ux_pictbridge_object_host;

                /* Get the object buffer address.  */
                pima_object_buffer = pima_object -> ux_device_class_pima_object_buffer;

                /* Reset the object length.  */
                object_length =  0;

                /* Clear the object memory buffer.  */
                _ux_utility_memory_set(pima_object_buffer, 0, UX_PICTBRIDGE_MAX_PIMA_OBJECT_BUFFER); /* Use case of memset is verified. */

                /* Add the line <?xml version="1.0"?>  */
                status = _ux_pictbridge_object_tag_line_add(pima_object_buffer, object_length, _ux_pictbridge_xml_tag_line_xmlversion,
                                                            UX_PICTBRIDGE_TAG_FLAG_BEGIN | UX_PICTBRIDGE_TAG_FLAG_FORCE_LF,
                                                            UX_NULL, 0, UX_NULL, &pima_object_buffer, &object_length);
                if (status != UX_SUCCESS)
                    continue;

                /* Add the line <dps xmlns="http://www.cipa.jp/dps/schema/">  */
                status = _ux_pictbridge_object_tag_line_add(pima_object_buffer, object_length, _ux_pictbridge_xml_tag_line_dpsxmlns,
                                                            UX_PICTBRIDGE_TAG_FLAG_BEGIN | UX_PICTBRIDGE_TAG_FLAG_FORCE_LF,
                                                            UX_NULL, 0, UX_NULL, &pima_object_buffer, &object_length);
                if (status != UX_SUCCESS)
                    continue;

                /* Add the line <output>  */
                status = _ux_pictbridge_object_tag_line_add(pima_object_buffer, object_length, _ux_pictbridge_xml_tag_line_output,
                                                            UX_PICTBRIDGE_TAG_FLAG_BEGIN | UX_PICTBRIDGE_TAG_FLAG_FORCE_LF,
                                                            UX_NULL, 0, UX_NULL, &pima_object_buffer, &object_length);
                if (status != UX_SUCCESS)
                    continue;

                /* Add the line <result> xxxxxxxx </result> */
                status = _ux_pictbridge_object_tag_line_add(pima_object_buffer, object_length, _ux_pictbridge_xml_tag_line_result,
                                                        UX_PICTBRIDGE_TAG_FLAG_BEGIN | UX_PICTBRIDGE_TAG_FLAG_END | UX_PICTBRIDGE_TAG_FLAG_VARIABLE_HEXA,
                                                        UX_NULL, 0, (VOID *)(ALIGN_TYPE) pictbridge -> ux_pictbridge_operation_result, &pima_object_buffer, &object_length);
                if (status != UX_SUCCESS)
                    continue;

                /* Check what function was demanded.  */
                if (actual_flags & UX_PICTBRIDGE_EVENT_FLAG_NOTIFY_JOB_STATUS)

                    /* Add the line <notifyJobStatus/> */
                    status = _ux_pictbridge_object_tag_line_add(pima_object_buffer, object_length, _ux_pictbridge_xml_tag_line_notifyjobstatus,
                                                            UX_PICTBRIDGE_TAG_FLAG_BEGIN | UX_PICTBRIDGE_TAG_FLAG_FORCE_SLASH_AT_END,
                                                            UX_NULL, 0, UX_NULL, &pima_object_buffer, &object_length);

                else


                    /* Add the line <notifyDeviceStatus/> */
                    status = _ux_pictbridge_object_tag_line_add(pima_object_buffer, object_length, _ux_pictbridge_xml_tag_line_notifydevicestatus,
                                                            UX_PICTBRIDGE_TAG_FLAG_BEGIN | UX_PICTBRIDGE_TAG_FLAG_FORCE_SLASH_AT_END,
                                                            UX_NULL, 0, UX_NULL, &pima_object_buffer, &object_length);
                if (status != UX_SUCCESS)
                    continue;

                /* Add the line </output>  */
                status = _ux_pictbridge_object_tag_line_add(pima_object_buffer, object_length, _ux_pictbridge_xml_tag_line_output,
                                                UX_PICTBRIDGE_TAG_FLAG_END,
                                                UX_NULL, 0, UX_NULL, &pima_object_buffer, &object_length);
                if (status != UX_SUCCESS)
                    continue;

                /* Add the line </dps>  */
                status = _ux_pictbridge_object_tag_line_add(pima_object_buffer, object_length, _ux_pictbridge_xml_tag_line_dps,
                                                            UX_PICTBRIDGE_TAG_FLAG_END,
                                                            UX_NULL, 0, UX_NULL, &pima_object_buffer, &object_length);
                if (status != UX_SUCCESS)
                    continue;

                /* Get the length of the object and store it into the object data set.  */
                pima_object -> ux_device_class_pima_object_length = object_length;
                pima_object -> ux_device_class_pima_object_compressed_size = object_length;

                /* Save the handle for this object.  */
                pima_object -> ux_device_class_pima_object_handle_id =  UX_PICTBRIDGE_OBJECT_HANDLE_HOST_RESPONSE;

                /* 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_drsponse_name, pima_object -> ux_device_class_pima_object_filename);

                /* Send the notification to the host that an object has been added.  */
                status = _ux_device_class_pima_object_add(pima, UX_PICTBRIDGE_OBJECT_HANDLE_HOST_RESPONSE);
                if (status != UX_SUCCESS)
                    continue;

                /* Check if user registered callback function is present.  */
                if (pictbridge -> ux_pictbridge_dps_event_callback_function != UX_NULL)
                {

                    /* Call the user callback function.  */
                    (pictbridge -> ux_pictbridge_dps_event_callback_function)(pictbridge, actual_flags);
                }

            }
        }
    }
}