Developing lightweight computation at the DSG edge

qmpinfo.lua 3.8 KB
Newer Older
1
#!/usr/bin/lua
2
--[[
3
    Copyright (C) 2011 Fundacio Privada per a la Xarxa Oberta, Lliure i Neutral guifi.net
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

    The full GNU General Public License is included in this distribution in
    the file called "COPYING".
--]]
22
23

iwinfo = require "iwinfo"
24
25
util = require "luci.util"
sys = require "luci.sys"
26
27
28
29
30
31
32
33
34
35

qmpinfo = {}

function qmpinfo.get_modes(dev)
	local iw = iwinfo[iwinfo.type(dev)]
	local ma = "" -- modes avaiable
	return iw.hwmodelist(dev)
	
end

36
37
38
39
40
41
42
43
44
45
46
47

function qmpinfo.get_txpower(dev)
	local iw = iwinfo[iwinfo.type(dev)]
	local txp = iw.txpwrlist(dev)
	local txpower_supported = {}
	for _,v in ipairs(txp) do
		table.insert(txpower_supported,v.dbm)
	end
	return txpower_supported
	
end

48
function qmpinfo.get_channels(dev)
49
50
51
52
53
54
55
56
57
58
59
60
61
	local clist = {} -- output channel list      
	local iw = iwinfo[iwinfo.type(dev)]            
	local ch = {}                                            

	-- if there are not wireless cards, returning a dummy value                                                 
	if iw == nil then                            
		ch.channel=0                           
		ch.adhoc=false                       
		ch.ht40p=false                       
		ch.ht40m=false                       
		table.insert(clist,ch)                                                                                           
		return clist                         
	end                                          
62
             
63
64
65
66
67
	local freqs = iw.freqlist(dev) --freqs list
	local c -- current channel
	local nc = 0 -- next channel
	local pc = 0 -- previous channel
	local adhoc
68
69
	local ht40_support = qmpinfo.get_modes(dev).n
	
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116

	for i,f in ipairs(freqs) do
		c = f.channel
		ch = {}
		ch.channel = c
		ch.ht40p = false
		ch.ht40m = false
		ch.adhoc = false

		-- 2.4Ghz band
		if c < 15 then
			if c < 4 then 
				ch.ht40p = true
				ch.adhoc = true 
			
			elseif c < 10 then 
				ch.ht40m = true  
				ch.ht40p = true
				ch.adhoc = true
			else 
				ch.ht40m = true
				ch.adhoc = true
			end
		  
		-- 5Ghz band
		elseif c > 14 then
			if #freqs == i then nc = nil
			else nc = freqs[i+1].channel
			end

			if i == 1 then pc = nil
			else pc = freqs[i-1].channel
			end

			if nc ~= nil and nc-c == 4 then 
				ch.ht40p = true 
			end
	
			if pc ~= nil and c-pc == 4 then
				ch.ht40m = true
			end

			adhoc = os.execute("iw list | grep \"no IBSS\" | grep -v disabled | grep -q " .. f.mhz .. " 2>/dev/null")
			if adhoc ~= 0 then
				ch.adhoc = true
			end
		end
117
118
119
120
121
122
123

		-- If the device does not support ht40, both vars (+/-) are false
		if not ht40_support then
			ch.ht40p = false
			ch.ht40m = false
		end

124
		table.insert(clist,ch)
125

126
127
128
129
	end	
	return clist
end

130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151

function qmpinfo.get_ipv4()
	local ipv4 = {}
	local ipv4_raw = util.exec("ip -4 a | grep inet | awk '{print $2}' | awk -F/ '{print $1}' | grep -v 127.0.0.1")
	for _,v in ipairs(util.split(ipv4_raw)) do
		if #util.trim(v) > 1 then 
			table.insert(ipv4,util.trim(v))
		end
	end
	return ipv4
end

function qmpinfo.get_hostname()
	local hostname = util.exec("cat /proc/sys/kernel/hostname")
	return hostname
end

function qmpinfo.get_uname()
	local uname = util.exec("uname -a")
	return uname
end

152
return qmpinfo