CoAP协议笔记[RFC7252]

时间:2016-04-07 20:47:51   收藏:0   阅读:1172

1. What is CoAP

    “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.”

 

2. Features

 

3. Protocol Overview 

3.1 A two-layer approach

技术分享

CoAP messaging layer used to deal with UDP and the asynchronous nature of the interactions.

Request/response interactions using Method Code and Response Code.

3.2 Messaging Model

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).

3.3 Request/Response Model 

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 ResponseSeparate Response, Non-confirmable Response.

 

4. Format

 技术分享

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加起来的值。

 

5. Message Transmission

     有四种消息类型: Confirmable (0), Non-confirmable (1), Acknowledgement (2), or Reset (3).

5.1 Messages Transmitted Reliably

5.2 Messages Transmitted without Reliability

总结下Mesage Type的使用如下:

技术分享

*表示不是正常的操作,只是为了引起对方回Reset message(CoAP ping)。

 

6. Request/Response

6.1 Requset

6.1.1 Method Code

6.2 Response

  有Piggybacked Response, Separate Response, Non-confirmable Response三种类型

三个Response类型的例子:

技术分享     技术分享

 

 技术分享    

可以看到Server回Separate Response的时候,Token和Client的Request时的Token是一致的,但是Message ID已经变掉了。

6.2.1 Response Code

Success 2.xx

Client Error 4.xx

Server Error 5.xx

7. Options

7.1 Option Numbers

技术分享

不同的Option有不同Option Number来表示。

Critical = (onum & 1);
UnSafe = (onum & 2);
NoCacheKey = ((onum & 0x1e) == 0x1c);

7.2 Option Value formats

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。

7.3 Base specification Options  

技术分享

7.3.1 Uri-Host, Uri-Port, Uri-Path, and Uri-Query

表示一个Resource的地址。 

7.3.2 Proxy-Uri and Proxy-Scheme

用于forward-proxy。

7.3.3 Content-Format

表示Message中Payload的类型。

技术分享

7.3.4 Accept

表明Client可以接受哪种content format。如果Server不能回复此种content format,则回4.06 "Not Acceptable"。

7.3.5 Max-Age

表示Cached reposne 为Fresh状态的最大时间,超过这个时间,Cached Response为Not Fresh。

7.3.6 ETag

提供Resource的Server产生这个Tag,表示的是同一个Resourc。

7.3.7 Location-Path and Location-Query

Relative URI to request URL that consists either of an absolute path, a query string, or both.

7.3.8 Conditional Request Options

使Client在某种特定情况下才向Server发Request。如果条件不满足Client发了request,Server会回4.12 (Precondition Failed) Response code。

7.3.8.1 if-Match
7.3.8.2 None-Match

7.3.11Size1

常用在Block-wise Transfer中。

也用在当Client的包太大时,Server回复4.13 Response code,带上Size1 Option告诉Client能接受的最大值。

 

7. Chching

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。

 

8. Proxying

对于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

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!