Kobo: Always use open/write/close for sysfs writes (#9635)

Also simplifies a few UIManager log messages (re: https://github.com/koreader/koreader-base/pull/1537)
pull/9636/head
NiLuJe 2 years ago committed by GitHub
parent 2a354912f4
commit d9eb6e9717
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1 +1 @@
Subproject commit 4216c40d662660c9bbc48e79893b437e97518254 Subproject commit 37506f4d552e9ccf2f6c0f71afe925c3ffd9abd2

@ -81,8 +81,8 @@ local function writeToSys(val, file)
logger.err("Cannot open file `" .. file .. "`:", ffi.string(C.strerror(ffi.errno()))) logger.err("Cannot open file `" .. file .. "`:", ffi.string(C.strerror(ffi.errno())))
return return
end end
local bytes = #val + 1 -- + LF local bytes = #val
local nw = C.write(fd, val .. "\n", bytes) local nw = C.write(fd, val, bytes)
if nw == -1 then if nw == -1 then
logger.err("Cannot write `" .. val .. "` to file `" .. file .. "`:", ffi.string(C.strerror(ffi.errno()))) logger.err("Cannot write `" .. val .. "` to file `" .. file .. "`:", ffi.string(C.strerror(ffi.errno())))
end end
@ -930,12 +930,13 @@ function Kobo:getFirmwareVersion()
local version_str = version_file:read("*line") local version_str = version_file:read("*line")
version_file:close() version_file:close()
local i = 0 local i = 1
for field in ffiUtil.gsplit(version_str, ",", false, false) do for field in version_str:gmatch("([^,]+)") do
i = i + 1 if i == 3 then
if (i == 3) then self.firmware_rev = field
self.firmware_rev = field break
end end
i = i + 1
end end
end end
@ -1272,13 +1273,11 @@ function Kobo:toggleChargingLED(toggle)
-- (when it does, it's an option in the Energy saving settings), -- (when it does, it's an option in the Energy saving settings),
-- which is why we also limit ourselves to "true" on devices where this was tested. -- which is why we also limit ourselves to "true" on devices where this was tested.
-- c.f., drivers/misc/ntx_misc_light.c -- c.f., drivers/misc/ntx_misc_light.c
local f = io.open(self.ntx_lit_sysfs_knob, "we") local fd = C.open(self.ntx_lit_sysfs_knob, bit.bor(C.O_WRONLY, C.O_CLOEXEC)) -- procfs/sysfs, we shouldn't need O_TRUNC
if not f then if fd == -1 then
logger.err("cannot open", self.ntx_lit_sysfs_knob, "for writing!") logger.err("Cannot open file `" .. self.ntx_lit_sysfs_knob .. "`:", ffi.string(C.strerror(ffi.errno())))
return false return false
end end
-- Relying on LFs is mildly more elegant than spamming f:flush() calls ;).
C.setlinebuf(f)
-- c.f., strace -fittTvyy -e trace=ioctl,file,signal,ipc,desc -s 256 -o /tmp/nickel.log -p $(pidof -s nickel) & -- c.f., strace -fittTvyy -e trace=ioctl,file,signal,ipc,desc -s 256 -o /tmp/nickel.log -p $(pidof -s nickel) &
-- This was observed on a Forma, so I'm mildly hopeful that it's safe on other Mk. 7 devices ;). -- This was observed on a Forma, so I'm mildly hopeful that it's safe on other Mk. 7 devices ;).
@ -1286,17 +1285,20 @@ function Kobo:toggleChargingLED(toggle)
if toggle == true then if toggle == true then
-- NOTE: Technically, Nickel forces a toggle off before that, too. -- NOTE: Technically, Nickel forces a toggle off before that, too.
-- But since we do that on startup, it shouldn't be necessary here... -- But since we do that on startup, it shouldn't be necessary here...
if self.led_uses_channel_3 then for ch = self.led_uses_channel_3 and 3 or 4, 4 do
f:write("ch 3\n", "cur 1\n", "dc 63\n") C.write(fd, "ch " .. tostring(ch), 4)
C.write(fd, "cur 1", 5)
C.write(fd, "dc 63", 5)
end end
f:write("ch 4\n", "cur 1\n", "dc 63\n")
else else
for ch = 3, 5 do for ch = 3, 5 do
f:write("ch " .. tostring(ch) .. "\n", "cur 1\n", "dc 0\n") C.write(fd, "ch " .. tostring(ch), 4)
C.write(fd, "cur 1", 5)
C.write(fd, "dc 0", 4)
end end
end end
f:close() C.close(fd)
end end
-- Return the highest core number -- Return the highest core number
@ -1315,7 +1317,7 @@ end
function Kobo:enableCPUCores(amount) function Kobo:enableCPUCores(amount)
-- CPU0 is *always* online ;). -- CPU0 is *always* online ;).
for n=1, self.cpu_count do for n = 1, self.cpu_count do
local path = "/sys/devices/system/cpu/cpu" .. n .. "/online" local path = "/sys/devices/system/cpu/cpu" .. n .. "/online"
local up local up
if n >= amount then if n >= amount then
@ -1324,11 +1326,7 @@ function Kobo:enableCPUCores(amount)
up = "1" up = "1"
end end
local f = io.open(path, "we") writeToSys(up, path)
if f then
f:write(up)
f:close()
end
end end
end end

@ -5,6 +5,10 @@
local logger = require("logger") local logger = require("logger")
local dbg = require("dbg") local dbg = require("dbg")
local ffi = require("ffi")
local C = ffi.C
require("ffi/posix_h")
local SysfsLight = { local SysfsLight = {
frontlight_white = nil, frontlight_white = nil,
frontlight_red = nil, frontlight_red = nil,
@ -131,19 +135,23 @@ function SysfsLight:_set_light_value(sysfs_directory, value)
self:_write_value(sysfs_directory .. "/brightness", value) self:_write_value(sysfs_directory .. "/brightness", value)
end end
function SysfsLight:_write_value(file, value) function SysfsLight:_write_value(file, val)
local f = io.open(file, "w") local fd = C.open(file, bit.bor(C.O_WRONLY, C.O_CLOEXEC)) -- procfs/sysfs, we shouldn't need O_TRUNC
if not f then if fd == -1 then
logger.err("Could not open file: ", file) logger.err("Cannot open file `" .. file .. "`:", ffi.string(C.strerror(ffi.errno())))
return false return false
end end
local ret, err_msg, err_code = f:write(value) val = tostring(val)
f:close() local bytes = #val
if not ret then local nw = C.write(fd, val, bytes)
logger.err("Write error: ", err_msg, err_code) if nw == -1 then
logger.err("Cannot write `" .. val .. "` to file `" .. file .. "`:", ffi.string(C.strerror(ffi.errno())))
C.close(fd)
return false return false
end end
return true C.close(fd)
-- NOTE: Allows the caller to possibly handle short writes (not that these should ever happen here).
return nw == bytes
end end
return SysfsLight return SysfsLight

@ -556,9 +556,9 @@ function UIManager:setDirty(widget, refreshtype, refreshregion, refreshdither)
self:_refresh(refreshtype, refreshregion, refreshdither) self:_refresh(refreshtype, refreshregion, refreshdither)
if dbg.is_on then if dbg.is_on then
if refreshregion then if refreshregion then
logger.dbg("setDirty", refreshtype, "from widget", widget_name, "w/ region", refreshregion.x, refreshregion.y, refreshregion.w, refreshregion.h, refreshdither and "AND w/ HW dithering" or "") logger.dbg("setDirty", refreshtype, "from widget", widget_name, "w/ region", refreshregion.x, refreshregion.y, refreshregion.w, refreshregion.h, "dithering:", refreshdither)
else else
logger.dbg("setDirty", refreshtype, "from widget", widget_name, "w/ NO region", refreshdither and "AND w/ HW dithering" or "") logger.dbg("setDirty", refreshtype, "from widget", widget_name, "w/ NO region; dithering:", refreshdither)
end end
end end
end end
@ -1085,7 +1085,7 @@ function UIManager:_refresh(mode, region, dither)
end end
-- if we've stopped hitting collisions, enqueue the refresh -- if we've stopped hitting collisions, enqueue the refresh
logger.dbg("_refresh: Enqueued", mode, "update for region", region.x, region.y, region.w, region.h, dither and "w/ HW dithering" or "") logger.dbg("_refresh: Enqueued", mode, "update for region", region.x, region.y, region.w, region.h, "dithering:", dither)
table.insert(self._refresh_stack, {mode = mode, region = region, dither = dither}) table.insert(self._refresh_stack, {mode = mode, region = region, dither = dither})
end end

@ -557,15 +557,14 @@ function FrontLightWidget:onTapProgress(arg, ges_ev)
end end
if ges_ev.pos:intersectWith(self.fl_progress.dimen) then if ges_ev.pos:intersectWith(self.fl_progress.dimen) then
-- Unschedule any pending updates.
UIManager:unschedule(self.refreshBrightnessWidgets)
local perc = self.fl_progress:getPercentageFromPosition(ges_ev.pos) local perc = self.fl_progress:getPercentageFromPosition(ges_ev.pos)
if not perc then if not perc then
return true return true
end end
local num = Math.round(perc * self.fl.max) -- Unschedule any pending updates.
UIManager:unschedule(self.refreshBrightnessWidgets)
local num = Math.round(perc * self.fl.max)
-- Always set the frontlight intensity. -- Always set the frontlight intensity.
self:setFrontLightIntensity(num) self:setFrontLightIntensity(num)

Loading…
Cancel
Save