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 /** NetX Component                                                        */
16 /**                                                                       */
17 /**   Cloud Helper                                                        */
18 /**                                                                       */
19 /**************************************************************************/
20 /**************************************************************************/
21 
22 #define NX_CLOUD_SOURCE_CODE
23 
24 
25 /* Force error checking to be disabled in this module */
26 
27 #ifndef NX_DISABLE_ERROR_CHECKING
28 #define NX_DISABLE_ERROR_CHECKING
29 #endif
30 
31 /* Include necessary system files.  */
32 
33 #include "nx_cloud.h"
34 
35 /* Bring in externs for caller checking code.  */
36 
37 NX_CALLER_CHECKING_EXTERNS
38 
39 
40 /* Define the DHCP Internal Function.  */
41 static VOID _nx_cloud_thread_entry(ULONG cloud_ptr_value);
42 static VOID _nx_cloud_periodic_timer_entry(ULONG cloud_ptr_value);
43 
44 
45 /**************************************************************************/
46 /*                                                                        */
47 /*  FUNCTION                                               RELEASE        */
48 /*                                                                        */
49 /*    _nxe_cloud_create                                   PORTABLE C      */
50 /*                                                           6.1          */
51 /*  AUTHOR                                                                */
52 /*                                                                        */
53 /*    Yuxin Zhou, Microsoft Corporation                                   */
54 /*                                                                        */
55 /*  DESCRIPTION                                                           */
56 /*                                                                        */
57 /*    This function checks for errors in the cloud create function call.  */
58 /*                                                                        */
59 /*  INPUT                                                                 */
60 /*                                                                        */
61 /*    cloud_ptr                             Pointer to cloud control block*/
62 /*    cloud_name                            Name of this cloud instnace   */
63 /*    memory_ptr                            Pointer memory area for cloud */
64 /*    memory_size                           Size of cloud memory area     */
65 /*    priority                              Priority of helper thread     */
66 /*                                                                        */
67 /*  OUTPUT                                                                */
68 /*                                                                        */
69 /*    status                                Completion status             */
70 /*                                                                        */
71 /*  CALLS                                                                 */
72 /*                                                                        */
73 /*    _nx_cloud_create                      Actual cloud create function  */
74 /*                                                                        */
75 /*  CALLED BY                                                             */
76 /*                                                                        */
77 /*    Application Code                                                    */
78 /*                                                                        */
79 /*  RELEASE HISTORY                                                       */
80 /*                                                                        */
81 /*    DATE              NAME                      DESCRIPTION             */
82 /*                                                                        */
83 /*  09-30-2020     Yuxin Zhou               Initial Version 6.1           */
84 /*                                                                        */
85 /**************************************************************************/
_nxe_cloud_create(NX_CLOUD * cloud_ptr,const CHAR * cloud_name,VOID * memory_ptr,ULONG memory_size,UINT priority)86 UINT _nxe_cloud_create(NX_CLOUD *cloud_ptr, const CHAR *cloud_name, VOID *memory_ptr, ULONG memory_size, UINT priority)
87 {
88 
89 UINT status;
90 
91 
92     /* Check for invalid input pointers.  */
93     if ((cloud_ptr == NX_NULL) || (memory_ptr == NX_NULL))
94     {
95         return(NX_PTR_ERROR);
96     }
97 
98     /* Check for a memory size error.  */
99     if (memory_size < TX_MINIMUM_STACK)
100     {
101         return(NX_SIZE_ERROR);
102     }
103 
104     /* Check the priority specified.  */
105     if (priority >= TX_MAX_PRIORITIES)
106     {
107         return(NX_OPTION_ERROR);
108     }
109 
110     /* Check for appropriate caller.  */
111     NX_THREADS_ONLY_CALLER_CHECKING
112 
113     /* Call actual Cloud instance create function.  */
114     status = _nx_cloud_create(cloud_ptr, cloud_name, memory_ptr, memory_size, priority);
115 
116     /* Return completion status.  */
117     return(status);
118 }
119 
120 
121 /**************************************************************************/
122 /*                                                                        */
123 /*  FUNCTION                                               RELEASE        */
124 /*                                                                        */
125 /*    _nx_cloud_create                                    PORTABLE C      */
126 /*                                                           6.1          */
127 /*  AUTHOR                                                                */
128 /*                                                                        */
129 /*    Yuxin Zhou, Microsoft Corporation                                   */
130 /*                                                                        */
131 /*  DESCRIPTION                                                           */
132 /*                                                                        */
133 /*    This function creates an Internet Protocol instance, and setting up */
134 /*    all appropriate data structures.                                    */
135 /*                                                                        */
136 /*  INPUT                                                                 */
137 /*                                                                        */
138 /*    cloud_ptr                             Pointer to cloud control block*/
139 /*    cloud_name                            Name of this cloud instnace   */
140 /*    memory_ptr                            Pointer memory area for cloud */
141 /*    memory_size                           Size of cloud memory area     */
142 /*    priority                              Priority of helper thread     */
143 /*                                                                        */
144 /*  OUTPUT                                                                */
145 /*                                                                        */
146 /*    status                                Completion status             */
147 /*                                                                        */
148 /*  CALLS                                                                 */
149 /*                                                                        */
150 /*    tx_event_flags_create                 Create cloud event flags      */
151 /*    tx_mutex_create                       Create cloud protection mutex */
152 /*    tx_thread_create                      Create cloud helper thread    */
153 /*                                                                        */
154 /*  CALLED BY                                                             */
155 /*                                                                        */
156 /*    Application                                                         */
157 /*                                                                        */
158 /*  RELEASE HISTORY                                                       */
159 /*                                                                        */
160 /*    DATE              NAME                      DESCRIPTION             */
161 /*                                                                        */
162 /*  09-30-2020     Yuxin Zhou               Initial Version 6.1           */
163 /*                                                                        */
164 /**************************************************************************/
_nx_cloud_create(NX_CLOUD * cloud_ptr,const CHAR * cloud_name,VOID * memory_ptr,ULONG memory_size,UINT priority)165 UINT _nx_cloud_create(NX_CLOUD *cloud_ptr, const CHAR* cloud_name, VOID* memory_ptr, ULONG memory_size, UINT priority)
166 {
167 
168 UINT status;
169 UINT old_threshold = 0;
170 
171 
172     /* Initialize the cloud control block to zero.  */
173     memset(cloud_ptr, 0, sizeof(NX_CLOUD));
174 
175     /* Save the supplied name.  */
176     cloud_ptr -> nx_cloud_name = cloud_name;
177 
178     /* Create the mutex.  */
179     status = tx_mutex_create(&(cloud_ptr -> nx_cloud_mutex), (CHAR*)cloud_name, TX_NO_INHERIT);
180 
181     /* Check status.  */
182     if (status)
183     {
184 
185         /* Return status.  */
186         return(status);
187     }
188 
189     /* Create the event flag.  */
190     status = tx_event_flags_create(&cloud_ptr -> nx_cloud_events, (CHAR*)cloud_name);
191 
192     /* Check status.  */
193     if (status)
194     {
195 
196         /* Release resource.  */
197         tx_mutex_delete(&(cloud_ptr -> nx_cloud_mutex));
198 
199         /* Return status.  */
200         return(status);
201     }
202 
203     /* Disable preemption. */
204     tx_thread_preemption_change(tx_thread_identify(), 0, &old_threshold);
205 
206     /* Create cloud helper thread. */
207     status = tx_thread_create(&(cloud_ptr -> nx_cloud_thread), (CHAR*)cloud_name,
208                               _nx_cloud_thread_entry, (ULONG)cloud_ptr,
209                               memory_ptr, memory_size, priority, priority, 1, TX_AUTO_START);
210 
211     /* Check status.  */
212     if (status)
213     {
214 
215         /* Release resources.  */
216         tx_event_flags_delete(&(cloud_ptr -> nx_cloud_events));
217         tx_mutex_delete(&(cloud_ptr -> nx_cloud_mutex));
218 
219         /* Restore preemption . */
220         tx_thread_preemption_change(tx_thread_identify(), old_threshold, &old_threshold);
221 
222         /* Return status.  */
223         return(status);
224     }
225 
226     /* Create the periodic timer for cloud modules.  */
227     status = tx_timer_create(&(cloud_ptr -> nx_cloud_periodic_timer), (CHAR*)cloud_name,
228                              _nx_cloud_periodic_timer_entry, (ULONG)cloud_ptr,
229                              NX_IP_PERIODIC_RATE, NX_IP_PERIODIC_RATE, TX_AUTO_ACTIVATE);
230 
231     /* Check status.  */
232     if (status)
233     {
234 
235         /* Release resources.  */
236         tx_thread_delete(&(cloud_ptr -> nx_cloud_thread));
237         tx_event_flags_delete(&(cloud_ptr -> nx_cloud_events));
238         tx_mutex_delete(&(cloud_ptr -> nx_cloud_mutex));
239 
240         /* Restore preemption . */
241         tx_thread_preemption_change(tx_thread_identify(), old_threshold, &old_threshold);
242 
243         /* Return status.  */
244         return(status);
245     }
246 
247     /* Load the ID field.  */
248     cloud_ptr -> nx_cloud_id = NX_CLOUD_ID;
249 
250     /* Restore preemption . */
251     tx_thread_preemption_change(tx_thread_identify(), old_threshold, &old_threshold);
252 
253     /* Return success to the caller.  */
254     return(NX_SUCCESS);
255 }
256 
257 
258 /**************************************************************************/
259 /*                                                                        */
260 /*  FUNCTION                                               RELEASE        */
261 /*                                                                        */
262 /*    _nxe_cloud_delete                                   PORTABLE C      */
263 /*                                                           6.1          */
264 /*  AUTHOR                                                                */
265 /*                                                                        */
266 /*    Yuxin Zhou, Microsoft Corporation                                   */
267 /*                                                                        */
268 /*  DESCRIPTION                                                           */
269 /*                                                                        */
270 /*    This function checks for errors in the cloud delete function call.  */
271 /*                                                                        */
272 /*  INPUT                                                                 */
273 /*                                                                        */
274 /*    cloud_ptr                             Pointer to cloud control block*/
275 /*                                                                        */
276 /*  OUTPUT                                                                */
277 /*                                                                        */
278 /*    status                                Completion status             */
279 /*                                                                        */
280 /*  CALLS                                                                 */
281 /*                                                                        */
282 /*    _nx_cloud_delete                      Actual cloud delete function  */
283 /*                                                                        */
284 /*  CALLED BY                                                             */
285 /*                                                                        */
286 /*    Application Code                                                    */
287 /*                                                                        */
288 /*  RELEASE HISTORY                                                       */
289 /*                                                                        */
290 /*    DATE              NAME                      DESCRIPTION             */
291 /*                                                                        */
292 /*  09-30-2020     Yuxin Zhou               Initial Version 6.1           */
293 /*                                                                        */
294 /**************************************************************************/
_nxe_cloud_delete(NX_CLOUD * cloud_ptr)295 UINT _nxe_cloud_delete(NX_CLOUD *cloud_ptr)
296 {
297 
298 UINT status;
299 
300 
301     /* Check for invalid input pointers.  */
302     if ((cloud_ptr == NX_NULL) || (cloud_ptr -> nx_cloud_id != NX_CLOUD_ID))
303     {
304         return(NX_PTR_ERROR);
305     }
306 
307     /* Check for appropriate caller.  */
308     NX_THREADS_ONLY_CALLER_CHECKING
309 
310     /* Call actual Cloud instance delete function.  */
311     status = _nx_cloud_delete(cloud_ptr);
312 
313     /* Return completion status.  */
314     return(status);
315 }
316 
317 
318 /**************************************************************************/
319 /*                                                                        */
320 /*  FUNCTION                                               RELEASE        */
321 /*                                                                        */
322 /*    _nx_cloud_delete                                    PORTABLE C      */
323 /*                                                           6.1          */
324 /*  AUTHOR                                                                */
325 /*                                                                        */
326 /*    Yuxin Zhou, Microsoft Corporation                                   */
327 /*                                                                        */
328 /*  DESCRIPTION                                                           */
329 /*                                                                        */
330 /*    This function creates an Internet Protocol instance, and setting up */
331 /*    all appropriate data structures.                                    */
332 /*                                                                        */
333 /*  INPUT                                                                 */
334 /*                                                                        */
335 /*    cloud_ptr                             Pointer to cloud control block*/
336 /*    cloud_name                            Name of this cloud instnace   */
337 /*    memory_ptr                            Pointer memory area for cloud */
338 /*    memory_size                           Size of cloud memory area     */
339 /*    priority                              Priority of helper thread     */
340 /*                                                                        */
341 /*  OUTPUT                                                                */
342 /*                                                                        */
343 /*    status                                Completion status             */
344 /*                                                                        */
345 /*  CALLS                                                                 */
346 /*                                                                        */
347 /*    tx_mutex_get                          Obtain cloud protection mutex */
348 /*    tx_mutex_put                          Release cloud protection mutex*/
349 /*    tx_mutex_delete                       Delete cloud protection mutex */
350 /*    tx_timer_deactivate                   Deactivate cloud timer        */
351 /*    tx_timer_delete                       Delete cloud timer            */
352 /*    tx_event_flags_delete                 Delete cloud event flags      */
353 /*    tx_thread_terminate                   Terminate cloud helper thread */
354 /*    tx_thread_delete                      Delete cloud helper thread    */
355 /*    tx_thread_preemption_change           Change thread preemption      */
356 /*                                                                        */
357 /*  CALLED BY                                                             */
358 /*                                                                        */
359 /*    Application                                                         */
360 /*                                                                        */
361 /*  RELEASE HISTORY                                                       */
362 /*                                                                        */
363 /*    DATE              NAME                      DESCRIPTION             */
364 /*                                                                        */
365 /*  09-30-2020     Yuxin Zhou               Initial Version 6.1           */
366 /*                                                                        */
367 /**************************************************************************/
_nx_cloud_delete(NX_CLOUD * cloud_ptr)368 UINT _nx_cloud_delete(NX_CLOUD *cloud_ptr)
369 {
370 
371 UINT old_threshold = 0;
372 
373 
374     /* Get mutex protection.  */
375     tx_mutex_get(&(cloud_ptr -> nx_cloud_mutex), TX_WAIT_FOREVER);
376 
377     /* Check if all modules are deregistered.  */
378     if (cloud_ptr -> nx_cloud_modules_count)
379     {
380 
381         /* Still modules bound to this Cloud instance. They must all be deleted prior
382            to deleting the Cloud instance.  Release the mutex and return
383            an error code.  */
384         tx_mutex_put(&(cloud_ptr -> nx_cloud_mutex));
385 
386         return(NX_CLOUD_MODULE_BOUND);
387     }
388 
389     /* Disable preemption. */
390     tx_thread_preemption_change(tx_thread_identify(), 0, &old_threshold);
391 
392     /* Release mutex protection.  */
393     tx_mutex_put(&(cloud_ptr -> nx_cloud_mutex));
394 
395     /* Deactivate and delete timer. */
396     tx_timer_deactivate(&(cloud_ptr -> nx_cloud_periodic_timer));
397     tx_timer_delete(&(cloud_ptr -> nx_cloud_periodic_timer));
398 
399     /* Terminate the internal cloud thread.  */
400     tx_thread_terminate(&(cloud_ptr -> nx_cloud_thread));
401 
402     /* Delete the internal cloud protection mutex.  */
403     tx_mutex_delete(&(cloud_ptr -> nx_cloud_mutex));
404 
405     /* Delete the internal cloud event flag object.  */
406     tx_event_flags_delete(&(cloud_ptr -> nx_cloud_events));
407 
408     /* Delete the internal cloud helper thread for handling more processing intensive
409        duties.  */
410     tx_thread_delete(&(cloud_ptr -> nx_cloud_thread));
411 
412     /* Clear the ID to make it invalid.  */
413     cloud_ptr -> nx_cloud_id =  0;
414 
415     /* Restore preemption . */
416     tx_thread_preemption_change(tx_thread_identify(), old_threshold, &old_threshold);
417 
418     /* Return success to the caller.  */
419     return(NX_SUCCESS);
420 }
421 
422 
423 /**************************************************************************/
424 /*                                                                        */
425 /*  FUNCTION                                               RELEASE        */
426 /*                                                                        */
427 /*    _nxe_cloud_module_register                          PORTABLE C      */
428 /*                                                           6.1          */
429 /*  AUTHOR                                                                */
430 /*                                                                        */
431 /*    Yuxin Zhou, Microsoft Corporation                                   */
432 /*                                                                        */
433 /*  DESCRIPTION                                                           */
434 /*                                                                        */
435 /*    This function checks for errors in the cloud module register        */
436 /*    function call.                                                      */
437 /*                                                                        */
438 /*  INPUT                                                                 */
439 /*                                                                        */
440 /*    cloud_ptr                             Pointer to cloud control block*/
441 /*    module_ptr                            Pointer to cloud module       */
442 /*                                            control block               */
443 /*    module_name                           Name of Cloud module          */
444 /*    module_registered_event               Module registered event       */
445 /*    module_process                        Module processing routine     */
446 /*    module_context                        Context                       */
447 /*                                                                        */
448 /*  OUTPUT                                                                */
449 /*                                                                        */
450 /*    status                                Completion status             */
451 /*                                                                        */
452 /*  CALLS                                                                 */
453 /*                                                                        */
454 /*    _nx_cloud_module_register             Actual cloud module register  */
455 /*                                            function                    */
456 /*                                                                        */
457 /*  CALLED BY                                                             */
458 /*                                                                        */
459 /*    Application Code                                                    */
460 /*                                                                        */
461 /*  RELEASE HISTORY                                                       */
462 /*                                                                        */
463 /*    DATE              NAME                      DESCRIPTION             */
464 /*                                                                        */
465 /*  09-30-2020     Yuxin Zhou               Initial Version 6.1           */
466 /*                                                                        */
467 /**************************************************************************/
_nxe_cloud_module_register(NX_CLOUD * cloud_ptr,NX_CLOUD_MODULE * module_ptr,const CHAR * module_name,ULONG module_registered_event,VOID (* module_process)(VOID * module_context,ULONG common_events,ULONG module_own_events),VOID * module_context)468 UINT _nxe_cloud_module_register(NX_CLOUD* cloud_ptr, NX_CLOUD_MODULE *module_ptr, const CHAR *module_name, ULONG module_registered_event,
469                                VOID (*module_process)(VOID* module_context, ULONG common_events, ULONG module_own_events), VOID *module_context)
470 {
471 
472  UINT status;
473 
474 
475     /* Check for invalid input pointers.  */
476     if ((cloud_ptr == NX_NULL) || (cloud_ptr -> nx_cloud_id != NX_CLOUD_ID) ||
477         (module_ptr == NX_NULL) || (module_process == NX_NULL))
478     {
479         return(NX_PTR_ERROR);
480     }
481 
482     /* Check for invalid module.  */
483     if (module_registered_event == 0)
484     {
485         return(NX_CLOUD_MODULE_EVENT_INVALID);
486     }
487 
488     /* Check for appropriate caller.  */
489     NX_THREADS_ONLY_CALLER_CHECKING
490 
491     /* Call actual Cloud instance create function.  */
492     status = _nx_cloud_module_register(cloud_ptr, module_ptr, module_name, module_registered_event, module_process, module_context);
493 
494     /* Return completion status.  */
495     return(status);
496 }
497 
498 
499 /**************************************************************************/
500 /*                                                                        */
501 /*  FUNCTION                                               RELEASE        */
502 /*                                                                        */
503 /*    _nx_cloud_module_register                           PORTABLE C      */
504 /*                                                           6.1          */
505 /*  AUTHOR                                                                */
506 /*                                                                        */
507 /*    Yuxin Zhou, Microsoft Corporation                                   */
508 /*                                                                        */
509 /*  DESCRIPTION                                                           */
510 /*                                                                        */
511 /*    This function registers the cloud module which is running on cloud  */
512 /*    helper thread.                                                      */
513 /*                                                                        */
514 /*    Note: Registered event should be module own event | common events,  */
515 /*    common events can be null or all.                                   */
516 /*                                                                        */
517 /*  INPUT                                                                 */
518 /*                                                                        */
519 /*    cloud_ptr                             Pointer to cloud control block*/
520 /*    module_ptr                            Pointer to cloud module       */
521 /*                                            control block               */
522 /*    module_name                           Name of Cloud module          */
523 /*    module_registered_event               Module registered event       */
524 /*    module_process                        Module processing routine     */
525 /*    module_context                        Context                       */
526 /*                                                                        */
527 /*  OUTPUT                                                                */
528 /*                                                                        */
529 /*    status                                Completion status             */
530 /*                                                                        */
531 /*  CALLS                                                                 */
532 /*                                                                        */
533 /*    tx_mutex_get                          Obtain cloud protection mutex */
534 /*    tx_mutex_put                          Release cloud protection mutex*/
535 /*                                                                        */
536 /*  CALLED BY                                                             */
537 /*                                                                        */
538 /*    Application Code                                                    */
539 /*                                                                        */
540 /*  RELEASE HISTORY                                                       */
541 /*                                                                        */
542 /*    DATE              NAME                      DESCRIPTION             */
543 /*                                                                        */
544 /*  09-30-2020     Yuxin Zhou               Initial Version 6.1           */
545 /*                                                                        */
546 /**************************************************************************/
_nx_cloud_module_register(NX_CLOUD * cloud_ptr,NX_CLOUD_MODULE * module_ptr,const CHAR * module_name,ULONG module_registered_event,VOID (* module_process)(VOID * module_context,ULONG common_events,ULONG module_own_events),VOID * module_context)547 UINT _nx_cloud_module_register(NX_CLOUD* cloud_ptr, NX_CLOUD_MODULE *module_ptr, const CHAR *module_name, ULONG module_registered_event,
548                                VOID(*module_process)(VOID *module_context, ULONG common_events, ULONG module_own_events), VOID *module_context)
549 {
550 
551 NX_CLOUD_MODULE *current_module;
552 
553 
554     /* Get mutex. */
555     tx_mutex_get(&(cloud_ptr -> nx_cloud_mutex), NX_WAIT_FOREVER);
556 
557     /* Perform duplicate module detection.  */
558     for (current_module = cloud_ptr -> nx_cloud_modules_list_header; current_module; current_module = current_module -> nx_cloud_module_next)
559     {
560 
561         /* Check if the module is already registered.  */
562         if (current_module == module_ptr)
563         {
564 
565             /* Release mutex. */
566             tx_mutex_put(&(cloud_ptr -> nx_cloud_mutex));
567 
568             return(NX_CLOUD_MODULE_ALREADY_REGISTERED);
569         }
570     }
571 
572     /* Set module info.  */
573     module_ptr -> nx_cloud_module_name = module_name;
574     module_ptr -> nx_cloud_module_registered_events = module_registered_event;
575     module_ptr -> nx_cloud_module_process = module_process;
576     module_ptr -> nx_cloud_module_context = module_context;
577     module_ptr -> nx_cloud_ptr = cloud_ptr;
578 
579     /* Update the module list and count.  */
580     module_ptr -> nx_cloud_module_next = cloud_ptr -> nx_cloud_modules_list_header;
581     cloud_ptr -> nx_cloud_modules_list_header = module_ptr;
582     cloud_ptr -> nx_cloud_modules_count ++;
583 
584     /* Release mutex. */
585     tx_mutex_put(&(cloud_ptr -> nx_cloud_mutex));
586 
587     return(NX_SUCCESS);
588 }
589 
590 
591 /**************************************************************************/
592 /*                                                                        */
593 /*  FUNCTION                                               RELEASE        */
594 /*                                                                        */
595 /*    _nxe_cloud_module_deregister                        PORTABLE C      */
596 /*                                                           6.1          */
597 /*  AUTHOR                                                                */
598 /*                                                                        */
599 /*    Yuxin Zhou, Microsoft Corporation                                   */
600 /*                                                                        */
601 /*  DESCRIPTION                                                           */
602 /*                                                                        */
603 /*    This function checks for errors in the cloud module deregister      */
604 /*    function call.                                                      */
605 /*                                                                        */
606 /*  INPUT                                                                 */
607 /*                                                                        */
608 /*    cloud_ptr                             Pointer to cloud control block*/
609 /*    module_ptr                            Pointer to cloud module       */
610 /*                                            control block               */
611 /*                                                                        */
612 /*  OUTPUT                                                                */
613 /*                                                                        */
614 /*    status                                Completion status             */
615 /*                                                                        */
616 /*  CALLS                                                                 */
617 /*                                                                        */
618 /*    _nx_cloud_module_deregister           Actual cloud module deregister*/
619 /*                                            function                    */
620 /*                                                                        */
621 /*  CALLED BY                                                             */
622 /*                                                                        */
623 /*    Application Code                                                    */
624 /*                                                                        */
625 /*  RELEASE HISTORY                                                       */
626 /*                                                                        */
627 /*    DATE              NAME                      DESCRIPTION             */
628 /*                                                                        */
629 /*  09-30-2020     Yuxin Zhou               Initial Version 6.1           */
630 /*                                                                        */
631 /**************************************************************************/
_nxe_cloud_module_deregister(NX_CLOUD * cloud_ptr,NX_CLOUD_MODULE * module_ptr)632 UINT _nxe_cloud_module_deregister(NX_CLOUD* cloud_ptr, NX_CLOUD_MODULE* module_ptr)
633 {
634 
635  UINT status;
636 
637 
638     /* Check for invalid input pointers.  */
639     if ((cloud_ptr == NX_NULL) || (cloud_ptr -> nx_cloud_id != NX_CLOUD_ID) ||
640         (module_ptr == NX_NULL))
641     {
642         return(NX_PTR_ERROR);
643     }
644 
645     /* Check for appropriate caller.  */
646     NX_THREADS_ONLY_CALLER_CHECKING
647 
648     /* Call actual Cloud instance create function.  */
649     status = _nx_cloud_module_deregister(cloud_ptr, module_ptr);
650 
651     /* Return completion status.  */
652     return(status);
653 }
654 
655 
656 /**************************************************************************/
657 /*                                                                        */
658 /*  FUNCTION                                               RELEASE        */
659 /*                                                                        */
660 /*    _nx_cloud_module_deregister                         PORTABLE C      */
661 /*                                                           6.1          */
662 /*  AUTHOR                                                                */
663 /*                                                                        */
664 /*    Yuxin Zhou, Microsoft Corporation                                   */
665 /*                                                                        */
666 /*  DESCRIPTION                                                           */
667 /*                                                                        */
668 /*    This function deregisters the cloud module which is running on      */
669 /*    cloud helper thread.                                                */
670 /*                                                                        */
671 /*  INPUT                                                                 */
672 /*                                                                        */
673 /*    cloud_ptr                             Pointer to cloud control block*/
674 /*    module_ptr                            Pointer to cloud module       */
675 /*                                            control block               */
676 /*                                                                        */
677 /*  OUTPUT                                                                */
678 /*                                                                        */
679 /*    status                                Completion status             */
680 /*                                                                        */
681 /*  CALLS                                                                 */
682 /*                                                                        */
683 /*    tx_mutex_get                          Obtain cloud protection mutex */
684 /*    tx_mutex_put                          Release cloud protection mutex*/
685 /*                                                                        */
686 /*  CALLED BY                                                             */
687 /*                                                                        */
688 /*    Application Code                                                    */
689 /*                                                                        */
690 /*  RELEASE HISTORY                                                       */
691 /*                                                                        */
692 /*    DATE              NAME                      DESCRIPTION             */
693 /*                                                                        */
694 /*  09-30-2020     Yuxin Zhou               Initial Version 6.1           */
695 /*                                                                        */
696 /**************************************************************************/
_nx_cloud_module_deregister(NX_CLOUD * cloud_ptr,NX_CLOUD_MODULE * module_ptr)697 UINT _nx_cloud_module_deregister(NX_CLOUD* cloud_ptr, NX_CLOUD_MODULE* module_ptr)
698 {
699 
700 NX_CLOUD_MODULE *previous_module;
701 NX_CLOUD_MODULE *current_module;
702 UINT            found = NX_FALSE;
703 
704 
705     /* Get mutex. */
706     tx_mutex_get(&(cloud_ptr -> nx_cloud_mutex), NX_WAIT_FOREVER);
707 
708     /* Loop to find the module.  */
709     for (previous_module = NX_NULL, current_module = cloud_ptr -> nx_cloud_modules_list_header;
710          current_module; previous_module = current_module, current_module = current_module -> nx_cloud_module_next)
711     {
712 
713         /* Check the module.  */
714         if (current_module == module_ptr)
715         {
716             found = NX_TRUE;
717             break;
718         }
719     }
720 
721     /* Check if found the module.  */
722     if (found == NX_FALSE)
723     {
724 
725         /* Release mutex. */
726         tx_mutex_put(&(cloud_ptr -> nx_cloud_mutex));
727 
728         return(NX_CLOUD_MODULE_NOT_REGISTERED);
729     }
730 
731     /* Yes, found.  */
732     if (previous_module == NX_NULL)
733     {
734 
735         /* This is the header of the list. */
736         cloud_ptr -> nx_cloud_modules_list_header = current_module -> nx_cloud_module_next;
737     }
738     else
739     {
740         previous_module -> nx_cloud_module_next = current_module -> nx_cloud_module_next;
741     }
742 
743     /* Decrease the created modules count.  */
744     cloud_ptr -> nx_cloud_modules_count--;
745 
746     /* Release mutex. */
747     tx_mutex_put(&(cloud_ptr -> nx_cloud_mutex));
748 
749     return(NX_SUCCESS);
750 }
751 
752 
753 /**************************************************************************/
754 /*                                                                        */
755 /*  FUNCTION                                               RELEASE        */
756 /*                                                                        */
757 /*    _nxe_cloud_module_event_set                         PORTABLE C      */
758 /*                                                           6.1          */
759 /*  AUTHOR                                                                */
760 /*                                                                        */
761 /*    Yuxin Zhou, Microsoft Corporation                                   */
762 /*                                                                        */
763 /*  DESCRIPTION                                                           */
764 /*                                                                        */
765 /*    This function checks for errors in the cloud module event set       */
766 /*    function call.                                                      */
767 /*                                                                        */
768 /*  INPUT                                                                 */
769 /*                                                                        */
770 /*    module_ptr                            Pointer to cloud module       */
771 /*                                            control block               */
772 /*    module_own_event                      Module event                  */
773 /*                                                                        */
774 /*  OUTPUT                                                                */
775 /*                                                                        */
776 /*    status                                Completion status             */
777 /*                                                                        */
778 /*  CALLS                                                                 */
779 /*                                                                        */
780 /*    _nx_cloud_module_event_set            Actual cloud module event set */
781 /*                                            function                    */
782 /*                                                                        */
783 /*  CALLED BY                                                             */
784 /*                                                                        */
785 /*    Application Code                                                    */
786 /*                                                                        */
787 /*  RELEASE HISTORY                                                       */
788 /*                                                                        */
789 /*    DATE              NAME                      DESCRIPTION             */
790 /*                                                                        */
791 /*  09-30-2020     Yuxin Zhou               Initial Version 6.1           */
792 /*                                                                        */
793 /**************************************************************************/
_nxe_cloud_module_event_set(NX_CLOUD_MODULE * cloud_module,ULONG module_own_event)794 UINT _nxe_cloud_module_event_set(NX_CLOUD_MODULE *cloud_module, ULONG module_own_event)
795 {
796 
797 UINT status;
798 
799     /* Check for invalid input pointers.  */
800     if (cloud_module == NX_NULL)
801     {
802         return(NX_PTR_ERROR);
803     }
804 
805     /* Check for invalid module event.  */
806     if (module_own_event == 0)
807     {
808         return(NX_CLOUD_MODULE_EVENT_INVALID);
809     }
810 
811     /* Call actual cloud module event set function.  */
812     status = _nx_cloud_module_event_set(cloud_module, module_own_event);
813 
814     /* Return completion status.  */
815     return(status);
816 }
817 
818 
819 /**************************************************************************/
820 /*                                                                        */
821 /*  FUNCTION                                               RELEASE        */
822 /*                                                                        */
823 /*    _nx_cloud_module_event_set                          PORTABLE C      */
824 /*                                                           6.1          */
825 /*  AUTHOR                                                                */
826 /*                                                                        */
827 /*    Yuxin Zhou, Microsoft Corporation                                   */
828 /*                                                                        */
829 /*  DESCRIPTION                                                           */
830 /*                                                                        */
831 /*    This function sets the cloud module events that are processed in    */
832 /*    module processing routine.                                          */
833 /*                                                                        */
834 /*  INPUT                                                                 */
835 /*                                                                        */
836 /*    module_ptr                            Pointer to cloud module       */
837 /*                                            control block               */
838 /*    module_own_event                      Module event                  */
839 /*                                                                        */
840 /*  OUTPUT                                                                */
841 /*                                                                        */
842 /*    status                                Completion status             */
843 /*                                                                        */
844 /*  CALLS                                                                 */
845 /*                                                                        */
846 /*    tx_mutex_get                          Obtain protection mutex       */
847 /*    tx_mutex_put                          Release protection mutex      */
848 /*    tx_event_flags_set                    Set event flags to wakeup     */
849 /*                                            cloud helper thread         */
850 /*                                                                        */
851 /*  CALLED BY                                                             */
852 /*                                                                        */
853 /*    Application Code                                                    */
854 /*                                                                        */
855 /*  RELEASE HISTORY                                                       */
856 /*                                                                        */
857 /*    DATE              NAME                      DESCRIPTION             */
858 /*                                                                        */
859 /*  09-30-2020     Yuxin Zhou               Initial Version 6.1           */
860 /*                                                                        */
861 /**************************************************************************/
_nx_cloud_module_event_set(NX_CLOUD_MODULE * cloud_module,ULONG module_own_event)862 UINT _nx_cloud_module_event_set(NX_CLOUD_MODULE *cloud_module, ULONG module_own_event)
863 {
864 
865 TX_INTERRUPT_SAVE_AREA
866 
867 NX_CLOUD        *cloud_ptr = cloud_module -> nx_cloud_ptr;
868 ULONG           registered_event;
869 
870 
871     /* Disable interrupts.  */
872     TX_DISABLE
873 
874     /* Define the actual module event in this module that are processed in module processing routine.  */
875     cloud_module -> nx_cloud_module_own_events |= module_own_event;
876 
877     /* Set module event that are used to stimulate the cloud helper thread and call the module processing routine.  */
878     registered_event = (cloud_module -> nx_cloud_module_registered_events)&(~NX_CLOUD_COMMON_PERIODIC_EVENT);
879 
880     /* Restore interrupts.  */
881     TX_RESTORE
882 
883     tx_event_flags_set(&(cloud_ptr -> nx_cloud_events), registered_event, TX_OR);
884 
885     return(NX_SUCCESS);
886 }
887 
888 
889 /**************************************************************************/
890 /*                                                                        */
891 /*  FUNCTION                                               RELEASE        */
892 /*                                                                        */
893 /*    _nxe_cloud_module_event_clear                       PORTABLE C      */
894 /*                                                           6.2.1        */
895 /*  AUTHOR                                                                */
896 /*                                                                        */
897 /*    Yuxin Zhou, Microsoft Corporation                                   */
898 /*                                                                        */
899 /*  DESCRIPTION                                                           */
900 /*                                                                        */
901 /*    This function checks for errors in the cloud module event clear     */
902 /*    function call.                                                      */
903 /*                                                                        */
904 /*  INPUT                                                                 */
905 /*                                                                        */
906 /*    module_ptr                            Pointer to cloud module       */
907 /*                                            control block               */
908 /*    module_own_event                      Module event                  */
909 /*                                                                        */
910 /*  OUTPUT                                                                */
911 /*                                                                        */
912 /*    status                                Completion status             */
913 /*                                                                        */
914 /*  CALLS                                                                 */
915 /*                                                                        */
916 /*    _nx_cloud_module_event_clear          Actual cloud module event     */
917 /*                                            clear function              */
918 /*                                                                        */
919 /*  CALLED BY                                                             */
920 /*                                                                        */
921 /*    Application Code                                                    */
922 /*                                                                        */
923 /*  RELEASE HISTORY                                                       */
924 /*                                                                        */
925 /*    DATE              NAME                      DESCRIPTION             */
926 /*                                                                        */
927 /*  09-30-2020     Yuxin Zhou               Initial Version 6.1           */
928 /*  03-08-2023     Bo Chen                  Modified comment(s), fixed    */
929 /*                                            event clear function,       */
930 /*                                            resulting in version 6.2.1  */
931 /*                                                                        */
932 /**************************************************************************/
_nxe_cloud_module_event_clear(NX_CLOUD_MODULE * cloud_module,ULONG module_own_event)933 UINT _nxe_cloud_module_event_clear(NX_CLOUD_MODULE *cloud_module, ULONG module_own_event)
934 {
935 
936 UINT status;
937 
938     /* Check for invalid input pointers.  */
939     if (cloud_module == NX_NULL)
940     {
941         return(NX_PTR_ERROR);
942     }
943 
944     /* Check for invalid module event.  */
945     if (module_own_event == 0)
946     {
947         return(NX_CLOUD_MODULE_EVENT_INVALID);
948     }
949 
950     /* Call actual cloud module event clear function.  */
951     status = _nx_cloud_module_event_clear(cloud_module, module_own_event);
952 
953     /* Return completion status.  */
954     return(status);
955 }
956 
957 
958 /**************************************************************************/
959 /*                                                                        */
960 /*  FUNCTION                                               RELEASE        */
961 /*                                                                        */
962 /*    _nx_cloud_module_event_clear                        PORTABLE C      */
963 /*                                                           6.1          */
964 /*  AUTHOR                                                                */
965 /*                                                                        */
966 /*    Yuxin Zhou, Microsoft Corporation                                   */
967 /*                                                                        */
968 /*  DESCRIPTION                                                           */
969 /*                                                                        */
970 /*    This function clears the cloud module events that are processed in  */
971 /*    module processing routine.                                          */
972 /*                                                                        */
973 /*  INPUT                                                                 */
974 /*                                                                        */
975 /*    module_ptr                            Pointer to cloud module       */
976 /*                                            control block               */
977 /*    module_event                          Module event                  */
978 /*                                                                        */
979 /*  OUTPUT                                                                */
980 /*                                                                        */
981 /*    status                                Completion status             */
982 /*                                                                        */
983 /*  CALLS                                                                 */
984 /*                                                                        */
985 /*    tx_mutex_get                          Obtain protection mutex       */
986 /*    tx_mutex_put                          Release protection mutex      */
987 /*    tx_event_flags_set                    Set event flags to wakeup     */
988 /*                                            cloud helper thread         */
989 /*                                                                        */
990 /*  CALLED BY                                                             */
991 /*                                                                        */
992 /*    Application Code                                                    */
993 /*                                                                        */
994 /*  RELEASE HISTORY                                                       */
995 /*                                                                        */
996 /*    DATE              NAME                      DESCRIPTION             */
997 /*                                                                        */
998 /*  09-30-2020     Yuxin Zhou               Initial Version 6.1           */
999 /*                                                                        */
1000 /**************************************************************************/
_nx_cloud_module_event_clear(NX_CLOUD_MODULE * cloud_module,ULONG module_own_event)1001 UINT _nx_cloud_module_event_clear(NX_CLOUD_MODULE *cloud_module, ULONG module_own_event)
1002 {
1003 
1004 TX_INTERRUPT_SAVE_AREA
1005 
1006 NX_CLOUD        *cloud_ptr = cloud_module -> nx_cloud_ptr;
1007 ULONG           registered_event;
1008 
1009 
1010     /* Disable interrupts.  */
1011     TX_DISABLE
1012 
1013     /* Define the actual module event in this module that are processed in module processing routine.  */
1014     cloud_module -> nx_cloud_module_own_events &= ~module_own_event;
1015 
1016     /* Check if have the own events of module.  */
1017     if (cloud_module -> nx_cloud_module_own_events == 0)
1018     {
1019 
1020         /* Clear the module event that are used to stimulate the cloud helper thread and call the module processing routine.  */
1021         registered_event = (cloud_module -> nx_cloud_module_registered_events)&(~NX_CLOUD_COMMON_PERIODIC_EVENT);
1022 
1023         /* Restore interrupts.  */
1024         TX_RESTORE
1025 
1026         tx_event_flags_set(&(cloud_ptr -> nx_cloud_events), ~registered_event, TX_AND);
1027     }
1028     else
1029     {
1030 
1031         /* Restore interrupts.  */
1032         TX_RESTORE
1033     }
1034 
1035     return(NX_SUCCESS);
1036 }
1037 
1038 
1039 /**************************************************************************/
1040 /*                                                                        */
1041 /*  FUNCTION                                               RELEASE        */
1042 /*                                                                        */
1043 /*    _nx_cloud_thread_entry                              PORTABLE C      */
1044 /*                                                           6.1          */
1045 /*  AUTHOR                                                                */
1046 /*                                                                        */
1047 /*    Yuxin Zhou, Microsoft Corporation                                   */
1048 /*                                                                        */
1049 /*  DESCRIPTION                                                           */
1050 /*                                                                        */
1051 /*    This function is the entry point for cloud helper thread. The cloud */
1052 /*    helper thread is responsible for periodic all modules events.       */
1053 /*                                                                        */
1054 /*  INPUT                                                                 */
1055 /*                                                                        */
1056 /*    cloud_ptr_value                       Pointer to Cloud block        */
1057 /*                                                                        */
1058 /*  OUTPUT                                                                */
1059 /*                                                                        */
1060 /*    None                                                                */
1061 /*                                                                        */
1062 /*  CALLS                                                                 */
1063 /*                                                                        */
1064 /*    tx_mutex_get                          Get the DHCP mutex            */
1065 /*    tx_mutex_put                          Release the DHCP mutex        */
1066 /*    (nx_cloud_module_process)             Module processing             */
1067 /*                                                                        */
1068 /*  CALLED BY                                                             */
1069 /*                                                                        */
1070 /*    ThreadX Scheduler                                                   */
1071 /*                                                                        */
1072 /*  RELEASE HISTORY                                                       */
1073 /*                                                                        */
1074 /*    DATE              NAME                      DESCRIPTION             */
1075 /*                                                                        */
1076 /*  09-30-2020     Yuxin Zhou               Initial Version 6.1           */
1077 /*                                                                        */
1078 /**************************************************************************/
_nx_cloud_thread_entry(ULONG cloud_ptr_value)1079 static VOID _nx_cloud_thread_entry(ULONG cloud_ptr_value)
1080 {
1081 
1082 TX_INTERRUPT_SAVE_AREA
1083 
1084 NX_CLOUD        *cloud_ptr;
1085 NX_CLOUD_MODULE *cloud_module;
1086 ULONG           cloud_events;
1087 ULONG           module_own_events;
1088 
1089 
1090     /* Setup the Cloud pointer.  */
1091     cloud_ptr = (NX_CLOUD*)cloud_ptr_value;
1092 
1093     for (;;)
1094     {
1095 
1096         /* Wait for event.  */
1097         tx_event_flags_get(&cloud_ptr -> nx_cloud_events, NX_CLOUD_ALL_EVENTS,
1098                            TX_OR_CLEAR, &cloud_events, TX_WAIT_FOREVER);
1099 
1100         /* Get mutex. */
1101         tx_mutex_get(&cloud_ptr -> nx_cloud_mutex, NX_WAIT_FOREVER);
1102 
1103         /* Wake up the module.  */
1104         for (cloud_module = cloud_ptr -> nx_cloud_modules_list_header; cloud_module; cloud_module = cloud_module -> nx_cloud_module_next)
1105         {
1106 
1107             /* Check the cloud events.  */
1108             if (cloud_events & cloud_module -> nx_cloud_module_registered_events)
1109             {
1110 
1111                 /* Disable interrupts.  */
1112                 TX_DISABLE
1113 
1114                 /* Get the module own events.  */
1115                 module_own_events = cloud_module -> nx_cloud_module_own_events;
1116 
1117                 /* Clear the module own events.  */
1118                 cloud_module -> nx_cloud_module_own_events = 0;
1119 
1120                 /* Restore interrupts.  */
1121                 TX_RESTORE
1122 
1123                 /* Release the mutex.  */
1124                 tx_mutex_put(&(cloud_ptr -> nx_cloud_mutex));
1125 
1126                 /* Call the module processing routine.  */
1127                 cloud_module -> nx_cloud_module_process(cloud_module -> nx_cloud_module_context, cloud_events & cloud_module -> nx_cloud_module_registered_events, module_own_events);
1128 
1129                 /* Get mutex. */
1130                 tx_mutex_get(&cloud_ptr -> nx_cloud_mutex, NX_WAIT_FOREVER);
1131             }
1132         }
1133 
1134         /* Release the mutex.  */
1135         tx_mutex_put(&(cloud_ptr -> nx_cloud_mutex));
1136     }
1137 }
1138 
1139 
1140 /**************************************************************************/
1141 /*                                                                        */
1142 /*  FUNCTION                                               RELEASE        */
1143 /*                                                                        */
1144 /*    _nx_cloud_periodic_timer_entry                      PORTABLE C      */
1145 /*                                                           6.1          */
1146 /*  AUTHOR                                                                */
1147 /*                                                                        */
1148 /*    Yuxin Zhou, Microsoft Corporation                                   */
1149 /*                                                                        */
1150 /*  DESCRIPTION                                                           */
1151 /*                                                                        */
1152 /*    This function handles waking up the Cloud helper thread on a        */
1153 /*    periodic timer event.                                               */
1154 /*                                                                        */
1155 /*  INPUT                                                                 */
1156 /*                                                                        */
1157 /*    cloud_ptr_value                       Cloud address in a ULONG      */
1158 /*                                                                        */
1159 /*  OUTPUT                                                                */
1160 /*                                                                        */
1161 /*    None                                                                */
1162 /*                                                                        */
1163 /*  CALLS                                                                 */
1164 /*                                                                        */
1165 /*    tx_event_flags_set                    Set event flags to wakeup     */
1166 /*                                            cloud helper thread         */
1167 /*                                                                        */
1168 /*  CALLED BY                                                             */
1169 /*                                                                        */
1170 /*    ThreadX system timer thread                                         */
1171 /*                                                                        */
1172 /*  RELEASE HISTORY                                                       */
1173 /*                                                                        */
1174 /*    DATE              NAME                      DESCRIPTION             */
1175 /*                                                                        */
1176 /*  09-30-2020     Yuxin Zhou               Initial Version 6.1           */
1177 /*                                                                        */
1178 /**************************************************************************/
_nx_cloud_periodic_timer_entry(ULONG cloud_ptr_value)1179 static VOID _nx_cloud_periodic_timer_entry(ULONG cloud_ptr_value)
1180 {
1181 
1182 NX_CLOUD *cloud_ptr = (NX_CLOUD *)cloud_ptr_value;
1183 
1184 
1185     /* Wakeup this cloud's helper thread.  */
1186     tx_event_flags_set(&(cloud_ptr -> nx_cloud_events), NX_CLOUD_COMMON_PERIODIC_EVENT, TX_OR);
1187 }