Module: SDN::Message::Helpers
- Included in:
- SDN::Message, ILT2::MasterControl
- Defined in:
- lib/sdn/message/helpers.rb
Instance Method Summary collapse
- #checksum(bytes) ⇒ Object
- #from_number(number, bytes = 1) ⇒ Object
- #from_string(string, bytes) ⇒ Object
- #group_address?(addr_bytes) ⇒ Boolean
- #node_type_from_number(number) ⇒ Object
- #node_type_to_number(type) ⇒ Object
- #node_type_to_string(type) ⇒ Object
- #parse_address(addr_string) ⇒ Object
- #print_address(addr_bytes) ⇒ Object
- #to_number(param, nillable: false) ⇒ Object
- #to_string(param) ⇒ Object
- #transform_param(param) ⇒ Object
Instance Method Details
#checksum(bytes) ⇒ Object
76 77 78 79 |
# File 'lib/sdn/message/helpers.rb', line 76 def checksum(bytes) result = bytes.sum [result >> 8, result & 0xff] end |
#from_number(number, bytes = 1) ⇒ Object
56 57 58 59 60 61 62 63 |
# File 'lib/sdn/message/helpers.rb', line 56 def from_number(number, bytes = 1) number ||= (1**(bytes * 8)) - 1 number = number.to_i bytes.times.each_with_object([]) do |_, res| res << ((0xff - number) & 0xff) number >>= 8 end end |
#from_string(string, bytes) ⇒ Object
70 71 72 73 74 |
# File 'lib/sdn/message/helpers.rb', line 70 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 |
#group_address?(addr_bytes) ⇒ Boolean
14 15 16 |
# File 'lib/sdn/message/helpers.rb', line 14 def group_address?(addr_bytes) addr_bytes[0..1] == [1, 1] end |
#node_type_from_number(number) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/sdn/message/helpers.rb', line 18 def node_type_from_number(number) case number when 1 then :st50ilt2 when 2 then :st30 when 6 then :glydea when 7 then :st50ac when 8 then :st50dc when 0x70 then :lt50 else; number end end |
#node_type_to_number(type) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/sdn/message/helpers.rb', line 30 def node_type_to_number(type) case type when :st50ilt2 then 1 when :st30 then 2 when :glydea then 6 when :st50ac then 7 when :st50dc then 8 when :lt50 then 0x70 else; type end end |
#node_type_to_string(type) ⇒ Object
42 43 44 |
# File 'lib/sdn/message/helpers.rb', line 42 def node_type_to_string(type) type.is_a?(Integer) ? format("%02xh", type) : type.inspect end |
#parse_address(addr_string) ⇒ Object
6 7 8 |
# File 'lib/sdn/message/helpers.rb', line 6 def parse_address(addr_string) addr_string.match(/^(\h{2})[:.]?(\h{2})[:.]?(\h{2})$/).captures.map { |byte| byte.to_i(16) } end |
#print_address(addr_bytes) ⇒ Object
10 11 12 |
# File 'lib/sdn/message/helpers.rb', line 10 def print_address(addr_bytes) format("%02X.%02X.%02X", *addr_bytes) end |
#to_number(param, nillable: false) ⇒ Object
50 51 52 53 54 |
# File 'lib/sdn/message/helpers.rb', line 50 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
65 66 67 68 |
# File 'lib/sdn/message/helpers.rb', line 65 def to_string(param) chars = param.map { |b| 0xff - b } chars.pack("C*").sub(/\0+$/, "").strip end |
#transform_param(param) ⇒ Object
46 47 48 |
# File 'lib/sdn/message/helpers.rb', line 46 def transform_param(param) Array(param).reverse.map { |byte| 0xff - byte } end |