1 /*
2 * Copyright (c) 2015 Texas Instruments Incorporated - http://www.ti.com
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 Fault.c
34 *
35 * @brief Sample application for fault recovery between MPU and Remote Proc
36 *
37 * ============================================================================
38 */
40 /* Standard headers */
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <unistd.h>
45 /* IPC Headers */
46 #include <ti/ipc/Std.h>
47 #include <ti/ipc/Ipc.h>
48 #include <ti/ipc/MessageQ.h>
50 /* App defines: Must match on remote proc side: */
51 #define HEAPID 0u
52 #define SLAVE_MESSAGEQNAME "SLAVE"
53 #define MPU_MESSAGEQNAME "HOST"
55 #define PROC_ID_DFLT 1 /* Host is zero, remote cores start at 1 */
56 #define NUM_LOOPS_DFLT 100
58 typedef struct SyncMsg {
59 MessageQ_MsgHeader header;
60 UInt32 numLoops; /* also used for msgId */
61 UInt32 print;
62 Int32 faultId;
63 } SyncMsg ;
65 Int MessageQApp_execute(UInt32 numLoops, UInt16 procId, UInt32 faultId)
66 {
67 Int32 status = 0;
68 MessageQ_Msg msg = NULL;
69 MessageQ_Params msgParams;
70 UInt32 i;
71 MessageQ_QueueId queueId = MessageQ_INVALIDMESSAGEQ;
72 MessageQ_Handle msgqHandle;
73 char remoteQueueName[64];
74 UInt32 msgId;
76 printf("Entered MessageQApp_execute\n");
78 /* Create the local Message Queue for receiving. */
79 MessageQ_Params_init(&msgParams);
80 msgqHandle = MessageQ_create(MPU_MESSAGEQNAME, &msgParams);
81 if (msgqHandle == NULL) {
82 printf("Error in MessageQ_create\n");
83 goto exit;
84 }
85 else {
86 printf("Local MessageQId: 0x%x\n", MessageQ_getQueueId(msgqHandle));
87 }
89 sprintf(remoteQueueName, "%s_%s", SLAVE_MESSAGEQNAME,
90 MultiProc_getName(procId));
92 /* Poll until remote side has it's messageQ created before we send: */
93 do {
94 status = MessageQ_open(remoteQueueName, &queueId);
95 sleep (1);
96 } while (status == MessageQ_E_NOTFOUND);
98 if (status < 0) {
99 printf("Error in MessageQ_open [%d]\n", status);
100 goto cleanup;
101 }
102 else {
103 printf("Remote queueId [0x%x]\n", queueId);
104 }
106 msg = MessageQ_alloc(HEAPID, sizeof(SyncMsg));
107 if (msg == NULL) {
108 printf("Error in MessageQ_alloc\n");
109 MessageQ_close(&queueId);
110 goto cleanup;
111 }
113 /* handshake with remote to set the number of loops */
114 MessageQ_setReplyQueue(msgqHandle, msg);
115 ((SyncMsg *)msg)->numLoops = numLoops;
116 ((SyncMsg *)msg)->print = FALSE;
117 MessageQ_put(queueId, msg);
118 MessageQ_get(msgqHandle, &msg, MessageQ_FOREVER);
120 printf("Exchanging %d messages with remote processor %s...\n",
121 numLoops, MultiProc_getName(procId));
123 for (i = 1 ; i <= numLoops; i++) {
124 ((SyncMsg *)msg)->numLoops = i;
125 ((SyncMsg *)msg)->faultId = faultId;
127 /* Have the remote proc reply to this message queue */
128 MessageQ_setReplyQueue(msgqHandle, msg);
130 if (faultId != 0) {
131 printf("About to send fault command, hit ENTER to continue...\n");
132 getchar();
133 }
135 status = MessageQ_put(queueId, msg);
136 if (status < 0) {
137 printf("Error in MessageQ_put [%d]\n", status);
138 MessageQ_free(msg);
139 break;
140 }
142 status = MessageQ_get(msgqHandle, &msg, MessageQ_FOREVER);
144 if (status < 0) {
145 printf("Error in MessageQ_get [%d]\n", status);
146 break;
147 }
148 else {
149 /* validate the returned message */
150 msgId = ((SyncMsg *)msg)->numLoops;
151 if ((msg != NULL) && (msgId != i)) {
152 printf("Data integrity failure!\n"
153 " Expected %d\n"
154 " Received %d\n",
155 i, msgId);
156 break;
157 }
158 }
160 if (numLoops <= 200) {
161 printf("MessageQ_get #%d Msg = 0x%x\n", i, (UInt)msg);
162 }
163 else if ((i % 1000) == 0) {
164 printf("MessageQ_get #%d Msg = 0x%x\n", i, (UInt)msg);
165 }
167 if (faultId == 1) {
168 printf("About to attempt sending data, hit ENTER to continue...\n");
169 getchar();
170 /* Reset fault id for next round */
171 faultId = 0;
172 }
173 }
175 printf("Exchanged %d messages with remote processor %s\n",
176 (i-1), MultiProc_getName(procId));
178 if (status >= 0) {
179 printf("Sample application successfully completed!\n");
180 MessageQ_free(msg);
181 }
183 MessageQ_close(&queueId);
185 cleanup:
186 /* Clean-up */
187 if (MessageQ_delete(&msgqHandle) < 0) {
188 printf("Error in MessageQ_delete [%d]\n", status);
189 }
191 exit:
192 printf("Leaving MessageQApp_execute\n\n");
194 return (status);
195 }
197 int main (int argc, char ** argv)
198 {
199 Int32 status = 0;
200 int opt;
201 UInt32 numLoops = NUM_LOOPS_DFLT;
202 UInt16 procId = PROC_ID_DFLT;
203 UInt32 faultId = 0;
205 while ((opt = getopt(argc, argv, "f:")) != -1) {
206 switch (opt) {
207 case 'f':
208 /*
209 * Argument for -f corresponds to remote-side "fault" commands.
210 * Negative commands cause remote fault before remote MessageQ_put.
211 * Positive commands cause remote fault after remote MessageQ_put.
212 */
213 faultId = atoi(optarg);
214 printf("fault %d will be sent in 1st msg\n", faultId);
215 break;
217 default:
218 fprintf(stderr, "Unknown arg '%s'\n", optarg);
219 return 1;
220 }
221 }
223 /* Parse Args: */
224 switch (argc - optind + 1) {
225 case 1:
226 /* use defaults */
227 break;
228 case 2:
229 numLoops = atoi(argv[optind]);
230 break;
231 case 3:
232 numLoops = atoi(argv[optind]);
233 procId = atoi(argv[optind + 1]);
234 break;
235 default:
236 printf("Usage: %s [<numLoops>] [<ProcId>]\n", argv[0]);
237 printf("\tDefaults: numLoops: %d; ProcId: %d\n",
238 NUM_LOOPS_DFLT, PROC_ID_DFLT);
239 exit(0);
240 }
242 /* IPC initialization */
243 status = Ipc_start();
245 if (status < 0) {
246 printf("Error: Ipc_start failed, error=%d\n", status);
247 goto exit;
248 }
250 if ((procId == 0) || (procId >= MultiProc_getNumProcessors())) {
251 printf("ProcId (%d) must be nonzero and less than %d\n",
252 procId, MultiProc_getNumProcessors());
253 Ipc_stop();
254 exit(0);
255 }
256 printf("Using numLoops: %d; procId : %d\n", numLoops, procId);
258 if (MessageQApp_execute(numLoops, procId, faultId) < 0) {
259 int nAttachAttempts = 1;
261 printf("MessageQApp_execute failed, attempting stop/start)...\n");
262 Ipc_stop();
263 while (Ipc_start() != Ipc_S_SUCCESS) {
264 nAttachAttempts++;
265 if ((nAttachAttempts % 1000) == 0) {
266 printf("Ipc_start() failed\n");
267 }
268 }
269 printf("Ipc_start() succeeded (after %d tries)\n", nAttachAttempts);
271 /* call without fault this time */
272 MessageQApp_execute(numLoops, procId, 0);
273 }
275 Ipc_stop();
277 exit:
278 return (status);
279 }