Skip to content

Commit 5fc2f04

Browse files
committed
paho_mqtt: Add mqtt compilation files
It provides the necessary compilation files and configuration management files, and offers a publish demo and a subscribe demo Signed-off-by: zhangshuai39 <zhangshuai39@xiaomi.com>
1 parent 4d98ecc commit 5fc2f04

File tree

5 files changed

+649
-0
lines changed

5 files changed

+649
-0
lines changed

netutils/paho_mqtt/CMakeLists.txt

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
# ##############################################################################
2+
# apps/netutils/paho_mqtt/CMakeLists.txt
3+
#
4+
# Licensed to the Apache Software Foundation (ASF) under one or more contributor
5+
# license agreements. See the NOTICE file distributed with this work for
6+
# additional information regarding copyright ownership. The ASF licenses this
7+
# file to you under the Apache License, Version 2.0 (the "License"); you may not
8+
# use this file except in compliance with the License. You may obtain a copy of
9+
# the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16+
# License for the specific language governing permissions and limitations under
17+
# the License.
18+
#
19+
# ##############################################################################
20+
21+
if(CONFIG_LIB_MQTT5)
22+
23+
# ############################################################################
24+
# Config and Fetch Paho MQTT C library
25+
# ############################################################################
26+
27+
set(PAHO_MQTT_DIR ${CMAKE_CURRENT_LIST_DIR}/paho_mqtt)
28+
29+
if(NOT EXISTS ${PAHO_MQTT_DIR})
30+
# Default version if not specified in config
31+
if(DEFINED CONFIG_NETUTILS_PAHO_MQTT_VERSION)
32+
set(PAHO_MQTT_VERSION ${CONFIG_NETUTILS_PAHO_MQTT_VERSION})
33+
else()
34+
set(PAHO_MQTT_VERSION "1.3.15")
35+
endif()
36+
37+
set(PAHO_MQTT_URL "https://github.com/eclipse-paho/paho.mqtt.c/archive")
38+
FetchContent_Declare(
39+
paho_mqtt_fetch
40+
URL ${PAHO_MQTT_URL}/v${PAHO_MQTT_VERSION}.zip
41+
SOURCE_DIR ${PAHO_MQTT_DIR} BINARY_DIR
42+
${CMAKE_BINARY_DIR}/apps/netutils/paho_mqtt/paho_mqtt
43+
DOWNLOAD_NO_PROGRESS true
44+
TIMEOUT 30)
45+
46+
FetchContent_GetProperties(paho_mqtt_fetch)
47+
48+
if(NOT paho_mqtt_fetch_POPULATED)
49+
FetchContent_Populate(paho_mqtt_fetch)
50+
51+
# GitHub ZIP files extract with a versioned top-level directory
52+
# Move contents from paho.mqtt.c-<version> to paho_mqtt if needed
53+
file(GLOB extracted_dirs "${PAHO_MQTT_DIR}/paho.mqtt.c-*")
54+
if(extracted_dirs)
55+
list(GET extracted_dirs 0 extracted_dir)
56+
# Move all contents from the versioned directory to paho_mqtt
57+
file(GLOB extracted_contents "${extracted_dir}/*")
58+
foreach(item ${extracted_contents})
59+
get_filename_component(item_name ${item} NAME)
60+
execute_process(
61+
COMMAND ${CMAKE_COMMAND} -E rename ${item} ${PAHO_MQTT_DIR}/${item_name}
62+
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR})
63+
endforeach()
64+
# Remove the empty versioned directory
65+
execute_process(
66+
COMMAND ${CMAKE_COMMAND} -E remove_directory ${extracted_dir}
67+
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR})
68+
message("Moved contents from versioned directory to paho_mqtt")
69+
endif()
70+
71+
# Remove downloaded zip file if exists in current directory
72+
file(GLOB zip_files "${CMAKE_CURRENT_LIST_DIR}/v${PAHO_MQTT_VERSION}.zip")
73+
if(zip_files)
74+
file(REMOVE ${zip_files})
75+
message("Removed downloaded zip file")
76+
endif()
77+
endif()
78+
79+
# Apply paho_mqtt_01 patch if exists
80+
if(EXISTS ${CMAKE_CURRENT_LIST_DIR}/paho_mqtt_01.patch)
81+
execute_process(
82+
COMMAND sh -c "patch -p1 --forward --ignore-whitespace < ${CMAKE_CURRENT_LIST_DIR}/paho_mqtt_01.patch || true"
83+
WORKING_DIRECTORY ${PAHO_MQTT_DIR})
84+
message("paho_mqtt_01 patching done")
85+
endif()
86+
87+
message("paho_mqtt download done")
88+
endif()
89+
90+
configure_file(paho_mqtt/src/VersionInfo.h.in
91+
${CMAKE_CURRENT_BINARY_DIR}/VersionInfo.h @ONLY)
92+
93+
set(MQTT5_INCDIR ${CMAKE_CURRENT_LIST_DIR}/paho_mqtt/src)
94+
list(APPEND MQTT5_INCDIR ${CMAKE_CURRENT_BINARY_DIR})
95+
96+
file(GLOB CSRCS paho_mqtt/src/*.c)
97+
98+
if(CONFIG_UTILS_MQTT5)
99+
list(APPEND CSRCS
100+
${CMAKE_CURRENT_LIST_DIR}/paho_mqtt/src/samples/pubsub_opts.c)
101+
endif()
102+
103+
if(CONFIG_OPENSSL_MBEDTLS_WRAPPER)
104+
list(REMOVE_ITEM CSRCS ${CMAKE_CURRENT_LIST_DIR}/paho_mqtt/src/SHA1.c)
105+
endif()
106+
107+
list(REMOVE_ITEM CSRCS ${CMAKE_CURRENT_LIST_DIR}/paho_mqtt/src/MQTTClient.c
108+
${CMAKE_CURRENT_LIST_DIR}/paho_mqtt/src/MQTTVersion.c)
109+
110+
nuttx_add_library(mqtt5)
111+
112+
target_sources(mqtt5 PRIVATE ${CSRCS})
113+
114+
target_include_directories(mqtt5 PRIVATE ${MQTT5_INCDIR})
115+
116+
target_compile_options(mqtt5 PRIVATE ${MQTT5_FLAGS})
117+
118+
if(CONFIG_UTILS_MQTT5)
119+
120+
set(MQTT_PUB_FLAGS
121+
-DmessageArrived=mqtt_pub_messageArrived
122+
-DonDisconnect=mqtt_pub_onDisconnect
123+
-DonConnectFailure5=mqtt_pub_onConnectFailure5
124+
-DonConnectFailure=mqtt_pub_onConnectFailure
125+
-DonConnect5=mqtt_pub_onConnect5
126+
-DonConnect=mqtt_pub_onConnect
127+
-Dmysleep=mqtt_pub_mysleep
128+
-DtoStop=mqtt_pub_toStop
129+
-Dopts=mqtt_pub_opts
130+
-Dmyconnect=mqtt_pub_myconnect
131+
-Dcfinish=mqtt_pub_cfinish
132+
-Dtrace_callback=mqtt_pub_trace_callback)
133+
134+
nuttx_add_application(
135+
NAME
136+
mqtt_pub
137+
SRCS
138+
paho_mqtt/src/samples/paho_c_pub.c
139+
DEPENDS
140+
mqtt5
141+
INCLUDE_DIRECTORIES
142+
${MQTT5_INCDIR}
143+
COMPILE_FLAGS
144+
${MQTT_PUB_FLAGS}
145+
STACKSIZE
146+
${CONFIG_UTILS_MQTT5_STACKSIZE}
147+
PRIORITY
148+
${CONFIG_UTILS_MQTT5_PRIORITY})
149+
150+
nuttx_add_application(
151+
NAME
152+
mqtt_sub
153+
SRCS
154+
paho_mqtt/src/samples/paho_c_sub.c
155+
DEPENDS
156+
mqtt5
157+
INCLUDE_DIRECTORIES
158+
${MQTT5_INCDIR}
159+
STACKSIZE
160+
${CONFIG_UTILS_MQTT5_STACKSIZE}
161+
PRIORITY
162+
${CONFIG_UTILS_MQTT5_PRIORITY})
163+
endif()
164+
endif()

netutils/paho_mqtt/Kconfig

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#
2+
# Copyright (C) 2020 Xiaomi Corporation
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
17+
config LIB_MQTT5
18+
bool "Enable mqtt5"
19+
default n
20+
---help---
21+
A library for accessing mqtt5 client services through C libraries calls in a simple manner.
22+
23+
config UTILS_MQTT5
24+
tristate "Enable mqtt5 tool"
25+
depends on LIB_MQTT5
26+
---help---
27+
Enable mqtt utility
28+
29+
if UTILS_MQTT5
30+
config UTILS_MQTT5_PRIORITY
31+
int "mqtt utility priority"
32+
default 100
33+
34+
config UTILS_MQTT5_STACKSIZE
35+
int "mqtt utility statcksize"
36+
default 16384
37+
endif

netutils/paho_mqtt/Make.defs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
############################################################################
2+
# apps/netutils/paho_mqtt/Make.defs
3+
#
4+
# Licensed to the Apache Software Foundation (ASF) under one or more
5+
# contributor license agreements. See the NOTICE file distributed with
6+
# this work for additional information regarding copyright ownership. The
7+
# ASF licenses this file to you under the Apache License, Version 2.0 (the
8+
# "License"); you may not use this file except in compliance with the
9+
# License. You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16+
# License for the specific language governing permissions and limitations
17+
# under the License.
18+
#
19+
############################################################################
20+
21+
ifneq ($(CONFIG_LIB_MQTT5),)
22+
CONFIGURED_APPS += $(APPDIR)/netutils/paho_mqtt
23+
endif

netutils/paho_mqtt/Makefile

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
############################################################################
2+
# apps/netutils/paho_mqtt/Makefile
3+
#
4+
# Licensed to the Apache Software Foundation (ASF) under one or more
5+
# contributor license agreements. See the NOTICE file distributed with
6+
# this work for additional information regarding copyright ownership. The
7+
# ASF licenses this file to you under the Apache License, Version 2.0 (the
8+
# "License"); you may not use this file except in compliance with the
9+
# License. You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15+
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16+
# License for the specific language governing permissions and limitations
17+
# under the License.
18+
#
19+
############################################################################
20+
21+
include $(APPDIR)/Make.defs
22+
23+
PAHO_MQTT_URL ?= "https://github.com/eclipse-paho/paho.mqtt.c/archive"
24+
25+
# Default version if not specified in config
26+
ifdef CONFIG_NETUTILS_PAHO_MQTT_VERSION
27+
PAHO_MQTT_VERSION := $(patsubst "%",%,$(CONFIG_NETUTILS_PAHO_MQTT_VERSION))
28+
else
29+
PAHO_MQTT_VERSION := 1.3.15
30+
endif
31+
32+
PAHO_MQTT_ZIP = v$(PAHO_MQTT_VERSION).zip
33+
PAHO_MQTT_UNPACK = paho_mqtt
34+
35+
SRCDIR = $(APPDIR)/netutils/paho_mqtt/$(PAHO_MQTT_UNPACK)/src
36+
37+
ifeq ($(CONFIG_LIB_MQTT5), y)
38+
39+
# Check if paho_mqtt directory exists, if not download and extract
40+
ifeq ($(wildcard $(PAHO_MQTT_UNPACK)/src),)
41+
$(PAHO_MQTT_ZIP):
42+
$(Q) echo "Downloading paho.mqtt.c-$(PAHO_MQTT_VERSION)"
43+
$(Q) curl -L -o $(PAHO_MQTT_ZIP) $(PAHO_MQTT_URL)/$(PAHO_MQTT_ZIP)
44+
45+
$(PAHO_MQTT_UNPACK): $(PAHO_MQTT_ZIP)
46+
$(Q) echo "Unpacking: $(PAHO_MQTT_ZIP) -> $(PAHO_MQTT_UNPACK)"
47+
$(Q) unzip -q $(PAHO_MQTT_ZIP)
48+
$(Q) mv paho.mqtt.c-$(PAHO_MQTT_VERSION) $(PAHO_MQTT_UNPACK)
49+
$(Q) rm -f $(PAHO_MQTT_ZIP)
50+
$(Q) if [ -f paho_mqtt_01.patch ]; then \
51+
echo "Applying paho_mqtt_01 patch to $(PAHO_MQTT_UNPACK)"; \
52+
cd $(PAHO_MQTT_UNPACK) && patch -p1 --forward --ignore-whitespace < ../paho_mqtt_01.patch || true; \
53+
fi
54+
$(Q) touch $(PAHO_MQTT_UNPACK)
55+
56+
context:: $(PAHO_MQTT_UNPACK)
57+
58+
distclean::
59+
$(call DELFILE, $(PAHO_MQTT_ZIP))
60+
$(call DELDIR, $(PAHO_MQTT_UNPACK))
61+
endif
62+
63+
ifeq ($(CONFIG_OPENSSL_MBEDTLS_WRAPPER), y)
64+
SKIP = $(SRCDIR)/SHA1.c
65+
endif
66+
67+
SKIP += $(SRCDIR)/MQTTClient.c
68+
SKIP += $(SRCDIR)/MQTTVersion.c
69+
70+
CSRCS = $(filter-out $(SKIP), $(wildcard $(SRCDIR)/*.c))
71+
72+
ifeq ($(CONFIG_UTILS_MQTT5),y)
73+
CSRCS += $(SRCDIR)/samples/pubsub_opts.c
74+
endif
75+
76+
VARS = BUILD_TIMESTAMP PROJECT_VERSION PROJECT_VERSION_MAJOR
77+
VARS += PROJECT_VERSION_MINOR PROJECT_VERSION_PATCH
78+
79+
MQTT5_VERSION = $(SRCDIR)/VersionInfo.h
80+
81+
SED_COMMANDS = $(foreach var,$(VARS),-e 's/@$(var)@/$($(var))/g')
82+
83+
$(MQTT5_VERSION): $(SRCDIR)/VersionInfo.h.in
84+
sed $(SED_COMMANDS) $< > $@
85+
86+
context:: $(MQTT5_VERSION)
87+
88+
distclean::
89+
$(call DELFILE, $(MQTT5_VERSION))
90+
91+
ifeq ($(CONFIG_UTILS_MQTT5), y)
92+
PROGNAME = mqtt_pub mqtt_sub
93+
MAINSRC = $(SRCDIR)/samples/paho_c_pub.c $(SRCDIR)/samples/paho_c_sub.c
94+
PRIORITY = $(CONFIG_UTILS_MQTT5_PRIORITY)
95+
STACKSIZE = $(CONFIG_UTILS_MQTT5_STACKSIZE)
96+
MODULE = $(CONFIG_UTILS_MQTT5)
97+
$(SRCDIR)/samples/paho_c_pub.c_CFLAGS += -DmessageArrived=mqtt_pub_messageArrived
98+
$(SRCDIR)/samples/paho_c_pub.c_CFLAGS += -DonDisconnect=mqtt_pub_onDisconnect
99+
$(SRCDIR)/samples/paho_c_pub.c_CFLAGS += -DonConnectFailure5=mqtt_pub_onConnectFailure5
100+
$(SRCDIR)/samples/paho_c_pub.c_CFLAGS += -DonConnectFailure=mqtt_pub_onConnectFailure
101+
$(SRCDIR)/samples/paho_c_pub.c_CFLAGS += -DonConnect5=mqtt_pub_onConnect5
102+
$(SRCDIR)/samples/paho_c_pub.c_CFLAGS += -DonConnect=mqtt_pub_onConnect
103+
$(SRCDIR)/samples/paho_c_pub.c_CFLAGS += -Dmysleep=mqtt_pub_mysleep -DtoStop=mqtt_pub_toStop
104+
$(SRCDIR)/samples/paho_c_pub.c_CFLAGS += -Dopts=mqtt_pub_opts -Dmyconnect=mqtt_pub_myconnect
105+
$(SRCDIR)/samples/paho_c_pub.c_CFLAGS += -Dcfinish=mqtt_pub_cfinish
106+
$(SRCDIR)/samples/paho_c_pub.c_CFLAGS += -Dtrace_callback=mqtt_pub_trace_callback
107+
endif
108+
109+
endif
110+
111+
include $(APPDIR)/Application.mk

0 commit comments

Comments
 (0)