diff options
author | jskyboo <jack@ron.hog.mooo.info> | 2017-01-23 20:04:13 -0800 |
---|---|---|
committer | Renato Botelho <renato@netgate.com> | 2017-01-25 16:43:07 -0200 |
commit | c977d6b10f9b639a27ab604fb48bb327c1472468 (patch) | |
tree | 5d888bc0b866849788de640386f83b6b72b88182 | |
parent | d6f1fbac1338c2bbbc544731dc7b812903edcb0a (diff) | |
download | pfsense-c977d6b10f9b639a27ab604fb48bb327c1472468.zip pfsense-c977d6b10f9b639a27ab604fb48bb327c1472468.tar.gz |
Feature #7159 Add GPS initialization command auto correct tool
Corrects malformed NMEA sentences by calculating and appending the checksum and adding missing special characters "$" and "*"
(cherry picked from commit 8c23d92a159c1282e7d185665977f0c45d45f845)
-rw-r--r-- | src/usr/local/www/services_ntpd_gps.php | 47 |
1 files changed, 45 insertions, 2 deletions
diff --git a/src/usr/local/www/services_ntpd_gps.php b/src/usr/local/www/services_ntpd_gps.php index 66bfcf3..b72372a 100644 --- a/src/usr/local/www/services_ntpd_gps.php +++ b/src/usr/local/www/services_ntpd_gps.php @@ -154,6 +154,30 @@ function parse_initcmd(&$nmeaset, $initcmd) { } } +function NMEAChecksum($cmd) { + $checksum = 0; + for ($i=0; $i<strlen($cmd); $i++) { + $checksum = ($checksum ^ ord($cmd[$i])); + } + return strtoupper(str_pad(dechex($checksum), 2, '0', STR_PAD_LEFT)); +} + +function autocorrect_initcmd($initcmd) { + $cmds = ''; + $split_initcmd = preg_split('/[\s]+/', $initcmd); + foreach ($split_initcmd as $line) { + if (!strlen($line)) { + continue; + } + $begin = ($line[0] == '$') ? 1 : 0; + $astpos = strrpos($line, '*'); + $end = ($astpos !== false) ? $astpos : strlen($line); + $trimline = substr($line, $begin, $end-$begin); + $cmds = $cmds . '$' . $trimline . '*' . NMEAChecksum($trimline) . "\r\n"; + } + return $cmds; +} + if ($_POST) { unset($input_errors); @@ -254,9 +278,19 @@ if ($_POST) { unset($config['ntpd']['gps']['extstatus']); } + if (!empty($_POST['autocorrect_initcmd'])) { + $config['ntpd']['gps']['autocorrect_initcmd'] = $_POST['autocorrect_initcmd']; + } elseif (isset($config['ntpd']['gps']['autocorrect_initcmd'])) { + unset($config['ntpd']['gps']['autocorrect_initcmd']); + } + if (!empty($_POST['gpsinitcmd'])) { - $config['ntpd']['gps']['initcmd'] = base64_encode($_POST['gpsinitcmd']); - parse_initcmd($config['ntpd']['gps']['nmeaset'], $_POST['gpsinitcmd']); + $initcmd = $_POST['gpsinitcmd']; + if ($config['ntpd']['gps']['autocorrect_initcmd']) { + $initcmd = autocorrect_initcmd($initcmd); + } + $config['ntpd']['gps']['initcmd'] = base64_encode($initcmd); + parse_initcmd($config['ntpd']['gps']['nmeaset'], $initcmd); } elseif (isset($config['ntpd']['gps']['initcmd'])) { unset($config['ntpd']['gps']['initcmd']); unset($config['ntpd']['gps']['nmeaset']); @@ -475,6 +509,13 @@ $section->addInput(new Form_Textarea( base64_decode($pconfig['initcmd']) ))->setHelp('Commands entered here will be sent to the GPS during initialization. Please read and understand the GPS documentation before making any changes here.'); +$section->addInput(new Form_Checkbox( + 'autocorrect_initcmd', + null, + 'Auto correct malformed initialization commands. (default: checked).', + $pconfig['autocorrect_initcmd'] +))->setHelp('Calculates and appends checksum and missing special characters "$" and "*". Disable if causing errors.'); + $group = new Form_Group('NMEA Checksum Calculator'); $group->add(new Form_Input( @@ -621,6 +662,7 @@ events.push(function() { $('#gpsflag4').prop('checked', false); $('#gpssubsec').prop('checked', false); $('#extstatus').prop('checked', true); + $('#autocorrect_initcmd').prop('checked', true); } // Show advanced GPS options ============================================== @@ -644,6 +686,7 @@ events.push(function() { } hideInput('gpsinitcmd', !showadvgps); + hideInput('autocorrect_initcmd', !showadvgps); hideClass('calculator', !showadvgps); if (showadvgps) { |