Sylvain Munaut | ce7be3a | 2009-12-22 21:53:22 +0100 | [diff] [blame] | 1 | /* |
| 2 | * rrlp.c |
| 3 | * |
| 4 | * RRLP implementation |
| 5 | * |
| 6 | * |
| 7 | * Copyright (C) 2009 Sylvain Munaut <tnt@246tNt.com> |
| 8 | * |
| 9 | * This program is free software: you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation, either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 21 | */ |
| 22 | |
| 23 | |
| 24 | #include <errno.h> |
Sylvain Munaut | 7a3e51f | 2009-12-28 14:31:41 +0100 | [diff] [blame] | 25 | #include <math.h> |
Sylvain Munaut | ce7be3a | 2009-12-22 21:53:22 +0100 | [diff] [blame] | 26 | |
| 27 | #include "gps.h" |
| 28 | #include "rrlp.h" |
| 29 | |
| 30 | #include <PDU.h> |
| 31 | #include <GPS-AssistData.h> |
| 32 | #include <NavigationModel.h> |
| 33 | #include <IonosphericModel.h> |
| 34 | #include <UTCModel.h> |
| 35 | #include <Almanac.h> |
| 36 | #include <RefLocation.h> |
| 37 | #include <ReferenceTime.h> |
| 38 | |
| 39 | |
| 40 | /* ------------------------------------------------------------------------ */ |
| 41 | /* RRLP Assistance request decoding */ |
| 42 | /* ---------------------------------------------------------------------{{{ */ |
| 43 | /* Decode and validate the assistance data request messages. |
| 44 | * See section 10.10 of |
| 45 | * . ETSI TS 149 031 V8.1.0 (2009-01) |
| 46 | * . 3GPP TS 49.031 version 8.1.0 Release 8 |
| 47 | */ |
| 48 | |
| 49 | /* Packed structure from 49.031 spec (RGA = Request GPS Assistance) */ |
| 50 | |
| 51 | #define RRLP_RGA0_ALMANAC (1<<0) |
| 52 | #define RRLP_RGA0_UTC_MODEL (1<<1) |
| 53 | #define RRLP_RGA0_IONO_MODEL (1<<2) |
| 54 | #define RRLP_RGA0_NAV_MODEL (1<<3) |
| 55 | #define RRLP_RGA0_DGPS (1<<4) |
| 56 | #define RRLP_RGA0_REF_LOC (1<<5) |
| 57 | #define RRLP_RGA0_REF_TIME (1<<6) |
| 58 | #define RRLP_RGA0_ACQ_ASSIST (1<<7) |
| 59 | |
| 60 | #define RRLP_RGA1_REALTIME_INT (1<<0) |
| 61 | #define RRLP_RGA1_EPH_EXT (1<<1) |
| 62 | #define RRLP_RGA1_EPH_EXT_CHECK (1<<2) |
| 63 | |
| 64 | struct rrlp_rga_hdr { |
| 65 | uint8_t items0; |
| 66 | uint8_t items1; |
| 67 | } __attribute__((packed)); |
| 68 | |
| 69 | struct rrlp_rga_eph_sv { |
| 70 | uint8_t sv_id; /* [7:6] reserved, [5:0] sv_id */ |
| 71 | uint8_t iode; /* latest eph in the MS memory in hours */ |
| 72 | } __attribute__((packed)); |
| 73 | |
| 74 | struct rrlp_rga_eph { |
| 75 | uint8_t wn_hi; /* [7:6] = wn[9:8] */ |
| 76 | uint8_t wn_lo; /* wn[7:0] */ |
| 77 | uint8_t toe; /* latest eph in the MS memory in hours */ |
| 78 | uint8_t nsat_tmtoe; /* [7:4] nstat, [3:0] T-Toe limit */ |
| 79 | struct rrlp_rga_eph_sv svs[0]; |
| 80 | } __attribute__((packed)); |
| 81 | |
| 82 | struct rrlp_rga_eph_ext { |
| 83 | uint8_t validity; /* in 4 hours units */ |
| 84 | } __attribute__((packed)); |
| 85 | |
| 86 | struct rrlp_rga_eph_ext_check { |
| 87 | /* weeks are in gps week modulo 4 */ |
| 88 | uint8_t wn_begin_end; /* [7:4] begin, [3:0] end */ |
| 89 | uint8_t tow_begin; |
| 90 | uint8_t tow_end; |
| 91 | } __attribute__((packed)); |
| 92 | |
| 93 | |
| 94 | /* Parsing function */ |
| 95 | |
| 96 | int |
| 97 | rrlp_decode_assistance_request( |
| 98 | struct rrlp_assist_req *ar, |
| 99 | void *req, int req_len) |
| 100 | { |
| 101 | struct rrlp_rga_hdr *hdr = NULL; |
| 102 | struct rrlp_rga_eph *eph = NULL; |
| 103 | struct rrlp_rga_eph_ext *eph_ext = NULL; |
| 104 | struct rrlp_rga_eph_ext_check *eph_ext_check = NULL; |
| 105 | int p = 0; |
| 106 | |
| 107 | /* Reset */ |
| 108 | ar->req_elems = 0; |
| 109 | ar->eph_svs = 0; |
| 110 | |
| 111 | /* Parse message */ |
| 112 | hdr = req; |
| 113 | p += sizeof(struct rrlp_rga_hdr); |
| 114 | if (p > req_len) |
| 115 | return -1; |
| 116 | |
| 117 | if (hdr->items0 & RRLP_RGA0_NAV_MODEL) { |
| 118 | eph = req + p; |
| 119 | p += sizeof(struct rrlp_rga_eph); |
| 120 | if (p > req_len) |
| 121 | return -1; |
| 122 | p += (eph->nsat_tmtoe >> 4) * sizeof(struct rrlp_rga_eph_sv); |
| 123 | if (p > req_len) |
| 124 | return -1; |
| 125 | } |
| 126 | |
| 127 | if (hdr->items1 & RRLP_RGA1_EPH_EXT) { |
| 128 | eph_ext = req + p; |
| 129 | p += sizeof(struct rrlp_rga_eph_ext); |
| 130 | if (p > req_len) |
| 131 | return -1; |
| 132 | } |
| 133 | |
| 134 | if (hdr->items1 & RRLP_RGA1_EPH_EXT_CHECK) { |
| 135 | eph_ext_check = req + p; |
| 136 | p += sizeof(struct rrlp_rga_eph_ext_check); |
| 137 | if (p > req_len) |
| 138 | return -1; |
| 139 | } |
| 140 | |
| 141 | if (p != req_len) |
| 142 | return -2; /* not all bytes consumed ??? */ |
| 143 | |
| 144 | /* Print a warning for unsupported requests */ |
| 145 | if ((eph_ext != NULL) || |
| 146 | (eph_ext_check != NULL) || |
| 147 | (hdr->items0 & (RRLP_RGA0_DGPS | RRLP_RGA0_ACQ_ASSIST)) || |
| 148 | (hdr->items1 & RRLP_RGA1_REALTIME_INT)) { |
| 149 | fprintf(stderr, "[w] Unsupported assistance data requested, ignored ...\n"); |
| 150 | } |
| 151 | |
| 152 | /* Copy the request */ |
| 153 | if (hdr->items0 & RRLP_RGA0_ALMANAC) |
| 154 | ar->req_elems |= RRLP_AR_ALMANAC; |
| 155 | |
| 156 | if (hdr->items0 & RRLP_RGA0_UTC_MODEL) |
| 157 | ar->req_elems |= RRLP_AR_UTC_MODEL; |
| 158 | |
| 159 | if (hdr->items0 & RRLP_RGA0_IONO_MODEL) |
| 160 | ar->req_elems |= RRLP_AR_IONO_MODEL; |
| 161 | |
| 162 | if (hdr->items0 & RRLP_RGA0_REF_LOC) |
| 163 | ar->req_elems |= RRLP_AR_REF_LOC; |
| 164 | |
| 165 | if (hdr->items0 & RRLP_RGA0_REF_TIME) |
| 166 | ar->req_elems |= RRLP_AR_REF_TIME; |
| 167 | |
| 168 | if (hdr->items0 & RRLP_RGA0_NAV_MODEL) { |
| 169 | int i, n_svs = eph->nsat_tmtoe >> 4; |
| 170 | ar->req_elems |= RRLP_AR_EPHEMERIS; |
| 171 | for (i=0; i<n_svs; i++) |
| 172 | ar->eph_svs |= (1ULL << (eph->svs[i].sv_id - 1)); |
| 173 | } |
| 174 | |
| 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | /* }}} */ |
| 179 | |
| 180 | |
| 181 | /* ------------------------------------------------------------------------ */ |
| 182 | /* RRLP elements fill */ |
| 183 | /* ---------------------------------------------------------------------{{{ */ |
| 184 | |
Sylvain Munaut | 7a3e51f | 2009-12-28 14:31:41 +0100 | [diff] [blame] | 185 | /* Helpers */ |
| 186 | |
| 187 | static void |
| 188 | _ts_23_032_store_latitude(double lat, uint8_t *b) |
| 189 | { |
| 190 | uint32_t x; |
| 191 | x = (uint32_t) floor(fabs(lat/90.0) * ((double)(1<<23))); |
| 192 | if (x >= (1<<23)) |
| 193 | x = (1<<23) - 1; |
| 194 | if (lat < 0.0) |
| 195 | x |= (1<<23); |
| 196 | b[0] = (x >> 16) & 0xff; |
| 197 | b[1] = (x >> 8) & 0xff; |
| 198 | b[2] = x & 0xff; |
| 199 | } |
| 200 | |
| 201 | static void |
| 202 | _ts_23_032_store_longitude(double lon, uint8_t *b) |
| 203 | { |
| 204 | int32_t x; |
| 205 | x = floor((lon/360.0) * ((double)(1<<24))); |
| 206 | if (x >= (1<<23)) |
| 207 | x = 0x007fffff; |
| 208 | else if (x < -(1<<23)) |
| 209 | x = 0x00800000; |
| 210 | b[0] = (x >> 16) & 0xff; |
| 211 | b[1] = (x >> 8) & 0xff; |
| 212 | b[2] = x & 0xff; |
| 213 | } |
| 214 | |
| 215 | static void |
| 216 | _ts_23_032_store_altitude(double alt, uint8_t *b) |
| 217 | { |
| 218 | int alt_i = (int)fabs(alt); |
| 219 | b[0] = ((alt_i >> 8) & 0x7f) | (alt<0.0 ? 0x80 : 0x00); |
| 220 | b[1] = alt_i & 0xff; |
| 221 | } |
| 222 | |
| 223 | |
| 224 | /* Fill methods */ |
| 225 | |
Sylvain Munaut | ce7be3a | 2009-12-22 21:53:22 +0100 | [diff] [blame] | 226 | static void |
| 227 | _rrlp_fill_navigation_model_element( |
| 228 | struct NavModelElement *rrlp_nme, |
| 229 | struct gps_ephemeris_sv *gps_eph_sv) |
| 230 | { |
| 231 | struct UncompressedEphemeris *rrlp_eph; |
| 232 | |
| 233 | rrlp_nme->satStatus.present = SatStatus_PR_newSatelliteAndModelUC; |
| 234 | rrlp_nme->satelliteID = gps_eph_sv->sv_id; |
| 235 | |
| 236 | rrlp_eph = &rrlp_nme->satStatus.choice.newSatelliteAndModelUC; |
| 237 | |
| 238 | rrlp_eph->ephemCodeOnL2 = gps_eph_sv->code_on_l2; |
| 239 | rrlp_eph->ephemURA = gps_eph_sv->sv_ura; |
| 240 | rrlp_eph->ephemSVhealth = gps_eph_sv->sv_health; |
| 241 | rrlp_eph->ephemIODC = gps_eph_sv->iodc; |
| 242 | rrlp_eph->ephemL2Pflag = gps_eph_sv->l2_p_flag; |
| 243 | rrlp_eph->ephemTgd = gps_eph_sv->t_gd; |
| 244 | rrlp_eph->ephemToc = gps_eph_sv->t_oc; |
| 245 | rrlp_eph->ephemAF2 = gps_eph_sv->a_f2; |
| 246 | rrlp_eph->ephemAF1 = gps_eph_sv->a_f1; |
| 247 | rrlp_eph->ephemAF0 = gps_eph_sv->a_f0; |
| 248 | rrlp_eph->ephemCrs = gps_eph_sv->c_rs; |
| 249 | rrlp_eph->ephemDeltaN = gps_eph_sv->delta_n; |
| 250 | rrlp_eph->ephemM0 = gps_eph_sv->m_0; |
| 251 | rrlp_eph->ephemCuc = gps_eph_sv->c_uc; |
| 252 | rrlp_eph->ephemE = gps_eph_sv->e; |
| 253 | rrlp_eph->ephemCus = gps_eph_sv->c_us; |
| 254 | rrlp_eph->ephemAPowerHalf = gps_eph_sv->a_powhalf; |
| 255 | rrlp_eph->ephemToe = gps_eph_sv->t_oe; |
| 256 | rrlp_eph->ephemFitFlag = gps_eph_sv->fit_flag; |
| 257 | rrlp_eph->ephemAODA = gps_eph_sv->aodo; |
| 258 | rrlp_eph->ephemCic = gps_eph_sv->c_ic; |
| 259 | rrlp_eph->ephemOmegaA0 = gps_eph_sv->omega_0; |
| 260 | rrlp_eph->ephemCis = gps_eph_sv->c_is; |
| 261 | rrlp_eph->ephemI0 = gps_eph_sv->i_0; |
| 262 | rrlp_eph->ephemCrc = gps_eph_sv->c_rc; |
| 263 | rrlp_eph->ephemW = gps_eph_sv->w; |
| 264 | rrlp_eph->ephemOmegaADot = gps_eph_sv->omega_dot; |
| 265 | rrlp_eph->ephemIDot = gps_eph_sv->idot; |
| 266 | |
| 267 | rrlp_eph->ephemSF1Rsvd.reserved1 = gps_eph_sv->_rsvd1; |
| 268 | rrlp_eph->ephemSF1Rsvd.reserved2 = gps_eph_sv->_rsvd2; |
| 269 | rrlp_eph->ephemSF1Rsvd.reserved3 = gps_eph_sv->_rsvd3; |
| 270 | rrlp_eph->ephemSF1Rsvd.reserved4 = gps_eph_sv->_rsvd4; |
| 271 | } |
| 272 | |
| 273 | static void |
| 274 | _rrlp_fill_almanac_element( |
| 275 | struct AlmanacElement *rrlp_ae, |
| 276 | struct gps_almanac_sv *gps_alm_sv) |
| 277 | { |
| 278 | rrlp_ae->satelliteID = gps_alm_sv->sv_id; |
| 279 | |
| 280 | rrlp_ae->almanacE = gps_alm_sv->e; |
| 281 | rrlp_ae->alamanacToa = gps_alm_sv->t_oa; |
| 282 | rrlp_ae->almanacKsii = gps_alm_sv->ksii; |
| 283 | rrlp_ae->almanacOmegaDot = gps_alm_sv->omega_dot; |
| 284 | rrlp_ae->almanacSVhealth = gps_alm_sv->sv_health; |
| 285 | rrlp_ae->almanacAPowerHalf = gps_alm_sv->a_powhalf; |
| 286 | rrlp_ae->almanacOmega0 = gps_alm_sv->omega_0; |
| 287 | rrlp_ae->almanacW = gps_alm_sv->w; |
| 288 | rrlp_ae->almanacM0 = gps_alm_sv->m_0; |
| 289 | rrlp_ae->almanacAF0 = gps_alm_sv->a_f0; |
| 290 | rrlp_ae->almanacAF1 = gps_alm_sv->a_f1; |
| 291 | |
| 292 | } |
| 293 | |
| 294 | static void |
| 295 | _rrlp_fill_ionospheric_model( |
| 296 | struct IonosphericModel *rrlp_iono, |
| 297 | struct gps_ionosphere_model *gps_iono) |
| 298 | { |
| 299 | rrlp_iono->alfa0 = gps_iono->alpha_0; |
| 300 | rrlp_iono->alfa1 = gps_iono->alpha_1; |
| 301 | rrlp_iono->alfa2 = gps_iono->alpha_2; |
| 302 | rrlp_iono->alfa3 = gps_iono->alpha_3; |
| 303 | rrlp_iono->beta0 = gps_iono->beta_0; |
| 304 | rrlp_iono->beta1 = gps_iono->beta_1; |
| 305 | rrlp_iono->beta2 = gps_iono->beta_2; |
| 306 | rrlp_iono->beta3 = gps_iono->beta_3; |
| 307 | } |
| 308 | |
| 309 | static void |
| 310 | _rrlp_fill_utc_model( |
| 311 | struct UTCModel *rrlp_utc, |
| 312 | struct gps_utc_model *gps_utc) |
| 313 | { |
| 314 | rrlp_utc->utcA1 = gps_utc->a1; |
| 315 | rrlp_utc->utcA0 = gps_utc->a0; |
| 316 | rrlp_utc->utcTot = gps_utc->t_ot; |
| 317 | rrlp_utc->utcWNt = gps_utc->wn_t & 0xff; |
| 318 | rrlp_utc->utcDeltaTls = gps_utc->delta_t_ls; |
| 319 | rrlp_utc->utcWNlsf = gps_utc->wn_lsf & 0xff; |
| 320 | rrlp_utc->utcDN = gps_utc->dn; |
| 321 | rrlp_utc->utcDeltaTlsf = gps_utc->delta_t_lsf; |
| 322 | } |
| 323 | |
| 324 | /* }}} */ |
| 325 | |
| 326 | |
| 327 | /* ------------------------------------------------------------------------ */ |
| 328 | /* RRLP Assistance PDU Generation */ |
| 329 | /* ---------------------------------------------------------------------{{{ */ |
| 330 | |
| 331 | struct PDU * |
| 332 | _rrlp_create_gps_assist_pdu(int refnum, struct GPS_AssistData **o_gps_ad) |
| 333 | { |
| 334 | struct PDU *pdu; |
| 335 | struct GPS_AssistData *gps_ad; |
| 336 | |
| 337 | pdu = calloc(1, sizeof(*pdu)); |
| 338 | if (!pdu) |
| 339 | return NULL; |
| 340 | |
| 341 | gps_ad = calloc(1, sizeof(*gps_ad)); |
| 342 | if (!gps_ad) { |
| 343 | free(pdu); |
| 344 | return NULL; |
| 345 | } |
| 346 | |
| 347 | if (o_gps_ad) |
| 348 | *o_gps_ad = gps_ad; |
| 349 | |
| 350 | pdu->referenceNumber = refnum; |
| 351 | pdu->component.present = RRLP_Component_PR_assistanceData; |
| 352 | pdu->component.choice.assistanceData.gps_AssistData = gps_ad; |
| 353 | |
| 354 | return pdu; |
| 355 | } |
| 356 | |
| 357 | static int |
| 358 | _rrlp_add_ionospheric_model( |
| 359 | struct GPS_AssistData *rrlp_gps_ad, |
| 360 | struct gps_assist_data *gps_ad) |
| 361 | { |
| 362 | struct IonosphericModel *rrlp_iono; |
| 363 | |
| 364 | if (!(gps_ad->fields & GPS_FIELD_IONOSPHERE)) |
| 365 | return -EINVAL; |
| 366 | |
| 367 | rrlp_iono = calloc(1, sizeof(*rrlp_iono)); |
| 368 | if (!rrlp_iono) |
| 369 | return -ENOMEM; |
| 370 | rrlp_gps_ad->controlHeader.ionosphericModel = rrlp_iono; |
| 371 | |
| 372 | _rrlp_fill_ionospheric_model(rrlp_iono, &gps_ad->ionosphere); |
| 373 | |
| 374 | return 0; |
| 375 | } |
| 376 | |
| 377 | static int |
| 378 | _rrlp_add_utc_model( |
| 379 | struct GPS_AssistData *rrlp_gps_ad, |
| 380 | struct gps_assist_data *gps_ad) |
| 381 | { |
| 382 | struct UTCModel *rrlp_utc; |
| 383 | |
| 384 | if (!(gps_ad->fields & GPS_FIELD_UTC)) |
| 385 | return -EINVAL; |
| 386 | |
| 387 | rrlp_utc = calloc(1, sizeof(*rrlp_utc)); |
| 388 | if (!rrlp_utc) |
| 389 | return -ENOMEM; |
| 390 | rrlp_gps_ad->controlHeader.utcModel = rrlp_utc; |
| 391 | |
| 392 | _rrlp_fill_utc_model(rrlp_utc, &gps_ad->utc); |
| 393 | |
| 394 | return 0; |
| 395 | } |
| 396 | |
| 397 | static int |
| 398 | _rrlp_add_reference_location( |
| 399 | struct GPS_AssistData *rrlp_gps_ad, |
| 400 | struct gps_assist_data *gps_ad) |
| 401 | { |
| 402 | struct RefLocation *rrlp_refloc; |
Sylvain Munaut | 7a3e51f | 2009-12-28 14:31:41 +0100 | [diff] [blame] | 403 | uint8_t *b; |
Sylvain Munaut | ce7be3a | 2009-12-22 21:53:22 +0100 | [diff] [blame] | 404 | |
Sylvain Munaut | 7a3e51f | 2009-12-28 14:31:41 +0100 | [diff] [blame] | 405 | if (!(gps_ad->fields & GPS_FIELD_REFPOS)) |
| 406 | return -EINVAL; |
Sylvain Munaut | ce7be3a | 2009-12-22 21:53:22 +0100 | [diff] [blame] | 407 | |
| 408 | rrlp_refloc = calloc(1, sizeof(*rrlp_refloc)); |
| 409 | if (!rrlp_refloc) |
| 410 | return -ENOMEM; |
| 411 | rrlp_gps_ad->controlHeader.refLocation = rrlp_refloc; |
| 412 | |
Sylvain Munaut | 7a3e51f | 2009-12-28 14:31:41 +0100 | [diff] [blame] | 413 | b = malloc(9); |
| 414 | |
| 415 | b[0] = 0x80; /* Ellipsoid Point with altitude */ |
| 416 | _ts_23_032_store_latitude(gps_ad->ref_pos.latitude, &b[1]); |
| 417 | _ts_23_032_store_longitude(gps_ad->ref_pos.longitude, &b[4]); |
| 418 | _ts_23_032_store_altitude(gps_ad->ref_pos.altitude, &b[7]); |
| 419 | |
| 420 | rrlp_refloc->threeDLocation.buf = b; |
| 421 | rrlp_refloc->threeDLocation.size = 9; |
Sylvain Munaut | ce7be3a | 2009-12-22 21:53:22 +0100 | [diff] [blame] | 422 | |
| 423 | return 0; |
| 424 | } |
| 425 | |
| 426 | static int |
| 427 | _rrlp_add_reference_time( |
| 428 | struct GPS_AssistData *rrlp_gps_ad, |
| 429 | struct gps_assist_data *gps_ad) |
| 430 | { |
| 431 | struct ReferenceTime *rrlp_reftime; |
| 432 | |
Sylvain Munaut | b53f6c1 | 2009-12-28 15:47:21 +0100 | [diff] [blame] | 433 | if (!(gps_ad->fields & GPS_FIELD_REFTIME)) |
| 434 | return -EINVAL; |
Sylvain Munaut | ce7be3a | 2009-12-22 21:53:22 +0100 | [diff] [blame] | 435 | |
| 436 | rrlp_reftime = calloc(1, sizeof(*rrlp_reftime)); |
| 437 | if (!rrlp_reftime) |
| 438 | return -ENOMEM; |
| 439 | rrlp_gps_ad->controlHeader.referenceTime = rrlp_reftime; |
| 440 | |
Sylvain Munaut | b53f6c1 | 2009-12-28 15:47:21 +0100 | [diff] [blame] | 441 | rrlp_reftime->gpsTime.gpsWeek = gps_ad->ref_time.wn & 0x3ff; /* 10b */ |
| 442 | rrlp_reftime->gpsTime.gpsTOW23b = |
| 443 | ((int)floor(gps_ad->ref_time.tow / 0.08)) & 0x7fffff; /* 23b */ |
Sylvain Munaut | ce7be3a | 2009-12-22 21:53:22 +0100 | [diff] [blame] | 444 | |
| 445 | return 0; |
| 446 | } |
| 447 | |
| 448 | static int |
| 449 | _rrlp_add_almanac( |
| 450 | struct GPS_AssistData *rrlp_gps_ad, |
| 451 | struct gps_assist_data *gps_ad, int *start, int count) |
| 452 | { |
| 453 | int i; |
| 454 | struct Almanac *rrlp_alm; |
| 455 | struct gps_almanac *gps_alm = &gps_ad->almanac; |
| 456 | |
| 457 | if (!(gps_ad->fields & GPS_FIELD_ALMANAC)) |
| 458 | return -EINVAL; |
| 459 | |
| 460 | rrlp_alm = calloc(1, sizeof(*rrlp_alm)); |
| 461 | if (!rrlp_alm) |
| 462 | return -ENOMEM; |
| 463 | rrlp_gps_ad->controlHeader.almanac = rrlp_alm; |
| 464 | |
| 465 | rrlp_alm->alamanacWNa = gps_alm->wna; |
| 466 | if (count == -1) |
| 467 | count = gps_alm->n_sv - *start; |
| 468 | for (i=*start; (i<*start+count) && (i<gps_alm->n_sv); i++) { |
| 469 | struct AlmanacElement *ae; |
| 470 | ae = calloc(1, sizeof(*ae)); |
| 471 | if (!ae) |
| 472 | return -ENOMEM; |
| 473 | _rrlp_fill_almanac_element(ae, &gps_alm->svs[i]); |
| 474 | ASN_SEQUENCE_ADD(&rrlp_alm->almanacList.list, ae); |
| 475 | } |
| 476 | |
| 477 | *start = i; |
| 478 | |
| 479 | return i < gps_alm->n_sv; |
| 480 | } |
| 481 | |
| 482 | static int |
| 483 | _rrlp_add_ephemeris( |
| 484 | struct GPS_AssistData *rrlp_gps_ad, |
| 485 | struct gps_assist_data *gps_ad, int *start, int count, uint64_t mask) |
| 486 | { |
| 487 | int i, j; |
| 488 | struct NavigationModel *rrlp_nav; |
| 489 | struct gps_ephemeris *gps_eph = &gps_ad->ephemeris; |
| 490 | |
| 491 | if (!(gps_ad->fields & GPS_FIELD_EPHEMERIS)) |
| 492 | return -EINVAL; |
| 493 | |
| 494 | rrlp_nav = calloc(1, sizeof(*rrlp_nav)); |
| 495 | if (!rrlp_nav) |
| 496 | return -ENOMEM; |
| 497 | rrlp_gps_ad->controlHeader.navigationModel = rrlp_nav; |
| 498 | |
| 499 | if (count == -1) |
| 500 | count = gps_eph->n_sv - *start; |
| 501 | for (i=*start,j=0; (j<count) && (i<gps_eph->n_sv); i++) { |
| 502 | if (!(mask & (1ULL<<(gps_eph->svs[i].sv_id-1)))) |
| 503 | continue; |
| 504 | struct NavModelElement *nme; |
| 505 | nme = calloc(1, sizeof(*nme)); |
| 506 | if (!nme) |
| 507 | return -ENOMEM; |
| 508 | _rrlp_fill_navigation_model_element(nme, &gps_eph->svs[i]); |
| 509 | ASN_SEQUENCE_ADD(&rrlp_nav->navModelList.list, nme); |
| 510 | j++; |
| 511 | } |
| 512 | |
| 513 | *start = i; |
| 514 | |
| 515 | return i < gps_eph->n_sv; |
| 516 | } |
| 517 | |
| 518 | |
| 519 | #define MAX_PDUS 64 |
| 520 | |
| 521 | int |
| 522 | rrlp_gps_assist_pdus( |
| 523 | struct gps_assist_data *gps_ad, struct rrlp_assist_req *req, |
| 524 | void **o_pdu, int *o_len, int o_max_pdus) |
| 525 | { |
| 526 | struct PDU *lst_pdu[MAX_PDUS]; |
| 527 | int lst_cnt = 0; |
| 528 | |
| 529 | struct PDU *rrlp_pdu = NULL; |
| 530 | struct GPS_AssistData *rrlp_gps_ad = NULL; |
| 531 | uint32_t re = req->req_elems; |
| 532 | int i, rv = 0; |
| 533 | |
| 534 | /* IonosphericModel, UTCModel, RefLocation, ReferenceTime */ |
| 535 | if (re & (RRLP_AR_IONO_MODEL | |
| 536 | RRLP_AR_UTC_MODEL | |
| 537 | RRLP_AR_REF_TIME | |
| 538 | RRLP_AR_REF_LOC)) |
| 539 | { |
| 540 | int pdu_has_data = 0; |
| 541 | |
| 542 | rrlp_pdu = _rrlp_create_gps_assist_pdu(1, &rrlp_gps_ad); |
| 543 | if (!rrlp_pdu) { |
| 544 | rv = -ENOMEM; |
| 545 | goto error; |
| 546 | } |
| 547 | |
| 548 | if (re & RRLP_AR_IONO_MODEL) |
| 549 | if (!_rrlp_add_ionospheric_model(rrlp_gps_ad, gps_ad)) |
| 550 | pdu_has_data = 1; |
| 551 | |
| 552 | if (re & RRLP_AR_UTC_MODEL) |
| 553 | if (!_rrlp_add_utc_model(rrlp_gps_ad, gps_ad)) |
| 554 | pdu_has_data = 1; |
| 555 | |
| 556 | if (re & RRLP_AR_REF_TIME) |
| 557 | if (!_rrlp_add_reference_time(rrlp_gps_ad, gps_ad)) |
| 558 | pdu_has_data = 1; |
| 559 | |
| 560 | if (re & RRLP_AR_REF_LOC) |
| 561 | if (!_rrlp_add_reference_location(rrlp_gps_ad, gps_ad)) |
| 562 | pdu_has_data = 1; |
| 563 | |
| 564 | if (pdu_has_data) { |
| 565 | lst_pdu[lst_cnt++] = rrlp_pdu; |
| 566 | rrlp_pdu = NULL; |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | /* Almanac */ |
| 571 | if (re & RRLP_AR_ALMANAC) { |
| 572 | i = 0; |
| 573 | do { |
| 574 | if (!(gps_ad->fields & GPS_FIELD_ALMANAC)) |
| 575 | break; |
| 576 | |
| 577 | if (!rrlp_pdu) { |
| 578 | rrlp_pdu = _rrlp_create_gps_assist_pdu(1, &rrlp_gps_ad); |
| 579 | if (!rrlp_pdu) { |
| 580 | rv = -ENOMEM; |
| 581 | goto error; |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | rv = _rrlp_add_almanac(rrlp_gps_ad, gps_ad, &i, 10); |
| 586 | if (rv < 0) |
| 587 | goto error; |
| 588 | |
| 589 | lst_pdu[lst_cnt++] = rrlp_pdu; |
| 590 | rrlp_pdu = NULL; |
| 591 | } while (rv); |
| 592 | } |
| 593 | |
| 594 | /* Ephemeris */ |
| 595 | if (re & RRLP_AR_EPHEMERIS) { |
| 596 | i = 0; |
| 597 | do { |
| 598 | if (!(gps_ad->fields & GPS_FIELD_EPHEMERIS)) |
| 599 | break; |
| 600 | |
| 601 | if (!rrlp_pdu) { |
| 602 | rrlp_pdu = _rrlp_create_gps_assist_pdu(1, &rrlp_gps_ad); |
| 603 | if (!rrlp_pdu) { |
| 604 | rv = -ENOMEM; |
| 605 | goto error; |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | rv = _rrlp_add_ephemeris(rrlp_gps_ad, gps_ad, &i, 2, req->eph_svs); |
| 610 | |
| 611 | lst_pdu[lst_cnt++] = rrlp_pdu; |
| 612 | rrlp_pdu = NULL; |
| 613 | |
| 614 | } while (rv); |
| 615 | } |
| 616 | |
| 617 | /* Serialize & Release all PDUs */ |
| 618 | for (i=0; i<lst_cnt && i<o_max_pdus; i++) { |
Sylvain Munaut | bcffc97 | 2009-12-28 00:04:56 +0100 | [diff] [blame] | 619 | /* Pseudo segmentation flags */ |
| 620 | MoreAssDataToBeSent_t *mad = calloc(1, sizeof(*mad)); |
| 621 | *mad = (i == (lst_cnt-1)) ? |
| 622 | MoreAssDataToBeSent_noMoreMessages : |
| 623 | MoreAssDataToBeSent_moreMessagesOnTheWay; |
| 624 | lst_pdu[i]->component.choice.assistanceData.moreAssDataToBeSent = mad; |
| 625 | |
| 626 | /* Serialization */ |
Sylvain Munaut | ce7be3a | 2009-12-22 21:53:22 +0100 | [diff] [blame] | 627 | // asn_fprint(stdout, &asn_DEF_PDU, lst_pdu[i]); |
| 628 | rv = uper_encode_to_new_buffer(&asn_DEF_PDU, NULL, lst_pdu[i], &o_pdu[i]); |
| 629 | if (rv < 0) |
| 630 | goto error; |
| 631 | o_len[i] = rv; |
| 632 | } |
| 633 | |
Sylvain Munaut | 9bef184 | 2009-12-28 00:00:45 +0100 | [diff] [blame] | 634 | rv = lst_cnt; |
Sylvain Munaut | ce7be3a | 2009-12-22 21:53:22 +0100 | [diff] [blame] | 635 | |
| 636 | /* Release ASN.1 objects */ |
| 637 | error: |
| 638 | if (rrlp_pdu) |
| 639 | asn_DEF_PDU.free_struct(&asn_DEF_PDU, (void*)rrlp_pdu, 0); |
| 640 | |
| 641 | for (i=0; i<lst_cnt; i++) |
| 642 | asn_DEF_PDU.free_struct(&asn_DEF_PDU, lst_pdu[i], 0); |
| 643 | |
| 644 | return rv; |
| 645 | } |
| 646 | |
| 647 | /* }}} */ |
| 648 | |