1 /*
2 * Copyright (c) 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 <ti/syslink/inc/usr/Qnx/IpcDrv.h>
55 //#include <SysLink.h>
57 /* IPC startup/shutdown stuff: */
58 #include <ti/ipc/MultiProc.h>
59 #include <ti/ipc/MessageQ.h>
60 #include <ti/ipc/NameServer.h>
61 #include <_MessageQ.h>
62 #include <_NameServer.h>
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 Int32 status = Ipc_S_SUCCESS;
75 UInt16 rprocId;
77 /* Catch ctrl-C, and cleanup: */
78 (void) signal(SIGINT, cleanup);
80 status = IpcDrv_open();
81 if (status < 0) {
82 printf("Ipc_start: IpcDrv_open() failed: %d\n", status);
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 UInt16 rprocId;
124 /* Now detach from all remote processors, assuming they are up. */
125 for (rprocId = 0;
126 (rprocId < MultiProc_getNumProcessors()) && (status >= 0);
127 rprocId++) {
128 if (0 == rprocId) {
129 /* Skip host, which should always be 0th entry. */
130 continue;
131 }
132 status = MessageQ_detach(rprocId);
133 if (status < 0) {
134 printf("Ipc_stop: MessageQ_detach(%d) failed: %d\n",
135 rprocId, status);
136 status = Ipc_E_FAIL;
137 goto exit;
138 }
139 }
141 status = MessageQ_destroy();
142 if (status < 0) {
143 printf("Ipc_stop: MessageQ_destroy() failed: %d\n", status);
144 status = Ipc_E_FAIL;
145 goto exit;
146 }
148 status = NameServer_destroy();
149 if (status < 0) {
150 printf("Ipc_stop: NameServer_destroy() failed: %d\n", status);
151 status = Ipc_E_FAIL;
152 goto exit;
153 }
155 IpcDrv_close();
157 exit:
158 return (status);
159 }
161 static void cleanup(int arg)
162 {
163 printf("Ipc: Caught SIGINT, calling Ipc_stop...\n");
164 Ipc_stop();
165 exit(0);
166 }