Module: XBee::Frame

Defined in:
lib/apimode/frame/frame.rb,
lib/apimode/frame/at_command.rb,
lib/apimode/frame/modem_status.rb,
lib/apimode/frame/receive_packet.rb,
lib/apimode/frame/transmit_status.rb,
lib/apimode/frame/transmit_request.rb,
lib/apimode/frame/at_command_response.rb,
lib/apimode/frame/explicit_rx_indicator.rb,
lib/apimode/frame/remote_command_request.rb,
lib/apimode/frame/remote_command_response.rb,
lib/apimode/frame/explicit_addressing_command.rb

Defined Under Namespace

Classes: ATCommand, ATCommandQueueParameterValue, ATCommandResponse, Base, ExplicitAddressingCommand, ExplicitRxIndicator, ModemStatus, ReceivePacket, ReceivedFrame, RemoteCommandRequest, RemoteCommandResponse, TransmitRequest, TransmitStatus

Class Method Summary collapse

Class Method Details

.checksum(data) ⇒ Object



14
15
16
# File 'lib/apimode/frame/frame.rb', line 14

def Frame.checksum(data)
  0xFF - (data.unpack("C*").inject(0) { |sum, byte| (sum + byte) & 0xFF })
end

.new(source_io) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/apimode/frame/frame.rb', line 18

def Frame.new(source_io)
  stray_bytes = []
  until (start_delimiter = source_io.readchar.unpack('H*').join.to_i(16)) == 0x7e
    #puts "Stray byte 0x%x" % start_delimiter
    print "DEBUG: #{start_delimiter} | " if $DEBUG
    stray_bytes << start_delimiter
  end
  puts "Got some stray bytes for ya: #{stray_bytes.map {|b| "0x%x" % b} .join(", ")}" unless stray_bytes.empty?
  header = source_io.read(3).xb_unescape
  print "Reading ... header after start byte: #{header.unpack("C*").join(", ")} | " if $DEBUG
  frame_remaining = frame_length = api_identifier = cmd_data = ""
  if header.length == 3
    frame_length, api_identifier = header.unpack("nC")
  else
    frame_length, api_identifier = header.unpack("n").first, source_io.readchar
  end
  #### DEBUG ####
  if $DEBUG then
  print "Frame length: #{frame_length} | "
  print "Api Identifier: #{api_identifier} | "
  end
  #### DEBUG ####
  cmd_data_intended_length = frame_length - 1
  while ((unescaped_length = cmd_data.xb_unescape.length) < cmd_data_intended_length)
    cmd_data += source_io.read(cmd_data_intended_length - unescaped_length)
  end
  data = api_identifier.chr + cmd_data.xb_unescape
  sent_checksum = source_io.getc.unpack('H*').join.to_i(16)
  #### DEBUG ####
  if $DEBUG then
  print "Sent checksum: #{sent_checksum} | "
  print "Received checksum: #{Frame.checksum(data)} | "
  print "Payload: #{cmd_data.unpack("C*").join(", ")} | "
  end
  #### DEBUG ####
  unless sent_checksum == Frame.checksum(data)
    raise "Bad checksum - data discarded"
  end
  case data[0].unpack('H*')[0].to_i(16)
    when 0x8A
      ModemStatus.new(data)
    when 0x88 
      ATCommandResponse.new(data)
    when 0x97 
      RemoteCommandResponse.new(data)
    when 0x8B 
      TransmitStatus.new(data)
    when 0x90 
      ReceivePacket.new(data)
    when 0x91 
      ExplicitRxIndicator.new(data)
    when 0x92
      IODataSampleRxIndicator.new(data)
    else
      ReceivedFrame.new(data)
  end
  rescue EOFError
    # No operation as we expect eventually something
end