diff --git a/ffac-update-location-gps/files/etc/hotplug.d/tty/10-update-location-gps b/ffac-update-location-gps/files/etc/hotplug.d/tty/10-update-location-gps new file mode 100644 index 00000000..3fd3c086 --- /dev/null +++ b/ffac-update-location-gps/files/etc/hotplug.d/tty/10-update-location-gps @@ -0,0 +1,24 @@ +#!/bin/sh +TTYPATH="/sys${DEVPATH}/tty" +lockpath="/var/lock/update-location-gps_${DEVNAME}.lock" + +if [ "${ACTION}" = "add" ]; then + enabled=$(uci get update-location-gps.settings.enabled) + [ "${enabled}" != "1" ] && exit 0 + test -e "${lockpath}" && exit 0 + if test -e "${TTYPATH}"; then + echo "hotplug-update-location-gps: TTY device ${DEVNAME} was plugged in" > /dev/kmsg + echo "${DEVPATH}" > "${lockpath}" + echo "*/5 * * * * /usr/bin/update-location-gps /dev/${DEVNAME} | logger -t update-location-gps" > "/usr/lib/micron.d/update-location-gps_${DEVNAME}" + /etc/init.d/micrond restart + fi +fi + +if [ "${ACTION}" = "remove" ]; then + if [ "${DEVPATH}" = "$(cat "${lockpath}")" ]; then + echo "hotplug-update-location-gps: TTY device ${DEVNAME} was removed" > /dev/kmsg + rm -f "/usr/lib/micron.d/update-location-gps_${DEVNAME}" + /etc/init.d/micrond restart + rm "${lockpath}" + fi +fi diff --git a/ffac-update-location-gps/files/etc/hotplug.d/usb/10-update-location-gps b/ffac-update-location-gps/files/etc/hotplug.d/usb/10-update-location-gps deleted file mode 100644 index 109e4315..00000000 --- a/ffac-update-location-gps/files/etc/hotplug.d/usb/10-update-location-gps +++ /dev/null @@ -1,26 +0,0 @@ -#!/bin/sh -TTYPATH="/sys${DEVPATH}/tty" -LOCKPATH="/tmp/update-location-gps" - -ENABLED=$(uci get update-location-gps.settings.enabled) - -if [ "${ACTION}" = "bind" ]; then - [ "${ENABLED}" != "1" ] && exit 0 - test -e "${LOCKPATH}" && exit 0 - if test -e "${TTYPATH}"; then - TTY_NAME=$(find "${TTYPATH}" -mindepth 1 -maxdepth 1 -type d 2>/dev/null | sed -n 's|.*/tty/||p') - logger -t hotplug "USB GPS device was plugged in" - echo "${DEVPATH}" > "${LOCKPATH}" - echo "*/5 * * * * /usr/bin/update-location-gps /dev/${TTY_NAME} | logger -t update-location-gps" > "/usr/lib/micron.d/update-location-gps" - /etc/init.d/micrond restart - fi -fi - -if [ "${ACTION}" = "unbind" ]; then - if [ "${DEVPATH}" = "$(cat ${LOCKPATH})" ]; then - logger -t hotplug "USB GPS device was removed" - rm -f "/usr/lib/micron.d/update-location-gps" - /etc/init.d/micrond restart - rm "${LOCKPATH}" - fi -fi \ No newline at end of file diff --git a/ffac-update-location-gps/luasrc/usr/bin/update-location-gps b/ffac-update-location-gps/luasrc/usr/bin/update-location-gps index cd1ecf0a..03c2f987 100755 --- a/ffac-update-location-gps/luasrc/usr/bin/update-location-gps +++ b/ffac-update-location-gps/luasrc/usr/bin/update-location-gps @@ -5,22 +5,51 @@ if not arg[1] then os.exit(1) end --- Get GPS device tty -local TTY_DEVICE = arg[1] - local uci = require('simple-uci').cursor() +local bit = require 'bit' +local fcntl = require 'posix.fcntl' +local unistd = require 'posix.unistd' + +-- Get GPS device tty +local tty_device = arg[1] -- Use GPS as Stream -local file = io.open(TTY_DEVICE, "r") +local file = io.open(tty_device, "r") if not file then - print("Error: Unable to open " .. TTY_DEVICE) + print("Error: Unable to open " .. tty_device) os.exit(2) end -while true do +local lockfilename = "/var/lock/update-location-gps_" .. string.gsub(tty_device, "/", "_") + +local lockfd, err = fcntl.open(lockfilename, bit.bor(fcntl.O_WRONLY, fcntl.O_CREAT), 384) -- mode 0600 +if not lockfd then + print('err', err) + local err_verbose = string.format("Unable to get file descriptor for lock file %s .", lockfilename) + print('err', err_verbose) + os.exit(1) +end + +local ok, _ = fcntl.fcntl(lockfd, fcntl.F_SETLK, { + l_start = 0, + l_len = 0, + l_type = fcntl.F_WRLCK, + l_whence = unistd.SEEK_SET, +}) +if not ok then + -- silent as this is run in cron + os.exit(1) +end + +local line_count = 0 +local max_lines = 50 + +while line_count < max_lines do local this_line = file:read("*line") if not this_line then break end -- Exit loop if no more lines + line_count = line_count + 1 -- Increment the line counter + local nc = this_line:match("^([^,]+)") if nc == '$GPRMC' then