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