1 /***************************************************************************
2  * Copyright (c) 2024 Microsoft Corporation
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the MIT License which is available at
6  * https://opensource.org/licenses/MIT.
7  *
8  * SPDX-License-Identifier: MIT
9  **************************************************************************/
10 
11 
12 /**************************************************************************/
13 /**************************************************************************/
14 /**                                                                       */
15 /** USBX Component                                                        */
16 /**                                                                       */
17 /**   Host Sierra Wireless AR module class                                */
18 /**                                                                       */
19 /**************************************************************************/
20 /**************************************************************************/
21 
22 
23 /* Include necessary system files.  */
24 
25 #define UX_SOURCE_CODE
26 
27 #include "ux_api.h"
28 #include "ux_host_class_swar.h"
29 #include "ux_host_stack.h"
30 
31 
32 /**************************************************************************/
33 /*                                                                        */
34 /*  FUNCTION                                               RELEASE        */
35 /*                                                                        */
36 /*    _ux_host_class_swar_reception_callback              PORTABLE C      */
37 /*                                                           6.1          */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Chaoqiong Xiao, Microsoft Corporation                               */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function is the callback from the USBX transfer functions,     */
45 /*    it is called when a full or partial transfer has been done for a    */
46 /*    bulk in transfer. It calls back the application.                    */
47 /*                                                                        */
48 /*  INPUT                                                                 */
49 /*                                                                        */
50 /*    transfer_request                      Pointer to transfer request   */
51 /*                                                                        */
52 /*  OUTPUT                                                                */
53 /*                                                                        */
54 /*    None                                                                */
55 /*                                                                        */
56 /*  CALLS                                                                 */
57 /*                                                                        */
58 /*    _ux_host_stack_transfer_request       Process transfer request      */
59 /*                                                                        */
60 /*  CALLED BY                                                             */
61 /*                                                                        */
62 /*    Application                                                         */
63 /*                                                                        */
64 /*  RELEASE HISTORY                                                       */
65 /*                                                                        */
66 /*    DATE              NAME                      DESCRIPTION             */
67 /*                                                                        */
68 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
69 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
70 /*                                            resulting in version 6.1    */
71 /*                                                                        */
72 /**************************************************************************/
_ux_host_class_swar_reception_callback(UX_TRANSFER * transfer_request)73 VOID  _ux_host_class_swar_reception_callback (UX_TRANSFER *transfer_request)
74 {
75 
76 UX_HOST_CLASS_SWAR               *swar;
77 UX_HOST_CLASS_SWAR_RECEPTION     *swar_reception;
78 
79     /* Get the class instance for this transfer request.  */
80     swar =  (UX_HOST_CLASS_SWAR *) transfer_request -> ux_transfer_request_class_instance;
81 
82     /* Get the pointer to the acm reception structure.  */
83     swar_reception =  swar -> ux_host_class_swar_reception;
84 
85     /* Check the state of the transfer.  If there is an error, we do not proceed with this report.  */
86     if (transfer_request -> ux_transfer_request_completion_code != UX_SUCCESS)
87     {
88 
89         /* The reception is stopped.  */
90         swar_reception -> ux_host_class_swar_reception_state =  UX_HOST_CLASS_SWAR_RECEPTION_STATE_STOPPED;
91 
92         /* We do not proceed.  */
93         return;
94 
95     }
96 
97     /* And move to the next reception buffer.  Check if we are at the end of the application buffer.  */
98     if (swar_reception -> ux_host_class_swar_reception_data_head + swar_reception -> ux_host_class_swar_reception_block_size >=
99         swar_reception -> ux_host_class_swar_reception_data_buffer + swar_reception -> ux_host_class_swar_reception_data_buffer_size)
100     {
101 
102         /* We are at the end of the buffer. Move back to the beginning if we have space available. */
103         if (swar_reception -> ux_host_class_swar_reception_data_tail ==  swar_reception -> ux_host_class_swar_reception_data_buffer)
104         {
105 
106             /* Error trap. */
107             _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_BUFFER_OVERFLOW);
108 
109             /* If trace is enabled, insert this event into the trace buffer.  */
110             UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_BUFFER_OVERFLOW, transfer_request, 0, 0, UX_TRACE_ERRORS, 0, 0)
111 
112             /* We have an overflow. We cannot continue.  Report to the application.  */
113             swar_reception -> ux_host_class_swar_reception_callback(swar, UX_BUFFER_OVERFLOW, UX_NULL, 0);
114 
115             /* And stop the transfer in progress flag.  */
116             swar_reception -> ux_host_class_swar_reception_state =  UX_HOST_CLASS_SWAR_RECEPTION_STATE_STOPPED;
117 
118             return;
119         }
120         else
121 
122             /* Program the head to be at the beginning of the application buffer.  */
123             swar_reception -> ux_host_class_swar_reception_data_head =  swar_reception -> ux_host_class_swar_reception_data_buffer;
124 
125     }
126     else
127 
128             /* Program the head to be after the current buffer.  */
129             swar_reception -> ux_host_class_swar_reception_data_head +=  swar_reception -> ux_host_class_swar_reception_block_size;
130 
131 
132     /* We need to report this transfer to the application.  */
133     swar_reception -> ux_host_class_swar_reception_callback(swar,
134                                                                     transfer_request -> ux_transfer_request_completion_code,
135                                                                     transfer_request -> ux_transfer_request_data_pointer,
136                                                                     transfer_request -> ux_transfer_request_actual_length);
137 
138     /* Arm another transfer.  */
139     _ux_host_stack_transfer_request(transfer_request);
140 
141     /* There is no status to be reported back to the stack.  */
142     return;
143 }
144 
145