Class: O3D3XX::Swupdate

Inherits:
Object
  • Object
show all
Defined in:
lib/o3d3xx/swupdate.rb

Constant Summary collapse

UPLOAD_SUCCESS =
{"Status"=>"3", "Msg"=>"SWUPDATE successful !", "Error"=>"0"}

Instance Method Summary collapse

Constructor Details

#initialize(settings = {}) ⇒ Swupdate

Returns a new instance of Swupdate.



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/o3d3xx/swupdate.rb', line 35

def initialize (settings={})
  raise 'No host name given !' if settings[:host].nil?
  @config = {
    :host => settings[:host],
  }
  if settings[:port].nil?
    @config[:port] = 8080
  else
    @config[:port] = settings[:port]
  end
  @base_uri = "http://#{@config[:host]}:#{@config[:port]}"
end

Instance Method Details

#not_in_swupdate?Boolean

Print out an error message when an error was raised

Returns:

  • (Boolean)


49
50
51
52
# File 'lib/o3d3xx/swupdate.rb', line 49

def not_in_swupdate?()
  puts 'FAILED'
  puts 'Device not in SWUPDATE mode?'
end

#query_statusObject

Query status from http server

Returns:

  • json representation of http status e.g. “Msg”=>“”, “Error”=>“0”



110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/o3d3xx/swupdate.rb', line 110

def query_status()
  rv = ''
  uri = URI.parse("#{@base_uri}/getstatus.json")
  begin
    response = Net::HTTP.get_response(uri)
    rv = JSON.parse(response.body)
    #Net::HTTP.get_print(uri)
  rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, Errno::ECONNREFUSED, EOFError,
      Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e
  end
  #puts rv
  rv
end

#read_status_empty(timeout = 5) ⇒ Object

Reads status queue empty on http server, i.e. will return status from server until two consecutive status values are identical.

Parameters:

  • timeout (defaults to: 5)

    Time to wait for http server to settle



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

def read_status_empty(timeout = 5)
  rv = false
  Timeout::timeout(timeout) do
    loop do
      rv1 = query_status
      rv2 = query_status
      break if rv1 == rv2
    end
    rv = true
  end
  rv
end

#restart_deviceObject

Restarts device



151
152
153
154
155
156
157
# File 'lib/o3d3xx/swupdate.rb', line 151

def restart_device()
  uri = URI.parse("#{@base_uri}/reboot_to_live")
  http = Net::HTTP.new(uri.host, uri.port)
  request = Net::HTTP::Post.new(uri.request_uri)
  request['Connection'] = 'keep-alive'
  http.request(request)
end

#upload_file(filename) ⇒ Object

Uploads a file to swupdate system. So far this file has to be a swu image file. This call is asynchronous to the following installation procedure, i.e. one has to poll for installation finish via query_status()

Parameters:

  • filename

    filename of swu image to install on target



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/o3d3xx/swupdate.rb', line 61

def upload_file(filename)
  res = false
  raise 'Invalid file name given !' unless File.exist?(filename)
  uri = URI.parse("#{@base_uri}/handle_post_request")
  http = Net::HTTP.new(uri.host, uri.port)
  header = {
    'Content-Type'=> 'application/octet-stream',
    'X_FILENAME'=> "#{File.basename(filename)}",
  }
  request = Net::HTTP::Post.new(uri.request_uri,header)
  request.body = File.read(filename)
  begin
    http.request(request) { |response|
      break
    }
    res = true
  rescue Errno::ECONNRESET
    not_in_swupdate?
  rescue Errno::ECONNREFUSED
    not_in_swupdate?
  end

  res
end

#wait_for_status(status_hash, timeout) ⇒ Object

Waits for specific status of http server with max. timeout

Parameters:

  • status_hash

    Hash of status values, e.g. “Msg”=>“”, “Error”=>“0”

  • timeout

    Time in seconds to wait for given status to be returned from http server



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/o3d3xx/swupdate.rb', line 132

def wait_for_status(status_hash, timeout)
  rv = false
  Timeout::timeout(timeout) do
    loop do
      rv1 = query_status
      break if rv1 == status_hash
      # Print error messages
      if rv1['Status'] && (rv1['Status'] == '4')
        puts rv1['Msg'] if rv1['Msg']
      end
      sleep 1
    end
    rv = true
  end
  rv
end