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 |
|
|
extern MCAPI_BUF_QUEUE MCAPI_Buf_Wait_List;
|
35 |
|
|
|
36 |
|
|
/*************************************************************************
|
37 |
|
|
*
|
38 |
|
|
* FUNCTION
|
39 |
|
|
*
|
40 |
|
|
* pkt_rcv
|
41 |
|
|
*
|
42 |
|
|
* DESCRIPTION
|
43 |
|
|
*
|
44 |
|
|
* Receive a packet over a connected channel. Called by both
|
45 |
|
|
* blocking and non-blocking transmission routines.
|
46 |
|
|
*
|
47 |
|
|
* INPUTS
|
48 |
|
|
*
|
49 |
|
|
* receive_handle The local handle that is receiving the data.
|
50 |
|
|
* **buffer A pointer to a pointer that will be set to the
|
51 |
|
|
* memory address of the incoming buffer.
|
52 |
|
|
* *received_size The number 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 pkt_rcv(mcapi_pktchan_recv_hndl_t receive_handle, void **buffer,
|
67 |
|
|
size_t *received_size, mcapi_request_t *request,
|
68 |
|
|
mcapi_status_t *mcapi_status, mcapi_uint32_t timeout)
|
69 |
|
|
{
|
70 |
|
|
MCAPI_GLOBAL_DATA *node_data;
|
71 |
|
|
MCAPI_ENDPOINT *rx_endp_ptr = MCAPI_NULL;
|
72 |
|
|
mcapi_port_t port_id;
|
73 |
|
|
mcapi_node_t node_id;
|
74 |
|
|
MCAPI_BUFFER *rx_buf = MCAPI_NULL;
|
75 |
|
|
|
76 |
|
|
/* Validate mcapi_status. */
|
77 |
|
|
if (mcapi_status)
|
78 |
|
|
{
|
79 |
|
|
/* Validate the input parameters. */
|
80 |
|
|
if ( (request) && (received_size) && (buffer) )
|
81 |
|
|
{
|
82 |
|
|
/* Initialize bytes received to zero. */
|
83 |
|
|
*received_size = 0;
|
84 |
|
|
request->mcapi_byte_count = 0;
|
85 |
|
|
|
86 |
|
|
/* Lock the global data structure. */
|
87 |
|
|
mcapi_lock_node_data();
|
88 |
|
|
|
89 |
|
|
/* Get a pointer to the global node list. */
|
90 |
|
|
node_data = mcapi_get_node_data();
|
91 |
|
|
|
92 |
|
|
/* Get a pointer to the endpoint that is receiving data. */
|
93 |
|
|
rx_endp_ptr = mcapi_decode_local_endpoint(node_data, &node_id,
|
94 |
|
|
&port_id,
|
95 |
|
|
receive_handle,
|
96 |
|
|
mcapi_status);
|
97 |
|
|
|
98 |
|
|
/* Ensure the receive endpoint is valid. */
|
99 |
|
|
if (rx_endp_ptr)
|
100 |
|
|
{
|
101 |
|
|
/* Ensure this is a receive endpoint. */
|
102 |
|
|
if (rx_endp_ptr->mcapi_state & MCAPI_ENDP_RX)
|
103 |
|
|
{
|
104 |
|
|
/* Ensure the handle is for a packet channel. */
|
105 |
|
|
if (rx_endp_ptr->mcapi_chan_type == MCAPI_CHAN_PKT_TYPE)
|
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_handle;
|
112 |
|
|
request->mcapi_chan_type = MCAPI_CHAN_PKT_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_pkt = buffer;
|
119 |
|
|
|
120 |
|
|
/* Check if there is data on the endpoint. */
|
121 |
|
|
if (rx_endp_ptr->mcapi_rx_queue.head)
|
122 |
|
|
{
|
123 |
|
|
/* Remove the buffer from the queue. */
|
124 |
|
|
rx_buf =
|
125 |
|
|
mcapi_dequeue(&rx_endp_ptr->mcapi_rx_queue);
|
126 |
|
|
|
127 |
|
|
/* Set the user buffer to the incoming buffer. */
|
128 |
|
|
*buffer = &rx_buf->buf_ptr[MCAPI_HEADER_LEN];
|
129 |
|
|
|
130 |
|
|
/* Set the size of the incoming buffer. */
|
131 |
|
|
*received_size = rx_buf->buf_size - MCAPI_HEADER_LEN;
|
132 |
|
|
|
133 |
|
|
/* Add the receive buffer structure to the list of
|
134 |
|
|
* buffers waiting to be freed by the application.
|
135 |
|
|
*/
|
136 |
|
|
mcapi_enqueue(&MCAPI_Buf_Wait_List, rx_buf);
|
137 |
|
|
|
138 |
|
|
/* Set the number of bytes in the request structure. */
|
139 |
|
|
request->mcapi_byte_count = *received_size;
|
140 |
|
|
|
141 |
|
|
/* Indicate completion of the operation. */
|
142 |
|
|
*mcapi_status =
|
143 |
|
|
request->mcapi_status = MCAPI_SUCCESS;
|
144 |
|
|
}
|
145 |
|
|
|
146 |
|
|
/* If there is no data waiting on the endpoint, ensure the
|
147 |
|
|
* endpoint is still part of a connection before blocking for
|
148 |
|
|
* data.
|
149 |
|
|
*/
|
150 |
|
|
else if (rx_endp_ptr->mcapi_state & MCAPI_ENDP_CONNECTED)
|
151 |
|
|
{
|
152 |
|
|
/* If this is a blocking request, suspend until the
|
153 |
|
|
* operation has completed or been canceled.
|
154 |
|
|
*/
|
155 |
|
|
if (timeout)
|
156 |
|
|
{
|
157 |
|
|
MCAPI_Suspend_Task(node_data, request,
|
158 |
|
|
&request->mcapi_cond,
|
159 |
|
|
MCAPI_TIMEOUT_INFINITE);
|
160 |
|
|
|
161 |
|
|
/* Return the number of bytes received. */
|
162 |
|
|
*received_size = request->mcapi_byte_count;
|
163 |
|
|
|
164 |
|
|
/* Set the return status. */
|
165 |
|
|
*mcapi_status = request->mcapi_status;
|
166 |
|
|
}
|
167 |
|
|
|
168 |
|
|
/* This is a non-blocking receive. */
|
169 |
|
|
else
|
170 |
|
|
{
|
171 |
|
|
/* Add the application's request structure to
|
172 |
|
|
* the list of pending requests for the node.
|
173 |
|
|
*/
|
174 |
|
|
mcapi_enqueue(&node_data->mcapi_local_req_queue, request);
|
175 |
|
|
|
176 |
|
|
/* Indicate completion of the operation. */
|
177 |
|
|
*mcapi_status = MCAPI_SUCCESS;
|
178 |
|
|
}
|
179 |
|
|
}
|
180 |
|
|
|
181 |
|
|
/* The connection has been closed. */
|
182 |
|
|
else
|
183 |
|
|
{
|
184 |
|
|
*mcapi_status = MCAPI_ERR_CHAN_INVALID;
|
185 |
|
|
}
|
186 |
|
|
}
|
187 |
|
|
|
188 |
|
|
/* The handle is for a scalar channel. */
|
189 |
|
|
else
|
190 |
|
|
{
|
191 |
|
|
*mcapi_status = MCAPI_ERR_CHAN_TYPE;
|
192 |
|
|
}
|
193 |
|
|
}
|
194 |
|
|
|
195 |
|
|
/* Attempting to receive on a send handle. */
|
196 |
|
|
else if (rx_endp_ptr->mcapi_state & MCAPI_ENDP_TX)
|
197 |
|
|
{
|
198 |
|
|
*mcapi_status = MCAPI_ERR_CHAN_DIRECTION;
|
199 |
|
|
}
|
200 |
|
|
|
201 |
|
|
/* The receive side has been closed. */
|
202 |
|
|
else
|
203 |
|
|
{
|
204 |
|
|
*mcapi_status = MCAPI_ERR_CHAN_INVALID;
|
205 |
|
|
}
|
206 |
|
|
}
|
207 |
|
|
|
208 |
|
|
else
|
209 |
|
|
{
|
210 |
|
|
*mcapi_status = MCAPI_ERR_CHAN_INVALID;
|
211 |
|
|
}
|
212 |
|
|
|
213 |
|
|
/* Unlock the global data structure. */
|
214 |
|
|
mcapi_unlock_node_data();
|
215 |
|
|
}
|
216 |
|
|
|
217 |
|
|
/* The request structure is invalid. */
|
218 |
|
|
else
|
219 |
|
|
{
|
220 |
|
|
*mcapi_status = MCAPI_ERR_PARAMETER;
|
221 |
|
|
}
|
222 |
|
|
}
|
223 |
|
|
|
224 |
|
|
}
|