214c6264612627943c5e500607488ba11093372d
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>
48 /* Common IPC headers: */
49 #include <ti/ipc/Std.h>
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 <_MultiProc.h>
59 #include <_MessageQ.h>
60 #include <_NameServer.h>
62 static LAD_ClientHandle ladHandle;
64 static void cleanup(int arg);
66 /** ============================================================================
67 * Functions
68 * ============================================================================
69 */
70 /* Function to start Ipc */
71 Int Ipc_start (Void)
72 {
73 MessageQ_Config msgqCfg;
74 MultiProc_Config mpCfg;
75 Int32 status;
76 LAD_Status ladStatus;
77 UInt16 rprocId;
78 Int32 attachedAny = 0;
80 /* Catch ctrl-C, and cleanup: */
81 (void) signal(SIGINT, cleanup);
83 ladStatus = LAD_connect(&ladHandle);
84 if (ladStatus != LAD_SUCCESS) {
85 printf("Ipc_start: LAD_connect() failed: %d\n", ladStatus);
86 status = Ipc_E_FAIL;
87 goto exit;
88 }
90 /*
91 * Get MultiProc configuration from LAD and initialize local MultiProc
92 * config structure.
93 */
94 MultiProc_getConfig(&mpCfg);
95 _MultiProc_initCfg(&mpCfg);
97 status = NameServer_setup();
98 if (status >= 0) {
99 MessageQ_getConfig(&msgqCfg);
100 MessageQ_setup(&msgqCfg);
102 /* Now attach to all remote processors, assuming they are up. */
103 for (rprocId = 0; rprocId < MultiProc_getNumProcessors(); rprocId++) {
104 if (0 == rprocId) {
105 /* Skip host, which should always be 0th entry. */
106 continue;
107 }
108 status = MessageQ_attach(rprocId, NULL);
109 if (status == MessageQ_E_RESOURCE) {
110 continue;
111 }
112 if (status < 0) {
113 printf("Ipc_start: MessageQ_attach(%d) failed: %d\n",
114 rprocId, status);
115 status = Ipc_E_FAIL;
117 break;
118 }
119 else {
120 attachedAny = 1;
121 }
122 }
123 if (attachedAny) {
124 status = Ipc_S_SUCCESS;
125 }
126 }
127 else {
128 printf("Ipc_start: NameServer_setup() failed: %d\n", status);
129 status = Ipc_E_FAIL;
130 }
132 exit:
133 return (status);
134 }
137 /* Function to stop Ipc */
138 Int Ipc_stop (Void)
139 {
140 Int32 status = Ipc_S_SUCCESS;
141 LAD_Status ladStatus;
142 UInt16 rprocId;
144 /* Now detach from all remote processors, assuming they are up. */
145 for (rprocId = 0;
146 (rprocId < MultiProc_getNumProcessors()) && (status >= 0);
147 rprocId++) {
148 if (0 == rprocId) {
149 /* Skip host, which should always be 0th entry. */
150 continue;
151 }
152 status = MessageQ_detach(rprocId);
153 if (status < 0) {
154 printf("Ipc_stop: MessageQ_detach(%d) failed: %d\n",
155 rprocId, status);
156 status = Ipc_E_FAIL;
157 goto exit;
158 }
159 }
161 status = MessageQ_destroy();
162 if (status < 0) {
163 printf("Ipc_stop: MessageQ_destroy() failed: %d\n", status);
164 status = Ipc_E_FAIL;
165 goto exit;
166 }
168 status = NameServer_destroy();
169 if (status < 0) {
170 printf("Ipc_stop: NameServer_destroy() failed: %d\n", status);
171 status = Ipc_E_FAIL;
172 goto exit;
173 }
175 ladStatus = LAD_disconnect(ladHandle);
176 if (ladStatus != LAD_SUCCESS) {
177 printf("LAD_disconnect() failed: %d\n", ladStatus);
178 status = Ipc_E_FAIL;
179 goto exit;
180 }
182 exit:
183 return (status);
184 }
186 static void cleanup(int arg)
187 {
188 printf("Ipc: Caught SIGINT, calling Ipc_stop...\n");
189 Ipc_stop();
190 exit(0);
191 }