Skip to content

Commit

Permalink
lime-hwd-usbradio: added inline documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
frank95 committed Jul 8, 2014
1 parent 1dd2c3a commit 1b7206a
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions packages/lime-hwd-usbradio/src/usbradio.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,22 @@ local fs = require("nixio.fs")

usbradio = {}

--! Remove configuration about no more plugged usb radio
function usbradio.clean()
local uci = libuci:cursor()

--! This function control if a usb radio configuration section is valid otherwise delete it
local function test_and_clean_device(s)
--! Check if passed section is an usb radio
if s["path"]:match("usb") then
local radioName = s[".name"]

--! Check if the section is autogenerated otherwise do not touch it
--! We check it to avoid delete usb radio sections manually configured
if config.get_bool(radioName, "autogenerated") then
local phyIndex = radioName:match("%d$")
--! Check if the usb radio exist
--! If the usb radio does not exist anymore (numberOfMatches < 1) delete it
local _, numberOfMatches = fs.glob("/sys/devices/"..s["path"].."/ieee80211/phy"..phyIndex)
if numberOfMatches < 1 then
uci:delete("wireless", radioName)
Expand All @@ -25,24 +33,36 @@ function usbradio.clean()
end
end

--! For each wifi-device section call test_and_clean_device function
uci:foreach("wireless", "wifi-device", test_and_clean_device)
uci:save("wireless")
end




--! Detect the usb radio and configurate it
function usbradio.detect_hardware()
local stdOutput = io.popen("find /sys/devices | grep usb | grep ieee80211 | grep 'phy[0-9]*$'")
local stdOutput = io.popen("find /sys/devices | grep usb | grep ieee80211 | grep 'phy[0-9]*$'") --TODO: optimization rewrite it in lua maybe fs.glob would be useful

for _,path in pairs(utils.split(stdOutput:read("*a"), "\n")) do
--! Repeat for each usb radio found
for _,path in pairs(utils.split(stdOutput:read("*a"), "\n")) do --TODO: read line by line instead of "*a"
--! Define useful variables
local endBasePath, phyEnd = string.find(path, "/ieee80211/phy")
local phyPath = string.sub(path, 14, endBasePath-1)
local phyIndex = string.sub(path, phyEnd+1)
local radioName = "radio"..phyIndex

--! If radioName exist and it is autogenerated configure it
--! If radioName does not exist it is created and configure it
--! Check if a radioName is autogenerated avoid delete usb radio introduced by the user
if ( (not config.get_all(radioName)) or config.get_bool(radioName, "autogenerated") ) then

local uci = libuci:cursor()

--! Delete the usb radio
uci:delete("wireless", radioName)
--! Create and configure the usb radio directly in the OpenWRT system (wireless)
uci:set("wireless", radioName, "wifi-device")
uci:set("wireless", radioName, "type", "mac80211")
uci:set("wireless", radioName, "channel", "11") --TODO: working on all 802.11bgn devices; find a general way for working in different devices
Expand All @@ -53,11 +73,16 @@ function usbradio.detect_hardware()

uci:save("wireless")

--! Write just once on the disk all the config.set
config.init_batch()
config.set(radioName, "wifi")
config.set(radioName, "autogenerated", "true")

--! Configuration of an usb radio using general option in LiMe
for option_name, value in pairs(config.get_all("wifi")) do
if not option_name:match("^%.") then
--! Options that start with point are hidden option, we exclude them as they can cause problems
if not option_name:match("^%.") then --TODO: optimization instead of a match check if the first character is a point
--! Needed a table or a string for config.set
if ( type(value) ~= "table" ) then value = tostring(value) end
config.set(radioName, option_name, value)
end
Expand Down

0 comments on commit 1b7206a

Please sign in to comment.