交互流程
Register(注册/登录)
Register接口用于设备向平台进行注册,通过发送CoAP POST请求到平台进行身份认证,认证通过,则设备被激活;认证失败,平台会向设备返回失败码。交互流程如下图所示:
LWM2M业务数据通过CoAP协议进行封装,参数定义参见下表:
CoAP请求消息 | 参数 | 示例说明 |
Message-Mode | CON | |
CoAP-Method | POST | |
CoAP-Option | Uri-Path(11):{path} | path: /rd |
Content-Format(12):application/link-format | 参见CoAP协议 | |
Uri-Query(15):ep={endpoint name} | Endpoint name支持五种格式: ① IMEI ② urn:imei:########### ③ urn:imei-imsi:########-###### ④ urn:imei+sm9:########-##### ⑤ urn:imei+simid:########-##### | |
Uri-Query(15): It={lifetime} | lifetime范围:300-86400 | |
Uri-Query(15): lwm2m={Lwm2m version} | Lwm2m version:1.0 | |
Uri-Query(15):b={binding} | 可选,默认"U" | |
CoAP-payload | {ObjectLinks} | 设备支持的对象和实例 参考样例: "</>;rt="oma.lwm2m",</1/0>,</3/0>,</4/0>,</5/0>,</19/0>" |
CoAP响应消息 | 参数 | 示例说明 |
Success Code | 2.01 Created | |
Failure Code | 4.00 Bad Request,参数非法 | |
4.03 Forbidden,接入鉴权失败 | ||
4.12 Precondition Failed (lwm2m或IOTVer版本不支持) | ||
CoAP-Option | Loction-path(8):{loction-path1} | loction-path1:rd |
Loction-path(8):{loction-path2} | loction-path2:{Endpoint} |
Update(更新)
在lifetime超期之前,设备需要发起Update流程,否则平台会认为设备离线,再次连接需要重新发起Register,当设备参数(lifetime、binding mode、ObjectLinks等)发生变化,设备可以通过Update通知平台参数改变。Update流程如下:
Update接口参数如下:
CoAP请求消息 | 参数 |
Message-Mode | CON |
CoAP-Method | POST |
CoAP-Option | Uri-Path(11):{loction-path1};同Register响应 |
Uri-Path(11):{loction-path2};同Register响应 | |
Uri-Query(15):lt={lifetime} | |
Uri-Query(15):b={binding} | |
CoAP响应消息 | 参数 |
Success Code | 2.04 changed |
Failure Code | 4.00 Bad Request,参数非法 |
4.03 Forbidden,接入鉴权失败 | |
4.04 Not Found,URI不存在 |
说明:
1. 在lifetime到期之前,需要提前一定的时间进行update(至少提前5秒),避免在lifetime到期时再update的情况。
2. 对于平台回复的CoAP响应错误码,如4.04等,设备应支持错误处理,必要时需要重新登录平台。
Deregister(去注册/登出)
设备通过Deregister触发登出流程
deregister接口参数如下:
CoAP请求消息 | 参数 |
Message-Mode | CON |
CoAP-Method | DELETE |
CoAP-Option | Uri-Path(11):{loction-path1};同Register响应 |
Uri-Path(11):{loction-path2};同Register响应 | |
CoAP响应消息 | 参数 |
Success Code | 2.02 Deleted |
Failure Code | 4.00 Bad Request,参数非法 |
4.03 Forbidden,接入鉴权失败 | |
4.04 Not Found,URI不存在 |
Observe(资源订阅)
订阅Object的交互流程:
observe接口参数:
CoAP请求消息 | 参数 |
Message-Mode | CON |
CoAP-Method | GET |
Token | {ObserveID} |
CoAP-Option | Observe(6):0 |
Uri-Path(11):{Object ID} | |
Uri-Path(11):{Object Instance ID} | |
Uri-Path(11):{Resource ID} | |
CoAP响应消息 | 参数 |
Success Code | 2.05 Content |
Failure Code | 4.00 Bad Request,参数非法 |
CoAP-Option | Observe(6):0 |
Notify(资源数据上报)
设备获取到的数据会上报给平台
Notify接口参数:
CoAP请求消息 | 参数 |
Asynchronous-response | 2.05 Content |
Token | {ObserveID};同observe报文 |
CoAP-Option | Observe(6):{id} |
Content-Format(12):application/octet-stream | |
CoAP-Payload | {Updated Values} |
说明:
对于notify报文,平台对于设备token已经超时删除或者携带错误的情况,会回复reset报文,设备应支持错误处理,必要时需要重新登陆平台。