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 |
|
|
* FILENAME
|
32 |
|
|
*
|
33 |
|
|
* create_svc.c
|
34 |
|
|
*
|
35 |
|
|
*
|
36 |
|
|
*************************************************************************/
|
37 |
|
|
#include "mcapid_support.h"
|
38 |
|
|
|
39 |
|
|
/************************************************************************
|
40 |
|
|
*
|
41 |
|
|
* FUNCTION
|
42 |
|
|
*
|
43 |
|
|
* MCAPID_Create_Service
|
44 |
|
|
*
|
45 |
|
|
* DESCRIPTION
|
46 |
|
|
*
|
47 |
|
|
* This function loops through a list of structures, performing
|
48 |
|
|
* service requests based on the type of endpoint in each structure.
|
49 |
|
|
* For each structure, a local endpoint is created and one of the
|
50 |
|
|
* following functions performed:
|
51 |
|
|
*
|
52 |
|
|
* Packet / Scalar Channel Client - If a service is specified, get
|
53 |
|
|
* the endpoint associated with the service, create a connection,
|
54 |
|
|
* and open the send side of the connection.
|
55 |
|
|
*
|
56 |
|
|
* Packet / Scalar Channel Server - If a service is specified,
|
57 |
|
|
* register the service with the registration node and open the
|
58 |
|
|
* receive side of the connection.
|
59 |
|
|
*
|
60 |
|
|
* Client Message - If a service is specified, get the endpoint
|
61 |
|
|
* associated with the service.
|
62 |
|
|
*
|
63 |
|
|
* Server Message - If a service is specified, register the service
|
64 |
|
|
* with the registration node.
|
65 |
|
|
*
|
66 |
|
|
* INPUTS
|
67 |
|
|
*
|
68 |
|
|
* *mcapi_struct A pointer to an array of MCAPID_STRUCT
|
69 |
|
|
* structures populated as follows:
|
70 |
|
|
*
|
71 |
|
|
* type - MCAPI_CHAN_PKT_TX_TYPE,
|
72 |
|
|
* MCAPI_CHAN_PKT_RX_TYPE,
|
73 |
|
|
* MCAPI_CHAN_SCL_TX_TYPE,
|
74 |
|
|
* MCAPI_CHAN_SCL_RX_TYPE,
|
75 |
|
|
* MCAPI_MSG_TX_TYPE,
|
76 |
|
|
* MCAPI_MSG_RX_TYPE
|
77 |
|
|
*
|
78 |
|
|
* local_port - The local port to use or
|
79 |
|
|
* MCAPI_ANY_PORT if unspecified.
|
80 |
|
|
*
|
81 |
|
|
* *service - The name of the service to
|
82 |
|
|
* register if type is a _RX_; otherwise,
|
83 |
|
|
* the name of the service to get.
|
84 |
|
|
*
|
85 |
|
|
* retry - If type is _TX_, the number of
|
86 |
|
|
* times to retry the service get request.
|
87 |
|
|
*
|
88 |
|
|
* count The number of elements in the mcapi_struct
|
89 |
|
|
* structure.
|
90 |
|
|
*
|
91 |
|
|
* RETURN
|
92 |
|
|
*
|
93 |
|
|
* None. The status field of each MCAPID_STRUCT parameter will be
|
94 |
|
|
* set according to the status of that service request.
|
95 |
|
|
*
|
96 |
|
|
*************************************************************************/
|
97 |
|
|
int MCAPID_Create_Service(MCAPID_STRUCT *mcapi_struct)
|
98 |
|
|
{
|
99 |
|
|
mcapi_request_t request;
|
100 |
|
|
int retry;
|
101 |
|
|
size_t size;
|
102 |
|
|
mcapi_status_t status;
|
103 |
|
|
static int next_free_port = 20; /* arbitrary */
|
104 |
|
|
int rc = 0;
|
105 |
|
|
|
106 |
|
|
/* Can't use MCAPI_PORT_ANY, because we need to know the port in order to
|
107 |
|
|
* register it. */
|
108 |
|
|
if (mcapi_struct->local_port == MCAPI_PORT_ANY) {
|
109 |
|
|
mcapi_struct->local_port = next_free_port++;
|
110 |
|
|
}
|
111 |
|
|
|
112 |
|
|
/* Create the local endpoint. */
|
113 |
|
|
mcapi_struct->local_endp = mcapi_create_endpoint(mcapi_struct->local_port,
|
114 |
|
|
&mcapi_struct->status);
|
115 |
|
|
|
116 |
|
|
if (mcapi_struct->status == MCAPI_SUCCESS)
|
117 |
|
|
{
|
118 |
|
|
/* If a service was specified. */
|
119 |
|
|
if (mcapi_struct->service)
|
120 |
|
|
{
|
121 |
|
|
/* Operate on the service field based on the type of endpoint. */
|
122 |
|
|
switch (mcapi_struct->type)
|
123 |
|
|
{
|
124 |
|
|
/* Client type endpoints get services. */
|
125 |
|
|
case MCAPI_CHAN_PKT_TX_TYPE:
|
126 |
|
|
case MCAPI_CHAN_SCL_TX_TYPE:
|
127 |
|
|
case MCAPI_MSG_TX_TYPE:
|
128 |
|
|
|
129 |
|
|
retry = mcapi_struct->retry;
|
130 |
|
|
|
131 |
|
|
do
|
132 |
|
|
{
|
133 |
|
|
/* Get the foreign endpoint. */
|
134 |
|
|
mcapi_struct->status =
|
135 |
|
|
MCAPID_Get_Service(mcapi_struct->service,
|
136 |
|
|
&mcapi_struct->foreign_endp);
|
137 |
|
|
|
138 |
|
|
if (retry > 0)
|
139 |
|
|
retry --;
|
140 |
|
|
|
141 |
|
|
/* All attempts have been made. */
|
142 |
|
|
else if (retry != 0xffffffff)
|
143 |
|
|
break;
|
144 |
|
|
|
145 |
|
|
} while ( (mcapi_struct->status != MCAPI_SUCCESS) &&
|
146 |
|
|
(mcapi_struct->status != MCAPI_ERR_GENERAL) );
|
147 |
|
|
|
148 |
|
|
break;
|
149 |
|
|
|
150 |
|
|
/* Server type endpoints register services. */
|
151 |
|
|
case MCAPI_CHAN_PKT_RX_TYPE:
|
152 |
|
|
case MCAPI_CHAN_SCL_RX_TYPE:
|
153 |
|
|
case MCAPI_MSG_RX_TYPE:
|
154 |
|
|
|
155 |
|
|
/* Register the service. */
|
156 |
|
|
mcapi_struct->status =
|
157 |
|
|
MCAPID_Register_Service(mcapi_struct->service,
|
158 |
|
|
mcapi_struct->node,
|
159 |
|
|
mcapi_struct->local_port);
|
160 |
|
|
|
161 |
|
|
break;
|
162 |
|
|
|
163 |
|
|
default:
|
164 |
|
|
|
165 |
|
|
break;
|
166 |
|
|
}
|
167 |
|
|
}
|
168 |
|
|
|
169 |
|
|
/* If the service was registered / retrieved. */
|
170 |
|
|
if (mcapi_struct->status == MCAPI_SUCCESS)
|
171 |
|
|
{
|
172 |
|
|
/* Operate on the structure based on the type of endpoint being
|
173 |
|
|
* created.
|
174 |
|
|
*/
|
175 |
|
|
switch (mcapi_struct->type)
|
176 |
|
|
{
|
177 |
|
|
/* The TX side of a packet channel. */
|
178 |
|
|
case MCAPI_CHAN_PKT_TX_TYPE:
|
179 |
|
|
|
180 |
|
|
/* If a service was specified. */
|
181 |
|
|
if (mcapi_struct->service)
|
182 |
|
|
{
|
183 |
|
|
/* Issue the connection. */
|
184 |
|
|
mcapi_connect_pktchan_i(mcapi_struct->local_endp,
|
185 |
|
|
mcapi_struct->foreign_endp,
|
186 |
|
|
&request, &mcapi_struct->status);
|
187 |
|
|
|
188 |
|
|
/* Wait for the connection to complete. */
|
189 |
|
|
mcapi_wait(&request, &size, &mcapi_struct->status,
|
190 |
|
|
MCAPI_TIMEOUT_INFINITE);
|
191 |
|
|
}
|
192 |
|
|
|
193 |
|
|
if (mcapi_struct->status == MCAPI_SUCCESS)
|
194 |
|
|
{
|
195 |
|
|
/* Open the TX side. */
|
196 |
|
|
mcapi_open_pktchan_send_i(&mcapi_struct->pkt_tx_handle,
|
197 |
|
|
mcapi_struct->local_endp,
|
198 |
|
|
&mcapi_struct->request,
|
199 |
|
|
&mcapi_struct->status);
|
200 |
|
|
mcapi_wait(&mcapi_struct->request, &size,
|
201 |
|
|
&mcapi_struct->status, MCAPI_TIMEOUT_INFINITE);
|
202 |
|
|
}
|
203 |
|
|
|
204 |
|
|
break;
|
205 |
|
|
|
206 |
|
|
case MCAPI_CHAN_PKT_RX_TYPE:
|
207 |
|
|
|
208 |
|
|
/* Open the RX side. */
|
209 |
|
|
mcapi_open_pktchan_recv_i(&mcapi_struct->pkt_rx_handle,
|
210 |
|
|
mcapi_struct->local_endp,
|
211 |
|
|
&mcapi_struct->request,
|
212 |
|
|
&mcapi_struct->status);
|
213 |
|
|
mcapi_wait(&mcapi_struct->request, &size,
|
214 |
|
|
&mcapi_struct->status, MCAPI_TIMEOUT_INFINITE);
|
215 |
|
|
|
216 |
|
|
break;
|
217 |
|
|
|
218 |
|
|
case MCAPI_CHAN_SCL_TX_TYPE:
|
219 |
|
|
|
220 |
|
|
/* If a service was specified. */
|
221 |
|
|
if (mcapi_struct->service)
|
222 |
|
|
{
|
223 |
|
|
/* Issue the connection. */
|
224 |
|
|
mcapi_connect_sclchan_i(mcapi_struct->local_endp,
|
225 |
|
|
mcapi_struct->foreign_endp,
|
226 |
|
|
&request, &mcapi_struct->status);
|
227 |
|
|
|
228 |
|
|
/* Wait for the connection to complete. */
|
229 |
|
|
mcapi_wait(&request, &size, &mcapi_struct->status,
|
230 |
|
|
MCAPI_TIMEOUT_INFINITE);
|
231 |
|
|
}
|
232 |
|
|
|
233 |
|
|
if (mcapi_struct->status == MCAPI_SUCCESS)
|
234 |
|
|
{
|
235 |
|
|
/* Open the TX side. */
|
236 |
|
|
mcapi_open_sclchan_send_i(&mcapi_struct->scl_tx_handle,
|
237 |
|
|
mcapi_struct->local_endp,
|
238 |
|
|
&mcapi_struct->request,
|
239 |
|
|
&mcapi_struct->status);
|
240 |
|
|
mcapi_wait(&mcapi_struct->request, &size,
|
241 |
|
|
&mcapi_struct->status, MCAPI_TIMEOUT_INFINITE);
|
242 |
|
|
}
|
243 |
|
|
|
244 |
|
|
break;
|
245 |
|
|
|
246 |
|
|
case MCAPI_CHAN_SCL_RX_TYPE:
|
247 |
|
|
|
248 |
|
|
/* Open the RX side. */
|
249 |
|
|
mcapi_open_sclchan_recv_i(&mcapi_struct->scl_rx_handle,
|
250 |
|
|
mcapi_struct->local_endp,
|
251 |
|
|
&mcapi_struct->request,
|
252 |
|
|
&mcapi_struct->status);
|
253 |
|
|
|
254 |
|
|
break;
|
255 |
|
|
|
256 |
|
|
default:
|
257 |
|
|
|
258 |
|
|
break;
|
259 |
|
|
}
|
260 |
|
|
|
261 |
|
|
if (mcapi_struct->thread_entry)
|
262 |
|
|
{
|
263 |
|
|
/* Start the service locally. */
|
264 |
|
|
MCAPID_Create_Thread(mcapi_struct->thread_entry, mcapi_struct);
|
265 |
|
|
}
|
266 |
|
|
else if (mcapi_struct->func)
|
267 |
|
|
{
|
268 |
|
|
rc = (int)mcapi_struct->func(mcapi_struct);
|
269 |
|
|
}
|
270 |
|
|
}
|
271 |
|
|
|
272 |
|
|
/* If an error occurred. */
|
273 |
|
|
if ( (mcapi_struct->status != MCAPI_SUCCESS) &&
|
274 |
|
|
(mcapi_struct->status != MGC_MCAPI_ERR_NOT_CONNECTED) )
|
275 |
|
|
{
|
276 |
|
|
/* Delete the endpoint since the service could not be
|
277 |
|
|
* registered / retrieved. The call has failed for this
|
278 |
|
|
* instance of the MCAPID_STRUCT.
|
279 |
|
|
*/
|
280 |
|
|
mcapi_delete_endpoint(mcapi_struct->local_endp, &status);
|
281 |
|
|
}
|
282 |
|
|
}
|
283 |
|
|
|
284 |
|
|
return rc;
|
285 |
|
|
} /* MCAPID_Create_Service */
|