/*
 *               Copyright (C) 2005, BroadOn Communications Corp.
 *
 *  These coded instructions, statements, and computer programs contain
 *  unpublished  proprietary information of BroadOn Communications Corp.,
 *  and  are protected by Federal copyright law. They may not be disclosed
 *  to  third  parties or copied or duplicated in any form, in whole or in
 *  part, without the prior written consent of BroadOn Communications Corp.
 *
 */
#ifndef __MESSAGE_H__
#define __MESSAGE_H__

#define IOS_MESSAGE_QUEUE_MAX 256

#define IOS_MESSAGE_QUEUE_EMPTYQ 0
#define IOS_MESSAGE_QUEUE_FULLQ  4
#define IOS_MESSAGE_QUEUE_OWNER  8
#define IOS_MESSAGE_QUEUE_COUNT  12
#define IOS_MESSAGE_QUEUE_RPTR   16
#define IOS_MESSAGE_QUEUE_SIZE   20
#define IOS_MESSAGE_QUEUE_BUFFER 24

#define IOS_EVENT_STATE_MQ       0
#define IOS_EVENT_STATE_MSG      4
#define IOS_EVENT_STATE_OWNER    8
#define IOS_EVENT_STATE_SIZE     16

#define CHECK_MESSAGE_QUEUE_ID(id) \
    if (((id) < 0) || ((id) >= IOS_MESSAGE_QUEUE_MAX)) {\
        rv = IOS_ERROR_INVALID; \
        goto end; \
    } else

#define CHECK_MESSAGE_QUEUE_FLAGS(flag) \
    if (((flag) != IOS_MESSAGE_BLOCK) && ((flag) != IOS_MESSAGE_NOBLOCK)) {\
        rv = IOS_ERROR_INVALID; \
        goto end; \
    } else

#define CHECK_MESSAGE_QUEUE_OWNER(mq) \
    if ((mq)->owner != THIS_THREAD->pid) {\
        rv = IOS_ERROR_ACCESS; \
        goto end; \
    } else

#define CHECK_EVENT(event) \
    if ((event) >= IOS_NUM_EVENTS) { \
        rv = IOS_ERROR_INVALID; \
        goto end; \
    } else

#if defined(_LANGUAGE_C)
typedef struct {
    IOSThread *emptyQ;
    IOSThread *fullQ;
    u32 owner;
    s32 count;
    s32 rptr;
    s32 size;
    IOSMessage *buffer;
} IOSMessageQueue;

typedef struct {
    IOSMessageQueue *messageQueue;
    IOSMessage message;
    u32 owner;
    u32 pad;
} IOSEventState;

extern IOSMessageQueue __iosQueues[IOS_MESSAGE_QUEUE_MAX];

IOSMessageQueueId iosCreateMessageQueue(IOSMessage *msgarray, u32 size);
IOSError __iosSendMessage(IOSMessageQueue *mq, IOSMessage msg, u32 flag);
IOSError __iosReceiveMessage(IOSMessageQueue *mq, IOSMessage *msg, u32 flag);
IOSError iosReceiveMessage(IOSMessageQueueId id, IOSMessage *msg, u32 flag);
IOSError iosReceiveMessageInt(IOSMessageQueueId id, IOSMessage *msg, u32 flag);
IOSError iosSendMessageInt(IOSMessageQueueId id, IOSMessage msg, u32 flag);
IOSError iosHandleEvent(IOSEvent event, IOSMessageQueueId id, IOSMessage msg);

#define LOCK_QUEUES  __iosDisableInt
#define UNLOCK_QUEUES(mask)  __iosRestoreInt((mask))

#endif /* _LANGUAGE_C */

#endif /*__MESSAGE_H__*/
