1. MQTT 传输协议

MQTT(消息队列遥测传输)是ISO 标准(ISO/IEC PRF 20922)下基于发布/订阅范式的消息协议。它工作在 TCP/IP协议族上,是为硬件性能低下的远程设备以及网络状况糟糕的情况下而设计的发布/订阅型消息协议,为此,它需要一个消息中间件。

文档v5


2. CoAP 传输协议

CoAP(Constrained Application Protocol)是一种在物联网世界的类web协议,它的详细规范定义在 RFC 7252。COAP名字翻译来就是“受限应用协议”,顾名思义,使用在资源受限的物联网设备上。物联网设备的ram,rom都通常非常小,运行TCP和HTTP是不可以接受的。

CoAP采用UDP传输,参照HTPP的REST实现POST、GET、PUT、DELETE访问服务端。

CoAP的默认端口是udp 5683(CoAPs是5684)

libcoap

4.2.1 doc

$ sudo yum install autoconf automake libtool
$ sudo yun install doxygen asciidoc

$ tar -xvf CUnit-2.1-3.tar.bz2
$ cd CUnit-2.1-3
$ aclocal
$ autoheader
$ autoconf
$ automake
# 如果在automake过程中有文件丢失,则执行下面命令:
$ automake --add-missing
$ automake
# 如果出现错误:configure.in:161:required file './ltmain.sh' not found,则执行下面的命令:
$ libtoolize --automake --copy --debug --force
$ automake
$ ./configure --prefix=/usr/local/cunit
$ make
$ sudo make install

$ wget https://www.openssl.org/source/openssl-1.1.1j.tar.gz
$ tar -xvf openssl-1.1.1j.tar.gz
$ cd openssl-1.1.1j
$ ./config --prefix=/usr/local/openssl
$ make
$ sudo make install
$ sudo ln -sf /usr/local/openssl/bin/openssl /usr/bin/openssl
$ sudo vi /etc/ld.so.conf.d/openssl-1.1.1j..conf
/usr/local/openssl/lib
$ sudo ldconfig -v
$ openssl version
OpenSSL 1.1.1j  16 Feb 2021

$ export PKG_CONFIG_PATH=/usr/local/openssl/lib/pkgconfig/:/usr/local/cunit/lib/pkgconfig/:$PKG_CONFIG_PATH

$ cd libcoap
$ git checkout remotes/origin/release-4.2.1
$ autoconf
configure.ac:23: error: possibly undefined macro: AM_INIT_AUTOMAKE
      If this token and others are legitimate, please use m4_pattern_allow.
      See the Autoconf documentation.
configure.ac:25: error: possibly undefined macro: AM_SILENT_RULES
configure.ac:34: error: possibly undefined macro: AM_PROG_CC_C_O
configure.ac:176: error: possibly undefined macro: AM_CONDITIONAL
configure.ac:198: error: possibly undefined macro: AC_MSG_WARN
configure.ac:199: error: possibly undefined macro: AC_MSG_ERROR
configure.ac:206: error: possibly undefined macro: AC_MSG_RESULT
configure.ac:268: error: possibly undefined macro: AM_COND_IF
configure.ac:268: error: possibly undefined macro: AC_CONFIG_FILES
configure.ac:534: error: possibly undefined macro: AC_DEFINE
# 以上错误的处理
$ autoreconf --install
$ autoconf
# -–enable-documentation=no
$ ./configure --with-openssl --enable-tests --enable-shared
$ make

4.3.0

$ ./autogen.sh
$ ./configure --disable-doxygen --disable-manpages --with-mbedtls --enable-examples=no --enable-examples-source=no --with-epoll=no --enable-add-default-names=no --enable-shared=no

...
libcoap configuration summary:
      libcoap package version  : "4.3.0"
      libcoap library version  : "3.0.0"
      libcoap API version      : "3"
      libcoap DTLS lib extn    : "-mbedtls"
      host system              : "x86_64-pc-linux-gnu"
      build with TCP support   : "yes"
      build DTLS support       : "yes"
         -->  Mbed TLS around  : "yes" (found Mbed TLS 2.16.0)
              MBEDTLS_CFLAGS   : ""
              MBEDTLS_LIBS     : "-lmbedtls -lmbedcrypto -lmbedx509"
      add default names        : "no"
      build using epoll        : "no"
      enable small stack size  : "no"
      enable separate responses: "yes"
      build doxygen pages      : "no"
      build man pages          : "no"
      build unit test binary   : "no"
      build examples           : "no"
      install examples source  : "no"
      build with gcov support  : "no"
      build shared library     : "no"
      build static library     : "yes"

4.3.0 cmake

$ cd build
$ cmake .. -DWITH_EPOLL=OFF -DENABLE_EXAMPLES=OFF -DENABLE_DOCS=OFF -DDTLS_BACKEND=mbedtls

-- DTLS_BACKEND: mbedtls
-- Found MBEDTLS: /usr/include  
-- MBEDTLS_INCLUDE_DIRS: /usr/include
-- MBEDTLS_LIBRARY: /usr/lib/x86_64-linux-gnu/libmbedtls.so
-- MBEDX509_LIBRARY: /usr/lib/x86_64-linux-gnu/libmbedx509.so
-- MBEDCRYPTO_LIBRARY: /usr/lib/x86_64-linux-gnu/libmbedcrypto.so
-- MBEDTLS_LIBRARIES: /usr/lib/x86_64-linux-gnu/libmbedtls.so;/usr/lib/x86_64-linux-gnu/libmbedx509.so;/usr/lib/x86_64-linux-gnu/libmbedcrypto.so
-- compiling with mbedtls support
-- ENABLE_DTLS:.....................ON
-- ENABLE_TCP:......................ON
-- ENABLE_DOCS:.....................OFF
-- ENABLE_EXAMPLES:.................OFF
-- DTLS_BACKEND:....................mbedtls
-- WITH_GNUTLS:.....................OFF
-- WITH_TINYDTLS:...................OFF
-- WITH_OPENSSL:....................OFF
-- WITH_MBEDTLS:....................ON
-- HAVE_LIBTINYDTLS:................
-- HAVE_LIBGNUTLS:..................
-- HAVE_OPENSSL:....................
-- HAVE_MBEDTLS:....................1
-- COAP_EPOLL_SUPPORT:..............
-- CMAKE_C_COMPILER:................/usr/bin/cc
-- BUILD_SHARED_LIBS:...............OFF
-- CMAKE_BUILD_TYPE:................Debug
-- CMAKE_SYSTEM_PROCESSOR:..........x86_64
-- Configuring done
-- Generating done
-- Build files have been written to: /home/wii/data/works/libcoap/build

例子


3. CBOR 数据格式协议

简明二进制对象展现(CBOR,Concise Binary Object Representation)是一种提供良好压缩性,扩展性强,不需要进行版本协商的二进制数据交换形式。这些特性使它有别于早期的ASN.1和MessagePack等二进制序列化方式。RFC 7049定义了详细的CBOR格式与说明。

TinyCBOR

$ git clone https://github.com/intel/tinycbor

# 安装依赖cJSON in Debian,不安装此库将不会编译json2cbor工具
$ sudo apt-get install libcjson-dev

# 编译
$ cd tinycbor
$ make

$ cd  bin
json2cbor # json转cbor
cbordump  # cbor解析

$ cat json.txt 
{"name": "kingname", "salary": 99999, "address": "上海", "skill": ["Python", "爬虫", "Golang"]}

$ ./json2cbor json.txt > cbor.bin
$ ./cbordump -j cbor.bin 
{"name":"kingname","salary":99999,"address":"上海","skill":["Python","爬虫","Golang"]}

$ ll
-rw-r--r-- 1 wii wii 100 3月  10 09:53 json.txt
-rw-r--r-- 1 wii wii 70 3月  10 10:14 cbor.bin

CBOR对JSON数据中的字符串并没有压缩效果,特别是Key-name字符串全都保留了,只是对JSON符号和数值产生一定的压缩效果。

实际CBOR的格式更像是结构而不是JSON这种K-V形式,它只是按K-V-K-V-K-V…的顺序把队列中的值认定为是K还是V值的。 也就是你不必强迫CBOR保存K-V结构的数据,只要不调用与JSON相关的函数即可。 但不以JSON的K-V形式而以结构形式保存数据时,字段的顺序就变得重要了,会丧失扩展性。