Fix gnss close timeout.

Change-Id: I567d84056e248643f28388fc00b9967e4f650c81
diff --git a/mbtk/mbtk_gnssd/gnss_6228.c b/mbtk/mbtk_gnssd/gnss_6228.c
index 59cb6df..9bbe8f4 100755
--- a/mbtk/mbtk_gnssd/gnss_6228.c
+++ b/mbtk/mbtk_gnssd/gnss_6228.c
Binary files differ
diff --git a/mbtk/mbtk_gnssd/gnss_6228.h b/mbtk/mbtk_gnssd/gnss_6228.h
index 2ea9dde..20a90b2 100755
--- a/mbtk/mbtk_gnssd/gnss_6228.h
+++ b/mbtk/mbtk_gnssd/gnss_6228.h
@@ -22,7 +22,7 @@
 
 int gnss_6228_dev_open();
 
-int gnss_6228_dev_close();
+int gnss_6228_dev_close(int fd);
 
 int gnss_6228_open(const char *dev);
 
diff --git a/mbtk/mbtk_gnssd/gnss_asr5311.c b/mbtk/mbtk_gnssd/gnss_asr5311.c
index 232e5f7..ffa43f8 100755
--- a/mbtk/mbtk_gnssd/gnss_asr5311.c
+++ b/mbtk/mbtk_gnssd/gnss_asr5311.c
@@ -112,8 +112,9 @@
     return 0;
 }
 
-int gnss_5311_dev_close()
+int gnss_5311_dev_close(int fd)
 {
+    gnss_send_cmd(fd, config_msg_pm5, strlen(config_msg_pm5));
     return 0;
 }
 
@@ -129,8 +130,6 @@
     pthread_mutex_destroy(&read_mutex);
     pthread_cond_destroy(&read_cond);
 
-    gnss_send_cmd(fd, config_msg_pm5, strlen(config_msg_pm5));
-
     return gnss_port_close(fd);
 }
 
diff --git a/mbtk/mbtk_gnssd/gnss_asr5311.h b/mbtk/mbtk_gnssd/gnss_asr5311.h
index 6d28b2b..e458507 100755
--- a/mbtk/mbtk_gnssd/gnss_asr5311.h
+++ b/mbtk/mbtk_gnssd/gnss_asr5311.h
@@ -14,7 +14,7 @@
 
 int gnss_5311_dev_open();
 
-int gnss_5311_dev_close();
+int gnss_5311_dev_close(int fd);
 
 int gnss_5311_open(const char *dev);
 
diff --git a/mbtk/mbtk_gnssd/gnss_hd8122.c b/mbtk/mbtk_gnssd/gnss_hd8122.c
index 2ae29c1..08216d2 100755
--- a/mbtk/mbtk_gnssd/gnss_hd8122.c
+++ b/mbtk/mbtk_gnssd/gnss_hd8122.c
@@ -417,7 +417,7 @@
     return mbtk_gpio_value_set(GNSS_POWER_GPIO, MBTK_GPIO_DIRECT_OUT, 1);
 }
 
-int gnss_8122_dev_close()
+int gnss_8122_dev_close(int fd)
 {
     return mbtk_gpio_value_set(GNSS_POWER_GPIO, MBTK_GPIO_DIRECT_OUT, 0);
 }
diff --git a/mbtk/mbtk_gnssd/gnss_hd8122.h b/mbtk/mbtk_gnssd/gnss_hd8122.h
index b9d73b9..4cd4f46 100755
--- a/mbtk/mbtk_gnssd/gnss_hd8122.h
+++ b/mbtk/mbtk_gnssd/gnss_hd8122.h
@@ -110,7 +110,7 @@
 
 int gnss_8122_dev_open();
 
-int gnss_8122_dev_close();
+int gnss_8122_dev_close(int fd);
 
 int gnss_8122_open();
 
diff --git a/mbtk/mbtk_gnssd/gnss_info.h b/mbtk/mbtk_gnssd/gnss_info.h
index 906d67f..1e279a3 100755
--- a/mbtk/mbtk_gnssd/gnss_info.h
+++ b/mbtk/mbtk_gnssd/gnss_info.h
@@ -73,7 +73,7 @@
 } gnss_memaver_type_enum;
 
 typedef int (*gnss_dev_open_func)();
-typedef int (*gnss_dev_close_func)();
+typedef int (*gnss_dev_close_func)(int fd);
 typedef int (*gnss_open_func)(const char *dev);
 typedef int (*gnss_close_func)(int fd);
 typedef int (*gnss_fw_dl_func)(int fd, const char *fw_name, const char *dev);
diff --git a/mbtk/mbtk_gnssd/gnss_main.c b/mbtk/mbtk_gnssd/gnss_main.c
index cfceb1a..5109126 100755
--- a/mbtk/mbtk_gnssd/gnss_main.c
+++ b/mbtk/mbtk_gnssd/gnss_main.c
@@ -9,6 +9,7 @@
 #include <signal.h>
 #include <cutils/properties.h>
 #include <ctype.h>
+#include <termios.h>
 
 #include "mbtk_type.h"
 #include "mbtk_log.h"
@@ -123,7 +124,7 @@
     // USB AT change.
     if((gnss_info.print_port & GNSS_PRINT_PORT_USB_AT) != (print_port & GNSS_PRINT_PORT_USB_AT)) {
         if(print_port & GNSS_PRINT_PORT_USB_AT) { // Open
-            if((gnss_usb_at_port_fd = gnss_port_open(GNSS_PORT_USB_AT, O_RDWR | O_NONBLOCK | O_NOCTTY, 115200, FALSE)) <= 0) {
+            if((gnss_usb_at_port_fd = gnss_port_open(GNSS_PORT_USB_AT, O_RDWR | O_NONBLOCK | O_NOCTTY, 115200, TRUE)) <= 0) {
                 return GNSS_ERR_OPEN_DEV;
             }
             LOGD("Open USB AT port success.");
@@ -139,7 +140,7 @@
     // USB NMEA change.
     if((gnss_info.print_port & GNSS_PRINT_PORT_USB_NMEA) != (print_port & GNSS_PRINT_PORT_USB_NMEA)) {
         if(print_port & GNSS_PRINT_PORT_USB_NMEA) { // Open
-            if((gnss_usb_nmea_port_fd = gnss_port_open(GNSS_PORT_USB_NMEA, O_RDWR | O_NONBLOCK | O_NOCTTY, 115200, FALSE)) <= 0) {
+            if((gnss_usb_nmea_port_fd = gnss_port_open(GNSS_PORT_USB_NMEA, O_RDWR | O_NONBLOCK | O_NOCTTY, 115200, TRUE)) <= 0) {
                 return GNSS_ERR_OPEN_DEV;
             }
             LOGD("Open USB NMEA port success.");
@@ -176,26 +177,31 @@
 static int gnss_ports_close()
 {
     if(gnss_usb_at_port_fd > 0) {
+        tcflush(gnss_usb_at_port_fd, TCIOFLUSH);
         close(gnss_usb_at_port_fd);
         gnss_usb_at_port_fd = -1;
     }
 
     if(gnss_usb_nmea_port_fd > 0) {
+        tcflush(gnss_usb_nmea_port_fd, TCIOFLUSH);
         close(gnss_usb_nmea_port_fd);
         gnss_usb_nmea_port_fd = -1;
     }
 
     if(gnss_uart_at_port_fd > 0) {
+        tcflush(gnss_uart_at_port_fd, TCIOFLUSH);
         close(gnss_uart_at_port_fd);
         gnss_uart_at_port_fd = -1;
     }
 
     if(gnss_pty_master_fd > 0) {
+        tcflush(gnss_pty_master_fd, TCIOFLUSH);
         close(gnss_pty_master_fd);
         gnss_pty_master_fd = -1;
     }
 
     if(gnss_pty_slave_fd > 0) {
+        tcflush(gnss_pty_slave_fd, TCIOFLUSH);
         close(gnss_pty_slave_fd);
         gnss_pty_slave_fd = -1;
         unlink(GNSS_PORT_PTY);
@@ -719,10 +725,6 @@
 
     return gnss_ports_open(print_port);
 
-exit_with_dev_close:
-    if(gnss_info.gnss_dev_close()) {
-        LOGE("gnss_dev_close() fail.");
-    }
 exit_with_thread_exit:
     gnss_info.state = GNSS_STATE_CLOSING;
     // Wait for read thread exit.
@@ -759,6 +761,11 @@
         return GNSS_ERR_UNKNOWN;
     }
 
+    if(gnss_info.gnss_dev_close(gnss_info.fd)) {
+        LOGE("gnss_dev_close() fail.");
+        return GNSS_ERR_UNKNOWN;
+    }
+
     // Wait for read thread exit.
     if(gnss_info.exit_fd[1] > 0) {
         write(gnss_info.exit_fd[1], "exit", 4);
@@ -776,11 +783,6 @@
         return GNSS_ERR_UNKNOWN;
     }
 
-    if(gnss_info.gnss_dev_close()) {
-        LOGE("gnss_dev_close() fail.");
-        return GNSS_ERR_UNKNOWN;
-    }
-
     if(gnss_ports_close()) {
         LOGE("gnss_ports_close fail.");
         return GNSS_ERR_UNKNOWN;