]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - ipc/ipcdev.git/blob - linux/src/tests/NameServerApp.c
7a12633d0cddedf33f85dc5eab2779aa608caffc
[ipc/ipcdev.git] / linux / src / tests / NameServerApp.c
1 /*
2  * Copyright (c) 2012-2015, 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"
63 #define NSLONGNAME "LongNameabcdefghijklmnopqrstuvwxyz1234567890abcdefghi" \
64     "jklmnopqrstuvwxyz1234567890abcdefghijklmnopqrstuvwxyz1234567890"
66 /** ============================================================================
67  *  Globals
68  *  ============================================================================
69  */
70 Int nameLenTest()
71 {
72     NameServer_Params params;
73     NameServer_Handle nsHandle;
74     Int32 status = 0;
75     Ptr ptr;
76     UInt32 val;
78     printf("Testing long names...\n");
80     NameServer_Params_init(&params);
82     params.maxValueLen = sizeof(UInt32);
83     params.maxNameLen = 32;
85     nsHandle = NameServer_create(NSLONGNAME, &params);
86     if (nsHandle == NULL) {
87         printf("Failed to create NameServer '%s'\n", NSLONGNAME);
88         return -1;
89     }
90     else {
91         printf("Created NameServer '%s'\n", NSLONGNAME);
92     }
94     /* This name should be too long for creation params and results in error */
95     printf("Trying to add a name that exceeds maxNameLen...\n");
96     ptr = NameServer_addUInt32(nsHandle, NSLONGNAME, 0xdeadbeef);
97     if (ptr == NULL) {
98         printf("    ...got expected Failure from NameServer_addUInt32()\n");
99     }
100     else {
101         printf("    Error: NameServer_addUInt32() returned non-NULL %p (but "
102             "was expected to fail)\n", ptr);
103         NameServer_delete(&nsHandle);
104         return -1;
105     }
107     printf("Deleting nsHandle...\n");
108     NameServer_delete(&nsHandle);
110     NameServer_Params_init(&params);
112     params.maxValueLen = sizeof(UInt32);
113     params.maxNameLen = 180;
115     nsHandle = NameServer_create(NSLONGNAME, &params);
116     if (nsHandle == NULL) {
117         printf("Failed to create NameServer '%s'\n", NSLONGNAME);
118         return -1;
119     }
120     else {
121         printf("Created NameServer '%s'\n", NSLONGNAME);
122     }
124     /* This name is too long for remote to handle and results in error */
125     printf("Trying to get a name that the remote cannot handle...\n");
126     val = 0x00c0ffee;
127     status = NameServer_getUInt32(nsHandle, NSLONGNAME, &val, NULL);
128     if (status == NameServer_E_NAMETOOLONG) {
129         printf("    ...got expected Failure from NameServer_getUInt32()\n");
130     }
131     else {
132         printf("    Error: NameServer_getUint32() returned unexpected "
133             "result: %d\n", status);
134         return -1;
135     }
137     printf("Deleting nsHandle...\n");
138     NameServer_delete(&nsHandle);
140     NameServer_Params_init(&params);
142     params.maxValueLen = sizeof(UInt32);
143     params.maxNameLen = 32;
145     nsHandle = NameServer_create(NSLONGNAME, &params);
146     if (nsHandle == NULL) {
147         printf("Failed to create NameServer '%s'\n", NSLONGNAME);
148         return -1;
149     }
150     else {
151         printf("Created NameServer '%s'\n", NSLONGNAME);
152     }
154     /* The instance name is too long for remote and results in error */
155     printf("Trying to use an instance name that the remote cannot "
156         "handle...\n");
157     val = 0x00c0ffee;
158     status = NameServer_getUInt32(nsHandle, "Key", &val, NULL);
159     if (status == NameServer_E_NAMETOOLONG) {
160         printf("    ...got expected Failure from NameServer_getUInt32()\n");
161     }
162     else {
163         printf("    Error: NameServer_getUint32() returned unexpected "
164             "result: %d\n", status);
165         return -1;
166     }
168     printf("Deleting nsHandle...\n");
169     NameServer_delete(&nsHandle);
171     return 0;
174 Int testNS(NameServer_Handle nsHandle, String name)
176     Int32 status = 0;
177     Ptr ptr;
178     UInt32 val;
179     char key[16];
180     Int i;
182     ptr = NameServer_addUInt32(nsHandle, name, 0xdeadbeef);
183     if (ptr == NULL) {
184         printf("Failed to NameServer_addUInt32()\n");
185         return -1;
186     }
187     else {
188         printf("NameServer_addUInt32() returned %p\n", ptr);
189     }
191     printf("Trying to add same key (should fail)...\n");
192     ptr = NameServer_addUInt32(nsHandle, name, 0xdeadc0de);
193     if (ptr == NULL) {
194         printf("    ...got expected Failure from NameServer_addUInt32()\n");
195     }
196     else {
197         printf("    Error: NameServer_addUInt32() returned non-NULL %p (but was expected to fail)\n", ptr);
198         return -1;
199     }
201     val = 0x00c0ffee;
202     status = NameServer_getUInt32(nsHandle, name, &val, NULL);
203     printf("NameServer_getUInt32() returned %d, val=0x%x (was 0x00c0ffee)\n", status, val);
205     printf("Removing 0xdeadbeef w/ NameServer_remove()...\n");
206     status = NameServer_remove(nsHandle, name);
207     if (status < 0) {
208         printf("NameServer_remove() failed: %d\n", status);
209         return -1;
210     }
212     ptr = NameServer_addUInt32(nsHandle, name, 0xdeadc0de);
213     if (ptr == NULL) {
214         printf("Error: NameServer_addUInt32() failed\n");
215         return -1;
216     }
217     else {
218         printf("NameServer_addUInt32(0xdeadc0de) succeeded\n");
219     }
221     val = 0x00c0ffee;
222     status = NameServer_getUInt32(nsHandle, name, &val, NULL);
223     printf("NameServer_getUInt32() returned %d, val=0x%x (was 0x00c0ffee)\n", status, val);
225     printf("Removing 0xdeadc0de w/ NameServer_removeEntry()...\n");
226     status = NameServer_removeEntry(nsHandle, ptr);
227     if (status < 0) {
228         printf("NameServer_remove() failed: %d\n", status);
229         return -1;
230     }
232     ptr = NameServer_addUInt32(nsHandle, name, 0x0badc0de);
233     if (ptr == NULL) {
234         printf("Error: NameServer_addUInt32() failed\n");
235         return -1;
236     }
237     else {
238         printf("NameServer_addUInt32(0x0badc0de) succeeded\n");
239     }
241     val = 0x00c0ffee;
242     status = NameServer_getUInt32(nsHandle, name, &val, NULL);
243     printf("NameServer_getUInt32() returned %d, val=0x%x (was 0x00c0ffee)\n", status, val);
245     status = NameServer_remove(nsHandle, name);
246     if (status < 0) {
247         printf("Error: NameServer_remove() failed\n");
248         return -1;
249     }
250     else {
251         printf("NameServer_remove(%s) succeeded\n", name);
252     }
254     for (i = 0; i < 10; i++) {
255         sprintf(key, "foobar%d", i);
257         ptr = NameServer_addUInt32(nsHandle, key, 0x0badc0de + i);
258         if (ptr == NULL) {
259             printf("Error: NameServer_addUInt32() failed\n");
260             return -1;
261         }
262         else {
263             printf("NameServer_addUInt32(%s, 0x%x) succeeded\n", key, 0x0badc0de + i);
264         }
266         val = 0x00c0ffee;
267         status = NameServer_getUInt32(nsHandle, key, &val, NULL);
268         printf("NameServer_getUInt32(%s) returned %d, val=0x%x (was 0x00c0ffee)\n", key, status, val);
270         if (val != (0x0badc0de + i)) {
271             printf("get val (0x%x) != add val (0x%x)!\n", val, 0x0badc0de + i);
272         }
273     }
275     for (i = 0; i < 10; i++) {
276         sprintf(key, "foobar%d", i);
278         status = NameServer_remove(nsHandle, key);
279         if (status < 0) {
280             printf("Error: NameServer_remove() failed\n");
281             return -1;
282         }
283         else {
284             printf("NameServer_remove(%s) succeeded\n", key);
285         }
286     }
288     return 0;
291 /** ============================================================================
292  *  Functions
293  *  ============================================================================
294  */
295 Int
296 NameServerApp_startup()
298     Int32 status = 0;
299     NameServer_Params params;
300     NameServer_Handle nsHandle;
301     NameServer_Handle nsHandleAlias;
302     NameServer_Handle nsHandle2;
303     Int iteration = 0;
304 #ifdef USE_NSD
305     LAD_ClientHandle ladHandle;
306     LAD_Status ladStatus;
307 #endif
309     printf ("Entered NameServerApp_startup\n");
311 #ifdef USE_NSD
312     ladStatus = LAD_connect(&ladHandle);
313     if (ladStatus != LAD_SUCCESS) {
314         printf("LAD_connect() failed: %d\n", ladStatus);
315         return -1;
316     }
317     else {
318         printf("LAD_connect() succeeded: ladHandle=%d\n", ladHandle);
319     }
320 #endif
322     printf("Calling NameServer_setup()...\n");
323     NameServer_setup();
325 again:
326     NameServer_Params_init(&params);
328     params.maxValueLen = sizeof(UInt32);
329     params.maxNameLen = 32;
331     printf("params.maxValueLen=%d\n", params.maxValueLen);
332     printf("params.maxNameLen=%d\n", params.maxNameLen);
333     printf("params.checkExisting=%d\n", params.checkExisting);
335     nsHandle = NameServer_create(NSNAME, &params);
336     if (nsHandle == NULL) {
337         printf("Failed to create NameServer '%s'\n", NSNAME);
338         return -1;
339     }
340     else {
341         printf("Created NameServer '%s'\n", NSNAME);
342     }
344     nsHandleAlias = NameServer_create(NSNAME, &params);
345     if (nsHandleAlias == NULL) {
346         printf("Failed to get handle to NameServer '%s'\n", NSNAME);
347         return -1;
348     }
349     else {
350         printf("Got another handle to NameServer '%s'\n", NSNAME);
351     }
353     NameServer_Params_init(&params);
355     params.maxValueLen = sizeof(UInt32);
356     params.maxNameLen = 32;
357     nsHandle2 = NameServer_create(NSNAME2, &params);
358     if (nsHandle2 == NULL) {
359         printf("Failed to create NameServer '%s'\n", NSNAME2);
360         return -1;
361     }
362     else {
363         printf("Created NameServer '%s'\n", NSNAME2);
364     }
366     printf("Testing nsHandle\n");
367     status = testNS(nsHandle, "Key");
368     if (status != 0) {
369         printf("test failed on nsHandle\n");
370         return status;
371     }
372     printf("Testing nsHandle2\n");
373     status = testNS(nsHandle2, "Key");
374     if (status != 0) {
375         printf("test failed on nsHandle2\n");
376         return status;
377     }
379     printf("Deleting nsHandle and nsHandle2...\n");
380     NameServer_delete(&nsHandle);
381     NameServer_delete(&nsHandle2);
383     /*
384      * Verify that we can still use the alias handle after deleting the
385      * initial handle
386      */
387     printf("Testing nsHandleAlias\n");
388     status = testNS(nsHandleAlias, "Key");
389     if (status != 0) {
390         printf("test failed on nsHandleAlias\n");
391         return status;
392     }
393     printf("Deleting nsHandleAlias...\n");
394     NameServer_delete(&nsHandleAlias);
396     iteration++;
397     if (iteration < 2) {
398         goto again;
399     }
401     status = nameLenTest();
402     if (status != 0) {
403         printf("Name Length test failed\n");
404         return status;
405     }
407     printf("Calling NameServer_destroy()...\n");
408     NameServer_destroy();
410 #ifdef USE_NSD
411     ladStatus = LAD_disconnect(ladHandle);
412     if (ladStatus != LAD_SUCCESS) {
413         printf("LAD_disconnect() failed: %d\n", ladStatus);
414         return -1;
415     }
416     else {
417         printf("LAD_disconnect() succeeded\n");
418     }
419 #endif
421     printf ("Leaving NameServerApp_startup: status = 0x%x\n", status);
423     return status;
427 Int
428 NameServerApp_execute()
430     Int32 status = 0;
432     printf ("Entered NameServerApp_execute\n");
434     printf ("Leaving NameServerApp_execute\n\n");
436     return status;
439 Int
440 NameServerApp_shutdown()
442     Int32 status = 0;
444     printf ("Entered NameServerApp_shutdown()\n");
446     printf ("Leave NameServerApp_shutdown()\n");
448     return status;
451 int
452 main (int argc, char ** argv)
454     NameServerApp_startup();
455     NameServerApp_execute();
456     NameServerApp_shutdown();
458     return(0);