Module: SDN::Message::Helpers
- Included in:
- SDN::Message, ILT2::MasterControl
- Defined in:
- lib/sdn/message/helpers.rb
Overview
Shared conversions for SDN addresses, encoded parameters, strings, and checksums.
Instance Method Summary collapse
-
#checksum(bytes) ⇒ (Integer, Integer)
Computes the SDN checksum bytes for the provided payload.
-
#from_number(number, bytes = 1) ⇒ <Integer>
Converts an integer into SDN-transformed bytes.
-
#from_string(string, bytes) ⇒ <Integer>
Encodes a string into a fixed-width SDN byte array.
-
#group_address?(addr_bytes) ⇒ true, false
Checks whether an address is a group address.
-
#node_type_from_number(number) ⇒ Symbol, Integer
Maps a numeric node type identifier to a symbolic name when known.
-
#node_type_to_number(type) ⇒ Integer
Maps a symbolic node type name to its numeric protocol identifier.
-
#node_type_to_string(type) ⇒ String
Formats a node type for inspection output.
-
#parse_address(addr_string) ⇒ (Integer, Integer, Integer)
Converts a printable SDN address into its byte representation.
-
#print_address(addr_bytes) ⇒ String
Formats an address byte array using dotted hexadecimal notation.
-
#to_number(param, nillable: false) ⇒ Integer?
Converts transformed SDN bytes into an integer.
-
#to_string(param) ⇒ String
Decodes transformed SDN bytes into a trimmed string.
-
#transform_param(param) ⇒ <Integer>
Applies the SDN byte inversion and endianness transformation to a parameter value.
Instance Method Details
#checksum(bytes) ⇒ (Integer, Integer)
Computes the SDN checksum bytes for the provided payload.
140 141 142 143 |
# File 'lib/sdn/message/helpers.rb', line 140 def checksum(bytes) result = bytes.sum [result >> 8, result & 0xff] end |
#from_number(number, bytes = 1) ⇒ <Integer>
Converts an integer into SDN-transformed bytes.
104 105 106 107 108 109 110 111 |
# File 'lib/sdn/message/helpers.rb', line 104 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) ⇒ <Integer>
Encodes a string into a fixed-width SDN byte array.
129 130 131 132 133 |
# File 'lib/sdn/message/helpers.rb', line 129 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) ⇒ true, false
Checks whether an address is a group address.
30 31 32 |
# File 'lib/sdn/message/helpers.rb', line 30 def group_address?(addr_bytes) addr_bytes[0..1] == [1, 1] end |
#node_type_from_number(number) ⇒ Symbol, Integer
Maps a numeric node type identifier to a symbolic name when known.
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/sdn/message/helpers.rb', line 39 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) ⇒ Integer
Maps a symbolic node type name to its numeric protocol identifier.
56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/sdn/message/helpers.rb', line 56 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) ⇒ String
Formats a node type for inspection output.
73 74 75 |
# File 'lib/sdn/message/helpers.rb', line 73 def node_type_to_string(type) type.is_a?(Integer) ? format("%02xh", type) : type.inspect end |
#parse_address(addr_string) ⇒ (Integer, Integer, Integer)
Converts a printable SDN address into its byte representation.
12 13 14 |
# File 'lib/sdn/message/helpers.rb', line 12 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) ⇒ String
Formats an address byte array using dotted hexadecimal notation.
21 22 23 |
# File 'lib/sdn/message/helpers.rb', line 21 def print_address(addr_bytes) format("%02X.%02X.%02X", *addr_bytes) end |
#to_number(param, nillable: false) ⇒ Integer?
Converts transformed SDN bytes into an integer.
92 93 94 95 96 |
# File 'lib/sdn/message/helpers.rb', line 92 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) ⇒ String
Decodes transformed SDN bytes into a trimmed string.
118 119 120 121 |
# File 'lib/sdn/message/helpers.rb', line 118 def to_string(param) chars = param.map { |b| 0xff - b } chars.pack("C*").sub(/\0+$/, "").strip end |
#transform_param(param) ⇒ <Integer>
Applies the SDN byte inversion and endianness transformation to a parameter value.
82 83 84 |
# File 'lib/sdn/message/helpers.rb', line 82 def transform_param(param) Array(param).reverse.map { |byte| 0xff - byte } end |