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