Merge "[Feature][T108][GNSS][task-view-1684]Compatible with HD8040" into GSW_V1453
diff --git a/mbtk/libgsw_lib/gsw_gnss_interface.c b/mbtk/libgsw_lib/gsw_gnss_interface.c
index 00afd57..86b12eb 100755
--- a/mbtk/libgsw_lib/gsw_gnss_interface.c
+++ b/mbtk/libgsw_lib/gsw_gnss_interface.c
@@ -200,7 +200,18 @@
     MODE_END = -1,                              /**< init value */

 }GSW_GNSS_MODE_CONFIGURATION_HD;

 

+typedef enum{

+    NO_DEVICE = 0,

+    HD8122,

+    HD8040D,

+}GSW_DEVICE;

 

+typedef struct {

+    int  fd;

+    GSW_DEVICE got_hd8;

+} rx_ctx;

+

+static GSW_DEVICE gsw_device = NO_DEVICE;

 GSW_GNSS_MODE_CONFIGURATION_HD map_gnss_mode(GSW_GNSS_MODE_CONFIGURATION mode)

 {

     switch (mode)

@@ -234,6 +245,54 @@
     }

 }  

 

+static void *rx_thread(void *arg)

+{

+    rx_ctx *c = (rx_ctx *)arg;

+    char buf[128];

+    ssize_t n;

+    time_t end = time(NULL) + 1;            /* 1 秒超时 */

+

+    while (time(NULL) < end) {

+        n = read(c->fd, buf, sizeof(buf));

+        if (n > 0 && memmem(buf, n, "HD8040D", 7)) {

+            c->got_hd8 = 2;

+            break;

+        }

+		else if (n > 0 && memmem(buf, n, "HD812", 5)) {

+			c->got_hd8 = 1;

+			break;

+		}

+    }

+    return NULL;

+}

+

+static int send_and_wait(int fd)

+{

+    int ret;

+    pthread_t tid;

+    rx_ctx ctx = { .fd = fd, .got_hd8 = NO_DEVICE };

+

+    pthread_create(&tid, NULL, rx_thread, &ctx);

+

+    unsigned char tx[8] = {0xF1,0xD9,0x0A,0x04,0x00,0x00,0x0E,0x34};

+    ret = write(fd, tx, sizeof(tx));

+    if(ret < 0)

+    {

+        LOGE("[GSW_gnss] send_and_wait write fail.ret = [%d]", ret);

+    }

+    pthread_join(tid, NULL);

+    return ctx.got_hd8;

+}

+

+static int get_gnss_device_version(void)

+{

+    int fd = open("/dev/ttyS3", O_RDWR | O_NOCTTY);

+    if (fd < 0) { perror("open"); return 1; }

+    gsw_device = send_and_wait(fd);

+    close(fd);

+    return gsw_device;

+}

+

 int mbtk_gnss_set_VTG()

 {

     int ret;

@@ -246,6 +305,33 @@
     return GSW_HAL_SUCCESS;

 }

 

+static int HD8040D_gsw_gnss_set_freq(int freq)

+{

+    int ret;

+    char cmd[64] = {0};

+    switch (freq)

+    {

+        case 1:

+            snprintf(cmd, sizeof(cmd), "/usr/sbin/gnss_gpio.sh %s > /dev/null 2>&1", "1HZ");

+            break;

+        case 5:

+            snprintf(cmd, sizeof(cmd), "/usr/sbin/gnss_gpio.sh %s > /dev/null 2>&1", "5HZ");

+            break;

+        case 10:

+            snprintf(cmd, sizeof(cmd), "/usr/sbin/gnss_gpio.sh %s > /dev/null 2>&1", "10HZ");

+            break;

+        default:

+            return GSW_HAL_NORMAL_FAIL;

+            break;

+    }

+    ret = system(cmd);

+    if(ret != 0)

+    {

+        LOGE("[GSW_gnss] gnss_gpio.sh GSW_HD8040D mode fail.ret = [%d]", ret);

+        return GSW_HAL_NORMAL_FAIL;

+    }

+    return ret;

+}

 /**

  * @brief SDK interface to set gnss sampling frequency, support 1Hz、2Hz、5Hz

  * @param  [in] freq

@@ -263,6 +349,16 @@
         gnss_freq = freq;

         return GSW_HAL_SUCCESS;

     }

+    if(gsw_device == NO_DEVICE)

+    {

+        ret = get_gnss_device_version();

+        LOGD("[GSW_gnss] get_gnss_device_version ret = [%d]", ret);

+    }

+    if(gsw_device == HD8040D)

+    {

+        ret = HD8040D_gsw_gnss_set_freq(freq);

+        return ret;

+    }

     char param_buf[32] = {0};

     snprintf(param_buf, 32, "$FREQCFG,%d", freq);

     mbtk_gnss_setting=(int(*)(const char *setting_cmd, int))dlsym(dlHandle_gnss, "mbtk_gnss_setting");

@@ -275,6 +371,61 @@
     return GSW_HAL_SUCCESS;

 }

 

+static int32_t HD8040D_gsw_gnss_set_start_mode(GSW_GNSS_MODE_CONFIGURATION start_mode)

+{

+    int ret;

+    char cmd[64] = {0};

+    switch (start_mode)

+    {

+    case GSW_MODE_GPS_GLONASS:

+        return GSW_HAL_NORMAL_FAIL;

+        break;

+    case GSW_MODE_GPS_BEIDOU:

+        snprintf(cmd, sizeof(cmd), "/usr/sbin/gnss_gpio.sh %s > /dev/null 2>&1", "GSW_GPS_BD_DUAL");

+        break;

+    case GSW_MODE_GPS_GLONASS_BEIDOU:

+        return GSW_HAL_NORMAL_FAIL;

+        break;

+    case GSW_MODE_GPS:

+        snprintf(cmd, sizeof(cmd), "/usr/sbin/gnss_gpio.sh %s > /dev/null 2>&1", "GSW_GPS_L1L5");

+        break;

+    case GSW_MODE_BEIDOU:

+        snprintf(cmd, sizeof(cmd), "/usr/sbin/gnss_gpio.sh %s > /dev/null 2>&1", "GSW_BD_B1I_B1C_B2A");

+        break;

+    case GSW_MODE_GLONASS:

+        snprintf(cmd, sizeof(cmd), "/usr/sbin/gnss_gpio.sh %s > /dev/null 2>&1", "GSW_GALILEO_E1_E5A");

+        break;

+    case GSW_MODE_GPS_GLONASS_BEIDOU_GALILEO:

+        return GSW_HAL_NORMAL_FAIL;

+        break;

+    case GSW_MODE_GPS_GALILEO:

+        return GSW_HAL_NORMAL_FAIL;

+        break;

+    case GSW_MODE_GPS_GLONASS_GALILEO:

+        return GSW_HAL_NORMAL_FAIL;

+        break;

+    case GSW_MODE_GPS_GALILEO_ONLY:

+        return GSW_HAL_NORMAL_FAIL;

+        break;

+    case GSW_MODE_GPS_GLONASS_BEIDOU_GALILEO_NAVIC:

+        return GSW_HAL_NORMAL_FAIL;

+        break;

+    case GSW_MODE_GNSS_END:

+        return GSW_HAL_NORMAL_FAIL;

+        break;

+    default:

+        return GSW_HAL_NORMAL_FAIL;

+        break;

+    }

+    ret = system(cmd);

+    if(ret != 0)

+    {

+        LOGE("[GSW_gnss] gnss_gpio.sh GSW_HD8040D mode fail.ret = [%d]", ret);

+        return GSW_HAL_NORMAL_FAIL;

+    }

+    return ret;

+}

+

 /**

  * @brief SDK interface to set gnss start mode,specific mode refreence GSW_HAL_GNSS_MODE_CONFIGURATION

  * @param  [in] start_mode 

@@ -291,6 +442,16 @@
         gnss_startmode = start_mode;

         return GSW_HAL_SUCCESS;

     }

+    if(gsw_device == NO_DEVICE)

+    {

+        ret = get_gnss_device_version();

+        LOGD("[GSW_gnss] get_gnss_device_version ret = [%d]", ret);

+    }

+    if(gsw_device == HD8040D)

+    {

+        ret = HD8040D_gsw_gnss_set_start_mode(start_mode);

+        return ret;

+    }

     char param_buf[32] = {0};

     snprintf(param_buf, 32, "$SYSCFG,%d", map_gnss_mode(start_mode));

     if(map_gnss_mode(start_mode) == -1)