summaryrefslogtreecommitdiff
path: root/addons/cloud/nx_cloud.c
blob: 880e8628d41cfa2ef42cd7a6dc1f0a928f34640a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
/***************************************************************************
 * Copyright (c) 2024 Microsoft Corporation
 * Copyright (c) 2025-present Eclipse ThreadX Contributors
 *
 * This program and the accompanying materials are made available under the
 * terms of the MIT License which is available at
 * https://opensource.org/licenses/MIT.
 *
 * SPDX-License-Identifier: MIT
 **************************************************************************/


/**************************************************************************/
/**************************************************************************/
/**                                                                       */
/** NetX Component                                                        */
/**                                                                       */
/**   Cloud Helper                                                        */
/**                                                                       */
/**************************************************************************/
/**************************************************************************/

#define NX_CLOUD_SOURCE_CODE


/* Force error checking to be disabled in this module */

#ifndef NX_DISABLE_ERROR_CHECKING
#define NX_DISABLE_ERROR_CHECKING
#endif

/* Include necessary system files.  */

#include "nx_cloud.h"

/* Bring in externs for caller checking code.  */

NX_CALLER_CHECKING_EXTERNS


/* Define the DHCP Internal Function.  */
static VOID _nx_cloud_thread_entry(ULONG cloud_ptr_value);
static VOID _nx_cloud_periodic_timer_entry(ULONG cloud_ptr_value);


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nxe_cloud_create                                   PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Yuxin Zhou, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function checks for errors in the cloud create function call.  */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    cloud_ptr                             Pointer to cloud control block*/
/*    cloud_name                            Name of this cloud instnace   */
/*    memory_ptr                            Pointer memory area for cloud */
/*    memory_size                           Size of cloud memory area     */
/*    priority                              Priority of helper thread     */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                                Completion status             */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _nx_cloud_create                      Actual cloud create function  */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application Code                                                    */
/*                                                                        */
/**************************************************************************/
UINT _nxe_cloud_create(NX_CLOUD *cloud_ptr, const CHAR *cloud_name, VOID *memory_ptr, ULONG memory_size, UINT priority)
{

UINT status;


    /* Check for invalid input pointers.  */
    if ((cloud_ptr == NX_NULL) || (memory_ptr == NX_NULL))
    {
        return(NX_PTR_ERROR);
    }

    /* Check for a memory size error.  */
    if (memory_size < TX_MINIMUM_STACK)
    {
        return(NX_SIZE_ERROR);
    }

    /* Check the priority specified.  */
    if (priority >= TX_MAX_PRIORITIES)
    {
        return(NX_OPTION_ERROR);
    }

    /* Check for appropriate caller.  */
    NX_THREADS_ONLY_CALLER_CHECKING

    /* Call actual Cloud instance create function.  */
    status = _nx_cloud_create(cloud_ptr, cloud_name, memory_ptr, memory_size, priority);

    /* Return completion status.  */
    return(status);
}


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_cloud_create                                    PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Yuxin Zhou, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function creates an Internet Protocol instance, and setting up */
/*    all appropriate data structures.                                    */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    cloud_ptr                             Pointer to cloud control block*/
/*    cloud_name                            Name of this cloud instnace   */
/*    memory_ptr                            Pointer memory area for cloud */
/*    memory_size                           Size of cloud memory area     */
/*    priority                              Priority of helper thread     */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                                Completion status             */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    tx_event_flags_create                 Create cloud event flags      */
/*    tx_mutex_create                       Create cloud protection mutex */
/*    tx_thread_create                      Create cloud helper thread    */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application                                                         */
/*                                                                        */
/**************************************************************************/
UINT _nx_cloud_create(NX_CLOUD *cloud_ptr, const CHAR* cloud_name, VOID* memory_ptr, ULONG memory_size, UINT priority)
{

UINT status;
UINT old_threshold = 0;


    /* Initialize the cloud control block to zero.  */
    memset(cloud_ptr, 0, sizeof(NX_CLOUD));

    /* Save the supplied name.  */
    cloud_ptr -> nx_cloud_name = cloud_name;

    /* Create the mutex.  */
    status = tx_mutex_create(&(cloud_ptr -> nx_cloud_mutex), (CHAR*)cloud_name, TX_NO_INHERIT);

    /* Check status.  */
    if (status)
    {

        /* Return status.  */
        return(status);
    }

    /* Create the event flag.  */
    status = tx_event_flags_create(&cloud_ptr -> nx_cloud_events, (CHAR*)cloud_name);
    
    /* Check status.  */
    if (status)
    {
        
        /* Release resource.  */
        tx_mutex_delete(&(cloud_ptr -> nx_cloud_mutex));

        /* Return status.  */
        return(status);
    }

    /* Disable preemption. */
    tx_thread_preemption_change(tx_thread_identify(), 0, &old_threshold);

    /* Create cloud helper thread. */
    status = tx_thread_create(&(cloud_ptr -> nx_cloud_thread), (CHAR*)cloud_name,
                              _nx_cloud_thread_entry, (ULONG)cloud_ptr,
                              memory_ptr, memory_size, priority, priority, 1, TX_AUTO_START);
        
    /* Check status.  */
    if (status)
    {
        
        /* Release resources.  */
        tx_event_flags_delete(&(cloud_ptr -> nx_cloud_events));
        tx_mutex_delete(&(cloud_ptr -> nx_cloud_mutex));

        /* Restore preemption . */
        tx_thread_preemption_change(tx_thread_identify(), old_threshold, &old_threshold);

        /* Return status.  */
        return(status);
    }
    
    /* Create the periodic timer for cloud modules.  */
    status = tx_timer_create(&(cloud_ptr -> nx_cloud_periodic_timer), (CHAR*)cloud_name,
                             _nx_cloud_periodic_timer_entry, (ULONG)cloud_ptr,
                             NX_IP_PERIODIC_RATE, NX_IP_PERIODIC_RATE, TX_AUTO_ACTIVATE);

    /* Check status.  */
    if (status)
    {
        
        /* Release resources.  */
        tx_thread_delete(&(cloud_ptr -> nx_cloud_thread));
        tx_event_flags_delete(&(cloud_ptr -> nx_cloud_events));
        tx_mutex_delete(&(cloud_ptr -> nx_cloud_mutex));

        /* Restore preemption . */
        tx_thread_preemption_change(tx_thread_identify(), old_threshold, &old_threshold);

        /* Return status.  */
        return(status);
    }

    /* Load the ID field.  */
    cloud_ptr -> nx_cloud_id = NX_CLOUD_ID;

    /* Restore preemption . */
    tx_thread_preemption_change(tx_thread_identify(), old_threshold, &old_threshold);

    /* Return success to the caller.  */
    return(NX_SUCCESS);
}


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nxe_cloud_delete                                   PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Yuxin Zhou, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function checks for errors in the cloud delete function call.  */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    cloud_ptr                             Pointer to cloud control block*/
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                                Completion status             */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _nx_cloud_delete                      Actual cloud delete function  */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application Code                                                    */
/*                                                                        */
/**************************************************************************/
UINT _nxe_cloud_delete(NX_CLOUD *cloud_ptr)
{

UINT status;


    /* Check for invalid input pointers.  */
    if ((cloud_ptr == NX_NULL) || (cloud_ptr -> nx_cloud_id != NX_CLOUD_ID))
    {
        return(NX_PTR_ERROR);
    }

    /* Check for appropriate caller.  */
    NX_THREADS_ONLY_CALLER_CHECKING

    /* Call actual Cloud instance delete function.  */
    status = _nx_cloud_delete(cloud_ptr);

    /* Return completion status.  */
    return(status);
}


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_cloud_delete                                    PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Yuxin Zhou, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function creates an Internet Protocol instance, and setting up */
/*    all appropriate data structures.                                    */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    cloud_ptr                             Pointer to cloud control block*/
/*    cloud_name                            Name of this cloud instnace   */
/*    memory_ptr                            Pointer memory area for cloud */
/*    memory_size                           Size of cloud memory area     */
/*    priority                              Priority of helper thread     */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                                Completion status             */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    tx_mutex_get                          Obtain cloud protection mutex */
/*    tx_mutex_put                          Release cloud protection mutex*/
/*    tx_mutex_delete                       Delete cloud protection mutex */
/*    tx_timer_deactivate                   Deactivate cloud timer        */
/*    tx_timer_delete                       Delete cloud timer            */
/*    tx_event_flags_delete                 Delete cloud event flags      */
/*    tx_thread_terminate                   Terminate cloud helper thread */
/*    tx_thread_delete                      Delete cloud helper thread    */
/*    tx_thread_preemption_change           Change thread preemption      */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application                                                         */
/*                                                                        */
/**************************************************************************/
UINT _nx_cloud_delete(NX_CLOUD *cloud_ptr)
{

UINT old_threshold = 0;


    /* Get mutex protection.  */
    tx_mutex_get(&(cloud_ptr -> nx_cloud_mutex), TX_WAIT_FOREVER);

    /* Check if all modules are deregistered.  */
    if (cloud_ptr -> nx_cloud_modules_count)
    {
        
        /* Still modules bound to this Cloud instance. They must all be deleted prior
           to deleting the Cloud instance.  Release the mutex and return
           an error code.  */
        tx_mutex_put(&(cloud_ptr -> nx_cloud_mutex));

        return(NX_CLOUD_MODULE_BOUND);
    }

    /* Disable preemption. */
    tx_thread_preemption_change(tx_thread_identify(), 0, &old_threshold);

    /* Release mutex protection.  */
    tx_mutex_put(&(cloud_ptr -> nx_cloud_mutex));
    
    /* Deactivate and delete timer. */
    tx_timer_deactivate(&(cloud_ptr -> nx_cloud_periodic_timer));
    tx_timer_delete(&(cloud_ptr -> nx_cloud_periodic_timer));

    /* Terminate the internal cloud thread.  */
    tx_thread_terminate(&(cloud_ptr -> nx_cloud_thread));
    
    /* Delete the internal cloud protection mutex.  */
    tx_mutex_delete(&(cloud_ptr -> nx_cloud_mutex));

    /* Delete the internal cloud event flag object.  */
    tx_event_flags_delete(&(cloud_ptr -> nx_cloud_events));

    /* Delete the internal cloud helper thread for handling more processing intensive
       duties.  */
    tx_thread_delete(&(cloud_ptr -> nx_cloud_thread));
    
    /* Clear the ID to make it invalid.  */
    cloud_ptr -> nx_cloud_id =  0;

    /* Restore preemption . */
    tx_thread_preemption_change(tx_thread_identify(), old_threshold, &old_threshold);

    /* Return success to the caller.  */
    return(NX_SUCCESS);
}


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nxe_cloud_module_register                          PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Yuxin Zhou, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function checks for errors in the cloud module register        */
/*    function call.                                                      */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    cloud_ptr                             Pointer to cloud control block*/
/*    module_ptr                            Pointer to cloud module       */
/*                                            control block               */
/*    module_name                           Name of Cloud module          */
/*    module_registered_event               Module registered event       */
/*    module_process                        Module processing routine     */
/*    module_context                        Context                       */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                                Completion status             */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _nx_cloud_module_register             Actual cloud module register  */
/*                                            function                    */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application Code                                                    */
/*                                                                        */
/**************************************************************************/
UINT _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)
{

 UINT status;


    /* Check for invalid input pointers.  */
    if ((cloud_ptr == NX_NULL) || (cloud_ptr -> nx_cloud_id != NX_CLOUD_ID) || 
        (module_ptr == NX_NULL) || (module_process == NX_NULL))
    {
        return(NX_PTR_ERROR);
    }

    /* Check for invalid module.  */
    if (module_registered_event == 0)
    {
        return(NX_CLOUD_MODULE_EVENT_INVALID);
    }

    /* Check for appropriate caller.  */
    NX_THREADS_ONLY_CALLER_CHECKING

    /* Call actual Cloud instance create function.  */
    status = _nx_cloud_module_register(cloud_ptr, module_ptr, module_name, module_registered_event, module_process, module_context);

    /* Return completion status.  */
    return(status);
}


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_cloud_module_register                           PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Yuxin Zhou, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function registers the cloud module which is running on cloud  */
/*    helper thread.                                                      */
/*                                                                        */
/*    Note: Registered event should be module own event | common events,  */
/*    common events can be null or all.                                   */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    cloud_ptr                             Pointer to cloud control block*/
/*    module_ptr                            Pointer to cloud module       */
/*                                            control block               */
/*    module_name                           Name of Cloud module          */
/*    module_registered_event               Module registered event       */
/*    module_process                        Module processing routine     */
/*    module_context                        Context                       */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                                Completion status             */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    tx_mutex_get                          Obtain cloud protection mutex */
/*    tx_mutex_put                          Release cloud protection mutex*/
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application Code                                                    */
/*                                                                        */
/**************************************************************************/
UINT _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)
{
    
NX_CLOUD_MODULE *current_module;


    /* Get mutex. */
    tx_mutex_get(&(cloud_ptr -> nx_cloud_mutex), NX_WAIT_FOREVER);
            
    /* Perform duplicate module detection.  */
    for (current_module = cloud_ptr -> nx_cloud_modules_list_header; current_module; current_module = current_module -> nx_cloud_module_next)
    {
        
        /* Check if the module is already registered.  */
        if (current_module == module_ptr)
        {

            /* Release mutex. */
            tx_mutex_put(&(cloud_ptr -> nx_cloud_mutex));

            return(NX_CLOUD_MODULE_ALREADY_REGISTERED);
        }
    }

    /* Set module info.  */
    module_ptr -> nx_cloud_module_name = module_name;
    module_ptr -> nx_cloud_module_registered_events = module_registered_event;
    module_ptr -> nx_cloud_module_process = module_process;
    module_ptr -> nx_cloud_module_context = module_context;
    module_ptr -> nx_cloud_ptr = cloud_ptr;

    /* Update the module list and count.  */
    module_ptr -> nx_cloud_module_next = cloud_ptr -> nx_cloud_modules_list_header;
    cloud_ptr -> nx_cloud_modules_list_header = module_ptr;
    cloud_ptr -> nx_cloud_modules_count ++;

    /* Release mutex. */
    tx_mutex_put(&(cloud_ptr -> nx_cloud_mutex));

    return(NX_SUCCESS);
}


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nxe_cloud_module_deregister                        PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Yuxin Zhou, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function checks for errors in the cloud module deregister      */
/*    function call.                                                      */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    cloud_ptr                             Pointer to cloud control block*/
/*    module_ptr                            Pointer to cloud module       */
/*                                            control block               */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                                Completion status             */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _nx_cloud_module_deregister           Actual cloud module deregister*/
/*                                            function                    */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application Code                                                    */
/*                                                                        */
/**************************************************************************/
UINT _nxe_cloud_module_deregister(NX_CLOUD* cloud_ptr, NX_CLOUD_MODULE* module_ptr)
{

 UINT status;


    /* Check for invalid input pointers.  */
    if ((cloud_ptr == NX_NULL) || (cloud_ptr -> nx_cloud_id != NX_CLOUD_ID) ||
        (module_ptr == NX_NULL))
    {
        return(NX_PTR_ERROR);
    }

    /* Check for appropriate caller.  */
    NX_THREADS_ONLY_CALLER_CHECKING

    /* Call actual Cloud instance create function.  */
    status = _nx_cloud_module_deregister(cloud_ptr, module_ptr);

    /* Return completion status.  */
    return(status);
}


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_cloud_module_deregister                         PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Yuxin Zhou, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function deregisters the cloud module which is running on      */
/*    cloud helper thread.                                                */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    cloud_ptr                             Pointer to cloud control block*/
/*    module_ptr                            Pointer to cloud module       */
/*                                            control block               */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                                Completion status             */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    tx_mutex_get                          Obtain cloud protection mutex */
/*    tx_mutex_put                          Release cloud protection mutex*/
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application Code                                                    */
/*                                                                        */
/**************************************************************************/
UINT _nx_cloud_module_deregister(NX_CLOUD* cloud_ptr, NX_CLOUD_MODULE* module_ptr)
{

NX_CLOUD_MODULE *previous_module;
NX_CLOUD_MODULE *current_module;
UINT            found = NX_FALSE;


    /* Get mutex. */
    tx_mutex_get(&(cloud_ptr -> nx_cloud_mutex), NX_WAIT_FOREVER);

    /* Loop to find the module.  */
    for (previous_module = NX_NULL, current_module = cloud_ptr -> nx_cloud_modules_list_header;
         current_module; previous_module = current_module, current_module = current_module -> nx_cloud_module_next)
    {

        /* Check the module.  */
        if (current_module == module_ptr)
        {
            found = NX_TRUE;
            break;
        }
    }

    /* Check if found the module.  */
    if (found == NX_FALSE)
    {

        /* Release mutex. */
        tx_mutex_put(&(cloud_ptr -> nx_cloud_mutex));

        return(NX_CLOUD_MODULE_NOT_REGISTERED);
    }

    /* Yes, found.  */
    if (previous_module == NX_NULL)
    {

        /* This is the header of the list. */
        cloud_ptr -> nx_cloud_modules_list_header = current_module -> nx_cloud_module_next;
    }
    else
    {
        previous_module -> nx_cloud_module_next = current_module -> nx_cloud_module_next;
    }
    
    /* Decrease the created modules count.  */
    cloud_ptr -> nx_cloud_modules_count--;

    /* Release mutex. */
    tx_mutex_put(&(cloud_ptr -> nx_cloud_mutex));

    return(NX_SUCCESS);
}


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nxe_cloud_module_event_set                         PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Yuxin Zhou, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function checks for errors in the cloud module event set       */
/*    function call.                                                      */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    module_ptr                            Pointer to cloud module       */
/*                                            control block               */
/*    module_own_event                      Module event                  */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                                Completion status             */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _nx_cloud_module_event_set            Actual cloud module event set */
/*                                            function                    */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application Code                                                    */
/*                                                                        */
/**************************************************************************/
UINT _nxe_cloud_module_event_set(NX_CLOUD_MODULE *cloud_module, ULONG module_own_event)
{
  
UINT status;

    /* Check for invalid input pointers.  */
    if (cloud_module == NX_NULL)
    {
        return(NX_PTR_ERROR);
    }

    /* Check for invalid module event.  */
    if (module_own_event == 0)
    {
        return(NX_CLOUD_MODULE_EVENT_INVALID);
    }

    /* Call actual cloud module event set function.  */
    status = _nx_cloud_module_event_set(cloud_module, module_own_event);

    /* Return completion status.  */
    return(status);
}


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_cloud_module_event_set                          PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Yuxin Zhou, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function sets the cloud module events that are processed in    */
/*    module processing routine.                                          */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    module_ptr                            Pointer to cloud module       */
/*                                            control block               */
/*    module_own_event                      Module event                  */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                                Completion status             */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    tx_mutex_get                          Obtain protection mutex       */
/*    tx_mutex_put                          Release protection mutex      */
/*    tx_event_flags_set                    Set event flags to wakeup     */
/*                                            cloud helper thread         */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application Code                                                    */
/*                                                                        */
/**************************************************************************/
UINT _nx_cloud_module_event_set(NX_CLOUD_MODULE *cloud_module, ULONG module_own_event)
{

TX_INTERRUPT_SAVE_AREA

NX_CLOUD        *cloud_ptr = cloud_module -> nx_cloud_ptr;
ULONG           registered_event;


    /* Disable interrupts.  */
    TX_DISABLE

    /* Define the actual module event in this module that are processed in module processing routine.  */
    cloud_module -> nx_cloud_module_own_events |= module_own_event;

    /* Set module event that are used to stimulate the cloud helper thread and call the module processing routine.  */
    registered_event = (cloud_module -> nx_cloud_module_registered_events)&(~NX_CLOUD_COMMON_PERIODIC_EVENT);

    /* Restore interrupts.  */
    TX_RESTORE

    tx_event_flags_set(&(cloud_ptr -> nx_cloud_events), registered_event, TX_OR);
    
    return(NX_SUCCESS);
}


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nxe_cloud_module_event_clear                       PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Yuxin Zhou, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function checks for errors in the cloud module event clear     */
/*    function call.                                                      */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    module_ptr                            Pointer to cloud module       */
/*                                            control block               */
/*    module_own_event                      Module event                  */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                                Completion status             */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _nx_cloud_module_event_clear          Actual cloud module event     */
/*                                            clear function              */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application Code                                                    */
/*                                                                        */
/**************************************************************************/
UINT _nxe_cloud_module_event_clear(NX_CLOUD_MODULE *cloud_module, ULONG module_own_event)
{
  
UINT status;

    /* Check for invalid input pointers.  */
    if (cloud_module == NX_NULL)
    {
        return(NX_PTR_ERROR);
    }

    /* Check for invalid module event.  */
    if (module_own_event == 0)
    {
        return(NX_CLOUD_MODULE_EVENT_INVALID);
    }

    /* Call actual cloud module event clear function.  */
    status = _nx_cloud_module_event_clear(cloud_module, module_own_event);

    /* Return completion status.  */
    return(status);
}


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_cloud_module_event_clear                        PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Yuxin Zhou, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function clears the cloud module events that are processed in  */
/*    module processing routine.                                          */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    module_ptr                            Pointer to cloud module       */
/*                                            control block               */
/*    module_event                          Module event                  */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                                Completion status             */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    tx_mutex_get                          Obtain protection mutex       */
/*    tx_mutex_put                          Release protection mutex      */
/*    tx_event_flags_set                    Set event flags to wakeup     */
/*                                            cloud helper thread         */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application Code                                                    */
/*                                                                        */
/**************************************************************************/
UINT _nx_cloud_module_event_clear(NX_CLOUD_MODULE *cloud_module, ULONG module_own_event)
{

TX_INTERRUPT_SAVE_AREA

NX_CLOUD        *cloud_ptr = cloud_module -> nx_cloud_ptr;
ULONG           registered_event;


    /* Disable interrupts.  */
    TX_DISABLE

    /* Define the actual module event in this module that are processed in module processing routine.  */
    cloud_module -> nx_cloud_module_own_events &= ~module_own_event;

    /* Check if have the own events of module.  */
    if (cloud_module -> nx_cloud_module_own_events == 0)
    {

        /* Clear the module event that are used to stimulate the cloud helper thread and call the module processing routine.  */
        registered_event = (cloud_module -> nx_cloud_module_registered_events)&(~NX_CLOUD_COMMON_PERIODIC_EVENT);

        /* Restore interrupts.  */
        TX_RESTORE

        tx_event_flags_set(&(cloud_ptr -> nx_cloud_events), ~registered_event, TX_AND);
    }
    else
    {

        /* Restore interrupts.  */
        TX_RESTORE
    }
    
    return(NX_SUCCESS);
}


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_cloud_thread_entry                              PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Yuxin Zhou, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function is the entry point for cloud helper thread. The cloud */
/*    helper thread is responsible for periodic all modules events.       */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    cloud_ptr_value                       Pointer to Cloud block        */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    tx_mutex_get                          Get the DHCP mutex            */
/*    tx_mutex_put                          Release the DHCP mutex        */
/*    (nx_cloud_module_process)             Module processing             */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    ThreadX Scheduler                                                   */
/*                                                                        */
/**************************************************************************/
static VOID _nx_cloud_thread_entry(ULONG cloud_ptr_value)
{

TX_INTERRUPT_SAVE_AREA

NX_CLOUD        *cloud_ptr;
NX_CLOUD_MODULE *cloud_module;
ULONG           cloud_events;
ULONG           module_own_events;


    /* Setup the Cloud pointer.  */
    cloud_ptr = (NX_CLOUD*)cloud_ptr_value;

    for (;;)
    {

        /* Wait for event.  */
        tx_event_flags_get(&cloud_ptr -> nx_cloud_events, NX_CLOUD_ALL_EVENTS,
                           TX_OR_CLEAR, &cloud_events, TX_WAIT_FOREVER);

        /* Get mutex. */
        tx_mutex_get(&cloud_ptr -> nx_cloud_mutex, NX_WAIT_FOREVER);

        /* Wake up the module.  */
        for (cloud_module = cloud_ptr -> nx_cloud_modules_list_header; cloud_module; cloud_module = cloud_module -> nx_cloud_module_next)
        {

            /* Check the cloud events.  */
            if (cloud_events & cloud_module -> nx_cloud_module_registered_events)
            {

                /* Disable interrupts.  */
                TX_DISABLE

                /* Get the module own events.  */
                module_own_events = cloud_module -> nx_cloud_module_own_events;

                /* Clear the module own events.  */
                cloud_module -> nx_cloud_module_own_events = 0;

                /* Restore interrupts.  */
                TX_RESTORE

                /* Release the mutex.  */
                tx_mutex_put(&(cloud_ptr -> nx_cloud_mutex));

                /* Call the module processing routine.  */
                cloud_module -> nx_cloud_module_process(cloud_module -> nx_cloud_module_context, cloud_events & cloud_module -> nx_cloud_module_registered_events, module_own_events);

                /* Get mutex. */
                tx_mutex_get(&cloud_ptr -> nx_cloud_mutex, NX_WAIT_FOREVER);
            }
        }

        /* Release the mutex.  */
        tx_mutex_put(&(cloud_ptr -> nx_cloud_mutex));
    }
}


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_cloud_periodic_timer_entry                      PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Yuxin Zhou, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function handles waking up the Cloud helper thread on a        */
/*    periodic timer event.                                               */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    cloud_ptr_value                       Cloud address in a ULONG      */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    tx_event_flags_set                    Set event flags to wakeup     */
/*                                            cloud helper thread         */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    ThreadX system timer thread                                         */
/*                                                                        */
/**************************************************************************/
static VOID _nx_cloud_periodic_timer_entry(ULONG cloud_ptr_value)
{

NX_CLOUD *cloud_ptr = (NX_CLOUD *)cloud_ptr_value;


    /* Wakeup this cloud's helper thread.  */
    tx_event_flags_set(&(cloud_ptr -> nx_cloud_events), NX_CLOUD_COMMON_PERIODIC_EVENT, TX_OR);
}