Class: Msp430Bsl::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/msp430_bsl/response.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(payload) ⇒ Response

Returns a new instance of Response.

Raises:

  • (ArgumentError)


14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/msp430_bsl/response.rb', line 14

def initialize(payload)
  raise ArgumentError, 'payload must be an Array' unless payload.is_a?(Array)

  @kind = payload[0]
  raise Exceptions::Response::KindNotSupported, kind unless self.class.supports_kind?(kind)

  @data = payload[1..-1]

  if is_data?
    raise Exceptions::Response::WrongDataSize.new(data, Configs::CMD_KINDS[:data][:payload_min_size]) if data.size < Configs::CMD_KINDS[:data][:payload_min_size]
  else
    raise Exceptions::Response::WrongDataSize.new(data, Configs::CMD_KINDS[:message][:payload_size]) if data.size != Configs::CMD_KINDS[:message][:payload_size]
  end
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



12
13
14
# File 'lib/msp430_bsl/response.rb', line 12

def data
  @data
end

#errorsObject (readonly)

Returns the value of attribute errors.



12
13
14
# File 'lib/msp430_bsl/response.rb', line 12

def errors
  @errors
end

#kindObject (readonly)

Returns the value of attribute kind.



12
13
14
# File 'lib/msp430_bsl/response.rb', line 12

def kind
  @kind
end

Class Method Details

.supports_kind?(kind) ⇒ Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/msp430_bsl/response.rb', line 7

def supports_kind?(kind)
  Configs::CMD_KINDS.values.map { |val| val[:code] }.include? kind
end

Instance Method Details

#is_data?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/msp430_bsl/response.rb', line 29

def is_data?
  kind == Configs::CMD_KINDS[:data][:code]
end

#is_message?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/msp430_bsl/response.rb', line 33

def is_message?
  kind == Configs::CMD_KINDS[:message][:code]
end

#is_ok_given_command?(command) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/msp430_bsl/response.rb', line 37

def is_ok_given_command?(command)
  @errors = []

  # Verify if response kind is compatible with sent command
  unless kind == command.configs[:response][:kind]
    @errors << [:kind, "Kind NOK. Expected response kind: 0x#{command.configs[:response][:kind].to_hex_str} - got: 0x#{kind.to_hex_str}"]
  end
  # Check response exact data size
  if command.configs.has_key?(:data_size) && data.size != command.configs[:data_size]
    @errors << [:data_size, "Data size NOK. Expected data size to be exactly '#{command.configs[:data_size]}' bytes, got '#{data.size}' bytes"]
  end
  # Check response min data size
  if command.configs.has_key?(:data_size_min) && data.size < command.configs[:data_size_min]
    @errors << [:data_size_min, "Min data size NOK. Expected data to have at least a size of '#{command.configs[:min_data_size]}' bytes, got '#{data.size}' bytes"]
  end
  # If kind is "message" check response code
  if is_message?
    success_code = Configs::RESPONSE_MESSAGES[:success][:code]
    if data[0] != success_code  # First (and only) data byte is message code
      @errors << [:message_code, "Message code NOK. Expected message code '#{success_code}', got '#{data[0]}'"]
    end
  end

  @errors.empty?
end