Module: SDN::Message::Helpers

Included in:
SDN::Message, ILT2::MasterControl
Defined in:
lib/sdn/message/helpers.rb

Instance Method Summary collapse

Instance Method Details

#checksum(bytes) ⇒ Object



75
76
77
78
# File 'lib/sdn/message/helpers.rb', line 75

def checksum(bytes)
  result = bytes.inject(&:+)
  [result >> 8, result & 0xff]
end

#from_number(number, bytes = 1) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/sdn/message/helpers.rb', line 54

def from_number(number, bytes = 1)
  number ||= 1 ** (bytes * 8) - 1
  number = number.to_i
  bytes.times.inject([]) do |res, _|
    res << (0xff - number & 0xff)
    number >>= 8
    res
  end
end

#from_string(string, bytes) ⇒ Object



69
70
71
72
73
# File 'lib/sdn/message/helpers.rb', line 69

def from_string(string, bytes)
  chars = string.bytes
  chars = chars[0...bytes].fill(' '.ord, chars.length, bytes - chars.length)
  chars.map { |b| 0xff - b }
end

#is_group_address?(addr_bytes) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/sdn/message/helpers.rb', line 12

def is_group_address?(addr_bytes)
  addr_bytes[0..1] == [1, 1]
end

#node_type_from_number(number) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/sdn/message/helpers.rb', line 16

def node_type_from_number(number)
  case number
  when 1; :st50ilt2
  when 2; :st30
  when 6; :glydea
  when 7; :st50ac
  when 8; :st50dc
  when 0x70; :lt50
  else; number
  end
end

#node_type_to_number(type) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/sdn/message/helpers.rb', line 28

def node_type_to_number(type)
  case type
  when :st50ilt2; 1
  when :st30; 2
  when :glydea; 6
  when :st50ac; 7
  when :st50dc; 8
  when :lt50; 0x70
  else; type
  end
end

#node_type_to_string(type) ⇒ Object



40
41
42
# File 'lib/sdn/message/helpers.rb', line 40

def node_type_to_string(type)
  type.is_a?(Integer) ? "%02xh" % type : type.inspect
end

#parse_address(addr_string) ⇒ Object



4
5
6
# File 'lib/sdn/message/helpers.rb', line 4

def parse_address(addr_string)
  addr_string.match(/^(\h{2})[:.]?(\h{2})[:.]?(\h{2})$/).captures.map { |byte| byte.to_i(16) }
end


8
9
10
# File 'lib/sdn/message/helpers.rb', line 8

def print_address(addr_bytes)
  "%02X.%02X.%02X" % addr_bytes
end

#to_number(param, nillable: false) ⇒ Object



48
49
50
51
52
# File 'lib/sdn/message/helpers.rb', line 48

def to_number(param, nillable: false)
  result = Array(param).reverse.inject(0) { |sum, byte| (sum << 8) + 0xff - byte }
  result = nil if nillable && result == (1 << (8 * Array(param).length)) - 1
  result
end

#to_string(param) ⇒ Object



64
65
66
67
# File 'lib/sdn/message/helpers.rb', line 64

def to_string(param)
  chars = param.map { |b| 0xff - b }
  chars.pack("C*").sub(/\0+$/, '').strip
end

#transform_param(param) ⇒ Object



44
45
46
# File 'lib/sdn/message/helpers.rb', line 44

def transform_param(param)
  Array(param).reverse.map { |byte| 0xff - byte }
end