ublox-gnss-config: split from gpsd layer, renamed, improved

Changes to baud rate config script:
+ more appropriate name
+ only one service
+ baud rate detector is faster in special case
+ shellcheck conform
- no longer creates default config file if file is missing, instead
throws error

BugzID: 60698

Signed-off-by: Tobias Jäggi <tobias.jaeggi@netmodule.com>
This commit is contained in:
Tobias Jäggi 2020-02-24 14:43:57 +01:00
parent 2778373994
commit 8c440ef470
10 changed files with 85 additions and 130 deletions

View File

@ -1,32 +0,0 @@
# Copyright (C) 2020 Tobias Jäggi <tobias.jaeggi@netmodule.com>
# Released under the MIT license (see COPYING.MIT for the terms)
DESCRIPTION = "ttyS3 config service"
HOMEPAGE = "www.netmodule.com"
LICENSE = "MIT"
SECTION = "bsp/firmware"
LIC_FILES_CHKSUM = "file://${COMMON_LICENSE_DIR}/MIT;md5=0835ade698e0bcf8506ecda2f7b4f302"
inherit systemd
SRC_URI = " \
file://config-gnss-tty.service \
file://config-gnss-tty.sh \
"
S = "${WORKDIR}"
SYSTEMD_SERVICE_${PN} = " \
config-gnss-tty.service \
"
FILES_${PN} = "${systemd_unitdir}/system ${bindir}"
do_install() {
install -d ${D}${systemd_unitdir}/system
install -m 644 ${WORKDIR}/config-gnss-tty.service ${D}${systemd_unitdir}/system/
install -d ${D}${bindir}
install -m 744 ${WORKDIR}/config-gnss-tty.sh ${D}${bindir}/config-gnss-tty
}

View File

@ -1,12 +0,0 @@
[Unit]
Description=config baud rate on NEO-M8L
Before=config-gnss-tty.service
[Service]
Type=oneshot
ExecStart=/usr/bin/config-gnss-receiver
RemainAfterExit=no
[Install]
WantedBy=multi-user.target

View File

@ -1,12 +0,0 @@
[Unit]
Description=config baud rate of tty used by gnss receiver
Before=gpsd.service
[Service]
Type=oneshot
ExecStart=/usr/bin/config-gnss-tty
RemainAfterExit=no
[Install]
WantedBy=multi-user.target

View File

@ -1,33 +0,0 @@
#!/bin/sh
# exit codes:
# 0: success
# 1: $CONFIG_FILE is missing (should have been created by config-gnss-receiver)
# 2: failed to set baud rate of $TTY_INTERFACE to $DESIRED_BAUD_RATE
TTY_INTERFACE="/dev/ttyS3"
CONFIG_FILE="/etc/gnss.conf"
# check if CONFIG_FILE exists
if ! test -f "$CONFIG_FILE"; then
# CONFIG_FILE doesn't exist
printf "config file missing\n"
exit 1 # $CONFIG_FILE is missing (should have been created by config-gnss-receiver)
fi
# read $DESIRED_BAUD_RATE specified in $CONFIG_FILE
read -r DESIRED_BAUD_RATE <<< "$(awk '/br=(\d*)/{print $0}' < $CONFIG_FILE)"
read -r DESIRED_BAUD_RATE <<< "$(echo "$DESIRED_BAUD_RATE" | cut -d'=' -f 2)"
# set baud rate
stty -F "$TTY_INTERFACE" speed "$DESIRED_BAUD_RATE" > /dev/null
# verify baud rate
read -r TTYS3_BAUD_RATE <<< "$(stty -F "$TTY_INTERFACE" | awk '/speed .* baud;/{print $2}')"
if [ "$TTYS3_BAUD_RATE" == "$DESIRED_BAUD_RATE" ];then
printf "successfully set baud rate of %s to %s baud\n" "$TTY_INTERFACE" "$DESIRED_BAUD_RATE"
exit 0 # success
else
printf "failed to set baud rate of %s to %s\n" "$TTY_INTERFACE" "$DESIRED_BAUD_RATE"
exit 2
fi

View File

@ -0,0 +1,12 @@
[Unit]
Description=config baud rate on NEO-M8L and relevant tty interface
Before=gpsd.service
[Service]
Type=oneshot
ExecStart=/usr/bin/ublox-gnss-config
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target

View File

@ -1,4 +1,4 @@
#!/bin/sh
#!/bin/bash
# This script tries to figure out the baud rate of $DEVICE_NAME connected to $TTY_INTERFACE.
# If the script successfully determines the baud rate of the receiver,
@ -7,19 +7,19 @@
# exit codes:
# 0 success
# 1 unable to detect baud rate
# 2 set baud rate was not successful
# 3 baud rate not set to $DESIRED_BAUD_RATE or verifiying baud rate failed
# 1 unable to detect baud rate of receiver
# 2 config file missing
# 3 reciever baud rate not set to $DESIRED_BAUD_RATE or verifiying baud rate failed
# 4 saving to flash failed
# 5 baud rate specified in $CONFIG_FILE not supported by $DEVICE_NAME
# 6 set tty baud rate was not successful
# static variables
DEVICE_NAME="NEO-M8L"
TTY_INTERFACE="/dev/ttyS3"
CONFIG_FILE="/etc/gnss.conf"
SAVE_FILE="/var/gnss-receiver-save-state"
# if the config file is missing, a new one gets created with $DEFAULT_BAUD_RATE baud
DEFAULT_BAUD_RATE=115200
# first baud rate in PRESELECTION_BAUD_RATES should be the one which is most likely the current baud rate of the GNSS receiver,
# the last one should be the one least likely used by the reciever.
# This can reduce the runtime of this script
@ -82,8 +82,8 @@ detect_baud_rate () {
local _polled_baud_rate
for br in "${_baud_rates[@]}";
do
printf "testing %s baud\n" "$br"
for i in $(eval echo {1.."$_allowed_retries"})
printf "testing %s baud\\n" "$br"
for i in $(eval "echo {1..$_allowed_retries}")
do
_polled_baud_rate="$(poll_baud_rate_receiver "$br")"
if [ "$_polled_baud_rate" == "0" ];then
@ -95,10 +95,10 @@ detect_baud_rate () {
return 0
fi
done
printf "no luck so far\n"
printf "no luck so far\\n"
done
# failed to detect baud rate
printf "failed to detect baud rate\n"
printf "failed to detect baud rate\\n"
return 1
}
@ -115,6 +115,18 @@ save_receiver_config () {
return 1
}
set_baud_rate_tty () {
# set baud rate of $TTY_INTERFACE
stty -F "$TTY_INTERFACE" speed "$DESIRED_BAUD_RATE" > /dev/null
# verify baud rate
read -r TTYS3_BAUD_RATE <<< "$(stty -F "$TTY_INTERFACE" | awk '/speed .* baud;/{print $2}')"
if [ "$TTYS3_BAUD_RATE" == "$DESIRED_BAUD_RATE" ];then
return 0
else
# baud rate configured on $TTY_INTERFACE is not matching baud rate specified in $CONFIG_FILE
return 1
fi
}
# start of main
# check if script was launched by systemd
@ -127,24 +139,26 @@ else
RESTART_GPSD=0
fi
# TODO: remove this hack in favour of a proper python environment
# hack to get pyserial working
PYTHONPATH=/usr/lib/python3.7/site-packages/ && export PYTHONPATH
# TODO: parse input args to display help which would explain usage of $CONFIG_FILE
# check if CONFIG_FILE exists
if ! test -f "$CONFIG_FILE"; then
# CONFIG_FILE doesn't exist
printf "config file missing\n"
touch $CONFIG_FILE
echo -e "#\n# Define a baud rate to be used by the GNSS receiver\n# This file is part of ublox-gnss-config service\n#\n# Required fields:\n# br: baud rate to be used by the GNSS receiver\n#\n\n\n[default]\nbr=$DEFAULT_BAUD_RATE\n" > $CONFIG_FILE
printf "created default config file %s with %s baud\n" "$CONFIG_FILE" "$DEFAULT_BAUD_RATE"
printf "config file missing\\n"
exit 2
fi
# check if SAVE_FILE exists
if ! test -f "$SAVE_FILE"; then
# SAVE_FILE doesn't exist
printf "save file missing\n"
printf "save file missing\\n"
touch $SAVE_FILE
echo 0 > $SAVE_FILE
printf "created save file %s\n" "$SAVE_FILE"
printf "created save file %s\\n" "$SAVE_FILE"
fi
# read $DESIRED_BAUD_RATE specified in $CONFIG_FILE
@ -161,13 +175,13 @@ do
fi
done
if [ "$baud_rate_supported_flag" == "0" ];then
printf "baud rate specified in %s is not supported by receiver %s\n" "$CONFIG_FILE" "$DEVICE_NAME"
printf "supported baud rates are:\n"
printf "baud rate specified in %s is not supported by receiver %s\\n" "$CONFIG_FILE" "$DEVICE_NAME"
printf "supported baud rates are:\\n"
for j in "${SUPPORTED_BAUD_RATES[@]}"
do
printf "%s " "$j"
done
printf "\n"
printf "\\n"
exit 5 # baud rate specified in $CONFIG_FILE not supported by $DEVICE_NAME
fi
@ -177,20 +191,30 @@ if br_receiver="$(poll_baud_rate_receiver "$DESIRED_BAUD_RATE")";then
if [ "$br_receiver" == "$DESIRED_BAUD_RATE" ];then
# baud rate of receiver is matching baud rate specified in CONFIG_FILE
if [ "$(cat "$SAVE_FILE")" == "1" ];then
printf "baud rate of %s is already at %s baud\n" "$DEVICE_NAME" "$DESIRED_BAUD_RATE"
printf "configuration is saved in flash of %s\n" "$DEVICE_NAME"
printf "baud rate of %s is already at %s baud\\n" "$DEVICE_NAME" "$DESIRED_BAUD_RATE"
printf "configuration is saved in flash of %s\\n" "$DEVICE_NAME"
if set_baud_rate_tty;then
printf "set baud rate of %s to %s baud\\n" "$TTY_INTERFACE" "$DESIRED_BAUD_RATE"
else
printf "failed to set baud rate of %s to %s\\n" "$TTY_INTERFACE" "$DESIRED_BAUD_RATE"
exit 6
fi
# script can exit
exit 0 # baud rate of receiver is already matching $DESIRED_BAUD_RATE
else
# TODO: this could be way faster by telling the detector which baud rate to use since we already know it
printf "baud rate of %s is already at %s baud\n" "$DEVICE_NAME" "$DESIRED_BAUD_RATE"
printf "configuration is not saved in flash of %s\n" "$DEVICE_NAME"
printf "baud rate of %s is already at %s baud\\n" "$DEVICE_NAME" "$DESIRED_BAUD_RATE"
printf "configuration is not saved in flash of %s\\n" "$DEVICE_NAME"
# hint for the detector
PRESELECTION_BAUD_RATES=("$br_receiver")
fi
else
printf "baud rates are not matching\n"
printf "baud rates are not matching\\n"
fi
else
printf "read failed\n"
printf "read failed\\n"
fi
# determine current baud rate of receiver
@ -198,20 +222,20 @@ args=("2" "${PRESELECTION_BAUD_RATES[@]}")
detect_baud_rate "${args[@]}"
if [ "$?" == "1" ];then
# retry with more baud rates and more tries per baud rate
printf "retrying\n"
printf "retrying\\n"
# maybe a time out increase would also work when dealing with lower baud rates?
args=("$MAX_ALLOWED_RETRIES" "${SUPPORTED_BAUD_RATES[@]}")
detect_baud_rate "${args[@]}"
if [ "$?" == "1" ];then
printf "baud rate not detected, even after retry\n"
printf "baud rate not detected, even after retry\\n"
exit 1 # unable to detect baud rate
fi
fi
printf "detected baud rate of %s is %s baud\n" "$DEVICE_NAME" "$DETECTED_BAUD_RATE"
printf "detected baud rate of %s is %s baud\\n" "$DEVICE_NAME" "$DETECTED_BAUD_RATE"
# upgrade to $DESIRED_BAUD_RATE baud
new_baud_rate_set_flag=0
for i in $(eval echo {0.."$MAX_ALLOWED_RETRIES"})
for i in $(eval "echo {0..$MAX_ALLOWED_RETRIES}")
do
set_baud_rate_receiver "$DETECTED_BAUD_RATE" "$DESIRED_BAUD_RATE"
@ -231,15 +255,15 @@ do
done
if [ "$new_baud_rate_set_flag" == "0" ];then
printf "baud rate of %s is not set to %s baud or unable to verify, retried %s times\n" "$DEVICE_NAME" "$DESIRED_BAUD_RATE" "$i"
printf "baud rate of %s is not set to %s baud or unable to verify, retried %s times\\n" "$DEVICE_NAME" "$DESIRED_BAUD_RATE" "$i"
exit 3 # baud rate not set to $DESIRED_BAUD_RATE or verifiying baud rate failed
else
printf "baud rate of %s is set to desired %s baud, it took %s retries\n" "$DEVICE_NAME" "$DESIRED_BAUD_RATE" "$i"
printf "baud rate of %s is set to desired %s baud, it took %s retries\\n" "$DEVICE_NAME" "$DESIRED_BAUD_RATE" "$i"
fi
# save config to flash
config_saved_flag=0
for i in $(eval echo {0.."$MAX_ALLOWED_RETRIES"})
for i in $(eval "echo {0..$MAX_ALLOWED_RETRIES}")
do
if save_receiver_config;then
config_saved_flag=1
@ -251,15 +275,21 @@ do
done
if [ "$config_saved_flag" == "0" ];then
printf "saving to flash was not successful, retried %s times\n" "$i"
printf "saving to flash was not successful, retried %s times\\n" "$i"
exit 4 # saving to flash failed
else
printf "saving to flash was successful, it took %s retries\n" "$i"
printf "saving to flash was successful, it took %s retries\\n" "$i"
fi
if set_baud_rate_tty;then
printf "set baud rate of %s to %s baud\\n" "$TTY_INTERFACE" "$DESIRED_BAUD_RATE"
else
printf "failed to set baud rate of %s to %s\\n" "$TTY_INTERFACE" "$DESIRED_BAUD_RATE"
exit 6
fi
# TODO: verify or error handling
if [ "$RESTART_GPSD" -eq "1" ]; then
config-gnss-tty # call config-gnss-tty to set baud rate of ttyS3 to the baud rate specified in $CONFIG_FILE before launching gpsd
systemctl start gpsd
fi

View File

@ -9,8 +9,8 @@ LIC_FILES_CHKSUM = "file://${COMMON_LICENSE_DIR}/MIT;md5=0835ade698e0bcf8506ecda
inherit systemd
SRC_URI = " \
file://config-gnss-receiver.service \
file://config-gnss-receiver.sh \
file://ublox-gnss-config.service \
file://ublox-gnss-config.sh \
file://gnss.conf \
file://gnss-receiver-save-state \
"
@ -18,17 +18,17 @@ SRC_URI = " \
S = "${WORKDIR}"
SYSTEMD_SERVICE_${PN} = " \
config-gnss-receiver.service \
ublox-gnss-config.service \
"
FILES_${PN} = "${systemd_unitdir}/system ${bindir} ${localstatedir} ${sysconfdir}"
do_install() {
install -d ${D}${systemd_unitdir}/system
install -m 644 ${WORKDIR}/config-gnss-receiver.service ${D}${systemd_unitdir}/system/
install -m 644 ${WORKDIR}/ublox-gnss-config.service ${D}${systemd_unitdir}/system/
install -d ${D}${bindir}
install -m 744 ${WORKDIR}/config-gnss-receiver.sh ${D}${bindir}/config-gnss-receiver
install -m 744 ${WORKDIR}/ublox-gnss-config.sh ${D}${bindir}/ublox-gnss-config
install -d ${D}${sysconfdir}
install -m 644 ${WORKDIR}/gnss.conf ${D}${sysconfdir}/gnss.conf
@ -37,4 +37,6 @@ do_install() {
install -m 644 ${WORKDIR}/gnss-receiver-save-state ${D}${localstatedir}/gnss-receiver-save-state
}
RDEPENDS_${PN} = "gpsd"
RDEPENDS_${PN} = "gpsd \
bash \
"