summaryrefslogtreecommitdiffstats
path: root/cpukit/itron/src/msgbuffer.c
blob: 4711ddbea90fdad16f2735c04e2f6bdd8f689d7d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/*
 *  The license and distribution terms for this file may be
 *  found in the file LICENSE in this distribution or at
 *  http://www.OARcorp.com/rtems/license.html.
 *
 *  $Id$
 */

#include <itron.h>

#include <rtems/itron/msgbuffer.h>
#include <rtems/itron/task.h>

ER _ITRON_Message_buffer_Translate_core_message_buffer_return_code(
    CORE_message_queue_Status status)
{
    switch (status) {
    case CORE_MESSAGE_QUEUE_STATUS_SUCCESSFUL:
        return E_OK;
    case CORE_MESSAGE_QUEUE_STATUS_TOO_MANY:
        return E_TMOUT;
    case CORE_MESSAGE_QUEUE_STATUS_INVALID_SIZE:
        return E_PAR;
    case CORE_MESSAGE_QUEUE_STATUS_UNSATISFIED_NOWAIT:
        return E_TMOUT;
    case CORE_MESSAGE_QUEUE_STATUS_TIMEOUT:
        return E_TMOUT;
    default:
        return E_ID;
    }
}

/*    
 *  _ITRON_Message_buffer_Manager_initialization
 *  
 *  This routine initializes all message buffer manager related data
 *  structures. 
 *
 *  Input parameters:
 *    maximum_message_buffers - maximum configured message buffers
 *
 *  Output parameters:  NONE
 */

void _ITRON_Message_buffer_Manager_initialization(
    unsigned32 maximum_message_buffers
    ) 
{
    _Objects_Initialize_information(
        &_ITRON_Message_buffer_Information, /* object information table */
        OBJECTS_ITRON_MESSAGE_BUFFERS,      /* object class */
        FALSE,                              /* TRUE if this is a
                                               global object class */ 
        maximum_message_buffers,            /* maximum objects of this class */
        sizeof( ITRON_Message_buffer_Control ),  /* size of this
                                                    object's control
                                                    block */ 
        FALSE,                         /* TRUE if names for this
                                          object are strings */ 
        RTEMS_MAXIMUM_NAME_LENGTH,     /* maximum length of each
                                          object's name */ 
        FALSE                          /* TRUE if this class is threads */
        );
    
    /*
     *  Register the MP Process Packet routine.
     *
     *  NOTE: No MP Support YET in RTEMS ITRON implementation.
     */
 
} 

/*
 *  cre_mbf - Create MessageBuffer
 */

ER cre_mbf(
    ID      mbfid,
    T_CMBF *pk_cmbf
    )
{
    CORE_message_queue_Attributes   the_message_queue_attributes;
    ITRON_Message_buffer_Control    *the_message_buffer;

    /*
     *  Bad pointer to the attributes structure
     */

    if ( !pk_cmbf )
        return E_PAR;

    /*
     *  Bits were set that were note defined.
     */

    if (pk_cmbf->mbfatr & ~(TA_TPRI))
        return E_RSATR;

    if (pk_cmbf->bufsz < 0 || pk_cmbf->maxmsz < 0)
        return E_PAR;
    
    if (pk_cmbf->bufsz < pk_cmbf->maxmsz)
        return E_PAR;

    _Thread_Disable_dispatch();             /* prevents deletion */

    the_message_buffer = _ITRON_Message_buffer_Allocate(mbfid);
    if ( !the_message_buffer ) {
        _Thread_Enable_dispatch();
        return _ITRON_Message_buffer_Clarify_allocation_id_error(mbfid);
    }

    if ( pk_cmbf->mbfatr & TA_TPRI )
        the_message_queue_attributes.discipline =
            CORE_MESSAGE_QUEUE_DISCIPLINES_PRIORITY;
    else
        the_message_queue_attributes.discipline =
            CORE_MESSAGE_QUEUE_DISCIPLINES_FIFO;

    _CORE_message_queue_Initialize(
        &the_message_buffer->message_queue,
        OBJECTS_ITRON_MESSAGE_BUFFERS,
        &the_message_queue_attributes,
        pk_cmbf->bufsz / pk_cmbf->maxmsz,
        pk_cmbf->maxmsz,
        NULL                           /* Multiprocessing not supported */
        );

    _ITRON_Objects_Open( &_ITRON_Message_buffer_Information,
                         &the_message_buffer->Object );
    
    /*
     *  If multiprocessing were supported, this is where we would announce
     *  the existence of the semaphore to the rest of the system.
     */

#if defined(RTEMS_MULTIPROCESSING)
#endif

    _Thread_Enable_dispatch();

    return E_OK;
}

/*
 *  del_mbf - Delete MessageBuffer
 */

ER del_mbf(
    ID mbfid
    )
{
    ITRON_Message_buffer_Control  *the_message_buffer;
    Objects_Locations              location;

    the_message_buffer = _ITRON_Message_buffer_Get(mbfid, &location);
    
    switch (location) {
    case OBJECTS_REMOTE:
    case OBJECTS_ERROR:           /* Multiprocessing not supported */
        return _ITRON_Message_buffer_Clarify_get_id_error(mbfid);

    case OBJECTS_LOCAL:
        _CORE_message_queue_Flush(&the_message_buffer->message_queue);
        _ITRON_Objects_Close(
            &_ITRON_Message_buffer_Information,
            &the_message_buffer->Object);
        _ITRON_Message_buffer_Free(the_message_buffer);
    /*
     *  If multiprocessing were supported, this is where we would announce
     *  the existence of the semaphore to the rest of the system.
     */

#if defined(RTEMS_MULTIPROCESSING)
#endif
        _Thread_Enable_dispatch();
        return E_OK;
    }
    
    return E_OK;
}

/*
 *  snd_mbf - Send Message to MessageBuffer
 */

ER snd_mbf(
    ID  mbfid,
    VP  msg,
    INT msgsz
    )
{
    return E_OK;
}

/*
 *  psnd_mbf - Poll and Send Message to MessageBuffer
 */

ER psnd_mbf(
    ID  mbfid,
    VP  msg,
    INT msgsz
    )
{
    ITRON_Message_buffer_Control  *the_message_buffer;
    Objects_Locations              location;
    CORE_message_queue_Status      status;

    if (msgsz <= 0 || !msg)
        return E_PAR;
    
    the_message_buffer = _ITRON_Message_buffer_Get(mbfid, &location);
    switch (location) {
    case OBJECTS_REMOTE:
    case OBJECTS_ERROR:           /* Multiprocessing not supported */
        return _ITRON_Message_buffer_Clarify_get_id_error(mbfid);

    case OBJECTS_LOCAL:
        status = _CORE_message_queue_Submit(
            &the_message_buffer->message_queue,
            msg,
            msgsz,
            the_message_buffer->Object.id,
            NULL,
            CORE_MESSAGE_QUEUE_SEND_REQUEST);
        _Thread_Enable_dispatch();
        return
            _ITRON_Message_buffer_Translate_core_message_buffer_return_code(
                status);
    }

    /*
     *  If multiprocessing were supported, this is where we would announce
     *  the existence of the semaphore to the rest of the system.
     */

#if defined(RTEMS_MULTIPROCESSING)
#endif

    return E_OK;
}

/*
 *  tsnd_mbf - Send Message to MessageBuffer with Timeout
 */

ER tsnd_mbf(
    ID  mbfid,
    VP  msg,
    INT msgsz,
    TMO tmout
    )
{
    return E_OK;
}

/*
 *  rcv_mbf - Receive Message from MessageBuffer
 */

ER rcv_mbf(
    VP   msg,
    INT *p_msgsz,
    ID   mbfid
    )
{
    return trcv_mbf(msg, p_msgsz, mbfid, TMO_FEVR);
}

/*
 *  prcv_mbf - Poll and Receive Message from MessageBuffer
 */

ER prcv_mbf(
    VP   msg,
    INT *p_msgsz,
    ID   mbfid
    )
{
    return trcv_mbf(msg, p_msgsz, mbfid, TMO_POL);
}

/*
 *  trcv_mbf - Receive Message from MessageBuffer with Timeout
 */

ER trcv_mbf(
    VP   msg,
    INT *p_msgsz,
    ID   mbfid,
    TMO  tmout
    )
{
    ITRON_Message_buffer_Control  *the_message_buffer;
    Objects_Locations              location;
    CORE_message_queue_Status      status;
    boolean                        wait;
    Watchdog_Interval              interval;

    interval = 0;
    if (tmout == TMO_POL) {
      wait = FALSE;
    } else {
      wait = TRUE;
      if (tmout != TMO_FEVR) 
	interval = TOD_MILLISECONDS_TO_TICKS(tmout);
    }

    if (wait && _ITRON_Is_in_non_task_state() ) 
      return E_CTX;

    if (!p_msgsz || !msg || tmout <= -2)
        return E_PAR;
    
    the_message_buffer = _ITRON_Message_buffer_Get(mbfid, &location);
    switch (location) {
    case OBJECTS_REMOTE:
    case OBJECTS_ERROR:           /* Multiprocessing not supported */
        return _ITRON_Message_buffer_Clarify_get_id_error(mbfid);

    case OBJECTS_LOCAL:
        _CORE_message_queue_Seize(
            &the_message_buffer->message_queue,
            the_message_buffer->Object.id,
            msg,
            p_msgsz,
            wait,
            interval);
        _Thread_Enable_dispatch();
	status =
            (CORE_message_queue_Status)_Thread_Executing->Wait.return_code; 
	return
            _ITRON_Message_buffer_Translate_core_message_buffer_return_code
            (status);
        
    }

    /*
     *  If multiprocessing were supported, this is where we would announce
     *  the existence of the semaphore to the rest of the system.
     */

#if defined(RTEMS_MULTIPROCESSING)
#endif
    return E_OK;
}

/*
 *  ref_mbf - Reference MessageBuffer Status
 */

ER ref_mbf(
    T_RMBF *pk_rmbf,
    ID      mbfid
    )
{
    ITRON_Message_buffer_Control *the_message_buffer;
    Objects_Locations             location;
        
    if ( !pk_rmbf )
        return E_PAR;   /* XXX check this error code */

    the_message_buffer = _ITRON_Message_buffer_Get( mbfid, &location );
    switch ( location ) {   
    case OBJECTS_REMOTE:               /* Multiprocessing not supported */
    case OBJECTS_ERROR:
        return _ITRON_Message_buffer_Clarify_get_id_error( mbfid );
  
    case OBJECTS_LOCAL:
        /*
         *  Fill in the size of message to be sent
         */

        if(the_message_buffer->message_queue.
           number_of_pending_messages == 0) {
            pk_rmbf->msgsz = 0;
        }
        else {
            CORE_message_queue_Buffer_control *the_message;
            the_message = (CORE_message_queue_Buffer_control*)
                the_message_buffer->message_queue.
                Pending_messages.first;
            pk_rmbf->msgsz = the_message->Contents.size;
        }
        
        /*
         *  Fill in the size of free buffer
         */

        pk_rmbf->frbufsz =
            (the_message_buffer->message_queue.maximum_pending_messages- 
             the_message_buffer->message_queue.number_of_pending_messages)*
            the_message_buffer->message_queue.maximum_message_size;


        /*
         *  Fill in whether or not there is a waiting task
         */

        if ( !_Thread_queue_First(
            &the_message_buffer->message_queue.Wait_queue ) ) 
            pk_rmbf->wtsk = FALSE;
        else
            pk_rmbf->wtsk =  TRUE;

        pk_rmbf->stsk = FALSE;
        _Thread_Enable_dispatch();
        return E_OK;
    }   
    return E_OK;
}