Class: Device::Network

Inherits:
Object
  • Object
show all
Defined in:
lib/device/network.rb

Constant Summary collapse

MEDIA_GPRS =
"gprs"
MEDIA_WIFI =
"wifi"
MEDIA_ETHERNET =
"ethernet"
AUTH_NONE_OPEN =
"open"
AUTH_NONE_WEP =
"wep"
AUTH_NONE_WEP_SHARED =
"wepshared"
AUTH_IEEE8021X =
"IEEE8021X"
AUTH_WPA_PSK =
"wpapsk"
AUTH_WPA_WPA2_PSK =
"wpawpa2psk"
AUTH_WPA2_PSK =
"wpa2psk"
PARE_CIPHERS_NONE =
"none"
PARE_CIPHERS_WEP64 =
"wep64"
PARE_CIPHERS_WEP128 =
"wep128"
PARE_CIPHERS_WEPX =
"wepx"
PARE_CIPHERS_CCMP =
"ccmp"
PARE_CIPHERS_TKIP =
"tkip"
MODE_IBSS =
"ibss"
MODE_STATION =
"station"
TIMEOUT =
-3320
NO_CONNECTION =
-1012
SUCCESS =
0
PROCESSING =
1

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.apnObject

Returns the value of attribute apn.



38
39
40
# File 'lib/device/network.rb', line 38

def apn
  @apn
end

.passwordObject

Returns the value of attribute password.



38
39
40
# File 'lib/device/network.rb', line 38

def password
  @password
end

.socketObject

Returns the value of attribute socket.



38
39
40
# File 'lib/device/network.rb', line 38

def socket
  @socket
end

.typeObject

Returns the value of attribute type.



38
39
40
# File 'lib/device/network.rb', line 38

def type
  @type
end

.userObject

Returns the value of attribute user.



38
39
40
# File 'lib/device/network.rb', line 38

def user
  @user
end

Class Method Details

.adapterObject



47
48
49
# File 'lib/device/network.rb', line 47

def self.adapter
  Device.adapter::Network
end

.attachObject



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/device/network.rb', line 138

def self.attach
  ret = Device::Network.connected?
  if ret != SUCCESS
    ret = Device::Network.init(*self.config)
    ret = Device::Network.connect
    ret = Device::Network.connected? if ret != SUCCESS
    while(ret == PROCESSING)
      ret = Device::Network.connected?
    end
    if ret == SUCCESS && (wifi? || ethernet?)
      Device::Network.dhcp_client(20000)
    end
    Device::Setting.network_configured = 0 if ret != SUCCESS 
  end
  ret
end

.configObject



155
156
157
158
# File 'lib/device/network.rb', line 155

def self.config
  # TODO raise some error if media was not set
  [Device::Setting.media, self.config_media]
end

.config_mediaObject

TODO should check if WIFI, ETHERNET and etc



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/device/network.rb', line 161

def self.config_media
  if gprs?
    {
      apn:      Device::Setting.apn,
      user:     Device::Setting.user,
      password: Device::Setting.password
    }
  elsif wifi?
    {
      authentication: Device::Setting.authentication,
      password:       Device::Setting.password,
      essid:          Device::Setting.essid,
      channel:        Device::Setting.channel,
      cipher:         Device::Setting.cipher,
      mode:           Device::Setting.mode
    }
  elsif ethernet?
    Hash.new
  end
end

.configured?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/device/network.rb', line 72

def self.configured?
  Device::Setting.network_configured == "1"
end

.connectObject



59
60
61
# File 'lib/device/network.rb', line 59

def self.connect
  adapter.connect
end

.connected?Boolean

Returns:

  • (Boolean)


63
64
65
66
67
68
69
70
# File 'lib/device/network.rb', line 63

def self.connected?
  if self.adapter.started? ||
    (self.configured? && Device::Network.init(*self.config) == SUCCESS)

    return adapter.connected?
  end
  NO_CONNECTION
end

.dhcp_client(timeout) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/device/network.rb', line 125

def self.dhcp_client(timeout)
  time = Time.now + (timeout.to_f / 1000.0)
  ret = adapter.dhcp_client_start
  if (ret == SUCCESS)
    ret = PROCESSING
    while(ret == PROCESSING) # 1 - In process to attach
      ret = adapter.dhcp_client_check
      break ret = TIMEOUT unless (time >= Time.now)
    end
  end
  ret
end

.disconnectObject



80
81
82
# File 'lib/device/network.rb', line 80

def self.disconnect
  adapter.disconnect
end

.ethernet?Boolean

Returns:

  • (Boolean)


190
191
192
# File 'lib/device/network.rb', line 190

def self.ethernet?
  Device::Setting.media == "ethernet"
end

.gprs?Boolean

Returns:

  • (Boolean)


182
183
184
# File 'lib/device/network.rb', line 182

def self.gprs?
  Device::Setting.media == "gprs"
end

.init(type, options) ⇒ Object



51
52
53
# File 'lib/device/network.rb', line 51

def self.init(type, options)
  adapter.init(type, options)
end

.ping(host, port) ⇒ Object



76
77
78
# File 'lib/device/network.rb', line 76

def self.ping(host, port)
  adapter.ping(host, port)
end

.power(command) ⇒ Object



55
56
57
# File 'lib/device/network.rb', line 55

def self.power(command)
  adapter.power(command)
end

.scanArray

Scan for wifi aps available

Examples:

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.password       = form("Password",
  :min => 0, :max => 127, :default => Device::Setting.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]

Returns:

  • (Array)

    Return an array of hash values containing the values necessary to configure connection



119
120
121
122
123
# File 'lib/device/network.rb', line 119

def self.scan
  if wifi?
    adapter.scan if Device::Network.init(*self.config)
  end
end

.signalFixnum

Check signal value

Returns:

  • (Fixnum)

    Signal value between 0 and 5.



95
96
97
# File 'lib/device/network.rb', line 95

def self.signal
  adapter.signal
end

.sim_idObject



84
85
86
87
88
89
90
# File 'lib/device/network.rb', line 84

def self.sim_id
  if adapter.respond_to? :sim_id
    adapter.sim_id
  else
    ""
  end
end

.wifi?Boolean

Returns:

  • (Boolean)


186
187
188
# File 'lib/device/network.rb', line 186

def self.wifi?
  Device::Setting.media == "wifi"
end