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