linux-bluetooth.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Vinicius Costa Gomes <vcgomes@gmail.com>
To: linux-bluetooth@vger.kernel.org
Cc: Vinicius Costa Gomes <vcgomes@gmail.com>
Subject: [RFC BlueZ 03/13] peripheral/gatt: Fix usage of mainloop_ functions
Date: Thu,  2 Jul 2015 11:33:44 -0300	[thread overview]
Message-ID: <1435847634-22831-4-git-send-email-vcgomes@gmail.com> (raw)
In-Reply-To: <1435847634-22831-1-git-send-email-vcgomes@gmail.com>

If we want that this example is mainloop agnostic, it should use the
mainloop agnostic functions.
---
 peripheral/gatt.c | 72 +++++++++++++++++++++++++++++++++----------------------
 1 file changed, 43 insertions(+), 29 deletions(-)

diff --git a/peripheral/gatt.c b/peripheral/gatt.c
index 4c5531d..a66292f 100644
--- a/peripheral/gatt.c
+++ b/peripheral/gatt.c
@@ -25,6 +25,7 @@
 #include <config.h>
 #endif
 
+#include <errno.h>
 #include <stdio.h>
 #include <unistd.h>
 #include <stdlib.h>
@@ -34,7 +35,7 @@
 #include "lib/bluetooth.h"
 #include "lib/l2cap.h"
 #include "lib/uuid.h"
-#include "src/shared/mainloop.h"
+#include "src/shared/io.h"
 #include "src/shared/util.h"
 #include "src/shared/queue.h"
 #include "src/shared/att.h"
@@ -53,7 +54,7 @@ struct gatt_conn {
 	struct bt_gatt_client *client;
 };
 
-static int att_fd = -1;
+static struct io *att_io = NULL;
 static struct queue *conn_list = NULL;
 static struct gatt_db *gatt_db = NULL;
 static struct gatt_db *gatt_cache = NULL;
@@ -153,41 +154,43 @@ static struct gatt_conn *gatt_conn_new(int fd)
 	return conn;
 }
 
-static void att_conn_callback(int fd, uint32_t events, void *user_data)
+static bool att_conn_callback(struct io *io, void *user_data)
 {
 	struct gatt_conn *conn;
 	struct sockaddr_l2 addr;
 	socklen_t addrlen;
-	int new_fd;
+	int fd, new_fd;
 
-	if (events & (EPOLLERR | EPOLLHUP)) {
-		mainloop_remove_fd(fd);
-		return;
-	}
+	fd = io_get_fd(io);
+
+	fprintf(stderr, "att_conn_callback io %p\n", io);
 
 	memset(&addr, 0, sizeof(addr));
 	addrlen = sizeof(addr);
 
-	new_fd = accept(att_fd, (struct sockaddr *) &addr, &addrlen);
+	new_fd = accept(fd, (struct sockaddr *) &addr, &addrlen);
 	if (new_fd < 0) {
 		fprintf(stderr, "Failed to accept new ATT connection: %m\n");
-		return;
+		return true;
 	}
 
 	conn = gatt_conn_new(new_fd);
 	if (!conn) {
 		fprintf(stderr, "Failed to create GATT connection\n");
 		close(new_fd);
-		return;
+		return true;
 	}
 
 	if (!queue_push_tail(conn_list, conn)) {
 		fprintf(stderr, "Failed to add GATT connection\n");
 		gatt_conn_destroy(conn);
 		close(new_fd);
+		return true;
 	}
 
 	printf("New device connected\n");
+
+	return true;
 }
 
 static void gap_device_name_read(struct gatt_db_attribute *attrib,
@@ -243,13 +246,16 @@ static void populate_devinfo_service(struct gatt_db *db)
 void gatt_server_start(void)
 {
 	struct sockaddr_l2 addr;
+	int fd;
+
+	fprintf(stderr, "gatt_server_start\n");
 
-	if (att_fd >= 0)
+	if (att_io)
 		return;
 
-	att_fd = socket(PF_BLUETOOTH, SOCK_SEQPACKET | SOCK_CLOEXEC,
+	fd = socket(PF_BLUETOOTH, SOCK_SEQPACKET | SOCK_CLOEXEC | SOCK_NONBLOCK,
 							BTPROTO_L2CAP);
-	if (att_fd < 0) {
+	if (fd < 0) {
 		fprintf(stderr, "Failed to create ATT server socket: %m\n");
 		return;
 	}
@@ -260,24 +266,21 @@ void gatt_server_start(void)
 	memcpy(&addr.l2_bdaddr, static_addr, 6);
 	addr.l2_bdaddr_type = BDADDR_LE_RANDOM;
 
-	if (bind(att_fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
+	if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
 		fprintf(stderr, "Failed to bind ATT server socket: %m\n");
-		close(att_fd);
-		att_fd = -1;
+		close(fd);
 		return;
 	}
 
-	if (listen(att_fd, 1) < 0) {
+	if (listen(fd, 1) < 0) {
 		fprintf(stderr, "Failed to listen on ATT server socket: %m\n");
-		close(att_fd);
-		att_fd = -1;
+		close(fd);
 		return;
 	}
 
 	gatt_db = gatt_db_new();
 	if (!gatt_db) {
-		close(att_fd);
-		att_fd = -1;
+		close(fd);
 		return;
 	}
 
@@ -290,20 +293,33 @@ void gatt_server_start(void)
 	if (!conn_list) {
 		gatt_db_unref(gatt_db);
 		gatt_db = NULL;
-		close(att_fd);
-		att_fd = -1;
+		close(fd);
+		return;
+	}
+
+	att_io = io_new(fd);
+
+	fprintf(stderr, "att_io %p\n", att_io);
+
+	if (!att_io) {
+		gatt_db_unref(gatt_db);
+		gatt_db = NULL;
+		close(fd);
+		queue_destroy(conn_list, NULL);
 		return;
 	}
 
-	mainloop_add_fd(att_fd, EPOLLIN, att_conn_callback, NULL, NULL);
+	if (!io_set_read_handler(att_io, att_conn_callback, NULL, NULL))
+		fprintf(stderr, "Failed to add read handler\n");
 }
 
 void gatt_server_stop(void)
 {
-	if (att_fd < 0)
+	if (!att_io)
 		return;
 
-	mainloop_remove_fd(att_fd);
+	io_destroy(att_io);
+	att_io = NULL;
 
 	queue_destroy(conn_list, gatt_conn_destroy);
 
@@ -313,6 +329,4 @@ void gatt_server_stop(void)
 	gatt_db_unref(gatt_db);
 	gatt_db = NULL;
 
-	close(att_fd);
-	att_fd = -1;
 }
-- 
2.4.5


  parent reply	other threads:[~2015-07-02 14:33 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-07-02 14:33 [RFC BlueZ 00/13] Introducing Soletta peripheral node-type Vinicius Costa Gomes
2015-07-02 14:33 ` [RFC BlueZ 01/13] build: Add configure-time checks for soletta Vinicius Costa Gomes
2015-07-02 14:33 ` [RFC BlueZ 02/13] shared: Add a mainloop implementation using soletta Vinicius Costa Gomes
2015-07-02 14:33 ` Vinicius Costa Gomes [this message]
2015-07-02 14:33 ` [RFC BlueZ 04/13] peripheral/gatt: Add a way to external services to register services Vinicius Costa Gomes
2015-07-02 14:33 ` [RFC BlueZ 05/13] peripheral/gap: Fix missing includes Vinicius Costa Gomes
2015-07-02 14:33 ` [RFC BlueZ 06/13] peripheral/gap: Init the gatt_server Vinicius Costa Gomes
2015-07-02 14:33 ` [RFC BlueZ 07/13] peripheral: Disable support for static random addresses Vinicius Costa Gomes
2015-07-02 14:33 ` [RFC BlueZ 08/13] peripheral/gap: Set the discoverable flag in the advertising Vinicius Costa Gomes
2015-07-02 14:33 ` [RFC BlueZ 09/13] peripheral/gatt: Use LOW security level Vinicius Costa Gomes
2015-07-02 14:33 ` [RFC BlueZ 10/13] soletta/heartrate: Add a node-type for the Heartrate profile Vinicius Costa Gomes
2015-07-02 14:33 ` [RFC BlueZ 11/13] .gitignore: Ignore soletta generated files Vinicius Costa Gomes
2015-07-02 14:33 ` [RFC BlueZ 12/13] build: Add heartrate soletta node type to the build system Vinicius Costa Gomes
2015-07-02 14:33 ` [RFC BlueZ 13/13] soletta: Add a sample flow using the heartrate node Vinicius Costa Gomes
2015-07-03  8:09 ` [RFC BlueZ 00/13] Introducing Soletta peripheral node-type Luiz Augusto von Dentz
2015-07-03 17:14   ` Vinicius Costa Gomes

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=1435847634-22831-4-git-send-email-vcgomes@gmail.com \
    --to=vcgomes@gmail.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).