标签:
“The Constrained Application Protocol (CoAP) is a specialized web transfer protocol for use with constrained nodes and constrained networks in the Internet of Things. The protocol is designed for machine-to-machine (M2M) applications such as smart energy and building automation.”
CoAP messaging layer used to deal with UDP and the asynchronous nature of the interactions.
Request/response interactions using Method Code and Response Code.
Reliability is provided by marking a message as Confirmable (CON) message.
A message that does not require reliable transmission can be sent as a Non-confirmable message (NON).
CoAP request and response semantics are carried in CoAP messages, which include either a Method Code or Response Code.
Request的Method code有GET, POST, PUT, DELETE四种。
Response有Piggybacked Response, Separate Response, Non-confirmable Response.
Version (Ver): 2-bit unsigned integer. Implementations of this specification MUST set this field to 1 (01 binary).
Type (T): 2-bit unsigned integer. Indicates if this message is of type Confirmable (0), Non-confirmable (1), Acknowledgement (2), or Reset (3).
Token Length (TKL): 4-bit unsigned integer. Indicates the length of the variable-length Token field (0-8 bytes).
Code:8-bit unsigned integer, split into a 3-bit class (most significant bits) and a 5-bit detail (least significant bits).一般写成c.dd的形式,可以参考CoAP Code Registries.
Message ID: 16-bit unsigned integer in network byte order. Used to match messages of type Acknowledgement/Reset to messages of type Confirmable/Nonconfirmable.
Token: Used to match a response with a request.[0-8bytes].
Option: 在CoAP mesage中可以携带一些列的options (以Option Number表示)。每一个option组成如下:
Option Delta:4-bit unsigned integer. A value between 0 and 12.
如果Option Delta == 13, 那么Option Delta (extended)部分为1 byte,Option Delta = 13+Option Delta (extended)
如果Option Delta == 14, 那么Option Delta (extended)部分为2 byte,Option Delta = 14+255+Option Delta (extended)
Option Delta == 15无效。
Option Length:4-bit unsigned integer. A value between 0 and 12.
如果Option Length== 13, 那么Option Length(extended)部分为1 byte,Option Length= 13+Option Length(extended)
如果Option Length== 14, 那么Option Length(extended)部分为2 byte,Option Length= 14+255+Option Length(extended)
Option Delta == 15无效。
Option Number的计算:
Option Number从option delta中得到。The Option Number is calculated by simply summing the Option Delta values of this and all previous options before it.也就是说某一个option的option number就是它之前所有Option的Option delta和它自己的option delta加起来的值。
有四种消息类型: Confirmable (0), Non-confirmable (1), Acknowledgement (2), or Reset (3).
总结下Mesage Type的使用如下:
*表示不是正常的操作,只是为了引起对方回Reset message(CoAP ping)。
有Piggybacked Response, Separate Response, Non-confirmable Response三种类型。
三个Response类型的例子:
可以看到Server回Separate Response的时候,Token和Client的Request时的Token是一致的,但是Message ID已经变掉了。
Success 2.xx
Client Error 4.xx
Server Error 5.xx
不同的Option有不同Option Number来表示。
Critical = (onum & 1);
UnSafe = (onum & 2);
NoCacheKey = ((onum & 0x1e) == 0x1c);
Critical/Elective Option:
根据对于未识别的Option的处理分为Critical/Elective Option。
--如果某个Option未被识别,如果是Elective Option,直接忽略;如果是Confirmable request中的Critical Option,Server回4.02 (Bad Option)。
--Confirmable response或者Nonconfirmable message中的Critical Option,对端需要Reject the message。
--只适用于non-proxying endpoints。
Unsafe or Safe-to-Forward:
根据Proxy对于未被识别的Option的处理分为Unsafe or Safe-to-Forward Option。
Cache-Key:
Repeatable:在一个mesage中可能有一个或者多个这样的option。
表示一个Resource的地址。
用于forward-proxy。
表示Message中Payload的类型。
表明Client可以接受哪种content format。如果Server不能回复此种content format,则回4.06 "Not Acceptable"。
表示Cached reposne 为Fresh状态的最大时间,超过这个时间,Cached Response为Not Fresh。
提供Resource的Server产生这个Tag,表示的是同一个Resourc。
Relative URI to request URL that consists either of an absolute path, a query string, or both.
使Client在某种特定情况下才向Server发Request。如果条件不满足Client发了request,Server会回4.12 (Precondition Failed) Response code。
常用在Block-wise Transfer中。
也用在当Client的包太大时,Server回复4.13 Response code,带上Size1 Option告诉Client能接受的最大值。
CoAP includes a simple caching model
Cacheability determined by response code
An option number mask determines if it is a cache key
7.1 Freshness model
Freshness checked using the Max-Age Option。
7.2 Validation model
Validity checked using the Etag Option。
所以我的理解是ETag就是一个标签(entity-tag),由提供Resource的Server生成,标签表示的是随时间变化的同一个Resource。
对于Safe-to-Forward options必须forward。
Forward-Proxies
作用是代理发往Server的Request。在Request message中使用Proxy-Uri Option表明Proxy,使用Uri-Host, Uri-Port, Uri-Path, and Uri-Query Options表明origin server。
Reverse-Proxies
不使用Proxy-Uri Option。
标签:
原文地址:http://www.cnblogs.com/tifnan2015/p/5363166.html