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, api_mode = :API1) ⇒ 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
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
107
108
109
110
|
# File 'lib/apimode/frame/frame.rb', line 18
def Frame.new(source_io, api_mode = :API1)
stray_bytes = []
unless api_mode == :API1 or api_mode == :API2
raise "XBee api_mode must be either :API1 (non-escaped) or :API2 (escaped, default)"
end
until (start_delimiter = source_io.readchar.unpack('H*').join.to_i(16)) == 0x7e
print "DEBUG: #{start_delimiter} | " if $DEBUG
stray_bytes << start_delimiter
end
if $VERBOSE
puts "Got some stray bytes for ya: #{stray_bytes.map {|b| "0x%x" % b} .join(", ")}" unless stray_bytes.empty?
end
if(api_mode == :API1)
= source_io.read(3)
elsif(api_mode == :API2)
= source_io.read(2).xb_unescape
end
print "Reading ... header after start byte: #{header.unpack("C*").join(", ")} | " if $DEBUG
frame_remaining = frame_length = api_identifier = cmd_data = ""
if api_mode == :API2
if .length == 2
frame_length = .unpack("n").first
else
= source_io.readchar
if( == "\175")
+= source_io.readchar
= .xb_unescape
else
+=
end
frame_length = .unpack("n")
end
api_identifier = source_io.readchar
if(api_identifier == "\175")
api_identifier += source_io.readchar
api_identifier = api_identifier.xb_unescape
end
api_identifier = api_identifier.unpack("C").first
else
frame_length, api_identifier = .unpack("nC")
end
if $DEBUG then
print "Frame length: #{frame_length} | "
print "Api Identifier: #{api_identifier} | "
end
cmd_data_intended_length = frame_length - 1
if api_mode == :API2
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
else
while (cmd_data.length < cmd_data_intended_length)
cmd_data += source_io.read(cmd_data_intended_length)
end
data = api_identifier.chr + cmd_data
end
sent_checksum = source_io.getc.unpack('H*').join.to_i(16)
if $DEBUG then
print "Sent checksum: #{sent_checksum} | "
print "Received checksum: #{Frame.checksum(data)} | "
print "Payload: #{cmd_data.unpack("C*").join(", ")} | "
end
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
end
|