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" |
| 29 | |
| 30 | #define UART_BITRATE_NMEA_DEF_FW 115200 // Default bitrate. |
| 31 | #define GNSS_POWER_GPIO 43 |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame] | 32 | #define GNSS_SET_TIMEOUT 3000 // 3s |
| 33 | #define GNSS_PACK_BUFF_SIZE 1024 |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 34 | #define GNSS_MSG_NUM_MAX 30 |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame] | 35 | |
b.liu | f9fbfa1 | 2024-06-14 15:53:59 +0800 | [diff] [blame] | 36 | |
| 37 | static pthread_cond_t read_cond; |
| 38 | static pthread_mutex_t read_mutex; |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 39 | static bool setting_waitting = FALSE; |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame] | 40 | static bool setting_busy = FALSE; |
| 41 | static void *gnss_set_rsp_ptr = NULL; |
| 42 | static gnss_err_enum gnss_set_result = GNSS_ERR_OK; |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 43 | static hd8122_msg_id_t msg_array[GNSS_MSG_NUM_MAX]; |
b.liu | f9fbfa1 | 2024-06-14 15:53:59 +0800 | [diff] [blame] | 44 | |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame] | 45 | int gnss_write(int fd, const void *data, int data_len); |
| 46 | |
| 47 | static uint16 fletcher16(const uint8_t* data, int data_len) { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 48 | uint32_t sum1 = 0; |
| 49 | uint32_t sum2 = 0; |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame] | 50 | int index; |
| 51 | |
| 52 | for (index = 0; index < data_len; ++index ) { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 53 | sum1 += data[index]; |
| 54 | sum2 += sum1; |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame] | 55 | } |
| 56 | |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 57 | return ((0xFF & sum2) << 8) | (0xFF & sum1); |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | static void gnss_set_timer_cb(int signo) |
| 61 | { |
| 62 | if(setting_busy) { |
| 63 | pthread_mutex_lock(&read_mutex); |
| 64 | pthread_cond_signal(&read_cond); |
| 65 | pthread_mutex_unlock(&read_mutex); |
| 66 | gnss_set_result = GNSS_ERR_TIMEOUT; |
| 67 | } |
| 68 | return; |
| 69 | } |
| 70 | |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 71 | static void msg_init() |
| 72 | { |
b.liu | 778645e | 2024-06-21 16:47:42 +0800 | [diff] [blame^] | 73 | unsigned int i = 0; |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 74 | while(i < ARRAY_SIZE(msg_array)) { |
| 75 | msg_array[i].enable = FALSE; |
| 76 | i++; |
| 77 | } |
| 78 | } |
| 79 | |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 80 | static int msg_insert(uint8 gid, uint8 sid) |
| 81 | { |
b.liu | 778645e | 2024-06-21 16:47:42 +0800 | [diff] [blame^] | 82 | unsigned int i = 0; |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 83 | while(i < ARRAY_SIZE(msg_array)) { |
| 84 | if(!msg_array[i].enable) |
| 85 | break; |
| 86 | i++; |
| 87 | } |
| 88 | |
| 89 | if(i == ARRAY_SIZE(msg_array)) { |
| 90 | LOGE("Msg full : %d", i); |
| 91 | return -1; |
| 92 | } else { |
| 93 | msg_array[i].enable = TRUE; |
| 94 | msg_array[i].gid = gid; |
| 95 | msg_array[i].sid = sid; |
| 96 | return 0; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | static int msg_find(uint8 gid, uint8 sid) |
| 101 | { |
b.liu | 778645e | 2024-06-21 16:47:42 +0800 | [diff] [blame^] | 102 | unsigned int i = 0; |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 103 | while(i < ARRAY_SIZE(msg_array)) { |
| 104 | if(msg_array[i].enable && gid == msg_array[i].gid && sid == msg_array[i].sid) |
| 105 | break; |
| 106 | i++; |
| 107 | } |
| 108 | |
| 109 | if(i == ARRAY_SIZE(msg_array)) { |
| 110 | LOGE("No found %d - %d", gid, sid); |
| 111 | return -1; |
| 112 | } else { |
| 113 | return i; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | static int msg_remove(uint8 gid, uint8 sid) |
| 118 | { |
| 119 | int i = msg_find(gid, sid); |
| 120 | if(i >= 0) { |
| 121 | msg_array[i].enable = FALSE; |
| 122 | msg_array[i].gid = 0; |
| 123 | msg_array[i].sid = 0; |
| 124 | return 0; |
| 125 | } else { |
| 126 | return -1; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | static int msg_count() |
| 131 | { |
b.liu | 778645e | 2024-06-21 16:47:42 +0800 | [diff] [blame^] | 132 | unsigned int i = 0; |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 133 | int count = 0; |
| 134 | while(i < ARRAY_SIZE(msg_array)) { |
| 135 | if(msg_array[i].enable) |
| 136 | count++; |
| 137 | i++; |
| 138 | } |
| 139 | return count; |
| 140 | } |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame] | 141 | |
| 142 | static int pack_create(hd8122_id_type_enum id_type, uint8 id, uint16 data_len, const uint8 *data, uint8 *pack, int pack_len) |
| 143 | { |
| 144 | if(pack == NULL || pack_len < HD8122_PACK_LEN_MIN) { |
| 145 | return -1; |
| 146 | } |
| 147 | memset(pack, 0, pack_len); |
| 148 | uint8 *data_ptr = pack; |
| 149 | data_ptr += uint16_2_byte(HD8122_PACK_HEAD, data_ptr, false); |
| 150 | *data_ptr++ = (uint8)id_type; |
| 151 | *data_ptr++ = id; |
| 152 | data_ptr += uint16_2_byte(data_len, data_ptr, false); |
| 153 | if(data_len > 0) { |
| 154 | memcpy(data_ptr, data, data_len); |
| 155 | data_ptr += data_len; |
| 156 | } |
| 157 | data_ptr += uint16_2_byte(fletcher16(pack + 2, 4 + data_len), data_ptr, false); |
| 158 | return (data_ptr - pack); |
| 159 | } |
| 160 | |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 161 | // f1 d9 05 01 02 00 06 01 0f 38 |
| 162 | // or |
| 163 | // f1 d9 05 00 02 00 06 01 0f 38 |
| 164 | static int msg_array_change(const uint8 *pack, int pack_len, hd8122_id_ack_enum *ack_nak) |
| 165 | { |
| 166 | if(pack_len == 0 || pack_len % 10) { |
| 167 | LOGE("pack_len(%d) error.", pack_len); |
| 168 | return -1; |
| 169 | } |
| 170 | int count = pack_len / 10; |
| 171 | int i = 0; |
| 172 | while(i < count) { |
b.liu | 778645e | 2024-06-21 16:47:42 +0800 | [diff] [blame^] | 173 | const uint8 *ptr = pack + i * 10; |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 174 | if(ptr[0] != 0xf1 || ptr[1] != 0xd9) { |
| 175 | LOGE("Pack head error : %02x %02x", ptr[0], ptr[1]); |
| 176 | return -1; |
| 177 | } |
| 178 | |
| 179 | if(ptr[2] != 0x05) { |
| 180 | LOGE("Type not 0x05 : %02x", ptr[2]); |
| 181 | return -1; |
| 182 | } |
| 183 | |
| 184 | int index = msg_find(ptr[6], ptr[7]); |
| 185 | if(index >= 0) { |
| 186 | if(ptr[3] == 0x01) { |
| 187 | msg_array[index].ack_nak = HD8122_ID_ACK_ACK; |
| 188 | } else if(ptr[3] == 0x00) { |
| 189 | msg_array[index].ack_nak = HD8122_ID_ACK_NAK; |
| 190 | |
| 191 | // There is a nak as a failure. |
| 192 | *ack_nak = HD8122_ID_ACK_NAK; |
| 193 | } else { |
| 194 | LOGE("ID not 0x00 or 0x01 : %02x", ptr[3]); |
| 195 | return -1; |
| 196 | } |
| 197 | |
| 198 | msg_array[index].enable = FALSE; |
| 199 | } else { |
| 200 | LOGE("Unknown gid - %d, sid - %d", ptr[6], ptr[7]); |
| 201 | return -1; |
| 202 | } |
| 203 | i++; |
| 204 | } |
| 205 | |
| 206 | return 0; |
| 207 | } |
| 208 | |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame] | 209 | static void gnss_cmd_rsp_process(const void *data, int data_len) { |
| 210 | const char *ptr = (const char*)data; |
| 211 | log_hex("RSP", data, data_len); |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame] | 212 | |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 213 | hd8122_id_ack_enum ack_nak = HD8122_ID_ACK_ACK; |
| 214 | if(!msg_array_change((const uint8*)data, data_len, &ack_nak)) { |
| 215 | if(setting_waitting && msg_count() == 0) |
| 216 | { |
| 217 | if(ack_nak == HD8122_ID_ACK_ACK) { |
| 218 | gnss_set_result = GNSS_ERR_OK; |
| 219 | } else { |
| 220 | gnss_set_result = GNSS_ERR_UNKNOWN; |
| 221 | } |
| 222 | |
| 223 | mbtk_timer_clear(); |
| 224 | |
| 225 | pthread_mutex_lock(&read_mutex); |
| 226 | pthread_cond_signal(&read_cond); |
| 227 | pthread_mutex_unlock(&read_mutex); |
| 228 | setting_waitting = FALSE; |
| 229 | } |
| 230 | } else { |
| 231 | LOGW("Unknown rsp data."); |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame] | 232 | } |
| 233 | } |
| 234 | |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 235 | static gnss_err_enum gnss_8122_reset(int fd, uint8 reset) |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame] | 236 | { |
| 237 | uint8 buff[GNSS_PACK_BUFF_SIZE]; |
| 238 | LOGD("RESET"); |
| 239 | int len = pack_create(HD8122_ID_TYPE_CFG, HD8122_ID_CFG_SIMPLERST, 1, (uint8*)(&reset), buff, sizeof(buff)); |
| 240 | if(len <= 0) { |
| 241 | LOGE("pack_create() fail."); |
| 242 | return GNSS_ERR_ARG; |
| 243 | } |
| 244 | log_hex("PACK", buff, len); |
| 245 | gnss_write(fd, buff, len); |
| 246 | return GNSS_ERR_OK; |
| 247 | } |
b.liu | f9fbfa1 | 2024-06-14 15:53:59 +0800 | [diff] [blame] | 248 | |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 249 | static gnss_err_enum gnss_8122_syscfg(int fd, uint32 mode) |
| 250 | { |
| 251 | uint8 buff[GNSS_PACK_BUFF_SIZE]; |
| 252 | LOGD("SYSCFG"); |
| 253 | //uint8 mode_str[4]; |
| 254 | //uint32_2_byte(mode, mode_str, TRUE); |
| 255 | int len = pack_create(HD8122_ID_TYPE_CFG, HD8122_ID_CFG_NAVSAT, 4, (uint8*)(&mode), buff, sizeof(buff)); |
| 256 | if(len <= 0) { |
| 257 | LOGE("pack_create() fail."); |
| 258 | return GNSS_ERR_ARG; |
| 259 | } |
| 260 | log_hex("PACK", buff, len); |
| 261 | gnss_write(fd, buff, len); |
| 262 | msg_insert(HD8122_ID_TYPE_CFG, HD8122_ID_CFG_NAVSAT); |
| 263 | return GNSS_ERR_OK; |
| 264 | } |
| 265 | |
| 266 | static gnss_err_enum gnss_8122_msgcfg(int fd, uint8 type, uint8 id, uint8 period) |
| 267 | { |
| 268 | uint8 buff[GNSS_PACK_BUFF_SIZE]; |
| 269 | LOGD("MSGCFG"); |
| 270 | uint8 data[3]; |
| 271 | data[0] = type; |
| 272 | data[1] = id; |
| 273 | data[2] = period; |
| 274 | int len = pack_create(HD8122_ID_TYPE_CFG, HD8122_ID_CFG_MSG, 3, data, buff, sizeof(buff)); |
| 275 | if(len <= 0) { |
| 276 | LOGE("pack_create() fail."); |
| 277 | return GNSS_ERR_ARG; |
| 278 | } |
| 279 | log_hex("PACK", buff, len); |
| 280 | gnss_write(fd, buff, len); |
| 281 | msg_insert(HD8122_ID_TYPE_CFG, HD8122_ID_CFG_MSG); |
| 282 | return GNSS_ERR_OK; |
| 283 | } |
| 284 | |
| 285 | static gnss_err_enum gnss_8122_minel(int fd, float *elev) |
| 286 | { |
| 287 | uint8 buff[GNSS_PACK_BUFF_SIZE]; |
| 288 | LOGD("ELEV"); |
| 289 | //uint8 elev_buff[4]; |
| 290 | //uint32_2_byte((uint32)elev, elev_buff, TRUE); |
| 291 | int len = pack_create(HD8122_ID_TYPE_CFG, HD8122_ID_CFG_ELEV, 8, (uint8*)elev, buff, sizeof(buff)); |
| 292 | if(len <= 0) { |
| 293 | LOGE("pack_create() fail."); |
| 294 | return GNSS_ERR_ARG; |
| 295 | } |
| 296 | log_hex("PACK", buff, len); |
| 297 | gnss_write(fd, buff, len); |
| 298 | return GNSS_ERR_OK; |
| 299 | } |
| 300 | |
| 301 | static gnss_err_enum gnss_8122_nmeaver(int fd, uint8 ver) |
| 302 | { |
| 303 | uint8 buff[GNSS_PACK_BUFF_SIZE]; |
| 304 | LOGD("NMEA-VER"); |
| 305 | int len = pack_create(HD8122_ID_TYPE_CFG, HD8122_ID_CFG_NMEAVER, 1, (uint8*)(&ver), buff, sizeof(buff)); |
| 306 | if(len <= 0) { |
| 307 | LOGE("pack_create() fail."); |
| 308 | return GNSS_ERR_ARG; |
| 309 | } |
| 310 | log_hex("PACK", buff, len); |
| 311 | gnss_write(fd, buff, len); |
| 312 | return GNSS_ERR_OK; |
| 313 | } |
| 314 | |
b.liu | f9fbfa1 | 2024-06-14 15:53:59 +0800 | [diff] [blame] | 315 | int gnss_8122_dev_open() |
| 316 | { |
| 317 | return mbtk_gpio_value_set(GNSS_POWER_GPIO, MBTK_GPIO_DIRECT_OUT, 1); |
| 318 | } |
| 319 | |
| 320 | int gnss_8122_dev_close() |
| 321 | { |
| 322 | return mbtk_gpio_value_set(GNSS_POWER_GPIO, MBTK_GPIO_DIRECT_OUT, 0); |
| 323 | } |
| 324 | |
| 325 | int gnss_8122_open(const char *dev) |
| 326 | { |
| 327 | pthread_mutex_init(&read_mutex, NULL); |
| 328 | pthread_cond_init(&read_cond, NULL); |
| 329 | return gnss_port_open(dev, O_RDWR | O_NONBLOCK | O_NOCTTY, UART_BITRATE_NMEA_DEF_FW, TRUE); |
| 330 | } |
| 331 | |
| 332 | int gnss_8122_close(int fd) |
| 333 | { |
| 334 | pthread_mutex_destroy(&read_mutex); |
| 335 | pthread_cond_destroy(&read_cond); |
| 336 | return gnss_port_close(fd); |
| 337 | } |
| 338 | |
b.liu | 99c645d | 2024-06-20 10:52:15 +0800 | [diff] [blame] | 339 | int gnss_8122_fw_dl(int fd, const char *dev) |
b.liu | f9fbfa1 | 2024-06-14 15:53:59 +0800 | [diff] [blame] | 340 | { |
| 341 | return 0; |
| 342 | } |
| 343 | |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame] | 344 | void gnss_8122_set_cb(const void *data, int data_len) |
| 345 | { |
| 346 | const char *buff = (const char*)data; |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 347 | if(setting_busy) { // Has setting cmd process. |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame] | 348 | gnss_cmd_rsp_process(data, data_len); |
| 349 | } |
| 350 | } |
b.liu | f9fbfa1 | 2024-06-14 15:53:59 +0800 | [diff] [blame] | 351 | |
| 352 | gnss_err_enum gnss_8122_set(int fd, const char *cmd, void *cmd_rsp, int cmd_rsp_len) |
| 353 | { |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame] | 354 | if(setting_busy) { |
| 355 | return GNSS_ERR_SET_BUSY; |
| 356 | } else { |
| 357 | bool should_wait_rsp = TRUE; |
| 358 | setting_busy = TRUE; |
| 359 | gnss_set_rsp_ptr = cmd_rsp; |
| 360 | gnss_set_result = GNSS_ERR_OK; |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 361 | msg_init(); |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame] | 362 | mbtk_timer_set(gnss_set_timer_cb, GNSS_SET_TIMEOUT); |
| 363 | |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 364 | if(memcmp(cmd, "$RESET", 6) == 0) { // $RESET,<mode> |
| 365 | gnss_reset_type_enum mode = (gnss_reset_type_enum)atoi(cmd + 7); |
| 366 | if(mode == GNSS_RESET_TYPE_HOT) { |
| 367 | gnss_set_result = gnss_8122_reset(fd, 3); |
| 368 | } else if(mode == GNSS_RESET_TYPE_WARM) { |
| 369 | gnss_set_result = gnss_8122_reset(fd, 2); |
| 370 | } else if(mode == GNSS_RESET_TYPE_COLD) { |
| 371 | gnss_set_result = gnss_8122_reset(fd, 1); |
| 372 | } else { |
| 373 | gnss_set_result = GNSS_ERR_ARG; |
| 374 | goto set_fail; |
| 375 | } |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame] | 376 | if(gnss_set_result != GNSS_ERR_OK) { |
| 377 | goto set_fail; |
| 378 | } |
| 379 | should_wait_rsp = FALSE; |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 380 | } else if(memcmp(cmd, "$SYSCFG", 7) == 0) { // $SYSCFG,<mode> |
| 381 | uint32 mode = 0; |
| 382 | mode = (uint32)atoi(cmd + 8); |
| 383 | uint32 new_mode = 0; |
| 384 | if(((GNSS_SET_SYSCFG_GPS | GNSS_SET_SYSCFG_BDS | GNSS_SET_SYSCFG_GLO | GNSS_SET_SYSCFG_GAL) & mode) != mode) { |
| 385 | gnss_set_result = GNSS_ERR_ARG; |
| 386 | goto set_fail; |
| 387 | } |
| 388 | |
| 389 | if(mode & GNSS_SET_SYSCFG_GPS) { // GPS |
| 390 | new_mode |= 0x00000001; |
| 391 | } |
| 392 | if(mode & GNSS_SET_SYSCFG_BDS) { // BDS |
| 393 | new_mode |= 0x00000002; |
| 394 | } |
| 395 | if(mode & GNSS_SET_SYSCFG_GLO) { // GLO |
| 396 | new_mode |= 0x00000004; |
| 397 | } |
| 398 | if(mode & GNSS_SET_SYSCFG_GAL) { // GAL |
| 399 | new_mode |= 0x00000010; |
| 400 | } |
| 401 | |
| 402 | gnss_set_result = gnss_8122_syscfg(fd, new_mode); |
| 403 | if(gnss_set_result != GNSS_ERR_OK) { |
| 404 | goto set_fail; |
| 405 | } |
| 406 | should_wait_rsp = TRUE; |
| 407 | } else if(memcmp(cmd, "$MSGCFG", 7) == 0) { // $MSGCFG,<mode>,<rate> |
| 408 | uint32 mode; |
| 409 | int rate; |
| 410 | if(2 == sscanf(cmd, "$MSGCFG,%d,%d", &mode, &rate)) { |
| 411 | int time = rate / 1000; // s |
| 412 | if(time < 0) { |
| 413 | gnss_set_result = GNSS_ERR_ARG; |
| 414 | goto set_fail; |
| 415 | } |
| 416 | |
| 417 | if(((GNSS_SET_MSGCFG_RMC | GNSS_SET_MSGCFG_VTG | GNSS_SET_MSGCFG_GGA | GNSS_SET_MSGCFG_GSA |
| 418 | | GNSS_SET_MSGCFG_GRS | GNSS_SET_MSGCFG_GSV | GNSS_SET_MSGCFG_GLL | GNSS_SET_MSGCFG_ZDA |
| 419 | | GNSS_SET_MSGCFG_GST | GNSS_SET_MSGCFG_TXT) & mode) != mode) { |
| 420 | gnss_set_result = GNSS_ERR_ARG; |
| 421 | goto set_fail; |
| 422 | } |
| 423 | |
| 424 | if(mode & GNSS_SET_MSGCFG_RMC) { |
| 425 | gnss_set_result = gnss_8122_msgcfg(fd, 0xF8, 0x05, time); |
| 426 | if(gnss_set_result != GNSS_ERR_OK) { |
| 427 | goto set_fail; |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | if(mode & GNSS_SET_MSGCFG_VTG) { |
| 432 | gnss_set_result = gnss_8122_msgcfg(fd, 0xF0, 0x06, time); |
| 433 | if(gnss_set_result != GNSS_ERR_OK) { |
| 434 | goto set_fail; |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | if(mode & GNSS_SET_MSGCFG_GGA) { |
| 439 | gnss_set_result = gnss_8122_msgcfg(fd, 0xF0, 0x00, time); |
| 440 | if(gnss_set_result != GNSS_ERR_OK) { |
| 441 | goto set_fail; |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | if(mode & GNSS_SET_MSGCFG_GSA) { |
| 446 | gnss_set_result = gnss_8122_msgcfg(fd, 0xF0, 0x02, time); |
| 447 | if(gnss_set_result != GNSS_ERR_OK) { |
| 448 | goto set_fail; |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | if(mode & GNSS_SET_MSGCFG_GRS) { |
| 453 | gnss_set_result = gnss_8122_msgcfg(fd, 0xF0, 0x03, time); |
| 454 | if(gnss_set_result != GNSS_ERR_OK) { |
| 455 | goto set_fail; |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | if(mode & GNSS_SET_MSGCFG_GSV) { |
| 460 | gnss_set_result = gnss_8122_msgcfg(fd, 0xF0, 0x04, time); |
| 461 | if(gnss_set_result != GNSS_ERR_OK) { |
| 462 | goto set_fail; |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | if(mode & GNSS_SET_MSGCFG_GLL) { |
| 467 | gnss_set_result = gnss_8122_msgcfg(fd, 0xF0, 0x01, time); |
| 468 | if(gnss_set_result != GNSS_ERR_OK) { |
| 469 | goto set_fail; |
| 470 | } |
| 471 | } |
| 472 | |
| 473 | if(mode & GNSS_SET_MSGCFG_ZDA) { |
| 474 | gnss_set_result = gnss_8122_msgcfg(fd, 0xF0, 0x07, time); |
| 475 | if(gnss_set_result != GNSS_ERR_OK) { |
| 476 | goto set_fail; |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | if(mode & GNSS_SET_MSGCFG_GST) { |
| 481 | gnss_set_result = gnss_8122_msgcfg(fd, 0xF0, 0x08, time); |
| 482 | if(gnss_set_result != GNSS_ERR_OK) { |
| 483 | goto set_fail; |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | if(mode & GNSS_SET_MSGCFG_TXT) { |
| 488 | gnss_set_result = gnss_8122_msgcfg(fd, 0xF0, 0x20, time); |
| 489 | if(gnss_set_result != GNSS_ERR_OK) { |
| 490 | goto set_fail; |
| 491 | } |
| 492 | } |
| 493 | } else { |
| 494 | gnss_set_result = GNSS_ERR_ARG; |
| 495 | goto set_fail; |
| 496 | } |
| 497 | |
| 498 | should_wait_rsp = TRUE; |
| 499 | } else if(memcmp(cmd, "$MINEL", 6) == 0) { // $MINEL,<elev> |
| 500 | #if 0 |
| 501 | float elev = 0.0f; |
| 502 | elev = (float)atof(cmd + 7); |
| 503 | // LOGD("ELEV : %f", elev); |
| 504 | // log_hex("ELEV", &elev, sizeof(double)); |
| 505 | if(elev < -90.0f || elev > 90.0f) { |
| 506 | gnss_set_result = GNSS_ERR_ARG; |
| 507 | goto set_fail; |
| 508 | } |
| 509 | float elevs[2]; |
| 510 | elevs[0] = elev; |
| 511 | elevs[1] = elev; |
| 512 | gnss_set_result = gnss_8122_minel(fd, elevs); |
| 513 | if(gnss_set_result != GNSS_ERR_OK) { |
| 514 | goto set_fail; |
| 515 | } |
| 516 | should_wait_rsp = FALSE; |
| 517 | #else |
| 518 | gnss_set_result = GNSS_ERR_UNSUPPORT; |
| 519 | goto set_fail; |
| 520 | #endif |
| 521 | } else if(memcmp(cmd, "$NMEACFG", 8) == 0) { // $NMEACFG,<ver> |
| 522 | #if 0 |
| 523 | gnss_memaver_type_enum version = (gnss_memaver_type_enum)atoi(cmd + 9); |
| 524 | if(version == GNSS_MEMAVER_TYPE_3_0) { |
| 525 | gnss_set_result = gnss_8122_nmeaver(fd, 1); |
| 526 | } else if(version == GNSS_MEMAVER_TYPE_4_0) { |
| 527 | gnss_set_result = gnss_8122_nmeaver(fd, 2); |
| 528 | } else if(version == GNSS_MEMAVER_TYPE_4_1) { |
| 529 | gnss_set_result = gnss_8122_nmeaver(fd, 3); |
| 530 | } else { |
| 531 | gnss_set_result = GNSS_ERR_ARG; |
| 532 | goto set_fail; |
| 533 | } |
| 534 | if(gnss_set_result != GNSS_ERR_OK) { |
| 535 | goto set_fail; |
| 536 | } |
| 537 | should_wait_rsp = FALSE; |
| 538 | #else |
| 539 | gnss_set_result = GNSS_ERR_UNSUPPORT; |
| 540 | goto set_fail; |
| 541 | #endif |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame] | 542 | } |
| 543 | else |
| 544 | { |
| 545 | LOGW("Unknown cmd:%s", cmd); |
| 546 | gnss_set_result = GNSS_ERR_UNSUPPORT; |
| 547 | goto set_fail; |
| 548 | } |
| 549 | |
| 550 | set_success: |
| 551 | if(should_wait_rsp) { |
b.liu | d0ba715 | 2024-06-19 14:47:21 +0800 | [diff] [blame] | 552 | setting_waitting = TRUE; |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame] | 553 | pthread_mutex_lock(&read_mutex); |
| 554 | pthread_cond_wait(&read_cond, &read_mutex); |
| 555 | pthread_mutex_unlock(&read_mutex); |
| 556 | } else { |
| 557 | mbtk_timer_clear(); |
| 558 | } |
| 559 | |
| 560 | setting_busy = FALSE; |
| 561 | return gnss_set_result; |
| 562 | set_fail: |
| 563 | setting_busy = FALSE; |
| 564 | mbtk_timer_clear(); |
| 565 | return gnss_set_result; |
| 566 | } |
b.liu | f9fbfa1 | 2024-06-14 15:53:59 +0800 | [diff] [blame] | 567 | } |
| 568 | |
b.liu | 5f950c5 | 2024-06-15 20:13:12 +0800 | [diff] [blame] | 569 | |