[Bugfix][T106][task-view-127]Optimize GNSS-5HZ output and file configuration function

Only Configure: Yes
Affected branch: master
Affected module: gnss
Is it affected on both ZXIC and MTK: only ZXIC
Self-test: yes
Doc Update: No

Change-Id: I01e6a2906907abf3adbdaa952ff8afbf5abf593b
diff --git a/cap/zx297520v3/src/lynq/lib/liblynq-qser-gnss/include/mbtk_gnss_internal.h b/cap/zx297520v3/src/lynq/lib/liblynq-qser-gnss/include/mbtk_gnss_internal.h
index 50cc58a..acd1c04 100755
--- a/cap/zx297520v3/src/lynq/lib/liblynq-qser-gnss/include/mbtk_gnss_internal.h
+++ b/cap/zx297520v3/src/lynq/lib/liblynq-qser-gnss/include/mbtk_gnss_internal.h
@@ -250,7 +250,7 @@
 
 #define MBTK_GNSS_DEV  "/dev/ttyS2"
 
-#define MBTK_UART_RECV_BUFFER_SIZE 1024
+#define MBTK_UART_RECV_BUFFER_SIZE 1024*1024
 #define MBTK_UART_SEND_BUFFER_MAX 128
 
 #define TRUE 1
@@ -349,6 +349,7 @@
 int lynq_gnss_get_device_info(uint32 h_gnss);
 int lynq_gnss_get_uart(uint32 h_gnss);
 int lynq_gnss_get_ap_data(void);
+int lynq_gnss_output_frq(int frequency);
 
 #ifdef __cplusplus
 }
diff --git a/cap/zx297520v3/src/lynq/lib/liblynq-qser-gnss/src/lynq_qser_gnss.cpp b/cap/zx297520v3/src/lynq/lib/liblynq-qser-gnss/src/lynq_qser_gnss.cpp
index 1aed4db..73f176e 100755
--- a/cap/zx297520v3/src/lynq/lib/liblynq-qser-gnss/src/lynq_qser_gnss.cpp
+++ b/cap/zx297520v3/src/lynq/lib/liblynq-qser-gnss/src/lynq_qser_gnss.cpp
@@ -266,56 +266,22 @@
  *
  * @return     
  */
-static unsigned char save_configuration[] = {0xF1,0xD9,0x06,0x09,0x08,0x00,0x00,0x00,0x00,0x00,0x0F,0x00,0x00,0x00,0x26,0x37};
-static unsigned char one_frequency[] = {0xF1,0xD9,0x06,0x42,0x14,0x00,0x00,0x01,0x00,0x00,0xE8,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x48,0xF6};
-static unsigned char two_frequency[] = {0xF1,0xD9,0x06,0x42,0x14,0x00,0x00,0x02,0x66,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC4,0x88};
-static unsigned char three_frequency[] = {0xF1,0xD9,0x06,0x42,0x14,0x00,0x00,0x05,0x66,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC7,0xC1};
+
 int qser_Gnss_Set_Frequency(uint32_t h_gnss, int frequency)
 {
     int ret;
-    int buf_lenth;
-    unsigned char *send_buf;
     if(!inited)
     {
         ALOGE("has not been initiated\n");
         return -1;
     }
-    struct mbtk_gnss_handle_t *gnss_handle = (struct mbtk_gnss_handle_t *)h_gnss;
-    switch(frequency)
-    {
-        case 1:
-        {
-            send_buf = one_frequency;
-            buf_lenth = sizeof(one_frequency);
-            break;
-        }
-        case 2:
-        {
-            send_buf = two_frequency;
-            buf_lenth = sizeof(two_frequency);
-            break;
-        }
-        case 5:
-        {
-            send_buf = three_frequency;
-            buf_lenth = sizeof(three_frequency);
-            break;
-        }
-        default:
-            return 0;
-    }
-    ret = write(gnss_handle->dev_fd, send_buf, buf_lenth);
+
+    ret = lynq_gnss_output_frq(frequency);
     if(ret < 0)
     {
         ALOGE("modify cmd send faild\n");
         return -1;
     }
-    ret = write(gnss_handle->dev_fd, save_configuration, sizeof(save_configuration));
-    if(ret < 0)
-    {
-        ALOGE("save configuration send faild\n");
-        return -1;
-    }
     return 0;
 }
 
diff --git a/cap/zx297520v3/src/lynq/lib/liblynq-qser-gnss/src/mbtk_gnss.cpp b/cap/zx297520v3/src/lynq/lib/liblynq-qser-gnss/src/mbtk_gnss.cpp
index 5087a82..caf1a36 100755
--- a/cap/zx297520v3/src/lynq/lib/liblynq-qser-gnss/src/mbtk_gnss.cpp
+++ b/cap/zx297520v3/src/lynq/lib/liblynq-qser-gnss/src/mbtk_gnss.cpp
@@ -13,6 +13,8 @@
 #include <termios.h>
 #include <time.h>
 #include <sys/ioctl.h>
+#include <dirent.h>
+#include <sys/stat.h>
 #include <malloc.h>
 #include <sys/time.h>
 #include <liblog/lynq_deflog.h>
@@ -26,11 +28,14 @@
 
 #define _XOPEN_SOURCE
 #define NMEA_RMC "RMC"
+#define NEMA_LOG_FILE "/mnt/userdata/gnss.conf"
+
 extern long timezone;
 extern int g_gnss_sync_enable_flag;
 extern int g_gnss_sync_done;
 extern int event;
 extern int device_fd;
+
 typedef int (*sc_rtc_time_get_cb)(unsigned int src_id, unsigned long ulsec);
 extern int sc_rtc_timer_init(void);
 extern int sc_rtc_timer_uninit(void);
@@ -136,8 +141,20 @@
 static pthread_mutex_t loc_cond_mutex_r = PTHREAD_MUTEX_INITIALIZER;
 static pthread_cond_t loc_sync_cond = PTHREAD_COND_INITIALIZER;
 #endif
+
+const char *config_flags[] =
+{  
+    "debug.dbg2file",       //NMEA_LOG_FLAG,
+    "debug.filename",       //NMEA_LOG_NAME_FLAG,
+    "debug.filesize",       //NMEA_LOG_SIZE_FLAG,
+    "debug.filenum",        //NMEA_LOG_NUM_FLAG,
+    "debug.frequency"       //NMEA_OUTPUT_FREQ
+};
+#define NUM_CONFIG_FLAGS (sizeof(config_flags) / sizeof(config_flags[0]))
+int gnss_frq = 1;
 volatile int nmea_state = 0;
 volatile int nmea_reading = 0;
+
 static struct mbtk_gnss_cmd_msg_t mbtk_gnss_cmd_msg_map[] = {
 { 1,     "$OK",         NULL,                 0},
 { 2,     "$Fail",       NULL,                 0},
@@ -340,6 +357,124 @@
     return ret;
 }
 
+int lynq_gnss_output_frq(int frequency)
+{
+    char save_configuration[] = {0xF1,0xD9,0x06,0x09,0x08,0x00,0x00,0x00,0x00,0x00,0x0F,0x00,0x00,0x00,0x26,0x37};
+    char one_frequency[] = {0xF1,0xD9,0x06,0x42,0x14,0x00,0x00,0x01,0x62,0x00,0xE8,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xAA,0xDA};
+    char two_frequency[] = {0xF1,0xD9,0x06,0x42,0x14,0x00,0x00,0x02,0x62,0x00,0xF4,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xB5,0x8F};
+    char five_frequency[] = {0xF1,0xD9,0x06,0x42,0x14,0x00,0x00,0x05,0x62,0x00,0xC8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x8B,0xF9};
+    int ret;
+    int buf_length;
+    char *send_buf = NULL;
+    int fd = open(MBTK_GNSS_DEV, O_RDWR | O_NOCTTY | O_NDELAY);
+    if(fd < 0) {
+        ALOGE("open error\n");
+        return -1;
+    }
+    switch(frequency)
+    {
+        case 1:
+        {
+            send_buf = one_frequency;
+            buf_length = sizeof(one_frequency);
+            break;
+        }
+        case 2:
+        {
+            send_buf = two_frequency;
+            buf_length = sizeof(two_frequency);
+            break;
+        }
+        case 5:
+        {
+            send_buf = five_frequency;
+            buf_length = sizeof(five_frequency);
+            break;
+        }
+        default:
+        {
+            close(fd);
+            return -2;
+        }
+    }
+    ret = write(fd, send_buf, buf_length);
+    if(ret < 0)
+    {
+        ALOGE("modify cmd send faild\n");
+        close(fd);
+        return -1;
+    }
+    ret = write(fd, save_configuration, sizeof(save_configuration));
+    if(ret < 0)
+    {
+        ALOGE("save configuration send faild\n");
+        close(fd);
+        return -1;
+    }
+    usleep(100000);
+    close(fd);
+    return 0;
+}
+
+int lynq_gnss_configure_init()
+{
+    FILE *file;
+    char buffer[128];
+    char *configure_pointers[NUM_CONFIG_FLAGS] = {NULL};
+    
+    file = fopen(NEMA_LOG_FILE, "r");
+    if(file == NULL)
+    {
+        ALOGD("dont't need configure\n");
+        return -1;
+    }
+    while(fgets(buffer, 128, file) != NULL)
+    {
+        size_t len = strlen(buffer);
+        if(len > 0 && buffer[len-1] == '\n')
+        {
+            buffer[len-1] = '\0';
+        }
+        for(int i = 0; i < NUM_CONFIG_FLAGS; i++)
+        {
+            if(strncmp(buffer, config_flags[i], strlen(config_flags[i])) == 0)
+            {
+                if(configure_pointers[i] == NULL)
+                {
+                    configure_pointers[i] = strdup(buffer); // 复制到相应位置  
+                }
+                break; // 找到匹配后可以退出循环  
+            }
+        }
+    }
+    fclose(file);
+
+    if(configure_pointers[4] != NULL)
+    {
+        int ret;
+        gnss_frq = atoi(configure_pointers[4] + strlen(config_flags[4]) + 1);
+        ALOGD("gnss_frq = %d\n", gnss_frq);
+        ret = lynq_gnss_output_frq(gnss_frq);
+        {
+            if(ret < 0)
+            {
+                ALOGE("set gnss frq error\n");
+            }
+        }
+    }
+
+    for(int j = 0; j < NUM_CONFIG_FLAGS; j++)
+    {  
+        if(configure_pointers[j] != NULL)
+        {  
+            free(configure_pointers[j]); // 释放使用 strdup 分配的内存  
+        }  
+    }
+    
+    return 0;
+}
+
+
 static void get_gnss_agnss_state(int cmd, char *str, void *data)
 {
 
@@ -788,12 +923,9 @@
                 break;
             }
         }
-
         if (i > (256 - 2))
             continue;
-
         ret = ring_buffer_dequeue_arr(&gnss_handle->ring_buffer, tmp_arr, i + 1);
-
         if(ret > 0 && 0 == mbtk_gnss_handle->getap_status) {
             get_gnss_from_str(gnss_handle, tmp_arr, ret);
             memset(tmp_arr, 0, sizeof(tmp_arr));
@@ -819,15 +951,12 @@
 
     while(mbtk_gnss_handle->inited)
     {
-        while(mbtk_gnss_handle->getap_status){
-            // 在读AP_DATA星历时,不能输出NMEA
-            usleep(100000);
-        }
         if(nmea_state == 1)
         {
             nmea_reading = 1;
             ret = mopen_gnss_read(gnss_handle->dev_fd, buf, MBTK_UART_RECV_BUFFER_SIZE);
             nmea_reading = 0;
+
             if(ret > 0) {
                 ring_buffer_queue_arr(&gnss_handle->ring_buffer, buf, ret);
                 memset(buf, 0, sizeof(buf));
@@ -835,7 +964,7 @@
                 gnss_log("read error\n");
             }
         }
-        usleep(100000);
+        usleep(5000);
     }
 
     pthread_exit(NULL);
@@ -996,6 +1125,8 @@
     return 0;
 }
 
+
+
 int lynq_gnss_client_init(uint32 *ph_gnss)
 {
     int ret;
@@ -1018,9 +1149,7 @@
     }
     memset(mbtk_gnss_handle, 0, sizeof(struct mbtk_gnss_handle_t));
     memset(&mopen_gnss_device_info, 0, sizeof(mopen_gnss_device_info));
-    lynq_open_gps(0);
-
-    mbtk_gnss_handle->dev_fd = mopen_gnss_open(MBTK_GNSS_DEV, 0);
+    mbtk_gnss_handle->dev_fd = mopen_gnss_open(MBTK_GNSS_DEV, B115200);
     mbtk_gnss_handle->rb = (char*)malloc(MBTK_UART_RECV_BUFFER_SIZE);
     if(NULL == mbtk_gnss_handle->rb)
     {
@@ -1028,6 +1157,12 @@
         return -1;
     }
 
+    lynq_open_gps(1);
+    sleep(1);
+    lynq_gnss_configure_init();
+
+    lynq_open_gps(0);
+
     ring_buffer_init(&mbtk_gnss_handle->ring_buffer,
                      mbtk_gnss_handle->rb,
                      MBTK_UART_RECV_BUFFER_SIZE);
@@ -1386,8 +1521,8 @@
     }
     if(mbtk_http_session_start(http_handle, http_session))
     {
-    printf("mbtk_http_session_start() fail.\n");
-    return -4;
+        printf("mbtk_http_session_start() fail.\n");
+        return -4;
     }
     if(mbtk_http_handle_free(http_handle))
     {