1 /*
2 * Copyright (c) 2012-2014, 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 * ======== NameServer_client.c ========
34 */
35 #include <ti/ipc/Std.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <errno.h>
41 #include <sys/stat.h>
42 #include <sys/types.h>
43 #include <unistd.h>
44 #include <time.h>
45 #include <pthread.h>
47 #include <ti/ipc/NameServer.h>
49 #include <ladclient.h>
50 #include <_lad.h>
52 static Bool verbose = FALSE;
55 /*
56 * The NameServer_*() APIs are reproduced here. These versions are just
57 * front-ends for communicating with the actual NameServer module (currently
58 * implemented as a daemon process ala LAD).
59 */
61 Int NameServer_setup(Void)
62 {
63 Int status;
64 LAD_ClientHandle handle;
65 struct LAD_CommandObj cmd;
66 union LAD_ResponseObj rsp;
68 handle = LAD_findHandle();
69 if (handle == LAD_MAXNUMCLIENTS) {
70 PRINTVERBOSE1(
71 "NameServer_setup: can't find connection to daemon for pid %d\n",
72 getpid())
74 return NameServer_E_RESOURCE;
75 }
77 cmd.cmd = LAD_NAMESERVER_SETUP;
78 cmd.clientId = handle;
80 if ((status = LAD_putCommand(&cmd)) != LAD_SUCCESS) {
81 PRINTVERBOSE1(
82 "NameServer_setup: sending LAD command failed, status=%d\n", status)
83 return NameServer_E_FAIL;
84 }
86 if ((status = LAD_getResponse(handle, &rsp)) != LAD_SUCCESS) {
87 PRINTVERBOSE1("NameServer_setup: no LAD response, status=%d\n", status)
88 return(status);
89 }
90 status = rsp.status;
92 PRINTVERBOSE2(
93 "NameServer_setup: got LAD response for client %d, status=%d\n",
94 handle, status)
96 return status;
97 }
99 Int NameServer_destroy(Void)
100 {
101 Int status;
102 LAD_ClientHandle handle;
103 struct LAD_CommandObj cmd;
104 union LAD_ResponseObj rsp;
106 PRINTVERBOSE0("NameServer_destroy: entered\n")
108 handle = LAD_findHandle();
109 if (handle == LAD_MAXNUMCLIENTS) {
110 PRINTVERBOSE1(
111 "NameServer_destroy: can't find connection to daemon for pid %d\n",
112 getpid())
114 return NameServer_E_RESOURCE;
115 }
117 cmd.cmd = LAD_NAMESERVER_DESTROY;
118 cmd.clientId = handle;
120 if ((status = LAD_putCommand(&cmd)) != LAD_SUCCESS) {
121 PRINTVERBOSE1(
122 "NameServer_destroy: sending LAD command failed, status=%d\n", status)
123 return NameServer_E_FAIL;
124 }
126 if ((status = LAD_getResponse(handle, &rsp)) != LAD_SUCCESS) {
127 PRINTVERBOSE1(
128 "NameServer_destroy: no LAD response, status=%d\n", status)
129 return(status);
130 }
131 status = rsp.status;
133 PRINTVERBOSE2(
134 "NameServer_destroy: got LAD response for client %d, status=%d\n",
135 handle, status)
137 return status;
138 }
140 Void NameServer_Params_init(NameServer_Params *params)
141 {
142 Int status;
143 LAD_ClientHandle handle;
144 struct LAD_CommandObj cmd;
145 union LAD_ResponseObj rsp;
147 handle = LAD_findHandle();
148 if (handle == LAD_MAXNUMCLIENTS) {
149 PRINTVERBOSE1(
150 "NameServer_Params_init: can't find connection to daemon for pid %d\n",
151 getpid())
153 return;
154 }
156 cmd.cmd = LAD_NAMESERVER_PARAMS_INIT;
157 cmd.clientId = handle;
159 if ((status = LAD_putCommand(&cmd)) != LAD_SUCCESS) {
160 PRINTVERBOSE1(
161 "NameServer_Params_init: sending LAD command failed, status=%d\n",
162 status)
163 return;
164 }
166 if ((status = LAD_getResponse(handle, &rsp)) != LAD_SUCCESS) {
167 PRINTVERBOSE1(
168 "NameServer_Params_init: no LAD response, status=%d\n", status)
169 return;
170 }
172 PRINTVERBOSE1("NameServer_Params_init: got LAD response for client %d\n",
173 handle)
175 memcpy(params, &rsp.params, sizeof(NameServer_Params));
177 return;
178 }
180 NameServer_Handle NameServer_create(String name,
181 const NameServer_Params *params)
182 {
183 Int status;
184 LAD_ClientHandle handle;
185 struct LAD_CommandObj cmd;
186 union LAD_ResponseObj rsp;
188 handle = LAD_findHandle();
189 if (handle == LAD_MAXNUMCLIENTS) {
190 PRINTVERBOSE1(
191 "NameServer_create: can't find connection to daemon for pid %d\n",
192 getpid())
194 return NULL;
195 }
197 cmd.cmd = LAD_NAMESERVER_CREATE;
198 cmd.clientId = handle;
199 strncpy(cmd.args.create.name, name, NameServer_Params_MAXNAMELEN);
200 memcpy(&cmd.args.create.params, params, sizeof(NameServer_Params));
202 if ((status = LAD_putCommand(&cmd)) != LAD_SUCCESS) {
203 PRINTVERBOSE1(
204 "NameServer_create: sending LAD command failed, status=%d\n",
205 status)
206 return NULL;
207 }
209 if ((status = LAD_getResponse(handle, &rsp)) != LAD_SUCCESS) {
210 PRINTVERBOSE1("NameServer_create: no LAD response, status=%d\n", status)
211 return NULL;
212 }
214 PRINTVERBOSE1("NameServer_create: got LAD response for client %d\n", handle)
216 return rsp.handle;
217 }
219 Ptr NameServer_add(NameServer_Handle nsHandle, String name, Ptr buf,
220 UInt32 len)
221 {
222 Int status;
223 LAD_ClientHandle clHandle;
224 struct LAD_CommandObj cmd;
225 union LAD_ResponseObj rsp;
227 clHandle = LAD_findHandle();
228 if (clHandle == LAD_MAXNUMCLIENTS) {
229 PRINTVERBOSE1(
230 "NameServer_add: can't find connection to daemon for pid %d\n",
231 getpid())
233 return NULL;
234 }
236 cmd.cmd = LAD_NAMESERVER_ADD;
237 cmd.clientId = clHandle;
238 cmd.args.add.handle = nsHandle;
239 strncpy(cmd.args.add.name, name, LAD_MAXENTRYNAMELEN);
240 cmd.args.add.len = len;
242 if (buf != NULL) {
243 memcpy(cmd.args.add.buf, buf, len);
244 }
246 if ((status = LAD_putCommand(&cmd)) != LAD_SUCCESS) {
247 PRINTVERBOSE1(
248 "NameServer_add: sending LAD command failed, status=%d\n",
249 status)
250 return NULL;
251 }
253 if ((status = LAD_getResponse(clHandle, &rsp)) != LAD_SUCCESS) {
254 PRINTVERBOSE1(
255 "NameServer_add: no LAD response, status=%d\n", status)
256 return NULL;
257 }
259 PRINTVERBOSE1(
260 "NameServer_add: got LAD response for client %d\n", clHandle)
262 return rsp.entryPtr;
263 }
265 Int NameServer_get(NameServer_Handle nsHandle, String name, Ptr buf,
266 UInt32 * len, UInt16 procId[])
267 {
268 Int status;
269 LAD_ClientHandle clHandle;
270 struct LAD_CommandObj cmd;
271 union LAD_ResponseObj rsp;
273 clHandle = LAD_findHandle();
274 if (clHandle == LAD_MAXNUMCLIENTS) {
275 PRINTVERBOSE1(
276 "NameServer_get: can't find connection to daemon for pid %d\n",
277 getpid())
279 return NameServer_E_RESOURCE;
280 }
282 cmd.cmd = LAD_NAMESERVER_GET;
283 cmd.clientId = clHandle;
284 cmd.args.get.handle = nsHandle;
285 strncpy(cmd.args.get.name, name, LAD_MAXENTRYNAMELEN);
286 if (procId != NULL) {
287 memcpy(cmd.args.get.procId, procId,
288 sizeof(UInt16) * MultiProc_MAXPROCESSORS);
289 }
290 else {
291 cmd.args.get.procId[0] = (UInt16)-1;
292 }
294 cmd.args.get.len = *len;
296 if ((status = LAD_putCommand(&cmd)) != LAD_SUCCESS) {
297 PRINTVERBOSE1(
298 "NameServer_get: sending LAD command failed, status=%d\n",
299 status)
300 return NameServer_E_FAIL;
301 }
303 if ((status = LAD_getResponse(clHandle, &rsp)) != LAD_SUCCESS) {
304 PRINTVERBOSE1("NameServer_get: no LAD response, status=%d\n",
305 status)
306 return NameServer_E_FAIL;
307 }
309 *len = rsp.get.len;
310 if (rsp.get.buf != NULL) {
311 memcpy(buf, rsp.get.buf, *len);
312 }
314 status = rsp.status;
316 PRINTVERBOSE1("NameServer_get: got LAD response for client %d\n",
317 clHandle)
319 return status;
320 }
322 Ptr NameServer_addUInt32(NameServer_Handle nsHandle, String name, UInt32 value)
323 {
324 Int status;
325 LAD_ClientHandle clHandle;
326 struct LAD_CommandObj cmd;
327 union LAD_ResponseObj rsp;
329 clHandle = LAD_findHandle();
330 if (clHandle == LAD_MAXNUMCLIENTS) {
331 PRINTVERBOSE1(
332 "NameServer_addUInt32: can't find connection to daemon for pid %d\n",
333 getpid())
335 return NULL;
336 }
338 cmd.cmd = LAD_NAMESERVER_ADDUINT32;
339 cmd.clientId = clHandle;
340 cmd.args.addUInt32.handle = nsHandle;
341 strncpy(cmd.args.addUInt32.name, name, LAD_MAXENTRYNAMELEN);
342 cmd.args.addUInt32.val = value;
344 if ((status = LAD_putCommand(&cmd)) != LAD_SUCCESS) {
345 PRINTVERBOSE1(
346 "NameServer_addUInt32: sending LAD command failed, status=%d\n",
347 status)
348 return NULL;
349 }
351 if ((status = LAD_getResponse(clHandle, &rsp)) != LAD_SUCCESS) {
352 PRINTVERBOSE1(
353 "NameServer_addUInt32: no LAD response, status=%d\n", status)
354 return NULL;
355 }
357 PRINTVERBOSE1(
358 "NameServer_addUInt32: got LAD response for client %d\n", clHandle)
360 return rsp.entryPtr;
361 }
363 Int NameServer_getUInt32(NameServer_Handle nsHandle, String name, Ptr buf,
364 UInt16 procId[])
365 {
366 Int status;
367 LAD_ClientHandle clHandle;
368 UInt32 *val;
369 struct LAD_CommandObj cmd;
370 union LAD_ResponseObj rsp;
372 clHandle = LAD_findHandle();
373 if (clHandle == LAD_MAXNUMCLIENTS) {
374 PRINTVERBOSE1(
375 "NameServer_getUInt32: can't find connection to daemon for pid %d\n",
376 getpid())
378 return NameServer_E_RESOURCE;
379 }
381 cmd.cmd = LAD_NAMESERVER_GETUINT32;
382 cmd.clientId = clHandle;
383 cmd.args.getUInt32.handle = nsHandle;
384 strncpy(cmd.args.getUInt32.name, name, LAD_MAXENTRYNAMELEN);
385 if (procId != NULL) {
386 memcpy(cmd.args.getUInt32.procId, procId,
387 sizeof(UInt16) * MultiProc_MAXPROCESSORS);
388 }
389 else {
390 cmd.args.getUInt32.procId[0] = (UInt16)-1;
391 }
393 if ((status = LAD_putCommand(&cmd)) != LAD_SUCCESS) {
394 PRINTVERBOSE1(
395 "NameServer_getUInt32: sending LAD command failed, status=%d\n",
396 status)
397 return NameServer_E_FAIL;
398 }
400 if ((status = LAD_getResponse(clHandle, &rsp)) != LAD_SUCCESS) {
401 PRINTVERBOSE1("NameServer_getUInt32: no LAD response, status=%d\n",
402 status)
403 return NameServer_E_FAIL;
404 }
406 val = (UInt32 *)buf;
407 *val = rsp.getUInt32.val;
408 status = rsp.status;
410 PRINTVERBOSE1("NameServer_getUInt32: got LAD response for client %d\n",
411 clHandle)
413 return status;
414 }
416 Int NameServer_remove(NameServer_Handle nsHandle, String name)
417 {
418 Int status;
419 LAD_ClientHandle clHandle;
420 struct LAD_CommandObj cmd;
421 union LAD_ResponseObj rsp;
423 clHandle = LAD_findHandle();
424 if (clHandle == LAD_MAXNUMCLIENTS) {
425 PRINTVERBOSE1(
426 "NameServer_remove: can't find connection to daemon for pid %d\n",
427 getpid())
429 return NameServer_E_RESOURCE;
430 }
432 cmd.cmd = LAD_NAMESERVER_REMOVE;
433 cmd.clientId = clHandle;
434 cmd.args.remove.handle = nsHandle;
435 strncpy(cmd.args.remove.name, name, LAD_MAXENTRYNAMELEN);
437 if ((status = LAD_putCommand(&cmd)) != LAD_SUCCESS) {
438 PRINTVERBOSE1(
439 "NameServer_remove: sending LAD command failed, status=%d\n",
440 status)
441 return NameServer_E_FAIL;
442 }
444 if ((status = LAD_getResponse(clHandle, &rsp)) != LAD_SUCCESS) {
445 PRINTVERBOSE1("NameServer_remove: no LAD response, status=%d\n", status)
446 return NameServer_E_FAIL;
447 }
449 status = rsp.status;
451 PRINTVERBOSE1("NameServer_remove: got LAD response for client %d\n",
452 clHandle)
454 return status;
455 }
457 Int NameServer_removeEntry(NameServer_Handle nsHandle, Ptr entry)
458 {
459 Int status;
460 LAD_ClientHandle clHandle;
461 struct LAD_CommandObj cmd;
462 union LAD_ResponseObj rsp;
464 clHandle = LAD_findHandle();
465 if (clHandle == LAD_MAXNUMCLIENTS) {
466 PRINTVERBOSE1(
467 "NameServer_removeEntry: can't find connection to daemon for pid %d\n",
468 getpid())
470 return NameServer_E_RESOURCE;
471 }
473 cmd.cmd = LAD_NAMESERVER_REMOVEENTRY;
474 cmd.clientId = clHandle;
475 cmd.args.removeEntry.handle = nsHandle;
476 cmd.args.removeEntry.entryPtr = entry;
478 if ((status = LAD_putCommand(&cmd)) != LAD_SUCCESS) {
479 PRINTVERBOSE1(
480 "NameServer_removeEntry: sending LAD command failed, status=%d\n",
481 status)
482 return NameServer_E_FAIL;
483 }
485 if ((status = LAD_getResponse(clHandle, &rsp)) != LAD_SUCCESS) {
486 PRINTVERBOSE1("NameServer_removeEntry: no LAD response, status=%d\n",
487 status)
488 return NameServer_E_FAIL;
489 }
491 status = rsp.status;
493 PRINTVERBOSE1("NameServer_removeEntry: got LAD response for client %d\n",
494 clHandle)
496 return status;
497 }
499 Int NameServer_delete(NameServer_Handle *nsHandle)
500 {
501 Int status;
502 LAD_ClientHandle clHandle;
503 struct LAD_CommandObj cmd;
504 union LAD_ResponseObj rsp;
506 clHandle = LAD_findHandle();
507 if (clHandle == LAD_MAXNUMCLIENTS) {
508 PRINTVERBOSE1(
509 "NameServer_delete: can't find connection to daemon for pid %d\n",
510 getpid())
512 return NameServer_E_RESOURCE;
513 }
515 cmd.cmd = LAD_NAMESERVER_DELETE;
516 cmd.clientId = clHandle;
517 cmd.args.delete.handle = *nsHandle;
519 if ((status = LAD_putCommand(&cmd)) != LAD_SUCCESS) {
520 PRINTVERBOSE1(
521 "NameServer_delete: sending LAD command failed, status=%d\n",
522 status)
523 return NameServer_E_FAIL;
524 }
526 if ((status = LAD_getResponse(clHandle, &rsp)) != LAD_SUCCESS) {
527 PRINTVERBOSE1("NameServer_delete: no LAD response, status=%d\n", status)
528 return NameServer_E_FAIL;
529 }
531 *nsHandle = rsp.delete.handle;
532 status = rsp.status;
534 PRINTVERBOSE1("NameServer_delete: got LAD response for client %d\n",
535 clHandle)
537 return status;
538 }