/*
	SCC232.C

   Sample Program for SCC channels A and B of the BL1300/Protocol Switch.

   This program monitors for a CR terminated string from a dumb terminal
	from either of the two SCC serial ports. If a completed string is detected,
	the string is sent back to the dumb terminal.
	
   If you want to try a link thru a modem, change the baud rate to 2400 and
   set ismodem=1. You should be able to use any pc terminal program to dial
   up the board.

   We suggest a Hayes SMARTMODEM. Otherwise, the user would have to jumper
   the following wires:

        PS                  MODEM
        
        TX  ----------------  RX
        RX  ----------------  TX
        COMM ---------------  COMM
        RTS---|          |----  RTS
        CTS---|          |----  CTS
                         |----  DTR

   If your modem is truly Hayes Smartmodem compatible, all you need is a null
   modem cable between the BL1300/Protocol Switch and the modem.

	A more elaborate example can be found in sccrem.c

	The SCC serial chip is only available on the PROTOCOL SWITCH.

	Supporting libaries: modem232.lib, scc232.lib, network.lib, drivers.lib.
*/

#use "default.h"

#if !((BOARD_TYPE==SMARTBLOCK))
#fatal "This program only runs on the BL1300/Protocol Switch."
#endif

/**********************************************
CONTROL PARAMETERS
***********************************************/
#define chanA   1
#define chanB   0
#define IBAUD   9600         /*baud rate
                                with modem either 2400 or 1200
                                without modem => 9600, 4800, etc         */
#define TBUFSIZE 384         // size of transmit buffer
#define RBUFSIZE 384         // size of receive buffer

char ismodem = 0;            // set to 1 if you want modem
char isecho = 1;             // set to 1 if you want character echoed back to the pc

int mode;

int send_prompt(int channel);
int read_serial(char *buf, char terminate, int channel);
int write_serial(char *buf, int len, int channel);

main()
{
    int  ercode;
    int  ok_count;
    char atbuf[TBUFSIZE];    // transmit buffer chan A
    char arbuf[RBUFSIZE];    // receive buffer chan A
    char btbuf[TBUFSIZE];    // transmit buffer chan B
    char brbuf[RBUFSIZE];    // receive buffer chan B
    char buf[20];         // dummy buffer for receiving a complete command

    Dkill_SCC();             // reset the SCC chans A and B
    mode = 0x4;              // 8 bit, no parity, 1 stop bit

                    // initialize rs232 serial port for pc to master comm
    Dinit_sca(arbuf, atbuf, RBUFSIZE, TBUFSIZE, mode, 
    IBAUD / 1200, ismodem, isecho);

    Dinit_scb(brbuf, btbuf, RBUFSIZE, TBUFSIZE, mode, 
    IBAUD / 1200, ismodem, isecho);

    if (!ismodem)
    {
        send_prompt(chanA);
        send_prompt(chanB);
    }

    for (;;)    // endless loop constantly monitoring for new command line
    {
        runwatch();
        if ((ercode = read_serial(buf, ENTER, chanA)) != 0)
        {
            send_prompt(chanA);
            write_serial(buf, strlen(buf), chanA);
            send_prompt(chanA);
        }
        if ((ercode = read_serial(buf, ENTER, chanB)) != 0)
        {
            send_prompt(chanB);
            write_serial(buf, strlen(buf), chanB);
            send_prompt(chanB);
        }
    }
}

int send_prompt(int channel)
{
    if (channel)
        Dscbsend_prompt();
    else 
        Dscasend_prompt();
}

int read_serial(char *buf, char terminate, int channel)
{
    if (channel) 
        return Dread_scb(buf, terminate);
    else 
        return Dread_sca(buf, terminate);
}

int write_serial(char *buf, int len, int channel)
{
    if (channel) 
        return Dwrite_scb(buf, len);
    else 
        return Dwrite_sca(buf, len);
}

