1 /*
2 * Copyright (c) 2013, Texas Instruments Incorporated
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * * Neither the name of Texas Instruments Incorporated nor the names of
17 * its contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32 /* =============================================================================
33 * @file NameServerApp.c
34 *
35 * @brief Sample application for NameServer module between MPU and Remote Proc
36 *
37 * ============================================================================
38 */
40 /* this define must precede inclusion of any xdc header file */
41 #define Registry_CURDESC Test__Desc
42 #define MODULE_NAME "Server"
44 /* Standard headers */
45 #include <stdio.h>
47 /* Ipc Standard header */
48 #include <xdc/std.h>
49 #include <xdc/runtime/Diags.h>
50 #include <xdc/runtime/Error.h>
51 #include <xdc/runtime/Log.h>
52 #include <xdc/runtime/System.h>
53 #include <xdc/runtime/Assert.h>
54 #include <xdc/runtime/Registry.h>
56 #include <ti/sysbios/BIOS.h>
57 #include <ti/sysbios/knl/Task.h>
59 #include <ti/ipc/Ipc.h>
60 //#include <_NameServer.h>
62 /* Module level headers */
63 #include <ti/ipc/NameServer.h>
66 /** ============================================================================
67 * Macros and types
68 * ============================================================================
69 */
70 #define NSNAME "MyNS"
71 #define NSNAME2 "MyNS2"
73 /* private data */
74 Registry_Desc Registry_CURDESC;
76 Void smain (UArg arg0, UArg arg1);
78 /** ============================================================================
79 * Globals
80 * ============================================================================
81 */
83 Int testNS(NameServer_Handle nsHandle, String name)
84 {
85 Int32 status = 0;
86 Ptr ptr;
87 UInt32 val;
88 char key[16];
89 Int i;
91 ptr = NameServer_addUInt32(nsHandle, name, 0xdeadbeef);
92 if (ptr == NULL) {
93 Log_print0(Diags_INFO, "Failed to NameServer_addUInt32()\n");
94 return -1;
95 }
96 else {
97 Log_print1(Diags_INFO, "NameServer_addUInt32() returned %p\n", (IArg)ptr);
98 }
100 Log_print0(Diags_INFO, "Trying to add same key (should fail)...\n");
101 ptr = NameServer_addUInt32(nsHandle, name, 0xdeadc0de);
102 if (ptr == NULL) {
103 Log_print0(Diags_INFO, " ...got expected Failure from NameServer_addUInt32()\n");
104 }
105 else {
106 Log_print1(Diags_INFO, " Error: NameServer_addUInt32() returned non-NULL %p (but was expected to fail)\n", (IArg)ptr);
107 return -1;
108 }
110 val = 0x00c0ffee;
111 status = NameServer_getUInt32(nsHandle, name, &val, NULL);
112 Log_print2(Diags_INFO, "NameServer_getUInt32() returned %d, val=0x%x (was 0x00c0ffee)\n", status, val);
114 Log_print0(Diags_INFO, "Removing 0xdeadbeef w/ NameServer_remove()...\n");
115 status = NameServer_remove(nsHandle, name);
116 if (status < 0) {
117 Log_print1(Diags_INFO, "NameServer_remove() failed: %d\n", status);
118 return -1;
119 }
121 ptr = NameServer_addUInt32(nsHandle, name, 0xdeadc0de);
122 if (ptr == NULL) {
123 Log_print0(Diags_INFO, "Error: NameServer_addUInt32() failed\n");
124 return -1;
125 }
126 else {
127 Log_print0(Diags_INFO, "NameServer_addUInt32(0xdeadc0de) succeeded\n");
128 }
130 val = 0x00c0ffee;
131 status = NameServer_getUInt32(nsHandle, name, &val, NULL);
132 Log_print2(Diags_INFO, "NameServer_getUInt32() returned %d, val=0x%x (was 0x00c0ffee)\n", status, val);
134 Log_print0(Diags_INFO, "Removing 0xdeadc0de w/ NameServer_removeEntry()...\n");
135 status = NameServer_removeEntry(nsHandle, ptr);
136 if (status < 0) {
137 Log_print1(Diags_INFO, "NameServer_remove() failed: %d\n", status);
138 return -1;
139 }
141 ptr = NameServer_addUInt32(nsHandle, name, 0x0badc0de);
142 if (ptr == NULL) {
143 Log_print0(Diags_INFO, "Error: NameServer_addUInt32() failed\n");
144 return -1;
145 }
146 else {
147 Log_print0(Diags_INFO, "NameServer_addUInt32(0x0badc0de) succeeded\n");
148 }
150 val = 0x00c0ffee;
151 status = NameServer_getUInt32(nsHandle, name, &val, NULL);
152 Log_print2(Diags_INFO, "NameServer_getUInt32() returned %d, val=0x%x (was 0x00c0ffee)\n", status, val);
154 status = NameServer_remove(nsHandle, name);
155 if (status < 0) {
156 Log_print0(Diags_INFO, "Error: NameServer_remove() failed\n");
157 return -1;
158 }
159 else {
160 Log_print1(Diags_INFO, "NameServer_remove(%s) succeeded\n", (IArg)name);
161 }
163 for (i = 0; i < 10; i++) {
164 sprintf(key, "foobar%d", i);
166 ptr = NameServer_addUInt32(nsHandle, key, 0x0badc0de + i);
167 if (ptr == NULL) {
168 Log_print0(Diags_INFO, "Error: NameServer_addUInt32() failed\n");
169 return -1;
170 }
171 else {
172 Log_print2(Diags_INFO, "NameServer_addUInt32(%s, 0x%x) succeeded\n", (IArg)key, 0x0badc0de + i);
173 }
175 val = 0x00c0ffee;
176 status = NameServer_getUInt32(nsHandle, key, &val, NULL);
177 Log_print3(Diags_INFO, "NameServer_getUInt32(%s) returned %d, val=0x%x (was 0x00c0ffee)\n", (IArg)key, status, val);
179 if (val != (0x0badc0de + i)) {
180 Log_print2(Diags_INFO, "get val (0x%x) != add val (0x%x)!\n", val, 0x0badc0de + i);
181 }
182 }
184 for (i = 0; i < 10; i++) {
185 sprintf(key, "foobar%d", i);
187 status = NameServer_remove(nsHandle, key);
188 if (status < 0) {
189 Log_print0(Diags_INFO, "Error: NameServer_remove() failed\n");
190 return -1;
191 }
192 else {
193 Log_print1(Diags_INFO, "NameServer_remove(%s) succeeded\n", (IArg)key);
194 }
195 }
197 return 0;
198 }
200 /** ============================================================================
201 * Functions
202 * ============================================================================
203 */
204 Int
205 NameServerApp_startup()
206 {
207 Int32 status = 0;
208 NameServer_Params params;
209 NameServer_Handle nsHandle;
210 NameServer_Handle nsHandleAlias;
211 NameServer_Handle nsHandle2;
212 Int iteration = 0;
214 Log_print0(Diags_INFO, "Entered NameServerApp_startup\n");
216 /* status = Ipc_start();
218 if (status < 0) {
219 Log_print1(Diags_INFO, "Ipc_start failed: status = %d\n", status);
220 return -1;
221 }
222 */
223 // Log_print0(Diags_INFO, "Calling NameServer_setup()...\n");
224 // NameServer_setup();
226 again:
227 NameServer_Params_init(¶ms);
229 params.maxValueLen = sizeof(UInt32);
230 params.maxNameLen = 32;
232 Log_print1(Diags_INFO, "params.maxValueLen=%d\n", params.maxValueLen);
233 Log_print1(Diags_INFO, "params.maxNameLen=%d\n", params.maxNameLen);
234 Log_print1(Diags_INFO, "params.checkExisting=%d\n", params.checkExisting);
236 nsHandle = NameServer_create(NSNAME, ¶ms);
237 if (nsHandle == NULL) {
238 Log_print1(Diags_INFO, "Failed to create NameServer '%s'\n", (IArg)NSNAME);
239 return -1;
240 }
241 else {
242 Log_print1(Diags_INFO, "Created NameServer '%s'\n", (IArg)NSNAME);
243 }
245 nsHandleAlias = NameServer_create(NSNAME, ¶ms);
246 if (nsHandleAlias == NULL) {
247 Log_print1(Diags_INFO, "Failed to get handle to NameServer '%s'\n", (IArg)NSNAME);
248 return -1;
249 }
250 else {
251 Log_print1(Diags_INFO, "Got another handle to NameServer '%s'\n", (IArg)NSNAME);
252 }
254 NameServer_Params_init(¶ms);
256 params.maxValueLen = sizeof(UInt32);
257 params.maxNameLen = 32;
258 nsHandle2 = NameServer_create(NSNAME2, ¶ms);
259 if (nsHandle2 == NULL) {
260 Log_print1(Diags_INFO, "Failed to create NameServer '%s'\n", (IArg)NSNAME2);
261 return -1;
262 }
263 else {
264 Log_print1(Diags_INFO, "Created NameServer '%s'\n", (IArg)NSNAME2);
265 }
267 Log_print0(Diags_INFO, "Testing nsHandle\n");
268 status = testNS(nsHandle, "Key");
269 if (status != 0) {
270 Log_print0(Diags_INFO, "test failed on nsHandle\n");
271 return status;
272 }
273 Log_print0(Diags_INFO, "Testing nsHandle2\n");
274 status = testNS(nsHandle2, "Key");
275 if (status != 0) {
276 Log_print0(Diags_INFO, "test failed on nsHandle2\n");
277 return status;
278 }
280 Log_print0(Diags_INFO, "Deleting nsHandle and nsHandle2...\n");
281 NameServer_delete(&nsHandle);
282 NameServer_delete(&nsHandle2);
284 /*
285 * Verify that we can still use the alias handle after deleting the
286 * initial handle
287 */
288 Log_print0(Diags_INFO, "Testing nsHandleAlias\n");
289 status = testNS(nsHandleAlias, "Key");
290 if (status != 0) {
291 Log_print0(Diags_INFO, "test failed on nsHandleAlias\n");
292 return status;
293 }
294 Log_print0(Diags_INFO, "Deleting nsHandleAlias...\n");
295 NameServer_delete(&nsHandleAlias);
297 iteration++;
298 if (iteration < 2) {
299 goto again;
300 }
302 // Log_print0(Diags_INFO, "Calling NameServer_destroy()...\n");
303 // NameServer_destroy();
305 Log_print1(Diags_INFO, "Leaving NameServerApp_startup: status = 0x%x\n", status);
307 return status;
308 }
311 Int
312 NameServerApp_execute()
313 {
314 Int32 status = 0;
316 Log_print0(Diags_INFO, "Entered NameServerApp_execute\n");
318 Log_print0(Diags_INFO, "Leaving NameServerApp_execute\n\n");
320 return status;
321 }
323 Int
324 NameServerApp_shutdown()
325 {
326 Int32 status = 0;
328 Log_print0(Diags_INFO, "Entered NameServerApp_shutdown()\n");
330 /* status = Ipc_stop();
331 if (status < 0) {
332 Log_print1(Diags_INFO, "Ipc_stop failed: status = %d\n", status);
333 }
334 */
335 Log_print0(Diags_INFO, "Leave NameServerApp_shutdown()\n");
337 return status;
338 }
340 Int main(Int argc, Char* argv[])
341 {
342 Error_Block eb;
343 Task_Params taskParams;
344 Registry_Result result;
346 Log_print0(Diags_ENTRY, "--> main:");
348 /* must initialize the error block before using it */
349 Error_init(&eb);
351 /* create main thread (interrupts not enabled in main on BIOS) */
352 Task_Params_init(&taskParams);
353 taskParams.instance->name = "smain";
354 taskParams.stackSize = 0x1000;
355 Task_create(smain, &taskParams, &eb);
357 if (Error_check(&eb)) {
358 System_abort("main: failed to create application startup thread");
359 }
361 /* register with xdc.runtime to get a diags mask */
362 result = Registry_addModule(&Registry_CURDESC, MODULE_NAME);
363 Assert_isTrue(result == Registry_SUCCESS, (Assert_Id)NULL);
365 /* start scheduler, this never returns */
366 BIOS_start();
368 /* should never get here */
369 Log_print0(Diags_EXIT, "<-- main:");
370 return (0);
371 }
373 Void smain (UArg arg0, UArg arg1)
374 {
375 int status = 0;
377 Log_print0(Diags_ENTRY | Diags_INFO, "--> smain:");
379 /* turn on Diags_INFO trace */
380 Diags_setMask("Server+F");
382 status = NameServerApp_startup();
383 if (status < 0) {
384 goto leave;
385 }
387 status = NameServerApp_execute();
388 if (status < 0) {
389 goto leave;
390 }
392 status = NameServerApp_shutdown();
393 if (status < 0) {
394 goto leave;
395 }
397 leave:
398 Log_print1(Diags_EXIT, "<-- smain: %d", (IArg)status);
399 return;
400 }