From: Ravi kumar Veeramally <ravikumar.veeramally@linux.intel.com>
To: linux-bluetooth@vger.kernel.org
Cc: Ravi kumar Veeramally <ravikumar.veeramally@linux.intel.com>
Subject: [PATCH_v2 4/6] bnep: Refactored bnep connect and disconnect calls
Date: Tue, 17 Dec 2013 00:05:14 +0200 [thread overview]
Message-ID: <1387231516-4127-4-git-send-email-ravikumar.veeramally@linux.intel.com> (raw)
In-Reply-To: <1387231516-4127-1-git-send-email-ravikumar.veeramally@linux.intel.com>
Refactored bnep connect and disconnect calls to simply and
keeping bnep related functionality behind curtains.
Provided bnep_conn struct globally. bnep_connect calls takes
care of bnep_setup until interface up then connect callback
will be called, disconnect_cb is registered as watchdog and it
will triggered by remote device on disconnect or any other
I/O error. bnep_disconnect should be called only when iface is
up/connected.
---
android/pan.c | 56 +++++++--------------
profiles/network/bnep.c | 112 +++++++++++++++++++++++++-----------------
profiles/network/bnep.h | 10 ++--
profiles/network/connection.c | 50 ++++++++++++-------
4 files changed, 122 insertions(+), 106 deletions(-)
diff --git a/android/pan.c b/android/pan.c
index 03db350..0cae6ac 100644
--- a/android/pan.c
+++ b/android/pan.c
@@ -54,7 +54,7 @@ struct pan_device {
uint8_t conn_state;
uint8_t role;
GIOChannel *io;
- guint watch;
+ struct bnep_conn *bnep;
};
static int device_cmp(gconstpointer s, gconstpointer user_data)
@@ -105,38 +105,31 @@ static void bt_pan_notify_ctrl_state(struct pan_device *dev, uint8_t state)
static void destroy_pan_device(struct pan_device *dev)
{
- local_role = HAL_PAN_ROLE_NONE;
-
- if (dev->watch > 0) {
- g_source_remove(dev->watch);
- dev->watch = 0;
- }
-
if (dev->io) {
+ g_io_channel_shutdown(dev->io, TRUE, NULL);
g_io_channel_unref(dev->io);
dev->io = NULL;
}
+ if (dev->conn_state == HAL_PAN_STATE_CONNECTED)
+ bnep_disconnect(dev->bnep);
+
bt_pan_notify_conn_state(dev, HAL_PAN_STATE_DISCONNECTED);
+ bnep_free(dev->bnep);
devices = g_slist_remove(devices, dev);
g_free(dev);
}
-static gboolean bnep_watchdog_cb(GIOChannel *chan, GIOCondition cond,
- gpointer data)
+static void bnep_disconn_cb(void *data)
{
struct pan_device *dev = data;
DBG("%s disconnected", dev->iface);
- bnep_if_down(dev->iface);
- bnep_conndel(&dev->dst);
destroy_pan_device(dev);
-
- return FALSE;
}
-static void bnep_conn_cb(GIOChannel *chan, char *iface, int err, void *data)
+static void bnep_conn_cb(char *iface, int err, void *data)
{
struct pan_device *dev = data;
@@ -144,22 +137,15 @@ static void bnep_conn_cb(GIOChannel *chan, char *iface, int err, void *data)
if (err < 0) {
error("bnep connect req failed: %s", strerror(-err));
- bnep_conndel(&dev->dst);
destroy_pan_device(dev);
return;
}
- memcpy(dev->iface, iface, sizeof(dev->iface));
-
- DBG("%s connected", dev->iface);
+ DBG("%s connected", iface);
+ memcpy(dev->iface, iface, sizeof(dev->iface));
bt_pan_notify_ctrl_state(dev, HAL_PAN_CTRL_ENABLED);
bt_pan_notify_conn_state(dev, HAL_PAN_STATE_CONNECTED);
-
- dev->watch = g_io_add_watch(chan, G_IO_ERR | G_IO_HUP | G_IO_NVAL,
- bnep_watchdog_cb, dev);
- g_io_channel_unref(dev->io);
- dev->io = NULL;
}
static void bt_io_connect_cb(GIOChannel *chan, GError *err, gpointer data)
@@ -179,7 +165,13 @@ static void bt_io_connect_cb(GIOChannel *chan, GError *err, gpointer data)
dst = (dev->role == HAL_PAN_ROLE_NAP) ? BNEP_SVC_NAP : BNEP_SVC_PANU;
sk = g_io_channel_unix_get_fd(dev->io);
- perr = bnep_connect(sk, src, dst, bnep_conn_cb, dev);
+ dev->bnep = bnep_new(src, dst, &dev->dst);
+ if (!dev->bnep) {
+ error("unable to allocate memory for dev->bnep");
+ goto fail;
+ }
+
+ perr = bnep_connect(dev->bnep, sk, bnep_conn_cb, bnep_disconn_cb, dev);
if (perr < 0) {
error("bnep connect req failed: %s", strerror(-perr));
goto fail;
@@ -267,7 +259,7 @@ static void bt_pan_disconnect(const void *buf, uint16_t len)
{
const struct hal_cmd_pan_disconnect *cmd = buf;
struct pan_device *dev;
- uint8_t status;
+ uint8_t status = HAL_STATUS_FAILED;
GSList *l;
bdaddr_t dst;
@@ -276,20 +268,10 @@ static void bt_pan_disconnect(const void *buf, uint16_t len)
android2bdaddr(&cmd->bdaddr, &dst);
l = g_slist_find_custom(devices, &dst, device_cmp);
- if (!l) {
- status = HAL_STATUS_FAILED;
+ if (!l)
goto failed;
- }
dev = l->data;
-
- if (dev->watch) {
- g_source_remove(dev->watch);
- dev->watch = 0;
- }
-
- bnep_if_down(dev->iface);
- bnep_conndel(&dst);
destroy_pan_device(dev);
status = HAL_STATUS_SUCCESS;
diff --git a/profiles/network/bnep.c b/profiles/network/bnep.c
index d7d8832..752e00e 100644
--- a/profiles/network/bnep.c
+++ b/profiles/network/bnep.c
@@ -72,25 +72,15 @@ struct bnep_conn {
uint16_t src;
uint16_t dst;
bdaddr_t dst_addr;
+ char iface[16];
guint attempts;
guint setup_to;
void *data;
- bnep_connect_cb conn_cb;
+ bnep_connect_cb conn_cb;
+ bnep_disconnect_cb disconn_cb;
+ int watch;
};
-static void free_bnep_connect(struct bnep_conn *bc)
-{
- if (!bc)
- return;
-
- if (bc->io) {
- g_io_channel_unref(bc->io);
- bc->io = NULL;
- }
-
- g_free(bc);
-}
-
uint16_t bnep_service_id(const char *svc)
{
int i;
@@ -268,19 +258,16 @@ struct bnep_conn *bnep_new(uint16_t src, uint16_t dst,
return bc;
}
-void bnep_free(struct bnep_conn *bc)
+static gboolean bnep_watchdog_cb(GIOChannel *chan, GIOCondition cond,
+ gpointer data)
{
- DBG("");
+ struct bnep_conn *bc = data;
- if (!bc)
- return;
+ DBG("");
- if (bc->io) {
- g_io_channel_unref(bc->io);
- bc->io = NULL;
- }
+ bc->disconn_cb(bc->data);
- g_free(bc);
+ return FALSE;
}
static gboolean bnep_setup_cb(GIOChannel *chan, GIOCondition cond,
@@ -290,12 +277,11 @@ static gboolean bnep_setup_cb(GIOChannel *chan, GIOCondition cond,
struct bnep_control_rsp *rsp;
struct timeval timeo;
char pkt[BNEP_MTU];
- char iface[16];
ssize_t r;
int sk;
if (cond & G_IO_NVAL)
- goto failed;
+ return FALSE;
if (bc->setup_to > 0) {
g_source_remove(bc->setup_to);
@@ -347,24 +333,28 @@ static gboolean bnep_setup_cb(GIOChannel *chan, GIOCondition cond,
setsockopt(sk, SOL_SOCKET, SO_RCVTIMEO, &timeo, sizeof(timeo));
sk = g_io_channel_unix_get_fd(bc->io);
- if (bnep_connadd(sk, bc->src, iface)) {
+ if (bnep_connadd(sk, bc->src, bc->iface)) {
error("bnep conn could not be added");
goto failed;
}
- if (bnep_if_up(iface)) {
- error("could not up %s", iface);
+ if (bnep_if_up(bc->iface)) {
+ error("could not up %s", bc->iface);
+ bnep_conndel(&bc->dst_addr);
goto failed;
}
- bc->conn_cb(chan, iface, 0, bc->data);
- free_bnep_connect(bc);
+ bc->watch = g_io_add_watch(bc->io, G_IO_ERR | G_IO_HUP | G_IO_NVAL,
+ bnep_watchdog_cb, bc);
+ g_io_channel_unref(bc->io);
+ bc->io = NULL;
+
+ bc->conn_cb(bc->iface, 0, bc->data);
return FALSE;
failed:
- bc->conn_cb(NULL, NULL, -EIO, bc->data);
- free_bnep_connect(bc);
+ bc->conn_cb(NULL, -EIO, bc->data);
return FALSE;
}
@@ -408,40 +398,72 @@ static gboolean bnep_conn_req_to(gpointer user_data)
return TRUE;
}
- bc->conn_cb(NULL, NULL, -ETIMEDOUT, bc->data);
- free_bnep_connect(bc);
+ bc->conn_cb(NULL, -ETIMEDOUT, bc->data);
return FALSE;
}
-int bnep_connect(int sk, uint16_t src, uint16_t dst, bnep_connect_cb conn_cb,
- void *data)
+int bnep_connect(struct bnep_conn *bc, int sk, bnep_connect_cb conn_cb,
+ bnep_disconnect_cb disconn_cb, void *data)
{
- struct bnep_conn *bc;
int err;
- if (!conn_cb)
+ if (!bc || !conn_cb || !disconn_cb)
return -EINVAL;
- bc = g_new0(struct bnep_conn, 1);
bc->io = g_io_channel_unix_new(sk);
- bc->attempts = 0;
- bc->src = src;
- bc->dst = dst;
- bc->conn_cb = conn_cb;
bc->data = data;
+ bc->conn_cb = conn_cb;
+ bc->disconn_cb = disconn_cb;
err = bnep_setup_conn_req(bc);
if (err < 0)
return err;
bc->setup_to = g_timeout_add_seconds(CON_SETUP_TO,
- bnep_conn_req_to, bc);
- g_io_add_watch(bc->io, G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
+ bnep_conn_req_to, bc);
+ bc->watch = g_io_add_watch(bc->io,
+ G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL,
bnep_setup_cb, bc);
return 0;
}
+void bnep_disconnect(struct bnep_conn *bc)
+{
+ DBG("");
+
+ if (!bc)
+ return;
+
+ if (bc->io) {
+ g_io_channel_unref(bc->io);
+ bc->io = NULL;
+ }
+
+ if (bc->watch) {
+ g_source_remove(bc->watch);
+ bc->watch = 0;
+ }
+
+ bnep_if_down(bc->iface);
+ bnep_conndel(&bc->dst_addr);
+}
+
+void bnep_free(struct bnep_conn *bc)
+{
+ DBG("");
+
+ if (!bc)
+ return;
+
+ if (bc->io) {
+ g_io_channel_unref(bc->io);
+ bc->io = NULL;
+ }
+
+ g_free(bc);
+}
+
int bnep_add_to_bridge(const char *devname, const char *bridge)
{
int ifindex;
diff --git a/profiles/network/bnep.h b/profiles/network/bnep.h
index 9c28899..51b09f1 100644
--- a/profiles/network/bnep.h
+++ b/profiles/network/bnep.h
@@ -33,6 +33,11 @@ const char *bnep_name(uint16_t id);
struct bnep_conn *bnep_new(uint16_t src, uint16_t dst,
const bdaddr_t *dst_addr);
void bnep_free(struct bnep_conn *bnep);
+typedef void (*bnep_connect_cb) (char *iface, int err, void *data);
+typedef void (*bnep_disconnect_cb) (void *data);
+int bnep_connect(struct bnep_conn *bnep, int sk, bnep_connect_cb conn_cb,
+ bnep_disconnect_cb disconn_cb, void *data);
+void bnep_disconnect(struct bnep_conn *bnep);
int bnep_connadd(int sk, uint16_t role, char *dev);
int bnep_conndel(const bdaddr_t *dst);
int bnep_if_up(const char *devname);
@@ -40,11 +45,6 @@ int bnep_if_down(const char *devname);
int bnep_add_to_bridge(const char *devname, const char *bridge);
int bnep_del_from_bridge(const char *devname, const char *bridge);
-typedef void (*bnep_connect_cb) (GIOChannel *chan, char *iface, int err,
- void *data);
-int bnep_connect(int sk, uint16_t src, uint16_t dst, bnep_connect_cb conn_cb,
- void *data);
-
ssize_t bnep_send_ctrl_rsp(int sk, uint8_t type, uint8_t ctrl, uint16_t resp);
uint16_t bnep_setup_chk(uint16_t dst_role, uint16_t src_role);
uint16_t bnep_setup_decode(struct bnep_setup_conn_req *req, uint16_t *dst,
diff --git a/profiles/network/connection.c b/profiles/network/connection.c
index fb3e1ce..c7cc99f 100644
--- a/profiles/network/connection.c
+++ b/profiles/network/connection.c
@@ -72,6 +72,7 @@ struct network_conn {
guint dc_id;
struct network_peer *peer;
DBusMessage *connect;
+ struct bnep_conn *bnep;
};
static GSList *peers = NULL;
@@ -106,8 +107,7 @@ static struct network_conn *find_connection_by_state(GSList *list,
return NULL;
}
-static gboolean bnep_watchdog_cb(GIOChannel *chan, GIOCondition cond,
- gpointer data)
+static void bnep_disconn_cb(void *data)
{
struct network_conn *nc = data;
DBusConnection *conn = btd_get_dbus_connection();
@@ -126,12 +126,18 @@ static gboolean bnep_watchdog_cb(GIOChannel *chan, GIOCondition cond,
info("%s disconnected", nc->dev);
- bnep_if_down(nc->dev);
nc->state = DISCONNECTED;
memset(nc->dev, 0, sizeof(nc->dev));
strcpy(nc->dev, "bnep%d");
- return FALSE;
+ bnep_free(nc->bnep);
+ nc->bnep = NULL;
+
+ if (nc->io) {
+ g_io_channel_shutdown(nc->io, TRUE, NULL);
+ g_io_channel_unref(nc->io);
+ nc->io = NULL;
+ }
}
static void local_connect_cb(struct network_conn *nc, int err)
@@ -158,9 +164,17 @@ static void cancel_connection(struct network_conn *nc, int err)
if (nc->connect)
local_connect_cb(nc, err);
- g_io_channel_shutdown(nc->io, TRUE, NULL);
- g_io_channel_unref(nc->io);
- nc->io = NULL;
+ if (nc->io) {
+ g_io_channel_shutdown(nc->io, TRUE, NULL);
+ g_io_channel_unref(nc->io);
+ nc->io = NULL;
+ }
+
+ if (nc->state == CONNECTED)
+ bnep_disconnect(nc->bnep);
+
+ bnep_free(nc->bnep);
+ nc->bnep = NULL;
nc->state = DISCONNECTED;
}
@@ -169,11 +183,7 @@ static void connection_destroy(DBusConnection *conn, void *user_data)
{
struct network_conn *nc = user_data;
- if (nc->state == CONNECTED) {
- bnep_if_down(nc->dev);
- bnep_conndel(device_get_address(nc->peer->device));
- } else if (nc->io)
- cancel_connection(nc, -EIO);
+ cancel_connection(nc, -EIO);
}
static void disconnect_cb(struct btd_device *device, gboolean removal,
@@ -186,7 +196,7 @@ static void disconnect_cb(struct btd_device *device, gboolean removal,
connection_destroy(NULL, user_data);
}
-static void bnep_conn_cb(GIOChannel *chan, char *iface, int err, void *data)
+static void bnep_conn_cb(char *iface, int err, void *data)
{
struct network_conn *nc = data;
const char *path;
@@ -220,11 +230,6 @@ static void bnep_conn_cb(GIOChannel *chan, char *iface, int err, void *data)
nc->state = CONNECTED;
nc->dc_id = device_add_disconnect_watch(nc->peer->device, disconnect_cb,
nc, NULL);
- g_io_add_watch(chan, G_IO_ERR | G_IO_HUP | G_IO_NVAL,
- bnep_watchdog_cb, nc);
- g_io_channel_unref(nc->io);
- nc->io = NULL;
-
return;
failed:
@@ -242,7 +247,14 @@ static void connect_cb(GIOChannel *chan, GError *err, gpointer data)
}
sk = g_io_channel_unix_get_fd(nc->io);
- perr = bnep_connect(sk, BNEP_SVC_PANU, nc->id, bnep_conn_cb, nc);
+ nc->bnep = bnep_new(BNEP_SVC_PANU, nc->id,
+ device_get_address(nc->peer->device));
+ if (!nc->bnep) {
+ error("unable to allocate memory for nc->bnep");
+ goto failed;
+ }
+
+ perr = bnep_connect(nc->bnep, sk, bnep_conn_cb, bnep_disconn_cb, nc);
if (perr < 0) {
error("bnep connect(): %s (%d)", strerror(-perr), -perr);
goto failed;
--
1.8.3.2
next prev parent reply other threads:[~2013-12-16 22:05 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-12-16 22:05 [PATCH_v2 1/6] android/pan: Rename pan_device_free to destroy_pan_device Ravi kumar Veeramally
2013-12-16 22:05 ` [PATCH_v2 2/6] android/pan: Rename connect_cb to bt_io_connect_cb Ravi kumar Veeramally
2013-12-17 9:27 ` Johan Hedberg
2013-12-17 9:38 ` Ravi kumar Veeramally
2013-12-16 22:05 ` [PATCH_v2 3/6] bnep: Add bnep_new and bnep_free api's Ravi kumar Veeramally
2013-12-17 9:19 ` Luiz Augusto von Dentz
2013-12-17 9:42 ` Ravi kumar Veeramally
2013-12-16 22:05 ` Ravi kumar Veeramally [this message]
2013-12-16 22:05 ` [PATCH_v2 5/6] bnep: Refactored bnep server apis for bridge addition and deletion Ravi kumar Veeramally
2013-12-16 22:05 ` [PATCH_v2 6/6] bnep: Refactor bnep setup response validation functionality Ravi kumar Veeramally
2013-12-17 9:09 ` [PATCH_v2 1/6] android/pan: Rename pan_device_free to destroy_pan_device Luiz Augusto von Dentz
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=1387231516-4127-4-git-send-email-ravikumar.veeramally@linux.intel.com \
--to=ravikumar.veeramally@linux.intel.com \
--cc=linux-bluetooth@vger.kernel.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;
as well as URLs for NNTP newsgroup(s).