/*
mtalkfd -- full rate mtalkd, using /dev/audio
sd = Scott Doty, scott@cs.santarosa.edu
Sun Mar 27 02:51:55 PST 1994
Extremely alpha -- use at your own risk -- don't blame me
or these guys:
*/

/* Copyright (C) 1993 Free Software Association of Germany

   The following is only valid, if you use this outside the Federal
   Republic of Germany.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   If you don't have a a copy of the GNU General Public License
   write to the Free Software Foundation, Inc., 675 Mass Ave, 
   Cambridge, MA 02139, USA.

   (C)opyright 1993,1994 FREE SOFTWARE ASSOCIATION OF GERMANY

   ----------------------------------------------------------------

   Fuer die Nutzung dieses Quell-Codes innerhalb der Bundesrepublik
   Deutschland gilt:

   Dieses Programm ist freie Software und kann unter den Bedingungen
   der Deutschen Free Software Lizenz weitergeben und/oder modifiziert
   werden.

   Wir haben dieses Programm in der Hoffnung entwickelt, dass es
   sich als nuetzlich erweist. Wir uebernehmen jedoch *KEINERLEI*
   Garantien auf die Funktion oder Verwendbarkeit dieses Programms.
   Der Anwender nutzt dieses Programm * AUF EIGENES RISIKO *

   Diesem Programm sollte eine Kopie der Deutschen Free Software Lizenz
   (DFSL) beigefuegt sein. Falls nicht, kann eine Kopie von uns angefordert
   werden: Free Software Association of Germany, Heimatring 19,
   60596 Frankfurt (info@elara.fsag..de), Telefon: ++49 - 69 - 6312083

   (C)opyright 1993,1994 FREE SOFTWARE ASSOCIATION OF GERMANY */

#include <stdio.h>
#include <fcntl.h>
#include <stdio.h>
#include <fcntl.h>
#include "mtalk.h"


#ifndef lint
static	char copyright[] =
"@(#) Copyright (c) 1993, 1994 Free Software Association of Germany.\n";
#endif

#ifndef lint
static char sccsid[] = "@(#) mtalkd  Version 0.90 Fri 25 Mar 1994 15:37\n";
#endif 
    

#define MAR 7 
#define MARA 5 
#define CNT 1 
#define CCNT 2 

static	unsigned char mbuf[SIZE_MB];


main(int argc, char **argv)
{
	int a,sz,b,c;
	unsigned char buf[512];
	printf("001 cb server ready\r\n");
	fflush(stdout);
	while (1)	{
		b = 0;
		for (a = 0; a < 100; a++)	{
			if (read(0,&c,1) != 1)
				exit(1);
			if (c == 10)
				break;
			buf[b++] = c;
			}
		buf[b] = 0;
		if (atoi(buf) == 0)	{
			printf("002 Thanks. Have a nice day\r\n");
			fflush(stdout);
			exit(1);
			}
		printf("003 Reading %d bytes\r\n",atoi(buf));
		fflush(stdout);
		sz = atoi(buf);
		a = slow_read(0,mbuf,sz);
		printf("Received %d bytes\r\n",a);
		fflush(stdout);
		c = 0;
		for (b = 0; b < 10; b++)	{
			sz = open("/dev/audio",O_WRONLY);
			if (sz >= 0)	{
				write(sz,mbuf,a);
				printf("004 ok\r\n");
				close(sz);
				c = 1;
				break;
				}
			sleep(1);
			} 
		if (c == 0)
			printf("005 data lost\r\n");
		fflush(stdout);
		}
} 

slow_read(int hd, char *s, int sz)
{
    int pos;
    int a;
    int written = 0;
    int tz;
    
    pos = 0;
    tz = sz;  


    while (1)   {
        a = read(hd,&s[pos],tz);
        if (a < 0)
            return (-1);
        pos +=a;
        tz -= a;
#ifdef DEBUG
        printf("slow_read: have <%d> still want <%d>\n",pos,tz);
#endif
        if (pos >= sz)
            return (pos);
        }
}
