Fix socket and tcp api.

Change-Id: I256eefee0c874377e66b334958b353c1ee66c2c5
diff --git a/mbtk/mbtk_lib/src/mbtk_log.c b/mbtk/mbtk_lib/src/mbtk_log.c
index 1606552..1631086 100755
--- a/mbtk/mbtk_lib/src/mbtk_log.c
+++ b/mbtk/mbtk_lib/src/mbtk_log.c
@@ -25,6 +25,8 @@
 static FILE* logfile = NULL;
 static int signal_fd = -1;
 
+static bool log_level_printed = FALSE;
+
 /**
  * @brief      mbtk_log_init
  *
@@ -84,6 +86,11 @@
         syslog(level, "%s", buf);
     } else if (2 == syslog_radio_enable) {
         __android_log_printf(LOG_ID_RADIO, level, "%s", buf);
+
+        if(!log_level_printed) {
+            __android_log_printf(LOG_ID_RADIO, LOG_ERR_LEVEL, "sloglevel = %d", get_service_log_level());
+            log_level_printed = TRUE;
+        }
     } else if (-1 != tlog_fd) {
         char tmp[50] = {0};
         gettimeofday(&log_time, NULL);
diff --git a/mbtk/mbtk_lib/src/mbtk_sock2.c b/mbtk/mbtk_lib/src/mbtk_sock2.c
index a843d6b..bb1ddb8 100755
--- a/mbtk/mbtk_lib/src/mbtk_sock2.c
+++ b/mbtk/mbtk_lib/src/mbtk_sock2.c
@@ -19,6 +19,7 @@
 #include <polarssl/error.h>
 #include <polarssl/debug.h>
 #include <polarssl/config.h>
+#include <sys/ioctl.h>
 
 #ifdef LOG_TAG
 #undef LOG_TAG
@@ -132,7 +133,7 @@
         nready = epoll_wait(epoll_fd,epoll_events,20,-1);
         int i;
         for(i=0;i<nready;++i) {
-            LOGD("fd[%d] event = %x",epoll_events[i].data.fd,epoll_events[i].events);
+            LOGV("fd[%d] event = %x",epoll_events[i].data.fd,epoll_events[i].events);
             if(pipe_fds[1] == epoll_events[i].data.fd) {
                 LOGD("Get exist sig.");
                 sock_thread_running = FALSE;
@@ -1256,7 +1257,11 @@
             if(len > 0) {
                 read_count += len;
             } else {
-                break;
+                if(errno == EWOULDBLOCK) { // No data
+                    break;
+                } else {
+                    LOGE("Will retry : len = %d, errno = %d", len, errno);
+                }
             }
         }
     } else if(mbtk_sock[handle]->infos[index].type == MBTK_SOCK_UDP) {
@@ -1273,7 +1278,11 @@
             if(len > 0) {
                 read_count += len;
             } else {
-                break;
+                if(errno == EWOULDBLOCK) { // No data
+                    break;
+                } else {
+                    LOGE("Will retry : len = %d, errno = %d", len, errno);
+                }
             }
         }
     } else {
@@ -1281,7 +1290,7 @@
         return -1;
     }
 
-    LOGV("Read data[%d/%d].",len,buf_len);
+    LOGV("Read data[%d/%d].",read_count,buf_len);
 
     return read_count;
 }
@@ -1367,6 +1376,7 @@
     return len;
 }
 
+
 extern int mbtk_sock_close(mbtk_sock_handle handle,mbtk_sock_session session,
             unsigned int timeout,
             int *mbtk_errno)
@@ -1482,4 +1492,52 @@
     return MBTK_SOCK_SUCCESS;
 }
 
+/*
+* Get TCP RECV buffer data length.
+*/
+int mbtk_sock_tcp_recv_len_get(mbtk_sock_handle handle,mbtk_sock_session session)
+{
+    if(handle < 0 || handle >= MBTK_HANDLE_MAX_NUM
+        || session < 0 || mbtk_sock[handle] == NULL) {
+        LOGE("Socket not inited.");
+        return -1;
+    }
+
+    mbtk_sock_inter_info_s *inter_info = NULL;
+    int index = 0;
+    while(index < MBTK_SOCK_MAX_NUM) {
+        if(session ==
+            mbtk_sock[handle]->inter_infos[index].fd) {
+            inter_info = &(mbtk_sock[handle]->inter_infos[index]);
+            break;
+        }
+        index++;
+    }
+    if(!sock_info_check(handle,inter_info)) {
+        LOGE("sock_info_check() fail.");
+        return -1;
+    }
+
+    index = sock_info_find_by_fd(handle,inter_info->fd);
+    if(index < 0) {
+        LOGE("No such socket in session list.");
+        return -1;
+    }
+
+    unsigned int count = 0;
+    int len = 0;
+    if(mbtk_sock[handle]->infos[index].type == MBTK_SOCK_TCP) {
+        if(ioctl(inter_info->fd, FIONREAD, &len))
+        {
+            LOGE("Get ioctl FIONREAD fail:%d", errno);
+            return -1;
+        }
+    } else {
+        LOGE("Only surrport for TCP.");
+        return -1;
+    }
+
+    return len;
+}
+
 
diff --git a/mbtk/mbtk_lib/src/mbtk_tcpip_at.c b/mbtk/mbtk_lib/src/mbtk_tcpip_at.c
index 2ecef9d..8c1003a 100755
--- a/mbtk/mbtk_lib/src/mbtk_tcpip_at.c
+++ b/mbtk/mbtk_lib/src/mbtk_tcpip_at.c
@@ -182,7 +182,7 @@
             int sock_error = 0;
             socklen_t socklen = sizeof(sock_error);
             if(getsockopt(sock_info->sock_fd, SOL_SOCKET, SO_ERROR, &sock_error, (socklen_t*)&socklen) == 0) {
-                LOGD("Socket error:%d", sock_error);
+                LOGV("Socket error:%d", sock_error);
             }
 
             if(sock_error) {
@@ -194,9 +194,9 @@
                 struct tcp_info info;
                 int len = sizeof(info);
                 if(getsockopt(sock_info->sock_fd, IPPROTO_TCP, TCP_INFO, &info, (socklen_t*)&len) == 0) {
-                    LOGD("State : %d", info.tcpi_state);
+                    LOGV("State : %d", info.tcpi_state);
 #if TCPIP_DEBUG
-                    tcp_info_print(&info);
+                    //tcp_info_print(&info);
 #endif
                 }
 
@@ -217,7 +217,7 @@
         }
 
         if(sock_info->event & EPOLLIN) { // READ
-            LOGD("fd[%d] can read.", sock_info->sock_fd);
+            LOGV("fd[%d] can read.", sock_info->sock_fd);
             int link_id = tcpip_fd_2_link(sock_info->sock_fd);
             if(link_id >= 0) {
                 if(tcpip_link[link_id].type == MBTK_TCPIP_TYPE_CLIENT) {
@@ -497,9 +497,8 @@
             LOGE("Set read_cb function,can not manual read.");
             return -1;
         }
-        int err;
-        int len = mbtk_sock_read(tcpip_handle, tcpip_link[link_id].tcpip_info.cli_info.sock_fd,
-                    buff, buff_size, 3000, &err);
+        int len = mbtk_sock_read_async(tcpip_handle, tcpip_link[link_id].tcpip_info.cli_info.sock_fd,
+                    buff, buff_size);
         if(len > 0) {
             tcpip_link[link_id].tcpip_info.cli_info.data_traffic_recv += len;
         }
@@ -564,3 +563,78 @@
     return tcpip_link[link_id].link_connected ? 1 : 0;
 }
 
+/*
+* Get TCP state informations.
+*/
+int mbtk_tcpip_info_get(int link_id, mbtk_tcpip_tcp_state_info_s *state_info)
+{
+    if(!tcpip_link_check(link_id)) {
+        LOGE("Link error.");
+        return -1;
+    }
+
+    if(state_info == NULL) {
+        LOGE("ARG error.");
+        return -1;
+    }
+
+    memset(state_info, 0x0, sizeof(mbtk_tcpip_tcp_state_info_s));
+
+    struct tcp_info info;
+    memset(&info, 0x0, sizeof(struct tcp_info));
+    int len = sizeof(info);
+    if(tcpip_link[link_id].prot_type == MBTK_SOCK_TCP) {
+        if(getsockopt(tcpip_link[link_id].tcpip_info.cli_info.sock_fd, IPPROTO_TCP, TCP_INFO, &info, (socklen_t*)&len)) {
+            LOGE("Get TCP_INFO fail:%d", errno);
+            return -1;
+        }
+
+#if TCPIP_DEBUG
+        tcp_info_print(&info);
+#endif
+        state_info->state = info.tcpi_state;
+    } else {
+        state_info->state = 0;
+    }
+
+#if TCPIP_DEBUG
+    int rcvbuf_size;
+    len = sizeof(rcvbuf_size);
+    if(getsockopt(tcpip_link[link_id].tcpip_info.cli_info.sock_fd, SOL_SOCKET, SO_RCVBUF, &rcvbuf_size, (socklen_t*)&len)) {
+        LOGE("Get SO_RCVBUF fail:%d", errno);
+        return -1;
+    }
+    LOGD("SO_RCVBUF = %d", rcvbuf_size);
+
+    int sndbuf_size;
+    len = sizeof(sndbuf_size);
+    if(getsockopt(tcpip_link[link_id].tcpip_info.cli_info.sock_fd, SOL_SOCKET, SO_SNDBUF, &sndbuf_size, (socklen_t*)&len)) {
+        LOGE("Get SO_SNDBUF fail:%d", errno);
+        return -1;
+    }
+    LOGD("SO_SNDBUF = %d", sndbuf_size);
+
+    int rcvlowat_size;
+    len = sizeof(rcvlowat_size);
+    if(getsockopt(tcpip_link[link_id].tcpip_info.cli_info.sock_fd, SOL_SOCKET, SO_RCVLOWAT, &rcvlowat_size, (socklen_t*)&len)) {
+        LOGE("Get SO_RCVLOWAT fail:%d", errno);
+        return -1;
+    }
+    LOGD("SO_RCVLOWAT = %d", rcvlowat_size);
+
+    int sndlowat_size;
+    len = sizeof(sndlowat_size);
+    if(getsockopt(tcpip_link[link_id].tcpip_info.cli_info.sock_fd, SOL_SOCKET, SO_SNDLOWAT, &sndlowat_size, (socklen_t*)&len)) {
+        LOGE("Get SO_SNDLOWAT fail:%d", errno);
+        return -1;
+    }
+    LOGD("SO_SNDLOWAT = %d", sndlowat_size);
+#endif
+
+    state_info->link_id = link_id;
+    state_info->sock_fd = tcpip_link[link_id].tcpip_info.cli_info.sock_fd;
+    state_info->recv_data_len = mbtk_sock_tcp_recv_len_get(tcpip_handle, tcpip_link[link_id].tcpip_info.cli_info.sock_fd);
+
+    return 0;
+}
+