class Device::Network

Constants

AUTH_IEEE8021X
AUTH_NONE_OPEN
AUTH_NONE_WEP
AUTH_NONE_WEP_SHARED
AUTH_WPA2_PSK
AUTH_WPA_PSK
AUTH_WPA_WPA2_PSK
ERR_USER_CANCEL
MEDIA_ETHERNET
MEDIA_GPRS
MEDIA_WIFI
MODE_IBSS
MODE_STATION
NO_CONNECTION
PARE_CIPHERS_CCMP
PARE_CIPHERS_NONE
PARE_CIPHERS_TKIP
PARE_CIPHERS_WEP128
PARE_CIPHERS_WEP64
PARE_CIPHERS_WEPX
POWER_OFF
POWER_ON
PROCESSING
SUCCESS
TIMEOUT

Attributes

apn[RW]
code[RW]
password[RW]
socket[RW]
type[RW]
user[RW]

Public Class Methods

adapter() click to toggle source
# File lib/device/network.rb, line 53
def self.adapter
  Device.adapter::Network
end
attach(options = nil) click to toggle source
# File lib/device/network.rb, line 190
def self.attach(options = nil)
  Device::Network.connected?
  if self.code != SUCCESS
    self.code = Device::Network.init(*self.config)
    self.code = Device::Network.connect
    Device::Network.connected? if self.code != SUCCESS

    hash = try_user(self.attach_timeout, options) do |process|
      Device::Network.connected?
      process[:ret] = self.code
      # TODO develop an interface to keep waiting communication module dial
      # based on platform returns
      process[:ret] == PROCESSING || process[:ret] == 2 || process[:ret] == -3307 # if true keep trying
    end
    self.code = hash[:ret]

    if self.code == SUCCESS
      self.code = Device::Network.dhcp_client(20000) if (wifi? || ethernet?)
    else
      self.code = ERR_USER_CANCEL if hash[:key] == Device::IO::CANCEL
      Device::Network.shutdown
    end
  end
  self.code
end
attach_timeout() click to toggle source
# File lib/device/network.rb, line 182
def self.attach_timeout
  if self.gprs?
    Device::Setting.attach_gprs_timeout || Device::IO.timeout
  else
    Device::IO.timeout
  end
end
config() click to toggle source
# File lib/device/network.rb, line 222
def self.config
  # TODO raise some error if media was not set
  [Device::Setting.media, self.config_media]
end
config_media() click to toggle source

TODO should check if WIFI, ETHERNET and etc

# File lib/device/network.rb, line 232
def self.config_media
  if gprs?
    {
      apn:      Device::Setting.apn,
      user:     Device::Setting.user,
      password: Device::Setting.apn_password
    }
  elsif wifi?
    {
      authentication: Device::Setting.authentication,
      password:       Device::Setting.wifi_password,
      essid:          Device::Setting.essid,
      channel:        Device::Setting.channel,
      cipher:         Device::Setting.cipher,
      mode:           Device::Setting.mode
    }
  elsif ethernet?
    Hash.new # TODO
  end
end
configure(type, options) click to toggle source
# File lib/device/network.rb, line 61
def self.configure(type, options)
  adapter.configure(type, options)
end
configured?() click to toggle source
# File lib/device/network.rb, line 82
def self.configured?
  Device::Setting.network_configured == "1" && ! Device::Setting.media.to_s.empty?
end
connect() click to toggle source
# File lib/device/network.rb, line 69
def self.connect
  adapter.connect
end
connected?() click to toggle source
# File lib/device/network.rb, line 73
def self.connected?
  if self.adapter.started? || self.setup
    self.code = adapter.connected?
    return self.code == Device::Network::SUCCESS
  end
  self.code = NO_CONNECTION
  false
end
dhcp_client(timeout) click to toggle source
# File lib/device/network.rb, line 166
def self.dhcp_client(timeout)
  ret = adapter.dhcp_client_start
  if (ret == SUCCESS)
    hash = try_user(timeout) do |processing|
      processing[:ret] = adapter.dhcp_client_check
      processing[:ret] == PROCESSING
    end
    ret = hash[:ret]

    unless ret == SUCCESS
      ret = ERR_USER_CANCEL if hash[:key] == Device::IO::CANCEL
    end
  end
  ret
end
disconnect() click to toggle source
# File lib/device/network.rb, line 90
def self.disconnect
  adapter.disconnect
end
ethernet?() click to toggle source
# File lib/device/network.rb, line 261
def self.ethernet?
  Device::Setting.media == "ethernet"
end
gprs?() click to toggle source
# File lib/device/network.rb, line 253
def self.gprs?
  Device::Setting.media == "gprs"
end
init(type, options) click to toggle source
# File lib/device/network.rb, line 57
def self.init(type, options)
  adapter.init(type, options)
end
mac_address(media = nil) click to toggle source

Get mac address of the current connection or from the parameters sent

@param media [String] From Network class, MEDIA_GPRS (“gprs”),

MEDIA_WIFI ("wifi"), MEDIA_ETHERNET ("ethernet")

@return [String] Return mac address based on the example “AA:BB:CC:DD:EE:FF”

@example

Device::Network.mac_address(Device::Network::MEDIA_GPRS)
# => "AA:BB:CC:DD:EE:FF"
# File lib/device/network.rb, line 118
def self.mac_address(media = nil)
  unless media
    self.adapter.mac_address(media2klass(Device::Setting.media))
  else
    self.adapter.mac_address(media2klass(media))
  end
end
media2klass(media) click to toggle source

Convert string media configuration to a class

# File lib/device/network.rb, line 127
def self.media2klass(media)
  case media
  when MEDIA_GPRS
    Network::Gprs
  when MEDIA_WIFI
    Network::Wifi
  when MEDIA_ETHERNET
    Network::Ethernet
  else
    Network::Wifi
  end
end
ping(host, port) click to toggle source
# File lib/device/network.rb, line 86
def self.ping(host, port)
  adapter.ping(host, port)
end
power(command) click to toggle source
# File lib/device/network.rb, line 65
def self.power(command)
  adapter.power(command)
end
scan() click to toggle source

Scan for wifi aps available

@return [Array] Return an array of hash values

containing the values necessary to configure connection

@example

aps = Device::Network.scan
# create a selection to menu method
selection = aps.inject({}) do |selection, hash|
  selection[hash[:essid]] = hash; selection
end
selected = menu("Select SSID:", selection)

Device::Setting.wifi_password  = form("Password",
  :min => 0, :max => 127, :default => Device::Setting.wifi_password)
Device::Setting.authentication = selected[:authentication]
Device::Setting.essid          = selected[:essid]
Device::Setting.channel        = selected[:channel]
Device::Setting.cipher         = selected[:cipher]
Device::Setting.mode           = selected[:mode]
# File lib/device/network.rb, line 160
def self.scan
  if wifi?
    adapter.scan if Device::Network.init(*self.config)
  end
end
setup() click to toggle source
# File lib/device/network.rb, line 227
def self.setup
  self.configured? && (Device::Network.configure(*self.config) == SUCCESS)
end
shutdown() click to toggle source
# File lib/device/network.rb, line 215
def self.shutdown
  if self.adapter.started?
    Device::Network.disconnect
    Device::Network.power(0)
  end
end
signal() click to toggle source

Check signal value

@return [Fixnum] Signal value between 0 and 5.

# File lib/device/network.rb, line 103
def self.signal
  if self.connected?
    adapter.signal
  end
end
sim_id() click to toggle source
# File lib/device/network.rb, line 94
def self.sim_id
  if self.adapter.started?
    adapter.sim_id
  end
end
wifi?() click to toggle source
# File lib/device/network.rb, line 257
def self.wifi?
  Device::Setting.media == "wifi"
end