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/] [msg_rcv.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
*       msg_recv
39
*
40
*   DESCRIPTION
41
*
42
*       Send a connectionless message to an endpoint.  Called by both
43
*       blocking and non-blocking transmission routines.
44
*
45
*   INPUTS
46
*
47
*       receive_endpoint        The local endpoint identifer that is
48
*                               receiving the data.
49
*       *buffer                 A pointer to memory that will be filled in
50
*                               with the received data.
51
*       buffer_size             The number of bytes that will fit in *buffer.
52
*       *bytes_recv             The numer of bytes received.
53
*       *request                A pointer to memory that will be filled in
54
*                               with data relevant to the operation, so the
55
*                               status of the operation can later be checked.
56
*       *mcapi_status           A pointer to memory that will be filled in
57
*                               with the status of the call.
58
*       timeout                 The amount of time to block for the operation
59
*                               to complete.
60
*
61
*   OUTPUTS
62
*
63
*       None.
64
*
65
*************************************************************************/
66
void msg_recv(mcapi_endpoint_t receive_endpoint, void *buffer,
67
              size_t buffer_size, size_t *bytes_recv,
68
              mcapi_request_t *request, mcapi_status_t *mcapi_status,
69
              mcapi_uint32_t timeout)
70
{
71
    MCAPI_GLOBAL_DATA   *node_data;
72
    MCAPI_ENDPOINT      *rx_endp_ptr = MCAPI_NULL;
73
    mcapi_port_t        port_id;
74
    mcapi_node_t        node_id;
75
    MCAPI_BUFFER        *cur_buf;
76
 
77
    /* Validate the mcapi_status parameter. */
78
    if (mcapi_status)
79
    {
80
        /* Validate the input parameters. */
81
        if ( (request) && (bytes_recv) && (buffer) && (buffer_size) )
82
        {
83
            /* Set bytes received to zero. */
84
            *bytes_recv = 0;
85
            request->mcapi_byte_count = 0;
86
 
87
            /* Get the lock. */
88
            mcapi_lock_node_data();
89
 
90
            /* Get a pointer to the global node list. */
91
            node_data = mcapi_get_node_data();
92
 
93
            /* Get a pointer to the endpoint. */
94
            rx_endp_ptr = mcapi_decode_local_endpoint(node_data, &node_id,
95
                                                      &port_id,
96
                                                      receive_endpoint,
97
                                                      mcapi_status);
98
 
99
            /* Ensure the receive endpoint is valid. */
100
            if (rx_endp_ptr)
101
            {
102
                /* Ensure the receive endpoint is not part of a
103
                 * connection.
104
                 */
105
                if (!(rx_endp_ptr->mcapi_state & MCAPI_ENDP_CONNECTING))
106
                {
107
                    /* Initialize the request structure. */
108
                    mcapi_init_request(request, MCAPI_REQ_RX_FIN);
109
 
110
                    /* Set up the request structure. */
111
                    request->mcapi_target_endp = receive_endpoint;
112
                    request->mcapi_chan_type = MCAPI_MSG_TYPE;
113
 
114
                    /* Set the request structure's buffer to the
115
                     * application's buffer that will be filled in when
116
                     * data is received.
117
                     */
118
                    request->mcapi_buffer = buffer;
119
 
120
                    /* Set the size of the application's buffer. */
121
                    request->mcapi_buf_size = buffer_size;
122
 
123
                    /* Check if there is data on the endpoint. */
124
                    if (rx_endp_ptr->mcapi_rx_queue.head)
125
                    {
126
                        /* Ensure all the data will fit in the user buffer. */
127
                        if (rx_endp_ptr->mcapi_rx_queue.head->buf_size -
128
                            MCAPI_HEADER_LEN <= buffer_size)
129
                        {
130
                            /* Copy the data into the user buffer. */
131
                            *bytes_recv = msg_recv_copy_data(rx_endp_ptr, buffer);
132
 
133
                            /* Remove the buffer from the receive queue. */
134
                            cur_buf = mcapi_dequeue(&rx_endp_ptr->mcapi_rx_queue);
135
 
136
                            /* Return the receive buffer to the pool of available buffers. */
137
                            ((MCAPI_INTERFACE*)(cur_buf->mcapi_dev_ptr))->
138
                                mcapi_recover_buffer(cur_buf);
139
 
140
                            /* Set the status. */
141
                            *mcapi_status = request->mcapi_status = MCAPI_SUCCESS;
142
 
143
                            /* Store the number of bytes received. */
144
                            request->mcapi_byte_count = *bytes_recv;
145
                        }
146
 
147
                        /* The data is too large for the buffer. */
148
                        else
149
                        {
150
                            *mcapi_status = request->mcapi_status =
151
                                MCAPI_ERR_MSG_TRUNCATED;
152
                        }
153
                    }
154
 
155
                    /* If this is a blocking request, suspend until the
156
                     * operation has completed or been canceled.
157
                     */
158
                    else if (timeout)
159
                    {
160
                        MCAPI_Suspend_Task(node_data, request, &request->mcapi_cond,
161
                                           MCAPI_TIMEOUT_INFINITE);
162
 
163
                        /* Return the number of bytes received. */
164
                        *bytes_recv = request->mcapi_byte_count;
165
 
166
                        /* Set the return status. */
167
                        *mcapi_status = request->mcapi_status;
168
                    }
169
 
170
                    /* This is a non-blocking receive. */
171
                    else
172
                    {
173
                        /* Add the application's request structure to
174
                         * the list of pending requests for the node.
175
                         */
176
                        mcapi_enqueue(&node_data->mcapi_local_req_queue, request);
177
 
178
                        /* Indicate completion of the operation. */
179
                        *mcapi_status = MCAPI_SUCCESS;
180
                    }
181
                }
182
 
183
                /* This endpoint is part of a connected channel. */
184
                else
185
                {
186
                    *mcapi_status = MCAPI_ERR_CHAN_CONNECTED;
187
                }
188
            }
189
 
190
            /* The endpoint is invalid. */
191
            else
192
            {
193
                *mcapi_status = MCAPI_ERR_ENDP_INVALID;
194
            }
195
 
196
            /* Release the lock. */
197
            mcapi_unlock_node_data();
198
        }
199
 
200
        /* The request structure is not valid. */
201
        else
202
        {
203
            *mcapi_status = MCAPI_ERR_PARAMETER;
204
        }
205
    }
206
 
207
}
208
 
209
/*************************************************************************
210
*
211
*   FUNCTION
212
*
213
*       msg_recv_copy_data
214
*
215
*   DESCRIPTION
216
*
217
*       Copies data from the receive queue into the user buffer and
218
*       sets the statuses accordingly.
219
*
220
*   INPUTS
221
*
222
*       *rx_endp_ptr            A pointer to the endpoint structure on which
223
*                               data is being received.
224
*       *buffer                 A pointer to memory that will be filled in
225
*                               with the received data.
226
*
227
*   OUTPUTS
228
*
229
*       None.
230
*
231
*************************************************************************/
232
size_t msg_recv_copy_data(MCAPI_ENDPOINT *rx_endp_ptr, void *buffer)
233
{
234
    MCAPI_BUFFER    *cur_buf;
235
 
236
    /* Get the head buffer from the receive queue. */
237
    cur_buf = rx_endp_ptr->mcapi_rx_queue.head;
238
 
239
    /* Copy the data into the user buffer, removing the MCAPI header. */
240
    memcpy(buffer, &cur_buf->buf_ptr[MCAPI_HEADER_LEN],
241
           cur_buf->buf_size - MCAPI_HEADER_LEN);
242
 
243
    return (cur_buf->buf_size - MCAPI_HEADER_LEN);
244
 
245
}

powered by: WebSVN 2.1.0

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