Class: Bandshell::WirelessConnection

Inherits:
Object
  • Object
show all
Defined in:
lib/bandshell/netconfig.rb

Overview

802.11* unencrypted wireless connections. These are managed by wpa_supplicant on Debian so we need to create its configuration file and link it to the interfaces file.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ WirelessConnection

Returns a new instance of WirelessConnection.



243
244
245
246
247
# File 'lib/bandshell/netconfig.rb', line 243

def initialize(args={})
  @ssid = args['ssid'] || ''
  @interface_name = args['interface_name'] if args['interface_name']
  @wpa_config_file = '/tmp/wpa_supplicant.concerto.conf'
end

Instance Attribute Details

#interface_nameObject

Returns the value of attribute interface_name.



249
250
251
# File 'lib/bandshell/netconfig.rb', line 249

def interface_name
  @interface_name
end

#ssidObject

Returns the value of attribute ssid.



249
250
251
# File 'lib/bandshell/netconfig.rb', line 249

def ssid
  @ssid
end

Class Method Details

.descriptionObject



298
299
300
# File 'lib/bandshell/netconfig.rb', line 298

def self.description
  "Wireless connection (no encryption)"
end

.interfacesObject



302
303
304
305
306
# File 'lib/bandshell/netconfig.rb', line 302

def self.interfaces
  # Again this is not guaranteed to be a catch all.
  devices = Dir.glob('/sys/class/net/{ath,wlan}*')
  devices.map { |d| Interface.new(File.basename(d)) }
end

Instance Method Details

#argsObject



291
292
293
294
295
296
# File 'lib/bandshell/netconfig.rb', line 291

def args
  {
    'interface_name' => @interface_name,
    'ssid' => @ssid
  }
end

#config_interface_nameObject



251
252
253
254
255
256
257
258
259
260
261
# File 'lib/bandshell/netconfig.rb', line 251

def config_interface_name
  # If the user has requested a specific interface, use it.
  # Otherwise, just pick the first wlan interface, assuming
  # it works and all wlan interfaces have approximately equal
  # reception. When this assumption is wrong the user must force.
  if @interface_name && @interface_name != ''
    @interface_name
  else
    self.class.interfaces[0].name
  end
end

#interfaces_linesObject



286
287
288
289
# File 'lib/bandshell/netconfig.rb', line 286

def interfaces_lines
  # This links the wpa config to the interfaces file.
  ["wpa-conf #{@wpa_config_file}"]
end

#safe_assignObject



269
270
271
# File 'lib/bandshell/netconfig.rb', line 269

def safe_assign
  [ :ssid, :interface_name ]
end

#validateObject



263
264
265
266
267
# File 'lib/bandshell/netconfig.rb', line 263

def validate
  if @ssid == ''
    fail "Need SSID for wireless connection"
  end
end

#write_configsObject



273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/bandshell/netconfig.rb', line 273

def write_configs
  # Write a wpa_supplicant.conf file for an unsecured network.
  File.open(@wpa_config_file, 'w') do |wpaconf|
    # long lines, sorry!
    wpaconf.puts "ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev"
    wpaconf.puts "network={"
    wpaconf.puts "ssid=\"#{@ssid}\""
    wpaconf.puts "scan_ssid=1"
    wpaconf.puts "key_mgmt=NONE"
    wpaconf.puts "}"
  end
end