1 /*
2 * Copyright (c) 2012-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 #define USE_NSD
42 /* Standard headers */
43 #include <stdio.h>
45 /* IPC Standard header */
46 #include <ti/ipc/Std.h>
48 #include <_NameServer.h>
49 #ifdef USE_NSD
50 #include <ladclient.h>
51 #endif
53 /* Module level headers */
54 #include <ti/ipc/NameServer.h>
57 /** ============================================================================
58 * Macros and types
59 * ============================================================================
60 */
61 #define NSNAME "MyNS"
62 #define NSNAME2 "MyNS2"
64 /** ============================================================================
65 * Globals
66 * ============================================================================
67 */
69 Int testNS(NameServer_Handle nsHandle, String name)
70 {
71 Int32 status = 0;
72 Ptr ptr;
73 UInt32 val;
74 char key[16];
75 Int i;
77 ptr = NameServer_addUInt32(nsHandle, name, 0xdeadbeef);
78 if (ptr == NULL) {
79 printf("Failed to NameServer_addUInt32()\n");
80 return -1;
81 }
82 else {
83 printf("NameServer_addUInt32() returned %p\n", ptr);
84 }
86 printf("Trying to add same key (should fail)...\n");
87 ptr = NameServer_addUInt32(nsHandle, name, 0xdeadc0de);
88 if (ptr == NULL) {
89 printf(" ...got expected Failure from NameServer_addUInt32()\n");
90 }
91 else {
92 printf(" Error: NameServer_addUInt32() returned non-NULL %p (but was expected to fail)\n", ptr);
93 return -1;
94 }
96 val = 0x00c0ffee;
97 status = NameServer_getUInt32(nsHandle, name, &val, NULL);
98 printf("NameServer_getUInt32() returned %d, val=0x%x (was 0x00c0ffee)\n", status, val);
100 printf("Removing 0xdeadbeef w/ NameServer_remove()...\n");
101 status = NameServer_remove(nsHandle, name);
102 if (status < 0) {
103 printf("NameServer_remove() failed: %d\n", status);
104 return -1;
105 }
107 ptr = NameServer_addUInt32(nsHandle, name, 0xdeadc0de);
108 if (ptr == NULL) {
109 printf("Error: NameServer_addUInt32() failed\n");
110 return -1;
111 }
112 else {
113 printf("NameServer_addUInt32(0xdeadc0de) succeeded\n");
114 }
116 val = 0x00c0ffee;
117 status = NameServer_getUInt32(nsHandle, name, &val, NULL);
118 printf("NameServer_getUInt32() returned %d, val=0x%x (was 0x00c0ffee)\n", status, val);
120 printf("Removing 0xdeadc0de w/ NameServer_removeEntry()...\n");
121 status = NameServer_removeEntry(nsHandle, ptr);
122 if (status < 0) {
123 printf("NameServer_remove() failed: %d\n", status);
124 return -1;
125 }
127 ptr = NameServer_addUInt32(nsHandle, name, 0x0badc0de);
128 if (ptr == NULL) {
129 printf("Error: NameServer_addUInt32() failed\n");
130 return -1;
131 }
132 else {
133 printf("NameServer_addUInt32(0x0badc0de) succeeded\n");
134 }
136 val = 0x00c0ffee;
137 status = NameServer_getUInt32(nsHandle, name, &val, NULL);
138 printf("NameServer_getUInt32() returned %d, val=0x%x (was 0x00c0ffee)\n", status, val);
140 status = NameServer_remove(nsHandle, name);
141 if (status < 0) {
142 printf("Error: NameServer_remove() failed\n");
143 return -1;
144 }
145 else {
146 printf("NameServer_remove(%s) succeeded\n", name);
147 }
149 for (i = 0; i < 10; i++) {
150 sprintf(key, "foobar%d", i);
152 ptr = NameServer_addUInt32(nsHandle, key, 0x0badc0de + i);
153 if (ptr == NULL) {
154 printf("Error: NameServer_addUInt32() failed\n");
155 return -1;
156 }
157 else {
158 printf("NameServer_addUInt32(%s, 0x%x) succeeded\n", key, 0x0badc0de + i);
159 }
161 val = 0x00c0ffee;
162 status = NameServer_getUInt32(nsHandle, key, &val, NULL);
163 printf("NameServer_getUInt32(%s) returned %d, val=0x%x (was 0x00c0ffee)\n", key, status, val);
165 if (val != (0x0badc0de + i)) {
166 printf("get val (0x%x) != add val (0x%x)!\n", val, 0x0badc0de + i);
167 }
168 }
170 for (i = 0; i < 10; i++) {
171 sprintf(key, "foobar%d", i);
173 status = NameServer_remove(nsHandle, key);
174 if (status < 0) {
175 printf("Error: NameServer_remove() failed\n");
176 return -1;
177 }
178 else {
179 printf("NameServer_remove(%s) succeeded\n", key);
180 }
181 }
183 return 0;
184 }
186 /** ============================================================================
187 * Functions
188 * ============================================================================
189 */
190 Int
191 NameServerApp_startup()
192 {
193 Int32 status = 0;
194 NameServer_Params params;
195 NameServer_Handle nsHandle;
196 NameServer_Handle nsHandleAlias;
197 NameServer_Handle nsHandle2;
198 Int iteration = 0;
199 #ifdef USE_NSD
200 LAD_ClientHandle ladHandle;
201 LAD_Status ladStatus;
202 #endif
204 printf ("Entered NameServerApp_startup\n");
206 #ifdef USE_NSD
207 ladStatus = LAD_connect(&ladHandle);
208 if (ladStatus != LAD_SUCCESS) {
209 printf("LAD_connect() failed: %d\n", ladStatus);
210 return -1;
211 }
212 else {
213 printf("LAD_connect() succeeded: ladHandle=%d\n", ladHandle);
214 }
215 #endif
217 printf("Calling NameServer_setup()...\n");
218 NameServer_setup();
220 again:
221 NameServer_Params_init(¶ms);
223 params.maxValueLen = sizeof(UInt32);
224 params.maxNameLen = 32;
226 printf("params.maxValueLen=%d\n", params.maxValueLen);
227 printf("params.maxNameLen=%d\n", params.maxNameLen);
228 printf("params.checkExisting=%d\n", params.checkExisting);
230 nsHandle = NameServer_create(NSNAME, ¶ms);
231 if (nsHandle == NULL) {
232 printf("Failed to create NameServer '%s'\n", NSNAME);
233 return -1;
234 }
235 else {
236 printf("Created NameServer '%s'\n", NSNAME);
237 }
239 nsHandleAlias = NameServer_create(NSNAME, ¶ms);
240 if (nsHandleAlias == NULL) {
241 printf("Failed to get handle to NameServer '%s'\n", NSNAME);
242 return -1;
243 }
244 else {
245 printf("Got another handle to NameServer '%s'\n", NSNAME);
246 }
248 NameServer_Params_init(¶ms);
250 params.maxValueLen = sizeof(UInt32);
251 params.maxNameLen = 32;
252 nsHandle2 = NameServer_create(NSNAME2, ¶ms);
253 if (nsHandle2 == NULL) {
254 printf("Failed to create NameServer '%s'\n", NSNAME2);
255 return -1;
256 }
257 else {
258 printf("Created NameServer '%s'\n", NSNAME2);
259 }
261 printf("Testing nsHandle\n");
262 status = testNS(nsHandle, "Key");
263 if (status != 0) {
264 printf("test failed on nsHandle\n");
265 return status;
266 }
267 printf("Testing nsHandle2\n");
268 status = testNS(nsHandle2, "Key");
269 if (status != 0) {
270 printf("test failed on nsHandle2\n");
271 return status;
272 }
274 printf("Deleting nsHandle and nsHandle2...\n");
275 NameServer_delete(&nsHandle);
276 NameServer_delete(&nsHandle2);
278 /*
279 * Verify that we can still use the alias handle after deleting the
280 * initial handle
281 */
282 printf("Testing nsHandleAlias\n");
283 status = testNS(nsHandleAlias, "Key");
284 if (status != 0) {
285 printf("test failed on nsHandleAlias\n");
286 return status;
287 }
288 printf("Deleting nsHandleAlias...\n");
289 NameServer_delete(&nsHandleAlias);
291 iteration++;
292 if (iteration < 2) {
293 goto again;
294 }
296 printf("Calling NameServer_destroy()...\n");
297 NameServer_destroy();
299 #ifdef USE_NSD
300 ladStatus = LAD_disconnect(ladHandle);
301 if (ladStatus != LAD_SUCCESS) {
302 printf("LAD_disconnect() failed: %d\n", ladStatus);
303 return -1;
304 }
305 else {
306 printf("LAD_disconnect() succeeded\n");
307 }
308 #endif
310 printf ("Leaving NameServerApp_startup: status = 0x%x\n", status);
312 return status;
313 }
316 Int
317 NameServerApp_execute()
318 {
319 Int32 status = 0;
321 printf ("Entered NameServerApp_execute\n");
323 printf ("Leaving NameServerApp_execute\n\n");
325 return status;
326 }
328 Int
329 NameServerApp_shutdown()
330 {
331 Int32 status = 0;
333 printf ("Entered NameServerApp_shutdown()\n");
335 printf ("Leave NameServerApp_shutdown()\n");
337 return status;
338 }
340 int
341 main (int argc, char ** argv)
342 {
343 NameServerApp_startup();
344 NameServerApp_execute();
345 NameServerApp_shutdown();
347 return(0);
348 }