OpenCores
URL https://opencores.org/ocsvn/funbase_ip_library/funbase_ip_library/trunk

Subversion Repositories funbase_ip_library

[/] [funbase_ip_library/] [trunk/] [TUT/] [ip.swp.api/] [openmcapi/] [1.0/] [libmcapi/] [mcapi/] [mcapi_init.c] - Blame information for rev 145

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 145 lanttu
/*
2
 * Copyright (c) 2010, Mentor Graphics Corporation
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 are met:
7
 *
8
 * 1. Redistributions of source code must retain the above copyright notice,
9
 *    this list of conditions and the following disclaimer.
10
 * 2. Redistributions in binary form must reproduce the above copyright notice,
11
 *    this list of conditions and the following disclaimer in the documentation
12
 *    and/or other materials provided with the distribution.
13
 * 3. Neither the name of the <ORGANIZATION> nor the names of its contributors
14
 *    may be used to endorse or promote products derived from this software
15
 *    without specific prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
 * POSSIBILITY OF SUCH DAMAGE.
28
 */
29
 
30
 
31
 
32
#include <openmcapi.h>
33
#include <udp_lite.h>
34
 
35
mcapi_node_t            MCAPI_Node_ID;
36
mcapi_uint8_t           MCAPI_Init = 0;
37
MCAPI_BUF_QUEUE         MCAPI_Buf_Wait_List;
38
mcapi_request_t         MCAPI_Free_Request_Structs[MCAPI_FREE_REQUEST_COUNT];
39
mcapi_endpoint_t        MCAPI_CTRL_TX_Endp;
40
MCAPI_BUF_QUEUE         MCAPI_RX_Queue[MCAPI_PRIO_COUNT];
41
MCAPI_MUTEX             MCAPI_RX_Lock;
42
mcapi_endpoint_t        MCAPI_CTRL_RX_Endp;
43
mcapi_uint16_t          MCAPI_Next_Port;
44
 
45
extern MCAPI_RT_INIT_STRUCT MCAPI_Route_List[];
46
 
47
/*************************************************************************
48
*
49
*   FUNCTION
50
*
51
*       mcapi_initialize
52
*
53
*   DESCRIPTION
54
*
55
*       API routine to initialize an MCAPI node.  This routine must be
56
*       called by each node using MCAPI.  This routine must be called only
57
*       once per node.
58
*
59
*   INPUTS
60
*
61
*       node_id                 The system-wide unique node ID for the
62
*                               node being initialized.
63
*       *mcapi_version          A pointer to memory that will be filled
64
*                               in with the MCAPI implemenation version
65
*                               value for the system.
66
*       *mcapi_status           A pointer to memory that will be filled in
67
*                               with the status of the call.
68
*
69
*   OUTPUTS
70
*
71
*       None.
72
*
73
*************************************************************************/
74
void mcapi_initialize(mcapi_node_t node_id, mcapi_version_t *mcapi_version,
75
                      mcapi_status_t *mcapi_status)
76
{
77
    MCAPI_GLOBAL_DATA   *node_data;
78
    int                 i, j, k;
79
 
80
    // initialize udp connection
81
    udp_init();
82
    /* Ensure mcapi_status is valid. */
83
    if (mcapi_status)
84
    {
85
        /* Validate the version pointer. */
86
        if (mcapi_version)
87
        {
88
            /* If the node has not already been initialized. */
89
            if (MCAPI_Init == 0)
90
            {
91
                /* Initialize status to success. */
92
                *mcapi_status = MCAPI_SUCCESS;
93
 
94
                /* Initialize the data for this node. */
95
                mcapi_init_node_data();
96
 
97
                /* Get the lock. */
98
                mcapi_lock_node_data();
99
 
100
                /* Initialize the wait list. */
101
                MCAPI_Buf_Wait_List.head = MCAPI_NULL;
102
                MCAPI_Buf_Wait_List.tail = MCAPI_NULL;
103
 
104
                /* Set each global request structure in the system to
105
                 * available.
106
                 */
107
                for (i = 0; i < MCAPI_FREE_REQUEST_COUNT; i++)
108
                {
109
                    /* Clear out the structure. */
110
                    memset(&MCAPI_Free_Request_Structs[i], 0,
111
                           sizeof(mcapi_request_t));
112
                }
113
 
114
                /* Initialize the receive queue that holds pending
115
                 * incoming messages.
116
                 */
117
 
118
                for (i = 0; i < MCAPI_PRIO_COUNT; i++)
119
                {
120
                    MCAPI_RX_Queue[i].head = MCAPI_NULL;
121
                    MCAPI_RX_Queue[i].tail = MCAPI_NULL;
122
                }
123
 
124
                /* Get a pointer to the global node list. */
125
                node_data = mcapi_get_node_data();
126
 
127
                /* Find the next available entry in the global list
128
                 * of nodes.
129
                 */
130
                for (i = 0; i < MCAPI_NODE_COUNT; i++)
131
                {
132
                    if (node_data->mcapi_node_list[i].mcapi_state ==
133
                        MCAPI_NODE_UNUSED)
134
                        break;
135
                }
136
 
137
                /* If a free entry was found. */
138
                if (i != MCAPI_NODE_COUNT)
139
                {
140
                    /* Ensure this node ID has not already been used. */
141
                    if (mcapi_find_node(node_id, node_data) == -1)
142
                    {
143
                        /* Increment the number of nodes in the system. */
144
                        node_data->mcapi_node_count ++;
145
 
146
                        /* Fill in the Node ID. */
147
                        node_data->mcapi_node_list[i].mcapi_node_id =
148
                            node_id;
149
 
150
                        /* Set the global node ID parameter for this node. */
151
                        MCAPI_Node_ID = node_id;
152
 
153
                        /* Set the state to MCAPI_NODE_INITIALIZED. */
154
                        node_data->mcapi_node_list[i].mcapi_state =
155
                            MCAPI_NODE_INITIALIZED;
156
 
157
                        /* Initialize the next port value to the configured
158
                         * first available port.
159
                         */
160
                        MCAPI_Next_Port = MCAPI_ENDP_PORT_INIT;
161
 
162
                        /* Initialize the open endpoint count to zero. */
163
                        node_data->mcapi_node_list[i].mcapi_endpoint_count = 0;
164
 
165
                        /* Initialize each endpoint. */
166
                        for (j = 0; j < MCAPI_MAX_ENDPOINTS; j++)
167
                        {
168
                            /* Set the initial state. */
169
                            node_data->mcapi_node_list[i].mcapi_endpoint_list[j].
170
                                mcapi_state = MCAPI_ENDP_CLOSED;
171
 
172
                            /* Set the head and tail of the RX queue. */
173
                            node_data->mcapi_node_list[i].mcapi_endpoint_list[j].
174
                                mcapi_rx_queue.head = MCAPI_NULL;
175
 
176
                            node_data->mcapi_node_list[i].mcapi_endpoint_list[j].
177
                                mcapi_rx_queue.tail = MCAPI_NULL;
178
                        }
179
 
180
                        /* Set the port to which other nodes should send status
181
                         * messages.
182
                         */
183
                        node_data->mcapi_node_list[i].mcapi_status_port =
184
                            MCAPI_RX_CONTROL_PORT;
185
 
186
                        /* Create an endpoint for receiving control messages.  This
187
                         * call will only fail if the node is configured as a router
188
                         * only, in which case it is OK to fail, as the control port
189
                         * is not necessary.
190
                         */
191
                        MCAPI_CTRL_RX_Endp = create_endpoint(&node_data->mcapi_node_list[i],
192
                                                             MCAPI_RX_CONTROL_PORT,
193
                                                             mcapi_status);
194
 
195
                        /* Initialize any OS specific data structures. */
196
                        if (MCAPI_Init_OS() == MCAPI_SUCCESS)
197
                        {
198
                            /* Create an endpoint for sending control messages to remote
199
                             * nodes.  This call will only fail if the node is configured
200
                             * as a router only, in which case it is OK to fail, as the
201
                             * control port is not necessary.
202
                             */
203
                            MCAPI_CTRL_TX_Endp = create_endpoint(&node_data->mcapi_node_list[i],
204
                                                                 MCAPI_PORT_ANY,
205
                                                                 mcapi_status);
206
 
207
                            /* Initialize all interfaces on the node. */
208
                            *mcapi_status = mcapi_init_interfaces(node_id);
209
 
210
                            if (*mcapi_status == MCAPI_SUCCESS)
211
                            {
212
                                /* Set the version. */
213
                                *mcapi_version = MCAPI_VERSION;
214
 
215
                                /* Indicate that this node has been initialized. */
216
                                MCAPI_Init = 1;
217
 
218
                                /* Add the respective routes to the node. */
219
                                for (j = 0, k = 0;
220
                                     MCAPI_Route_List[j].mcapi_int_name[0] != '0';
221
                                     j++)
222
                                {
223
                                    /* Set the destination of the route. */
224
                                    node_data->mcapi_node_list[i].mcapi_route_list[k].
225
                                        mcapi_rt_dest_node_id =
226
                                        MCAPI_Route_List[j].mcapi_rt_dest_id;
227
 
228
                                    /* Set a pointer to the interface for the route. */
229
                                    node_data->mcapi_node_list[i].mcapi_route_list[k].mcapi_rt_int =
230
                                        mcapi_find_interface(MCAPI_Route_List[j].mcapi_int_name);
231
 
232
                                    k++;
233
                                }
234
                            }
235
                        }
236
 
237
                        else
238
                        {
239
                            *mcapi_status = MCAPI_ERR_TRANSMISSION;
240
                        }
241
                    }
242
 
243
                    /* The Node ID is not unique. */
244
                    else
245
                    {
246
                        *mcapi_status = MCAPI_ERR_NODE_INVALID;
247
                    }
248
                }
249
 
250
                /* The Node ID is not the local node's ID. */
251
                else
252
                {
253
                    *mcapi_status = MCAPI_ERR_NODE_INVALID;
254
                }
255
 
256
                /* Release the lock. */
257
                mcapi_unlock_node_data();
258
            }
259
 
260
            /* Initialization has already been called for this node. */
261
            else
262
            {
263
                *mcapi_status = MCAPI_ERR_NODE_INITIALIZED;
264
            }
265
        }
266
 
267
        /* The version parameter is invalid. */
268
        else
269
        {
270
            *mcapi_status = MCAPI_ERR_PARAMETER;
271
        }
272
    }
273
 
274
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.