From e6318f51769ec684a713876460d89cfb3f595c11 Mon Sep 17 00:00:00 2001 From: Michal Hanus Date: Mon, 1 Apr 2024 14:53:48 +0200 Subject: backup --- .gitignore | 6 - .vscode/settings.json | 10 - .west/config | 7 + CMakeLists.txt | 14 - Kconfig | 78 ---- README.rst | 188 -------- bootloader/mcuboot | 1 + mbedtls | 1 + modules/bsim_hw_models/nrf_hw_models | 1 + modules/crypto/mbedtls | 1 + modules/crypto/tinycrypt | 1 + modules/debug/TraceRecorder | 1 + modules/debug/mipi-sys-t | 1 + modules/debug/segger | 1 + modules/fs/fatfs | 1 + modules/fs/littlefs | 1 + modules/hal/cirrus-logic | 1 + modules/hal/cmsis | 1 + modules/hal/libmetal | 1 + modules/hal/nordic | 1 + modules/hal/st | 1 + modules/hal/wurthelektronik | 1 + modules/lib/azure-sdk-for-c | 1 + modules/lib/canopennode | 1 + modules/lib/chre | 1 + modules/lib/cjson | 1 + modules/lib/gui/lvgl | 1 + modules/lib/hostap | 1 + modules/lib/liblc3 | 1 + modules/lib/loramac-node | 1 + modules/lib/lz4 | 1 + modules/lib/matter | 1 + modules/lib/memfault-firmware-sdk | 1 + modules/lib/nanopb | 1 + modules/lib/open-amp | 1 + modules/lib/openthread | 1 + modules/lib/picolibc | 1 + modules/lib/tinycbor | 1 + modules/lib/uoscore-uedhoc | 1 + modules/lib/zcbor | 1 + modules/lib/zscilib | 1 + modules/tee/tf-m/psa-arch-tests | 1 + modules/tee/tf-m/tf-m-tests | 1 + modules/tee/tf-m/trusted-firmware-m | 1 + modules/tee/tfm-mcuboot | 1 + mqtt_simple/.gitignore | 6 + mqtt_simple/.vscode/settings.json | 10 + mqtt_simple/CMakeLists.txt | 14 + mqtt_simple/Kconfig | 78 ++++ mqtt_simple/README.rst | 188 ++++++++ mqtt_simple/nrf9160dk_nrf9160_ns.overlay | 16 + mqtt_simple/overlay-qemu-x86-tls.conf | 22 + mqtt_simple/overlay-tls.conf | 13 + mqtt_simple/prj.conf | 59 +++ mqtt_simple/prj_qemu_x86.conf | 43 ++ mqtt_simple/sample.yaml | 9 + mqtt_simple/src/certificates.h | 38 ++ mqtt_simple/src/main.c | 766 +++++++++++++++++++++++++++++++ mqtt_simple/west.yml | 13 + nrf | 1 + nrf9160dk_nrf9160_ns.overlay | 16 - nrfxlib | 1 + overlay-qemu-x86-tls.conf | 22 - overlay-tls.conf | 13 - prj.conf | 59 --- prj_qemu_x86.conf | 43 -- sample.yaml | 9 - simple_blocking | 1 + src/certificates.h | 38 -- src/main.c | 766 ------------------------------- test/cmock | 1 + tools/edtt | 1 + tools/net-tools | 1 + west.yml | 13 - zephyr | 1 + 75 files changed, 1328 insertions(+), 1275 deletions(-) delete mode 100644 .gitignore delete mode 100644 .vscode/settings.json create mode 100644 .west/config delete mode 100644 CMakeLists.txt delete mode 100644 Kconfig delete mode 100644 README.rst create mode 160000 bootloader/mcuboot create mode 160000 mbedtls create mode 160000 modules/bsim_hw_models/nrf_hw_models create mode 160000 modules/crypto/mbedtls create mode 160000 modules/crypto/tinycrypt create mode 160000 modules/debug/TraceRecorder create mode 160000 modules/debug/mipi-sys-t create mode 160000 modules/debug/segger create mode 160000 modules/fs/fatfs create mode 160000 modules/fs/littlefs create mode 160000 modules/hal/cirrus-logic create mode 160000 modules/hal/cmsis create mode 160000 modules/hal/libmetal create mode 160000 modules/hal/nordic create mode 160000 modules/hal/st create mode 160000 modules/hal/wurthelektronik create mode 160000 modules/lib/azure-sdk-for-c create mode 160000 modules/lib/canopennode create mode 160000 modules/lib/chre create mode 160000 modules/lib/cjson create mode 160000 modules/lib/gui/lvgl create mode 160000 modules/lib/hostap create mode 160000 modules/lib/liblc3 create mode 160000 modules/lib/loramac-node create mode 160000 modules/lib/lz4 create mode 160000 modules/lib/matter create mode 160000 modules/lib/memfault-firmware-sdk create mode 160000 modules/lib/nanopb create mode 160000 modules/lib/open-amp create mode 160000 modules/lib/openthread create mode 160000 modules/lib/picolibc create mode 160000 modules/lib/tinycbor create mode 160000 modules/lib/uoscore-uedhoc create mode 160000 modules/lib/zcbor create mode 160000 modules/lib/zscilib create mode 160000 modules/tee/tf-m/psa-arch-tests create mode 160000 modules/tee/tf-m/tf-m-tests create mode 160000 modules/tee/tf-m/trusted-firmware-m create mode 160000 modules/tee/tfm-mcuboot create mode 100644 mqtt_simple/.gitignore create mode 100644 mqtt_simple/.vscode/settings.json create mode 100644 mqtt_simple/CMakeLists.txt create mode 100644 mqtt_simple/Kconfig create mode 100644 mqtt_simple/README.rst create mode 100644 mqtt_simple/nrf9160dk_nrf9160_ns.overlay create mode 100644 mqtt_simple/overlay-qemu-x86-tls.conf create mode 100644 mqtt_simple/overlay-tls.conf create mode 100644 mqtt_simple/prj.conf create mode 100644 mqtt_simple/prj_qemu_x86.conf create mode 100644 mqtt_simple/sample.yaml create mode 100644 mqtt_simple/src/certificates.h create mode 100644 mqtt_simple/src/main.c create mode 100644 mqtt_simple/west.yml create mode 160000 nrf delete mode 100644 nrf9160dk_nrf9160_ns.overlay create mode 160000 nrfxlib delete mode 100644 overlay-qemu-x86-tls.conf delete mode 100644 overlay-tls.conf delete mode 100644 prj.conf delete mode 100644 prj_qemu_x86.conf delete mode 100644 sample.yaml create mode 160000 simple_blocking delete mode 100644 src/certificates.h delete mode 100644 src/main.c create mode 160000 test/cmock create mode 160000 tools/edtt create mode 160000 tools/net-tools delete mode 100644 west.yml create mode 160000 zephyr diff --git a/.gitignore b/.gitignore deleted file mode 100644 index 635a99b..0000000 --- a/.gitignore +++ /dev/null @@ -1,6 +0,0 @@ -# editors -*.swp -*~ - -# build -/build*/ diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index c9c54ff..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,10 +0,0 @@ -{ - "nrf-connect.topdir": "/home/mh/Documents/skola/bc/mqtt_simple", - "nrf-connect.toolchain.path": "${nrf-connect.toolchain:2.2.0}", - "files.associations": { - "certificates.h": "c", - "w1.h": "c", - "shell.h": "c", - "nrf9160.h": "c" - } -} \ No newline at end of file diff --git a/.west/config b/.west/config new file mode 100644 index 0000000..c567b6b --- /dev/null +++ b/.west/config @@ -0,0 +1,7 @@ +[manifest] +path = mqtt_simple +file = west.yml + +[zephyr] +base = zephyr + diff --git a/CMakeLists.txt b/CMakeLists.txt deleted file mode 100644 index 8fe58a9..0000000 --- a/CMakeLists.txt +++ /dev/null @@ -1,14 +0,0 @@ -# -# Copyright (c) 2018 Nordic Semiconductor ASA -# -# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause -# - -cmake_minimum_required(VERSION 3.20.0) - -find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) -project(mqtt-simple) - -# NORDIC SDK APP START -target_sources(app PRIVATE src/main.c) -# NORDIC SDK APP END diff --git a/Kconfig b/Kconfig deleted file mode 100644 index 8c7b93e..0000000 --- a/Kconfig +++ /dev/null @@ -1,78 +0,0 @@ -# -# Copyright (c) 2020 Nordic Semiconductor ASA -# -# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause -# - -menu "MQTT simple sample" -config MQTT_PUB_TOPIC - string "MQTT publish topic" - default "pub/topic" - -config MQTT_SUB_TOPIC - string "MQTT subscribe topic" - default "subs/topic" - -config MQTT_CLIENT_ID - string "nrf" - help - Use a custom Client ID string. If not set, the client ID will be - generated based on IMEI number (for nRF9160 based targets) or - randomly (for other platforms). - default "" - -config MQTT_BROKER_HOSTNAME - string "MQTT broker hostname" - default "mikehanus.xyz" - -config MQTT_BROKER_PORT - int "MQTT broker port" - default 1883 - -config MQTT_MESSAGE_BUFFER_SIZE - int "MQTT message buffer size" - default 128 - -config MQTT_PAYLOAD_BUFFER_SIZE - int "MQTT payload buffer size" - default 128 - -config BUTTON_EVENT_PUBLISH_MSG - string "The message to publish on a button event" - default "Hello from nRF91 MQTT Simple Sample" - -config BUTTON_EVENT_BTN_NUM - int "The button number" - default 1 - -config MQTT_RECONNECT_DELAY_S - int "Seconds to delay before attempting to reconnect to the broker" - default 60 - -config LTE_CONNECT_RETRY_DELAY_S - int "Seconds to delay before attempting to retry LTE connection" - default 120 - -config MQTT_TLS_SEC_TAG - int "TLS credentials security tag" - default 24 - -config MQTT_TLS_SESSION_CACHING - bool "Enable TLS session caching" - -config MQTT_TLS_PEER_VERIFY - int "Set peer verification level" - default 2 - help - Set to 0 for VERIFY_NONE, 1 for VERIFY_OPTIONAL, and 2 for - VERIFY_REQUIRED. - -endmenu - -menu "Zephyr Kernel" -source "Kconfig.zephyr" -endmenu - -module = MQTT_SIMPLE -module-str = MQTT Simple -source "${ZEPHYR_BASE}/subsys/logging/Kconfig.template.log_config" diff --git a/README.rst b/README.rst deleted file mode 100644 index e10c3dd..0000000 --- a/README.rst +++ /dev/null @@ -1,188 +0,0 @@ -.. _mqtt_simple_sample: - -nRF9160: Simple MQTT -#################### - -.. contents:: - :local: - :depth: 2 - -The Simple MQTT sample demonstrates how to easily connect an nRF9160 SiP to an MQTT broker and send and receive data. - -Requirements -************ - -The sample supports the following development kit: - -.. table-from-sample-yaml:: - -Additionally, the sample supports :ref:`qemu_x86`. - -.. include:: /includes/tfm.txt - -Overview -********* - -The sample connects to an MQTT broker and publishes the data it receives on the configured subscribe topic to the configured publish topic. -On a button press event or typing ``mqtt publish`` in the terminal emulator, the sample publishes the configured message to the configured publish topic. -By default, the sample can establish a secure (TLS) connection or a non-secure connection to the configured MQTT broker. -The sample disables power saving modes (PSM and eDRX) so that network events are processed as soon as possible. - -Configuration -************* - -|config| - -Configuration options -===================== - -Check and configure the following configuration options for the sample: - -.. _CONFIG_MQTT_BROKER_HOSTNAME: - -CONFIG_MQTT_BROKER_HOSTNAME - MQTT Broker host name - This configuration option defines the MQTT Broker host name. - -.. _CONFIG_MQTT_CLIENT_ID: - -CONFIG_MQTT_CLIENT_ID - MQTT Client ID - This configuration option specifies the MQTT Client ID. - -.. _CONFIG_MQTT_SUB_TOPIC: - -CONFIG_MQTT_SUB_TOPIC - MQTT Subscribe topic - This configuration option sets the MQTT Subscribe topic. - -.. _CONFIG_MQTT_PUB_TOPIC: - -CONFIG_MQTT_PUB_TOPIC - MQTT Publish topic - This configuration option sets the MQTT Publish topic. - -.. _CONFIG_MQTT_BROKER_PORT: - -CONFIG_MQTT_BROKER_PORT - MQTT Broker Port - This configuration option specifies the port number associated with the MQTT broker. - -.. _CONFIG_BUTTON_EVENT_PUBLISH_MSG: - -CONFIG_BUTTON_EVENT_PUBLISH_MSG - Button event publish message - This configuration option specifies the message text which is published on a button press. - -.. _CONFIG_BUTTON_EVENT_BTN_NUM: - -CONFIG_BUTTON_EVENT_BTN_NUM - Button number for publish - This configuration option specifies the button number which, when pressed, will publish an MQTT message. - -.. _CONFIG_MQTT_RECONNECT_DELAY_S: - -CONFIG_MQTT_RECONNECT_DELAY_S - MQTT broker reconnect delay - This configuration option specifies the delay (in seconds) before attempting to reconnect to the broker. - -.. _CONFIG_LTE_CONNECT_RETRY_DELAY_S: - -CONFIG_LTE_CONNECT_RETRY_DELAY_S - LTE connection retry delay - This configuration option specifies delay (in seconds) before attempting to retry LTE connection. - -Configuration files -===================== - -The sample provides the following predefined configuration files for the following development kits: - -* :file:`prj.conf` - For nRF9160 DK -* :file:`prj_qemu_x86.conf` - For x86 Emulation (QEMU) - -In addition, the sample provides overlay configuration files, which are used to enable additional features in the sample: - -* :file:`overlay-tls.conf` - TLS overlay configuration file for nRF9160 DK -* :file:`overlay-qemu-x86-tls.conf` - TLS overlay configuration file for x86 Emulation (QEMU) - -They are located in :file:`samples/nrf9160/mqtt_simple` folder. - - -To add a specific overlay configuration file to the build, add the ``-- -DOVERLAY_CONFIG=`` flag to your build. - -See :ref:`cmake_options` for instructions on how to add this option to your build. -For example, when building on the command line, you can build the sample with the TLS configuration for nRF9160 DK as follows: - - .. code-block:: console - - west build -b nrf9160dk_nrf9160_ns -- -DOVERLAY_CONFIG=overlay-tls.conf - -.. note:: - The CA certificate for the default MQTT broker is included in the project and automatically provisioned after boot if the sample is built with the TLS configuration. - -Building and running -******************** - -.. |sample path| replace:: :file:`samples/nrf9160/mqtt_simple` - -.. include:: /includes/build_and_run_ns.txt - -Testing -======= - -|test_sample| - -1. |connect_kit| -#. |connect_terminal| -#. Reset your nRF9160 DK. -#. Observe that the sample displays the following information in the terminal:: - - The MQTT simple sample started -#. Observe that the development kit connects to the configured MQTT broker (:ref:`CONFIG_MQTT_BROKER_HOSTNAME `) after it gets the LTE connection. - At this stage, the development kit is ready to echo the data sent to it on the configured subscribe topic (:ref:`CONFIG_MQTT_SUB_TOPIC `). -#. Use an MQTT client like `Mosquitto`_ to subscribe to and publish data to the broker. - Observe that the development kit publishes all the data that you publish to :ref:`CONFIG_MQTT_SUB_TOPIC ` on :ref:`CONFIG_MQTT_PUB_TOPIC `. - -Sample output -============= - -The following serial UART output is displayed in the terminal emulator: - -.. code-block:: console - - *** Booting Zephyr OS build v2.4.0-ncs1-rc1-6-g45f2d5cf8ea4 *** - mqtt_simple: The MQTT simple sample started - mqtt_simple: LTE Link Connecting... - mqtt_simple: LTE Link Connected! - mqtt_simple: Disabling PSM and eDRX - mqtt_simple: IPv4 Address found 137.135.83.217 - mqtt_simple: MQTT client connected - mqtt_simple: Subscribing to: my/subscribe/topic len 18 - mqtt_simple: SUBACK packet id: 1234 - mqtt_simple: Publishing: Hello from nRF91 MQTT Simple Sample - mqtt_simple: to topic: my/publish/topic len: 16 - mqtt_simple: PUBACK packet id: 51700 - - - -Troubleshooting -=============== - -Public MQTT brokers might be unstable. -If you experience problems connecting to the MQTT broker, try switching to another MQTT broker by changing the value of the :ref:`CONFIG_MQTT_BROKER_HOSTNAME ` configuration option. - -.. note:: - If the :ref:`CONFIG_MQTT_BROKER_HOSTNAME ` configuration option is changed and the overlay TLS configuration is used, the included CA certificate must be updated with the CA certificate for - the newly configurated MQTT broker. - - -Dependencies -************ - -This sample uses the following |NCS| library: - -* :ref:`lte_lc_readme` - -It uses the following `sdk-nrfxlib`_ library: - -* :ref:`nrfxlib:nrf_modem` - -It uses the following Zephyr library: - -* :ref:`MQTT ` -* :ref:`zephyr:shell_api` - -In addition, it uses the following secure firmware component: - -* :ref:`Trusted Firmware-M ` diff --git a/bootloader/mcuboot b/bootloader/mcuboot new file mode 160000 index 0000000..78fd7ff --- /dev/null +++ b/bootloader/mcuboot @@ -0,0 +1 @@ +Subproject commit 78fd7ff9f31d88b9119734965e2afdda0cb9689a diff --git a/mbedtls b/mbedtls new file mode 160000 index 0000000..89419b3 --- /dev/null +++ b/mbedtls @@ -0,0 +1 @@ +Subproject commit 89419b374d17d30b7637cfb91f699c2d8432127a diff --git a/modules/bsim_hw_models/nrf_hw_models b/modules/bsim_hw_models/nrf_hw_models new file mode 160000 index 0000000..65bc530 --- /dev/null +++ b/modules/bsim_hw_models/nrf_hw_models @@ -0,0 +1 @@ +Subproject commit 65bc5305d432c08e24a3f343006d1e7deaff4908 diff --git a/modules/crypto/mbedtls b/modules/crypto/mbedtls new file mode 160000 index 0000000..7fed49c --- /dev/null +++ b/modules/crypto/mbedtls @@ -0,0 +1 @@ +Subproject commit 7fed49c9b9f983ad6416986661ef637459723bcb diff --git a/modules/crypto/tinycrypt b/modules/crypto/tinycrypt new file mode 160000 index 0000000..3e9a49d --- /dev/null +++ b/modules/crypto/tinycrypt @@ -0,0 +1 @@ +Subproject commit 3e9a49d2672ec01435ffbf0d788db6d95ef28de0 diff --git a/modules/debug/TraceRecorder b/modules/debug/TraceRecorder new file mode 160000 index 0000000..9893bf1 --- /dev/null +++ b/modules/debug/TraceRecorder @@ -0,0 +1 @@ +Subproject commit 9893bf1cf649a2c4ee2e27293f887994f3d0da5b diff --git a/modules/debug/mipi-sys-t b/modules/debug/mipi-sys-t new file mode 160000 index 0000000..0d521d8 --- /dev/null +++ b/modules/debug/mipi-sys-t @@ -0,0 +1 @@ +Subproject commit 0d521d8055f3b2b4842f728b0365d3f0ece9c37f diff --git a/modules/debug/segger b/modules/debug/segger new file mode 160000 index 0000000..d4e568a --- /dev/null +++ b/modules/debug/segger @@ -0,0 +1 @@ +Subproject commit d4e568a920b4bd087886170a5624c167b2d0665e diff --git a/modules/fs/fatfs b/modules/fs/fatfs new file mode 160000 index 0000000..a30531a --- /dev/null +++ b/modules/fs/fatfs @@ -0,0 +1 @@ +Subproject commit a30531af3a95a9a3ea7d771ea8a578ebfed45514 diff --git a/modules/fs/littlefs b/modules/fs/littlefs new file mode 160000 index 0000000..ca583fd --- /dev/null +++ b/modules/fs/littlefs @@ -0,0 +1 @@ +Subproject commit ca583fd297ceb48bced3c2548600dc615d67af24 diff --git a/modules/hal/cirrus-logic b/modules/hal/cirrus-logic new file mode 160000 index 0000000..9f6b381 --- /dev/null +++ b/modules/hal/cirrus-logic @@ -0,0 +1 @@ +Subproject commit 9f6b3812237fbb0d4157ba3584c13f1644fcbe3a diff --git a/modules/hal/cmsis b/modules/hal/cmsis new file mode 160000 index 0000000..093de61 --- /dev/null +++ b/modules/hal/cmsis @@ -0,0 +1 @@ +Subproject commit 093de61c2a7d12dc9253daf8692f61f793a9254a diff --git a/modules/hal/libmetal b/modules/hal/libmetal new file mode 160000 index 0000000..2f586b4 --- /dev/null +++ b/modules/hal/libmetal @@ -0,0 +1 @@ +Subproject commit 2f586b4f1276fb075ee145421bdf6cbe5403aa41 diff --git a/modules/hal/nordic b/modules/hal/nordic new file mode 160000 index 0000000..fe0be3c --- /dev/null +++ b/modules/hal/nordic @@ -0,0 +1 @@ +Subproject commit fe0be3c3546e136a67264084c0e3edfd7354645a diff --git a/modules/hal/st b/modules/hal/st new file mode 160000 index 0000000..cccbc24 --- /dev/null +++ b/modules/hal/st @@ -0,0 +1 @@ +Subproject commit cccbc24c14decfd3f93959f7b14514536af973c7 diff --git a/modules/hal/wurthelektronik b/modules/hal/wurthelektronik new file mode 160000 index 0000000..24ca987 --- /dev/null +++ b/modules/hal/wurthelektronik @@ -0,0 +1 @@ +Subproject commit 24ca9873c3d608fad1fea0431836bc8f144c132e diff --git a/modules/lib/azure-sdk-for-c b/modules/lib/azure-sdk-for-c new file mode 160000 index 0000000..308c171 --- /dev/null +++ b/modules/lib/azure-sdk-for-c @@ -0,0 +1 @@ +Subproject commit 308c171cb4b5eed266649012a68406487ec81fb2 diff --git a/modules/lib/canopennode b/modules/lib/canopennode new file mode 160000 index 0000000..53d3415 --- /dev/null +++ b/modules/lib/canopennode @@ -0,0 +1 @@ +Subproject commit 53d3415c14d60f8f4bfca54bfbc5d5a667d7e724 diff --git a/modules/lib/chre b/modules/lib/chre new file mode 160000 index 0000000..ef76d34 --- /dev/null +++ b/modules/lib/chre @@ -0,0 +1 @@ +Subproject commit ef76d3456db07e4959df555047d6962279528c8d diff --git a/modules/lib/cjson b/modules/lib/cjson new file mode 160000 index 0000000..c6af068 --- /dev/null +++ b/modules/lib/cjson @@ -0,0 +1 @@ +Subproject commit c6af068b7f05207b28d68880740e4b9ec1e4b50a diff --git a/modules/lib/gui/lvgl b/modules/lib/gui/lvgl new file mode 160000 index 0000000..5ab8309 --- /dev/null +++ b/modules/lib/gui/lvgl @@ -0,0 +1 @@ +Subproject commit 5ab83099854719ed326b6ecb9b09b07b1eee4818 diff --git a/modules/lib/hostap b/modules/lib/hostap new file mode 160000 index 0000000..49fddb8 --- /dev/null +++ b/modules/lib/hostap @@ -0,0 +1 @@ +Subproject commit 49fddb8f5bb9511cf8d9e56bf680576b26fe2fc8 diff --git a/modules/lib/liblc3 b/modules/lib/liblc3 new file mode 160000 index 0000000..448f3de --- /dev/null +++ b/modules/lib/liblc3 @@ -0,0 +1 @@ +Subproject commit 448f3de31f49a838988a162ef1e23a89ddf2d2ed diff --git a/modules/lib/loramac-node b/modules/lib/loramac-node new file mode 160000 index 0000000..ce57712 --- /dev/null +++ b/modules/lib/loramac-node @@ -0,0 +1 @@ +Subproject commit ce57712f3e426bbbb13acaec97b45369f716f43a diff --git a/modules/lib/lz4 b/modules/lib/lz4 new file mode 160000 index 0000000..8e303c2 --- /dev/null +++ b/modules/lib/lz4 @@ -0,0 +1 @@ +Subproject commit 8e303c264fc21c2116dc612658003a22e933124d diff --git a/modules/lib/matter b/modules/lib/matter new file mode 160000 index 0000000..f4a75a5 --- /dev/null +++ b/modules/lib/matter @@ -0,0 +1 @@ +Subproject commit f4a75a5dcff35c70d65ae2bc10c15f946fc6f8d8 diff --git a/modules/lib/memfault-firmware-sdk b/modules/lib/memfault-firmware-sdk new file mode 160000 index 0000000..d7ed96d --- /dev/null +++ b/modules/lib/memfault-firmware-sdk @@ -0,0 +1 @@ +Subproject commit d7ed96dabb9fbdb73efd65f895950a17b496b84a diff --git a/modules/lib/nanopb b/modules/lib/nanopb new file mode 160000 index 0000000..dc4deed --- /dev/null +++ b/modules/lib/nanopb @@ -0,0 +1 @@ +Subproject commit dc4deed54fd4c7e1935e3b6387eedf21bb45dc38 diff --git a/modules/lib/open-amp b/modules/lib/open-amp new file mode 160000 index 0000000..8d53544 --- /dev/null +++ b/modules/lib/open-amp @@ -0,0 +1 @@ +Subproject commit 8d53544871e1f300c478224faca6be8384ab0d04 diff --git a/modules/lib/openthread b/modules/lib/openthread new file mode 160000 index 0000000..0759de4 --- /dev/null +++ b/modules/lib/openthread @@ -0,0 +1 @@ +Subproject commit 0759de4fc52c8935b90e7f1a79d85148b66dc56e diff --git a/modules/lib/picolibc b/modules/lib/picolibc new file mode 160000 index 0000000..04ada59 --- /dev/null +++ b/modules/lib/picolibc @@ -0,0 +1 @@ +Subproject commit 04ada5951cbaf8e7b17f8226ce31cb6837c28ba7 diff --git a/modules/lib/tinycbor b/modules/lib/tinycbor new file mode 160000 index 0000000..9e1f34b --- /dev/null +++ b/modules/lib/tinycbor @@ -0,0 +1 @@ +Subproject commit 9e1f34bc08123aaad7666d3652aaa839e8178b3b diff --git a/modules/lib/uoscore-uedhoc b/modules/lib/uoscore-uedhoc new file mode 160000 index 0000000..e892019 --- /dev/null +++ b/modules/lib/uoscore-uedhoc @@ -0,0 +1 @@ +Subproject commit e8920192b66db4f909eb9cd3f155d5245c1ae825 diff --git a/modules/lib/zcbor b/modules/lib/zcbor new file mode 160000 index 0000000..0556eda --- /dev/null +++ b/modules/lib/zcbor @@ -0,0 +1 @@ +Subproject commit 0556edac504d1f34699aa8a22fcd507c14756c9e diff --git a/modules/lib/zscilib b/modules/lib/zscilib new file mode 160000 index 0000000..ca070dd --- /dev/null +++ b/modules/lib/zscilib @@ -0,0 +1 @@ +Subproject commit ca070ddabdaf67175a2da901d0bd62e8899371c5 diff --git a/modules/tee/tf-m/psa-arch-tests b/modules/tee/tf-m/psa-arch-tests new file mode 160000 index 0000000..f4fc244 --- /dev/null +++ b/modules/tee/tf-m/psa-arch-tests @@ -0,0 +1 @@ +Subproject commit f4fc2442b8e29e2a03d9899e46e5a3ea3df8c2c9 diff --git a/modules/tee/tf-m/tf-m-tests b/modules/tee/tf-m/tf-m-tests new file mode 160000 index 0000000..bcb53bc --- /dev/null +++ b/modules/tee/tf-m/tf-m-tests @@ -0,0 +1 @@ +Subproject commit bcb53bccccdc05c713aade707e7a8ddad35c210f diff --git a/modules/tee/tf-m/trusted-firmware-m b/modules/tee/tf-m/trusted-firmware-m new file mode 160000 index 0000000..4a92971 --- /dev/null +++ b/modules/tee/tf-m/trusted-firmware-m @@ -0,0 +1 @@ +Subproject commit 4a9297127f03e71a36c14538587eb6eb1d86fed9 diff --git a/modules/tee/tfm-mcuboot b/modules/tee/tfm-mcuboot new file mode 160000 index 0000000..4aa516e --- /dev/null +++ b/modules/tee/tfm-mcuboot @@ -0,0 +1 @@ +Subproject commit 4aa516e7281fc6f9a2dce0b0efda9acc580fa254 diff --git a/mqtt_simple/.gitignore b/mqtt_simple/.gitignore new file mode 100644 index 0000000..635a99b --- /dev/null +++ b/mqtt_simple/.gitignore @@ -0,0 +1,6 @@ +# editors +*.swp +*~ + +# build +/build*/ diff --git a/mqtt_simple/.vscode/settings.json b/mqtt_simple/.vscode/settings.json new file mode 100644 index 0000000..c9c54ff --- /dev/null +++ b/mqtt_simple/.vscode/settings.json @@ -0,0 +1,10 @@ +{ + "nrf-connect.topdir": "/home/mh/Documents/skola/bc/mqtt_simple", + "nrf-connect.toolchain.path": "${nrf-connect.toolchain:2.2.0}", + "files.associations": { + "certificates.h": "c", + "w1.h": "c", + "shell.h": "c", + "nrf9160.h": "c" + } +} \ No newline at end of file diff --git a/mqtt_simple/CMakeLists.txt b/mqtt_simple/CMakeLists.txt new file mode 100644 index 0000000..8fe58a9 --- /dev/null +++ b/mqtt_simple/CMakeLists.txt @@ -0,0 +1,14 @@ +# +# Copyright (c) 2018 Nordic Semiconductor ASA +# +# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause +# + +cmake_minimum_required(VERSION 3.20.0) + +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(mqtt-simple) + +# NORDIC SDK APP START +target_sources(app PRIVATE src/main.c) +# NORDIC SDK APP END diff --git a/mqtt_simple/Kconfig b/mqtt_simple/Kconfig new file mode 100644 index 0000000..8c7b93e --- /dev/null +++ b/mqtt_simple/Kconfig @@ -0,0 +1,78 @@ +# +# Copyright (c) 2020 Nordic Semiconductor ASA +# +# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause +# + +menu "MQTT simple sample" +config MQTT_PUB_TOPIC + string "MQTT publish topic" + default "pub/topic" + +config MQTT_SUB_TOPIC + string "MQTT subscribe topic" + default "subs/topic" + +config MQTT_CLIENT_ID + string "nrf" + help + Use a custom Client ID string. If not set, the client ID will be + generated based on IMEI number (for nRF9160 based targets) or + randomly (for other platforms). + default "" + +config MQTT_BROKER_HOSTNAME + string "MQTT broker hostname" + default "mikehanus.xyz" + +config MQTT_BROKER_PORT + int "MQTT broker port" + default 1883 + +config MQTT_MESSAGE_BUFFER_SIZE + int "MQTT message buffer size" + default 128 + +config MQTT_PAYLOAD_BUFFER_SIZE + int "MQTT payload buffer size" + default 128 + +config BUTTON_EVENT_PUBLISH_MSG + string "The message to publish on a button event" + default "Hello from nRF91 MQTT Simple Sample" + +config BUTTON_EVENT_BTN_NUM + int "The button number" + default 1 + +config MQTT_RECONNECT_DELAY_S + int "Seconds to delay before attempting to reconnect to the broker" + default 60 + +config LTE_CONNECT_RETRY_DELAY_S + int "Seconds to delay before attempting to retry LTE connection" + default 120 + +config MQTT_TLS_SEC_TAG + int "TLS credentials security tag" + default 24 + +config MQTT_TLS_SESSION_CACHING + bool "Enable TLS session caching" + +config MQTT_TLS_PEER_VERIFY + int "Set peer verification level" + default 2 + help + Set to 0 for VERIFY_NONE, 1 for VERIFY_OPTIONAL, and 2 for + VERIFY_REQUIRED. + +endmenu + +menu "Zephyr Kernel" +source "Kconfig.zephyr" +endmenu + +module = MQTT_SIMPLE +module-str = MQTT Simple +source "${ZEPHYR_BASE}/subsys/logging/Kconfig.template.log_config" diff --git a/mqtt_simple/README.rst b/mqtt_simple/README.rst new file mode 100644 index 0000000..e10c3dd --- /dev/null +++ b/mqtt_simple/README.rst @@ -0,0 +1,188 @@ +.. _mqtt_simple_sample: + +nRF9160: Simple MQTT +#################### + +.. contents:: + :local: + :depth: 2 + +The Simple MQTT sample demonstrates how to easily connect an nRF9160 SiP to an MQTT broker and send and receive data. + +Requirements +************ + +The sample supports the following development kit: + +.. table-from-sample-yaml:: + +Additionally, the sample supports :ref:`qemu_x86`. + +.. include:: /includes/tfm.txt + +Overview +********* + +The sample connects to an MQTT broker and publishes the data it receives on the configured subscribe topic to the configured publish topic. +On a button press event or typing ``mqtt publish`` in the terminal emulator, the sample publishes the configured message to the configured publish topic. +By default, the sample can establish a secure (TLS) connection or a non-secure connection to the configured MQTT broker. +The sample disables power saving modes (PSM and eDRX) so that network events are processed as soon as possible. + +Configuration +************* + +|config| + +Configuration options +===================== + +Check and configure the following configuration options for the sample: + +.. _CONFIG_MQTT_BROKER_HOSTNAME: + +CONFIG_MQTT_BROKER_HOSTNAME - MQTT Broker host name + This configuration option defines the MQTT Broker host name. + +.. _CONFIG_MQTT_CLIENT_ID: + +CONFIG_MQTT_CLIENT_ID - MQTT Client ID + This configuration option specifies the MQTT Client ID. + +.. _CONFIG_MQTT_SUB_TOPIC: + +CONFIG_MQTT_SUB_TOPIC - MQTT Subscribe topic + This configuration option sets the MQTT Subscribe topic. + +.. _CONFIG_MQTT_PUB_TOPIC: + +CONFIG_MQTT_PUB_TOPIC - MQTT Publish topic + This configuration option sets the MQTT Publish topic. + +.. _CONFIG_MQTT_BROKER_PORT: + +CONFIG_MQTT_BROKER_PORT - MQTT Broker Port + This configuration option specifies the port number associated with the MQTT broker. + +.. _CONFIG_BUTTON_EVENT_PUBLISH_MSG: + +CONFIG_BUTTON_EVENT_PUBLISH_MSG - Button event publish message + This configuration option specifies the message text which is published on a button press. + +.. _CONFIG_BUTTON_EVENT_BTN_NUM: + +CONFIG_BUTTON_EVENT_BTN_NUM - Button number for publish + This configuration option specifies the button number which, when pressed, will publish an MQTT message. + +.. _CONFIG_MQTT_RECONNECT_DELAY_S: + +CONFIG_MQTT_RECONNECT_DELAY_S - MQTT broker reconnect delay + This configuration option specifies the delay (in seconds) before attempting to reconnect to the broker. + +.. _CONFIG_LTE_CONNECT_RETRY_DELAY_S: + +CONFIG_LTE_CONNECT_RETRY_DELAY_S - LTE connection retry delay + This configuration option specifies delay (in seconds) before attempting to retry LTE connection. + +Configuration files +===================== + +The sample provides the following predefined configuration files for the following development kits: + +* :file:`prj.conf` - For nRF9160 DK +* :file:`prj_qemu_x86.conf` - For x86 Emulation (QEMU) + +In addition, the sample provides overlay configuration files, which are used to enable additional features in the sample: + +* :file:`overlay-tls.conf` - TLS overlay configuration file for nRF9160 DK +* :file:`overlay-qemu-x86-tls.conf` - TLS overlay configuration file for x86 Emulation (QEMU) + +They are located in :file:`samples/nrf9160/mqtt_simple` folder. + + +To add a specific overlay configuration file to the build, add the ``-- -DOVERLAY_CONFIG=`` flag to your build. + +See :ref:`cmake_options` for instructions on how to add this option to your build. +For example, when building on the command line, you can build the sample with the TLS configuration for nRF9160 DK as follows: + + .. code-block:: console + + west build -b nrf9160dk_nrf9160_ns -- -DOVERLAY_CONFIG=overlay-tls.conf + +.. note:: + The CA certificate for the default MQTT broker is included in the project and automatically provisioned after boot if the sample is built with the TLS configuration. + +Building and running +******************** + +.. |sample path| replace:: :file:`samples/nrf9160/mqtt_simple` + +.. include:: /includes/build_and_run_ns.txt + +Testing +======= + +|test_sample| + +1. |connect_kit| +#. |connect_terminal| +#. Reset your nRF9160 DK. +#. Observe that the sample displays the following information in the terminal:: + + The MQTT simple sample started +#. Observe that the development kit connects to the configured MQTT broker (:ref:`CONFIG_MQTT_BROKER_HOSTNAME `) after it gets the LTE connection. + At this stage, the development kit is ready to echo the data sent to it on the configured subscribe topic (:ref:`CONFIG_MQTT_SUB_TOPIC `). +#. Use an MQTT client like `Mosquitto`_ to subscribe to and publish data to the broker. + Observe that the development kit publishes all the data that you publish to :ref:`CONFIG_MQTT_SUB_TOPIC ` on :ref:`CONFIG_MQTT_PUB_TOPIC `. + +Sample output +============= + +The following serial UART output is displayed in the terminal emulator: + +.. code-block:: console + + *** Booting Zephyr OS build v2.4.0-ncs1-rc1-6-g45f2d5cf8ea4 *** + mqtt_simple: The MQTT simple sample started + mqtt_simple: LTE Link Connecting... + mqtt_simple: LTE Link Connected! + mqtt_simple: Disabling PSM and eDRX + mqtt_simple: IPv4 Address found 137.135.83.217 + mqtt_simple: MQTT client connected + mqtt_simple: Subscribing to: my/subscribe/topic len 18 + mqtt_simple: SUBACK packet id: 1234 + mqtt_simple: Publishing: Hello from nRF91 MQTT Simple Sample + mqtt_simple: to topic: my/publish/topic len: 16 + mqtt_simple: PUBACK packet id: 51700 + + + +Troubleshooting +=============== + +Public MQTT brokers might be unstable. +If you experience problems connecting to the MQTT broker, try switching to another MQTT broker by changing the value of the :ref:`CONFIG_MQTT_BROKER_HOSTNAME ` configuration option. + +.. note:: + If the :ref:`CONFIG_MQTT_BROKER_HOSTNAME ` configuration option is changed and the overlay TLS configuration is used, the included CA certificate must be updated with the CA certificate for + the newly configurated MQTT broker. + + +Dependencies +************ + +This sample uses the following |NCS| library: + +* :ref:`lte_lc_readme` + +It uses the following `sdk-nrfxlib`_ library: + +* :ref:`nrfxlib:nrf_modem` + +It uses the following Zephyr library: + +* :ref:`MQTT ` +* :ref:`zephyr:shell_api` + +In addition, it uses the following secure firmware component: + +* :ref:`Trusted Firmware-M ` diff --git a/mqtt_simple/nrf9160dk_nrf9160_ns.overlay b/mqtt_simple/nrf9160dk_nrf9160_ns.overlay new file mode 100644 index 0000000..8f6d2f9 --- /dev/null +++ b/mqtt_simple/nrf9160dk_nrf9160_ns.overlay @@ -0,0 +1,16 @@ +// To get started, press Ctrl+Space to bring up the completion menu and view the available nodes. + +// You can also use the buttons in the sidebar to perform actions on nodes. +// Actions currently available include: + +// * Enabling / disabling the node +// * Adding the bus to a bus +// * Removing the node +// * Connecting ADC channels + +// For more help, browse the DeviceTree documentation at https://docs.zephyrproject.org/latest/guides/dts/index.html +// You can also visit the nRF DeviceTree extension documentation at https://nrfconnect.github.io/vscode-nrf-connect/devicetree/nrfdevicetree.html + +&adc { + status = "ok"; +}; \ No newline at end of file diff --git a/mqtt_simple/overlay-qemu-x86-tls.conf b/mqtt_simple/overlay-qemu-x86-tls.conf new file mode 100644 index 0000000..1e17556 --- /dev/null +++ b/mqtt_simple/overlay-qemu-x86-tls.conf @@ -0,0 +1,22 @@ +# +# Copyright (c) 2020 Nordic Semiconductor ASA +# +# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause +# + +# MQTT TLS +CONFIG_MQTT_LIB_TLS=y +CONFIG_MQTT_BROKER_PORT=8883 + +# MBED TLS +CONFIG_MBEDTLS=y +CONFIG_MBEDTLS_BUILTIN=y +CONFIG_MBEDTLS_ENABLE_HEAP=y +CONFIG_MBEDTLS_HEAP_SIZE=120000 +CONFIG_MBEDTLS_SSL_MAX_CONTENT_LEN=16384 +CONFIG_MBEDTLS_PEM_CERTIFICATE_FORMAT=y +CONFIG_MBEDTLS_KEY_EXCHANGE_RSA_ENABLED=y +CONFIG_MBEDTLS_MAC_SHA256_ENABLED=y + +# NET SOCKETS +CONFIG_NET_SOCKETS_SOCKOPT_TLS=y diff --git a/mqtt_simple/overlay-tls.conf b/mqtt_simple/overlay-tls.conf new file mode 100644 index 0000000..9a58e44 --- /dev/null +++ b/mqtt_simple/overlay-tls.conf @@ -0,0 +1,13 @@ +# +# Copyright (c) 2020 Nordic Semiconductor ASA +# +# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause +# + +# MQTT TLS +CONFIG_MQTT_LIB_TLS=y +CONFIG_MQTT_BROKER_PORT=8886 +CONFIG_MQTT_KEEPALIVE=30 + +# Modem key management, for provisioning certificates +CONFIG_MODEM_KEY_MGMT=y diff --git a/mqtt_simple/prj.conf b/mqtt_simple/prj.conf new file mode 100644 index 0000000..ca490a9 --- /dev/null +++ b/mqtt_simple/prj.conf @@ -0,0 +1,59 @@ +# +# Copyright (c) 2020 Nordic Semiconductor ASA +# +# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause +# + +# Networking +CONFIG_NETWORKING=y +CONFIG_NET_NATIVE=n +CONFIG_NET_SOCKETS_OFFLOAD=y +CONFIG_NET_SOCKETS=y +CONFIG_NET_SOCKETS_POSIX_NAMES=y + +# LTE link control +CONFIG_LTE_LINK_CONTROL=y +CONFIG_LTE_AUTO_INIT_AND_CONNECT=n + +# Modem library +CONFIG_NRF_MODEM_LIB=y + +# MQTT +CONFIG_MQTT_LIB=y +CONFIG_MQTT_LIB_TLS=n +CONFIG_MQTT_CLEAN_SESSION=y + +# Application +# CONFIG_MQTT_PUB_TOPIC="my/publish/topic" +# CONFIG_MQTT_SUB_TOPIC="my/subscribe/topic" +# CONFIG_MQTT_CLIENT_ID="my-client-id" +# CONFIG_MQTT_BROKER_HOSTNAME="test.mosquitto.org" +# CONFIG_MQTT_BROKER_PORT=1883 + +# Button support +CONFIG_DK_LIBRARY=y + +# Enable logging +CONFIG_LOG=y +CONFIG_MQTT_SIMPLE_LOG_LEVEL_DBG=y + +# Memory +CONFIG_MAIN_STACK_SIZE=4096 +CONFIG_HEAP_MEM_POOL_SIZE=2048 + +# NewLib C +CONFIG_NEWLIB_LIBC=y + +# Shell support +CONFIG_SHELL=y +CONFIG_HW_ID_LIBRARY=y + +CONFIG_MQTT_SUB_TOPIC="sub/topic" +#CONFIG_MODEM_ANTENNA_AT_MAGPIO="AT%XMAGPIO=1,0,0,1,1,1574,1577" +#CONFIG_MODEM_ANTENNA_AT_COEX0="AT%XCOEX0=1,1,1565,1586" +#CONFIG_NEWLIB_LIBC_MIN_REQUIRED_HEAP_SIZE=2048 +#CONFIG_LTE_NETWORK_MODE_NBIOT=y +#CONFIG_SENSOR=y +#CONFIG_W1=y +CONFIG_ADC=y +CONFIG_ADC_NRFX_SAADC=y \ No newline at end of file diff --git a/mqtt_simple/prj_qemu_x86.conf b/mqtt_simple/prj_qemu_x86.conf new file mode 100644 index 0000000..cf5863e --- /dev/null +++ b/mqtt_simple/prj_qemu_x86.conf @@ -0,0 +1,43 @@ +# +# Copyright (c) 2020 Nordic Semiconductor ASA +# +# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause +# + +# General config +CONFIG_TEST_RANDOM_GENERATOR=y +CONFIG_NEWLIB_LIBC=y +CONFIG_LOG=y + +# MQTT +CONFIG_MQTT_LIB=y +CONFIG_MQTT_LIB_TLS=n + +# Networking config +CONFIG_NETWORKING=y +CONFIG_NET_IPV4=y +CONFIG_NET_TCP=y +CONFIG_NET_SOCKETS=y +CONFIG_NET_SOCKETS_POSIX_NAMES=y + +CONFIG_DNS_RESOLVER=y +CONFIG_DNS_SERVER_IP_ADDRESSES=y +CONFIG_DNS_SERVER1="8.8.8.8" + +# Network address config +CONFIG_NET_CONFIG_SETTINGS=y +CONFIG_NET_CONFIG_NEED_IPV4=y +CONFIG_NET_CONFIG_MY_IPV4_ADDR="192.0.2.1" +CONFIG_NET_CONFIG_PEER_IPV4_ADDR="192.0.2.2" +CONFIG_NET_CONFIG_MY_IPV4_GW="192.0.2.2" + +# Application +#CONFIG_MQTT_PUB_TOPIC="/my/publish/topic" +#CONFIG_MQTT_SUB_TOPIC="/my/subscribe/topic" +#CONFIG_MQTT_CLIENT_ID="my-client-id" +#CONFIG_MQTT_BROKER_HOSTNAME="mqtt.eclipseprojects.io" +#CONFIG_MQTT_BROKER_PORT=1883 + +# Memory +CONFIG_HEAP_MEM_POOL_SIZE=16384 +CONFIG_MAIN_STACK_SIZE=16384 diff --git a/mqtt_simple/sample.yaml b/mqtt_simple/sample.yaml new file mode 100644 index 0000000..1f8a578 --- /dev/null +++ b/mqtt_simple/sample.yaml @@ -0,0 +1,9 @@ +sample: + name: mqtt simple sample +tests: + sample.nrf9160.mqtt_simple: + build_only: true + integration_platforms: + - nrf9160dk_nrf9160_ns + platform_allow: nrf9160dk_nrf9160_ns + tags: ci_build diff --git a/mqtt_simple/src/certificates.h b/mqtt_simple/src/certificates.h new file mode 100644 index 0000000..aebb1e8 --- /dev/null +++ b/mqtt_simple/src/certificates.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2020 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause + */ + +/** CA certificate for test.mosquitto.org, Let’s Encrypt R3 */ +#define CA_CERTIFICATE \ + "-----BEGIN CERTIFICATE-----\n" \ + "MIIFFjCCAv6gAwIBAgIRAJErCErPDBinU/bWLiWnX1owDQYJKoZIhvcNAQELBQAw\n" \ + "TzELMAkGA1UEBhMCVVMxKTAnBgNVBAoTIEludGVybmV0IFNlY3VyaXR5IFJlc2Vh\n" \ + "cmNoIEdyb3VwMRUwEwYDVQQDEwxJU1JHIFJvb3QgWDEwHhcNMjAwOTA0MDAwMDAw\n" \ + "WhcNMjUwOTE1MTYwMDAwWjAyMQswCQYDVQQGEwJVUzEWMBQGA1UEChMNTGV0J3Mg\n" \ + "RW5jcnlwdDELMAkGA1UEAxMCUjMwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEK\n" \ + "AoIBAQC7AhUozPaglNMPEuyNVZLD+ILxmaZ6QoinXSaqtSu5xUyxr45r+XXIo9cP\n" \ + "R5QUVTVXjJ6oojkZ9YI8QqlObvU7wy7bjcCwXPNZOOftz2nwWgsbvsCUJCWH+jdx\n" \ + "sxPnHKzhm+/b5DtFUkWWqcFTzjTIUu61ru2P3mBw4qVUq7ZtDpelQDRrK9O8Zutm\n" \ + "NHz6a4uPVymZ+DAXXbpyb/uBxa3Shlg9F8fnCbvxK/eG3MHacV3URuPMrSXBiLxg\n" \ + "Z3Vms/EY96Jc5lP/Ooi2R6X/ExjqmAl3P51T+c8B5fWmcBcUr2Ok/5mzk53cU6cG\n" \ + "/kiFHaFpriV1uxPMUgP17VGhi9sVAgMBAAGjggEIMIIBBDAOBgNVHQ8BAf8EBAMC\n" \ + "AYYwHQYDVR0lBBYwFAYIKwYBBQUHAwIGCCsGAQUFBwMBMBIGA1UdEwEB/wQIMAYB\n" \ + "Af8CAQAwHQYDVR0OBBYEFBQusxe3WFbLrlAJQOYfr52LFMLGMB8GA1UdIwQYMBaA\n" \ + "FHm0WeZ7tuXkAXOACIjIGlj26ZtuMDIGCCsGAQUFBwEBBCYwJDAiBggrBgEFBQcw\n" \ + "AoYWaHR0cDovL3gxLmkubGVuY3Iub3JnLzAnBgNVHR8EIDAeMBygGqAYhhZodHRw\n" \ + "Oi8veDEuYy5sZW5jci5vcmcvMCIGA1UdIAQbMBkwCAYGZ4EMAQIBMA0GCysGAQQB\n" \ + "gt8TAQEBMA0GCSqGSIb3DQEBCwUAA4ICAQCFyk5HPqP3hUSFvNVneLKYY611TR6W\n" \ + "PTNlclQtgaDqw+34IL9fzLdwALduO/ZelN7kIJ+m74uyA+eitRY8kc607TkC53wl\n" \ + "ikfmZW4/RvTZ8M6UK+5UzhK8jCdLuMGYL6KvzXGRSgi3yLgjewQtCPkIVz6D2QQz\n" \ + "CkcheAmCJ8MqyJu5zlzyZMjAvnnAT45tRAxekrsu94sQ4egdRCnbWSDtY7kh+BIm\n" \ + "lJNXoB1lBMEKIq4QDUOXoRgffuDghje1WrG9ML+Hbisq/yFOGwXD9RiX8F6sw6W4\n" \ + "avAuvDszue5L3sz85K+EC4Y/wFVDNvZo4TYXao6Z0f+lQKc0t8DQYzk1OXVu8rp2\n" \ + "yJMC6alLbBfODALZvYH7n7do1AZls4I9d1P4jnkDrQoxB3UqQ9hVl3LEKQ73xF1O\n" \ + "yK5GhDDX8oVfGKF5u+decIsH4YaTw7mP3GFxJSqv3+0lUFJoi5Lc5da149p90Ids\n" \ + "hCExroL1+7mryIkXPeFM5TgO9r0rvZaBFOvV2z0gp35Z0+L4WPlbuEjN/lxPFin+\n" \ + "HlUjr8gRsI3qfJOQFy/9rKIJR0Y/8Omwt/8oTWgy1mdeHmmjk7j1nYsvC9JSQ6Zv\n" \ + "MldlTTKB3zhThV1+XWYp6rjd5JW1zbVWEkLNxE7GJThEUG3szgBVGP7pSWTUTsqX\n" \ + "nLRbwHOoq7hHwg==\n" \ + "-----END CERTIFICATE-----" diff --git a/mqtt_simple/src/main.c b/mqtt_simple/src/main.c new file mode 100644 index 0000000..15af38b --- /dev/null +++ b/mqtt_simple/src/main.c @@ -0,0 +1,766 @@ +/* + * Copyright (c) 2018 Nordic Semiconductor ASA + * + * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#if defined(CONFIG_NRF_MODEM_LIB) +#include +#endif /* CONFIG_NRF_MODEM_LIB */ +#include +#include +#if defined(CONFIG_MODEM_KEY_MGMT) +#include +#endif +#include + +#include "certificates.h" + +#include +#include +#include +#include + +struct device *adc_dev; +uint8_t sensors_enabled = 0xff; // bit 0 - PIR, bit 1 - HAL, bit 2 - temperature, bit 3 - gas + +#include +#define ADC_DEVICE_NAME DT_NODELABEL(adc) +#define ADC_RESOLUTION 10 +#define ADC_GAIN ADC_GAIN_1_2 +#define ADC_REFERENCE ADC_REF_INTERNAL +#define ADC_ACQUISITION_TIME ADC_ACQ_TIME(ADC_ACQ_TIME_MICROSECONDS, 10) + +static const struct adc_channel_cfg adc_temp_channel_cfg = { + .gain = ADC_GAIN, + .reference = ADC_REFERENCE, + .acquisition_time = ADC_ACQUISITION_TIME, + .channel_id = 4, +#if defined(CONFIG_ADC_CONFIGURABLE_INPUTS) + .input_positive = 5, +#endif +}; + +static const struct adc_channel_cfg adc_gas_channel_cfg = { + .gain = ADC_GAIN, + .reference = ADC_REFERENCE, + .acquisition_time = ADC_ACQUISITION_TIME, + .channel_id = 5, +#if defined(CONFIG_ADC_CONFIGURABLE_INPUTS) + .input_positive = 6, +#endif +}; + + +static int16_t adc_sample(uint8_t idx) +{ + int ret; + int16_t temp; //mV + + if(idx > 1) return -1; + + const struct adc_sequence sequence = { + .channels = BIT(idx + 4), // choose AIN4 on AIN5 + .buffer = &temp, // only single value, make to pointer to simulate array + .buffer_size = sizeof(temp), + .resolution = ADC_RESOLUTION, + }; + + if (!adc_dev) { + return -1; + } + + ret = adc_read(adc_dev, &sequence); + + temp = (temp * 6) / 5; // simple aproximation of 0-1023 range to 0-1200 mV range + + return temp; +} + +LOG_MODULE_REGISTER(mqtt_simple, CONFIG_MQTT_SIMPLE_LOG_LEVEL); + +/* Buffers for MQTT client. */ +static uint8_t rx_buffer[CONFIG_MQTT_MESSAGE_BUFFER_SIZE]; +static uint8_t tx_buffer[CONFIG_MQTT_MESSAGE_BUFFER_SIZE]; +static uint8_t payload_buf[CONFIG_MQTT_PAYLOAD_BUFFER_SIZE]; + +/* The mqtt client struct */ +static struct mqtt_client client; + +/* MQTT Broker details. */ +static struct sockaddr_storage broker; + +/* File descriptor */ +static struct pollfd fds; + +#if defined(CONFIG_MQTT_LIB_TLS) +static int certificates_provision(void) +{ + int err = 0; + + LOG_INF("Provisioning certificates"); + +#if defined(CONFIG_NRF_MODEM_LIB) && defined(CONFIG_MODEM_KEY_MGMT) + + err = modem_key_mgmt_write(CONFIG_MQTT_TLS_SEC_TAG, + MODEM_KEY_MGMT_CRED_TYPE_CA_CHAIN, + CA_CERTIFICATE, + strlen(CA_CERTIFICATE)); + if (err) { + LOG_ERR("Failed to provision CA certificate: %d", err); + return err; + } + +#elif defined(CONFIG_BOARD_QEMU_X86) && defined(CONFIG_NET_SOCKETS_SOCKOPT_TLS) + + err = tls_credential_add(CONFIG_MQTT_TLS_SEC_TAG, + TLS_CREDENTIAL_CA_CERTIFICATE, + CA_CERTIFICATE, + sizeof(CA_CERTIFICATE)); + if (err) { + LOG_ERR("Failed to register CA certificate: %d", err); + return err; + } + +#endif + + return err; +} +#endif /* defined(CONFIG_MQTT_LIB_TLS) */ + +/**@brief Function to print strings without null-termination + */ +static void data_print(uint8_t *prefix, uint8_t *data, size_t len) +{ + char buf[len + 1]; + + memcpy(buf, data, len); + buf[len] = 0; + LOG_INF("%s%s", (char *)prefix, (char *)buf); +} + +/**@brief Function to publish data on a topic + */ +static int data_publish(struct mqtt_client *c, enum mqtt_qos qos, const char *topic, + uint8_t *data, size_t len) +{ + struct mqtt_publish_param param; + + param.message.topic.qos = qos; + param.message.topic.topic.utf8 = topic; + param.message.topic.topic.size = strlen(topic); + param.message.payload.data = data; + param.message.payload.len = len; + param.message_id = sys_rand32_get(); + param.dup_flag = 0; + param.retain_flag = 0; + + data_print("Publishing: ", data, len); + LOG_INF("to topic: %s len: %u", + CONFIG_MQTT_PUB_TOPIC, + (unsigned int)strlen(CONFIG_MQTT_PUB_TOPIC)); + + return mqtt_publish(c, ¶m); +} + +/**@brief Function to subscribe to the configured topic + */ +static int subscribe(void) +{ + struct mqtt_topic subscribe_topic = { + .topic = { + .utf8 = CONFIG_MQTT_SUB_TOPIC, + .size = strlen(CONFIG_MQTT_SUB_TOPIC) + }, + .qos = MQTT_QOS_1_AT_LEAST_ONCE + }; + + const struct mqtt_subscription_list subscription_list = { + .list = &subscribe_topic, + .list_count = 1, + .message_id = 1234 + }; + + LOG_INF("Subscribing to: %s len %u", CONFIG_MQTT_SUB_TOPIC, + (unsigned int)strlen(CONFIG_MQTT_SUB_TOPIC)); + + return mqtt_subscribe(&client, &subscription_list); +} + +/**@brief Function to read the published payload. + */ +static int publish_get_payload(struct mqtt_client *c, size_t length) +{ + int ret; + int err = 0; + + /* Return an error if the payload is larger than the payload buffer. + * Note: To allow new messages, we have to read the payload before returning. + */ + if (length > sizeof(payload_buf)) { + err = -EMSGSIZE; + } + + /* Truncate payload until it fits in the payload buffer. */ + while (length > sizeof(payload_buf)) { + ret = mqtt_read_publish_payload_blocking( + c, payload_buf, (length - sizeof(payload_buf))); + if (ret == 0) { + return -EIO; + } else if (ret < 0) { + return ret; + } + + length -= ret; + } + + ret = mqtt_readall_publish_payload(c, payload_buf, length); + if (ret) { + return ret; + } + + return err; +} + +/**@brief MQTT client event handler + */ +void mqtt_evt_handler(struct mqtt_client *const c, + const struct mqtt_evt *evt) +{ + int err; + + switch (evt->type) { + case MQTT_EVT_CONNACK: + if (evt->result != 0) { + LOG_ERR("MQTT connect failed: %d", evt->result); + break; + } + + LOG_INF("MQTT client connected"); + subscribe(); + break; + + case MQTT_EVT_DISCONNECT: + LOG_INF("MQTT client disconnected: %d", evt->result); + break; + + case MQTT_EVT_PUBLISH: { + const struct mqtt_publish_param *p = &evt->param.publish; + + LOG_INF("MQTT PUBLISH result=%d len=%d", + evt->result, p->message.payload.len); + err = publish_get_payload(c, p->message.payload.len); + + if (p->message.topic.qos == MQTT_QOS_1_AT_LEAST_ONCE) { + const struct mqtt_puback_param ack = { + .message_id = p->message_id + }; + + /* Send acknowledgment. */ + mqtt_publish_qos1_ack(&client, &ack); + } + + if (err >= 0) { + data_print("Received: ", payload_buf, + p->message.payload.len); + + // for both way communication demonstration: ox turn on led x, [any other char]x turns off led x + if(p->message.payload.len >= 2 && payload_buf[1] >= '0' && payload_buf[1] <= '3') { + dk_set_led(payload_buf[1] - 48, payload_buf[0] == 'o'); + } + + /* Echo back received data */ + data_publish(&client, MQTT_QOS_1_AT_LEAST_ONCE, "nrf/echo", + payload_buf, p->message.payload.len); + } else if (err == -EMSGSIZE) { + LOG_ERR("Received payload (%d bytes) is larger than the payload buffer " + "size (%d bytes).", + p->message.payload.len, sizeof(payload_buf)); + } else { + LOG_ERR("publish_get_payload failed: %d", err); + LOG_INF("Disconnecting MQTT client..."); + + err = mqtt_disconnect(c); + if (err) { + LOG_ERR("Could not disconnect: %d", err); + } + } + } break; + + case MQTT_EVT_PUBACK: + if (evt->result != 0) { + LOG_ERR("MQTT PUBACK error: %d", evt->result); + break; + } + + LOG_INF("PUBACK packet id: %u", evt->param.puback.message_id); + break; + + case MQTT_EVT_SUBACK: + if (evt->result != 0) { + LOG_ERR("MQTT SUBACK error: %d", evt->result); + break; + } + + LOG_INF("SUBACK packet id: %u", evt->param.suback.message_id); + break; + + case MQTT_EVT_PINGRESP: + if (evt->result != 0) { + LOG_ERR("MQTT PINGRESP error: %d", evt->result); + } + break; + + default: + LOG_INF("Unhandled MQTT event type: %d", evt->type); + break; + } +} + +/**@brief Resolves the configured hostname and + * initializes the MQTT broker structure + */ +static int broker_init(void) +{ + int err; + struct addrinfo *result; + struct addrinfo *addr; + struct addrinfo hints = { + .ai_family = AF_INET, + .ai_socktype = SOCK_STREAM + }; + + err = getaddrinfo(CONFIG_MQTT_BROKER_HOSTNAME, NULL, &hints, &result); + if (err) { + LOG_ERR("getaddrinfo failed: %d", err); + return -ECHILD; + } + + addr = result; + + /* Look for address of the broker. */ + while (addr != NULL) { + /* IPv4 Address. */ + if (addr->ai_addrlen == sizeof(struct sockaddr_in)) { + struct sockaddr_in *broker4 = + ((struct sockaddr_in *)&broker); + char ipv4_addr[NET_IPV4_ADDR_LEN]; + + broker4->sin_addr.s_addr = + ((struct sockaddr_in *)addr->ai_addr) + ->sin_addr.s_addr; + broker4->sin_family = AF_INET; + broker4->sin_port = htons(CONFIG_MQTT_BROKER_PORT); + + inet_ntop(AF_INET, &broker4->sin_addr.s_addr, + ipv4_addr, sizeof(ipv4_addr)); + LOG_INF("IPv4 Address found %s", ipv4_addr); + + break; + } else { + LOG_ERR("ai_addrlen = %u should be %u or %u", + (unsigned int)addr->ai_addrlen, + (unsigned int)sizeof(struct sockaddr_in), + (unsigned int)sizeof(struct sockaddr_in6)); + } + + addr = addr->ai_next; + } + + /* Free the address. */ + freeaddrinfo(result); + + return err; +} + +#define RANDOM_LEN 10 +#define CLIENT_ID_LEN sizeof(CONFIG_BOARD) + 1 + RANDOM_LEN + +/* Function to get the client id */ +static const uint8_t* client_id_get(void) +{ + static uint8_t client_id[MAX(sizeof(CONFIG_MQTT_CLIENT_ID), + CLIENT_ID_LEN)]; + + if (strlen(CONFIG_MQTT_CLIENT_ID) > 0) { + snprintf(client_id, sizeof(client_id), "%s", + CONFIG_MQTT_CLIENT_ID); + goto exit; + } + + char hw_id_buf[HW_ID_LEN] = {0}; + + int err = hw_id_get(hw_id_buf, ARRAY_SIZE(hw_id_buf)); + + if (!err) { + snprintf(client_id, sizeof(client_id), "nrf-%s", + hw_id_buf); + goto exit; + } + + LOG_ERR("failed to retrieve HW ID, err: %d", err); + + uint32_t id = sys_rand32_get(); + snprintf(client_id, sizeof(client_id), "%s-%010u", CONFIG_BOARD, id); + +exit: + LOG_DBG("client_id = %s", (char *)client_id); + + return client_id; +} + +/**@brief Initialize the MQTT client structure + */ +static int client_init(struct mqtt_client *client) +{ + int err; + + mqtt_client_init(client); + + err = broker_init(); + if (err) { + LOG_ERR("Failed to initialize broker connection"); + return err; + } + + /* MQTT client configuration */ + client->broker = &broker; + client->evt_cb = mqtt_evt_handler; + client->client_id.utf8 = client_id_get(); + client->client_id.size = strlen(client->client_id.utf8); + client->password = NULL; + client->user_name = NULL; + client->protocol_version = MQTT_VERSION_3_1_1; + + /* MQTT buffers configuration */ + client->rx_buf = rx_buffer; + client->rx_buf_size = sizeof(rx_buffer); + client->tx_buf = tx_buffer; + client->tx_buf_size = sizeof(tx_buffer); + + /* MQTT transport configuration */ +#if defined(CONFIG_MQTT_LIB_TLS) + struct mqtt_sec_config *tls_cfg = &(client->transport).tls.config; + static sec_tag_t sec_tag_list[] = { CONFIG_MQTT_TLS_SEC_TAG }; + + LOG_INF("TLS enabled"); + client->transport.type = MQTT_TRANSPORT_SECURE; + + tls_cfg->peer_verify = CONFIG_MQTT_TLS_PEER_VERIFY; + tls_cfg->cipher_count = 0; + tls_cfg->cipher_list = NULL; + tls_cfg->sec_tag_count = ARRAY_SIZE(sec_tag_list); + tls_cfg->sec_tag_list = sec_tag_list; + tls_cfg->hostname = CONFIG_MQTT_BROKER_HOSTNAME; + +#if defined(CONFIG_NRF_MODEM_LIB) + tls_cfg->session_cache = IS_ENABLED(CONFIG_MQTT_TLS_SESSION_CACHING) ? + TLS_SESSION_CACHE_ENABLED : + TLS_SESSION_CACHE_DISABLED; +#else + /* TLS session caching is not supported by the Zephyr network stack */ + tls_cfg->session_cache = TLS_SESSION_CACHE_DISABLED; + +#endif + +#else + client->transport.type = MQTT_TRANSPORT_NON_SECURE; +#endif + + return err; +} + +/**@brief Initialize the file descriptor structure used by poll. + */ +static int fds_init(struct mqtt_client *c) +{ + if (c->transport.type == MQTT_TRANSPORT_NON_SECURE) { + fds.fd = c->transport.tcp.sock; + } else { +#if defined(CONFIG_MQTT_LIB_TLS) + fds.fd = c->transport.tls.sock; +#else + return -ENOTSUP; +#endif + } + + fds.events = POLLIN; + + return 0; +} + +#if defined(CONFIG_DK_LIBRARY) +static void button_handler(uint32_t button_states, uint32_t has_changed) +{ + // After inicializtion pressed button and active switch make sensor disabled + if(sensors_enabled == 0xff) { + sensors_enabled = (~button_states) & 0xf; // mask only 4 real buttons + } + + if (has_changed & button_states & + BIT(CONFIG_BUTTON_EVENT_BTN_NUM - 1)) { + int ret; + + ret = data_publish(&client, + MQTT_QOS_1_AT_LEAST_ONCE, + "nrf/button", + "1 pressed", + 9); + if (ret) { + LOG_ERR("Publish failed: %d", ret); + } + } + + if ((has_changed & BIT(4)) && (sensors_enabled & BIT(0))) { + int ret; + + dk_set_led(0, button_states & BIT(4)); + + ret = data_publish(&client, + MQTT_QOS_1_AT_LEAST_ONCE, + "nrf/pir", + (button_states & BIT(4))? "1":"0", + 1); + if (ret) { + LOG_ERR("Publish failed: %d", ret); + } + } + + if (has_changed & BIT(5) && (sensors_enabled & BIT(1))) { + int ret; + + dk_set_led(1, button_states & BIT(5)); + + ret = data_publish(&client, + MQTT_QOS_1_AT_LEAST_ONCE, + "nrf/magnet", + (has_changed & BIT(5))? "1":"0", + 1); + if (ret) { + LOG_ERR("Publish failed: %d", ret); + } + } +} + +#endif + +static int shell_mqtt_publish(const struct shell *shell, size_t argc, char **argv) +{ + ARG_UNUSED(argc); + ARG_UNUSED(argv); + + int ret; + + ret = data_publish(&client, + MQTT_QOS_1_AT_LEAST_ONCE, + CONFIG_MQTT_PUB_TOPIC, + CONFIG_BUTTON_EVENT_PUBLISH_MSG, + sizeof(CONFIG_BUTTON_EVENT_PUBLISH_MSG) - 1); + if (ret) { + LOG_ERR("Publish failed: %d", ret); + } + + return ret; +} + +SHELL_STATIC_SUBCMD_SET_CREATE(mqtt_sub, + SHELL_CMD(publish, NULL, "Publish data to configured publish topic", + shell_mqtt_publish), + SHELL_SUBCMD_SET_END /* Array terminated. */ +); +SHELL_CMD_REGISTER(mqtt, &mqtt_sub, "MQTT operations", NULL); + +/**@brief Configures modem to provide LTE link. Blocks until link is + * successfully established. + */ +static int modem_configure(void) +{ +#if defined(CONFIG_LTE_LINK_CONTROL) + /* Turn off LTE power saving features for a more responsive demo. Also, + * request power saving features before network registration. Some + * networks rejects timer updates after the device has registered to the + * LTE network. + */ + LOG_INF("Disabling PSM and eDRX"); + lte_lc_psm_req(false); + lte_lc_edrx_req(false); + + if (IS_ENABLED(CONFIG_LTE_AUTO_INIT_AND_CONNECT)) { + /* Do nothing, modem is already turned on + * and connected. + */ + } else { + int err; + + LOG_INF("LTE Link Connecting..."); + err = lte_lc_init_and_connect(); + if (err) { + LOG_INF("Failed to establish LTE connection: %d", err); + return err; + } + LOG_INF("LTE Link Connected!"); + } +#endif /* defined(CONFIG_LTE_LINK_CONTROL) */ + + return 0; +} + +void main(void) +{ + int err; + uint32_t connect_attempt = 0, last_measurement = 0; + + uint16_t adcval = 0; + uint8_t buf[10]; + + LOG_INF("The MQTT simple sample started"); + +#if defined(CONFIG_MQTT_LIB_TLS) + err = certificates_provision(); + if (err != 0) { + LOG_ERR("Failed to provision certificates"); + return; + } +#endif /* defined(CONFIG_MQTT_LIB_TLS) */ + + do { + err = modem_configure(); + if (err) { + LOG_INF("Retrying in %d seconds", + CONFIG_LTE_CONNECT_RETRY_DELAY_S); + k_sleep(K_SECONDS(CONFIG_LTE_CONNECT_RETRY_DELAY_S)); + } + } while (err); + + err = client_init(&client); + if (err != 0) { + LOG_ERR("client_init: %d", err); + return; + } + +#if defined(CONFIG_DK_LIBRARY) + dk_buttons_init(button_handler); + dk_leds_init(); + + adc_dev = DEVICE_DT_GET(ADC_DEVICE_NAME); + + if (!adc_dev) { + printk("device_get_binding ADC failed\n"); + while(1); + } + err = adc_channel_setup(adc_dev, &adc_temp_channel_cfg); + if (err) { + printk("Error in adc setup: %d\n", err); + } + + err = adc_channel_setup(adc_dev, &adc_gas_channel_cfg); + if (err) { + printk("Error in adc setup: %d\n", err); + } +#endif + +do_connect: + if (connect_attempt++ > 0) { + LOG_INF("Reconnecting in %d seconds...", + CONFIG_MQTT_RECONNECT_DELAY_S); + k_sleep(K_SECONDS(CONFIG_MQTT_RECONNECT_DELAY_S)); + } + err = mqtt_connect(&client); + if (err != 0) { + LOG_ERR("mqtt_connect %d", err); + goto do_connect; + } + + err = fds_init(&client); + if (err != 0) { + LOG_ERR("fds_init: %d", err); + return; + } + + // One second blink with all led to inform abot + dk_set_leds(0b1111); + k_msleep(1000); + dk_set_leds(0b1111); + + while (1) { + err = poll(&fds, 1, mqtt_keepalive_time_left(&client)); + if (err < 0) { + LOG_ERR("poll: %d", errno); + break; + } + + err = mqtt_live(&client); + if ((err != 0) && (err != -EAGAIN)) { + LOG_ERR("ERROR: mqtt_live: %d", err); + break; + } + + if ((fds.revents & POLLIN) == POLLIN) { + err = mqtt_input(&client); + if (err != 0) { + LOG_ERR("mqtt_input: %d", err); + break; + } + } + + if ((fds.revents & POLLERR) == POLLERR) { + LOG_ERR("POLLERR"); + break; + } + + if ((fds.revents & POLLNVAL) == POLLNVAL) { + LOG_ERR("POLLNVAL"); + break; + } + + if(last_measurement < (NRF_RTC1->COUNTER) || last_measurement + 1000000 > (NRF_RTC1->COUNTER)) + { + last_measurement = (NRF_RTC1->COUNTER); + if (sensors_enabled & BIT(2)) { //enabled temperature + // Sample AIN4 - LM35 + adcval = adc_sample(0); + printk("Temp: %d mV\n", adcval); + + if(adcval > 280) { // 26 deg C + snprintf(buf, 10, "%d", adcval); + data_publish(&client, MQTT_QOS_1_AT_LEAST_ONCE, "nrf/temp", buf, strlen(buf)); + dk_set_led(2, 1); + k_msleep(1000); + } + else dk_set_led(2, 0); + } + + if (sensors_enabled & BIT(3)) { // enabled gas + // Sample AIN5 - gas sensor + adcval = adc_sample(1); + printk("Gas: %d mV %d\n", adcval, sensors_enabled); + + if(adcval > 200) { // reliable limit got by testing + snprintf(buf, 10, "%d", adcval); + data_publish(&client, MQTT_QOS_1_AT_LEAST_ONCE, "nrf/gas", buf, strlen(buf)); + dk_set_led(3, 1); + k_msleep(1000); + } + else dk_set_led(3, 0); + } + } + + } + + LOG_INF("Disconnecting MQTT client..."); + + err = mqtt_disconnect(&client); + if (err) { + LOG_ERR("Could not disconnect MQTT client: %d", err); + } + goto do_connect; +} diff --git a/mqtt_simple/west.yml b/mqtt_simple/west.yml new file mode 100644 index 0000000..7fd440b --- /dev/null +++ b/mqtt_simple/west.yml @@ -0,0 +1,13 @@ +manifest: + remotes: + - name: ncs + url-base: https://github.com/nrfconnect + projects: + - name: sdk-nrf + remote: ncs + path: nrf + revision: v2.2.0 + import: true + clone-depth: 1 + self: + path: mqtt_simple diff --git a/nrf b/nrf new file mode 160000 index 0000000..188a160 --- /dev/null +++ b/nrf @@ -0,0 +1 @@ +Subproject commit 188a16036ab968bc0f9226ba178ca461548a8c66 diff --git a/nrf9160dk_nrf9160_ns.overlay b/nrf9160dk_nrf9160_ns.overlay deleted file mode 100644 index 8f6d2f9..0000000 --- a/nrf9160dk_nrf9160_ns.overlay +++ /dev/null @@ -1,16 +0,0 @@ -// To get started, press Ctrl+Space to bring up the completion menu and view the available nodes. - -// You can also use the buttons in the sidebar to perform actions on nodes. -// Actions currently available include: - -// * Enabling / disabling the node -// * Adding the bus to a bus -// * Removing the node -// * Connecting ADC channels - -// For more help, browse the DeviceTree documentation at https://docs.zephyrproject.org/latest/guides/dts/index.html -// You can also visit the nRF DeviceTree extension documentation at https://nrfconnect.github.io/vscode-nrf-connect/devicetree/nrfdevicetree.html - -&adc { - status = "ok"; -}; \ No newline at end of file diff --git a/nrfxlib b/nrfxlib new file mode 160000 index 0000000..03d0965 --- /dev/null +++ b/nrfxlib @@ -0,0 +1 @@ +Subproject commit 03d0965d9d8a3e8a6dfe3c959aefe27f1320c3ca diff --git a/overlay-qemu-x86-tls.conf b/overlay-qemu-x86-tls.conf deleted file mode 100644 index 1e17556..0000000 --- a/overlay-qemu-x86-tls.conf +++ /dev/null @@ -1,22 +0,0 @@ -# -# Copyright (c) 2020 Nordic Semiconductor ASA -# -# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause -# - -# MQTT TLS -CONFIG_MQTT_LIB_TLS=y -CONFIG_MQTT_BROKER_PORT=8883 - -# MBED TLS -CONFIG_MBEDTLS=y -CONFIG_MBEDTLS_BUILTIN=y -CONFIG_MBEDTLS_ENABLE_HEAP=y -CONFIG_MBEDTLS_HEAP_SIZE=120000 -CONFIG_MBEDTLS_SSL_MAX_CONTENT_LEN=16384 -CONFIG_MBEDTLS_PEM_CERTIFICATE_FORMAT=y -CONFIG_MBEDTLS_KEY_EXCHANGE_RSA_ENABLED=y -CONFIG_MBEDTLS_MAC_SHA256_ENABLED=y - -# NET SOCKETS -CONFIG_NET_SOCKETS_SOCKOPT_TLS=y diff --git a/overlay-tls.conf b/overlay-tls.conf deleted file mode 100644 index 9a58e44..0000000 --- a/overlay-tls.conf +++ /dev/null @@ -1,13 +0,0 @@ -# -# Copyright (c) 2020 Nordic Semiconductor ASA -# -# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause -# - -# MQTT TLS -CONFIG_MQTT_LIB_TLS=y -CONFIG_MQTT_BROKER_PORT=8886 -CONFIG_MQTT_KEEPALIVE=30 - -# Modem key management, for provisioning certificates -CONFIG_MODEM_KEY_MGMT=y diff --git a/prj.conf b/prj.conf deleted file mode 100644 index ca490a9..0000000 --- a/prj.conf +++ /dev/null @@ -1,59 +0,0 @@ -# -# Copyright (c) 2020 Nordic Semiconductor ASA -# -# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause -# - -# Networking -CONFIG_NETWORKING=y -CONFIG_NET_NATIVE=n -CONFIG_NET_SOCKETS_OFFLOAD=y -CONFIG_NET_SOCKETS=y -CONFIG_NET_SOCKETS_POSIX_NAMES=y - -# LTE link control -CONFIG_LTE_LINK_CONTROL=y -CONFIG_LTE_AUTO_INIT_AND_CONNECT=n - -# Modem library -CONFIG_NRF_MODEM_LIB=y - -# MQTT -CONFIG_MQTT_LIB=y -CONFIG_MQTT_LIB_TLS=n -CONFIG_MQTT_CLEAN_SESSION=y - -# Application -# CONFIG_MQTT_PUB_TOPIC="my/publish/topic" -# CONFIG_MQTT_SUB_TOPIC="my/subscribe/topic" -# CONFIG_MQTT_CLIENT_ID="my-client-id" -# CONFIG_MQTT_BROKER_HOSTNAME="test.mosquitto.org" -# CONFIG_MQTT_BROKER_PORT=1883 - -# Button support -CONFIG_DK_LIBRARY=y - -# Enable logging -CONFIG_LOG=y -CONFIG_MQTT_SIMPLE_LOG_LEVEL_DBG=y - -# Memory -CONFIG_MAIN_STACK_SIZE=4096 -CONFIG_HEAP_MEM_POOL_SIZE=2048 - -# NewLib C -CONFIG_NEWLIB_LIBC=y - -# Shell support -CONFIG_SHELL=y -CONFIG_HW_ID_LIBRARY=y - -CONFIG_MQTT_SUB_TOPIC="sub/topic" -#CONFIG_MODEM_ANTENNA_AT_MAGPIO="AT%XMAGPIO=1,0,0,1,1,1574,1577" -#CONFIG_MODEM_ANTENNA_AT_COEX0="AT%XCOEX0=1,1,1565,1586" -#CONFIG_NEWLIB_LIBC_MIN_REQUIRED_HEAP_SIZE=2048 -#CONFIG_LTE_NETWORK_MODE_NBIOT=y -#CONFIG_SENSOR=y -#CONFIG_W1=y -CONFIG_ADC=y -CONFIG_ADC_NRFX_SAADC=y \ No newline at end of file diff --git a/prj_qemu_x86.conf b/prj_qemu_x86.conf deleted file mode 100644 index cf5863e..0000000 --- a/prj_qemu_x86.conf +++ /dev/null @@ -1,43 +0,0 @@ -# -# Copyright (c) 2020 Nordic Semiconductor ASA -# -# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause -# - -# General config -CONFIG_TEST_RANDOM_GENERATOR=y -CONFIG_NEWLIB_LIBC=y -CONFIG_LOG=y - -# MQTT -CONFIG_MQTT_LIB=y -CONFIG_MQTT_LIB_TLS=n - -# Networking config -CONFIG_NETWORKING=y -CONFIG_NET_IPV4=y -CONFIG_NET_TCP=y -CONFIG_NET_SOCKETS=y -CONFIG_NET_SOCKETS_POSIX_NAMES=y - -CONFIG_DNS_RESOLVER=y -CONFIG_DNS_SERVER_IP_ADDRESSES=y -CONFIG_DNS_SERVER1="8.8.8.8" - -# Network address config -CONFIG_NET_CONFIG_SETTINGS=y -CONFIG_NET_CONFIG_NEED_IPV4=y -CONFIG_NET_CONFIG_MY_IPV4_ADDR="192.0.2.1" -CONFIG_NET_CONFIG_PEER_IPV4_ADDR="192.0.2.2" -CONFIG_NET_CONFIG_MY_IPV4_GW="192.0.2.2" - -# Application -#CONFIG_MQTT_PUB_TOPIC="/my/publish/topic" -#CONFIG_MQTT_SUB_TOPIC="/my/subscribe/topic" -#CONFIG_MQTT_CLIENT_ID="my-client-id" -#CONFIG_MQTT_BROKER_HOSTNAME="mqtt.eclipseprojects.io" -#CONFIG_MQTT_BROKER_PORT=1883 - -# Memory -CONFIG_HEAP_MEM_POOL_SIZE=16384 -CONFIG_MAIN_STACK_SIZE=16384 diff --git a/sample.yaml b/sample.yaml deleted file mode 100644 index 1f8a578..0000000 --- a/sample.yaml +++ /dev/null @@ -1,9 +0,0 @@ -sample: - name: mqtt simple sample -tests: - sample.nrf9160.mqtt_simple: - build_only: true - integration_platforms: - - nrf9160dk_nrf9160_ns - platform_allow: nrf9160dk_nrf9160_ns - tags: ci_build diff --git a/simple_blocking b/simple_blocking new file mode 160000 index 0000000..f7803b1 --- /dev/null +++ b/simple_blocking @@ -0,0 +1 @@ +Subproject commit f7803b13341860adf7a9b47c18d540e469a9a0ff diff --git a/src/certificates.h b/src/certificates.h deleted file mode 100644 index aebb1e8..0000000 --- a/src/certificates.h +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright (c) 2020 Nordic Semiconductor ASA - * - * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause - */ - -/** CA certificate for test.mosquitto.org, Let’s Encrypt R3 */ -#define CA_CERTIFICATE \ - "-----BEGIN CERTIFICATE-----\n" \ - "MIIFFjCCAv6gAwIBAgIRAJErCErPDBinU/bWLiWnX1owDQYJKoZIhvcNAQELBQAw\n" \ - "TzELMAkGA1UEBhMCVVMxKTAnBgNVBAoTIEludGVybmV0IFNlY3VyaXR5IFJlc2Vh\n" \ - "cmNoIEdyb3VwMRUwEwYDVQQDEwxJU1JHIFJvb3QgWDEwHhcNMjAwOTA0MDAwMDAw\n" \ - "WhcNMjUwOTE1MTYwMDAwWjAyMQswCQYDVQQGEwJVUzEWMBQGA1UEChMNTGV0J3Mg\n" \ - "RW5jcnlwdDELMAkGA1UEAxMCUjMwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEK\n" \ - "AoIBAQC7AhUozPaglNMPEuyNVZLD+ILxmaZ6QoinXSaqtSu5xUyxr45r+XXIo9cP\n" \ - "R5QUVTVXjJ6oojkZ9YI8QqlObvU7wy7bjcCwXPNZOOftz2nwWgsbvsCUJCWH+jdx\n" \ - "sxPnHKzhm+/b5DtFUkWWqcFTzjTIUu61ru2P3mBw4qVUq7ZtDpelQDRrK9O8Zutm\n" \ - "NHz6a4uPVymZ+DAXXbpyb/uBxa3Shlg9F8fnCbvxK/eG3MHacV3URuPMrSXBiLxg\n" \ - "Z3Vms/EY96Jc5lP/Ooi2R6X/ExjqmAl3P51T+c8B5fWmcBcUr2Ok/5mzk53cU6cG\n" \ - "/kiFHaFpriV1uxPMUgP17VGhi9sVAgMBAAGjggEIMIIBBDAOBgNVHQ8BAf8EBAMC\n" \ - "AYYwHQYDVR0lBBYwFAYIKwYBBQUHAwIGCCsGAQUFBwMBMBIGA1UdEwEB/wQIMAYB\n" \ - "Af8CAQAwHQYDVR0OBBYEFBQusxe3WFbLrlAJQOYfr52LFMLGMB8GA1UdIwQYMBaA\n" \ - "FHm0WeZ7tuXkAXOACIjIGlj26ZtuMDIGCCsGAQUFBwEBBCYwJDAiBggrBgEFBQcw\n" \ - "AoYWaHR0cDovL3gxLmkubGVuY3Iub3JnLzAnBgNVHR8EIDAeMBygGqAYhhZodHRw\n" \ - "Oi8veDEuYy5sZW5jci5vcmcvMCIGA1UdIAQbMBkwCAYGZ4EMAQIBMA0GCysGAQQB\n" \ - "gt8TAQEBMA0GCSqGSIb3DQEBCwUAA4ICAQCFyk5HPqP3hUSFvNVneLKYY611TR6W\n" \ - "PTNlclQtgaDqw+34IL9fzLdwALduO/ZelN7kIJ+m74uyA+eitRY8kc607TkC53wl\n" \ - "ikfmZW4/RvTZ8M6UK+5UzhK8jCdLuMGYL6KvzXGRSgi3yLgjewQtCPkIVz6D2QQz\n" \ - "CkcheAmCJ8MqyJu5zlzyZMjAvnnAT45tRAxekrsu94sQ4egdRCnbWSDtY7kh+BIm\n" \ - "lJNXoB1lBMEKIq4QDUOXoRgffuDghje1WrG9ML+Hbisq/yFOGwXD9RiX8F6sw6W4\n" \ - "avAuvDszue5L3sz85K+EC4Y/wFVDNvZo4TYXao6Z0f+lQKc0t8DQYzk1OXVu8rp2\n" \ - "yJMC6alLbBfODALZvYH7n7do1AZls4I9d1P4jnkDrQoxB3UqQ9hVl3LEKQ73xF1O\n" \ - "yK5GhDDX8oVfGKF5u+decIsH4YaTw7mP3GFxJSqv3+0lUFJoi5Lc5da149p90Ids\n" \ - "hCExroL1+7mryIkXPeFM5TgO9r0rvZaBFOvV2z0gp35Z0+L4WPlbuEjN/lxPFin+\n" \ - "HlUjr8gRsI3qfJOQFy/9rKIJR0Y/8Omwt/8oTWgy1mdeHmmjk7j1nYsvC9JSQ6Zv\n" \ - "MldlTTKB3zhThV1+XWYp6rjd5JW1zbVWEkLNxE7GJThEUG3szgBVGP7pSWTUTsqX\n" \ - "nLRbwHOoq7hHwg==\n" \ - "-----END CERTIFICATE-----" diff --git a/src/main.c b/src/main.c deleted file mode 100644 index 15af38b..0000000 --- a/src/main.c +++ /dev/null @@ -1,766 +0,0 @@ -/* - * Copyright (c) 2018 Nordic Semiconductor ASA - * - * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#if defined(CONFIG_NRF_MODEM_LIB) -#include -#endif /* CONFIG_NRF_MODEM_LIB */ -#include -#include -#if defined(CONFIG_MODEM_KEY_MGMT) -#include -#endif -#include - -#include "certificates.h" - -#include -#include -#include -#include - -struct device *adc_dev; -uint8_t sensors_enabled = 0xff; // bit 0 - PIR, bit 1 - HAL, bit 2 - temperature, bit 3 - gas - -#include -#define ADC_DEVICE_NAME DT_NODELABEL(adc) -#define ADC_RESOLUTION 10 -#define ADC_GAIN ADC_GAIN_1_2 -#define ADC_REFERENCE ADC_REF_INTERNAL -#define ADC_ACQUISITION_TIME ADC_ACQ_TIME(ADC_ACQ_TIME_MICROSECONDS, 10) - -static const struct adc_channel_cfg adc_temp_channel_cfg = { - .gain = ADC_GAIN, - .reference = ADC_REFERENCE, - .acquisition_time = ADC_ACQUISITION_TIME, - .channel_id = 4, -#if defined(CONFIG_ADC_CONFIGURABLE_INPUTS) - .input_positive = 5, -#endif -}; - -static const struct adc_channel_cfg adc_gas_channel_cfg = { - .gain = ADC_GAIN, - .reference = ADC_REFERENCE, - .acquisition_time = ADC_ACQUISITION_TIME, - .channel_id = 5, -#if defined(CONFIG_ADC_CONFIGURABLE_INPUTS) - .input_positive = 6, -#endif -}; - - -static int16_t adc_sample(uint8_t idx) -{ - int ret; - int16_t temp; //mV - - if(idx > 1) return -1; - - const struct adc_sequence sequence = { - .channels = BIT(idx + 4), // choose AIN4 on AIN5 - .buffer = &temp, // only single value, make to pointer to simulate array - .buffer_size = sizeof(temp), - .resolution = ADC_RESOLUTION, - }; - - if (!adc_dev) { - return -1; - } - - ret = adc_read(adc_dev, &sequence); - - temp = (temp * 6) / 5; // simple aproximation of 0-1023 range to 0-1200 mV range - - return temp; -} - -LOG_MODULE_REGISTER(mqtt_simple, CONFIG_MQTT_SIMPLE_LOG_LEVEL); - -/* Buffers for MQTT client. */ -static uint8_t rx_buffer[CONFIG_MQTT_MESSAGE_BUFFER_SIZE]; -static uint8_t tx_buffer[CONFIG_MQTT_MESSAGE_BUFFER_SIZE]; -static uint8_t payload_buf[CONFIG_MQTT_PAYLOAD_BUFFER_SIZE]; - -/* The mqtt client struct */ -static struct mqtt_client client; - -/* MQTT Broker details. */ -static struct sockaddr_storage broker; - -/* File descriptor */ -static struct pollfd fds; - -#if defined(CONFIG_MQTT_LIB_TLS) -static int certificates_provision(void) -{ - int err = 0; - - LOG_INF("Provisioning certificates"); - -#if defined(CONFIG_NRF_MODEM_LIB) && defined(CONFIG_MODEM_KEY_MGMT) - - err = modem_key_mgmt_write(CONFIG_MQTT_TLS_SEC_TAG, - MODEM_KEY_MGMT_CRED_TYPE_CA_CHAIN, - CA_CERTIFICATE, - strlen(CA_CERTIFICATE)); - if (err) { - LOG_ERR("Failed to provision CA certificate: %d", err); - return err; - } - -#elif defined(CONFIG_BOARD_QEMU_X86) && defined(CONFIG_NET_SOCKETS_SOCKOPT_TLS) - - err = tls_credential_add(CONFIG_MQTT_TLS_SEC_TAG, - TLS_CREDENTIAL_CA_CERTIFICATE, - CA_CERTIFICATE, - sizeof(CA_CERTIFICATE)); - if (err) { - LOG_ERR("Failed to register CA certificate: %d", err); - return err; - } - -#endif - - return err; -} -#endif /* defined(CONFIG_MQTT_LIB_TLS) */ - -/**@brief Function to print strings without null-termination - */ -static void data_print(uint8_t *prefix, uint8_t *data, size_t len) -{ - char buf[len + 1]; - - memcpy(buf, data, len); - buf[len] = 0; - LOG_INF("%s%s", (char *)prefix, (char *)buf); -} - -/**@brief Function to publish data on a topic - */ -static int data_publish(struct mqtt_client *c, enum mqtt_qos qos, const char *topic, - uint8_t *data, size_t len) -{ - struct mqtt_publish_param param; - - param.message.topic.qos = qos; - param.message.topic.topic.utf8 = topic; - param.message.topic.topic.size = strlen(topic); - param.message.payload.data = data; - param.message.payload.len = len; - param.message_id = sys_rand32_get(); - param.dup_flag = 0; - param.retain_flag = 0; - - data_print("Publishing: ", data, len); - LOG_INF("to topic: %s len: %u", - CONFIG_MQTT_PUB_TOPIC, - (unsigned int)strlen(CONFIG_MQTT_PUB_TOPIC)); - - return mqtt_publish(c, ¶m); -} - -/**@brief Function to subscribe to the configured topic - */ -static int subscribe(void) -{ - struct mqtt_topic subscribe_topic = { - .topic = { - .utf8 = CONFIG_MQTT_SUB_TOPIC, - .size = strlen(CONFIG_MQTT_SUB_TOPIC) - }, - .qos = MQTT_QOS_1_AT_LEAST_ONCE - }; - - const struct mqtt_subscription_list subscription_list = { - .list = &subscribe_topic, - .list_count = 1, - .message_id = 1234 - }; - - LOG_INF("Subscribing to: %s len %u", CONFIG_MQTT_SUB_TOPIC, - (unsigned int)strlen(CONFIG_MQTT_SUB_TOPIC)); - - return mqtt_subscribe(&client, &subscription_list); -} - -/**@brief Function to read the published payload. - */ -static int publish_get_payload(struct mqtt_client *c, size_t length) -{ - int ret; - int err = 0; - - /* Return an error if the payload is larger than the payload buffer. - * Note: To allow new messages, we have to read the payload before returning. - */ - if (length > sizeof(payload_buf)) { - err = -EMSGSIZE; - } - - /* Truncate payload until it fits in the payload buffer. */ - while (length > sizeof(payload_buf)) { - ret = mqtt_read_publish_payload_blocking( - c, payload_buf, (length - sizeof(payload_buf))); - if (ret == 0) { - return -EIO; - } else if (ret < 0) { - return ret; - } - - length -= ret; - } - - ret = mqtt_readall_publish_payload(c, payload_buf, length); - if (ret) { - return ret; - } - - return err; -} - -/**@brief MQTT client event handler - */ -void mqtt_evt_handler(struct mqtt_client *const c, - const struct mqtt_evt *evt) -{ - int err; - - switch (evt->type) { - case MQTT_EVT_CONNACK: - if (evt->result != 0) { - LOG_ERR("MQTT connect failed: %d", evt->result); - break; - } - - LOG_INF("MQTT client connected"); - subscribe(); - break; - - case MQTT_EVT_DISCONNECT: - LOG_INF("MQTT client disconnected: %d", evt->result); - break; - - case MQTT_EVT_PUBLISH: { - const struct mqtt_publish_param *p = &evt->param.publish; - - LOG_INF("MQTT PUBLISH result=%d len=%d", - evt->result, p->message.payload.len); - err = publish_get_payload(c, p->message.payload.len); - - if (p->message.topic.qos == MQTT_QOS_1_AT_LEAST_ONCE) { - const struct mqtt_puback_param ack = { - .message_id = p->message_id - }; - - /* Send acknowledgment. */ - mqtt_publish_qos1_ack(&client, &ack); - } - - if (err >= 0) { - data_print("Received: ", payload_buf, - p->message.payload.len); - - // for both way communication demonstration: ox turn on led x, [any other char]x turns off led x - if(p->message.payload.len >= 2 && payload_buf[1] >= '0' && payload_buf[1] <= '3') { - dk_set_led(payload_buf[1] - 48, payload_buf[0] == 'o'); - } - - /* Echo back received data */ - data_publish(&client, MQTT_QOS_1_AT_LEAST_ONCE, "nrf/echo", - payload_buf, p->message.payload.len); - } else if (err == -EMSGSIZE) { - LOG_ERR("Received payload (%d bytes) is larger than the payload buffer " - "size (%d bytes).", - p->message.payload.len, sizeof(payload_buf)); - } else { - LOG_ERR("publish_get_payload failed: %d", err); - LOG_INF("Disconnecting MQTT client..."); - - err = mqtt_disconnect(c); - if (err) { - LOG_ERR("Could not disconnect: %d", err); - } - } - } break; - - case MQTT_EVT_PUBACK: - if (evt->result != 0) { - LOG_ERR("MQTT PUBACK error: %d", evt->result); - break; - } - - LOG_INF("PUBACK packet id: %u", evt->param.puback.message_id); - break; - - case MQTT_EVT_SUBACK: - if (evt->result != 0) { - LOG_ERR("MQTT SUBACK error: %d", evt->result); - break; - } - - LOG_INF("SUBACK packet id: %u", evt->param.suback.message_id); - break; - - case MQTT_EVT_PINGRESP: - if (evt->result != 0) { - LOG_ERR("MQTT PINGRESP error: %d", evt->result); - } - break; - - default: - LOG_INF("Unhandled MQTT event type: %d", evt->type); - break; - } -} - -/**@brief Resolves the configured hostname and - * initializes the MQTT broker structure - */ -static int broker_init(void) -{ - int err; - struct addrinfo *result; - struct addrinfo *addr; - struct addrinfo hints = { - .ai_family = AF_INET, - .ai_socktype = SOCK_STREAM - }; - - err = getaddrinfo(CONFIG_MQTT_BROKER_HOSTNAME, NULL, &hints, &result); - if (err) { - LOG_ERR("getaddrinfo failed: %d", err); - return -ECHILD; - } - - addr = result; - - /* Look for address of the broker. */ - while (addr != NULL) { - /* IPv4 Address. */ - if (addr->ai_addrlen == sizeof(struct sockaddr_in)) { - struct sockaddr_in *broker4 = - ((struct sockaddr_in *)&broker); - char ipv4_addr[NET_IPV4_ADDR_LEN]; - - broker4->sin_addr.s_addr = - ((struct sockaddr_in *)addr->ai_addr) - ->sin_addr.s_addr; - broker4->sin_family = AF_INET; - broker4->sin_port = htons(CONFIG_MQTT_BROKER_PORT); - - inet_ntop(AF_INET, &broker4->sin_addr.s_addr, - ipv4_addr, sizeof(ipv4_addr)); - LOG_INF("IPv4 Address found %s", ipv4_addr); - - break; - } else { - LOG_ERR("ai_addrlen = %u should be %u or %u", - (unsigned int)addr->ai_addrlen, - (unsigned int)sizeof(struct sockaddr_in), - (unsigned int)sizeof(struct sockaddr_in6)); - } - - addr = addr->ai_next; - } - - /* Free the address. */ - freeaddrinfo(result); - - return err; -} - -#define RANDOM_LEN 10 -#define CLIENT_ID_LEN sizeof(CONFIG_BOARD) + 1 + RANDOM_LEN - -/* Function to get the client id */ -static const uint8_t* client_id_get(void) -{ - static uint8_t client_id[MAX(sizeof(CONFIG_MQTT_CLIENT_ID), - CLIENT_ID_LEN)]; - - if (strlen(CONFIG_MQTT_CLIENT_ID) > 0) { - snprintf(client_id, sizeof(client_id), "%s", - CONFIG_MQTT_CLIENT_ID); - goto exit; - } - - char hw_id_buf[HW_ID_LEN] = {0}; - - int err = hw_id_get(hw_id_buf, ARRAY_SIZE(hw_id_buf)); - - if (!err) { - snprintf(client_id, sizeof(client_id), "nrf-%s", - hw_id_buf); - goto exit; - } - - LOG_ERR("failed to retrieve HW ID, err: %d", err); - - uint32_t id = sys_rand32_get(); - snprintf(client_id, sizeof(client_id), "%s-%010u", CONFIG_BOARD, id); - -exit: - LOG_DBG("client_id = %s", (char *)client_id); - - return client_id; -} - -/**@brief Initialize the MQTT client structure - */ -static int client_init(struct mqtt_client *client) -{ - int err; - - mqtt_client_init(client); - - err = broker_init(); - if (err) { - LOG_ERR("Failed to initialize broker connection"); - return err; - } - - /* MQTT client configuration */ - client->broker = &broker; - client->evt_cb = mqtt_evt_handler; - client->client_id.utf8 = client_id_get(); - client->client_id.size = strlen(client->client_id.utf8); - client->password = NULL; - client->user_name = NULL; - client->protocol_version = MQTT_VERSION_3_1_1; - - /* MQTT buffers configuration */ - client->rx_buf = rx_buffer; - client->rx_buf_size = sizeof(rx_buffer); - client->tx_buf = tx_buffer; - client->tx_buf_size = sizeof(tx_buffer); - - /* MQTT transport configuration */ -#if defined(CONFIG_MQTT_LIB_TLS) - struct mqtt_sec_config *tls_cfg = &(client->transport).tls.config; - static sec_tag_t sec_tag_list[] = { CONFIG_MQTT_TLS_SEC_TAG }; - - LOG_INF("TLS enabled"); - client->transport.type = MQTT_TRANSPORT_SECURE; - - tls_cfg->peer_verify = CONFIG_MQTT_TLS_PEER_VERIFY; - tls_cfg->cipher_count = 0; - tls_cfg->cipher_list = NULL; - tls_cfg->sec_tag_count = ARRAY_SIZE(sec_tag_list); - tls_cfg->sec_tag_list = sec_tag_list; - tls_cfg->hostname = CONFIG_MQTT_BROKER_HOSTNAME; - -#if defined(CONFIG_NRF_MODEM_LIB) - tls_cfg->session_cache = IS_ENABLED(CONFIG_MQTT_TLS_SESSION_CACHING) ? - TLS_SESSION_CACHE_ENABLED : - TLS_SESSION_CACHE_DISABLED; -#else - /* TLS session caching is not supported by the Zephyr network stack */ - tls_cfg->session_cache = TLS_SESSION_CACHE_DISABLED; - -#endif - -#else - client->transport.type = MQTT_TRANSPORT_NON_SECURE; -#endif - - return err; -} - -/**@brief Initialize the file descriptor structure used by poll. - */ -static int fds_init(struct mqtt_client *c) -{ - if (c->transport.type == MQTT_TRANSPORT_NON_SECURE) { - fds.fd = c->transport.tcp.sock; - } else { -#if defined(CONFIG_MQTT_LIB_TLS) - fds.fd = c->transport.tls.sock; -#else - return -ENOTSUP; -#endif - } - - fds.events = POLLIN; - - return 0; -} - -#if defined(CONFIG_DK_LIBRARY) -static void button_handler(uint32_t button_states, uint32_t has_changed) -{ - // After inicializtion pressed button and active switch make sensor disabled - if(sensors_enabled == 0xff) { - sensors_enabled = (~button_states) & 0xf; // mask only 4 real buttons - } - - if (has_changed & button_states & - BIT(CONFIG_BUTTON_EVENT_BTN_NUM - 1)) { - int ret; - - ret = data_publish(&client, - MQTT_QOS_1_AT_LEAST_ONCE, - "nrf/button", - "1 pressed", - 9); - if (ret) { - LOG_ERR("Publish failed: %d", ret); - } - } - - if ((has_changed & BIT(4)) && (sensors_enabled & BIT(0))) { - int ret; - - dk_set_led(0, button_states & BIT(4)); - - ret = data_publish(&client, - MQTT_QOS_1_AT_LEAST_ONCE, - "nrf/pir", - (button_states & BIT(4))? "1":"0", - 1); - if (ret) { - LOG_ERR("Publish failed: %d", ret); - } - } - - if (has_changed & BIT(5) && (sensors_enabled & BIT(1))) { - int ret; - - dk_set_led(1, button_states & BIT(5)); - - ret = data_publish(&client, - MQTT_QOS_1_AT_LEAST_ONCE, - "nrf/magnet", - (has_changed & BIT(5))? "1":"0", - 1); - if (ret) { - LOG_ERR("Publish failed: %d", ret); - } - } -} - -#endif - -static int shell_mqtt_publish(const struct shell *shell, size_t argc, char **argv) -{ - ARG_UNUSED(argc); - ARG_UNUSED(argv); - - int ret; - - ret = data_publish(&client, - MQTT_QOS_1_AT_LEAST_ONCE, - CONFIG_MQTT_PUB_TOPIC, - CONFIG_BUTTON_EVENT_PUBLISH_MSG, - sizeof(CONFIG_BUTTON_EVENT_PUBLISH_MSG) - 1); - if (ret) { - LOG_ERR("Publish failed: %d", ret); - } - - return ret; -} - -SHELL_STATIC_SUBCMD_SET_CREATE(mqtt_sub, - SHELL_CMD(publish, NULL, "Publish data to configured publish topic", - shell_mqtt_publish), - SHELL_SUBCMD_SET_END /* Array terminated. */ -); -SHELL_CMD_REGISTER(mqtt, &mqtt_sub, "MQTT operations", NULL); - -/**@brief Configures modem to provide LTE link. Blocks until link is - * successfully established. - */ -static int modem_configure(void) -{ -#if defined(CONFIG_LTE_LINK_CONTROL) - /* Turn off LTE power saving features for a more responsive demo. Also, - * request power saving features before network registration. Some - * networks rejects timer updates after the device has registered to the - * LTE network. - */ - LOG_INF("Disabling PSM and eDRX"); - lte_lc_psm_req(false); - lte_lc_edrx_req(false); - - if (IS_ENABLED(CONFIG_LTE_AUTO_INIT_AND_CONNECT)) { - /* Do nothing, modem is already turned on - * and connected. - */ - } else { - int err; - - LOG_INF("LTE Link Connecting..."); - err = lte_lc_init_and_connect(); - if (err) { - LOG_INF("Failed to establish LTE connection: %d", err); - return err; - } - LOG_INF("LTE Link Connected!"); - } -#endif /* defined(CONFIG_LTE_LINK_CONTROL) */ - - return 0; -} - -void main(void) -{ - int err; - uint32_t connect_attempt = 0, last_measurement = 0; - - uint16_t adcval = 0; - uint8_t buf[10]; - - LOG_INF("The MQTT simple sample started"); - -#if defined(CONFIG_MQTT_LIB_TLS) - err = certificates_provision(); - if (err != 0) { - LOG_ERR("Failed to provision certificates"); - return; - } -#endif /* defined(CONFIG_MQTT_LIB_TLS) */ - - do { - err = modem_configure(); - if (err) { - LOG_INF("Retrying in %d seconds", - CONFIG_LTE_CONNECT_RETRY_DELAY_S); - k_sleep(K_SECONDS(CONFIG_LTE_CONNECT_RETRY_DELAY_S)); - } - } while (err); - - err = client_init(&client); - if (err != 0) { - LOG_ERR("client_init: %d", err); - return; - } - -#if defined(CONFIG_DK_LIBRARY) - dk_buttons_init(button_handler); - dk_leds_init(); - - adc_dev = DEVICE_DT_GET(ADC_DEVICE_NAME); - - if (!adc_dev) { - printk("device_get_binding ADC failed\n"); - while(1); - } - err = adc_channel_setup(adc_dev, &adc_temp_channel_cfg); - if (err) { - printk("Error in adc setup: %d\n", err); - } - - err = adc_channel_setup(adc_dev, &adc_gas_channel_cfg); - if (err) { - printk("Error in adc setup: %d\n", err); - } -#endif - -do_connect: - if (connect_attempt++ > 0) { - LOG_INF("Reconnecting in %d seconds...", - CONFIG_MQTT_RECONNECT_DELAY_S); - k_sleep(K_SECONDS(CONFIG_MQTT_RECONNECT_DELAY_S)); - } - err = mqtt_connect(&client); - if (err != 0) { - LOG_ERR("mqtt_connect %d", err); - goto do_connect; - } - - err = fds_init(&client); - if (err != 0) { - LOG_ERR("fds_init: %d", err); - return; - } - - // One second blink with all led to inform abot - dk_set_leds(0b1111); - k_msleep(1000); - dk_set_leds(0b1111); - - while (1) { - err = poll(&fds, 1, mqtt_keepalive_time_left(&client)); - if (err < 0) { - LOG_ERR("poll: %d", errno); - break; - } - - err = mqtt_live(&client); - if ((err != 0) && (err != -EAGAIN)) { - LOG_ERR("ERROR: mqtt_live: %d", err); - break; - } - - if ((fds.revents & POLLIN) == POLLIN) { - err = mqtt_input(&client); - if (err != 0) { - LOG_ERR("mqtt_input: %d", err); - break; - } - } - - if ((fds.revents & POLLERR) == POLLERR) { - LOG_ERR("POLLERR"); - break; - } - - if ((fds.revents & POLLNVAL) == POLLNVAL) { - LOG_ERR("POLLNVAL"); - break; - } - - if(last_measurement < (NRF_RTC1->COUNTER) || last_measurement + 1000000 > (NRF_RTC1->COUNTER)) - { - last_measurement = (NRF_RTC1->COUNTER); - if (sensors_enabled & BIT(2)) { //enabled temperature - // Sample AIN4 - LM35 - adcval = adc_sample(0); - printk("Temp: %d mV\n", adcval); - - if(adcval > 280) { // 26 deg C - snprintf(buf, 10, "%d", adcval); - data_publish(&client, MQTT_QOS_1_AT_LEAST_ONCE, "nrf/temp", buf, strlen(buf)); - dk_set_led(2, 1); - k_msleep(1000); - } - else dk_set_led(2, 0); - } - - if (sensors_enabled & BIT(3)) { // enabled gas - // Sample AIN5 - gas sensor - adcval = adc_sample(1); - printk("Gas: %d mV %d\n", adcval, sensors_enabled); - - if(adcval > 200) { // reliable limit got by testing - snprintf(buf, 10, "%d", adcval); - data_publish(&client, MQTT_QOS_1_AT_LEAST_ONCE, "nrf/gas", buf, strlen(buf)); - dk_set_led(3, 1); - k_msleep(1000); - } - else dk_set_led(3, 0); - } - } - - } - - LOG_INF("Disconnecting MQTT client..."); - - err = mqtt_disconnect(&client); - if (err) { - LOG_ERR("Could not disconnect MQTT client: %d", err); - } - goto do_connect; -} diff --git a/test/cmock b/test/cmock new file mode 160000 index 0000000..f65066f --- /dev/null +++ b/test/cmock @@ -0,0 +1 @@ +Subproject commit f65066f15d8248e6dcb778efb8739904a4512087 diff --git a/tools/edtt b/tools/edtt new file mode 160000 index 0000000..1ea61a3 --- /dev/null +++ b/tools/edtt @@ -0,0 +1 @@ +Subproject commit 1ea61a390d2bfcf3b2ecdba8f8b0b98dfdffbd11 diff --git a/tools/net-tools b/tools/net-tools new file mode 160000 index 0000000..e0828aa --- /dev/null +++ b/tools/net-tools @@ -0,0 +1 @@ +Subproject commit e0828aa9629b533644dc96ff6d1295c939bd713c diff --git a/west.yml b/west.yml deleted file mode 100644 index 7fd440b..0000000 --- a/west.yml +++ /dev/null @@ -1,13 +0,0 @@ -manifest: - remotes: - - name: ncs - url-base: https://github.com/nrfconnect - projects: - - name: sdk-nrf - remote: ncs - path: nrf - revision: v2.2.0 - import: true - clone-depth: 1 - self: - path: mqtt_simple diff --git a/zephyr b/zephyr new file mode 160000 index 0000000..c0689b1 --- /dev/null +++ b/zephyr @@ -0,0 +1 @@ +Subproject commit c0689b16ff127d1b71a4cc20310d476e1807e26e -- cgit v1.2.3