Class: CoAP::Message

Inherits:
Struct
  • Object
show all
Defined in:
lib/coap/message.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Message

convenience: .new(tt?, mcode?, mid?, payload?, hash)



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/coap/message.rb', line 6

def initialize(*args)     # convenience: .new(tt?, mcode?, mid?, payload?, hash)
  if args.size < 6
    h = args.pop.dup
    tt = h.delete(:tt) || args.shift
    mcode = h.delete(:mcode) || args.shift
    case mcode
    when Integer then mcode = METHODS[mcode] || [mcode >> 5, mcode & 0x1f]
    when Float then mcode = [mcode.to_i, (mcode * 100 % 100).round] # accept 2.05 and such
    end
    mid = h.delete(:mid) || args.shift
    payload = h.delete(:payload) || args.shift || EMPTY # no payload = empty payload
    fail 'CoRE::CoAPMessage.new: hash or all args' unless args.empty?
    super(1, tt, mcode, mid, h, payload)
  else
    super
  end
end

Instance Attribute Details

#mcodeObject

Returns the value of attribute mcode

Returns:

  • (Object)

    the current value of mcode



5
6
7
# File 'lib/coap/message.rb', line 5

def mcode
  @mcode
end

#midObject

Returns the value of attribute mid

Returns:

  • (Object)

    the current value of mid



5
6
7
# File 'lib/coap/message.rb', line 5

def mid
  @mid
end

#optionsObject

Returns the value of attribute options

Returns:

  • (Object)

    the current value of options



5
6
7
# File 'lib/coap/message.rb', line 5

def options
  @options
end

#payloadObject

Returns the value of attribute payload

Returns:

  • (Object)

    the current value of payload



5
6
7
# File 'lib/coap/message.rb', line 5

def payload
  @payload
end

#ttObject

Returns the value of attribute tt

Returns:

  • (Object)

    the current value of tt



5
6
7
# File 'lib/coap/message.rb', line 5

def tt
  @tt
end

#verObject

Returns the value of attribute ver

Returns:

  • (Object)

    the current value of ver



5
6
7
# File 'lib/coap/message.rb', line 5

def ver
  @ver
end

Class Method Details

.decode_options_and_put_together(b1, tt, mcode, mid, options, payload) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/coap/message.rb', line 87

def self.decode_options_and_put_together(b1, tt, mcode, mid, options, payload)
  # check and decode option values
  decoded_options = DEFAULTING_OPTIONS.dup
  options.each_pair do |k, v|
    if oinfo = OPTIONS[k]
      oname, _, minmax, repeatable, decoder, _ = *oinfo
      repeatable or v.size <= 1 or
        fail ArgumentError, "repeated unrepeatable option #{oname}"
      v.each do |v1|
        unless minmax === v1.bytesize
          fail ArgumentError, "#{v1.inspect} out of #{minmax} for #{oname}"
        end
      end
      decoded_options[oname] = decoder.call(v)
    else
      decoded_options[k] = v # we don't know what that is -- keep it in raw
    end
  end

  new(b1, tt, mcode, mid, Hash[decoded_options], payload) # XXX: why Hash[] again?
end

.deklausify(d, dpos, len) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/coap/message.rb', line 33

def self.deklausify(d, dpos, len)
  case len
  when 0..12
    [len, dpos]
  when 13
    [d.getbyte(dpos) + 13, dpos += 1]
  when 14
    [d.byteslice(dpos, 2).unpack('n')[0] + 269, dpos += 2]
  else
    fail "[#{d.inspect}] Bad delta/length nibble #{len} at #{dpos}"
  end
end

.parse(d) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/coap/message.rb', line 46

def self.parse(d)
  # dpos keeps our current position in parsing d
  b1, mcode, mid = d.unpack('CCn'); dpos = 4
  toklen = b1 & 0xf
  token = d.byteslice(dpos, toklen); dpos += toklen
  b1 >>= 4
  tt = TTYPES[b1 & 0x3]
  b1 >>= 2
  fail ArgumentError, "unknown CoAP version #{b1}" unless b1 == 1
  mcode = METHODS[mcode] || [mcode >> 5, mcode & 0x1F]

  # collect options
  onumber = 0             # current option number
  options = Hash.new { |h, k| h[k] = [] }
  dlen = d.bytesize
  while dpos < dlen
    tl1 = d.getbyte(dpos); dpos += 1
    fail ArgumentError, "option is not there at #{dpos} with oc #{orig_numopt}" unless tl1 # XXX

    break if tl1 == 0xff

    odelta, dpos = deklausify(d, dpos, tl1 >> 4)
    olen, dpos = deklausify(d, dpos, tl1 & 0xF)

    onumber += odelta

    if dpos + olen > dlen
      fail ArgumentError, "#{olen}-byte option at #{dpos} -- not enough data in #{dlen} total"
    end

    oval = d.byteslice(dpos, olen); dpos += olen
    options[onumber] << oval
  end

  options[TOKEN_ON] = [token] if token != ''

  # d.bytesize = more than all the rest...
  decode_options_and_put_together(b1, tt, mcode, mid, options,
                                  d.byteslice(dpos, d.bytesize))
end

Instance Method Details

#klausify(n) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/coap/message.rb', line 130

def klausify(n)
  if n < 13
    [n, '']
  else
    n -= 13
    if n < 256
      [13, [n].pack('C')]
    else
      [14, [n - 256].pack('n')]
    end
  end
end

#mcode_readableObject



24
25
26
27
28
29
30
31
# File 'lib/coap/message.rb', line 24

def mcode_readable
  case mcode
  when Array
    "#{mcode[0]}.#{"%02d" % mcode[1]}"
  else
    mcode.to_s
  end
end

#prepare_optionsObject



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/coap/message.rb', line 109

def prepare_options
  prepared_options = {}
  options.each do |k, v|
    # puts "k = #{k.inspect}, oinfo_i = #{OPTIONS_I[k].inspect}"
    if oinfo_i = OPTIONS_I[k]
      onum, oname, defv, minmax, rep, _, encoder = *oinfo_i
      prepared_options[onum] = a = encoder.call(v)
      rep or a.size <= 1 or fail "repeated option #{oname} #{a.inspect}"
      a.each do |v1|
        unless minmax === v1.bytesize
          fail ArgumentError, "#{v1.inspect} out of #{minmax} for #{oname}"
        end
      end
    else
      fail ArgumentError, "#{k.inspect}: unknown option" unless Integer === k
      prepared_options[k] = Array(v) # store raw option
    end
  end
  prepared_options
end

#to_wireObject



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/coap/message.rb', line 143

def to_wire
  # check and encode option values
  prepared_options = prepare_options
  # puts "prepared_options: #{prepared_options}"

  token = (prepared_options.delete(TOKEN_ON) || [nil])[0] || ''
  # puts "TOKEN: #{token.inspect}" unless token

  b1 = 0x40 | TTYPES_I[tt] << 4 | token.bytesize
  b2 = METHODS_I[mcode] || (mcode[0] << 5) + mcode[1]
  result = [b1, b2, mid].pack('CCn')
  result << token

  # stuff options in packet
  onumber = 0
  num_encoded_options = 0

  prepared_options.keys.sort.each do |k|
    fail "Bad Option Type #{k.inspect}" unless Integer === k && k >= 0
    a = prepared_options[k]
    a.each do |v|
      # result << frob(k, v)
      odelta = k - onumber
      onumber = k

      odelta1, odelta2 = klausify(odelta)
      odelta1 <<= 4

      length1, length2 = klausify(v.bytesize)
      result << [odelta1 | length1].pack('C')
      result << odelta2
      result << length2
      result << v.dup.force_encoding(BIN)         # value
    end

  end

  if payload != ''
    result << 0xFF
    result << payload.dup.force_encoding(BIN)
  end
  result
end