Fix socket and tcp api.

Change-Id: I256eefee0c874377e66b334958b353c1ee66c2c5
diff --git a/mbtk/include/mbtk/mbtk_sock2.h b/mbtk/include/mbtk/mbtk_sock2.h
index 42224cd..3dfaad1 100755
--- a/mbtk/include/mbtk/mbtk_sock2.h
+++ b/mbtk/include/mbtk/mbtk_sock2.h
@@ -180,6 +180,11 @@
             int *mbtk_errno,
             int *read_line_count,
             char *buf_ptr);
+/*
+* Get TCP RECV buffer data length.
+*/
+int mbtk_sock_tcp_recv_len_get(mbtk_sock_handle handle,mbtk_sock_session session);
+
 
 #ifdef __cplusplus
 } // extern "C"
diff --git a/mbtk/include/mbtk/mbtk_tcpip.h b/mbtk/include/mbtk/mbtk_tcpip.h
index a97dad1..55e7b31 100755
--- a/mbtk/include/mbtk/mbtk_tcpip.h
+++ b/mbtk/include/mbtk/mbtk_tcpip.h
@@ -56,6 +56,13 @@
     mbtk_tcpip_read_callback_func read_cb;
 } mbtk_tcpip_info_t;
 
+typedef struct {
+    int link_id;
+    int sock_fd;
+    int state;
+    int recv_data_len;
+} mbtk_tcpip_tcp_state_info_s;
+
 mbtk_tcpip_err_enum mbtk_tcpip_net_open(mbtk_tcpip_net_callback_func net_cb, mbtk_tcpip_sock_callback_func sock_cb);
 mbtk_tcpip_err_enum mbtk_tcpip_net_close();
 mbtk_tcpip_err_enum mbtk_tcpip_sock_open(const mbtk_tcpip_info_t *tcpip_info);
@@ -79,5 +86,10 @@
 */
 int mbtk_tcpip_link_state_get(int link_id);
 
+/*
+* Get TCP state informations.
+*/
+int mbtk_tcpip_info_get(int link_id, mbtk_tcpip_tcp_state_info_s *state_info);
+
 
 #endif /* _MBTK_TCPIP_H */
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;
+}
+
diff --git a/mbtk/test/mbtk_tcpip_test.c b/mbtk/test/mbtk_tcpip_test.c
index 5bd39f1..d30d706 100755
--- a/mbtk/test/mbtk_tcpip_test.c
+++ b/mbtk/test/mbtk_tcpip_test.c
@@ -38,10 +38,11 @@
     printf("link_open <link_id> <ser_addr> <ser_port> <loc_port> <TCP/UDP> <CLI/SER> <ack> <ssl> <ignore_cert> <heartbeat_time> <delay_time> <read_cb>: Open link.\n");
     printf("link_close <link_id>: Close link.\n");
     printf("send <link_id> <data>:Send data.\n");
-    printf("recv <link_id>:Recv data.\n");
+    printf("recv <link_id> <len>:Recv data.\n");
     printf("traffic_reset <link_id>:Traffic reset.\n");
     printf("traffic_get <link_id>:Traffic get.\n");
     printf("state_get <link_id>:Link state get.\n");
+    printf("tcp_info <link_id>:TCP information state get.\n");
     printf("\n************************************************************************\n");
 }
 
@@ -101,6 +102,14 @@
     }
 }
 
+void tcpip_print_tcp_info(mbtk_tcpip_tcp_state_info_s *info)
+{
+    printf("Link - %d\n", info->link_id);
+    printf("fd - %d\n", info->sock_fd);
+    printf("State - %d\n", info->state);
+    printf("Recv data length - %d\n", info->recv_data_len);
+}
+
 void tcpip_read_cb(int link_id, const char* data, int data_len)
 {
     printf("\nRECV(%d-%d):%s\n", link_id, data_len, data);
@@ -227,12 +236,13 @@
                 } else {
                     printf("ARG error.\n");
                 }
-            } else if(!strncasecmp(cmd, "recv", 4)){ // recv <link_id>
+            } else if(!strncasecmp(cmd, "recv", 4)){ // recv <link_id> <len>
                 int link_id;
-                int count = sscanf(cmd, "recv %d", &link_id);
-                if(count == 1) {
+                int read_size;
+                int count = sscanf(cmd, "recv %d %d", &link_id, &read_size);
+                if(count == 2 && read_size > 0 && read_size <= 2048) {
                     char buff[2048] = {0};
-                    int len = mbtk_tcpip_read(link_id, buff, 2048);
+                    int len = mbtk_tcpip_read(link_id, buff, read_size);
                     if(len > 0) {
                         printf("RECV[%d]:%s\n", len, buff);
                     } else {
@@ -283,6 +293,20 @@
                     printf("ARG error.\n");
                 }
             }
+            else if(!strncasecmp(cmd, "tcp_info", 8)){ // tcp_info <link_id>
+                int link_id;
+                int count = sscanf(cmd, "tcp_info %d", &link_id);
+                if(count == 1) {
+                    mbtk_tcpip_tcp_state_info_s tcp_info;
+                    if(mbtk_tcpip_info_get(link_id, &tcp_info)) {
+                        printf("mbtk_tcpip_info_get() fail.\n");
+                    } else {
+                        tcpip_print_tcp_info(&tcp_info);
+                    }
+                } else {
+                    printf("ARG error.\n");
+                }
+            }
             else if(!strcasecmp(cmd, "h") || !strcasecmp(cmd, "help")) {
                 help();
             } else if(!strcasecmp(cmd, "q")) {