Add 8122 gnss download(Not OK)

Change-Id: Ib276104c1f5f3c0e0284791602fe409a629bde8f
diff --git a/mbtk/mbtk_gnssd/gnss_main.c b/mbtk/mbtk_gnssd/gnss_main.c
index 9510f3d..a20b75e 100755
--- a/mbtk/mbtk_gnssd/gnss_main.c
+++ b/mbtk/mbtk_gnssd/gnss_main.c
@@ -99,33 +99,33 @@
 {
     if(print_port & GNSS_PRINT_PORT_TTY_AT) {
         if(gnss_pty_open(&gnss_pty_master_fd, &gnss_pty_slave_fd, GNSS_PORT_PTY)) {
-            return -1;
+            return GNSS_ERR_OPEN_DEV;
         }
         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 -1;
+            return GNSS_ERR_OPEN_DEV;
         }
         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 -1;
+            return GNSS_ERR_OPEN_DEV;
         }
         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 -1;
+            return GNSS_ERR_OPEN_DEV;
         }
         LOGD("Open UART AT port success.");
     }
 
-    return 0;
+    return GNSS_ERR_OK;
 }
 
 static int gnss_ports_close()
@@ -408,6 +408,8 @@
     }
 #endif
 
+    LOGD("uart_fd - %d, exit_fd - %d", gnss_info.fd, gnss_info.exit_fd[0]);
+
     while(gnss_info.state >= GNSS_STATE_OPEN) {
         ret = select(fd_max + 1, &fdr, &fdw, 0, NULL);
         //LOGD("select - %d", ret);
@@ -456,7 +458,7 @@
         else if (FD_ISSET(gnss_info.exit_fd[0], &fdr))
         {
             memset(buffer, 0, GNSS_BUFF_SIZE);
-            len = read(gnss_info.fd, buffer, GNSS_BUFF_SIZE);
+            len = read(gnss_info.exit_fd[0], buffer, GNSS_BUFF_SIZE);
             if(len > 0) {
                 if(strcmp(buffer, "exit") == 0) {
                     LOGD("Get thread exit message.");
@@ -547,29 +549,29 @@
 {
     if(gnss_info.state != GNSS_STATE_CLOSE) {
         LOGW("GNSS not close:%d", gnss_info.state);
-        return 0;
+        return GNSS_ERR_OK;
     }
 
     int ret = 0;
     if(gnss_info.dl_befor_open) {
         //if(gnss_info.auto_dl_fw) {
             gnss_info.state = GNSS_STATE_DOWNLOAD;
-            ret = gnss_info.gnss_fw_dl(gnss_info.fd, gnss_info.dev_name);
+            ret = gnss_info.gnss_fw_dl(gnss_info.fd, NULL, gnss_info.dev_name);
             if(ret) {
                 LOGE("gnss_fw_dl() fail : %d", ret);
                 gnss_info.state = GNSS_STATE_CLOSE;
-                return -1;
+                return GNSS_ERR_DL_FW;
             }
 
             gnss_info.fd = gnss_info.gnss_open(gnss_info.dev_name);
             if(gnss_info.fd <= 0) {
                 LOGE("gnss_open(%s) fail : %d", gnss_info.dev_name, gnss_info.fd);
                 gnss_info.state = GNSS_STATE_CLOSE;
-                return -1;
+                return GNSS_ERR_OPEN_DEV;
             }
             if(pipe(gnss_info.exit_fd)) {
                 LOGE("pipe() fail[%d].", errno);
-                return -1;
+                return GNSS_ERR_UNKNOWN;
             }
             // GNSS is opened.
             gnss_info.state = GNSS_STATE_OPEN;
@@ -604,11 +606,11 @@
         if(gnss_info.fd <= 0) {
             LOGE("gnss_open(%s) fail : %d", gnss_info.dev_name, gnss_info.fd);
             gnss_info.state = GNSS_STATE_CLOSE;
-            return -1;
+            return GNSS_ERR_OPEN_DEV;
         }
         if(pipe(gnss_info.exit_fd)) {
             LOGE("pipe() fail[%d].", errno);
-            return -1;
+            return GNSS_ERR_UNKNOWN;
         }
         // GNSS is opened.
         gnss_info.state = GNSS_STATE_OPEN;
@@ -637,15 +639,6 @@
             LOGE("gnss_dev_open() fail : %d", ret);
             goto exit_with_thread_exit;
         }
-
-        if(gnss_info.auto_dl_fw) {
-            gnss_info.state = GNSS_STATE_DOWNLOAD;
-            ret = gnss_info.gnss_fw_dl(gnss_info.fd, gnss_info.dev_name);
-            if(ret) {
-                LOGE("gnss_fw_dl() fail : %d", ret);
-                goto exit_with_dev_close;
-            }
-        }
     }
 
     // GNSS is ready, NMEA can print from uart.
@@ -680,20 +673,20 @@
         gnss_info.exit_fd[1] = -1;
     }
     gnss_info.state = GNSS_STATE_CLOSE;
-    return -1;
+    return GNSS_ERR_UNKNOWN;
 }
 
 int gnss_deinit()
 {
     if(gnss_info.state == GNSS_STATE_CLOSE) {
         LOGW("GNSS is closed.");
-        return 0;
+        return GNSS_ERR_OK;
     } else if(gnss_info.state == GNSS_STATE_CLOSING) {
         LOGW("GNSS is closing...");
-        return -1;
+        return GNSS_ERR_UNKNOWN;
     } else if(gnss_info.state == GNSS_STATE_DOWNLOAD) {
         LOGW("GNSS is downloading...");
-        return -1;
+        return GNSS_ERR_UNKNOWN;
     }
 
     // Wait for read thread exit.
@@ -705,22 +698,22 @@
     int ret = pthread_join(gnss_info.read_pid, NULL);
     if(ret){
         LOGE("pthrad_join fail(%d)",ret);
-        return -1;
+        return GNSS_ERR_UNKNOWN;
     }
 
     if(gnss_info.gnss_close(gnss_info.fd)) {
         LOGE("gnss_close() fail.");
-        return -1;
+        return GNSS_ERR_UNKNOWN;
     }
 
     if(gnss_info.gnss_dev_close()) {
         LOGE("gnss_dev_close() fail.");
-        return -1;
+        return GNSS_ERR_UNKNOWN;
     }
 
     if(gnss_ports_close()) {
         LOGE("gnss_ports_close fail.");
-        return -1;
+        return GNSS_ERR_UNKNOWN;
     }
 
     LOGD("gnss_ports_close() complete.");
@@ -736,7 +729,7 @@
     }
     gnss_info.state = GNSS_STATE_CLOSE;
     LOGD("GNSS close success.");
-    return 0;
+    return GNSS_ERR_OK;
 }
 
 int gnss_set(const void* buf, unsigned int buf_len, void *cmd_rsp, int cmd_rsp_len)
@@ -747,7 +740,30 @@
         }
         return gnss_info.gnss_set(gnss_info.fd, buf, cmd_rsp, cmd_rsp_len);
     } else {
-        return -1;
+        return GNSS_ERR_UNKNOWN;
+    }
+}
+
+int gnss_dl_fw(const char* fw_name, void *rsp, int rsp_len)
+{
+    if(gnss_info.gnss_id != GNSS_TYPE_8122) {
+        return GNSS_ERR_UNSUPPORT;
+    }
+
+    if(rsp && rsp_len > 0) {
+        memset(rsp, 0, rsp_len);
+    }
+
+    if(gnss_info.gnss_fw_dl) {
+        if(GNSS_ERR_OK != gnss_deinit()) {
+            LOGE("Close gnss fail.");
+            return GNSS_ERR_UNKNOWN;
+        } else {
+            LOGD("Start gnss fw dl.");
+            return gnss_info.gnss_fw_dl(gnss_info.fd, fw_name, gnss_info.dev_name);
+        }
+    } else {
+        return GNSS_ERR_UNKNOWN;
     }
 }