## Page Guide

- **Use Cases**: Suitable for low-power, weak-network, lightweight devices connecting to the platform over the CoAP protocol.
- **Prerequisites**: The device resource URI, message format, authentication Token, Message ID, and context handling method have been confirmed.
- **Debugging Tools**: You can use coap-cli or libcoap to verify GET, POST, PUT, and DELETE requests.
- **Acceptance Criteria**: The device can complete resource discovery, time query, device registration, property reporting, and command response.

## 1. CoAP Protocol Device Access

### CoAP Roadmap

* CoAP message encoding and decoding: message header, options, message body
* CoAP data route registration
* CoAP observe mechanism
* CoAP message block handling
* CoAP authentication handling: Token, Message ID
* CoAP content context
* CoAP TransportService northbound interface

### CoAP Southbound Interface

* .well-known/core: query the URI resources supported by the server
* /utc-time: query the server utc-time
* Other extensions (device registration, device online/offline, device property reporting, device)

### CoAP Northbound Interface (TransportService, the interface class exposed to the business layer)

* Device command delivery
* Device property setting
* Device upgrade
* Device restart

### CoAP TransportContext Context

* Forwarders (HTTP forwarding, MQTT broker forwarding)
* Message format adapters (Thing Model encoding, JSON, byte encoding, raw data)
* Scheduler
* Execution thread pool
* Request messages
* Response messages
* Session management

### CoAP TransportHandler Handlers

* ReqDispatcher message dispatching

## 2. Test Tools

To test whether our code works correctly, we need a CoAP command line tool. There are currently two good tools available.

* CoAP-cli, a NodeJS-based CoAP command line tool whose core is built on the Node-CoAP library.
* libcoap, a CoAP command line tool written in C.

### 1. Node CoAP CLI

The installation command is as follows

```
npm install coap-cli -g
```

#### 1.1 CoAP Command Line

In coap-cli, there are four methods in total, corresponding to the four different REST operations:

```
Commands:

get                    performs a GET request
put                    performs a PUT request
post                   performs a POST request
delete                 performs a DELETE request
Here, we use coap://vs0.inf.ethz.ch/ for a simple test

coap get coap://vs0.inf.ethz.ch/
(2.05)  ************************************************************
I-D
Let's test the smallest IoT system available today, the CoAP edition

coap get coap://iot-coap.phodal.com/id/1
(2.05)  [{"id":1,"value":"is id 1","sensors1":19,"sensors2":20}]
```

### 2. libcoap

#### 2.1 Windows libcoap Installation

Use the pre-compiled libcoap image:

```
$ docker run -it --rm --name libcoap --network host heeejianbo/my-libcoap:1.0
```

By running the coap-client command inside it, we can check the version of the CoAP client currently in use:

```
$ coap-client
coap-client v4.3.1 -- a small CoAP implementation
Copyright (C) 2010-2022 Olaf Bergmann <bergmann@tzi.org> and others

Build: v4.3.1-4-g02b7647
TLS Library: OpenSSL - runtime 1.1.1t, libcoap built for 1.1.1t
(DTLS and TLS support; PSK, PKI, PKCS11, and no RPK support)
```

### 3. EMQX CoAP Gateway Test

#### 3.1 PubSub Mode

* Publish a message

```
Use coap-client to publish a message to test/topic for testing:
coap-client -m post -e "Hello, CoAP" "coap://127.0.0.1/ps/test/topic?qos=0"
```

* Subscribe to messages

```
Create a CoAP subscriber and wait 60 seconds to receive messages on the test/topic topic:
coap-client -m get -s 60 -O 6,0x00 -o - -T "obstoken" "coap://127.0.0.1/ps/test/topic"
```

#### 3.2 Connection Mode

* Create a connection
  The CoAP client can create a connection by requesting /mqtt/connection, for example:

```
coap-client -m post -e "" "coap://127.0.0.1/mqtt/connection?clientid=coaptest&username=admin&password=admin123"
coap-client -m post -e "" "coap://192.168.5.48/mqtt/connection?clientid=coaptest&username=admin&password=admin123"
After the connection is created successfully, a Token is returned.
```

* Disconnect

```
coap-client -m delete -e "" "coap://127.0.0.1/mqtt/connection?clientid=coaptest&token=3404490787"
```

* Heartbeat

```
coap-client -m put -e "" "coap://127.0.0.1/mqtt/connection?clientid=coaptest&token=3404490787"
```

* Send messages

```
Sending messages in connection mode requires the Token and ClientId parameters, for example:
coap-client -m post -e "Hi, this is libcoap" "coap://127.0.0.1/ps/coap/test?clientid=coaptest&token=3404490787"


coap-client -m post -e "[{\"id\": \"temperature\", \"value\": 17, \"remark\": \"test\" }]" "coap://192.168.5.48/ps//41/D1ELV3A5TOJS/property/post?clientid=coaptest&token=3404490787"
coap-client -m post -e "[{\"id\": \"switch\", \"value\": 1, \"remark\": \"test\" }]" "coap://192.168.5.48/ps//41/D1ELV3A5TOJS/function/post?clientid=coaptest&token=3404490787"
coap-client -m post -e "[{\"id\": \"height_temperature\", \"value\": 50, \"remark\": \"test\" }]" "coap://192.168.5.48/ps//41/D1ELV3A5TOJS/event/post?clientid=coaptest&token=3404490787"

```

* Subscribe to topics

```
coap-client -m get -s 60 -O 6,0x00 -o - -T "obstoken" "coap://127.0.0.1/ps/coap/test?clientid=coaptest&token=3404490787"
coap-client -m get -s 60 -O 6,0x00 -o - -T "obstoken" "coap://192.168.5.48/ps//41/D1ELV3A5TOJS/function/post?clientid=coaptest&token=3404490787"
```

* Unsubscribe

```
coap-client -m get -O 6,0x01 "coap://127.0.0.1/ps/coap/test?clientid=coaptest&token=3404490787"
```

### 4. FastBee CoAP Access Test

#### Test Commands

* Create a connection
  The CoAP client can create a connection by requesting /mqtt/connection, for example:

```
clientid = S-D1K031BE49X2-146-1

coap-client -m post -e "" "coap://192.168.5.48/connection?clientid=S-D1K031BE49X2-146-1&username=FastBee&password=P1N864X0420SN171"
After the connection is created successfully, a Token is returned.
```

* Disconnect

```
coap-client -m delete -e "" "coap://192.168.5.48/connection?clientid=S-D1K031BE49X2-146-1&token=CYG4UQRZKWY4SUT"
```

* Heartbeat

```
coap-client -m post -e "" "coap://192.168.5.48/keepalive?token=CYG4UQRZKWY4SUT"
```

* Send messages

```
Sending messages requires the Token parameter, for example:

coap-client -m post -e "[{\"rssi\": -43, \"firmwareVersion\": 1.2, \"userId\": 2, \"status\": 3, \"longitude\": 0, \"latitude\": 0}]" "coap://192.168.5.48/info/post?token=CYG4UQRZKWY4SUT"
coap-client -m post -e "[{\"id\": \"temperature\", \"value\": 17, \"remark\": \"test\" }]" "coap://192.168.5.48/property/post?token=CYG4UQRZKWY4SUT"
coap-client -m post -e "[{\"id\": \"switch\", \"value\": 1, \"remark\": \"test\" }]" "coap://192.168.5.48/function/post?token=CYG4UQRZKWY4SUT"
coap-client -m post -e "[{\"id\": \"height_temperature\", \"value\": 50, \"remark\": \"test\" }]" "coap://192.168.5.48/event/post?token=CYG4UQRZKWY4SUT"
```
