/****************************************************************************/
/*	Copyright (c) 2009, Bendor Research Pty. Ltd. All rights reserved.		*/
/*	This software is released under the GNU General Public License, version	*/
/*	3, with the exemptions that 1) the header files installed by this 	 	*/
/*	package can be included in closed-source code and 2) the libraries		*/
/*	built by this package can be statically linked into a closed-source 	*/
/*	binary. See the file LICENSE for the details.							*/
/*	This software is provided "AS IS", without any warranty or guarantee.	*/
/*	By using this software you acknowledge that neither Bendor Research nor	*/
/*	any of its personnel or affiliates can be held liable for any damages,	*/
/*	including, but not limited to, loss of business, loss of money, loss of */
/*	life, loss of reputation, arising out of this software being faulty or	*/
/*	not performing as advertised, even if Bendor Research was aware of such	*/
/*	faults in the software. You use this software at your own risk, any and */
/*	all liabilities that arise out of your using this software are strictly	*/
/*	yours.																	*/
/****************************************************************************/

#include "coop.h"

/** @addtogroup coop_port */
/** @{ */

/**
*	Sends a message to a port.
*
*	A message is sent to the port. The port must be open.
*	The caller is not suspended. The owner of the port is notified that
*	a message is available. This function can be called from interrupts.
*
*	@param	port	The ID of the port
*
*	@param	type	A message type, a single byte object that will be
*					copied to the 'type' field of the BZ_MESG structure
*					when the owner of the port reads the message.
*
*	@param	spar	A 2-byte message parameter that will be copied into
*					the 'spar' field of the BZ_MESG structure when the
*					owner of the port reads the message.

*
*	@param	lpar	A 4-byte message parameter that will be copied into
*					the 'lpar' field of the BZ_MESG structure when the
*					owner of the port reads the message.
*
*	@retval	BZ_ILLEGAL_PORT	The specified port ID is not valid
*	@retval	BZ_NOT_OPEN		The specified port is not open
*	@retval	BZ_OUT_OF_MESG	The kernel has no free message buffers
*	@retval	0				Success
*
*/

int		BZ_PortSend( int port, int type, int spar, int lpar )
{
BZ_STAT	*base = &_bz_stat;
BZ_PORT	*pptr;
BZ_MESG	*mesg;
int		stat, indx;

	if ( (unsigned) port >= base->pnum )

		return( BZ_ILLEGAL_PORT );

	pptr = base->port + port;

	if ( pptr->pid == NIL )

		return( BZ_NOT_OPEN );

	stat = BZ_IrqDisable( 1 );

	if ( ( indx = base->free ) == NIL ) {

		// Message pool is empty

		BZ_IrqDisable( stat );
		return( BZ_OUT_OF_MESG );
	}

	// Get a free message buffer from the pool

	mesg = base->pool + indx;
	base->free = mesg->link;

	// Store the user info in it

	mesg->type = type;
	mesg->spar = spar;
	mesg->lpar = lpar;
	mesg->link = NIL;

	// Append to the list on the port

	if ( pptr->tail == NIL ) {

		type = 1;
		pptr->head = indx;
	}
	else {

		type = 0;
		base->pool[ pptr->tail ].link = indx;
	}

	pptr->tail = indx;

	// If there were no messages on the port, post an event.
	// If there were messages already, then the event was sent
	// previously so we can save a little time by not sending it again.

	BZ_IrqDisable( stat );
	if ( type ) BZ_EventPost( pptr->pid, 1 << pptr->sig );
	return( 0 );
}

/** @} */

