1 /*
2  * Copyright 2024 Trackunit Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/drivers/gnss.h>
8 #include <zephyr/kernel.h>
9 #include <zephyr/sys/atomic.h>
10 #include <zephyr/ztest.h>
11 
12 static const struct device *dev = DEVICE_DT_GET(DT_ALIAS(gnss));
13 static const enum gnss_navigation_mode nav_modes[] = {
14 	GNSS_NAVIGATION_MODE_ZERO_DYNAMICS,
15 	GNSS_NAVIGATION_MODE_LOW_DYNAMICS,
16 	GNSS_NAVIGATION_MODE_BALANCED_DYNAMICS,
17 	GNSS_NAVIGATION_MODE_HIGH_DYNAMICS,
18 };
19 
test_set_nav_mode(enum gnss_navigation_mode nav_mode)20 static bool test_set_nav_mode(enum gnss_navigation_mode nav_mode)
21 {
22 	int ret;
23 
24 	ret = gnss_set_navigation_mode(dev, nav_mode);
25 	if (ret == -ENOSYS) {
26 		ztest_test_skip();
27 	}
28 	if (ret == -EINVAL) {
29 		return false;
30 	}
31 	zassert_ok(ret, "failed to set navigation mode %u", nav_mode);
32 	return true;
33 }
34 
test_validate_nav_mode(enum gnss_navigation_mode nav_mode)35 static void test_validate_nav_mode(enum gnss_navigation_mode nav_mode)
36 {
37 	int ret;
38 	enum gnss_navigation_mode set_nav_mode;
39 
40 	ret = gnss_get_navigation_mode(dev, &set_nav_mode);
41 	if (ret == -ENOSYS) {
42 		return;
43 	}
44 	zassert_ok(ret, "failed to get navigation mode %u", nav_mode);
45 }
46 
ZTEST(gnss_api,test_navigation_mode)47 ZTEST(gnss_api, test_navigation_mode)
48 {
49 	enum gnss_navigation_mode nav_mode;
50 
51 	for (uint8_t i = 0; i < ARRAY_SIZE(nav_modes); i++) {
52 		nav_mode = nav_modes[i];
53 
54 		if (!test_set_nav_mode(nav_mode)) {
55 			continue;
56 		}
57 
58 		test_validate_nav_mode(nav_mode);
59 	}
60 }
61