b.liu | 1c74d69 | 2024-08-14 17:43:59 +0800 | [diff] [blame] | 1 | #include <time.h> |
| 2 | #include <pthread.h> |
| 3 | #include <stdio.h> |
| 4 | #include <stdlib.h> |
| 5 | #include <string.h> |
| 6 | |
| 7 | #include "lynq_gnss.h" |
| 8 | |
| 9 | #define QSER_RESULT_SUCCESS 0 |
| 10 | #define QSER_RESULT_FAIL -1 |
| 11 | #define QSER_GNSS_TIMEOUT 5 |
| 12 | |
| 13 | /**********************************VARIABLE***********************************/ |
| 14 | static bool inited = FALSE; |
| 15 | static uint32_t qser_h_gnss = 0x5F6F7F8F; |
| 16 | gnss_handler_func_t qser_gnss_callback = NULL; |
| 17 | static time_t qser_gnss_time = 0; |
| 18 | qser_agps_info qser_agps_info_save = {0}; |
| 19 | gnss_async_func_t qser_gnss_async_callback = NULL; |
| 20 | |
| 21 | extern long timezone; |
| 22 | /**********************************VARIABLE***********************************/ |
| 23 | |
| 24 | /**********************************FUNC***********************************/ |
| 25 | static void qser_gnss_async_set_cb(gnss_async_func_t cb) |
| 26 | { |
| 27 | qser_gnss_async_callback = cb; |
| 28 | } |
| 29 | |
| 30 | static gnss_async_func_t qser_gnss_async_get_cb(void) |
| 31 | { |
| 32 | return qser_gnss_async_callback; |
| 33 | } |
| 34 | |
| 35 | |
| 36 | static time_t qser_get_timestamp(char *time) |
| 37 | { |
| 38 | char tmp_char[4] = {0}; |
| 39 | struct tm* tmp_time = (struct tm*)malloc(sizeof(struct tm)); |
| 40 | |
| 41 | memset(tmp_time, 0, sizeof(struct tm)); |
| 42 | memset(tmp_char, 0, sizeof(tmp_char)); |
| 43 | memcpy(tmp_char, &time[4], 2); |
| 44 | tmp_time->tm_sec = atoi(tmp_char); |
| 45 | memcpy(tmp_char, &time[2], 2); |
| 46 | tmp_time->tm_min = atoi(tmp_char); |
| 47 | memcpy(tmp_char, &time[0], 2); |
| 48 | tmp_time->tm_hour = atoi(tmp_char); |
| 49 | memcpy(tmp_char, &time[6], 2); |
| 50 | tmp_time->tm_mday = atoi(tmp_char); |
| 51 | memcpy(tmp_char, &time[8], 2); |
| 52 | tmp_time->tm_mon = atoi(tmp_char) - 1; |
| 53 | memcpy(tmp_char, &time[10], 2); |
| 54 | tmp_time->tm_year = 100 + atoi(tmp_char); |
| 55 | |
| 56 | time_t _t = mktime(tmp_time);//按当地时区解析tmp_time |
| 57 | //gnss_log("timestamp: %ld\n",_t); |
| 58 | tzset(); // 自动设置本地时区 |
| 59 | _t = _t - timezone; |
| 60 | //gnss_log("timestamp: %ld\n",_t); |
| 61 | |
| 62 | free(tmp_time); |
| 63 | return _t; |
| 64 | } |
| 65 | |
| 66 | static time_t qser_get_gnss_time_sec(const void *data, int data_len) |
| 67 | { |
| 68 | int i = 0, num = 0; |
| 69 | const char *nmea = (const char *)data; |
| 70 | char time[15] = {0}; |
| 71 | char *check_state = NULL; |
| 72 | |
| 73 | //$GNRMC,024142.000,A,3039.364421,N,10403.417935,E,0.051,0.00,030124,,E,A*00 |
| 74 | check_state = strstr(nmea, "RMC"); |
| 75 | if(check_state != NULL) |
| 76 | { |
| 77 | for(i = 0; i < data_len; i++) |
| 78 | { |
| 79 | if(check_state[i] == ',') |
| 80 | { |
| 81 | num++; |
| 82 | i++; |
| 83 | if(num == 1)//get time |
| 84 | { |
| 85 | if(check_state[i] >= '0' && check_state[i] <= '9') |
| 86 | { |
| 87 | memcpy(time, check_state + i, 6); |
| 88 | LOGE("[qser_gnss] %s.", time); |
| 89 | } |
| 90 | else |
| 91 | { |
| 92 | qser_gnss_time = 0; |
| 93 | return qser_gnss_time; |
| 94 | } |
| 95 | } |
| 96 | else if(num == 9)//get date |
| 97 | { |
| 98 | if(check_state[i] >= '0' && check_state[i] <= '9') |
| 99 | { |
| 100 | memcpy(time + 6, check_state + i, 6); |
| 101 | LOGE("[qser_gnss] %s.", time); |
| 102 | break; |
| 103 | } |
| 104 | else |
| 105 | { |
| 106 | qser_gnss_time = 0; |
| 107 | return qser_gnss_time; |
| 108 | } |
| 109 | } |
| 110 | else if(num > 9) |
| 111 | { |
| 112 | qser_gnss_time = 0; |
| 113 | return qser_gnss_time; |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | qser_gnss_time = qser_get_timestamp(time); |
| 119 | } |
| 120 | |
| 121 | return qser_gnss_time; |
| 122 | } |
| 123 | |
| 124 | static void* gnss_async_thread(void* arg) |
| 125 | { |
| 126 | qser_gnss_error_e state = QSER_GNSS_ERROR_SUCCESS; |
| 127 | gnss_async_func_t cb = qser_gnss_async_get_cb(); |
| 128 | int ret = qser_Gnss_Start(qser_h_gnss); |
| 129 | if(ret != QSER_RESULT_SUCCESS) |
| 130 | { |
| 131 | LOGE("[qser_gnss] gnss_async_thread() fail."); |
| 132 | state = QSER_GNSS_ERROR_FAIL; |
| 133 | } |
| 134 | |
| 135 | if(cb != NULL) |
| 136 | { |
| 137 | cb(state); |
| 138 | } |
| 139 | return NULL; |
| 140 | } |
| 141 | |
| 142 | static void gnss_callback(uint32 ind_type, const void* data, uint32 data_len) |
| 143 | { |
| 144 | if(data == NULL || data_len <= 0) |
| 145 | { |
| 146 | LOGE("[qser_gnss] data is NULL."); |
| 147 | return; |
| 148 | } |
| 149 | |
| 150 | if(qser_gnss_callback == NULL) |
| 151 | { |
| 152 | //LOGE("[qser_gnss] qser_gnss_callback is NULL."); |
| 153 | return; |
| 154 | } |
| 155 | |
| 156 | if(ind_type == MBTK_GNSS_IND_LOCATION) { |
| 157 | if(data_len != sizeof(mbtk_gnss_location_info_t)) |
| 158 | { |
| 159 | LOGE("[qser_gnss] data size error"); |
| 160 | return; |
| 161 | } |
| 162 | mbtk_gnss_location_info_t *locl_info = (mbtk_gnss_location_info_t *)data; |
| 163 | mopen_location_info_t qser_locl_info; |
| 164 | memset(&qser_locl_info, 0x0, sizeof(mopen_location_info_t)); |
| 165 | qser_locl_info.latitude = locl_info->latitude; |
| 166 | qser_locl_info.longitude = locl_info->longitude; |
| 167 | qser_locl_info.altitude = locl_info->altitude; |
| 168 | qser_locl_info.speed = locl_info->speed; |
| 169 | qser_locl_info.bearing = locl_info->bearing; |
| 170 | qser_locl_info.timestamp = locl_info->timestamp; |
| 171 | qser_gnss_callback(NULL, E_MT_LOC_MSG_ID_LOCATION_INFO, (void *)(&qser_locl_info), NULL); |
| 172 | } else if(ind_type == MBTK_GNSS_IND_NMEA) { |
| 173 | mopen_gnss_nmea_info_t qser_nmea = {0}; |
| 174 | memset(&qser_nmea, 0x0, sizeof(mopen_gnss_nmea_info_t)); |
| 175 | qser_nmea.length = data_len; |
| 176 | memcpy(qser_nmea.nmea, (char *)data, data_len); |
| 177 | qser_nmea.timestamp = qser_get_gnss_time_sec(data, data_len); |
| 178 | qser_gnss_callback(NULL, E_MT_LOC_MSG_ID_NMEA_INFO, (void *)(&qser_nmea), NULL); |
| 179 | } else { |
| 180 | printf("Unknown IND : %d\n", ind_type); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | |
| 185 | /**********************************FUNC***********************************/ |
| 186 | |
| 187 | /**********************************API***********************************/ |
| 188 | int qser_Gnss_Init (uint32_t *h_gnss) |
| 189 | { |
| 190 | //UNUSED(h_gnss); |
| 191 | gnss_err_enum ret = GNSS_ERR_UNKNOWN; |
| 192 | |
| 193 | if(!inited) |
| 194 | { |
| 195 | ret = mbtk_gnss_init(gnss_callback); |
| 196 | if(ret == GNSS_ERR_OK) |
| 197 | { |
| 198 | ret = mbtk_gnss_ind_set(MBTK_GNSS_IND_NMEA, QSER_GNSS_TIMEOUT); |
| 199 | if(ret == GNSS_ERR_OK) |
| 200 | { |
| 201 | inited = TRUE; |
| 202 | } |
| 203 | else |
| 204 | { |
| 205 | LOGE("[qser_gnss] init mbtk_gnss_ind_set() fail."); |
| 206 | return QSER_RESULT_FAIL; |
| 207 | } |
| 208 | } |
| 209 | else |
| 210 | { |
| 211 | LOGE("[qser_gnss] mbtk_gnss_init() fail."); |
| 212 | return QSER_RESULT_FAIL; |
| 213 | } |
| 214 | } |
| 215 | *h_gnss = qser_h_gnss; |
| 216 | |
| 217 | return QSER_RESULT_SUCCESS; |
| 218 | } |
| 219 | |
| 220 | int qser_Gnss_Deinit (uint32_t h_gnss) |
| 221 | { |
| 222 | //UNUSED(h_gnss); |
| 223 | if(h_gnss != qser_h_gnss) |
| 224 | { |
| 225 | LOGE("[qser_gnss] h_gnss is error."); |
| 226 | return QSER_RESULT_FAIL; |
| 227 | } |
| 228 | |
| 229 | gnss_err_enum ret = GNSS_ERR_UNKNOWN; |
| 230 | |
| 231 | if(inited) |
| 232 | { |
| 233 | ret = mbtk_gnss_deinit(); |
| 234 | if(ret == GNSS_ERR_OK) |
| 235 | { |
| 236 | inited = FALSE; |
| 237 | } |
| 238 | else |
| 239 | { |
| 240 | LOGE("[qser_gnss] mbtk_gnss_init() fail."); |
| 241 | return QSER_RESULT_FAIL; |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | return QSER_RESULT_SUCCESS; |
| 246 | } |
| 247 | |
| 248 | int qser_AddRxIndMsgHandler (gnss_handler_func_t handler_ptr,uint32_t h_gnss) |
| 249 | { |
| 250 | //UNUSED(handler_ptr); |
| 251 | //UNUSED(h_gnss); |
| 252 | if(h_gnss != qser_h_gnss) |
| 253 | { |
| 254 | LOGE("[qser_gnss] h_gnss is error."); |
| 255 | return QSER_RESULT_FAIL; |
| 256 | } |
| 257 | |
| 258 | if(handler_ptr == NULL) |
| 259 | { |
| 260 | LOGE("[qser_gnss] handler_ptr is NULL."); |
| 261 | return QSER_RESULT_FAIL; |
| 262 | } |
| 263 | |
| 264 | qser_gnss_callback = handler_ptr; |
| 265 | return QSER_RESULT_SUCCESS; |
| 266 | } |
| 267 | |
| 268 | int qser_Set_Indications (uint32_t h_gnss,e_msg_id_t type) |
| 269 | { |
| 270 | //UNUSED(h_gnss); |
| 271 | //UNUSED(type); |
| 272 | if(h_gnss != qser_h_gnss) |
| 273 | { |
| 274 | LOGE("[qser_gnss] h_gnss is error."); |
| 275 | return QSER_RESULT_FAIL; |
| 276 | } |
| 277 | |
| 278 | gnss_err_enum ret = GNSS_ERR_OK; |
| 279 | if(type == E_MT_LOC_MSG_ID_LOCATION_INFO) |
| 280 | { |
| 281 | ret = mbtk_gnss_ind_set(MBTK_GNSS_IND_LOCATION, QSER_GNSS_TIMEOUT); |
| 282 | } |
| 283 | else if(type == E_MT_LOC_MSG_ID_NMEA_INFO) |
| 284 | { |
| 285 | ret = mbtk_gnss_ind_set(MBTK_GNSS_IND_NMEA, QSER_GNSS_TIMEOUT); |
| 286 | } |
| 287 | else |
| 288 | { |
| 289 | LOGE("[qser_gnss] type is fail."); |
| 290 | return QSER_RESULT_FAIL; |
| 291 | } |
| 292 | |
| 293 | if(ret != GNSS_ERR_OK) |
| 294 | { |
| 295 | LOGE("[qser_gnss] mbtk_gnss_ind_set() fail."); |
| 296 | return QSER_RESULT_FAIL; |
| 297 | } |
| 298 | return QSER_RESULT_SUCCESS; |
| 299 | } |
| 300 | |
| 301 | int qser_Gnss_Set_Async_Callback(gnss_async_func_t cb) |
| 302 | { |
| 303 | qser_gnss_async_set_cb(cb); |
| 304 | return QSER_RESULT_SUCCESS; |
| 305 | } |
| 306 | |
| 307 | int qser_Gnss_Start (uint32_t h_gnss) |
| 308 | { |
| 309 | //UNUSED(h_gnss); |
| 310 | if(h_gnss != qser_h_gnss) |
| 311 | { |
| 312 | LOGE("[qser_gnss] h_gnss is error."); |
| 313 | return QSER_RESULT_FAIL; |
| 314 | } |
| 315 | |
| 316 | gnss_err_enum ret = GNSS_ERR_UNKNOWN; |
| 317 | |
| 318 | ret = mbtk_gnss_open(255, QSER_GNSS_TIMEOUT); |
| 319 | if(ret != GNSS_ERR_OK) |
| 320 | { |
| 321 | LOGE("[qser_gnss] mbtk_gnss_open is error."); |
| 322 | return QSER_RESULT_FAIL; |
| 323 | } |
| 324 | |
| 325 | return QSER_RESULT_SUCCESS; |
| 326 | } |
| 327 | |
| 328 | int qser_Gnss_Start_Async(uint32_t h_gnss) |
| 329 | { |
| 330 | //UNUSED(h_gnss); |
| 331 | if(h_gnss != qser_h_gnss) |
| 332 | { |
| 333 | LOGE("[qser_gnss] h_gnss is error."); |
| 334 | return QSER_RESULT_FAIL; |
| 335 | } |
| 336 | |
| 337 | pthread_attr_t thread_attr; |
| 338 | pthread_t gnss_thread_id; |
| 339 | pthread_attr_init(&thread_attr); |
| 340 | if(pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED)) |
| 341 | { |
| 342 | LOGE("[qser_gnss] pthread_attr_setdetachstate() fail."); |
| 343 | return QSER_RESULT_FAIL; |
| 344 | } |
| 345 | |
| 346 | //memcpy(&qser_data_backup, data_call, sizeof(qser_data_call_s)); |
| 347 | if(pthread_create(&gnss_thread_id, &thread_attr, gnss_async_thread, NULL)) |
| 348 | { |
| 349 | LOGE("[qser_gnss] pthread_create() fail."); |
| 350 | return QSER_RESULT_FAIL; |
| 351 | } |
| 352 | pthread_attr_destroy(&thread_attr); |
| 353 | |
| 354 | return QSER_RESULT_SUCCESS; |
| 355 | } |
| 356 | |
| 357 | int qser_Gnss_Stop (uint32_t h_gnss) |
| 358 | { |
| 359 | //UNUSED(h_gnss); |
| 360 | if(h_gnss != qser_h_gnss) |
| 361 | { |
| 362 | LOGE("[qser_gnss] h_gnss is error."); |
| 363 | return QSER_RESULT_FAIL; |
| 364 | } |
| 365 | |
| 366 | gnss_err_enum ret = GNSS_ERR_UNKNOWN; |
| 367 | ret = mbtk_gnss_close(QSER_GNSS_TIMEOUT); |
| 368 | if(ret != GNSS_ERR_OK) |
| 369 | { |
| 370 | LOGE("[qser_gnss] mbtk_gnss_close is error."); |
| 371 | return QSER_RESULT_FAIL; |
| 372 | } |
| 373 | |
| 374 | return QSER_RESULT_SUCCESS; |
| 375 | } |
| 376 | |
| 377 | int qser_Gnss_InjectTime (uint32_t h_gnss,LYNQ_INJECT_TIME_INTO_T *time_info) |
| 378 | { |
| 379 | //UNUSED(h_gnss); |
| 380 | UNUSED(time_info); |
| 381 | |
| 382 | if(h_gnss != qser_h_gnss) |
| 383 | { |
| 384 | LOGE("[qser_gnss] h_gnss is error."); |
| 385 | return QSER_RESULT_FAIL; |
| 386 | } |
| 387 | return QSER_RESULT_SUCCESS; |
| 388 | } |
| 389 | |
| 390 | int qser_Gnss_Delete_Aiding_Data (uint32_t h_gnss,DELETE_AIDING_DATA_TYPE_T flags) |
| 391 | { |
| 392 | //UNUSED(h_gnss); |
| 393 | //UNUSED(flags); |
| 394 | |
| 395 | if(h_gnss != qser_h_gnss) |
| 396 | { |
| 397 | LOGE("[qser_gnss] h_gnss is error."); |
| 398 | return QSER_RESULT_FAIL; |
| 399 | } |
| 400 | return QSER_RESULT_SUCCESS; |
| 401 | } |
| 402 | |
| 403 | int qser_Gnss_Server_Configuration(char *host, char *id, char *password) |
| 404 | { |
| 405 | //UNUSED(host); |
| 406 | //UNUSED(id); |
| 407 | //UNUSED(password); |
| 408 | |
| 409 | if(!inited) |
| 410 | { |
| 411 | LOGE("[qser_gnss] api not init."); |
| 412 | return QSER_RESULT_FAIL; |
| 413 | } |
| 414 | |
| 415 | if(host != NULL) |
| 416 | { |
| 417 | memcpy(qser_agps_info_save.host, host, QSER_LEN_MAX); |
| 418 | } |
| 419 | |
| 420 | if(id != NULL) |
| 421 | { |
| 422 | memcpy(qser_agps_info_save.id, id, QSER_LEN_MAX); |
| 423 | } |
| 424 | |
| 425 | if(password != NULL) |
| 426 | { |
| 427 | memcpy(qser_agps_info_save.passwd, password, QSER_LEN_MAX); |
| 428 | } |
| 429 | return QSER_RESULT_SUCCESS; |
| 430 | } |
| 431 | |
| 432 | int qser_Gnss_download_tle() |
| 433 | { |
| 434 | if(!inited) |
| 435 | { |
| 436 | LOGE("[qser_gnss] api not init."); |
| 437 | return QSER_RESULT_FAIL; |
| 438 | } |
| 439 | return QSER_RESULT_SUCCESS; |
| 440 | } |
| 441 | |
| 442 | int qser_Gnss_injectEphemeris(uint32_t h_gnss) |
| 443 | { |
| 444 | //UNUSED(h_gnss); |
| 445 | |
| 446 | if(h_gnss != qser_h_gnss) |
| 447 | { |
| 448 | LOGE("[qser_gnss] h_gnss is error."); |
| 449 | return QSER_RESULT_FAIL; |
| 450 | } |
| 451 | return QSER_RESULT_SUCCESS; |
| 452 | } |
| 453 | |
| 454 | int qser_Gnss_Set_Frequency(uint32_t h_gnss, int frequency) |
| 455 | { |
| 456 | //UNUSED(h_gnss); |
| 457 | //UNUSED(frequency); |
| 458 | |
| 459 | if(h_gnss != qser_h_gnss) |
| 460 | { |
| 461 | LOGE("[qser_gnss] h_gnss is error."); |
| 462 | return QSER_RESULT_FAIL; |
| 463 | } |
| 464 | |
| 465 | if(frequency != 1 && frequency != 2 && frequency != 5) |
| 466 | { |
| 467 | LOGE("[qser_gnss] frequency out of range."); |
| 468 | return QSER_RESULT_FAIL; |
| 469 | } |
| 470 | |
| 471 | gnss_err_enum ret = GNSS_ERR_UNKNOWN; |
| 472 | char param_buf[32] = {0}; |
| 473 | int length = 0; |
| 474 | length = snprintf(param_buf, 32, "$FREQCFG,%d", frequency); |
| 475 | ret = mbtk_gnss_setting(param_buf, QSER_GNSS_TIMEOUT); |
| 476 | if(ret != GNSS_ERR_OK) |
| 477 | { |
| 478 | LOGE("[qser_gnss] mbtk_gnss_setting fail."); |
| 479 | return QSER_RESULT_FAIL; |
| 480 | } |
| 481 | return QSER_RESULT_SUCCESS; |
| 482 | } |
| 483 | /**********************************API***********************************/ |
| 484 | |
| 485 | |