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 /** ThreadX Component                                                     */
16 /**                                                                       */
17 /**   Module                                                              */
18 /**                                                                       */
19 /**************************************************************************/
20 /**************************************************************************/
21 
22 #ifndef TXM_MODULE
23 #define TXM_MODULE
24 #endif
25 
26 #ifndef TX_SOURCE_CODE
27 #define TX_SOURCE_CODE
28 #endif
29 
30 
31 /* Include necessary system files.  */
32 
33 #include "txm_module.h"
34 #include "tx_queue.h"
35 
36 
37 /* Define the global module entry pointer from the start thread of the module.
38    This structure contains the pointer to the request queue as well as the
39    pointer to the callback response queue.  */
40 
41 extern TXM_MODULE_THREAD_ENTRY_INFO    *_txm_module_entry_info;
42 
43 /**************************************************************************/
44 /*                                                                        */
45 /*  FUNCTION                                               RELEASE        */
46 /*                                                                        */
47 /*    _txm_module_callback_request_thread_entry           PORTABLE C      */
48 /*                                                           6.1.10       */
49 /*  AUTHOR                                                                */
50 /*                                                                        */
51 /*    Scott Larson, Microsoft Corporation                                 */
52 /*                                                                        */
53 /*  DESCRIPTION                                                           */
54 /*                                                                        */
55 /*    This function processes all module callback requests, transferred   */
56 /*    by the resident code via the callback queue. When the callback is   */
57 /*    complete, the response is sent back to the resident code to         */
58 /*    acknowledge it.                                                     */
59 /*                                                                        */
60 /*  INPUT                                                                 */
61 /*                                                                        */
62 /*    id                                Module thread ID                  */
63 /*                                                                        */
64 /*  OUTPUT                                                                */
65 /*                                                                        */
66 /*    None                                                                */
67 /*                                                                        */
68 /*  CALLS                                                                 */
69 /*                                                                        */
70 /*    tx_queue_receive                  Receive callback request          */
71 /*                                                                        */
72 /*  CALLED BY                                                             */
73 /*                                                                        */
74 /*    Initial thread stack frame                                          */
75 /*                                                                        */
76 /*  RELEASE HISTORY                                                       */
77 /*                                                                        */
78 /*    DATE              NAME                      DESCRIPTION             */
79 /*                                                                        */
80 /*  09-30-2020      Scott Larson            Initial Version 6.1           */
81 /*  01-31-2022      Scott Larson            Modified comments and added   */
82 /*                                            CALL_NOT_USED option,       */
83 /*                                            resulting in version 6.1.10 */
84 /*                                                                        */
85 /**************************************************************************/
_txm_module_callback_request_thread_entry(ULONG id)86 VOID _txm_module_callback_request_thread_entry(ULONG id)
87 {
88 
89 TX_QUEUE                    *request_queue;
90 TXM_MODULE_CALLBACK_MESSAGE callback_message;
91 ULONG                       activation_count;
92 VOID                        (*timer_callback)(ULONG);
93 VOID                        (*events_set_notify)(TX_EVENT_FLAGS_GROUP *);
94 VOID                        (*semaphore_put_notify)(TX_SEMAPHORE *);
95 VOID                        (*queue_send_notify)(TX_QUEUE *);
96 VOID                        (*thread_entry_exit_notify)(TX_THREAD *, UINT);
97 UINT                        status;
98 
99     /* Disable warning of parameter not used. */
100     TX_PARAMETER_NOT_USED(id);
101 
102     /* Pickup pointer to the request queue.  */
103     request_queue =  _txm_module_entry_info -> txm_module_thread_entry_info_callback_request_queue;
104 
105     /* Loop to process callback messages from the module manager.  */
106     while(1)
107     {
108 
109         /* Wait for the callback request for the module.  */
110         status =  _txe_queue_receive(request_queue, (VOID *) &callback_message, TX_WAIT_FOREVER);
111 
112         /* Check to see if a request was received.  */
113         if (status != TX_SUCCESS)
114         {
115 
116             /* This should not happen - get out of the loop.  */
117             break;
118         }
119 
120         /* Pickup the activation count in the message.  */
121         activation_count =  callback_message.txm_module_callback_message_activation_count;
122 
123         /* Loop to call the callback function the correct number of times.  */
124         while (activation_count)
125         {
126 
127             /* Decrement the activation count.  */
128             activation_count--;
129 
130             /* Now dispatch the callback function.  */
131             switch (callback_message.txm_module_callback_message_type)
132             {
133 
134             case TXM_TIMER_CALLBACK:
135 
136                 /* Setup timer callback pointer.  */
137                 timer_callback =  (void (*)(ULONG)) callback_message.txm_module_callback_message_application_function;
138 
139                 /* Call application's timer callback.  */
140                 (timer_callback)((ULONG) callback_message.txm_module_callback_message_param_1);
141 
142                 break;
143 
144             case TXM_EVENTS_SET_CALLBACK:
145 
146                 /* Setup events set callback pointer.  */
147                 events_set_notify =  (void (*)(TX_EVENT_FLAGS_GROUP *)) callback_message.txm_module_callback_message_application_function;
148 
149                 /* Call events set notify callback.  */
150                 (events_set_notify)((TX_EVENT_FLAGS_GROUP *) callback_message.txm_module_callback_message_param_1);
151 
152                 break;
153 
154             case TXM_QUEUE_SEND_CALLBACK:
155 
156                 /* Setup queue send callback pointer.  */
157                 queue_send_notify =  (void (*)(TX_QUEUE *)) callback_message.txm_module_callback_message_application_function;
158 
159                 /* Call queue send notify callback.  */
160                 (queue_send_notify)((TX_QUEUE *) callback_message.txm_module_callback_message_param_1);
161 
162                 break;
163 
164             case TXM_SEMAPHORE_PUT_CALLBACK:
165 
166                 /* Setup semaphore put callback pointer.  */
167                 semaphore_put_notify =  (void (*)(TX_SEMAPHORE *)) callback_message.txm_module_callback_message_application_function;
168 
169                 /* Call semaphore put notify callback.  */
170                 (semaphore_put_notify)((TX_SEMAPHORE *) callback_message.txm_module_callback_message_param_1);
171 
172                 break;
173 
174             case TXM_THREAD_ENTRY_EXIT_CALLBACK:
175 
176                 /* Setup thread entry/exit callback pointer.  */
177                 thread_entry_exit_notify =  (void (*)(TX_THREAD *, UINT)) callback_message.txm_module_callback_message_application_function;
178 
179                 /* Call thread entry/exit notify callback.  */
180                 (thread_entry_exit_notify)((TX_THREAD *) callback_message.txm_module_callback_message_param_1, (UINT) callback_message.txm_module_callback_message_param_2);
181 
182                 break;
183 
184             default:
185 
186 #ifdef TXM_MODULE_ENABLE_NETX
187 
188                 /* Determine if there is a NetX callback.  */
189                 if ((callback_message.txm_module_callback_message_type >= TXM_NETX_CALLBACKS_START) && (callback_message.txm_module_callback_message_type < TXM_NETX_CALLBACKS_END))
190                 {
191 
192                     /* Call the NetX module callback function.  */
193                     _txm_module_netx_callback_request(&callback_message);
194                 }
195 #endif
196 
197 #ifdef TXM_MODULE_ENABLE_NETXDUO
198 
199                 /* Determine if there is a NetX Duo callback.  */
200                 if ((callback_message.txm_module_callback_message_type >= TXM_NETXDUO_CALLBACKS_START) && (callback_message.txm_module_callback_message_type < TXM_NETXDUO_CALLBACKS_END))
201                 {
202 
203                     /* Call the NetX Duo module callback function.  */
204                     _txm_module_netxduo_callback_request(&callback_message);
205                 }
206 #endif
207 
208 #ifdef TXM_MODULE_ENABLE_FILEX
209 
210                 /* Determine if there is a FileX callback.  */
211                 if ((callback_message.txm_module_callback_message_type >= TXM_FILEX_CALLBACKS_START) && (callback_message.txm_module_callback_message_type < TXM_FILEX_CALLBACKS_END))
212                 {
213 
214                     /* Call the FileX module callback function.  */
215                     _txm_module_filex_callback_request(&callback_message);
216                 }
217 #endif
218 
219 #ifdef TXM_MODULE_ENABLE_GUIX
220 
221                 /* Determine if there is a GUIX callback.  */
222                 if ((callback_message.txm_module_callback_message_type >= TXM_GUIX_CALLBACKS_START) && (callback_message.txm_module_callback_message_type < TXM_GUIX_CALLBACKS_END))
223                 {
224 
225                     /* Call the GUIX module callback function.  */
226                     _txm_module_guix_callback_request(&callback_message);
227                 }
228 #endif
229 
230 #ifdef TXM_MODULE_ENABLE_USBX
231 
232                 /* Determine if there is a USBX callback.  */
233                 if ((callback_message.txm_module_callback_message_type >= TXM_USBX_CALLBACKS_START) && (callback_message.txm_module_callback_message_type < TXM_USBX_CALLBACKS_END))
234                 {
235 
236                     /* Call the USBX callback function.  */
237                     _txm_module_usbx_callback_request(&callback_message);
238                 }
239 #endif
240 
241                 break;
242             }
243         }
244     }
245 }
246