]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - ipc/ipcdev.git/blob - linux/src/tests/fault.c
1c97c26617e8665bd211ed292a37ccc33c0f8d0e
[ipc/ipcdev.git] / linux / src / tests / fault.c
1 /*
2  * Copyright (c) 2012-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>
49 #include <ti/ipc/transports/TransportRpmsg.h>
51 /* App defines:  Must match on remote proc side: */
52 #define HEAPID              0u
53 #define SLAVE_MESSAGEQNAME  "SLAVE"
54 #define MPU_MESSAGEQNAME    "HOST"
56 #define PROC_ID_DFLT        1     /* Host is zero, remote cores start at 1 */
57 #define NUM_LOOPS_DFLT   100
59 typedef struct SyncMsg {
60     MessageQ_MsgHeader header;
61     UInt32 numLoops;  /* also used for msgId */
62     UInt32 print;
63     Int32 faultId;
64 } SyncMsg ;
66 Int MessageQApp_execute(UInt32 numLoops, UInt16 procId, UInt32 faultId)
67 {
68     Int32                    status = 0;
69     MessageQ_Msg             msg = NULL;
70     MessageQ_Params          msgParams;
71     UInt32                   i;
72     MessageQ_QueueId         queueId = MessageQ_INVALIDMESSAGEQ;
73     MessageQ_Handle          msgqHandle;
74     char                     remoteQueueName[64];
75     UInt32                   msgId;
77     printf("Entered MessageQApp_execute\n");
79     /* Create the local Message Queue for receiving. */
80     MessageQ_Params_init(&msgParams);
81     msgqHandle = MessageQ_create(MPU_MESSAGEQNAME, &msgParams);
82     if (msgqHandle == NULL) {
83         printf("Error in MessageQ_create\n");
84         goto exit;
85     }
86     else {
87         printf("Local MessageQId: 0x%x\n", MessageQ_getQueueId(msgqHandle));
88     }
90     sprintf(remoteQueueName, "%s_%s", SLAVE_MESSAGEQNAME,
91              MultiProc_getName(procId));
93     /* Poll until remote side has it's messageQ created before we send: */
94     do {
95         status = MessageQ_open(remoteQueueName, &queueId);
96         sleep (1);
97     } while (status == MessageQ_E_NOTFOUND);
99     if (status < 0) {
100         printf("Error in MessageQ_open [%d]\n", status);
101         goto cleanup;
102     }
103     else {
104         printf("Remote queueId  [0x%x]\n", queueId);
105     }
107     msg = MessageQ_alloc(HEAPID, sizeof(SyncMsg));
108     if (msg == NULL) {
109         printf("Error in MessageQ_alloc\n");
110         MessageQ_close(&queueId);
111         goto cleanup;
112     }
114     /* handshake with remote to set the number of loops */
115     MessageQ_setReplyQueue(msgqHandle, msg);
116     ((SyncMsg *)msg)->numLoops = numLoops;
117     ((SyncMsg *)msg)->print = FALSE;
118     MessageQ_put(queueId, msg);
119     MessageQ_get(msgqHandle, &msg, MessageQ_FOREVER);
121     printf("Exchanging %d messages with remote processor %s...\n",
122            numLoops, MultiProc_getName(procId));
124     for (i = 1 ; i <= numLoops; i++) {
125         ((SyncMsg *)msg)->numLoops = i;
126         ((SyncMsg *)msg)->faultId = faultId;
128         /* Have the remote proc reply to this message queue */
129         MessageQ_setReplyQueue(msgqHandle, msg);
131         if (faultId != 0) {
132             printf("About to send fault command, hit ENTER to continue...\n");
133             getchar();
134         }
136         status = MessageQ_put(queueId, msg);
137         if (status < 0) {
138             printf("Error in MessageQ_put [%d]\n", status);
139             MessageQ_free(msg);
140             break;
141         }
143         status = MessageQ_get(msgqHandle, &msg, MessageQ_FOREVER);
145         if (status < 0) {
146             printf("Error in MessageQ_get [%d]\n", status);
147             break;
148         }
149         else {
150             /* validate the returned message */
151             msgId = ((SyncMsg *)msg)->numLoops;
152             if ((msg != NULL) && (msgId != i)) {
153                 printf("Data integrity failure!\n"
154                         "    Expected %d\n"
155                         "    Received %d\n",
156                         i, msgId);
157                 break;
158             }
159         }
161         if (numLoops <= 200) {
162             printf("MessageQ_get #%d Msg = 0x%x\n", i, (UInt)msg);
163         }
164         else if ((i % 1000) == 0) {
165             printf("MessageQ_get #%d Msg = 0x%x\n", i, (UInt)msg);
166         }
167     }
169     printf("Exchanged %d messages with remote processor %s\n",
170         (i-1), MultiProc_getName(procId));
172     if (status >= 0) {
173         printf("Sample application successfully completed!\n");
174         MessageQ_free(msg);
175     }
177     MessageQ_close(&queueId);
179 cleanup:
180     /* Clean-up */
181     if (MessageQ_delete(&msgqHandle) < 0) {
182         printf("Error in MessageQ_delete [%d]\n", status);
183     }
185 exit:
186     printf("Leaving MessageQApp_execute\n\n");
188     return (status);
191 int main (int argc, char ** argv)
193     Int status = 0;
194     int opt;
195     UInt numLoops = NUM_LOOPS_DFLT;
196     UInt16 procId = PROC_ID_DFLT;
197     UInt32 faultId = 0;
199     while ((opt = getopt(argc, argv, "f:")) != -1) {
200         switch (opt) {
201           case 'f':
202             /*
203              * Argument for -f corresponds to remote-side "fault" commands.
204              * Negative commands cause remote fault before remote MessageQ_put.
205              * Positive commands cause remote fault after remote MessageQ_put.
206              */
207             faultId = atoi(optarg);
208             printf("fault %d will be sent in 1st msg\n", faultId);
209             break;
211           default:
212             fprintf(stderr, "Unknown arg '%s'\n", optarg);
213             return 1;
214         }
215     }
217     /* Parse Args: */
218     switch (argc - optind + 1) {
219         case 1:
220            /* use defaults */
221            break;
222         case 2:
223            numLoops   = atoi(argv[optind]);
224            break;
225         case 3:
226            numLoops   = atoi(argv[optind]);
227            procId     = atoi(argv[optind + 1]);
228            break;
229         default:
230            printf("Usage: %s [<numLoops>] [<ProcId>]\n", argv[0]);
231            printf("\tDefaults: numLoops: %d; ProcId: %d\n",
232                    NUM_LOOPS_DFLT, PROC_ID_DFLT);
233            exit(0);
234     }
236     /* configure the transport factory */
237     Ipc_transportConfig(&TransportRpmsg_Factory);
239     /* IPC initialization */
240     status = Ipc_start();
242     if (status < 0) {
243         printf("Error: Ipc_start failed, error=%d\n", status);
244         goto exit;
245     }
247     if ((procId == 0) || (procId >= MultiProc_getNumProcessors())) {
248         printf("ProcId (%d) must be nonzero and less than %d\n",
249                 procId, MultiProc_getNumProcessors());
250         Ipc_stop();
251         exit(0);
252     }
253     printf("Using numLoops: %d; procId : %d\n", numLoops, procId);
255     if (MessageQApp_execute(numLoops, procId, faultId) < 0) {
256         int nAttachAttempts = 0;
258         printf("MessageQApp_execute failed, attempting detach/attach...\n");
259         Ipc_detach(procId);
260         while (Ipc_attach(procId) != Ipc_S_SUCCESS) {
261             nAttachAttempts++;
262             if ((nAttachAttempts % 1000) == 0) {
263                 printf("Ipc_attach(%d) failed\n", procId);
264             }
265         }
266         printf("Ipc_attach(%d) succeeded (after %d tries)\n", procId, nAttachAttempts);
268         /* call without fault this time */
269         MessageQApp_execute(numLoops, procId, 0);
270     }
272     Ipc_stop();
274 exit:
275     return (status);