Skip to content

Commit

Permalink
feat: breakout CoAP header
Browse files Browse the repository at this point in the history
allows for improved payload processing as we can check if the data is possibly good before processing or buffering
  • Loading branch information
stakach committed Jul 29, 2023
1 parent 4c1f4ae commit 75cfb66
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 12 deletions.
4 changes: 2 additions & 2 deletions shard.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ version: 2.0
shards:
ameba:
git: https://github.com/veelenga/ameba.git
version: 0.14.2
version: 1.5.0

bindata:
git: https://github.com/spider-gazelle/bindata.git
version: 1.8.3
version: 1.11.1

dtls:
git: https://github.com/spider-gazelle/crystal-dtls.git
Expand Down
2 changes: 1 addition & 1 deletion shard.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: coap
version: 1.1.0
version: 1.2.0
crystal: ">= 0.36.1"

dependencies:
Expand Down
6 changes: 3 additions & 3 deletions src/coap/client.cr
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ class CoAP::Client
end

def self.new(uri : URI, tls : TLSContext = nil)
scheme = uri.scheme.not_nil!.downcase
scheme = uri.scheme.as(String).downcase
port = uri.port || URI.default_port(scheme) || raise("unable to infer CoAP port for #{uri}")
raise "TLS required for coaps" if tls == false && scheme == "coaps"
self.new(uri.host.not_nil!, port, tls)
self.new(uri.host.as(String), port, tls)
end

@mutex = Mutex.new(:reentrant)
Expand Down Expand Up @@ -131,7 +131,7 @@ class CoAP::Client
end

def exec!(request : CoAP::Request) : CoAP::ResponseHandler
exec(request).not_nil!
exec(request).as(CoAP::ResponseHandler)
end

# message_id => token id
Expand Down
8 changes: 4 additions & 4 deletions src/coap/client/request.cr
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ class CoAP::Request < HTTP::Request
message.code_detail = CoAP::MethodCode.parse(self.method.upcase).to_u8

options = self.path.split('/').compact_map { |segment| CoAP::Option.new.string(segment).type(CoAP::Options::Uri_Path) if segment.presence }
options << CoAP::Option.new.string(self.query.not_nil!).type(CoAP::Options::Uri_Query) if self.query.presence
options << CoAP::Option.new.string(self.query.as(String)).type(CoAP::Options::Uri_Query) if self.query.presence

if origin = self.headers.delete("Origin")
uri = URI.parse origin
default_port = URI.default_port(uri.scheme.not_nil!.downcase)
default_port = URI.default_port(uri.scheme.as(String).downcase)
port = uri.port || default_port || raise("unable to infer CoAP port for #{origin}")

options << CoAP::Option.new.string(uri.host.not_nil!).type(CoAP::Options::Uri_Host)
options << CoAP::Option.new.string(uri.host.as(String)).type(CoAP::Options::Uri_Host)
options << CoAP::Option.new.uri_port(port).type(CoAP::Options::Uri_Port) unless port == default_port
else
raise "no 'Origin' header provided"
Expand Down Expand Up @@ -79,7 +79,7 @@ class CoAP::Request < HTTP::Request
# Extract the payload
buffer = IO::Memory.new
buf = Bytes.new(64)
while ((bytes = data.read(buf)) > 0)
while (bytes = data.read(buf)) > 0
buffer.write(buf[0, bytes])
end
message.payload_data = buffer.to_slice
Expand Down
10 changes: 8 additions & 2 deletions src/coap/message.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ require "./option"
# coap:// default port 5683
# coaps:// default port 5684

# https://tools.ietf.org/html/rfc7252#section-3
class CoAP::Message < BinData
class CoAP::Header < BinData
endian big

enum Type
Expand All @@ -25,7 +24,14 @@ class CoAP::Message < BinData
enum_bits 2, type : Type = Type::Confirmable

bits 4, :token_length, value: ->{ token.size.to_u8 }
end
end

# https://tools.ietf.org/html/rfc7252#section-3
class CoAP::Message < CoAP::Header
endian big

bit_field do
# https://tools.ietf.org/html/rfc7252#section-12.1
enum_bits 3, code_class : CodeClass = CodeClass::Method

Expand Down

0 comments on commit 75cfb66

Please sign in to comment.