Part 1
요약·해설
Socket을 직접 소유하는 application과 plain-buffer CoAP library의 책임 경계, server resource dispatch, client packet 작성, URI wildcard와 libcoap·TTCN-3 시험을 설명합니다.
RFC 7252
Client · Server
libcoap · TTCN-3
Part 2
접을 수 있는 영어 원문 전체
영어 원문 전체 펼치기
8F7A30D7ADC7677806EB5B445DF438F7268DA3DFC735DD7C1D35F82E2B99B7BE
.. _coap_sock_interface:
CoAP
#####
.. contents::
:local:
:depth: 2
Overview
********
The Constrained Application Protocol (CoAP) is a specialized web transfer
protocol for use with constrained nodes and constrained (e.g., low-power,
lossy) networks. It provides a convenient API for RESTful Web services
that support CoAP's features. For more information about the protocol
itself, see `IETF RFC7252 The Constrained Application Protocol <https://tools.ietf.org/html/rfc7252>`_.
Zephyr provides a CoAP library which supports client and server roles.
The library can be enabled with :kconfig:option:`CONFIG_COAP` Kconfig option and
is configurable as per user needs. The Zephyr CoAP library
is implemented using plain buffers. Users of the API create sockets
for communication and pass the buffer to the library for parsing and other
purposes. The library itself doesn't create any sockets for users.
On top of CoAP, Zephyr has support for LWM2M "Lightweight Machine 2 Machine"
protocol, a simple, low-cost remote management and service enablement mechanism.
See :ref:`lwm2m_interface` for more information.
Supported RFCs:
- `RFC7252: The Constrained Application Protocol (CoAP) <https://tools.ietf.org/html/rfc7252>`_
- `RFC6690: Constrained RESTful Environments (CoRE) Link Format <https://tools.ietf.org/html/rfc6690>`_
- `RFC7959: Block-Wise Transfers in the Constrained Application Protocol (CoAP) <https://tools.ietf.org/html/rfc7959>`_
- `RFC7641: Observing Resources in the Constrained Application Protocol (CoAP) <https://tools.ietf.org/html/rfc7641>`_
.. note:: Not all parts of these RFCs are supported. Features are supported based on Zephyr requirements.
Sample Usage
************
CoAP Server
===========
.. note::
A :ref:`coap_server_interface` subsystem is available, the following is for creating a custom
server implementation.
To create a CoAP server, resources for the server need to be defined.
The ``.well-known/core`` resource should be added before all other
resources that should be included in the responses of the ``.well-known/core``
resource.
.. code-block:: c
static struct coap_resource resources[] = {
{ .get = well_known_core_get,
.path = COAP_WELL_KNOWN_CORE_PATH,
},
{ .get = sample_get,
.post = sample_post,
.del = sample_del,
.put = sample_put,
.path = sample_path
},
{ },
};
An application reads data from the socket and passes the buffer to the CoAP library
to parse the message. If the CoAP message is proper, the library uses the buffer
along with resources defined above to call the correct callback function
to handle the CoAP request from the client. It's the callback function's
responsibility to either reply or act according to CoAP request.
.. code-block:: c
coap_packet_parse(&request, data, data_len, options, opt_num);
...
coap_handle_request(&request, resources, options, opt_num,
client_addr, client_addr_len);
If :kconfig:option:`CONFIG_COAP_URI_WILDCARD` enabled, server may accept multiple resources
using MQTT-like wildcard style:
- the plus symbol represents a single-level wild card in the path;
- the hash symbol represents the multi-level wild card in the path.
.. code-block:: c
static const char * const led_set[] = { "led","+","set", NULL };
static const char * const btn_get[] = { "button","#", NULL };
static const char * const no_wc[] = { "test","+1", NULL };
It accepts /led/0/set, led/1234/set, led/any/set, /button/door/1, /test/+1,
but returns -ENOENT for /led/1, /test/21, /test/1.
This option is enabled by default, disable it to avoid unexpected behaviour
with resource path like '/some_resource/+/#'.
CoAP Client
===========
.. note::
A :ref:`coap_client_interface` subsystem is available, the following is for creating a custom
client implementation.
If the CoAP client knows about resources in the CoAP server, the client can start
prepare CoAP requests and wait for responses. If the client doesn't know
about resources in the CoAP server, it can request resources through
the ``.well-known/core`` CoAP message.
.. code-block:: c
/* Initialize the CoAP message */
char *path = "test";
struct coap_packet request;
uint8_t data[100];
uint8_t payload[20];
coap_packet_init(&request, data, sizeof(data),
1, COAP_TYPE_CON, 8, coap_next_token(),
COAP_METHOD_GET, coap_next_id());
/* Append options */
coap_packet_append_option(&request, COAP_OPTION_URI_PATH,
path, strlen(path));
/* Append Payload marker if you are going to add payload */
coap_packet_append_payload_marker(&request);
/* Append payload */
coap_packet_append_payload(&request, (uint8_t *)payload,
sizeof(payload) - 1);
/* send over sockets */
Testing
*******
There are various ways to test Zephyr CoAP library.
libcoap
=======
libcoap implements a lightweight application-protocol for devices that are
resource constrained, such as by computing power, RF range, memory, bandwidth,
or network packet sizes. Sources can be found here `libcoap <https://github.com/obgm/libcoap>`_.
libcoap has a script (``examples/etsi_coaptest.sh``) to test coap-server functionality
in Zephyr.
See the `net-tools <https://github.com/zephyrproject-rtos/net-tools>`_ project for more details
The :zephyr:code-sample:`coap-server` sample can be built and executed on QEMU as described
in :ref:`networking_with_qemu`.
Use this command on the host to run the libcoap implementation of
the ETSI test cases:
.. code-block:: console
sudo ./libcoap/examples/etsi_coaptest.sh -i tap0 2001:db8::1
TTCN3
=====
Eclipse has TTCN3 based tests to run against CoAP implementations.
Install eclipse-titan and set symbolic links for titan tools
.. code-block:: console
sudo apt-get install eclipse-titan
cd /usr/share/titan
sudo ln -s /usr/bin bin
sudo ln /usr/bin/titanver bin
sudo ln -s /usr/bin/mctr_cli bin
sudo ln -s /usr/include/titan include
sudo ln -s /usr/lib/titan lib
export TTCN3_DIR=/usr/share/titan
git clone https://gitlab.eclipse.org/eclipse/titan/titan.misc.git
cd titan.misc
Follow the instruction to setup CoAP test suite from here:
- https://gitlab.eclipse.org/eclipse/titan/titan.misc
- https://gitlab.eclipse.org/eclipse/titan/titan.misc/-/tree/master/CoAP_Conf
After the build is complete, the :zephyr:code-sample:`coap-server` sample can be built
and executed on QEMU as described in :ref:`networking_with_qemu`.
Change the client (test suite) and server (Zephyr coap-server sample) addresses
in coap.cfg file as per your setup.
Execute the test cases with following command.
.. code-block:: console
ttcn3_start coaptests coap.cfg
Sample output of ttcn3 tests looks like this.
.. code-block:: console
Verdict statistics: 0 none (0.00 %), 10 pass (100.00 %), 0 inconc (0.00 %), 0 fail (0.00 %), 0 error (0.00 %).
Test execution summary: 10 test cases were executed. Overall verdict: pass
API Reference
*************
.. doxygengroup:: coap
Part 3
한국어 전문 번역
CoAP
Overview
Constrained Application Protocol(CoAP)은 constrained node와 low-power·lossy 같은 constrained network에 쓰는 특수 web transfer protocol입니다. CoAP feature를 지원하는 RESTful Web service용 편리한 API를 제공합니다. Protocol 자체는 IETF RFC 7252를 참고합니다.
CONFIG_COAP으로 Zephyr CoAP library를 활성화하며 user requirement에 맞게 설정할 수 있습니다. Library는 plain buffer로 구현됩니다. API 사용자가 통신 socket을 만들고 parsing 등의 작업을 위해 buffer를 library에 넘깁니다. Library 자체는 사용자를 대신해 socket을 만들지 않습니다.
Zephyr는 CoAP 위에서 LWM2M, 즉 Lightweight Machine to Machine protocol도 지원합니다. LWM2M은 단순하고 비용이 낮은 remote management·service enablement mechanism입니다.
지원 RFC는 다음과 같습니다.
- RFC 7252: The Constrained Application Protocol(CoAP)
- RFC 6690: Constrained RESTful Environments(CoRE) Link Format
- RFC 7959: Block-Wise Transfers in CoAP
- RFC 7641: Observing Resources in CoAP
Sample Usage
CoAP Server
CoAP server를 만들려면 server resource를 정의합니다. .well-known/core response에 포함할 모든 resource보다 .well-known/core resource를 먼저 추가해야 합니다.
static struct coap_resource resources[] = {
{ .get = well_known_core_get,
.path = COAP_WELL_KNOWN_CORE_PATH,
},
{ .get = sample_get,
.post = sample_post,
.del = sample_del,
.put = sample_put,
.path = sample_path
},
{ },
};Application은 socket에서 data를 읽어 CoAP library가 message를 parse하도록 buffer를 전달합니다. CoAP message가 올바르면 library는 그 buffer와 위 resource 목록을 사용해 맞는 callback function을 호출합니다. Callback은 client request에 답하거나 request가 요구한 동작을 수행해야 합니다.
coap_packet_parse(&request, data, data_len, options, opt_num);
...
coap_handle_request(&request, resources, options, opt_num,
client_addr, client_addr_len);CONFIG_COAP_URI_WILDCARD를 활성화하면 MQTT와 비슷한 wildcard style로 server resource 여러 개를 받을 수 있습니다.
+symbol은 path의 single-level wildcard입니다.#symbol은 path의 multi-level wildcard입니다.
static const char * const led_set[] = { "led","+","set", NULL };
static const char * const btn_get[] = { "button","#", NULL };
static const char * const no_wc[] = { "test","+1", NULL };/led/0/set, led/1234/set, led/any/set, /button/door/1, /test/+1은 받아들이지만 /led/1, /test/21, /test/1은 -ENOENT를 반환합니다.
이 option은 기본 활성화입니다. /some_resource/+/# 같은 resource path에서 예상하지 못한 동작을 피하려면 비활성화합니다.
CoAP Client
Client가 server resource를 안다면 CoAP request를 준비하고 response를 기다릴 수 있습니다. Resource를 모르면 .well-known/core CoAP message로 resource 목록을 요청할 수 있습니다.
/* Initialize the CoAP message */
char *path = "test";
struct coap_packet request;
uint8_t data[100];
uint8_t payload[20];
coap_packet_init(&request, data, sizeof(data),
1, COAP_TYPE_CON, 8, coap_next_token(),
COAP_METHOD_GET, coap_next_id());
/* Append options */
coap_packet_append_option(&request, COAP_OPTION_URI_PATH,
path, strlen(path));
/* Append Payload marker if you are going to add payload */
coap_packet_append_payload_marker(&request);
/* Append payload */
coap_packet_append_payload(&request, (uint8_t *)payload,
sizeof(payload) - 1);
/* send over sockets */Testing
Zephyr CoAP library는 여러 방식으로 시험할 수 있습니다.
libcoap
libcoap은 compute power, RF range, memory, bandwidth 또는 packet size가 제한된 장치를 위한 lightweight application protocol 구현입니다. Source는 libcoap repository에 있습니다.
examples/etsi_coaptest.sh script로 Zephyr coap-server 기능을 시험할 수 있습니다. 자세한 환경은 Zephyr net-tools project를 참고합니다. coap-server sample은 networking with QEMU 절차로 build·실행할 수 있습니다.
Host에서 ETSI test case의 libcoap 구현을 실행합니다.
sudo ./libcoap/examples/etsi_coaptest.sh -i tap0 2001:db8::1TTCN-3
Eclipse는 CoAP 구현을 시험할 TTCN-3 기반 test를 제공합니다. Eclipse Titan을 설치하고 Titan tool symbolic link를 설정합니다.
sudo apt-get install eclipse-titan
cd /usr/share/titan
sudo ln -s /usr/bin bin
sudo ln /usr/bin/titanver bin
sudo ln -s /usr/bin/mctr_cli bin
sudo ln -s /usr/include/titan include
sudo ln -s /usr/lib/titan lib
export TTCN3_DIR=/usr/share/titan
git clone https://gitlab.eclipse.org/eclipse/titan/titan.misc.git
cd titan.miscCoAP test suite 설정 지침은 다음 project와 CoAP_Conf directory를 따릅니다.
coap-server sample을 QEMU에서 build·실행하고 환경에 맞게 coap.cfg의 client test suite와 Zephyr server address를 바꿉니다.
다음 command로 test case를 실행합니다.
ttcn3_start coaptests coap.cfgTTCN-3 test output 예입니다.
Verdict statistics: 0 none (0.00 %), 10 pass (100.00 %), 0 inconc (0.00 %), 0 fail (0.00 %), 0 error (0.00 %).
Test execution summary: 10 test cases were executed. Overall verdict: passAPI Reference
Doxygen group coap가 API reference를 제공합니다.
Source
출처
영어 원문에는 문단, 문서 지시문, 표, 코드, 코드 심벌, 경로를 그대로 보존했습니다.