Add hd8122 download API.

Change-Id: Id631c2fbde1364240114e5527f85a4cf1d0987b3
diff --git a/mbtk/mbtk_gnssd/Makefile b/mbtk/mbtk_gnssd/Makefile
index 79f937a..683a48e 100755
--- a/mbtk/mbtk_gnssd/Makefile
+++ b/mbtk/mbtk_gnssd/Makefile
@@ -5,6 +5,7 @@
 
 INC_DIR += \
 		-I$(LOCAL_PATH) \
+		-I$(LOCAL_PATH)/hd8122_dl \
 		-I$(BUILD_ROOT)/libmbtk_ril
 
 LIB_DIR +=
@@ -24,7 +25,14 @@
 #MY_FILES_PATH += $(LOCAL_PATH)/platform/linux
 #endif
 #LOCAL_SRC_FILES = $(wildcard *.c) $(wildcard *.cpp)
-LOCAL_SRC_FILES = gnss_main.c gnss_nmea.c gnss_6228.c gnss_utils.c gnss_hd8122.c gnss_asr5311.c
+LOCAL_SRC_FILES = 	gnss_main.c \
+					gnss_nmea.c \
+					gnss_6228.c \
+					gnss_utils.c \
+					gnss_hd8122.c \
+					gnss_asr5311.c \
+					hd8122_dl/port.c \
+					hd8122_dl/fwup.c
 
 # Enable ubus
 #LOCAL_SRC_FILES += gnss_ubus.c
diff --git a/mbtk/mbtk_gnssd/gnss_6228.c b/mbtk/mbtk_gnssd/gnss_6228.c
index 0cc248c..04cfc18 100755
--- a/mbtk/mbtk_gnssd/gnss_6228.c
+++ b/mbtk/mbtk_gnssd/gnss_6228.c
Binary files differ
diff --git a/mbtk/mbtk_gnssd/gnss_asr5311.c b/mbtk/mbtk_gnssd/gnss_asr5311.c
index 654fcdc..069e53a 100755
--- a/mbtk/mbtk_gnssd/gnss_asr5311.c
+++ b/mbtk/mbtk_gnssd/gnss_asr5311.c
@@ -18,10 +18,12 @@
 #include <unistd.h>
 #include <errno.h>
 #include <fcntl.h>
+#include <pthread.h>
 
 #include "mbtk_log.h"
 #include "mbtk_type.h"
 #include "mbtk_gpio.h"
+#include "mbtk_utils.h"
 #include "gnss_utils.h"
 #include "gnss_asr5311.h"
 
diff --git a/mbtk/mbtk_gnssd/gnss_hd8122.c b/mbtk/mbtk_gnssd/gnss_hd8122.c
index 5ee816c..58048f1 100755
--- a/mbtk/mbtk_gnssd/gnss_hd8122.c
+++ b/mbtk/mbtk_gnssd/gnss_hd8122.c
@@ -18,10 +18,12 @@
 #include <unistd.h>
 #include <errno.h>
 #include <fcntl.h>
+#include <pthread.h>
 
 #include "mbtk_log.h"
 #include "mbtk_type.h"
 #include "mbtk_gpio.h"
+#include "mbtk_utils.h"
 #include "gnss_utils.h"
 #include "gnss_hd8122.h"
 
@@ -68,7 +70,7 @@
 
 static void msg_init()
 {
-    int i = 0;
+    unsigned int i = 0;
     while(i < ARRAY_SIZE(msg_array)) {
         msg_array[i].enable = FALSE;
         i++;
@@ -77,7 +79,7 @@
 
 static int msg_insert(uint8 gid, uint8 sid)
 {
-    int i = 0;
+    unsigned int i = 0;
     while(i < ARRAY_SIZE(msg_array)) {
         if(!msg_array[i].enable)
             break;
@@ -97,7 +99,7 @@
 
 static int msg_find(uint8 gid, uint8 sid)
 {
-    int i = 0;
+    unsigned int i = 0;
     while(i < ARRAY_SIZE(msg_array)) {
         if(msg_array[i].enable && gid == msg_array[i].gid && sid == msg_array[i].sid)
             break;
@@ -127,7 +129,7 @@
 
 static int msg_count()
 {
-    int i = 0;
+    unsigned int i = 0;
     int count = 0;
     while(i < ARRAY_SIZE(msg_array)) {
         if(msg_array[i].enable)
@@ -168,7 +170,7 @@
     int count = pack_len / 10;
     int i = 0;
     while(i < count) {
-        uint8 *ptr = pack + i * 10;
+        const uint8 *ptr = pack + i * 10;
         if(ptr[0] != 0xf1 || ptr[1] != 0xd9) {
             LOGE("Pack head error : %02x %02x", ptr[0], ptr[1]);
             return -1;
diff --git a/mbtk/mbtk_gnssd/gnss_main.c b/mbtk/mbtk_gnssd/gnss_main.c
index 3f887f1..9510f3d 100755
--- a/mbtk/mbtk_gnssd/gnss_main.c
+++ b/mbtk/mbtk_gnssd/gnss_main.c
@@ -8,11 +8,12 @@
 #include <libubus.h>
 #include <signal.h>
 #include <cutils/properties.h>
+#include <ctype.h>
 
 #include "mbtk_type.h"
 #include "mbtk_log.h"
 #include "gnss_info.h"
-
+#include "gnss_utils.h"
 #include "gnss_6228.h"
 #include "gnss_hd8122.h"
 #include "gnss_asr5311.h"
@@ -511,10 +512,10 @@
     return count;
 }
 #else
-int gnss_write(int fd, const void* buf, unsigned int buf_len)
+int gnss_write(int fd, const void* buf, int buf_len)
 {
-    size_t size;
-    size_t size_to_wr;
+    ssize_t size;
+    ssize_t size_to_wr;
     ssize_t size_written;
     if(GNSS_BUFF_SIZE < buf_len)
     {
@@ -526,7 +527,7 @@
         if( size_to_wr > GNSS_BUFF_SIZE)
             size_to_wr = GNSS_BUFF_SIZE;
 
-        size_written = write(fd, &buf[size], size_to_wr);
+        size_written = write(fd, (const uint8*)buf + size, size_to_wr);
         if (size_written==-1)
         {
             return -1;
diff --git a/mbtk/mbtk_gnssd/gnss_utils.c b/mbtk/mbtk_gnssd/gnss_utils.c
index e0fa746..6f5ad6a 100755
--- a/mbtk/mbtk_gnssd/gnss_utils.c
+++ b/mbtk/mbtk_gnssd/gnss_utils.c
@@ -6,6 +6,8 @@
 #include <termios.h>
 #include <string.h>
 #include <stdarg.h>
+#include <pty.h>
+#include <libubox/uloop.h>
 
 #include "mbtk_type.h"
 #include "mbtk_log.h"
diff --git a/mbtk/mbtk_gnssd/hd8122_dl/custom_upgrade_api.c b/mbtk/mbtk_gnssd/hd8122_dl/custom_upgrade_api.c
new file mode 100755
index 0000000..9bd674d
--- /dev/null
+++ b/mbtk/mbtk_gnssd/hd8122_dl/custom_upgrade_api.c
@@ -0,0 +1,110 @@
+#ifndef _CUSTOM_UPGRADE_H_

+#define  _CUSTOM_UPGRADE_H_

+//#include "hdbd_types.h"

+#include "stdint.h"

+#include "port.h"

+

+extern uint8_t g_bin_buff[500*1024];

+

+void gnss_gpio_init()

+{

+	// init reset, prtrg, uart etc .

+	//......

+

+	//......

+}

+

+void gnss_enter_boot(int32_t fd)

+{

+	//entern boot mode

+	/*......

+	// demo

+	hd_delayMs(200);

+	GNSS_PRTRG_LOW();

+	hd_delayMs(1000);

+	GNSS_RESET_LOW(1000);

+	hd_delayMs(1000);

+	GNSS_RESET_HIGH();

+	hd_delayMs(1000);

+	GNSS_PRTRG_HIGH();

+	hd_delayMs();

+	......*/

+

+}

+

+void gnss_reset()

+{

+	//gnss chip reset

+	/*......

+	// demo

+	GNSS_RESET_LOW();

+	hd_delayMs(300);

+	GNSS_RESET_HIGH();

+	......*/

+}

+

+int32_t set_gnss_baud(int32_t fd, uint8_t cmd_baud)

+{

+    tcflush(fd, TCIOFLUSH);

+    if(cmd_baud == 1)

+	{

+		uart_init(fd, 460800, 0, 8, 1, 'N');

+    }

+    else if(cmd_baud == 2)

+	{

+		uart_init(fd, 115200, 0, 8, 1, 'N');

+    }

+    else if(cmd_baud == 3)

+	{

+		uart_init(fd, 9600, 0, 8, 1, 'N');

+    }

+	else

+	{

+		uart_init(fd, 115200, 0, 8, 1, 'N');

+	}

+    tcflush(fd, TCIOFLUSH);

+

+	return 0;

+}

+

+extern int uart_send(int32_t fd, uint8_t *cmd, int cmd_len);

+

+extern int uart_recv(int32_t fd, uint8_t *buf, int *actual_length, int timeout);

+

+int bsp_flash_readbytes(uint32_t addr, uint8_t *pbuf, uint32_t len)

+{

+	if( (pbuf == NULL) || (len == 0) )

+	{

+        printf("[%s %d] input info error!", __FUNCTION__, __LINE__);

+		return -1;

+	}

+	memcpy(pbuf, addr+g_bin_buff, len);

+

+	return 0;

+}

+

+void uart_clear_buf(int32_t fd)

+{

+    tcflush(fd, TCIFLUSH);

+	//clearBuffer(fd, TCIOFLUSH);

+	return;

+}

+

+int uart_control(int32_t fd, int type)

+{

+	// uart authority control

+

+	//......

+

+	//......

+

+	return 0;

+}

+

+void hd_delayMs(uint32_t ms)

+{

+	usleep(ms*1000);

+	return;

+}

+

+#endif

diff --git a/mbtk/mbtk_gnssd/hd8122_dl/custom_upgrade_api.h b/mbtk/mbtk_gnssd/hd8122_dl/custom_upgrade_api.h
new file mode 100755
index 0000000..59b2cc8
--- /dev/null
+++ b/mbtk/mbtk_gnssd/hd8122_dl/custom_upgrade_api.h
@@ -0,0 +1,28 @@
+#ifndef _CUSTOM_UPGRADE_H_

+#define  _CUSTOM_UPGRADE_H_

+#include "hd8040_upgrade.h"

+#include "stdint.h"

+

+

+

+extern void gnss_gpio_init();

+

+extern void gnss_enter_boot(int32_t fd);

+

+extern void gnss_reset();

+

+extern int32_t set_gnss_baud(int32_t fd, uint8_t cmd_baud);

+

+extern int uart_send(int32_t fd, uint8_t *cmd, int cmd_len);

+

+extern int uart_recv(int32_t fd, uint8_t *buf, int *actual_length, int timeout);

+

+extern int bsp_flash_readbytes(uint32_t addr, uint8_t *pbuf, uint32_t len) ; 	

+

+extern void uart_clear_buf(int32_t fd);

+

+extern int uart_control(int32_t fd, int type);

+

+extern void hd_delayMs(uint32_t ms);

+

+#endif

diff --git a/mbtk/mbtk_gnssd/hd8122_dl/fwup.c b/mbtk/mbtk_gnssd/hd8122_dl/fwup.c
new file mode 100755
index 0000000..16b30a5
--- /dev/null
+++ b/mbtk/mbtk_gnssd/hd8122_dl/fwup.c
@@ -0,0 +1,472 @@
+/*!

+ *****************************************************************************

+ **

+ ** \file        fwup.c

+ ** \brief       firmware update function

+ **

+ ** \attention   This sample code is provided "as is" without warranty of any

+ **              kind. We disclaim all warranties, either express or implied,

+ **              including the warranties of merchantability and fitness for a

+ **              particular purpose.

+ **

+ **

+ *****************************************************************************/

+//#include "hd8040_upgrade.h"

+#include "port.h"

+

+#define READ_LENGTH 128

+#define VERSION_MAX_LEN 16

+

+const uint8_t cmdMONVER[] = {0xf1, 0xd9, 0x0a, 0x04, 0x00, 0x00, 0x0e, 0x34};

+const uint8_t cmdSETFRQ[] = {0xf1, 0xd9, 0xf4, 0x00, 0x04, 0x00, 0x80, 0xba, 0x8c, 0x01, 0xbf, 0xff};

+uint8_t cmdSETFRQ_ONLY[] = {0xF1, 0xD9, 0xF4, 0x03, 0x08, 0x00, 0x00, 0x08, 0x07, 0x00, 0x00, 0x08, 0x07, 0x00, 0x1D, 0x69};

+const uint8_t cmdCFGFWUP[] = {0xf1, 0xd9, 0x06, 0x50, 0x01, 0x00, 0x10, 0x67, 0x71, 0x00};

+uint8_t cmdOpenWrtPro[] = 	{0xF1, 0xD9, 0xF4, 0x08, 0x04, 0x00, 0x00, 0x02, 0x00, 0x80, 0x82, 0x76};

+uint8_t cmdOpenQe_and_wrtPro[] = {0xF1, 0xD9, 0xF4, 0x13, 0x05, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x0E, 0x55};

+const uint8_t cmdBOOTERASE[] = {0xf1, 0xd9, 0xf4, 0x05, 0x06, 0x00, 0x00, 0x00, 0x00, 0x90, 0x00, 0x00, 0x8f, 0x95};

+uint8_t cmdBOOTERASE_100[] = {0xf1, 0xd9, 0xf4, 0x05, 0x06, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x0f, 0x25};

+const uint8_t cmdCFGRST[] = {0xf1, 0xd9, 0x06, 0x40, 0x01, 0x00, 0x00, 0x47, 0x21};

+uint8_t cmdBOOTERASE_900[] = {0xf1, 0xd9, 0xf4, 0x05, 0x06, 0x00, 0x00, 0x00, 0x00, 0x90, 0x00, 0x00, 0x8f, 0x95};

+uint8_t cmd_RadFlash[] = {0xF1, 0xD9, 0xF4, 0x07, 0x01, 0x00, 0x02, 0xFE, 0xE5};

+

+const uint8_t cmdBOOTBAUD[] = {0xf1, 0xd9, 0xf4, 0x03, 0x08, 0x00, 0x00, 0xc2, 0x01, 0x00, 0x00, 0xc2, 0x01, 0x00, 0x85, 0x7d};

+uint8_t cmdNMEAOFF[] = {

+0xf1, 0xd9, 0x06, 0x01, 0x03, 0x00, 0xf0, 0x00, 0x00, 0xfa, 0x0f,

+0xf1, 0xd9, 0x06, 0x01, 0x03, 0x00, 0xf0, 0x02, 0x00, 0xfc, 0x13,

+0xf1, 0xd9, 0x06, 0x01, 0x03, 0x00, 0xf0, 0x03, 0x00, 0xfd, 0x15,

+0xf1, 0xd9, 0x06, 0x01, 0x03, 0x00, 0xf0, 0x04, 0x00, 0xfe, 0x17,

+0xf1, 0xd9, 0x06, 0x01, 0x03, 0x00, 0xf0, 0x05, 0x00, 0xff, 0x19,

+0xf1, 0xd9, 0x06, 0x01, 0x03, 0x00, 0xf0, 0x06, 0x00, 0x00, 0x1b,

+0xf1, 0xd9, 0x06, 0x01, 0x03, 0x00, 0xf0, 0x07, 0x00, 0x01, 0x1d

+};

+

+void uart_clear_buf(int32_t fd)

+{

+    tcflush(fd, TCIFLUSH);

+	//clearBuffer(fd, TCIOFLUSH);

+	return;

+}

+

+static int

+byte_array_to_hex_string(char *pResult, uint8_t const * pData, int dataLen)

+{

+    int strLen = 0;

+    int res = 0;

+    char * pMsg = pResult;

+    int i = 0;

+

+    if(!pData || !pResult || dataLen <= 0)

+    {

+        HDBD_LOG("[%s %d]Para err, return -1\n", __FUNCTION__, __LINE__);

+        return -1;

+    }

+

+    for(i = 0; i < dataLen; ++i)

+    {

+        /*  The first Hex value does not need a leading '-' */

+        if(i != 0)

+        {

+            res = snprintf(pMsg + strLen, 2, "-");

+            /* increase the total strLength */

+            if(res >= 0)

+            {

+                strLen += res;

+            }

+            else

+            {

+                HDBD_LOG("[%s %d]add leading Error, return -1\n", __FUNCTION__, __LINE__);

+                return -1;

+            }

+        }

+

+        /*  Print the hex character to the string */

+        res = snprintf(pMsg + strLen, 3, "%02X", pData[i]);

+        if(res >= 0)

+        {

+            strLen += res;

+        }

+        else

+        {

+            HDBD_LOG("[%s %d]Print the hex character to the string Error, return -1\n", __FUNCTION__, __LINE__);

+            return -1;

+        }

+    }

+

+    return strLen;

+}

+

+

+/**

+  * @brief  send cmd to chip wait result

+  * @param  uart_fd : uart fd

+  * @param  cmd : send data command

+  * @param  recv_buf : send result

+  * @param  send_length: send cmd length

+  * @param  recv_length : result length

+  * @param  timeout : wait result time

+  * @retval SUCCESS/FAIL

+  */

+static int

+send_cmd_and_recv_result(int uart_fd, uint8_t *cmd, uint8_t *recv_buf, int send_length, int *recv_length, int timeout)

+{

+    int ret = -1;

+

+    ret = uart_send(uart_fd, cmd, send_length);

+    if(ret == -1)

+    {

+        HDBD_LOG("[%s %d] uart send failed\n", __FUNCTION__, __LINE__);

+        return  ret;

+    }

+

+    ret = uart_recv(uart_fd, recv_buf, recv_length, timeout);

+    if(ret == -1)

+    {

+        HDBD_LOG("[%s %d] uart recv failed\n", __FUNCTION__, __LINE__);

+        return  ret;

+    }

+

+    return 0;

+}

+

+/**

+  * @brief  Boot mode send one block data of firmware

+  * @param  addr: address of the data in HD8030 chip memory

+  * @param  cnt : the number of the block

+  * @param  data: the pointer of the block data

+  * @param  len : the length of data

+  * @retval None

+  */

+int sendfwboot(int uart_fd, uint32_t addr, uint16_t cnt, uint8_t *data, uint32_t len, uint8_t *recv_buf)

+{

+    uint8_t cmd[] = {0xf1, 0xd9, 0xf4, 0x05, 0x06, 0x08, 0x00, 0x08, 0x00, 0x00, 0x01, 0x00};

+	uint8_t ck[2] = { 0 };

+    uint32_t i = 0;

+	uint32_t ck1 = 0, ck2 = 0;

+    int actual_length = 10;//READ_LENGTH-1;

+    int time_out = 3; /* 1 second */

+

+	*((uint16_t *)(cmd + 4)) = len + 6;

+	*((uint32_t *)(cmd + 6)) = addr;

+	*((uint16_t *)(cmd + 10)) = cnt;

+	//check sum

+	for(i = 0; i < 12; ++i)

+	{

+		if(i >= 2)

+		{

+			ck1 += cmd[i];

+			ck2 += ck1;

+		}

+	}

+	for(i = 0; i < len; ++i)

+	{

+		ck1 += data[i];

+		ck2 += ck1;

+	}

+	ck[0] = ck1;

+	ck[1] = ck2;

+

+	uart_send(uart_fd, (uint8_t *)cmd, 12);

+	uart_send(uart_fd, (uint8_t *)data, len);

+	//uart_send((uint8_t *)ck, 2);

+    send_cmd_and_recv_result(uart_fd, (uint8_t *)ck, recv_buf, 2, &actual_length, time_out);

+    HDBD_LOG("[%s %d]boot up: cnt is <%d>, recv [%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x]\n", __FUNCTION__, __LINE__, cnt, recv_buf[0], recv_buf[1], recv_buf[2], recv_buf[3],recv_buf[4], recv_buf[5], recv_buf[6], recv_buf[7], recv_buf[8], recv_buf[9]);

+	if( (recv_buf[0] == 0xF1) && (recv_buf[1] == 0xD9) && (recv_buf[2] == 0x05) && (recv_buf[3] == 0x01) )

+	{

+		return 0;

+	}

+

+	return -1;

+}

+

+

+

+

+/**

+  * @brief  get version from chip,if user do not save version, so get_mon_ver(NULL, 0)

+  * @param  ver_buf:save version buffer

+  * @param  ver_len:length of version buffer

+  * @retval SUCCESS/FAIL

+  */

+int get_mon_ver(int uart_fd,uint8_t *ver_buf, int ver_len)

+{

+    int ret = -1;

+    int actual_length = 40;

+    uint8_t cur_sw_ver[VERSION_MAX_LEN + 1] = {0};

+    uint8_t cur_hw_ver[VERSION_MAX_LEN + 1] = {0};

+    uint8_t recv_buf[READ_LENGTH] = {0};

+    //uint8_t *p1 = recv_buf;

+	uint16_t f1StarAddr = 0;

+	uint8_t read_count = 0;

+    int time_out = 3; /* 1 second */

+    char str_hex[512] = {0};

+

+    if(NULL == ver_buf || ver_len < 41)

+    {

+        HDBD_LOG("[%s %d]Para mybe error, FAIL\n", __FUNCTION__, __LINE__);

+        return -1;

+    }

+

+    memset(recv_buf, 0x0, sizeof(recv_buf));

+    memset(cur_sw_ver, 0x0, VERSION_MAX_LEN);

+    memset(cur_hw_ver, 0x0, VERSION_MAX_LEN);

+

+	HDBD_LOG("[%s %d] enter\n", __FUNCTION__, __LINE__);

+    //ret = send_cmd_and_recv_result(uart_fd,cmdMONVER, recv_buf, sizeof(cmdMONVER), &actual_length, time_out);

+    ret = uart_send(uart_fd, cmdMONVER, sizeof(cmdMONVER));

+    if(ret == -1)

+    {

+        HDBD_LOG("[%s %d] uart send failed\n", __FUNCTION__, __LINE__);

+        return  ret;

+    }

+

+	read_count = 1;

+	while(1)

+	{

+		if((read_count % 10) == 0)

+		{

+			ret = uart_send(uart_fd, cmdMONVER, sizeof(cmdMONVER));

+			if(ret == -1)

+			{

+				HDBD_LOG("[%s %d] uart send failed\n", __FUNCTION__, __LINE__);

+				return	ret;

+			}

+		}

+

+	    ret = uart_recv(uart_fd, recv_buf, &actual_length, time_out);

+	    if(ret == -1)

+	    {

+	        HDBD_LOG("[%s %d] uart recv failed\n", __FUNCTION__, __LINE__);

+	        return  ret;

+	    }

+

+		f1StarAddr = 0;

+		HDBD_LOG("[%s %d] actual_length:%d, read_count:%d\n", __FUNCTION__, __LINE__, actual_length, read_count);

+		memset(str_hex, 0x0, sizeof(str_hex));

+		byte_array_to_hex_string(str_hex, recv_buf+f1StarAddr, actual_length);

+		HDBD_LOG("[%s %d]str_hex : %s\n", __FUNCTION__, __LINE__, str_hex);

+		while(f1StarAddr < (actual_length-16))

+		{

+			if( (recv_buf[f1StarAddr] == 0xF1) && (recv_buf[f1StarAddr+1] == 0xD9) )

+			{

+				goto READ_VER;

+			}

+

+			f1StarAddr ++;

+		}

+

+		read_count ++;

+		if(read_count >= 25)

+		{

+	        return  -1;

+		}

+

+	}

+

+READ_VER:

+	if(f1StarAddr >= (actual_length-16))

+	{

+		HDBD_LOG("[%s %d] get ver fail\n", __FUNCTION__, __LINE__);

+		return -1;

+	}

+

+	//memset(str_hex, 0x0, sizeof(str_hex));

+    //byte_array_to_hex_string(str_hex, recv_buf+f1StarAddr, actual_length);

+    //ALOGI("[%s %d]Version : %s\n", __FUNCTION__, __LINE__, str_hex);

+    memcpy(cur_sw_ver, recv_buf + f1StarAddr + 6, VERSION_MAX_LEN);

+    memcpy(cur_hw_ver, recv_buf + f1StarAddr + 6 + VERSION_MAX_LEN, VERSION_MAX_LEN);

+    HDBD_LOG("[%s %d]=====> sw_ver:%s\n", __FUNCTION__, __LINE__, cur_sw_ver);

+    HDBD_LOG("[%s %d]=====> hw_ver:%s\n", __FUNCTION__, __LINE__, cur_hw_ver);

+

+    if ( (strncmp((char *)(recv_buf + f1StarAddr + 6), "FB03BOOT", 8) != 0)

+		&& (strncmp((char *)(recv_buf + f1StarAddr + 6), "FB02BOOT", 8) != 0) )

+    {

+        HDBD_LOG("[%s %d] user mode\n", __FUNCTION__, __LINE__);

+

+        if(strncmp((char *)(recv_buf + f1StarAddr + 6 + VERSION_MAX_LEN), "HD", 2) == 0)

+		{

+			HDBD_LOG("[%s %d] get user ver sucess\n", __FUNCTION__, __LINE__);

+		}

+		else

+		{

+			HDBD_LOG("[%s %d] get user ver fail\n", __FUNCTION__, __LINE__);

+			return -1;

+		}

+    }

+	else

+    {

+        HDBD_LOG("[%s %d]boot_mode\n", __FUNCTION__, __LINE__);

+    }

+

+    /* COPY version to user, the length of recv_buf is more than 40 bytes */

+    if (ver_buf != NULL && (ver_len > 16))

+    {

+        memcpy(ver_buf, cur_sw_ver, 16);

+    }

+

+    return 0;

+}

+

+

+/**

+  * @brief  Boot mode firmware update

+  * @param  data: the pointer of the firmware data

+  * @param  len : the length of firmware data

+  * @retval None

+  */

+int fw_update_boot(int uart_fd, uint8_t *data, uint32_t len)

+{

+	int i = 1;

+	uint8_t dataAck[10];

+	//1st 1KB send at last

+	uint8_t *data1st1k = data;

+    uint8_t recvBuf[READ_LENGTH] = {0};

+	uint8_t errorCount = 0;

+	int32_t ret = 0;

+	int j = 0;

+    if (NULL == data )

+    {

+        HDBD_LOG("[%s %d]firmware data error\n", __FUNCTION__, __LINE__);

+        return -1;

+    }

+

+	uart_clear_buf(uart_fd);

+	errorCount = 0;

+	while(1)

+	{

+		ret = get_mon_ver(uart_fd, (uint8_t *)recvBuf, READ_LENGTH - 1);

+		errorCount ++;

+		if (ret != 0)

+		{

+			HDBD_LOG("[%s %d]boot: Get version FAIL in baudrate 460800, return\n", __FUNCTION__, __LINE__);

+			if(errorCount >= 3)

+			{

+				HDBD_LOG("[%s %d] enter boot failed!\n", __FUNCTION__, __LINE__);

+				return -1;

+			}

+		}

+		else

+		{

+			break;

+		}

+	}

+

+	i = 0;

+	while(i < 15)

+	{

+		if( (recvBuf[i] == 'F') && (recvBuf[i+1] == 'B') && (recvBuf[i+2] == '0') && (recvBuf[i+3] == '3'))

+		{

+			goto BOOT;

+		}

+		HDBD_LOG("[%s %d] enter boot failed!\n", __FUNCTION__, __LINE__);

+		return -1;

+	}

+

+BOOT:

+	ret = uart_send(uart_fd, (uint8_t *)cmdSETFRQ, sizeof(cmdSETFRQ));

+	usleep(50000);

+

+	uart_clear_buf(uart_fd);

+	memset(recvBuf, 0x00, sizeof(recvBuf));

+	i = 10;

+	ret = send_cmd_and_recv_result(uart_fd,cmdOpenWrtPro, recvBuf, sizeof(cmdOpenWrtPro), &i, 3);

+	HDBD_LOG("[%s %d]ret:%d,recvBuf(%d)(%02x,%x,%x,%x,%x,%x,%x,%x,%x,%x)\n", __FUNCTION__, __LINE__, ret, i,

+		recvBuf[0],recvBuf[1],recvBuf[2],recvBuf[3],recvBuf[4],recvBuf[5],

+		recvBuf[6],recvBuf[7],recvBuf[8],recvBuf[9]);

+	if( (recvBuf[8] != 0x04) || (recvBuf[9] != 0x1b))

+	{

+		HDBD_LOG("[%s %d] upgrade write:open flash write protect failed!\n", __FUNCTION__, __LINE__);

+		return -1;

+	}

+

+

+	i = 10;

+	ret = send_cmd_and_recv_result(uart_fd, cmdOpenQe_and_wrtPro, recvBuf, sizeof(cmdOpenQe_and_wrtPro), &i, 3);

+	HDBD_LOG("[%s %d]ret:%d,cmdOpenQe_and_wrtPro(%d)(%02x,%x,%x,%x,%x,%x,%x,%x,%x,%x)\n", __FUNCTION__, __LINE__, ret, i,

+		recvBuf[0],recvBuf[1],recvBuf[2],recvBuf[3],recvBuf[4],recvBuf[5],

+		recvBuf[6],recvBuf[7],recvBuf[8],recvBuf[9]);

+	if((recvBuf[2] == 0x05) && (recvBuf[3] == 0x00))

+	{

+

+		i = 10;

+		ret = send_cmd_and_recv_result(uart_fd, cmd_RadFlash, recvBuf, sizeof(cmd_RadFlash), &i, 3);

+		HDBD_LOG("[%s %d]ret:%d,cmd_RadFlash(%d)(%02x,%x,%x,%x,%x,%x,%x,%x,%x,%x)\n", __FUNCTION__, __LINE__, ret, i,

+			recvBuf[0],recvBuf[1],recvBuf[2],recvBuf[3],recvBuf[4],recvBuf[5],

+			recvBuf[6],recvBuf[7],recvBuf[8],recvBuf[9]);

+		if((recvBuf[2] != 0xF4) || (recvBuf[3] != 0x07))

+		{

+			HDBD_LOG("[%s %d] cmd_RadFlash error!\n", __FUNCTION__, __LINE__);

+			return -1;

+		}

+

+		i = 10;

+		ret = send_cmd_and_recv_result(uart_fd, cmdOpenQe_and_wrtPro, recvBuf, sizeof(cmdOpenQe_and_wrtPro), &i, 3);

+		HDBD_LOG("[%s %d]ret:%d,cmdOpenQe_and_wrtPro(%d)(%02x,%x,%x,%x,%x,%x,%x,%x,%x,%x)\n", __FUNCTION__, __LINE__, ret, i,

+			recvBuf[0],recvBuf[1],recvBuf[2],recvBuf[3],recvBuf[4],recvBuf[5],

+			recvBuf[6],recvBuf[7],recvBuf[8],recvBuf[9]);

+		j = 0;

+		while(j < i)

+		{

+			if((recvBuf[j] == 0xF1) && (recvBuf[j+1] == 0xD9))

+			{

+				if((recvBuf[j+2] != 0x05) || (recvBuf[j+3] != 0x01))

+				{

+					HDBD_LOG("[%s %d] cmdOpenQe_and_wrtPro error!\n", __FUNCTION__, __LINE__);

+					return -1;

+				}

+			}

+

+			j++;

+

+			if(j > 20)

+			{

+				break;

+			}

+		}

+	}

+

+

+	ret = send_cmd_and_recv_result(uart_fd,cmdBOOTERASE_900, recvBuf, sizeof(cmdBOOTERASE_900), &i, 3);

+	HDBD_LOG("[%s %d]ret:%d,cmdOpenQe_and_wrtPro(%d)(%02x,%x,%x,%x,%x,%x,%x,%x,%x,%x)\n", __FUNCTION__, __LINE__, ret, i,

+		recvBuf[0],recvBuf[1],recvBuf[2],recvBuf[3],recvBuf[4],recvBuf[5],

+		recvBuf[6],recvBuf[7],recvBuf[8],recvBuf[9]);

+	if( (recvBuf[0] == 0xF1) && (recvBuf[1] == 0xD9) && (recvBuf[2] == 0x05) && (recvBuf[3] != 0x01) )

+	{

+		HDBD_LOG("[%s %d] cmdBOOTERASE error!\n", __FUNCTION__, __LINE__);

+		return -1;

+	}

+

+	uart_clear_buf(uart_fd);

+

+	data += 1024;

+	len -= 1024;

+	for(i = 1 ;len > 1024; ++i, data += 1024, len -= 1024)

+	{

+		ret = sendfwboot(uart_fd, 0x90000000 + i * 0x400, i, data, 1024, recvBuf);

+		if (ret != 0)

+        {

+            HDBD_LOG("[%s %d] upgrade error,return!\n", __FUNCTION__, __LINE__);

+			return -1;

+        }

+	}

+

+	ret = sendfwboot(uart_fd, 0x90000000 + i * 0x400, i, data, len, recvBuf);

+	if (ret != 0)

+	{

+		HDBD_LOG("[%s %d] upgrade error,return!\n", __FUNCTION__, __LINE__);

+		return -1;

+	}

+	i++;

+

+	ret = sendfwboot(uart_fd, 0x90000000, i, data1st1k, 1024, recvBuf);

+	if (ret != 0)

+	{

+		HDBD_LOG("[%s %d] upgrade error,return!\n", __FUNCTION__, __LINE__);

+		return -1;

+	}

+

+	//uart_send((uint8_t *)cmdCFGRST, 9);

+	HDBD_LOG("[%s %d] upgrade success!\n", __FUNCTION__, __LINE__);

+	return 1;

+}

+

+

diff --git a/mbtk/mbtk_gnssd/hd8122_dl/hd8040_upgrade.h b/mbtk/mbtk_gnssd/hd8122_dl/hd8040_upgrade.h
new file mode 100755
index 0000000..0462f58
--- /dev/null
+++ b/mbtk/mbtk_gnssd/hd8122_dl/hd8040_upgrade.h
@@ -0,0 +1,110 @@
+#ifndef  __HD8040_UPGRADE_H

+#define  __HD8040_UPGRADE_H

+

+#include <stdio.h>

+#include <stdlib.h>

+#include <string.h>

+#include "custom_upgrade_api.h"

+#include "stdint.h"

+

+//cfg

+#define SWITCH_BAUD_460800 1

+#define UPG_CHECK_VER 1

+

+typedef int  sys_err_t;

+

+#define sys_TRUE       (1)

+#define sys_FALSE      (0)  

+

+#define sys_SUCCESS   (0)

+#define sys_FAILED    (-1)

+

+

+typedef unsigned char  uint8_t;

+typedef unsigned short int uint16_t;

+

+#define HDBD_LOG printf

+

+

+/*

+HDBD_UPG_SUCESS£ºÉý¼¶³É¹¦

+HDBD_UPG_ENTER_BOOTMODE_ERROR£º½øÈëbootʧ°Ü£¨¶ÁÈ¡²»µ½°æ±¾ºÅ»òÕß´®¿Ú·¢ËÍʱÐò²»ÕýÈ·£©

+HDBD_UPG_FAIL£ºÉý¼¶Ê§°Ü

+HDBD_UPG_GETVER_ERROR£ºÉý¼¶Ö®ºó¶ÁÈ¡°æ±¾ºÅʧ°Ü

+HDBD_UPG_COM_CFG_ERROR£º´®¿ÚÅäÖôíÎó

+HDBD_UPG_IN_INFO_ERROR£ºÊäÈëµÄclientID´íÎó»òÕßÊäÈëµÄ°æ±¾²ÎÊý²»ÕýÈ·

+HDBD_BIN_FILE_ERROR£ºBinÎļþ´íÎó*/

+typedef enum {

+    HDBD_UPG_SUCESS = 0,

+    HDBD_UPG_ENTER_BOOTMODE_ERROR  = 1,

+    HDBD_UPG_FAIL  = 2,

+    HDBD_UPG_GETVER_ERROR = 3,

+    HDBD_UPG_COM_CFG_ERROR = 4,

+    HDBD_UPG_IN_INFO_ERROR = 5,

+    HDBD_BIN_FILE_ERROR = 6,

+	HDBD_UPG_CHECK_ERR = 7

+}HDBD_RET;

+

+#define COMM_BAUDRATE_PHONE 115200

+

+

+#define ADDR_FLASH_BIN_INFO_ADDR 0x0000000//

+

+/**

+ * @brief  upgrade entry

+ * @param  fd:uart fd

+ * @param  uart_baud_max:uart max baud

+ * @param  binSize:upgrade file bin size

+ * @param  upgrade_clientID:need upgrade clientID

+ * @param  new_ver:upgrade success version

+ * @retval hd8040_upgrade.h  ERROR_LIST

+ */

+

+//#define PYTHON_CALL_BACK_SUPPORT 0

+

+#ifdef PYTHON_CALL_BACK_SUPPORT

+typedef struct python_callback_str

+{

+	int (*gnss_gpio_init_callback)();

+	int (*gnss_enter_boot_callback)();

+	int (*gnss_reset_callback)();

+	int (*set_gnss_baud_callback)(int32_t, uint8_t);

+	int (*uart_send_callback)(int32_t, uint8_t *, int32_t);

+	int (*uart_recv_callback)(int32_t, uint8_t *, int32_t *, int32_t);

+	int (*bsp_flash_readbytes_callback)(uint32_t, uint8_t *, uint32_t);

+	int (*uart_clear_buf_callback)(int32_t);

+	int (*uart_control_callback)(int32_t, uint16_t);

+}PYTHON_CALLBACK;

+

+typedef int (callback)(uint8_t *, uint16_t);

+typedef int (*gnss_gpio_init_callback_)();

+typedef int (*gnss_enter_boot_callback_)();

+typedef int (*gnss_reset_callback_)();

+typedef int (*set_gnss_baud_callback_)(int32_t, uint8_t);

+typedef int (*uart_send_callback_)(int32_t, uint8_t *, int32_t);

+typedef int (*uart_recv_callback_)(int32_t, uint8_t *, int32_t *, int32_t);

+typedef int (*bsp_flash_readbytes_callback_)(uint32_t, uint8_t *, uint32_t);

+typedef int (*uart_clear_buf_callback_)(int32_t);

+typedef int (*uart_control_callback_)(int32_t, uint16_t);

+

+extern PYTHON_CALLBACK g_python_callback;

+extern HDBD_RET hdbd_upgrade_entry(int32_t fd, int32_t uart_baud_max, int32_t binSize, uint8_t *upgrade_clientID, uint8_t *new_ver, 

+	gnss_gpio_init_callback_ gnss_gpio_init_callback__,

+	gnss_enter_boot_callback_ gnss_enter_boot_callback__,

+	gnss_reset_callback_ gnss_reset_callback__,

+	set_gnss_baud_callback_ set_gnss_baud_callback__,

+	uart_send_callback_ uart_send_callback__,

+	uart_recv_callback_ uart_recv_callback__,

+	bsp_flash_readbytes_callback_ bsp_flash_readbytes_callback__,

+	uart_clear_buf_callback_ uart_clear_buf_callback__,

+	uart_control_callback_ uart_control_callback__

+	);

+#else

+extern HDBD_RET hdbd_upgrade_entry(int32_t fd, int32_t uart_baud_max, int32_t binSize, uint8_t *upgrade_clientID, uint8_t *new_ver);

+#endif

+

+extern HDBD_RET hdbd_enter_boot(int32_t fd, int32_t uart_baud_max, int binSize, uint8_t *upgrade_clientID, uint8_t *new_ver);

+

+

+#endif

+

diff --git a/mbtk/mbtk_gnssd/hd8122_dl/main.c b/mbtk/mbtk_gnssd/hd8122_dl/main.c
new file mode 100755
index 0000000..190e8c0
--- /dev/null
+++ b/mbtk/mbtk_gnssd/hd8122_dl/main.c
@@ -0,0 +1,295 @@
+#include "hd8040_upgrade.h"

+#include "port.h"

+#include <getopt.h>

+

+#define READ_MAX_LENGTH 128

+#define PORT_NAME ("/dev/ttyS2")

+uint8_t g_bin_buff[500*1024] = {0};

+uint32_t g_bin_buff_size = 0;

+

+extern int get_mon_ver(int uart_fd,uint8_t *ver_buf, int ver_len);

+

+#define READ_LEN_MAX 1024 //BOOT_UPGRADE_BUFF_MAX_1

+int read_bin_file(uint8_t *path, uint8_t *buff)

+{

+    int fp = -1;

+    int ret = 0;

+    int i = 0;

+    int size = 0;

+	

+    if (NULL == path || NULL == buff)

+    {

+        printf("[%s %d] str error", __FUNCTION__, __LINE__);

+        return -1;

+    }

+

+    fp = open((char *)path, O_RDONLY);

+    if(fp < 0)

+    {

+        printf( "[%s %d]open file failed ! errno is %d\n", __FUNCTION__, __LINE__, errno); 

+        return -1;

+    }

+

+	size = lseek(fp, 0x00, SEEK_END);

+	if(size <= 0)

+	{

+        printf( "[%s %d]file is empty\n", __FUNCTION__, __LINE__); 

+        return -1;

+	}

+	

+	printf( "[%s %d]file size is:%d\n", __FUNCTION__, __LINE__, size);

+	lseek(fp, 0x00, SEEK_SET);

+	while(1)

+	{

+		ret = read(fp, buff, READ_LEN_MAX);

+		//HDBD_LOG( "[%s %d]ret:%d\n", __FUNCTION__, __LINE__, ret);

+		i += ret;

+	    if(ret == READ_LEN_MAX)

+	    {

+	        buff += READ_LEN_MAX;

+	    }

+		else

+		{

+			break;

+		}

+	}

+	

+	printf( "[%s %d]file size is:%d,i:%d\n", __FUNCTION__, __LINE__, size, i);

+    close(fp);

+	if(size != i)

+	{

+		return -1;

+	}

+	g_bin_buff_size = size;

+    return 0;

+}

+

+static void print_usage(const char *prog)

+{

+	printf("Usage: %s [-BUTVh]\n", prog);

+	puts("  -B --boot    boot mode select\n"

+		 "  -U --user  user mode select \n"

+		 "  -T --usertoboot  usertoboot mode select \n"

+	     "  -V --version firmware version get \n"

+	     "  -h --help    help message print \n");

+	exit(1);

+}

+

+struct option long_options[] = {

+{ "name", 0, NULL, 'n' },

+{ "bf_name", 0, NULL, 'b' },

+{ "love", 1, NULL, 'l' },

+{ 0, 0, 0, 0},

+};

+

+static const struct option lopts[] = {

+	{ "boot",	required_argument, NULL, 'B' },//required_argument

+	{ "user",	required_argument, NULL, 'U' },

+	{ "usertoboot", required_argument, NULL, 'T' },

+	{ "version",   0, NULL, 'V' },

+	{ "help",	0, NULL, 'h' },

+	{ 0, 0, 0, 0 },

+};

+char* const short_options1 = "B:U:T:Vh";

+

+int write_file(uint8_t *path, uint8_t *buff, int len)

+{

+    int fp = -1;

+    int ret = 0;

+    int i = 0;

+    int size = 0;

+	

+    if (NULL == path || NULL == buff)

+    {

+        printf("[%s %d] str error", __FUNCTION__, __LINE__);

+        return -1;

+    }

+

+    fp = open((char *)path, O_RDWR );

+    if(fp < 0)

+    {

+        printf( "[%s %d]open file failed ! errno is %d\n", __FUNCTION__, __LINE__, errno); 

+        return -1;

+    }

+

+	size = lseek(fp, 0x00, SEEK_END);

+

+	write(fp, buff, len);	

+	

+	printf( "[%s %d]file size is:%d,i:%d\n", __FUNCTION__, __LINE__, size, i);

+    close(fp);

+    return 0;

+}

+

+//example  //升级过程中,禁止其他程序操作该串口,禁止断电复位等操作

+int main(int argc, char *argv[])

+{

+	uint8_t newVersionBuf[READ_MAX_LENGTH] = {0};

+	uint8_t cleintID[READ_MAX_LENGTH] = "999999";

+    int fd = -1; // uart handle

+	int ret = -1;

+	uint8_t filePath[256] = {0};

+	uint16_t filePtahLen = 0;

+

+	if (argc == 1) 

+	{

+		printf("This program needs arguments....\n\n");

+		print_usage(argv[0]);

+	}

+

+	int num;

+	while ((num = getopt_long(argc, argv, short_options1, lopts, NULL)) != -1)

+	{

+		printf("getopt_long C:%d\n", num);

+		switch (num) 

+		{

+		

+			case 'B':

+			{

+				printf("optarg:%s\n", optarg); 

+				if ( optarg == NULL )

+				{

+					printf("%s: option 'B' requires argument...\n", argv[0]);

+					break;

+				}

+				filePtahLen = strlen(optarg);

+				if(filePtahLen < sizeof(filePath))

+				{

+					memcpy((char *)filePath, optarg, filePtahLen);

+					goto HD_UPG;

+				}

+				

+				//sleep(1);

+			}

+			break;

+			

+			case 'U':

+			{

+				printf("userUpgMode\n"); 

+			}

+			break;

+			case 'T':

+			{

+				printf("userToBootMode\n"); 

+			}

+			break;

+			case 'V':

+			{

+				printf("check version\n");

+				//fd = uart_open((unsigned char *)PORT_NAME);//PORT_NAME234

+				fd = OpenUart((char *)PORT_NAME);//PORT_NAME234

+				if (fd < 0)

+				{

+				

+					printf("open uart failed %d\n", fd);

+					return -1;

+				}

+				get_mon_ver(fd, newVersionBuf, sizeof(newVersionBuf));

+				printf("version is: %s\n", newVersionBuf);

+				uart_close(fd);

+				return 0;

+			}

+			break;

+			case 'h':

+			{

+				printf("333333333333\n"); 

+				//print_usage(argv[0]);

+			}

+			break;

+			default:

+			{

+				//print_usage(argv[0]);

+			}

+			break;

+		}

+	}

+

+   //芯片上电

+HD_UPG:

+    //...

+	//fd = uart_open((unsigned char *)PORT_NAME);//PORT_NAME234

+	fd = OpenUart((char *)PORT_NAME);//PORT_NAME234

+	if (fd < 0)

+	{

+	

+		printf("open uart failed %d\n", fd);

+		return -1;

+	}

+    ret = read_bin_file(filePath, g_bin_buff);

+	if (ret < 0)

+	{

+	   printf("open file failed %d\n", fd);

+	   return -1;

+	}

+

+	// int runCount = 1;

+	// uint8_t resultBuf[256] = {0x00};

+	// //write_file("result.txt", "*********************start***********************************************", strlen("*********************start***********************************************"));

+	// while(runCount <= 2)

+	// {

+	// 	memset(g_bin_buff, 0x00, sizeof(g_bin_buff));

+	// 	if(runCount%2 == 0)

+	// 	{

+	// 		//ret = read_bin_file("HD8120.user2boot.9989.d4cae.e97e7.0620.bin", g_bin_buff);

+	// 		ret = read_bin_file("HD8125.ZTE.GN3.115200.0090.a2ff3.682bc.0815.GBQ.CFG.PPS13.ANT.LDO.JAMDetection.NAVPVT.bin", g_bin_buff);

+			

+	// 	}

+	// 	else

+	// 	{

+	// 		//ret = read_bin_file("HD8120.user2boot.9989.d4cae.e97e7.0619.bin", g_bin_buff);

+	// 		ret = read_bin_file("HD8125.GHT.115200.9956.8da5b.7ecbc.230105R1.PPS9.LDO.GBQB1C.bin", g_bin_buff);

+	// 	}

+	// 	if (ret < 0)

+	// 	{

+	// 	   HDBD_LOG("open file failed %d\n", fd);

+	// 	   return -1;

+	// 	}

+

+    	ret = fw_update_boot(fd, g_bin_buff, g_bin_buff_size);

+        if (ret < 0)

+	{

+	   printf("open file failed %d\n", fd);

+	   return -1;

+	}

+		//check result get version

+		/*

+		gnss_reset();

+		get_mon_ver(......)

+		check version

+

+		

+		*/

+		memset(resultBuf, 0x00, sizeof(resultBuf));

+		if(ret == HDBD_UPG_SUCESS)

+		{

+			sprintf(resultBuf, "result count num:%d, result:success! newVersion:%s.\r\n", runCount, newVersionBuf);

+		}

+		else

+		{

+			sprintf(resultBuf, "result count num:%d, result:failed! newVersion:%s.\r\n", runCount, newVersionBuf);

+		}

+		//write_file("result.txt", resultBuf, strlen(resultBuf));

+		runCount ++;

+		sleep(5);

+	

+	write_file("result1.txt", "*********************end***********************************************", strlen("*********************end***********************************************"));

+  //ret = hdbd_enter_boot(fd, 115200, g_bin_buff_size, cleintID, newVersionBuf); 

+	if(ret == HDBD_UPG_SUCESS)

+	{

+		printf("upgrade sucess!\r\n");

+	}

+	else

+	{

+		printf("upgrade FAIL, fail style:%d\r\n", ret);

+	}

+	uart_close(fd);

+	return ret;	

+}

+/*

+HDBD_UPG_SUCESS:升级成功

+HDBD_UPG_ENTER_BOOTMODE_ERROR:进入boot失败(读取不到版本号或者串口发送时序不正确)

+HDBD_UPG_FAIL:升级失败

+HDBD_UPG_GETVER_ERROR:升级之后读取版本号失败

+HDBD_UPG_COM_CFG_ERROR:串口配置错误

+HDBD_UPG_IN_INFO_ERROR:输入的clientID错误或者输入的版本参数不正确

+HDBD_BIN_FILE_ERROR:Bin文件错误*/

diff --git a/mbtk/mbtk_gnssd/hd8122_dl/port.c b/mbtk/mbtk_gnssd/hd8122_dl/port.c
new file mode 100755
index 0000000..89e1596
--- /dev/null
+++ b/mbtk/mbtk_gnssd/hd8122_dl/port.c
@@ -0,0 +1,504 @@
+

+#include "port.h"

+

+/**

+  * @brief  Send Data to serial port

+  * @param  fd:file desc

+  * @param  cmd:data pointer

+  * @param  cmd_len:data length

+  * @retval SUCCESS/FAIL

+  */

+int uart_send(int fd, const uint8_t *cmd, int cmd_len)

+{

+    int ret = FAIL;

+

+    ret = write(fd, cmd, cmd_len);

+    if(ret < 0)

+    {

+        HDBD_LOG("[%s %d]write port failed,ret: %d\n", __FUNCTION__, __LINE__, ret);

+        return FAIL;

+    }

+

+    return SUCCESS;

+}

+

+

+/**

+  * @brief  Receive data from serial port0

+  * @param  fd:file desc

+  * @param  buf:receive buffer

+  * @param  actual_length:receive data length

+  * @param  timeout:para of timeout

+  * @retval SUCCESS/FAIL

+  */

+int uart_recv(int fd, uint8_t *buf, int *actual_length, int timeout)

+{

+    int ret;

+	int temp = 0;

+	int len = *actual_length;

+	uint8_t tempBuf[128] = {0};

+	int errorCount = 0;

+    fd_set rd;

+    struct timeval tv;

+    tv.tv_sec = timeout;

+    tv.tv_usec = 0;

+

+    FD_ZERO(&rd);

+    FD_SET(fd, &rd);

+	*actual_length = 0;

+	HDBD_LOG("[%s %d] len:%d\n", __FUNCTION__, __LINE__, len);

+	while(1)

+	{

+	    ret = select(fd + 1, &rd, NULL, NULL, &tv);

+	    if(ret > 0){

+	        if (FD_ISSET(fd, &rd)) {

+	            temp = read(fd, tempBuf, 128);

+				if(temp > 0)

+				{

+					errorCount = 0;

+					if((*actual_length + temp) <= len)

+					{

+						memcpy(buf+*actual_length, tempBuf, temp);

+						*actual_length += temp;

+					}

+					else

+					{

+						memcpy(buf+*actual_length, tempBuf, (len - *actual_length));

+						*actual_length = len;

+					}

+				}

+				else

+				{

+					errorCount++;

+				}

+				HDBD_LOG("[%s %d]len:%d, *actual_length:%d, temp:%d\n", __FUNCTION__, __LINE__, len, *actual_length, temp);

+	        }

+	    }

+	    else if(ret == 0)

+		{

+	        HDBD_LOG("[%s %d]timeout\n", __FUNCTION__, __LINE__);

+			if(*actual_length <= 2)

+			{

+	        	goto ERR1;

+			}

+			else

+			{

+				break;

+			}

+	    }

+	    else

+		{

+	        HDBD_LOG("[%s %d] select error!\n", __FUNCTION__, __LINE__);

+	        goto ERR1;

+	    }

+

+		if((*actual_length >= len) || (errorCount >= 5))

+		{

+			HDBD_LOG("[%s %d]*actual_length:%d, recv sucess,break\n", __FUNCTION__, __LINE__, *actual_length);

+			break;

+		}

+

+	}

+

+	HDBD_LOG("[%s %d]*actual_length:%d, ret:%d\n", __FUNCTION__, __LINE__, *actual_length, ret);

+    return SUCCESS;

+

+ERR1:

+    return FAIL;

+}

+

+int UartRead(int fd, uint8_t *RxBuff, long RxLen)

+{

+    #if 1

+    int GetRxLen=0;

+    int index;

+    while(RxLen){

+        //printf("RxLen=%d\r\n", RxLen);

+        index = read(fd, RxBuff+GetRxLen, 1);

+        //printf("index=%d\r\n", index);

+        GetRxLen += index;

+        //printf("GetRxLen=%d\r\n", GetRxLen);

+        RxLen--;

+        //printf("RxLen=%d\r\n", RxLen);

+        if(index == 0)

+        {

+            return GetRxLen;

+        }

+    }

+

+    return GetRxLen ;

+    #else

+    int GetRxLen=0;

+    while(RxLen){

+        //printf("[RxLen=%ld]\r\n", RxLen);

+        GetRxLen += read(fd, RxBuff+GetRxLen, 1);

+        RxLen--;

+    }

+

+    return GetRxLen ;

+    #endif

+}

+

+/**

+  * @brief  Write gpio node

+  * @param  path:path of gpio node

+  * @param  value:'1' or '0'

+  * @retval SUCCESS/FAIL

+  */

+int write_gpio(char const* path, int value)

+{

+    int fd = -1;

+    char buffer[20] = {0};

+    int bytes = 0;

+    ssize_t amt = 0;

+

+    memset(buffer, 0x0, sizeof(buffer));

+    fd = open(path, O_RDWR);

+

+    if (fd >= 0)

+    {

+        bytes = snprintf(buffer, sizeof(buffer), "%d\n", value);

+        amt = write(fd, buffer, (size_t)bytes);

+        close(fd);

+        return amt == -1 ? -errno : 0;

+    }

+    else

+    {

+        HDBD_LOG("[%s %d]write_int failed to open %s, errno is %d \n", __FUNCTION__, __LINE__, path, errno);

+    }

+

+    return -errno;

+}

+

+/**

+  * @brief  Read gpio node

+  * @param  path:path of gpio node

+  * @param  value:'1' or '0'

+  * @retval SUCCESS/FAIL

+  */

+int read_gpio(char const* path)

+{

+    int fd = -1;

+    char buffer[20] = {0};

+    ssize_t amt = 0;

+

+    memset(buffer, 0x0, sizeof(buffer));

+    fd = open(path, O_RDWR);

+

+    if (fd >= 0)

+    {

+        amt = read(fd, buffer, 1);

+        close(fd);

+        HDBD_LOG("[%s %d]read to <%s>, value is <%zd> \n", __FUNCTION__, __LINE__, path, amt);

+        return 0;

+    }

+    else

+    {

+        HDBD_LOG("[%s %d]read failed to open <%s>, errno is <%d> \n", __FUNCTION__, __LINE__, path, errno);

+    }

+

+    return -1;

+}

+

+/**

+  * @brief  Set serial port Para

+  * @param  fd:file desc

+  * @param  speed:baudrate

+  * @param  flow_ctrl:flow control flag

+  * @param  databits:data bits

+  * @param  stopbits:stop bits

+  * @param  parity:parity flag

+  * @retval SUCCESS/FAIL

+  */

+int uart_set(int fd, int speed, int flow_ctrl, int databits, int stopbits, int parity)

+{

+    int   i;

+    int   speed_arr[11] = {B921600, B576000, B460800,B230400, B115200, B19200, B9600, B4800, B2400, B1200, B300};

+    int   name_arr[11]  = {921600,576000,460800,230400, 115200,  19200,  9600,  4800,  2400,  1200,  300};

+

+    struct termios options;

+

+    if ( tcgetattr( fd, &options)  !=  0)

+    {

+        HDBD_LOG("[%s %d]get serial options Fail\n", __FUNCTION__, __LINE__);

+        return FAIL;

+    }

+

+    for ( i = 0;  i < 11;  i++) /* sizeof(speed_arr) / sizeof(int) */

+    {

+        if  (speed == name_arr[i])

+        {

+            cfsetispeed(&options, speed_arr[i]);

+            cfsetospeed(&options, speed_arr[i]);

+            break;

+        }

+    }

+

+    options.c_cflag |= CLOCAL;

+    options.c_cflag |= CREAD;

+

+    switch (flow_ctrl)

+    {

+

+    case 0 :

+        options.c_cflag &= ~CRTSCTS;

+        break;

+

+    case 1 :

+        options.c_cflag |= CRTSCTS;

+        break;

+    case 2 :

+        options.c_cflag |= IXON | IXOFF | IXANY;

+        break;

+    }

+

+    options.c_cflag &= ~CSIZE;

+    switch (databits)

+    {

+    case 5    :

+        options.c_cflag |= CS5;

+        break;

+    case 6    :

+        options.c_cflag |= CS6;

+        break;

+    case 7    :

+        options.c_cflag |= CS7;

+        break;

+    case 8:

+        options.c_cflag |= CS8;

+        break;

+    default:

+        HDBD_LOG("[%s %d] Unsupported data size\n", __FUNCTION__, __LINE__);

+        return FAIL;

+    }

+

+    switch (parity)

+    {

+    case 'n':

+    case 'N':

+        options.c_cflag &= ~PARENB;

+        options.c_iflag &= ~INPCK;

+        break;

+    case 'o':

+    case 'O':

+        options.c_cflag |= (PARODD | PARENB);

+        options.c_iflag |= INPCK;

+        break;

+    case 'e':

+    case 'E':

+        options.c_cflag |= PARENB;

+        options.c_cflag &= ~PARODD;

+        options.c_iflag |= INPCK;

+        break;

+    case 's':

+    case 'S':

+        options.c_cflag &= ~PARENB;

+        options.c_cflag &= ~CSTOPB;

+        break;

+    default:

+        HDBD_LOG("[%s %d] Unsupported parity\n", __FUNCTION__, __LINE__);

+        return FAIL;

+    }

+

+    switch (stopbits)

+    {

+    case 1:

+        options.c_cflag &= ~CSTOPB; break;

+    case 2:

+        options.c_cflag |= CSTOPB; break;

+    default:

+        HDBD_LOG("[%s %d] Unsupported stop bits\n", __FUNCTION__, __LINE__);

+        return FAIL;

+    }

+

+    options.c_oflag &= ~OPOST;

+    options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);

+    //options.c_lflag &= ~(ISIG | ICANON);

+    options.c_cc[VTIME] = 1;

+    options.c_cc[VMIN] = 1;

+    tcflush(fd, TCIFLUSH);

+

+    if (tcsetattr(fd, TCSANOW, &options) != 0)

+    {

+        HDBD_LOG("[%s %d] com set error!\n", __FUNCTION__, __LINE__);

+        return FAIL;

+    }

+

+    return SUCCESS;

+}

+

+/**

+  * @brief  Init serial port

+  * @param  fd:file desc

+  * @param  speed:baudrate

+  * @param  flow_ctrl:flow control flag

+  * @param  databits:data bits

+  * @param  stopbits:stop bits

+  * @param  parity:parity flag

+  * @retval SUCCESS/FAIL

+  */

+int uart_init(int fd, int speed, int flow_ctrl, int databits, int stopbits, int parity)

+{

+    if (uart_set(fd, speed, flow_ctrl, databits, stopbits, parity) == FAIL)

+    {

+        HDBD_LOG("[%s %d] Uart init false\n", __FUNCTION__, __LINE__);

+        return FAIL;

+    }

+    else

+    {

+        return  SUCCESS;

+    }

+}

+

+/**

+  * @brief  Init serial port

+  * @param  fd:uart fd

+  * @param  selector:TCIOFLUSH/TCIFLUSH

+  */

+void clearBuffer(int fd, int selector)

+{

+    tcflush(fd, selector);

+}

+

+/**

+  * @brief  open serial port

+  * @param  GPS_DEVICE:uart port

+  * @retval SUCCESS/FAIL

+  */

+int uart_open(unsigned char * GPS_DEVICE)

+{

+    int fd = -1;

+    int uart_init_ret = -1;

+    //*****************************************************************************

+    fd = open((char *)GPS_DEVICE, O_RDWR|O_NOCTTY|O_NDELAY);

+

+    if( fd < 0)

+    {

+        HDBD_LOG("[%s %d] : open port ERROR..state->fd=%d, errno=%d\n",__FUNCTION__, __LINE__, fd, errno);

+        return -1;

+    }

+

+    HDBD_LOG("[%s %d] : open port succceed..state->fd=%d OK\n", __FUNCTION__, __LINE__, fd);

+

+    /* set port */

+    if(fcntl( fd,F_SETFL,0)<0)

+    {

+        HDBD_LOG("[%s %d] : fcntl F_SETFL Fail\n", __FUNCTION__, __LINE__);

+        return -1;

+    }

+

+    tcflush(fd, TCIOFLUSH);

+

+    uart_init_ret = uart_init(fd, 115200, 0, 8, 1, 'N');

+    if (-1 == uart_init_ret)

+    {

+        HDBD_LOG("[%s %d] : uart_init_ret is <%d>, return -1\n", __FUNCTION__, __LINE__, uart_init_ret);

+        return -1;

+    }

+

+    HDBD_LOG("[%s %d] : Port setup finished..OK\n", __FUNCTION__, __LINE__);

+    //**********************************************************************

+    return fd;

+}

+

+int uart_close(int fd)

+{

+	close(fd);

+	return 0;

+}

+

+

+void SetOpt(int fd)

+{

+    static struct termios termold, termnew;

+    tcgetattr(fd, &termold);

+    bzero(&termnew, sizeof(termnew));

+

+    termnew.c_iflag &= ~(ICRNL | IGNCR);

+    termnew.c_cflag |= CLOCAL | CREAD;   //CLOCAL:忽略modem控制线  CREAD:打开接受者

+    termnew.c_cflag &= ~CSIZE;

+    termnew.c_cflag |= CS8;

+    termnew.c_cflag &= ~CRTSCTS;

+    termnew.c_cflag &= ~PARENB;

+

+    cfsetispeed(&termnew, B115200);

+    cfsetospeed(&termnew, B115200);

+

+    termnew.c_cflag &=  ~CSTOPB;

+    termnew.c_cc[VTIME]  = 1;    //VTIME:非cannoical模式读时的延时,以十分之一秒位单位

+    termnew.c_cc[VMIN] = 0;       //VMIN:非canonical模式读到最小字符数

+    tcflush(fd, TCIFLUSH);

+    tcsetattr(fd, TCSANOW, &termnew);

+}

+

+/*

+* band : 0->460800, 1->115200, 2->9600, other->115200

+*/

+int set_baudrate(int fd, uint8_t baud)

+{

+    static struct termios termold, termnew;

+    tcgetattr(fd, &termold);

+    bzero(&termnew, sizeof(termnew));

+

+    termnew.c_iflag &= ~(ICRNL | IGNCR);

+    termnew.c_cflag |= CLOCAL | CREAD;   //CLOCAL:忽略modem控制线  CREAD:打开接受者

+    termnew.c_cflag &= ~CSIZE;

+    termnew.c_cflag |= CS8;

+    termnew.c_cflag &= ~CRTSCTS;

+    termnew.c_cflag &= ~PARENB;

+

+    if(baud == 1)

+    {

+        cfsetispeed(&termnew, B460800);

+        cfsetospeed(&termnew, B460800);

+    }

+    else if(baud == 2)

+    {

+        cfsetispeed(&termnew, B115200);

+        cfsetospeed(&termnew, B115200);

+    }

+    else if(baud)

+    {

+        cfsetispeed(&termnew, B9600);

+        cfsetospeed(&termnew, B9600);

+    }

+    else

+    {

+        cfsetispeed(&termnew, B115200);

+        cfsetospeed(&termnew, B115200);

+    }

+    termnew.c_cflag &=  ~CSTOPB;

+    termnew.c_cc[VTIME]  = 1;    //VTIME:非cannoical模式读时的延时,以十分之一秒位单位

+    termnew.c_cc[VMIN] = 0;       //VMIN:非canonical模式读到最小字符数

+    tcflush(fd, TCIFLUSH);

+    tcsetattr(fd, TCSANOW, &termnew);

+    return 0;

+}

+

+int OpenUart(char* UART_DEV)

+{

+    int fd=0;

+

+    /*第1个参数:想要打开的文件路径名,或者文件名

+     *第2个参数:open_Status:文件打开方式,可采用下面的文件打开模式:

+          O_RDONLY:以只读方式打开文件

+          O_WRONLY:以只写方式打开文件

+          O_RDWR:以读写方式打开文件

+          O_APPEND:写入数据时添加到文件末尾

+          O_CREATE:如果文件不存在则产生该文件,使用该标志需要设置访问权限位mode_t

+          O_EXCL:指定该标志,并且指定了O_CREATE标志,如果打开的文件存在则会产生一个错误

+          O_TRUNC:如果文件存在并且成功以写或者只写方式打开,则清除文件所有内容,使得文件长度变为0

+          O_NOCTTY:如果打开的是一个终端设备,这个程序不会成为对应这个端口的控制终端,如果没有该标志,任何一个输入,例如键盘中止信号等,都将影响进程。

+          O_NONBLOCK:该标志与早期使用的O_NDELAY标志作用差不多。程序不关心DCD信号线的状态,如果指定该标志,进程将一直在休眠状态,直到DCD信号线为0。

+          O_NONBLOCK和O_NDELAY所产生的结果都是使I/O变成非搁置模式(non-blocking),在读取不到数据或是写入缓冲区已满会马上return,而不会搁置程序动作,直到有数据或写入完成;

+          它们的差别在于设立O_NDELAY会使I/O函式马上回传0,但是又衍生出一个问题,因为读取到档案结尾时所回传的也是0,这样无法得知是哪中情况;因此,O_NONBLOCK就产生出来,它在读取不到数据时会回传-1,并且设置errno为EAGAIN。

+       第3个参数:设置文件访问权限的初始值

+    */

+    fd = open(UART_DEV , O_RDWR|O_NOCTTY);

+    if (fd < 0)

+    {

+       return -1;

+    }

+    SetOpt(fd);

+    return  fd;

+}

diff --git a/mbtk/mbtk_gnssd/hd8122_dl/port.h b/mbtk/mbtk_gnssd/hd8122_dl/port.h
new file mode 100755
index 0000000..56ea1e9
--- /dev/null
+++ b/mbtk/mbtk_gnssd/hd8122_dl/port.h
@@ -0,0 +1,50 @@
+#ifndef  __HD8040_PORT_H

+#define  __HD8040_PORT_H

+

+#include <stdio.h>

+#include <stdlib.h>

+#include <unistd.h>

+#include <string.h>

+#include <errno.h>

+#include <fcntl.h>

+#include <termios.h>

+#include <sys/types.h>

+#include <sys/wait.h>

+#include <sys/un.h>

+//#include <sys/prctl.h>

+#include <sys/time.h>

+#include <dirent.h>

+#include <sys/stat.h>

+

+#define SUCCESS 0

+#define FAIL -1

+

+#ifndef HDBD_LOG

+#define HDBD_LOG printf

+#endif

+

+typedef unsigned char  uint8_t;

+typedef unsigned short int uint16_t;

+typedef unsigned int   uint32_t;

+/* For communication buadrate */

+#define COMM_BAUDRATE_PHONE 115200

+

+/* For upgrade baudrate */

+#define UPGRADE_BAUDRATE_PHONE 460800

+

+/* <Function define Config begin */

+#define DEBUG_RUN_TIME 0

+/* Function define Config end> */

+

+/* Function Declaration */

+/* Basic operation for serial port */

+extern int uart_set(int fd, int speed, int flow_ctrl, int databits, int stopbits, int parity);

+extern int uart_init(int fd, int speed, int flow_ctrl, int databits, int stopbits, int parity);

+extern int uart_send(int fd, const uint8_t *cmd, int cmd_len);

+extern int uart_recv(int fd, uint8_t *buf, int *actual_length, int timeout);

+extern void clearBuffer(int fd, int selector);

+extern int uart_open(unsigned char * GPS_DEVICE);

+extern int uart_close(int fd);

+extern int OpenUart(char* UART_DEV);

+extern int set_baudrate(int fd, uint8_t baud);

+#endif