1 /*
2 * Copyright (c) 2012, 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_addUInt32(NameServer_Handle nsHandle, String name, UInt32 value)
220 {
221 Int status;
222 LAD_ClientHandle clHandle;
223 struct LAD_CommandObj cmd;
224 union LAD_ResponseObj rsp;
226 clHandle = LAD_findHandle();
227 if (clHandle == LAD_MAXNUMCLIENTS) {
228 PRINTVERBOSE1(
229 "NameServer_addUInt32: can't find connection to daemon for pid %d\n",
230 getpid())
232 return NULL;
233 }
235 cmd.cmd = LAD_NAMESERVER_ADDUINT32;
236 cmd.clientId = clHandle;
237 cmd.args.addUInt32.handle = nsHandle;
238 strncpy(cmd.args.addUInt32.name, name, NameServer_Params_MAXNAMELEN);
239 cmd.args.addUInt32.val = value;
241 if ((status = LAD_putCommand(&cmd)) != LAD_SUCCESS) {
242 PRINTVERBOSE1(
243 "NameServer_addUInt32: sending LAD command failed, status=%d\n",
244 status)
245 return NULL;
246 }
248 if ((status = LAD_getResponse(clHandle, &rsp)) != LAD_SUCCESS) {
249 PRINTVERBOSE1(
250 "NameServer_addUInt32: no LAD response, status=%d\n", status)
251 return NULL;
252 }
254 PRINTVERBOSE1(
255 "NameServer_addUInt32: got LAD response for client %d\n", clHandle)
257 return rsp.entryPtr;
258 }
260 Int NameServer_getUInt32(NameServer_Handle nsHandle, String name, Ptr buf,
261 UInt16 procId[])
262 {
263 Int status;
264 LAD_ClientHandle clHandle;
265 UInt32 *val;
266 struct LAD_CommandObj cmd;
267 union LAD_ResponseObj rsp;
269 clHandle = LAD_findHandle();
270 if (clHandle == LAD_MAXNUMCLIENTS) {
271 PRINTVERBOSE1(
272 "NameServer_getUInt32: can't find connection to daemon for pid %d\n",
273 getpid())
275 return NameServer_E_RESOURCE;
276 }
278 cmd.cmd = LAD_NAMESERVER_GETUINT32;
279 cmd.clientId = clHandle;
280 cmd.args.getUInt32.handle = nsHandle;
281 strncpy(cmd.args.getUInt32.name, name, NameServer_Params_MAXNAMELEN);
282 if (procId != NULL) {
283 memcpy(cmd.args.getUInt32.procId, procId,
284 sizeof(UInt16) * MultiProc_MAXPROCESSORS);
285 }
286 else {
287 cmd.args.getUInt32.procId[0] = (UInt16)-1;
288 }
290 if ((status = LAD_putCommand(&cmd)) != LAD_SUCCESS) {
291 PRINTVERBOSE1(
292 "NameServer_getUInt32: sending LAD command failed, status=%d\n",
293 status)
294 return NameServer_E_FAIL;
295 }
297 if ((status = LAD_getResponse(clHandle, &rsp)) != LAD_SUCCESS) {
298 PRINTVERBOSE1("NameServer_getUInt32: no LAD response, status=%d\n",
299 status)
300 return NameServer_E_FAIL;
301 }
303 val = (UInt32 *)buf;
304 *val = rsp.getUInt32.val;
305 status = rsp.status;
307 PRINTVERBOSE1("NameServer_getUInt32: got LAD response for client %d\n",
308 clHandle)
310 return status;
311 }
313 Int NameServer_remove(NameServer_Handle nsHandle, String name)
314 {
315 Int status;
316 LAD_ClientHandle clHandle;
317 struct LAD_CommandObj cmd;
318 union LAD_ResponseObj rsp;
320 clHandle = LAD_findHandle();
321 if (clHandle == LAD_MAXNUMCLIENTS) {
322 PRINTVERBOSE1(
323 "NameServer_remove: can't find connection to daemon for pid %d\n",
324 getpid())
326 return NameServer_E_RESOURCE;
327 }
329 cmd.cmd = LAD_NAMESERVER_REMOVE;
330 cmd.clientId = clHandle;
331 cmd.args.remove.handle = nsHandle;
332 strncpy(cmd.args.remove.name, name, NameServer_Params_MAXNAMELEN);
334 if ((status = LAD_putCommand(&cmd)) != LAD_SUCCESS) {
335 PRINTVERBOSE1(
336 "NameServer_remove: sending LAD command failed, status=%d\n",
337 status)
338 return NameServer_E_FAIL;
339 }
341 if ((status = LAD_getResponse(clHandle, &rsp)) != LAD_SUCCESS) {
342 PRINTVERBOSE1("NameServer_remove: no LAD response, status=%d\n", status)
343 return NameServer_E_FAIL;
344 }
346 status = rsp.status;
348 PRINTVERBOSE1("NameServer_remove: got LAD response for client %d\n",
349 clHandle)
351 return status;
352 }
354 Int NameServer_removeEntry(NameServer_Handle nsHandle, Ptr entry)
355 {
356 Int status;
357 LAD_ClientHandle clHandle;
358 struct LAD_CommandObj cmd;
359 union LAD_ResponseObj rsp;
361 clHandle = LAD_findHandle();
362 if (clHandle == LAD_MAXNUMCLIENTS) {
363 PRINTVERBOSE1(
364 "NameServer_removeEntry: can't find connection to daemon for pid %d\n",
365 getpid())
367 return NameServer_E_RESOURCE;
368 }
370 cmd.cmd = LAD_NAMESERVER_REMOVEENTRY;
371 cmd.clientId = clHandle;
372 cmd.args.removeEntry.handle = nsHandle;
373 cmd.args.removeEntry.entryPtr = entry;
375 if ((status = LAD_putCommand(&cmd)) != LAD_SUCCESS) {
376 PRINTVERBOSE1(
377 "NameServer_removeEntry: sending LAD command failed, status=%d\n",
378 status)
379 return NameServer_E_FAIL;
380 }
382 if ((status = LAD_getResponse(clHandle, &rsp)) != LAD_SUCCESS) {
383 PRINTVERBOSE1("NameServer_removeEntry: no LAD response, status=%d\n",
384 status)
385 return NameServer_E_FAIL;
386 }
388 status = rsp.status;
390 PRINTVERBOSE1("NameServer_removeEntry: got LAD response for client %d\n",
391 clHandle)
393 return status;
394 }
396 Int NameServer_delete(NameServer_Handle *nsHandle)
397 {
398 Int status;
399 LAD_ClientHandle clHandle;
400 struct LAD_CommandObj cmd;
401 union LAD_ResponseObj rsp;
403 clHandle = LAD_findHandle();
404 if (clHandle == LAD_MAXNUMCLIENTS) {
405 PRINTVERBOSE1(
406 "NameServer_delete: can't find connection to daemon for pid %d\n",
407 getpid())
409 return NameServer_E_RESOURCE;
410 }
412 cmd.cmd = LAD_NAMESERVER_DELETE;
413 cmd.clientId = clHandle;
414 cmd.args.delete.handle = *nsHandle;
416 if ((status = LAD_putCommand(&cmd)) != LAD_SUCCESS) {
417 PRINTVERBOSE1(
418 "NameServer_delete: sending LAD command failed, status=%d\n",
419 status)
420 return NameServer_E_FAIL;
421 }
423 if ((status = LAD_getResponse(clHandle, &rsp)) != LAD_SUCCESS) {
424 PRINTVERBOSE1("NameServer_delete: no LAD response, status=%d\n", status)
425 return NameServer_E_FAIL;
426 }
428 *nsHandle = rsp.delete.handle;
429 status = rsp.status;
431 PRINTVERBOSE1("NameServer_delete: got LAD response for client %d\n",
432 clHandle)
434 return status;
435 }