Open Source Telephony
 help / color / mirror / Atom feed
From: Philippe Nunes <philippe.nunes@linux.intel.com>
To: ofono@ofono.org
Subject: [PATCH v2 07/11] stk: Add support for the proactive command 'Receive data'
Date: Mon, 04 Jul 2011 18:06:38 +0200	[thread overview]
Message-ID: <1309795602-26046-8-git-send-email-philippe.nunes@linux.intel.com> (raw)
In-Reply-To: <1309795602-26046-1-git-send-email-philippe.nunes@linux.intel.com>

[-- Attachment #1: Type: text/plain, Size: 6128 bytes --]

---
 src/stk.c |  133 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 133 insertions(+), 0 deletions(-)

diff --git a/src/stk.c b/src/stk.c
index 58fdbdf..612eafb 100644
--- a/src/stk.c
+++ b/src/stk.c
@@ -31,6 +31,7 @@
 #include <arpa/inet.h>
 #include <net/if.h>
 #include <unistd.h>
+#include <sys/ioctl.h>
 
 #include <glib.h>
 #include <gdbus.h>
@@ -86,6 +87,7 @@ struct ofono_stk {
 	struct {
 		struct stk_channel channel;
 		struct ofono_gprs_primary_context context;
+		gsize rx_remaining;
 		gsize tx_avail;
 		gboolean link_on_demand;
 		struct ofono_gprs *gprs;
@@ -126,6 +128,13 @@ static void timers_update(struct ofono_stk *stk);
  */
 #define DEFAULT_CHANNEL_ID 1
 
+/*
+ * According the structure and coding of the Terminal response defined in
+ * TS 102 223 section 6.8, the maximum number of bytes possible for the channel
+ * data object is 243
+ */
+#define CHANNEL_DATA_OBJECT_MAX_LENGTH 243
+
 static int stk_respond(struct ofono_stk *stk, struct stk_response *rsp,
 			ofono_stk_generic_cb_t cb)
 {
@@ -538,6 +547,11 @@ static void stk_close_channel(struct ofono_stk *stk)
 	struct ofono_modem *modem = __ofono_atom_get_modem(stk->atom);
 	struct ofono_atom *gprs_atom;
 
+	if (stk->bip.read_watch > 0)
+		g_source_remove(stk->bip.read_watch);
+	else if (stk->bip.io)
+		g_io_channel_unref(stk->bip.io);
+
 	gprs_atom = __ofono_modem_find_atom(modem, OFONO_ATOM_TYPE_GPRS);
 	if (gprs_atom == NULL || !__ofono_atom_get_registered(gprs_atom))
 		goto out;
@@ -609,6 +623,31 @@ static void stk_alpha_id_unset(struct ofono_stk *stk)
 static gboolean receive_callback(GIOChannel *channel, GIOCondition cond,
 				gpointer userdata)
 {
+	struct ofono_stk *stk = (struct ofono_stk *) userdata;
+
+	if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP))
+		return FALSE;
+
+	if (cond & G_IO_IN) {
+		int fd = g_io_channel_unix_get_fd(channel);
+		int len = 0;
+
+		ioctl(fd, FIONREAD, &len);
+		DBG("%zd bytes to read", len);
+
+		/*
+		 * TODO
+		 * send data available event if the buffer is empty
+		 * when new data arrives in it
+		 */
+
+		stk->bip.rx_remaining = len;
+
+		/* Disable the read watch until the SIM requests all bytes */
+		g_io_channel_ref(channel);
+		g_source_remove(stk->bip.read_watch);
+		stk->bip.read_watch = 0;
+	}
 	return TRUE;
 }
 
@@ -787,6 +826,7 @@ static void stk_open_channel(struct ofono_stk *stk)
 
 	stk->bip.channel.id = DEFAULT_CHANNEL_ID;
 	stk->bip.tx_avail = oc->buf_size;
+	stk->bip.io = NULL;
 
 	memset(&stk->bip.context, 0, sizeof(stk->bip.context));
 	stk->bip.context.proto = OFONO_GPRS_PROTO_IP;
@@ -872,6 +912,62 @@ out:
 		stk_close_channel(stk);
 }
 
+static void stk_receive_data(struct ofono_stk *stk, unsigned char toread)
+{
+	struct stk_response rsp;
+	struct ofono_error failure = { .type = OFONO_ERROR_TYPE_FAILURE };
+	gsize bytes_read;
+	unsigned char buf[CHANNEL_DATA_OBJECT_MAX_LENGTH];
+	GIOStatus status;
+	int fd;
+
+	memset(&rsp, 0, sizeof(rsp));
+	rsp.result.type = STK_RESULT_TYPE_SUCCESS;
+
+	/* update bytes_to_read */
+	fd = g_io_channel_unix_get_fd(stk->bip.io);
+	ioctl(fd, FIONREAD, &stk->bip.rx_remaining);
+	DBG("%zd bytes to read", stk->bip.rx_remaining);
+
+	if (stk->bip.rx_remaining == 0) {
+		rsp.result.type = STK_RESULT_TYPE_MISSING_INFO;
+		goto out;
+	}
+
+	if (toread > stk->bip.rx_remaining) {
+		rsp.result.type = STK_RESULT_TYPE_MISSING_INFO;
+		toread = stk->bip.rx_remaining;
+	}
+
+	if (toread > CHANNEL_DATA_OBJECT_MAX_LENGTH)
+		toread = CHANNEL_DATA_OBJECT_MAX_LENGTH;
+
+	status = g_io_channel_read_chars(stk->bip.io, (gchar *)buf, toread,
+						&bytes_read, NULL);
+
+	if (status != G_IO_STATUS_NORMAL && status != G_IO_STATUS_AGAIN) {
+		rsp.result.type = STK_RESULT_TYPE_MISSING_INFO;
+		goto out;
+	}
+
+	rsp.receive_data.rx_data.len = bytes_read;
+	rsp.receive_data.rx_data.array = buf;
+	stk->bip.rx_remaining -= bytes_read;
+	rsp.receive_data.rx_remaining = stk->bip.rx_remaining;
+
+	if (stk->bip.rx_remaining == 0) {
+		stk->bip.read_watch = g_io_add_watch_full(stk->bip.io,
+					G_PRIORITY_DEFAULT,
+					G_IO_IN | G_IO_HUP | G_IO_ERR |
+					G_IO_NVAL, receive_callback, stk, NULL);
+		g_io_channel_unref(stk->bip.io);
+	}
+
+out:
+	if (stk_respond(stk, &rsp, stk_command_cb))
+		stk_command_cb(&failure, stk);
+}
+
 static int duration_to_msecs(const struct stk_duration *duration)
 {
 	int msecs = duration->interval;
@@ -3067,6 +3163,38 @@ static gboolean handle_command_close_channel(const struct stk_command *cmd,
 	return FALSE;
 }
 
+static gboolean handle_command_receive_data(const struct stk_command *cmd,
+						struct stk_response *rsp,
+						struct ofono_stk *stk)
+{
+	const struct stk_command_receive_data *rd = &cmd->receive_data;
+
+	/* Check if channel identifier is valid or already closed */
+	if (cmd->dst != (stk->bip.channel.id | 0x20)) {
+		unsigned char addnl_info[1];
+
+		addnl_info[1] = STK_RESULT_ADDNL_BIP_PB_CHANNEL_ID_NOT_VALID;
+		ADD_ERROR_RESULT(rsp->result, STK_RESULT_TYPE_BIP_ERROR,
+					addnl_info);
+		return TRUE;
+	}
+
+	/*
+	 * Don't inform the user during data transfer if AID is
+	 * a null data object or is not provided
+	 */
+	if (rd->alpha_id && rd->alpha_id[0] != '\0')
+		stk_alpha_id_set(stk, rd->alpha_id, &rd->text_attr,
+					&rd->icon_id);
+
+	stk->respond_on_exit = TRUE;
+	stk->cancel_cmd = stk_request_cancel;
+
+	stk_receive_data(stk, rd->data_len);
+
+	return FALSE;
+}
+
 static gboolean handle_command_get_channel_status(const struct stk_command *cmd,
 						struct stk_response *rsp,
 						struct ofono_stk *stk)
@@ -3280,6 +3408,11 @@ void ofono_stk_proactive_command_notify(struct ofono_stk *stk,
 							&rsp, stk);
 		break;
 
+	case STK_COMMAND_TYPE_RECEIVE_DATA:
+		respond = handle_command_receive_data(stk->pending_cmd,
+							&rsp, stk);
+		break;
+
 	case STK_COMMAND_TYPE_GET_CHANNEL_STATUS:
 		respond = handle_command_get_channel_status(stk->pending_cmd,
 							&rsp, stk);
-- 
1.7.1


  parent reply	other threads:[~2011-07-04 16:06 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-07-04 16:06 [PATCH v2 00/11] Add BIP commands support Philippe Nunes
2011-07-04 16:06 ` [PATCH v2 01/11] gprs: Add private APIs to activate/deactivate a private context for STK Philippe Nunes
2011-07-04 16:06 ` [PATCH v2 02/11] stk: Add support for the proactive command 'Open channel' Philippe Nunes
2011-07-04 16:06 ` [PATCH v2 03/11] stk: Add support for the proactive command 'Get channel status' Philippe Nunes
2011-07-04 16:06 ` [PATCH v2 04/11] stk: Add support for the proactive command 'Close channel' Philippe Nunes
2011-07-04 16:06 ` [PATCH v2 05/11] stk: Add 'ofono_stk_activate_cb' definition Philippe Nunes
2011-07-04 16:06 ` [PATCH v2 06/11] stk: Add 'ofono_stk_deactivate_context_cb' definition Philippe Nunes
2011-07-04 16:06 ` Philippe Nunes [this message]
2011-07-04 16:06 ` [PATCH v2 08/11] stk: Add support for the proactive command 'Send data' Philippe Nunes
2011-07-04 16:06 ` [PATCH v2 09/11] stk: Add support of the Setup event list proactive command Philippe Nunes
2011-07-04 16:06 ` [PATCH v2 10/11] stk: Add host route to route the traffic through the stk interface Philippe Nunes
2011-07-04 16:06 ` [PATCH v2 11/11] gprs: Add API to set a 'detach'notification callback Philippe Nunes

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1309795602-26046-8-git-send-email-philippe.nunes@linux.intel.com \
    --to=philippe.nunes@linux.intel.com \
    --cc=ofono@ofono.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox