Class: CoAP::Message
- Inherits:
-
Struct
- Object
- Struct
- CoAP::Message
- Defined in:
- lib/coap/message.rb
Instance Attribute Summary collapse
-
#mcode ⇒ Object
Returns the value of attribute mcode.
-
#mid ⇒ Object
Returns the value of attribute mid.
-
#options ⇒ Object
Returns the value of attribute options.
-
#payload ⇒ Object
Returns the value of attribute payload.
-
#tt ⇒ Object
Returns the value of attribute tt.
-
#ver ⇒ Object
Returns the value of attribute ver.
Class Method Summary collapse
- .decode_options_and_put_together(b1, tt, mcode, mid, options, payload) ⇒ Object
- .deklausify(d, dpos, len) ⇒ Object
- .parse(d) ⇒ Object
Instance Method Summary collapse
-
#initialize(*args) ⇒ Message
constructor
convenience: .new(tt?, mcode?, mid?, payload?, hash).
- #klausify(n) ⇒ Object
- #mcode_readable ⇒ Object
- #prepare_options ⇒ Object
- #to_wire ⇒ Object
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
#mcode ⇒ Object
Returns the value of attribute mcode
5 6 7 |
# File 'lib/coap/message.rb', line 5 def mcode @mcode end |
#mid ⇒ Object
Returns the value of attribute mid
5 6 7 |
# File 'lib/coap/message.rb', line 5 def mid @mid end |
#options ⇒ Object
Returns the value of attribute options
5 6 7 |
# File 'lib/coap/message.rb', line 5 def @options end |
#payload ⇒ Object
Returns the value of attribute payload
5 6 7 |
# File 'lib/coap/message.rb', line 5 def payload @payload end |
#tt ⇒ Object
Returns the value of attribute tt
5 6 7 |
# File 'lib/coap/message.rb', line 5 def tt @tt end |
#ver ⇒ Object
Returns the value of attribute 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.(b1, tt, mcode, mid, , payload) # check and decode option values = DEFAULTING_OPTIONS.dup .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 [oname] = decoder.call(v) else [k] = v # we don't know what that is -- keep it in raw end end new(b1, tt, mcode, mid, Hash[], 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 = 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 [onumber] << oval end [TOKEN_ON] = [token] if token != '' # d.bytesize = more than all the rest... (b1, tt, mcode, mid, , 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_readable ⇒ Object
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_options ⇒ Object
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 = {} .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 [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 [k] = Array(v) # store raw option end end end |
#to_wire ⇒ Object
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 = # puts "prepared_options: #{prepared_options}" token = (.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 = 0 .keys.sort.each do |k| fail "Bad Option Type #{k.inspect}" unless Integer === k && k >= 0 a = [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 |