Skip to content

Commit

Permalink
add lock to update-location-gps lua - improve loop handling
Browse files Browse the repository at this point in the history
  • Loading branch information
maurerle committed Aug 8, 2024
1 parent 607fe94 commit 1f0018d
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -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

This file was deleted.

41 changes: 35 additions & 6 deletions ffac-update-location-gps/luasrc/usr/bin/update-location-gps
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1f0018d

Please sign in to comment.