summaryrefslogtreecommitdiffstats
path: root/btif/src/btif_sock_thread.c
diff options
context:
space:
mode:
Diffstat (limited to 'btif/src/btif_sock_thread.c')
-rw-r--r--btif/src/btif_sock_thread.c126
1 files changed, 49 insertions, 77 deletions
diff --git a/btif/src/btif_sock_thread.c b/btif/src/btif_sock_thread.c
index 599195f..475b8de 100644
--- a/btif/src/btif_sock_thread.c
+++ b/btif/src/btif_sock_thread.c
@@ -60,19 +60,16 @@
#include "btif_sock_util.h"
#include <cutils/log.h>
-#define info(fmt, ...) ALOGI ("%s: " fmt,__FUNCTION__, ## __VA_ARGS__)
-#define debug(fmt, ...) ALOGD ("%s: " fmt,__FUNCTION__, ## __VA_ARGS__)
-#define error(fmt, ...) ALOGE ("## ERROR : %s: " fmt "##",__FUNCTION__, ## __VA_ARGS__)
-#define asrt(s) if(!(s)) ALOGE ("## %s assert %s failed at line:%d ##",__FUNCTION__, #s, __LINE__)
+#define asrt(s) if(!(s)) APPL_TRACE_ERROR3("## %s assert %s failed at line:%d ##",__FUNCTION__, #s, __LINE__)
#define print_events(events) do { \
- debug("print poll event:%x", events); \
- if (events & POLLIN) debug( " POLLIN "); \
- if (events & POLLPRI) debug( " POLLPRI "); \
- if (events & POLLOUT) debug( " POLLOUT "); \
- if (events & POLLERR) debug( " POLLERR "); \
- if (events & POLLHUP) debug( " POLLHUP "); \
- if (events & POLLNVAL) debug(" POLLNVAL "); \
- if (events & POLLRDHUP) debug(" POLLRDHUP"); \
+ APPL_TRACE_DEBUG1("print poll event:%x", events); \
+ if (events & POLLIN) APPL_TRACE_DEBUG0( " POLLIN "); \
+ if (events & POLLPRI) APPL_TRACE_DEBUG0( " POLLPRI "); \
+ if (events & POLLOUT) APPL_TRACE_DEBUG0( " POLLOUT "); \
+ if (events & POLLERR) APPL_TRACE_DEBUG0( " POLLERR "); \
+ if (events & POLLHUP) APPL_TRACE_DEBUG0( " POLLHUP "); \
+ if (events & POLLNVAL) APPL_TRACE_DEBUG0(" POLLNVAL "); \
+ if (events & POLLRDHUP) APPL_TRACE_DEBUG0(" POLLRDHUP"); \
} while(0)
#define MAX_THREAD 8
@@ -119,7 +116,7 @@ static inline void set_socket_blocking(int s, int blocking)
{
int opts;
opts = fcntl(s, F_GETFL);
- if (opts<0) error("set blocking (%s)", strerror(errno));
+ if (opts<0) APPL_TRACE_ERROR1("set blocking (%s)", strerror(errno));
if(blocking)
opts &= ~O_NONBLOCK;
else opts |= O_NONBLOCK;
@@ -129,17 +126,17 @@ static inline void set_socket_blocking(int s, int blocking)
static inline int create_server_socket(const char* name)
{
int s = socket(AF_LOCAL, SOCK_STREAM, 0);
- debug("covert name to android abstract name:%s", name);
+ APPL_TRACE_DEBUG1("covert name to android abstract name:%s", name);
if(socket_local_server_bind(s, name, ANDROID_SOCKET_NAMESPACE_ABSTRACT) >= 0)
{
if(listen(s, 5) == 0)
{
- debug("listen to local socket:%s, fd:%d", name, s);
+ APPL_TRACE_DEBUG2("listen to local socket:%s, fd:%d", name, s);
return s;
}
- else error("listen to local socket:%s, fd:%d failed, errno:%d", name, s, errno);
+ else APPL_TRACE_ERROR3("listen to local socket:%s, fd:%d failed, errno:%d", name, s, errno);
}
- else error("create local socket:%s fd:%d, failed, errno:%d", name, s, errno);
+ else APPL_TRACE_ERROR3("create local socket:%s fd:%d, failed, errno:%d", name, s, errno);
close(s);
return -1;
}
@@ -149,10 +146,10 @@ static inline int connect_server_socket(const char* name)
set_socket_blocking(s, TRUE);
if(socket_local_client_connect(s, name, ANDROID_SOCKET_NAMESPACE_ABSTRACT, SOCK_STREAM) >= 0)
{
- debug("connected to local socket:%s, fd:%d", name, s);
+ APPL_TRACE_DEBUG2("connected to local socket:%s, fd:%d", name, s);
return s;
}
- else error("connect to local socket:%s, fd:%d failed, errno:%d", name, s, errno);
+ else APPL_TRACE_ERROR3("connect to local socket:%s, fd:%d failed, errno:%d", name, s, errno);
close(s);
return -1;
}
@@ -161,7 +158,7 @@ static inline int accept_server_socket(int s)
struct sockaddr_un client_address;
socklen_t clen;
int fd = accept(s, (struct sockaddr*)&client_address, &clen);
- debug("accepted fd:%d for server fd:%d", fd, s);
+ APPL_TRACE_DEBUG2("accepted fd:%d for server fd:%d", fd, s);
return fd;
}
static inline pthread_t create_thread(void *(*start_routine)(void *), void * arg)
@@ -172,7 +169,7 @@ static inline pthread_t create_thread(void *(*start_routine)(void *), void * arg
pthread_t thread_id = -1;
if( pthread_create(&thread_id, &thread_attr, start_routine, arg)!=0 )
{
- error("pthread_create : %s", strerror(errno));
+ APPL_TRACE_ERROR1("pthread_create : %s", strerror(errno));
return -1;
}
return thread_id;
@@ -184,14 +181,14 @@ static int alloc_thread_slot()
//revserd order to save guard uninitialized access to 0 index
for(i = MAX_THREAD - 1; i >=0; i--)
{
- debug("ts[%d].used:%d", i, ts[i].used);
+ APPL_TRACE_DEBUG2("ts[%d].used:%d", i, ts[i].used);
if(!ts[i].used)
{
ts[i].used = 1;
return i;
}
}
- error("execeeded max thread count");
+ APPL_TRACE_ERROR0("execeeded max thread count");
return -1;
}
static void free_thread_slot(int h)
@@ -201,12 +198,12 @@ static void free_thread_slot(int h)
close_cmd_fd(h);
ts[h].used = 0;
}
- else error("invalid thread handle:%d", h);
+ else APPL_TRACE_ERROR1("invalid thread handle:%d", h);
}
int btsock_thread_init()
{
static int initialized;
- debug("in initialized:%d", initialized);
+ APPL_TRACE_DEBUG1("in initialized:%d", initialized);
if(!initialized)
{
initialized = 1;
@@ -231,13 +228,13 @@ int btsock_thread_create(btsock_signaled_cb callback, btsock_cmd_cb cmd_callback
lock_slot(&thread_slot_lock);
int h = alloc_thread_slot();
unlock_slot(&thread_slot_lock);
- debug("alloc_thread_slot ret:%d", h);
+ APPL_TRACE_DEBUG1("alloc_thread_slot ret:%d", h);
if(h >= 0)
{
init_poll(h);
if((ts[h].thread_id = create_thread(sock_poll_thread, (void*)h)) != -1)
{
- debug("h:%d, thread id:%d", h, ts[h].thread_id);
+ APPL_TRACE_DEBUG2("h:%d, thread id:%d", h, ts[h].thread_id);
ts[h].callback = callback;
ts[h].cmd_callback = cmd_callback;
}
@@ -256,10 +253,10 @@ static inline void init_cmd_fd(int h)
asrt(ts[h].cmd_fdr == -1 && ts[h].cmd_fdw == -1);
if(socketpair(AF_UNIX, SOCK_STREAM, 0, &ts[h].cmd_fdr) < 0)
{
- error("socketpair failed: %s", strerror(errno));
+ APPL_TRACE_ERROR1("socketpair failed: %s", strerror(errno));
return;
}
- debug("h:%d, cmd_fdr:%d, cmd_fdw:%d", h, ts[h].cmd_fdr, ts[h].cmd_fdw);
+ APPL_TRACE_DEBUG3("h:%d, cmd_fdr:%d, cmd_fdw:%d", h, ts[h].cmd_fdr, ts[h].cmd_fdw);
//add the cmd fd for read & write
add_poll(h, ts[h].cmd_fdr, 0, SOCK_THREAD_FD_RD, 0);
}
@@ -288,12 +285,12 @@ int btsock_thread_add_fd(int h, int fd, int type, int flags, uint32_t user_id)
{
if(h < 0 || h >= MAX_THREAD)
{
- error("invalid bt thread handle:%d", h);
+ APPL_TRACE_ERROR1("invalid bt thread handle:%d", h);
return FALSE;
}
if(ts[h].cmd_fdw == -1)
{
- error("cmd socket is not created. socket thread may not initialized");
+ APPL_TRACE_ERROR0("cmd socket is not created. socket thread may not initialized");
return FALSE;
}
if(flags & SOCK_THREAD_ADD_FD_SYNC)
@@ -306,26 +303,26 @@ int btsock_thread_add_fd(int h, int fd, int type, int flags, uint32_t user_id)
add_poll(h, fd, type, flags, user_id);
return TRUE;
}
- debug("SOCK_THREAD_ADD_FD_SYNC is not called in poll thread context, fallback to async");
+ APPL_TRACE_DEBUG0("THREAD_ADD_FD_SYNC is not called in poll thread, fallback to async");
}
sock_cmd_t cmd = {CMD_ADD_FD, fd, type, flags, user_id};
- debug("adding fd:%d, flags:0x%x", fd, flags);
+ APPL_TRACE_DEBUG2("adding fd:%d, flags:0x%x", fd, flags);
return send(ts[h].cmd_fdw, &cmd, sizeof(cmd), 0) == sizeof(cmd);
}
int btsock_thread_post_cmd(int h, int type, const unsigned char* data, int size, uint32_t user_id)
{
if(h < 0 || h >= MAX_THREAD)
{
- error("invalid bt thread handle:%d", h);
+ APPL_TRACE_ERROR1("invalid bt thread handle:%d", h);
return FALSE;
}
if(ts[h].cmd_fdw == -1)
{
- error("cmd socket is not created. socket thread may not initialized");
+ APPL_TRACE_ERROR0("cmd socket is not created. socket thread may not initialized");
return FALSE;
}
sock_cmd_t cmd = {CMD_USER_PRIVATE, 0, type, size, user_id};
- debug("post cmd type:%d, size:%d, h:%d, ", type, size, h);
+ APPL_TRACE_DEBUG3("post cmd type:%d, size:%d, h:%d, ", type, size, h);
sock_cmd_t* cmd_send = &cmd;
int size_send = sizeof(cmd);
if(data && size)
@@ -339,7 +336,7 @@ int btsock_thread_post_cmd(int h, int type, const unsigned char* data, int size,
}
else
{
- error("alloca failed at h:%d, cmd type:%d, size:%d", h, type, size_send);
+ APPL_TRACE_ERROR3("alloca failed at h:%d, cmd type:%d, size:%d", h, type, size_send);
return FALSE;
}
}
@@ -349,12 +346,12 @@ int btsock_thread_wakeup(int h)
{
if(h < 0 || h >= MAX_THREAD)
{
- error("invalid bt thread handle:%d", h);
+ APPL_TRACE_ERROR1("invalid bt thread handle:%d", h);
return FALSE;
}
if(ts[h].cmd_fdw == -1)
{
- error("thread handle:%d, cmd socket is not created", h);
+ APPL_TRACE_ERROR1("thread handle:%d, cmd socket is not created", h);
return FALSE;
}
sock_cmd_t cmd = {CMD_WAKEUP, 0, 0, 0, 0};
@@ -364,12 +361,12 @@ int btsock_thread_exit(int h)
{
if(h < 0 || h >= MAX_THREAD)
{
- error("invalid bt thread handle:%d", h);
+ APPL_TRACE_ERROR1("invalid bt thread handle:%d", h);
return FALSE;
}
if(ts[h].cmd_fdw == -1)
{
- error("cmd socket is not created");
+ APPL_TRACE_ERROR0("cmd socket is not created");
return FALSE;
}
sock_cmd_t cmd = {CMD_EXIT, 0, 0, 0, 0};
@@ -413,7 +410,7 @@ static inline void set_poll(poll_slot_t* ps, int fd, int type, int flags, uint32
ps->pfd.fd = fd;
ps->user_id = user_id;
if(ps->type != 0 && ps->type != type)
- error("poll socket type should not changed! type was:%d, type now:%d", ps->type, type);
+ APPL_TRACE_ERROR2("poll socket type should not changed! type was:%d, type now:%d", ps->type, type);
ps->type = type;
ps->flags = flags;
ps->pfd.events = flags2pevents(flags);
@@ -430,9 +427,6 @@ static inline void add_poll(int h, int fd, int type, int flags, uint32_t user_id
{
if(ps[i].pfd.fd == fd)
{
- debug("update fd poll, count:%d, slot:%d, fd:%d, \
- type was:%d, now:%d; flags was:%x, now:%x; user_id was:%d, now:%d",
- ts[h].poll_count, i, fd, ps[i].type, type, ps[i].flags, flags, ps[i].user_id, user_id);
asrt(ts[h].poll_count < MAX_POLL);
set_poll(&ps[i], fd, type, flags | ps[i].flags, user_id);
@@ -446,11 +440,9 @@ static inline void add_poll(int h, int fd, int type, int flags, uint32_t user_id
asrt(ts[h].poll_count < MAX_POLL);
set_poll(&ps[empty], fd, type, flags, user_id);
++ts[h].poll_count;
- debug("add new fd poll, count:%d, slot:%d, fd:%d, type:%d; flags:%x; user_id:%d",
- ts[h].poll_count, empty, fd, type, flags, user_id);
return;
}
- error("exceeded max poll slot:%d!", MAX_POLL);
+ APPL_TRACE_ERROR1("exceeded max poll slot:%d!", MAX_POLL);
}
static inline void remove_poll(int h, poll_slot_t* ps, int flags)
{
@@ -458,15 +450,12 @@ static inline void remove_poll(int h, poll_slot_t* ps, int flags)
{
//all monitored events signaled. To remove it, just clear the slot
--ts[h].poll_count;
- debug("remove poll, count:%d, fd:%d, flags:%x", ts[h].poll_count, ps->pfd.fd, ps->flags);
memset(ps, 0, sizeof(*ps));
ps->pfd.fd = -1;
}
else
{
//one read or one write monitor event signaled, removed the accordding bit
- debug("remove poll flag, count:%d, fd:%d, flags signaled:%x, poll flags was:%x, new:%x",
- ts[h].poll_count, ps->pfd.fd, flags, ps->flags, ps->flags & (~flags));
ps->flags &= ~flags;
//update the poll events mask
ps->pfd.events = flags2pevents(ps->flags);
@@ -478,37 +467,32 @@ static int process_cmd_sock(int h)
int fd = ts[h].cmd_fdr;
if(recv(fd, &cmd, sizeof(cmd), MSG_WAITALL) != sizeof(cmd))
{
- error("recv cmd errno:%d", errno);
+ APPL_TRACE_ERROR1("recv cmd errno:%d", errno);
return FALSE;
}
- debug("cmd.id:%d", cmd.id);
+ APPL_TRACE_DEBUG1("cmd.id:%d", cmd.id);
switch(cmd.id)
{
case CMD_ADD_FD:
- debug("CMD_ADD_FD, fd:%d, flags:%d", cmd.fd, cmd.flags);
add_poll(h, cmd.fd, cmd.type, cmd.flags, cmd.user_id);
break;
case CMD_WAKEUP:
- debug("CMD_WAKEUP");
break;
case CMD_USER_PRIVATE:
- debug("CMD_USER_PRIVATE");
asrt(ts[h].cmd_callback);
if(ts[h].cmd_callback)
ts[h].cmd_callback(fd, cmd.type, cmd.flags, cmd.user_id);
break;
case CMD_EXIT:
- debug("CMD_EXIT");
return FALSE;
default:
- debug("unknown cmd: %d", cmd.id);
+ APPL_TRACE_DEBUG1("unknown cmd: %d", cmd.id);
break;
}
return TRUE;
}
static void process_data_sock(int h, struct pollfd *pfds, int count)
{
- debug("in, poll count:%d ts[h].poll_count:%d", count, ts[h].poll_count);
asrt(count <= ts[h].poll_count);
int i;
for( i= 1; i < ts[h].poll_count; i++)
@@ -520,22 +504,17 @@ static void process_data_sock(int h, struct pollfd *pfds, int count)
uint32_t user_id = ts[h].ps[ps_i].user_id;
int type = ts[h].ps[ps_i].type;
int flags = 0;
- debug("signaled data fd:%d, start print poll revents[", pfds[i].fd);
print_events(pfds[i].revents);
- debug("signaled data fd:%d, end print poll revents]", pfds[i].fd);
if(IS_READ(pfds[i].revents))
{
- debug("read event signaled, fd:%d, user_id:%d", pfds[i].fd, user_id);
flags |= SOCK_THREAD_FD_RD;
}
if(IS_WRITE(pfds[i].revents))
{
- debug("write event signaled, fd:%d, user_id:%d", pfds[i].fd, user_id);
flags |= SOCK_THREAD_FD_WR;
}
if(IS_EXCEPTION(pfds[i].revents))
{
- debug("exception event signaled, fd:%d, user_id:%d", pfds[i].fd, user_id);
flags |= SOCK_THREAD_FD_EXCEPTION;
//remove the whole slot not flags
remove_poll(h, &ts[h].ps[ps_i], ts[h].ps[ps_i].flags);
@@ -546,8 +525,8 @@ static void process_data_sock(int h, struct pollfd *pfds, int count)
ts[h].callback(pfds[i].fd, type, flags, user_id);
}
}
- debug("out");
}
+
static void prepare_poll_fds(int h, struct pollfd* pfds)
{
int count = 0;
@@ -559,7 +538,7 @@ static void prepare_poll_fds(int h, struct pollfd* pfds)
{
if(ps_i >= MAX_POLL)
{
- error("exceed max poll range, ps_i:%d, MAX_POLL:%d, count:%d, ts[h].poll_count:%d",
+ APPL_TRACE_ERROR4("exceed max poll range, ps_i:%d, MAX_POLL:%d, count:%d, ts[h].poll_count:%d",
ps_i, MAX_POLL, count, ts[h].poll_count);
return;
}
@@ -581,28 +560,21 @@ static void *sock_poll_thread(void *arg)
for(;;)
{
prepare_poll_fds(h, pfds);
- debug("call poll, thread handle h:%d, cmd fd read:%d, ts[h].poll_count:%d",
- h, ts[h].cmd_fdr, ts[h].poll_count);
int ret = poll(pfds, ts[h].poll_count, -1);
if(ret == -1)
{
- error("poll ret -1, exit the thread, errno:%d, err:%s", errno, strerror(errno));
- debug("ts[%d].poll_count:%d", h, ts[h].poll_count);
+ APPL_TRACE_ERROR2("poll ret -1, exit the thread, errno:%d, err:%s", errno, strerror(errno));
break;
}
if(ret != 0)
{
- debug("select wake up, h:%d, ret:%d, ts[h].poll_count:%d", h, ret, ts[h].poll_count);
int need_process_data_fd = TRUE;
if(pfds[0].revents) //cmd fd always is the first one
{
- debug("signaled cmd_fd:%d, start print poll revents[", pfds[0].fd);
- print_events(pfds[0].revents);
- debug("signaled cmd_fd:%d, end print poll revents]", pfds[0].fd);
asrt(pfds[0].fd == ts[h].cmd_fdr);
if(!process_cmd_sock(h))
{
- debug("h:%d, process_cmd_sock return false, exit...", h);
+ APPL_TRACE_DEBUG1("h:%d, process_cmd_sock return false, exit...", h);
break;
}
if(ret == 1)
@@ -612,10 +584,10 @@ static void *sock_poll_thread(void *arg)
if(need_process_data_fd)
process_data_sock(h, pfds, ret);
}
- else debug("no data, select ret: %d", ret);
+ else {APPL_TRACE_DEBUG1("no data, select ret: %d", ret)};
}
ts[h].thread_id = -1;
- debug("socket poll thread exiting, h:%d", h);
+ APPL_TRACE_DEBUG1("socket poll thread exiting, h:%d", h);
return 0;
}