Neels Hofmeyr | 086bd33 | 2020-09-18 18:00:50 +0200 | [diff] [blame] | 1 | /*! \addtogroup gad |
| 2 | * @{ |
| 3 | * \file gad.h |
| 4 | * Message encoding and decoding for 3GPP TS 23.032 GAD: Universal Geographical Area Description. |
| 5 | */ |
| 6 | /* |
| 7 | * (C) 2020 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de> |
| 8 | * |
| 9 | * All Rights Reserved |
| 10 | * |
| 11 | * Author: Neels Hofmeyr <neels@hofmeyr.de> |
| 12 | * |
| 13 | * This program is free software; you can redistribute it and/or modify |
| 14 | * it under the terms of the GNU Affero General Public License as published by |
| 15 | * the Free Software Foundation; either version 3 of the License, or |
| 16 | * (at your option) any later version. |
| 17 | * |
| 18 | * This program is distributed in the hope that it will be useful, |
| 19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | * GNU Affero General Public License for more details. |
| 22 | * |
| 23 | * You should have received a copy of the GNU Affero General Public License |
| 24 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 25 | * |
| 26 | */ |
| 27 | |
| 28 | #pragma once |
| 29 | |
| 30 | #include <osmocom/gsm/protocol/gsm_23_032.h> |
| 31 | #include <osmocom/core/utils.h> |
| 32 | |
| 33 | struct msgb; |
| 34 | |
| 35 | struct osmo_gad_ell_point { |
| 36 | /*! Latitude in micro degrees (degrees * 1e6), -90'000'000 (S) .. 90'000'000 (N). */ |
| 37 | int32_t lat; |
| 38 | /*! Longitude in micro degrees (degrees * 1e6), -180'000'000 (W) .. 180'000'000 (E). */ |
| 39 | int32_t lon; |
| 40 | }; |
| 41 | |
| 42 | struct osmo_gad_ell_point_unc_circle { |
| 43 | /*! Latitude in micro degrees (degrees * 1e6), -90'000'000 (S) .. 90'000'000 (N). */ |
| 44 | int32_t lat; |
| 45 | /*! Longitude in micro degrees (degrees * 1e6), -180'000'000 (W) .. 180'000'000 (E). */ |
| 46 | int32_t lon; |
| 47 | /*! Uncertainty circle radius in millimeters (m * 1e3), 0 .. 18'000'000. */ |
| 48 | uint32_t unc; |
| 49 | }; |
| 50 | |
| 51 | struct osmo_gad_ell_point_unc_ellipse { |
| 52 | /*! Latitude in micro degrees (degrees * 1e6), -90'000'000 (S) .. 90'000'000 (N). */ |
| 53 | int32_t lat; |
| 54 | /*! Longitude in micro degrees (degrees * 1e6), -180'000'000 (W) .. 180'000'000 (E). */ |
| 55 | int32_t lon; |
| 56 | /*! Uncertainty ellipsoid radius of major axis in millimeters, 0 .. 18'000'000. |
| 57 | * Coding of uncertainty is non-linear, use osmo_gad_dec_unc(osmo_gad_enc_unc(val)) to clamp. */ |
| 58 | uint32_t unc_semi_major; |
| 59 | /*! Uncertainty ellipsoid radius of minor axis in millimeters, 0 .. 18'000'000. |
| 60 | * Coding of uncertainty is non-linear, use osmo_gad_dec_unc(osmo_gad_enc_unc(val)) to clamp. */ |
| 61 | uint32_t unc_semi_minor; |
| 62 | /*! Major axis orientation in degrees (DEG), 0 (N) .. 90 (E) .. 179 (SSE). */ |
| 63 | uint8_t major_ori; |
| 64 | /*! Confidence in percent, 0 = no information, 1..100%, 101..128 = no information. */ |
| 65 | uint8_t confidence; |
| 66 | }; |
| 67 | |
| 68 | struct osmo_gad_polygon { |
| 69 | uint8_t num_points; |
| 70 | struct osmo_gad_ell_point point[15]; |
| 71 | }; |
| 72 | |
| 73 | struct osmo_gad_ell_point_alt { |
| 74 | /*! latitude in micro degrees (degrees * 1e6), -90'000'000 (S) .. 90'000'000 (N). */ |
| 75 | int32_t lat; |
| 76 | /*! longitude in micro degrees (degrees * 1e6), -180'000'000 (W) .. 180'000'000 (E). */ |
| 77 | int32_t lon; |
| 78 | /*! Altitude in meters, -32767 (depth) .. 32767 (height) */ |
| 79 | int16_t alt; |
| 80 | }; |
| 81 | |
| 82 | struct osmo_gad_ell_point_alt_unc_ell { |
| 83 | /*! latitude in micro degrees (degrees * 1e6), -90'000'000 (S) .. 90'000'000 (N). */ |
| 84 | int32_t lat; |
| 85 | /*! longitude in micro degrees (degrees * 1e6), -180'000'000 (W) .. 180'000'000 (E). */ |
| 86 | int32_t lon; |
| 87 | /*! Altitude in meters, -32767 (depth) .. 32767 (height) */ |
| 88 | int16_t alt; |
| 89 | /*! Uncertainty ellipsoid radius of major axis in millimeters, 0 .. 18'000'000. |
| 90 | * Coding of uncertainty is non-linear, use osmo_gad_dec_unc(osmo_gad_enc_unc(val)) to clamp. */ |
| 91 | uint32_t unc_semi_major; |
| 92 | /*! Uncertainty ellipsoid radius of minor axis in millimeters, 0 .. 18'000'000. |
| 93 | * Coding of uncertainty is non-linear, use osmo_gad_dec_unc(osmo_gad_enc_unc(val)) to clamp. */ |
| 94 | uint32_t unc_semi_minor; |
| 95 | /*! Major axis orientation in degrees (DEG), 0 (N) .. 90 (E) .. 179 (SSE). */ |
| 96 | uint8_t major_ori; |
| 97 | /*! Uncertainty altitude in millimeters, 0 .. 990'000. |
| 98 | * Coding of uncertainty altitude is non-linear, and distinct from the non-altitude uncertainty coding. Use |
| 99 | * osmo_gad_dec_unc_alt(osmo_gad_enc_unc_alt(val)) to clamp. */ |
| 100 | int32_t unc_alt; |
| 101 | /*! Confidence in percent, 0 = no information, 1..100%, 101..128 = no information. */ |
| 102 | uint8_t confidence; |
| 103 | }; |
| 104 | |
| 105 | struct osmo_gad_ell_arc { |
| 106 | /*! latitude in micro degrees (degrees * 1e6), -90'000'000 (S) .. 90'000'000 (N). */ |
| 107 | int32_t lat; |
| 108 | /*! longitude in micro degrees (degrees * 1e6), -180'000'000 (W) .. 180'000'000 (E). */ |
| 109 | int32_t lon; |
| 110 | /*! inner circle radius in mm (m * 1e3) */ |
| 111 | uint32_t inner_r; |
| 112 | /*! Uncertainty circle radius in millimeters, 0 .. 18'000'000. |
| 113 | * Coding of uncertainty is non-linear, use osmo_gad_dec_unc(osmo_gad_enc_unc(val)) to clamp. */ |
| 114 | uint32_t unc_r; |
| 115 | /*! Offset angle of first arc edge in degrees from North clockwise (eastwards), 0..359. |
| 116 | * Angle is coded in increments of 2 degrees. */ |
| 117 | uint16_t ofs_angle; |
| 118 | /*! Included angle defining the angular width of the arc, in degrees clockwise, 1..360. |
| 119 | * Angle is coded in increments of 2 degrees. */ |
| 120 | uint16_t incl_angle; |
| 121 | /*! Confidence in percent, 0 = no information, 1..100%, 101..128 = no information. */ |
| 122 | uint8_t confidence; |
| 123 | }; |
| 124 | |
| 125 | struct osmo_gad_ha_ell_point_alt_unc_ell { |
| 126 | /*! latitude in micro degrees (degrees * 1e6), -90'000'000 (S) .. 90'000'000 (N). */ |
| 127 | int32_t lat; |
| 128 | /*! longitude in micro degrees (degrees * 1e6), -180'000'000 (W) .. 180'000'000 (E). */ |
| 129 | int32_t lon; |
| 130 | /*! Altitude in millimeters, -500'000 (depth) .. 10'000'000 (height) */ |
| 131 | int32_t alt; |
| 132 | /*! Uncertainty ellipsoid radius of major axis in millimeters, 0 .. 46'491. |
| 133 | * Coding of high-accuracy uncertainty is non-linear, use osmo_gad_dec_ha_unc(osmo_gad_enc_ha_unc(val)) to |
| 134 | * clamp. */ |
| 135 | uint32_t unc_semi_major; |
| 136 | /*! Uncertainty ellipsoid radius of minor axis in millimeters, 0 .. 46'491. |
| 137 | * Coding of high-accuracy uncertainty is non-linear, use osmo_gad_dec_ha_unc(osmo_gad_enc_ha_unc(val)) to |
| 138 | * clamp. */ |
| 139 | uint32_t unc_semi_minor; |
| 140 | /*! Major axis orientation in degrees (DEG), 0 (N) .. 90 (E) .. 179 (SSE). */ |
| 141 | uint8_t major_ori; |
| 142 | /*! Horizontal confidence in percent, 0 = no information, 1..100%, 101..128 = no information. */ |
| 143 | uint8_t h_confidence; |
| 144 | /*! High-Accuracy uncertainty altitude */ |
| 145 | int32_t unc_alt; |
| 146 | /*! Vertical confidence in percent, 0 = no information, 1..100%, 101..128 = no information. */ |
| 147 | uint8_t v_confidence; |
| 148 | }; |
| 149 | |
| 150 | struct osmo_gad { |
| 151 | enum gad_type type; |
| 152 | union { |
| 153 | struct osmo_gad_ell_point ell_point; |
| 154 | struct osmo_gad_ell_point_unc_circle ell_point_unc_circle; |
| 155 | struct osmo_gad_ell_point_unc_ellipse ell_point_unc_ellipse; |
| 156 | struct osmo_gad_polygon polygon; |
| 157 | struct osmo_gad_ell_point_alt ell_point_alt; |
| 158 | struct osmo_gad_ell_point_alt_unc_ell ell_point_alt_unc_ell; |
| 159 | struct osmo_gad_ell_arc ell_arc; |
| 160 | struct osmo_gad_ell_point_unc_ellipse ha_ell_point_unc_ellipse; |
| 161 | struct osmo_gad_ha_ell_point_alt_unc_ell ha_ell_point_alt_unc_ell; |
| 162 | }; |
| 163 | }; |
| 164 | |
| 165 | struct osmo_gad_err { |
| 166 | int rc; |
| 167 | enum gad_type type; |
| 168 | char *logmsg; |
| 169 | }; |
| 170 | |
| 171 | extern const struct value_string osmo_gad_type_names[]; |
| 172 | static inline const char *osmo_gad_type_name(enum gad_type val) |
| 173 | { return get_value_string(osmo_gad_type_names, val); } |
| 174 | |
| 175 | int osmo_gad_raw_write(struct msgb *msg, const union gad_raw *gad_raw); |
| 176 | int osmo_gad_raw_read(union gad_raw *gad_raw, struct osmo_gad_err **err, void *err_ctx, const uint8_t *data, uint8_t len); |
| 177 | |
| 178 | int osmo_gad_enc(union gad_raw *gad_raw, const struct osmo_gad *gad); |
| 179 | int osmo_gad_dec(struct osmo_gad *gad, struct osmo_gad_err **err, void *err_ctx, const union gad_raw *gad_raw); |
| 180 | |
| 181 | int osmo_gad_to_str_buf(char *buf, size_t buflen, const struct osmo_gad *gad); |
| 182 | char *osmo_gad_to_str_c(void *ctx, const struct osmo_gad *gad); |
| 183 | |
| 184 | uint32_t osmo_gad_enc_lat(int32_t deg_1e6); |
| 185 | int32_t osmo_gad_dec_lat(uint32_t lat); |
| 186 | uint32_t osmo_gad_enc_lon(int32_t deg_1e6); |
| 187 | int32_t osmo_gad_dec_lon(uint32_t lon); |
| 188 | uint8_t osmo_gad_enc_unc(uint32_t mm); |
| 189 | uint32_t osmo_gad_dec_unc(uint8_t unc); |
| 190 | /*! @} */ |