1 /*
2  * Copyright (c) 2023 Trackunit Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <stdio.h>
8 #include <zephyr/device.h>
9 #include <zephyr/drivers/gnss.h>
10 #include <zephyr/logging/log.h>
11 
12 #define GNSS_MODEM DEVICE_DT_GET(DT_ALIAS(gnss))
13 
gnss_data_cb(const struct device * dev,const struct gnss_data * data)14 static void gnss_data_cb(const struct device *dev, const struct gnss_data *data)
15 {
16 	uint64_t timepulse_ns;
17 	k_ticks_t timepulse;
18 
19 	if (data->info.fix_status != GNSS_FIX_STATUS_NO_FIX) {
20 		if (gnss_get_latest_timepulse(dev, &timepulse) == 0) {
21 			timepulse_ns = k_ticks_to_ns_near64(timepulse);
22 			printf("Got a fix (type: %d) @ %lld ns\n", data->info.fix_status,
23 			       timepulse_ns);
24 		} else {
25 			printf("Got a fix (type: %d)\n", data->info.fix_status);
26 		}
27 	}
28 }
29 GNSS_DATA_CALLBACK_DEFINE(GNSS_MODEM, gnss_data_cb);
30 
31 #if CONFIG_GNSS_SATELLITES
gnss_satellites_cb(const struct device * dev,const struct gnss_satellite * satellites,uint16_t size)32 static void gnss_satellites_cb(const struct device *dev, const struct gnss_satellite *satellites,
33 			       uint16_t size)
34 {
35 	unsigned int tracked_count = 0;
36 	unsigned int corrected_count = 0;
37 
38 	for (unsigned int i = 0; i != size; ++i) {
39 		tracked_count += satellites[i].is_tracked;
40 		corrected_count += satellites[i].is_corrected;
41 	}
42 	printf("%u satellite%s reported (of which %u tracked, of which %u has RTK corrections)!\n",
43 	       size, size > 1 ? "s" : "", tracked_count, corrected_count);
44 }
45 #endif
46 GNSS_SATELLITES_CALLBACK_DEFINE(GNSS_MODEM, gnss_satellites_cb);
47 
48 #define GNSS_SYSTEMS_PRINTF(define, supported, enabled)                                            \
49 	printf("\t%20s: Supported: %3s Enabled: %3s\n",                                            \
50 	       STRINGIFY(define), (supported & define) ? "Yes" : "No",                             \
51 			 (enabled & define) ? "Yes" : "No");
52 
main(void)53 int main(void)
54 {
55 	gnss_systems_t supported, enabled;
56 	uint32_t fix_interval;
57 	int rc;
58 
59 	rc = gnss_get_supported_systems(GNSS_MODEM, &supported);
60 	if (rc < 0) {
61 		printf("Failed to query supported systems (%d)\n", rc);
62 		return rc;
63 	}
64 	rc = gnss_get_enabled_systems(GNSS_MODEM, &enabled);
65 	if (rc < 0) {
66 		printf("Failed to query enabled systems (%d)\n", rc);
67 		return rc;
68 	}
69 	printf("GNSS Systems:\n");
70 	GNSS_SYSTEMS_PRINTF(GNSS_SYSTEM_GPS, supported, enabled);
71 	GNSS_SYSTEMS_PRINTF(GNSS_SYSTEM_GLONASS, supported, enabled);
72 	GNSS_SYSTEMS_PRINTF(GNSS_SYSTEM_GALILEO, supported, enabled);
73 	GNSS_SYSTEMS_PRINTF(GNSS_SYSTEM_BEIDOU, supported, enabled);
74 	GNSS_SYSTEMS_PRINTF(GNSS_SYSTEM_QZSS, supported, enabled);
75 	GNSS_SYSTEMS_PRINTF(GNSS_SYSTEM_IRNSS, supported, enabled);
76 	GNSS_SYSTEMS_PRINTF(GNSS_SYSTEM_SBAS, supported, enabled);
77 	GNSS_SYSTEMS_PRINTF(GNSS_SYSTEM_IMES, supported, enabled);
78 
79 	rc = gnss_get_fix_rate(GNSS_MODEM, &fix_interval);
80 	if (rc < 0) {
81 		printf("Failed to query fix rate (%d)\n", rc);
82 		return rc;
83 	}
84 	printf("Fix rate = %d ms\n", fix_interval);
85 
86 	return 0;
87 }
88