b.liu | f9fbfa1 | 2024-06-14 15:53:59 +0800 | [diff] [blame] | 1 | /* |
| 2 | * gnss_hd8122.c |
| 3 | * |
| 4 | * HD8122 GNSS source. |
| 5 | * |
| 6 | */ |
| 7 | /****************************************************************************** |
| 8 | |
| 9 | EDIT HISTORY FOR FILE |
| 10 | |
| 11 | WHEN WHO WHAT,WHERE,WHY |
| 12 | -------- -------- ------------------------------------------------------- |
| 13 | 2024/6/14 LiuBin Initial version |
| 14 | |
| 15 | ******************************************************************************/ |
| 16 | #include <stdio.h> |
| 17 | #include <stdlib.h> |
| 18 | #include <unistd.h> |
| 19 | #include <errno.h> |
| 20 | #include <fcntl.h> |
b.liu | 778645e | 2024-06-21 16:47:42 +0800 | [diff] [blame] | 21 | #include <pthread.h> |
b.liu | f9fbfa1 | 2024-06-14 15:53:59 +0800 | [diff] [blame] | 22 | |
| 23 | #include "mbtk_log.h" |
| 24 | #include "mbtk_type.h" |
| 25 | #include "mbtk_gpio.h" |
b.liu | 778645e | 2024-06-21 16:47:42 +0800 | [diff] [blame] | 26 | #include "mbtk_utils.h" |
b.liu | f9fbfa1 | 2024-06-14 15:53:59 +0800 | [diff] [blame] | 27 | #include "gnss_utils.h" |
| 28 | #include "gnss_hd8122.h" |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 29 | #include "hd8122_dl/hd8040_upgrade.h" |
b.liu | f9fbfa1 | 2024-06-14 15:53:59 +0800 | [diff] [blame] | 30 | |
| 31 | #define UART_BITRATE_NMEA_DEF_FW 115200 // Default bitrate. |
| 32 | #define GNSS_POWER_GPIO 43 |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame] | 33 | #define GNSS_SET_TIMEOUT 3000 // 3s |
| 34 | #define GNSS_PACK_BUFF_SIZE 1024 |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 35 | #define GNSS_MSG_NUM_MAX 30 |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 36 | #define READ_LEN_MAX 1024 //BOOT_UPGRADE_BUFF_MAX_1 |
yq.wang | af363dc | 2024-08-06 20:16:00 -0700 | [diff] [blame^] | 37 | #define GNSS_FW_GQALS_PATH "/lib/firmware/HD8122.YIKE.GN3.115200.0035.720e5.53ef0.GQALS.ANT.EPH.CFG.PPS13.240115R1.bin" |
| 38 | #define GNSS_FW_GAQBS_PATH "/lib/firmware/HD8122.YIKE.GN3.115200.0037.dbd12.53ef0.GAQBS.B1C.ANT.EPH.CFG.PPS13.240416R1.bin" |
b.liu | f9fbfa1 | 2024-06-14 15:53:59 +0800 | [diff] [blame] | 39 | |
| 40 | static pthread_cond_t read_cond; |
| 41 | static pthread_mutex_t read_mutex; |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 42 | static bool setting_waitting = FALSE; |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame] | 43 | static bool setting_busy = FALSE; |
| 44 | static void *gnss_set_rsp_ptr = NULL; |
| 45 | static gnss_err_enum gnss_set_result = GNSS_ERR_OK; |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 46 | static hd8122_msg_id_t msg_array[GNSS_MSG_NUM_MAX]; |
yq.wang | af363dc | 2024-08-06 20:16:00 -0700 | [diff] [blame^] | 47 | static char gnss_ctrl_path[128] = "/sys/devices/platform/mbtk-gnss/ctrl"; |
| 48 | |
b.liu | f9fbfa1 | 2024-06-14 15:53:59 +0800 | [diff] [blame] | 49 | |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame] | 50 | int gnss_write(int fd, const void *data, int data_len); |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 51 | int OpenUart(char* UART_DEV); |
| 52 | int uart_close(int fd); |
| 53 | int fw_update_boot(int uart_fd, uint8_t *data, uint32_t len); |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame] | 54 | |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 55 | static uint16 fletcher16(const uint8_t* data, int data_len) |
| 56 | { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 57 | uint32_t sum1 = 0; |
| 58 | uint32_t sum2 = 0; |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame] | 59 | int index; |
| 60 | |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 61 | for (index = 0; index < data_len; ++index ) |
| 62 | { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 63 | sum1 += data[index]; |
| 64 | sum2 += sum1; |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame] | 65 | } |
| 66 | |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 67 | return ((0xFF & sum2) << 8) | (0xFF & sum1); |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | static void gnss_set_timer_cb(int signo) |
| 71 | { |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 72 | if(setting_busy) |
| 73 | { |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame] | 74 | pthread_mutex_lock(&read_mutex); |
| 75 | pthread_cond_signal(&read_cond); |
| 76 | pthread_mutex_unlock(&read_mutex); |
| 77 | gnss_set_result = GNSS_ERR_TIMEOUT; |
| 78 | } |
| 79 | return; |
| 80 | } |
| 81 | |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 82 | static void msg_init() |
| 83 | { |
b.liu | 778645e | 2024-06-21 16:47:42 +0800 | [diff] [blame] | 84 | unsigned int i = 0; |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 85 | while(i < ARRAY_SIZE(msg_array)) |
| 86 | { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 87 | msg_array[i].enable = FALSE; |
| 88 | i++; |
| 89 | } |
| 90 | } |
| 91 | |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 92 | static int msg_insert(uint8 gid, uint8 sid) |
| 93 | { |
b.liu | 778645e | 2024-06-21 16:47:42 +0800 | [diff] [blame] | 94 | unsigned int i = 0; |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 95 | while(i < ARRAY_SIZE(msg_array)) |
| 96 | { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 97 | if(!msg_array[i].enable) |
| 98 | break; |
| 99 | i++; |
| 100 | } |
| 101 | |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 102 | if(i == ARRAY_SIZE(msg_array)) |
| 103 | { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 104 | LOGE("Msg full : %d", i); |
| 105 | return -1; |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 106 | } |
| 107 | else |
| 108 | { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 109 | msg_array[i].enable = TRUE; |
| 110 | msg_array[i].gid = gid; |
| 111 | msg_array[i].sid = sid; |
| 112 | return 0; |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | static int msg_find(uint8 gid, uint8 sid) |
| 117 | { |
b.liu | 778645e | 2024-06-21 16:47:42 +0800 | [diff] [blame] | 118 | unsigned int i = 0; |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 119 | while(i < ARRAY_SIZE(msg_array)) |
| 120 | { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 121 | if(msg_array[i].enable && gid == msg_array[i].gid && sid == msg_array[i].sid) |
| 122 | break; |
| 123 | i++; |
| 124 | } |
| 125 | |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 126 | if(i == ARRAY_SIZE(msg_array)) |
| 127 | { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 128 | LOGE("No found %d - %d", gid, sid); |
| 129 | return -1; |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 130 | } |
| 131 | else |
| 132 | { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 133 | return i; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | static int msg_remove(uint8 gid, uint8 sid) |
| 138 | { |
| 139 | int i = msg_find(gid, sid); |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 140 | if(i >= 0) |
| 141 | { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 142 | msg_array[i].enable = FALSE; |
| 143 | msg_array[i].gid = 0; |
| 144 | msg_array[i].sid = 0; |
| 145 | return 0; |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 146 | } |
| 147 | else |
| 148 | { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 149 | return -1; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | static int msg_count() |
| 154 | { |
b.liu | 778645e | 2024-06-21 16:47:42 +0800 | [diff] [blame] | 155 | unsigned int i = 0; |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 156 | int count = 0; |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 157 | while(i < ARRAY_SIZE(msg_array)) |
| 158 | { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 159 | if(msg_array[i].enable) |
| 160 | count++; |
| 161 | i++; |
| 162 | } |
| 163 | return count; |
| 164 | } |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame] | 165 | |
yq.wang | af363dc | 2024-08-06 20:16:00 -0700 | [diff] [blame^] | 166 | static int gnss_ctrl_write(char* str) |
| 167 | { |
| 168 | int fd = -1; |
| 169 | int ret = -1; |
| 170 | fd = open((const char*)gnss_ctrl_path, O_WRONLY); |
| 171 | if (fd < 0) |
| 172 | { |
| 173 | LOGE("open %s fail", gnss_ctrl_path); |
| 174 | return -1; |
| 175 | } |
| 176 | else |
| 177 | { |
| 178 | ret = write(fd, str, strlen(str)); |
| 179 | close(fd); |
| 180 | if(ret < 0) |
| 181 | { |
| 182 | LOGE("write %s fail", str); |
| 183 | return -1; |
| 184 | } |
| 185 | } |
| 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | static int gnss_fwdl_enable() |
| 190 | { |
| 191 | int ret = -1; |
| 192 | ret = gnss_ctrl_write("fdl"); |
| 193 | if(ret < 0) |
| 194 | { |
| 195 | LOGE("gnss_ctrl_write fail"); |
| 196 | return -1; |
| 197 | } |
| 198 | return 0; |
| 199 | } |
| 200 | |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 201 | static uint32_t read_bin_file(uint8_t *path, uint8_t *buff) |
| 202 | { |
| 203 | int fp = -1; |
| 204 | int ret = 0; |
| 205 | int i = 0; |
| 206 | int size = 0; |
| 207 | |
| 208 | if (NULL == path || NULL == buff) |
| 209 | { |
| 210 | LOGE("ARG error"); |
| 211 | return 0; |
| 212 | } |
| 213 | |
| 214 | fp = open((char *)path, O_RDONLY); |
| 215 | if(fp < 0) |
| 216 | { |
| 217 | LOGE( "open file failed ! errno is %d", errno); |
| 218 | return 0; |
| 219 | } |
| 220 | |
| 221 | size = lseek(fp, 0x00, SEEK_END); |
| 222 | if(size <= 0) |
| 223 | { |
| 224 | LOGE( "file is empty."); |
| 225 | return 0; |
| 226 | } |
| 227 | |
| 228 | LOGD( "file size is:%d", size); |
| 229 | lseek(fp, 0x00, SEEK_SET); |
| 230 | while(1) |
| 231 | { |
| 232 | ret = read(fp, buff, READ_LEN_MAX); |
| 233 | i += ret; |
| 234 | if(ret == READ_LEN_MAX) |
| 235 | { |
| 236 | buff += READ_LEN_MAX; |
| 237 | } |
| 238 | else |
| 239 | { |
| 240 | break; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | LOGD("file size is:%d,i:%d", size, i); |
| 245 | close(fp); |
| 246 | if(size != i) |
| 247 | { |
| 248 | return 0; |
| 249 | } |
| 250 | return size; |
| 251 | } |
| 252 | |
| 253 | |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame] | 254 | static int pack_create(hd8122_id_type_enum id_type, uint8 id, uint16 data_len, const uint8 *data, uint8 *pack, int pack_len) |
| 255 | { |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 256 | if(pack == NULL || pack_len < HD8122_PACK_LEN_MIN) |
| 257 | { |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame] | 258 | return -1; |
| 259 | } |
| 260 | memset(pack, 0, pack_len); |
| 261 | uint8 *data_ptr = pack; |
| 262 | data_ptr += uint16_2_byte(HD8122_PACK_HEAD, data_ptr, false); |
| 263 | *data_ptr++ = (uint8)id_type; |
| 264 | *data_ptr++ = id; |
| 265 | data_ptr += uint16_2_byte(data_len, data_ptr, false); |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 266 | if(data_len > 0) |
| 267 | { |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame] | 268 | memcpy(data_ptr, data, data_len); |
| 269 | data_ptr += data_len; |
| 270 | } |
| 271 | data_ptr += uint16_2_byte(fletcher16(pack + 2, 4 + data_len), data_ptr, false); |
| 272 | return (data_ptr - pack); |
| 273 | } |
| 274 | |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 275 | // f1 d9 05 01 02 00 06 01 0f 38 |
| 276 | // or |
| 277 | // f1 d9 05 00 02 00 06 01 0f 38 |
| 278 | static int msg_array_change(const uint8 *pack, int pack_len, hd8122_id_ack_enum *ack_nak) |
| 279 | { |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 280 | if(pack_len == 0 || pack_len % 10) |
| 281 | { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 282 | LOGE("pack_len(%d) error.", pack_len); |
| 283 | return -1; |
| 284 | } |
| 285 | int count = pack_len / 10; |
| 286 | int i = 0; |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 287 | while(i < count) |
| 288 | { |
b.liu | 778645e | 2024-06-21 16:47:42 +0800 | [diff] [blame] | 289 | const uint8 *ptr = pack + i * 10; |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 290 | if(ptr[0] != 0xf1 || ptr[1] != 0xd9) |
| 291 | { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 292 | LOGE("Pack head error : %02x %02x", ptr[0], ptr[1]); |
| 293 | return -1; |
| 294 | } |
| 295 | |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 296 | if(ptr[2] != 0x05) |
| 297 | { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 298 | LOGE("Type not 0x05 : %02x", ptr[2]); |
| 299 | return -1; |
| 300 | } |
| 301 | |
| 302 | int index = msg_find(ptr[6], ptr[7]); |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 303 | if(index >= 0) |
| 304 | { |
| 305 | if(ptr[3] == 0x01) |
| 306 | { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 307 | msg_array[index].ack_nak = HD8122_ID_ACK_ACK; |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 308 | } |
| 309 | else if(ptr[3] == 0x00) |
| 310 | { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 311 | msg_array[index].ack_nak = HD8122_ID_ACK_NAK; |
| 312 | |
| 313 | // There is a nak as a failure. |
| 314 | *ack_nak = HD8122_ID_ACK_NAK; |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 315 | } |
| 316 | else |
| 317 | { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 318 | LOGE("ID not 0x00 or 0x01 : %02x", ptr[3]); |
| 319 | return -1; |
| 320 | } |
| 321 | |
| 322 | msg_array[index].enable = FALSE; |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 323 | } |
| 324 | else |
| 325 | { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 326 | LOGE("Unknown gid - %d, sid - %d", ptr[6], ptr[7]); |
| 327 | return -1; |
| 328 | } |
| 329 | i++; |
| 330 | } |
| 331 | |
| 332 | return 0; |
| 333 | } |
| 334 | |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 335 | static void gnss_cmd_rsp_process(const void *data, int data_len) |
| 336 | { |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame] | 337 | const char *ptr = (const char*)data; |
| 338 | log_hex("RSP", data, data_len); |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame] | 339 | |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 340 | hd8122_id_ack_enum ack_nak = HD8122_ID_ACK_ACK; |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 341 | if(!msg_array_change((const uint8*)data, data_len, &ack_nak)) |
| 342 | { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 343 | if(setting_waitting && msg_count() == 0) |
| 344 | { |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 345 | if(ack_nak == HD8122_ID_ACK_ACK) |
| 346 | { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 347 | gnss_set_result = GNSS_ERR_OK; |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 348 | } |
| 349 | else |
| 350 | { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 351 | gnss_set_result = GNSS_ERR_UNKNOWN; |
| 352 | } |
| 353 | |
| 354 | mbtk_timer_clear(); |
| 355 | |
| 356 | pthread_mutex_lock(&read_mutex); |
| 357 | pthread_cond_signal(&read_cond); |
| 358 | pthread_mutex_unlock(&read_mutex); |
| 359 | setting_waitting = FALSE; |
| 360 | } |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 361 | } |
| 362 | else |
| 363 | { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 364 | LOGW("Unknown rsp data."); |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame] | 365 | } |
| 366 | } |
| 367 | |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 368 | static gnss_err_enum gnss_8122_reset(int fd, uint8 reset) |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame] | 369 | { |
| 370 | uint8 buff[GNSS_PACK_BUFF_SIZE]; |
| 371 | LOGD("RESET"); |
| 372 | int len = pack_create(HD8122_ID_TYPE_CFG, HD8122_ID_CFG_SIMPLERST, 1, (uint8*)(&reset), buff, sizeof(buff)); |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 373 | if(len <= 0) |
| 374 | { |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame] | 375 | LOGE("pack_create() fail."); |
| 376 | return GNSS_ERR_ARG; |
| 377 | } |
| 378 | log_hex("PACK", buff, len); |
| 379 | gnss_write(fd, buff, len); |
| 380 | return GNSS_ERR_OK; |
| 381 | } |
b.liu | f9fbfa1 | 2024-06-14 15:53:59 +0800 | [diff] [blame] | 382 | |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 383 | static gnss_err_enum gnss_8122_syscfg(int fd, uint32 mode) |
| 384 | { |
| 385 | uint8 buff[GNSS_PACK_BUFF_SIZE]; |
| 386 | LOGD("SYSCFG"); |
| 387 | //uint8 mode_str[4]; |
| 388 | //uint32_2_byte(mode, mode_str, TRUE); |
| 389 | int len = pack_create(HD8122_ID_TYPE_CFG, HD8122_ID_CFG_NAVSAT, 4, (uint8*)(&mode), buff, sizeof(buff)); |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 390 | if(len <= 0) |
| 391 | { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 392 | LOGE("pack_create() fail."); |
| 393 | return GNSS_ERR_ARG; |
| 394 | } |
| 395 | log_hex("PACK", buff, len); |
| 396 | gnss_write(fd, buff, len); |
| 397 | msg_insert(HD8122_ID_TYPE_CFG, HD8122_ID_CFG_NAVSAT); |
| 398 | return GNSS_ERR_OK; |
| 399 | } |
| 400 | |
yq.wang | 6a3437f | 2024-07-27 02:57:20 -0700 | [diff] [blame] | 401 | static gnss_err_enum gnss_8122_freqcfg(int fd, uint8 mode) |
| 402 | { |
| 403 | uint8 buff[GNSS_PACK_BUFF_SIZE]; |
| 404 | LOGD("FREQCFG"); |
| 405 | uint8 data[20]; |
| 406 | memset(data, 0x00, 20); |
| 407 | data[1] = mode; |
| 408 | data[2] = 0x66; |
| 409 | int len = pack_create(HD8122_ID_TYPE_CFG, HD8122_ID_CFG_PWRCTL, 20, data, buff, sizeof(buff)); |
| 410 | if(len <= 0) |
| 411 | { |
| 412 | LOGE("pack_create() fail."); |
| 413 | return GNSS_ERR_ARG; |
| 414 | } |
| 415 | log_hex("PACK", buff, len); |
| 416 | gnss_write(fd, buff, len); |
| 417 | msg_insert(HD8122_ID_TYPE_CFG, HD8122_ID_CFG_PWRCTL); |
| 418 | return GNSS_ERR_OK; |
| 419 | } |
| 420 | |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 421 | static gnss_err_enum gnss_8122_msgcfg(int fd, uint8 type, uint8 id, uint8 period) |
| 422 | { |
| 423 | uint8 buff[GNSS_PACK_BUFF_SIZE]; |
| 424 | LOGD("MSGCFG"); |
| 425 | uint8 data[3]; |
| 426 | data[0] = type; |
| 427 | data[1] = id; |
| 428 | data[2] = period; |
| 429 | int len = pack_create(HD8122_ID_TYPE_CFG, HD8122_ID_CFG_MSG, 3, data, buff, sizeof(buff)); |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 430 | if(len <= 0) |
| 431 | { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 432 | LOGE("pack_create() fail."); |
| 433 | return GNSS_ERR_ARG; |
| 434 | } |
| 435 | log_hex("PACK", buff, len); |
| 436 | gnss_write(fd, buff, len); |
| 437 | msg_insert(HD8122_ID_TYPE_CFG, HD8122_ID_CFG_MSG); |
| 438 | return GNSS_ERR_OK; |
| 439 | } |
| 440 | |
| 441 | static gnss_err_enum gnss_8122_minel(int fd, float *elev) |
| 442 | { |
| 443 | uint8 buff[GNSS_PACK_BUFF_SIZE]; |
| 444 | LOGD("ELEV"); |
| 445 | //uint8 elev_buff[4]; |
| 446 | //uint32_2_byte((uint32)elev, elev_buff, TRUE); |
| 447 | int len = pack_create(HD8122_ID_TYPE_CFG, HD8122_ID_CFG_ELEV, 8, (uint8*)elev, buff, sizeof(buff)); |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 448 | if(len <= 0) |
| 449 | { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 450 | LOGE("pack_create() fail."); |
| 451 | return GNSS_ERR_ARG; |
| 452 | } |
| 453 | log_hex("PACK", buff, len); |
| 454 | gnss_write(fd, buff, len); |
| 455 | return GNSS_ERR_OK; |
| 456 | } |
| 457 | |
| 458 | static gnss_err_enum gnss_8122_nmeaver(int fd, uint8 ver) |
| 459 | { |
| 460 | uint8 buff[GNSS_PACK_BUFF_SIZE]; |
| 461 | LOGD("NMEA-VER"); |
| 462 | int len = pack_create(HD8122_ID_TYPE_CFG, HD8122_ID_CFG_NMEAVER, 1, (uint8*)(&ver), buff, sizeof(buff)); |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 463 | if(len <= 0) |
| 464 | { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 465 | LOGE("pack_create() fail."); |
| 466 | return GNSS_ERR_ARG; |
| 467 | } |
| 468 | log_hex("PACK", buff, len); |
| 469 | gnss_write(fd, buff, len); |
| 470 | return GNSS_ERR_OK; |
| 471 | } |
| 472 | |
b.liu | f9fbfa1 | 2024-06-14 15:53:59 +0800 | [diff] [blame] | 473 | int gnss_8122_dev_open() |
| 474 | { |
yq.wang | 5fe6e61 | 2024-07-17 01:18:12 -0700 | [diff] [blame] | 475 | //return mbtk_gpio_value_set(GNSS_POWER_GPIO, MBTK_GPIO_DIRECT_OUT, 1); |
| 476 | system("i2cset -y -f 2 0x31 0x15 0x86"); |
| 477 | return 0; |
b.liu | f9fbfa1 | 2024-06-14 15:53:59 +0800 | [diff] [blame] | 478 | } |
| 479 | |
b.liu | 978f543 | 2024-07-01 18:04:18 +0800 | [diff] [blame] | 480 | int gnss_8122_dev_close(int fd) |
b.liu | f9fbfa1 | 2024-06-14 15:53:59 +0800 | [diff] [blame] | 481 | { |
yq.wang | 5fe6e61 | 2024-07-17 01:18:12 -0700 | [diff] [blame] | 482 | //return mbtk_gpio_value_set(GNSS_POWER_GPIO, MBTK_GPIO_DIRECT_OUT, 0); |
| 483 | system("i2cset -y -f 2 0x31 0x15 0x00"); |
| 484 | return 0; |
b.liu | f9fbfa1 | 2024-06-14 15:53:59 +0800 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | int gnss_8122_open(const char *dev) |
| 488 | { |
| 489 | pthread_mutex_init(&read_mutex, NULL); |
| 490 | pthread_cond_init(&read_cond, NULL); |
| 491 | return gnss_port_open(dev, O_RDWR | O_NONBLOCK | O_NOCTTY, UART_BITRATE_NMEA_DEF_FW, TRUE); |
| 492 | } |
| 493 | |
| 494 | int gnss_8122_close(int fd) |
| 495 | { |
| 496 | pthread_mutex_destroy(&read_mutex); |
| 497 | pthread_cond_destroy(&read_cond); |
| 498 | return gnss_port_close(fd); |
| 499 | } |
| 500 | |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 501 | int gnss_8122_fw_dl(int fd, const char *fw_name, const char *dev) |
b.liu | f9fbfa1 | 2024-06-14 15:53:59 +0800 | [diff] [blame] | 502 | { |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 503 | int uart_fd = OpenUart(dev);//PORT_NAME234 |
| 504 | if (uart_fd < 0) |
| 505 | { |
| 506 | LOGE("open uart failed %d[%d]", uart_fd, errno); |
| 507 | return GNSS_ERR_OPEN_DEV; |
| 508 | } |
| 509 | |
yq.wang | af363dc | 2024-08-06 20:16:00 -0700 | [diff] [blame^] | 510 | uint8_t fw_path[256] = {0}; |
| 511 | memset(fw_path, 0x0, 256); |
| 512 | if(memcmp(fw_name, "gp_gl_ga", 8) == 0) |
| 513 | { |
| 514 | memcpy(fw_path, GNSS_FW_GQALS_PATH, strlen(GNSS_FW_GQALS_PATH)); |
| 515 | } |
| 516 | else if(memcmp(fw_name, "gp_bd_ga", 8) == 0) |
| 517 | { |
| 518 | memcpy(fw_path, GNSS_FW_GAQBS_PATH, strlen(GNSS_FW_GAQBS_PATH)); |
| 519 | } |
| 520 | else |
| 521 | { |
| 522 | LOGE("fw dl param error"); |
| 523 | return GNSS_ERR_ARG; |
| 524 | } |
| 525 | |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 526 | uint8_t *g_bin_buff = (uint8_t*)malloc(500*1024); |
| 527 | if(g_bin_buff == NULL) { |
| 528 | LOGE("malloc() fail : %d", errno); |
| 529 | return GNSS_ERR_UNKNOWN; |
| 530 | } |
| 531 | memset(g_bin_buff, 0, 500*1024); |
yq.wang | af363dc | 2024-08-06 20:16:00 -0700 | [diff] [blame^] | 532 | uint32_t len = read_bin_file(fw_path, g_bin_buff); |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 533 | if (len <= 0) |
| 534 | { |
| 535 | LOGE("Read file failed ,len = %d", len); |
yq.wang | af363dc | 2024-08-06 20:16:00 -0700 | [diff] [blame^] | 536 | goto error; |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 537 | } |
yq.wang | af363dc | 2024-08-06 20:16:00 -0700 | [diff] [blame^] | 538 | if(gnss_8122_dev_open()) |
| 539 | { |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 540 | LOGE("open gnss device fail:%d", errno); |
yq.wang | af363dc | 2024-08-06 20:16:00 -0700 | [diff] [blame^] | 541 | goto error; |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 542 | } |
yq.wang | af363dc | 2024-08-06 20:16:00 -0700 | [diff] [blame^] | 543 | |
| 544 | if(gnss_fwdl_enable() < 0) |
| 545 | { |
| 546 | LOGE("gnss_fwdl_enable fail"); |
| 547 | goto error; |
| 548 | } |
| 549 | |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 550 | int ret = fw_update_boot(uart_fd, g_bin_buff, len); |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 551 | if (ret < 0) |
| 552 | { |
| 553 | LOGE("fw_update_boot() fail : %d", ret); |
yq.wang | af363dc | 2024-08-06 20:16:00 -0700 | [diff] [blame^] | 554 | goto error; |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 555 | } |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 556 | if(ret == HDBD_UPG_SUCESS) |
| 557 | { |
| 558 | LOGD("upgrade sucess!"); |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 559 | } |
| 560 | else |
| 561 | { |
| 562 | LOGD("upgrade FAIL, fail style:%d", ret); |
yq.wang | af363dc | 2024-08-06 20:16:00 -0700 | [diff] [blame^] | 563 | goto error; |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 564 | } |
yq.wang | af363dc | 2024-08-06 20:16:00 -0700 | [diff] [blame^] | 565 | |
| 566 | if(gnss_8122_dev_close(0)) |
| 567 | { |
| 568 | LOGE("close gnss device fail:%d", errno); |
| 569 | goto error; |
| 570 | } |
| 571 | free(g_bin_buff); |
| 572 | g_bin_buff = NULL; |
| 573 | uart_close(uart_fd); |
| 574 | return GNSS_ERR_OK; |
| 575 | error: |
| 576 | if(g_bin_buff) |
| 577 | { |
| 578 | free(g_bin_buff); |
| 579 | g_bin_buff = NULL; |
| 580 | } |
| 581 | if(uart_fd > 0) |
| 582 | { |
| 583 | uart_close(uart_fd); |
| 584 | uart_fd = -1; |
| 585 | } |
| 586 | return GNSS_ERR_DL_FW; |
b.liu | f9fbfa1 | 2024-06-14 15:53:59 +0800 | [diff] [blame] | 587 | } |
| 588 | |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame] | 589 | void gnss_8122_set_cb(const void *data, int data_len) |
| 590 | { |
| 591 | const char *buff = (const char*)data; |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 592 | if(setting_busy) // Has setting cmd process. |
| 593 | { |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame] | 594 | gnss_cmd_rsp_process(data, data_len); |
| 595 | } |
| 596 | } |
b.liu | f9fbfa1 | 2024-06-14 15:53:59 +0800 | [diff] [blame] | 597 | |
| 598 | gnss_err_enum gnss_8122_set(int fd, const char *cmd, void *cmd_rsp, int cmd_rsp_len) |
| 599 | { |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 600 | if(setting_busy) |
| 601 | { |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame] | 602 | return GNSS_ERR_SET_BUSY; |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 603 | } |
| 604 | else |
| 605 | { |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame] | 606 | bool should_wait_rsp = TRUE; |
| 607 | setting_busy = TRUE; |
| 608 | gnss_set_rsp_ptr = cmd_rsp; |
| 609 | gnss_set_result = GNSS_ERR_OK; |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 610 | msg_init(); |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame] | 611 | mbtk_timer_set(gnss_set_timer_cb, GNSS_SET_TIMEOUT); |
| 612 | |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 613 | if(memcmp(cmd, "$RESET", 6) == 0) // $RESET,<mode> |
| 614 | { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 615 | gnss_reset_type_enum mode = (gnss_reset_type_enum)atoi(cmd + 7); |
yq.wang | 1ddd1fd | 2024-07-25 23:00:14 -0700 | [diff] [blame] | 616 | LOGD("set reset: %d", mode); |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 617 | if(mode == GNSS_RESET_TYPE_HOT) |
| 618 | { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 619 | gnss_set_result = gnss_8122_reset(fd, 3); |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 620 | } |
| 621 | else if(mode == GNSS_RESET_TYPE_WARM) |
| 622 | { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 623 | gnss_set_result = gnss_8122_reset(fd, 2); |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 624 | } |
| 625 | else if(mode == GNSS_RESET_TYPE_COLD) |
| 626 | { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 627 | gnss_set_result = gnss_8122_reset(fd, 1); |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 628 | } |
| 629 | else |
| 630 | { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 631 | gnss_set_result = GNSS_ERR_ARG; |
| 632 | goto set_fail; |
| 633 | } |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 634 | if(gnss_set_result != GNSS_ERR_OK) |
| 635 | { |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame] | 636 | goto set_fail; |
| 637 | } |
| 638 | should_wait_rsp = FALSE; |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 639 | } |
| 640 | else if(memcmp(cmd, "$SYSCFG", 7) == 0) // $SYSCFG,<mode> |
| 641 | { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 642 | uint32 mode = 0; |
| 643 | mode = (uint32)atoi(cmd + 8); |
| 644 | uint32 new_mode = 0; |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 645 | if(((GNSS_SET_SYSCFG_GPS | GNSS_SET_SYSCFG_BDS | GNSS_SET_SYSCFG_GLO | GNSS_SET_SYSCFG_GAL) & mode) != mode) |
| 646 | { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 647 | gnss_set_result = GNSS_ERR_ARG; |
| 648 | goto set_fail; |
| 649 | } |
| 650 | |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 651 | if(mode & GNSS_SET_SYSCFG_GPS) // GPS |
| 652 | { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 653 | new_mode |= 0x00000001; |
| 654 | } |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 655 | if(mode & GNSS_SET_SYSCFG_BDS) // BDS |
| 656 | { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 657 | new_mode |= 0x00000002; |
| 658 | } |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 659 | if(mode & GNSS_SET_SYSCFG_GLO) // GLO |
| 660 | { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 661 | new_mode |= 0x00000004; |
| 662 | } |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 663 | if(mode & GNSS_SET_SYSCFG_GAL) // GAL |
| 664 | { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 665 | new_mode |= 0x00000010; |
| 666 | } |
| 667 | |
| 668 | gnss_set_result = gnss_8122_syscfg(fd, new_mode); |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 669 | if(gnss_set_result != GNSS_ERR_OK) |
| 670 | { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 671 | goto set_fail; |
| 672 | } |
| 673 | should_wait_rsp = TRUE; |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 674 | } |
yq.wang | 6a3437f | 2024-07-27 02:57:20 -0700 | [diff] [blame] | 675 | else if(memcmp(cmd, "$FREQCFG", 8) == 0) //$FREQCFG,<freq> |
| 676 | { |
| 677 | uint32 freq = 0; |
| 678 | freq = (uint32)atoi(cmd + 9); |
| 679 | LOGD("set freq: %d", freq); |
| 680 | if((GNSS_SET_FREQCFG_1 != freq) && (GNSS_SET_FREQCFG_2 != freq) && (GNSS_SET_FREQCFG_5 != freq)) |
| 681 | { |
| 682 | gnss_set_result = GNSS_ERR_ARG; |
| 683 | goto set_fail; |
| 684 | } |
| 685 | gnss_set_result = gnss_8122_freqcfg(fd, (uint8)freq); |
| 686 | if(gnss_set_result != GNSS_ERR_OK) |
| 687 | { |
| 688 | goto set_fail; |
| 689 | } |
| 690 | should_wait_rsp = TRUE; |
| 691 | } |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 692 | else if(memcmp(cmd, "$MSGCFG", 7) == 0) // $MSGCFG,<mode>,<rate> |
| 693 | { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 694 | uint32 mode; |
| 695 | int rate; |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 696 | if(2 == sscanf(cmd, "$MSGCFG,%d,%d", &mode, &rate)) |
| 697 | { |
yq.wang | 1ddd1fd | 2024-07-25 23:00:14 -0700 | [diff] [blame] | 698 | LOGD("set msgcfg: %d, %d", mode, rate); |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 699 | int time = rate / 1000; // s |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 700 | if(time < 0) |
| 701 | { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 702 | gnss_set_result = GNSS_ERR_ARG; |
| 703 | goto set_fail; |
| 704 | } |
| 705 | |
| 706 | if(((GNSS_SET_MSGCFG_RMC | GNSS_SET_MSGCFG_VTG | GNSS_SET_MSGCFG_GGA | GNSS_SET_MSGCFG_GSA |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 707 | | GNSS_SET_MSGCFG_GRS | GNSS_SET_MSGCFG_GSV | GNSS_SET_MSGCFG_GLL | GNSS_SET_MSGCFG_ZDA |
| 708 | | GNSS_SET_MSGCFG_GST | GNSS_SET_MSGCFG_TXT) & mode) != mode) |
| 709 | { |
yq.wang | 1ddd1fd | 2024-07-25 23:00:14 -0700 | [diff] [blame] | 710 | LOGD("msgcfg not support mode"); |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 711 | gnss_set_result = GNSS_ERR_ARG; |
| 712 | goto set_fail; |
| 713 | } |
| 714 | |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 715 | if(mode & GNSS_SET_MSGCFG_RMC) |
| 716 | { |
yq.wang | 1ddd1fd | 2024-07-25 23:00:14 -0700 | [diff] [blame] | 717 | gnss_set_result = gnss_8122_msgcfg(fd, 0xF0, 0x05, time); |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 718 | if(gnss_set_result != GNSS_ERR_OK) |
| 719 | { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 720 | goto set_fail; |
| 721 | } |
| 722 | } |
| 723 | |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 724 | if(mode & GNSS_SET_MSGCFG_VTG) |
| 725 | { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 726 | gnss_set_result = gnss_8122_msgcfg(fd, 0xF0, 0x06, time); |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 727 | if(gnss_set_result != GNSS_ERR_OK) |
| 728 | { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 729 | goto set_fail; |
| 730 | } |
| 731 | } |
| 732 | |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 733 | if(mode & GNSS_SET_MSGCFG_GGA) |
| 734 | { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 735 | gnss_set_result = gnss_8122_msgcfg(fd, 0xF0, 0x00, time); |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 736 | if(gnss_set_result != GNSS_ERR_OK) |
| 737 | { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 738 | goto set_fail; |
| 739 | } |
| 740 | } |
| 741 | |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 742 | if(mode & GNSS_SET_MSGCFG_GSA) |
| 743 | { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 744 | gnss_set_result = gnss_8122_msgcfg(fd, 0xF0, 0x02, time); |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 745 | if(gnss_set_result != GNSS_ERR_OK) |
| 746 | { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 747 | goto set_fail; |
| 748 | } |
| 749 | } |
| 750 | |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 751 | if(mode & GNSS_SET_MSGCFG_GRS) |
| 752 | { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 753 | gnss_set_result = gnss_8122_msgcfg(fd, 0xF0, 0x03, time); |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 754 | if(gnss_set_result != GNSS_ERR_OK) |
| 755 | { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 756 | goto set_fail; |
| 757 | } |
| 758 | } |
| 759 | |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 760 | if(mode & GNSS_SET_MSGCFG_GSV) |
| 761 | { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 762 | gnss_set_result = gnss_8122_msgcfg(fd, 0xF0, 0x04, time); |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 763 | if(gnss_set_result != GNSS_ERR_OK) |
| 764 | { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 765 | goto set_fail; |
| 766 | } |
| 767 | } |
| 768 | |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 769 | if(mode & GNSS_SET_MSGCFG_GLL) |
| 770 | { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 771 | gnss_set_result = gnss_8122_msgcfg(fd, 0xF0, 0x01, time); |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 772 | if(gnss_set_result != GNSS_ERR_OK) |
| 773 | { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 774 | goto set_fail; |
| 775 | } |
| 776 | } |
| 777 | |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 778 | if(mode & GNSS_SET_MSGCFG_ZDA) |
| 779 | { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 780 | gnss_set_result = gnss_8122_msgcfg(fd, 0xF0, 0x07, time); |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 781 | if(gnss_set_result != GNSS_ERR_OK) |
| 782 | { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 783 | goto set_fail; |
| 784 | } |
| 785 | } |
| 786 | |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 787 | if(mode & GNSS_SET_MSGCFG_GST) |
| 788 | { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 789 | gnss_set_result = gnss_8122_msgcfg(fd, 0xF0, 0x08, time); |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 790 | if(gnss_set_result != GNSS_ERR_OK) |
| 791 | { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 792 | goto set_fail; |
| 793 | } |
| 794 | } |
| 795 | |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 796 | if(mode & GNSS_SET_MSGCFG_TXT) |
| 797 | { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 798 | gnss_set_result = gnss_8122_msgcfg(fd, 0xF0, 0x20, time); |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 799 | if(gnss_set_result != GNSS_ERR_OK) |
| 800 | { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 801 | goto set_fail; |
| 802 | } |
| 803 | } |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 804 | } |
| 805 | else |
| 806 | { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 807 | gnss_set_result = GNSS_ERR_ARG; |
| 808 | goto set_fail; |
| 809 | } |
| 810 | |
| 811 | should_wait_rsp = TRUE; |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 812 | } |
| 813 | else if(memcmp(cmd, "$MINEL", 6) == 0) // $MINEL,<elev> |
| 814 | { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 815 | #if 0 |
| 816 | float elev = 0.0f; |
| 817 | elev = (float)atof(cmd + 7); |
| 818 | // LOGD("ELEV : %f", elev); |
| 819 | // log_hex("ELEV", &elev, sizeof(double)); |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 820 | if(elev < -90.0f || elev > 90.0f) |
| 821 | { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 822 | gnss_set_result = GNSS_ERR_ARG; |
| 823 | goto set_fail; |
| 824 | } |
| 825 | float elevs[2]; |
| 826 | elevs[0] = elev; |
| 827 | elevs[1] = elev; |
| 828 | gnss_set_result = gnss_8122_minel(fd, elevs); |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 829 | if(gnss_set_result != GNSS_ERR_OK) |
| 830 | { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 831 | goto set_fail; |
| 832 | } |
| 833 | should_wait_rsp = FALSE; |
| 834 | #else |
| 835 | gnss_set_result = GNSS_ERR_UNSUPPORT; |
| 836 | goto set_fail; |
| 837 | #endif |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 838 | } |
| 839 | else if(memcmp(cmd, "$NMEACFG", 8) == 0) // $NMEACFG,<ver> |
| 840 | { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 841 | #if 0 |
| 842 | gnss_memaver_type_enum version = (gnss_memaver_type_enum)atoi(cmd + 9); |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 843 | if(version == GNSS_MEMAVER_TYPE_3_0) |
| 844 | { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 845 | gnss_set_result = gnss_8122_nmeaver(fd, 1); |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 846 | } |
| 847 | else if(version == GNSS_MEMAVER_TYPE_4_0) |
| 848 | { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 849 | gnss_set_result = gnss_8122_nmeaver(fd, 2); |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 850 | } |
| 851 | else if(version == GNSS_MEMAVER_TYPE_4_1) |
| 852 | { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 853 | gnss_set_result = gnss_8122_nmeaver(fd, 3); |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 854 | } |
| 855 | else |
| 856 | { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 857 | gnss_set_result = GNSS_ERR_ARG; |
| 858 | goto set_fail; |
| 859 | } |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 860 | if(gnss_set_result != GNSS_ERR_OK) |
| 861 | { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 862 | goto set_fail; |
| 863 | } |
| 864 | should_wait_rsp = FALSE; |
| 865 | #else |
| 866 | gnss_set_result = GNSS_ERR_UNSUPPORT; |
| 867 | goto set_fail; |
| 868 | #endif |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame] | 869 | } |
| 870 | else |
| 871 | { |
| 872 | LOGW("Unknown cmd:%s", cmd); |
| 873 | gnss_set_result = GNSS_ERR_UNSUPPORT; |
| 874 | goto set_fail; |
| 875 | } |
| 876 | |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 877 | set_success: |
| 878 | if(should_wait_rsp) |
| 879 | { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 880 | setting_waitting = TRUE; |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame] | 881 | pthread_mutex_lock(&read_mutex); |
| 882 | pthread_cond_wait(&read_cond, &read_mutex); |
| 883 | pthread_mutex_unlock(&read_mutex); |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 884 | } |
| 885 | else |
| 886 | { |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame] | 887 | mbtk_timer_clear(); |
| 888 | } |
| 889 | |
| 890 | setting_busy = FALSE; |
| 891 | return gnss_set_result; |
b.liu | dbc3f4b | 2024-06-25 18:22:24 +0800 | [diff] [blame] | 892 | set_fail: |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame] | 893 | setting_busy = FALSE; |
| 894 | mbtk_timer_clear(); |
| 895 | return gnss_set_result; |
| 896 | } |
b.liu | f9fbfa1 | 2024-06-14 15:53:59 +0800 | [diff] [blame] | 897 | } |
| 898 | |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame] | 899 | |