openwrt移植 中国移动物联网开放平台OneNET MQTT SDK套件

准备工作:
1,注册中国移动OneNET 物联网平台账号,创建设备用于平台调试
2,准备支持openwrt的开发版
3,这里我使用Ubuntu-64 构建openwrt编译环境
4,我们使用MQTTS功能,所以需要openwrt先定制openssl相关库

移植paho-MQTT-client
OneNET的MQTT Client SDK 是基于paho.mqtt.c 开发的,所以我们需要将paho.mqtt.c移植到openwrt中,并且将paho.mqtt.c编译成静态LIB库,供其它模块使用。
在package 目录下创建 paho.mqtt.c文件夹,创建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.1
PKG_RELEASE:=1
PKG_LICENSE:=GPL-2.0
PKG_MAINTAINER:= kai <[email protected]>

PKG_SOURCE:=v$(PKG_VERSION).tar.gz
PKG_SOURCE_URL:=https://github.com/eclipse/paho.mqtt.c/archive/
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://eclipse.org/paho
  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/lib
	$(CP) $(PKG_INSTALL_DIR)/usr/lib/libpaho-mqtt3*.so* $(1)/usr/lib/	
endef

$(eval $(call BuildPackage,libpaho.mqtt.c))

移植OneNET MQTT client demo
1,在package目录下创建OneNET-MQTT目录, 创建Makefile文件

#
# Copyright (C) 2006-2018 OpenWrt.org
#
# This is free software, licensed under the GNU General Public License v2.
#

include $(TOPDIR)/rules.mk

PKG_NAME:=onenet
PKG_VERSION:=1.0.0
PKG_RELEASE:=1

PKG_LICENSE:=GPL-2.0

PKG_MAINTAINER:=kai
PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME)-$(PKG_VERSION)

include $(INCLUDE_DIR)/package.mk
include $(INCLUDE_DIR)/cmake.mk

define Package/$(PKG_NAME)
  SECTION:=net
  CATEGORY:=onenet
  TITLE:= onenet
  DEPENDS:=+libpthread +libopenssl +libpaho.mqtt.c
endef

define Package/$(PKG_NAME)/description
  OneNet MQTT client for openwrt
endef

define Build/Prepare
        mkdir -p $(PKG_BUILD_DIR)
	$(CP) ./src/* $(PKG_BUILD_DIR)
endef

define Build/Clean
	rm -rf $(PKG_BUILD_DIR)
endef

define Package/$(PKG_NAME)/install
	$(INSTALL_DIR) $(1)/bin
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/mqtts-sample $(1)/bin/mqtts-sample
endef

$(eval $(call BuildPackage,$(PKG_NAME)))

2、下载OneNET mqtts demo源码,OneNET-MQTT目录下src目录
下载地址
修改src/CMakeLists.txt 注意最后一句话

cmake_minimum_required (VERSION 2.8)

PROJECT (mqtts-sample C)

SET(EXE_NAME mqtts-sample)


INCLUDE_DIRECTORIES(
	/usr/local/include
    )

AUX_SOURCE_DIRECTORY(${CMAKE_SOURCE_DIR} SRC_LIST)
message("${SRC_LIST}")
SET(COMPILE_FLAG -g -o2 -Wdeprecated-declarations -fno-strict-aliasing -Wcast-qual -Wpointer-arith -pipe -Wall -fPIC)
foreach(_lib_dir ${LIBRARY_DIRS})
SET(COMPILE_FLAG "${COMPILE_FLAG},-rpath=${_lib_dir}")
endforeach()
add_compile_options(${COMPILE_FLAG})

# paho.c.mqtt依赖openssl 编译时打开PAHO_WITH_SSL选项
LIST(APPEND LIBS paho-mqtt3cs rt pthread crypto)
ADD_EXECUTABLE(${EXE_NAME} ${SRC_LIST})
TARGET_LINK_LIBRARIES(${EXE_NAME}
  ${LIBS}
)
#重点是加入这句话,OneNET编译出来进程名称为mqtts-sample 需要制定openwrt安装路径
INSTALL(TARGETS mqtts-sample RUNTIME DESTINATION bin)

3,menuconfig选中paho.mqtt.c和onenet 模块编译即可。

下一章节讲述如何调试

你可能感兴趣的:(openwrt)