summaryrefslogtreecommitdiff
path: root/common/usbx_host_classes/src/ux_host_class_pima_notification.c
blob: 2058d2a4cace90a198b4bd19cae251770106beae (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
/***************************************************************************
 * 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                                                        */
/**                                                                       */
/**   Pima Class                                                          */
/**                                                                       */
/**************************************************************************/
/**************************************************************************/


/* Include necessary system files.  */

#define UX_SOURCE_CODE

#include "ux_api.h"
#include "ux_host_class_pima.h"
#include "ux_host_stack.h"


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _ux_host_class_pima_notification                    PORTABLE C      */
/*                                                           6.1          */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Chaoqiong Xiao, Microsoft Corporation                               */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function is called by the completion thread when a transfer    */
/*    request has been completed either because the transfer is           */
/*    successful or there was an error.                                   */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    transfer_request                      Pointer to transfer request   */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _ux_host_stack_transfer_request       Transfer request              */
/*    _ux_utility_memory_copy               Copy memory                   */
/*    _ux_utility_short_get                 Get 16-bit value              */
/*    _ux_utility_long_get                  Get 32-bit value              */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    USBX stack                                                          */
/*                                                                        */
/**************************************************************************/
VOID  _ux_host_class_pima_notification(UX_TRANSFER *transfer_request)
{

UX_HOST_CLASS_PIMA                       *pima;
UX_HOST_CLASS_PIMA_SESSION                 *pima_session;
UX_HOST_CLASS_PIMA_EVENT                pima_event;

    /* Get the class instance for this transfer request.  */
    pima =  (UX_HOST_CLASS_PIMA *) transfer_request -> ux_transfer_request_class_instance;

    /* Check the state of the transfer.  If there is an error, we do not proceed with this notification.  */
    if (transfer_request -> ux_transfer_request_completion_code != UX_SUCCESS)
    {

        /* We have an error. We do not rehook another transfer if the device instance is shutting down or
           if the transfer was aborted by the class..  */
        if ((pima -> ux_host_class_pima_state ==  UX_HOST_CLASS_INSTANCE_SHUTDOWN) ||
            (transfer_request -> ux_transfer_request_completion_code == UX_TRANSFER_STATUS_ABORT))

            /* We do not proceed.  */
            return;
        else

        {

            /* Reactivate the PIMA interrupt pipe.  */
            _ux_host_stack_transfer_request(transfer_request);

            /* We do not proceed.  */
            return;
        }

    }

    /* Check if this packet is the first, if so we have the total length expected in the event.  */
    if (pima -> ux_host_class_pima_event_buffer_current_length == 0)
    {

        /* First packet. Maybe the only one needed. It may happen that the notification event is split amongst
           several interrupt packets.  */
        pima -> ux_host_class_pima_event_buffer_expected_length  = _ux_utility_long_get(transfer_request -> ux_transfer_request_data_pointer
                                                                                            + UX_HOST_CLASS_PIMA_AEI_DATA_LENGTH);

        /* Set the current offset to the beginning of the buffer.  */
        pima -> ux_host_class_pima_event_buffer_current_offset =  pima -> ux_host_class_pima_event_buffer;

    }

    /* Check the length of this payload and make sure we have enough space.  */
    if ((pima -> ux_host_class_pima_event_buffer_current_length + transfer_request -> ux_transfer_request_actual_length) <=
            UX_HOST_CLASS_PIMA_AEI_MAX_LENGTH)
    {

        /* We copy the current payload into our event notification buffer.  */
        _ux_utility_memory_copy(pima -> ux_host_class_pima_event_buffer_current_offset, transfer_request -> ux_transfer_request_data_pointer,
                            transfer_request -> ux_transfer_request_actual_length); /* Use case of memcpy is verified. */

        /* Set the new offset address. */
        pima -> ux_host_class_pima_event_buffer_current_offset += transfer_request -> ux_transfer_request_actual_length;

        /* Adjust the length.  */
        pima -> ux_host_class_pima_event_buffer_current_length += transfer_request -> ux_transfer_request_actual_length;
    }
    else

        /* We come here when we have a buffer overflow. Do not proceed.  */
        return;

    /* Check if we have a complete notification event.  */
    if (pima -> ux_host_class_pima_event_buffer_current_length == pima -> ux_host_class_pima_event_buffer_expected_length)
    {

        /* Save the current event in the pima instance.  First, unpack the event code.  */
        pima -> ux_host_class_pima_event_code = _ux_utility_short_get(pima -> ux_host_class_pima_event_buffer + UX_HOST_CLASS_PIMA_AEI_EVENT_CODE);

        /* Unpack the Transaction ID.  */
        pima -> ux_host_class_pima_event_transaction_id = _ux_utility_long_get(pima -> ux_host_class_pima_event_buffer + UX_HOST_CLASS_PIMA_AEI_TRANSACTION_ID);

        /* Unpack the parameter 1.  */
        pima -> ux_host_class_pima_event_parameter_1 = _ux_utility_long_get(pima -> ux_host_class_pima_event_buffer + UX_HOST_CLASS_PIMA_AEI_PARAMETER_1);

        /* Unpack the parameter 2.  */
        pima -> ux_host_class_pima_event_parameter_2 = _ux_utility_long_get(pima -> ux_host_class_pima_event_buffer + UX_HOST_CLASS_PIMA_AEI_PARAMETER_2);

        /* Unpack the parameter 3.  */
        pima -> ux_host_class_pima_event_parameter_3 = _ux_utility_long_get(pima -> ux_host_class_pima_event_buffer + UX_HOST_CLASS_PIMA_AEI_PARAMETER_3);

        /* Check if a session is valid.  */
        if (pima -> ux_host_class_pima_session != UX_NULL)
        {

            /* Get session pointer.  */
            pima_session =  pima -> ux_host_class_pima_session;

            /* Check if this session is valid or not.  */
            if ((pima_session -> ux_host_class_pima_session_magic == UX_HOST_CLASS_PIMA_MAGIC_NUMBER) &&
               (pima_session -> ux_host_class_pima_session_state == UX_HOST_CLASS_PIMA_SESSION_STATE_OPENED))
            {

                /* If the application demanded a callback when event occur, create a event notification
                   message.  */
                if (pima_session -> ux_host_class_pima_session_event_callback != UX_NULL)
                {

                    /* Fill in the pima event structure. First with pima instance.  */
                    pima_event.ux_host_class_pima_event_pima_instance = pima;

                    /* Fill in the opened session pointer.  Since the pima class only supports
                       one session, this address is hardwired to the opened session and is not coming from
                       the event buffer.  */
                    pima_event.ux_host_class_pima_event_session = pima_session;

                    /* Fill in all the event parameters in the event callback structure.  */
                    pima_event.ux_host_class_pima_event_code =  pima -> ux_host_class_pima_event_code;
                    pima_event.ux_host_class_pima_event_transaction_id =  pima -> ux_host_class_pima_event_transaction_id;
                    pima_event.ux_host_class_pima_event_parameter_1 =  pima -> ux_host_class_pima_event_parameter_1;
                    pima_event.ux_host_class_pima_event_parameter_2 =  pima -> ux_host_class_pima_event_parameter_2;
                    pima_event.ux_host_class_pima_event_parameter_3 =  pima -> ux_host_class_pima_event_parameter_3;

                    /* Send this event to the application.  */
                    pima_session -> ux_host_class_pima_session_event_callback(&pima_event);

                    /* If trace is enabled, insert this event into the trace buffer.  */
                    UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_PIMA_NOTIFICATION, pima,
                                            pima -> ux_host_class_pima_event_code,
                                            pima -> ux_host_class_pima_event_transaction_id,
                                            pima -> ux_host_class_pima_event_parameter_1,
                                            UX_TRACE_HOST_CLASS_EVENTS, 0, 0)

                }
            }
        }

        /* We will receive a complete new transaction.  */
        pima -> ux_host_class_pima_event_buffer_current_length =  0;
    }
    /* Reactivate the PIMA interrupt pipe.  */
    _ux_host_stack_transfer_request(transfer_request);

    /* Return to caller.  */
    return;
}