Class: Device::Network
- Inherits:
-
Object
- Object
- Device::Network
- 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
-
.apn ⇒ Object
Returns the value of attribute apn.
-
.code ⇒ Object
Returns the value of attribute code.
-
.password ⇒ Object
Returns the value of attribute password.
-
.socket ⇒ Object
Returns the value of attribute socket.
-
.type ⇒ Object
Returns the value of attribute type.
-
.user ⇒ Object
Returns the value of attribute user.
Class Method Summary collapse
- .adapter ⇒ Object
- .attach(options = nil) ⇒ Object
- .attach_timeout ⇒ Object
- .config ⇒ Object
-
.config_media ⇒ Object
TODO should check if WIFI, ETHERNET and etc.
- .configure(type, options) ⇒ Object
- .configured? ⇒ Boolean
- .connect ⇒ Object
- .connected? ⇒ Boolean
- .dhcp_client(timeout) ⇒ Object
- .disconnect ⇒ Object
- .ethernet? ⇒ Boolean
- .gprs? ⇒ Boolean
- .init(type, options) ⇒ Object
- .load_metadata ⇒ Object
-
.mac_address(media = nil) ⇒ String
Get mac address of the current connection or from the parameters sent.
-
.media2klass(media) ⇒ Object
Convert string media configuration to a class.
- .ping(host, port) ⇒ Object
- .power(command) ⇒ Object
-
.scan ⇒ Array
Scan for wifi aps available.
- .setup ⇒ Object
- .shutdown ⇒ Object
-
.signal ⇒ Fixnum
Check signal value.
- .sim_id ⇒ Object
- .wifi? ⇒ Boolean
Class Attribute Details
.apn ⇒ Object
Returns the value of attribute apn.
43 44 45 |
# File 'lib/device/network.rb', line 43 def apn @apn end |
.code ⇒ Object
Returns the value of attribute code.
43 44 45 |
# File 'lib/device/network.rb', line 43 def code @code end |
.password ⇒ Object
Returns the value of attribute password.
43 44 45 |
# File 'lib/device/network.rb', line 43 def password @password end |
.socket ⇒ Object
Returns the value of attribute socket.
43 44 45 |
# File 'lib/device/network.rb', line 43 def socket @socket end |
.type ⇒ Object
Returns the value of attribute type.
43 44 45 |
# File 'lib/device/network.rb', line 43 def type @type end |
.user ⇒ Object
Returns the value of attribute user.
43 44 45 |
# File 'lib/device/network.rb', line 43 def user @user end |
Class Method Details
.adapter ⇒ Object
53 54 55 |
# File 'lib/device/network.rb', line 53 def self.adapter Device.adapter::Network end |
.attach(options = nil) ⇒ Object
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/device/network.rb', line 192 def self.attach( = nil) Device::Network.connected? Context::ThreadPubSub.publish('communication_update') if self.code != SUCCESS ThreadScheduler.pausing_communication do 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, ) 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. 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 end Context::ThreadPubSub.publish('communication_update') self.code end |
.attach_timeout ⇒ Object
184 185 186 187 188 189 190 |
# File 'lib/device/network.rb', line 184 def self.attach_timeout if self.gprs? Device::Setting.attach_gprs_timeout || Device::IO.timeout else Device::IO.timeout end end |
.config ⇒ Object
236 237 238 239 |
# File 'lib/device/network.rb', line 236 def self.config # TODO raise some error if media was not set [Device::Setting.media, self.config_media] end |
.config_media ⇒ Object
TODO should check if WIFI, ETHERNET and etc
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/device/network.rb', line 246 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) ⇒ Object
61 62 63 |
# File 'lib/device/network.rb', line 61 def self.configure(type, ) adapter.configure(type, ) end |
.configured? ⇒ Boolean
82 83 84 |
# File 'lib/device/network.rb', line 82 def self.configured? Device::Setting.network_configured == "1" && ! Device::Setting.media.to_s.empty? end |
.connect ⇒ Object
69 70 71 |
# File 'lib/device/network.rb', line 69 def self.connect adapter.connect end |
.connected? ⇒ Boolean
73 74 75 76 77 78 79 80 |
# 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) ⇒ Object
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/device/network.rb', line 168 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 ⇒ Object
90 91 92 |
# File 'lib/device/network.rb', line 90 def self.disconnect adapter.disconnect end |
.ethernet? ⇒ Boolean
275 276 277 |
# File 'lib/device/network.rb', line 275 def self.ethernet? Device::Setting.media == "ethernet" end |
.gprs? ⇒ Boolean
267 268 269 |
# File 'lib/device/network.rb', line 267 def self.gprs? Device::Setting.media == "gprs" end |
.init(type, options) ⇒ Object
57 58 59 |
# File 'lib/device/network.rb', line 57 def self.init(type, ) adapter.init(type, ) end |
.load_metadata ⇒ Object
223 224 225 226 227 |
# File 'lib/device/network.rb', line 223 def self. if Object.const_defined?(:CwMetadata) CwMetadata.load_variable if CwMetadata.respond_to?(:load_variable) end end |
.mac_address(media = nil) ⇒ String
Get mac address of the current connection or from the parameters sent
118 119 120 121 122 123 124 |
# 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) ⇒ Object
Convert string media configuration to a class
127 128 129 130 131 132 133 134 135 136 137 138 |
# 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) ⇒ Object
86 87 88 |
# File 'lib/device/network.rb', line 86 def self.ping(host, port) adapter.ping(host, port) end |
.power(command) ⇒ Object
65 66 67 |
# File 'lib/device/network.rb', line 65 def self.power(command) adapter.power(command) end |
.scan ⇒ Array
Scan for wifi aps available
160 161 162 163 164 165 166 |
# File 'lib/device/network.rb', line 160 def self.scan if wifi? ThreadScheduler.pausing_communication do adapter.scan if Device::Network.init(*self.config) end end end |
.setup ⇒ Object
241 242 243 |
# File 'lib/device/network.rb', line 241 def self.setup self.configured? && (Device::Network.configure(*self.config) == SUCCESS) end |
.shutdown ⇒ Object
229 230 231 232 233 234 |
# File 'lib/device/network.rb', line 229 def self.shutdown if self.adapter.started? Device::Network.disconnect Device::Network.power(0) end end |
.signal ⇒ Fixnum
Check signal value
103 104 105 106 107 |
# File 'lib/device/network.rb', line 103 def self.signal if self.connected? adapter.signal end end |
.sim_id ⇒ Object
94 95 96 97 98 |
# File 'lib/device/network.rb', line 94 def self.sim_id if self.adapter.started? adapter.sim_id end end |