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 nsHandle2;
197 Int iteration = 0;
198 #ifdef USE_NSD
199 LAD_ClientHandle ladHandle;
200 LAD_Status ladStatus;
201 #endif
203 printf ("Entered NameServerApp_startup\n");
205 #ifdef USE_NSD
206 ladStatus = LAD_connect(&ladHandle);
207 if (ladStatus != LAD_SUCCESS) {
208 printf("LAD_connect() failed: %d\n", ladStatus);
209 return -1;
210 }
211 else {
212 printf("LAD_connect() succeeded: ladHandle=%d\n", ladHandle);
213 }
214 #endif
216 printf("Calling NameServer_setup()...\n");
217 NameServer_setup();
219 again:
220 NameServer_Params_init(¶ms);
221 printf("params.maxValueLen=%d\n", params.maxValueLen);
222 printf("params.maxNameLen=%d\n", params.maxNameLen);
223 printf("params.checkExisting=%d\n", params.checkExisting);
225 params.maxValueLen = sizeof(UInt32);
226 params.maxNameLen = 32;
227 nsHandle = NameServer_create(NSNAME, ¶ms);
228 if (nsHandle == NULL) {
229 printf("Failed to create NameServer '%s'\n", NSNAME);
230 return -1;
231 }
232 else {
233 printf("Created NameServer '%s'\n", NSNAME);
234 }
236 NameServer_Params_init(¶ms);
238 params.maxValueLen = sizeof(UInt32);
239 params.maxNameLen = 32;
240 nsHandle2 = NameServer_create(NSNAME2, ¶ms);
241 if (nsHandle2 == NULL) {
242 printf("Failed to create NameServer '%s'\n", NSNAME2);
243 return -1;
244 }
245 else {
246 printf("Created NameServer '%s'\n", NSNAME2);
247 }
249 status = testNS(nsHandle, "Key");
250 status = testNS(nsHandle2, "Key");
252 printf("Deleting nsHandle and nsHandle2...\n");
253 NameServer_delete(&nsHandle);
254 NameServer_delete(&nsHandle2);
256 iteration++;
257 if (iteration < 2) {
258 goto again;
259 }
261 printf("Calling NameServer_destroy()...\n");
262 NameServer_destroy();
264 #ifdef USE_NSD
265 ladStatus = LAD_disconnect(ladHandle);
266 if (ladStatus != LAD_SUCCESS) {
267 printf("LAD_disconnect() failed: %d\n", ladStatus);
268 return -1;
269 }
270 else {
271 printf("LAD_disconnect() succeeded\n");
272 }
273 #endif
275 printf ("Leaving NameServerApp_startup: status = 0x%x\n", status);
277 return status;
278 }
281 Int
282 NameServerApp_execute()
283 {
284 Int32 status = 0;
286 printf ("Entered NameServerApp_execute\n");
288 printf ("Leaving NameServerApp_execute\n\n");
290 return status;
291 }
293 Int
294 NameServerApp_shutdown()
295 {
296 Int32 status = 0;
298 printf ("Entered NameServerApp_shutdown()\n");
300 printf ("Leave NameServerApp_shutdown()\n");
302 return status;
303 }
305 int
306 main (int argc, char ** argv)
307 {
308 NameServerApp_startup();
309 NameServerApp_execute();
310 NameServerApp_shutdown();
312 return(0);
313 }