Class: Cosmos::LincHandshake

Inherits:
Object
  • Object
show all
Defined in:
lib/cosmos/interfaces/linc_interface.rb

Overview

The LincHandshake class is used only by the LincInterface. It processes the handshake and helps with finding the information regarding the internal command.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(handshake, interface_target_name) ⇒ LincHandshake

Returns a new instance of LincHandshake.



287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# File 'lib/cosmos/interfaces/linc_interface.rb', line 287

def initialize(handshake, interface_target_name)
  @handshake = handshake

  # Interpret the command field of the handshake packet
  # Where DATA is defined as:
  # 1 byte target name length
  # X byte target name
  # 1 byte packet name length
  # X byte packet name
  # 4 byte packet data length
  # X byte packet data
  # 4 byte error source length
  # X byte error source
  data = handshake.read('DATA')
  raise "Data field too short for target name length" if data.length == 0

  # Get target name length
  target_name_length = data[0..0].unpack('C')[0]
  raise "Invalid target name length" if target_name_length == 0
  data = data[1..-1]

  # get target name
  raise "Data field too short for target name" if data.length < target_name_length
  # target_name = data[0..(target_name_length - 1)] # Unused
  data = data[target_name_length..-1]

  # get packet name length
  raise "Data field too short for packet name length" if data.length == 0
  packet_name_length = data[0..0].unpack('C')[0]
  raise "Invalid packet name length" if packet_name_length == 0
  data = data[1..-1]

  # get packet name
  raise "Data field too short for packet name" if data.length < packet_name_length
  packet_name = data[0..(packet_name_length - 1)]
  data = data[packet_name_length..-1]

  # get packet data length
  raise "Data field too short for packet data length" if data.length < 4
  packet_data_length = data[0..3].unpack('N')[0]
  raise "Invalid data length" if packet_data_length == 0
  data = data[4..-1]

  # get packet data
  raise "Data field too short for packet data" if data.length < packet_data_length
  packet_data = data[0..(packet_data_length - 1)]
  data = data[packet_data_length..-1]

  # get error source length
  raise "Data field too short for error source length" if data.length < 4
  error_source_length = data[0..3].unpack('N')[0]
  # note it is OK to have a 0 source length
  data = data[4..-1]

  # get error source - store on object
  if error_source_length > 0
    @error_source = data[0..(error_source_length - 1)]
  else
    @error_source = ''
  end

  # make packet - store on object as a defined packet of type command that this handshakes
  @identified_command = System.commands.packet(interface_target_name, packet_name).clone
  @identified_command.buffer = packet_data
  @identified_command.received_time = Time.at(handshake.read('TIME_SECONDS'), handshake.read('TIME_MICROSECONDS'))
end

Instance Attribute Details

#error_sourceObject

Returns the value of attribute error_source.



285
286
287
# File 'lib/cosmos/interfaces/linc_interface.rb', line 285

def error_source
  @error_source
end

#handshakeObject

Returns the value of attribute handshake.



283
284
285
# File 'lib/cosmos/interfaces/linc_interface.rb', line 283

def handshake
  @handshake
end

#identified_commandObject

Returns the value of attribute identified_command.



284
285
286
# File 'lib/cosmos/interfaces/linc_interface.rb', line 284

def identified_command
  @identified_command
end

Instance Method Details

#get_cmd_guid(fieldname_guid) ⇒ Object



354
355
356
# File 'lib/cosmos/interfaces/linc_interface.rb', line 354

def get_cmd_guid(fieldname_guid)
  return @identified_command.read(fieldname_guid)
end