/* User process for Kantronics Host Mode
   Copyright Peter Hardie VE5VA 1990
*/
#include <exec/types.h>
#include <exec/exec.h>
#include <ctype.h>
#include <libraries/dos.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "kiss.h"
#include "port.h"


extern long windbit,keybit;
extern unsigned char ichar;
extern struct IOStdReq *Con_Read;
extern char *title;
short debug;
extern short interflag;
extern char tmpstr[];
ULONG class;
USHORT code;

long getbits,portbit,sendbit;
struct MsgPort *Ctl_Port;
struct kiss_msg km;
struct MsgPort *ctl_port,*my_port,*sendport;

unsigned char inbuf[100];
short hostport,hoststream;

/* The window isn't open yet so have to printf in initser but
   ttputs in anything else.
*/
initser(port,stream)
short port,stream;
{
   register short i;
   register struct regd *rp;
   if((port != 1) && (port != 2)) {
      printf("Illegal port number\n");
      return(1);
   }
   if((stream != -1) && ((stream < 1) || (stream > 26))) {
      printf("Illegal stream number\n");
      return(1);
   }
   km.rstream = stream;
   km.rport = port;
   /* Find the host process - don't wait long though */
   for(i=0;i < 6; i++ ) {
      if(ctl_port = FindPort(CTL_NAME))break;
      Delay(250L);
      if((i%6) == 0)
         printf("port %d stream %d waiting for reader\n",port,stream);
   }
   if(ctl_port == 0) {
      printf("Can't find control process\n");
      return(1);
   }
   /* Register as a user on the specified port and stream */
   km.channel = K_REGISTER;
   my_port = km.cm.mn_ReplyPort = CreatePort(0L,0L);
   if(my_port == 0L) {
      printf("Can't create port\n");
      return(2);
   }
   portbit = (1L << my_port->mp_SigBit);
   km.cm.mn_Node.ln_Type = NT_MESSAGE;
   PutMsg(ctl_port,&km);
   Wait(portbit);

   /* Create new port in the message struct to send the message to */
   if(km.channel == -1) {
      printf("Error returned from control process\n");
      DeletePort(my_port);
      DeletePort(sendport);
      return(3);
   }
   GetMsg(my_port);

/*
   All drivers must return a port and stream in rport and rstream
   when K_REGISTER is called. The program converts them to characters.
*/
   hostport = km.rport +'0';
   hoststream = km.rstream -1 + 'A';

   /* portbit will signal a received message from the TNC
      sendbit will signal that the driver has ReplyMsg to a
      message that we have sent to the driver.
   */
   sendport = km.cm.mn_ReplyPort = CreatePort(0L,0L);
   sendbit = (1L << sendport->mp_SigBit);

   return(0);
}
char portname[10];
struct MsgPort *our_port;
makeport()
{
   sprintf(portname,"CBBS-%c%c",hostport,hoststream);
   if(FindPort(portname))return(1);
   if((our_port = CreatePort(portname,0L)) == 0)return(1);
   return(0);
}
closeport()
{
   if(our_port)DeletePort(our_port);
}

sendcommand(s)
unsigned char *s;
{
   register unsigned char *cp;
   register unsigned char *tp;
   cp = s;

   km.channel = K_WRT_CMND;
   tp = &km.msg[0];
   while(*cp)*tp++ = *cp++;
   /* Do not send a carriage return at the end of a command */
   if((*(tp-1) == '\r') || (*(tp-1) == '\n'))tp--;
   *tp = 0;
   km.msgsize = tp - &km.msg[0];
   PutMsg(ctl_port,&km);
   Wait(sendbit);
   GetMsg(sendport);
}
senddata(s)
unsigned char *s;
{
   register unsigned char *cp;
   register unsigned char *tp;
   cp = s;

   km.channel = K_WRT_DATA;

   tp = &km.msg[0];
   while(*cp)*tp++ = *cp++;
   *tp = 0;
   km.msgsize = tp - &km.msg[0];
   PutMsg(ctl_port,&km);
   Wait(sendbit);
   GetMsg(sendport);
}
schar(c)
unsigned char c;
{

   km.channel = K_WRT_DATA;

   km.msg[0] = c;
   km.msgsize = 1;
   PutMsg(ctl_port,&km);
   Wait(sendbit);
   GetMsg(sendport);
}
cleanser()
{
   /* Send shutdown message to control process
      (If it is still there)
    */
   if(FindPort(CTL_NAME)) {
      km.cm.mn_ReplyPort = my_port;
      km.channel = K_SHUTUP;
      PutMsg(ctl_port,&km);
      WaitPort(my_port);
   }
   DeletePort(my_port);
   DeletePort(sendport);
}
beep()
{
   km.channel = K_BEEP;
   km.msgsize = 0;
   PutMsg(ctl_port,&km);
   Wait(sendbit);
   GetMsg(sendport);
}
getstat()
{
   km.channel = K_GET_STAT;
   km.msgsize = 0;
   PutMsg(ctl_port,&km);
   Wait(sendbit);
   GetMsg(sendport);
}



/* Put all the stuff that's not used in here */
char passchr,hfstream,vhfstream;
/* Don't need the audio device */
initbeep()
{
}
donebeep()
{
}
inittnc()
{
}
breakport()
{
}
force_modem()
{
}
