2014-11-11 04:12:34 +00:00
|
|
|
local InfoMessage = require("ui/widget/infomessage")
|
2014-07-17 13:00:45 +00:00
|
|
|
local ConfirmBox = require("ui/widget/confirmbox")
|
|
|
|
local UIManager = require("ui/uimanager")
|
2014-10-30 18:42:18 +00:00
|
|
|
local Device = require("device")
|
2014-07-17 13:00:45 +00:00
|
|
|
local DEBUG = require("dbg")
|
2014-11-28 21:38:54 +00:00
|
|
|
local T = require("ffi/util").template
|
2014-07-17 13:00:45 +00:00
|
|
|
local _ = require("gettext")
|
|
|
|
local NetworkMgr = {}
|
|
|
|
|
|
|
|
local function kindleEnableWifi(toggle)
|
|
|
|
local lipc = require("liblipclua")
|
|
|
|
local lipc_handle = nil
|
|
|
|
if lipc then
|
|
|
|
lipc_handle = lipc.init("com.github.koreader.networkmgr")
|
|
|
|
end
|
|
|
|
if lipc_handle then
|
|
|
|
lipc_handle:set_int_property("com.lab126.cmd", "wirelessEnable", toggle)
|
|
|
|
lipc_handle:close()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-08-18 22:48:06 +00:00
|
|
|
local function koboEnableWifi(toggle)
|
2014-08-20 07:47:10 +00:00
|
|
|
if toggle == 1 then
|
2015-02-08 17:28:28 +00:00
|
|
|
os.execute("lsmod | grep -q sdio_wifi_pwr || insmod /drivers/$PLATFORM/wifi/sdio_wifi_pwr.ko")
|
|
|
|
os.execute("lsmod | grep -q dhd || insmod /drivers/$PLATFORM/wifi/dhd.ko")
|
|
|
|
os.execute("sleep 2")
|
2014-08-20 07:47:10 +00:00
|
|
|
os.execute("ifconfig eth0 up")
|
|
|
|
os.execute("wlarm_le -i eth0 up")
|
2015-02-08 17:28:28 +00:00
|
|
|
os.execute("pidof wpa_supplicant >/dev/null || wpa_supplicant -s -i eth0 -c /etc/wpa_supplicant/wpa_supplicant.conf -C /var/run/wpa_supplicant -B")
|
|
|
|
os.execute("sleep 1")
|
2015-02-09 15:01:30 +00:00
|
|
|
os.execute("/sbin/udhcpc -S -i eth0 -s /etc/udhcpc.d/default.script -t15 -T10 -A3 -b -q >/dev/null 2>&1 &")
|
2014-08-18 22:48:06 +00:00
|
|
|
else
|
2014-08-20 07:47:10 +00:00
|
|
|
os.execute("killall udhcpc wpa_supplicant 2>/dev/null")
|
|
|
|
os.execute("wlarm_le -i eth0 down")
|
|
|
|
os.execute("ifconfig eth0 down")
|
|
|
|
os.execute("rmmod -r dhd")
|
|
|
|
os.execute("rmmod -r sdio_wifi_pwr")
|
2014-08-18 22:48:06 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-02-01 07:16:27 +00:00
|
|
|
local function pocketbookEnableWifi(toggle)
|
|
|
|
os.execute("/ebrmain/bin/netagent " .. (toggle == 1 and "connect" or "disconnect"))
|
|
|
|
end
|
|
|
|
|
2014-07-17 13:00:45 +00:00
|
|
|
function NetworkMgr:turnOnWifi()
|
|
|
|
if Device:isKindle() then
|
|
|
|
kindleEnableWifi(1)
|
|
|
|
elseif Device:isKobo() then
|
2014-08-18 22:48:06 +00:00
|
|
|
koboEnableWifi(1)
|
2015-02-01 07:16:27 +00:00
|
|
|
elseif Device:isPocketBook() then
|
|
|
|
pocketbookEnableWifi(1)
|
2014-07-17 13:00:45 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function NetworkMgr:turnOffWifi()
|
|
|
|
if Device:isKindle() then
|
|
|
|
kindleEnableWifi(0)
|
|
|
|
elseif Device:isKobo() then
|
2014-08-18 22:48:06 +00:00
|
|
|
koboEnableWifi(0)
|
2015-02-01 07:16:27 +00:00
|
|
|
elseif Device:isPocketBook() then
|
|
|
|
pocketbookEnableWifi(0)
|
2014-07-17 13:00:45 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function NetworkMgr:promptWifiOn()
|
|
|
|
UIManager:show(ConfirmBox:new{
|
2014-11-12 11:29:38 +00:00
|
|
|
text = _("Do you want to turn on Wifi?"),
|
2014-07-17 13:00:45 +00:00
|
|
|
ok_callback = function()
|
|
|
|
self:turnOnWifi()
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
function NetworkMgr:promptWifiOff()
|
|
|
|
UIManager:show(ConfirmBox:new{
|
2014-11-12 11:29:38 +00:00
|
|
|
text = _("Do you want to turn off Wifi?"),
|
2014-07-17 13:00:45 +00:00
|
|
|
ok_callback = function()
|
|
|
|
self:turnOffWifi()
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2014-08-24 12:34:38 +00:00
|
|
|
function NetworkMgr:getWifiStatus()
|
2015-01-19 16:07:36 +00:00
|
|
|
local socket = require("socket")
|
|
|
|
return socket.dns.toip("www.google.com") ~= nil
|
2014-08-24 12:34:38 +00:00
|
|
|
end
|
|
|
|
|
2014-11-11 04:12:34 +00:00
|
|
|
function NetworkMgr:setHTTPProxy(proxy)
|
|
|
|
local http = require("socket.http")
|
|
|
|
http.PROXY = proxy
|
|
|
|
if proxy then
|
|
|
|
G_reader_settings:saveSetting("http_proxy", proxy)
|
|
|
|
G_reader_settings:saveSetting("http_proxy_enabled", true)
|
|
|
|
else
|
|
|
|
G_reader_settings:saveSetting("http_proxy_enabled", false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function NetworkMgr:getWifiMenuTable()
|
|
|
|
return {
|
|
|
|
text = _("Wifi connection"),
|
|
|
|
enabled_func = function() return Device:isKindle() or Device:isKobo() end,
|
|
|
|
checked_func = function() return NetworkMgr:getWifiStatus() end,
|
|
|
|
callback = function()
|
|
|
|
if NetworkMgr:getWifiStatus() then
|
|
|
|
NetworkMgr:promptWifiOff()
|
|
|
|
else
|
|
|
|
NetworkMgr:promptWifiOn()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
function NetworkMgr:getProxyMenuTable()
|
|
|
|
local proxy_enabled = function()
|
|
|
|
return G_reader_settings:readSetting("http_proxy_enabled")
|
|
|
|
end
|
|
|
|
local proxy = function()
|
|
|
|
return G_reader_settings:readSetting("http_proxy")
|
|
|
|
end
|
|
|
|
return {
|
|
|
|
text_func = function()
|
2014-11-28 21:38:54 +00:00
|
|
|
return T(_("HTTP proxy %1"), (proxy_enabled() and proxy() or ""))
|
2014-11-11 04:12:34 +00:00
|
|
|
end,
|
|
|
|
checked_func = function() return proxy_enabled() end,
|
|
|
|
callback = function()
|
|
|
|
if not proxy_enabled() and proxy() then
|
|
|
|
NetworkMgr:setHTTPProxy(proxy())
|
|
|
|
elseif proxy_enabled() then
|
|
|
|
NetworkMgr:setHTTPProxy(nil)
|
|
|
|
end
|
|
|
|
if not proxy() then
|
|
|
|
UIManager:show(InfoMessage:new{
|
2014-11-28 13:10:37 +00:00
|
|
|
text = _("Tip:\nLong press on this menu entry to configure HTTP proxy."),
|
2014-11-11 04:12:34 +00:00
|
|
|
})
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
hold_input = {
|
2014-11-28 13:10:37 +00:00
|
|
|
title = _("Enter proxy address"),
|
2014-11-11 04:12:34 +00:00
|
|
|
type = "text",
|
|
|
|
hint = proxy() or "",
|
|
|
|
callback = function(input)
|
|
|
|
if input ~= "" then
|
|
|
|
NetworkMgr:setHTTPProxy(input)
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2014-10-05 08:04:49 +00:00
|
|
|
-- set network proxy if global variable NETWORK_PROXY is defined
|
|
|
|
if NETWORK_PROXY then
|
2014-11-11 04:12:34 +00:00
|
|
|
NetworkMgr:setHTTPProxy(NETWORK_PROXY)
|
2014-10-05 08:04:49 +00:00
|
|
|
end
|
|
|
|
|
2014-07-17 13:00:45 +00:00
|
|
|
return NetworkMgr
|