Chapters

CoAP Block and Discovery Contracts: Reference

Return to the parent chapter.

Related reference: CoAP message types.

A device has received part of a 5,000-byte firmware file when a response is lost. Should it request the missing bytes, resume after waking, or discard what it saved? Work through the byte positions first, then check whether the server and cache still describe the same file.

A protocol defines message-exchange rules. CoAP means Constrained Application Protocol. Firmware is software installed on a device. A payload is the application data inside a message. A representation is the bytes supplied for a resource in a particular format and version; a block carries one numbered part of those bytes.

Reassemble the Image the Server Actually Sent

For the running example, assume the client already knows the firmware resource and the server keeps one version available. The file contains 5,000 bytes and the agreed block payload size is 1,024 bytes. Four full blocks carry 4×1,024=4,0964 \times 1{,}024 = 4{,}096 bytes; the fifth carries 5,000−4,096=9045{,}000 - 4{,}096 = 904 bytes.

Number the blocks from zero. Blocks 0, 1, 2 and 3 start at byte offsets 0, 1,024, 2,048 and 3,072. The final block, number 4, starts at 4,096. The More flag, written M, is 1 on the first four data blocks and 0 on the final one. A final block may be shorter than the agreed size.

Now lose block 2’s response. Request block 2 again and place its bytes at offset 2,048. Do not append a duplicate response at the end of the file. This recovery works only while the saved blocks and repeated response belong to the same representation.

A validator identifies a representation so a client can check whether it changed. An ETag is one such validator: an identifier the client can compare across responses. If the server returns a different ETag during this download, stop combining blocks and restart against a consistent version. A matching ETag supports continuity; it is not a signature proving who authorized the firmware.

Read the figure in the parent chapter as a sequence of decisions: locate the resource, request a numbered part, place the response at its byte offset, and check continuity before accepting the next part. A digest, or hash, is a value calculated from the file bytes for comparison with the expected file. A digital signature supports checking the firmware against a trusted signing identity. After assembly, check the expected length, digest and firmware signature under the update policy before installation. Reaching 5,000 bytes alone does not make an image trustworthy.

Start With the Broken Firmware Download

Resume One Download Without Trusting Stale Details

A proxy is an intermediary that exchanges requests with the origin server on a client’s behalf. It may keep a cache, a stored response reused when the request and caching rules permit it. When the device wakes, both its saved blocks and the proxy’s metadata need checking.

Use the 5,000-byte fixture to test the decision before relying on a real update. These are test instructions, not a claim that a transfer was run here.

  1. Capture discovery and the block responses. Expect blocks 0–4, four full payloads and a 904-byte final payload. A different total or offset means the fixture, size choice or assembly is wrong.
  2. Drop block 2’s response and repeat its request. Expect the same bytes at offset 2,048 while the representation remains unchanged. An extra appended segment means duplicate placement is wrong; changed bytes require a continuity check.
  3. Restart the client with blocks 0 and 1 saved at offsets 0 and 1,024 (2,048 bytes total), ETag fw-A, and the recorded firmware URI, format and 1,024-byte block size. In this fixture, the proxy’s discovery response has Max-Age 45 seconds and age 50 seconds: it is stale by 5 seconds. Revalidate discovery; suppose 2.03 Valid refreshes it and the firmware link remains present. Before reading the outcomes below, choose retry, resume or restart for a resumed Block2 response with NUM 2, SZX 6 and ETag fw-A, then choose again if its ETag is fw-B. Explain whether refreshed discovery alone permits reuse of the saved firmware bytes. If the URI, format, size, offsets or validator evidence is missing, discard partial assembly and restart.
  4. Compare the outcomes. With fw-A and the recorded transfer identity unchanged, resume by requesting block 2 and placing its 1,024 bytes at offset 2,048; expect 3,072 assembled bytes with no duplicate segment. A lost response during that same transfer calls for a retry of block 2 at the same offset. With fw-B, stop combining blocks, discard the saved fw-A assembly and restart against the new representation; no fw-A bytes may enter the new assembly. Refreshed discovery validates the link list, not the firmware representation. Installing mixed versions is a failed continuity test even if the final length is correct.
  5. Remove the firmware link from discovery and query again. Expect the new discovery result to omit that link. Test direct access separately: removing a link does not revoke permission to a known resource address.

Retain the responses, cache age and Max-Age, discovery revalidation, repeated requests, firmware validators, assembled length, digest, signature result and install decision. Each record answers a different part of the resume decision.

Learning Objectives

After this page, you should be able to:

  • Decide whether a partial download can resume, using saved offsets and representation identity.
  • Choose Block1 for request data or Block2 for response data, then decode NUM, M and SZX against the trace.
  • Use typed discovery links to select a resource while checking access permission separately.
  • Decide whether a proxy may reuse a response, must revalidate it, or must replace its cached bytes.

Why This Contract Comes After Advanced Features

CoAP Advanced Features introduces the feature choices. Here, the missing-block example turns them into a decision procedure: locate the resource, map blocks to bytes, check representation continuity, then check cache freshness and installation evidence.

Use the same procedure for a log upload or a cached sensor reading. The direction of transfer and the final application check change; byte placement and evidence still need an explicit contract.

Overview: Block-wise Transfer Moves Big Payloads Through Small Datagrams

On the UDP transport discussed here, a CoAP message occupies a UDP datagram. A datagram is one network-carried message unit. Splitting the firmware body into CoAP blocks lets the client request a missing portion rather than depend on reassembling one large fragmented packet. Losing a required network fragment prevents that packet from being reassembled.

Block2, option 23, controls a response body: the firmware bytes returned by GET, the method used to retrieve a resource. Block1, option 27, controls a request body: for example, log bytes sent using PUT (create or replace at an address) or POST (submit data for processing). Each block participates in a request/response exchange. A protocol retransmission and a later application request for the same block are distinct events; neither should cause duplicate bytes in the assembled object.

In the running download, request Block2 number 2 again after its response is lost. Keep blocks 0 and 1 only if they still belong to the same representation. A restart does not automatically preserve a usable transfer: record what was saved and how its identity will be checked before resuming.

Inspect the figure in the parent chapter for the link between a numbered request and the bytes retained by the client.

Follow the figure in the parent chapter from the requested block to its position in the assembled file. Retry places bytes in that position again; validator checks decide whether earlier bytes remain usable. The final length and digest checks follow assembly, rather than replacing continuity checks.

Block2 (option 23)

For the firmware GET, request response blocks 0 through 4. The final data block clears M; its payload contains the remaining 904 bytes.

Block1 (option 27)

For a log upload, split the request body across blocks. Check the server response before advancing or declaring the upload complete.

Failure isolation

Repeat the missing block when saved bytes and representation identity remain valid. Lost state or a changed version can require a restart.

No IP fragmentation

Choose a block size whose complete message, including headers and security overhead, fits the path limits. Block-wise transfer alone does not guarantee the absence of fragmentation.

Practitioner: The Block Option Encodes NUM, More, and a Size Exponent

For the firmware response, NUM is the block number, M indicates whether more data blocks follow, and SZX encodes the selected block size. With SZX 6, size is 2^(6 + 4) = 1024 bytes. NUM 4 therefore starts at 4 × 1024 = 4096; M 0 ends the body after its 904-byte payload. SZX sets the block size, not the exact length of a short final payload.

Transfer example. An 18,432-byte log upload uses Block1 with SZX 4, or 2^(4 + 4) = 256 bytes per block. It needs 18,432 / 256 = 72 full blocks. NUM 31 starts at 31 × 256 = 7936 with M 1. NUM 71 starts at 71 × 256 = 18176 and carries the final 256 bytes with M 0. Compare byte offsets even when a library supplies the option fields.

SZX
Block size
Blocks for 4,096 bytes
Trade-off
2
64 bytes
64 blocks
64-byte payload buffer; 64 blocks to exchange.
4
256 bytes
16 blocks
256-byte payload buffer; 16 blocks to exchange.
6
1024 bytes
4 blocks
1,024-byte payload buffer; check full-message path fit.

For ordinary UDP blocks, SZX values 0 through 6 select sizes from 16 through 1,024 bytes using 2^(SZX + 4). A smaller size can be negotiated when buffers or path limits require it. If size changes, recalculate NUM from the required byte position; continuing the old number with a new size points to different bytes. The table assumes a fixed size throughout a 4,096-byte body.

In a successful continuing Block1 upload, 2.31 Continue asks for further request data; it does not prove final application acceptance. Check the final operation response, such as 2.01 Created or 2.04 Changed on success. An incomplete request can produce 4.08 Request Entity Incomplete; an oversized request can produce 4.13 Request Entity Too Large, possibly with a smaller size hint. Specify upload identity and commit behavior separately: the Block2 ETag example does not establish a generic upload-resume contract.

Under the Hood: Discovery via CoRE Link Format, and Caching Through Proxies

A resource address is a URI. To find advertised resource addresses, request GET /.well-known/core from a server that supports discovery. Its CoRE Link Format response is a comma-separated list of links and attributes, sent as Content-Format 40 (application/link-format). Content-Format identifies the payload format. A link names a resource; its attributes describe it.

For example, </sensors/temp>;rt="temperature";if="sensor";ct=50;obs advertises a temperature resource. Here rt names the resource type, if describes its interface, ct lists a supported content format (50 means JSON), and obs advertises support for observing changes. These attributes do not grant access or guarantee that an Observe registration will succeed. Query /.well-known/core?rt=temperature when that agreed type is the one needed; inspect the returned links rather than guessing paths.

Transfer example. A greenhouse gateway discovers </zone/3/temp>;rt="temperature";if="sensor";ct=60;obs and </zone/3/valve>;rt="irrigation";if="actuator";ct=50. Suppose the temperature GET response permits caching and supplies Max-Age 45 seconds: its freshness lifetime. At age 30 seconds, 15 seconds remain; at age 50 seconds, it is stale by 5 seconds and needs revalidation before reuse. A proxy may reuse it for an eligible matching request within the remaining lifetime and access policy. The cache key is the request information used to select a stored response. After expiry, send its ETag to the origin in a validating GET. If the representation is unchanged, 2.03 Valid refreshes the cached metadata without a response payload; if changed, 2.05 Content supplies replacement bytes and their ETag. An unchanged validator does not, by itself, extend freshness.

/.well-known/core

Check the returned resource address and Content-Format 40. Discovery describes what is advertised; access to a known address remains a separate decision.

Link attributes

Use rt for type selection, if for interface information and ct for formats. Treat sz size metadata and obs capability as claims to check against transfer and registration results.

Max-Age caching

Max-Age is option 14; when absent, its default is 60 seconds. Check remaining freshness, the request cache key and sharing policy before reusing an eligible cached GET response.

ETag revalidation

ETag is option 4. A validating GET can yield 2.03 Valid without a payload; keep the stored bytes and update freshness from the response. A changed representation requires replacement bytes.

A firmware body is 2,050 bytes and the path uses 256-byte CoAP blocks. How many data blocks carry the body?

Nine blocks cover the 2,050-byte firmware body.

Correct. Eight full blocks carry 2,048 bytes, leaving two bytes for a ninth.

A Block2 response has NUM=4, SZX=4 and M=1. Where should its bytes go, and is it final?

Offset 1,024; more data follows.

Correct. 4 × 256 = 1,024, and M=1 signals further data blocks.

Release Checklist

Before accepting the design, retain evidence for each decision:

  • Locate: discovery request, Content-Format 40 response, relevant link attributes and tested supported filters such as ?rt=temperature. Check authorization separately; a hidden link does not revoke direct access.
  • Place: Block1 or Block2 direction, payload length, negotiated SZX, NUM-to-offset calculation, M flags and received payload lengths. Include the repeated request after loss and show that assembly did not append duplicate bytes.
  • Resume: saved offsets, resource and format identity, representation validator, retry cap and timeout policy. Show that a changed version or missing continuity evidence stops reuse of partial bytes.
  • Accept: final response status, complete length, digest, firmware signature and rollback decision. A short final block or M 0 alone cannot authorize installation.
  • Reuse: origin and proxy responses, cache key, remaining Max-Age, ETag and unchanged/changed revalidation outcome. Keep state-changing valve commands separate from cacheable temperature GET responses; do not treat a command response as reusable sensor data.
  • Diagnose: identify whether the missing evidence belongs to the origin, proxy, transport or application. Do not use an origin trace alone to claim what the proxy served.

See Also

Return to the feature tour for the broad use cases, then check method and multicast contracts where those choices affect exchanges.

Next

Return to CoAP Advanced Features, then continue to CoAP Security and Applications for securing block transfers and discovered resources.

Hi, I’m Broker Bex. What do you need?

Find help

Let’s find what you need

Find a lesson or return to your module guide.

Give feedback

Help improve this chapter

Tell us what helped or what needs fixing.

Tips

Try a small next step

A few ways to get more from this chapter.

Ask AI

Continue the conversation

Take the conversation further with your preferred assistant.

AI help is available in supported modules when enabled in Settings.

No live person answers on independent study. Feedback does not promise a personal reply.

For welfare or safety concerns, follow your school’s procedures outside IoTClass.