Open Source Telephony
 help / color / mirror / Atom feed
From: Philippe Nunes <philippe.nunes@linux.intel.com>
To: ofono@ofono.org
Subject: [PATCH 05/11] stk: Add 'ofono_stk_activate_cb' definition
Date: Tue, 28 Jun 2011 19:16:17 +0200	[thread overview]
Message-ID: <1309281383-6605-6-git-send-email-philippe.nunes@linux.intel.com> (raw)
In-Reply-To: <1309281383-6605-1-git-send-email-philippe.nunes@linux.intel.com>

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

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

diff --git a/src/stk.c b/src/stk.c
index 8426af7..1db45eb 100644
--- a/src/stk.c
+++ b/src/stk.c
@@ -28,6 +28,9 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdint.h>
+#include <arpa/inet.h>
+#include <net/if.h>
+#include <unistd.h>
 
 #include <glib.h>
 #include <gdbus.h>
@@ -87,6 +90,9 @@ struct ofono_stk {
 	struct ofono_gprs *gprs;
 	struct stk_bearer_description bearer_desc;
 	enum stk_transport_protocol_type protocol;
+	struct sockaddr_in dest_addr;
+	GIOChannel *io;
+	guint read_watch;
 };
 
 struct envelope_op {
@@ -571,10 +577,150 @@ static void stk_alpha_id_unset(struct ofono_stk *stk)
 	stk_agent_request_cancel(stk->current_agent);
 }
 
+static gboolean receive_callback(GIOChannel *channel, GIOCondition cond,
+				gpointer userdata)
+{
+	return TRUE;
+}
+
 static void ofono_stk_activate_context_cb(int error, const char *interface,
 						const char *ip,
 						void *data)
 {
+	struct ofono_stk *stk = data;
+	struct stk_response rsp;
+	struct ofono_error failure = { .type = OFONO_ERROR_TYPE_FAILURE };
+	GIOChannel *io;
+	int sk;
+
+	DBG("");
+
+	memset(&rsp, 0, sizeof(rsp));
+	rsp.result.type = STK_RESULT_TYPE_SUCCESS;
+
+	if (error < 0) {
+		ofono_error("Failed to connect gprs context");
+
+		if (stk->pending_cmd == NULL ||
+				(stk->pending_cmd->type !=
+				STK_COMMAND_TYPE_OPEN_CHANNEL &&
+				stk->pending_cmd->type !=
+				STK_COMMAND_TYPE_SEND_DATA)) {
+			/* TODO send channel status event */
+			return;
+		} else {
+			rsp.result.type = STK_RESULT_TYPE_NETWORK_UNAVAILABLE;
+			goto out;
+		}
+	} else {
+		DBG("Interface %s, IP = %s", interface, ip);
+		stk->channel.status = STK_CHANNEL_PACKET_DATA_SERVICE_ACTIVATED;
+	}
+
+	if (stk->protocol == STK_TRANSPORT_PROTOCOL_TCP_CLIENT_REMOTE) {
+		sk = socket(AF_INET, SOCK_STREAM, 0);
+		if (sk < 0) {
+			rsp.result.type = STK_RESULT_TYPE_NOT_CAPABLE;
+			goto out;
+		}
+
+		error = connect(sk, (struct sockaddr *) &stk->dest_addr,
+						sizeof(stk->dest_addr));
+
+		if (error < 0) {
+			close(sk);
+			rsp.result.type = STK_RESULT_TYPE_NOT_CAPABLE;
+			goto out;
+		}
+	} else if (stk->protocol == STK_TRANSPORT_PROTOCOL_UDP_CLIENT_REMOTE) {
+		struct sockaddr_in addr;
+
+		sk = socket(AF_INET, SOCK_DGRAM, 0);
+		if (sk < 0) {
+			rsp.result.type = STK_RESULT_TYPE_NOT_CAPABLE;
+			goto out;
+		}
+
+		memset(&addr, 0, sizeof(addr));
+		addr.sin_family = AF_INET;
+		addr.sin_addr.s_addr = htonl(INADDR_ANY);
+		addr.sin_port = htons(0);
+
+		error = bind(sk, (struct sockaddr *) &addr, sizeof(addr));
+		if (error < 0) {
+			close(sk);
+			rsp.result.type = STK_RESULT_TYPE_NOT_CAPABLE;
+			goto out;
+		}
+	} else {
+		/* raw IP */
+		sk = socket(AF_INET, SOCK_RAW, 0);
+		if (sk < 0) {
+			rsp.result.type = STK_RESULT_TYPE_NOT_CAPABLE;
+			goto out;
+		}
+
+		/* Bind socket to specified interface */
+		if (interface) {
+			struct ifreq ifreq;
+
+			memset(&ifreq, 0, sizeof(ifreq));
+			strcpy(ifreq.ifr_name, interface);
+			error = setsockopt(sk, SOL_SOCKET,
+					SO_BINDTODEVICE, &ifreq, sizeof(ifreq));
+			if (error < 0) {
+				ofono_error("Failed to bind socket "
+						"to interface");
+				close(sk);
+				rsp.result.type = STK_RESULT_TYPE_NOT_CAPABLE;
+				goto out;
+			}
+		}
+	}
+
+	io = g_io_channel_unix_new(sk);
+	if (io == NULL) {
+		close(sk);
+		error = -ENOMEM;
+		rsp.result.type = STK_RESULT_TYPE_NOT_CAPABLE;
+		goto out;
+	}
+
+	g_io_channel_set_close_on_unref(io, TRUE);
+	g_io_channel_set_flags(io, 0, NULL);
+	g_io_channel_set_encoding(io, NULL, NULL);
+	g_io_channel_set_buffered(io, FALSE);
+
+	stk->read_watch = g_io_add_watch_full(io, G_PRIORITY_DEFAULT,
+				G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
+				receive_callback, stk, NULL);
+	stk->io = io;
+	g_io_channel_unref(io);
+
+	if (stk->pending_cmd == NULL) {
+		/* TODO send channel status event */
+		return;
+	} else if (stk->pending_cmd->type == STK_COMMAND_TYPE_OPEN_CHANNEL) {
+		rsp.open_channel.channel.id = stk->channel.id;
+		rsp.open_channel.channel.status = stk->channel.status;
+		rsp.open_channel.buf_size = stk->tx_avail;
+		memcpy(&rsp.open_channel.bearer_desc, &stk->bearer_desc,
+				sizeof(struct stk_bearer_description));
+	} else if (stk->pending_cmd->type == STK_COMMAND_TYPE_SEND_DATA &&
+			stk->link_on_demand) {
+		/* TODO send data */
+		rsp.send_data.tx_avail = stk->tx_avail;
+	} else {
+		/* TODO send channel status event */
+		return;
+	}
+
+out:
+	if (stk_respond(stk, &rsp, stk_command_cb))
+		stk_command_cb(&failure, stk);
+
+	if (rsp.result.type != STK_RESULT_TYPE_SUCCESS)
+		stk_close_channel(stk);
 }
 
 static void stk_open_channel(struct ofono_stk *stk)
@@ -602,6 +748,11 @@ static void stk_open_channel(struct ofono_stk *stk)
 		 */
 		rsp.result.type = STK_RESULT_TYPE_NOT_CAPABLE;
 		goto out;
+	} else if (oc->data_dest_addr.type == STK_ADDRESS_IPV4) {
+		memset(&stk->dest_addr, 0, sizeof(stk->dest_addr));
+		stk->dest_addr.sin_family = AF_INET;
+		stk->dest_addr.sin_port = htons(oc->uti.port);
+		stk->dest_addr.sin_addr.s_addr = oc->data_dest_addr.addr.ipv4;
 	}
 
 	stk->channel.id = DEFAULT_CHANNEL_ID;
-- 
1.7.1


  parent reply	other threads:[~2011-06-28 17:16 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-06-28 17:16 [PATCH 00/11] Add BIP command support Philippe Nunes
2011-06-28 17:16 ` [PATCH 01/11] gprs: Add private APIs to activate/deactivate private contexts Philippe Nunes
2011-06-29 22:22   ` Denis Kenzior
2011-06-30 16:24     ` Philippe Nunes
2011-06-30  6:11       ` Denis Kenzior
2011-06-28 17:16 ` [PATCH 02/11] stk: Add support for the proactive command 'Open channel' Philippe Nunes
2011-06-29 22:38   ` Denis Kenzior
2011-06-28 17:16 ` [PATCH 03/11] stk: Add support for the proactive command 'Get channel status' Philippe Nunes
2011-06-28 17:16 ` [PATCH 04/11] stk: Add support for the proactive command 'Close channel' Philippe Nunes
2011-06-28 17:16 ` Philippe Nunes [this message]
2011-06-28 17:16 ` [PATCH 06/11] stk: Add 'ofono_stk_deactivate_context_cb' definition Philippe Nunes
2011-06-28 17:16 ` [PATCH 07/11] stk: Add support for the proactive command 'Receive data' Philippe Nunes
2011-06-28 17:16 ` [PATCH 08/11] stk: Add support for the proactive command 'Send data' Philippe Nunes
2011-06-28 17:16 ` [PATCH 09/11] stk: Add support of the Setup event list proactive command Philippe Nunes
2011-06-28 17:16 ` [PATCH 10/11] stk: Add host route to route the traffic through the stk interface Philippe Nunes
2011-06-28 17:16 ` [PATCH 11/11] gprs: Add API to set a 'detach'notification callback Philippe Nunes
2011-06-29 22:46   ` Denis Kenzior

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=1309281383-6605-6-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