| #include <time.h> |
| #include "lynq_gnss.h" |
| #include "mbtk_gnss_5311.h" |
| |
| #define QSER_RESULT_SUCCESS 0 |
| #define QSER_RESULT_FAIL -1 |
| |
| /**********************************VARIABLE***********************************/ |
| static bool inited = FALSE; |
| static int qser_info_handle_num = 0; |
| static uint qser_h_sim = 0x5F6F7F8F; |
| gnss_handler_func_t qser_gnss_callback = NULL; |
| e_msg_id_t qser_gnss_msg_type = E_MT_LOC_MSG_ID_NMEA_INFO; |
| static time_t qser_gnss_time = 0; |
| |
| |
| extern long timezone; |
| /**********************************VARIABLE***********************************/ |
| |
| /**********************************FUNC***********************************/ |
| static time_t qser_get_timestamp(char *time) |
| { |
| char tmp_char[4] = {0}; |
| struct tm* tmp_time = (struct tm*)malloc(sizeof(struct tm)); |
| |
| memset(tmp_time, 0, sizeof(struct tm)); |
| memset(tmp_char, 0, sizeof(tmp_char)); |
| memcpy(tmp_char, &time[4], 2); |
| tmp_time->tm_sec = atoi(tmp_char); |
| memcpy(tmp_char, &time[2], 2); |
| tmp_time->tm_min = atoi(tmp_char); |
| memcpy(tmp_char, &time[0], 2); |
| tmp_time->tm_hour = atoi(tmp_char); |
| memcpy(tmp_char, &time[6], 2); |
| tmp_time->tm_mday = atoi(tmp_char); |
| memcpy(tmp_char, &time[8], 2); |
| tmp_time->tm_mon = atoi(tmp_char) - 1; |
| memcpy(tmp_char, &time[10], 2); |
| tmp_time->tm_year = 100 + atoi(tmp_char); |
| |
| time_t _t = mktime(tmp_time);//按当地时区解析tmp_time |
| //gnss_log("timestamp: %ld\n",_t); |
| tzset(); // 自动设置本地时区 |
| _t = _t - timezone; |
| //gnss_log("timestamp: %ld\n",_t); |
| |
| free(tmp_time); |
| return _t; |
| } |
| |
| static time_t qser_get_gnss_time_sec(void *data, int data_len) |
| { |
| int i = 0, num = 0; |
| char *nmea = (char *)data; |
| char time[15] = {0}; |
| char *check_state = NULL; |
| |
| //$GNRMC,024142.000,A,3039.364421,N,10403.417935,E,0.051,0.00,030124,,E,A*00 |
| check_state = strstr(nmea, "RMC"); |
| if(check_state != NULL) |
| { |
| for(i = 0; i < data_len; i++) |
| { |
| if(check_state[i] == ',') |
| { |
| num++; |
| i++; |
| if(num == 1)//get time |
| { |
| if(check_state[i] >= '0' && check_state[i] <= '9') |
| { |
| memcpy(time, check_state + i, 6); |
| //LOGE("[qser_gnss] %s.", time); |
| } |
| else |
| { |
| qser_gnss_time = 0; |
| return qser_gnss_time; |
| } |
| } |
| else if(num == 9)//get date |
| { |
| if(check_state[i] >= '0' && check_state[i] <= '9') |
| { |
| memcpy(time + 6, check_state + i, 6); |
| //LOGE("[qser_gnss] %s.", time); |
| break; |
| } |
| else |
| { |
| qser_gnss_time = 0; |
| return qser_gnss_time; |
| } |
| } |
| else if(num > 9) |
| { |
| qser_gnss_time = 0; |
| return qser_gnss_time; |
| } |
| } |
| } |
| |
| qser_gnss_time = qser_get_timestamp(time); |
| } |
| |
| return qser_gnss_time; |
| } |
| |
| static void qser_gnss_msg_cb(void *data, int data_len) |
| { |
| if(data == NULL || data_len <= 0) |
| { |
| LOGE("[qser_gnss] data is NULL."); |
| return; |
| } |
| |
| if(qser_gnss_callback == NULL) |
| { |
| //LOGE("[qser_gnss] qser_gnss_callback is NULL."); |
| return; |
| } |
| |
| if(qser_gnss_msg_type == E_MT_LOC_MSG_ID_NMEA_INFO) |
| { |
| mopen_gnss_nmea_info_t qser_nmea = {0}; |
| memset(&qser_nmea, 0x0, sizeof(mopen_gnss_nmea_info_t)); |
| qser_nmea.length = data_len; |
| memcpy(qser_nmea.nmea, (char *)data, data_len); |
| qser_nmea.timestamp = qser_get_gnss_time_sec(data, data_len); |
| |
| if(qser_gnss_callback != NULL) |
| { |
| qser_gnss_callback(NULL, qser_gnss_msg_type, (void *)(&qser_nmea), NULL); |
| } |
| } |
| else if(qser_gnss_msg_type == E_MT_LOC_MSG_ID_LOCATION_INFO) |
| { |
| //NOT USE |
| } |
| else |
| { |
| //UNKNOWN |
| } |
| } |
| /**********************************FUNC***********************************/ |
| |
| /**********************************API***********************************/ |
| int qser_Gnss_Init (uint32_t *h_gnss) |
| { |
| //UNUSED(h_gnss); |
| MBTK_GNSS_5311_RESULT_TYPE ret = MBTK_GNSS_RESULT_FAIL; |
| |
| if(!inited) |
| { |
| ret = mbtk_gnss_init(); |
| if(ret == MBTK_GNSS_RESULT_SUCCESS) |
| { |
| qser_info_handle_num++; |
| inited = TRUE; |
| } |
| else |
| { |
| LOGE("[qser_gnss] mbtk_gnss_init() fail."); |
| return QSER_RESULT_FAIL; |
| } |
| } |
| else |
| { |
| qser_info_handle_num++; |
| inited = TRUE; |
| } |
| *h_gnss = qser_h_sim; |
| |
| return QSER_RESULT_SUCCESS; |
| } |
| |
| int qser_Gnss_Deinit (uint32_t h_gnss) |
| { |
| //UNUSED(h_gnss); |
| if(h_gnss != qser_h_sim) |
| { |
| LOGE("[qser_gnss] h_sim is error."); |
| return QSER_RESULT_FAIL; |
| } |
| |
| MBTK_GNSS_5311_RESULT_TYPE ret = MBTK_GNSS_RESULT_FAIL; |
| |
| if(qser_info_handle_num == 1) |
| { |
| ret = mbtk_gnss_deinit(); |
| if(ret == MBTK_GNSS_RESULT_SUCCESS) |
| { |
| qser_info_handle_num = 0; |
| inited = FALSE; |
| } |
| else |
| { |
| LOGE("[qser_gnss] mbtk_gnss_init() fail."); |
| return QSER_RESULT_FAIL; |
| } |
| } |
| else |
| { |
| qser_info_handle_num--; |
| } |
| |
| return QSER_RESULT_SUCCESS; |
| } |
| |
| int qser_AddRxIndMsgHandler (gnss_handler_func_t handler_ptr,uint32_t h_gnss) |
| { |
| //UNUSED(handler_ptr); |
| //UNUSED(h_gnss); |
| if(h_gnss != qser_h_sim) |
| { |
| LOGE("[qser_gnss] h_sim is error."); |
| return QSER_RESULT_FAIL; |
| } |
| |
| if(handler_ptr == NULL) |
| { |
| LOGE("[qser_gnss] handler_ptr is NULL."); |
| return QSER_RESULT_FAIL; |
| } |
| MBTK_GNSS_5311_RESULT_TYPE ret = MBTK_GNSS_RESULT_FAIL; |
| |
| qser_gnss_callback = handler_ptr; |
| return QSER_RESULT_SUCCESS; |
| } |
| |
| int qser_Set_Indications (uint32_t h_gnss,e_msg_id_t type) |
| { |
| //UNUSED(h_gnss); |
| //UNUSED(type); |
| if(h_gnss != qser_h_sim) |
| { |
| LOGE("[qser_gnss] h_sim is error."); |
| return QSER_RESULT_FAIL; |
| } |
| |
| qser_gnss_msg_type = type; |
| |
| return QSER_RESULT_SUCCESS; |
| } |
| |
| int qser_Gnss_Start (uint32_t h_gnss) |
| { |
| //UNUSED(h_gnss); |
| if(h_gnss != qser_h_sim) |
| { |
| LOGE("[qser_gnss] h_sim is error."); |
| return QSER_RESULT_FAIL; |
| } |
| |
| MBTK_GNSS_5311_RESULT_TYPE ret = MBTK_GNSS_RESULT_FAIL; |
| |
| ret = mbtk_gnss_add_nmea_out_func(qser_gnss_msg_cb); |
| if(ret != MBTK_GNSS_RESULT_SUCCESS) |
| { |
| LOGE("[qser_gnss] mbtk_gnss_add_nmea_out_func fail."); |
| return QSER_RESULT_FAIL; |
| } |
| |
| ret = mbtk_gnss_open(); |
| if(ret != MBTK_GNSS_RESULT_SUCCESS) |
| { |
| LOGE("[qser_gnss] mbtk_gnss_open is error."); |
| return QSER_RESULT_FAIL; |
| } |
| |
| return QSER_RESULT_SUCCESS; |
| } |
| |
| int qser_Gnss_Stop (uint32_t h_gnss) |
| { |
| //UNUSED(h_gnss); |
| if(h_gnss != qser_h_sim) |
| { |
| LOGE("[qser_gnss] h_sim is error."); |
| return QSER_RESULT_FAIL; |
| } |
| |
| MBTK_GNSS_5311_RESULT_TYPE ret = MBTK_GNSS_RESULT_FAIL; |
| ret = mbtk_gnss_close(); |
| if(ret != MBTK_GNSS_RESULT_SUCCESS) |
| { |
| LOGE("[qser_gnss] mbtk_gnss_close is error."); |
| return QSER_RESULT_FAIL; |
| } |
| |
| return QSER_RESULT_SUCCESS; |
| } |
| |
| int qser_Gnss_InjectTime (uint32_t h_gnss,LYNQ_INJECT_TIME_INTO_T *time_info) |
| { |
| UNUSED(h_gnss); |
| UNUSED(time_info); |
| |
| return 0; |
| } |
| |
| int qser_Gnss_Delete_Aiding_Data (uint32_t h_gnss,DELETE_AIDING_DATA_TYPE_T flags) |
| { |
| //UNUSED(h_gnss); |
| //UNUSED(flags); |
| |
| if(h_gnss != qser_h_sim) |
| { |
| LOGE("[qser_gnss] h_sim is error."); |
| return QSER_RESULT_FAIL; |
| } |
| |
| MBTK_GNSS_5311_RESULT_TYPE ret = MBTK_GNSS_RESULT_FAIL; |
| char param_buf[32] = {0}; |
| int length = 0; |
| |
| switch(flags) |
| { |
| case DELETE_NOTHING: |
| { |
| memcpy(param_buf, "RESET,1", strlen("RESET,1")); |
| break; |
| } |
| case DELETE_EPHEMERIS: |
| case DELETE_ALMANAC: |
| case DELETE_POSITION_TIME: |
| case DELETE_UTC: |
| { |
| memcpy(param_buf, "RESET,2", strlen("RESET,2")); |
| break; |
| } |
| case DELETE_ALL: |
| { |
| memcpy(param_buf, "RESET,3", strlen("RESET,3")); |
| break; |
| } |
| default: |
| { |
| LOGE("[qser_gnss] flags UNKOWN."); |
| return QSER_RESULT_FAIL; |
| } |
| } |
| |
| length = strlen(param_buf); |
| ret = mbtk_gnss_param_config(param_buf, length); |
| if(ret != MBTK_GNSS_RESULT_SUCCESS) |
| { |
| LOGE("[qser_gnss] mbtk_gnss_param_config is error."); |
| return QSER_RESULT_FAIL; |
| } |
| return QSER_RESULT_SUCCESS; |
| } |
| |
| int qser_Gnss_Server_Configuration(char *host, char *id, char *password) |
| { |
| UNUSED(host); |
| UNUSED(id); |
| UNUSED(password); |
| return 0; |
| } |
| |
| int qser_Gnss_download_tle() |
| { |
| return 0; |
| } |
| |
| int qser_Gnss_injectEphemeris(uint32_t h_gnss) |
| { |
| UNUSED(h_gnss); |
| |
| return 0; |
| } |
| |
| int qser_Gnss_Set_Frequency(uint32_t h_gnss, int frequency) |
| { |
| //UNUSED(h_gnss); |
| //UNUSED(frequency); |
| |
| if(h_gnss != qser_h_sim) |
| { |
| LOGE("[qser_gnss] h_sim is error."); |
| return QSER_RESULT_FAIL; |
| } |
| |
| if(frequency != 1 && frequency != 2 && frequency != 5) |
| { |
| LOGE("[qser_gnss] frequency out of range."); |
| return QSER_RESULT_FAIL; |
| } |
| |
| MBTK_GNSS_5311_RESULT_TYPE ret = MBTK_GNSS_RESULT_FAIL; |
| char param_buf[32] = {0}; |
| int length = 0; |
| length = snprintf(param_buf, 32, "NAVISYSCFG,7,%d", frequency); |
| ret = mbtk_gnss_param_config(param_buf, length); |
| if(ret != MBTK_GNSS_RESULT_SUCCESS) |
| { |
| LOGE("[qser_gnss] mbtk_gnss_param_config is error."); |
| return QSER_RESULT_FAIL; |
| } |
| return QSER_RESULT_SUCCESS; |
| } |
| /**********************************API***********************************/ |
| |
| |