summaryrefslogtreecommitdiff
path: root/recipes-navigation/gpsd/gpsd/gpsd_ubx_settime.sh
blob: f74e7b0fa9bcdd26bd386b19be824ebb03ac0e6c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/bin/bash
# PPS= 1484247472.29561104 clock= 1484247990.00000000 offset= 517.704388959
# DEBUG=1 to set debug
NAME=gpsd_ubx_settime
shopt -s expand_aliases
alias LG="logger -t $NAME"
function kill_it
{
    cmd=$1
    pid=$2
    sig=$3
    ((DEBUG)) && LG -p user.info "terminating $cmd(${pid}) with SIG${sig}"
    kill -SIG${sig} ${pid}
    result=$?
    ((DEBUG)) && LG -p user.info "kill -SIG${sig} ${pid} result is $result"
}        

trap "stty echo icanon" exit

OIFS=$IFS
IFS=$'\n'
if ! /usr/sbin/gpsd_ubx_fixed </dev/null ; then
   LG -s -p user.warn "GPS does not have a fix yet.  Try again later."
   exit 1
fi


# Terminate gpsmon after 10 seconds if it is our child
# gpsmon has issues with terminals and stalling on terminal
# read of the console.
(
  sleep 10
  ppid=$BASHPID
  pgid=$(ps --no-heading -o pgid -p $ppid)
  
  if ((${#pgid} == 0)) ; then
    LG -p user.err "Could not find a pgid for $ppid"
    ps -fjp $ppid | LG -p user.error
  fi

  gpsmonpid=$(pgrep -lg $pgid | egrep '[[:space:]]gpsmon$' | sed 's/[[:space:]].*//')
  ((DEBUG)) && LG -p user.info "$pgid is pgid $gpsmonpid is gpsmonpid"
  # Find the gpsmonpid that is our grandchild
  ((didkill=0))
  
  # for loop is in case gpsmonpid has children.
  for p in ${gpsmonpid} ; do
    if ((${#p})) ; then
        kill_it gpsmon $p TERM
        ((didkill=p))
        break
    fi
  done
  if ((didkill == 0)) ; then
    exit 0
  fi
  # Do a sigkill to be sure.
  sleep 2
  kill_it gpsmon $didkill KILL
) &
                    
epoch=$(gpsmon -a 2>&1 | ( while read ln ; do
	if [[ $ln =~ ^[[:space:]]*PPS=.*clock=[[:space:]]*([0-9]*)\. ]] ; then
		echo ${BASH_REMATCH[1]}
		break
	fi
done ))
if [[ $epoch =~ ^[0-9]+$ ]] ; then
  LG -p user.warn "Updating time to epoch time, ${epoch}."
  date +%s -s @${epoch} >/dev/null
else
  LG -p user.err "gpsmon output is bad."
  exit 1
fi
exit 0