Fix gnss close timeout.

Change-Id: I567d84056e248643f28388fc00b9967e4f650c81
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;