3 #$CSK: gpsd.php,v 1.39 2006/11/21 22:31:10 ckuethe Exp $
5 # Copyright (c) 2006,2010 Chris Kuethe <chris.kuethe@gmail.com>
7 # Permission to use, copy, modify, and distribute this software for any
8 # purpose with or without fee is hereby granted, provided that the above
9 # copyright notice and this permission notice appear in all copies.
11 # THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 # ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 # OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 global $head, $blurb, $title, $googlemap, $autorefresh, $footer, $gmap_key;
20 global $server, $advertise, $port, $open, $swap_ew, $testmode;
21 $testmode = 1; # leave this set to 1
23 # Public script parameters:
24 # host: host name or address where GPSd runs. Default: from config file
25 # port: port of GPSd. Default: from config file
26 # op=view: show just the skyview image instead of the whole HTML page
27 # sz=small: used with op=view, display a small (240x240px) skyview
28 # op=json: respond with the GPSd POLL JSON structure
29 # jsonp=prefix: used with op=json, wrap the POLL JSON in parentheses
32 # If you're running PHP with the Suhosin patch (like the Debian PHP5 package),
33 # it may be necessary to increase the value of the
34 # suhosin.get.max_value_length parameter to 2048. The imgdata parameter used
35 # for displaying the skyview is longer than the default 512 allowed by Suhosin.
36 # Debian has the config file at /etc/php5/conf.d/suhosin.ini.
38 # this script shouldn't take more than a few seconds to run
40 ini_set('max_execution_time', 3);
42 if (!file_exists("gpsd_config.inc"))
45 require_once("gpsd_config.inc");
49 {"class":"POLL","timestamp":1270517274.846,"active":1,
50 "fixes":[{"class":"TPV","tag":"MID41","device":"/dev/ttyUSB0",
51 "time":1270517264.240,"ept":0.005,"lat":40.035093060,
52 "lon":-75.519748733,"alt":31.1,"track":99.4319,
53 "speed":0.123,"mode":3}],
54 "skyviews":[{"class":"SKY","tag":"MID41","device":"/dev/ttyUSB0",
55 "time":1270517264.240,"hdop":9.20,"vdop":12.1,
56 "satellites":[{"PRN":16,"el":55,"az":42,"ss":36,"used":true},
57 {"PRN":19,"el":25,"az":177,"ss":0,"used":false},
58 {"PRN":7,"el":13,"az":295,"ss":0,"used":false},
59 {"PRN":6,"el":56,"az":135,"ss":32,"used":true},
60 {"PRN":13,"el":47,"az":304,"ss":0,"used":false},
61 {"PRN":23,"el":66,"az":259,"ss":40,"used":true},
62 {"PRN":20,"el":7,"az":226,"ss":0,"used":false},
63 {"PRN":3,"el":52,"az":163,"ss":32,"used":true},
64 {"PRN":31,"el":16,"az":102,"ss":0,"used":false}
73 # if we're passing in a query, let's unpack and use it
74 $op = isset($_GET['op']) ? $_GET['op'] : '';
75 if (isset($_GET['imgdata']) && $op == 'view'){
76 $resp = base64_decode($_GET['imgdata']);
82 if (isset($_GET['host']))
83 if (!preg_match('/[^a-zA-Z0-9\.-]/', $_GET['host']))
84 $server = $_GET['host'];
86 if (isset($_GET['port']))
87 if (!preg_match('/\D/', $_GET['port']) && ($port>0) && ($port<65536))
88 $port = $_GET['port'];
91 $sock = @fsockopen($server, $port, $errno, $errstr, 2);
92 @fwrite($sock, "?POLL;\n");
93 for($tries = 0; $tries < 10; $tries++){
94 $resp = @fread($sock, 1536); # SKY can be pretty big
95 if (preg_match('/{"class":"POLL".+}/i', $resp, $m)){
102 $resp = '{"class":"ERROR","message":"no response from GPS daemon"}';
108 else if ($op == 'json')
115 ###########################################################################
116 function colorsetup($im){
117 $C['white'] = imageColorAllocate($im, 255, 255, 255);
118 $C['ltgray'] = imageColorAllocate($im, 191, 191, 191);
119 $C['mdgray'] = imageColorAllocate($im, 127, 127, 127);
120 $C['dkgray'] = imageColorAllocate($im, 63, 63, 63);
121 $C['black'] = imageColorAllocate($im, 0, 0, 0);
122 $C['red'] = imageColorAllocate($im, 255, 0, 0);
123 $C['brightgreen'] = imageColorAllocate($im, 0, 255, 0);
124 $C['darkgreen'] = imageColorAllocate($im, 0, 192, 0);
125 $C['blue'] = imageColorAllocate($im, 0, 0, 255);
126 $C['cyan'] = imageColorAllocate($im, 0, 255, 255);
127 $C['magenta'] = imageColorAllocate($im, 255, 0, 255);
128 $C['yellow'] = imageColorAllocate($im, 255, 255, 0);
129 $C['orange'] = imageColorAllocate($im, 255, 128, 0);
134 function legend($im, $sz, $C){
137 $x = $sz - (4*$r+7) - 2;
140 imageFilledRectangle($im, $x, $y, $x + 4*$r + 7, $y + $r +1, $C['dkgray']);
141 imageRectangle($im, $x+0*$r+1, $y+1, $x + 1*$r + 0, $y + $r, $C['red']);
142 imageRectangle($im, $x+1*$r+2, $y+1, $x + 2*$r + 2, $y + $r, $C['yellow']);
143 imageRectangle($im, $x+2*$r+4, $y+1, $x + 3*$r + 4, $y + $r, $C['darkgreen']);
144 imageRectangle($im, $x+4*$r+6, $y+1, $x + 3*$r + 6, $y + $r, $C['brightgreen']);
145 imageString($im, $fn, $x+3+0*$r, $y+$r/3, "<30", $C['red']);
146 imageString($im, $fn, $x+5+1*$r, $y+$r/3, "30+", $C['yellow']);
147 imageString($im, $fn, $x+7+2*$r, $y+$r/3, "35+", $C['darkgreen']);
148 imageString($im, $fn, $x+9+3*$r, $y+$r/3, "40+", $C['brightgreen']);
151 function radial($angle, $sz){
153 $angle = deg2rad($angle);
155 # determine length of radius
156 $r = $sz * 0.5 * 0.95;
158 # and convert length/azimuth to cartesian
159 $x0 = sprintf("%d", (($sz * 0.5) - ($r * cos($angle))));
160 $y0 = sprintf("%d", (($sz * 0.5) - ($r * sin($angle))));
161 $x1 = sprintf("%d", (($sz * 0.5) + ($r * cos($angle))));
162 $y1 = sprintf("%d", (($sz * 0.5) + ($r * sin($angle))));
164 return array($x0, $y0, $x1, $y1);
167 function azel2xy($az, $el, $sz){
169 #rotate coords... 90deg W = 180deg trig
175 # determine length of radius
176 $r = $sz * 0.5 * 0.95;
177 $r -= ($r * ($el/90));
179 # and convert length/azimuth to cartesian
180 $x = sprintf("%d", (($sz * 0.5) + ($r * cos($az))));
181 $y = sprintf("%d", (($sz * 0.5) + ($r * sin($az))));
185 return array($x, $y);
188 function splot($im, $sz, $C, $e){
189 if ((0 == $e['PRN']) || (0 == $e['az'] + $e['el'] + $e['ss']) ||
190 ($e['az'] < 0) || ($e['el'] < 0))
193 $color = $C['brightgreen'];
195 $color = $C['darkgreen'];
197 $color = $C['yellow'];
203 $color = $C['black'];
205 list($x, $y) = azel2xy($e['az'], $e['el'], $sz);
208 if (isset($_GET['sz']) && ($_GET['sz'] == 'small'))
211 imageString($im, 3, $x+4, $y+4, $e['PRN'], $C['black']);
212 if ($e['used'] == true)
214 imageFilledDiamond($im, $x, $y, $r, $color);
216 imageFilledArc($im, $x, $y, $r, $r, 0, 360, $color, 0);
219 imageDiamond($im, $x, $y, $r, $color);
221 imageArc($im, $x, $y, $r, $r, 0, 360, $color);
224 function imageDiamond($im, $x, $y, $r, $color){
226 # this lunacy is because imagesetthickness doesn't seem to work
227 $vx = array ( $x+$t, $y, $x, $y+$t, $x-$t, $y, $x, $y-$t );
228 imagepolygon($im, $vx, 4, $color);
230 $vx = array ( $x+$t, $y, $x, $y+$t, $x-$t, $y, $x, $y-$t );
231 imagepolygon($im, $vx, 4, $color);
233 $vx = array ( $x+$t, $y, $x, $y+$t, $x-$t, $y, $x, $y-$t );
234 imagepolygon($im, $vx, 4, $color);
237 function imageFilledDiamond($im, $x, $y, $r, $color){
240 $vx = array ( $x+$t, $y, $x, $y+$t, $x-$t, $y, $x, $y-$t );
241 imagepolygon($im, $vx, 4, $color);
246 function elevation($im, $sz, $C, $a){
248 $a = $sz * 0.95 * ($a/180);
249 imageArc($im, $sz/2, $sz/2, $a*2, $a*2, 0, 360, $C['ltgray']);
252 imageString($im, 2, $x, $y, $b, $C['ltgray']);
255 function skyview($im, $sz, $C){
257 $a = 90; $a = $sz * 0.95 * ($a/180);
258 imageFilledArc($im, $sz/2, $sz/2, $a*2, $a*2, 0, 360, $C['mdgray'], 0);
259 imageArc($im, $sz/2, $sz/2, $a*2, $a*2, 0, 360, $C['black']);
260 $x = $sz/2 - 16; $y = $sz/2 - $a;
261 imageString($im, 2, $x, $y, "0", $C['ltgray']);
263 $a = 85; $a = $sz * 0.95 * ($a/180);
264 imageFilledArc($im, $sz/2, $sz/2, $a*2, $a*2, 0, 360, $C['white'], 0);
265 imageArc($im, $sz/2, $sz/2, $a*2, $a*2, 0, 360, $C['ltgray']);
266 imageString($im, 1, $sz/2 - 6, $sz+$a, '5', $C['black']);
267 $x = $sz/2 - 16; $y = $sz/2 - $a;
268 imageString($im, 2, $x, $y, "5", $C['ltgray']);
270 for($i = 0; $i < 180; $i += 15){
271 list($x0, $y0, $x1, $y1) = radial($i, $sz);
272 imageLine($im, $x0, $y0, $x1, $y1, $C['ltgray']);
275 for($i = 15; $i < 90; $i += 15)
276 elevation($im, $sz, $C, $i);
278 $x = $sz/2 - 16; $y = $sz/2 - 8;
279 /* imageString($im, 2, $x, $y, "90", $C['ltgray']); */
281 imageString($im, 4, $sz/2 + 4, 2 , 'N', $C['black']);
282 imageString($im, 4, $sz/2 + 4, $sz - 16 , 'S', $C['black']);
284 imageString($im, 4, 4 , $sz/2 + 4, 'E', $C['black']);
285 imageString($im, 4, $sz - 10 , $sz/2 + 4, 'W', $C['black']);
287 imageString($im, 4, 4 , $sz/2 + 4, 'W', $C['black']);
288 imageString($im, 4, $sz - 10 , $sz/2 + 4, 'E', $C['black']);
292 function gen_image($resp){
294 if (isset($_GET['sz']) && ($_GET['sz'] == 'small'))
297 $GPS = json_decode($resp, true);
298 if ($GPS['class'] != "POLL"){
299 die("json_decode error: $resp");
302 $im = imageCreate($sz, $sz);
303 $C = colorsetup($im);
304 skyview($im, $sz, $C);
306 legend($im, $sz, $C);
308 for($i = 0; $i < count($GPS['skyviews'][0]['satellites']); $i++){
309 splot($im, $sz, $C, $GPS['skyviews'][0]['satellites'][$i]);
312 header("Content-type: image/png");
317 function dfix($x, $y, $z){
319 $x = sprintf("%f %s", -1 * $x, $z);
321 $x = sprintf("%f %s", $x, $y);
326 function write_html($resp){
327 global $sock, $errstr, $errno, $server, $port, $head, $body, $open;
328 global $blurb, $title, $autorefresh, $googlemap, $gmap_key, $footer;
329 global $testmode, $advertise;
331 $GPS = json_decode($resp, true);
332 if ($GPS['class'] != 'POLL'){
333 die("json_decode error: $resp");
336 header("Content-type: text/html; charset=UTF-8");
339 $lat = (float)$GPS['fixes'][0]['lat'];
340 $lon = (float)$GPS['fixes'][0]['lon'];
341 $x = $server; $y = $port;
342 $imgdata = base64_encode($resp);
343 $server = $x; $port = $y;
345 if ($autorefresh > 0)
346 $autorefresh = "<meta http-equiv='Refresh' content='$autorefresh'/>";
350 $gmap_head = $gmap_body = $gmap_code = '';
352 $gmap_head = gen_gmap_head();
353 $gmap_body = 'onload="Load()" onunload="GUnload()"';
354 $gmap_code = gen_gmap_code();
357 <?xml version="1.0" encoding="UTF-8"?>
358 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
359 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
360 <html xmlns="http://www.w3.org/1999/xhtml">
364 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
365 <meta http-equiv="Content-Language" content="en,en-us"/>
366 <title>{$title} - GPSD Test Station {$lat}, {$lon}</title>
374 font-family: courier, fixed;
379 margin: 1ex 3em 1ex 3em; /* top right bottom left */
384 font-family: verdana, sans-serif;
389 <body {$body} {$gmap_body}>
392 <tr><td align="justify">
397 if (!strlen($advertise))
398 $advertise = $server;
400 if ($testmode && !$sock)
404 <!-- ------------------------------------------------------------ -->
406 <td rowspan="4" align="center" valign="top">
407 <img src="?op=view&imgdata={$imgdata}"
408 width="600" height="600"/>
410 <p class="caption">A filled circle means the satellite was used in
411 the last fix. Green-yellow-red colors indicate signal strength in dB,
412 green=most and red=least. Diamonds indicate SBAS satellites.</p>
421 <!-- ------------------------------------------------------------ -->
423 <tr><td align="justify">To get real-time information, connect to
424 <span class="fixed">telnet://{$advertise}:{$port}/</span> and type "?POLL;"
425 or "?WATCH={"enable":true,"raw":true}".<br/>
426 Use a different server:<br/>
427 <form method=GET action="${_SERVER['SCRIPT_NAME']}">
428 <input name="host" value="{$advertise}">:
429 <input name="port" value="{$port}" size="5" maxlength="5">
430 <input type=submit value="Get Position"><input type=reset></form>
436 if ($testmode && !$sock)
437 $part4 = "<tr><td><font color='red'>The gpsd instance that this page monitors is not running.</font></td></tr>";
439 $nsv = count($GPS['skyviews'][0]['satellites']);
440 $ts = gmdate("r", $GPS['fixes'][0]['time']);
442 <!-- ------------------------------------------------------------ -->
443 <tr><td align=center valign=top>
445 <tr><td colspan=2 align=center><b>Current Information</b></td></tr>
446 <tr><td>Time (UTC)</td><td>{$ts}</td></tr>
447 <tr><td>Latitude</td><td>{$GPS['fixes'][0]['lat']}</td></tr>
448 <tr><td>Longitude</td><td>{$GPS['fixes'][0]['lon']}</td></tr>
449 <tr><td>Altitude</td><td>{$GPS['fixes'][0]['alt']}</td></tr>
450 <tr><td>Fix Type</td><td>{$GPS['fixes'][0]['mode']}</td></tr>
451 <tr><td>Satellites</td><td>{$nsv}</td></tr>
452 <tr><td>HDOP</td><td>{$GPS['skyviews'][0]['hdop']}</td></tr>
468 <span class="administrivia">This script is distributed by the <a href="http://gpsd.berlios.de">GPSD project</a>.</span><br/>
474 print $part1 . $part2 . $part3 . $part4 . $part5;
478 function write_json($resp){
479 header('Content-Type: text/javascript');
480 if (isset($_GET['jsonp']))
481 print "{$_GET['jsonp']}({$resp})";
486 function write_config(){
487 $f = fopen("gpsd_config.inc", "a");
489 die("can't generate prototype config file. try running this script as root in DOCUMENT_ROOT");
493 \$title = 'My GPS Server';
494 \$server = 'localhost';
495 #\$advertise = 'localhost';
497 \$autorefresh = 0; # number of seconds after which to refresh
498 \$googlemap = 0; # set to 1 if you want to have a google map
499 \$gmap_key = 'GetYourOwnGoogleKey'; # your google API key goes here
500 \$swap_ew = 0; # set to 1 if you don't understand projections
501 \$open = 0; # set to 1 to show the form to change the GPSd server
503 ## You can read the header, footer and blurb from a file...
504 # \$head = file_get_contents('/path/to/header.inc');
505 # \$body = file_get_contents('/path/to/body.inc');
506 # \$footer = file_get_contents('/path/to/footer.hinc');
507 # \$blurb = file_get_contents('/path/to/blurb.inc');
509 ## ... or you can just define them here
515 <a href="http://gpsd.berlios.de">gpsd</a>
516 server <blink><font color="red">located someplace</font></blink>.
519 <blink><font color="red">hardware description and link</font></blink>.
521 This machine is maintained by
522 <a href="mailto:you@example.com">Your Name Goes Here</a>.<br/>
532 function gen_gmap_head() {
535 <script src="http://maps.google.com/maps?file=api&v=2&key={$gmap_key}" type="text/javascript"></script>
536 <script type="text/javascript">
538 // Create a base icon for all of our markers that specifies the shadow, icon
541 if (GBrowserIsCompatible()) {
542 var map = new GMap2(document.getElementById("map"));
543 var point = new GLatLng( {$GLOBALS['lat']}, {$GLOBALS['lon']} );
544 map.setCenter( point, 14);
545 map.addControl(new GLargeMapControl());
546 map.addControl(new GMapTypeControl());
548 var baseIcon = new GIcon();
549 baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
550 baseIcon.iconSize = new GSize(20, 34);
551 baseIcon.shadowSize = new GSize(37, 34);
552 baseIcon.iconAnchor = new GPoint(9, 34);
553 baseIcon.infoWindowAnchor = new GPoint(9, 2);
554 baseIcon.infoShadowAnchor = new GPoint(18, 25);
556 var icon = new GIcon(baseIcon);
557 icon.image = "http://www.google.com/mapfiles/marker.png";
558 var marker = new GMarker(point, icon);
559 map.addOverlay(marker);
568 function gen_gmap_code() {
571 <div id="map" style="width: 550px; height: 400px; border:1px; border-style: solid;">
574 <span class='warning'>Sorry: you must enable javascript to view our maps.</span><br/>