Merge "[Feature][ZXW-197]add T106E Agps function and the user information can be configured"
diff --git a/cap/zx297520v3/src/lynq/lib/liblynq-qser-gnss/include/lynq_qser_gnss.h b/cap/zx297520v3/src/lynq/lib/liblynq-qser-gnss/include/lynq_qser_gnss.h
index b98377e..9eead07 100755
--- a/cap/zx297520v3/src/lynq/lib/liblynq-qser-gnss/include/lynq_qser_gnss.h
+++ b/cap/zx297520v3/src/lynq/lib/liblynq-qser-gnss/include/lynq_qser_gnss.h
@@ -38,6 +38,15 @@
     float       azimuth;                /**<   Azimuth of the SV in degrees. */ 
 }QL_LOC_SV_INFO_T;                      /* Type */
 
+struct login_information  /*Used To Stored User Information*/
+{
+    char*   host;
+    char*   id;
+    char*   password;
+};
+
+static struct login_information *login_information_t = NULL;
+
 #define     QL_LOC_GPS_SUPPORT_SVS_MAX   32  /**  Maximum number of satellites in view. */
 typedef struct 
 {
@@ -114,6 +123,7 @@
 int qser_Gnss_Stop(uint32_t h_gnss);
 int qser_Gnss_Set_Frequency(uint32_t h_gnss, int frequency);
 int qser_Gnss_Delete_Aiding_Data(uint32_t h_gnss,DELETE_AIDING_DATA_TYPE_T flags);
+int qser_Gnss_Server_Configuration(char *host, char *id, char *password);
 int qser_Gnss_InjectTime(uint32_t h_gnss,LYNQ_INJECT_TIME_INTO_T *time_info);
 int qser_firmware_update(uint32_t h_gnss);
 int qser_Gnss_injectEphemeris(uint32_t h_gnss);
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 ce124b2..bb24b17 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
@@ -302,7 +302,9 @@
 int mopen_gnss_set_system_config(uint32 h_gnss, int mode);
 int mopen_gnss_set_nema_config(uint32 h_gnss, int mode);
 int mopen_gnss_download_tle(void);
+int hd_gnss_download_tle(char *host, char *id, char *password);
 int mopen_gnss_injects_aidpos(uint32 h_gnss);
+int hd_gnss_injects_aidpos(uint32 h_gnss);
 int mopen_gnss_firmware_update(uint32 ph_gnss);
 int mbtk_at_gnss_start_ttff(int type, int timeout_sec, int count);
 int lynq_gnss_send_cmd(uint32 h_gnss, const char *cmd, int cmd_len);
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 52241a9..4b7bba9 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
@@ -50,7 +50,19 @@
     ret = lynq_gnss_client_init(h_gnss);
     if(ret)
     {
-        printf("Init faild\n");
+        ALOGE("Init faild\n");
+    }
+    if(gpio21_value == 1)
+    {
+        login_information_t = (login_information*)malloc(sizeof(struct login_information));
+        if(NULL == login_information_t)
+        {
+            ALOGE("malloc memory error\n");
+            return -1;
+        }
+        login_information_t->host = NULL;
+        login_information_t->id = NULL;
+        login_information_t->password = NULL;
     }
     return 0;
 }
@@ -60,7 +72,12 @@
     ret = lynq_gnss_client_deinit(h_gnss);
     if(ret)
     {
-        printf("Deinit faild\n");
+        ALOGE("Deinit faild\n");
+    }
+    if(login_information_t != NULL)
+    {
+        free(login_information_t);
+        login_information_t = NULL;
     }
     return 0;
 }
@@ -70,13 +87,23 @@
     struct mbtk_gnss_handle_t *gnss_handle = (struct mbtk_gnss_handle_t *)h_gnss;
     if(0 == h_gnss && NULL == handler_ptr)
     {
-        printf("%s handler invalid.\n", __func__);
+        ALOGE("%s handler invalid.\n", __func__);
         return -1;
     }
     gnss_handle->gnss_handler_func = handler_ptr;
     return 0;
 }
 
+/**
+ * @brief      qser_Set_Indications
+ *
+ * @details    Set Output Mode
+ *             
+ * @param      e_msg_id_t type
+ *
+ * @return     
+ */
+
 int qser_Set_Indications(uint32_t h_gnss,e_msg_id_t type)
 {
     struct mbtk_gnss_handle_t *gnss_handle = (struct mbtk_gnss_handle_t *)h_gnss;
@@ -102,6 +129,15 @@
     return 0;
 }
 
+/**
+ * @brief      qser_Gnss_InjectTime
+ *
+ * @details    injection time
+ *             
+ * @param      LYNQ_INJECT_TIME_INTO_T *time_info
+ *
+ * @return     
+ */
 
 /*need  set system time in advance*/
 int qser_Gnss_InjectTime(uint32_t h_gnss,LYNQ_INJECT_TIME_INTO_T *time_info)
@@ -126,7 +162,7 @@
     ret = lynq_gnss_send_cmd(h_gnss, send_buf, sizeof(send_buf));
     if(ret < 0)
     {
-        printf("send fail\n");
+        ALOGE("InjectTime send fail\n");
         return -1;
     }
     lynq_gnss_get_aidinfo(h_gnss);
@@ -168,6 +204,15 @@
     return 0;
 }
 
+/**
+ * @brief      qser_Gnss_Set_Frequency
+ *
+ * @details    Change output frequency(just for 106E)
+ *             
+ * @param      frequency:1 2 5hz
+ *
+ * @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};
@@ -204,13 +249,13 @@
     ret = write(gnss_handle->dev_fd, send_buf, buf_lenth);
     if(ret < 0)
     {
-        printf("send faild");
+        ALOGE("modify cmd send faild\n");
         return -1;
     }
     ret = write(gnss_handle->dev_fd, save_configuration, sizeof(save_configuration));
     if(ret < 0)
     {
-        printf("send faild");
+        ALOGE("save configuration send faild\n");
         return -1;
     }
     return 0;
@@ -274,10 +319,10 @@
     ret = lynq_gnss_send_cmd(h_gnss, send_buf, strlen(send_buf));
     if(ret < 0)
     {
-        printf("send faild");
+        ALOGE("Delete_Aiding_Data cmd send faild");
         return -1;
     }
-    printf("reset OK!!!!!\n");
+    ALOGE("reset OK!!!!!\n");
 
     ret = set_baudrate(gnss_handle->dev_fd, B9600);
     if(-1 == ret)
@@ -299,24 +344,90 @@
     return 0;
 }
 
+/**
+ * @brief      qser_Gnss_Server_Configuration
+ *
+ * @details    Configure user information for 106E-AGPS
+ *             
+ * @param      host id password (if the parameter is NULL,use default Configure)
+ *
+ * @return     
+ */
+
+int qser_Gnss_Server_Configuration(char *host, char *id, char *password)
+{
+    if(NULL == login_information_t)
+    {
+        ALOGE("don't have login information\n");
+        return -1;
+    }
+    login_information_t->host = host;
+    login_information_t->id = id;
+    login_information_t->password = password;
+    ALOGD("host:%s\nid:%s\npw:%s\n",login_information_t->host,login_information_t->id,login_information_t->password);
+    return 0;
+}
+
+/**
+ * @brief      qser_Gnss_download_tle
+ *
+ * @details    download_tle 106-AGPS tle,this interface not require init, but it must be able to dial up for internet access
+ *             the download_tle will save in /mnt/userdata/agnss_tle
+ * @param      NULL
+ *
+ * @return     
+ */
+
 int qser_Gnss_download_tle()
 {
     int ret;
-    ret = mopen_gnss_download_tle();
-    if(ret < 0)
+    if(gpio21_value == 0)
     {
-        return -1;
+        ret = mopen_gnss_download_tle();
+        if(ret < 0)
+        {
+            return -1;
+        }
+    }
+    else if(gpio21_value == 1)
+    {
+        ret = hd_gnss_download_tle(login_information_t->host, login_information_t->id, login_information_t->password);
+        if(ret < 0)
+        {
+            return -1;
+        }
     }
     return 0;
 }
 
+/**
+ * @brief      qser_Gnss_injectEphemeris
+ *
+ * @details    inject Ephemeris 
+ *             
+ * @param      
+ *
+ * @return     
+ */
+
 int qser_Gnss_injectEphemeris(uint32_t h_gnss)
 {
     int ret;
-    ret = mopen_gnss_injects_aidpos(h_gnss);
-    if(ret < 0)
+    if(gpio21_value == 0)
     {
-        return -1;
+        ret = mopen_gnss_injects_aidpos(h_gnss);
+        if(ret < 0)
+        {
+            return -1;
+        }
+    }
+    else if(gpio21_value == 1)
+    {
+        ret = hd_gnss_injects_aidpos(h_gnss);
+        if(ret < 0)
+        {
+            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 6cc38c0..862aeea 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
@@ -1028,6 +1028,7 @@
         *ph_gnss = (uint32)mbtk_gnss_handle;
         return 0;
     }
+    
     mbtk_gnss_handle = (mbtk_gnss_handle_t*)malloc(sizeof(struct mbtk_gnss_handle_t));
     if(NULL == mbtk_gnss_handle)
     {
@@ -1386,15 +1387,44 @@
     return 0;
 }
 
-/**********************************
+static int hd_gnss_http_requst(char *host)
+{
+    int http_handle = mbtk_http_handle_get(TRUE, http_data_cb_func);
+    if(http_handle < 0)
+    {
+        printf("mbtk_http_handle_get() fail.");
+        return -1;
+    }
+    int http_session = mbtk_http_session_create(http_handle, HTTP_OPTION_GET, HTTP_VERSION_1_1);
+    if(http_handle < 0)
+    {
+        printf("mbtk_http_session_create() fail.");
+        return -2;
+    }
 
- ID1: TempID1Expire20221031
- Base 64 PW1: RlJYdkFTNE9DWXJhN2ZWTA==
-**************************************/
+    if(mbtk_http_session_url_set(http_handle, http_session, host))
+    {
+        printf("mbtk_http_session_url_set() fail.\n");
+        return -3;
+    }
+    if(mbtk_http_session_start(http_handle, http_session))
+    {
+    printf("mbtk_http_session_start() fail.\n");
+    return -4;
+    }
+    if(mbtk_http_handle_free(http_handle))
+    {
+        printf("mbtk_http_handle_free() fail.");
+        return -5;
+    }
+
+    return 0;
+}
+
 #define AGNSS_CONFIG_FILE  "/data/gnss_update/agps.conf"
 
 /**
- * @brief      mopen_gnss_download_tle
+ * @brief      mopen_gnss_download_tle;hd_gnss_download_tle
  *
  * @details    下载星历数据
  *             (卫星星历,又称为两行轨道数据(TLE,Two-Line Orbital Element))
@@ -1438,6 +1468,21 @@
     return gnss_http_requst(_id, _passwd);
 }
 
+int hd_gnss_download_tle(char *host, char *id, char *password)
+{
+    //id and pw are currently not being used for future access to self built servers
+    const char default_host[] = "http://uagnss.allystar.com/ephemeris/HD_GPS.hdb?compid=yikecs1&token=Z38w5urAuawubTxi";
+    if(host == NULL)
+    {
+        hd_gnss_http_requst(default_host);
+    }
+    else
+    {
+        hd_gnss_http_requst(host);
+    }
+    return 0;
+}
+
 /**
  * @brief      mopen_gnss_injects_aidpos
  *
@@ -1490,6 +1535,58 @@
     return 0;
 }
 
+#define BUFFER_SIZE 1024
+int hd_gnss_injects_aidpos(uint32 h_gnss)
+{
+    int packet_length;
+    int hd_agnss_fd = 0;
+    int size = 0;
+    int left_size = 0;
+    int total_size = 0;
+    char *databuf = (char *)malloc(BUFFER_SIZE);
+    struct mbtk_gnss_handle_t *gnss_handle = (struct mbtk_gnss_handle_t *)h_gnss;
+    hd_agnss_fd = open(AGNSS_TLE_FILE, O_RDWR);
+    if (hd_agnss_fd <= 0)
+    {
+        printf("%s open file FAIL. errno:%d\n", __FUNCTION__, errno);
+        return -1;
+    }
+    while(0 < (size = read(hd_agnss_fd, (databuf + left_size), BUFFER_SIZE)))
+    {
+        total_size = size + left_size;
+        left_size = 0;
+        for(int i=0; i < total_size;)
+        {
+            gnss_log("%s Write[%d]\r\n", __FUNCTION__, size);
+            if((databuf[i] == 0xF1) && (databuf[i + 1] == 0xD9))
+            {
+                packet_length = (databuf[i + 4] | (databuf[i + 5] << 8));
+                if (i + packet_length + 8 <= total_size)
+                {
+                    write(gnss_handle->dev_fd, databuf + i, packet_length + 8);
+                    usleep(5000);
+                    i = i + packet_length + 8;
+                }
+                else
+                {
+                    left_size = total_size - i;
+                    char *tmp = databuf;
+                    databuf = (char *)malloc(BUFFER_SIZE + left_size);
+                    memcpy(databuf, tmp + i ,left_size);
+                    free(tmp);
+                    break;
+                }
+            }
+            else
+                i++;
+        }
+    }
+    free(databuf);
+    databuf = NULL;
+    close(hd_agnss_fd);
+    return 0;
+}
+
 
 /**
  * @brief
@@ -1606,10 +1703,6 @@
     return ret;
 }
 
-
-/*update ephemeris from network*/
-#define AGNSS_TLE_FILE "/mnt/userdata/agnss_tle" //deposit ephemeris
-
 /*Injection ephemeris AGNSS_TLE_FILE*/
 int lynq_gnss_Injection_ephemeris(uint32 h_gnss)
 {