import Dieter's rrlpd based on Sylvain's code from August 10, 2011

master
Harald Welte 11 years ago
parent a5c8d80688
commit 47ab210af9
  1. 46
      rrlpd/README
  2. 27
      rrlpd/patches_OpenBSC/diff_gsm_04_08
  3. 289
      rrlpd/patches_OpenBSC/diff_rrlp
  4. 17
      rrlpd/patches_asn1c/01_fix_per_encoding_dieter.diff
  5. 41
      rrlpd/src/Makefile
  6. 1055
      rrlpd/src/asn1/rrlp.asn
  7. 131
      rrlpd/src/gps.c
  8. 193
      rrlpd/src/gps.h
  9. 1298
      rrlpd/src/main.c
  10. 864
      rrlpd/src/rrlp.c
  11. 65
      rrlpd/src/rrlp.h
  12. 246
      rrlpd/src/ubx-parse.c
  13. 45
      rrlpd/src/ubx-parse.h
  14. 96
      rrlpd/src/ubx.c
  15. 240
      rrlpd/src/ubx.h

@ -0,0 +1,46 @@
RRLP Server
------------
- Adjust ASN1C paths in the makefile (maybe adjust the makefile too)
- ** VERY IMPORTANT **: apply ASN1C patch, otherwise invalid PDUs will
be generated
- Patches for OpenBSC are "TODO"
- Adjust IP Address of RRLP Server in OpenBSC rrlp.c (TODO: ajust code to
get this setting from config file)
- Requires a u-Blox GPS receiver. The receiver is supposed to be connected
over its USB port. Some changes are problably required if the reciver is
connected through its UART port (see "#define GPS_USB ..." in main.c)
- To work properly, the GPS receiver should already have a GPS fix
- Enable RRLP in OpenBSC config file ("rrlp mode ms-based")
- How to run: (Parameter: interface IP Address where to listen,
GPS receiver port), e.g.:
./rrlp-serv 192.168.1.1 /dev/ttyS0
Issues:
- "Work in Progress": code not yet properly organized and cleaned up
- very verbose output for debugging/testing
- rrlp.c: find out if data channel is slow (SDCCH) so that long assistance
data will not be sent
- send an RRLP request not just when paging a phone
- paging: sometimes no RRLP response is reveicved !?
- Location update: response of the phone got lost, channel is closed too
early !?
- GPS reference time: do we need an offset so that the time is correct when
the phone receives it ?

@ -0,0 +1,27 @@
--- libmsc/gsm_04_08.c Mon Jul 18 11:19:21 2011
+++ r:libmsc/gsm_04_08.c Tue Aug 09 14:34:30 2011
@@ -1136,16 +1135,24 @@
static int gsm48_rx_rr_app_info(struct gsm_subscriber_connection *conn, struct msgb *msg)
{
struct gsm48_hdr *gh = msgb_l3(msg);
u_int8_t apdu_id_flags;
u_int8_t apdu_len;
u_int8_t *apdu_data;
apdu_id_flags = gh->data[0];
apdu_len = gh->data[1];
apdu_data = gh->data+2;
DEBUGP(DNM, "RX APPLICATION INFO id/flags=0x%02x apdu_len=%u apdu=%s",
apdu_id_flags, apdu_len, osmo_hexdump(apdu_data, apdu_len));
+
+#if 1 /* RRLP Server */
+ if(apdu_id_flags == 0x00) { /* RRLP */
+ extern int handle_rrlp(struct gsm_subscriber_connection *conn, uint8_t *data, int len);
+
+ handle_rrlp(conn, apdu_data, apdu_len);
+ }
+#endif
return db_apdu_blob_store(conn->subscr, apdu_id_flags, apdu_len, apdu_data);
}

@ -0,0 +1,289 @@
--- libmsc/rrlp.c Mon Jul 18 11:19:21 2011
+++ r:libmsc/rrlp.c Wed Aug 10 07:34:26 2011
@@ -20,37 +20,317 @@
*/
#include <sys/types.h>
#include <openbsc/gsm_04_08.h>
#include <openbsc/signal.h>
#include <openbsc/gsm_subscriber.h>
#include <openbsc/chan_alloc.h>
+/* ----------------------------------------------- */
+
+/* TODO: move in a separate file ? */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+
+#define RRLP_SERV_PORT 7890
+#define RRLP_SERV_IP "192.168.5.250" /* TODO: from config file */
+
+#define MAX_RRLP_DATA 256
+
+/* Server cmds */
+
+#define RRLP_CMD_MS_DATA 1 /* data from MS */
+#define RRLP_CMD_MS_DATA_SLOW 2 /* data from MS, slow channel */
+
+/* Server response */
+
+#define RRLP_RSP_ASSIST_DATA 1 /* assitance data, send to MS */
+#define RRLP_RSP_RRLP_ERROR 2 /* RRLP error */
+#define RRLP_RSP_RRLP_POSITION 3 /* RRLP position */
+#define RRLP_RSP_ERROR 4 /* something went wrong */
+
+/* TODO: adjust error messages, use logging */
+
+static int rrlp_serv_cmd(struct gsm_subscriber_connection *conn,
+ uint8_t cmd, uint8_t *data, int len_data,
+ uint8_t *cmd_reply, uint8_t *reply, int *len_reply)
+{
+ static int fd = -1;
+ static struct sockaddr_in sa;
+ int len;
+ uint8_t buf[2 + 1 + 8 + MAX_RRLP_DATA]; /* len, cmd, subscriber ID, data */
+ int len_pkt, offs;
+ int rc;
+ long long unsigned int id;
+ struct sockaddr_in from;
+ int from_len;
+ fd_set readset;
+ struct timeval tv;
+
+ if(len_data > MAX_RRLP_DATA) {
+ fprintf(stderr, "len_data > MAX_RRLP_DATA: %d\n", len_data);
+ return -1;
+ }
+ if(fd == -1) {
+ fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
+ if(fd < 0) {
+ fprintf(stderr, "socket() failed: (%d) %s\n", fd, strerror(errno));
+ return -1;
+ }
+
+ sa.sin_family = AF_INET;
+ sa.sin_port = htons(RRLP_SERV_PORT);
+ if(inet_aton(RRLP_SERV_IP, &sa.sin_addr) != 1) {
+ fprintf(stderr, "inet_aton() failed: %s\n", strerror(errno));
+ close(fd);
+ fd = -1;
+ return -1;
+ }
+
+ rc = connect(fd, (struct sockaddr *)&sa, sizeof(sa));
+ if(rc < 0) {
+ fprintf(stderr, "connect() failed: (%d) %s\n", rc, strerror(errno));
+ close(fd);
+ fd = -1;
+ return -1;
+ }
+ }
+
+ /* we are now connected */
+
+ id = conn->subscr->id;
+
+ /* build cmd packet */
+
+ len_pkt = 2 + 1 + 8 + len_data;
+ buf[0] = len_pkt & 0xFF;
+ buf[1] = (len_pkt >> 8) & 0xFF;
+
+ buf[2] = cmd;
+
+ buf[3] = id & 0xFF;
+ buf[4] = (id >> 8) & 0xFF;
+ buf[5] = (id >> 16) & 0xFF;
+ buf[6] = (id >> 24) & 0xFF;
+ buf[7] = (id >> 32) & 0xFF;
+ buf[8] = (id >> 40) & 0xFF;
+ buf[9] = (id >> 48) & 0xFF;
+ buf[10] = (id >> 56) & 0xFF;
+ /* data */
+ memcpy(&buf[11], data, len_data);
+
+ /* send cmd */
+
+ len = sendto(fd, buf, len_pkt, 0, (struct sockaddr*)&sa, sizeof(sa));
+ if(len < 0) {
+ fprintf(stderr, "sendto() failed: (%d) %s\n", len, strerror(errno));
+ close(fd);
+ fd = -1;
+ return -1;
+ }
+ if(len != len_pkt) {
+ fprintf(stderr, "sendto: len != len_pkt: %d %d\n", len, len_pkt);
+ close(fd);
+ fd = -1;
+ return -1;
+ }
+
+ /* wait at most 500 ms for a reply */
+
+ FD_ZERO(&readset);
+ FD_SET(fd, &readset);
+ tv.tv_sec = 0;
+ tv.tv_usec = 500 * 1000;
+
+ /* this creates another UDP socket on Cygwin !? */
+ rc = select(fd + 1, &readset, NULL, NULL, &tv);
+ if(rc < 0) {
+ fprintf(stderr, "select() failed: (%d) %s\n", rc, strerror(errno));
+ close(fd);
+ fd = -1;
+ return -1;
+ }
+
+ if(!FD_ISSET(fd, &readset)) {
+ fprintf(stderr, "timeout select()\n");
+ close(fd);
+ fd = -1;
+ return -1;
+ }
+
+ /* read packet */
+ from_len = sizeof(from);
+ len = recvfrom(fd, buf, sizeof(buf), 0, (struct sockaddr*)&from, &from_len);
+ if(len < 0) {
+ fprintf(stderr, "recvfrom() failed: (%d) %s\n", len, strerror(errno));
+ close(fd);
+ fd = -1;
+ return -1;
+ }
+ if(len < 2) {
+ fprintf(stderr, "len < 2: %d\n", len);
+ close(fd);
+ fd = -1;
+ return -1;
+ }
+ len_pkt = buf[0] + (buf[1] << 8);
+ if(len_pkt < 2 + 1) {
+ fprintf(stderr, "len_pkt < 2 + 1: %d\n", len_pkt);
+ close(fd);
+ fd = -1;
+ return -1;
+ }
+ if(len != len_pkt) {
+ fprintf(stderr, "recvfrom: len != len_pkt: %d %d\n", len, len_pkt);
+ close(fd);
+ fd = -1;
+ return -1;
+ }
+ len_pkt -= 2;
+ offs = 2;
+
+#if 0 /* dump packet */
+ {
+ int i;
+ for(i = 0; i < len_pkt; i++)
+ printf("%02X ", buf[offs + i]);
+ printf("\n");
+ }
+#endif
+
+ /* process packet */
+
+ *len_reply = len_pkt - 1;
+ *cmd_reply = buf[offs];
+ memcpy(reply, &buf[offs + 1], *len_reply);
+
+ return 0;
+}
+
+/* ----------------------------------------------- */
+
+static int send_rrlp_req(struct gsm_subscriber_connection *conn);
+
+/* TODO: adjust error messages, use logging */
+
+int handle_rrlp(struct gsm_subscriber_connection *conn, uint8_t *data, int len)
+{
+ struct gsm_network *net = conn->bts->network;
+ int rc;
+ uint8_t cmd_reply;
+ uint8_t reply[MAX_RRLP_DATA];
+ int len_reply;
+ uint8_t cmd;
+
+ if (net->rrlp.mode == RRLP_MODE_NONE)
+ return 0;
+
+ if(len > MAX_RRLP_DATA) {
+ fprintf(stderr, "too many data for handle_rrlp (%d)\n", len);
+ return -1;
+ }
+
+ /* TODO: decide if channel is slow (SDCCH), for slow channels
+ only short assistance data should be sent */
+
+ if(1)
+ cmd = RRLP_CMD_MS_DATA;
+ else
+ cmd = RRLP_CMD_MS_DATA_SLOW;
+
+ rc = rrlp_serv_cmd(conn, cmd, data, len, &cmd_reply, reply, &len_reply);
+ if(rc != 0) {
+ fprintf(stderr, "rrlp_serv_cmd failed (%d)\n", rc);
+ return rc;
+ }
+
+ if(cmd_reply == RRLP_RSP_ERROR) {
+ printf("RRLP Server error (general): %s\n", reply);
+ return 0;
+ }
+
+ if(cmd_reply == RRLP_RSP_RRLP_ERROR) {
+ printf("RRLP Server error (RRLP): %s\n", reply);
+ return 0;
+ }
+
+ if(cmd_reply == RRLP_RSP_RRLP_POSITION) {
+ long latitude;
+ long longitude;
+ long altitude;
+
+ if(len_reply != 12) {
+ fprintf(stderr, "invalid RRLP position length (%d)\n", len_reply);
+ return -1;
+ }
+
+ latitude = reply[0] + (reply[1] << 8) + (reply[2] << 16) + (reply[3] << 24);
+ longitude = reply[4] + (reply[5] << 8) + (reply[6] << 16) + (reply[7] << 24);
+ altitude = reply[8] + (reply[9] << 8) + (reply[10] << 16) + (reply[11] << 24);
+
+ /* TODO: do something useful with the position */
+
+ printf("RRLP Server position: ");
+ printf("latitude = %f ", ((double)latitude * 90.0) / 0x800000L);
+ printf("longitude = %f ", ((double)longitude * 360.0) / 0x1000000L);
+ printf("altitude = %ld\n", altitude);
+
+ return 0;
+ }
+
+ if(cmd_reply == RRLP_RSP_ASSIST_DATA) {
+ printf("Assistance data, len %d\n", len_reply);
+
+ /*
+ If there are assistance data, send them. If there are no more,
+ repeat the measurement request
+ */
+
+ if(len_reply)
+ return gsm48_send_rr_app_info(conn, 0x00, len_reply, reply);
+ else
+ send_rrlp_req(conn);
+ }
+
+ return 0;
+}
+

@ -0,0 +1,17 @@
Index: skeletons/per_support.c
===================================================================
--- skeletons/per_support.c (revision 1407)
+++ skeletons/per_support.c (working copy)
@@ -336,7 +336,12 @@
buf[3] = bits;
else {
ASN_DEBUG("->[PER out split %d]", obits);
+#if 1 // Dieter
+ po->nboff -= obits; // undo incrementation from a few lines above
+ per_put_few_bits(po, bits >> (obits - 24), 24); // shift according to the rest of the bits
+#else
per_put_few_bits(po, bits >> 8, 24);
+#endif
per_put_few_bits(po, bits, obits - 24);
ASN_DEBUG("<-[PER out split %d]", obits);
}

@ -0,0 +1,41 @@
# !! adjust as needed !!
ASN1C=/usr/local/bin/asn1c
ASN1_INCLUDE=/usr/src/asn1c/skeletons
CC=gcc
# -DEMIT_ASN_DEBUG=1 ??
CFLAGS=-I$(ASN1_INCLUDE) -I./asn1_gen -O3 -Wall
ASN1_FILES=$(wildcard asn1/*.asn)
all: rrlp-serv
rrlp-serv: librrlp-asn1.a main.o rrlp.o ubx.o ubx-parse.o gps.o
$(CC) $(CFLAGS) -o $@ main.o rrlp.o ubx.o ubx-parse.o gps.o -L. -lrrlp-asn1
#
# ASN1 file autogeneration (need recursive makefile call)
#
ASN1_SOURCES = $(wildcard asn1_gen/*.c)
ASN1_OBJECTS = $(ASN1_SOURCES:.c=.o)
# -fnative-types ??
librrlp-asn1.a: $(ASN1_FILES)
mkdir -p asn1_gen && \
cd asn1_gen && \
$(ASN1C) -fskeletons-copy -gen-PER $(addprefix ../,$^) && \
rm converter-sample.c Makefile.am.sample && \
$(ASN1C) -gen-PER $(addprefix ../,$^)
@$(MAKE) librrlp-asn1.a.submake
librrlp-asn1.a.submake: $(ASN1_OBJECTS)
$(AR) rcs librrlp-asn1.a $^
.PHONY: librrlp-asn1.a.submake
clean:
rm -Rf asn1_gen
rm -f *.o rrlp-serv rrlp-serv.exe test-clnt test-clnt.exe

File diff suppressed because it is too large Load Diff

@ -0,0 +1,131 @@
/*
* gps.c
*
* A few utility functions to deal with low level GPS data
*
*
* Copyright (C) 2009 Sylvain Munaut <tnt@246tNt.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include "gps.h"
#define GET_FIELD_U(w, nb, pos) (((w) >> (pos)) & ((1<<(nb))-1))
#define GET_FIELD_S(w, nb, pos) (((int)((w) << (32-(nb)-(pos)))) >> (32-(nb)))
/*
* Unpacks GPS Subframe 1,2,3 payloads (3 * 8 words)
*
* Note: eph->sv_id is not filled here since not present in those subframes
*
* (no parity bit checking is done, only the lower 24 bits of each word
* are used)
*/
int
gps_unpack_sf123(uint32_t *sf, struct gps_ephemeris_sv *eph)
{
uint32_t *sf1 = &sf[0];
uint32_t *sf2 = &sf[8];
uint32_t *sf3 = &sf[16];
int iode1, iode2;
eph->week_no = GET_FIELD_U(sf1[0], 10, 14);
eph->code_on_l2 = GET_FIELD_U(sf1[0], 2, 12);
eph->sv_ura = GET_FIELD_U(sf1[0], 4, 8);
eph->sv_health = GET_FIELD_U(sf1[0], 6, 2);
eph->l2_p_flag = GET_FIELD_U(sf1[1], 1, 23);
eph->t_gd = GET_FIELD_S(sf1[4], 8, 0);
eph->iodc = (GET_FIELD_U(sf1[0], 2, 0) << 8) | \
GET_FIELD_U(sf1[5], 8, 16);
eph->t_oc = GET_FIELD_U(sf1[5], 16, 0);
eph->a_f2 = GET_FIELD_S(sf1[6], 8, 16);
eph->a_f1 = GET_FIELD_S(sf1[6], 16, 0);
eph->a_f0 = GET_FIELD_S(sf1[7], 22, 2);
iode1 = GET_FIELD_U(sf2[0], 8, 16);
eph->c_rs = GET_FIELD_S(sf2[0], 16, 0);
eph->delta_n = GET_FIELD_S(sf2[1], 16, 8);
eph->m_0 = (GET_FIELD_S(sf2[1], 8, 0) << 24) | \
GET_FIELD_U(sf2[2], 24, 0);
eph->c_uc = GET_FIELD_S(sf2[3], 16, 8);
eph->e = (GET_FIELD_U(sf2[3], 8, 0) << 24) | \
GET_FIELD_U(sf2[4], 24, 0);
eph->c_us = GET_FIELD_S(sf2[5], 16, 8);
eph->a_powhalf = (GET_FIELD_U(sf2[5], 8, 0) << 24) | \
GET_FIELD_U(sf2[6], 24, 0);
eph->t_oe = GET_FIELD_U(sf2[7], 16, 8);
eph->fit_flag = GET_FIELD_U(sf2[7], 1, 7);
eph->c_ic = GET_FIELD_S(sf3[0], 16, 8);
eph->omega_0 = (GET_FIELD_S(sf3[0], 8, 0) << 24) | \
GET_FIELD_U(sf3[1], 24, 0);
eph->c_is = GET_FIELD_S(sf3[2], 16, 8);
eph->i_0 = (GET_FIELD_S(sf3[2], 8, 0) << 24) | \
GET_FIELD_U(sf3[3], 24, 0);
eph->c_rc = GET_FIELD_S(sf3[4], 16, 8);
eph->w = (GET_FIELD_S(sf3[4], 8, 0) << 24) | \
GET_FIELD_U(sf3[5], 24, 0);
eph->omega_dot = GET_FIELD_S(sf3[6], 24, 0);
iode2 = GET_FIELD_U(sf3[7], 8, 16);
eph->idot = GET_FIELD_S(sf3[7], 14, 2);
eph->_rsvd1 = GET_FIELD_U(sf1[1], 23, 0);
eph->_rsvd2 = GET_FIELD_U(sf1[2], 24, 0);
eph->_rsvd3 = GET_FIELD_U(sf1[3], 24, 0);
eph->_rsvd4 = GET_FIELD_U(sf1[4], 16, 8);
eph->aodo = GET_FIELD_U(sf2[7], 5, 2);
/* Check & cross-validate iodc[7:0], iode1, iode2 */
if ((iode1 != iode2) || (iode1 != (eph->iodc & 0xff))) {
fprintf(stderr, "gps_unpack_sf123 failed\n");
return -1;
}
return 0;
}
/*
* Unpacks GPS Subframe 4 or 5 Almanac pages payload (8 words)
*
* (no parity bit checking is done, only the lower 24 bits of each word
* are used)
*/
int
gps_unpack_sf45_almanac(uint32_t *sf, struct gps_almanac_sv *alm)
{
/* this is the page ID but not the satellite ID, its wrong in subframe 5 */
alm->sv_id = GET_FIELD_U(sf[0], 6, 16);
alm->e = GET_FIELD_U(sf[0], 16, 0);
alm->t_oa = GET_FIELD_U(sf[1], 8, 16);
alm->ksii = GET_FIELD_S(sf[1], 16, 0);
alm->omega_dot = GET_FIELD_S(sf[2], 16, 8);
alm->sv_health = GET_FIELD_U(sf[2], 8, 0);
alm->a_powhalf = GET_FIELD_U(sf[3], 24, 0);
alm->omega_0 = GET_FIELD_S(sf[4], 24, 0);
alm->w = GET_FIELD_S(sf[5], 24, 0);
alm->m_0 = GET_FIELD_S(sf[6], 24, 0);
alm->a_f0 = (GET_FIELD_S(sf[7], 8, 16) << 3) | \
GET_FIELD_U(sf[7], 3, 2);
alm->a_f1 = GET_FIELD_S(sf[7], 11, 5);
return 0;
}

@ -0,0 +1,193 @@
/*
* gps.h
*
* Header to deal with low level GPS data
*
*
* Copyright (C) 2009 Sylvain Munaut <tnt@246tNt.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __GPS_H__
#define __GPS_H__
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
#include <time.h>
#define MAX_SV 64
/* Ionosperic model data */
struct gps_ionosphere_model {
/* #bits Scale factor Effective Units */
/* (LSB) range */
int alpha_0; /* s 8 2^-30 seconds */
int alpha_1; /* s 8 2^-27 s / semi-circles */
int alpha_2; /* s 8 2^-24 s / (semi-circles)^2 */
int alpha_3; /* s 8 2^-24 s / (semi-circles)^3 */
int beta_0; /* s 8 2^11 seconds */
int beta_1; /* s 8 2^14 s / semi-circles */
int beta_2; /* s 8 2^16 s / (semi-circles)^2 */
int beta_3; /* s 8 2^16 s / (semi-circles)^3 */
};
/* UTC model data */
struct gps_utc_model {
/* #bits Scale factor Effective Units */
/* (LSB) range */
int a0; /* s 32 2^-30 seconds */
int a1; /* s 24 2^-50 seconds / seconds */
int delta_t_ls; /* s 8 1 seconds */
int t_ot; /* u 8 2^12 602,112 seconds */
int wn_t; /* u 8 1 weeks */
int wn_lsf; /* u 8 1 weeks */
int dn; /* u 8 1 7 days */
int delta_t_lsf;/* s 8 1 seconds */
};
/* Almanach data */
struct gps_almanac_sv {
int sv_id;
int sv_health;
/* #bits Scale factor Effective Units */
/* (LSB) range */
int e; /* u 16 2^-21 */
int t_oa; /* u 8 2^12 602,112 seconds */
int ksii; /* s 16 2^-19 semi-circles */
int omega_dot; /* s 16 2^-38 semi-circles / s */
int a_powhalf; /* u 24 2^-11 meters */
int omega_0; /* s 24 2^-23 semi-circles */
int w; /* s 24 2^-23 semi-circles */
int m_0; /* s 24 2^-23 semi-circles */
int a_f0; /* s 11 2^-20 seconds */
int a_f1; /* s 11 2^-38 seconds / seconds */
};
struct gps_almanac {
int wna;
int n_sv;
struct gps_almanac_sv svs[MAX_SV];
};
/* Ephemeris data */
struct gps_ephemeris_sv {
int sv_id;
/* #bits Scale factor Effective Units */
/* (LSB) range */
int code_on_l2; /* u 2 1 / */
int week_no; /* u 10 1 week */
int l2_p_flag; /* u 1 1 / */
int sv_ura; /* u 4 / / */
int sv_health; /* u 6 / / */
int t_gd; /* s 8 2^-31 seconds */
int iodc; /* u 10 / / */
int t_oc; /* u 16 2^4 604,784 seconds */
int a_f2; /* s 8 2^-55 sec / sec^2 */
int a_f1; /* s 16 2^-43 sec / sec */
int a_f0; /* s 22 2^-31 seconds */
int c_rs; /* s 16 2^-5 meters */
int delta_n; /* s 16 2^-43 semi-circles / s */
int m_0; /* s 32 2^-31 semi-circles */
int c_uc; /* s 16 2^-29 radians */
unsigned int e; /* u 32 2^-33 0.03 / */
int c_us; /* s 16 2^-29 radians */
unsigned int a_powhalf; /* u 32 2^-19 meters^(1/2) */
int t_oe; /* u 16 2^4 604,784 seconds */
int fit_flag; /* u 1 / / */
int c_ic; /* s 16 2^-29 radians */
int omega_0; /* s 32 2^-31 semi-circles */
int c_is; /* s 16 2^-29 radians */
int i_0; /* s 32 2^-31 semi-circles */
int c_rc; /* s 16 2^-5 meters */
int w; /* s 32 2^-31 semi-circles */
int omega_dot; /* s 24 2^-43 semi-circles / s */
int idot; /* s 14 2^-43 semi-circles / s */
int _rsvd1; /* 23 bits */
int _rsvd2; /* 24 bits */
int _rsvd3; /* 24 bits */
int _rsvd4; /* 16 bits */
int aodo; /* 8 bits Not sure it needs to be here ... */
};
struct gps_ephemeris {
int n_sv;
struct gps_ephemeris_sv svs[MAX_SV];
};
/* Reference position */
struct gps_ref_pos { /* WSG84 ellipsoid */
double latitude; /* deg */
double longitude; /* deg */
double altitude; /* m above ellipsoid */
};
/* Reference time */
struct gps_ref_time {
int wn; /* GPS week number */
double tow; /* in seconds */
time_t when; /* time in seconds when time was set */
};
/* All assist data */
#define GPS_FIELD_IONOSPHERE (1<<0)
#define GPS_FIELD_UTC (1<<1)
#define GPS_FIELD_ALMANAC (1<<2)
#define GPS_FIELD_EPHEMERIS (1<<3)
#define GPS_FIELD_REFPOS (1<<4)
#define GPS_FIELD_REFTIME (1<<5)
#define GPS_FIELD_REALTIME_INT (1<<6)
struct gps_assist_data {
int fields;
struct gps_ionosphere_model ionosphere;
struct gps_utc_model utc;
struct gps_almanac almanac;
struct gps_ephemeris ephemeris;
struct gps_ref_pos ref_pos;
struct gps_ref_time ref_time;
};
/* GPS Subframe utility methods (see gps.c for details) */
int gps_unpack_sf123(uint32_t *sf, struct gps_ephemeris_sv *eph);
int gps_unpack_sf45_almanac(uint32_t *sf, struct gps_almanac_sv *alm);
#ifdef __cplusplus
}
#endif
#endif /* __GPS_H__ */

File diff suppressed because it is too large Load Diff

@ -0,0 +1,864 @@
/*
* rrlp.c
*
* RRLP implementation
*
*
* Copyright (C) 2009 Sylvain Munaut <tnt@246tNt.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <errno.h>
#include <math.h>
#include "gps.h"
#include "rrlp.h"
#include <PDU.h>
#include <GPS-AssistData.h>
#include <NavigationModel.h>
#include <IonosphericModel.h>
#include <UTCModel.h>
#include <Almanac.h>
#include <RefLocation.h>
#include <ReferenceTime.h>
/* ------------------------------------------------------------------------ */
/* RRLP Assistance request decoding */
/* ---------------------------------------------------------------------{{{ */
/* Decode and validate the assistance data request messages.
* See section 10.10 of
* . ETSI TS 149 031 V8.1.0 (2009-01)
* . 3GPP TS 49.031 version 8.1.0 Release 8
*/
/* Packed structure from 49.031 spec (RGA = Request GPS Assistance) */
#define RRLP_RGA0_ALMANAC (1<<0)
#define RRLP_RGA0_UTC_MODEL (1<<1)
#define RRLP_RGA0_IONO_MODEL (1<<2)
#define RRLP_RGA0_NAV_MODEL (1<<3)
#define RRLP_RGA0_DGPS (1<<4)
#define RRLP_RGA0_REF_LOC (1<<5)
#define RRLP_RGA0_REF_TIME (1<<6)
#define RRLP_RGA0_ACQ_ASSIST (1<<7)
#define RRLP_RGA1_REALTIME_INT (1<<0)
#define RRLP_RGA1_EPH_EXT (1<<1)
#define RRLP_RGA1_EPH_EXT_CHECK (1<<2)
struct rrlp_rga_hdr {
uint8_t items0;
uint8_t items1;
} __attribute__((packed));
struct rrlp_rga_eph_sv {
uint8_t sv_id; /* [7:6] reserved, [5:0] sv_id */
uint8_t iode; /* latest eph in the MS memory in hours */
} __attribute__((packed));
struct rrlp_rga_eph {
uint8_t wn_hi; /* [7:6] = wn[9:8] */
uint8_t wn_lo; /* wn[7:0] */
uint8_t toe; /* latest eph in the MS memory in hours */
uint8_t nsat_tmtoe; /* [7:4] nstat, [3:0] T-Toe limit */
struct rrlp_rga_eph_sv svs[0];
} __attribute__((packed));
struct rrlp_rga_eph_ext {
uint8_t validity; /* in 4 hours units */
} __attribute__((packed));
struct rrlp_rga_eph_ext_check {
/* weeks are in gps week modulo 4 */
uint8_t wn_begin_end; /* [7:4] begin, [3:0] end */
uint8_t tow_begin;
uint8_t tow_end;
} __attribute__((packed));
/* Parsing function */
int
rrlp_decode_assistance_request(
struct rrlp_assist_req *ar,
void *req, int req_len)
{
struct rrlp_rga_hdr *hdr = NULL;
struct rrlp_rga_eph *eph = NULL;
struct rrlp_rga_eph_ext *eph_ext = NULL;
struct rrlp_rga_eph_ext_check *eph_ext_check = NULL;
int p = 0;
int rc = 0;
/* Reset */
ar->req_elems = 0;
ar->eph_svs = 0;
/* Parse message */
hdr = req;
p += sizeof(struct rrlp_rga_hdr);
if (p > req_len)
return -1;
if (hdr->items0 & RRLP_RGA0_NAV_MODEL) {
printf("NAV_MODEL\n");
eph = req + p;
p += sizeof(struct rrlp_rga_eph);
if (p > req_len)
return -1;
p += (eph->nsat_tmtoe >> 4) * sizeof(struct rrlp_rga_eph_sv);
if (p > req_len)
return -1;
printf(" GPS week = %d\n", (eph->wn_hi << 8) + eph->wn_lo);
printf(" TOE = %d\n", eph->toe);
printf(" T-TOE limit = %d\n", eph->nsat_tmtoe & 0x0F);
int i;
for(i = 0; i < (eph->nsat_tmtoe >> 4); i++) {
printf(" %2d: sv_id = %2d (%d) IODE = %3d\n", i, eph->svs[i].sv_id & 0x3F, eph->svs[i].sv_id >> 6, eph->svs[i].iode);
if(eph->svs[i].sv_id >> 6) {
/* most certainly invalid data or have to be interpreted differently */
rc = -3;
}
}
}
if (hdr->items1 & RRLP_RGA1_EPH_EXT) {
printf("EPH_EXT\n");
eph_ext = req + p;
p += sizeof(struct rrlp_rga_eph_ext);
if (p > req_len)
return -1;
}
if (hdr->items1 & RRLP_RGA1_EPH_EXT_CHECK) {
printf("EPH_EXT_CHECK\n");
eph_ext_check = req + p;
p += sizeof(struct rrlp_rga_eph_ext_check);
if (p > req_len)
return -1;
}
if (p != req_len && (p != 2 || req_len != 6)) { /* P==2 && req_len == 6 might happen */
fprintf(stderr, "p != req_len (%d %d)\n", p, req_len);
return -2; /* not all bytes consumed ??? */
}
/* Print a warning for unsupported requests */
if ((eph_ext != NULL) ||
(eph_ext_check != NULL) ||
(hdr->items0 & (RRLP_RGA0_DGPS | RRLP_RGA0_ACQ_ASSIST)) ||
#if 0
(hdr->items1 & RRLP_RGA1_REALTIME_INT)) {
#else
0) {
#endif
fprintf(stderr, "[w] Unsupported assistance data requested, ignored ...\n");
if(hdr->items0 & RRLP_RGA0_DGPS)
printf("Unsupported assistance data requested: RRLP_RGA0_DGPS\n");
if(hdr->items0 & RRLP_RGA0_ACQ_ASSIST)
printf("Unsupported assistance data requested: RRLP_RGA0_ACQ_ASSIST\n");
if(hdr->items1 & RRLP_RGA1_REALTIME_INT)
printf("Unsupported assistance data requested: RRLP_RGA1_REALTIME_INT\n");
}
/* Copy the request */
if (hdr->items0 & RRLP_RGA0_ALMANAC) {
printf("ALMANAC\n");
ar->req_elems |= RRLP_AR_ALMANAC;
}
if (hdr->items0 & RRLP_RGA0_UTC_MODEL) {
printf("UTC_MODEL\n");
ar->req_elems |= RRLP_AR_UTC_MODEL;
}
if (hdr->items0 & RRLP_RGA0_IONO_MODEL) {
printf("IONO_MODEL\n");
ar->req_elems |= RRLP_AR_IONO_MODEL;
}
if (hdr->items0 & RRLP_RGA0_REF_LOC) {
printf("REF_LOC\n");
ar->req_elems |= RRLP_AR_REF_LOC;
}
if (hdr->items0 & RRLP_RGA0_REF_TIME) {
printf("REF_TIME\n");
ar->req_elems |= RRLP_AR_REF_TIME;
}
if (hdr->items1 & RRLP_RGA1_REALTIME_INT) {
printf("REALTIME_INTEGRITY\n");
ar->req_elems |= RRLP_AR_REALTIME_INT;
}
if (hdr->items0 & RRLP_RGA0_NAV_MODEL) {
printf("NAV_MODEL\n");
int i, n_svs = eph->nsat_tmtoe >> 4;
ar->req_elems |= RRLP_AR_EPHEMERIS;
if(n_svs == 0) {
ar->eph_svs = 0xFFFFFFFFFFFFFFFFULL;
}
else {
for (i=0; i<n_svs; i++)
ar->eph_svs |= (1ULL << (eph->svs[i].sv_id - 1)); /* Dieter: CHECK */
}
}
return rc;
}
/* }}} */
/* ------------------------------------------------------------------------ */
/* RRLP elements fill */
/* ---------------------------------------------------------------------{{{ */
/* Helpers */
static void
_ts_23_032_store_latitude(double lat, uint8_t *b)
{
uint32_t x;
x = (uint32_t) floor(fabs(lat/90.0) * ((double)(1<<23)));
if (x >= (1<<23))
x = (1<<23) - 1;
if (lat < 0.0)
x |= (1<<23);
b[0] = (x >> 16) & 0xff;
b[1] = (x >> 8) & 0xff;
b[2] = x & 0xff;
}
static void
_ts_23_032_store_longitude(double lon, uint8_t *b)
{
int32_t x;
x = floor((lon/360.0) * ((double)(1<<24)));
if (x >= (1<<23))
x = 0x007fffff;
else if (x < -(1<<23))
x = 0x00800000;
b[0] = (x >> 16) & 0xff;
b[1] = (x >> 8) & 0xff;
b[2] = x & 0xff;
}
static void
_ts_23_032_store_altitude(double alt, uint8_t *b)
{
int alt_i = (int)fabs(alt);
b[0] = ((alt_i >> 8) & 0x7f) | (alt<0.0 ? 0x80 : 0x00);
b[1] = alt_i & 0xff;
}
/* Fill methods */
static void
_rrlp_fill_navigation_model_element(
struct NavModelElement *rrlp_nme,
struct gps_ephemeris_sv *gps_eph_sv)
{
struct UncompressedEphemeris *rrlp_eph;
#if 1
rrlp_nme->satStatus.present = SatStatus_PR_newSatelliteAndModelUC;
rrlp_eph = &rrlp_nme->satStatus.choice.newSatelliteAndModelUC;
#else /* does this make any difference fro the MS !? */
rrlp_nme->satStatus.present = SatStatus_PR_newNaviModelUC;
rrlp_eph = &rrlp_nme->satStatus.choice.newNaviModelUC;
#endif
rrlp_nme->satelliteID = gps_eph_sv->sv_id - 1; /* Dieter: satellite ID is zero based */
rrlp_eph->ephemCodeOnL2 = gps_eph_sv->code_on_l2;
rrlp_eph->ephemURA = gps_eph_sv->sv_ura;
rrlp_eph->ephemSVhealth = gps_eph_sv->sv_health;
rrlp_eph->ephemIODC = gps_eph_sv->iodc;
rrlp_eph->ephemL2Pflag = gps_eph_sv->l2_p_flag;
rrlp_eph->ephemTgd = gps_eph_sv->t_gd;
rrlp_eph->ephemToc = gps_eph_sv->t_oc;
rrlp_eph->ephemAF2 = gps_eph_sv->a_f2;
rrlp_eph->ephemAF1 = gps_eph_sv->a_f1;
rrlp_eph->ephemAF0 = gps_eph_sv->a_f0;
rrlp_eph->ephemCrs = gps_eph_sv->c_rs;
rrlp_eph->ephemDeltaN = gps_eph_sv->delta_n;
rrlp_eph->ephemM0 = gps_eph_sv->m_0;
rrlp_eph->ephemCuc = gps_eph_sv->c_uc;
rrlp_eph->ephemE = gps_eph_sv->e;
rrlp_eph->ephemCus = gps_eph_sv->c_us;
rrlp_eph->ephemAPowerHalf = gps_eph_sv->a_powhalf;
rrlp_eph->ephemToe = gps_eph_sv->t_oe;
rrlp_eph->ephemFitFlag = gps_eph_sv->fit_flag;
rrlp_eph->ephemAODA = gps_eph_sv->aodo;
rrlp_eph->ephemCic = gps_eph_sv->c_ic;
rrlp_eph->ephemOmegaA0 = gps_eph_sv->omega_0;
rrlp_eph->ephemCis = gps_eph_sv->c_is;
rrlp_eph->ephemI0 = gps_eph_sv->i_0;
rrlp_eph->ephemCrc = gps_eph_sv->c_rc;
rrlp_eph->ephemW = gps_eph_sv->w;
rrlp_eph->ephemOmegaADot = gps_eph_sv->omega_dot;
rrlp_eph->ephemIDot = gps_eph_sv->idot;
rrlp_eph->ephemSF1Rsvd.reserved1 = gps_eph_sv->_rsvd1;
rrlp_eph->ephemSF1Rsvd.reserved2 = gps_eph_sv->_rsvd2;
rrlp_eph->ephemSF1Rsvd.reserved3 = gps_eph_sv->_rsvd3;
rrlp_eph->ephemSF1Rsvd.reserved4 = gps_eph_sv->_rsvd4;
}
static void
_rrlp_fill_almanac_element(
struct AlmanacElement *rrlp_ae,
struct gps_almanac_sv *gps_alm_sv)
{
rrlp_ae->satelliteID = gps_alm_sv->sv_id - 1; /* Dieter: satellite ID is zero based */
rrlp_ae->almanacE = gps_alm_sv->e;
rrlp_ae->alamanacToa = gps_alm_sv->t_oa;
rrlp_ae->almanacKsii = gps_alm_sv->ksii;
rrlp_ae->almanacOmegaDot = gps_alm_sv->omega_dot;
rrlp_ae->almanacSVhealth = gps_alm_sv->sv_health;
rrlp_ae->almanacAPowerHalf = gps_alm_sv->a_powhalf;
rrlp_ae->almanacOmega0 = gps_alm_sv->omega_0;
rrlp_ae->almanacW = gps_alm_sv->w;
rrlp_ae->almanacM0 = gps_alm_sv->m_0;
rrlp_ae->almanacAF0 = gps_alm_sv->a_f0;
rrlp_ae->almanacAF1 = gps_alm_sv->a_f1;
}
static void
_rrlp_fill_ionospheric_model(
struct IonosphericModel *rrlp_iono,
struct gps_ionosphere_model *gps_iono)
{
rrlp_iono->alfa0 = gps_iono->alpha_0;
rrlp_iono->alfa1 = gps_iono->alpha_1;
rrlp_iono->alfa2 = gps_iono->alpha_2;
rrlp_iono->alfa3 = gps_iono->alpha_3;
rrlp_iono->beta0 = gps_iono->beta_0;
rrlp_iono->beta1 = gps_iono->beta_1;
rrlp_iono->beta2 = gps_iono->beta_2;
rrlp_iono->beta3 = gps_iono->beta_3;
}
static void
_rrlp_fill_utc_model(
struct UTCModel *rrlp_utc,
struct gps_utc_model *gps_utc)
{
rrlp_utc->utcA1 = gps_utc->a1;
rrlp_utc->utcA0 = gps_utc->a0;
rrlp_utc->utcTot = gps_utc->t_ot;
rrlp_utc->utcWNt = gps_utc->wn_t & 0xff;
rrlp_utc->utcDeltaTls = gps_utc->delta_t_ls;
rrlp_utc->utcWNlsf = gps_utc->wn_lsf & 0xff;
rrlp_utc->utcDN = gps_utc->dn;
rrlp_utc->utcDeltaTlsf = gps_utc->delta_t_lsf;
printf("UTC: 0x%X\n", (unsigned)rrlp_utc->utcA1);
printf("UTC: 0x%X\n", (unsigned)rrlp_utc->utcA0);
printf("UTC: 0x%X\n", (unsigned)rrlp_utc->utcTot);
printf("UTC: 0x%X\n", (unsigned)rrlp_utc->utcWNt);
printf("UTC: 0x%X\n", (unsigned)rrlp_utc->utcDeltaTls);
printf("UTC: 0x%X\n", (unsigned)rrlp_utc->utcWNlsf);
printf("UTC: 0x%X\n", (unsigned)rrlp_utc->utcDN);
printf("UTC: 0x%X\n", (unsigned)rrlp_utc->utcDeltaTlsf);
}
/* }}} */
/* ------------------------------------------------------------------------ */
/* RRLP Assistance PDU Generation */
/* ---------------------------------------------------------------------{{{ */
struct PDU *
_rrlp_create_gps_assist_pdu(int refnum, struct GPS_AssistData **o_gps_ad, int iLast)
{
struct PDU *pdu;
struct GPS_AssistData *gps_ad;
MoreAssDataToBeSent_t *moreAssDataToBeSent;
pdu = calloc(1, sizeof(*pdu));
if (!pdu)
return NULL;
gps_ad = calloc(1, sizeof(*gps_ad));
if (!gps_ad) {
free(pdu);
return NULL;
}
moreAssDataToBeSent = calloc(1, sizeof(*moreAssDataToBeSent));
if (!moreAssDataToBeSent) {
free(gps_ad);
free(pdu);
return NULL;
}
/* last message or more messages ? */
if(iLast) {
if(asn_long2INTEGER(moreAssDataToBeSent, MoreAssDataToBeSent_noMoreMessages) != 0)
fprintf(stderr, "encode error\n");
} else {
if(asn_long2INTEGER(moreAssDataToBeSent, MoreAssDataToBeSent_moreMessagesOnTheWay) != 0)
fprintf(stderr, "encode error\n");
}
if (o_gps_ad)
*o_gps_ad = gps_ad;
pdu->referenceNumber = refnum;
pdu->component.present = RRLP_Component_PR_assistanceData;
pdu->component.choice.assistanceData.gps_AssistData = gps_ad;
pdu->component.choice.assistanceData.moreAssDataToBeSent = moreAssDataToBeSent;
return pdu;
}
static int
_rrlp_add_ionospheric_model(
struct GPS_AssistData *rrlp_gps_ad,
struct gps_assist_data *gps_ad)
{
struct IonosphericModel *rrlp_iono;
if (!(gps_ad->fields & GPS_FIELD_IONOSPHERE))
return -EINVAL;
rrlp_iono = calloc(1, sizeof(*rrlp_iono));
if (!rrlp_iono)
return -ENOMEM;
rrlp_gps_ad->controlHeader.ionosphericModel = rrlp_iono;
_rrlp_fill_ionospheric_model(rrlp_iono, &gps_ad->ionosphere);
return 0;
}
static int
_rrlp_add_utc_model(
struct GPS_AssistData *rrlp_gps_ad,
struct gps_assist_data *gps_ad)
{
struct UTCModel *rrlp_utc;
if (!(gps_ad->fields & GPS_FIELD_UTC))
return -EINVAL;
rrlp_utc = calloc(1, sizeof(*rrlp_utc));
if (!rrlp_utc)
return -ENOMEM;
rrlp_gps_ad->controlHeader.utcModel = rrlp_utc;
_rrlp_fill_utc_model(rrlp_utc, &gps_ad->utc);
return 0;
}
static int
_rrlp_add_reference_location(
struct GPS_AssistData *rrlp_gps_ad,
struct gps_assist_data *gps_ad)
{
#if 0 /* old */
struct RefLocation *rrlp_refloc;
/* FIXME: Check if info is in gps_ad */
rrlp_refloc = calloc(1, sizeof(*rrlp_refloc));
if (!rrlp_refloc)
return -ENOMEM;
rrlp_gps_ad->controlHeader.refLocation = rrlp_refloc;
/* FIXME */
{
uint8_t gps_loc[] = {
0x80, /* Ellipsoid Point with altitude */
#if 0
0x48, 0x0f, 0x93, /* 50.667778 N */
0x03, 0x47, 0x87, /* 4.611667 E */
0x00, 0x72, /* 114m */
#else // Dieter
0x44, 0xEF, 0xEB,
0x09, 0x33, 0xAD,
0x01, 0xEC,
#endif
};
uint8_t *b = malloc(sizeof(gps_loc));
memcpy(b, gps_loc, sizeof(gps_loc));
rrlp_refloc->threeDLocation.buf = b;
rrlp_refloc->threeDLocation.size = sizeof(gps_loc);
}
return 0;
#else /* new */
struct RefLocation *rrlp_refloc;
uint8_t *b;
if (!(gps_ad->fields & GPS_FIELD_REFPOS))
return -EINVAL;
rrlp_refloc = calloc(1, sizeof(*rrlp_refloc));
if (!rrlp_refloc)
return -ENOMEM;
rrlp_gps_ad->controlHeader.refLocation = rrlp_refloc;
b = malloc(9);
b[0] = 0x80; /* Ellipsoid Point with altitude */
_ts_23_032_store_latitude(gps_ad->ref_pos.latitude, &b[1]);
_ts_23_032_store_longitude(gps_ad->ref_pos.longitude, &b[4]);
_ts_23_032_store_altitude(gps_ad->ref_pos.altitude, &b[7]);
rrlp_refloc->threeDLocation.buf = b;
rrlp_refloc->threeDLocation.size = 9;
return 0;
#endif
}
static int
_rrlp_add_reference_time(
struct GPS_AssistData *rrlp_gps_ad,
struct gps_assist_data *gps_ad)
{
#if 0 /* old */
struct ReferenceTime *rrlp_reftime;
/* FIXME: Check if info is in gps_ad */
rrlp_reftime = calloc(1, sizeof(*rrlp_reftime));
if (!rrlp_reftime)
return -ENOMEM;
rrlp_gps_ad->controlHeader.referenceTime = rrlp_reftime;
/* FIXME */
// rrlp_reftime.gpsTime.gpsTOW23b = g_gps_tow / 80; /* 23 bits */
// rrlp_reftime.gpsTime.gpsWeek = g_gps_week & 0x3ff; /* 10 bits */
#if 1 // Dieter
printf("Time %d\n", (int)time(NULL));
//rrlp_reftime->gpsTime.gpsTOW23b = (time(NULL) - 1261643144) + 375961;
rrlp_reftime->gpsTime.gpsTOW23b = (time(NULL) - 1281949530) + 119148;
printf("GPS TOW %d\n", (int)rrlp_reftime->gpsTime.gpsTOW23b);
rrlp_reftime->gpsTime.gpsTOW23b = (uint32_t)((double)rrlp_reftime->gpsTime.gpsTOW23b * 12.5 + 0.5) & 0x7FFFFF;
//rrlp_reftime->gpsTime.gpsWeek = 1563 & 0x3FF; // KW52 2009
//rrlp_reftime->gpsTime.gpsWeek = 1565 & 0x3FF; // KW1 2010
rrlp_reftime->gpsTime.gpsWeek = 1597 & 0x3FF; // KW33 2010
#endif
return 0;
#else /* new */
struct ReferenceTime *rrlp_reftime;
double tow;
if (!(gps_ad->fields & GPS_FIELD_REFTIME))
return -EINVAL;
rrlp_reftime = calloc(1, sizeof(*rrlp_reftime));
if (!rrlp_reftime)
return -ENOMEM;
rrlp_gps_ad->controlHeader.referenceTime = rrlp_reftime;
/* TODO: adjust offset so that MS receives the correct time ? */
#define MY_OFFSET 0
tow = gps_ad->ref_time.tow + (time(NULL) - gps_ad->ref_time.when) + MY_OFFSET;
printf("TOW = %f\n", tow);
rrlp_reftime->gpsTime.gpsWeek = gps_ad->ref_time.wn & 0x3ff; /* 10b */
rrlp_reftime->gpsTime.gpsTOW23b =
((int)floor(tow / 0.08)) & 0x7fffff; /* 23b */
return 0;
#endif
}
static int
_rrlp_add_realtime_integrity(
struct GPS_AssistData *rrlp_gps_ad,
struct gps_assist_data *gps_ad)
{
struct SeqOf_BadSatelliteSet *rrlp_realtime_integrity;
#if 0 /* not yet */
if (!(gps_ad->fields & GPS_FIELD_REALTIME_INT))
return -EINVAL;
#endif
rrlp_realtime_integrity = calloc(1, sizeof(*rrlp_realtime_integrity));
if (!rrlp_realtime_integrity)
return -ENOMEM;
rrlp_gps_ad->controlHeader.realTimeIntegrity = rrlp_realtime_integrity;
#if 1 /* TODO */
SatelliteID_t *sa_id;
sa_id = calloc(1, sizeof(*sa_id));
*sa_id = 63;
ASN_SEQUENCE_ADD(&rrlp_realtime_integrity->list, sa_id);
#endif
return 0;
}
static int
_rrlp_add_almanac(
struct GPS_AssistData *rrlp_gps_ad,
struct gps_assist_data *gps_ad, int *start, int count)
{
int i;
struct Almanac *rrlp_alm;
struct gps_almanac *gps_alm = &gps_ad->almanac;
if (!(gps_ad->fields & GPS_FIELD_ALMANAC))
return -EINVAL;
rrlp_alm = calloc(1, sizeof(*rrlp_alm));
if (!rrlp_alm)
return -ENOMEM;
rrlp_gps_ad->controlHeader.almanac = rrlp_alm;
rrlp_alm->alamanacWNa = gps_alm->wna;
if (count == -1)
count = gps_alm->n_sv - *start;
for (i=*start; (i<*start+count) && (i<gps_alm->n_sv); i++) {
struct AlmanacElement *ae;
ae = calloc(1, sizeof(*ae));
if (!ae)
return -ENOMEM;
_rrlp_fill_almanac_element(ae, &gps_alm->svs[i]);
ASN_SEQUENCE_ADD(&rrlp_alm->almanacList.list, ae);
}
*start = i;
return i < gps_alm->n_sv;
}
static int
_rrlp_add_ephemeris(
struct GPS_AssistData *rrlp_gps_ad,
struct gps_assist_data *gps_ad, int *start, int count, uint64_t mask)
{
int i, j;
struct NavigationModel *rrlp_nav;
struct gps_ephemeris *gps_eph = &gps_ad->ephemeris;
if (!(gps_ad->fields & GPS_FIELD_EPHEMERIS))
return -EINVAL;
rrlp_nav = calloc(1, sizeof(*rrlp_nav));
if (!rrlp_nav)
return -ENOMEM;
rrlp_gps_ad->controlHeader.navigationModel = rrlp_nav;
if (count == -1)
count = gps_eph->n_sv - *start;
for (i=*start,j=0; (j<count) && (i<gps_eph->n_sv); i++) {
if (!(mask & (1ULL<<(gps_eph->svs[i].sv_id-1)))) /* Dieter: CHECK */
continue;
struct NavModelElement *nme;
nme = calloc(1, sizeof(*nme));
if (!nme)
return -ENOMEM;
_rrlp_fill_navigation_model_element(nme, &gps_eph->svs[i]);
ASN_SEQUENCE_ADD(&rrlp_nav->navModelList.list, nme);
j++;
}
*start = i;
return i < gps_eph->n_sv;
}
#define MAX_PDUS 64