/**
 * Simple file I/O based IVS and PSAP modems.
 *
 * @copyright 2008, research in motion ltd.
 *
 * @version $id: //depot/main/dsp/ecall/c/testlab/testlab_sock.c#3 $
 */

#include <stdio.h>
#include "modem.h"

static FILE *ivsIn = NULL;
static FILE *ivsOut = NULL;
static FILE *psapIn = NULL;
static FILE *psapOut = NULL;

void IvsReset(const Ord8 *msd, int length)
{
    /* These will be closed when the process exits */
    if(!ivsIn) ivsIn = fopen("ivs_in.pcm", "rb");
    if(!ivsOut) ivsOut = fopen("ivs_out.pcm", "wb");
}

void IvsProcess(Int16 *pcm)
{
    if(ivsOut) {
        fwrite(pcm, sizeof(Int16), PCM_LENGTH, ivsOut);
        fflush(ivsOut);
    }
    memset(pcm, 0, sizeof(Int16) * PCM_LENGTH);
    if(ivsIn && !feof(ivsIn)) {
        fread(pcm, sizeof(Int16), PCM_LENGTH, ivsIn);
    }
}

void PsapReset(void)
{
    /* These will be closed when the process exits */
    if(!psapIn) psapIn = fopen("psap_in.pcm", "rb");
    if(!psapOut) psapOut = fopen("psap_out.pcm", "wb");
}

void PsapSendStart(void) {}

void PsapProcess(Int16 *pcm)
{
    if(psapOut) {
        fwrite(pcm, sizeof(Int16), PCM_LENGTH, psapOut);
        fflush(psapOut);
    }
    memset(pcm, 0, sizeof(Int16) * PCM_LENGTH);
    if(psapIn && !feof(psapIn)) {
        fread(pcm, sizeof(Int16), PCM_LENGTH, psapIn);
    }
}
