Skip to content
This repository was archived by the owner on Apr 13, 2021. It is now read-only.

Commit b3b8814

Browse files
committed
Merge pull request #234 from jacobmcnamee/signal_utils
add signal utility functions
2 parents df48fe0 + 0884a58 commit b3b8814

File tree

3 files changed

+148
-0
lines changed

3 files changed

+148
-0
lines changed

include/libswiftnav/signal.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ enum band {
3333
#define GPS_FIRST_PRN 0
3434
#define SBAS_FIRST_PRN 120
3535

36+
#define SID_STR_LEN_MAX 16
37+
3638
typedef struct __attribute__((packed)) {
3739
u16 sat;
3840
u8 band;
@@ -59,5 +61,23 @@ static inline bool sid_is_equal(const gnss_signal_t a, const gnss_signal_t b)
5961
return sid_compare(a, b) == 0;
6062
}
6163

64+
/** Print a string representation of sid to the buffer s of capacity n.
65+
* Returns the number of characters printed, excluding the terminating null.
66+
*/
67+
int sid_to_string(char *s, int n, gnss_signal_t sid);
68+
69+
/** Returns true if sid corresponds to a known constellation, band, and
70+
* satellite identifier.
71+
*/
72+
bool sid_valid(gnss_signal_t sid);
73+
74+
/** Converts the global index i in [0, NUM_SATS) to the corresponding sid
75+
*/
76+
gnss_signal_t sid_from_index(u32 i);
77+
78+
/** Converts sid to a global index in [0, NUM_SATS)
79+
*/
80+
u32 sid_to_index(gnss_signal_t sid);
81+
6282
#endif /* LIBSWIFTNAV_SIGNAL_H */
6383

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ set(libswiftnav_SRCS
4141
ambiguity_test.c
4242
printing_utils.c
4343
filter_utils.c
44+
signal.c
4445
${plover_SRCS}
4546

4647
CACHE INTERNAL ""

src/signal.c

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/*
2+
* Copyright (c) 2015 Swift Navigation Inc.
3+
* Contact: Jacob McNamee <jacob@swiftnav.com>
4+
*
5+
* This source is subject to the license found in the file 'LICENSE' which must
6+
* be be distributed together with this source. All other rights reserved.
7+
*
8+
* THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
9+
* EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
10+
* WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
11+
*/
12+
13+
#include "signal.h"
14+
15+
#include <stdio.h>
16+
#include <assert.h>
17+
18+
typedef struct {
19+
u8 constellation;
20+
u8 band;
21+
u16 sat_count;
22+
u16 (*sat_get)(u32 local_index);
23+
u32 (*local_index_get)(u16 sat);
24+
} sat_group_t;
25+
26+
static const char * constellation_strs[CONSTELLATION_COUNT] = {
27+
[CONSTELLATION_GPS] = "GPS",
28+
[CONSTELLATION_SBAS] = "SBAS"
29+
};
30+
31+
static const char * band_strs[BAND_COUNT] = {
32+
[BAND_L1] = "L1"
33+
};
34+
35+
static const char * unknown_str = "?";
36+
37+
static u16 sat_get_gps(u32 local_index);
38+
static u16 sat_get_sbas(u32 local_index);
39+
static u32 local_index_get_gps(u16 sat);
40+
static u32 local_index_get_sbas(u16 sat);
41+
42+
static const sat_group_t sat_groups[CONSTELLATION_COUNT] = {
43+
[CONSTELLATION_GPS] = {
44+
CONSTELLATION_GPS, BAND_L1, NUM_SATS_GPS,
45+
sat_get_gps, local_index_get_gps
46+
},
47+
[CONSTELLATION_SBAS] = {
48+
CONSTELLATION_SBAS, BAND_L1, NUM_SATS_SBAS,
49+
sat_get_sbas, local_index_get_sbas
50+
}
51+
};
52+
53+
static u16 sat_get_gps(u32 local_index)
54+
{
55+
return local_index + GPS_FIRST_PRN;
56+
}
57+
58+
static u16 sat_get_sbas(u32 local_index)
59+
{
60+
return local_index + SBAS_FIRST_PRN;
61+
}
62+
63+
static u32 local_index_get_gps(u16 sat)
64+
{
65+
return sat - GPS_FIRST_PRN;
66+
}
67+
68+
static u32 local_index_get_sbas(u16 sat)
69+
{
70+
return sat - SBAS_FIRST_PRN;
71+
}
72+
73+
int sid_to_string(char *s, int n, gnss_signal_t sid)
74+
{
75+
const char *constellation_str = (sid.constellation < CONSTELLATION_COUNT) ?
76+
constellation_strs[sid.constellation] : unknown_str;
77+
const char *band_str = (sid.band < BAND_COUNT) ?
78+
band_strs[sid.band] : unknown_str;
79+
80+
int nchars = snprintf(s, n, "%s %s %u", constellation_str, band_str, sid.sat);
81+
s[n-1] = 0;
82+
return nchars;
83+
}
84+
85+
bool sid_valid(gnss_signal_t sid)
86+
{
87+
if (sid.constellation >= CONSTELLATION_COUNT)
88+
return false;
89+
if (sid.band >= BAND_COUNT)
90+
return false;
91+
if (sat_groups[sid.constellation].local_index_get(sid.sat)
92+
>= sat_groups[sid.constellation].sat_count)
93+
return false;
94+
return true;
95+
}
96+
97+
gnss_signal_t sid_from_index(u32 i)
98+
{
99+
assert(i < NUM_SATS);
100+
gnss_signal_t sid = {0, 0, 0};
101+
u32 offset = i;
102+
u32 group_index = 0;
103+
while (group_index < sizeof(sat_groups) / sizeof(sat_groups[0])) {
104+
if (offset >= sat_groups[group_index].sat_count) {
105+
offset -= sat_groups[group_index].sat_count;
106+
group_index++;
107+
} else {
108+
sid = (gnss_signal_t) {
109+
.constellation = sat_groups[group_index].constellation,
110+
.band = sat_groups[group_index].band,
111+
.sat = sat_groups[group_index].sat_get(offset)
112+
};
113+
return sid;
114+
}
115+
}
116+
assert("sid_from_index() failed");
117+
return sid;
118+
}
119+
120+
u32 sid_to_index(gnss_signal_t sid)
121+
{
122+
assert(sid_valid(sid));
123+
u32 offset = 0;
124+
for (u32 i=0; i<sid.constellation; i++)
125+
offset += sat_groups[i].sat_count;
126+
return offset + sat_groups[sid.constellation].local_index_get(sid.sat);
127+
}

0 commit comments

Comments
 (0)