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

This commit is contained in:
Harald Welte 2012-07-18 22:04:09 +02:00
parent a5c8d80688
commit 47ab210af9
15 changed files with 4653 additions and 0 deletions

46
rrlpd/README Normal file
View File

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

View File

@ -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);
}

View File

@ -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;
+}
+

View File

@ -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);
}

41
rrlpd/src/Makefile Normal file
View File

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

1055
rrlpd/src/asn1/rrlp.asn Normal file

File diff suppressed because it is too large Load Diff

131
rrlpd/src/gps.c Normal file
View File

@ -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;
}

193
rrlpd/src/gps.h Normal file
View File

@ -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__ */

1298
rrlpd/src/main.c Normal file

File diff suppressed because it is too large Load Diff

864
rrlpd/src/rrlp.c Normal file
View File

@ -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
int
rrlp_gps_assist_pdus(int refnum,
struct gps_assist_data *gps_ad, struct rrlp_assist_req *req,
void **o_pdu, int *o_len, int o_max_pdus)
{
struct PDU *lst_pdu[MAX_PDUS];
int lst_cnt = 0;
struct PDU *rrlp_pdu = NULL;
struct GPS_AssistData *rrlp_gps_ad = NULL;
uint32_t re = req->req_elems;
int i, rv = 0;
/* IonosphericModel, UTCModel, RefLocation, ReferenceTime */
if (re & (RRLP_AR_IONO_MODEL |
RRLP_AR_UTC_MODEL |
RRLP_AR_REF_TIME |
RRLP_AR_REF_LOC |
RRLP_AR_REALTIME_INT))
{
int pdu_has_data = 0;
rrlp_pdu = _rrlp_create_gps_assist_pdu(refnum, &rrlp_gps_ad, 0);
if (!rrlp_pdu) {
rv = -ENOMEM;
goto error;
}
#if 1 /* enable/disable component */
if (re & RRLP_AR_IONO_MODEL) {
printf("+ IONO_MODEL\n");
if (!_rrlp_add_ionospheric_model(rrlp_gps_ad, gps_ad))
pdu_has_data = 1;
}
#endif
#if 1 /* enable/disable component */
if (re & RRLP_AR_UTC_MODEL) {
printf("+ UTC_MODEL\n");
if (!_rrlp_add_utc_model(rrlp_gps_ad, gps_ad))
pdu_has_data = 1;
}
#endif
#if 1 /* enable/disable component */
if (re & RRLP_AR_REF_TIME) {
printf("+ REF_TIME\n");
if (!_rrlp_add_reference_time(rrlp_gps_ad, gps_ad))
pdu_has_data = 1;
}
#endif
#if 1 /* enable/disable component */
if (re & RRLP_AR_REF_LOC) {
printf("+ REF_LOC\n");
if (!_rrlp_add_reference_location(rrlp_gps_ad, gps_ad))
pdu_has_data = 1;
}
#endif
#if 0 /* enable/disable component (skip this as it is for now a dummy list only) */
if (re & RRLP_AR_REALTIME_INT) {
printf("+ REALTIME_INTEGRITY\n");
if (!_rrlp_add_realtime_integrity(rrlp_gps_ad, gps_ad))
pdu_has_data = 1;
}
#endif
if (pdu_has_data) {
lst_pdu[lst_cnt++] = rrlp_pdu;
rrlp_pdu = NULL;
}
}
#if 1 /* enable/disable component */
/* Almanac */
if (re & RRLP_AR_ALMANAC) {
i = 0;
do {
if (!(gps_ad->fields & GPS_FIELD_ALMANAC))
break;
printf("+ ALMANAC\n");
if (!rrlp_pdu) {
rrlp_pdu = _rrlp_create_gps_assist_pdu(refnum, &rrlp_gps_ad, 0);
if (!rrlp_pdu) {
rv = -ENOMEM;
goto error;
}
}
/* adjust count so that messages fit in a single PDU */
rv = _rrlp_add_almanac(rrlp_gps_ad, gps_ad, &i, 10);
if (rv < 0)
goto error;
lst_pdu[lst_cnt++] = rrlp_pdu;
rrlp_pdu = NULL;
} while (rv);
}
#endif
#if 1 /* enable/disable component */
/* Ephemeris */
if (re & RRLP_AR_EPHEMERIS) {
i = 0;
do {
if (!(gps_ad->fields & GPS_FIELD_EPHEMERIS))
break;
printf("+ EPHEMERIS\n");
if (!rrlp_pdu) {
rrlp_pdu = _rrlp_create_gps_assist_pdu(refnum, &rrlp_gps_ad, 0);
if (!rrlp_pdu) {
rv = -ENOMEM;
goto error;
}
}
#if 1 /* two sets in one PDS to be not larger than maximum message size */
rv = _rrlp_add_ephemeris(rrlp_gps_ad, gps_ad, &i, 2, req->eph_svs);
#elif 0 /* three sets in one PDU, too large */
rv = _rrlp_add_ephemeris(rrlp_gps_ad, gps_ad, &i, 3, req->eph_svs);
#elif 0 /* all sets in one PDU, too large */
rv = _rrlp_add_ephemeris(rrlp_gps_ad, gps_ad, &i, -1, req->eph_svs);
#endif
lst_pdu[lst_cnt++] = rrlp_pdu;
rrlp_pdu = NULL;
} while (rv);
}
#endif
/* set last message flag */
if(asn_long2INTEGER(lst_pdu[lst_cnt - 1]->component.choice.assistanceData.moreAssDataToBeSent, MoreAssDataToBeSent_noMoreMessages) != 0)
fprintf(stderr, "encode error\n");
/* Serialize & Release all PDUs */
for (i=0; i<lst_cnt && i<o_max_pdus; i++) {
/* Debugging, dump PDU */
asn_fprint(stdout, &asn_DEF_PDU, lst_pdu[i]);
rv = uper_encode_to_new_buffer(&asn_DEF_PDU, NULL, lst_pdu[i], &o_pdu[i]);
if (rv < 0) {
printf("uper_encode_to_new_buffer error %d (%d)\n", rv, i);
goto error;
}
o_len[i] = rv;
}
return lst_cnt;
/* Release ASN.1 objects */
error:
if (rrlp_pdu)
asn_DEF_PDU.free_struct(&asn_DEF_PDU, (void*)rrlp_pdu, 0);
for (i=0; i<lst_cnt; i++)
asn_DEF_PDU.free_struct(&asn_DEF_PDU, lst_pdu[i], 0);
return rv;
}
/* }}} */

65
rrlpd/src/rrlp.h Normal file
View File

@ -0,0 +1,65 @@
/*
* rrlp.h
*
* RRLP Header
*
*
* 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 __RRLP_H__
#define __RRLP_H__
#include <stdint.h>
#include "gps.h"
#ifdef __cplusplus
extern "C" {
#endif
/* Our internal simplified structure for requests */
#define RRLP_AR_REF_LOC (1<<0)
#define RRLP_AR_REF_TIME (1<<1)
#define RRLP_AR_UTC_MODEL (1<<2)
#define RRLP_AR_IONO_MODEL (1<<3)
#define RRLP_AR_ALMANAC (1<<4)
#define RRLP_AR_EPHEMERIS (1<<5)
#define RRLP_AR_REALTIME_INT (1<<6)
struct rrlp_assist_req {
uint32_t req_elems;
uint64_t eph_svs;
};
/* Methods */
int rrlp_decode_assistance_request(struct rrlp_assist_req *ar,
void *req, int req_len);
int rrlp_gps_assist_pdus(int refnum,
struct gps_assist_data *gps_ad, struct rrlp_assist_req *req,
void **o_pdu, int *o_len, int o_max_pdus);
#ifdef __cplusplus
}
#endif
#endif /* __RRLP_H__ */

246
rrlpd/src/ubx-parse.c Normal file
View File

@ -0,0 +1,246 @@
/*
* ubx-parse.c
*
* Implementation of parsing code converting UBX messages to GPS assist
* 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"
#include "ubx.h"
#include "ubx-parse.h"
#define DEBUG 1
#if DEBUG
#define printd(x, args ...) printf(x, ## args)
#else
#define printd(x, args ...)
#endif
#define DEBUG1 0
#if DEBUG1
#define printd1(x, args ...) printf(x, ## args)
#else
#define printd1(x, args ...)
#endif
/* Helpers */
static int
float_to_fixedpoint(float f, int sf)
{
if (sf < 0) {
while (sf++ < 0)
f *= 2.0f;
} else {
while (sf-- > 0)
f *= 0.5f;
}
return (int)f;
}
static inline int
double_to_fixedpoint(double d, int sf)
{
if (sf < 0) {
while (sf++ < 0)
d *= 2.0;
} else {
while (sf-- > 0)
d *= 0.5;
}
return (int)d;
}
/* UBX message parsing to fill gps assist data */
static void
_ubx_msg_parse_nav_posllh(struct ubx_hdr *hdr, void *pl, int pl_len, void *ud)
{
struct ubx_nav_posllh *nav_posllh = pl;
struct gps_assist_data *gps = ud;
printd("[.] NAV_POSLLH\n");
gps->fields |= GPS_FIELD_REFPOS;
gps->ref_pos.latitude = (double)(nav_posllh->lat) * 1e-7;
gps->ref_pos.longitude = (double)(nav_posllh->lon) * 1e-7;
gps->ref_pos.altitude = (double)(nav_posllh->height) * 1e-3;
printd(" TOW %lu\n", nav_posllh->itow);
printd(" latitude %f\n", gps->ref_pos.latitude);
printd(" longitude %f\n", gps->ref_pos.longitude);
printd(" altitude %f\n", gps->ref_pos.altitude);
}
static void
_ubx_msg_parse_aid_ini(struct ubx_hdr *hdr, void *pl, int pl_len, void *ud)
{
struct ubx_aid_ini *aid_ini = pl;
struct gps_assist_data *gps = ud;
printd("[.] AID_INI\n");
/* Extract info for "Reference Time" */
gps->fields |= GPS_FIELD_REFTIME;
gps->ref_time.wn = aid_ini->wn;
gps->ref_time.tow = (double)aid_ini->tow * 1e-3;
gps->ref_time.when = time(NULL);
printd(" WN %d\n", gps->ref_time.wn);
printd(" TOW %ld\n", aid_ini->tow);
if((aid_ini->flags & 0x03) != 0x03) { /* time and pos valid ? */
fprintf(stderr, "Postion and/or time not valid (0x%lx)", aid_ini->flags);
}
// FIXME: We could extract ref position as well but we need it in
// WGS84 geodetic coordinates and it's provided as ecef, so
// we need a lot of math ...
}
static void
_ubx_msg_parse_aid_hui(struct ubx_hdr *hdr, void *pl, int pl_len, void *ud)
{
struct ubx_aid_hui *aid_hui = pl;
struct gps_assist_data *gps = ud;
printd("[.] AID_HUI\n");
if (aid_hui->flags & 0x2) { /* UTC parameters valid */
struct gps_utc_model *utc = &gps->utc;
printd(" UTC\n");
gps->fields |= GPS_FIELD_UTC;
utc->a0 = double_to_fixedpoint(aid_hui->utc_a0, -30);
utc->a1 = double_to_fixedpoint(aid_hui->utc_a1, -50);
utc->delta_t_ls = aid_hui->utc_ls;
utc->t_ot = aid_hui->utc_tot >> 12;
utc->wn_t = aid_hui->utc_wnt;
utc->wn_lsf = aid_hui->utc_wnf;
utc->dn = aid_hui->utc_dn;
utc->delta_t_lsf = aid_hui->utc_lsf;
}
if (aid_hui->flags & 0x04) { /* Klobuchar parameters valid */
struct gps_ionosphere_model *iono = &gps->ionosphere;
printd(" IONOSPHERE\n");
gps->fields |= GPS_FIELD_IONOSPHERE;
iono->alpha_0 = float_to_fixedpoint(aid_hui->klob_a0, -30);
iono->alpha_1 = float_to_fixedpoint(aid_hui->klob_a1, -27);
iono->alpha_2 = float_to_fixedpoint(aid_hui->klob_a2, -24);
iono->alpha_3 = float_to_fixedpoint(aid_hui->klob_a3, -24);
iono->beta_0 = float_to_fixedpoint(aid_hui->klob_b0, 11);
iono->beta_1 = float_to_fixedpoint(aid_hui->klob_b1, 14);
iono->beta_2 = float_to_fixedpoint(aid_hui->klob_b2, 16);
iono->beta_3 = float_to_fixedpoint(aid_hui->klob_b3, 16);
}
}
static void
_ubx_msg_parse_aid_alm(struct ubx_hdr *hdr, void *pl, int pl_len, void *ud)
{
struct ubx_aid_alm *aid_alm = pl;
struct gps_assist_data *gps = ud;
if(pl_len == 8) /* length if not available */
return;
if(pl_len != sizeof(struct ubx_aid_alm)) {
fprintf(stderr, "pl_len != sizeof(struct ubx_aid_alm) (%d)\n", pl_len);
return;
}
printd("[.] AID_ALM %2ld - %ld (nsv = %d)\n", aid_alm->sv_id, aid_alm->gps_week, gps->almanac.n_sv);
if (aid_alm->gps_week) {
int i = gps->almanac.n_sv++;
gps->fields |= GPS_FIELD_ALMANAC;
gps->almanac.wna = aid_alm->gps_week & 0xff;
gps_unpack_sf45_almanac(aid_alm->alm_words, &gps->almanac.svs[i]);
/* set satellite ID this way, otherwise it will be wrong */
gps->almanac.svs[i].sv_id = aid_alm->sv_id;
}
}
static void
_ubx_msg_parse_aid_eph(struct ubx_hdr *hdr, void *pl, int pl_len, void *ud)
{
struct ubx_aid_eph *aid_eph = pl;
struct gps_assist_data *gps = ud;
if(pl_len == 8) /* length if not available */
return;
if(pl_len != sizeof(struct ubx_aid_eph)) {
fprintf(stderr, "pl_len != sizeof(struct ubx_aid_eph) (%d)\n", pl_len);
return;
}
printd("[.] AID_EPH %2ld - %s (nsv = %d)\n", aid_eph->sv_id, aid_eph->present ? "present" : "", gps->ephemeris.n_sv);
if (aid_eph->present) {
int i = gps->ephemeris.n_sv++;
gps->fields |= GPS_FIELD_EPHEMERIS;
gps->ephemeris.svs[i].sv_id = aid_eph->sv_id;
gps_unpack_sf123(aid_eph->eph_words, &gps->ephemeris.svs[i]);
}
}
static void
_ubx_msg_parse_nav_timegps(struct ubx_hdr *hdr, void *pl, int pl_len, void *ud)
{
struct ubx_nav_timegps *nav_timegps = pl;
struct gps_assist_data *gps = ud;
printd1("[.] NAV_TIMEGPS\n");
/* Extract info for "Reference Time" */
gps->fields |= GPS_FIELD_REFTIME;
gps->ref_time.wn = nav_timegps->week;
gps->ref_time.tow = (double)nav_timegps->itow * 1e-3;
gps->ref_time.when = time(NULL);
printd1(" WN %d\n", nav_timegps->week);
printd1(" TOW %ld\n", nav_timegps->itow);
}
/* Dispatch table */
struct ubx_dispatch_entry ubx_parse_dt[] = {
UBX_DISPATCH(NAV, POSLLH, _ubx_msg_parse_nav_posllh),
UBX_DISPATCH(AID, INI, _ubx_msg_parse_aid_ini),
UBX_DISPATCH(AID, HUI, _ubx_msg_parse_aid_hui),
UBX_DISPATCH(AID, ALM, _ubx_msg_parse_aid_alm),
UBX_DISPATCH(AID, EPH, _ubx_msg_parse_aid_eph),
UBX_DISPATCH(NAV, TIMEGPS, _ubx_msg_parse_nav_timegps),
};

45
rrlpd/src/ubx-parse.h Normal file
View File

@ -0,0 +1,45 @@
/*
* ubx-parse.h
*
* Header for parsing code converting UBX messages to GPS assist 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 __UBX_PARSE_H__
#define __UBX_PARSE_H__
#include "gps.h"
#include "ubx.h"
#ifdef __cplusplus
extern "C" {
#endif
/* Dispatch table */
extern struct ubx_dispatch_entry ubx_parse_dt[];
#ifdef __cplusplus
}
#endif
#endif /* __UBX_PARSE_H__ */

96
rrlpd/src/ubx.c Normal file
View File

@ -0,0 +1,96 @@
/*
* ubx.c
*
* Implementation of generic UBX helpers
*
*
* 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 <stdint.h>
#include "ubx.h"
static void
ubx_checksum(uint8_t *data, int len, uint8_t *cksum)
{
int i;
uint8_t ck0 = 0, ck1 = 0;
for (i=0; i<len; i++) {
ck0 += data[i];
ck1 += ck0;
}
cksum[0] = ck0;
cksum[1] = ck1;
}
static ubx_msg_handler_t
ubx_find_handler(struct ubx_dispatch_entry *dt, uint8_t msg_class, uint8_t msg_id)
{
while (dt->handler) {
if ((dt->msg_class == msg_class) && (dt->msg_id == msg_id))
return dt->handler;
dt++;
}
return NULL;
}
int
ubx_msg_dispatch(struct ubx_dispatch_entry *dt,
void *msg, int len, void *userdata)
{
struct ubx_hdr *hdr = msg;
uint8_t cksum[2], *cksum_ptr;
ubx_msg_handler_t h;
if(len < 2) {
fprintf(stderr, "[!] Length too small (%d)\n", len);
return -1;
}
if ((hdr->sync[0] != UBX_SYNC0) || (hdr->sync[1] != UBX_SYNC1)) {
fprintf(stderr, "[!] Invalid sync bytes\n");
return -1;
}
if(len < sizeof(struct ubx_hdr)) {
fprintf(stderr, "[!] Length too small for UBX header (%d)\n", len);
return -1;
}
if(len < sizeof(struct ubx_hdr) + hdr->payload_len - 2) {
fprintf(stderr, "[!] Length too small for UBX header and payload (%d)\n", len);
return -1;
}
ubx_checksum(msg + 2, sizeof(struct ubx_hdr) + hdr->payload_len - 2, cksum);
cksum_ptr = msg + (sizeof(struct ubx_hdr) + hdr->payload_len);
if ((cksum_ptr[0] != cksum[0]) || (cksum_ptr[1] != cksum[1])) {
fprintf(stderr, "[!] Invalid checksum\n");
return -1;
}
h = ubx_find_handler(dt, hdr->msg_class, hdr->msg_id);
if (h)
h(hdr, msg + sizeof(struct ubx_hdr), hdr->payload_len, userdata);
return sizeof(struct ubx_hdr) + hdr->payload_len + 2;
}

240
rrlpd/src/ubx.h Normal file
View File

@ -0,0 +1,240 @@
/*
* ubx.h
*
* Header for UBX related stuff
*
*
* 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 __UBX_H__
#define __UBX_H__
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
/* Constants used in UBX */
/* Sync bytes (two first bytes of each message) */
#define UBX_SYNC0 0xb5
#define UBX_SYNC1 0x62
/* UBX messages classes */
#define UBX_CLASS_NAV 0x01
#define UBX_CLASS_RXM 0x02
#define UBX_CLASS_INF 0x04
#define UBX_CLASS_ACK 0x05
#define UBX_CLASS_CFG 0x06
#define UBX_CLASS_UPD 0x09
#define UBX_CLASS_MON 0x0a
#define UBX_CLASS_AID 0x0b
#define UBX_CLASS_TIM 0x0d
/* UBX messages type ID (by class) */
#define UBX_NAV_POSECEF 0x01
#define UBX_NAV_POSLLH 0x02
#define UBX_NAV_STATUS 0x03
#define UBX_NAV_DOP 0x04
#define UBX_NAV_SOL 0x06
#define UBX_NAV_POSUTM 0x08
#define UBX_NAV_VELECEF 0x11
#define UBX_NAV_VELNED 0x12
#define UBX_NAV_TIMEGPS 0x20
#define UBX_NAV_TIMEUTC 0x21
#define UBX_NAV_CLOCK 0x22
#define UBX_NAV_SVINFO 0x30
#define UBX_NAV_DGPS 0x31
#define UBX_NAV_SBAS 0x32
#define UBX_NAV_EKFSTATUS 0x40
#define UBX_RXM_RAW 0x10
#define UBX_RXM_SFRB 0x11
#define UBX_RXM_SVSI 0x20
#define UBX_RXM_SVSI_GPS 0x20
#define UBX_RXM_ALM 0x30
#define UBX_RXM_EPH 0x31
#define UBX_RXM_POSREQ 0x40
#define UBX_INF_ERROR 0x00
#define UBX_INF_WARNING 0x01
#define UBX_INF_NOTICE 0x02
#define UBX_INF_TEST 0x03
#define UBX_INF_DEBUG 0x04
#define UBX_INF_USER 0x07
#define UBX_ACK_NAK 0x00
#define UBX_ACK_ACK 0x01
#define UBX_CFG_PRT 0x00
#define UBX_CFG_USB 0x1b
#define UBX_CFG_MSG 0x01
#define UBX_CFG_NMEA 0x17
#define UBX_CFG_RATE 0x08
#define UBX_CFG_CFG 0x09
#define UBX_CFG_TP 0x07
#define UBX_CFG_NAV2 0x1a
#define UBX_CFG_DAT 0x06
#define UBX_CFG_INF 0x02
#define UBX_CFG_RST 0x04
#define UBX_CFG_RXM 0x11
#define UBX_CFG_ANT 0x13
#define UBX_CFG_FXN 0x0e
#define UBX_CFG_SBAS 0x16
#define UBX_CFG_LIC 0x80
#define UBX_CFG_TM 0x10
#define UBX_CFG_TM2 0x19
#define UBX_CFG_TMODE 0x1d
#define UBX_CFG_EKF 0x12
#define UBX_UPD_DOWNL 0x01
#define UBX_UPD_UPLOAD 0x02
#define UBX_UPD_EXEC 0x03
#define UBX_UPD_MEMCPY 0x04
#define UBX_MON_SCHD 0x01
#define UBX_MON_IO 0x02
#define UBX_MON_IPC 0x03
#define UBX_MON_VER 0x04
#define UBX_MON_EXCEPT 0x05
#define UBX_MON_MSGPP 0x06
#define UBX_MON_RXBUF 0x07
#define UBX_MON_TXBUF 0x08
#define UBX_MON_HW 0x09
#define UBX_MON_USB 0x0a
#define UBX_AID_REQ 0x00
#define UBX_AID_INI 0x01
#define UBX_AID_HUI 0x02
#define UBX_AID_DATA 0x10
#define UBX_AID_ALM 0x30
#define UBX_AID_EPH 0x31
#define UBX_TIM_TP 0x01
#define UBX_TIM_TM 0x02
#define UBX_TIM_TM2 0x03
#define UBX_TIM_SVIN 0x04
/* Header */
struct ubx_hdr {
uint8_t sync[2];
uint8_t msg_class;
uint8_t msg_id;
uint16_t payload_len;
} __attribute__((packed));
/* Payload formats (some of them) */
struct ubx_nav_posllh {
uint32_t itow; /* ms */
int32_t lon; /* scaling 1e-7 */
int32_t lat; /* scaling 1e-7 */
int32_t height;/* mm */
int32_t hsl; /* mm */
uint32_t hacc; /* mm */
uint32_t vacc; /* mm */
} __attribute__((packed));
struct ubx_aid_ini {
int32_t x;
int32_t y;
int32_t z;
uint32_t posacc;
uint16_t tm_cfg;
uint16_t wn;
uint32_t tow; /* ms */
int32_t tow_ns;
uint32_t tacc_ms;
uint32_t tacc_ns;
int32_t clkd;
uint32_t clkdacc;
uint32_t flags;
} __attribute__((packed));
struct ubx_aid_hui {
uint32_t health;
double utc_a1;
double utc_a0;
int32_t utc_tot;
int16_t utc_wnt;
int16_t utc_ls;
int16_t utc_wnf;
int16_t utc_dn;
int16_t utc_lsf;
int16_t utc_spare;
float klob_a0;
float klob_a1;
float klob_a2;
float klob_a3;
float klob_b0;
float klob_b1;
float klob_b2;
float klob_b3;
uint32_t flags;
} __attribute__((packed));
struct ubx_aid_alm {
uint32_t sv_id;
uint32_t gps_week;
uint32_t alm_words[8]; /* Present only if 'gps_week' != 0 */
} __attribute__((packed));
struct ubx_aid_eph {
uint32_t sv_id;
uint32_t present;
uint32_t eph_words[24]; /* Present only if 'present' != 0 */
} __attribute__((packed));
struct ubx_nav_timegps {
uint32_t itow; /* ms */
int32_t ftow; /* ns */
int16_t week;
uint8_t leaps;
uint8_t valid;
uint32_t tacc; /* ns */
} __attribute__((packed));
/* Message handler */
typedef void (*ubx_msg_handler_t)(
struct ubx_hdr *hdr, void *payload, int payload_len, void *userdata);
struct ubx_dispatch_entry {
uint8_t msg_class;
uint8_t msg_id;
ubx_msg_handler_t handler;
};
#define UBX_DISPATCH(kls,id,hdl) { \
.msg_class = UBX_CLASS_ ## kls , \
.msg_id = UBX_ ## kls ## _ ## id, \
.handler = (hdl), \
}
/* Methods */
int ubx_msg_dispatch(struct ubx_dispatch_entry *dt,
void *msg, int len, void *userdata);
#ifdef __cplusplus
}
#endif
#endif /* __UBX_H__ */