Class: Samsung::Protocol::Frame

Inherits:
Object
  • Object
show all
Defined in:
lib/samsung/protocol/frame.rb

Direct Known Subclasses

AuthFrame, HeaderFrame, KeyFrame, Response

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = '') ⇒ Frame

Returns a new instance of Frame.



8
9
10
# File 'lib/samsung/protocol/frame.rb', line 8

def initialize data = ''
  @data = data
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



6
7
8
# File 'lib/samsung/protocol/frame.rb', line 6

def data
  @data
end

Instance Method Details

#hexObject



16
17
18
19
20
# File 'lib/samsung/protocol/frame.rb', line 16

def hex
  data.split('').map do |c|
    "0x%.2x" % c.ord
  end.join(' ')
end

#pop_frameObject



82
83
84
85
# File 'lib/samsung/protocol/frame.rb', line 82

def pop_frame
  frame_size = pop_int16
  Frame.new(data.slice!(0..frame_size-1))
end

#pop_int16Object



61
62
63
# File 'lib/samsung/protocol/frame.rb', line 61

def pop_int16
  data.slice!(0..1).unpack('S<').first
end

#pop_int32Object



71
72
73
# File 'lib/samsung/protocol/frame.rb', line 71

def pop_int32
  data.slice!(0..1).unpack('L<').first
end

#pop_int8Object



51
52
53
# File 'lib/samsung/protocol/frame.rb', line 51

def pop_int8
  data.slice!(0).unpack('C').first
end

#pop_stringObject



29
30
31
32
# File 'lib/samsung/protocol/frame.rb', line 29

def pop_string
  len = pop_int16
  data.slice!(0..len-1)
end

#pop_string64Object



40
41
42
43
# File 'lib/samsung/protocol/frame.rb', line 40

def pop_string64
  len = pop_int16
  Base64.decode64(data.slice!(0..len-1))
end

#push_frame(frame) ⇒ Object

Frames



77
78
79
80
# File 'lib/samsung/protocol/frame.rb', line 77

def push_frame(frame)
  push_int16(frame.size)
  self.data += frame.data
end

#push_int16(v) ⇒ Object

16-bit integer



57
58
59
# File 'lib/samsung/protocol/frame.rb', line 57

def push_int16(v)
  self.data += [v].pack('S<')
end

#push_int32(v) ⇒ Object

32-bit integer



67
68
69
# File 'lib/samsung/protocol/frame.rb', line 67

def push_int32(v)
  self.data += [v].pack('L<')
end

#push_int8(v) ⇒ Object

8-bit integer



47
48
49
# File 'lib/samsung/protocol/frame.rb', line 47

def push_int8(v)
  self.data += [v].pack('C')
end

#push_string(s) ⇒ Object

String



24
25
26
27
# File 'lib/samsung/protocol/frame.rb', line 24

def push_string(s)
  push_int16(s.size)
  self.data += s
end

#push_string64(s) ⇒ Object

Base64 string



36
37
38
# File 'lib/samsung/protocol/frame.rb', line 36

def push_string64(s)
  push_string(Base64.encode64(s))
end

#sizeObject



12
13
14
# File 'lib/samsung/protocol/frame.rb', line 12

def size
  @data.size
end