d91be3e2f3fd07fc0c83254147376de31195a608
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 Ipc.c
34 *
35 * @brief Starts and stops user side Ipc
36 * All setup/destroy APIs on user side will be call from this
37 * module.
38 *
39 * @ver 0002
40 *
41 */
43 /* Standard headers */
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <signal.h>
47 #include <Std.h>
49 /* Common IPC headers: */
50 #include <ti/ipc/Ipc.h>
51 #include <ti/ipc/NameServer.h>
53 /* User side headers */
54 #include <ladclient.h>
56 /* IPC startup/shutdown stuff: */
57 #include <ti/ipc/MultiProc.h>
58 #include <_MessageQ.h>
59 #include <_NameServer.h>
61 static LAD_ClientHandle ladHandle;
63 static void cleanup(int arg);
65 /** ============================================================================
66 * Functions
67 * ============================================================================
68 */
69 /* Function to start Ipc */
70 Int Ipc_start (Void)
71 {
72 MessageQ_Config msgqCfg;
73 Int32 status = Ipc_S_SUCCESS;
74 LAD_Status ladStatus;
75 UInt16 rprocId;
77 /* Catch ctrl-C, and cleanup: */
78 (void) signal(SIGINT, cleanup);
80 ladStatus = LAD_connect(&ladHandle);
81 if (ladStatus != LAD_SUCCESS) {
82 printf("Ipc_start: LAD_connect() failed: %d\n", ladStatus);
83 status = Ipc_E_FAIL;
84 goto exit;
85 }
87 status = NameServer_setup();
88 if (status >= 0) {
89 MessageQ_getConfig(&msgqCfg);
90 MessageQ_setup(&msgqCfg);
92 /* Now attach to all remote processors, assuming they are up. */
93 for (rprocId = 0;
94 (rprocId < MultiProc_getNumProcessors()) && (status >= 0);
95 rprocId++) {
96 if (0 == rprocId) {
97 /* Skip host, which should always be 0th entry. */
98 continue;
99 }
100 status = MessageQ_attach (rprocId, NULL);
101 if (status < 0) {
102 printf("Ipc_start: MessageQ_attach(%d) failed: %d\n",
103 rprocId, status);
104 status = Ipc_E_FAIL;
105 }
106 }
107 }
108 else {
109 printf("Ipc_start: NameServer_setup() failed: %d\n", status);
110 status = Ipc_E_FAIL;
111 }
113 exit:
114 return (status);
115 }
118 /* Function to stop Ipc */
119 Int Ipc_stop (Void)
120 {
121 Int32 status = Ipc_S_SUCCESS;
122 LAD_Status ladStatus;
123 UInt16 rprocId;
125 /* Now detach from all remote processors, assuming they are up. */
126 for (rprocId = 0;
127 (rprocId < MultiProc_getNumProcessors()) && (status >= 0);
128 rprocId++) {
129 if (0 == rprocId) {
130 /* Skip host, which should always be 0th entry. */
131 continue;
132 }
133 status = MessageQ_detach(rprocId);
134 if (status < 0) {
135 printf("Ipc_stop: MessageQ_detach(%d) failed: %d\n",
136 rprocId, status);
137 status = Ipc_E_FAIL;
138 goto exit;
139 }
140 }
142 status = MessageQ_destroy();
143 if (status < 0) {
144 printf("Ipc_stop: MessageQ_destroy() failed: %d\n", status);
145 status = Ipc_E_FAIL;
146 goto exit;
147 }
149 status = NameServer_destroy();
150 if (status < 0) {
151 printf("Ipc_stop: NameServer_destroy() failed: %d\n", status);
152 status = Ipc_E_FAIL;
153 goto exit;
154 }
156 ladStatus = LAD_disconnect(ladHandle);
157 if (ladStatus != LAD_SUCCESS) {
158 printf("LAD_disconnect() failed: %d\n", ladStatus);
159 status = Ipc_E_FAIL;
160 goto exit;
161 }
163 exit:
164 return (status);
165 }
167 static void cleanup(int arg)
168 {
169 printf("Ipc: Caught SIGINT, calling Ipc_stop...\n");
170 Ipc_stop();
171 exit(0);
172 }