Class: Device::Network

Inherits:
Object
  • Object
show all
Includes:
DaFunk::Helper
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"
ERR_USER_CANCEL =
-1010
TIMEOUT =
-3320
NO_CONNECTION =
-1012
SUCCESS =
0
PROCESSING =
1
POWER_OFF =
0
POWER_ON =
1

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.apnObject

Returns the value of attribute apn.



43
44
45
# File 'lib/device/network.rb', line 43

def apn
  @apn
end

.passwordObject

Returns the value of attribute password.



43
44
45
# File 'lib/device/network.rb', line 43

def password
  @password
end

.socketObject

Returns the value of attribute socket.



43
44
45
# File 'lib/device/network.rb', line 43

def socket
  @socket
end

.typeObject

Returns the value of attribute type.



43
44
45
# File 'lib/device/network.rb', line 43

def type
  @type
end

.userObject

Returns the value of attribute user.



43
44
45
# File 'lib/device/network.rb', line 43

def user
  @user
end

Class Method Details

.adapterObject



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

def self.adapter
  Device.adapter::Network
end

.attachObject



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/device/network.rb', line 146

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

    hash = try_user do |process|
      process[:ret] = Device::Network.connected?
      process[:ret] == PROCESSING # if true keep trying
    end
    ret = hash[:ret]

    if ret == SUCCESS
      ret = Device::Network.dhcp_client(20000) if (wifi? || ethernet?)
      Device::Setting.network_configured = 1
    else
      ret = ERR_USER_CANCEL if hash[:key] == Device::IO::CANCEL
      Device::Setting.network_configured = 0
      Device::Network.disconnect
    end
  end
  ret
end

.configObject



171
172
173
174
# File 'lib/device/network.rb', line 171

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



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/device/network.rb', line 177

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)


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

def self.configured?
  Device::Setting.network_configured == "1" && ! Device::Setting.media.to_s.empty?
end

.connectObject



64
65
66
# File 'lib/device/network.rb', line 64

def self.connect
  adapter.connect
end

.connected?Boolean

Returns:

  • (Boolean)


68
69
70
71
72
73
74
75
# File 'lib/device/network.rb', line 68

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



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/device/network.rb', line 130

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

.disconnectObject



85
86
87
# File 'lib/device/network.rb', line 85

def self.disconnect
  adapter.disconnect
end

.ethernet?Boolean

Returns:

  • (Boolean)


206
207
208
# File 'lib/device/network.rb', line 206

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

.gprs?Boolean

Returns:

  • (Boolean)


198
199
200
# File 'lib/device/network.rb', line 198

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

.init(type, options) ⇒ Object



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

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

.ping(host, port) ⇒ Object



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

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

.power(command) ⇒ Object



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

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



124
125
126
127
128
# File 'lib/device/network.rb', line 124

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.



98
99
100
101
102
# File 'lib/device/network.rb', line 98

def self.signal
  if self.connected? == 0
    adapter.signal
  end
end

.sim_idObject



89
90
91
92
93
# File 'lib/device/network.rb', line 89

def self.sim_id
  if self.adapter.started?
    adapter.sim_id
  end
end

.wifi?Boolean

Returns:

  • (Boolean)


202
203
204
# File 'lib/device/network.rb', line 202

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