]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - ipc/ipcdev.git/blob - linux/src/tests/MessageQApp.c
dc3444c0934b979e212dc109083fa16e58123a5b
[ipc/ipcdev.git] / linux / src / tests / MessageQApp.c
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   MessageQApp.c
34  *
35  *  @brief  Sample application for MessageQ module between MPU and Remote Proc
36  *
37  *  ============================================================================
38  */
40 /* Standard headers */
41 #include <stdio.h>
42 #include <stdlib.h>
44 /* IPC Headers */
45 #include <ti/ipc/Std.h>
46 #include <ti/ipc/Ipc.h>
47 #include <ti/ipc/MessageQ.h>
49 /* App defines:  Must match on remote proc side: */
50 #define HEAPID              0u
51 #define SLAVE_MESSAGEQNAME  "SLAVE"
52 #define MPU_MESSAGEQNAME    "HOST"
54 #define PROC_ID_DFLT        1     /* Host is zero, remote cores start at 1 */
55 #define NUM_LOOPS_DFLT   100
57 typedef struct SyncMsg {
58     MessageQ_MsgHeader header;
59     UInt32 numLoops;  /* also used for msgId */
60     UInt32 print;
61 } SyncMsg ;
63 Int MessageQApp_execute(UInt32 numLoops, UInt16 procId)
64 {
65     Int32                    status = 0;
66     MessageQ_Msg             msg = NULL;
67     MessageQ_Params          msgParams;
68     UInt32                   i;
69     MessageQ_QueueId         queueId = MessageQ_INVALIDMESSAGEQ;
70     MessageQ_Handle          msgqHandle;
71     char                     remoteQueueName[64];
72     UInt32                   msgId;
74     printf("Entered MessageQApp_execute\n");
76     /* Create the local Message Queue for receiving. */
77     MessageQ_Params_init(&msgParams);
78     msgqHandle = MessageQ_create(MPU_MESSAGEQNAME, &msgParams);
79     if (msgqHandle == NULL) {
80         printf("Error in MessageQ_create\n");
81         goto exit;
82     }
83     else {
84         printf("Local MessageQId: 0x%x\n", MessageQ_getQueueId(msgqHandle));
85     }
87     sprintf(remoteQueueName, "%s_%s", SLAVE_MESSAGEQNAME,
88              MultiProc_getName(procId));
90     /* Poll until remote side has it's messageQ created before we send: */
91     do {
92         status = MessageQ_open(remoteQueueName, &queueId);
93         sleep (1);
94     } while (status == MessageQ_E_NOTFOUND);
96     if (status < 0) {
97         printf("Error in MessageQ_open [%d]\n", status);
98         goto cleanup;
99     }
100     else {
101         printf("Remote queueId  [0x%x]\n", queueId);
102     }
104     msg = MessageQ_alloc(HEAPID, sizeof(SyncMsg));
105     if (msg == NULL) {
106         printf("Error in MessageQ_alloc\n");
107         MessageQ_close(&queueId);
108         goto cleanup;
109     }
111     /* handshake with remote to set the number of loops */
112     MessageQ_setReplyQueue(msgqHandle, msg);
113     ((SyncMsg *)msg)->numLoops = numLoops;
114     ((SyncMsg *)msg)->print = FALSE;
115     MessageQ_put(queueId, msg);
116     MessageQ_get(msgqHandle, &msg, MessageQ_FOREVER);
118     printf("Exchanging %d messages with remote processor %s...\n",
119            numLoops, MultiProc_getName(procId));
121     for (i = 1 ; i <= numLoops; i++) {
122         ((SyncMsg *)msg)->numLoops = i;
124         /* Have the remote proc reply to this message queue */
125         MessageQ_setReplyQueue(msgqHandle, msg);
127         status = MessageQ_put(queueId, msg);
128         if (status < 0) {
129             printf("Error in MessageQ_put [%d]\n", status);
130             break;
131         }
133         status = MessageQ_get(msgqHandle, &msg, MessageQ_FOREVER);
135         if (status < 0) {
136             printf("Error in MessageQ_get [%d]\n", status);
137             break;
138         }
139         else {
140             /* validate the returned message */
141             msgId = ((SyncMsg *)msg)->numLoops;
142             if ((msg != NULL) && (msgId != i)) {
143                 printf("Data integrity failure!\n"
144                         "    Expected %d\n"
145                         "    Received %d\n",
146                         i, msgId);
147                 break;
148             }
149         }
151         if (numLoops <= 200) {
152             printf("MessageQ_get #%d Msg = 0x%x\n", i, (UInt)msg);
153         }
154         else if ((i % 1000) == 0) {
155             printf("MessageQ_get #%d Msg = 0x%x\n", i, (UInt)msg);
156         }
157     }
159     printf("Exchanged %d messages with remote processor %s\n",
160         (i-1), MultiProc_getName(procId));
162     if (status >= 0) {
163        printf("Sample application successfully completed!\n");
164     }
166     MessageQ_free(msg);
167     MessageQ_close(&queueId);
169 cleanup:
170     /* Clean-up */
171     status = MessageQ_delete(&msgqHandle);
172     if (status < 0) {
173         printf("Error in MessageQ_delete [%d]\n", status);
174     }
176 exit:
177     printf("Leaving MessageQApp_execute\n\n");
179     return (status);
182 int main (int argc, char ** argv)
184     Int32 status = 0;
185     UInt32 numLoops = NUM_LOOPS_DFLT;
186     UInt16 procId = PROC_ID_DFLT;
188     /* Parse Args: */
189     switch (argc) {
190         case 1:
191            /* use defaults */
192            break;
193         case 2:
194            numLoops   = atoi(argv[1]);
195            break;
196         case 3:
197            numLoops   = atoi(argv[1]);
198            procId     = atoi(argv[2]);
199            break;
200         default:
201            printf("Usage: %s [<numLoops>] [<ProcId>]\n", argv[0]);
202            printf("\tDefaults: numLoops: %d; ProcId: %d\n",
203                    NUM_LOOPS_DFLT, PROC_ID_DFLT);
204            exit(0);
205     }
207     status = Ipc_start();
209     if (procId >= MultiProc_getNumProcessors()) {
210         printf("ProcId must be less than %d\n", MultiProc_getNumProcessors());
211         Ipc_stop();
212         exit(0);
213     }
214     printf("Using numLoops: %d; procId : %d\n", numLoops, procId);
216     if (status >= 0) {
217         MessageQApp_execute(numLoops, procId);
218         Ipc_stop();
219     }
220     else {
221         printf("Ipc_start failed: status = %d\n", status);
222     }
224     return(0);