Class: Px4LogReader::MessageDescriptor
- Inherits:
-
Object
- Object
- Px4LogReader::MessageDescriptor
- Defined in:
- lib/px4_log_reader/message_descriptor.rb
Constant Summary collapse
- @@validation_level =
:strict
Instance Attribute Summary collapse
-
#field_list ⇒ Object
readonly
Returns the value of attribute field_list.
-
#format ⇒ Object
readonly
Returns the value of attribute format.
-
#format_specifier ⇒ Object
readonly
Returns the value of attribute format_specifier.
-
#length ⇒ Object
readonly
Returns the value of attribute length.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Class Method Summary collapse
Instance Method Summary collapse
- #from_message(message) ⇒ Object
-
#initialize(attrs = {}) ⇒ MessageDescriptor
constructor
A new instance of MessageDescriptor.
- #pack_message(fields) ⇒ Object
- #to_message ⇒ Object
- #to_s ⇒ Object
- #unpack_message(message_data) ⇒ Object
Constructor Details
#initialize(attrs = {}) ⇒ MessageDescriptor
Returns a new instance of MessageDescriptor.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/px4_log_reader/message_descriptor.rb', line 46 def initialize(attrs={}) if attrs.keys.uniq.sort == [:name,:type,:length,:format,:fields].uniq.sort @name = attrs[:name] @type = attrs[:type] @length = attrs[:length] @format = attrs[:format] @format_specifier = MessageDescriptor.build_format_specifier( @format ) fields = attrs[:fields] @field_list = MessageDescriptor.build_field_list( fields ) elsif attrs.size > 0 raise "Missing attributes" else @name = nil @type = nil @length = nil @format = nil @field_list = {} end end |
Instance Attribute Details
#field_list ⇒ Object (readonly)
Returns the value of attribute field_list.
43 44 45 |
# File 'lib/px4_log_reader/message_descriptor.rb', line 43 def field_list @field_list end |
#format ⇒ Object (readonly)
Returns the value of attribute format.
42 43 44 |
# File 'lib/px4_log_reader/message_descriptor.rb', line 42 def format @format end |
#format_specifier ⇒ Object (readonly)
Returns the value of attribute format_specifier.
44 45 46 |
# File 'lib/px4_log_reader/message_descriptor.rb', line 44 def format_specifier @format_specifier end |
#length ⇒ Object (readonly)
Returns the value of attribute length.
41 42 43 |
# File 'lib/px4_log_reader/message_descriptor.rb', line 41 def length @length end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
39 40 41 |
# File 'lib/px4_log_reader/message_descriptor.rb', line 39 def name @name end |
#type ⇒ Object (readonly)
Returns the value of attribute type.
40 41 42 |
# File 'lib/px4_log_reader/message_descriptor.rb', line 40 def type @type end |
Class Method Details
.build_field_list(fields) ⇒ Object
114 115 116 117 118 119 120 |
# File 'lib/px4_log_reader/message_descriptor.rb', line 114 def build_field_list( fields ) field_list = {} fields.each_with_index do |field,index| field_list[field] = index end field_list end |
.build_format_specifier(px4_format_string) ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/px4_log_reader/message_descriptor.rb', line 122 def build_format_specifier( px4_format_string ) format_specifier = '' px4_format_string.unpack('C*').each do |field_format| case field_format when 0x66 # 'f' format_specifier << 'F' when 0x71, 0x51 # 'q', 'Q' format_specifier << 'l!' when 0x69, 0x4C, 0x65 # 'i', 'L', 'e' format_specifier << 'l' when 0x49, 0x45 # 'I', 'E' format_specifier << 'L' when 0x62 # 'b' format_specifier << 'c' when 0x42, 0x4D # 'B', 'M' format_specifier << 'C' when 0x68, 0x63 # 'h', 'c' format_specifier << 's' when 0x48, 0x43 # 'H', 'C' format_specifier << 'S' when 0x6E # 'n' format_specifier << 'A4' when 0x4E # 'N' format_specifier << 'A16' when 0x5A # 'Z' format_specifier << 'A64' else raise InvalidDescriptorError.new( "Invalid format specifier: '#{ '0x%02X' % field_format }' in #{px4_format_string}") end end return format_specifier end |
Instance Method Details
#from_message(message) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/px4_log_reader/message_descriptor.rb', line 68 def ( ) if .descriptor.type != 0x80 raise InvalidDescriptorError.new( 'Invalid descriptor type for format specifier message' ) elsif .fields.count != 5 raise InvalidDescriptorError.new( "Invalid field count for format specifier message: expected 5 fields, found #{.fields.count}" ) end @type, @length, @name, @format, fields_string = .fields @format_specifier = MessageDescriptor.build_format_specifier( @format ) unless fields_string.empty? fields = fields_string.split(',') if (@@validation_level == :strict) && (fields.length != @format.length) puts "fields = #{fields_string}" puts "format = '#{@format}'" puts "length = #{@length}" puts "type = 0x#{'%02X'% @type}" raise InvalidDescriptorError.new( "Field count must match format length: expected #{@format.length}; found #{fields.length}") elsif (@@validation_level == :lenient) && (fields.length > @format.length) raise InvalidDescriptorError.new( "Field count is greater than format length: expected #{@format.length}; found #{fields.length}") else @field_list = MessageDescriptor.build_field_list( fields ) end end end |
#pack_message(fields) ⇒ Object
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/px4_log_reader/message_descriptor.rb', line 184 def ( fields ) if fields.count != @format.length raise "Descriptor format length must match message field count" end corrected_fields = fields.dup @format.unpack('C*').each_with_index do |field_format,index| case field_format when 0x4C # 'L' corrected_fields[index] /= 1.0E-7 when 0x63, 0x43, 0x45 # 'c', 'C', 'E' corrected_fields[index] /= 1.0E-2 else end end return corrected_fields.pack( @format_specifier ) end |
#to_message ⇒ Object
108 109 110 |
# File 'lib/px4_log_reader/message_descriptor.rb', line 108 def return LogMessage.new( FORMAT_MESSAGE, [ @type, @length, @name, @format, @fields_string ] ) end |
#to_s ⇒ Object
204 205 206 207 208 209 210 211 |
# File 'lib/px4_log_reader/message_descriptor.rb', line 204 def to_s puts "#{@name}( #{@type} ):" puts " length = #{@length}" puts " format = #{@format}" @field_list.each do |name,index| puts " field#{index} = #{name}" end end |
#unpack_message(message_data) ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/px4_log_reader/message_descriptor.rb', line 159 def ( ) if @format_specifier.nil? @format_specifier = MessageDescriptor.build_format_specifier( @format ) end fields = .unpack( @format_specifier ) @format.unpack('C*').each_with_index do |field_format,index| case field_format when 0x4C # 'L' fields[index] = fields[index] * 1.0E-7 when 0x63, 0x43, 0x45 # 'c', 'C', 'E' fields[index] = fields[index] * 1.0E-2 else end end if fields.nil? || fields.empty? raise "No fields" end return LogMessage.new( self, fields ) end |