From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <pawelx.wodkowski@intel.com>
Received: from mga01.intel.com (mga01.intel.com [192.55.52.88])
 by dpdk.org (Postfix) with ESMTP id 072085694
 for <dev@dpdk.org>; Wed, 13 May 2015 14:07:39 +0200 (CEST)
Received: from orsmga002.jf.intel.com ([10.7.209.21])
 by fmsmga101.fm.intel.com with ESMTP; 13 May 2015 05:07:39 -0700
X-ExtLoop1: 1
X-IronPort-AV: E=Sophos;i="5.13,420,1427785200"; d="scan'208";a="728494592"
Received: from unknown (HELO stargo) ([10.217.248.233])
 by orsmga002.jf.intel.com with SMTP; 13 May 2015 05:07:38 -0700
Received: by stargo (sSMTP sendmail emulation); Wed, 13 May 2015 14:01:42 +0200
From: Pawel Wodkowski <pawelx.wodkowski@intel.com>
To: dev@dpdk.org
Date: Wed, 13 May 2015 14:00:01 +0200
Message-Id: <1431518401-16315-3-git-send-email-pawelx.wodkowski@intel.com>
X-Mailer: git-send-email 1.9.1
In-Reply-To: <1431518401-16315-1-git-send-email-pawelx.wodkowski@intel.com>
References: <1431429019-21130-1-git-send-email-pawelx.wodkowski@intel.com>
 <1431518401-16315-1-git-send-email-pawelx.wodkowski@intel.com>
Subject: [dpdk-dev] [PATCH v2 2/2] cmdline: add polling mode for command line
X-BeenThere: dev@dpdk.org
X-Mailman-Version: 2.1.15
Precedence: list
List-Id: patches and discussions about DPDK <dev.dpdk.org>
List-Unsubscribe: <http://dpdk.org/ml/options/dev>,
 <mailto:dev-request@dpdk.org?subject=unsubscribe>
List-Archive: <http://dpdk.org/ml/archives/dev/>
List-Post: <mailto:dev@dpdk.org>
List-Help: <mailto:dev-request@dpdk.org?subject=help>
List-Subscribe: <http://dpdk.org/ml/listinfo/dev>,
 <mailto:dev-request@dpdk.org?subject=subscribe>
X-List-Received-Date: Wed, 13 May 2015 12:07:40 -0000

This patch adds the ability to process console input in the same thread
as packet processing by using poll() function.

Signed-off-by: Pawel Wodkowski <pawelx.wodkowski@intel.com>
---
 doc/api/doxy-api.conf                      |  1 +
 lib/librte_cmdline/cmdline.c               | 35 ++++++++++++++++++++++++++++++
 lib/librte_cmdline/cmdline.h               | 21 ++++++++++++++++++
 lib/librte_cmdline/rte_cmdline_version.map |  8 +++++++
 4 files changed, 65 insertions(+)

diff --git a/doc/api/doxy-api.conf b/doc/api/doxy-api.conf
index 50b0105..51b11c7 100644
--- a/doc/api/doxy-api.conf
+++ b/doc/api/doxy-api.conf
@@ -33,6 +33,7 @@ INPUT                   = doc/api/doxy-api-index.md \
                           lib/librte_eal/common/include \
                           lib/librte_eal/common/include/generic \
                           lib/librte_acl \
+                          lib/librte_cmdline \
                           lib/librte_distributor \
                           lib/librte_ether \
                           lib/librte_hash \
diff --git a/lib/librte_cmdline/cmdline.c b/lib/librte_cmdline/cmdline.c
index e61c4f2..6a55f1f 100644
--- a/lib/librte_cmdline/cmdline.c
+++ b/lib/librte_cmdline/cmdline.c
@@ -65,6 +65,7 @@
 #include <stdarg.h>
 #include <inttypes.h>
 #include <fcntl.h>
+#include <poll.h>
 #include <errno.h>
 #include <termios.h>
 #include <netinet/in.h>
@@ -246,6 +247,40 @@ cmdline_quit(struct cmdline *cl)
 	rdline_quit(&cl->rdl);
 }
 
+int
+cmdline_poll(struct cmdline *cl)
+{
+	struct pollfd pfd;
+	int status;
+	ssize_t read_status;
+	char c;
+
+	if (!cl)
+		return -EINVAL;
+	else if (cl->rdl.status == RDLINE_EXITED)
+		return RDLINE_EXITED;
+
+	pfd.fd = cl->s_in;
+	pfd.events = POLLIN;
+	pfd.revents = 0;
+
+	status = poll(&pfd, 1, 0);
+	if (status < 0)
+		return status;
+	else if (status > 0) {
+		c = -1;
+		read_status = read(cl->s_in, &c, 1);
+		if (read_status < 0)
+			return read_status;
+
+		status = cmdline_in(cl, &c, 1);
+		if (status < 0 && cl->rdl.status != RDLINE_EXITED)
+			return status;
+	}
+
+	return cl->rdl.status;
+}
+
 void
 cmdline_interact(struct cmdline *cl)
 {
diff --git a/lib/librte_cmdline/cmdline.h b/lib/librte_cmdline/cmdline.h
index 9085ff6..2578ca8 100644
--- a/lib/librte_cmdline/cmdline.h
+++ b/lib/librte_cmdline/cmdline.h
@@ -64,6 +64,12 @@
 #include <termios.h>
 #include <cmdline_rdline.h>
 
+/**
+ * @file
+ *
+ * Command line API
+ */
+
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -84,6 +90,21 @@ void cmdline_printf(const struct cmdline *cl, const char *fmt, ...)
 	__attribute__((format(printf,2,3)));
 int cmdline_in(struct cmdline *cl, const char *buf, int size);
 int cmdline_write_char(struct rdline *rdl, char c);
+
+/**
+ * This function is nonblocking equivalent of ``cmdline_interact()``. It polls
+ * *cl* for one character and interpret it. If return value is *RDLINE_EXITED*
+ * it mean that ``cmdline_quit()`` was invoked.
+ *
+ * @param cl
+ *   The command line object.
+ *
+ * @return
+ *   On success return object status - one of *enum rdline_status*.
+ *   On error return negative value.
+ */
+int cmdline_poll(struct cmdline *cl);
+
 void cmdline_interact(struct cmdline *cl);
 void cmdline_quit(struct cmdline *cl);
 
diff --git a/lib/librte_cmdline/rte_cmdline_version.map b/lib/librte_cmdline/rte_cmdline_version.map
index 6193462..1b0c863 100644
--- a/lib/librte_cmdline/rte_cmdline_version.map
+++ b/lib/librte_cmdline/rte_cmdline_version.map
@@ -69,3 +69,11 @@ DPDK_2.0 {
 
 	local: *;
 };
+
+DPDK_2.1 {
+	global:
+
+	cmdline_poll;
+
+	local: *;
+} DPDK_2.0;
-- 
1.9.1