[coap] add CoAP(s) block-wise transfer support to CoAP(s) client and server (#4247)

Implemented CoAP/-s block-wise transfer according to RFC 7959. The
implementation includes methods and functions available to the
application layer to add Block1 and Block2 option values to a coap
message and an automatic block-wise transfer enabled by the compile
switch "COAP_BLOCK".
This commit is contained in:
Aurelio Schellenbaum
2021-01-26 13:27:32 -08:00
committed by GitHub
parent a3b5e370be
commit 02d6d49a5e
26 changed files with 2310 additions and 63 deletions
+44 -1
View File
@@ -1884,6 +1884,12 @@ class NodeImpl:
"""
return self._coap_rq('get', ipaddr, uri, con, payload)
def coap_get_block(self, ipaddr, uri, size=16, count=0):
"""
Send a GET request via CoAP.
"""
return self._coap_rq_block('get', ipaddr, uri, size, count)
def coap_observe(self, ipaddr, uri, con=False, payload=None):
"""
Send a GET request via CoAP with Observe set.
@@ -1896,12 +1902,24 @@ class NodeImpl:
"""
return self._coap_rq('post', ipaddr, uri, con, payload)
def coap_post_block(self, ipaddr, uri, size=16, count=0):
"""
Send a POST request via CoAP.
"""
return self._coap_rq_block('post', ipaddr, uri, size, count)
def coap_put(self, ipaddr, uri, con=False, payload=None):
"""
Send a PUT request via CoAP.
"""
return self._coap_rq('put', ipaddr, uri, con, payload)
def coap_put_block(self, ipaddr, uri, size=16, count=0):
"""
Send a PUT request via CoAP.
"""
return self._coap_rq_block('put', ipaddr, uri, size, count)
def _coap_rq(self, method, ipaddr, uri, con=False, payload=None):
"""
Issue a GET/POST/PUT/DELETE/GET OBSERVE request.
@@ -1918,6 +1936,20 @@ class NodeImpl:
self.send_command(cmd)
return self.coap_wait_response()
def _coap_rq_block(self, method, ipaddr, uri, size=16, count=0):
"""
Issue a GET/POST/PUT/DELETE/GET OBSERVE BLOCK request.
"""
cmd = 'coap %s %s %s' % (method, ipaddr, uri)
cmd += ' block-%d' % size
if count is not 0:
cmd += ' %d' % count
self.send_command(cmd)
return self.coap_wait_response()
def coap_wait_response(self):
"""
Wait for a CoAP response, and return it.
@@ -1938,7 +1970,10 @@ class NodeImpl:
observe = int(observe, base=10)
if payload is not None:
payload = binascii.a2b_hex(payload).decode('UTF-8')
try:
payload = binascii.a2b_hex(payload).decode('UTF-8')
except UnicodeDecodeError:
pass
# Return the values received
return dict(source=source, observe=observe, payload=payload)
@@ -2004,6 +2039,14 @@ class NodeImpl:
self.send_command(cmd)
self._expect_done()
def coap_set_resource_path_block(self, path, count=0):
"""
Set the path for the CoAP resource and how many blocks can be received from this resource.
"""
cmd = 'coap resource %s %d' % (path, count)
self.send_command(cmd)
self._expect('Done')
def coap_set_content(self, content):
"""
Set the content of the CoAP resource.