From b7e06668878af06fc6e74842861e725b982b142f Mon Sep 17 00:00:00 2001 From: James Maki Date: Fri, 7 May 2010 10:12:55 -0500 Subject: add device lock --- src/atcmd.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'src/atcmd.c') diff --git a/src/atcmd.c b/src/atcmd.c index 55d2ebc..6dee9ed 100644 --- a/src/atcmd.c +++ b/src/atcmd.c @@ -267,6 +267,10 @@ int tty_set_read_timeout(int fd, int timeout) int tty_close(int fd) { + if (fd < 0) { + return -1; + } + tcflush(fd, TCIOFLUSH); return close(fd); } @@ -1113,19 +1117,41 @@ static int sms_atcmd_init(int fd) return 0; } +static char *lock_path; + +int sms_device_close(int fd) +{ + int ret; + + ret = tty_close(fd); + + if (lock_path) { + unlink(lock_path); + } + + return ret; +} + int sms_device_open(void) { int fd; int tmp; + lock_path = device_lock(Global.core.device); + if (!lock_path) { + return -1; + } + fd = tty_open(Global.core.device, Global.core.baud_rate); if (fd < 0) { + unlink(lock_path); return -1; } tmp = tty_set_read_timeout(fd, Global.core.read_timeout); if (tmp < 0) { close(fd); + unlink(lock_path); return tmp; } @@ -1133,6 +1159,7 @@ int sms_device_open(void) tmp = sms_atcmd_init(fd); if (tmp < 0) { close(fd); + unlink(lock_path); return tmp; } } -- cgit v1.2.3