connectivity/networking/api/coap_client.html · connectivity/networking/api/coap_client.rst 공식 원문 ↗

Zephyr 3.7.0 · Connectivity · Networking API

CoAP Client

UDP 또는 DTLS socket에서 request를 보내고 callback으로 response block을 처리합니다.

Official pathconnectivity/networking/api/coap_client.html
Source fileconnectivity/networking/api/coap_client.rst
Source versionZephyr 3.7.0
TranslationFull · reviewed

본문 기준: Zephyr 3.7.0 LTS3 · 이후 유지보수 패치는 별도입니다.

Part 1

요약·해설

Application이 socket lifetime을 소유하고 CoAP client library가 request/response 통신을 담당하는 구조, 동시 request 제한과 block-wise callback 종료 조건을 설명합니다.

Transport

UDP · DTLS

Concurrency

1 request/client

Response

last_block callback

Part 2

접을 수 있는 영어 원문 전체

영어 원문 전체 펼치기
원문 SHA-256 B64BF2AEC7CC81093F065F0A322E926AFFCEF652C4758818DFEAB1704BBB6176
.. _coap_client_interface:

CoAP client
###########

.. contents::
    :local:
    :depth: 2

Overview
********

The CoAP client library allows application to send CoAP requests and parse CoAP responses.
The library can be enabled with :kconfig:option:`CONFIG_COAP_CLIENT` Kconfig option.
The application is notified about the response via a callback that is provided to the API
in the request. The CoAP client handles the communication over sockets.
As the CoAP client doesn't create socket it is using, the application is responsible for creating
the socket. Plain UDP or DTLS sockets are supported.

Sample Usage
************

The following is an example of a CoAP client initialization and request sending:

.. code-block:: c

    static struct coap_client;
    struct coap_client_request req = { 0 };

    coap_client_init(&client, NULL);

    req.method = COAP_METHOD_GET;
    req.confirmable = true;
    req.path = "test";
    req.fmt = COAP_CONTENT_FORMAT_TEXT_PLAIN;
    req.cb = response_cb;
    req.payload = NULL;
    req.len = 0;

    /* Sock is a file descriptor referencing a socket, address is the sockaddr struct for the
     * destination address of the request or NULL if the socket is already connected.
     */
    ret = coap_client_req(&client, sock, &address, &req, -1);

Before any requests can be sent, the CoAP client needs to be initialized.
After initialization, the application can send a CoAP request and wait for the response.
Currently only one request can be sent for a single CoAP client at a time. There can be multiple
CoAP clients.

The callback provided in the callback will be called in following cases:

- There is a response for the request
- The request failed for some reason

The callback contains a flag `last_block`, which indicates if there is more data to come in the
response and means that the current response is part of a blockwise transfer. When the `last_block`
is set to true, the response is finished and the client is ready for the next request after
returning from the callback.

If the server responds to the request, the library provides the response to the
application through the response callback registered in the request structure.
As the response can be a blockwise transfer and the client calls the callback once per each
block, the application should be to process all of the blocks to be able to process the response.

The following is an example of a very simple response handling function:

.. code-block:: c

    void response_cb(int16_t code, size_t offset, const uint8_t *payload, size_t len,
                     bool last_block, void *user_data)
    {
        if (code >= 0) {
	        LOG_INF("CoAP response from server %d", code);
                if (last_block) {
                        LOG_INF("Last packet received");
                }
        } else {
                LOG_ERR("Error in sending request %d", code);
        }
    }

API Reference
*************

.. doxygengroup:: coap_client

Part 3

한국어 전문 번역

CoAP client

Overview

CoAP client library는 application이 CoAP request를 보내고 CoAP response를 parse하게 합니다. CONFIG_COAP_CLIENT Kconfig option으로 활성화합니다.

Application은 request를 API에 전달할 때 response callback을 함께 제공하며, library는 response가 오면 이 callback으로 알립니다. CoAP client가 socket 통신을 처리하지만 socket 자체를 만들지는 않습니다. Socket 생성은 application 책임이며 plain UDP와 DTLS socket을 지원합니다.

Sample Usage

CoAP client를 초기화하고 request를 보내는 예입니다.

static struct coap_client;
struct coap_client_request req = { 0 };

coap_client_init(&client, NULL);

req.method = COAP_METHOD_GET;
req.confirmable = true;
req.path = "test";
req.fmt = COAP_CONTENT_FORMAT_TEXT_PLAIN;
req.cb = response_cb;
req.payload = NULL;
req.len = 0;

/* Sock is a file descriptor referencing a socket, address is the sockaddr struct for the
 * destination address of the request or NULL if the socket is already connected.
 */
ret = coap_client_req(&client, sock, &address, &req, -1);

Request를 보내기 전에 coap_client_init()으로 client를 초기화해야 합니다. 초기화 후 application은 coap_client_req()로 request를 보내고 response를 기다립니다.

현재 CoAP client 하나는 한 번에 request 하나만 보낼 수 있습니다. 다만 client object를 여러 개 만들 수 있습니다.

등록한 callback은 다음 경우 호출됩니다.

  • Request에 대한 response가 도착했을 때
  • 어떤 이유로 request가 실패했을 때

last_block flag는 response에서 더 받을 data가 있는지를 나타냅니다. 더 받을 data가 있다면 현재 response가 block-wise transfer 일부라는 뜻입니다. last_block이 true이면 response가 끝났고 callback이 return한 뒤 client가 다음 request를 받을 준비가 됩니다.

Server가 답하면 library는 request structure에 등록한 response callback으로 response를 application에 전달합니다. Block-wise response이면 block마다 callback이 한 번 호출되므로 application은 전체 response를 처리할 수 있도록 모든 block을 처리해야 합니다.

아주 단순한 response handler 예입니다.

void response_cb(int16_t code, size_t offset, const uint8_t *payload, size_t len,
                 bool last_block, void *user_data)
{
    if (code >= 0) {
     LOG_INF("CoAP response from server %d", code);
            if (last_block) {
                    LOG_INF("Last packet received");
            }
    } else {
            LOG_ERR("Error in sending request %d", code);
    }
}

API Reference

Doxygen group coap_client가 API reference를 제공합니다.

Source

출처

영어 원문에는 문단, 문서 지시문, 표, 코드, 코드 심벌, 경로를 그대로 보존했습니다.