coap 返回版本信息_CoAP协议详解

The Constrained Application Protocol(CoAP)是一种专用的Web传输协议,用于受约束的节点和受约束的(例如,低功率,有损)网络。

节点通常具有带少量ROM和RAM的8位微控制器,而诸如低功耗无线个人局域网 (6LoWPAN)上的IPv6之类的受限网络通常具有较高的分组错误率,典型吞吐量为10 kbps 。该协议旨在用于机器对机器(M2M)应用,例如智能能源和楼宇自动化。

CoAP提供了应用程序端点之间的请求/响应交互模型,支持服务的资源发现,并包括Web的关键概念,例如URI和Internet媒体类型。CoAP旨在轻松与HTTP交互以与Web集成,同时满足诸如多播支持,非常低的开销以及在受限环境中的简单性等特殊要求。

介绍

互联网上的Web服务(Web API)的使用在大多数应用程序中已经无处不在,并且依赖于Web 的Representational State Transfer(REST)体系结构。

Constrained RESTful Environments(CoRE)的工作旨在以最合适的形式实现REST体系结构,以适用于最受约束的节点(例如RAM和ROM受限的8位微控制器)和网络(例如6LoWPAN)。诸如6LoWPAN之类的受约束的网络支持将IPv6数据包分段成小的链路层帧。但是,这会大大减少数据包交付概率。CoAP的一个设计目标是保持消息开销较小,从而限制了分段的需要。

CoAP的主要目标之一是针对这种受限环境的特殊要求设计通用的Web协议,尤其是考虑到能源,楼宇自动化以及其他机器对机器(M2M)应用程序。

CoAP的目标不是盲目地压缩HTTP,而是实现与HTTP通用但针对M2M应用程序进行了优化的REST的子集。尽管CoAP可用于将简单的HTTP接口重新生成更紧凑的协议,更重要的是,它还提供了M2M的功能,例如内置资源发现,多播支持和异步消息交换。

该协议可以轻松转换为HTTP以与现有Web集成,同时满足特殊要求,例如多播支持,非常低的开销以及受约束环境和M2M应用程序的简便性。

特性

CoAP具有以下主要功能:

在受限条件下满足M2M要求的Web协议

UDP [ RFC0768 ]绑定,具有可选的可靠性,支持单播和多播请求。

异步消息交换。

低的报头开销和解析复杂度。

简单的代理和缓存功能。

无状态HTTP映射,允许构建代理通过HTTP统一方式或HTTP访问CoAP资源。

绑定到数据报传输层安全性(DTLS)的安全性。

URI和内容类型支持。

消息格式

0 1 2 3

0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1

+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

|Ver| T | TKL | Code | Message ID |

+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

| Token (if any, TKL bytes) ...

+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

| Options (if any) ...

+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

|1 1 1 1 1 1 1 1| Payload (if any) ...

+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

上图的头部字段定义如下:

Version(Ver): 2位无符号整数。CoAP版本号。本规范的实现必须将此字段设置为1(01二进制)。其他值保留用于将来的版本。

Type(T): 2位无符号整数。指示此消息的类型是否为Confirmable(0), Non-Confirmable, Acknowledgement(2)或者Reset(3)。

Token Length(TKL): 4位无符号整数。指示可变长度令牌字段的长度(0-8个字节)。长度9-15 保留,不得发送,并且必须作为消息格式错误进行处理。

Code: 8位无符号整数,响应码。

Message ID: 网络字节顺序的16位无符号整数。用于检测消息重复并将确认/重置类型的消息与可确认/不可确认类型的消息进行匹配。

CoAP初体验

CoAP有两款我觉得比较不错的客户端:

CoAP-CLI: CoAP-CLI是CoAP的命令行界面,基于node.js和node-coap所构建。

The CoAP Shell提供用于与CoAP协议交互的命令行界面。它支持coap:和coaps模式(例如UDP和DTLS)。CoAP Shell建立在Spring Shell, Californium(Cf)和Scandium(Sc)项目之上。它是一个SpringBoot应用程序,它内置于单个可自我执行的jar中,并且可以在任何Java8+环境中运行。

由于CoAP Shell能更好的体现和展示CoAP的功能、特性和消息格式,接下来我将通过CoAP Shell来讲述CoAP协议相关知识点。

以下是CoAP Shell效果图

如何构建

从GitHub克隆项目并使用Maven进行构建,或者直接从下方下载。

git clone https://github.com/sanshengshui/coap-shell

cd coap-shell

mvn clean install

然后在该target文件夹中运行可执行jar包。

快速开始

启动Shell:

java -jar coap-shell-1.1.2-SNAPSHOT.jar

_____ ___ ___ ______ ____

/ ___/__ / _ | / _ \ / __/ / ___ / / /

/ /__/ _ \/ __ |/ ___/ _\ \/ _ \/ -_) / /

\___/\___/_/ |_/_/ /___/_//_/\__/_/_/

CoAP Shell (v1.1.2-SNAPSHOT)

For assistance hit TAB or type "help".

server-unknown:>

连接到CoAP服务器(例如coap://californium.eclipse.org/或coap://coap.me)

server-unknown:>connect coap://coap.me

available

coap://coap.me:>ping

available

coap://coap.me:>

发现可用的CoAP资源

coap://coap.me:>discover

┌──────────────────────────────┬────────────────────────┬─────────────────────────┬───────────┬──────┬─────────────┐

│Path [href] │Resource Type [rt] │Content Type [ct] │Interface │Size │Observable │

│ │ │ │[if] │[sz] │[obs] │

├──────────────────────────────┼────────────────────────┼─────────────────────────┼───────────┼──────┼─────────────┤

│/123412341234123412341234 │123412341234123412341234│text/plain (0) │ │ │ │

│/3 │3 │application/json (50) │ │ │ │

│/4 │4 │application/json (50) │ │ │ │

│/5 │5 │application/json (50) │ │ │ │

│/bl%C3%A5b%C3%A6rsyltet%C3%B8y│blåbærsyltetøy │text/plain (0) │ │ │ │

│/broken │Type2, Type1 │text/plain (0) │If2, If1 │ │ │

│/create1 │create1 │text/plain (0) │ │ │ │

│/hello │Type1 │text/plain (0) │If1 │ │ │

│/large │Type1, Type2 │text/plain (0) │If2 │1700 │ │

│/large-create │large-create │text/plain (0) │ │ │ │

│/large-update │large-update │text/plain (0) │ │ │ │

│/location-query │location-query │text/plain (0) │ │ │ │

│/location1 │location1 │application/link-format │ │ │ │

│ │ │(40) │ │ │ │

│/multi-format │multi-format │text/plain (0) │ │ │ │

│/path │path │application/link-format │ │ │ │

│ │ │(40) │ │ │ │

│/query │query │text/plain (0) │ │ │ │

│/secret │secret │text/plain (0) │ │ │ │

│/seg1 │seg1 │application/link-format │ │ │ │

│ │ │(40) │ │ │ │

│/separate │separate │text/plain (0) │ │ │ │

│/sink │sink │text/plain (0) │ │ │ │

│/test │test │text/plain (0) │ │ │ │

│/validate │validate │text/plain (0) │ │ │ │

│/weird33 │weird33 │text/plain (0) │ │ │ │

│/weird333 │weird333 │text/plain (0) │ │ │ │

│/weird3333 │weird3333 │text/plain (0) │ │ │ │

│/weird33333 │weird33333 │text/plain (0) │ │ │ │

│/weird44 │weird44 │text/plain (0) │ │ │ │

│/weird55 │weird55 │text/plain (0) │ │ │ │

└──────────────────────────────┴────────────────────────┴─────────────────────────┴───────────┴──────┴─────────────┘

coap://coap.me:>

GET获取资源数据

coap://coap.me:>get /hello

----------------------------------- Response -----------------------------------

GET coap://coap.me/hello

MID: 64187, Type: ACK, Token: 50E8F0AC1BA8D277, RTT: 3368ms

Options: {"Content-Format":"text/plain"}

Status : 205-Reset Content, Payload: 5B

................................... Payload ....................................

world

--------------------------------------------------------------------------------

从以上列表能清晰的看出CoAP的消息格式和数据包。

coap://coap.me/hello : 是CoAP协议的uri

MID: 消息的ID,用于唯一区分消息。

Type: 消息类型,这里的消息类型为:Acknowledgement(确认)

Token: 消息会话

Options:{"Content-Format":"text/plain"}消息可选性,这里表示消息的数据类型为文本

Status: 状态码

Payload: 消息负载数据

Delete资源数据

coap://coap.me:>get /sink

NULL response!

coap://coap.me:>delete /sink

----------------------------------- Response -----------------------------------

DELETE coap://coap.me/sink

MID: 64264, Type: ACK, Token: 0C2338F7FB9447F7, RTT: 274ms

Options: {"Content-Format":"text/plain"}

Status : 202-Accepted, Payload: 9B

................................... Payload ....................................

DELETE OK

--------------------------------------------------------------------------------

coap://coap.me:>get /sink

----------------------------------- Response -----------------------------------

GET coap://coap.me/sink

MID: 64265, Type: ACK, Token: 04A8DACB450186A7, RTT: 276ms

Options: {"ETag":0xa6166ef62ce0b4bc, "Content-Format":"text/plain"}

Status : 205-Reset Content, Payload: 38B

................................... Payload ....................................

I was deleted, and you put here: hello

--------------------------------------------------------------------------------

coap://coap.me:>

PUT资源数据

coap://coap.me:>put /sink --payload 'Hi From IoT Technology' --format text/plain

----------------------------------- Response -----------------------------------

PUT coap://coap.me/sink

MID: 64266, Type: ACK, Token: FC2CE751AD5A232A, RTT: 368ms

Options: {"Content-Format":"text/plain"}

Status : 204-No Content, Payload: 6B

................................... Payload ....................................

PUT OK

--------------------------------------------------------------------------------

coap://coap.me:>get /sink

----------------------------------- Response -----------------------------------

GET coap://coap.me/sink

MID: 64267, Type: ACK, Token: 3439364639206648, RTT: 13478ms

Options: {"ETag":0x01fd3e1298b1fb7a, "Content-Format":"text/plain"}

Status : 205-Reset Content, Payload: 36B

................................... Payload ....................................

you put here: Hi From IoT Technology

--------------------------------------------------------------------------------

coap://coap.me:>

POST资源数据

coap://coap.me:>delete /sink

----------------------------------- Response -----------------------------------

DELETE coap://coap.me/sink

MID: 64268, Type: ACK, Token: 0CCB572626A124A6, RTT: 391ms

Options: {"Content-Format":"text/plain"}

Status : 202-Accepted, Payload: 9B

................................... Payload ....................................

DELETE OK

--------------------------------------------------------------------------------

coap://coap.me:>post /sink --payload 'testing for post data' --format text/plain

----------------------------------- Response -----------------------------------

POST coap://coap.me/sink

MID: 64269, Type: ACK, Token: A441392CC855852D, RTT: 424ms

Options: {"Location-Path":["location1","location2","location3"], "Content-Format":"text/plain"}

Status : 201-Created, Payload: 7B

................................... Payload ....................................

POST OK

--------------------------------------------------------------------------------

coap://coap.me:>get /sink

----------------------------------- Response -----------------------------------

GET coap://coap.me/sink

MID: 64270, Type: ACK, Token: DC24713232F17DB7, RTT: 325ms

Options: {"ETag":0xf97973ea26db6781, "Content-Format":"text/plain"}

Status : 205-Reset Content, Payload: 54B

................................... Payload ....................................

I was deleted, and you put here: testing for post data

--------------------------------------------------------------------------------

到此为止,我相信大家对CoAP协议的消息格式应该有了一定的认知。更多信息和使用请浏览这个库的README。

参考资料

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值