Add glibc support(default)

Change-Id: I7675edcf14df8707ecd424a962e4cc464a4c6ae4
diff --git a/mbtk/mbtk_gnssd/gnss_main.c b/mbtk/mbtk_gnssd/gnss_main.c
index 772cf21..1966855 100755
--- a/mbtk/mbtk_gnssd/gnss_main.c
+++ b/mbtk/mbtk_gnssd/gnss_main.c
@@ -85,9 +85,12 @@
         goto check_fail;
     }
 
-    if(strcmp(argv[3], "0") && strcmp(argv[3], "1")) {
+#if 0
+    int init_mode = atoi(argv[3]);
+    if(((GNSS_PRINT_PORT_UART1 | GNSS_PRINT_PORT_USB_NMEA | GNSS_PRINT_PORT_USB_AT | GNSS_PRINT_PORT_TTY_AT) & init_mode) != init_mode) {
         goto check_fail;
     }
+#endif
 
     return 0;
 check_fail:
@@ -97,34 +100,73 @@
 
 static int gnss_ports_open(uint32 print_port)
 {
-    if(print_port & GNSS_PRINT_PORT_TTY_AT) {
-        if(gnss_pty_open(&gnss_pty_master_fd, &gnss_pty_slave_fd, GNSS_PORT_PTY)) {
-            return GNSS_ERR_OPEN_DEV;
+    // TTY AT change.
+    if((gnss_info.print_port & GNSS_PRINT_PORT_TTY_AT) != (print_port & GNSS_PRINT_PORT_TTY_AT)) {
+        if(print_port & GNSS_PRINT_PORT_TTY_AT) { // Open
+            if(gnss_pty_open(&gnss_pty_master_fd, &gnss_pty_slave_fd, GNSS_PORT_PTY)) {
+                return GNSS_ERR_OPEN_DEV;
+            }
+            LOGD("Open PTY port success.");
+        } else { // Close
+            if(gnss_pty_slave_fd > 0) {
+                close(gnss_pty_slave_fd);
+                gnss_pty_slave_fd = -1;
+                unlink(GNSS_PORT_PTY);
+            }
+            LOGD("Close PTY port success.");
         }
-        LOGD("Open PTY port success.");
     }
 
-    if(print_port & GNSS_PRINT_PORT_USB_AT) {
-        if((gnss_usb_at_port_fd = gnss_port_open(GNSS_PORT_USB_AT, O_RDWR | O_NONBLOCK | O_NOCTTY, 115200, FALSE)) <= 0) {
-            return GNSS_ERR_OPEN_DEV;
+    // 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) {
+                return GNSS_ERR_OPEN_DEV;
+            }
+            LOGD("Open USB AT port success.");
+        } else { // Close
+            if(gnss_usb_at_port_fd > 0) {
+                close(gnss_usb_at_port_fd);
+                gnss_usb_at_port_fd = -1;
+            }
+            LOGD("Close USB AT port success.");
         }
-        LOGD("Open USB AT port success.");
     }
 
-    if(print_port & GNSS_PRINT_PORT_USB_NMEA) {
-        if((gnss_usb_nmea_port_fd = gnss_port_open(GNSS_PORT_USB_NMEA, O_RDWR | O_NONBLOCK | O_NOCTTY, 115200, FALSE)) <= 0) {
-            return GNSS_ERR_OPEN_DEV;
+    // 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) {
+                return GNSS_ERR_OPEN_DEV;
+            }
+            LOGD("Open USB NMEA port success.");
+        } else { // Close
+            if(gnss_usb_nmea_port_fd > 0) {
+                close(gnss_usb_nmea_port_fd);
+                gnss_usb_nmea_port_fd = -1;
+            }
+            LOGD("Close USB NMEA port success.");
         }
-        LOGD("Open USB NMEA port success.");
     }
 
-    if(print_port & GNSS_PRINT_PORT_UART1) {
-        if((gnss_uart_at_port_fd = gnss_port_open(GNSS_PORT_UART_AT, O_RDWR | O_NONBLOCK | O_NOCTTY, 115200, TRUE)) <= 0) {
-            return GNSS_ERR_OPEN_DEV;
+    // Uart AT change.
+    if((gnss_info.print_port & GNSS_PRINT_PORT_UART1) != (print_port & GNSS_PRINT_PORT_UART1)) {
+        if(print_port & GNSS_PRINT_PORT_UART1) { // Open
+            if((gnss_uart_at_port_fd = gnss_port_open(GNSS_PORT_UART_AT, O_RDWR | O_NONBLOCK | O_NOCTTY, 115200, TRUE)) <= 0) {
+                return GNSS_ERR_OPEN_DEV;
+            }
+            LOGD("Open UART AT port success.");
+        } else { // Close
+            if(gnss_uart_at_port_fd > 0) {
+                close(gnss_uart_at_port_fd);
+                gnss_uart_at_port_fd = -1;
+            }
+            LOGD("Close UART AT port success.");
         }
-        LOGD("Open UART AT port success.");
     }
 
+    gnss_info.print_port = print_port;
+
     return GNSS_ERR_OK;
 }
 
@@ -156,6 +198,8 @@
         unlink(GNSS_PORT_PTY);
     }
 
+    gnss_info.print_port = 0;
+
     return 0;
 }
 
@@ -564,7 +608,16 @@
 {
     if(gnss_info.state != GNSS_STATE_CLOSE) {
         LOGW("GNSS not close:%d", gnss_info.state);
-        return GNSS_ERR_OK;
+        if(gnss_info.state == GNSS_STATE_READY) {
+            LOGD("Reset print port : %d -> %d", gnss_info.print_port, print_port);
+            if(gnss_info.print_port != print_port) {
+                return gnss_ports_open(print_port);
+            } else {
+                return GNSS_ERR_OK;
+            }
+        } else {
+            return GNSS_ERR_OK;
+        }
     }
 
     int ret = 0;
@@ -658,11 +711,10 @@
 
     // GNSS is ready, NMEA can print from uart.
     gnss_info.state = GNSS_STATE_READY;
-    gnss_info.print_port = print_port;
 
     LOGD("GNSS open success.");
 
-    return gnss_ports_open(gnss_info.print_port);
+    return gnss_ports_open(print_port);
 
 exit_with_dev_close:
     if(gnss_info.gnss_dev_close()) {
@@ -826,7 +878,7 @@
 }
 
 
-// mbtk_gnssd 6228 /dev/ttyS2 baud 0/1
+// mbtk_gnssd 6228 /dev/ttyS2 baud 0/1 <port_type>
 int main(int argc, char *argv[])
 {
     mbtk_log_init("radio", GNSS_TAG);
@@ -902,10 +954,16 @@
     LOGD("GNSS : %s, Device: %s", argv[1], gnss_info.dev_name);
     // Auto open gnss.
     if(gnss_info.auto_open) {
-        if(gnss_init(0)) { // No print to any port.
-            LOGE("gnss_init() fail.");
-            return -1;
+        int init_mode = atoi(argv[3]);
+        if(((GNSS_PRINT_PORT_UART1 | GNSS_PRINT_PORT_USB_NMEA | GNSS_PRINT_PORT_USB_AT | GNSS_PRINT_PORT_TTY_AT) & init_mode) != init_mode) {
+            init_mode = 0;
         }
+        if(gnss_init((uint32)init_mode)) {
+            LOGE("gnss_init() fail.");
+            // return -1;
+        }
+    } else {
+        gnss_info.print_port = 0;
     }
 
     // Init ubus and waitting IPC commands.