1 #include    "tx_api.h"
2 #include    "nx_api.h"
3 
4 extern void test_control_return(UINT);
5 
6 #ifdef __PRODUCT_NETXDUO__
7 #include    "nx_cloud.h"
8 
9 #define DEMO_STACK_SIZE     4096
10 
11 static TX_THREAD            test_thread;
12 static NX_CLOUD             cloud;
13 static UCHAR                cloud_stack[2048];
14 
15 typedef struct NX_CLOUD_APP_STRUCT
16 {
17 
18 NX_CLOUD_MODULE             nx_cloud_module;
19 ULONG                       nx_cloud_module_event_periodic;
20 ULONG                       nx_cloud_module_event_1;
21 ULONG                       nx_cloud_module_event_2;
22 ULONG                       nx_cloud_module_event_3;
23 ULONG                       nx_cloud_module_event_4;
24 } NX_CLOUD_APP;
25 
26 static NX_CLOUD_APP         cloud_module[3];
27 
28 typedef struct MODULE_EVENT_STRUCT
29 {
30 ULONG                       nx_cloud_module_event_1;
31 ULONG                       nx_cloud_module_event_2;
32 ULONG                       nx_cloud_module_event_3;
33 ULONG                       nx_cloud_module_event_4;
34 } MODULE_EVENT;
35 
36 static MODULE_EVENT         module_event[3];
37 
38 /* Define module event.  */
39 #define NX_CLOUD_MODULE_1_EVENT 0x10000000u
40 #define NX_CLOUD_MODULE_2_EVENT 0x20000000u
41 #define NX_CLOUD_MODULE_3_EVENT 0x40000000u
42 
43 /* Define module own events.  */
44 #define NX_CLOUD_MODULE_EVENT_1 0x00000001
45 #define NX_CLOUD_MODULE_EVENT_2 0x00000002
46 #define NX_CLOUD_MODULE_EVENT_3 0x00000004
47 #define NX_CLOUD_MODULE_EVENT_4 0x00000008
48 
49 static void test_entry(ULONG thread_input);
50 static void cloud_module_event_process(VOID *module_ptr, ULONG common_events, ULONG module_own_events);
51 
52 
53 #ifdef CTEST
test_application_define(void * first_unused_memory)54 VOID test_application_define(void *first_unused_memory)
55 #else
56 void    netx_cloud_module_event_test_application_define(void *first_unused_memory)
57 #endif
58 {
59 
60 CHAR    *pointer;
61 
62 
63     /* Setup the working pointer.  */
64     pointer =  (CHAR *) first_unused_memory;
65 
66     /* Create a helper thread for the server. */
67     tx_thread_create(&test_thread, "Test thread", test_entry, 0,
68                      pointer, DEMO_STACK_SIZE,
69                      4, 4, TX_NO_TIME_SLICE, TX_AUTO_START);
70 
71     pointer =  pointer + DEMO_STACK_SIZE;
72 
73     /* Initialize the NetX system.  */
74     nx_system_initialize();
75 }
76 
test_entry(ULONG thread_input)77 void test_entry(ULONG thread_input)
78 {
79 
80 UINT            status;
81 UINT            i;
82 UINT            j;
83 UINT            module_id;
84 ULONG           module_own_events;
85 ULONG           start_time;
86 ULONG           periodic_count;
87 
88 
89     /* Print out test information banner.  */
90     printf("NetX Test:   Cloud Module Event Test...................................");
91 
92     /* Create cloud.  */
93     status = nx_cloud_create(&cloud, "Cloud", cloud_stack, 2048, 3);
94 
95     /* Check status.  */
96     if (status)
97     {
98         printf("ERROR!\n");
99         test_control_return(1);
100     }
101 
102     /* Register cloud module 1.  */
103     status = nx_cloud_module_register(&cloud, &cloud_module[0].nx_cloud_module, "Module 1", (NX_CLOUD_COMMON_PERIODIC_EVENT | NX_CLOUD_MODULE_1_EVENT), cloud_module_event_process, (void *)(&cloud_module[0]));
104 
105     /* Check status.  */
106     if (status)
107     {
108         printf("ERROR!\n");
109         test_control_return(1);
110     }
111 
112     /* Register cloud module 2.  */
113     status = nx_cloud_module_register(&cloud, &cloud_module[1].nx_cloud_module, "Module 2", NX_CLOUD_MODULE_1_EVENT, cloud_module_event_process, (void *)(&cloud_module[1]));
114 
115     /* Check status.  */
116     if (status)
117     {
118         printf("ERROR!\n");
119         test_control_return(1);
120     }
121 
122     /* Register cloud module 3.  */
123     status = nx_cloud_module_register(&cloud, &cloud_module[2].nx_cloud_module, "Module 3", (NX_CLOUD_COMMON_PERIODIC_EVENT|NX_CLOUD_MODULE_1_EVENT), cloud_module_event_process, (void *)(&cloud_module[2]));
124 
125     /* Check status.  */
126     if (status)
127     {
128         printf("ERROR!\n");
129         test_control_return(1);
130     }
131 
132     /* Set the time.  */
133     start_time = tx_time_get();
134 
135     /* Loop to set event.  */
136     for (i = 0; i < 100; i ++)
137     {
138         module_id = rand() % 3;
139         module_own_events = 0;
140         if(rand() % 2)
141         {
142             module_own_events |= NX_CLOUD_MODULE_EVENT_1;
143             module_event[module_id].nx_cloud_module_event_1 ++;
144         }
145         if(rand() % 2)
146         {
147             module_own_events |= NX_CLOUD_MODULE_EVENT_2;
148             module_event[module_id].nx_cloud_module_event_2 ++;
149         }
150         if(rand() % 2)
151         {
152             module_own_events |= NX_CLOUD_MODULE_EVENT_3;
153             module_event[module_id].nx_cloud_module_event_3 ++;
154         }
155         if(rand() % 2)
156         {
157             module_own_events |= NX_CLOUD_MODULE_EVENT_4;
158             module_event[module_id].nx_cloud_module_event_4 ++;
159         }
160         nx_cloud_module_event_set(&cloud_module[module_id].nx_cloud_module, module_own_events);
161 
162         /* Check modules event.  */
163         for (j = 0; j < 3; j++)
164         {
165             if ((cloud_module[j].nx_cloud_module_event_1 != module_event[j].nx_cloud_module_event_1) ||
166                 (cloud_module[j].nx_cloud_module_event_2 != module_event[j].nx_cloud_module_event_2) ||
167                 (cloud_module[j].nx_cloud_module_event_3 != module_event[j].nx_cloud_module_event_3) ||
168                 (cloud_module[j].nx_cloud_module_event_4 != module_event[j].nx_cloud_module_event_4))
169             {
170                 printf("ERROR!\n");
171                 test_control_return(1);
172             }
173         }
174 
175         tx_thread_sleep(10);
176     }
177 
178     /* Get periodic count.  */
179     periodic_count = (tx_time_get() - start_time) / NX_IP_PERIODIC_RATE;
180 
181     /* Check periodic events.  */
182     if ((cloud_module[0].nx_cloud_module_event_periodic != periodic_count) ||
183         (cloud_module[1].nx_cloud_module_event_periodic != 0) ||
184         (cloud_module[2].nx_cloud_module_event_periodic != periodic_count))
185     {
186         printf("ERROR!\n");
187         test_control_return(1);
188     }
189 
190     /* Test module event clear.  */
191     nx_cloud_module_event_clear(&cloud_module[0].nx_cloud_module, NX_CLOUD_MODULE_EVENT_1);
192     if (cloud_module[0].nx_cloud_module_event_1 != module_event[0].nx_cloud_module_event_1)
193     {
194         printf("ERROR!\n");
195         test_control_return(1);
196     }
197 
198     /* Deregister cloud module 3.  */
199     status = nx_cloud_module_deregister(&cloud, &cloud_module[2].nx_cloud_module);
200 
201     /* Check status.  */
202     if (status)
203     {
204         printf("ERROR!\n");
205         test_control_return(1);
206     }
207 
208     /* Deregister cloud module 2.  */
209     status = nx_cloud_module_deregister(&cloud, &cloud_module[1].nx_cloud_module);
210 
211     /* Check status.  */
212     if (status)
213     {
214         printf("ERROR!\n");
215         test_control_return(1);
216     }
217 
218     /* Deregister cloud module 1.  */
219     status = nx_cloud_module_deregister(&cloud, &cloud_module[0].nx_cloud_module);
220 
221     /* Check status.  */
222     if (status)
223     {
224         printf("ERROR!\n");
225         test_control_return(1);
226     }
227 
228     /* Delete cloud.  */
229     status = nx_cloud_delete(&cloud);
230 
231     /* Check status.  */
232     if (status)
233     {
234         printf("ERROR!\n");
235         test_control_return(1);
236     }
237 
238     printf("SUCCESS!\n");
239     test_control_return(0);
240 }
241 
cloud_module_event_process(VOID * module_ptr,ULONG common_events,ULONG module_own_events)242 static void cloud_module_event_process(VOID *module_ptr, ULONG common_events, ULONG module_own_events)
243 {
244 
245 NX_CLOUD_APP *cloud_app = (NX_CLOUD_APP*) module_ptr;
246 
247     /* Process common_events.  */
248     if (common_events & NX_CLOUD_COMMON_PERIODIC_EVENT)
249     {
250         cloud_app -> nx_cloud_module_event_periodic++;
251     }
252 
253     /* Process module_own_events.  */
254     if (module_own_events & NX_CLOUD_MODULE_EVENT_1)
255     {
256         cloud_app -> nx_cloud_module_event_1++;
257     }
258     if (module_own_events & NX_CLOUD_MODULE_EVENT_2)
259     {
260         cloud_app -> nx_cloud_module_event_2++;
261     }
262     if (module_own_events & NX_CLOUD_MODULE_EVENT_3)
263     {
264         cloud_app -> nx_cloud_module_event_3++;
265     }
266     if (module_own_events & NX_CLOUD_MODULE_EVENT_4)
267     {
268         cloud_app -> nx_cloud_module_event_4++;
269     }
270 }
271 
272 #else
273 
274 #ifdef CTEST
test_application_define(void * first_unused_memory)275 VOID test_application_define(void *first_unused_memory)
276 #else
277 void    netx_cloud_module_event_test_application_define(void *first_unused_memory)
278 #endif
279 {
280 
281     /* Print out test information banner.  */
282     printf("NetX Test:   Cloud Module Event Test...................................N/A\n");
283 
284     test_control_return(3);
285 }
286 #endif
287 
288