Module: OmfRc::Util::Iw

Extended by:
ResourceProxyDSL::ClassMethods
Includes:
Cocaine, Hashie, ResourceProxyDSL, Hostapd, Ip, Wpa
Included in:
ResourceProxy::Wlan
Defined in:
omf_rc/lib/omf_rc/util/iw.rb

Overview

Utility for executing command 'iw'

Constant Summary

Constant Summary

Constants included from ResourceProxyDSL

ResourceProxyDSL::DEFAULT_PROP_ACCESS, ResourceProxyDSL::PROXY_DIR, ResourceProxyDSL::UTIL_DIR

FRCP Request Methods (collapse)

FRCP Configure Methods (collapse)

OMF RC Work Helper Methods (collapse)

Methods included from ResourceProxyDSL::ClassMethods

call_hook, configure, extend_configure, extend_hook, extend_request, extend_work, hook, property, register_proxy, request, utility, work

Methods included from ResourceProxyDSL

#call_hook, included

Methods included from Ip

#configure_ip_addr, #flush_ip_addrs, #interface_up, #request_ip_addr, #request_mac_addr

Instance Method Details

- (String) add_interface

Note:

Method 'add_interface' generated by DSL method 'work'

Add interface to device



177
178
179
180
181
182
# File 'omf_rc/lib/omf_rc/util/iw.rb', line 177

work :add_interface do |device, type|
  CommandLine.new("iw", "phy :phy interface add :dev type :type",
                  :phy => device.property.phy,
                  :dev => device.property.if_name,
                  :type => type.to_s).run
end

- (String) configure_mode(opts)

Note:

Method 'configure_mode' generated by DSL method 'configure'

Configure the interface with mode: managed, master, adhoc or monitor

Examples:

Sample opts for mode property

# Master
{ mode: :master, hw_mode: 'a', channel: 1, essid: 'bob' }

# Managed
{ mode: :managed, essid: 'bob' }

# Ad-hoc
{ mode: :adhoc, essid: 'bob', frequency: 2412 }

# Monitor
{ mode: :monitor }

Options Hash (opts):

  • :mode (Symbol)

    wireless connection mode (:master, :managed, :adhoc)

  • :hw_mode (Symbol)

    wireless connection hardware mode ('a', 'b', 'g', 'n')

  • :essid (Symbol)
  • :channel (Symbol)
  • :frequency (Symbol)

Raises:

  • (ArgumentError)

    if wifi device specified cannot be found on the system



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'omf_rc/lib/omf_rc/util/iw.rb', line 123

configure :mode do |device, opts|
  # capture opts hash and store internally
  device.property.update(opts)

  if device.property.phy && device.property.phy =~ /^%(\d+)%$/
    wlan_phy_device = device.request_wlan_devices[$1.to_i]
    if wlan_phy_device
      device.property.phy = wlan_phy_device[:name]
    else
      raise ArgumentError, "Could not find your wifi device no #{$1.to_i}"
    end
  end

  device.validate_iw_properties

  device.delete_interface rescue logger.warn "Interface #{device.property.if_name} not found"

  # TODO should just remove all interfaces from physical device, at least make it optional

  case device.property.mode.to_sym
  when :master
    device.add_interface(:managed)
    device.hostapd
  when :managed
    device.add_interface(:managed)
    device.wpasup
  when :adhoc
    device.add_interface(:adhoc)
    device.interface_up
    device.join_ibss
  when :monitor
    device.add_interface(:monitor)
    device.interface_up
  end

  device.configure_ip_addr(device.property.ip_addr) if device.property.ip_addr
end

- (String) delete_interface

Note:

Method 'delete_interface' generated by DSL method 'work'

Delete current interface, clean up



169
170
171
# File 'omf_rc/lib/omf_rc/util/iw.rb', line 169

work :delete_interface do |device|
  CommandLine.new("iw", "dev :dev del", :dev => device.property.if_name).run
end

- (String) join_ibss

Note:

Method 'join_ibss' generated by DSL method 'work'

Set up or join a ibss network



188
189
190
191
192
193
# File 'omf_rc/lib/omf_rc/util/iw.rb', line 188

work :join_ibss do |device|
  CommandLine.new("iw", "dev :device ibss join :essid :frequency",
                    :device => device.property.if_name.to_s,
                    :essid => device.property.essid.to_s,
                    :frequency => device.property.frequency.to_s).run
end

- (Mash) request_info

Note:

Method 'request_info' generated by DSL method 'request'

Parse iw info command output and return as a mash

Examples:

return value


{ ifindex: '3', type: 'managed', wiphy: '0' }


76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'omf_rc/lib/omf_rc/util/iw.rb', line 76

request :info do |device|
  known_properties = Mash.new

  command = CommandLine.new("iw", "dev :dev info", :dev => device.property.if_name)

  command.run.chomp.split("\n").drop(1).each do |v|
    v.match(/^\W*(.+) (.+)$/).tap do |m|
      m && known_properties[m[1].downcase.gsub(/\W+/, '_')] = m[2].gsub(/^\W+/, '')
    end
  end

  known_properties
end
Note:

Method 'request_link' generated by DSL method 'request'

Parse iw link command output and return as a mash

Examples:

return value


{ ssid: 'ap', freq: '2412', signal: '-67 dBm' }


53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'omf_rc/lib/omf_rc/util/iw.rb', line 53

request :link do |device|
  known_properties = Mash.new

  command = CommandLine.new("iw", "dev :dev link", :dev => device.property.if_name)

  command.run.chomp.gsub(/^\t/, '').split("\n").drop(1).each do |v|
    v.match(/^(.+):\W*(.+)$/).tap do |m|
      m && known_properties[m[1].downcase.gsub(/\W+/, '_')] = m[2].gsub(/^\W+/, '')
    end
  end

  known_properties
end

- (nil) validate_iw_properties

Note:

Method 'validate_iw_properties' generated by DSL method 'work'

Validate internal properties based on interface mode

Raises:

  • (ArgumentError)

    if validation failed



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'omf_rc/lib/omf_rc/util/iw.rb', line 201

work :validate_iw_properties do |device|
  raise ArgumentError, "Missing phyical device name" if device.property.phy.nil?

  unless %w(master managed adhoc monitor).include? device.property.mode
    raise ArgumentError, "Mode must be master, managed, adhoc, or monitor, got #{device.property.mode}"
  end

  case device.property.mode.to_sym
  when :master
    unless %w(a b g n).include? device.property.hw_mode
      raise ArgumentError, "Hardware mode must be a, b, g, or n, got #{device.property.hw_mode}"
    end
    %w(channel essid).each do |p|
      raise ArgumentError, "#{p} must not be nil" if device.property.send(p).nil?
    end
  when :managed
    %w(essid).each do |p|
      raise ArgumentError, "#{p} must not be nil" if device.property.send(p).nil?
    end
  when :adhoc
    %w(essid frequency).each do |p|
      raise ArgumentError, "#{p} must not be nil" if device.property.send(p).nil?
    end
  end
end