Fix TCP/IP udp support.
Change-Id: I819c54e13b5198d11b9e526e8eb2ca8bccc069f8
diff --git a/mbtk/mbtk_lib/inc/mbtk_sock_internal.h b/mbtk/mbtk_lib/inc/mbtk_sock_internal.h
old mode 100644
new mode 100755
index 9e2ec2e..e0cbaa0
--- a/mbtk/mbtk_lib/inc/mbtk_sock_internal.h
+++ b/mbtk/mbtk_lib/inc/mbtk_sock_internal.h
@@ -8,6 +8,7 @@
typedef struct {
int fd;
+ mbtk_sock_type type; // socket type:TCP or UDP
entropy_context* entropy;
ctr_drbg_context* ctr_drbg;
ssl_context *ssl;
diff --git a/mbtk/mbtk_lib/src/mbtk_sock2.c b/mbtk/mbtk_lib/src/mbtk_sock2.c
index f9f6b6b..a843d6b 100755
--- a/mbtk/mbtk_lib/src/mbtk_sock2.c
+++ b/mbtk/mbtk_lib/src/mbtk_sock2.c
@@ -145,13 +145,17 @@
int index = sock_info_find_by_fd(handle,epoll_events[i].data.fd);
if(index >= 0 && mbtk_sock[handle]->init_info.sock_cb != NULL) {
mbtk_sock_inter_info_s *inter_info = &(mbtk_sock[handle]->inter_infos[index]);
+ mbtk_sock_info *info = &(mbtk_sock[handle]->infos[index]);
//if(sock_is_close(epoll_events[i].data.fd)) {
// LOGE("Socket %d is closed.",epoll_events[i].data.fd);
// break;
//}
-
- mbtk_sock[handle]->init_info.sock_cb(handle,inter_info->fd,epoll_events[i].events);
+ mbtk_sock_cb_info_s sock_info;
+ sock_info.sock_fd = inter_info->fd;
+ sock_info.event = epoll_events[i].events;
+ sock_info.sock_type = info->type;
+ mbtk_sock[handle]->init_info.sock_cb(handle, &sock_info);
if(epoll_events[i].events & EPOLLERR){ // error[ UDP server can't use.]
LOGD("%d EPOLLERR.",epoll_events[i].data.fd);
@@ -208,8 +212,11 @@
while(handle < MBTK_HANDLE_MAX_NUM) {
if(mbtk_sock[handle] != NULL) {
if(mbtk_sock[handle]->init_info.net_cb != NULL) {
- mbtk_sock[handle]->init_info.net_cb(handle, (addr_info->type == MBTK_NET_ADDR_CHANGE_TYPE_ADD) ? 1 : 0,
- addr_info->addr, addr_info->if_name);
+ mbtk_net_cb_info_s net_info;
+ net_info.state = (addr_info->type == MBTK_NET_ADDR_CHANGE_TYPE_ADD) ? 1 : 0;
+ net_info.addr = addr_info->addr;
+ net_info.if_name = addr_info->if_name;
+ mbtk_sock[handle]->init_info.net_cb(handle, &net_info);
}
}
diff --git a/mbtk/mbtk_lib/src/mbtk_tcpip_at.c b/mbtk/mbtk_lib/src/mbtk_tcpip_at.c
index 76dc526..2ecef9d 100755
--- a/mbtk/mbtk_lib/src/mbtk_tcpip_at.c
+++ b/mbtk/mbtk_lib/src/mbtk_tcpip_at.c
@@ -175,13 +175,13 @@
}
}
-static void tcpip_sock_cb_func(int handle, int fd, int event)
+static void tcpip_sock_cb_func(int handle, mbtk_sock_cb_info_s *sock_info)
{
if(tcpip_inited && tcpip_handle == handle/* && http_fd == fd*/) {
- if(event & (EPOLLIN | EPOLLOUT)) { // Cand read or write.
+ if(sock_info->event & (EPOLLIN | EPOLLOUT)) { // Cand read or write.
int sock_error = 0;
socklen_t socklen = sizeof(sock_error);
- if(getsockopt(fd, SOL_SOCKET, SO_ERROR, &sock_error, (socklen_t*)&socklen) == 0) {
+ if(getsockopt(sock_info->sock_fd, SOL_SOCKET, SO_ERROR, &sock_error, (socklen_t*)&socklen) == 0) {
LOGD("Socket error:%d", sock_error);
}
@@ -190,39 +190,41 @@
return;
}
- struct tcp_info info;
- int len = sizeof(info);
- if(getsockopt(fd, IPPROTO_TCP, TCP_INFO, &info, (socklen_t*)&len) == 0) {
- LOGD("State : %d", info.tcpi_state);
+ if(sock_info->sock_type == MBTK_SOCK_TCP) {
+ 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);
#if TCPIP_DEBUG
- tcp_info_print(&info);
+ tcp_info_print(&info);
#endif
- }
-
- if(TCP_ESTABLISHED != info.tcpi_state) {
- LOGW("errno = %d", errno);
- int link_id = tcpip_fd_2_link(fd);
- if(link_id >= 0) {
- // Socket disconnected?
- mbtk_tcpip_sock_close(link_id);
-
- if(tcpip_sock_cb) {
- tcpip_sock_cb(link_id, 0);
- }
}
- return;
+
+ if(TCP_ESTABLISHED != info.tcpi_state) {
+ LOGW("errno = %d", errno);
+ int link_id = tcpip_fd_2_link(sock_info->sock_fd);
+ if(link_id >= 0) {
+ // Socket disconnected?
+ mbtk_tcpip_sock_close(link_id);
+
+ if(tcpip_sock_cb) {
+ tcpip_sock_cb(link_id, 0);
+ }
+ }
+ return;
+ }
}
}
- if(event & EPOLLIN) { // READ
- LOGD("fd[%d] can read.", fd);
- int link_id = tcpip_fd_2_link(fd);
+ if(sock_info->event & EPOLLIN) { // READ
+ LOGD("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) {
if(tcpip_link[link_id].tcpip_info.cli_info.read_cb) {
char buff[MBTK_TCPIP_READ_BUFF_SIZE];
memset(buff, 0x0, MBTK_TCPIP_READ_BUFF_SIZE);
- int read_len = mbtk_sock_read_async(tcpip_handle, fd, buff, MBTK_TCPIP_READ_BUFF_SIZE);
+ int read_len = mbtk_sock_read_async(tcpip_handle, sock_info->sock_fd, buff, MBTK_TCPIP_READ_BUFF_SIZE);
if(read_len > 0) {
tcpip_link[link_id].tcpip_info.cli_info.data_traffic_recv += read_len;
tcpip_link[link_id].tcpip_info.cli_info.read_cb(link_id, (const char*)buff, read_len);
@@ -240,7 +242,7 @@
#endif
while(read_len > 0) {
memset(buff, 0x0, MBTK_TCPIP_READ_BUFF_SIZE);
- read_len = mbtk_sock_read_async(tcpip_handle, fd, buff, MBTK_TCPIP_READ_BUFF_SIZE);
+ read_len = mbtk_sock_read_async(tcpip_handle, sock_info->sock_fd, buff, MBTK_TCPIP_READ_BUFF_SIZE);
// LOGD("read_len = %d", read_len);
if(read_len > 0) {
tcpip_link[link_id].tcpip_info.cli_info.data_traffic_recv += read_len;
@@ -253,24 +255,24 @@
}
}
- } else if(event & EPOLLRDHUP) { // Close
- LOGD("fd[%d] Closed?", fd);
+ } else if(sock_info->event & EPOLLRDHUP) { // Close
+ LOGD("fd[%d] Closed?", sock_info->sock_fd);
} else {
- LOGW("Unknown event:%x",event);
+ LOGW("Unknown event:%x",sock_info->event);
}
}
}
-static void tcpip_net_cb_func(mbtk_sock_handle handle, int state, const char* addr, const char* if_name)
+static void tcpip_net_cb_func(mbtk_sock_handle handle, mbtk_net_cb_info_s *info)
{
if(tcpip_inited && tcpip_handle == handle) {
- LOGD("Net state : %d, %s, %s", state, if_name, addr);
- if(state == 0) {
+ LOGD("Net state : %d, %s, %s", info->state, info->if_name, info->addr);
+ if(info->state == 0) {
mbtk_tcpip_net_close();
}
if(tcpip_net_cb) {
- tcpip_net_cb(state, addr);
+ tcpip_net_cb(info->state, info->addr);
}
}
}