笔者在mtk7628 平台上移植过 paho.mqtt.c-1.3.1 版本的库,移植还是比较顺利,产品实际测试时, 出现如下问题: 网络连接的broker服务器时、通讯各方面未测试出问题,可以正常使用,也比较稳定;当网线断开后, 应用程序会周期性连接服务器、当长时间未能连接到服务器时、paho.mqtt.c的库中的线程就出现问题, 会退出线程、导致应用软件无法连接服务器。 笔者在 https://github.com/eclipse/paho.mqtt.c/issues/1121 看到用类似问题,源码维护者答复是 paho.mqtt.c 1.3.9 以后版本解决此问题,所以才有这篇移植 1.3.10 版本mqtt记录。
step 1:
在 openWrt 源码路径
mkdir -p package/paho.mqtt.c
cd package/paho.mqtt.c
touch Makefile
Makefile 内容如下:
#
# Copyright (C) 2006-2020 OpenWrt.org
#
# This is free software, licensed under the GNU General Public License v2.
#
include $(TOPDIR)/rules.mk
PKG_NAME:=paho.mqtt.c
PKG_VERSION:=1.3.10
PKG_RELEASE:=1
PKG_LICENSE:=GPL-2.0
PKG_MAINTAINER:= kai <peterson@sina.cn>
PKG_SOURCE:=v$(PKG_VERSION).tar.gz
PKG_SOURCE_URL:=https:
#PKG_MD5SUM:=1b709fd42bf5f1e52ae8f07a620a2a83
PKG_FIXUP:=autoreconf
PKG_INSTALL:=1
include $(INCLUDE_DIR)/package.mk
include $(INCLUDE_DIR)/cmake.mk
CMAKE_OPTIONS = -DPAHO_BUILD_SAMPLES=TRUE -DPAHO_WITH_SSL=TRUE
TARGET_CFLAGS += $(FPIC)
define Package/libpaho.mqtt.c
SECTION:=libs
CATEGORY:=Libraries
TITLE:= paho-MQTT-client
URL:=http:
DEPENDS:=+libpthread +libopenssl
endef
define Package/libpaho.mqtt.c/description
Paho-MQTT-client
endef
define Build/InstallDev
$(INSTALL_DIR) $(1)/usr/include
$(CP) $(PKG_INSTALL_DIR)/usr/include/MQTT*.h $(1)/usr/include/
$(INSTALL_DIR) $(1)/usr/lib
$(CP) $(PKG_INSTALL_DIR)/usr/lib/libpaho-mqtt3*.so* $(1)/usr/lib/
endef
define Package/libpaho.mqtt.c/install
$(INSTALL_DIR) $(1)/usr/include
$(CP) $(PKG_INSTALL_DIR)/usr/include/MQTT*.h $(1)/usr/include/
$(INSTALL_DIR) $(1)/usr/lib
$(CP) $(PKG_INSTALL_DIR)/usr/lib/libpaho-mqtt3*.so* $(1)/usr/lib/
endef
$(eval $(call BuildPackage,libpaho.mqtt.c))
step 2:
退出 package/paho.mqtt.c 路径,到源码顶级目录,
make menuconfig 到 library 下 选择 paho.mqtt.c 勾选上,再次编译 openWrt 源码后,就把 paho.mqtt.c 库编译 到openwrt 镜像中。
step 3:
解决错误 1:: cannot find -lOpenSSL::SSL
/home/wooya/mtk7688-openwrt/openwrt-hiwooya/staging_dir/toolchain-mipsel_24kec+dsp_gcc-4.8-linaro_uClibc-0.9.33.2/lib/gcc/mipsel-openwrt-linux-uclibc/4.8.3/../../../../mipsel-openwrt-linux-uclibc/bin/ld: cannot find -lOpenSSL::SSL
/home/wooya/mtk7688-openwrt/openwrt-hiwooya/staging_dir/toolchain-mipsel_24kec+dsp_gcc-4.8-linaro_uClibc-0.9.33.2/lib/gcc/mipsel-openwrt-linux-uclibc/4.8.3/../../../../mipsel-openwrt-linux-uclibc/bin/ld: cannot find -lOpenSSL::Crypto
collect2: error: ld returned 1 exit status
解决办法,修改 build_dir/…/paho.mqtt.c-1.3.10/src/CMakeList.txt 文件,增加如下内容。 文件中已经标注修改内容和方法。
IF (PAHO_WITH_SSL)
SET(OPENSSL_ROOT_DIR "" CACHE PATH "Directory containing OpenSSL libraries and includes")
### repair 1 start ------->
#find_package(OpenSSL REQUIRED)
IF (${CMAKE_SYSTEM_NAME} STREQUAL "Darwin")
SET(OPENSSL_SEARCH_PATH "/usr/local/opt/openssl")
ENDIF (${CMAKE_SYSTEM_NAME} STREQUAL "Darwin")
IF (WIN32)
SET(OPENSSL_SEARCH_PATH "C:/OpenSSL-Win64")
SET(OPENSSL_SEARCH_LIB_PATH "${OPENSSL_SEARCH_PATH}/lib64")
IF (DEFINED ENV{PAHO_WINDOWS_BUILD_BIT})
IF ($ENV{PAHO_WINDOWS_BUILD_BIT} STREQUAL "x86")
SET(OPENSSL_SEARCH_PATH "C:/OpenSSL-Win32")
SET(OPENSSL_SEARCH_LIB_PATH "${OPENSSL_SEARCH_PATH}/lib32")
ENDIF ()
ENDIF ()
ELSE()
SET(OPENSSL_SEARCH_LIB_PATH "${OPENSSL_SEARCH_PATH}/lib64")
ENDIF ()
FIND_PATH(OPENSSL_INCLUDE_DIR openssl/ssl.h
HINTS ${OPENSSL_SEARCH_PATH}/include)
FIND_LIBRARY(OPENSSL_LIB NAMES ssl libssl
HINTS ${OPENSSL_SEARCH_PATH}/lib ${OPENSSL_SEARCH_LIB_PATH})
FIND_LIBRARY(OPENSSLCRYPTO_LIB NAMES crypto libcrypto
HINTS ${OPENSSL_SEARCH_PATH}/lib ${OPENSSL_SEARCH_LIB_PATH})
MESSAGE(STATUS "OpenSSL hints: ${OPENSSL_SEARCH_PATH}")
MESSAGE(STATUS "OpenSSL headers found at ${OPENSSL_INCLUDE_DIR}")
MESSAGE(STATUS "OpenSSL library found at ${OPENSSL_LIB}")
MESSAGE(STATUS "OpenSSL Crypto library found at ${OPENSSLCRYPTO_LIB}")
INCLUDE_DIRECTORIES(
${OPENSSL_INCLUDE_DIR}
)
### end <--------
IF (PAHO_BUILD_SHARED)
## common compilation for libpaho-mqtt3cs and libpaho-mqtt3as
## Note: SSL libraries must be recompiled due ifdefs
ADD_LIBRARY(common_ssl_obj OBJECT ${common_src})
TARGET_INCLUDE_DIRECTORIES(common_ssl_obj PUBLIC ${OPENSSL_INCLUDE_DIR})
SET_PROPERTY(TARGET common_ssl_obj PROPERTY POSITION_INDEPENDENT_CODE ON)
SET_PROPERTY(TARGET common_ssl_obj PROPERTY COMPILE_DEFINITIONS "OPENSSL=1;PAHO_MQTT_EXPORTS=1")
ADD_LIBRARY(paho-mqtt3cs SHARED $<TARGET_OBJECTS:common_ssl_obj> MQTTClient.c SSLSocket.c)
ADD_LIBRARY(paho-mqtt3as SHARED $<TARGET_OBJECTS:common_ssl_obj> MQTTAsync.c MQTTAsyncUtils.c SSLSocket.c)
SET_TARGET_PROPERTIES(
paho-mqtt3cs paho-mqtt3as PROPERTIES
VERSION ${CLIENT_VERSION}
SOVERSION ${PAHO_VERSION_MAJOR}
COMPILE_DEFINITIONS "OPENSSL=1;PAHO_MQTT_EXPORTS=1")
IF(${CMAKE_SYSTEM_NAME} STREQUAL "Darwin")
SET(MQTTCLIENT_ENTRY_POINT _MQTTClient_init)
SET(MQTTASYNC_ENTRY_POINT _MQTTAsync_init)
ELSEIF (NOT WIN32)
SET(MQTTCLIENT_ENTRY_POINT MQTTClient_init)
SET(MQTTASYNC_ENTRY_POINT MQTTAsync_init)
ENDIF()
IF (NOT WIN32)
SET_TARGET_PROPERTIES(
paho-mqtt3cs PROPERTIES
LINK_FLAGS "-Wl,-init,${MQTTCLIENT_ENTRY_POINT}")
SET_TARGET_PROPERTIES(
paho-mqtt3as PROPERTIES
LINK_FLAGS "-Wl,-init,${MQTTASYNC_ENTRY_POINT}")
ENDIF()
FOREACH(TARGET paho-mqtt3cs paho-mqtt3as)
TARGET_INCLUDE_DIRECTORIES(${TARGET}
PUBLIC
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
PRIVATE
${CMAKE_BINARY_DIR})
TARGET_LINK_LIBRARIES(${TARGET}
##### repair 2
PUBLIC
${OPENSSL_LIB} ${OPENSSLCRYPTO_LIB} ${LIBS_SYSTEM})
ENDFOREACH()
INSTALL(TARGETS paho-mqtt3cs paho-mqtt3as
EXPORT eclipse-paho-mqtt-cTargets
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
ENDIF()
IF (PAHO_BUILD_STATIC)
## common compilation for libpaho-mqtt3cs and libpaho-mqtt3as
## Note: SSL libraries must be recompiled due ifdefs
ADD_LIBRARY(common_ssl_obj_static OBJECT ${common_src})
TARGET_INCLUDE_DIRECTORIES(common_ssl_obj_static PUBLIC ${OPENSSL_INCLUDE_DIR})
SET_PROPERTY(TARGET common_ssl_obj_static PROPERTY POSITION_INDEPENDENT_CODE ON)
SET_PROPERTY(TARGET common_ssl_obj_static PROPERTY COMPILE_DEFINITIONS "OPENSSL=1;PAHO_MQTT_STATIC=1")
ADD_LIBRARY(paho-mqtt3cs-static STATIC $<TARGET_OBJECTS:common_ssl_obj_static> MQTTClient.c SSLSocket.c)
ADD_LIBRARY(paho-mqtt3as-static STATIC $<TARGET_OBJECTS:common_ssl_obj_static> MQTTAsync.c MQTTAsyncUtils.c SSLSocket.c)
SET_TARGET_PROPERTIES(
paho-mqtt3cs-static paho-mqtt3as-static PROPERTIES
VERSION ${CLIENT_VERSION}
SOVERSION ${PAHO_VERSION_MAJOR}
COMPILE_DEFINITIONS "OPENSSL=1;PAHO_MQTT_STATIC=1")
IF (NOT WIN32)
SET_TARGET_PROPERTIES(paho-mqtt3cs-static PROPERTIES OUTPUT_NAME paho-mqtt3cs)
SET_TARGET_PROPERTIES(paho-mqtt3as-static PROPERTIES OUTPUT_NAME paho-mqtt3as)
ENDIF()
IF(${CMAKE_SYSTEM_NAME} STREQUAL "Darwin")
SET(MQTTCLIENT_ENTRY_POINT _MQTTClient_init)
SET(MQTTASYNC_ENTRY_POINT _MQTTAsync_init)
ELSEIF (NOT WIN32)
SET(MQTTCLIENT_ENTRY_POINT MQTTClient_init)
SET(MQTTASYNC_ENTRY_POINT MQTTAsync_init)
ENDIF()
IF (NOT WIN32)
SET_TARGET_PROPERTIES(
paho-mqtt3cs-static PROPERTIES
LINK_FLAGS "-Wl,-init,${MQTTCLIENT_ENTRY_POINT}")
SET_TARGET_PROPERTIES(
paho-mqtt3as-static PROPERTIES
LINK_FLAGS "-Wl,-init,${MQTTASYNC_ENTRY_POINT}")
ENDIF()
IF (NOT PAHO_BUILD_SHARED)
INSTALL(TARGETS paho-mqtt3cs-static paho-mqtt3as-static
EXPORT eclipse-paho-mqtt-cTargets
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
ELSE()
INSTALL(TARGETS paho-mqtt3cs-static paho-mqtt3as-static
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
ENDIF()
FOREACH(TARGET paho-mqtt3cs-static paho-mqtt3as-static)
TARGET_INCLUDE_DIRECTORIES(${TARGET}
PUBLIC
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
PRIVATE
${CMAKE_BINARY_DIR})
TARGET_LINK_LIBRARIES(${TARGET}
##### repair 3
PUBLIC
${OPENSSL_LIB} ${OPENSSLCRYPTO_LIB} ${LIBS_SYSTEM})
ENDFOREACH()
ENDIF()
ENDIF()
解决错误 2:: install package missing librt.so.0
find /home/wooya/mtk7688-openwrt/openwrt-hiwooya/build_dir/target-mipsel_24kec+dsp_uClibc-0.9.33.2/paho.mqtt.c-1.3.9/ipkg-ramips_24kec/libpaho.mqtt.c -name 'CVS' -o -name '.svn' -o -name '.#*' -o -name '*~'| xargs -r rm -rf
Package libpaho.mqtt.c is missing dependencies for the following libraries:
librt.so.0
Makefile:55: recipe for target '/home/wooya/mtk7688-openwrt/openwrt-hiwooya/bin/ramips/packages/base/libpaho.mqtt.c_1.3.9-1_ramips_24kec.ipk' failed
解决办法,是在package/paho.mqtt.c/Makefile 文件中,增加librt 的库依赖,再次编译openwrt就未出现其他错误。
define Package/libpaho.mqtt.c
SECTION:=libs
CATEGORY:=Libraries
TITLE:= paho-MQTT-client
URL:=http:
DEPENDS:=+libpthread +libopenssl +librt
endef
step 4:
编译用户应用程序,过程如下
~/gateway_monitor/build$ export STAGING_DIR=/opt/openWrt-sdk/staging_dir/target-mipsel_24kec+dsp_uClibc-0.9.33.2
wooya@hi-wooya:~/gateway_monitor/build$ make -j4
编译结果
[ 4%] Building CXX object CMakeFiles/GateWayMonitor-V0.06.dir/src/main_thread.cpp.o
[ 8%] Building CXX object CMakeFiles/GateWayMonitor-V0.06.dir/src/lora_gateway.cpp.o
[ 12%] Building CXX object CMakeFiles/GateWayMonitor-V0.06.dir/src/sqlite3/gateway_db_manager.cpp.o
[ 16%] Building CXX object CMakeFiles/GateWayMonitor-V0.06.dir/src/gw_to_locker_proto_wrapper.cpp.o
[ 20%] Building CXX object CMakeFiles/GateWayMonitor-V0.06.dir/src/mqtt/MQTTClient_publish.cpp.o
[ 24%] Building CXX object CMakeFiles/GateWayMonitor-V0.06.dir/src/mqtt/MQTTClient_publish_async.cpp.o
[ 28%] Building CXX object CMakeFiles/GateWayMonitor-V0.06.dir/src/mqtt/MQTTClient_subscribe.cpp.o
[ 32%] Building CXX object CMakeFiles/GateWayMonitor-V0.06.dir/src/mqtt/publish_package.cpp.o
[ 36%] Building CXX object CMakeFiles/GateWayMonitor-V0.06.dir/src/mqtt/link_format.cpp.o
[ 40%] Building CXX object CMakeFiles/GateWayMonitor-V0.06.dir/src/util/crc_16.cpp.o
[ 44%] Building CXX object CMakeFiles/GateWayMonitor-V0.06.dir/src/util/crc32.cpp.o
[ 48%] Building CXX object CMakeFiles/GateWayMonitor-V0.06.dir/src/util/d3des.cpp.o
[ 52%] Building CXX object CMakeFiles/GateWayMonitor-V0.06.dir/src/util/util.cpp.o
[ 56%] Building CXX object CMakeFiles/GateWayMonitor-V0.06.dir/src/utilcpp/sha256_com.cpp.o
[ 60%] Building CXX object CMakeFiles/GateWayMonitor-V0.06.dir/src/utilcpp/rsa_com.cpp.o
[ 64%] Building CXX object CMakeFiles/GateWayMonitor-V0.06.dir/src/utilcpp/hmac_algorithm.cpp.o
[ 68%] Building CXX object CMakeFiles/GateWayMonitor-V0.06.dir/src/utilcpp/ValueChange.cpp.o
[ 72%] Building CXX object CMakeFiles/GateWayMonitor-V0.06.dir/src/uart/lora_locker.cpp.o
[ 76%] Building CXX object CMakeFiles/GateWayMonitor-V0.06.dir/src/uart/uart_base.cpp.o
[ 80%] Building CXX object CMakeFiles/GateWayMonitor-V0.06.dir/src/uart/remote_load_finger.cpp.o
[ 84%] Building CXX object CMakeFiles/GateWayMonitor-V0.06.dir/src/uart/package/uart_package.cpp.o
[ 88%] Building CXX object CMakeFiles/GateWayMonitor-V0.06.dir/src/uart/package/uart_unpackage.cpp.o
[ 92%] Building CXX object CMakeFiles/GateWayMonitor-V0.06.dir/third_party/serial/src/serial.cc.o
[ 96%] Building CXX object CMakeFiles/GateWayMonitor-V0.06.dir/third_party/serial/src/impl/unix.cc.o
[100%] Linking CXX executable GateWayMonitor-V0.06
/opt/openWrt-sdk/staging_dir/target-mipsel_24kec+dsp_uClibc-0.9.33.2/usr/lib/libcrypto.so: warning: gethostbyname is obsolescent, use getnameinfo() instead.
/opt/openWrt-sdk/staging_dir/target-mipsel_24kec+dsp_uClibc-0.9.33.2/usr/lib/libuci.so: warning: the use of `mktemp' is dangerous, better use `mkstemp'
[100%] Built target GateWayMonitor-V0.06
用户应用程序编译没有问题.
step 5:
重新烧写 openwrt 镜像、测试网络断开后、服务器连接情况,测试结果待补充。
|