/* ---------------------------------------------------------------------------
 * remote_misc.c                                        (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, Nov 11, 2008, MMi
 *                   Creation.
 * ---------------------------------------------------------------------------
 */

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

/* FreeRTOS includes */
#include "FreeRTOS.h"
#include "Task.h"
#include "Semphr.h"
/* ---------------------------------------------------------------------------
 * Application include files
 * ---------------------------------------------------------------------------
 */ 
#include "BusProtocol.h"
#include "protocolfunctions.h"

#include "remote_misc.h"
#include "SerOut.h"

/* ---------------------------------------------------------------------------
 * Local constant and macro definitions
 * ---------------------------------------------------------------------------
 */
#define ident_calibrated                0x55
#define ident_default                   0xAA
/* ---------------------------------------------------------------------------
 * Global variable definitions
 * ---------------------------------------------------------------------------
 */
extern UINT32 UINT32result;                 /* from topoftests.c            */
/* ---------------------------------------------------------------------------
 * Local variable definitions
 * ---------------------------------------------------------------------------
 */

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

#if ((PINSET_TESTER == 1) || (PINSET_TESTER == 2)) 
BOOLEAN makeHandshake (void)
{
/* Make Handshake with first Slave device.
 * Take Semaphore, call remote function and take Semaphore again with
 * waitcycle. Semaphore can just be taken when remote function
 * finished and returns call to release Semaphore.
 */
    
    BOOLEAN handshakeResult;
    
    xSemaphoreTake(generalSemaphore, 1);    /* Take generalSemaphore        */
    bpSendCallRpc (handleBus1, 2, 1, 0, NULL);  /* Call Slaves Handshake    */
    if (xSemaphoreTake(generalSemaphore, 2000) == pdPASS)
        {                                    /* Wait for Handshake done     */
        handshakeResult = TRUE;
        }
    else
        {
        handshakeResult = FALSE;
        }
    
    return (handshakeResult);
}
#endif

UINT8 returnRemoteCalibrationStatus (INT32 correctionType)
{
    xSemaphoreTake(generalSemaphore, 0);
    bpSendCallRpc(handleBus1, 2, 13, 1, &correctionType);
    if (xSemaphoreTake(generalSemaphore, 5000) != pdPASS)
        {
        /* Semaphore is not released, timeout in transmission   */
        xSemaphoreGive (generalSemaphore);
        UINT32result = 0;
        }
    else
        {
        if (UINT32result & ident_default)
            {
            }
        else if (UINT32result & ident_calibrated)
            {
            }
        else
            {
            }
        }
    return ((UINT8) UINT32result);
}


UINT8 showRemoteCalibrationStatus (INT32 correctionType)
{
    xSemaphoreTake(generalSemaphore, 0);
    bpSendCallRpc(handleBus1, 2, 13, 1, &correctionType);
    if (xSemaphoreTake(generalSemaphore, 5000) != pdPASS)
        {
        /* Semaphore is not released, timeout in transmission   */
        xSemaphoreGive (generalSemaphore);
        }
    else
        {
        if (UINT32result & ident_default)
            {
            sendString (SerOutPort, FALSE, testMessage,
                    "Values are in DEFAULT Mode", Dummy, Dummy);
            }
        else if (UINT32result & ident_calibrated)
            {
            sendString (SerOutPort, FALSE, testMessage,
                    "Values are in CALIBRATED Mode", Dummy, Dummy);
            }
        else
            {
            sendString (SerOutPort, FALSE, testMessage,
                    "NO VALID VALUES IN EEPROM", Dummy, Dummy);
            }
        }
    return ((UINT8) UINT32result);
}


