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

.socket_sslObject

Returns the value of attribute socket_ssl.



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

def socket_ssl
  @socket_ssl
end

.socket_tcpObject

Returns the value of attribute socket_tcp.



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

def socket_tcp
  @socket_tcp
end

.sslObject

Returns the value of attribute ssl.



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

def ssl
  @ssl
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



41
42
43
# File 'lib/device/network.rb', line 41

def self.adapter
  Device.adapter::Network
end

.attachObject



146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/device/network.rb', line 146

def self.attach
  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
  ret
end

.close_socketObject



136
137
138
139
140
141
142
143
144
# File 'lib/device/network.rb', line 136

def self.close_socket
  @socket.close
  if @socket != @socket_tcp
    @socket_tcp.close
  end
  @socket     = nil
  @socket_tcp = nil
  @socket_ssl = nil
end

.configObject



159
160
161
162
# File 'lib/device/network.rb', line 159

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



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/device/network.rb', line 165

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:



62
63
64
# File 'lib/device/network.rb', line 62

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

.connectObject



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

def self.connect
  adapter.connect
end

.connected?Boolean

Returns:



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

def self.connected?
  return NO_CONNECTION unless self.adapter.started?
  adapter.connected?
end

.create_socketObject



132
133
134
# File 'lib/device/network.rb', line 132

def self.create_socket
  TCPSocket.new(Device::Setting.host, Device::Setting.host_port)
end

.dhcp_client(timeout) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/device/network.rb', line 74

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



70
71
72
# File 'lib/device/network.rb', line 70

def self.disconnect
  adapter.disconnect
end

.ethernet?Boolean

Returns:



194
195
196
# File 'lib/device/network.rb', line 194

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

.gprs?Boolean

Returns:



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

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

.handshake!Object



105
106
107
108
109
110
111
112
113
114
# File 'lib/device/network.rb', line 105

def self.handshake!
  handshake = "#{Device::System.serial};#{Device::System.app};#{Device::Setting.logical_number};#{Device.version}"
  socket.write("#{handshake.size.chr}#{handshake}")

  company_name = socket_tcp.closed? ? nil : socket.read(3)
  return false if company_name == "err" || company_name.nil?

  Device::Setting.company_name = company_name
  true
end

.handshake_ssl(tcp) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/device/network.rb', line 93

def self.handshake_ssl(tcp)
  return if tcp.closed?
  entropy = PolarSSL::Entropy.new
  ctr_drbg = PolarSSL::CtrDrbg.new entropy
  s_ssl = PolarSSL::SSL.new
  s_ssl.set_endpoint PolarSSL::SSL::SSL_IS_CLIENT
  s_ssl.set_rng ctr_drbg
  s_ssl.set_socket tcp
  s_ssl.handshake
  s_ssl
end

.handshake_ssl!Object



87
88
89
90
91
# File 'lib/device/network.rb', line 87

def self.handshake_ssl!
  @socket_ssl = self.handshake_ssl(@socket_tcp)
  @ssl = true
  @socket = @socket_ssl
end

.init(type, options) ⇒ Object



45
46
47
# File 'lib/device/network.rb', line 45

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

.ping(host, port) ⇒ Object



66
67
68
# File 'lib/device/network.rb', line 66

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

.power(command) ⇒ Object



49
50
51
# File 'lib/device/network.rb', line 49

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

.walk_socketObject

Create Socket in Walk Switch



117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/device/network.rb', line 117

def self.walk_socket
  if @socket
    @socket
  else
    @socket_tcp = create_socket
    if Device::Setting.ssl == "1"
      handshake_ssl!
    else
      @socket = @socket_tcp
    end
    handshake!
    @socket
  end
end

.wifi?Boolean

Returns:



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

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