Add gnss 5311 support.

Change-Id: I436cbc7eabe8e6448d318b6eee81f49dddcce756
diff --git a/mbtk/mbtk_gnssd/gnss_main.c b/mbtk/mbtk_gnssd/gnss_main.c
index 688f8dc..3f887f1 100755
--- a/mbtk/mbtk_gnssd/gnss_main.c
+++ b/mbtk/mbtk_gnssd/gnss_main.c
@@ -15,6 +15,8 @@
 
 #include "gnss_6228.h"
 #include "gnss_hd8122.h"
+#include "gnss_asr5311.h"
+
 
 #define GNSS_DEBUG 1
 #define GNSS_UBUS_ENABLE 1
@@ -29,10 +31,8 @@
 
 #ifdef GNSS_DEBUG
 #define GNSS_NMEA_FILE_LOG      "/tmp/mbtk_gnss_nmea.log"
-#define GNSS_NMEA_FILE_LOG_MAX  10485760    // 10MB
-
 #define GNSS_FILE_LOG           "/tmp/mbtk_gnss.log"
-#define GNSS_FILE_LOG_MAX       10485760    // 10MB
+#define GNSS_FILE_LOG_MAX       104857600    // 100MB
 #endif
 
 gnss_info_t gnss_info;
@@ -54,7 +54,8 @@
 #ifdef GNSS_DEBUG
 static bool nmea_log_enable = FALSE;
 static int nmea_log_fd = -1;
-static int nmea_log_fd_len = 0;
+static int debug_fd = -1;
+static int debug_fd_len = 0;
 #endif
 static int gnss_pty_master_fd = -1;
 static int gnss_pty_slave_fd = -1;
@@ -65,7 +66,7 @@
 
 static void help()
 {
-    LOGD("mbtk_gnssd <6228/8122...> <gnss_dev> <0/1>");
+    LOGD("mbtk_gnssd <6228/8122/5311> <gnss_dev> <0/1>");
 }
 
 static int arg_check(int argc, char *argv[])
@@ -74,8 +75,8 @@
         goto check_fail;
     }
 
-    // Only support 6228/8122.
-    if(strcmp(argv[1], GNSS_ID_6228) && strcmp(argv[1], GNSS_ID_8122)) {
+    // Only support 6228/8122/5311.
+    if(strcmp(argv[1], GNSS_ID_6228) && strcmp(argv[1], GNSS_ID_8122) && strcmp(argv[1], GNSS_ID_5311)) {
         goto check_fail;
     }
 
@@ -157,26 +158,44 @@
     return 0;
 }
 
-static void nmea_print(const char *nmea, int nmea_len)
-{
 #ifdef GNSS_DEBUG
+static void log_save(int fd, const char *data, int data_len)
+{
     if(nmea_log_enable){
-        if(nmea_log_fd_len > GNSS_NMEA_FILE_LOG_MAX) {
+        if(0 /* debug_fd_len > GNSS_FILE_LOG_MAX */) {
+            LOGD("Reopen file:%s(len = %d)", GNSS_FILE_LOG, debug_fd_len);
+            close(debug_fd);
+            debug_fd = open(GNSS_FILE_LOG, O_WRONLY | O_CREAT | O_TRUNC, 0666);
+            if(debug_fd < 0) {
+                LOGE("Open debug fd fail.");
+            }
+            debug_fd_len = 0;
+
+            LOGD("Reopen file:%s", GNSS_NMEA_FILE_LOG);
             close(nmea_log_fd);
             nmea_log_fd = open(GNSS_NMEA_FILE_LOG, O_WRONLY | O_CREAT | O_TRUNC, 0666);
             if(nmea_log_fd < 0) {
                 LOGE("Open debug fd fail.");
             }
-            nmea_log_fd_len = 0;
         }
 
-        if(nmea_log_fd > 0) {
-            write(nmea_log_fd, nmea, nmea_len);
-            nmea_log_fd_len += nmea_len;
+        if(fd == nmea_log_fd) {
+            if(nmea_log_fd > 0) {
+                write(nmea_log_fd, data, data_len);
+                debug_fd_len += data_len;
+            }
+        } else if(fd == debug_fd) {
+            if(debug_fd > 0) {
+                write(debug_fd, data, data_len);
+                debug_fd_len += data_len;
+            }
         }
     }
+}
 #endif
 
+static void nmea_print(const char *nmea, int nmea_len)
+{
     if(gnss_usb_at_port_fd > 0) {
         write(gnss_usb_at_port_fd, nmea, nmea_len);
     }
@@ -236,11 +255,22 @@
 
 static void gnss_nmea_process(const char *data, int data_len)
 {
+    // LOGD("gnss_nmea_process() : data_len - %d", data_len);
+#if 0
     char nmea[GNSS_BUFF_SIZE] = {0};
     memcpy(nmea, data, data_len);
+#else
+    const char *nmea = data;
+#endif
 
     if(!nmea_check(nmea, data_len)) {
-        gnss_info.gnss_set_cb(nmea, data_len);
+        LOGD("NO-NMEA:%s", nmea);
+#if GNSS_DEBUG
+        log_save(nmea_log_fd, "/**/", 4);
+        log_save(nmea_log_fd, nmea, data_len);
+#endif
+        if(gnss_info.gnss_set_cb)
+            gnss_info.gnss_set_cb(nmea, data_len);
         return;
     }
 
@@ -248,6 +278,8 @@
     if(nmea_log_enable) {
         LOGD("NMEA[%d]:%s", data_len, nmea);
     }
+
+    log_save(nmea_log_fd, nmea, data_len);
 #endif
 
     nmea_print(nmea, data_len);
@@ -265,7 +297,7 @@
 static bool nmea_char_check(char ch)
 {
     if(isalnum(ch) || ch == '$' || ch == '\r' || ch == '\n' || ch == '.'
-        || ch == ',' || ch == '*' || ch == '\0' || ch == '/' || ch == '_')
+        || ch == ',' || ch == '*' || ch == '\0' || ch == '/' || ch == '_' || ch == '=')
         return TRUE;
 
     return FALSE;
@@ -277,7 +309,9 @@
         LOGD("GNSS_OPEN[%d]:%s", data_len, data);
     } else if(gnss_info.state == GNSS_STATE_DOWNLOAD) {
         // LOGD("GNSS_DL[%d]:%s", data_len, data);
-        gnss_info.gnss_dl_read_cb(data, data_len);
+        if(gnss_info.gnss_dl_read_cb) {
+            gnss_info.gnss_dl_read_cb(data, data_len);
+        }
     } else if(gnss_info.state == GNSS_STATE_READY) {
         int index = 0;
         while(index < data_len) {
@@ -298,12 +332,26 @@
                     nmea_buff[nmea_buff_len++] = data[index];
                     if(nmea_buff[nmea_buff_len - 1] == '\n') {
                         if(data_buff_len > 0) {
-                            gnss_info.gnss_set_cb(data_buff, data_buff_len);
+#if GNSS_DEBUG
+                            log_save(nmea_log_fd, "/**/", 4);
+                            log_save(nmea_log_fd, data_buff, data_buff_len);
+#endif
+                            if(gnss_info.gnss_set_cb) {
+                                gnss_info.gnss_set_cb(data_buff, data_buff_len);
+                            }
                             data_buff_len = 0;
                         }
 
                         if(nmea_buff_len > 6 && nmea_buff[nmea_buff_len - 5] == '*') { // $XXX*YY\r\n
+                            nmea_buff[nmea_buff_len] = '\0';
                             gnss_nmea_process(nmea_buff, nmea_buff_len);
+                        } else if(nmea_buff_len > 0) {
+                            nmea_buff[nmea_buff_len] = '\0';
+                            LOGD("NO-NMEA:%s", nmea_buff);
+#if GNSS_DEBUG
+                            log_save(nmea_log_fd, "/**/", 4);
+                            log_save(nmea_log_fd, nmea_buff, nmea_buff_len);
+#endif
                         }
 
                         nmea_buff_len = 0;
@@ -346,8 +394,6 @@
     nmea_buff_len = 0;
     data_buff_len = 0;
 #if GNSS_DEBUG
-    int debug_fd = -1;
-    int debug_fd_len = 0;
     if(nmea_log_enable) {
         debug_fd = open(GNSS_FILE_LOG, O_WRONLY | O_CREAT | O_TRUNC, 0666);
         if(debug_fd < 0) {
@@ -357,7 +403,7 @@
         if(nmea_log_fd < 0) {
             LOGE("Open nmea fd fail.");
         }
-        nmea_log_fd_len = 0;
+        debug_fd_len = 0;
     }
 #endif
 
@@ -382,26 +428,13 @@
         if (FD_ISSET(gnss_info.fd, &fdr))
         {
             memset(buffer, 0, GNSS_BUFF_SIZE);
-            len = read(gnss_info.fd, buffer, GNSS_BUFF_SIZE);
+            len = read(gnss_info.fd, buffer, GNSS_BUFF_SIZE - 1);
             if(len > 0) {
                 //log_hex("READ", buffer, len);
 
 #if GNSS_DEBUG
-                if(nmea_log_enable){
-                    if(debug_fd_len > GNSS_FILE_LOG_MAX) {
-                        close(debug_fd);
-                        debug_fd = open(GNSS_FILE_LOG, O_WRONLY | O_CREAT | O_TRUNC, 0666);
-                        if(debug_fd < 0) {
-                            LOGE("Open debug fd fail.");
-                        }
-                        debug_fd_len = 0;
-                    }
-
-                    if(debug_fd > 0) {
-                        write(debug_fd, buffer, len);
-                        debug_fd_len += len;
-                    }
-                }
+                //LOGD("read data_len = %d", len);
+                log_save(debug_fd, buffer, len);
 #endif
 
                 gnss_data_process(buffer, len);
@@ -517,51 +550,100 @@
     }
 
     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);
+            if(ret) {
+                LOGE("gnss_fw_dl() fail : %d", ret);
+                gnss_info.state = GNSS_STATE_CLOSE;
+                return -1;
+            }
 
-    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;
-    }
-    if(pipe(gnss_info.exit_fd)) {
-        LOGE("pipe() fail[%d].", errno);
-        return -1;
-    }
-    // GNSS is opened.
-    gnss_info.state = GNSS_STATE_OPEN;
+            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;
+            }
+            if(pipe(gnss_info.exit_fd)) {
+                LOGE("pipe() fail[%d].", errno);
+                return -1;
+            }
+            // GNSS is opened.
+            gnss_info.state = GNSS_STATE_OPEN;
 
-    #if 0
-    // Start gnss read thread.
-    pthread_attr_t thread_attr;
-    pthread_attr_init(&thread_attr);
-    if(pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED))
-    {
-        LOGE("pthread_attr_setdetachstate() fail.");
-        goto main_exit;
-    }
+#if 0
+            // Start gnss read thread.
+            pthread_attr_t thread_attr;
+            pthread_attr_init(&thread_attr);
+            if(pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED))
+            {
+                LOGE("pthread_attr_setdetachstate() fail.");
+                goto main_exit;
+            }
 
-    if(pthread_create(&gnss_info.read_pid, &thread_attr, gnss_read_pthread, NULL))
+            if(pthread_create(&gnss_info.read_pid, &thread_attr, gnss_read_pthread, NULL))
 #else
-    if(pthread_create(&gnss_info.read_pid, NULL, gnss_read_pthread, NULL))
+            if(pthread_create(&gnss_info.read_pid, NULL, gnss_read_pthread, NULL))
 #endif
-    {
-        LOGE("pthread_create() fail.");
-        goto exit_with_close;
-    }
+            {
+                LOGE("pthread_create() fail.");
+                goto exit_with_close;
+            }
 
-    ret = gnss_info.gnss_dev_open();
-    if(ret) {
-        LOGE("gnss_dev_open() fail : %d", ret);
-        goto exit_with_thread_exit;
-    }
+            ret = gnss_info.gnss_dev_open();
+            if(ret) {
+                LOGE("gnss_dev_open() fail : %d", ret);
+                goto exit_with_thread_exit;
+            }
+        //}
+    } else {
+        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;
+        }
+        if(pipe(gnss_info.exit_fd)) {
+            LOGE("pipe() fail[%d].", errno);
+            return -1;
+        }
+        // GNSS is opened.
+        gnss_info.state = GNSS_STATE_OPEN;
 
-    if(gnss_info.auto_dl_fw) {
-        gnss_info.state = GNSS_STATE_DOWNLOAD;
-        ret = gnss_info.gnss_fw_dl(gnss_info.fd);
+#if 0
+        // Start gnss read thread.
+        pthread_attr_t thread_attr;
+        pthread_attr_init(&thread_attr);
+        if(pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED))
+        {
+            LOGE("pthread_attr_setdetachstate() fail.");
+            goto main_exit;
+        }
+
+        if(pthread_create(&gnss_info.read_pid, &thread_attr, gnss_read_pthread, NULL))
+#else
+        if(pthread_create(&gnss_info.read_pid, NULL, gnss_read_pthread, NULL))
+#endif
+        {
+            LOGE("pthread_create() fail.");
+            goto exit_with_close;
+        }
+
+        ret = gnss_info.gnss_dev_open();
         if(ret) {
-            LOGE("gnss_fw_dl() fail : %d", ret);
-            goto exit_with_dev_close;
+            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;
+            }
         }
     }
 
@@ -745,6 +827,7 @@
         gnss_info.gnss_id = GNSS_TYPE_6228;
         gnss_info.auto_open = (bool)atoi(argv[3]);
         gnss_info.auto_dl_fw = TRUE;
+        gnss_info.dl_befor_open = FALSE;
         gnss_info.gnss_dev_open = gnss_6228_dev_open;
         gnss_info.gnss_dev_close = gnss_6228_dev_close;
         gnss_info.gnss_open = gnss_6228_open;
@@ -757,6 +840,7 @@
         gnss_info.gnss_id = GNSS_TYPE_8122;
         gnss_info.auto_open = (bool)atoi(argv[3]);
         gnss_info.auto_dl_fw = FALSE;
+        gnss_info.dl_befor_open = FALSE;
         gnss_info.gnss_dev_open = gnss_8122_dev_open;
         gnss_info.gnss_dev_close = gnss_8122_dev_close;
         gnss_info.gnss_open = gnss_8122_open;
@@ -765,6 +849,19 @@
         gnss_info.gnss_dl_read_cb = NULL;
         gnss_info.gnss_set = gnss_8122_set;
         gnss_info.gnss_set_cb = gnss_8122_set_cb;
+    } else if(!strcmp(argv[1], GNSS_ID_5311)) {
+        gnss_info.gnss_id = GNSS_TYPE_5311;
+        gnss_info.auto_open = (bool)atoi(argv[3]);
+        gnss_info.auto_dl_fw = TRUE;
+        gnss_info.dl_befor_open = TRUE;
+        gnss_info.gnss_dev_open = gnss_5311_dev_open;
+        gnss_info.gnss_dev_close = gnss_5311_dev_close;
+        gnss_info.gnss_open = gnss_5311_open;
+        gnss_info.gnss_close = gnss_5311_close;
+        gnss_info.gnss_fw_dl = gnss_5311_fw_dl;
+        gnss_info.gnss_dl_read_cb = NULL;
+        gnss_info.gnss_set = gnss_5311_set;
+        gnss_info.gnss_set_cb = gnss_5311_set_cb;
     } else {
         LOGE("No support : %s", argv[1]);
         return -1;