/* ---------------------------------------------------------------------------
 * protocolfunctions.c - 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: Initialisation and direct usage of the Bus Protocol
 * ---------------------------------------------------------------------------
 * Version(s):  0.1, Jun 2, 2008, MMi
 *                   Creation.
 * ---------------------------------------------------------------------------
 */

/* ---------------------------------------------------------------------------
 * System include files
 * ---------------------------------------------------------------------------
 */
#include "LPC23xx.h"
#include "types.h"

#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"
/* ---------------------------------------------------------------------------
 * Application include files
 * ---------------------------------------------------------------------------
 */
#include "protocolfunctions.h"
#include "BusProtocol.h"

#include "adc.h"
#include "dac.h"
#include "bus.h"
#include "dio.h"
#include "serial.h" 
#include "SerOut.h"
#include "topoftest.h"

#include "remote_digital.h"
#include "remote_analogue.h"
#include "remote_relay.h"
/* ---------------------------------------------------------------------------
 * Local constant and macro definitions
 * ---------------------------------------------------------------------------
 */

/* ---------------------------------------------------------------------------
 * Global variable definitions
 * ---------------------------------------------------------------------------
 */


extern BOOLEAN auto_testResult;
extern UINT32 UINT32result;
/* ---------------------------------------------------------------------------
 * Local variable definitions
 * ---------------------------------------------------------------------------
 */

/* ---------------------------------------------------------------------------
 * Local function definitions
 * ---------------------------------------------------------------------------
 */   

void protocolInit (t_bus_devices BUS_ID, UINT32 DEV_ID, UINT32 MAX_ID)
{
    /* Create both needed Semaphores to be able to use them                 */
    vSemaphoreCreateBinary (generalSemaphore);
    vSemaphoreCreateBinary (slaveRestartSemaphore);

    /* Init Device as Master of BUS1 System                                 */
    handleBus1 = bpInit (BUS2, BUS1, DEV_ID, MAX_ID, 16);
    
    /* Attach basicly needed Handshake Functions                            */
    bpAttachRpc (handleBus1, 1, "AvailableID", (t_rpc_remote_procedure_call) availableBusID, 0);
    bpAttachRpcResult(handleBus1, 1, (t_bp_rpcresult_callback) generalResultFunction, 0);
    
    /* Following Attachments depend on Device ID                            */
    if (DEV_ID == 1)
        {
        /* If Device is Master, attach these functions                      */
        /* Attach Slave Result Functions (called by Slave on the Master)    */
        bpAttachRpcResult(handleBus1, 2, (t_bp_rpcresult_callback) testResultFunction, 1);
        bpAttachRpcResult(handleBus1, 3, (t_bp_rpcresult_callback) slaveRestartFunction, 0);
        bpAttachRpcResult(handleBus1, 4, (t_bp_rpcresult_callback) UINT32ResultFunction, 1);
        bpAttachRpcResult(handleBus1, 9, (t_bp_rpcresult_callback) receiveRemoteAttachedFunctions, 1);
        
        /* Attach test result functions */
        bpAttachRpcResult(handleBus1, 11, (t_bp_rpcresult_callback) digitalReadResult, 1);
        bpAttachRpcResult(handleBus1, 12, (t_bp_rpcresult_callback) analogueReadResult, 1);
        bpAttachRpcResult(handleBus1, 13, (t_bp_rpcresult_callback) digitalReadAllResult, maxDI_Channels);
        bpAttachRpcResult(handleBus1, 14, (t_bp_rpcresult_callback) analogueReadAllResult, maxDAC_Channels);
        
        /* Attach semaphore release functions */
        bpAttachRpcResult(handleBus1, 20, (t_bp_rpcresult_callback) remoteDigitalSemaphoreRelease, 0);
        bpAttachRpcResult(handleBus1, 21, (t_bp_rpcresult_callback) remoteAnalogueSemaphoreRelease, 0);
        bpAttachRpcResult(handleBus1, 22, (t_bp_rpcresult_callback) remoteRelaySemaphoreRelease, 0);
        
        }
    
    if (DEV_ID > 1)
        {
        /* If Device is Slave, attach these functions                       */
        } 
}

void availableBusID (UINT8 senderId, UINT8 targetId, UINT8 requestNr, 
                     UINT8 functionId, UINT8 nrOfParams, UINT32 *params)
{
    /* Just call first Result Function on Slave                             */
    bpSendRpcResult(handleBus1, 2, 1, 1, 0, NULL);
}

/* generalResultFunction is the easiest Way to realise a Handshake between
 * Master and Slave. Master software takes a Semaphore and calls a function
 * on Slave, that is only calling generalResultfunction() on Master. If this
 * happens, the Semaphore will be released and Handshake is done
 */
void generalResultFunction (UINT8 requestNr, UINT8 nrOfResults, UINT32 *results)
{
     /* If called, release the generalSemaphore                             */
    if (xSemaphoreGive (generalSemaphore) == pdTRUE)
        {
        }
    else 
        {
        }
}

/* Test functions called on Slave will return their Test Results with a 
 * call on this function. Results on Slave are from type BOOLEAN
 */
void testResultFunction (UINT8 requestNr, UINT8 nrOfResults, UINT32 *results)
{
    /* Set Slave Test result depending on what slave sends back as parameter*/
    if (*results == 1)
        {
        /* Test was successfull                                             */
        auto_testResult = TRUE;
        }
    else
        {
        /* Test was a failure                                               */
        auto_testResult = FALSE;   
        }
}


void slaveRestartFunction (UINT8 requestNr, UINT8 nrOfResults, UINT32 *results)
{
    /* Release slaveRestartSemaphore if Slave calls this Function on Reboot */
    if (xSemaphoreGive (slaveRestartSemaphore) == pdTRUE)
        {
        }
    else 
        {
        }
}


void UINT32ResultFunction (UINT8 requestNr, UINT8 nrOfResults, UINT32 *results)
{
    UINT32result = *results;
}


void receiveRemoteAttachedFunctions (UINT8 requestNr, UINT8 nrOfResults, UINT32 *results)
{
    sendString (SerOutPort, TRUE, importantMessage, (char *) results, Dummy, Dummy);
}
