Skip to content

Instantly share code, notes, and snippets.

@bonds bonds/gps_on
Created Aug 11, 2017

Embed
What would you like to do?
script to run gpsd using a Ericsson H5321 on OpenBSD
#!/bin/sh
# This script allows you to use the gps function of a Lenovo H5321 (aka
# Ericsson H5321) mobile broadband module under OpenBSD 6.1.
#
# Requires: gpsd, socat, access to port 2947 on localhost (i.e. pf.conf)
# Usage: gps_on [interval] [tempfile]
#
# Forked from:
#
# https://gist.github.com/worldeater/8049555
# http://wiki.volution.ro/CiprianDorinCraciun/Notes/Public/F5521gw
#
# Further reading:
#
# http://www.thinkwiki.org/wiki/Ericsson_H5321_gw_Mobile_Broadband_Module
# https://opensource.org/licenses/ISC
# http://man.openbsd.org/umb.4
# https://www.foxtrotgps.org/
# http://catb.org/gpsd/gpspipe.html
#
# Copyright 2017 Scott Bonds <scott@ggr.com>
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
# AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
# OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
# PERFORMANCE OF THIS SOFTWARE.
interval="${1:-10}" # update interval in seconds (default: 10)
tempfile="${2:-/var/run/gpsd.data}" # a pty the GPS data is redirected to
# require root
if [[ `id -u` != 0 ]]; then
echo "error: this script must be run by root"
exit 1
fi
# cleanup after ourselves when asked to shutdown
trap shutdown HUP INT TERM
function shutdown {
echo shutting down...
(
exec <>/dev/cuaU1 >&0 && \
chat -s -S -t5 -- \
"*EMRDY: 1" "AT" \
"OK" "AT+CFUN=0" \
"OK" ""
)
exit
}
# set up the card for GPS usage and stream the GPS data to a temporary pty
# NOTE: socat's "echo=0" prevents gpsd from using 100% CPU time
(
exec <>/dev/cuaU1 >&0 && \
chat -s -S -t5 -- \
"*EMRDY: 1" "AT" \
"OK" "AT+CFUN=1" \
"OK" ""
)
(
exec <>/dev/cuaU2 >&0 && \
chat -s -S -t5 -- \
"*EMRDY: 1" "AT" \
"OK" "AT*E2GPSCTL=1,$interval,1" \
"OK" "AT*E2GPSNPD" \
"OK" "" && \
exec socat \
STDIO,echo=0 \
PTY,echo=0,link="$tempfile"
) &
# run gpsd in read-only mode and have it take the GPS data from the pty
gpsd -N -D 2 -F /var/run/gpsd.sock -b "$tempfile"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.