/* ---------------------------------------------------------------------------
 * BusProtocol.h - v0.1                                 (c) 2008 Micro-key bv
 * ---------------------------------------------------------------------------
 * Micro-key bv
 * Industrieweg 28, 9804 TG Noordhorn
 * Postbus 92, 9800 AB Zuidhorn
 * The Netherlands
 * Tel: +31 594 503020
 * Fax: +31 594 505825
 * Email: support@microkey.nl
 * Web: www.microkey.nl
 * ---------------------------------------------------------------------------
 * Description: 
 * ---------------------------------------------------------------------------
 * Version(s):  0.1, Jan 28, 2008, FSc
 *                   Creation.
 * ---------------------------------------------------------------------------
 */
#ifndef __BUSPROTOCOL_H__
#define __BUSPROTOCOL_H__
/** \file BusProtocol.h 
    \brief Implementation of BusProtocol
*/

/* ---------------------------------------------------------------------------
 * System include files.
 * ---------------------------------------------------------------------------
 */

/* ---------------------------------------------------------------------------
 * Application include files.
 * ---------------------------------------------------------------------------
 */
#include <stdio.h>
#include "BpPort.h"
#include "bus.h"
#include "RemoteProcedureCalls.h"
#include "BpMessageFormat.h"

/* ---------------------------------------------------------------------------
 * Constant and macro definitions.
 * ---------------------------------------------------------------------------
 */
#define MASTER_DEVICE_ID    (1)
#define MAX_PAYLOAD_SIZE    (50)
#define BP_DEBUG_OUT(a)     /*printf("%c", a); fflush( stdout );*/

/* ---------------------------------------------------------------------------
 * Type definitions.
 * ---------------------------------------------------------------------------
 */
typedef void (*t_bp_myturn_callback)(void);
typedef void (*t_bp_rpcresult_callback)( UINT8 requestNr, UINT8 nrOfResults, UINT32 *results );
typedef void (*t_bp_messagehandler)(t_bpmsg_message *receivedMessage, int ownHandler );

/* ---------------------------------------------------------------------------
 * Variable declarations.
 * ---------------------------------------------------------------------------
 */

/* ---------------------------------------------------------------------------
 * Function declarations.
 * ---------------------------------------------------------------------------
 */

/** \brief Initialises the BusProtocol */
int bpInit( t_bus_devices bus, UINT8 deviceId, UINT8 highestDeviceId, UINT8 inputQueueSize );

/** \brief Closes the active BusProtocol */
void bpDeinit( int handle );

/** \brief Indicates whether a message a device is received in the last 10 seconds 
 * Only used by the master 
 */
BOOLEAN bpDeviceIsDetected( int handle, UINT8 deviceId );

/** \brief Sends message to pass turn (Nothing to send) */
void bpSendPassTurn( int handle );

/** \brief Sends message to reset another bus device */
void bpSendResetClient( int handle, UINT8 deviceId );

/** \brief Sends message with all electronic information (DAC's, ADC's and digital I/O) */
void bpSendGiveElectronicStatus( int handle, 
	     UINT8 nrOfAdcValues,
	     UINT16 *adcValues,
	     UINT8 nrOfDacValues,
	     UINT16 *dacValues,
	     UINT8 nrOfDiValues,
	     UINT8 *diValues,
	     UINT8 nrOfDoValues,
	     UINT8 *doValues
	   );

/** \brief Sends message to set a DAC on another bus device */
void bpSendSetDacValue( int handle, UINT8 deviceId, UINT8 channelNr, UINT8 dacMode, UINT16 dacValue );

/** \brief Sends message to set the values of all DAC's on another bus device */
void bpSendSetAllDacValues( int handle, UINT8 deviceId, UINT16 *dacValue );

/** \brief Sends message to set a digital out on another bus device */
void bpSendSetDigitalOutValue( int handle, UINT8 deviceId, UINT8 bitNr, UINT8 value );

/** \brief Sends message to set all digital out ports at once  on another bus device */
void bpSendSetAllDigitalOut( int handle, UINT8 deviceId, UINT8 bits);

/** \brief Sends message to set all outputs (analogue & digital)  on another bus device */
void bpSendSetAllOutput( int handle, UINT8 deviceId, UINT8 bits, UINT16 *dacValue);

/** \brief Sends message to call an Remote Procedure Call on an other bus device */
void bpSendCallRpc( int handle, UINT8 deviceId, UINT8 functionId, UINT8 nrOfParams, INT32 *params );

/** \brief Attachs a callback, which is called when it is the device its turn to send data on the bus */
void bpAttachOnMyTurn( int handle, t_bp_myturn_callback onMyTurnCallback );

/** \brief Detaches the above callback */
void bpDetachOnMyTurn( int handle, t_bp_myturn_callback onMyTurnCallback );

/** \brief Attach callback on receiving a specific message */
void bpAttachMessageHandler( int handle, UINT8 messageId, t_bp_messagehandler messageHandler);

/** \brief Attach callback on receiving a specific message */
void bpDetachMessageHandler( int handle, UINT8 messageId, t_bp_messagehandler messageHandler);

/** \brief Attachs a RPC-function, which can be called by another bus device */
void bpAttachRpc( int handle, UINT8 functionId, char * functionName, t_rpc_remote_procedure_call functionPointer, UINT8 nrOfParams );

/** \brief Detaches the above RPC-function */
void bpDetachRpc( int handle, UINT8 functionId );

/** \brief Attachs a "RPC result"-function, which is a result of a requeste RPC-call  on another bus device */
void bpAttachRpcResult( int handle, UINT8 functionId, t_bp_rpcresult_callback functionPointer, UINT8 nrOfResult  );

/** \brief Detaches the above "RPC result"-function */
void bpDetachRpcResult( int handle, UINT8 functionId );

t_rpc_entity *bpLookupRpcEntry( int handle, UINT8 functionId );

/** \brief Sends message to give result on issued RPC-function */
void bpSendRpcResult( int handle, UINT8 deviceId, UINT8 functionId, UINT8 requestNr, UINT8 nrOfResults, INT32 *results );

#endif /* __BUSPROTOCOL_H__ */
