]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - ipc/ipcdev.git/blob - linux/src/api/Ipc.c
Changes to support non-NameServer-capable slaves on Linux
[ipc/ipcdev.git] / linux / src / api / Ipc.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       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 <_MessageQ.h>
59 #include <_NameServer.h>
61 MultiProc_Config _MultiProc_cfg;
63 static LAD_ClientHandle ladHandle;
65 static void cleanup(int arg);
67 /** ============================================================================
68  *  Functions
69  *  ============================================================================
70  */
71 /* Function to start Ipc */
72 Int Ipc_start (Void)
73 {
74     MessageQ_Config   msgqCfg;
75     MultiProc_Config  mpCfg;
76     Int32             status;
77     LAD_Status        ladStatus;
78     UInt16            rprocId;
79     Int32             attachedAny = 0;
81     /* Catch ctrl-C, and cleanup: */
82     (void) signal(SIGINT, cleanup);
84     ladStatus = LAD_connect(&ladHandle);
85     if (ladStatus != LAD_SUCCESS) {
86         printf("Ipc_start: LAD_connect() failed: %d\n", ladStatus);
87         status = Ipc_E_FAIL;
88         goto exit;
89     }
91     /* Setup and get MultiProc configuration from LAD */
92     MultiProc_getConfig(&mpCfg);
93     _MultiProc_cfg = mpCfg;
95     status = NameServer_setup();
96     if (status >= 0) {
97         MessageQ_getConfig(&msgqCfg);
98         MessageQ_setup(&msgqCfg);
100         /* Now attach to all remote processors, assuming they are up. */
101         for (rprocId = 0; rprocId < MultiProc_getNumProcessors(); rprocId++) {
102             if (0 == rprocId) {
103                 /* Skip host, which should always be 0th entry. */
104                 continue;
105             }
106             status = MessageQ_attach(rprocId, NULL);
107             if (status == MessageQ_E_RESOURCE) {
108                 continue;
109             }
110             if (status < 0) {
111                 printf("Ipc_start: MessageQ_attach(%d) failed: %d\n",
112                        rprocId, status);
113                 status = Ipc_E_FAIL;
115                 break;
116             }
117             else {
118                 attachedAny = 1;
119             }
120         }
121         if (attachedAny) {
122             status = Ipc_S_SUCCESS;
123         }
124     }
125     else {
126         printf("Ipc_start: NameServer_setup() failed: %d\n", status);
127         status = Ipc_E_FAIL;
128     }
130 exit:
131     return (status);
135 /* Function to stop Ipc */
136 Int Ipc_stop (Void)
138     Int32             status = Ipc_S_SUCCESS;
139     LAD_Status        ladStatus;
140     UInt16            rprocId;
142     /* Now detach from all remote processors, assuming they are up. */
143     for (rprocId = 0;
144          (rprocId < MultiProc_getNumProcessors()) && (status >= 0);
145          rprocId++) {
146         if (0 == rprocId) {
147           /* Skip host, which should always be 0th entry. */
148           continue;
149         }
150         status = MessageQ_detach(rprocId);
151         if (status < 0) {
152             printf("Ipc_stop: MessageQ_detach(%d) failed: %d\n",
153                 rprocId, status);
154             status = Ipc_E_FAIL;
155             goto exit;
156        }
157     }
159     status = MessageQ_destroy();
160     if (status < 0) {
161         printf("Ipc_stop: MessageQ_destroy() failed: %d\n", status);
162         status = Ipc_E_FAIL;
163         goto exit;
164     }
166     status = NameServer_destroy();
167     if (status < 0) {
168         printf("Ipc_stop: NameServer_destroy() failed: %d\n", status);
169         status = Ipc_E_FAIL;
170         goto exit;
171     }
173     ladStatus = LAD_disconnect(ladHandle);
174     if (ladStatus != LAD_SUCCESS) {
175         printf("LAD_disconnect() failed: %d\n", ladStatus);
176         status = Ipc_E_FAIL;
177         goto exit;
178     }
180 exit:
181     return (status);
184 static void cleanup(int arg)
186     printf("Ipc: Caught SIGINT, calling Ipc_stop...\n");
187     Ipc_stop();
188     exit(0);