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/] [endpoint.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
 
34
/*************************************************************************
35
*
36
*   FUNCTION
37
*
38
*       mcapi_find_endpoint
39
*
40
*   DESCRIPTION
41
*
42
*       Finds a specific endpoint in the system.
43
*
44
*   INPUTS
45
*
46
*       port_id                 The port ID of the target endpoint.
47
*       *node_ptr               A pointer to the node structure
48
*                               associated with the endpoint.
49
*
50
*   OUTPUTS
51
*
52
*       Index into the endpoint list of the respective node or
53
*       -1 if the endpoint does not exist.
54
*
55
*************************************************************************/
56
int mcapi_find_endpoint(mcapi_port_t port_id, MCAPI_NODE *node_ptr)
57
{
58
    int     endp_idx, i = 0;
59
 
60
    /* Compute the hash value. */
61
    endp_idx = (port_id % MCAPI_MAX_ENDPOINTS);
62
 
63
    /* If this is the matching endpoint. */
64
    if (node_ptr->mcapi_endpoint_list[endp_idx].mcapi_port_id == port_id)
65
    {
66
        /* Ensure the endpoint has not been closed. */
67
        if (!(node_ptr->mcapi_endpoint_list[endp_idx].mcapi_state & MCAPI_ENDP_OPEN))
68
        {
69
            /* Return an error. */
70
            endp_idx = -1;
71
        }
72
    }
73
 
74
    else
75
    {
76
        /* Find an empty slot. */
77
        do
78
        {
79
            /* Move to the next slot. */
80
            endp_idx ++;
81
 
82
            /* If the index has rolled over. */
83
            if ( (endp_idx < 0) ||
84
                 (endp_idx >= MCAPI_MAX_ENDPOINTS) )
85
            {
86
                endp_idx = 0;
87
            }
88
 
89
            /* If this is the target endpoint. */
90
            if (node_ptr->mcapi_endpoint_list[endp_idx].mcapi_port_id == port_id)
91
            {
92
                /* Ensure the endpoint has not been closed. */
93
                if (!(node_ptr->mcapi_endpoint_list[endp_idx].mcapi_state & MCAPI_ENDP_OPEN))
94
                {
95
                    /* Return an error. */
96
                    endp_idx = -1;
97
                }
98
 
99
                break;
100
            }
101
 
102
            i ++;
103
 
104
        } while (i < MCAPI_MAX_ENDPOINTS);
105
 
106
        /* If a matching endpoint was not found. */
107
        if (i >= MCAPI_MAX_ENDPOINTS)
108
            endp_idx = -1;
109
    }
110
 
111
    return (endp_idx);
112
 
113
}
114
 
115
/*************************************************************************
116
*
117
*   FUNCTION
118
*
119
*       mcapi_encode_endpoint
120
*
121
*   DESCRIPTION
122
*
123
*       Encodes a handle using the index of the respective node and
124
*       the port ID of the endpoint.
125
*
126
*   INPUTS
127
*
128
*       node_id                 Node ID of the endpoint.
129
*       port_id                 Port ID of the endpoint.
130
*
131
*   OUTPUTS
132
*
133
*       The 32-bit encoded handle.
134
*
135
*************************************************************************/
136
mcapi_endpoint_t mcapi_encode_endpoint(mcapi_node_t node_id,
137
                                       mcapi_port_t port_id)
138
{
139
    mcapi_endpoint_t    handle = 0;
140
 
141
    /* Put the node index in the handle. */
142
    handle = node_id;
143
 
144
    /* Move the node ID left 16-bits. */
145
    handle <<= 16;
146
 
147
    /* Add the port ID to the lower 16-bits of the handle. */
148
    handle |= port_id;
149
 
150
    return (handle);
151
 
152
}
153
 
154
/*************************************************************************
155
*
156
*   FUNCTION
157
*
158
*       mcapi_decode_endpoint
159
*
160
*   DESCRIPTION
161
*
162
*       Decodes a handle to retrieve the index of the respective node and
163
*       endpoint.
164
*
165
*   INPUTS
166
*
167
*       handle                  The encoded handle.
168
*       *node_id                A pointer to memory that will hold the
169
*                               decoded node ID.
170
*       *endp_id                A pointer to memory that will hold the
171
*                               decoded endpoint ID.
172
*
173
*   OUTPUTS
174
*
175
*       None.
176
*
177
*************************************************************************/
178
void mcapi_decode_endpoint(mcapi_endpoint_t handle, mcapi_node_t *node_id,
179
                           mcapi_port_t *port_id)
180
{
181
    /* Extract the high 16-bits by chopping off the low 16-bits and moving
182
     * the remaining data over 16-bits.
183
     */
184
    *node_id = (handle & 0xffff0000) >> 16;
185
 
186
    /* Extract the low 16-bits by chopping off the high 16-bits. */
187
    *port_id = (handle & 0x0000ffff);
188
 
189
}
190
 
191
/*************************************************************************
192
*
193
*   FUNCTION
194
*
195
*       mcapi_decode_local_endpoint
196
*
197
*   DESCRIPTION
198
*
199
*       Decodes a handle to retrieve a pointer to the MCAPI_ENDPOINT
200
*       structure associated with a local endpoint.
201
*
202
*   INPUTS
203
*
204
*       *node_data              A pointer to the global database.
205
*       *node_id                A pointer to memory that will hold the
206
*                               decoded node ID.
207
*       *port_id                A pointer to memory that will hold the
208
*                               decoded endpoint ID.
209
*       endpoint                The encoded endpoint.
210
*       *status                 The status to be filled in.
211
*
212
*   OUTPUTS
213
*
214
*       A pointer to the endpoint structure or MCAPI_NULL if one
215
*       does not exist.
216
*
217
*************************************************************************/
218
MCAPI_ENDPOINT *mcapi_decode_local_endpoint(MCAPI_GLOBAL_DATA *node_data,
219
                                            mcapi_node_t *node_id,
220
                                            mcapi_port_t *port_id,
221
                                            mcapi_endpoint_t endpoint,
222
                                            mcapi_status_t *mcapi_status)
223
{
224
    int             node_idx, endp_idx;
225
    MCAPI_ENDPOINT  *endp_ptr = MCAPI_NULL;
226
 
227
    /* Decode the node and port IDs. */
228
    mcapi_decode_endpoint(endpoint, node_id, port_id);
229
 
230
    /* Get the index of the node. */
231
    node_idx = mcapi_find_node(*node_id, node_data);
232
 
233
    /* If the node is valid. */
234
    if (node_idx != -1)
235
    {
236
        /* Get the index of the endpoint. */
237
        endp_idx =
238
            mcapi_find_endpoint(*port_id, &node_data->mcapi_node_list[node_idx]);
239
 
240
        /* Validate the endpoint index. */
241
        if ( (endp_idx >= 0) && (endp_idx < MCAPI_MAX_ENDPOINTS) )
242
        {
243
            endp_ptr =
244
                &node_data->mcapi_node_list[node_idx].mcapi_endpoint_list[endp_idx];
245
 
246
            *mcapi_status = MCAPI_SUCCESS;
247
        }
248
 
249
        /* The endpoint does not exist. */
250
        else
251
        {
252
            *mcapi_status = MCAPI_ERR_ENDP_INVALID;
253
        }
254
    }
255
 
256
    /* The node is not a local node. */
257
    else
258
    {
259
        *mcapi_status = MCAPI_ERR_ENDP_NOTOWNER;
260
    }
261
 
262
    return (endp_ptr);
263
 
264
}
265
 
266
/*************************************************************************
267
*
268
*   FUNCTION
269
*
270
*       mcapi_find_local_endpoint
271
*
272
*   DESCRIPTION
273
*
274
*       Returns the MCAPI_ENDPOINT structure associated with a
275
*       local node ID, port ID combination.
276
*
277
*   INPUTS
278
*
279
*       *node_data              A pointer to the global database.
280
*       node_id                 The node ID of the target endpoint.
281
*       port_id                 The port ID of the target endpoint.
282
*
283
*   OUTPUTS
284
*
285
*       A pointer to the endpoint structure or MCAPI_NULL if one
286
*       does not exist.
287
*
288
*************************************************************************/
289
MCAPI_ENDPOINT *mcapi_find_local_endpoint(MCAPI_GLOBAL_DATA *node_data,
290
                                          mcapi_node_t node_id,
291
                                          mcapi_port_t port_id)
292
{
293
    int             node_idx, endp_idx;
294
    MCAPI_ENDPOINT  *endp_ptr = MCAPI_NULL;
295
 
296
    /* Get the index of the local node. */
297
    node_idx = mcapi_find_node(node_id, node_data);
298
 
299
    /* If the node is local. */
300
    if (node_idx != -1)
301
    {
302
        /* Get a pointer to the local endpoint. */
303
        endp_idx =
304
            mcapi_find_endpoint(port_id, &node_data->mcapi_node_list[node_idx]);
305
 
306
        /* If the endpoint exists. */
307
        if (endp_idx >= 0)
308
        {
309
            endp_ptr = &node_data->mcapi_node_list[node_idx].
310
                mcapi_endpoint_list[endp_idx];
311
        }
312
    }
313
 
314
    return (endp_ptr);
315
 
316
}
317
 

powered by: WebSVN 2.1.0

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