prefect
Home Engineering Publications
DACS: CDROM Service Provider Main Code, cdaudio_daemon.c
 
  next up previous contents
Next: CDROM Service Provider CD Up: MIDI Controller Previous: CDROM Service Provider CD   Contents

CDROM Service Provider Main Code, cdaudio_daemon.c

This code establishes the server and handles requests from a client.
/*****************************************************************************
 * DACS : Distributed Audio Control System
 *============================================================================
 *         File: cdaudio_daemon.c
 *       Author: Stephen S. Richardson
 * Date Created: 04.22.97
 *  Environment: GNU C Compiler (GCC) v2.7.1, Linux i486 v2.0.28
 *        Build: make
 *============================================================================
 * The code, executables, documentation, firmware images, and all related
 * material of DACS are  
 * Copyright (C) 1997 Stephen S. Richardson - ALL RIGHTS RESERVED
 *****************************************************************************/


#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <netdb.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/cdrom.h>
#include "server.h"
#include "cdaudio_func.h"
#include "cdaudio_comm.h"

void oops (char *mesg)
{
  perror (mesg);
  exit (1);
}

int discjob (struct cdtype *cmd, int cdfd, struct discinfo *di)
{
  int v=0;

  switch (cmd->function) {
  case CD_PLAY:
    printf ("[CD Play: Track %d seek to %2.2d:%2.2d for %2.2d:%2.2d]...\n", cmd->track, cmd->s_min, cmd->s_sec, cmd->d_min, cmd->d_sec); fflush (stdout);
    v=cdrPlay (cmd->track, cmd->s_min, cmd->s_sec, cmd->d_min, cmd->d_sec, cdfd, di);
    break;

  case CD_CUE:
    printf ("[CD Cue: Track %d seek to %2.2d:%2.2d for %2.2d:%2.2d]...\n", cmd->track, cmd->s_min, cmd->s_sec, cmd->d_min, cmd->d_sec); fflush (stdout);
    v=cdrCue (cmd->track, cmd->s_min, cmd->s_sec, cmd->d_min, cmd->d_sec, cdfd, di);
    break;

  case CD_PAUSE:
    printf ("[CD Pause]...\n"); fflush (stdout);
    v=cdrPause (cdfd);
    break;

  case CD_RESUME:
    printf ("[CD Resume]...\n"); fflush (stdout); 
    v=cdrResume (cdfd);
    break;

  case CD_STOP:
    printf ("[CD Stop]...\n"); fflush (stdout);
    v=cdrStop (cdfd);
    break;

  case CD_EJECT:
    printf ("[CD Eject]...\n"); fflush (stdout);
    v=cdrEject (cdfd);
    break;
  }

  if (v==SUCCESS) printf ("OK\n");
  else printf ("Failed.\n");
  fflush(stdout);
  return (v);
}

int cdtracks (int cdfd, struct discinfo *di)
{
  struct cdhw_t *cd;
  int err, i;

  printf ("[Read disc info]... \n"); fflush (stdout);
  cd = read_hw (cdfd, &err);
  if (err!=SUCCESS) {
    printf ("Failed.\n");
    fflush(stdout);
    free (cd);
    return (ERROR_CDROMHW);
  } else {
    printf ("OK.\n");
    fflush(stdout);
    di->tsec[0]=0;
    for (i=cd->tochdr.cdth_trk0; i<=(cd->tochdr.cdth_trk1+1);i++) {
      di->tsec[i+1]=cd->tocentries[i].cdte_addr.msf.minute*60+
	cd->tocentries[i].cdte_addr.msf.second;
      printf ("[Track %d starts %d seconds into disc.]\n", i, di->tsec[i]);
      fflush(stdout);
    }
    free (cd);
    return (SUCCESS);
  }

}

void main (void)
{
  int s, sfd, ex, hlt, v=0, cdstat, cdf=-1;
  struct ipaddx ip;
  struct cdtype *st;
  struct discinfo cdinfo;
  char buffer[CD_TCP_BUFSIZE];
  char dev[80];

  printf ("DACS : Distributed Audio Control System\nCopyright (C) 1997 Stephen S. Richardson / SR Engineering\n\n");
  fflush (stdout);
  printf ("Attempting to establish server... \n"); fflush (stdout);
  s=tcpEstablishServer ("localhost", CD_TCP_PORT);
  if (s<0) oops ("cdaudio_daemon (establish server)");
  hlt=0;
  printf ("OK.\n");
  fflush (stdout);

  printf ("Waiting for client connection... \n");

  while (!hlt) {
    while ((tcpDataWaiting(s)) != DATA) {
      usleep (100000);
      /* idle */
    }
    
    sfd=tcpAcceptConn (s, &ip);
    if (sfd<0) oops ("cd_daemon (accept connection)");
    printf ("Accepted.\n");
    fflush (stdout);

    ex=0;
    cdstat=CD_CLOSE;

    while (ex==0) {
      if (tcpDataWaiting (sfd)==DATA) {
	v=tcpReadBuffer (sfd, buffer, sizeof (buffer));
	if (v==ERROR_HANGUP) {
	  printf ("[CD Stop]...\n");
          fflush (stdout);
	  cdrStop (cdf);
	    if (cdstat==CD_OPEN) {
	      printf ("[CLOSE CDROM device]..."); fflush (stdout);
              fflush (stdout);
	      close (cdf);
	      if (cdf<0) {
		printf ("Failed!\n");
                fflush (stdout);
	      } else {
		printf ("OK.\n");
                fflush (stdout);
	      }
	      cdstat=CD_CLOSE;
	    }
	  printf ("Client disconnect.\nWaiting for client connection... \n");
	  fflush (stdout);
	  ex=1;
	}
	else if (v==ERROR_READ) oops ("tcp_daemon (read buffer)");
	else {
	  st = (struct cdtype *) buffer;

	  /**** HANDLE OPENING/CLOSING THE CDROM DEVICE ****/

	  if (st->function == CD_OPEN) {
	    if (cdstat==CD_CLOSE) {
	      sprintf (dev, "%s%d", CDROM_DEV, st->disc);
	      printf ("[OPEN CDROM device %s]...\n", dev); fflush (stdout);
	      cdf=open(dev, O_RDONLY);
	      if (cdf<0) {
		printf ("Failed!\n");
                fflush (stdout);
		cdstat=CD_CLOSE;
	      } else {
		printf ("OK.\n");
                fflush (stdout);
		cdstat=CD_OPEN;
		cdtracks (cdf, &cdinfo);
	      }
	    } else {
	      close (cdf);
	      sprintf (dev, "%s%d", CDROM_DEV, st->disc);
	      printf ("[OPEN CDROM device %s]...\n", dev); fflush (stdout);
              fflush (stdout);
	      cdf=open(dev, O_RDONLY);
	      if (cdf<0) {
		printf ("Failed!\n");
                fflush (stdout);
		cdstat=CD_CLOSE;
	      } else {
		printf ("OK.\n");
                fflush (stdout);
	 	cdstat=CD_OPEN;
		cdtracks (cdf, &cdinfo);
	      }
	    }
	  } else if (st->function == CD_CLOSE) {
	    if (cdstat==CD_OPEN) {
	      printf ("[CLOSE CDROM device]...\n"); fflush (stdout);
	      close (cdf);
	      if (cdf<0) {
		printf ("Failed!\n");
                fflush (stdout);
	      } else {
		printf ("OK.\n");
                fflush (stdout);
	      }
	      cdstat=CD_CLOSE;
	    }
	  } else if (st->function == CD_PRG_EXIT) {
	    printf ("Exiting.\n");
	    fflush(stdout);
	    hlt=1;
	  } else {
	    if (cdstat == CD_OPEN) {
	      v=discjob (st, cdf, &cdinfo);
	    } else {
	      printf ("[tried to perform a function without first opening CDROM]\n");
	     fflush(stdout);       
	    }
	  }
	}
      } else {
	/* idle */
	usleep (2000);
      }
    }
  }
}


Steve Richardson 2000-07-06
Table of Contents

[PDF] [Whole document in PDF 1.9MB]

[more photos and information]

 
Page last modified:
Copyright © 1993-2000 prefect - All Rights Reserved.