Module: RubyHome::HAP::TLV

Extended by:
TLV
Included in:
TLV
Defined in:
lib/ruby_home/hap/tlv.rb

Defined Under Namespace

Classes: Bytes, Payload, TLV, UTF8String

Constant Summary collapse

TYPE_NAMES =
{
  0 => 'kTLVType_Method',
  1 => 'kTLVType_Identifier',
  2 => 'kTLVType_Salt',
  3 => 'kTLVType_PublicKey',
  4 => 'kTLVType_Proof',
  5 => 'kTLVType_EncryptedData',
  6 => 'kTLVType_State',
  7 => 'kTLVType_Error',
  8 => 'kTLVType_RetryDelay',
  9 => 'kTLVType_Certificate',
 10 => 'kTLVType_Signature',
 11 => 'kTLVType_Permissions',
 12 => 'kTLVType_FragmentData',
 13 => 'kTLVType_FragmentLast',
}.freeze
NAME_TYPES =
TYPE_NAMES.invert.freeze
READER =
BinData::Array.new(type: :tlv, read_until: :eof)

Instance Method Summary collapse

Instance Method Details

#encode(hash) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/ruby_home/hap/tlv.rb', line 76

def encode(hash)
  hash.to_hash.each_with_object(String.new) do |(key, value), memo|
    type_id = NAME_TYPES[key]
    next unless type_id

    if value.is_a?(String)
      value.scan(/.{1,255}/m)
    else
      [value]
    end.each do |frame_value|
      tlv = TLV.new(type_id: type_id, val: frame_value).tap do |tlv|
        tlv.len = tlv.val.to_binary_s.length
      end
      memo << tlv.to_binary_s
    end
  end
end

#read(input) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/ruby_home/hap/tlv.rb', line 61

def read(input)
  READER.clear
  READER.read(input)
  READER.snapshot.each_with_object({}) do |(hash), memo|
    type = TYPE_NAMES[hash[:type_id]]
    next unless type

    if memo[type]
      memo[type] << hash[:val]
    else
      memo[type] = hash[:val]
    end
  end
end