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

Zephyr 3.7.0 · Connectivity · Networking API

MQTT-SN

TCP 없이 UDP·Bluetooth·UART 같은 message transport로 gateway에 연결합니다.

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

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

Part 1

요약·해설

MQTT-SN 1.2 client와 gateway, UDP transport, automatic upkeep, deinit과 미지원 표준 기능을 설명합니다.

Version

MQTT-SN 1.2

Transport

Message-based

Bridge

MQTT-SN gateway

Part 2

접을 수 있는 영어 원문 전체

영어 원문 전체 펼치기
원문 SHA-256 63E2584F899FC4C80BEA235732C8014C1CFF2E0AE3ADF9850F1909E4ACA62A31
.. _mqtt_sn_socket_interface:

MQTT-SN
#######

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

Overview
********

MQTT-SN is a variant of the well-known MQTT protocol - see :ref:`mqtt_socket_interface`.

In contrast to MQTT, MQTT-SN does not require a TCP transport, but is designed to be used
over any message-based transport. Originally, it was mainly created with ZigBee in mind,
but others like Bluetooth, UDP or even a UART can be used just as well.

Zephyr provides an MQTT-SN client library built on top of BSD sockets API. The
library can be enabled with :kconfig:option:`CONFIG_MQTT_SN_LIB` Kconfig option
and is configurable at a per-client basis, with support for MQTT-SN version
1.2. The Zephyr MQTT-SN implementation can be used with any message-based transport,
but support for UDP is already built-in.

MQTT-SN clients require an MQTT-SN gateway to connect to. These gateways translate between
MQTT-SN and MQTT. The Eclipse Paho project offers an implementation of a MQTT-SN gateway, but
others are available too.
https://www.eclipse.org/paho/index.php?page=components/mqtt-sn-transparent-gateway/index.php

The MQTT-SN spec v1.2 can be found here:
https://www.oasis-open.org/committees/download.php/66091/MQTT-SN_spec_v1.2.pdf

Sample usage
************

To create an MQTT-SN client, a client context structure and buffers need to be
defined:

.. code-block:: c

   /* Buffers for MQTT client. */
   static uint8_t rx_buffer[256];
   static uint8_t tx_buffer[256];

   /* MQTT-SN client context */
   static struct mqtt_sn_client client;

Multiple MQTT-SN client instances can be created in the application and managed
independently. Additionally, a structure for the transport is needed as well.
The library already comes with an example implementation for UDP.

.. code-block:: c

   /* MQTT Broker address information. */
   static struct mqtt_sn_transport tp;

The MQTT-SN library will inform clients about certain events using a callback.

.. code-block:: c

   static void evt_cb(struct mqtt_sn_client *client,
                      const struct mqtt_sn_evt *evt)
   {
      switch(evt->type) {
      {
         /* Handle events here. */
      }
   }

For a list of possible events, see :ref:`mqtt_sn_api_reference`.

The client context structure needs to be initialized and set up before it can be
used. An example configuration for UDP transport is shown below:

.. code-block:: c

   struct mqtt_sn_data client_id = MQTT_SN_DATA_STRING_LITERAL("ZEPHYR");
   struct sockaddr_in gateway = {0};

   uint8_t tx_buf[256];
   uint8_t rx_buf[256];

   mqtt_sn_transport_udp_init(&tp, (struct sockaddr*)&gateway, sizeof((gateway)));

   mqtt_sn_client_init(&client, &client_id, &tp.tp, evt_cb, tx_buf, sizeof(tx_buf), rx_buf, sizeof(rx_buf));

After the configuration is set up, the MQTT-SN client can connect to the gateway.
While the MQTT-SN protocol offers functionality to discover gateways through an
advertisement mechanism, this is not implemented yet in the library.

Call the ``mqtt_sn_connect`` function, which will send a ``CONNECT`` message.
The application should periodically call the ``mqtt_sn_input`` function to process
the response received. The application does not have to call ``mqtt_sn_input`` if it
knows that no data has been received (e.g. when using Bluetooth). Note that
``mqtt_sn_input`` is a non-blocking function, if the transport struct contains a
``poll`` compatible function pointer.
If the connection was successful, ``MQTT_SN_EVT_CONNECTED`` will be notified to the
application through the callback function.

.. code-block:: c

	err = mqtt_sn_connect(&client, false, true);
	__ASSERT(err == 0, "mqtt_sn_connect() failed %d", err);

	while (1) {
		mqtt_sn_input(&client);
		if (connected) {
			mqtt_sn_publish(&client, MQTT_SN_QOS_0, &topic_p, false, &pubdata);
		}
		k_sleep(K_MSEC(500));
	}

In the above code snippet, the event handler function should set the ``connected``
flag upon a successful connection. If the connection fails at the MQTT level
or a timeout occurs, the connection will be aborted.

After the connection is established, an application needs to call ``mqtt_input``
function periodically to process incoming data. Connection upkeep, on the other hand,
is done automatically using a k_work item.
If a MQTT message is received, an MQTT callback function will be called and an
appropriate event notified.

The connection can be closed by calling the ``mqtt_sn_disconnect`` function. This
has no effect on the transport, however. If you want to close the transport (e.g.
the socket), call ``mqtt_sn_client_deinit``, which will deinit the transport as well.

Zephyr provides sample code utilizing the MQTT-SN client API. See
:zephyr:code-sample:`mqtt-sn-publisher` for more information.

Deviations from the standard
****************************

Certain parts of the protocol are not yet supported in the library.

* Pre-defined topic IDs
* QoS -1 - it's most useful with predefined topics
* Gateway discovery using ADVERTISE, SEARCHGW and GWINFO messages.
* Setting the will topic and message after the initial connect
* Forwarder Encapsulation

.. _mqtt_sn_api_reference:

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

.. doxygengroup:: mqtt_sn_socket

Part 3

한국어 전문 번역

MQTT-SN

Overview

MQTT-SN은 MQTT protocol의 변형입니다.

MQTT와 달리 MQTT-SN은 TCP transport를 요구하지 않으며 모든 message-based transport에서 사용하도록 설계됐습니다. 처음에는 주로 ZigBee를 염두에 두었지만 Bluetooth, UDP, UART도 사용할 수 있습니다.

CONFIG_MQTT_SN_LIB Kconfig option으로 BSD socket API 기반 MQTT-SN client library를 활성화할 수 있습니다. Client별로 설정할 수 있고 MQTT-SN 1.2를 지원합니다. 모든 message-based transport를 사용할 수 있으며 UDP support는 library에 내장돼 있습니다.

MQTT-SN client는 MQTT-SN gateway에 연결합니다. Gateway는 MQTT-SN과 MQTT 사이를 변환합니다. 구현 예로 Eclipse Paho MQTT-SN gateway가 있습니다.

MQTT-SN 1.2 specification은 OASIS 문서에서 확인할 수 있습니다.

Sample usage

MQTT-SN client를 만들려면 client context structure와 buffer를 정의합니다.

/* Buffers for MQTT client. */
static uint8_t rx_buffer[256];
static uint8_t tx_buffer[256];

/* MQTT-SN client context */
static struct mqtt_sn_client client;

Application에서 MQTT-SN client instance를 여러 개 만들어 독립적으로 관리할 수 있습니다. Transport structure도 필요하며 library가 UDP example implementation을 제공합니다.

/* MQTT Broker address information. */
static struct mqtt_sn_transport tp;

MQTT-SN library는 callback을 사용해 client에 event를 알립니다.

static void evt_cb(struct mqtt_sn_client *client,
                   const struct mqtt_sn_evt *evt)
{
   switch(evt->type) {
   {
      /* Handle events here. */
   }
}

가능한 event 목록은 MQTT-SN API reference에서 확인할 수 있습니다.

Client context structure는 사용 전에 initialize하고 설정해야 합니다. 다음은 UDP transport configuration 예입니다.

struct mqtt_sn_data client_id = MQTT_SN_DATA_STRING_LITERAL("ZEPHYR");
struct sockaddr_in gateway = {0};

uint8_t tx_buf[256];
uint8_t rx_buf[256];

mqtt_sn_transport_udp_init(&tp, (struct sockaddr*)&gateway, sizeof((gateway)));

mqtt_sn_client_init(&client, &client_id, &tp.tp, evt_cb, tx_buf, sizeof(tx_buf), rx_buf, sizeof(rx_buf));

mqtt_sn_transport_udp_init()으로 gateway address와 UDP transport를 설정하고 mqtt_sn_client_init()에 client ID, transport, event callback, TX·RX buffer를 전달합니다.

Configuration 뒤 MQTT-SN client가 gateway에 연결할 수 있습니다. MQTT-SN protocol에는 advertisement mechanism으로 gateway를 발견하는 기능이 있지만 Zephyr library에는 아직 구현되지 않았습니다.

mqtt_sn_connect()는 CONNECT message를 보냅니다. Application은 mqtt_sn_input()을 주기적으로 호출해 response를 처리합니다. Bluetooth처럼 data를 받지 않았다는 것을 확실히 아는 경우에는 호출하지 않아도 됩니다. Transport structure에 poll compatible function pointer가 있으면 mqtt_sn_input()은 non-blocking입니다. 성공 시 callback으로 MQTT_SN_EVT_CONNECTED가 전달됩니다.

err = mqtt_sn_connect(&client, false, true);
__ASSERT(err == 0, "mqtt_sn_connect() failed %d", err);

while (1) {
	mqtt_sn_input(&client);
	if (connected) {
		mqtt_sn_publish(&client, MQTT_SN_QOS_0, &topic_p, false, &pubdata);
	}
	k_sleep(K_MSEC(500));
}

위 예에서 event handler는 성공 시 connected flag를 설정해야 합니다. MQTT level connection failure 또는 timeout이면 connection이 중단됩니다.

Connection 뒤 incoming data를 처리하려면 input function을 주기적으로 호출해야 합니다. 공식 원문의 이 문단은 함수 이름을 mqtt_input()으로 적지만 앞 설명과 실제 MQTT-SN 예제는 mqtt_sn_input()을 사용합니다. Connection upkeep은 k_work item이 자동으로 수행합니다. Message가 도착하면 callback과 해당 event가 호출됩니다.

mqtt_sn_disconnect()는 MQTT-SN connection을 닫지만 transport 자체에는 영향을 주지 않습니다. Socket 같은 transport까지 닫으려면 mqtt_sn_client_deinit()을 호출해 transport도 deinitialize해야 합니다.

mqtt-sn-publisher sample에서 MQTT-SN client API 사용 code를 확인할 수 있습니다.

Deviations from the standard

Library가 아직 지원하지 않는 protocol 기능은 다음과 같습니다.

  • Pre-defined topic ID
  • QoS -1. Pre-defined topic에서 가장 유용한 기능입니다.
  • ADVERTISE, SEARCHGW, GWINFO message를 사용하는 gateway discovery
  • Initial connect 뒤 will topic과 message 설정
  • Forwarder Encapsulation

API Reference

Doxygen group mqtt_sn_socket이 API reference를 제공합니다.

Source

출처

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