xref: /freebsd/contrib/ntp/html/drivers/oncore-shmem.html (revision 0b3105a37d7adcadcb720112fed4dc4e8040be99)
1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
2
3<html>
4
5	<head>
6		<meta http-equiv="content-type" content="text/html;charset=iso-8859-1">
7		<title>ONCORE - SHMEM</title>
8		<link href="scripts/style.css" type="text/css" rel="stylesheet">
9	</head>
10
11	<body>
12		<h3>Motorola ONCORE - The Shared Memory Interface</h3>
13<p>Last update:
14  <!-- #BeginDate format:En2m -->21-Oct-2010  23:44<!-- #EndDate -->
15  UTC</p>
16		<hr>
17		<h4>Introduction</h4>
18		<p>In NMEA mode, the Oncore GPS receiver provides the user with the same information as other GPS receivers. In BINARY mode, it can provide a lot of additional information.</p>
19		<p>In particular, you can ask for satellite positions, satellite health, signal levels, the ephemeris and the almanac, and you can set many operational parameters. In the case of the VP, you can get the pseudorange corrections necessary to act as a DGPS base station, and you can see the raw satellite data messages themselves.</p>
20		<p>When using the Oncore GPS receiver with NTP, this additional information is usually not available since the receiver is only talking to the oncore driver in NTPD. To make this information available for use in other programs, (say graphic displays of satellites positions, plots of SA, etc.), a shared memory interface (SHMEM) has been added to the refclock_oncore driver on those operating systems that support shared memory.</p>
21		<p>To make use of this information you will need an Oncore Reference Manual for the Oncore GPS receiver that you have. The Manual for the VP only exists as a paper document, the UT+/GT+/M12 manuals are available as a pdf documents at <a href="http://www.synergy-gps.com/Mot_Manuals.html">Synergy</a> .</p>
22		<p>This interface was written by Poul-Henning Kamp (phk@FreeBSD.org), and modified by Reg Clemens (reg@dwf.com). The interface is known to work in FreeBSD, Linux, and Solaris.</p>
23		<h4>Activating the Interface</h4>
24		<p>Although the Shared Memory Interface will be compiled into the Oncore driver on those systems where Shared Memory is supported, to activate this interface you must include a <b>STATUS</b> or <b>SHMEM</b> line in the <tt>/etc/ntp.oncore</tt> data file that looks like</p>
25		<pre>
26        STATUS &lt; file_name &gt;<br>
27
28        or<br>
29
30        SHMEM &lt; file_name &gt;
31</pre>
32		Thus a line like
33		<pre>
34        SHMEM /var/adm/ntpstats/ONCORE
35</pre>
36		<p>would be acceptable. This file name will be used to access the Shared Memory.</p>
37		<p>In addition, one the two keywords <b>Posn2D</b> and <b>Posn3D</b> can be added to see @@Ea records containing the 2D or 3D position of the station (see below). Thus to activate the interface, and see 3D positions, something like</p>
38		<pre>
39        SHMEM /var/adm/ntpstats/ONCORE
40        Posn3D
41</pre>
42		<p>would be required.</p>
43		<h4>Storage of Messages in Shared Memory</h4>
44		<p>With the shared memory interface, the oncore driver (refclock_oncore) allocates space for all of the messages that it is configured to receive, and then puts each message in the appropriate slot in shared memory as it arrives from the receiver. Since there is no easy way for a client program to know when the shared memory has been updated, a sequence number is associated with each message, and is incremented when a new message arrives. With the sequence number it is easy to check through the shared memory segment for messages that have changed.</p>
45		<p>The Oncore binary messages are kept in their full length, as described in the Reference manual, that is everything from the @@ prefix thru the &lt;checksum&gt;&lt;CR&gt;&lt;LF&gt;.</p>
46		<p>The data starts at location ONE of SHMEM (NOT location ZERO).</p>
47		<p>The messages are stacked in a series of variable length structures, that look like</p>
48		<pre>
49        struct message {
50                u_int   length;
51                u_char  sequence;
52                u_char  message[length];
53        }
54</pre>
55		<p>if something like that were legal. That is, there are two bytes (caution, these may NOT be aligned with word boundaries, so the field needs to be treated as a pair of u_char), that contains the length of the next message. This is followed by a u_char sequence number, that is incremented whenever a new message of this type is received. This is followed by 'length' characters of the actual message.</p>
56		<p>The next structure starts immediately following the last char of the previous message (no alignment). Thus, each structure starts a distance of 'length+3' from the previous structure.</p>
57		<p>Following the last structure, is a u_int containing a zero length to indicate the end of the data.</p>
58		<p>The messages are recognized by reading the headers in the data itself, viz @@Ea or whatever.</p>
59		<p>There are two special cases.</p>
60		<p>(1) The almanac takes a total of 34 submessages all starting with @@Cb.<br>
61			35 slots are allocated in shared memory. Each @@Cb message is initially placed in the first of these locations, and then later it is moved to the appropriate location for that submessage. The submessages can be distinguished by the first two characters following the @@Cb header, and new data is received only when the almanac changes.</p>
62		<p>(2) The @@Ea message contains the calculated location of the antenna, and is received once per second. However, when in timekeeping mode, the receiver is normally put in 0D mode, with the position fixed, to get better accuracy. In 0D mode no position is calculated.</p>
63		<p>When the SHMEM option is active, and if one of <b>Posn2D</b> or <b>Posn3D</b> is specified, one @@Ea record is hijacked each 15s, and the receiver is put back in 2D/3D mode so the the current location can be determined (for position determination, or for tracking SA). The timekeeping code is careful NOT to use the time associated with this (less accurate) 2D/3D tick in its timekeeping functions.</p>
64		<p>Following the initial @@Ea message are 3 additional slots for a total of four. As with the almanac, the first gets filled each time a new record becomes available, later in the code, the message is distributed to the appropriate slot. The additional slots are for messages containing 0D, 2D and 3D positions. These messages can be distinguished by different bit patterns in the last data byte of the record.</p>
65		<h4>Opening the Shared Memory File</h4>
66		<p>The shared memory segment is accessed through a file name given on a <b>SHMEM</b> card in the <tt>/etc/ntp.oncore</tt> input file. The following code could be used to open the Shared Memory Segment:</p>
67		<pre>
68        char *Buf, *file;
69        int size, fd;
70        struct stat statbuf;
71
72        file = &quot;/var/adm/ntpstats/ONCORE&quot;;  /* the file name on my ACCESS card */
73        if ((fd=open(file, O_RDONLY)) &lt; 0) {
74                fprintf(stderr, &quot;Cant open %s\n&quot;, file);
75                exit(1);
76        }
77
78        if (stat(file, &amp;statbuf) &lt; 0) {
79                fprintf(stderr, &quot;Cant stat %s\n&quot;, file);
80                exit(1);
81        }
82
83        size = statbuf.st_size;
84        if ((Buf=mmap(0, size, PROT_READ, MAP_SHARED, fd, (off_t) 0)) &lt; 0) {
85                fprintf(stderr, &quot;MMAP failed\n&quot;);
86                exit(1);
87        }
88</pre>
89		<h4>Accessing the data</h4>
90		<p>The following code shows how to get to the individual records.</p>
91		<pre>
92        void    oncore_msg_Ea(), oncore_msg_As(), oncore_msg_Bb();
93
94        struct Msg {
95            char         c[5];
96            unsigned int seq;
97            void         (*go_to)(uchar *);
98        };
99
100        struct Msg Hdr[] = { {&quot;@@Bb&quot;, 0, &amp;oncore_msg_Bb},
101                             {&quot;@@Ea&quot;, 0, &amp;oncore_msg_Ea},
102                             {&quot;@@As&quot;, 0, &amp;oncore_msg_As}};
103
104        void
105        read_data()
106        {
107            int     i, j, k, n, iseq, jseq;
108            uchar   *cp, *cp1;
109
110
111            for(cp=Buf+1; (n = 256*(*cp) + *(cp+1)) != 0;  cp+=(n+3)) {
112                for (k=0; k &lt; sizeof(Hdr)/sizeof(Hdr[0]);  k++) {
113                    if (!strncmp(cp+3, Hdr[k].c, 4)) {      /* am I interested? */
114                        iseq = *(cp+2);
115                        jseq = Hdr[k].seq;
116                        Hdr[k].seq = iseq;
117                        if (iseq &gt; jseq) {              /* has it changed? */
118                            /* verify checksum */
119                            j = 0;
120                            cp1 = cp+3;             /* points to start of oncore response */
121                            for (i=2; i &lt; n-3; i++)
122                                j ^= cp1[i];
123                            if (j == cp1[n-3]) {    /* good checksum */
124                                    Hdr[k].go_to(cp1);
125                            } else {
126                                fprintf(stderr, &quot;Bad Checksum for %s\n&quot;, Hdr[k].c);
127                                break;
128                            }
129                        }
130                    }
131                }
132                if (!strncmp(cp+3, &quot;@@Ea&quot;, 4))
133                    cp += 3*(n+3);
134                if (!strncmp(cp+3, &quot;@@Cb&quot;, 4))
135                    cp += 34*(n+3);
136            }
137        }
138
139        oncore_msg_Bb(uchar *buf)
140        {
141                /* process Bb messages */
142        }
143
144        oncore_msg_Ea(uchar *buf)
145        {
146                /* process Ea messages */
147        }
148
149        oncore_msg_As(uchar *buf)
150        {
151                /* process As messages */
152        }
153</pre>
154		<p>The structure Hdr contains the Identifying string for each of the messages that we want to examine, and the name of a program to call when a new message of that type is arrives. The loop can be run every few seconds to check for new data.</p>
155		<h4>Examples</h4>
156		<p>There are two complete examples available. The first plots satellite positions and the station position as affected by SA, and keeps track of the mean station position, so you can run it for periods of days to get a better station position. The second shows the effective horizon by watching satellite tracks. The examples will be found in the GNU-zipped tar file <a href="ftp://ftp.udel.edu/pub/ntp/software/OncorePlot.tar.gz">ftp://ftp.udel.edu/pub/ntp/software/OncorePlot.tar.gz</a>.</p>
157		<p>Try the new interface, enjoy.</p>
158		<hr>
159		<address>Reg.Clemens (reg@dwf.com), Poul-Henning Kamp (phk@FreeBSD.org)</address>
160		<hr>
161		<script type="text/javascript" language="javascript" src="scripts/footer.txt"></script>
162	</body>
163
164</html>
165