Beispiel #1
0
int silitek_init(void)
{
    if (!tty_create_lock(drv.device)) {
        logprintf(LIRC_ERROR, "could not create lock files");
        return 0;
    }

    drv.fd = open(drv.device, O_RDWR | O_NOCTTY | O_NDELAY);
    if (drv.fd < 0) {
        logprintf(LIRC_ERROR, "could not open %s", drv.device);
        logperror(LIRC_ERROR, "silitek_init()");
        tty_delete_lock();
        return 0;
    }

    if (!tty_reset(drv.fd)) {
        logprintf(LIRC_ERROR, "could not reset %s", drv.device);
        silitek_deinit();
        return 0;
    }

    if (!tty_setbaud(drv.fd, 1200)) {
        logprintf(LIRC_ERROR, "could not set baud rate on %s", drv.device);
        silitek_deinit();
        return 0;
    }

    return 1;
}
Beispiel #2
0
int mp3anywhere_init(void)
{
	signal_length=hw.code_length*1000000/9600;
	
	if(!tty_create_lock(hw.device))
	{
		logprintf(LOG_ERR,"could not create lock files");
		return(0);
	}
	if((hw.fd=open(hw.device,O_RDWR|O_NONBLOCK|O_NOCTTY))<0)
	{
		logprintf(LOG_ERR,"could not open %s",hw.device);
		logperror(LOG_ERR,"mp3anywhere_init()");
		tty_delete_lock();
		return(0);
	}
	if(!tty_reset(hw.fd))
	{
		logprintf(LOG_ERR,"could not reset tty");
		mp3anywhere_deinit();
		return(0);
	}
	if(!tty_setbaud(hw.fd,9600))
	{
		logprintf(LOG_ERR,"could not set baud rate");
		mp3anywhere_deinit();
		return(0);
	}
	return(1);
}
Beispiel #3
0
/**************************************************************************
 * Lock and initialize the serial port.
 * This function is called by the LIRC daemon when the first client
 * registers itself.
 * Return 1 on success, 0 on error.
 **************************************************************************/
int mplay_init(void)
{
	int result = 1;
	LOGPRINTF(1, "Entering mplay_init()");
	/* Creation of a lock file for the port */
	if (!tty_create_lock(hw.device)) {
		logprintf(LOG_ERR, "Could not create the lock file");
		LOGPRINTF(1, "Could not create the lock file");
		result = 0;
	}
	/* Try to open serial port */
	else if ((hw.fd = open(hw.device, O_RDWR | O_NONBLOCK | O_NOCTTY)) < 0) {
		logprintf(LOG_ERR, "Could not open the serial port");
		LOGPRINTF(1, "Could not open the serial port");
		mplay_deinit();
		result = 0;
	}
	/* Serial port configuration */
	else if (!tty_reset(hw.fd) || !tty_setbaud(hw.fd, MPLAY_BAUD_RATE)) {
		logprintf(LOG_ERR, "could not configure the serial port for '%s'", hw.device);
		LOGPRINTF(1, "could not configure the serial port for '%s'", hw.device);
		mplay_deinit();
	}
	return result;
}
Beispiel #4
0
int ea65_init(void)
{
	logprintf(LOG_INFO, "EA65: device %s", hw.device); 

	if (!tty_create_lock(hw.device)) {
		logprintf(LOG_ERR,"EA65: could not create lock files");
		return 0;
	}

	hw.fd = open(hw.device, O_RDWR | O_NONBLOCK | O_NOCTTY);
	if (hw.fd < 0) {
		logprintf(LOG_ERR,"EA65: could not open %s",hw.device);
		tty_delete_lock();
		return 0;
	}

	if (!tty_reset(hw.fd)) {
		logprintf(LOG_ERR,"EA65: could not reset tty");
		ea65_release();
		return 0;
	}

	if (!tty_setbaud(hw.fd, 9600)) {
		logprintf(LOG_ERR,"EA65: could not set baud rate");
		ea65_release();
		return 0;
	}

	return 1;
}
Beispiel #5
0
/* returns 0-3, the port, or -1 if it can't find the device */
int autodetect(void)
{
	int port, i;
	long backup;
	char device[20];

	/* hardcoded the device names.. it's easy enough to change
	 * that, but it's unlikely to be on something else. */

	for (i = 0; i < 4; i++) {
		sprintf(device, "/dev/ttyS%d", i);

		if (!tty_create_lock(device))
			continue;
		port = open("/dev/ttyS0", O_RDONLY | O_NOCTTY);
		if (port < 0) {
			logprintf(LIRC_WARNING, "couldn't open %s", device);
			tty_delete_lock();
			continue;
		} else {
			ioctl(port, TIOCMGET, &backup);

			if (is_it_is_it_huh(port)) {
				ioctl(port, TIOCMSET, &backup);
				close(port);
				tty_delete_lock();
				return i;
			}
			ioctl(port, TIOCMSET, &backup);
			close(port);
		}
		tty_delete_lock();
	}
	return -1;
}
int silitek_init(void) {
    if(!tty_create_lock(hw.device)) {
        logprintf(LOG_ERR, "could not create lock files");
        return(0);
    }

    if((hw.fd = open(hw.device, O_RDWR | O_NOCTTY | O_NDELAY)) < 0) {
        logprintf(LOG_ERR, "could not open %s", hw.device);
        logperror(LOG_ERR, "silitek_init()");
        tty_delete_lock();
        return(0);
    }

    if(!tty_reset(hw.fd)) {
        logprintf(LOG_ERR,"could not reset %s", hw.device);
        silitek_deinit();
        return(0);
    }

    if(!tty_setbaud(hw.fd, 1200)) {
        logprintf(LOG_ERR,"could not set baud rate on %s", hw.device);
        silitek_deinit();
        return(0);
    }

    return(1);
}
Beispiel #7
0
int tira_init(void)
{
	if (child_pid != -1) tira_deinit();

	LOGPRINTF (1, "Tira init");
	
	if(!tty_create_lock(hw.device))
	{
		logprintf(LOG_ERR,"could not create lock files");
		return 0;
	}
	if ( (hw.fd = open (hw.device, O_RDWR | O_NONBLOCK | O_NOCTTY)) < 0)
	{
		tty_delete_lock ();
		logprintf (LOG_ERR, "Could not open the '%s' device",
			   hw.device);
		return 0;
	}
	LOGPRINTF(1, "device '%s' opened", hw.device);

	/* We want 9600 8N1 with CTS/RTS handshaking, lets set that
	 * up. The specs state a baud rate of 100000, looking at the
	 * ftdi_sio driver it forces the issue so we can set to what
	 * we would like. And seeing as this is mapped to 9600 under
	 * windows should be a safe bet.
	 */

	/* Determine device type */
	device_type=0;
	if (check_tira()) device_type='t';else
	if (check_ira()) device_type='i';

#ifdef DEBUG
	const char *device_string;
	
	switch(device_type)
	{
	case 't':
		device_string = "Tira";
		break;
	case 'i':
		device_string = "Ira";
		break;
	default:
		device_string = "unknown";
	}
	LOGPRINTF(1, "device type %s", device_string);
#endif

	if (device_type == 0)
	{
		tira_deinit();
		return 0;
	}  

	return 1;
}
Beispiel #8
0
int bte_init(void)
{
	LOGPRINTF(3, "bte_init called, device %s", hw.device);

	if (!tty_create_lock(hw.device)) {
		logprintf(LOG_ERR, "bte_init: could not create lock file");
		return 0;
	}
	if (!bte_connect()) {
		// return 0;
	}
	return 1;
}
Beispiel #9
0
int irman_init(void)
{
	if(!tty_create_lock(hw.device))
	{
		logprintf(LOG_ERR,"could not create lock files");
		return(0);
	}
	if((hw.fd=ir_init(hw.device))<0)
	{
		logprintf(LOG_ERR,"could not open %s",hw.device);
		logperror(LOG_ERR,"irman_init()");
		tty_delete_lock();
		return(0);
	}
	return(1);
}
Beispiel #10
0
//-------------------------------------------------------------------------
// Lock and initialize the serial port.
// This function is called by the LIRC daemon when the first client
// registers itself.
// Return 1 on success, 0 on error.
//-------------------------------------------------------------------------
int accent_init(void)
{

    LOGPRINTF(LOG_DEBUG, "Entering accent_init()");

    // Calculate the time length of a remote signal (in microseconds):
    // (bits + total_stop_bits) * 1000000 / bitrate
    signal_length = (hw.code_length + (hw.code_length / 8)) * 1000000
                    / ACCENT_BAUD_RATE;

    if (!tty_create_lock(hw.device)) {
        logprintf(LOG_ERR,   "Could not create the lock file");
        LOGPRINTF(LOG_EMERG, "Could not create the lock file");
        return(0);
    }
    if ((hw.fd = accent_open_serial_port(hw.device)) < 0) {
        logprintf(LOG_ERR,   "Could not open the serial port");
        LOGPRINTF(LOG_EMERG, "Could not open the serial port");
        accent_deinit();
        return(0);
    }
    return(1);
}
Beispiel #11
0
static int irlink_open(const char * portName)
{
    int port = -1;
    if(!tty_create_lock((char*)portName))
    {
        logprintf(LOG_ERR,"could not create lock files");
        return -1;
    }
    if((port=open(portName,O_RDWR | O_NOCTTY | O_NDELAY))<0)
    {
        logprintf(LOG_ERR,"could not open %s",hw.device);
        tty_delete_lock();
        return -1;
    }
    if(tty_reset(port) < 0 ||
            tty_setbaud(port , 115200) < 0 ||
            tty_setcsize(port , 8) < 0 ||
            tty_setrtscts(port , 0) < 0 )
    {
        irlink_close(port);
        port = -1;
    }
    return port;
}
Beispiel #12
0
int tty_create_lock(char *name)
{
	char filename[FILENAME_MAX+1];
	char symlink[FILENAME_MAX+1];
	char cwd[FILENAME_MAX+1];
	char *last,*s;
	char id[10+1+1];
	int lock;
	int len;
	
	strcpy(filename,"/var/lock/LCK..");
	
	last=strrchr(name,'/');
	if(last!=NULL)
		s=last+1;
	else
		s=name;
	
	if(strlen(filename)+strlen(s)>FILENAME_MAX)
	{
		logprintf(LOG_ERR,"invalid filename \"%s%s\"",
			  filename,s);
		return(0);
	}
	strcat(filename,s);
	
 tty_create_lock_retry:
	if((len=snprintf(id,10+1+1,"%10d\n",getpid()))==-1)
	{
		logprintf(LOG_ERR,"invalid pid \"%d\"",getpid());
		return(0);
	}
	lock=open(filename,O_CREAT|O_EXCL|O_WRONLY,0644);
	if(lock==-1)
	{
		logprintf(LOG_ERR,"could not create lock file \"%s\"",
			  filename);
		logperror(LOG_ERR,NULL);
		lock=open(filename,O_RDONLY);
		if(lock!=-1)
		{
			pid_t otherpid;
			
			id[10+1]=0;
			if(read(lock,id,10+1)==10+1 &&
			   read(lock,id,1)==0 &&
			   sscanf(id,"%d\n",&otherpid)>0)
			{
				if(kill(otherpid,0)==-1 && errno==ESRCH)
				{
					logprintf(LOG_WARNING,
						  "detected stale "
						  "lockfile %s",
						  filename);
					close(lock);
					if(unlink(filename)!=-1)
					{
						logprintf(LOG_WARNING,
							  "stale lockfile "
							  "removed");
						goto tty_create_lock_retry;
					}
					else
					{
						logprintf(LOG_ERR,
							  "could not remove "
							  "stale lockfile");
						logperror(LOG_ERR,NULL);
					}
					return(0);
				}
				else
				{
					logprintf(LOG_ERR,
						  "%s is locked by PID %d",
						  name,otherpid);
				}
			}
			else
			{
				logprintf(LOG_ERR,
					  "invalid lockfile %s encountered",
					  filename);
			}
			close(lock);
		}
		return(0);
	}
	if(write(lock,id,len)!=len)
	{
		logprintf(LOG_ERR,"could not write pid to lock file");
		logperror(LOG_ERR,NULL);
		close(lock);
		if(unlink(filename)==-1)
		{
			logprintf(LOG_ERR,"could not delete file \"%s\"",
				  filename);
			logperror(LOG_ERR,NULL);
			/* FALLTHROUGH */
		}
		return(0);
	}
	if(close(lock)==-1)
	{
		logprintf(LOG_ERR,"could not close lock file");
		logperror(LOG_ERR,NULL);
		if(unlink(filename)==-1)
		{
			logprintf(LOG_ERR,"could not delete file \"%s\"",
				  filename);
			logperror(LOG_ERR,NULL);
			/* FALLTHROUGH */
		}
		return(0);
	}

	if((len=readlink(name,symlink,FILENAME_MAX))==-1)
	{
		if(errno!=EINVAL) /* symlink */
		{
			logprintf(LOG_ERR,"readlink() failed for \"%s\"",name);
			logperror(LOG_ERR,NULL);
			if(unlink(filename)==-1)
			{
				logprintf(LOG_ERR,"could not delete file \"%s\"",
					  filename);
				logperror(LOG_ERR,NULL);
				/* FALLTHROUGH */
			}
			return(0);
		}
	}
	else
	{
		symlink[len]=0;

		if(last)
		{
			char dirname[FILENAME_MAX+1];

			if(getcwd(cwd,FILENAME_MAX)==NULL)
			{
				logprintf(LOG_ERR,"getcwd() failed");
				logperror(LOG_ERR,NULL);
				if(unlink(filename)==-1)
				{
					logprintf(LOG_ERR,"could not delete "
						  "file \"%s\"",filename);
					logperror(LOG_ERR,NULL);
				        /* FALLTHROUGH */
				}
				return(0);
			}
			
			strcpy(dirname,name);
			dirname[strlen(name)-strlen(last)]=0;
			if(chdir(dirname)==-1)
			{
				logprintf(LOG_ERR,"chdir() to \"%s\" "
					  "failed",dirname);
				logperror(LOG_ERR,NULL);
				if(unlink(filename)==-1)
				{
					logprintf(LOG_ERR,"could not delete "
						  "file \"%s\"",filename);
					logperror(LOG_ERR,NULL);
				        /* FALLTHROUGH */
				}
				return(0);
			}
		}
		if(tty_create_lock(symlink)==-1)
		{
			if(unlink(filename)==-1)
			{
				logprintf(LOG_ERR,"could not delete file "
					  "\"%s\"",filename);
				logperror(LOG_ERR,NULL);
				/* FALLTHROUGH */
			}
			return(0);
		}
		if(last)
		{
			if(chdir(cwd)==-1)
			{
				logprintf(LOG_ERR,"chdir() to \"%s\" failed",
					  cwd);
				logperror(LOG_ERR,NULL);
				if(unlink(filename)==-1)
				{
					logprintf(LOG_ERR,"could not delete "
						  "file \"%s\"",filename);
					logperror(LOG_ERR,NULL);
				        /* FALLTHROUGH */
				}
				return(0);
			}
		}
	}
	return(1);
}
Beispiel #13
0
static int uirt2_raw_init(void)
{
	int version;

	if (!tty_create_lock(hw.device)) {
		logprintf(LOG_ERR, "uirt2_raw: could not create lock files");
		return (0);
	}

	if ((hw.fd = open(hw.device, O_RDWR | O_NONBLOCK | O_NOCTTY)) < 0) {
		logprintf(LOG_ERR, "uirt2_raw: could not open %s", hw.device);
		tty_delete_lock();
		return (0);
	}

	if (!tty_reset(hw.fd)) {
		logprintf(LOG_ERR, "uirt2_raw: could not reset tty");
		close(hw.fd);
		tty_delete_lock();
		return (0);
	}

	/* Wait for UIRT device to power up */
	usleep(100 * 1000);

	if (!tty_setbaud(hw.fd, 115200)) {
		logprintf(LOG_ERR, "uirt2_raw: could not set baud rate");
		close(hw.fd);
		tty_delete_lock();
		return (0);
	}

	if (!tty_setcsize(hw.fd, 8)) {
		logprintf(LOG_ERR, "uirt2_raw: could not set csize");
		close(hw.fd);
		tty_delete_lock();
		return (0);
	}

	if (!tty_setrtscts(hw.fd, 1)) {
		logprintf(LOG_ERR, "uirt2_raw: could not enable hardware flow");
		close(hw.fd);
		tty_delete_lock();
		return (0);
	}

	if ((dev = uirt2_init(hw.fd)) == NULL) {
		logprintf(LOG_ERR, "uirt2_raw: No UIRT2 device found at %s", hw.device);
		close(hw.fd);
		tty_delete_lock();
		return (0);
	}

	if (uirt2_setmoderaw(dev) < 0) {
		logprintf(LOG_ERR, "uirt2_raw: could not set raw mode");
		uirt2_raw_deinit();
		return (0);
	}

	if (uirt2_getversion(dev, &version) < 0) {
		uirt2_raw_deinit();
		return (0);
	}
	if (version >= 0x0905) {
		if (!tty_setdtr(hw.fd, 0)) {
			logprintf(LOG_ERR, "uirt2_raw: could not set DTR");
			uirt2_raw_deinit();
			return (0);
		}
	}

	init_rec_buffer();
	init_send_buffer();

	rec_rptr = 0;
	rec_wptr = 0;
	rec_size = sizeof(rec_buf) / sizeof(rec_buf[0]);

	return (1);
}
Beispiel #14
0
int pinsys_init(void)
{
	signal_length = (hw.code_length + (hw.code_length / BITS_COUNT) * 2) * 1000000 / 1200;

	if (!tty_create_lock(hw.device)) {
		logprintf(LOG_ERR, "could not create lock files");
		return (0);
	}
	if ((hw.fd = open(hw.device, O_RDWR | O_NONBLOCK | O_NOCTTY)) < 0) {
		int detected;
		/* last character gets overwritten */
		char auto_lirc_device[] = "/dev/ttyS_";

		tty_delete_lock();
		logprintf(LOG_WARNING, "could not open %s, autodetecting on /dev/ttyS[0-3]", hw.device);
		logperror(LOG_WARNING, "pinsys_init()");
		/* it can also mean you compiled serial support as a
		   module and it isn't inserted, but that's unlikely
		   unless you're me. */

		detected = autodetect();

		if (detected == -1) {
			logprintf(LOG_ERR, "no device found on /dev/ttyS[0-3]");
			tty_delete_lock();
			return (0);
		} else {	/* detected */

			auto_lirc_device[9] = '0' + detected;
			hw.device = auto_lirc_device;
			if ((hw.fd = open(hw.device, O_RDWR | O_NONBLOCK | O_NOCTTY)) < 0) {
				/* unlikely, but hey. */
				logprintf(LOG_ERR, "couldn't open autodetected device \"%s\"", hw.device);
				logperror(LOG_ERR, "pinsys_init()");
				tty_delete_lock();
				return (0);
			}
		}
	}
	if (!tty_reset(hw.fd)) {
		logprintf(LOG_ERR, "could not reset tty");
		pinsys_deinit();
		return (0);
	}
	if (!tty_setbaud(hw.fd, 1200)) {
		logprintf(LOG_ERR, "could not set baud rate");
		pinsys_deinit();
		return (0);
	}
	/* set RTS, clear DTR */
	if (!tty_set(hw.fd, 1, 0) || !tty_clear(hw.fd, 0, 1)) {
		logprintf(LOG_ERR, "could not set modem lines (DTR/RTS)");
		pinsys_deinit();
		return (0);
	}

	/* I dunno, but when lircd starts may log `reading of byte 1
	   failed' I know that happened when testing, it's a zero
	   byte. Problem is, flushing doesn't fix it. It's not fatal,
	   it's an indication that it gets fixed.  still... */

	if (tcflush(hw.fd, TCIFLUSH) < 0) {
		logprintf(LOG_ERR, "could not flush input buffer");
		pinsys_deinit();
		return (0);
	}
	return (1);
}
Beispiel #15
0
int mplay2_init(void)
{
	struct termios portset;
	signed int len;
	char buf = 0x96;
	char psResponse[11];

	LOGPRINTF(1, "Entering mplay_init()");
	/* Creation of a lock file for the port */
	if (!tty_create_lock(hw.device)) {
		logprintf(LOG_ERR, "Could not create the lock file");
		LOGPRINTF(1, "Could not create the lock file");
		return 0;
	}

	LOGPRINTF(0, "open serial port");
	/* Try to open serial port (Monueal Moncaso 312 device doesn't like O_NONBLOCK */
	if ((hw.fd = open(hw.device, O_RDWR | O_NOCTTY)) < 0) {
		logprintf(LOG_ERR, "Could not open the serial port");
		LOGPRINTF(1, "Could not open the serial port");
		tty_delete_lock();
		return 0;
	}

	/* Get serial device parameters */
	if (tcgetattr(hw.fd, &portset) < 0) {
		logprintf(LOG_ERR, "Could not get serial port attributes");
		LOGPRINTF(1, "Could not get serial port attributes");
		mplay_deinit();
		return 0;
	}

	/* use own termios struct instead of using tty_reset , Moncaso doesn't like TCSAFLUSH */
	portset.c_cflag &= ~PARENB;
	portset.c_cflag &= ~CSTOPB;
	portset.c_cflag &= ~CSIZE;
	portset.c_cflag = B57600 | CS8;
	portset.c_cflag |= (CLOCAL | CREAD);
	portset.c_iflag |= (IXON | IXOFF | IXANY);
	portset.c_oflag &= ~OPOST;
	portset.c_lflag &= ~(ICANON | ECHOE | ECHO | ISIG);
	portset.c_cc[VSTART] = 0x11;
	portset.c_cc[VSTOP] = 0x13;
	portset.c_cc[VEOF] = 0x20;
	portset.c_cc[VMIN] = 1;
	portset.c_cc[VTIME] = 3;

	if (tcsetattr(hw.fd, TCSANOW, &portset) < 0) {
		logprintf(LOG_ERR, "Error setting TCSANOW mode of serial device");
		LOGPRINTF(1, "Error setting TCSANOW mode of serial device");
		mplay_deinit();
		return 0;
	}

	len = write(hw.fd, &buf, 1);
	if (len < 0) {
		LOGPRINTF(LOG_ERR, "couldn't write to device");
		mplay_deinit();
		return 0;
	}

	len = read(hw.fd, &psResponse, 11);
	if (len < 0) {
		LOGPRINTF(1, "No data recieved during reading");
		mplay_deinit();
		return 0;
	} else
		LOGPRINTF(1, "read chars: %s", psResponse);

	if (tcgetattr(hw.fd, &portset) < 0) {
		logprintf(LOG_ERR, "Could not get serial port attributes");
		LOGPRINTF(1, "Could not get serial port attributes");
		mplay_deinit();
		return 0;
	}

	portset.c_cflag &= ~PARENB;
	portset.c_cflag &= ~CSTOPB;
	portset.c_cflag &= ~CSIZE;
	portset.c_cflag = B57600 | CS8;
	portset.c_cflag |= (CLOCAL | CREAD);
	portset.c_iflag |= (IXON | IXOFF | IXANY);
	portset.c_oflag &= ~OPOST;
	portset.c_lflag &= ~(ICANON | ECHOE | ECHO | ISIG);
	portset.c_cc[VSTART] = 0x11;
	portset.c_cc[VSTOP] = 0x13;
	portset.c_cc[VEOF] = 0x1C;
	portset.c_cc[VMIN] = 1;
	portset.c_cc[VTIME] = 3;

	if (tcsetattr(hw.fd, TCSANOW, &portset) < 0) {
		logprintf(LOG_ERR, "Error setting TCSANOW mode of serial device");
		LOGPRINTF(1, "Error setting TCSANOW mode of serial device");
		mplay_deinit();
		return 0;
	}

	return 1;
}