Overview of CoAP Blockwise Transfer
Given that CoAP is designed for small devices, we need to put reasonable restrictions on the size of the payload and the message to ensure fragmentation is avoided during transfer and also there is limited processing need at the machine end. But sometimes, it might become necessary to transfer large (comparatively) amounts of data between machines. This is where the block transfer scheme of CoAP comes in. Please note, the block size applies to the payload and not to the entire message.
To get a full view of the block wise transfer, please read the draft specification. The contents below provide key excerpts from the specification.
In block transfer, either the client or the server can initiate a block transfer. There are two block options that have been introduced “Block1” and “Block2”. While the specification states that “Block1” pertains to the request payload and “Block2” to the response payload, our understanding of the specification tells us, that this is not really 100% accurate. We will see that a little later in this post.
A block transfer option contains 3 pieces of information – the sequence number of the block, a Boolean indicating if there are more blocks following the current block or not, and, a value indicating what the maximum size in bytes is, this block can contain. We need to be a bit careful on the size aspect. For example, let’s assume that we have 1424 bytes of data to transfer and maximum size we have defined, per block, is 1024 bytes. Therefore, the first block must contain 1024 bytes and should have the more flag set. The second block can have only 400 bytes with the more flag not set.
The size of the block is defined by an exponent whose value ranges from 0-6. The actual size is then calculated using the formula Size = 2 ^ (Exp + 4)
Thus, if exponent is zero, Size = 2 ^ (0 + 4) = 2 ^ 4 = 16 and when the exponent size is 6 (the maximum) the size is = 2 ^ (6 + 4) = 2 ^ 10 = 1024
The specification document on the block option is a bit confusing, so we take a different approach to understand how to use the block options using cases. Before that, let’s assume we want to put 35 bytes of data, in this example, using size exponent at 0 (meaning maximum size of each block is 16 bytes).
S represents the sequence number of the block, M represents the more flag (1 means set and 0 means not set) and E represents the size exponent.
- Client sends S=0,M=1,E=0 to Server. Payload is 16 bytes. The method is PUT. Use Block1 to describe this.
- Server sends back S=0, M=0,E=0 in the response to indicate all data was accepted. Server responds back with Block1.There is a further complicated case. If server sets the M bit to 1, then it means that server has not finished collecting all the data from the endpoint and is still busy. For that client should wait. When server finishes collecting all data from the endpoint, it will send back the ACK with M bit not set, indicating, it’s done collecting all the data that was sent.
- Client again sends S=1,M=1,E=0. Payload is 16 bytes (total 32 bytes transferred so far). Method is PUT. Use Block1 to describe this.
- Server sends back S=1,M=0,E=0 (We are assuming that server is able to “gobble-up” all data and is sending the final response)
- Client sends S=2,M=0,E=0. Payload is 3 bytes (now total 35 bytes transferred). Method is PUT. Use Block1 to describe this.
- Server sends S=2,M=0,E=0 and the transfer is complete
Client wants to get a large value from the server using block wise transfer.
- Client sends a Block2 option, with S=0, M=0, E=0 Meaning, get me the 0th block with size of each block being 16 bytes.
- Server responds back with Block2 with S=0, M=1,E=0 Meaning, here is the 0th block, of size 16 bytes, but there are more bytes to send.
- Client then issues S=1,M=0,E=0. Meaning, get me the 1st block with size of each block being 16 bytes.
- Server responds back with S=1, M=1,E=0 Meaning, here is the first block, of size 16 bytes, but there are more bytes to send.
- Client then issues S=2, M=0,E=0. Meaning, get me the next block, of size 16 bytes.
- Server responds back with S=2, M=0,E=0 Meaning, here is the 3rd block of size 16 bytes, and this is the last one (more bit is not set).
The client and server can even negotiate the size too. After the client requests a specific size, the server might request a different size or vice versa.
Sometimes, a resource being observed by clients, might be large enough to qualify for a block wise transfer. How this happens is shown as a sequence outlined below :
- Client sends a Block2 request with the given size to register for notifications (meaning Observe option is included)
- Once the resource representation changes at the server end, the server sends multiple blocks of data back to client using Block2 option. Please note, with each transfer, the Observe sequence number remains the same, until all blocks have been transferred.
Additional rule to note is that for we must use CON messages.
posted on 2016-04-06 16:15 jianqi2010 阅读(689) 评论(0) 编辑 收藏 举报