CoAP协议学习——CoAP基础


更多CoAP文章请参考博文索引——【 物联网学习笔记——索引博文
推广
《IoT开发实战:CoAP卷》已经由机械工业出版社出版,如果想学习CoAP的更多内容请于淘宝、天猫或京东平台搜索"CoAP"。


什么是CoAP
    CoAP是受限制的应用协议(Constrained Application Protocol)的代名词。最近几年专家们预测会有更多的设备相互连接,而这些设备的数量将远超人类的数量。在这种大背景下,物联网和M2M技术应运而生。虽然对人们而言,连接入互联网显得方便容易,但是对于那些微型设备而言接入互联网非常困难。在当前由PC机组成的世界,信息交换是通过TCP和应用层协议HTTP实现的。但是对于小型设备而言,实现TCP和HTTP协议显然是一个过分的要求。为了让小设备可以接入互联网,CoAP协议被设计出来。 CoAP是一种应用层协议,它运行于 UDP协议之上而不是像HTTP那样运行于TCP之上。CoAP协议非常小巧,最小的数据包仅为4字节。

CoAP协议是否可以替换HTTP协议?
    CoAP并不能替代HTTP协议,但是对于那些小设备(256KB Flash 32KB RAM 20MHz主频)而言CoAP的确是一个好的解决方案。

CoAP消息类型
CoAP采用与HTTP协议相同的请求响应工作模式。CoAP协议共有4中不同的消息类型。
CON——需要被确认的请求,如果CON请求被发送,那么对方必须做出响应。
NON——不需要被确认的请求,如果NON请求被发送,那么对方不必做出回应。
ACK——应答消息,接受到CON消息的响应。
RST——复位消息,当接收者接受到的消息包含一个错误,接受者解析消息或者不再关心发送者发送的内容,那么复位消息将会被发送。

CoAP消息结构
一个CoAP消息最小为4个字节,以下是CoAP协议不同部分的描述。
【版本Version】:类似于IPv6和IPv6,仅仅是一个版本号。
【消息类型Message Type】:CON,NON,ACK,RST。
【消息ID Message ID】:每个CoAP消息都有一个ID,在一次会话中ID总是保持不变。但在这个会话之后该ID会被回收利用。
【标记 Token】:标记是ID的另一种表现。
【选项 Options】:CoAP选项类似于HTTP请求头,它包括CoAP消息本身,例如CoAP端口号,CoAP主机和CoAP查询字符串等。
【负载Payload】:真正有用的被交互的数据。



图 CoAP消息结构
CoAP的URL
    在HTTP的世界中,RESTFul协议由于其简单性和适用性,在WEB应用中越来越受欢迎,这样的道理同样适用于CoAP。一个CoAP资源可以被一个URI所描述,例如一个设备可以测量温度,那么这个温度传感器的URI被描述为:CoAP://machine.address:5683/sensors/temperature。请注意,CoAP的默认UDP端口号为5683。

CoAP观察模式
    在物联网的世界中,你需要去监控某个传感器例如温度或湿度等。在这种情况下,CoAP客户端并不需要不停的查询CoAP服务器端的数据变化情况。CoAP客户端可以发送一个观察请求到服务器端。从该时间点开始计算,服务器便会记住客户端的连接信息,一旦温度发生变化,服务器将会把新结果发送给客户端。如果客户端不在希望获得温度检测结果,那么客户端将会发送一个RST复位请求,此时服务器便会清除与客户端的连接信息。

CoAP块传输
    CoAP协议的特点是传输的内容小巧精简,但是在某些情况下不得不传输较大的数据。在这种情况下可以使用CoAP协议中的某个选项设定分块传输的大小,那么无论是服务器或客户端可完成分片和组装这两个动作。

参考资料
【CSDN博客: CoAP协议和开源实现



  • 24
    点赞
  • 104
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
CoAP(Constrained Application Protocol)是一种轻量级的应用层协议,用于在受限的网络环境中传输数据。以下是一个简单的CoAP协议C语言实现的示例: ``` #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h> #include <arpa/inet.h> #define COAP_VERSION 1 #define COAP_HEADER_LENGTH 4 typedef struct { uint8_t version; uint8_t type; uint8_t token_length; uint8_t code; uint16_t message_id; } coap_header_t; typedef struct { uint8_t *token; uint16_t token_length; uint8_t *payload; uint16_t payload_length; } coap_message_t; typedef struct { int socket_fd; struct sockaddr_in address; } coap_endpoint_t; typedef struct { uint8_t *data; uint16_t length; } coap_buffer_t; coap_header_t *coap_create_header(uint8_t type, uint8_t code, uint16_t message_id) { coap_header_t *header = calloc(1, sizeof(coap_header_t)); header->version = COAP_VERSION; header->type = type; header->token_length = 0; header->code = code; header->message_id = htons(message_id); return header; } void coap_free_header(coap_header_t *header) { free(header); } coap_message_t *coap_create_message(uint8_t type, uint8_t code, uint16_t message_id) { coap_message_t *message = calloc(1, sizeof(coap_message_t)); message->token = NULL; message->token_length = 0; message->payload = NULL; message->payload_length = 0; coap_header_t *header = coap_create_header(type, code, message_id); message->payload_length += COAP_HEADER_LENGTH; message->payload = calloc(1, message->payload_length); memcpy(message->payload, header, COAP_HEADER_LENGTH); coap_free_header(header); return message; } void coap_free_message(coap_message_t *message) { free(message->token); free(message->payload); free(message); } coap_endpoint_t *coap_create_endpoint(char *ip, int port) { coap_endpoint_t *endpoint = calloc(1, sizeof(coap_endpoint_t)); endpoint->socket_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); memset(&endpoint->address, 0, sizeof(endpoint->address)); endpoint->address.sin_family = AF_INET; endpoint->address.sin_addr.s_addr = inet_addr(ip); endpoint->address.sin_port = htons(port); return endpoint; } void coap_free_endpoint(coap_endpoint_t *endpoint) { close(endpoint->socket_fd); free(endpoint); } coap_buffer_t *coap_receive(coap_endpoint_t *endpoint) { coap_buffer_t *buffer = calloc(1, sizeof(coap_buffer_t)); socklen_t address_length = sizeof(endpoint->address); int length = recvfrom(endpoint->socket_fd, buffer->data, buffer->length, 0, (struct sockaddr *) &endpoint->address, &address_length); buffer->length = length; return buffer; } void coap_send(coap_endpoint_t *endpoint, coap_message_t *message) { sendto(endpoint->socket_fd, message->payload, message->payload_length, 0, (struct sockaddr *) &endpoint->address, sizeof(endpoint->address)); } int main() { coap_endpoint_t *endpoint = coap_create_endpoint("127.0.0.1", 5683); coap_message_t *message = coap_create_message(0, 1, 1); coap_send(endpoint, message); coap_buffer_t *buffer = coap_receive(endpoint); printf("Received %d bytes: %.*s\n", buffer->length, buffer->length, buffer->data); coap_free_message(message); coap_free_endpoint(endpoint); free(buffer); return 0; } ``` 这个示例实现了CoAP协议的基本功能,包括创建和发送CoAP消息,以及接收和处理CoAP消息。在这个示例中,我们使用了标准的C语言库函数和一些网络相关的函数来实现CoAP协议的功能。如果您想要了解更多关于CoAP协议的信息,请参考CoAP协议的官方网站。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值