From: Andrei Emeltchenko <[email protected]>
This is initial code implementing socket HAL. OPP currently works with send/receive files. Probaly
other profiles works as well, not tested yet.
Changes:
* v9: Merged this serie and RFC, two patches merged.
* v8: Fixes Szymon's comment: added missing L2CAP Protocol ID in SDP Protocol Descriptor
List, fixes getting channel number.
* v7: Rebase and fixing missing unref. All needed SDP records for supported profiles are created.
Support listen() with specified channel like in bluedroid.
* v6: Use watch_id to remove source and general cleanup
* v5: Changed uuid_to_chan table to profile table keeping information about profile like: channel, create_record,
svc_hint and sec_level.
* v4: Changed name rfslot -> rfsock following Johan's comment and other cosmetic changes, fixed one bug in SDP
record, use NULL instead of 0 for sdp functions.
* v3: Fixed coding style with write/send between file descriptors.
* v2: Following Marcel comments changed way copying between file descriptors works, added SDP record
for OPP and now it is possible to send files through GUI. Merged one patch with structures with actual user.
* v1: Rebased and use static src address, hal_fd removed from structure and closed after sent to framework,
added connect calls and SDP parsing, added cleanup_rfcomm function, minor fixes.
* RFC Initial
TODO:
* Use sec_level / check what to do with Android supplied security flags.
* Use splice() (requires bionic change first)
For tracking rfcomm sockets I use structure rfslot which has following
fields:
- real_sock - real RFCOMM socket
- fd - fd to communicate with Android framework
create_rfslot sets hal_fd which is fd passed to Android framework with CMSG
Andrei Emeltchenko (21):
android/socket: Parse SDP response and connect
android/socket: Implement HAL connect call
android/socket: Send RFCOMM channel to framework
android/socket: Send connect signal to Android framework
android/socket: Close file descriptor after sending
android/socket: Add general service create/remove function
android/socket: Add OPP SDP record
android/socket: Add MAS uuid to profile table
android/socket: Add SPP uuid to profile table
android/socket: Add PBAP SDP record
android/socket: Add SPP SDP record
android/socket: Support listen() with supplied chan number
android/hal-sock: Print bdaddr on connect
android/socket: Refactor socket send_fd function
android/hal-utils: Fix possible NULL pointer dereference
android/socket: Use default sec_level for listen
android/socket: Add error printing possible close() failure
android/socket: Use security level for connect
android/socket: Keep server iochannel reference
android/socket: Handle Android events for server socket
android/socket: Check create_rfsock returns valid structure
android/hal-sock.c | 4 +-
android/hal-utils.c | 6 +
android/socket.c | 527 +++++++++++++++++++++++++++++++++++++++++++++++++---
3 files changed, 514 insertions(+), 23 deletions(-)
--
1.8.3.2
From: Andrei Emeltchenko <[email protected]>
---
android/socket.c | 11 ++++++++++-
1 file changed, 10 insertions(+), 1 deletion(-)
diff --git a/android/socket.c b/android/socket.c
index 0817b5c..66b240d 100644
--- a/android/socket.c
+++ b/android/socket.c
@@ -647,6 +647,12 @@ static void accept_cb(GIOChannel *io, GError *err, gpointer user_data)
sock_acc = g_io_channel_unix_get_fd(io);
rfsock_acc = create_rfsock(sock_acc, &hal_fd);
+ if (!rfsock_acc) {
+ g_io_channel_shutdown(io, TRUE, NULL);
+ g_io_channel_unref(io);
+ return;
+ }
+
connections = g_list_append(connections, rfsock_acc);
DBG("rfsock: fd %d real_sock %d chan %u sock %d",
@@ -905,8 +911,11 @@ static int handle_connect(void *buf)
DBG("");
- android2bdaddr(cmd->bdaddr, &dst);
rfsock = create_rfsock(-1, &hal_fd);
+ if (!rfsock)
+ return -1;
+
+ android2bdaddr(cmd->bdaddr, &dst);
bacpy(&rfsock->dst, &dst);
memset(&uuid, 0, sizeof(uuid));
--
1.8.3.2
From: Andrei Emeltchenko <[email protected]>
Use MEDIUM security level for connections without profile and default
sec_level for others. rfsock now has pointer to profile info.
---
android/socket.c | 14 ++++++++++++--
1 file changed, 12 insertions(+), 2 deletions(-)
diff --git a/android/socket.c b/android/socket.c
index 83e6996..e31e391 100644
--- a/android/socket.c
+++ b/android/socket.c
@@ -60,6 +60,8 @@ GList *servers = NULL;
/* Simple list of RFCOMM connected sockets */
GList *connections = NULL;
+struct profile_info;
+
struct rfcomm_sock {
int fd; /* descriptor for communication with Java framework */
int real_sock; /* real RFCOMM socket */
@@ -70,6 +72,8 @@ struct rfcomm_sock {
bdaddr_t dst;
uint32_t service_handle;
+
+ struct profile_info *profile;
};
static struct rfcomm_sock *create_rfsock(int sock, int *hal_fd)
@@ -667,7 +671,7 @@ static int handle_listen(void *buf)
return -1;
else {
chan = cmd->channel;
- sec_level = BT_IO_SEC_LOW;
+ sec_level = BT_IO_SEC_MEDIUM;
}
} else {
chan = profile->channel;
@@ -786,6 +790,7 @@ fail:
static void sdp_search_cb(sdp_list_t *recs, int err, gpointer data)
{
struct rfcomm_sock *rfsock = data;
+ BtIOSecLevel sec_level = BT_IO_SEC_MEDIUM;
GError *gerr = NULL;
sdp_list_t *list;
GIOChannel *io;
@@ -829,11 +834,14 @@ static void sdp_search_cb(sdp_list_t *recs, int err, gpointer data)
DBG("Got RFCOMM channel %d", chan);
+ if (rfsock->profile)
+ sec_level = rfsock->profile->sec_level;
+
io = bt_io_connect(connect_cb, rfsock, NULL, &gerr,
BT_IO_OPT_SOURCE_BDADDR, &adapter_addr,
BT_IO_OPT_DEST_BDADDR, &rfsock->dst,
BT_IO_OPT_CHANNEL, chan,
- BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_LOW,
+ BT_IO_OPT_SEC_LEVEL, sec_level,
BT_IO_OPT_INVALID);
if (!io) {
error("Failed connect: %s", gerr->message);
@@ -875,6 +883,8 @@ static int handle_connect(void *buf)
uuid.type = SDP_UUID128;
memcpy(&uuid.value.uuid128, cmd->uuid, sizeof(uint128_t));
+ rfsock->profile = get_profile_by_uuid(cmd->uuid);
+
if (bt_search_service(&adapter_addr, &dst, &uuid, sdp_search_cb, rfsock,
NULL) < 0) {
error("Failed to search SDP records");
--
1.8.3.2
From: Andrei Emeltchenko <[email protected]>
server io channel reference will be stored in rfsock structure
and might be deleted when Android end closes connection or when
HAL makes cleanup() call.
---
android/socket.c | 11 ++++++++---
1 file changed, 8 insertions(+), 3 deletions(-)
diff --git a/android/socket.c b/android/socket.c
index e31e391..447e0f1 100644
--- a/android/socket.c
+++ b/android/socket.c
@@ -70,6 +70,8 @@ struct rfcomm_sock {
guint rfcomm_watch;
guint stack_watch;
+ GIOChannel *srv_io;
+
bdaddr_t dst;
uint32_t service_handle;
@@ -121,6 +123,11 @@ static void cleanup_rfsock(struct rfcomm_sock *rfsock)
if (rfsock->service_handle)
bt_adapter_remove_record(rfsock->service_handle);
+ if (rfsock->srv_io) {
+ g_io_channel_shutdown(rfsock->srv_io, TRUE, NULL);
+ g_io_channel_unref(rfsock->srv_io);
+ }
+
g_free(rfsock);
}
@@ -699,9 +706,7 @@ static int handle_listen(void *buf)
rfsock->real_sock = g_io_channel_unix_get_fd(io);
servers = g_list_append(servers, rfsock);
- /* TODO: Add server watch */
- g_io_channel_set_close_on_unref(io, TRUE);
- g_io_channel_unref(io);
+ rfsock->srv_io = io;
DBG("real_sock %d fd %d hal_fd %d", rfsock->real_sock, rfsock->fd,
hal_fd);
--
1.8.3.2
From: Andrei Emeltchenko <[email protected]>
Add watch for tracking events from Android framework for server socket.
---
android/socket.c | 27 ++++++++++++++++++++++++++-
1 file changed, 26 insertions(+), 1 deletion(-)
diff --git a/android/socket.c b/android/socket.c
index 447e0f1..0817b5c 100644
--- a/android/socket.c
+++ b/android/socket.c
@@ -596,6 +596,24 @@ static bool sock_send_accept(struct rfcomm_sock *rfsock, bdaddr_t *bdaddr,
return true;
}
+static gboolean sock_server_stack_event_cb(GIOChannel *io, GIOCondition cond,
+ gpointer data)
+{
+ struct rfcomm_sock *rfsock = data;
+
+ DBG("");
+
+ if (cond & (G_IO_ERR | G_IO_HUP | G_IO_NVAL)) {
+ error("Socket error: sock %d cond %d",
+ g_io_channel_unix_get_fd(io), cond);
+ cleanup_rfsock(rfsock);
+
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
static void accept_cb(GIOChannel *io, GError *err, gpointer user_data)
{
struct rfcomm_sock *rfsock = user_data;
@@ -665,7 +683,8 @@ static int handle_listen(void *buf)
struct profile_info *profile;
struct rfcomm_sock *rfsock;
BtIOSecLevel sec_level;
- GIOChannel *io;
+ GIOChannel *io, *io_stack;
+ GIOCondition cond;
GError *err = NULL;
int hal_fd;
int chan;
@@ -708,6 +727,12 @@ static int handle_listen(void *buf)
rfsock->srv_io = io;
+ /* Handle events from Android */
+ cond = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL;
+ io_stack = g_io_channel_unix_new(rfsock->fd);
+ g_io_add_watch(io_stack, cond, sock_server_stack_event_cb, rfsock);
+ g_io_channel_unref(io_stack);
+
DBG("real_sock %d fd %d hal_fd %d", rfsock->real_sock, rfsock->fd,
hal_fd);
--
1.8.3.2
Hi Andrei,
On Mon, Nov 25, 2013, Andrei Emeltchenko wrote:
> Use low security level for connections without profile and default
> sec_level for others. rfsock now has pointer to profile info for
> outcoming connections.
> ---
> android/socket.c | 12 +++++++++++-
> 1 file changed, 11 insertions(+), 1 deletion(-)
Patches 1-17 have been applied, but I can't apply from this one onwards
since the default security level should be MEDIUM and not LOW. OPP is
the only exception that should have LOW and SAP the only exception that
should have HIGH.
Johan
From: Andrei Emeltchenko <[email protected]>
---
android/socket.c | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 72 insertions(+), 1 deletion(-)
diff --git a/android/socket.c b/android/socket.c
index c053a1e..a8f9a6b 100644
--- a/android/socket.c
+++ b/android/socket.c
@@ -261,6 +261,75 @@ static sdp_record_t *create_pbap_record(uint8_t chan, const char *svc_name)
return record;
}
+static sdp_record_t *create_spp_record(uint8_t chan, const char *svc_name)
+{
+ const char *service_name = "Serial Port";
+ sdp_list_t *svclass_id, *apseq, *profiles, *root;
+ uuid_t root_uuid, sp_uuid, l2cap, rfcomm;
+ sdp_profile_desc_t profile;
+ sdp_list_t *aproto, *proto[2];
+ sdp_data_t *channel;
+ sdp_record_t *record;
+
+ record = sdp_record_alloc();
+ if (!record)
+ return NULL;
+
+ record->handle = sdp_next_handle();
+
+ sdp_uuid16_create(&root_uuid, PUBLIC_BROWSE_GROUP);
+ root = sdp_list_append(NULL, &root_uuid);
+ sdp_set_browse_groups(record, root);
+
+ sdp_uuid16_create(&sp_uuid, SERIAL_PORT_SVCLASS_ID);
+ svclass_id = sdp_list_append(NULL, &sp_uuid);
+ sdp_set_service_classes(record, svclass_id);
+
+ sdp_uuid16_create(&profile.uuid, SERIAL_PORT_PROFILE_ID);
+ profile.version = 0x0100;
+ profiles = sdp_list_append(NULL, &profile);
+ sdp_set_profile_descs(record, profiles);
+
+ sdp_uuid16_create(&l2cap, L2CAP_UUID);
+ proto[0] = sdp_list_append(NULL, &l2cap);
+ apseq = sdp_list_append(NULL, proto[0]);
+
+ sdp_uuid16_create(&rfcomm, RFCOMM_UUID);
+ proto[1] = sdp_list_append(NULL, &rfcomm);
+ channel = sdp_data_alloc(SDP_UINT8, &chan);
+ proto[1] = sdp_list_append(proto[1], channel);
+ apseq = sdp_list_append(apseq, proto[1]);
+
+ aproto = sdp_list_append(NULL, apseq);
+ sdp_set_access_protos(record, aproto);
+
+ sdp_add_lang_attr(record);
+
+ if (svc_name)
+ service_name = svc_name;
+
+ sdp_set_info_attr(record, service_name, "BlueZ", "COM Port");
+
+ sdp_set_url_attr(record, "http://www.bluez.org/",
+ "http://www.bluez.org/", "http://www.bluez.org/");
+
+ sdp_set_service_id(record, sp_uuid);
+ sdp_set_service_ttl(record, 0xffff);
+ sdp_set_service_avail(record, 0xff);
+ sdp_set_record_state(record, 0x00001234);
+
+ sdp_data_free(channel);
+ sdp_list_free(proto[0], NULL);
+ sdp_list_free(proto[1], NULL);
+ sdp_list_free(apseq, NULL);
+ sdp_list_free(aproto, NULL);
+ sdp_list_free(root, NULL);
+ sdp_list_free(svclass_id, NULL);
+ sdp_list_free(profiles, NULL);
+
+ return record;
+}
+
static struct profile_info {
uint8_t uuid[16];
uint8_t channel;
@@ -295,7 +364,9 @@ static struct profile_info {
0x00, 0x00, 0x11, 0x01, 0x00, 0x00, 0x10, 0x00,
0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB
},
- .channel = SPP_DEFAULT_CHANNEL
+ .channel = SPP_DEFAULT_CHANNEL,
+ .svc_hint = 0,
+ .create_record = create_spp_record
},
};
--
1.8.3.2
From: Andrei Emeltchenko <[email protected]>
Parse SDP response, find RFCOMM channel and connect.
---
android/socket.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 68 insertions(+)
diff --git a/android/socket.c b/android/socket.c
index 1815367..cd958d9 100644
--- a/android/socket.c
+++ b/android/socket.c
@@ -424,9 +424,77 @@ static int handle_listen(void *buf)
return hal_fd;
}
+static void connect_cb(GIOChannel *io, GError *err, gpointer user_data)
+{
+}
+
static void sdp_search_cb(sdp_list_t *recs, int err, gpointer data)
{
+ struct rfcomm_sock *rfsock = data;
+ GError *gerr = NULL;
+ sdp_list_t *list;
+ GIOChannel *io;
+ int chan;
+
DBG("");
+
+ if (err < 0) {
+ error("Unable to get SDP record: %s", strerror(-err));
+ goto fail;
+ }
+
+ if (!recs || !recs->data) {
+ error("No SDP records found");
+ goto fail;
+ }
+
+ for (list = recs; list != NULL; list = list->next) {
+ sdp_record_t *rec = list->data;
+ sdp_list_t *protos;
+
+ if (sdp_get_access_protos(rec, &protos) < 0) {
+ error("Unable to get proto list");
+ goto fail;
+ }
+
+ chan = sdp_get_proto_port(protos, RFCOMM_UUID);
+
+ sdp_list_foreach(protos, (sdp_list_func_t) sdp_list_free,
+ NULL);
+ sdp_list_free(protos, NULL);
+
+ if (chan)
+ break;
+ }
+
+ if (chan <= 0) {
+ error("Could not get RFCOMM channel %d", chan);
+ goto fail;
+ }
+
+ DBG("Got RFCOMM channel %d", chan);
+
+ io = bt_io_connect(connect_cb, rfsock, NULL, &gerr,
+ BT_IO_OPT_SOURCE_BDADDR, &adapter_addr,
+ BT_IO_OPT_DEST_BDADDR, &rfsock->dst,
+ BT_IO_OPT_CHANNEL, chan,
+ BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_LOW,
+ BT_IO_OPT_INVALID);
+ if (!io) {
+ error("Failed connect: %s", gerr->message);
+ g_error_free(gerr);
+ goto fail;
+ }
+
+ rfsock->real_sock = g_io_channel_unix_get_fd(io);
+ rfsock->channel = chan;
+ connections = g_list_append(connections, rfsock);
+
+ g_io_channel_unref(io);
+
+ return;
+fail:
+ cleanup_rfsock(rfsock);
}
static int handle_connect(void *buf)
--
1.8.3.2
From: Andrei Emeltchenko <[email protected]>
This adds SDP record for OPP shown below:
Service Name: OBEX Object Push
Service RecHandle: 0x10002
Service Class ID List:
"OBEX Object Push" (0x1105)
Protocol Descriptor List:
"RFCOMM" (0x0003)
Channel: 9
"OBEX" (0x0008)
Profile Descriptor List:
"OBEX Object Push" (0x1105)
Version: 0x0100
---
android/socket.c | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 81 insertions(+), 1 deletion(-)
diff --git a/android/socket.c b/android/socket.c
index 740907b..64f54e4 100644
--- a/android/socket.c
+++ b/android/socket.c
@@ -48,6 +48,8 @@
#define OPP_DEFAULT_CHANNEL 9
#define PBAP_DEFAULT_CHANNEL 15
+#define SVC_HINT_OBEX 0x10
+
static bdaddr_t adapter_addr;
/* Simple list of RFCOMM server sockets */
@@ -111,6 +113,82 @@ static void cleanup_rfsock(struct rfcomm_sock *rfsock)
g_free(rfsock);
}
+static sdp_record_t *create_opp_record(uint8_t chan, const char *svc_name)
+{
+ const char *service_name = "OBEX Object Push";
+ sdp_list_t *svclass_id, *pfseq, *apseq, *root;
+ uuid_t root_uuid, opush_uuid, l2cap_uuid, rfcomm_uuid, obex_uuid;
+ sdp_profile_desc_t profile[1];
+ sdp_list_t *aproto, *proto[3];
+ uint8_t formats[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0xff };
+ void *dtds[sizeof(formats)], *values[sizeof(formats)];
+ unsigned int i;
+ uint8_t dtd = SDP_UINT8;
+ sdp_data_t *sflist;
+ sdp_data_t *channel;
+ sdp_record_t *record;
+
+ record = sdp_record_alloc();
+ if (!record)
+ return NULL;
+
+ record->handle = sdp_next_handle();
+
+ sdp_uuid16_create(&root_uuid, PUBLIC_BROWSE_GROUP);
+ root = sdp_list_append(NULL, &root_uuid);
+ sdp_set_browse_groups(record, root);
+
+ sdp_uuid16_create(&opush_uuid, OBEX_OBJPUSH_SVCLASS_ID);
+ svclass_id = sdp_list_append(NULL, &opush_uuid);
+ sdp_set_service_classes(record, svclass_id);
+
+ sdp_uuid16_create(&profile[0].uuid, OBEX_OBJPUSH_PROFILE_ID);
+ profile[0].version = 0x0100;
+ pfseq = sdp_list_append(NULL, profile);
+ sdp_set_profile_descs(record, pfseq);
+
+ sdp_uuid16_create(&l2cap_uuid, L2CAP_UUID);
+ proto[0] = sdp_list_append(NULL, &l2cap_uuid);
+ apseq = sdp_list_append(NULL, proto[0]);
+
+ sdp_uuid16_create(&rfcomm_uuid, RFCOMM_UUID);
+ proto[1] = sdp_list_append(NULL, &rfcomm_uuid);
+ channel = sdp_data_alloc(SDP_UINT8, &chan);
+ proto[1] = sdp_list_append(proto[1], channel);
+ apseq = sdp_list_append(apseq, proto[1]);
+
+ sdp_uuid16_create(&obex_uuid, OBEX_UUID);
+ proto[2] = sdp_list_append(NULL, &obex_uuid);
+ apseq = sdp_list_append(apseq, proto[2]);
+
+ aproto = sdp_list_append(NULL, apseq);
+ sdp_set_access_protos(record, aproto);
+
+ for (i = 0; i < sizeof(formats); i++) {
+ dtds[i] = &dtd;
+ values[i] = &formats[i];
+ }
+ sflist = sdp_seq_alloc(dtds, values, sizeof(formats));
+ sdp_attr_add(record, SDP_ATTR_SUPPORTED_FORMATS_LIST, sflist);
+
+ if (svc_name)
+ service_name = svc_name;
+
+ sdp_set_info_attr(record, service_name, NULL, NULL);
+
+ sdp_data_free(channel);
+ sdp_list_free(proto[0], NULL);
+ sdp_list_free(proto[1], NULL);
+ sdp_list_free(proto[2], NULL);
+ sdp_list_free(apseq, NULL);
+ sdp_list_free(pfseq, NULL);
+ sdp_list_free(aproto, NULL);
+ sdp_list_free(root, NULL);
+ sdp_list_free(svclass_id, NULL);
+
+ return record;
+}
+
static struct profile_info {
uint8_t uuid[16];
uint8_t channel;
@@ -129,7 +207,9 @@ static struct profile_info {
0x00, 0x00, 0x11, 0x05, 0x00, 0x00, 0x10, 0x00,
0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB
},
- .channel = OPP_DEFAULT_CHANNEL
+ .channel = OPP_DEFAULT_CHANNEL,
+ .svc_hint = SVC_HINT_OBEX,
+ .create_record = create_opp_record
}
};
--
1.8.3.2
From: Andrei Emeltchenko <[email protected]>
---
android/socket.c | 2 ++
1 file changed, 2 insertions(+)
diff --git a/android/socket.c b/android/socket.c
index f9a6587..9f8c535 100644
--- a/android/socket.c
+++ b/android/socket.c
@@ -607,6 +607,7 @@ void bt_sock_handle_cmd(int sk, uint8_t opcode, void *buf, uint16_t len)
break;
ipc_send(sk, HAL_SERVICE_ID_SOCK, opcode, 0, NULL, fd);
+ close(fd);
return;
case HAL_OP_SOCK_CONNECT:
fd = handle_connect(buf);
@@ -614,6 +615,7 @@ void bt_sock_handle_cmd(int sk, uint8_t opcode, void *buf, uint16_t len)
break;
ipc_send(sk, HAL_SERVICE_ID_SOCK, opcode, 0, NULL, fd);
+ close(fd);
return;
default:
DBG("Unhandled command, opcode 0x%x", opcode);
--
1.8.3.2
From: Andrei Emeltchenko <[email protected]>
---
android/socket.c | 23 +++++++++++++++++------
1 file changed, 17 insertions(+), 6 deletions(-)
diff --git a/android/socket.c b/android/socket.c
index 11358e3..db47667 100644
--- a/android/socket.c
+++ b/android/socket.c
@@ -96,10 +96,15 @@ static void cleanup_rfsock(struct rfcomm_sock *rfsock)
DBG("rfsock: %p fd %d real_sock %d chan %u",
rfsock, rfsock->fd, rfsock->real_sock, rfsock->channel);
- if (rfsock->fd > 0)
- close(rfsock->fd);
- if (rfsock->real_sock > 0)
- close(rfsock->real_sock);
+ if (rfsock->fd >= 0)
+ if (close(rfsock->fd) < 0)
+ error("close() fd %d failed: %s", rfsock->fd,
+ strerror(errno));
+
+ if (rfsock->real_sock >= 0)
+ if (close(rfsock->real_sock) < 0)
+ error("close() fd %d: failed: %s", rfsock->real_sock,
+ strerror(errno));
if (rfsock->rfcomm_watch > 0)
if (!g_source_remove(rfsock->rfcomm_watch))
@@ -891,7 +896,10 @@ void bt_sock_handle_cmd(int sk, uint8_t opcode, void *buf, uint16_t len)
break;
ipc_send(sk, HAL_SERVICE_ID_SOCK, opcode, 0, NULL, fd);
- close(fd);
+
+ if (close(fd) < 0)
+ error("close() fd %d failed: %s", fd, strerror(errno));
+
return;
case HAL_OP_SOCK_CONNECT:
fd = handle_connect(buf);
@@ -899,7 +907,10 @@ void bt_sock_handle_cmd(int sk, uint8_t opcode, void *buf, uint16_t len)
break;
ipc_send(sk, HAL_SERVICE_ID_SOCK, opcode, 0, NULL, fd);
- close(fd);
+
+ if (close(fd) < 0)
+ error("close() fd %d failed: %s", fd, strerror(errno));
+
return;
default:
DBG("Unhandled command, opcode 0x%x", opcode);
--
1.8.3.2
From: Andrei Emeltchenko <[email protected]>
Use low security level for connections without profile and default
sec_level for others. rfsock now has pointer to profile info for
outcoming connections.
---
android/socket.c | 12 +++++++++++-
1 file changed, 11 insertions(+), 1 deletion(-)
diff --git a/android/socket.c b/android/socket.c
index db47667..6f2c498 100644
--- a/android/socket.c
+++ b/android/socket.c
@@ -60,6 +60,8 @@ GList *servers = NULL;
/* Simple list of RFCOMM connected sockets */
GList *connections = NULL;
+struct profile_info;
+
struct rfcomm_sock {
int fd; /* descriptor for communication with Java framework */
int real_sock; /* real RFCOMM socket */
@@ -70,6 +72,8 @@ struct rfcomm_sock {
bdaddr_t dst;
uint32_t service_handle;
+
+ struct profile_info *profile;
};
static struct rfcomm_sock *create_rfsock(int sock, int *hal_fd)
@@ -786,6 +790,7 @@ fail:
static void sdp_search_cb(sdp_list_t *recs, int err, gpointer data)
{
struct rfcomm_sock *rfsock = data;
+ BtIOSecLevel sec_level = BT_IO_SEC_LOW;
GError *gerr = NULL;
sdp_list_t *list;
GIOChannel *io;
@@ -829,11 +834,14 @@ static void sdp_search_cb(sdp_list_t *recs, int err, gpointer data)
DBG("Got RFCOMM channel %d", chan);
+ if (rfsock->profile)
+ sec_level = rfsock->profile->sec_level;
+
io = bt_io_connect(connect_cb, rfsock, NULL, &gerr,
BT_IO_OPT_SOURCE_BDADDR, &adapter_addr,
BT_IO_OPT_DEST_BDADDR, &rfsock->dst,
BT_IO_OPT_CHANNEL, chan,
- BT_IO_OPT_SEC_LEVEL, BT_IO_SEC_LOW,
+ BT_IO_OPT_SEC_LEVEL, sec_level,
BT_IO_OPT_INVALID);
if (!io) {
error("Failed connect: %s", gerr->message);
@@ -875,6 +883,8 @@ static int handle_connect(void *buf)
uuid.type = SDP_UUID128;
memcpy(&uuid.value.uuid128, cmd->uuid, sizeof(uint128_t));
+ rfsock->profile = get_profile_by_uuid(cmd->uuid);
+
if (bt_search_service(&adapter_addr, &dst, &uuid, sdp_search_cb, rfsock,
NULL) < 0) {
error("Failed to search SDP records");
--
1.8.3.2
From: Andrei Emeltchenko <[email protected]>
Add watch for tracking events from Android framework for server socket.
---
android/socket.c | 27 ++++++++++++++++++++++++++-
1 file changed, 26 insertions(+), 1 deletion(-)
diff --git a/android/socket.c b/android/socket.c
index 93b3d48..03b6d6d 100644
--- a/android/socket.c
+++ b/android/socket.c
@@ -596,6 +596,24 @@ static bool sock_send_accept(struct rfcomm_sock *rfsock, bdaddr_t *bdaddr,
return true;
}
+static gboolean sock_server_stack_event_cb(GIOChannel *io, GIOCondition cond,
+ gpointer data)
+{
+ struct rfcomm_sock *rfsock = data;
+
+ DBG("");
+
+ if (cond & (G_IO_ERR | G_IO_HUP | G_IO_NVAL)) {
+ error("Socket error: sock %d cond %d",
+ g_io_channel_unix_get_fd(io), cond);
+ cleanup_rfsock(rfsock);
+
+ return FALSE;
+ }
+
+ return TRUE;
+}
+
static void accept_cb(GIOChannel *io, GError *err, gpointer user_data)
{
struct rfcomm_sock *rfsock = user_data;
@@ -665,7 +683,8 @@ static int handle_listen(void *buf)
struct profile_info *profile;
struct rfcomm_sock *rfsock;
BtIOSecLevel sec_level;
- GIOChannel *io;
+ GIOChannel *io, *io_stack;
+ GIOCondition cond;
GError *err = NULL;
int hal_fd;
int chan;
@@ -708,6 +727,12 @@ static int handle_listen(void *buf)
rfsock->srv_io = io;
+ /* Handle events from Android */
+ cond = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL;
+ io_stack = g_io_channel_unix_new(rfsock->fd);
+ g_io_add_watch(io_stack, cond, sock_server_stack_event_cb, rfsock);
+ g_io_channel_unref(io_stack);
+
DBG("real_sock %d fd %d hal_fd %d", rfsock->real_sock, rfsock->fd,
hal_fd);
--
1.8.3.2
From: Andrei Emeltchenko <[email protected]>
---
android/socket.c | 11 ++++++++++-
1 file changed, 10 insertions(+), 1 deletion(-)
diff --git a/android/socket.c b/android/socket.c
index 03b6d6d..fe16e49 100644
--- a/android/socket.c
+++ b/android/socket.c
@@ -647,6 +647,12 @@ static void accept_cb(GIOChannel *io, GError *err, gpointer user_data)
sock_acc = g_io_channel_unix_get_fd(io);
rfsock_acc = create_rfsock(sock_acc, &hal_fd);
+ if (!rfsock_acc) {
+ g_io_channel_shutdown(io, TRUE, NULL);
+ g_io_channel_unref(io);
+ return;
+ }
+
connections = g_list_append(connections, rfsock_acc);
DBG("rfsock: fd %d real_sock %d chan %u sock %d",
@@ -905,8 +911,11 @@ static int handle_connect(void *buf)
DBG("");
- android2bdaddr(cmd->bdaddr, &dst);
rfsock = create_rfsock(-1, &hal_fd);
+ if (!rfsock)
+ return -1;
+
+ android2bdaddr(cmd->bdaddr, &dst);
bacpy(&rfsock->dst, &dst);
memset(&uuid, 0, sizeof(uuid));
--
1.8.3.2
From: Andrei Emeltchenko <[email protected]>
Make code cleaner and initialize local cmsg buffer to zeroes.
---
android/socket.c | 9 ++++++---
1 file changed, 6 insertions(+), 3 deletions(-)
diff --git a/android/socket.c b/android/socket.c
index 9ed8ea6..0fbf0f0 100644
--- a/android/socket.c
+++ b/android/socket.c
@@ -397,7 +397,7 @@ static int bt_sock_send_fd(int sock_fd, const void *buf, int len, int send_fd)
struct msghdr msg;
struct cmsghdr *cmsg;
struct iovec iv;
- char msgbuf[CMSG_SPACE(1)];
+ char cmsgbuf[CMSG_SPACE(sizeof(int))];
DBG("len %d sock_fd %d send_fd %d", len, sock_fd, send_fd);
@@ -405,13 +405,16 @@ static int bt_sock_send_fd(int sock_fd, const void *buf, int len, int send_fd)
return -1;
memset(&msg, 0, sizeof(msg));
+ memset(cmsgbuf, 0, sizeof(cmsgbuf));
+
+ msg.msg_control = cmsgbuf;
+ msg.msg_controllen = sizeof(cmsgbuf);
- msg.msg_control = msgbuf;
- msg.msg_controllen = sizeof(msgbuf);
cmsg = CMSG_FIRSTHDR(&msg);
cmsg->cmsg_level = SOL_SOCKET;
cmsg->cmsg_type = SCM_RIGHTS;
cmsg->cmsg_len = CMSG_LEN(sizeof(send_fd));
+
memcpy(CMSG_DATA(cmsg), &send_fd, sizeof(send_fd));
iv.iov_base = (unsigned char *) buf;
--
1.8.3.2
From: Andrei Emeltchenko <[email protected]>
create_record function from profile is used to create SDP service record.
The record is removed from rfsock cleanup function.
---
android/socket.c | 33 +++++++++++++++++++++++++++++++--
1 file changed, 31 insertions(+), 2 deletions(-)
diff --git a/android/socket.c b/android/socket.c
index 9f8c535..740907b 100644
--- a/android/socket.c
+++ b/android/socket.c
@@ -35,7 +35,9 @@
#include "lib/sdp.h"
#include "lib/sdp_lib.h"
#include "src/sdp-client.h"
+#include "src/sdpd.h"
+#include "bluetooth.h"
#include "log.h"
#include "hal-msg.h"
#include "hal-ipc.h"
@@ -63,6 +65,7 @@ struct rfcomm_sock {
guint stack_watch;
bdaddr_t dst;
+ uint32_t service_handle;
};
static struct rfcomm_sock *create_rfsock(int sock, int *hal_fd)
@@ -102,6 +105,9 @@ static void cleanup_rfsock(struct rfcomm_sock *rfsock)
if (!g_source_remove(rfsock->stack_watch))
error("stack_watch source was not found");
+ if (rfsock->service_handle)
+ bt_adapter_remove_record(rfsock->service_handle);
+
g_free(rfsock);
}
@@ -110,7 +116,7 @@ static struct profile_info {
uint8_t channel;
uint8_t svc_hint;
BtIOSecLevel sec_level;
- sdp_record_t * (*create_record)(uint8_t chan);
+ sdp_record_t * (*create_record)(uint8_t chan, const char *svc_name);
} profiles[] = {
{
.uuid = {
@@ -127,6 +133,27 @@ static struct profile_info {
}
};
+static uint32_t sdp_service_register(struct profile_info *profile,
+ const void *svc_name)
+{
+ sdp_record_t *record;
+
+ if (!profile || !profile->create_record)
+ return 0;
+
+ record = profile->create_record(profile->channel, svc_name);
+ if (!record)
+ return 0;
+
+ if (bt_adapter_add_record(record, profile->svc_hint) < 0) {
+ error("Failed to register on SDP record");
+ sdp_record_free(record);
+ return 0;
+ }
+
+ return record->handle;
+}
+
static int bt_sock_send_fd(int sock_fd, const void *buf, int len, int send_fd)
{
ssize_t ret;
@@ -388,7 +415,7 @@ static int handle_listen(void *buf)
chan = profile->channel;
- DBG("rfcomm channel %d", chan);
+ DBG("rfcomm channel %d svc_name %s", chan, cmd->name);
rfsock = create_rfsock(-1, &hal_fd);
if (!rfsock)
@@ -421,6 +448,8 @@ static int handle_listen(void *buf)
return -1;
}
+ rfsock->service_handle = sdp_service_register(profile, cmd->name);
+
return hal_fd;
}
--
1.8.3.2
From: Andrei Emeltchenko <[email protected]>
---
android/socket.c | 7 +++++++
1 file changed, 7 insertions(+)
diff --git a/android/socket.c b/android/socket.c
index 64f54e4..43324d0 100644
--- a/android/socket.c
+++ b/android/socket.c
@@ -47,6 +47,7 @@
#define OPP_DEFAULT_CHANNEL 9
#define PBAP_DEFAULT_CHANNEL 15
+#define MAS_DEFAULT_CHANNEL 16
#define SVC_HINT_OBEX 0x10
@@ -210,6 +211,12 @@ static struct profile_info {
.channel = OPP_DEFAULT_CHANNEL,
.svc_hint = SVC_HINT_OBEX,
.create_record = create_opp_record
+ }, {
+ .uuid = {
+ 0x00, 0x00, 0x11, 0x32, 0x00, 0x00, 0x10, 0x00,
+ 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB
+ },
+ .channel = MAS_DEFAULT_CHANNEL
}
};
--
1.8.3.2
From: Andrei Emeltchenko <[email protected]>
---
android/hal-sock.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/android/hal-sock.c b/android/hal-sock.c
index bd88ad8..e02a49a 100644
--- a/android/hal-sock.c
+++ b/android/hal-sock.c
@@ -82,8 +82,8 @@ static bt_status_t sock_connect(const bt_bdaddr_t *bdaddr, btsock_type_t type,
return BT_STATUS_PARM_INVALID;
}
- DBG("uuid %s chan %d sock %p type %d flags 0x%02x",
- btuuid2str(uuid), chan, sock, type, flags);
+ DBG("bdaddr %s uuid %s chan %d sock %p type %d flags 0x%02x",
+ bdaddr2str(bdaddr), btuuid2str(uuid), chan, sock, type, flags);
if (type != BTSOCK_RFCOMM) {
error("Socket type %u not supported", type);
--
1.8.3.2
From: Andrei Emeltchenko <[email protected]>
server io channel reference will be stored in rfsock structure
and might be deleted when Android end closes connection or when
HAL makes cleanup() call.
---
android/socket.c | 11 ++++++++---
1 file changed, 8 insertions(+), 3 deletions(-)
diff --git a/android/socket.c b/android/socket.c
index 6f2c498..93b3d48 100644
--- a/android/socket.c
+++ b/android/socket.c
@@ -70,6 +70,8 @@ struct rfcomm_sock {
guint rfcomm_watch;
guint stack_watch;
+ GIOChannel *srv_io;
+
bdaddr_t dst;
uint32_t service_handle;
@@ -121,6 +123,11 @@ static void cleanup_rfsock(struct rfcomm_sock *rfsock)
if (rfsock->service_handle)
bt_adapter_remove_record(rfsock->service_handle);
+ if (rfsock->srv_io) {
+ g_io_channel_shutdown(rfsock->srv_io, TRUE, NULL);
+ g_io_channel_unref(rfsock->srv_io);
+ }
+
g_free(rfsock);
}
@@ -699,9 +706,7 @@ static int handle_listen(void *buf)
rfsock->real_sock = g_io_channel_unix_get_fd(io);
servers = g_list_append(servers, rfsock);
- /* TODO: Add server watch */
- g_io_channel_set_close_on_unref(io, TRUE);
- g_io_channel_unref(io);
+ rfsock->srv_io = io;
DBG("real_sock %d fd %d hal_fd %d", rfsock->real_sock, rfsock->fd,
hal_fd);
--
1.8.3.2
From: Andrei Emeltchenko <[email protected]>
This adds SDP service record like shown below:
Service Name: OBEX Phonebook Access Server
Service RecHandle: 0x10002
Service Class ID List:
"Phonebook Access - PSE" (0x112f)
Protocol Descriptor List:
"RFCOMM" (0x0003)
Channel: 15
"OBEX" (0x0008)
Profile Descriptor List:
"Phonebook Access" (0x1130)
Version: 0x0100
---
android/socket.c | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 73 insertions(+), 1 deletion(-)
diff --git a/android/socket.c b/android/socket.c
index a03c34f..c053a1e 100644
--- a/android/socket.c
+++ b/android/socket.c
@@ -191,6 +191,76 @@ static sdp_record_t *create_opp_record(uint8_t chan, const char *svc_name)
return record;
}
+static sdp_record_t *create_pbap_record(uint8_t chan, const char *svc_name)
+{
+ const char *service_name = "OBEX Phonebook Access Server";
+ sdp_list_t *svclass_id, *pfseq, *apseq, *root;
+ uuid_t root_uuid, pbap_uuid, l2cap_uuid, rfcomm_uuid, obex_uuid;
+ sdp_profile_desc_t profile[1];
+ sdp_list_t *aproto, *proto[3];
+ sdp_data_t *channel;
+ uint8_t formats[] = { 0x01 };
+ uint8_t dtd = SDP_UINT8;
+ sdp_data_t *sflist;
+ sdp_record_t *record;
+
+ record = sdp_record_alloc();
+ if (!record)
+ return NULL;
+
+ record->handle = sdp_next_handle();
+
+ sdp_uuid16_create(&root_uuid, PUBLIC_BROWSE_GROUP);
+ root = sdp_list_append(NULL, &root_uuid);
+ sdp_set_browse_groups(record, root);
+
+ sdp_uuid16_create(&pbap_uuid, PBAP_PSE_SVCLASS_ID);
+ svclass_id = sdp_list_append(NULL, &pbap_uuid);
+ sdp_set_service_classes(record, svclass_id);
+
+ sdp_uuid16_create(&profile[0].uuid, PBAP_PROFILE_ID);
+ profile[0].version = 0x0100;
+ pfseq = sdp_list_append(NULL, profile);
+ sdp_set_profile_descs(record, pfseq);
+
+ sdp_uuid16_create(&l2cap_uuid, L2CAP_UUID);
+ proto[0] = sdp_list_append(NULL, &l2cap_uuid);
+ apseq = sdp_list_append(NULL, proto[0]);
+
+ sdp_uuid16_create(&rfcomm_uuid, RFCOMM_UUID);
+ proto[1] = sdp_list_append(NULL, &rfcomm_uuid);
+ channel = sdp_data_alloc(SDP_UINT8, &chan);
+ proto[1] = sdp_list_append(proto[1], channel);
+ apseq = sdp_list_append(apseq, proto[1]);
+
+ sdp_uuid16_create(&obex_uuid, OBEX_UUID);
+ proto[2] = sdp_list_append(NULL, &obex_uuid);
+ apseq = sdp_list_append(apseq, proto[2]);
+
+ aproto = sdp_list_append(NULL, apseq);
+ sdp_set_access_protos(record, aproto);
+
+ sflist = sdp_data_alloc(dtd, formats);
+ sdp_attr_add(record, SDP_ATTR_SUPPORTED_REPOSITORIES, sflist);
+
+ if (svc_name)
+ service_name = svc_name;
+
+ sdp_set_info_attr(record, service_name, NULL, NULL);
+
+ sdp_data_free(channel);
+ sdp_list_free(proto[0], NULL);
+ sdp_list_free(proto[1], NULL);
+ sdp_list_free(proto[2], NULL);
+ sdp_list_free(apseq, NULL);
+ sdp_list_free(pfseq, NULL);
+ sdp_list_free(aproto, NULL);
+ sdp_list_free(root, NULL);
+ sdp_list_free(svclass_id, NULL);
+
+ return record;
+}
+
static struct profile_info {
uint8_t uuid[16];
uint8_t channel;
@@ -203,7 +273,9 @@ static struct profile_info {
0x00, 0x00, 0x11, 0x2F, 0x00, 0x00, 0x10, 0x00,
0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB
},
- .channel = PBAP_DEFAULT_CHANNEL
+ .channel = PBAP_DEFAULT_CHANNEL,
+ .svc_hint = SVC_HINT_OBEX,
+ .create_record = create_pbap_record
}, {
.uuid = {
0x00, 0x00, 0x11, 0x05, 0x00, 0x00, 0x10, 0x00,
--
1.8.3.2
From: Andrei Emeltchenko <[email protected]>
Set default security level low for OPP and SPP and medium for PBAP and MAS.
Default security level would be low for listening without profile.
---
android/socket.c | 18 +++++++++++++++---
1 file changed, 15 insertions(+), 3 deletions(-)
diff --git a/android/socket.c b/android/socket.c
index 0fbf0f0..11358e3 100644
--- a/android/socket.c
+++ b/android/socket.c
@@ -344,6 +344,7 @@ static struct profile_info {
},
.channel = PBAP_DEFAULT_CHANNEL,
.svc_hint = SVC_HINT_OBEX,
+ .sec_level = BT_IO_SEC_MEDIUM,
.create_record = create_pbap_record
}, {
.uuid = {
@@ -352,13 +353,17 @@ static struct profile_info {
},
.channel = OPP_DEFAULT_CHANNEL,
.svc_hint = SVC_HINT_OBEX,
+ .sec_level = BT_IO_SEC_LOW,
.create_record = create_opp_record
}, {
.uuid = {
0x00, 0x00, 0x11, 0x32, 0x00, 0x00, 0x10, 0x00,
0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB
},
- .channel = MAS_DEFAULT_CHANNEL
+ .channel = MAS_DEFAULT_CHANNEL,
+ .svc_hint = 0,
+ .sec_level = BT_IO_SEC_MEDIUM,
+ .create_record = NULL
}, {
.uuid = {
0x00, 0x00, 0x11, 0x01, 0x00, 0x00, 0x10, 0x00,
@@ -366,6 +371,7 @@ static struct profile_info {
},
.channel = SPP_DEFAULT_CHANNEL,
.svc_hint = 0,
+ .sec_level = BT_IO_SEC_LOW,
.create_record = create_spp_record
},
};
@@ -642,6 +648,7 @@ static int handle_listen(void *buf)
struct hal_cmd_sock_listen *cmd = buf;
struct profile_info *profile;
struct rfcomm_sock *rfsock;
+ BtIOSecLevel sec_level;
GIOChannel *io;
GError *err = NULL;
int hal_fd;
@@ -653,10 +660,14 @@ static int handle_listen(void *buf)
if (!profile) {
if (!cmd->channel)
return -1;
- else
+ else {
chan = cmd->channel;
- } else
+ sec_level = BT_IO_SEC_LOW;
+ }
+ } else {
chan = profile->channel;
+ sec_level = profile->sec_level;
+ }
DBG("rfcomm channel %d svc_name %s", chan, cmd->name);
@@ -667,6 +678,7 @@ static int handle_listen(void *buf)
io = bt_io_listen(accept_cb, NULL, rfsock, NULL, &err,
BT_IO_OPT_SOURCE_BDADDR, &adapter_addr,
BT_IO_OPT_CHANNEL, chan,
+ BT_IO_OPT_SEC_LEVEL, sec_level,
BT_IO_OPT_INVALID);
if (!io) {
error("Failed listen: %s", err->message);
--
1.8.3.2
From: Andrei Emeltchenko <[email protected]>
No profile is assigned in this case. There is a possibility to use
Serial Port Profile.
---
android/socket.c | 11 +++++++----
1 file changed, 7 insertions(+), 4 deletions(-)
diff --git a/android/socket.c b/android/socket.c
index a8f9a6b..9ed8ea6 100644
--- a/android/socket.c
+++ b/android/socket.c
@@ -647,10 +647,13 @@ static int handle_listen(void *buf)
DBG("");
profile = get_profile_by_uuid(cmd->uuid);
- if (!profile)
- return -1;
-
- chan = profile->channel;
+ if (!profile) {
+ if (!cmd->channel)
+ return -1;
+ else
+ chan = cmd->channel;
+ } else
+ chan = profile->channel;
DBG("rfcomm channel %d svc_name %s", chan, cmd->name);
--
1.8.3.2
From: Andrei Emeltchenko <[email protected]>
---
android/hal-utils.c | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/android/hal-utils.c b/android/hal-utils.c
index 4f44d98..e3c0c60 100644
--- a/android/hal-utils.c
+++ b/android/hal-utils.c
@@ -33,6 +33,9 @@ const char *bt_uuid_t2str(const uint8_t *uuid, char *buf)
unsigned int i;
int is_bt;
+ if (!uuid)
+ return strcpy(buf, "NULL");
+
is_bt = !memcmp(&uuid[4], &BT_BASE_UUID[4], HAL_UUID_LEN - 4);
for (i = 0; i < HAL_UUID_LEN; i++) {
@@ -167,6 +170,9 @@ const char *bt_bdaddr_t2str(const bt_bdaddr_t *bd_addr, char *buf)
{
const uint8_t *p = bd_addr->address;
+ if (!bd_addr)
+ return strcpy(buf, "NULL");
+
snprintf(buf, MAX_ADDR_STR_LEN, "%02x:%02x:%02x:%02x:%02x:%02x",
p[0], p[1], p[2], p[3], p[4], p[5]);
--
1.8.3.2
From: Andrei Emeltchenko <[email protected]>
---
android/socket.c | 9 ++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)
diff --git a/android/socket.c b/android/socket.c
index 43324d0..a03c34f 100644
--- a/android/socket.c
+++ b/android/socket.c
@@ -45,6 +45,7 @@
#include "utils.h"
#include "socket.h"
+#define SPP_DEFAULT_CHANNEL 3
#define OPP_DEFAULT_CHANNEL 9
#define PBAP_DEFAULT_CHANNEL 15
#define MAS_DEFAULT_CHANNEL 16
@@ -217,7 +218,13 @@ static struct profile_info {
0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB
},
.channel = MAS_DEFAULT_CHANNEL
- }
+ }, {
+ .uuid = {
+ 0x00, 0x00, 0x11, 0x01, 0x00, 0x00, 0x10, 0x00,
+ 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB
+ },
+ .channel = SPP_DEFAULT_CHANNEL
+ },
};
static uint32_t sdp_service_register(struct profile_info *profile,
--
1.8.3.2
From: Andrei Emeltchenko <[email protected]>
Android framework expects connect signal to be sent when
remote device is connected.
---
android/socket.c | 30 ++++++++++++++++++++++++++++++
1 file changed, 30 insertions(+)
diff --git a/android/socket.c b/android/socket.c
index 4722cf8..f9a6587 100644
--- a/android/socket.c
+++ b/android/socket.c
@@ -424,6 +424,33 @@ static int handle_listen(void *buf)
return hal_fd;
}
+static bool sock_send_connect(struct rfcomm_sock *rfsock, bdaddr_t *bdaddr)
+{
+ struct hal_sock_connect_signal cmd;
+ int len;
+
+ DBG("");
+
+ memset(&cmd, 0, sizeof(cmd));
+ cmd.size = sizeof(cmd);
+ bdaddr2android(bdaddr, cmd.bdaddr);
+ cmd.channel = rfsock->channel;
+ cmd.status = 0;
+
+ len = write(rfsock->fd, &cmd, sizeof(cmd));
+ if (len < 0) {
+ error("%s", strerror(errno));
+ return false;
+ }
+
+ if (len != sizeof(cmd)) {
+ error("Error sending connect signal");
+ return false;
+ }
+
+ return true;
+}
+
static void connect_cb(GIOChannel *io, GError *err, gpointer user_data)
{
struct rfcomm_sock *rfsock = user_data;
@@ -445,6 +472,9 @@ static void connect_cb(GIOChannel *io, GError *err, gpointer user_data)
rfsock->fd, rfsock->real_sock, rfsock->channel,
g_io_channel_unix_get_fd(io));
+ if (!sock_send_connect(rfsock, dst))
+ goto fail;
+
/* Handle events from Android */
cond = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL;
io_stack = g_io_channel_unix_new(rfsock->fd);
--
1.8.3.2
From: Andrei Emeltchenko <[email protected]>
Framework expects channel to be send.
---
android/socket.c | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/android/socket.c b/android/socket.c
index 00231a3..4722cf8 100644
--- a/android/socket.c
+++ b/android/socket.c
@@ -522,6 +522,11 @@ static void sdp_search_cb(sdp_list_t *recs, int err, gpointer data)
goto fail;
}
+ if (write(rfsock->fd, &chan, sizeof(chan)) != sizeof(chan)) {
+ error("Error sending RFCOMM channel");
+ goto fail;
+ }
+
rfsock->real_sock = g_io_channel_unix_get_fd(io);
rfsock->channel = chan;
connections = g_list_append(connections, rfsock);
--
1.8.3.2
From: Andrei Emeltchenko <[email protected]>
HAL connect uses similar event handlers like listen call.
---
android/socket.c | 36 ++++++++++++++++++++++++++++++++++++
1 file changed, 36 insertions(+)
diff --git a/android/socket.c b/android/socket.c
index cd958d9..00231a3 100644
--- a/android/socket.c
+++ b/android/socket.c
@@ -426,6 +426,42 @@ static int handle_listen(void *buf)
static void connect_cb(GIOChannel *io, GError *err, gpointer user_data)
{
+ struct rfcomm_sock *rfsock = user_data;
+ bdaddr_t *dst = &rfsock->dst;
+ GIOChannel *io_stack;
+ char address[18];
+ guint id;
+ GIOCondition cond;
+
+ if (err) {
+ error("%s", err->message);
+ goto fail;
+ }
+
+ ba2str(dst, address);
+ DBG("Connected to %s", address);
+
+ DBG("rfsock: fd %d real_sock %d chan %u sock %d",
+ rfsock->fd, rfsock->real_sock, rfsock->channel,
+ g_io_channel_unix_get_fd(io));
+
+ /* Handle events from Android */
+ cond = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL;
+ io_stack = g_io_channel_unix_new(rfsock->fd);
+ id = g_io_add_watch(io_stack, cond, sock_stack_event_cb, rfsock);
+ g_io_channel_unref(io_stack);
+
+ rfsock->stack_watch = id;
+
+ /* Handle rfcomm events */
+ cond = G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL;
+ id = g_io_add_watch(io, cond, sock_rfcomm_event_cb, rfsock);
+
+ rfsock->rfcomm_watch = id;
+
+ return;
+fail:
+ cleanup_rfsock(rfsock);
}
static void sdp_search_cb(sdp_list_t *recs, int err, gpointer data)
--
1.8.3.2