Skip to content

Commit 81d5416

Browse files
committed
lib: sgp4: move tests from module to lib, expose public API
Move NTN tests from `tests/module/` to `tests/lib/` and update testcase identifiers from `fw` to `lib` prefix. Move `sat_data_sib32` struct to the header and expose `parse_sibconfig32_at` and `jd_from_unix_time_ms` as public functions with proper documentation. Use `ASSET_TRACKER_TEMPLATE_DIR` variable in CMake files instead of relative paths. Signed-off-by: Simen S. Røstad <simen.rostad@nordicsemi.no>
1 parent 47257dd commit 81d5416

11 files changed

Lines changed: 84 additions & 39 deletions

File tree

lib/sgp4/Kconfig

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,5 @@ config SGP4_PASS_PREDICT
1313
bool "SGP4 satellite pass prediction"
1414
default n
1515
select SGP4
16-
select DATE_TIME
1716
help
1817
Enable satellite pass prediction using SGP4 and TLE/SIB32 data.

lib/sgp4/sgp4_pass_predict.c

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
55
*/
66

7+
#ifdef _POSIX_C_SOURCE
8+
#undef _POSIX_C_SOURCE
9+
#endif
10+
/* Required for strtok_r() on native_sim / picolibc. */
11+
#define _POSIX_C_SOURCE 200809L
12+
713
#include <string.h>
814
#include <stdint.h>
915
#include <stdlib.h>
@@ -34,26 +40,6 @@ struct datetime {
3440
double second;
3541
};
3642

37-
/* 3GPP TS 36.331 ephemeris parameters format */
38-
struct sat_data_sib32 {
39-
int64_t satelliteId;
40-
int64_t epochStar;
41-
int64_t meanMotion;
42-
int64_t eccentricity;
43-
int64_t inclination;
44-
int64_t rightAscension;
45-
int64_t argumentPerigee;
46-
int64_t meanAnomaly;
47-
int64_t bStarDecimal;
48-
int64_t bStarExponent;
49-
int64_t serviceStart;
50-
int64_t elevationAngleLeft;
51-
int64_t elevationAngleRight;
52-
int64_t referencePointLongitude;
53-
int64_t referencePointLatitude;
54-
int64_t radius;
55-
};
56-
5743
/* SIBCONFIG 32 AT ephemeris struct field order */
5844
enum sib_ephemeris_field {
5945
FIELD_SATELLITE_ID = 0,
@@ -190,7 +176,7 @@ static uint64_t datetime_to_ts(struct datetime *dt)
190176
return days*86400000 + dt->hour*3600000 + dt->minute*60000 + dt->second*1000;
191177
}
192178

193-
static void jd_from_unix_time_ms(int64_t unix_time_ms, double *jd, double *jdfract)
179+
void jd_from_unix_time_ms(int64_t unix_time_ms, double *jd, double *jdfract)
194180
{
195181
*jd = (unix_time_ms / 86400000) + 2440587.5;
196182
*jdfract = (unix_time_ms % 86400000) / 86400000.0;
@@ -359,7 +345,7 @@ static int parse_ephemeris_struct(char **saveptr, struct sat_data_sib32 *sib32)
359345
return 0;
360346
}
361347

362-
static int parse_sibconfig32_at(const char *atsib32, char *cell_id, struct sat_data_sib32 *sib32)
348+
int parse_sibconfig32_at(const char *atsib32, char *cell_id, struct sat_data_sib32 *sib32)
363349
{
364350
int err;
365351
int sibnr;

lib/sgp4/sgp4_pass_predict.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,26 @@
1313

1414
#define MAX_SATELLITES 4
1515

16+
/* 3GPP TS 36.331 ephemeris parameters format */
17+
struct sat_data_sib32 {
18+
int64_t satelliteId;
19+
int64_t epochStar;
20+
int64_t meanMotion;
21+
int64_t eccentricity;
22+
int64_t inclination;
23+
int64_t rightAscension;
24+
int64_t argumentPerigee;
25+
int64_t meanAnomaly;
26+
int64_t bStarDecimal;
27+
int64_t bStarExponent;
28+
int64_t serviceStart;
29+
int64_t elevationAngleLeft;
30+
int64_t elevationAngleRight;
31+
int64_t referencePointLongitude;
32+
int64_t referencePointLatitude;
33+
int64_t radius;
34+
};
35+
1636
struct next_pass {
1737
int64_t start_time_ms;
1838
int64_t end_time_ms;
@@ -83,4 +103,24 @@ int sat_data_calculate_next_pass(struct sat_data *sat_data, int sat_index,
83103
*/
84104
int sat_data_set_name(struct sat_data *sat_data, const char *name);
85105

106+
/**
107+
* @brief Parse SIBCONFIG 32 AT command ephemeris parameters
108+
*
109+
* @param atsib32 The AT SIB32 parameters from modem
110+
* @param cell_id Output buffer for cell ID (at least 9 bytes)
111+
* @param sib32 Output array for parsed SIB entries, or NULL to only count entries
112+
*
113+
* @return Number of SIB entries on success, negative error code otherwise
114+
*/
115+
int parse_sibconfig32_at(const char *atsib32, char *cell_id, struct sat_data_sib32 *sib32);
116+
117+
/**
118+
* @brief Convert Unix time in milliseconds to Julian date
119+
*
120+
* @param unix_time_ms Unix timestamp in milliseconds
121+
* @param jd Whole Julian day
122+
* @param jdfract Fractional part of Julian day
123+
*/
124+
void jd_from_unix_time_ms(int64_t unix_time_ms, double *jd, double *jdfract);
125+
86126
#endif
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,20 @@
77
cmake_minimum_required(VERSION 3.20.0)
88

99
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
10-
project(sgp4_pass_test)
10+
project(ntn_sib32_test)
1111

1212
test_runner_generate(src/test_sib32_tle.c)
1313

14+
set(ASSET_TRACKER_TEMPLATE_DIR ../../..)
15+
1416
target_sources(app
1517
PRIVATE
1618
src/test_sib32_tle.c
1719
)
1820

1921
zephyr_include_directories(${ZEPHYR_BASE}/include/zephyr/)
2022
zephyr_include_directories(${ZEPHYR_BASE}/subsys/testsuite/include)
21-
zephyr_include_directories(../../../app/src/common)
22-
zephyr_include_directories(../../../lib/sgp4)
23+
zephyr_include_directories(${ASSET_TRACKER_TEMPLATE_DIR}/app/src/common)
24+
zephyr_include_directories(${ASSET_TRACKER_TEMPLATE_DIR}/lib/sgp4)
2325

2426
target_link_options(app PRIVATE --whole-archive)
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#
66

77
CONFIG_SGP4_PASS_PREDICT=y
8-
CONFIG_DATE_TIME_NTP=n
98
CONFIG_UNITY=y
109
CONFIG_LOG=y
1110
CONFIG_CBPRINTF_FP_SUPPORT=y

tests/module/ntn-sib32/src/test_sib32_tle.c renamed to tests/lib/ntn-sib32/src/test_sib32_tle.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,28 @@
1717

1818
/* missing headers */
1919
struct tm *gmtime_r(const int64_t *timep, struct tm *result);
20-
char *strtok_r(char *str, const char *delim, char **saveptr);
21-
2220

2321
DEFINE_FFF_GLOBALS;
2422

23+
FAKE_VALUE_FUNC(int, date_time_now, int64_t *);
24+
25+
LOG_MODULE_REGISTER(ntn_sib32_test, LOG_LEVEL_DBG);
26+
2527
/* Thu Feb 12 2026 09:38:29 GMT+0000 */
2628
#define FAKE_TIME_MS 1770889109000LL
27-
int date_time_now(int64_t *time)
29+
30+
static int date_time_now_custom_fake(int64_t *time)
2831
{
2932
*time = FAKE_TIME_MS;
3033
return 0;
3134
}
3235

36+
void setUp(void)
37+
{
38+
RESET_FAKE(date_time_now);
39+
date_time_now_fake.custom_fake = date_time_now_custom_fake;
40+
}
41+
3342
#define LAT_TRD 63.43
3443
#define LON_TRD 10.39
3544
#define ALT_TRD 40.0
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
tests:
2-
asset_tracker_template.fw.ntn-sib32:
2+
asset_tracker_template.lib.ntn-sib32:
33
platform_allow:
44
- native_sim
55
- native_sim/native/64
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,20 @@
77
cmake_minimum_required(VERSION 3.20.0)
88

99
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
10-
project(sgp4_pass_test)
10+
project(ntn_sgp4_test)
1111

1212
test_runner_generate(src/sgp4_pass_test.c)
1313

14+
set(ASSET_TRACKER_TEMPLATE_DIR ../../..)
15+
1416
target_sources(app
1517
PRIVATE
1618
src/sgp4_pass_test.c
1719
)
1820

1921
zephyr_include_directories(${ZEPHYR_BASE}/include/zephyr/)
2022
zephyr_include_directories(${ZEPHYR_BASE}/subsys/testsuite/include)
21-
zephyr_include_directories(../../../app/src/common)
22-
zephyr_include_directories(../../../lib/sgp4)
23+
zephyr_include_directories(${ASSET_TRACKER_TEMPLATE_DIR}/app/src/common)
24+
zephyr_include_directories(${ASSET_TRACKER_TEMPLATE_DIR}/lib/sgp4)
2325

2426
target_link_options(app PRIVATE --whole-archive)
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#
66

77
CONFIG_SGP4_PASS_PREDICT=y
8-
CONFIG_DATE_TIME_NTP=n
98
CONFIG_UNITY=y
109
CONFIG_LOG=y
1110
CONFIG_CBPRINTF_FP_SUPPORT=y
Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,30 @@
1717

1818
/* missing headers */
1919
struct tm *gmtime_r(const int64_t *timep, struct tm *result);
20-
char *strtok_r(char *str, const char *delim, char **saveptr);
21-
2220

2321
DEFINE_FFF_GLOBALS;
2422

23+
FAKE_VALUE_FUNC(int, date_time_now, int64_t *);
24+
25+
LOG_MODULE_REGISTER(ntn_sgp4_test, LOG_LEVEL_DBG);
26+
2527
static struct sat_data sat_data_tle;
2628

2729
/* Mon Mar 02 2026 08:50:24 */
2830
#define FAKE_TIME_MS 1772441424123LL
29-
int date_time_now(int64_t *time)
31+
32+
static int date_time_now_custom_fake(int64_t *time)
3033
{
3134
*time = FAKE_TIME_MS;
3235
return 0;
3336
}
3437

38+
void setUp(void)
39+
{
40+
RESET_FAKE(date_time_now);
41+
date_time_now_fake.custom_fake = date_time_now_custom_fake;
42+
}
43+
3544
static void debug_print_next_pass(struct sat_data *data)
3645
{
3746
struct tm start_tm, end_tm, max_elevation_tm;

0 commit comments

Comments
 (0)