emqx/apps/emqx_gateway/src/coap
2021-08-09 21:46:07 +02:00
..
include style(emqx_gateway): improve some emqx_coap code 2021-08-03 09:48:08 +08:00
resources fix(coap): log error level only when it error 2021-08-09 21:46:07 +02:00
test chore: rename acl to authz 2021-07-26 16:10:33 +08:00
emqx_coap_channel.erl style(emqx_gateway): improve some emqx_coap code 2021-08-03 09:48:08 +08:00
emqx_coap_frame.erl style(emqx_gateway): improve some emqx_coap code 2021-08-03 09:48:08 +08:00
emqx_coap_impl.erl refactor: unify all io:format calls to use the same macro 2021-08-01 09:36:59 +02:00
emqx_coap_message.erl refactor(eqmx_gateway): port emqx_coap into emqx_gateway framework 2021-07-31 15:50:09 +08:00
emqx_coap_observe_res.erl style(emqx_gateway): improve some emqx_coap code 2021-08-03 09:48:08 +08:00
emqx_coap_resource.erl refactor(eqmx_gateway): port emqx_coap into emqx_gateway framework 2021-07-31 15:50:09 +08:00
emqx_coap_session.erl style(emqx_gateway): improve some emqx_coap code 2021-08-03 09:48:08 +08:00
emqx_coap_tm.erl style(emqx_gateway): improve some emqx_coap code 2021-08-03 09:48:08 +08:00
emqx_coap_transport.erl style(emqx_gateway): improve some emqx_coap code 2021-08-03 09:48:08 +08:00
README.md refactor(eqmx_gateway): port emqx_coap into emqx_gateway framework 2021-07-31 15:50:09 +08:00

Table of Contents

  1. EMQX 5.0 CoAP Gateway
  2. CoAP Message Processing Flow
    1. Request Timing Diagram
      1. Transport && Transport Manager
      2. Resource
  3. Resource
    1. MQTT Resource
    2. PubSub Resource
  4. Heartbeat
  5. Command
  6. MQTT QOS <=> CoAP non/con

EMQX 5.0 CoAP Gateway

emqx-coap is a CoAP Gateway for EMQ X Broker. It translates CoAP messages into MQTT messages and make it possible to communiate between CoAP clients and MQTT clients.

CoAP Message Processing Flow

Request Timing Diagram

,------.          ,------------.          ,-----------------.          ,---------.          ,--------.
|client|          |coap_gateway|          |transport_manager|          |transport|          |resource|
`--+---'          `-----+------'          `--------+--------'          `----+----'          `---+----'
   |                    |                          |                        |                   |
   | ------------------->                          |                        |                   |
   |                    |                          |                        |                   |
   |                    |                          |                        |                   |
   |                    | ------------------------>|                        |                   |
   |                    |                          |                        |                   |
   |                    |                          |                        |                   |
   |                    |                          |----------------------->|                   |
   |                    |                          |                        |                   |
   |                    |                          |                        |                   |
   |                    |                          |                        |------------------>|
   |                    |                          |                        |                   |
   |                    |                          |                        |                   |
   |                    |                          |                        |<------------------|
   |                    |                          |                        |                   |
   |                    |                          |                        |                   |
   |                    |                          |<-----------------------|                   |
   |                    |                          |                        |                   |
   |                    |                          |                        |                   |
   |                    | <------------------------|                        |                   |
   |                    |                          |                        |                   |
   |                    |                          |                        |                   |
   | <-------------------                          |                        |                   |
,--+---.          ,-----+------.          ,--------+--------.          ,----+----.          ,---+----.
|client|          |coap_gateway|          |transport_manager|          |transport|          |resource|
`------'          `------------'          `-----------------'          `---------'          `--------'

Transport && Transport Manager

Transport is a module that manages the life cycle and behaviour of CoAP messages
And the transport manager is to manage all transport which in this gateway

Resource

The Resource is a behaviour that must implement GET/PUT/POST/DELETE method
Different Resources can have different implementations of this four method
Each gateway can only use one Resource module to process CoAP Request Message

Resource

MQTT Resource

The MQTT Resource is a simple CoAP to MQTT adapter, the implementation of each method is as follows:

  • use uri path as topic
  • GET: subscribe the topic
  • PUT: publish message to this topic
  • POST: like PUT
  • DELETE: unsubscribe the topic

PubSub Resource

The PubSub Resource like the MQTT Resource, but has a retained topic's message database
This Resource is shared, only can has one instance. The implementation:

  • use uri path as topic
  • GET:
    • GET with observe = 0: subscribe the topic
    • GET with observe = 1: unsubscribe the topic
    • GET without observe: read lastest message from the message database, key is the topic
  • PUT: insert message into the message database, key is the topic
  • POST: like PUT, but will publish the message
  • DELETE: delete message from the database, key is topic

Heartbeat

At present, the CoAP gateway only supports UDP/DTLS connection, don't support UDP over TCP and UDP over WebSocket. Because UDP is connectionless, so the client needs to send heartbeat ping to the server interval. Otherwise, the server will close related resources Use POST with empty uri path as a heartbeat ping

example:

coap-client -m post coap://127.0.0.1

Command

Command is means the operation which outside the CoAP protocol, like authorization The Command format:

  1. use POST method
  2. uri path is empty
  3. query string is like action=comandX&argX=valuex&argY=valueY

example:

  1. connect:
coap-client -m post coap://127.0.0.1?action=connect&clientid=XXX&username=XXX&password=XXX
  1. disconnect:
coap-client -m post coap://127.0.0.1?action=disconnect

MQTT QOS <=> CoAP non/con

CoAP gateway uses some options to control the conversion between MQTT qos and coap non/con:

1.notify_type Control the type of notify messages when the observed object has changed.Can be:

  • non
  • con
  • qos in this value, MQTT QOS0 -> non, QOS1/QOS2 -> con

2.subscribe_qos Control the qos of subscribe.Can be:

  • qos0
  • qos1
  • qos2
  • coap in this value, CoAP non -> qos0, con -> qos1

3.publish_qos like subscribe_qos, but control the qos of the publish MQTT message

License

Apache License Version 2.0

Author

EMQ X Team.