Around about 16/02/05 14:34, Simon Kenyon typed ...
well it doesn't work every time - so there you go!

:-)


post your wrapper and i'll give it a whirl
also, post your changes to use nanosleep()

Here goes; cat'd over SSH and reformatted in case there's anything odd in there :-)



Not sure the locking functions (purloined) are really as tidy as I'd want [hence the possibility of rolling it all into one perl script using flock()] but it seems to suffice, and be nice & reliable, so for the time being, it stays.


--
[EMAIL PROTECTED] ~]# rm -f .signature
[EMAIL PROTECTED] ~]# ls -l .signature
ls: .signature: No such file or directory
[EMAIL PROTECTED] ~]# exit
#include <unistd.h>
#include <stdio.h>              /* Standard input/output definitions */
#include <string.h>             /* String function definitions */
#include <unistd.h>             /* UNIX standard function definitions */
#include <fcntl.h>              /* File control definitions */
#include <errno.h>              /* Error number definitions */
#include <asm/ioctls.h>
#include <asm/termios.h>        /* POSIX terminal control definitions */
#include <time.h>

#ifndef COM_PORT
# define COM_PORT               "/dev/ttyS0"
#endif

#ifndef WAIT_TIME
# define WAIT_TIME              2
#endif

#ifndef DEBUG
# define DEBUG                  0
#endif

static int debug = DEBUG;

/* the "RTS" line is actually the DTR line in linux */
#ifdef linux
# define BIT_TO_TWIDDLE         TIOCM_DTR
#else
# define BIT_TO_TWIDDLE         TIOCM_RTS
#endif

/*
 * open_port() - Open serial port
 * returns the file descriptor on success or -1 on error
 */
int open_port(char *com_port)
{
  int fd;                       /* file descriptor for the port */
  struct termios options;

  fd = open (com_port, O_WRONLY | O_NOCTTY | O_NDELAY);
  if (fd == -1)
  {
    /* Could not open the port. */
    perror ("open_port(): unable to open com port");
    exit (-1);
  }
  else
  {
    fcntl (fd, F_SETFL, 0);
  }

  /* get the current options for the port */
  tcgetattr (fd, &options);

  /* Set the baud rates to 9600 */
  cfsetispeed (&options, B9600);
  cfsetospeed (&options, B9600);

  /* enable the receiver and set local mode */
  options.c_cflag |= (CLOCAL);
  options.c_cflag &= ~PARENB;
  options.c_cflag &= ~CSTOPB;
  options.c_cflag &= ~CSIZE;
  options.c_cflag |= CS8;
  options.c_cflag &= ~CREAD;
  options.c_cflag &= ~CRTSCTS;
  options.c_iflag &= ~(IXON | IXOFF | IXANY);

  /* set the new options for the port */
  tcsetattr (fd, TCSANOW, &options);
  return (fd);
}

void set_rts(int fd)
{
  int status;
  int bitset = BIT_TO_TWIDDLE;
  ioctl (fd, TIOCMGET, &status);
  if (debug)
  {
    if (status & bitset)
    {
      printf("RTS bit is set\n");
    }
    else
    {
      printf("RTS bit is unset\n");
    }
  }
  status |= bitset;
  ioctl (fd, TIOCMSET, &status);
  ioctl (fd, TIOCMGET, &status);
  if (status & bitset)
  {
    if (debug) printf("RTS bit set ok\n");
  }
  else
  {
    perror("set_rts(): failed to set RTS bit");
    exit(-1);
  }
}

int main (int argc, char **argv)
{
  int fd;
  char *com_port = COM_PORT;
  int n;
  char *data;
  int wait_time = WAIT_TIME;

  if (argc == 1 || argc > 4)
  {
      printf("usage: digibox codestring [waittime [device]]\n");
      return -1;
  }

  data = argv[1];
  if (argc > 2)
      wait_time = atoi(argv[2]);
  if (argc > 3)
      com_port = argv[3];

  if (debug) printf("opening port for %s and setting RTS\n", com_port);
  fd = open_port(com_port);
  set_rts (fd);

  /* needed to power up the IR system */
  {
    int done = 0;
    struct timespec poweruptime;
    poweruptime.tv_sec = 0;
    poweruptime.tv_nsec = 20000000;
    while (!done)
    {
      int status = nanosleep(&poweruptime,&poweruptime);
      if (status == 0)
      {
        done = 1;
      }
      else if (status == -1)
      {
        if (errno != EINTR)
        {
          sleep(1);
          done = 1;
        }
      }
    }
  }

  if (debug) printf("writing data (\"%s\") now\n",data);
  n = write (fd, data, strlen (data));
  if (n < 0)
  {
    perror("main(): write failed");
    exit(-1);
  }
  if (debug) printf("data written\n");
  tcdrain (fd);
  if (wait_time > 0)
  {
    if (debug) printf("sleeping for %d seconds\n",wait_time);
    sleep (wait_time);
  }
  if (debug) printf("start close\n");
  close (fd);
  if (debug) printf("finished\n");

  return 0;
}
# irfunctions from skychannel.tar.bz2
# http://www.nexusuk.org/projects/mythtv/lirc/

lockir() {
        declare -i count=0
        while [ "$count" -lt "10" ]; do
                if mktemp /tmp/irlock 2>&1 >/dev/null; then
                        echo "$$" >> /tmp/irlock
                        return 0
                fi
                if ! kill -0 `cat /tmp/irlock` 2>&1 >/dev/null; then
                        rm /tmp/irlock
                        if mktemp /tmp/irlock 2>&1 >/dev/null; then
                                echo "$$" >> /tmp/irlock
                                return 0
                        fi
                fi
                sleep 1
                count=$count+1
        done
        return 1
}

unlockir() {
        rm -f /tmp/irlock 2>&1 >/dev/null
}

#!/bin/bash

##exec 1>/tmp/skychannel.dbg
##exec 2>&1
##set -x -v
##chmod a+rw /tmp/skychannel

[ -z "$1" ] && exit 1

. /usr/local/bin/irfunctions

STAT=/tmp/skychannel
lockir
echo $$ > $STAT
unlockir

channel="$1"

channel=`printf '%03d' $channel`

chno=$channel
d1=$((chno/100))
chno=$(( chno-(d1*100) ))
d2=$((chno/10))
chno=$(( chno-(d2*10) ))
d3=$chno

if [ $1 -ge 100 -a $1 -lt 1000 ]; then
        # Channel no. in range
        (
                # Get access to IR & send channel+<Sky>
                lockir
                /usr/local/bin/digibox "${d1}${d2}${d3}T"
                unlockir
                # Wait a little bit in case it'#s powering up
                sleep 1
                # Grab IR again and see if we've beenm called again in meantime
                lockir
                prev=`cat $STAT`
                if [ $prev -eq $$ ]; then
                        # No other calls since our call;  change channel again 
just to make sure
                        # in case we ere powering up.  <Backup> first to ensure 
I/F is clean
                        /usr/local/bin/digibox "K$d1$d2$d3"
                fi
                unlockir
         ) &
fi
_______________________________________________
mythtv-users mailing list
[email protected]
http://mythtv.org/cgi-bin/mailman/listinfo/mythtv-users

Reply via email to