summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarcel Holtmann <marcel@holtmann.org>2009-06-30 12:48:42 -0700
committerMarcel Holtmann <marcel@holtmann.org>2009-06-30 12:48:42 -0700
commit19493f049ddd01ed8b03c11ef7159ef90a2896df (patch)
tree8862ae42b7088f382f559f57bb7639227c401feb
parent16a08aaac7c8b30573c86ad9e1caf1277ec19b35 (diff)
downloadconnman-19493f049ddd01ed8b03c11ef7159ef90a2896df.tar.gz
connman-19493f049ddd01ed8b03c11ef7159ef90a2896df.tar.bz2
connman-19493f049ddd01ed8b03c11ef7159ef90a2896df.zip
Add support for opening TTYs directly
-rw-r--r--gatchat/gatchat.c41
-rw-r--r--gatchat/gatchat.h1
2 files changed, 42 insertions, 0 deletions
diff --git a/gatchat/gatchat.c b/gatchat/gatchat.c
index b400157d..dc0d7c9c 100644
--- a/gatchat/gatchat.c
+++ b/gatchat/gatchat.c
@@ -24,8 +24,11 @@
#endif
#include <stdio.h>
+#include <fcntl.h>
+#include <unistd.h>
#include <string.h>
#include <assert.h>
+#include <termios.h>
#include <glib.h>
@@ -935,6 +938,44 @@ error:
return NULL;
}
+static int open_device(const char *device)
+{
+ struct termios ti;
+ int fd;
+
+ fd = open(device, O_RDWR | O_NOCTTY);
+ if (fd < 0)
+ return -1;
+
+ tcflush(fd, TCIOFLUSH);
+
+ /* Switch TTY to raw mode */
+ memset(&ti, 0, sizeof(ti));
+ cfmakeraw(&ti);
+
+ tcsetattr(fd, TCSANOW, &ti);
+
+ return fd;
+}
+
+GAtChat *g_at_chat_new_from_tty(const char *device, int flags)
+{
+ GIOChannel *channel;
+ int fd;
+
+ fd = open_device(device);
+ if (fd < 0)
+ return NULL;
+
+ channel = g_io_channel_unix_new(fd);
+ if (!channel) {
+ close(fd);
+ return NULL;
+ }
+
+ return g_at_chat_new(channel, flags);
+}
+
GAtChat *g_at_chat_ref(GAtChat *chat)
{
if (chat == NULL)
diff --git a/gatchat/gatchat.h b/gatchat/gatchat.h
index 52c6b368..3c8a889f 100644
--- a/gatchat/gatchat.h
+++ b/gatchat/gatchat.h
@@ -44,6 +44,7 @@ enum _GAtChatFlags {
typedef enum _GAtChatFlags GAtChatFlags;
GAtChat *g_at_chat_new(GIOChannel *channel, int flags);
+GAtChat *g_at_chat_new_from_tty(const char *device, int flags);
GAtChat *g_at_chat_ref(GAtChat *chat);
void g_at_chat_unref(GAtChat *chat);