blob: d6d7b01616f5e110984816414de809c78a2e10b5 [file] [log] [blame]
b.liu68a94c92025-05-24 12:53:41 +08001#include <stdio.h>
2#include <string.h>
3#include <strings.h>
4#include <stdlib.h>
5#include <errno.h>
6#include <fcntl.h>
7#include <signal.h>
8#include <sys/types.h>
9#include <unistd.h>
10#include <pthread.h>
11#include <termios.h>
12#include <time.h>
13#include <sys/ioctl.h>
14#include <dlfcn.h>
15#include <stdint.h>
16#include <stdbool.h>
17
zw.wang581aab12025-05-28 19:43:53 +080018#include "gsw_gnss_interface.h"
19
20#define MBTK_GNSS_IND_LOCATION (1) // 1
21#define MBTK_GNSS_IND_NMEA (1 << 1) // 2
22#define QSER_GNSS_TIMEOUT 5
b.liu68a94c92025-05-24 12:53:41 +080023
24#ifndef LOG_ERR_LEVEL
25#define LOG_ERR_LEVEL 3 /* error conditions */
26#endif
27#ifndef LOG_WARN_LEVEL
28#define LOG_WARN_LEVEL 4 /* warning conditions */
29#endif
30#ifndef LOG_INFO_LEVEL
31#define LOG_INFO_LEVEL 6 /* informational */
32#endif
33#ifndef LOG_DEBUG_LEVEL
34#define LOG_DEBUG_LEVEL 7 /* debug-level messages */
35#endif
36#ifndef LOG_VERBOSE_LEVEL
37#define LOG_VERBOSE_LEVEL 8
38#endif
39
l.yang6a42e4d2025-05-28 01:04:20 -070040#define GSW_GNSS "[HAL][GSW_GNSS]"
41
42
b.liu68a94c92025-05-24 12:53:41 +080043#define LOGV(fmt, args ...) \
44 do{ \
45 char *file_ptr_1001 = __FILE__; \
46 char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
47 char line_1001[10] = {0}; \
48 sprintf(line_1001, "%d", __LINE__); \
49 while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \
50 if(*ptr_1001 == '/') \
51 break; \
52 ptr_1001--; \
53 } \
l.yang6a42e4d2025-05-28 01:04:20 -070054 fun_ptr_log(LOG_VERBOSE_LEVEL, "%s#%s: "GSW_GNSS"" fmt, ptr_1001 + 1, line_1001, ##args); \
b.liu68a94c92025-05-24 12:53:41 +080055 } while(0)
56
57#define LOGI(fmt, args...) \
58 do{ \
59 char *file_ptr_1001 = __FILE__; \
60 char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
61 char line_1001[10] = {0}; \
62 sprintf(line_1001, "%d", __LINE__); \
63 while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \
64 if(*ptr_1001 == '/') \
65 break; \
66 ptr_1001--; \
67 } \
l.yang6a42e4d2025-05-28 01:04:20 -070068 fun_ptr_log(LOG_INFO_LEVEL, "%s#%s: "GSW_GNSS"" fmt, ptr_1001 + 1, line_1001, ##args); \
b.liu68a94c92025-05-24 12:53:41 +080069 } while(0)
70
71#define LOGD(fmt, args...) \
72 do{ \
73 char *file_ptr_1001 = __FILE__; \
74 char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
75 char line_1001[10] = {0}; \
76 sprintf(line_1001, "%d", __LINE__); \
77 while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \
78 if(*ptr_1001 == '/') \
79 break; \
80 ptr_1001--; \
81 } \
l.yang6a42e4d2025-05-28 01:04:20 -070082 fun_ptr_log(LOG_DEBUG_LEVEL, "%s#%s: "GSW_GNSS"" fmt, ptr_1001 + 1, line_1001, ##args); \
b.liu68a94c92025-05-24 12:53:41 +080083 } while(0)
84
85#define LOGW(fmt, args...) \
86 do{ \
87 char *file_ptr_1001 = __FILE__; \
88 char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
89 char line_1001[10] = {0}; \
90 sprintf(line_1001, "%d", __LINE__); \
91 while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \
92 if(*ptr_1001 == '/') \
93 break; \
94 ptr_1001--; \
95 } \
l.yang6a42e4d2025-05-28 01:04:20 -070096 fun_ptr_log(LOG_WARN_LEVEL, "%s#%s: "GSW_GNSS"" fmt, ptr_1001 + 1, line_1001, ##args); \
b.liu68a94c92025-05-24 12:53:41 +080097 } while(0)
98
99#define LOGE(fmt, args...) \
100 do{ \
101 char *file_ptr_1001 = __FILE__; \
102 char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
103 char line_1001[10] = {0}; \
104 sprintf(line_1001, "%d", __LINE__); \
105 while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \
106 if(*ptr_1001 == '/') \
107 break; \
108 ptr_1001--; \
109 } \
l.yang6a42e4d2025-05-28 01:04:20 -0700110 fun_ptr_log(LOG_ERR_LEVEL, "%s#%s: "GSW_GNSS"" fmt, ptr_1001 + 1, line_1001, ##args); \
b.liu68a94c92025-05-24 12:53:41 +0800111 } while(0)
b.liu68a94c92025-05-24 12:53:41 +0800112
113#define MOPEN_GNSS_NMEA_MAX_LENGTH 255 /** NMEA string maximum length. */
114static gsw_gnss_cb *gsw_cb = NULL;
115static bool inited = false;
116static bool strated = false;
117
118typedef void (*mbtk_gnss_callback_func)(uint32_t ind_type, const void* data, uint32_t data_len);
119typedef void (*mbtk_log)(int level, const char *format,...);
120static mbtk_log fun_ptr_log = NULL;
121
122int (*mbtk_gnss_init)(mbtk_gnss_callback_func cb);
123int (*mbtk_gnss_deinit)();
124
125int (*mbtk_gnss_ind_set)(uint32_t ,int);
126int (*mbtk_gnss_open)(int, int);
127int (*mbtk_gnss_close)(int);
128int (*mbtk_gnss_setting)(const char *setting_cmd, int);
129
130int (*mbtk_gnss_eph_download)(int);
131int (*mbtk_gnss_eph_inject)(int);
132
133int gnss_freq = -1;
134GSW_GNSS_MODE_CONFIGURATION gnss_startmode = -1;
zw.wang581aab12025-05-28 19:43:53 +0800135GSW_CONF_SWITCH gnss_switch_op = -1;
b.liu68a94c92025-05-24 12:53:41 +0800136void *dlHandle_gnss;
137char *lynqLib_gnss = "/lib/libmbtk_lib.so";
138
139typedef enum
140{
141 E_MT_LOC_MSG_ID_LOCATION_INFO = 1, /**< pv_data = & mopen_location_info_t */
142 E_MT_LOC_MSG_ID_NMEA_INFO = 3, /**< pv_data = & mopen_gnss_nmea_info_t */
143} e_msg_id_t;
144
145typedef struct
146{
147 int64_t timestamp; /**< System Timestamp, marked for when got the nmea data */
148 int length; /**< NMEA string length. */
149 char nmea[MOPEN_GNSS_NMEA_MAX_LENGTH + 1]; /**< NMEA string.*/
150}mopen_gnss_nmea_info_t; /* Message */
151
152typedef struct
153{
154 uint32_t size; /**< Set to the size of mcm_gps_location_t. */
155 int flags; /**< Contains GPS location flags bits. */
156 int position_source; /**< Provider indicator for HYBRID or GPS. */ //功能暂未实现,可不用添加进结构体
157 double latitude; /**< Latitude in degrees. */
158 double longitude; /**< Longitude in degrees. */
159 double altitude; /**< Altitude in meters above the WGS 84 reference ellipsoid. */
160 float speed; /**< Speed in meters per second. */
161 float bearing; /**< Heading in degrees. */ //功能暂未实现,可不用添加进结构体
162 float accuracy; /**< Expected accuracy in meters. */ //功能暂未实现,可不用添加进结构体
163 int64_t timestamp; /**< Timestamp for the location fix in UTC million-second base. */
164 int32_t is_indoor; /**< Location is indoors. */ //功能暂未实现,可不用添加进结构体
165 float floor_number; /**< Indicates the floor number. */
166}mopen_location_info_t;//功能暂未实现,可不用添加进结构体
167
168
169typedef struct {
170 uint32_t flags;
171 double latitude; /**< Latitude in degrees. */
172 double longitude; /**< Longitude in degrees. */
173 double altitude; /**< Altitude in meters above the WGS 84 reference ellipsoid. */
174 float speed; /**< Speed in meters per second. */
175 float bearing; /**< Heading in degrees. */
176 int64_t timestamp; /**< Timestamp for the location fix in UTC million-second base. */
177} mbtk_gnss_location_info_t;
178
zw.wang581aab12025-05-28 19:43:53 +0800179typedef struct{
180 GSW_GNSS_MODE_CONFIGURATION start_mode;
181 gnss_freq_type freq;
182 gsw_gnss_cb callback;
183}gsw_gnss_init_configure_t;
184
b.liu68a94c92025-05-24 12:53:41 +0800185typedef enum{
186 MODE_GPS = 1, /**< GPS only */
187 MODE_BEIDOU, /**< BEIDOU only*/
188 MODE_GPS_BEIDOU, /**< GPS+BEIDOU */
189 MODE_GLONASS, /**< GLONASS only */
190 MODE_GPS_GLONASS, /**< GPS+GLONASS */
191 MODE_GLONASS_BEIDOU, /**< GLONASS+BEIDOU */ /* The type does not support this type */
192 MODE_GPS_GLONASS_BEIDOU, /**< GPS+GLONASS+BEIDOU */ /* The type does not support this type */
193 MODE_GALILEO, /**< GALILEO only */
194 MODE_GPS_GALILEO, /**< GPS+GALILEO */
195 MODE_BEIDOU_GALILEO, /**< BEIDOU+GALILEO */
196 MODE_GPS_BEIDOU_GALILEO, /**< GPS+BEIDOU+GALILEO */
197 MODE_GLONASS_GALILEO, /**< GLONASS+GALILEO */
198 MODE_GPS_GLONASS_GALILEO, /**< GPS+GLONASS+GALILEO */
199 MODE_BEIDOU_GLONASS_GALILEO, /**< BEIDOU+GLONASS+GALILEO */ /* The type does not support this type */
200 MODE_END = -1, /**< init value */
201}GSW_GNSS_MODE_CONFIGURATION_HD;
202
203
204GSW_GNSS_MODE_CONFIGURATION_HD map_gnss_mode(GSW_GNSS_MODE_CONFIGURATION mode)
205{
206 switch (mode)
207 {
208 case GSW_MODE_GPS_GLONASS:
209 return MODE_GPS_GLONASS;
210 case GSW_MODE_GPS_BEIDOU:
211 return MODE_GPS_BEIDOU;
212 case GSW_MODE_GPS_GLONASS_BEIDOU:
213 return MODE_END;
214 case GSW_MODE_GPS:
215 return MODE_GPS;
216 case GSW_MODE_BEIDOU:
217 return MODE_BEIDOU;
218 case GSW_MODE_GLONASS:
219 return MODE_GLONASS;
220 case GSW_MODE_GPS_GLONASS_BEIDOU_GALILEO:
221 return MODE_END;
222 case GSW_MODE_GPS_GALILEO:
223 return MODE_GPS_GALILEO;
224 case GSW_MODE_GPS_GLONASS_GALILEO:
225 return MODE_GPS_GLONASS_GALILEO;
226 case GSW_MODE_GPS_GALILEO_ONLY:
227 return MODE_GALILEO;
228 case GSW_MODE_GPS_GLONASS_BEIDOU_GALILEO_NAVIC:
229 return MODE_END;
230 case GSW_MODE_GNSS_END:
231 return MODE_END;
232 default:
233 return MODE_END;
234 }
235}
236
237int mbtk_gnss_set_VTG()
238{
239 int ret;
240 char param_buf[32] = {0};
241 snprintf(param_buf, 32, "$MSGCFG,2,1000");
242 mbtk_gnss_setting=(int(*)(const char *setting_cmd, int))dlsym(dlHandle_gnss, "mbtk_gnss_setting");
243 ret = mbtk_gnss_setting(param_buf, QSER_GNSS_TIMEOUT);
244 if(ret != 0)
245 {
246 LOGE("[qser_gnss] mbtk_gnss_setting fail.ret = [%d]", ret);
hong.liud2417072025-06-27 07:10:37 -0700247 return GSW_HAL_NORMAL_FAIL;
b.liu68a94c92025-05-24 12:53:41 +0800248 }
249 return GSW_HAL_SUCCESS;
250}
251
252/**
253 * @brief SDK interface to set gnss sampling frequency, support 1Hz、2Hz、5Hz
254 * @param [in] freq
255 * @retval 0: success
256 * @retval other: fail
257 */
258int gsw_gnss_set_freq(int freq)
259{
zw.wangbc534c02025-06-26 09:31:44 +0800260 if(!inited)
hong.liud2417072025-06-27 07:10:37 -0700261 return GSW_HAL_NORMAL_FAIL;
zw.wangbc534c02025-06-26 09:31:44 +0800262
b.liu68a94c92025-05-24 12:53:41 +0800263 int ret;
264 if (!strated)
265 {
266 gnss_freq = freq;
267 return GSW_HAL_SUCCESS;
268 }
269 char param_buf[32] = {0};
270 snprintf(param_buf, 32, "$FREQCFG,%d", freq);
271 mbtk_gnss_setting=(int(*)(const char *setting_cmd, int))dlsym(dlHandle_gnss, "mbtk_gnss_setting");
272 ret = mbtk_gnss_setting(param_buf, QSER_GNSS_TIMEOUT);
273 if(ret != 0)
274 {
275 LOGE("[qser_gnss] mbtk_gnss_setting fail.ret = [%d]", ret);
hong.liud2417072025-06-27 07:10:37 -0700276 return GSW_HAL_NORMAL_FAIL;
b.liu68a94c92025-05-24 12:53:41 +0800277 }
278 return GSW_HAL_SUCCESS;
279}
280
281/**
282 * @brief SDK interface to set gnss start mode,specific mode refreence GSW_HAL_GNSS_MODE_CONFIGURATION
283 * @param [in] start_mode
284 * @retval 0: success
285 * @retval other: fail
286 */
hong.liud2417072025-06-27 07:10:37 -0700287int32_t gsw_gnss_set_start_mode(GSW_GNSS_MODE_CONFIGURATION start_mode)
b.liu68a94c92025-05-24 12:53:41 +0800288{
zw.wangbc534c02025-06-26 09:31:44 +0800289 if(!inited)
hong.liud2417072025-06-27 07:10:37 -0700290 return GSW_HAL_NORMAL_FAIL;
b.liu68a94c92025-05-24 12:53:41 +0800291 int ret;
292 if (!strated)
293 {
294 gnss_startmode = start_mode;
295 return GSW_HAL_SUCCESS;
296 }
297 char param_buf[32] = {0};
298 snprintf(param_buf, 32, "$SYSCFG,%d", map_gnss_mode(start_mode));
299 if(map_gnss_mode(start_mode) == -1)
300 {
301 LOGE("[qser_gnss] mbtk_gnss_start_mode con't support");
hong.liud2417072025-06-27 07:10:37 -0700302 return GSW_HAL_NORMAL_FAIL;
b.liu68a94c92025-05-24 12:53:41 +0800303 }
304 mbtk_gnss_setting=(int(*)(const char *setting_cmd, int))dlsym(dlHandle_gnss, "mbtk_gnss_setting");
305 ret = mbtk_gnss_setting(param_buf, QSER_GNSS_TIMEOUT);
306 if(ret != 0)
307 {
308 LOGE("[qser_gnss] mbtk_gnss_setting fail.ret = [%d]", ret);
hong.liud2417072025-06-27 07:10:37 -0700309 return GSW_HAL_NORMAL_FAIL;
b.liu68a94c92025-05-24 12:53:41 +0800310 }
311 return GSW_HAL_SUCCESS;
312}
313
314/**
315 * @brief SDK interface to set EPO switch if open or close
316 * @param [in] switch_op
317 * @retval 0: success
318 * @retval other: fail
319 */
hong.liud2417072025-06-27 07:10:37 -0700320int32_t gsw_gnss_epo_switch(GSW_CONF_SWITCH switch_op)
b.liu68a94c92025-05-24 12:53:41 +0800321{
zw.wangbc534c02025-06-26 09:31:44 +0800322 if(!inited)
hong.liud2417072025-06-27 07:10:37 -0700323 return GSW_HAL_NORMAL_FAIL;
b.liu68a94c92025-05-24 12:53:41 +0800324 int ret;
325 if (!strated)
326 {
327 gnss_switch_op = switch_op;
328 return GSW_HAL_SUCCESS;
329 }
330
331 if(switch_op)
332 {
333 mbtk_gnss_eph_download=(int(*)(int))dlsym(dlHandle_gnss, "mbtk_gnss_eph_download");
334 ret = mbtk_gnss_eph_download(QSER_GNSS_TIMEOUT);
335 if(ret != 0)
336 {
337 LOGE("[qser_gnss] mbtk_gnss_eph_download fail.ret = [%d]", ret);
hong.liud2417072025-06-27 07:10:37 -0700338 return GSW_HAL_NORMAL_FAIL;
b.liu68a94c92025-05-24 12:53:41 +0800339 }
340 mbtk_gnss_eph_inject=(int(*)(int))dlsym(dlHandle_gnss, "mbtk_gnss_eph_inject");
341 ret = mbtk_gnss_eph_inject(QSER_GNSS_TIMEOUT);
342 if(ret != 0)
343 {
344 LOGE("[qser_gnss] mbtk_gnss_eph_inject fail.ret = [%d]", ret);
hong.liud2417072025-06-27 07:10:37 -0700345 return GSW_HAL_NORMAL_FAIL;
b.liu68a94c92025-05-24 12:53:41 +0800346 }
347 }
348 return GSW_HAL_SUCCESS;
349}
350
351static void gnss_callback(uint32_t ind_type, const void* data, uint32_t data_len)
352{
353 if(data == NULL || data_len <= 0)
354 {
355 LOGE("[GSW_gnss] data is NULL.");
356 return;
357 }
358
359 if(gsw_cb == NULL)
360 {
361 //LOGE("[qser_gnss] qser_gnss_callback is NULL.");
362 return;
363 }
364
zw.wanga5250d62025-06-13 16:01:34 +0800365 if(ind_type == MBTK_GNSS_IND_LOCATION && gsw_cb->gsw_location_cb != NULL) {
b.liu68a94c92025-05-24 12:53:41 +0800366 if(data_len != sizeof(mbtk_gnss_location_info_t))
367 {
368 LOGE("[GSW_gnss] data size error");
369 return;
370 }
371 GSW_GNSS_LOCATION_EXT_T gsw_location;
hong.liud2417072025-06-27 07:10:37 -0700372 GSW_GNSS_LOCATION_T gsw_location_t = {0};
b.liu68a94c92025-05-24 12:53:41 +0800373 mbtk_gnss_location_info_t *locl_info = (mbtk_gnss_location_info_t *)data;
374 gsw_location_t.altitude = locl_info->altitude;
375 gsw_location_t.latitude = locl_info->latitude;
376 gsw_location_t.longitude = locl_info->longitude;
377 gsw_location_t.speed = locl_info->speed;
378 gsw_location_t.bearing = locl_info->bearing;
379 gsw_location_t.timestamp = locl_info->timestamp;
zw.wanga5250d62025-06-13 16:01:34 +0800380 gsw_location_t.flags = locl_info->flags;
b.liu68a94c92025-05-24 12:53:41 +0800381 gsw_location.legacyLocation = gsw_location_t;
382 gsw_cb->gsw_location_cb(&gsw_location);
zw.wanga5250d62025-06-13 16:01:34 +0800383 } else if(ind_type == MBTK_GNSS_IND_NMEA && gsw_cb->gsw_nmea_cb != NULL) {
b.liu68a94c92025-05-24 12:53:41 +0800384 mopen_gnss_nmea_info_t qser_nmea = {0};
385 memset(&qser_nmea, 0x0, sizeof(mopen_gnss_nmea_info_t));
386 qser_nmea.length = data_len;
387 memcpy(qser_nmea.nmea, (char *)data, data_len);
388 //qser_nmea.timestamp = qser_get_gnss_time_sec(data, data_len);
389 gsw_cb->gsw_nmea_cb(data,data_len);
390 } else {
391 LOGD("Unknown IND : %d\n", ind_type);
392 }
393}
394
hong.liud2417072025-06-27 07:10:37 -0700395int32_t gsw_gnss_init(void)
b.liu68a94c92025-05-24 12:53:41 +0800396{
lichengzhanga7089172025-07-01 20:41:46 +0800397 int ret;
b.liu68a94c92025-05-24 12:53:41 +0800398 if(!inited)
399 {
lichengzhanga7089172025-07-01 20:41:46 +0800400 ret = system("serial_atcmd AT*IMLCONFIG=13");
401 if(ret != 0)
402 {
403 LOGE("serial_atcmd fail\n");
404 return GSW_HAL_NORMAL_FAIL;
405 }
zw.wang1907e8f2025-07-01 16:26:39 +0800406 if(dlHandle_gnss == NULL)
407 dlHandle_gnss = dlopen(lynqLib_gnss, RTLD_NOW);
408 if(fun_ptr_log == NULL)
409 fun_ptr_log = (mbtk_log)dlsym(dlHandle_gnss, "mbtk_log");
b.liu68a94c92025-05-24 12:53:41 +0800410 if(fun_ptr_log == NULL || dlHandle_gnss == NULL)
411 {
hong.liud2417072025-06-27 07:10:37 -0700412 return GSW_HAL_NORMAL_FAIL;
b.liu68a94c92025-05-24 12:53:41 +0800413 }
414 mbtk_gnss_init=(int(*)(mbtk_gnss_callback_func))dlsym(dlHandle_gnss, "mbtk_gnss_init");
415 ret = mbtk_gnss_init(gnss_callback);
416 if(ret == 0)
417 {
418 mbtk_gnss_ind_set=(int(*)(uint32_t ,int))dlsym(dlHandle_gnss, "mbtk_gnss_ind_set");
zw.wanga5250d62025-06-13 16:01:34 +0800419 ret = mbtk_gnss_ind_set(MBTK_GNSS_IND_NMEA |MBTK_GNSS_IND_LOCATION, QSER_GNSS_TIMEOUT);
b.liu68a94c92025-05-24 12:53:41 +0800420 if(ret == 0)
421 {
422 inited = true;
423 }
424 else
425 {
426 LOGE("[GSW_gnss] init mbtk_gnss_ind_set() fail.ret = [%d]", ret);
hong.liud2417072025-06-27 07:10:37 -0700427 return GSW_HAL_NORMAL_FAIL;
b.liu68a94c92025-05-24 12:53:41 +0800428 }
429 }
430 else
431 {
432 LOGE("[GSW_gnss] mbtk_gnss_init() fail.ret = [%d]", ret);
hong.liud2417072025-06-27 07:10:37 -0700433 return GSW_HAL_NORMAL_FAIL;
b.liu68a94c92025-05-24 12:53:41 +0800434 }
435 }
436
437 return GSW_HAL_SUCCESS;
438}
439
440/**
441 * @brief SDK interface to registered callback function
442 * @param [in] callback
443 * @retval 0: success
444 * @retval other: fail
445 */
hong.liud2417072025-06-27 07:10:37 -0700446int32_t gsw_gnss_reg_cb_group(gsw_gnss_cb callback)
b.liu68a94c92025-05-24 12:53:41 +0800447{
zw.wangbc534c02025-06-26 09:31:44 +0800448 if(!inited)
hong.liud2417072025-06-27 07:10:37 -0700449 return GSW_HAL_NORMAL_FAIL;
b.liu68a94c92025-05-24 12:53:41 +0800450 if(callback.gsw_location_cb == NULL && callback.gsw_nmea_cb == NULL)
451 {
452 LOGE("[GSW_gnss] handler_ptr is NULL.");
hong.liud2417072025-06-27 07:10:37 -0700453 return GSW_HAL_NORMAL_FAIL;
b.liu68a94c92025-05-24 12:53:41 +0800454 }
455 if (gsw_cb == NULL)
456 {
457 gsw_cb = (gsw_gnss_cb *)malloc(sizeof(gsw_gnss_cb));
458 if (gsw_cb == NULL)
459 {
460 LOGE("[GSW_gnss] Memory allocation failed.");
hong.liud2417072025-06-27 07:10:37 -0700461 return GSW_HAL_NORMAL_FAIL;
b.liu68a94c92025-05-24 12:53:41 +0800462 }
463 }
464 gsw_cb->gsw_location_cb = callback.gsw_location_cb;
465 gsw_cb->gsw_nmea_cb = callback.gsw_nmea_cb;
466 return GSW_HAL_SUCCESS;
467}
468/**
469 * @brief SDK interface to start gnss
470 * @param
471 * @retval 0: success
472 * @retval other: fail
473 */
hong.liud2417072025-06-27 07:10:37 -0700474int32_t gsw_gnss_start(void)
b.liu68a94c92025-05-24 12:53:41 +0800475{
476 int ret;
zw.wangbc534c02025-06-26 09:31:44 +0800477 if(!inited)
hong.liud2417072025-06-27 07:10:37 -0700478 return GSW_HAL_NORMAL_FAIL;
zw.wangbc534c02025-06-26 09:31:44 +0800479 ret = system("/usr/sbin/gnss_gpio.sh on > /dev/null 2>&1");
480 if(ret != 0)
481 {
482 LOGE("[GSW_gnss] gnss_gpio.sh on fail.ret = [%d]", ret);
hong.liud2417072025-06-27 07:10:37 -0700483 return GSW_HAL_NORMAL_FAIL;
zw.wangbc534c02025-06-26 09:31:44 +0800484 }
b.liu68a94c92025-05-24 12:53:41 +0800485 mbtk_gnss_open=(int(*)(int,int))dlsym(dlHandle_gnss, "mbtk_gnss_open");
486 ret = mbtk_gnss_open(255, QSER_GNSS_TIMEOUT);
487 if(ret != 0)
488 {
489 LOGE("[GSW_gnss] mbtk_gnss_open is error.ret = [%d]", ret);
hong.liud2417072025-06-27 07:10:37 -0700490 return GSW_HAL_NORMAL_FAIL;
b.liu68a94c92025-05-24 12:53:41 +0800491 }
zw.wang89c18242025-06-24 19:07:10 +0800492
b.liu68a94c92025-05-24 12:53:41 +0800493 strated = true;
zw.wang7aa34062025-06-27 17:54:55 +0800494 /*
b.liu68a94c92025-05-24 12:53:41 +0800495 if (gnss_freq > 0)
496 gsw_gnss_set_freq(gnss_freq);
497 if (gnss_startmode >= 0)
498 gsw_gnss_set_start_mode(gnss_startmode);
499 if (gnss_switch_op > 0)
500 gsw_gnss_epo_switch(gnss_switch_op);
zw.wang7aa34062025-06-27 17:54:55 +0800501 */
b.liu68a94c92025-05-24 12:53:41 +0800502 mbtk_gnss_set_VTG();
503
504 return GSW_HAL_SUCCESS;
505}
506
507/**
508 * @brief SDK interface to stop gnss
509 * @param
510 * @retval 0: success
511 * @retval other: fail
512 */
hong.liud2417072025-06-27 07:10:37 -0700513int32_t gsw_gnss_stop(void)
b.liu68a94c92025-05-24 12:53:41 +0800514{
515 int ret;
zw.wangbc534c02025-06-26 09:31:44 +0800516 if(!inited)
hong.liud2417072025-06-27 07:10:37 -0700517 return GSW_HAL_NORMAL_FAIL;
b.liu68a94c92025-05-24 12:53:41 +0800518 mbtk_gnss_close=(int(*)(int))dlsym(dlHandle_gnss, "mbtk_gnss_close");
519 ret = mbtk_gnss_close(QSER_GNSS_TIMEOUT);
520 if(ret != 0)
521 {
522 LOGE("[GSW_gnss] mbtk_gnss_close is error.ret = [%d]", ret);
hong.liud2417072025-06-27 07:10:37 -0700523 return GSW_HAL_NORMAL_FAIL;
b.liu68a94c92025-05-24 12:53:41 +0800524 }
zw.wangbc534c02025-06-26 09:31:44 +0800525 ret = system("/usr/sbin/gnss_gpio.sh off > /dev/null 2>&1");
526 if(ret != 0)
527 {
528 LOGE("[GSW_gnss] gnss_gpio.sh off fail.ret = [%d]", ret);
hong.liud2417072025-06-27 07:10:37 -0700529 return GSW_HAL_NORMAL_FAIL;
zw.wangbc534c02025-06-26 09:31:44 +0800530 }
b.liu68a94c92025-05-24 12:53:41 +0800531 strated = false;
532 return GSW_HAL_SUCCESS;
533}
534
535/**
536 * @brief SDK interface to de initialization gnss
537 * @param
538 * @retval 0: success
539 * @retval other: fail
540 */
hong.liud2417072025-06-27 07:10:37 -0700541int32_t gsw_gnss_deinit(void)
b.liu68a94c92025-05-24 12:53:41 +0800542{
543 int ret;
zw.wangbc534c02025-06-26 09:31:44 +0800544 if(!inited)
hong.liud2417072025-06-27 07:10:37 -0700545 return GSW_HAL_NORMAL_FAIL;
b.liu68a94c92025-05-24 12:53:41 +0800546 if(inited)
547 {
lichengzhanga7089172025-07-01 20:41:46 +0800548 ret = system("serial_atcmd AT*IMLCONFIG=12");
549 if(ret != 0)
550 {
551 LOGE("serial_atcmd fail\n");
552 return GSW_HAL_NORMAL_FAIL;
553 }
b.liu68a94c92025-05-24 12:53:41 +0800554 mbtk_gnss_deinit=(int(*)())dlsym(dlHandle_gnss, "mbtk_gnss_deinit");
555 ret = mbtk_gnss_deinit();
556 if(ret == 0)
557 {
558 inited = false;
559 }
560 else
561 {
zw.wang1907e8f2025-07-01 16:26:39 +0800562 LOGE("[GSW_gnss] mbtk_gnss_deinit() fail.ret = [%d]", ret);
563 /*
b.liu68a94c92025-05-24 12:53:41 +0800564 dlclose(dlHandle_gnss);
565 dlHandle_gnss = NULL;
zw.wang1907e8f2025-07-01 16:26:39 +0800566 */
hong.liud2417072025-06-27 07:10:37 -0700567 return GSW_HAL_NORMAL_FAIL;
b.liu68a94c92025-05-24 12:53:41 +0800568 }
569 }
570
zw.wang1907e8f2025-07-01 16:26:39 +0800571 /*
b.liu68a94c92025-05-24 12:53:41 +0800572 dlclose(dlHandle_gnss);
573 dlHandle_gnss = NULL;
zw.wang1907e8f2025-07-01 16:26:39 +0800574 */
b.liu68a94c92025-05-24 12:53:41 +0800575 gnss_freq = -1;
576 gnss_startmode = -1;
577 gnss_switch_op = -1;
578 return GSW_HAL_SUCCESS;
579}
580
zw.wang75e98ea2025-05-29 17:57:38 +0800581int gsw_gnss_add_lib(void)
582{
583 return GSW_HAL_SUCCESS;
584}
b.liu68a94c92025-05-24 12:53:41 +0800585
586/**
587 * @brief SDK interface to enable XTRA switch
588 * @param [in] state
589 * @retval 0: success
590 * @retval other: fail
591 */
hong.liud2417072025-06-27 07:10:37 -0700592int32_t gsw_gnss_xtra_is_enable(gsw_xtra_state_e state)
b.liu68a94c92025-05-24 12:53:41 +0800593{
594 return GSW_HAL_SUCCESS;
595}
596
597/**
598 * @brief SDK interface to delete aiding data,delete aiding data for cold start(1-H,2-W,3-C)
599 * @param [in] switch_op
600 * @retval 0: success
601 * @retval other: fail
602 */
hong.liud2417072025-06-27 07:10:37 -0700603int32_t gsw_gnss_delete_aiding_data(unsigned int flags) /*1-don`t delete == hot_start ; 2-delete EPH == warm start ; 3-delete all == cold start*/
b.liu68a94c92025-05-24 12:53:41 +0800604{
zw.wangbc534c02025-06-26 09:31:44 +0800605 if(!inited)
hong.liud2417072025-06-27 07:10:37 -0700606 return GSW_HAL_NORMAL_FAIL;
b.liu68a94c92025-05-24 12:53:41 +0800607 int ret;
608 char param_buf[32] = {0};
609 snprintf(param_buf, 32, "$RESET,%u", flags);
610 mbtk_gnss_setting=(int(*)(const char *setting_cmd, int))dlsym(dlHandle_gnss, "mbtk_gnss_setting");
611 ret = mbtk_gnss_setting(param_buf, QSER_GNSS_TIMEOUT);
612 if(ret != 0)
613 {
614 LOGE("[qser_gnss] mbtk_gnss_setting fail.ret = [%d]", ret);
hong.liud2417072025-06-27 07:10:37 -0700615 return GSW_HAL_NORMAL_FAIL;
b.liu68a94c92025-05-24 12:53:41 +0800616 }
617 return GSW_HAL_SUCCESS;
618}
619
620/**
621 * @brief init and configure gps
622 * @param [in] init_configure
623 * @retval 0: success
624 * @retval other: fail
625 */
hong.liud2417072025-06-27 07:10:37 -0700626int32_t gsw_gnss_init_configure_gps(gsw_gnss_init_configure_t init_configure)
b.liu68a94c92025-05-24 12:53:41 +0800627{
628 return GSW_HAL_SUCCESS;
629}
hong.liud2417072025-06-27 07:10:37 -0700630
631int gsw_gnss_get_tail_nmea_type(char *tail_type, int len)
632{
633 if(NULL == tail_type){
634 LOGE("get_tail_nmea_type fail, tail_type is NULL");
635 return GSW_HAL_ERROR_GNSS_FAIL;
636 }
637
638 if(len >= 3){
639 strcpy(tail_type, "RMC");
640 return GSW_HAL_GNSS_SUCCESS;
641 }else{
642 LOGE("get_tail_nmea_type fail, len[%d] is too short", len);
643 return GSW_HAL_ERROR_GNSS_FAIL;
644 }
645}