Class: CoRE::CoAP::Message
- Inherits:
-
Struct
- Object
- Struct
- CoRE::CoAP::Message
- Defined in:
- lib/core/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_s ⇒ Object
- #to_wire ⇒ Object
Constructor Details
#initialize(*args) ⇒ Message
convenience: .new(tt?, mcode?, mid?, payload?, hash?)
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/core/coap/message.rb', line 7 def initialize(*args) # convenience: .new(tt?, mcode?, mid?, payload?, hash?) if args.size < 6 h = {} h = args.pop.dup if args.last.is_a? Hash tt = h.delete(:tt) || args.shift mcode = h.delete(:mcode) || args.shift # TODO Allow later setting of mcode by Float. case mcode when Integer mcode = METHODS[mcode] || [mcode >> 5, mcode & 0x1f] when Float 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 unless args.empty? raise 'CoRE::CoAP::Message.new: Either specify Hash or all arguments.' end super(1, tt, mcode, mid, h, payload) else super end end |
Instance Attribute Details
#mcode ⇒ Object
Returns the value of attribute mcode
6 7 8 |
# File 'lib/core/coap/message.rb', line 6 def mcode @mcode end |
#mid ⇒ Object
Returns the value of attribute mid
6 7 8 |
# File 'lib/core/coap/message.rb', line 6 def mid @mid end |
#options ⇒ Object
Returns the value of attribute options
6 7 8 |
# File 'lib/core/coap/message.rb', line 6 def @options end |
#payload ⇒ Object
Returns the value of attribute payload
6 7 8 |
# File 'lib/core/coap/message.rb', line 6 def payload @payload end |
#tt ⇒ Object
Returns the value of attribute tt
6 7 8 |
# File 'lib/core/coap/message.rb', line 6 def tt @tt end |
#ver ⇒ Object
Returns the value of attribute ver
6 7 8 |
# File 'lib/core/coap/message.rb', line 6 def ver @ver end |
Class Method Details
.decode_options_and_put_together(b1, tt, mcode, mid, options, payload) ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/core/coap/message.rb', line 124 def self.(b1, tt, mcode, mid, , payload) # check and decode option values = CoAP::DEFAULTING_OPTIONS.dup .each_pair do |k, v| if oinfo = CoAP::OPTIONS[k] oname, _, minmax, repeatable, decoder, _ = *oinfo repeatable or v.size <= 1 or raise ArgumentError, "repeated unrepeatable option #{oname}" v.each do |v1| unless minmax === v1.bytesize raise 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
146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/core/coap/message.rb', line 146 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 raise "[#{d.inspect}] Bad delta/length nibble #{len} at #{dpos}" end end |
.parse(d) ⇒ Object
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 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'lib/core/coap/message.rb', line 159 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 raise 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 raise 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 raise ArgumentError, "#{olen}-byte option at #{dpos} -- not enough data in #{dlen} total" end oval = d.byteslice(dpos, olen); dpos += olen [onumber] << oval end [CoAP::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
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/core/coap/message.rb', line 38 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
51 52 53 54 |
# File 'lib/core/coap/message.rb', line 51 def mcode_readable return "#{mcode[0]}.#{"%02d" % mcode[1]}" if mcode.is_a? Array mcode.to_s end |
#prepare_options ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/core/coap/message.rb', line 56 def = {} .each do |k, v| if oinfo_i = CoAP::OPTIONS_I[k] onum, oname, defv, minmax, rep, _, encoder = *oinfo_i [onum] = a = encoder.call(v) rep or a.size <= 1 or raise "repeated option #{oname} #{a.inspect}" a.each do |v1| unless minmax === v1.bytesize raise ArgumentError, "#{v1.inspect} out of #{minmax} for #{oname}" end end else raise ArgumentError, "#{k.inspect}: unknown option" unless Integer === k [k] = Array(v) # store raw option end end end |
#to_s ⇒ Object
76 77 78 79 80 81 |
# File 'lib/core/coap/message.rb', line 76 def to_s path = CoAP.path_encode(self.[:uri_path]) query = CoAP.query_encode(self.[:uri_query]) [tt, mcode_readable, path, query].join(' ').rstrip end |
#to_wire ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/core/coap/message.rb', line 83 def to_wire # check and encode option values = token = (.delete(CoAP::TOKEN_ON) || [nil])[0] || '' 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| raise "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(CoAP::BIN) # value end end if payload != '' && !payload.nil? result << 0xFF result << payload.dup.force_encoding(CoAP::BIN) end result end |