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