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/] [test/] [support_suite/] [reg_svcs.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
*   FILENAME
32
*
33
*       reg_svcs.c
34
*
35
*
36
*************************************************************************/
37
 
38
#include "openmcapi.h" /* XXX remove me */
39
#include "mcapid_support.h"
40
#include "mcapid.h"
41
 
42
MCAPID_SERVICE_STRUCT   MCAPID_Registered_Services[MCAPID_MAX_SERVICES];
43
 
44
/************************************************************************
45
*
46
*   FUNCTION
47
*
48
*       MCAPID_Registration_Server
49
*
50
*   DESCRIPTION
51
*
52
*       This function services incoming registration requests.
53
*
54
*************************************************************************/
55
MCAPI_THREAD_ENTRY(MCAPID_Registration_Server)
56
{
57
    mcapi_status_t      status;
58
    mcapi_endpoint_t    rx_endp;
59
    unsigned char       buffer[MCAPID_REG_MSG_LEN];
60
    size_t              rx_len;
61
    int                 i, avail;
62
 
63
    /* Initialize each entry to available. */
64
    for (i = 0; i < MCAPID_MAX_SERVICES; i ++)
65
    {
66
        MCAPID_Registered_Services[i].avail = MCAPI_TRUE;
67
    }
68
 
69
    /* Create the registration service receive endpoint. */
70
    rx_endp = mcapi_create_endpoint(MCAPID_REG_SERVER_PORT, &status);
71
 
72
    if (status == MCAPI_SUCCESS)
73
    {
74
        for (;;)
75
        {
76
            /* Wait for a node to issue a request. */
77
            mcapi_msg_recv(rx_endp, buffer, MCAPID_REG_MSG_LEN, &rx_len,
78
                           &status);
79
 
80
            if (status == MCAPI_SUCCESS)
81
            {
82
                /* If this is a registration request. */
83
                if (mcapi_get32(buffer, MCAPID_SVCREG_TYPE_OFFSET) == MCAPID_REG_SVC)
84
                {
85
                    /* Initialize the "available" variable. */
86
                    avail = -1;
87
 
88
                    /* Check that this registration is unique and find an available
89
                     * slot for the new registration.
90
                     */
91
                    for (i = 0; i < MCAPID_MAX_SERVICES; i ++)
92
                    {
93
                        /* If this entry is available, save it for later. */
94
                        if ( (avail == -1) &&
95
                             (MCAPID_Registered_Services[i].avail == MCAPI_TRUE) )
96
                        {
97
                            avail = i;
98
                        }
99
 
100
                        /* If the two strings match. */
101
                        if ( (MCAPID_Registered_Services[i].avail == MCAPI_FALSE) &&
102
                             (strcmp((char*)&buffer[MCAPID_SVCREG_NAME_OFFSET],
103
                                     MCAPID_Registered_Services[i].service) == 0) )
104
                        {
105
                            status = -1;
106
                            break;
107
                        }
108
                    }
109
 
110
                    /* If the registration request was successful and the service
111
                     * name will fit in the available memory.
112
                     */
113
                    if ( (status == MCAPI_SUCCESS) && (avail != -1) &&
114
                         (strlen((char*)&buffer[MCAPID_SVCREG_NAME_OFFSET]) <= MCAPID_SVC_LEN) )
115
                    {
116
                        /* Store the service name. */
117
                        strcpy(MCAPID_Registered_Services[avail].service,
118
                               (char*)&buffer[MCAPID_SVCREG_NAME_OFFSET]);
119
 
120
                        /* Store the node and endpoint. */
121
                        MCAPID_Registered_Services[avail].port =
122
                            mcapi_get32(buffer, MCAPID_SVCREG_PORT_OFFSET);
123
                        MCAPID_Registered_Services[avail].node =
124
                            mcapi_get32(buffer, MCAPID_SVCREG_NODE_OFFSET);
125
 
126
                        /* Indicate that this block is taken. */
127
                        MCAPID_Registered_Services[avail].avail = MCAPI_FALSE;
128
 
129
                        /* Set status to success. */
130
                        mcapi_put32(buffer, MCAPID_SVCREG_STATUS_OFFSET, MCAPI_SUCCESS);
131
                    }
132
 
133
                    /* An error occurred. */
134
                    else
135
                    {
136
                        /* Set the status to an error. */
137
                        mcapi_put32(buffer, MCAPID_SVCREG_STATUS_OFFSET, 0xffffffff);
138
                    }
139
                }
140
 
141
                /* If this is a removal request. */
142
                else if (mcapi_get32(buffer, MCAPID_SVCREG_TYPE_OFFSET) == MCAPID_REM_SVC)
143
                {
144
                    /* Find the target service. */
145
                    for (i = 0; i < MCAPID_MAX_SERVICES; i ++)
146
                    {
147
                        /* If the two strings match. */
148
                        if (strcmp((char*)&buffer[MCAPID_SVCREG_NAME_OFFSET],
149
                                   MCAPID_Registered_Services[i].service) == 0)
150
                        {
151
                            /* Indicate that this block is free. */
152
                            MCAPID_Registered_Services[i].avail = MCAPI_TRUE;
153
 
154
                            break;
155
                        }
156
                    }
157
 
158
                    /* Return success regardless. */
159
                    mcapi_put32(buffer, MCAPID_SVCREG_STATUS_OFFSET, MCAPI_SUCCESS);
160
                }
161
 
162
                /* If this is a registration query. */
163
                else if (mcapi_get32(buffer, MCAPID_SVCREG_TYPE_OFFSET) == MCAPID_GET_SVC)
164
                {
165
                    /* Find the target service. */
166
                    for (i = 0; i < MCAPID_MAX_SERVICES; i ++)
167
                    {
168
                        /* If the two strings match. */
169
                        if (strcmp((char*)&buffer[MCAPID_SVCREG_NAME_OFFSET],
170
                                   MCAPID_Registered_Services[i].service) == 0)
171
                        {
172
                            /* Store the node and port in the outgoing buffer. */
173
                            mcapi_put32(buffer, MCAPID_SVCREG_PORT_OFFSET,
174
                                        MCAPID_Registered_Services[i].port);
175
                            mcapi_put32(buffer, MCAPID_SVCREG_NODE_OFFSET,
176
                                        MCAPID_Registered_Services[i].node);
177
 
178
                            /* Set the status to success. */
179
                            mcapi_put32(buffer, MCAPID_SVCREG_STATUS_OFFSET, MCAPI_SUCCESS);
180
 
181
                            break;
182
                        }
183
                    }
184
 
185
                    /* If the service could not be found. */
186
                    if (i == MCAPID_MAX_SERVICES)
187
                    {
188
                        /* Set the status to an error. */
189
                        mcapi_put32(buffer, MCAPID_SVCREG_STATUS_OFFSET, 0xffffffff);
190
                    }
191
                }
192
 
193
                /* Send the reply. */
194
                mcapi_msg_send(rx_endp, mcapi_get32(buffer, MCAPID_SVCREG_RXENDP_OFFSET),
195
                               buffer, rx_len, MCAPI_DEFAULT_PRIO, &status);
196
            }
197
 
198
            else
199
            {
200
                /* Terminate this task. */
201
                MCAPI_Cleanup_Task();
202
 
203
                break;
204
            }
205
        }
206
    }
207
 
208
} /* MCAPID_Registration_Server */
209
 
210
/************************************************************************
211
*
212
*   FUNCTION
213
*
214
*       MCAPID_Remove_Service
215
*
216
*   DESCRIPTION
217
*
218
*       This function issues a de-registration request for a service/endpoint
219
*       tuple.
220
*
221
*************************************************************************/
222
mcapi_status_t MCAPID_Remove_Service(char *service, mcapi_node_t node,
223
                                     mcapi_port_t port)
224
{
225
    unsigned char       buffer[MCAPID_REG_MSG_LEN];
226
    mcapi_endpoint_t    rx_endp, tx_endp;
227
    size_t              rx_len;
228
    mcapi_status_t      status, local_status;
229
 
230
    /* Get the registration service endpoint. */
231
    tx_endp = mcapi_get_endpoint(MCAPID_REG_SERVER_NODE, MCAPID_REG_SERVER_PORT,
232
                                 &status);
233
 
234
    if (status == MCAPI_SUCCESS)
235
    {
236
        /* Create an endpoint for sending the request and receiving the
237
         * reply.
238
         */
239
        rx_endp = mcapi_create_endpoint(MCAPI_PORT_ANY, &status);
240
 
241
        if (status == MCAPI_SUCCESS)
242
        {
243
            /* Set the type to indicate a de-registration request. */
244
            mcapi_put32(buffer, MCAPID_SVCREG_TYPE_OFFSET, MCAPID_REM_SVC);
245
 
246
            /* Store the endpoint to be registered. */
247
            mcapi_put32(buffer, MCAPID_SVCREG_PORT_OFFSET, port);
248
            mcapi_put32(buffer, MCAPID_SVCREG_NODE_OFFSET, node);
249
 
250
            /* Store the endpoint to which the reply should be sent. */
251
            mcapi_put32(buffer, MCAPID_SVCREG_RXENDP_OFFSET, rx_endp);
252
 
253
            /* Store the name of the service. */
254
            strcpy((char*)&buffer[MCAPID_SVCREG_NAME_OFFSET], service);
255
 
256
            /* Send the message. */
257
            mcapi_msg_send(rx_endp, tx_endp, buffer, MCAPID_REG_MSG_LEN,
258
                           MCAPI_DEFAULT_PRIO, &status);
259
 
260
            if (status == MCAPI_SUCCESS)
261
            {
262
                /* Wait for a reply. */
263
                mcapi_msg_recv(rx_endp, buffer, MCAPID_REG_MSG_LEN, &rx_len,
264
                               &status);
265
 
266
                if (status == MCAPI_SUCCESS)
267
                {
268
                    /* Extract the status of the operation. */
269
                    status = mcapi_get32(buffer, MCAPID_SVCREG_STATUS_OFFSET);
270
                }
271
            }
272
 
273
            /* Delete the endpoint. */
274
            mcapi_delete_endpoint(rx_endp, &local_status);
275
        }
276
    }
277
 
278
    return (status);
279
 
280
} /* MCAPID_Remove_Service */
281
 
282
/************************************************************************
283
*
284
*   FUNCTION
285
*
286
*       MCAPID_Register_Service
287
*
288
*   DESCRIPTION
289
*
290
*       This function issues a registration request for a service/endpoint
291
*       tuple.
292
*
293
*************************************************************************/
294
mcapi_status_t MCAPID_Register_Service(char *service, mcapi_node_t node,
295
                                       mcapi_port_t port)
296
{
297
    unsigned char       buffer[MCAPID_REG_MSG_LEN];
298
    mcapi_endpoint_t    rx_endp, tx_endp;
299
    size_t              rx_len;
300
    mcapi_status_t      status, local_status;
301
 
302
    /* Get the registration service endpoint. */
303
    tx_endp = mcapi_get_endpoint(MCAPID_REG_SERVER_NODE, MCAPID_REG_SERVER_PORT,
304
                                 &status);
305
 
306
    if (status == MCAPI_SUCCESS)
307
    {
308
        /* Create an endpoint for sending the request and receiving the
309
         * reply.
310
         */
311
        rx_endp = mcapi_create_endpoint(MCAPI_PORT_ANY, &status);
312
 
313
        if (status == MCAPI_SUCCESS)
314
        {
315
            if (status == MCAPI_SUCCESS)
316
            {
317
                /* Set the type to indicate a registration request. */
318
                mcapi_put32(buffer, MCAPID_SVCREG_TYPE_OFFSET, MCAPID_REG_SVC);
319
 
320
                /* Store the node and port to be registered. */
321
                mcapi_put32(buffer, MCAPID_SVCREG_NODE_OFFSET, node);
322
                mcapi_put32(buffer, MCAPID_SVCREG_PORT_OFFSET, port);
323
 
324
                /* Store the endpoint to which the reply should be sent. */
325
                mcapi_put32(buffer, MCAPID_SVCREG_RXENDP_OFFSET, rx_endp);
326
 
327
                /* Store the name of the service. */
328
                strcpy((char*)&buffer[MCAPID_SVCREG_NAME_OFFSET], service);
329
 
330
                /* Send the message. */
331
                mcapi_msg_send(rx_endp, tx_endp, buffer, MCAPID_REG_MSG_LEN,
332
                               MCAPI_DEFAULT_PRIO, &status);
333
 
334
                if (status == MCAPI_SUCCESS)
335
                {
336
                    /* Wait for a reply. */
337
                    mcapi_msg_recv(rx_endp, buffer, MCAPID_REG_MSG_LEN, &rx_len,
338
                                   &status);
339
 
340
                    if (status == MCAPI_SUCCESS)
341
                    {
342
                        /* Extract the status of the operation. */
343
                        status = mcapi_get32(buffer, MCAPID_SVCREG_STATUS_OFFSET);
344
                    }
345
                }
346
            }
347
 
348
            /* Delete the endpoint. */
349
            mcapi_delete_endpoint(rx_endp, &local_status);
350
        }
351
    }
352
 
353
    return (status);
354
 
355
} /* MCAPID_Register_Service */
356
 
357
/************************************************************************
358
*
359
*   FUNCTION
360
*
361
*       MCAPID_Get_Service
362
*
363
*   DESCRIPTION
364
*
365
*       This function gets the node and port associated with a service.
366
*
367
*************************************************************************/
368
mcapi_status_t MCAPID_Get_Service(char *service, mcapi_endpoint_t *endp)
369
{
370
    unsigned char       buffer[MCAPID_REG_MSG_LEN];
371
    mcapi_endpoint_t    rx_endp, tx_endp;
372
    size_t              rx_len;
373
    mcapi_status_t      status, local_status;
374
 
375
    /* Get the registration service endpoint. */
376
    tx_endp = mcapi_get_endpoint(MCAPID_REG_SERVER_NODE, MCAPID_REG_SERVER_PORT,
377
                                 &status);
378
 
379
    if (status == MCAPI_SUCCESS)
380
    {
381
        /* Create an endpoint for sending the request and receiving the
382
         * reply.
383
         */
384
        rx_endp = mcapi_create_endpoint(MCAPI_PORT_ANY, &status);
385
 
386
        if (status == MCAPI_SUCCESS)
387
        {
388
            /* Set the type to indicate a get request. */
389
            mcapi_put32(buffer, MCAPID_SVCREG_TYPE_OFFSET, MCAPID_GET_SVC);
390
 
391
            /* Store the endpoint to which the reply should be sent. */
392
            mcapi_put32(buffer, MCAPID_SVCREG_RXENDP_OFFSET, rx_endp);
393
 
394
            /* Store the name of the service. */
395
            strcpy((char*)&buffer[MCAPID_SVCREG_NAME_OFFSET], service);
396
 
397
            /* Send the message. */
398
            mcapi_msg_send(rx_endp, tx_endp, buffer, MCAPID_REG_MSG_LEN,
399
                           MCAPI_DEFAULT_PRIO, &status);
400
 
401
            if (status == MCAPI_SUCCESS)
402
            {
403
                /* Wait for a reply. */
404
                mcapi_msg_recv(rx_endp, buffer, MCAPID_REG_MSG_LEN, &rx_len,
405
                               &status);
406
 
407
                if (status == MCAPI_SUCCESS)
408
                {
409
                    /* Extract the status. */
410
                    status = mcapi_get32(buffer, MCAPID_SVCREG_STATUS_OFFSET);
411
 
412
                    if (status == MCAPI_SUCCESS)
413
                    {
414
                        /* Extract the node and port. */
415
                        mcapi_port_t port;
416
                        mcapi_node_t node;
417
 
418
                        port = mcapi_get32(buffer, MCAPID_SVCREG_PORT_OFFSET);
419
                        node = mcapi_get32(buffer, MCAPID_SVCREG_NODE_OFFSET);
420
 
421
                        *endp = mcapi_get_endpoint(node, port, &status);
422
                    }
423
                }
424
            }
425
 
426
            /* Delete the endpoint. */
427
            mcapi_delete_endpoint(rx_endp, &local_status);
428
        }
429
    }
430
 
431
    return (status);
432
 
433
} /* MCAPID_Get_Service */
434
 
435
/************************************************************************
436
*
437
*   FUNCTION
438
*
439
*       MCAPID_Finished
440
*
441
*   DESCRIPTION
442
*
443
*       Infinite loop entered when the demonstration has completed.
444
*
445
*************************************************************************/
446
void MCAPID_Finished(void)
447
{
448
    for (;;)
449
    {
450
        MCAPID_Sleep(1000);
451
    }
452
} /* MCAPID_Finished */

powered by: WebSVN 2.1.0

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