aboutsummaryrefslogtreecommitdiffstats
AgeCommit message (Expand)AuthorFilesLines
2003-07-28[lcd4linux @ 2003-07-28 08:22:17 by reinelt]reinelt9-474/+0
2003-07-24[lcd4linux @ 2003-07-24 04:48:09 by reinelt]reinelt18-84/+161
2003-07-21[lcd4linux @ 2003-07-21 06:34:14 by reinelt]reinelt1-9/+19
2003-07-21[lcd4linux @ 2003-07-21 06:10:11 by reinelt]reinelt1-25/+17
2003-07-21[lcd4linux @ 2003-07-21 05:56:48 by reinelt]reinelt1-0/+2
2003-07-19[lcd4linux @ 2003-07-19 09:42:42 by reinelt]reinelt4-7/+177
2003-07-18[lcd4linux @ 2003-07-18 04:43:14 by reinelt]reinelt1-5/+4
2003-06-26[lcd4linux @ 2003-06-26 05:31:16 by reinelt]reinelt1-3/+6
2003-06-22[lcd4linux @ 2003-06-22 19:31:24 by reinelt]reinelt2-0/+128
2003-06-21[lcd4linux @ 2003-06-21 05:46:18 by reinelt]reinelt6-12/+60
2003-06-13[lcd4linux @ 2003-06-13 06:35:56 by reinelt]reinelt1-22/+100
2003-06-13[lcd4linux @ 2003-06-13 05:11:10 by reinelt]reinelt3-7/+16
2003-05-19[lcd4linux @ 2003-05-19 05:55:17 by reinelt]reinelt1-2/+5
2003-05-14[lcd4linux @ 2003-05-14 06:17:39 by reinelt]reinelt1-21/+63
2003-04-12[lcd4linux @ 2003-04-12 16:23:10 by reinelt]reinelt1-2/+6
2003-04-07[lcd4linux @ 2003-04-07 06:02:58 by reinelt]reinelt6-221/+245
2003-04-04[lcd4linux @ 2003-04-04 06:01:59 by reinelt]reinelt8-375/+516
2003-02-27[lcd4linux @ 2003-02-27 07:48:57 by reinelt]reinelt2-12/+12
2003-02-27[lcd4linux @ 2003-02-27 07:43:10 by reinelt]reinelt11-750/+2247
2003-02-24[lcd4linux @ 2003-02-24 04:50:57 by reinelt]reinelt1-25/+65
2003-02-22[lcd4linux @ 2003-02-22 07:53:09 by reinelt]reinelt23-97/+166
2003-02-22[lcd4linux @ 2003-02-22 07:23:24 by reinelt]reinelt1-88/+102
2003-02-18[lcd4linux @ 2003-02-18 06:13:44 by reinelt]reinelt2-329/+327
2003-02-17[lcd4linux @ 2003-02-17 06:06:12 by reinelt]reinelt1-1/+10
2003-02-17[lcd4linux @ 2003-02-17 04:27:58 by reinelt]reinelt1-13/+32
2003-02-13[lcd4linux @ 2003-02-13 10:40:17 by reinelt]reinelt2-32/+128
2003-02-05[lcd4linux @ 2003-02-05 04:31:38 by reinelt]reinelt2-8/+29
2003-01-12[lcd4linux @ 2003-01-12 06:51:27 by reinelt]reinelt1-8/+16
2002-12-08[lcd4linux @ 2002-12-08 07:36:06 by reinelt]reinelt11-315/+314
2002-12-06[lcd4linux @ 2002-12-06 07:38:43 by reinelt]reinelt6-1305/+2559
2002-12-05[lcd4linux @ 2002-12-05 19:23:01 by reinelt]reinelt3-7/+16
2002-12-05[lcd4linux @ 2002-12-05 19:12:47 by reinelt]reinelt1-3/+15
2002-12-05[lcd4linux @ 2002-12-05 19:09:57 by reinelt]reinelt6-17/+29
2002-09-12[lcd4linux @ 2002-09-12 05:24:54 by reinelt]reinelt1-478/+199
2002-09-11[lcd4linux @ 2002-09-11 05:32:35 by reinelt]reinelt1-255/+47
2002-09-11[lcd4linux @ 2002-09-11 05:16:32 by reinelt]reinelt8-476/+1335
2002-08-30[lcd4linux @ 2002-08-30 03:54:01 by reinelt]reinelt1-3/+8
2002-08-22[lcd4linux @ 2002-08-22 05:51:36 by reinelt]reinelt1-7/+4
2002-08-21[lcd4linux @ 2002-08-21 06:09:53 by reinelt]reinelt2-10/+48
2002-08-19[lcd4linux @ 2002-08-19 10:51:06 by reinelt]reinelt2-259/+60
2002-08-19[lcd4linux @ 2002-08-19 09:43:43 by reinelt]reinelt1-261/+57
2002-08-19[lcd4linux @ 2002-08-19 09:30:18 by reinelt]reinelt1-256/+106
2002-08-19[lcd4linux @ 2002-08-19 09:11:34 by reinelt]reinelt2-257/+51
2002-08-19[lcd4linux @ 2002-08-19 07:52:19 by reinelt]reinelt3-8/+17
2002-08-19[lcd4linux @ 2002-08-19 07:36:29 by reinelt]reinelt7-359/+481
2002-08-19[lcd4linux @ 2002-08-19 04:41:20 by reinelt]reinelt20-32/+166
2002-08-18[lcd4linux @ 2002-08-18 08:11:11 by reinelt]reinelt1-26/+28
2002-08-17[lcd4linux @ 2002-08-17 14:14:21 by reinelt]reinelt3-67/+101
2002-08-17[lcd4linux @ 2002-08-17 13:10:22 by reinelt]reinelt9-182/+765
2002-08-17[lcd4linux @ 2002-08-17 12:54:08 by reinelt]reinelt1-7/+23
ber.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
/* $Id$
 * $URL$
 *
 * generic driver helper for serial and usbserial displays
 *
 * Copyright (C) 1999, 2000 Michael Reinelt <michael@reinelt.co.at>
 * Copyright (C) 2004 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net>
 *
 * This file is part of LCD4Linux.
 *
 * LCD4Linux 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.
 *
 * LCD4Linux 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

/*
 *
 * exported fuctions:
 *
 * int drv_generic_serial_open (char *section, char *driver, unsigned int flags)
 *   opens the serial port
 *
 * int drv_generic_serial_poll (char *string, int len)
 *   reads from the serial or USB port
 *   without retry
 *
 * int drv_generic_serial_read (char *string, int len);
 *   reads from the serial or USB port
 *   with retry
 *
 * void drv_generic_serial_write (char *string, int len);
 *   writes to the serial or USB port
 *
 * int drv_generic_serial_close (void);
 *   closes the serial port
 *
 */

#include "config.h"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <termios.h>
#include <fcntl.h>
#include <time.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>

#include "debug.h"
#include "qprintf.h"
#include "cfg.h"
#include "drv_generic_serial.h"


extern int got_signal;

static char *Section;
static char *Driver;
static char *Port;
static speed_t Speed;
static int Device = -1;


#define LOCK "/var/lock/LCK..%s"


/****************************************/
/*** generic serial/USB communication ***/
/****************************************/

static pid_t drv_generic_serial_lock_port(const char *Port)
{
    char lockfile[256];
    char tempfile[256];
    char buffer[16];
    char *port, *p;
    int fd, len, pid;

    if (strncmp(Port, "/dev/", 5) == 0) {
	port = strdup(Port + 5);
    } else {
	port = strdup(Port);
    }

    while ((p = strchr(port, '/')) != NULL) {
	*p = '_';
    }

    qprintf(lockfile, sizeof(lockfile), LOCK, port);
    qprintf(tempfile, sizeof(tempfile), LOCK, "TMP.XXXXXX");

    free(port);

    if ((fd = mkstemp(tempfile)) == -1) {
	error("mkstemp(%s) failed: %s", tempfile, strerror(errno));
	return -1;
    }

    if (fchmod(fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) == -1) {
	error("fchmod(%s) failed: %s", tempfile, strerror(errno));
	close(fd);
	unlink(tempfile);
	return -1;
    }

    snprintf(buffer, sizeof(buffer), "%10d\n", (int) getpid());
    len = strlen(buffer);
    if (write(fd, buffer, len) != len) {
	error("write(%s) failed: %s", tempfile, strerror(errno));
	close(fd);
	unlink(tempfile);
	return -1;
    }
    close(fd);


    while (link(tempfile, lockfile) == -1) {

	if (errno != EEXIST) {
	    error("link(%s, %s) failed: %s", tempfile, lockfile, strerror(errno));
	    unlink(tempfile);
	    return -1;
	}

	if ((fd = open(lockfile, O_RDONLY)) == -1) {
	    if (errno == ENOENT)
		continue;	/* lockfile disappared */
	    error("open(%s) failed: %s", lockfile, strerror(errno));
	    unlink(tempfile);
	    return -1;
	}

	len = read(fd, buffer, sizeof(buffer) - 1);
	if (len < 0) {
	    error("read(%s) failed: %s", lockfile, strerror(errno));
	    unlink(tempfile);
	    return -1;
	}

	buffer[len] = '\0';
	if (sscanf(buffer, "%d", &pid) != 1 || pid == 0) {
	    error("scan(%s) failed.", lockfile);
	    unlink(tempfile);
	    return -1;
	}

	if (pid == getpid()) {
	    error("%s already locked by us. uh-oh...", lockfile);
	    unlink(tempfile);
	    return 0;
	}

	if ((kill(pid, 0) == -1) && errno == ESRCH) {
	    error("removing stale lockfile %s", lockfile);
	    if (unlink(lockfile) == -1 && errno != ENOENT) {
		error("unlink(%s) failed: %s", lockfile, strerror(errno));
		unlink(tempfile);
		return pid;
	    }
	    continue;
	}
	unlink(tempfile);
	return pid;
    }

    unlink(tempfile);
    return 0;
}


static pid_t drv_generic_serial_unlock_port(const char *Port)
{
    char lockfile[256];
    char *port, *p;

    if (strncmp(Port, "/dev/", 5) == 0) {
	port = strdup(Port + 5);
    } else {
	port = strdup(Port);
    }

    while ((p = strchr(port, '/')) != NULL) {
	*p = '_';
    }

    qprintf(lockfile, sizeof(lockfile), LOCK, port);
    unlink(lockfile);
    free(port);

    return 0;
}


int drv_generic_serial_open(const char *section, const char *driver, const unsigned int flags)
{
    int i, fd;
    pid_t pid;
    struct termios portset;

    Section = (char *) section;
    Driver = (char *) driver;

    Port = cfg_get(section, "Port", NULL);
    if (Port == NULL || *Port == '\0') {
	error("%s: no '%s.Port' entry from %s", Driver, section, cfg_source());
	return -1;
    }

    if (cfg_number(section, "Speed", 19200, 1200, 230400, &i) < 0)
	return -1;
    switch (i) {
    case 1200:
	Speed = B1200;
	break;
    case 2400:
	Speed = B2400;
	break;
    case 4800:
	Speed = B4800;
	break;
    case 9600:
	Speed = B9600;
	break;
    case 19200:
	Speed = B19200;
	break;
    case 38400:
	Speed = B38400;
	break;
    case 57600:
	Speed = B57600;
	break;
    case 115200:
	Speed = B115200;
	break;
#ifdef B230400
    case 230400:
	Speed = B230400;
	break;
#endif
    default:
	error("%s: unsupported speed '%d' from %s", Driver, i, cfg_source());
	return -1;
    }

    info("%s: using port '%s' at %d baud", Driver, Port, i);

    if ((pid = drv_generic_serial_lock_port(Port)) != 0) {
	if (pid == -1)
	    error("%s: port %s could not be locked", Driver, Port);
	else
	    error("%s: port %s is locked by process %d", Driver, Port, pid);
	return -1;
    }

    fd = open(Port, O_RDWR | O_NOCTTY | O_NDELAY);
    if (fd == -1) {
	error("%s: open(%s) failed: %s", Driver, Port, strerror(errno));
	drv_generic_serial_unlock_port(Port);
	return -1;
    }

    if (tcgetattr(fd, &portset) == -1) {
	error("%s: tcgetattr(%s) failed: %s", Driver, Port, strerror(errno));
	drv_generic_serial_unlock_port(Port);
	return -1;
    }

    cfmakeraw(&portset);
    portset.c_cflag |= flags;
    cfsetispeed(&portset, Speed);
    cfsetospeed(&portset, Speed);
    if (tcsetattr(fd, TCSANOW, &portset) == -1) {
	error("%s: tcsetattr(%s) failed: %s", Driver, Port, strerror(errno));
	drv_generic_serial_unlock_port(Port);
	return -1;
    }

    Device = fd;
    return Device;
}


int drv_generic_serial_poll(char *string, const int len)
{
    int ret;
    if (Device == -1)
	return -1;
    ret = read(Device, string, len);
    if (ret < 0 && errno != EAGAIN) {
	error("%s: read(%s) failed: %s", Driver, Port, strerror(errno));
    }
    return ret;
}


int drv_generic_serial_read(char *string, const int len)
{
    int count, run, ret;

    count = len < 0 ? -len : len;

    for (run = 0; run < 10; run++) {
	ret = drv_generic_serial_poll(string, count);
	if (ret >= 0 || errno != EAGAIN)
	    break;
	info("%s: read(%s): EAGAIN", Driver, Port);
	usleep(1000);
    }

    if (ret > 0 && ret != count && len > 0) {
	error("%s: partial read(%s): len=%d ret=%d", Driver, Port, len, ret);
    }

    return ret;
}


void drv_generic_serial_write(const char *string, const int len)
{
    static int error = 0;
    int run, ret;

    if (Device == -1) {
	error("%s: write to closed port %s failed!", Driver, Port);
	return;
    }

    for (run = 0; run < 10; run++) {
	ret = write(Device, string, len);
	if (ret >= 0 || errno != EAGAIN) {
	    break;
	}
	/* EAGAIN: retry 10 times, emit message after 2nd retry */
	if (run > 0) {
	    info("%s: write(%s): EAGAIN #%d", Driver, Port, run);
	}
	usleep(1000);
    }

    if (ret < 0) {
	error("%s: write(%s) failed: %s", Driver, Port, strerror(errno));
	if (++error > 10) {
	    error("%s: too much errors, giving up", Driver);
	    got_signal = -1;
	}
    } else if (ret != len) {
	error("%s: partial write(%s): len=%d ret=%d", Driver, Port, len, ret);
    } else {
	error = 0;
    }
    return;
}


int drv_generic_serial_close(void)
{
    info("%s: closing port %s", Driver, Port);
    close(Device);
    drv_generic_serial_unlock_port(Port);
    free(Port);
    return 0;
}