|
| 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