Class: Msp430Bsl::Response
- Inherits:
-
Object
- Object
- Msp430Bsl::Response
- Defined in:
- lib/msp430_bsl/response.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#kind ⇒ Object
readonly
Returns the value of attribute kind.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(payload) ⇒ Response
constructor
A new instance of Response.
- #is_data? ⇒ Boolean
- #is_message? ⇒ Boolean
- #is_ok_given_command?(command) ⇒ Boolean
Constructor Details
#initialize(payload) ⇒ Response
Returns a new instance of Response.
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
#data ⇒ Object (readonly)
Returns the value of attribute data.
12 13 14 |
# File 'lib/msp430_bsl/response.rb', line 12 def data @data end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
12 13 14 |
# File 'lib/msp430_bsl/response.rb', line 12 def errors @errors end |
#kind ⇒ Object (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
Instance Method Details
#is_data? ⇒ 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
33 34 35 |
# File 'lib/msp430_bsl/response.rb', line 33 def kind == Configs::CMD_KINDS[:message][:code] end |
#is_ok_given_command?(command) ⇒ 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 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 |