From c533e7aa4aeb3b43865da48b986659d15e6bb916 Mon Sep 17 00:00:00 2001 From: JonesKisaka Date: Wed, 16 Jul 2025 03:39:52 +0300 Subject: [PATCH 01/16] Add wifi update for S0 --- targets/zephyr_targets/wifi/.gitignore | 2 + targets/zephyr_targets/wifi/CMakeLists.txt | 8 + targets/zephyr_targets/wifi/README.md | 20 +++ targets/zephyr_targets/wifi/README.rst | 69 +++++++++ targets/zephyr_targets/wifi/config.h | 22 +++ targets/zephyr_targets/wifi/prj.conf | 28 ++++ targets/zephyr_targets/wifi/src/main.c | 168 +++++++++++++++++++++ 7 files changed, 317 insertions(+) create mode 100644 targets/zephyr_targets/wifi/.gitignore create mode 100644 targets/zephyr_targets/wifi/CMakeLists.txt create mode 100644 targets/zephyr_targets/wifi/README.md create mode 100644 targets/zephyr_targets/wifi/README.rst create mode 100644 targets/zephyr_targets/wifi/config.h create mode 100644 targets/zephyr_targets/wifi/prj.conf create mode 100644 targets/zephyr_targets/wifi/src/main.c diff --git a/targets/zephyr_targets/wifi/.gitignore b/targets/zephyr_targets/wifi/.gitignore new file mode 100644 index 0000000..5f0760a --- /dev/null +++ b/targets/zephyr_targets/wifi/.gitignore @@ -0,0 +1,2 @@ +README.rst/ +/socs diff --git a/targets/zephyr_targets/wifi/CMakeLists.txt b/targets/zephyr_targets/wifi/CMakeLists.txt new file mode 100644 index 0000000..2a074d9 --- /dev/null +++ b/targets/zephyr_targets/wifi/CMakeLists.txt @@ -0,0 +1,8 @@ +# SPDX-License-Identifier: Apache-2.0 + +cmake_minimum_required(VERSION 3.20.0) + +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(wifi) + +target_sources(app PRIVATE src/main.c) diff --git a/targets/zephyr_targets/wifi/README.md b/targets/zephyr_targets/wifi/README.md new file mode 100644 index 0000000..70620d0 --- /dev/null +++ b/targets/zephyr_targets/wifi/README.md @@ -0,0 +1,20 @@ + WiFi- M0 (ESP32C6) target +## Requirements +1. Mainflux broker details including: hostname, ThingID, Thing Credentials and Channel ID +2. [Zephyr](https://www.zephyrproject.org/) + + +## Configure +1. Edit the [config file](/config.h) with your WiFi AP/STA details. + +## Build +The project can be built by utilising the make file within the target directory + +```bash +west build -p always -b wifi +``` +## Flash +Platform io generate a build directory with the fimware.bin within it. Use the make command to flash to board +```bash +west flash +``` \ No newline at end of file diff --git a/targets/zephyr_targets/wifi/README.rst b/targets/zephyr_targets/wifi/README.rst new file mode 100644 index 0000000..c080536 --- /dev/null +++ b/targets/zephyr_targets/wifi/README.rst @@ -0,0 +1,69 @@ +.. zephyr:code-sample:: wifi-ap-sta-mode + :name: Wi-Fi AP-STA mode + :relevant-api: wifi_mgmt dhcpv4_server + + Configure a Wi-Fi board to operate as both an Access Point (AP) and a Station (STA). + +Overview +******** + +The Wi-Fi AP-STA mode of a Wi-Fi board allows it to function as both +an Access Point (AP) and a Station (STA) simultaneously. +This sample demonstrates how to configure and utilize AP-STA mode. + +Configuration and usage of following interfaces is shown in sample. + +1. ``AP mode``: AP mode is configured and enabled. DHCPv4 server is also + configured to assign IP addresses to the joining station. +2. ``STA mode``: Provide the SSID and PSK of you router + +In this demo, AP-STA mode is enabled using :kconfig:option:`CONFIG_ESP32_WIFI_AP_STA_MODE`. +An additional Wi-Fi node is added in the ``.overlay`` file. The ``net_if``. +In the sample code, initially, the AP mode is enabled, followed by enabling the STA mode. +The driver checks if AP mode was previously enabled. If so, it transitions +the board into AP-STA mode to support both modes and attempts to connect to the +AP specified by the provided SSID and PSK. + +Requirements +************ + +This example should be able to run on any commonly available +:zephyr:board:`esp32_devkitc` development board without any extra hardware. + +To enable or disable ``AP-STA`` mode, modify the :kconfig:option:`CONFIG_ESP32_WIFI_AP_STA_MODE` +parameter in the ``boards/esp32_devkitc_procpu.conf`` file of the demo. Moreover, an +extra Wi-Fi node is included in ``boards/esp32_devkitc_procpu.overlay``. + +By default, AP-STA mode is disabled. + +Building, Flashing and Running +****************************** + +.. zephyr-app-commands:: + :zephyr-app: samples/net/wifi/apsta_mode + :board: esp32_devkitc/esp32/procpu + :goals: build flash + :compact: + +Sample Output +================= + +.. code-block:: console + + *** Booting Zephyr OS build v3.7.0-rc3-104-gd1e5c5b3f9b7 *** + [00:00:05.171,000] MAIN: Turning on AP Mode + [00:00:05.172,000] net_dhcpv4_server: net_dhcpv4_server_start: Started DHCPv4 server, address pool: + [00:00:05.172,000] net_dhcpv4_server: net_dhcpv4_server_start: 0: 192.168.4.11 + [00:00:05.172,000] net_dhcpv4_server: net_dhcpv4_server_start: 1: 192.168.4.12 + [00:00:05.172,000] net_dhcpv4_server: net_dhcpv4_server_start: 2: 192.168.4.13 + [00:00:05.172,000] net_dhcpv4_server: net_dhcpv4_server_start: 3: 192.168.4.14 + [00:00:05.172,000] MAIN: DHCPv4 server started... + + [00:00:05.350,000] MAIN: AP Mode is enabled. Waiting for sta to connect ESP32-AP + [00:00:05.350,000] MAIN: Connecting to SSID: ZIN-Dummy + + [00:00:09.498,000] net_dhcpv4: Received: 192.168.43.44 + [00:00:09.499,000] MAIN: Connected to ZIN-Dummy + [00:00:32.739,000] MAIN: station: 7C:50:79:17:89:19 joined + [00:00:32.832,000] net_dhcpv4_server: dhcpv4_handle_discover: DHCPv4 processing Discover - reserved 192.168.4.11 + [00:00:33.839,000] net_dhcpv4_server: dhcpv4_handle_request: DHCPv4 processing Request - allocated 192.168.4.11 diff --git a/targets/zephyr_targets/wifi/config.h b/targets/zephyr_targets/wifi/config.h new file mode 100644 index 0000000..178384c --- /dev/null +++ b/targets/zephyr_targets/wifi/config.h @@ -0,0 +1,22 @@ +#ifndef CONFIG_H +#define CONFIG_H + +#define MACSTR "%02X:%02X:%02X:%02X:%02X:%02X" + +#define NET_EVENT_WIFI_MASK \ + (NET_EVENT_WIFI_CONNECT_RESULT | NET_EVENT_WIFI_DISCONNECT_RESULT | \ + NET_EVENT_WIFI_AP_ENABLE_RESULT | NET_EVENT_WIFI_AP_DISABLE_RESULT | \ + NET_EVENT_WIFI_AP_STA_CONNECTED | NET_EVENT_WIFI_AP_STA_DISCONNECTED) + +/* AP Mode Configuration */ +#define WIFI_AP_SSID "M0-AP" +#define WIFI_AP_PSK "" +#define WIFI_AP_IP_ADDRESS "192.168.4.1" +#define WIFI_AP_NETMASK "255.255.255.0" + +/* STA Mode Configuration */ +#define WIFI_SSID "SSID" /* Replace `SSID` with WiFi ssid. */ +#define WIFI_PSK "PASSWORD" /* Replace `PASSWORD` with Router password. */ + + +#endif \ No newline at end of file diff --git a/targets/zephyr_targets/wifi/prj.conf b/targets/zephyr_targets/wifi/prj.conf new file mode 100644 index 0000000..632f1af --- /dev/null +++ b/targets/zephyr_targets/wifi/prj.conf @@ -0,0 +1,28 @@ +# Wi-Fi Configuration +CONFIG_WIFI=y + +# Network Configuration +CONFIG_NET_CONFIG_AUTO_INIT=n +CONFIG_NET_CONNECTION_MANAGER=y +CONFIG_NET_DHCPV4=y +CONFIG_NET_DHCPV4_SERVER=y +CONFIG_NET_IF_MAX_IPV4_COUNT=2 +CONFIG_NET_IF_MAX_IPV6_COUNT=2 +CONFIG_NET_IPV4=y +CONFIG_NET_L2_ETHERNET=y +CONFIG_NET_L2_WIFI_MGMT=y +CONFIG_NET_MGMT=y +CONFIG_NET_MGMT_EVENT=y +CONFIG_NET_MGMT_EVENT_INFO=y +CONFIG_NET_MGMT_EVENT_QUEUE_SIZE=10 +CONFIG_NET_MGMT_EVENT_STACK_SIZE=4096 +CONFIG_NET_PKT_RX_COUNT=16 +CONFIG_NET_PKT_TX_COUNT=16 +CONFIG_NET_SOCKETS_SERVICE_STACK_SIZE=4096 +CONFIG_NET_TCP=y +CONFIG_NET_UDP=y +CONFIG_NETWORKING=y + +# LOG Configuration +CONFIG_NET_LOG=y +CONFIG_NET_DHCPV4_SERVER_LOG_LEVEL_DBG=y diff --git a/targets/zephyr_targets/wifi/src/main.c b/targets/zephyr_targets/wifi/src/main.c new file mode 100644 index 0000000..15e5e79 --- /dev/null +++ b/targets/zephyr_targets/wifi/src/main.c @@ -0,0 +1,168 @@ +#include +#include +#include +#include + +#include "config.h" + +LOG_MODULE_REGISTER(MAIN); + +static struct net_if *ap_iface; +static struct net_if *sta_iface; + +static struct wifi_connect_req_params ap_config; +static struct wifi_connect_req_params sta_config; + +static struct net_mgmt_event_callback cb; + +static void wifi_event_handler(struct net_mgmt_event_callback *cb, uint64_t mgmt_event, + struct net_if *iface) +{ + switch (mgmt_event) { + case NET_EVENT_WIFI_CONNECT_RESULT: { + LOG_INF("Connected to %s", WIFI_SSID); + break; + } + case NET_EVENT_WIFI_DISCONNECT_RESULT: { + LOG_INF("Disconnected from %s", WIFI_SSID); + break; + } + case NET_EVENT_WIFI_AP_ENABLE_RESULT: { + LOG_INF("AP Mode is enabled. Waiting for station to connect"); + break; + } + case NET_EVENT_WIFI_AP_DISABLE_RESULT: { + LOG_INF("AP Mode is disabled."); + break; + } + case NET_EVENT_WIFI_AP_STA_CONNECTED: { + struct wifi_ap_sta_info *sta_info = (struct wifi_ap_sta_info *)cb->info; + + LOG_INF("station: " MACSTR " joined ", sta_info->mac[0], sta_info->mac[1], + sta_info->mac[2], sta_info->mac[3], sta_info->mac[4], sta_info->mac[5]); + break; + } + case NET_EVENT_WIFI_AP_STA_DISCONNECTED: { + struct wifi_ap_sta_info *sta_info = (struct wifi_ap_sta_info *)cb->info; + + LOG_INF("station: " MACSTR " leave ", sta_info->mac[0], sta_info->mac[1], + sta_info->mac[2], sta_info->mac[3], sta_info->mac[4], sta_info->mac[5]); + break; + } + default: + break; + } +} + +static void enable_dhcpv4_server(void) +{ + static struct in_addr addr; + static struct in_addr netmaskAddr; + + if (net_addr_pton(AF_INET, WIFI_AP_IP_ADDRESS, &addr)) { + LOG_ERR("Invalid address: %s", WIFI_AP_IP_ADDRESS); + return; + } + + if (net_addr_pton(AF_INET, WIFI_AP_NETMASK, &netmaskAddr)) { + LOG_ERR("Invalid netmask: %s", WIFI_AP_NETMASK); + return; + } + + net_if_ipv4_set_gw(ap_iface, &addr); + + if (net_if_ipv4_addr_add(ap_iface, &addr, NET_ADDR_MANUAL, 0) == NULL) { + LOG_ERR("unable to set IP address for AP interface"); + } + + if (!net_if_ipv4_set_netmask_by_addr(ap_iface, &addr, &netmaskAddr)) { + LOG_ERR("Unable to set netmask for AP interface: %s", WIFI_AP_NETMASK); + } + + addr.s4_addr[3] += 10; /* Starting IPv4 address for DHCPv4 address pool. */ + + if (net_dhcpv4_server_start(ap_iface, &addr) != 0) { + LOG_ERR("DHCP server is not started for desired IP"); + return; + } + + LOG_INF("DHCPv4 server started...\n"); +} + +static int enable_ap_mode(void) +{ + if (!ap_iface) { + LOG_INF("AP: is not initialized"); + return -EIO; + } + + LOG_INF("Turning on AP Mode"); + ap_config.ssid = (const uint8_t *)WIFI_AP_SSID; + ap_config.ssid_length = strlen(WIFI_AP_SSID); + ap_config.psk = (const uint8_t *)WIFI_AP_PSK; + ap_config.psk_length = strlen(WIFI_AP_PSK); + ap_config.channel = WIFI_CHANNEL_ANY; + ap_config.band = WIFI_FREQ_BAND_2_4_GHZ; + + if (strlen(WIFI_AP_PSK) == 0) { + ap_config.security = WIFI_SECURITY_TYPE_NONE; + } else { + + ap_config.security = WIFI_SECURITY_TYPE_PSK; + } + + enable_dhcpv4_server(); + + int ret = net_mgmt(NET_REQUEST_WIFI_AP_ENABLE, ap_iface, &ap_config, + sizeof(struct wifi_connect_req_params)); + if (ret) { + LOG_ERR("NET_REQUEST_WIFI_AP_ENABLE failed, err: %d", ret); + } + + return ret; +} + +static int connect_to_wifi(void) +{ + if (!sta_iface) { + LOG_INF("STA: interface no initialized"); + return -EIO; + } + + sta_config.ssid = (const uint8_t *)WIFI_SSID; + sta_config.ssid_length = strlen(WIFI_SSID); + sta_config.psk = (const uint8_t *)WIFI_PSK; + sta_config.psk_length = strlen(WIFI_PSK); + sta_config.security = WIFI_SECURITY_TYPE_PSK; + sta_config.channel = WIFI_CHANNEL_ANY; + sta_config.band = WIFI_FREQ_BAND_2_4_GHZ; + + LOG_INF("Connecting to SSID: %s\n", sta_config.ssid); + + int ret = net_mgmt(NET_REQUEST_WIFI_CONNECT, sta_iface, &sta_config, + sizeof(struct wifi_connect_req_params)); + if (ret) { + LOG_ERR("Unable to Connect to (%s)", WIFI_SSID); + } + + return ret; +} + +int main(void) +{ + k_sleep(K_SECONDS(5)); + + net_mgmt_init_event_callback(&cb, wifi_event_handler, NET_EVENT_WIFI_MASK); + net_mgmt_add_event_callback(&cb); + + /* Get AP interface in AP-STA mode. */ + ap_iface = net_if_get_wifi_sap(); + + /* Get STA interface in AP-STA mode. */ + sta_iface = net_if_get_wifi_sta(); + + enable_ap_mode(); + connect_to_wifi(); + + return 0; +} From 2ab178cbdee2a23742f95fd3807a47708418e15d Mon Sep 17 00:00:00 2001 From: JonesKisaka Date: Wed, 16 Jul 2025 03:48:49 +0300 Subject: [PATCH 02/16] add .gitignore --- targets/zephyr_targets/wifi/.gitignore | 4 ++-- targets/zephyr_targets/wifi/{ => src}/config.h | 0 2 files changed, 2 insertions(+), 2 deletions(-) rename targets/zephyr_targets/wifi/{ => src}/config.h (100%) diff --git a/targets/zephyr_targets/wifi/.gitignore b/targets/zephyr_targets/wifi/.gitignore index 5f0760a..d325909 100644 --- a/targets/zephyr_targets/wifi/.gitignore +++ b/targets/zephyr_targets/wifi/.gitignore @@ -1,2 +1,2 @@ -README.rst/ -/socs +README.rst +socs/ diff --git a/targets/zephyr_targets/wifi/config.h b/targets/zephyr_targets/wifi/src/config.h similarity index 100% rename from targets/zephyr_targets/wifi/config.h rename to targets/zephyr_targets/wifi/src/config.h From cc2d4faa90c43ca86a2875d51f2824a23a7819e1 Mon Sep 17 00:00:00 2001 From: JonesKisaka Date: Wed, 16 Jul 2025 03:52:11 +0300 Subject: [PATCH 03/16] Add gitignore --- targets/zephyr_targets/wifi/README.rst | 69 -------------------------- 1 file changed, 69 deletions(-) delete mode 100644 targets/zephyr_targets/wifi/README.rst diff --git a/targets/zephyr_targets/wifi/README.rst b/targets/zephyr_targets/wifi/README.rst deleted file mode 100644 index c080536..0000000 --- a/targets/zephyr_targets/wifi/README.rst +++ /dev/null @@ -1,69 +0,0 @@ -.. zephyr:code-sample:: wifi-ap-sta-mode - :name: Wi-Fi AP-STA mode - :relevant-api: wifi_mgmt dhcpv4_server - - Configure a Wi-Fi board to operate as both an Access Point (AP) and a Station (STA). - -Overview -******** - -The Wi-Fi AP-STA mode of a Wi-Fi board allows it to function as both -an Access Point (AP) and a Station (STA) simultaneously. -This sample demonstrates how to configure and utilize AP-STA mode. - -Configuration and usage of following interfaces is shown in sample. - -1. ``AP mode``: AP mode is configured and enabled. DHCPv4 server is also - configured to assign IP addresses to the joining station. -2. ``STA mode``: Provide the SSID and PSK of you router - -In this demo, AP-STA mode is enabled using :kconfig:option:`CONFIG_ESP32_WIFI_AP_STA_MODE`. -An additional Wi-Fi node is added in the ``.overlay`` file. The ``net_if``. -In the sample code, initially, the AP mode is enabled, followed by enabling the STA mode. -The driver checks if AP mode was previously enabled. If so, it transitions -the board into AP-STA mode to support both modes and attempts to connect to the -AP specified by the provided SSID and PSK. - -Requirements -************ - -This example should be able to run on any commonly available -:zephyr:board:`esp32_devkitc` development board without any extra hardware. - -To enable or disable ``AP-STA`` mode, modify the :kconfig:option:`CONFIG_ESP32_WIFI_AP_STA_MODE` -parameter in the ``boards/esp32_devkitc_procpu.conf`` file of the demo. Moreover, an -extra Wi-Fi node is included in ``boards/esp32_devkitc_procpu.overlay``. - -By default, AP-STA mode is disabled. - -Building, Flashing and Running -****************************** - -.. zephyr-app-commands:: - :zephyr-app: samples/net/wifi/apsta_mode - :board: esp32_devkitc/esp32/procpu - :goals: build flash - :compact: - -Sample Output -================= - -.. code-block:: console - - *** Booting Zephyr OS build v3.7.0-rc3-104-gd1e5c5b3f9b7 *** - [00:00:05.171,000] MAIN: Turning on AP Mode - [00:00:05.172,000] net_dhcpv4_server: net_dhcpv4_server_start: Started DHCPv4 server, address pool: - [00:00:05.172,000] net_dhcpv4_server: net_dhcpv4_server_start: 0: 192.168.4.11 - [00:00:05.172,000] net_dhcpv4_server: net_dhcpv4_server_start: 1: 192.168.4.12 - [00:00:05.172,000] net_dhcpv4_server: net_dhcpv4_server_start: 2: 192.168.4.13 - [00:00:05.172,000] net_dhcpv4_server: net_dhcpv4_server_start: 3: 192.168.4.14 - [00:00:05.172,000] MAIN: DHCPv4 server started... - - [00:00:05.350,000] MAIN: AP Mode is enabled. Waiting for sta to connect ESP32-AP - [00:00:05.350,000] MAIN: Connecting to SSID: ZIN-Dummy - - [00:00:09.498,000] net_dhcpv4: Received: 192.168.43.44 - [00:00:09.499,000] MAIN: Connected to ZIN-Dummy - [00:00:32.739,000] MAIN: station: 7C:50:79:17:89:19 joined - [00:00:32.832,000] net_dhcpv4_server: dhcpv4_handle_discover: DHCPv4 processing Discover - reserved 192.168.4.11 - [00:00:33.839,000] net_dhcpv4_server: dhcpv4_handle_request: DHCPv4 processing Request - allocated 192.168.4.11 From b244d48a481b694a969ce72cc27052c55cd33568 Mon Sep 17 00:00:00 2001 From: JonesKisaka Date: Thu, 24 Jul 2025 16:30:45 +0300 Subject: [PATCH 04/16] Add MQTT M0 Zephyr code --- .../m0-mqtt/.vscode/settings.json | 3 + targets/zephyr_targets/m0-mqtt/CMakeLists.txt | 11 + targets/zephyr_targets/m0-mqtt/Kconfig | 19 + targets/zephyr_targets/m0-mqtt/anonymous.conf | 5 + targets/zephyr_targets/m0-mqtt/prj.conf | 55 ++ targets/zephyr_targets/m0-mqtt/src/config.h | 13 + targets/zephyr_targets/m0-mqtt/src/main.c | 586 ++++++++++++++++++ 7 files changed, 692 insertions(+) create mode 100644 targets/zephyr_targets/m0-mqtt/.vscode/settings.json create mode 100644 targets/zephyr_targets/m0-mqtt/CMakeLists.txt create mode 100644 targets/zephyr_targets/m0-mqtt/Kconfig create mode 100644 targets/zephyr_targets/m0-mqtt/anonymous.conf create mode 100644 targets/zephyr_targets/m0-mqtt/prj.conf create mode 100644 targets/zephyr_targets/m0-mqtt/src/config.h create mode 100644 targets/zephyr_targets/m0-mqtt/src/main.c diff --git a/targets/zephyr_targets/m0-mqtt/.vscode/settings.json b/targets/zephyr_targets/m0-mqtt/.vscode/settings.json new file mode 100644 index 0000000..b4deda3 --- /dev/null +++ b/targets/zephyr_targets/m0-mqtt/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "idf.pythonInstallPath": "/usr/bin/python" +} \ No newline at end of file diff --git a/targets/zephyr_targets/m0-mqtt/CMakeLists.txt b/targets/zephyr_targets/m0-mqtt/CMakeLists.txt new file mode 100644 index 0000000..df65010 --- /dev/null +++ b/targets/zephyr_targets/m0-mqtt/CMakeLists.txt @@ -0,0 +1,11 @@ +# SPDX-License-Identifier: Apache-2.0 + +cmake_minimum_required(VERSION 3.20.0) + +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(mqtt_publisher) + +FILE(GLOB app_sources src/*.c) +target_sources(app PRIVATE ${app_sources}) + +include(${ZEPHYR_BASE}/samples/net/common/common.cmake) diff --git a/targets/zephyr_targets/m0-mqtt/Kconfig b/targets/zephyr_targets/m0-mqtt/Kconfig new file mode 100644 index 0000000..f870531 --- /dev/null +++ b/targets/zephyr_targets/m0-mqtt/Kconfig @@ -0,0 +1,19 @@ +mainmenu "Networking mqtt_publisher sample application" + +config NET_SAMPLE_APP_MAX_ITERATIONS + int "Number of times to Publish sample MQTT messages" + default 500 + help + Send sample MQTT messages this many times in a MQTT connection + before exiting. A value of zero means that the messages are sent + forever. + +config NET_SAMPLE_APP_MAX_CONNECTIONS + int "Number of times to connect to the MQTT server" + default 0 + help + Number of times to connect to the MQTT server. With each connection + send NET_SAMPLE_APP_MAX_ITERATIONS amount of MQTT sample messages. + A value of zero means to continue forever. + +source "Kconfig.zephyr" diff --git a/targets/zephyr_targets/m0-mqtt/anonymous.conf b/targets/zephyr_targets/m0-mqtt/anonymous.conf new file mode 100644 index 0000000..05761b8 --- /dev/null +++ b/targets/zephyr_targets/m0-mqtt/anonymous.conf @@ -0,0 +1,5 @@ +#This is the Configuration File if using Mosquitto as broker. +#mosquitto.conf +listener 1883 +allow_anonymous true +persistence true diff --git a/targets/zephyr_targets/m0-mqtt/prj.conf b/targets/zephyr_targets/m0-mqtt/prj.conf new file mode 100644 index 0000000..2e855c0 --- /dev/null +++ b/targets/zephyr_targets/m0-mqtt/prj.conf @@ -0,0 +1,55 @@ +CONFIG_NETWORKING=y +CONFIG_NET_SOCKETS=y +CONFIG_NET_TCP=y +CONFIG_NET_LOG=y + +#Confuguring WiFi +CONFIG_WIFI=y +CONFIG_NET_CONFIG_AUTO_INIT=n +CONFIG_NET_CONNECTION_MANAGER=y +CONFIG_NET_DHCPV4=y +CONFIG_NET_DHCPV4_SERVER=y +CONFIG_NET_IF_MAX_IPV4_COUNT=2 +CONFIG_NET_IF_MAX_IPV6_COUNT=2 +CONFIG_NET_L2_ETHERNET=y +CONFIG_NET_L2_WIFI_MGMT=y +CONFIG_NET_MGMT=y +CONFIG_NET_MGMT_EVENT=y +CONFIG_NET_MGMT_EVENT_INFO=y +CONFIG_NET_MGMT_EVENT_QUEUE_SIZE=10 +CONFIG_NET_MGMT_EVENT_STACK_SIZE=4096 +CONFIG_NET_PKT_RX_COUNT=16 +CONFIG_NET_PKT_TX_COUNT=16 +CONFIG_NET_SOCKETS_SERVICE_STACK_SIZE=4096 +CONFIG_NET_UDP=y +CONFIG_NET_DHCPV4_SERVER_LOG_LEVEL_DBG=y + +#Configuring Network +CONFIG_NET_IPV6_RA_RDNSS=y +CONFIG_NET_IF_UNICAST_IPV6_ADDR_COUNT=3 +CONFIG_NET_IF_MCAST_IPV6_ADDR_COUNT=2 +CONFIG_NET_CONFIG_SETTINGS=y +CONFIG_NET_CONFIG_MY_IPV6_ADDR="2001:db8::1" +CONFIG_NET_CONFIG_PEER_IPV6_ADDR="2001:db8::2" +CONFIG_NET_CONFIG_MY_IPV4_ADDR="192.0.2.1" +CONFIG_NET_CONFIG_PEER_IPV4_ADDR="192.168.8.102" +CONFIG_NET_IPV6=n # Enable IPv6 support +CONFIG_NET_IPV4=y # Enable IPv4 support +# For IPv6 +CONFIG_NET_BUF_DATA_SIZE=256 +CONFIG_NET_SHELL=y + + +CONFIG_PRINTK=y +CONFIG_STDOUT_CONSOLE=y +CONFIG_POSIX_API=y + + +# Enable the MQTT Lib +CONFIG_MQTT_LIB=y + +CONFIG_MAIN_STACK_SIZE=2048 + + +CONFIG_ENTROPY_GENERATOR=y +CONFIG_TEST_RANDOM_GENERATOR=y diff --git a/targets/zephyr_targets/m0-mqtt/src/config.h b/targets/zephyr_targets/m0-mqtt/src/config.h new file mode 100644 index 0000000..484e75a --- /dev/null +++ b/targets/zephyr_targets/m0-mqtt/src/config.h @@ -0,0 +1,13 @@ +#ifndef __CONFIG_H__ +#define __CONFIG_H__ + +#define ZEPHYR_ADDR "192.168.1.101" +#define SERVER_ADDR "192.168.8.102" +#define SERVER_PORT 1883 +#define APP_CONNECT_TIMEOUT_MS 2000 +#define APP_SLEEP_MSECS 200 +#define APP_CONNECT_TRIES 5 +#define APP_MQTT_BUFFER_SIZE 128 +#define MQTT_CLIENTID "zephyr_publisher" + +#endif diff --git a/targets/zephyr_targets/m0-mqtt/src/main.c b/targets/zephyr_targets/m0-mqtt/src/main.c new file mode 100644 index 0000000..8622cce --- /dev/null +++ b/targets/zephyr_targets/m0-mqtt/src/main.c @@ -0,0 +1,586 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +LOG_MODULE_REGISTER(net_mqtt_publisher_sample, LOG_LEVEL_DBG); +#define MACSTR "%02X:%02X:%02X:%02X:%02X:%02X" +#define NET_EVENT_WIFI_MASK \ + (NET_EVENT_WIFI_CONNECT_RESULT | NET_EVENT_WIFI_DISCONNECT_RESULT | \ + NET_EVENT_WIFI_AP_ENABLE_RESULT | NET_EVENT_WIFI_AP_DISABLE_RESULT | \ + NET_EVENT_WIFI_AP_STA_CONNECTED | NET_EVENT_WIFI_AP_STA_DISCONNECTED) + +/* AP Mode Configuration */ +#define WIFI_AP_SSID "M0-AP" +#define WIFI_AP_PSK "" +#define WIFI_AP_IP_ADDRESS "192.168.4.1" +#define WIFI_AP_NETMASK "255.255.255.0" + +/* STA Mode Configuration */ +#define WIFI_SSID "SSID" /* Replace `SSID` with WiFi ssid. */ +#define WIFI_PSK "PASSWORD" /* Replace `PASSWORD` with Router password. */ + + +static struct net_if *ap_iface; +static struct net_if *sta_iface; + +static struct wifi_connect_req_params ap_config; +static struct wifi_connect_req_params sta_config; + +static struct net_mgmt_event_callback cb; + +#if defined(CONFIG_LOG_BACKEND_MQTT) +#include +#endif + +#include +#include + +#include "config.h" +#include "net_sample_common.h" + +#if defined(CONFIG_USERSPACE) +#include +K_APPMEM_PARTITION_DEFINE(app_partition); +struct k_mem_domain app_domain; +#define APP_BMEM K_APP_BMEM(app_partition) +#define APP_DMEM K_APP_DMEM(app_partition) +#else +#define APP_BMEM +#define APP_DMEM +#endif + +/* Buffers for MQTT client. */ +static APP_BMEM uint8_t rx_buffer[APP_MQTT_BUFFER_SIZE]; +static APP_BMEM uint8_t tx_buffer[APP_MQTT_BUFFER_SIZE]; + +#if defined(CONFIG_MQTT_LIB_WEBSOCKET) +/* Making RX buffer large enough that the full IPv6 packet can fit into it */ +#define MQTT_LIB_WEBSOCKET_RECV_BUF_LEN 1280 + +/* Websocket needs temporary buffer to store partial packets */ +static APP_BMEM uint8_t temp_ws_rx_buf[MQTT_LIB_WEBSOCKET_RECV_BUF_LEN]; +#endif + +/* The mqtt client struct */ +static APP_BMEM struct mqtt_client client_ctx; + +/* MQTT Broker details. */ +static APP_BMEM struct sockaddr_storage broker; +#define BROKER "BROKER_IP" +#define BROKER_PORT 1883 /*Change to your broker port */ + +#if defined(CONFIG_SOCKS) +static APP_BMEM struct sockaddr socks5_proxy; +#endif + +static APP_BMEM struct pollfd fds[1]; +static APP_BMEM int nfds; +static APP_BMEM bool connected; + +/* Whether to include full topic in the publish message*/ +static APP_BMEM bool include_topic; +static APP_BMEM bool aliases_enabled; + +#define APP_TOPIC_ALIAS 1 + /* CONFIG_MQTT_LIB_TLS */ +static void wifi_event_handler(struct net_mgmt_event_callback *cb, uint64_t mgmt_event, + struct net_if *iface) +{ + switch (mgmt_event) { + case NET_EVENT_WIFI_CONNECT_RESULT: { + LOG_INF("Connected to %s", WIFI_SSID); + break; + } + case NET_EVENT_WIFI_DISCONNECT_RESULT: { + LOG_INF("Disconnected from %s", WIFI_SSID); + break; + } + case NET_EVENT_WIFI_AP_ENABLE_RESULT: { + LOG_INF("AP Mode is enabled. Waiting for station to connect"); + break; + } + case NET_EVENT_WIFI_AP_DISABLE_RESULT: { + LOG_INF("AP Mode is disabled."); + break; + } + case NET_EVENT_WIFI_AP_STA_CONNECTED: { + struct wifi_ap_sta_info *sta_info = (struct wifi_ap_sta_info *)cb->info; + + LOG_INF("station: " MACSTR " joined ", sta_info->mac[0], sta_info->mac[1], + sta_info->mac[2], sta_info->mac[3], sta_info->mac[4], sta_info->mac[5]); + break; + } + case NET_EVENT_WIFI_AP_STA_DISCONNECTED: { + struct wifi_ap_sta_info *sta_info = (struct wifi_ap_sta_info *)cb->info; + + LOG_INF("station: " MACSTR " leave ", sta_info->mac[0], sta_info->mac[1], + sta_info->mac[2], sta_info->mac[3], sta_info->mac[4], sta_info->mac[5]); + break; + } + default: + break; + } +} + +static void enable_dhcpv4_server(void) +{ + static struct in_addr addr; + static struct in_addr netmaskAddr; + + if (net_addr_pton(AF_INET, WIFI_AP_IP_ADDRESS, &addr)) { + LOG_ERR("Invalid address: %s", WIFI_AP_IP_ADDRESS); + return; + } + + if (net_addr_pton(AF_INET, WIFI_AP_NETMASK, &netmaskAddr)) { + LOG_ERR("Invalid netmask: %s", WIFI_AP_NETMASK); + return; + } + + net_if_ipv4_set_gw(ap_iface, &addr); + + if (net_if_ipv4_addr_add(ap_iface, &addr, NET_ADDR_MANUAL, 0) == NULL) { + LOG_ERR("unable to set IP address for AP interface"); + } + + if (!net_if_ipv4_set_netmask_by_addr(ap_iface, &addr, &netmaskAddr)) { + LOG_ERR("Unable to set netmask for AP interface: %s", WIFI_AP_NETMASK); + } + + addr.s4_addr[3] += 10; /* Starting IPv4 address for DHCPv4 address pool. */ + + if (net_dhcpv4_server_start(ap_iface, &addr) != 0) { + LOG_ERR("DHCP server is not started for desired IP"); + return; + } + + LOG_INF("DHCPv4 server started...\n"); +} + +static int enable_ap_mode(void) +{ + if (!ap_iface) { + LOG_INF("AP: is not initialized"); + return -EIO; + } + + LOG_INF("Turning on AP Mode"); + ap_config.ssid = (const uint8_t *)WIFI_AP_SSID; + ap_config.ssid_length = strlen(WIFI_AP_SSID); + ap_config.psk = (const uint8_t *)WIFI_AP_PSK; + ap_config.psk_length = strlen(WIFI_AP_PSK); + ap_config.channel = WIFI_CHANNEL_ANY; + ap_config.band = WIFI_FREQ_BAND_2_4_GHZ; + + if (strlen(WIFI_AP_PSK) == 0) { + ap_config.security = WIFI_SECURITY_TYPE_NONE; + } else { + + ap_config.security = WIFI_SECURITY_TYPE_PSK; + } + + enable_dhcpv4_server(); + + int ret = net_mgmt(NET_REQUEST_WIFI_AP_ENABLE, ap_iface, &ap_config, + sizeof(struct wifi_connect_req_params)); + if (ret) { + LOG_ERR("NET_REQUEST_WIFI_AP_ENABLE failed, err: %d", ret); + } + + return ret; +} + +static int connect_to_wifi(void) +{ + if (!sta_iface) { + LOG_INF("STA: interface no initialized"); + return -EIO; + } + + sta_config.ssid = (const uint8_t *)WIFI_SSID; + sta_config.ssid_length = strlen(WIFI_SSID); + sta_config.psk = (const uint8_t *)WIFI_PSK; + sta_config.psk_length = strlen(WIFI_PSK); + sta_config.security = WIFI_SECURITY_TYPE_PSK; + sta_config.channel = WIFI_CHANNEL_ANY; + sta_config.band = WIFI_FREQ_BAND_2_4_GHZ; + + LOG_INF("Connecting to SSID: %s\n", sta_config.ssid); + + int ret = net_mgmt(NET_REQUEST_WIFI_CONNECT, sta_iface, &sta_config, + sizeof(struct wifi_connect_req_params)); + if (ret) { + LOG_ERR("Unable to Connect to (%s)", WIFI_SSID); + } + + return ret; +} + +static void prepare_fds(struct mqtt_client *client) +{ + if (client->transport.type == MQTT_TRANSPORT_NON_SECURE) { + fds[0].fd = client->transport.tcp.sock; + } + + fds[0].events = POLLIN; + nfds = 1; +} + +static void clear_fds(void) +{ + nfds = 0; +} + +static int wait(int timeout) +{ + int ret = 0; + + if (nfds > 0) { + ret = poll(fds, nfds, timeout); + if (ret < 0) { + LOG_ERR("poll error: %d", errno); + } + } + + return ret; +} + +void mqtt_evt_handler(struct mqtt_client *const client, + 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; + } + + connected = true; + LOG_INF("MQTT client connected!"); + + break; + + case MQTT_EVT_DISCONNECT: + LOG_INF("MQTT client disconnected %d", evt->result); + + connected = false; + clear_fds(); + + 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_PUBREC: + if (evt->result != 0) { + LOG_ERR("MQTT PUBREC error %d", evt->result); + break; + } + + LOG_INF("PUBREC packet id: %u", evt->param.pubrec.message_id); + + const struct mqtt_pubrel_param rel_param = { + .message_id = evt->param.pubrec.message_id + }; + + err = mqtt_publish_qos2_release(client, &rel_param); + if (err != 0) { + LOG_ERR("Failed to send MQTT PUBREL: %d", err); + } + + break; + + case MQTT_EVT_PUBCOMP: + if (evt->result != 0) { + LOG_ERR("MQTT PUBCOMP error %d", evt->result); + break; + } + + LOG_INF("PUBCOMP packet id: %u", + evt->param.pubcomp.message_id); + + break; + + case MQTT_EVT_PINGRESP: + LOG_INF("PINGRESP packet"); + break; + + default: + break; + } +} + +static char *get_mqtt_payload(enum mqtt_qos qos) +{ + static APP_DMEM char payload[] = "Hello World. Its M0 here!"; + + payload[strlen(payload) - 1] = '0' + qos; + + return payload; +} + +static char *get_mqtt_topic(void) +{ + return "sensors"; +} + +static int publish(struct mqtt_client *client, enum mqtt_qos qos) +{ + struct mqtt_publish_param param = { 0 }; + + /* Always true for MQTT 3.1.1. + * True only on first publish message for MQTT 5.0 if broker allows aliases. + */ + if (include_topic) { + param.message.topic.topic.utf8 = (uint8_t *)get_mqtt_topic(); + param.message.topic.topic.size = + strlen(param.message.topic.topic.utf8); + } + + param.message.topic.qos = qos; + param.message.payload.data = get_mqtt_payload(qos); + param.message.payload.len = + strlen(param.message.payload.data); + param.message_id = sys_rand16_get(); + param.dup_flag = 0U; + param.retain_flag = 0U; + + return mqtt_publish(client, ¶m); +} + +#define RC_STR(rc) ((rc) == 0 ? "OK" : "ERROR") + +#define PRINT_RESULT(func, rc) \ + LOG_INF("%s: %d <%s>", (func), rc, RC_STR(rc)) + +static void broker_init(void) +{ + struct sockaddr_in *broker4 = (struct sockaddr_in *)&broker; + + broker4->sin_family = AF_INET; + broker4->sin_port = htons(BROKER_PORT); + inet_pton(AF_INET, BROKER, &broker4->sin_addr); +} + +static void client_init(struct mqtt_client *client) +{ + LOG_INF("CLient Initialization!"); + mqtt_client_init(client); + LOG_INF("CLient Initialized!"); + k_sleep(K_SECONDS(5)); ////////////// + LOG_INF("Broker Initialization!"); + broker_init(); + LOG_INF("Broker Initialized!"); + + /* MQTT client configuration */ + client->broker = &broker; + client->evt_cb = mqtt_evt_handler; + client->client_id.utf8 = (uint8_t *)MQTT_CLIENTID; + client->client_id.size = strlen(MQTT_CLIENTID); + client->password = NULL; + client->user_name = NULL; + client->protocol_version = MQTT_VERSION_3_1_1; + + LOG_INF("MQTT buffers configuration"); + /* 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 */ + client->transport.type = MQTT_TRANSPORT_NON_SECURE; +} + +/* In this routine we block until the connected variable is 1 */ +static int try_to_connect(struct mqtt_client *client) +{ + int rc, i = 0; + + while (i++ < APP_CONNECT_TRIES && !connected) { + + client_init(client); + + rc = mqtt_connect(client); + if (rc != 0) { + PRINT_RESULT("mqtt_connect", rc); + k_sleep(K_MSEC(APP_SLEEP_MSECS)); + continue; + } + + prepare_fds(client); + + if (wait(APP_CONNECT_TIMEOUT_MS)) { + mqtt_input(client); + } + + if (!connected) { + mqtt_abort(client); + } + } + + if (connected) { + return 0; + } + + return -EINVAL; +} + +static int process_mqtt_and_sleep(struct mqtt_client *client, int timeout) +{ + int64_t remaining = timeout; + int64_t start_time = k_uptime_get(); + int rc; + + while (remaining > 0 && connected) { + if (wait(remaining)) { + rc = mqtt_input(client); + if (rc != 0) { + PRINT_RESULT("mqtt_input", rc); + return rc; + } + } + + rc = mqtt_live(client); + if (rc != 0 && rc != -EAGAIN) { + PRINT_RESULT("mqtt_live", rc); + return rc; + } else if (rc == 0) { + rc = mqtt_input(client); + if (rc != 0) { + PRINT_RESULT("mqtt_input", rc); + return rc; + } + } + + remaining = timeout + start_time - k_uptime_get(); + } + + return 0; +} + +#define SUCCESS_OR_EXIT(rc) { if (rc != 0) { return 1; } } +#define SUCCESS_OR_BREAK(rc) { if (rc != 0) { break; } } + +static int publisher(void) +{ + int i, rc, r = 0; + + include_topic = true; + aliases_enabled = false; + + LOG_INF("attempting to connect: "); + rc = try_to_connect(&client_ctx); + PRINT_RESULT("try_to_connect", rc); + SUCCESS_OR_EXIT(rc); + + i = 0; + while (i++ < CONFIG_NET_SAMPLE_APP_MAX_ITERATIONS && connected) { + r = -1; + + rc = mqtt_ping(&client_ctx); + PRINT_RESULT("mqtt_ping", rc); + SUCCESS_OR_BREAK(rc); + + rc = process_mqtt_and_sleep(&client_ctx, APP_SLEEP_MSECS); + SUCCESS_OR_BREAK(rc); + + rc = publish(&client_ctx, MQTT_QOS_0_AT_MOST_ONCE); + PRINT_RESULT("mqtt_publish", rc); + SUCCESS_OR_BREAK(rc); + + rc = process_mqtt_and_sleep(&client_ctx, APP_SLEEP_MSECS); + SUCCESS_OR_BREAK(rc); + + rc = publish(&client_ctx, MQTT_QOS_1_AT_LEAST_ONCE); + PRINT_RESULT("mqtt_publish", rc); + SUCCESS_OR_BREAK(rc); + + rc = process_mqtt_and_sleep(&client_ctx, APP_SLEEP_MSECS); + SUCCESS_OR_BREAK(rc); + + rc = publish(&client_ctx, MQTT_QOS_2_EXACTLY_ONCE); + PRINT_RESULT("mqtt_publish", rc); + SUCCESS_OR_BREAK(rc); + + rc = process_mqtt_and_sleep(&client_ctx, APP_SLEEP_MSECS); + SUCCESS_OR_BREAK(rc); + + r = 0; + } + + rc = mqtt_disconnect(&client_ctx, NULL); + PRINT_RESULT("mqtt_disconnect", rc); + + LOG_INF("Bye!"); + + return r; +} + +static int start_app(void) +{ + int r = 0, i = 0; + + while (!CONFIG_NET_SAMPLE_APP_MAX_CONNECTIONS || + i++ < CONFIG_NET_SAMPLE_APP_MAX_CONNECTIONS) { + r = publisher(); + + if (!CONFIG_NET_SAMPLE_APP_MAX_CONNECTIONS) { + k_sleep(K_MSEC(500)); + } + } + + return r; +} + +#if defined(CONFIG_USERSPACE) +#define STACK_SIZE 2048 + +#if defined(CONFIG_NET_TC_THREAD_COOPERATIVE) +#define THREAD_PRIORITY K_PRIO_COOP(CONFIG_NUM_COOP_PRIORITIES - 1) +#else +#define THREAD_PRIORITY K_PRIO_PREEMPT(8) +#endif + +K_THREAD_DEFINE(app_thread, STACK_SIZE, + start_app, NULL, NULL, NULL, + THREAD_PRIORITY, K_USER, -1); + +static K_HEAP_DEFINE(app_mem_pool, 1024 * 2); +#endif + +int main(void) +{ + //wait_for_network(); + k_sleep(K_SECONDS(5)); + + net_mgmt_init_event_callback(&cb, wifi_event_handler, NET_EVENT_WIFI_MASK); + net_mgmt_add_event_callback(&cb); + + /* Get AP interface in AP-STA mode. */ + ap_iface = net_if_get_wifi_sap(); + + /* Get STA interface in AP-STA mode. */ + sta_iface = net_if_get_wifi_sta(); + + enable_ap_mode(); + connect_to_wifi(); + + k_sleep(K_SECONDS(15)); + exit(start_app()); + return 0; +} From ccc40db8dd3a11d6291a092c947eb4fec326019a Mon Sep 17 00:00:00 2001 From: JonesKisaka Date: Thu, 24 Jul 2025 16:56:19 +0300 Subject: [PATCH 05/16] remove .vscode --- targets/zephyr_targets/m0-mqtt/.vscode/settings.json | 3 --- 1 file changed, 3 deletions(-) delete mode 100644 targets/zephyr_targets/m0-mqtt/.vscode/settings.json diff --git a/targets/zephyr_targets/m0-mqtt/.vscode/settings.json b/targets/zephyr_targets/m0-mqtt/.vscode/settings.json deleted file mode 100644 index b4deda3..0000000 --- a/targets/zephyr_targets/m0-mqtt/.vscode/settings.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "idf.pythonInstallPath": "/usr/bin/python" -} \ No newline at end of file From 6d726f37b0013c147d8ac71af10b8c2836ab718d Mon Sep 17 00:00:00 2001 From: JonesKisaka Date: Mon, 28 Jul 2025 13:19:52 +0300 Subject: [PATCH 06/16] Addition of M0 HTTP Connection --- .vscode/c_cpp_properties.json | 18 +++++++++++ .vscode/launch.json | 24 ++++++++++++++ .vscode/settings.json | 59 +++++++++++++++++++++++++++++++++++ 3 files changed, 101 insertions(+) create mode 100644 .vscode/c_cpp_properties.json create mode 100644 .vscode/launch.json create mode 100644 .vscode/settings.json diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..c2098a2 --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,18 @@ +{ + "configurations": [ + { + "name": "linux-gcc-x64", + "includePath": [ + "${workspaceFolder}/**" + ], + "compilerPath": "/usr/bin/gcc", + "cStandard": "${default}", + "cppStandard": "${default}", + "intelliSenseMode": "linux-gcc-x64", + "compilerArgs": [ + "" + ] + } + ], + "version": 4 +} \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..da121fc --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,24 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "C/C++ Runner: Debug Session", + "type": "cppdbg", + "request": "launch", + "args": [], + "stopAtEntry": false, + "externalConsole": false, + "cwd": "/home/kisaka_the_jones/ULTRAVIOLET/Absmach/embedded/targets/zephyr/wifi/src", + "program": "/home/kisaka_the_jones/ULTRAVIOLET/Absmach/embedded/targets/zephyr/wifi/src/build/Debug/outDebug", + "MIMode": "gdb", + "miDebuggerPath": "gdb", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ] + } + ] +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..3e5eb95 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,59 @@ +{ + "C_Cpp_Runner.cCompilerPath": "gcc", + "C_Cpp_Runner.cppCompilerPath": "g++", + "C_Cpp_Runner.debuggerPath": "gdb", + "C_Cpp_Runner.cStandard": "", + "C_Cpp_Runner.cppStandard": "", + "C_Cpp_Runner.msvcBatchPath": "", + "C_Cpp_Runner.useMsvc": false, + "C_Cpp_Runner.warnings": [ + "-Wall", + "-Wextra", + "-Wpedantic", + "-Wshadow", + "-Wformat=2", + "-Wcast-align", + "-Wconversion", + "-Wsign-conversion", + "-Wnull-dereference" + ], + "C_Cpp_Runner.msvcWarnings": [ + "/W4", + "/permissive-", + "/w14242", + "/w14287", + "/w14296", + "/w14311", + "/w14826", + "/w44062", + "/w44242", + "/w14905", + "/w14906", + "/w14263", + "/w44265", + "/w14928" + ], + "C_Cpp_Runner.enableWarnings": true, + "C_Cpp_Runner.warningsAsError": false, + "C_Cpp_Runner.compilerArgs": [], + "C_Cpp_Runner.linkerArgs": [], + "C_Cpp_Runner.includePaths": [], + "C_Cpp_Runner.includeSearch": [ + "*", + "**/*" + ], + "C_Cpp_Runner.excludeSearch": [ + "**/build", + "**/build/**", + "**/.*", + "**/.*/**", + "**/.vscode", + "**/.vscode/**" + ], + "C_Cpp_Runner.useAddressSanitizer": false, + "C_Cpp_Runner.useUndefinedSanitizer": false, + "C_Cpp_Runner.useLeakSanitizer": false, + "C_Cpp_Runner.showCompilationTime": false, + "C_Cpp_Runner.useLinkTimeOptimization": false, + "C_Cpp_Runner.msvcSecureNoWarnings": false +} \ No newline at end of file From 02c6522ffbafd51583f51029724c68e99e8d8dcb Mon Sep 17 00:00:00 2001 From: JonesKisaka Date: Mon, 28 Jul 2025 14:15:43 +0300 Subject: [PATCH 07/16] remove .vscode --- targets/zephyr/m0-http/ad | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 targets/zephyr/m0-http/ad diff --git a/targets/zephyr/m0-http/ad b/targets/zephyr/m0-http/ad new file mode 100644 index 0000000..e69de29 From f8d9214ec0db608fe82e4faa4e090e641ae0c83b Mon Sep 17 00:00:00 2001 From: JonesKisaka Date: Mon, 28 Jul 2025 14:16:25 +0300 Subject: [PATCH 08/16] remove .vscode --- targets/zephyr/m0-http/ad | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 targets/zephyr/m0-http/ad diff --git a/targets/zephyr/m0-http/ad b/targets/zephyr/m0-http/ad deleted file mode 100644 index e69de29..0000000 From 642cbbdcf1a070e39ee19af9008dcfbb325c2896 Mon Sep 17 00:00:00 2001 From: JonesKisaka Date: Mon, 28 Jul 2025 14:17:23 +0300 Subject: [PATCH 09/16] remove .vscode --- .vscode/c_cpp_properties.json | 18 ----------- .vscode/launch.json | 24 -------------- .vscode/settings.json | 59 ----------------------------------- 3 files changed, 101 deletions(-) delete mode 100644 .vscode/c_cpp_properties.json delete mode 100644 .vscode/launch.json delete mode 100644 .vscode/settings.json diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json deleted file mode 100644 index c2098a2..0000000 --- a/.vscode/c_cpp_properties.json +++ /dev/null @@ -1,18 +0,0 @@ -{ - "configurations": [ - { - "name": "linux-gcc-x64", - "includePath": [ - "${workspaceFolder}/**" - ], - "compilerPath": "/usr/bin/gcc", - "cStandard": "${default}", - "cppStandard": "${default}", - "intelliSenseMode": "linux-gcc-x64", - "compilerArgs": [ - "" - ] - } - ], - "version": 4 -} \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json deleted file mode 100644 index da121fc..0000000 --- a/.vscode/launch.json +++ /dev/null @@ -1,24 +0,0 @@ -{ - "version": "0.2.0", - "configurations": [ - { - "name": "C/C++ Runner: Debug Session", - "type": "cppdbg", - "request": "launch", - "args": [], - "stopAtEntry": false, - "externalConsole": false, - "cwd": "/home/kisaka_the_jones/ULTRAVIOLET/Absmach/embedded/targets/zephyr/wifi/src", - "program": "/home/kisaka_the_jones/ULTRAVIOLET/Absmach/embedded/targets/zephyr/wifi/src/build/Debug/outDebug", - "MIMode": "gdb", - "miDebuggerPath": "gdb", - "setupCommands": [ - { - "description": "Enable pretty-printing for gdb", - "text": "-enable-pretty-printing", - "ignoreFailures": true - } - ] - } - ] -} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index 3e5eb95..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,59 +0,0 @@ -{ - "C_Cpp_Runner.cCompilerPath": "gcc", - "C_Cpp_Runner.cppCompilerPath": "g++", - "C_Cpp_Runner.debuggerPath": "gdb", - "C_Cpp_Runner.cStandard": "", - "C_Cpp_Runner.cppStandard": "", - "C_Cpp_Runner.msvcBatchPath": "", - "C_Cpp_Runner.useMsvc": false, - "C_Cpp_Runner.warnings": [ - "-Wall", - "-Wextra", - "-Wpedantic", - "-Wshadow", - "-Wformat=2", - "-Wcast-align", - "-Wconversion", - "-Wsign-conversion", - "-Wnull-dereference" - ], - "C_Cpp_Runner.msvcWarnings": [ - "/W4", - "/permissive-", - "/w14242", - "/w14287", - "/w14296", - "/w14311", - "/w14826", - "/w44062", - "/w44242", - "/w14905", - "/w14906", - "/w14263", - "/w44265", - "/w14928" - ], - "C_Cpp_Runner.enableWarnings": true, - "C_Cpp_Runner.warningsAsError": false, - "C_Cpp_Runner.compilerArgs": [], - "C_Cpp_Runner.linkerArgs": [], - "C_Cpp_Runner.includePaths": [], - "C_Cpp_Runner.includeSearch": [ - "*", - "**/*" - ], - "C_Cpp_Runner.excludeSearch": [ - "**/build", - "**/build/**", - "**/.*", - "**/.*/**", - "**/.vscode", - "**/.vscode/**" - ], - "C_Cpp_Runner.useAddressSanitizer": false, - "C_Cpp_Runner.useUndefinedSanitizer": false, - "C_Cpp_Runner.useLeakSanitizer": false, - "C_Cpp_Runner.showCompilationTime": false, - "C_Cpp_Runner.useLinkTimeOptimization": false, - "C_Cpp_Runner.msvcSecureNoWarnings": false -} \ No newline at end of file From deb1a3080a0bc00c5de492e0787dbc877eb071f3 Mon Sep 17 00:00:00 2001 From: JonesKisaka Date: Mon, 28 Jul 2025 14:26:33 +0300 Subject: [PATCH 10/16] new m0-http directory --- targets/zephyr/m0-http/jkl | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 targets/zephyr/m0-http/jkl diff --git a/targets/zephyr/m0-http/jkl b/targets/zephyr/m0-http/jkl new file mode 100644 index 0000000..e69de29 From f429851d18aaf5226b35c3a4289542ce8106307c Mon Sep 17 00:00:00 2001 From: JonesKisaka Date: Mon, 28 Jul 2025 14:27:19 +0300 Subject: [PATCH 11/16] new m0-http directory --- targets/zephyr/m0-http/jkl | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 targets/zephyr/m0-http/jkl diff --git a/targets/zephyr/m0-http/jkl b/targets/zephyr/m0-http/jkl deleted file mode 100644 index e69de29..0000000 From 2d7c28a273297cd84c8f03edcafc306329326c36 Mon Sep 17 00:00:00 2001 From: JonesKisaka Date: Mon, 28 Jul 2025 14:28:54 +0300 Subject: [PATCH 12/16] new m0-http directory --- targets/zephyr/m0-http/sdf | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 targets/zephyr/m0-http/sdf diff --git a/targets/zephyr/m0-http/sdf b/targets/zephyr/m0-http/sdf new file mode 100644 index 0000000..e69de29 From 83b46e7b6454cf87c503deba6fa47512f4735d42 Mon Sep 17 00:00:00 2001 From: JonesKisaka Date: Mon, 28 Jul 2025 14:30:21 +0300 Subject: [PATCH 13/16] new m0-http directory --- targets/zephyr/m0-http/sdf | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 targets/zephyr/m0-http/sdf diff --git a/targets/zephyr/m0-http/sdf b/targets/zephyr/m0-http/sdf deleted file mode 100644 index e69de29..0000000 From 1601f2889fdc30a59b20be095e7d874813cd7a6e Mon Sep 17 00:00:00 2001 From: JonesKisaka Date: Tue, 29 Jul 2025 16:29:03 +0300 Subject: [PATCH 14/16] Zephyr dir added --- targets/zephyr/m0-http/read.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 targets/zephyr/m0-http/read.txt diff --git a/targets/zephyr/m0-http/read.txt b/targets/zephyr/m0-http/read.txt new file mode 100644 index 0000000..2b9d1f6 --- /dev/null +++ b/targets/zephyr/m0-http/read.txt @@ -0,0 +1 @@ +m0-http From b3971f334249c70e92bebb7f9ae06aa76244abef Mon Sep 17 00:00:00 2001 From: JonesKisaka Date: Mon, 4 Aug 2025 13:14:12 +0300 Subject: [PATCH 15/16] branch cleanup --- targets/zephyr_targets/m0-mqtt/CMakeLists.txt | 11 - targets/zephyr_targets/m0-mqtt/Kconfig | 19 - targets/zephyr_targets/m0-mqtt/anonymous.conf | 5 - targets/zephyr_targets/m0-mqtt/prj.conf | 55 -- targets/zephyr_targets/m0-mqtt/src/config.h | 13 - targets/zephyr_targets/m0-mqtt/src/main.c | 586 ------------------ targets/zephyr_targets/wifi/.gitignore | 2 - targets/zephyr_targets/wifi/CMakeLists.txt | 8 - targets/zephyr_targets/wifi/README.md | 20 - targets/zephyr_targets/wifi/prj.conf | 28 - targets/zephyr_targets/wifi/src/config.h | 22 - targets/zephyr_targets/wifi/src/main.c | 168 ----- 12 files changed, 937 deletions(-) delete mode 100644 targets/zephyr_targets/m0-mqtt/CMakeLists.txt delete mode 100644 targets/zephyr_targets/m0-mqtt/Kconfig delete mode 100644 targets/zephyr_targets/m0-mqtt/anonymous.conf delete mode 100644 targets/zephyr_targets/m0-mqtt/prj.conf delete mode 100644 targets/zephyr_targets/m0-mqtt/src/config.h delete mode 100644 targets/zephyr_targets/m0-mqtt/src/main.c delete mode 100644 targets/zephyr_targets/wifi/.gitignore delete mode 100644 targets/zephyr_targets/wifi/CMakeLists.txt delete mode 100644 targets/zephyr_targets/wifi/README.md delete mode 100644 targets/zephyr_targets/wifi/prj.conf delete mode 100644 targets/zephyr_targets/wifi/src/config.h delete mode 100644 targets/zephyr_targets/wifi/src/main.c diff --git a/targets/zephyr_targets/m0-mqtt/CMakeLists.txt b/targets/zephyr_targets/m0-mqtt/CMakeLists.txt deleted file mode 100644 index df65010..0000000 --- a/targets/zephyr_targets/m0-mqtt/CMakeLists.txt +++ /dev/null @@ -1,11 +0,0 @@ -# SPDX-License-Identifier: Apache-2.0 - -cmake_minimum_required(VERSION 3.20.0) - -find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) -project(mqtt_publisher) - -FILE(GLOB app_sources src/*.c) -target_sources(app PRIVATE ${app_sources}) - -include(${ZEPHYR_BASE}/samples/net/common/common.cmake) diff --git a/targets/zephyr_targets/m0-mqtt/Kconfig b/targets/zephyr_targets/m0-mqtt/Kconfig deleted file mode 100644 index f870531..0000000 --- a/targets/zephyr_targets/m0-mqtt/Kconfig +++ /dev/null @@ -1,19 +0,0 @@ -mainmenu "Networking mqtt_publisher sample application" - -config NET_SAMPLE_APP_MAX_ITERATIONS - int "Number of times to Publish sample MQTT messages" - default 500 - help - Send sample MQTT messages this many times in a MQTT connection - before exiting. A value of zero means that the messages are sent - forever. - -config NET_SAMPLE_APP_MAX_CONNECTIONS - int "Number of times to connect to the MQTT server" - default 0 - help - Number of times to connect to the MQTT server. With each connection - send NET_SAMPLE_APP_MAX_ITERATIONS amount of MQTT sample messages. - A value of zero means to continue forever. - -source "Kconfig.zephyr" diff --git a/targets/zephyr_targets/m0-mqtt/anonymous.conf b/targets/zephyr_targets/m0-mqtt/anonymous.conf deleted file mode 100644 index 05761b8..0000000 --- a/targets/zephyr_targets/m0-mqtt/anonymous.conf +++ /dev/null @@ -1,5 +0,0 @@ -#This is the Configuration File if using Mosquitto as broker. -#mosquitto.conf -listener 1883 -allow_anonymous true -persistence true diff --git a/targets/zephyr_targets/m0-mqtt/prj.conf b/targets/zephyr_targets/m0-mqtt/prj.conf deleted file mode 100644 index 2e855c0..0000000 --- a/targets/zephyr_targets/m0-mqtt/prj.conf +++ /dev/null @@ -1,55 +0,0 @@ -CONFIG_NETWORKING=y -CONFIG_NET_SOCKETS=y -CONFIG_NET_TCP=y -CONFIG_NET_LOG=y - -#Confuguring WiFi -CONFIG_WIFI=y -CONFIG_NET_CONFIG_AUTO_INIT=n -CONFIG_NET_CONNECTION_MANAGER=y -CONFIG_NET_DHCPV4=y -CONFIG_NET_DHCPV4_SERVER=y -CONFIG_NET_IF_MAX_IPV4_COUNT=2 -CONFIG_NET_IF_MAX_IPV6_COUNT=2 -CONFIG_NET_L2_ETHERNET=y -CONFIG_NET_L2_WIFI_MGMT=y -CONFIG_NET_MGMT=y -CONFIG_NET_MGMT_EVENT=y -CONFIG_NET_MGMT_EVENT_INFO=y -CONFIG_NET_MGMT_EVENT_QUEUE_SIZE=10 -CONFIG_NET_MGMT_EVENT_STACK_SIZE=4096 -CONFIG_NET_PKT_RX_COUNT=16 -CONFIG_NET_PKT_TX_COUNT=16 -CONFIG_NET_SOCKETS_SERVICE_STACK_SIZE=4096 -CONFIG_NET_UDP=y -CONFIG_NET_DHCPV4_SERVER_LOG_LEVEL_DBG=y - -#Configuring Network -CONFIG_NET_IPV6_RA_RDNSS=y -CONFIG_NET_IF_UNICAST_IPV6_ADDR_COUNT=3 -CONFIG_NET_IF_MCAST_IPV6_ADDR_COUNT=2 -CONFIG_NET_CONFIG_SETTINGS=y -CONFIG_NET_CONFIG_MY_IPV6_ADDR="2001:db8::1" -CONFIG_NET_CONFIG_PEER_IPV6_ADDR="2001:db8::2" -CONFIG_NET_CONFIG_MY_IPV4_ADDR="192.0.2.1" -CONFIG_NET_CONFIG_PEER_IPV4_ADDR="192.168.8.102" -CONFIG_NET_IPV6=n # Enable IPv6 support -CONFIG_NET_IPV4=y # Enable IPv4 support -# For IPv6 -CONFIG_NET_BUF_DATA_SIZE=256 -CONFIG_NET_SHELL=y - - -CONFIG_PRINTK=y -CONFIG_STDOUT_CONSOLE=y -CONFIG_POSIX_API=y - - -# Enable the MQTT Lib -CONFIG_MQTT_LIB=y - -CONFIG_MAIN_STACK_SIZE=2048 - - -CONFIG_ENTROPY_GENERATOR=y -CONFIG_TEST_RANDOM_GENERATOR=y diff --git a/targets/zephyr_targets/m0-mqtt/src/config.h b/targets/zephyr_targets/m0-mqtt/src/config.h deleted file mode 100644 index 484e75a..0000000 --- a/targets/zephyr_targets/m0-mqtt/src/config.h +++ /dev/null @@ -1,13 +0,0 @@ -#ifndef __CONFIG_H__ -#define __CONFIG_H__ - -#define ZEPHYR_ADDR "192.168.1.101" -#define SERVER_ADDR "192.168.8.102" -#define SERVER_PORT 1883 -#define APP_CONNECT_TIMEOUT_MS 2000 -#define APP_SLEEP_MSECS 200 -#define APP_CONNECT_TRIES 5 -#define APP_MQTT_BUFFER_SIZE 128 -#define MQTT_CLIENTID "zephyr_publisher" - -#endif diff --git a/targets/zephyr_targets/m0-mqtt/src/main.c b/targets/zephyr_targets/m0-mqtt/src/main.c deleted file mode 100644 index 8622cce..0000000 --- a/targets/zephyr_targets/m0-mqtt/src/main.c +++ /dev/null @@ -1,586 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include - -LOG_MODULE_REGISTER(net_mqtt_publisher_sample, LOG_LEVEL_DBG); -#define MACSTR "%02X:%02X:%02X:%02X:%02X:%02X" -#define NET_EVENT_WIFI_MASK \ - (NET_EVENT_WIFI_CONNECT_RESULT | NET_EVENT_WIFI_DISCONNECT_RESULT | \ - NET_EVENT_WIFI_AP_ENABLE_RESULT | NET_EVENT_WIFI_AP_DISABLE_RESULT | \ - NET_EVENT_WIFI_AP_STA_CONNECTED | NET_EVENT_WIFI_AP_STA_DISCONNECTED) - -/* AP Mode Configuration */ -#define WIFI_AP_SSID "M0-AP" -#define WIFI_AP_PSK "" -#define WIFI_AP_IP_ADDRESS "192.168.4.1" -#define WIFI_AP_NETMASK "255.255.255.0" - -/* STA Mode Configuration */ -#define WIFI_SSID "SSID" /* Replace `SSID` with WiFi ssid. */ -#define WIFI_PSK "PASSWORD" /* Replace `PASSWORD` with Router password. */ - - -static struct net_if *ap_iface; -static struct net_if *sta_iface; - -static struct wifi_connect_req_params ap_config; -static struct wifi_connect_req_params sta_config; - -static struct net_mgmt_event_callback cb; - -#if defined(CONFIG_LOG_BACKEND_MQTT) -#include -#endif - -#include -#include - -#include "config.h" -#include "net_sample_common.h" - -#if defined(CONFIG_USERSPACE) -#include -K_APPMEM_PARTITION_DEFINE(app_partition); -struct k_mem_domain app_domain; -#define APP_BMEM K_APP_BMEM(app_partition) -#define APP_DMEM K_APP_DMEM(app_partition) -#else -#define APP_BMEM -#define APP_DMEM -#endif - -/* Buffers for MQTT client. */ -static APP_BMEM uint8_t rx_buffer[APP_MQTT_BUFFER_SIZE]; -static APP_BMEM uint8_t tx_buffer[APP_MQTT_BUFFER_SIZE]; - -#if defined(CONFIG_MQTT_LIB_WEBSOCKET) -/* Making RX buffer large enough that the full IPv6 packet can fit into it */ -#define MQTT_LIB_WEBSOCKET_RECV_BUF_LEN 1280 - -/* Websocket needs temporary buffer to store partial packets */ -static APP_BMEM uint8_t temp_ws_rx_buf[MQTT_LIB_WEBSOCKET_RECV_BUF_LEN]; -#endif - -/* The mqtt client struct */ -static APP_BMEM struct mqtt_client client_ctx; - -/* MQTT Broker details. */ -static APP_BMEM struct sockaddr_storage broker; -#define BROKER "BROKER_IP" -#define BROKER_PORT 1883 /*Change to your broker port */ - -#if defined(CONFIG_SOCKS) -static APP_BMEM struct sockaddr socks5_proxy; -#endif - -static APP_BMEM struct pollfd fds[1]; -static APP_BMEM int nfds; -static APP_BMEM bool connected; - -/* Whether to include full topic in the publish message*/ -static APP_BMEM bool include_topic; -static APP_BMEM bool aliases_enabled; - -#define APP_TOPIC_ALIAS 1 - /* CONFIG_MQTT_LIB_TLS */ -static void wifi_event_handler(struct net_mgmt_event_callback *cb, uint64_t mgmt_event, - struct net_if *iface) -{ - switch (mgmt_event) { - case NET_EVENT_WIFI_CONNECT_RESULT: { - LOG_INF("Connected to %s", WIFI_SSID); - break; - } - case NET_EVENT_WIFI_DISCONNECT_RESULT: { - LOG_INF("Disconnected from %s", WIFI_SSID); - break; - } - case NET_EVENT_WIFI_AP_ENABLE_RESULT: { - LOG_INF("AP Mode is enabled. Waiting for station to connect"); - break; - } - case NET_EVENT_WIFI_AP_DISABLE_RESULT: { - LOG_INF("AP Mode is disabled."); - break; - } - case NET_EVENT_WIFI_AP_STA_CONNECTED: { - struct wifi_ap_sta_info *sta_info = (struct wifi_ap_sta_info *)cb->info; - - LOG_INF("station: " MACSTR " joined ", sta_info->mac[0], sta_info->mac[1], - sta_info->mac[2], sta_info->mac[3], sta_info->mac[4], sta_info->mac[5]); - break; - } - case NET_EVENT_WIFI_AP_STA_DISCONNECTED: { - struct wifi_ap_sta_info *sta_info = (struct wifi_ap_sta_info *)cb->info; - - LOG_INF("station: " MACSTR " leave ", sta_info->mac[0], sta_info->mac[1], - sta_info->mac[2], sta_info->mac[3], sta_info->mac[4], sta_info->mac[5]); - break; - } - default: - break; - } -} - -static void enable_dhcpv4_server(void) -{ - static struct in_addr addr; - static struct in_addr netmaskAddr; - - if (net_addr_pton(AF_INET, WIFI_AP_IP_ADDRESS, &addr)) { - LOG_ERR("Invalid address: %s", WIFI_AP_IP_ADDRESS); - return; - } - - if (net_addr_pton(AF_INET, WIFI_AP_NETMASK, &netmaskAddr)) { - LOG_ERR("Invalid netmask: %s", WIFI_AP_NETMASK); - return; - } - - net_if_ipv4_set_gw(ap_iface, &addr); - - if (net_if_ipv4_addr_add(ap_iface, &addr, NET_ADDR_MANUAL, 0) == NULL) { - LOG_ERR("unable to set IP address for AP interface"); - } - - if (!net_if_ipv4_set_netmask_by_addr(ap_iface, &addr, &netmaskAddr)) { - LOG_ERR("Unable to set netmask for AP interface: %s", WIFI_AP_NETMASK); - } - - addr.s4_addr[3] += 10; /* Starting IPv4 address for DHCPv4 address pool. */ - - if (net_dhcpv4_server_start(ap_iface, &addr) != 0) { - LOG_ERR("DHCP server is not started for desired IP"); - return; - } - - LOG_INF("DHCPv4 server started...\n"); -} - -static int enable_ap_mode(void) -{ - if (!ap_iface) { - LOG_INF("AP: is not initialized"); - return -EIO; - } - - LOG_INF("Turning on AP Mode"); - ap_config.ssid = (const uint8_t *)WIFI_AP_SSID; - ap_config.ssid_length = strlen(WIFI_AP_SSID); - ap_config.psk = (const uint8_t *)WIFI_AP_PSK; - ap_config.psk_length = strlen(WIFI_AP_PSK); - ap_config.channel = WIFI_CHANNEL_ANY; - ap_config.band = WIFI_FREQ_BAND_2_4_GHZ; - - if (strlen(WIFI_AP_PSK) == 0) { - ap_config.security = WIFI_SECURITY_TYPE_NONE; - } else { - - ap_config.security = WIFI_SECURITY_TYPE_PSK; - } - - enable_dhcpv4_server(); - - int ret = net_mgmt(NET_REQUEST_WIFI_AP_ENABLE, ap_iface, &ap_config, - sizeof(struct wifi_connect_req_params)); - if (ret) { - LOG_ERR("NET_REQUEST_WIFI_AP_ENABLE failed, err: %d", ret); - } - - return ret; -} - -static int connect_to_wifi(void) -{ - if (!sta_iface) { - LOG_INF("STA: interface no initialized"); - return -EIO; - } - - sta_config.ssid = (const uint8_t *)WIFI_SSID; - sta_config.ssid_length = strlen(WIFI_SSID); - sta_config.psk = (const uint8_t *)WIFI_PSK; - sta_config.psk_length = strlen(WIFI_PSK); - sta_config.security = WIFI_SECURITY_TYPE_PSK; - sta_config.channel = WIFI_CHANNEL_ANY; - sta_config.band = WIFI_FREQ_BAND_2_4_GHZ; - - LOG_INF("Connecting to SSID: %s\n", sta_config.ssid); - - int ret = net_mgmt(NET_REQUEST_WIFI_CONNECT, sta_iface, &sta_config, - sizeof(struct wifi_connect_req_params)); - if (ret) { - LOG_ERR("Unable to Connect to (%s)", WIFI_SSID); - } - - return ret; -} - -static void prepare_fds(struct mqtt_client *client) -{ - if (client->transport.type == MQTT_TRANSPORT_NON_SECURE) { - fds[0].fd = client->transport.tcp.sock; - } - - fds[0].events = POLLIN; - nfds = 1; -} - -static void clear_fds(void) -{ - nfds = 0; -} - -static int wait(int timeout) -{ - int ret = 0; - - if (nfds > 0) { - ret = poll(fds, nfds, timeout); - if (ret < 0) { - LOG_ERR("poll error: %d", errno); - } - } - - return ret; -} - -void mqtt_evt_handler(struct mqtt_client *const client, - 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; - } - - connected = true; - LOG_INF("MQTT client connected!"); - - break; - - case MQTT_EVT_DISCONNECT: - LOG_INF("MQTT client disconnected %d", evt->result); - - connected = false; - clear_fds(); - - 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_PUBREC: - if (evt->result != 0) { - LOG_ERR("MQTT PUBREC error %d", evt->result); - break; - } - - LOG_INF("PUBREC packet id: %u", evt->param.pubrec.message_id); - - const struct mqtt_pubrel_param rel_param = { - .message_id = evt->param.pubrec.message_id - }; - - err = mqtt_publish_qos2_release(client, &rel_param); - if (err != 0) { - LOG_ERR("Failed to send MQTT PUBREL: %d", err); - } - - break; - - case MQTT_EVT_PUBCOMP: - if (evt->result != 0) { - LOG_ERR("MQTT PUBCOMP error %d", evt->result); - break; - } - - LOG_INF("PUBCOMP packet id: %u", - evt->param.pubcomp.message_id); - - break; - - case MQTT_EVT_PINGRESP: - LOG_INF("PINGRESP packet"); - break; - - default: - break; - } -} - -static char *get_mqtt_payload(enum mqtt_qos qos) -{ - static APP_DMEM char payload[] = "Hello World. Its M0 here!"; - - payload[strlen(payload) - 1] = '0' + qos; - - return payload; -} - -static char *get_mqtt_topic(void) -{ - return "sensors"; -} - -static int publish(struct mqtt_client *client, enum mqtt_qos qos) -{ - struct mqtt_publish_param param = { 0 }; - - /* Always true for MQTT 3.1.1. - * True only on first publish message for MQTT 5.0 if broker allows aliases. - */ - if (include_topic) { - param.message.topic.topic.utf8 = (uint8_t *)get_mqtt_topic(); - param.message.topic.topic.size = - strlen(param.message.topic.topic.utf8); - } - - param.message.topic.qos = qos; - param.message.payload.data = get_mqtt_payload(qos); - param.message.payload.len = - strlen(param.message.payload.data); - param.message_id = sys_rand16_get(); - param.dup_flag = 0U; - param.retain_flag = 0U; - - return mqtt_publish(client, ¶m); -} - -#define RC_STR(rc) ((rc) == 0 ? "OK" : "ERROR") - -#define PRINT_RESULT(func, rc) \ - LOG_INF("%s: %d <%s>", (func), rc, RC_STR(rc)) - -static void broker_init(void) -{ - struct sockaddr_in *broker4 = (struct sockaddr_in *)&broker; - - broker4->sin_family = AF_INET; - broker4->sin_port = htons(BROKER_PORT); - inet_pton(AF_INET, BROKER, &broker4->sin_addr); -} - -static void client_init(struct mqtt_client *client) -{ - LOG_INF("CLient Initialization!"); - mqtt_client_init(client); - LOG_INF("CLient Initialized!"); - k_sleep(K_SECONDS(5)); ////////////// - LOG_INF("Broker Initialization!"); - broker_init(); - LOG_INF("Broker Initialized!"); - - /* MQTT client configuration */ - client->broker = &broker; - client->evt_cb = mqtt_evt_handler; - client->client_id.utf8 = (uint8_t *)MQTT_CLIENTID; - client->client_id.size = strlen(MQTT_CLIENTID); - client->password = NULL; - client->user_name = NULL; - client->protocol_version = MQTT_VERSION_3_1_1; - - LOG_INF("MQTT buffers configuration"); - /* 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 */ - client->transport.type = MQTT_TRANSPORT_NON_SECURE; -} - -/* In this routine we block until the connected variable is 1 */ -static int try_to_connect(struct mqtt_client *client) -{ - int rc, i = 0; - - while (i++ < APP_CONNECT_TRIES && !connected) { - - client_init(client); - - rc = mqtt_connect(client); - if (rc != 0) { - PRINT_RESULT("mqtt_connect", rc); - k_sleep(K_MSEC(APP_SLEEP_MSECS)); - continue; - } - - prepare_fds(client); - - if (wait(APP_CONNECT_TIMEOUT_MS)) { - mqtt_input(client); - } - - if (!connected) { - mqtt_abort(client); - } - } - - if (connected) { - return 0; - } - - return -EINVAL; -} - -static int process_mqtt_and_sleep(struct mqtt_client *client, int timeout) -{ - int64_t remaining = timeout; - int64_t start_time = k_uptime_get(); - int rc; - - while (remaining > 0 && connected) { - if (wait(remaining)) { - rc = mqtt_input(client); - if (rc != 0) { - PRINT_RESULT("mqtt_input", rc); - return rc; - } - } - - rc = mqtt_live(client); - if (rc != 0 && rc != -EAGAIN) { - PRINT_RESULT("mqtt_live", rc); - return rc; - } else if (rc == 0) { - rc = mqtt_input(client); - if (rc != 0) { - PRINT_RESULT("mqtt_input", rc); - return rc; - } - } - - remaining = timeout + start_time - k_uptime_get(); - } - - return 0; -} - -#define SUCCESS_OR_EXIT(rc) { if (rc != 0) { return 1; } } -#define SUCCESS_OR_BREAK(rc) { if (rc != 0) { break; } } - -static int publisher(void) -{ - int i, rc, r = 0; - - include_topic = true; - aliases_enabled = false; - - LOG_INF("attempting to connect: "); - rc = try_to_connect(&client_ctx); - PRINT_RESULT("try_to_connect", rc); - SUCCESS_OR_EXIT(rc); - - i = 0; - while (i++ < CONFIG_NET_SAMPLE_APP_MAX_ITERATIONS && connected) { - r = -1; - - rc = mqtt_ping(&client_ctx); - PRINT_RESULT("mqtt_ping", rc); - SUCCESS_OR_BREAK(rc); - - rc = process_mqtt_and_sleep(&client_ctx, APP_SLEEP_MSECS); - SUCCESS_OR_BREAK(rc); - - rc = publish(&client_ctx, MQTT_QOS_0_AT_MOST_ONCE); - PRINT_RESULT("mqtt_publish", rc); - SUCCESS_OR_BREAK(rc); - - rc = process_mqtt_and_sleep(&client_ctx, APP_SLEEP_MSECS); - SUCCESS_OR_BREAK(rc); - - rc = publish(&client_ctx, MQTT_QOS_1_AT_LEAST_ONCE); - PRINT_RESULT("mqtt_publish", rc); - SUCCESS_OR_BREAK(rc); - - rc = process_mqtt_and_sleep(&client_ctx, APP_SLEEP_MSECS); - SUCCESS_OR_BREAK(rc); - - rc = publish(&client_ctx, MQTT_QOS_2_EXACTLY_ONCE); - PRINT_RESULT("mqtt_publish", rc); - SUCCESS_OR_BREAK(rc); - - rc = process_mqtt_and_sleep(&client_ctx, APP_SLEEP_MSECS); - SUCCESS_OR_BREAK(rc); - - r = 0; - } - - rc = mqtt_disconnect(&client_ctx, NULL); - PRINT_RESULT("mqtt_disconnect", rc); - - LOG_INF("Bye!"); - - return r; -} - -static int start_app(void) -{ - int r = 0, i = 0; - - while (!CONFIG_NET_SAMPLE_APP_MAX_CONNECTIONS || - i++ < CONFIG_NET_SAMPLE_APP_MAX_CONNECTIONS) { - r = publisher(); - - if (!CONFIG_NET_SAMPLE_APP_MAX_CONNECTIONS) { - k_sleep(K_MSEC(500)); - } - } - - return r; -} - -#if defined(CONFIG_USERSPACE) -#define STACK_SIZE 2048 - -#if defined(CONFIG_NET_TC_THREAD_COOPERATIVE) -#define THREAD_PRIORITY K_PRIO_COOP(CONFIG_NUM_COOP_PRIORITIES - 1) -#else -#define THREAD_PRIORITY K_PRIO_PREEMPT(8) -#endif - -K_THREAD_DEFINE(app_thread, STACK_SIZE, - start_app, NULL, NULL, NULL, - THREAD_PRIORITY, K_USER, -1); - -static K_HEAP_DEFINE(app_mem_pool, 1024 * 2); -#endif - -int main(void) -{ - //wait_for_network(); - k_sleep(K_SECONDS(5)); - - net_mgmt_init_event_callback(&cb, wifi_event_handler, NET_EVENT_WIFI_MASK); - net_mgmt_add_event_callback(&cb); - - /* Get AP interface in AP-STA mode. */ - ap_iface = net_if_get_wifi_sap(); - - /* Get STA interface in AP-STA mode. */ - sta_iface = net_if_get_wifi_sta(); - - enable_ap_mode(); - connect_to_wifi(); - - k_sleep(K_SECONDS(15)); - exit(start_app()); - return 0; -} diff --git a/targets/zephyr_targets/wifi/.gitignore b/targets/zephyr_targets/wifi/.gitignore deleted file mode 100644 index d325909..0000000 --- a/targets/zephyr_targets/wifi/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -README.rst -socs/ diff --git a/targets/zephyr_targets/wifi/CMakeLists.txt b/targets/zephyr_targets/wifi/CMakeLists.txt deleted file mode 100644 index 2a074d9..0000000 --- a/targets/zephyr_targets/wifi/CMakeLists.txt +++ /dev/null @@ -1,8 +0,0 @@ -# SPDX-License-Identifier: Apache-2.0 - -cmake_minimum_required(VERSION 3.20.0) - -find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) -project(wifi) - -target_sources(app PRIVATE src/main.c) diff --git a/targets/zephyr_targets/wifi/README.md b/targets/zephyr_targets/wifi/README.md deleted file mode 100644 index 70620d0..0000000 --- a/targets/zephyr_targets/wifi/README.md +++ /dev/null @@ -1,20 +0,0 @@ - WiFi- M0 (ESP32C6) target -## Requirements -1. Mainflux broker details including: hostname, ThingID, Thing Credentials and Channel ID -2. [Zephyr](https://www.zephyrproject.org/) - - -## Configure -1. Edit the [config file](/config.h) with your WiFi AP/STA details. - -## Build -The project can be built by utilising the make file within the target directory - -```bash -west build -p always -b wifi -``` -## Flash -Platform io generate a build directory with the fimware.bin within it. Use the make command to flash to board -```bash -west flash -``` \ No newline at end of file diff --git a/targets/zephyr_targets/wifi/prj.conf b/targets/zephyr_targets/wifi/prj.conf deleted file mode 100644 index 632f1af..0000000 --- a/targets/zephyr_targets/wifi/prj.conf +++ /dev/null @@ -1,28 +0,0 @@ -# Wi-Fi Configuration -CONFIG_WIFI=y - -# Network Configuration -CONFIG_NET_CONFIG_AUTO_INIT=n -CONFIG_NET_CONNECTION_MANAGER=y -CONFIG_NET_DHCPV4=y -CONFIG_NET_DHCPV4_SERVER=y -CONFIG_NET_IF_MAX_IPV4_COUNT=2 -CONFIG_NET_IF_MAX_IPV6_COUNT=2 -CONFIG_NET_IPV4=y -CONFIG_NET_L2_ETHERNET=y -CONFIG_NET_L2_WIFI_MGMT=y -CONFIG_NET_MGMT=y -CONFIG_NET_MGMT_EVENT=y -CONFIG_NET_MGMT_EVENT_INFO=y -CONFIG_NET_MGMT_EVENT_QUEUE_SIZE=10 -CONFIG_NET_MGMT_EVENT_STACK_SIZE=4096 -CONFIG_NET_PKT_RX_COUNT=16 -CONFIG_NET_PKT_TX_COUNT=16 -CONFIG_NET_SOCKETS_SERVICE_STACK_SIZE=4096 -CONFIG_NET_TCP=y -CONFIG_NET_UDP=y -CONFIG_NETWORKING=y - -# LOG Configuration -CONFIG_NET_LOG=y -CONFIG_NET_DHCPV4_SERVER_LOG_LEVEL_DBG=y diff --git a/targets/zephyr_targets/wifi/src/config.h b/targets/zephyr_targets/wifi/src/config.h deleted file mode 100644 index 178384c..0000000 --- a/targets/zephyr_targets/wifi/src/config.h +++ /dev/null @@ -1,22 +0,0 @@ -#ifndef CONFIG_H -#define CONFIG_H - -#define MACSTR "%02X:%02X:%02X:%02X:%02X:%02X" - -#define NET_EVENT_WIFI_MASK \ - (NET_EVENT_WIFI_CONNECT_RESULT | NET_EVENT_WIFI_DISCONNECT_RESULT | \ - NET_EVENT_WIFI_AP_ENABLE_RESULT | NET_EVENT_WIFI_AP_DISABLE_RESULT | \ - NET_EVENT_WIFI_AP_STA_CONNECTED | NET_EVENT_WIFI_AP_STA_DISCONNECTED) - -/* AP Mode Configuration */ -#define WIFI_AP_SSID "M0-AP" -#define WIFI_AP_PSK "" -#define WIFI_AP_IP_ADDRESS "192.168.4.1" -#define WIFI_AP_NETMASK "255.255.255.0" - -/* STA Mode Configuration */ -#define WIFI_SSID "SSID" /* Replace `SSID` with WiFi ssid. */ -#define WIFI_PSK "PASSWORD" /* Replace `PASSWORD` with Router password. */ - - -#endif \ No newline at end of file diff --git a/targets/zephyr_targets/wifi/src/main.c b/targets/zephyr_targets/wifi/src/main.c deleted file mode 100644 index 15e5e79..0000000 --- a/targets/zephyr_targets/wifi/src/main.c +++ /dev/null @@ -1,168 +0,0 @@ -#include -#include -#include -#include - -#include "config.h" - -LOG_MODULE_REGISTER(MAIN); - -static struct net_if *ap_iface; -static struct net_if *sta_iface; - -static struct wifi_connect_req_params ap_config; -static struct wifi_connect_req_params sta_config; - -static struct net_mgmt_event_callback cb; - -static void wifi_event_handler(struct net_mgmt_event_callback *cb, uint64_t mgmt_event, - struct net_if *iface) -{ - switch (mgmt_event) { - case NET_EVENT_WIFI_CONNECT_RESULT: { - LOG_INF("Connected to %s", WIFI_SSID); - break; - } - case NET_EVENT_WIFI_DISCONNECT_RESULT: { - LOG_INF("Disconnected from %s", WIFI_SSID); - break; - } - case NET_EVENT_WIFI_AP_ENABLE_RESULT: { - LOG_INF("AP Mode is enabled. Waiting for station to connect"); - break; - } - case NET_EVENT_WIFI_AP_DISABLE_RESULT: { - LOG_INF("AP Mode is disabled."); - break; - } - case NET_EVENT_WIFI_AP_STA_CONNECTED: { - struct wifi_ap_sta_info *sta_info = (struct wifi_ap_sta_info *)cb->info; - - LOG_INF("station: " MACSTR " joined ", sta_info->mac[0], sta_info->mac[1], - sta_info->mac[2], sta_info->mac[3], sta_info->mac[4], sta_info->mac[5]); - break; - } - case NET_EVENT_WIFI_AP_STA_DISCONNECTED: { - struct wifi_ap_sta_info *sta_info = (struct wifi_ap_sta_info *)cb->info; - - LOG_INF("station: " MACSTR " leave ", sta_info->mac[0], sta_info->mac[1], - sta_info->mac[2], sta_info->mac[3], sta_info->mac[4], sta_info->mac[5]); - break; - } - default: - break; - } -} - -static void enable_dhcpv4_server(void) -{ - static struct in_addr addr; - static struct in_addr netmaskAddr; - - if (net_addr_pton(AF_INET, WIFI_AP_IP_ADDRESS, &addr)) { - LOG_ERR("Invalid address: %s", WIFI_AP_IP_ADDRESS); - return; - } - - if (net_addr_pton(AF_INET, WIFI_AP_NETMASK, &netmaskAddr)) { - LOG_ERR("Invalid netmask: %s", WIFI_AP_NETMASK); - return; - } - - net_if_ipv4_set_gw(ap_iface, &addr); - - if (net_if_ipv4_addr_add(ap_iface, &addr, NET_ADDR_MANUAL, 0) == NULL) { - LOG_ERR("unable to set IP address for AP interface"); - } - - if (!net_if_ipv4_set_netmask_by_addr(ap_iface, &addr, &netmaskAddr)) { - LOG_ERR("Unable to set netmask for AP interface: %s", WIFI_AP_NETMASK); - } - - addr.s4_addr[3] += 10; /* Starting IPv4 address for DHCPv4 address pool. */ - - if (net_dhcpv4_server_start(ap_iface, &addr) != 0) { - LOG_ERR("DHCP server is not started for desired IP"); - return; - } - - LOG_INF("DHCPv4 server started...\n"); -} - -static int enable_ap_mode(void) -{ - if (!ap_iface) { - LOG_INF("AP: is not initialized"); - return -EIO; - } - - LOG_INF("Turning on AP Mode"); - ap_config.ssid = (const uint8_t *)WIFI_AP_SSID; - ap_config.ssid_length = strlen(WIFI_AP_SSID); - ap_config.psk = (const uint8_t *)WIFI_AP_PSK; - ap_config.psk_length = strlen(WIFI_AP_PSK); - ap_config.channel = WIFI_CHANNEL_ANY; - ap_config.band = WIFI_FREQ_BAND_2_4_GHZ; - - if (strlen(WIFI_AP_PSK) == 0) { - ap_config.security = WIFI_SECURITY_TYPE_NONE; - } else { - - ap_config.security = WIFI_SECURITY_TYPE_PSK; - } - - enable_dhcpv4_server(); - - int ret = net_mgmt(NET_REQUEST_WIFI_AP_ENABLE, ap_iface, &ap_config, - sizeof(struct wifi_connect_req_params)); - if (ret) { - LOG_ERR("NET_REQUEST_WIFI_AP_ENABLE failed, err: %d", ret); - } - - return ret; -} - -static int connect_to_wifi(void) -{ - if (!sta_iface) { - LOG_INF("STA: interface no initialized"); - return -EIO; - } - - sta_config.ssid = (const uint8_t *)WIFI_SSID; - sta_config.ssid_length = strlen(WIFI_SSID); - sta_config.psk = (const uint8_t *)WIFI_PSK; - sta_config.psk_length = strlen(WIFI_PSK); - sta_config.security = WIFI_SECURITY_TYPE_PSK; - sta_config.channel = WIFI_CHANNEL_ANY; - sta_config.band = WIFI_FREQ_BAND_2_4_GHZ; - - LOG_INF("Connecting to SSID: %s\n", sta_config.ssid); - - int ret = net_mgmt(NET_REQUEST_WIFI_CONNECT, sta_iface, &sta_config, - sizeof(struct wifi_connect_req_params)); - if (ret) { - LOG_ERR("Unable to Connect to (%s)", WIFI_SSID); - } - - return ret; -} - -int main(void) -{ - k_sleep(K_SECONDS(5)); - - net_mgmt_init_event_callback(&cb, wifi_event_handler, NET_EVENT_WIFI_MASK); - net_mgmt_add_event_callback(&cb); - - /* Get AP interface in AP-STA mode. */ - ap_iface = net_if_get_wifi_sap(); - - /* Get STA interface in AP-STA mode. */ - sta_iface = net_if_get_wifi_sta(); - - enable_ap_mode(); - connect_to_wifi(); - - return 0; -} From ae7cbcc284888dbdb7f71c6690fa2841ad961f2e Mon Sep 17 00:00:00 2001 From: Kisaka the Jones <85192767+JonesKisaka@users.noreply.github.com> Date: Mon, 11 Aug 2025 17:21:39 +0300 Subject: [PATCH 16/16] updated code for test. --- targets/zephyr/m0-http/CMakeLists.txt | 6 + targets/zephyr/m0-http/prj.conf | 53 +++++ targets/zephyr/m0-http/src/main.c | 307 ++++++++++++++++++++++++++ 3 files changed, 366 insertions(+) create mode 100644 targets/zephyr/m0-http/CMakeLists.txt create mode 100644 targets/zephyr/m0-http/prj.conf create mode 100644 targets/zephyr/m0-http/src/main.c diff --git a/targets/zephyr/m0-http/CMakeLists.txt b/targets/zephyr/m0-http/CMakeLists.txt new file mode 100644 index 0000000..3ff1fb8 --- /dev/null +++ b/targets/zephyr/m0-http/CMakeLists.txt @@ -0,0 +1,6 @@ +cmake_minimum_required(VERSION 3.20.0) +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) +project(http_client) + +FILE(GLOB app_sources src/*.c) +target_sources(app PRIVATE ${app_sources}) diff --git a/targets/zephyr/m0-http/prj.conf b/targets/zephyr/m0-http/prj.conf new file mode 100644 index 0000000..5233e6a --- /dev/null +++ b/targets/zephyr/m0-http/prj.conf @@ -0,0 +1,53 @@ +# General config +CONFIG_MAIN_STACK_SIZE=3072 +CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=2048 +CONFIG_WIFI=y + +# Networking config +CONFIG_NETWORKING=y +CONFIG_NET_IPV4=y +CONFIG_NET_TCP=y +# CONFIG_NET_SHELL=n +CONFIG_NET_CONFIG_AUTO_INIT=n +CONFIG_NET_CONNECTION_MANAGER=y +CONFIG_NET_DHCPV4=n +CONFIG_NET_DHCPV4_SERVER=n +CONFIG_NET_L2_ETHERNET=y +CONFIG_NET_L2_WIFI_MGMT=y +CONFIG_NET_MGMT=y +CONFIG_NET_MGMT_EVENT=y +CONFIG_NET_MGMT_EVENT_INFO=y +CONFIG_NET_MGMT_EVENT_QUEUE_SIZE=10 +CONFIG_NET_MGMT_EVENT_STACK_SIZE=4096 +CONFIG_NET_PKT_RX_COUNT=16 +CONFIG_NET_PKT_TX_COUNT=16 +CONFIG_NET_SOCKETS_SERVICE_STACK_SIZE=4096 +CONFIG_NET_UDP=y + +# Sockets +CONFIG_NET_SOCKETS=y +CONFIG_ZVFS_POLL_MAX=4 +CONFIG_POSIX_API=y + +# Network driver config +CONFIG_TEST_RANDOM_GENERATOR=y + +# Network buffers +CONFIG_NET_PKT_RX_COUNT=16 +CONFIG_NET_PKT_TX_COUNT=16 +CONFIG_NET_BUF_RX_COUNT=128 +CONFIG_NET_BUF_TX_COUNT=128 +CONFIG_NET_CONTEXT_NET_PKT_POOL=y + +# Network config +CONFIG_NET_CONFIG_NEED_IPV4=y + +# HTTP +CONFIG_HTTP_CLIENT=y + +# Network debug config +CONFIG_LOG=y +CONFIG_NET_LOG=y +CONFIG_NET_DHCPV4_SERVER_LOG_LEVEL_DBG=y +CONFIG_NET_SOCKETS_LOG_LEVEL_DBG=n +CONFIG_NET_HTTP_LOG_LEVEL_DBG=n diff --git a/targets/zephyr/m0-http/src/main.c b/targets/zephyr/m0-http/src/main.c new file mode 100644 index 0000000..488a19f --- /dev/null +++ b/targets/zephyr/m0-http/src/main.c @@ -0,0 +1,307 @@ +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + + + +LOG_MODULE_REGISTER(http_client, LOG_LEVEL_DBG); + +/*WiFi configuration*/ +#define MACSTR "%02X:%02X:%02X:%02X:%02X:%02X" +#define NET_EVENT_WIFI_MASK \ + (NET_EVENT_WIFI_CONNECT_RESULT | NET_EVENT_WIFI_DISCONNECT_RESULT | \ + NET_EVENT_WIFI_AP_ENABLE_RESULT | NET_EVENT_WIFI_AP_DISABLE_RESULT | \ + NET_EVENT_WIFI_AP_STA_CONNECTED | NET_EVENT_WIFI_AP_STA_DISCONNECTED) + +/* Magistrala Credentials */ +#define DOMAIN_ID "" /* Replace with your actual domain ID */ +#define CHANNEL_ID "" /* Replace with your actual channel ID */ +#define CLIENT_SECRET "" /* Replace with your actual client secret */ + +/* STA Mode Configuration */ +#define WIFI_SSID "" /* Replace `SSID` with WiFi ssid. */ +#define WIFI_PSK "" /* Replace `PASSWORD` with Router password. */ + +#define HTTP_PORT 8008 /* supermq-http-adapter */ +#define MAGISTRALA_IP "" /* Replace with the actual IP address of the Magistrala server */ +#define MAX_RECV_BUF_LEN 1024 + +#define CONFIG_NET_SAMPLE_SEND_ITERATIONS 5 + +static const char *payload = + "[" + " {\"bn\": \"some-base-name:\", \"bt\": 1.276020076001e+09, \"bu\": \"A\", \"bver\": 5, \"n\": \"voltage\", \"u\": \"V\", \"v\": 120.1}," + " {\"n\": \"current\", \"t\": -5, \"v\": 1.2}," + " {\"n\": \"current\", \"t\": -4, \"v\": 1.3}" + "]"; + +static uint8_t recv_buf_ipv4[MAX_RECV_BUF_LEN]; +static struct net_if *sta_iface; + +static struct wifi_connect_req_params sta_config; + +static struct net_mgmt_event_callback cb; + +void set_static_ip(void) +{ + struct net_if *iface = net_if_get_default(); + + struct in_addr static_ip; + struct in_addr netmask; + struct in_addr gw; + + // Set IP: 192.168.1.100 + net_addr_pton(AF_INET, "192.168.8.112", &static_ip); + + // Set Netmask: 255.255.255.0 + net_addr_pton(AF_INET, "255.255.255.0", &netmask); + + // Set Gateway: 192.168.1.1 + net_addr_pton(AF_INET, "192.168.1.1", &gw); + + net_if_ipv4_addr_add(iface, &static_ip, NET_ADDR_MANUAL, 0); + net_if_ipv4_set_netmask(iface, &netmask); + net_if_ipv4_set_gw(iface, &gw); + + LOG_INF("Static IP configured"); +} + +static void wifi_event_handler(struct net_mgmt_event_callback *cb, uint64_t mgmt_event, + struct net_if *iface) +{ + switch (mgmt_event) { + case NET_EVENT_WIFI_CONNECT_RESULT: { + LOG_INF("Connected to %s", WIFI_SSID); + break; + } + case NET_EVENT_WIFI_DISCONNECT_RESULT: { + LOG_INF("Disconnected from %s", WIFI_SSID); + break; + } + case NET_EVENT_WIFI_AP_ENABLE_RESULT: { + LOG_INF("AP Mode is enabled. Waiting for station to connect"); + break; + } + case NET_EVENT_WIFI_AP_DISABLE_RESULT: { + LOG_INF("AP Mode is disabled."); + break; + } + case NET_EVENT_WIFI_AP_STA_CONNECTED: { + struct wifi_ap_sta_info *sta_info = (struct wifi_ap_sta_info *)cb->info; + + LOG_INF("station: " MACSTR " joined ", sta_info->mac[0], sta_info->mac[1], + sta_info->mac[2], sta_info->mac[3], sta_info->mac[4], sta_info->mac[5]); + break; + } + case NET_EVENT_WIFI_AP_STA_DISCONNECTED: { + struct wifi_ap_sta_info *sta_info = (struct wifi_ap_sta_info *)cb->info; + + LOG_INF("station: " MACSTR " leave ", sta_info->mac[0], sta_info->mac[1], + sta_info->mac[2], sta_info->mac[3], sta_info->mac[4], sta_info->mac[5]); + break; + } + default: + break; + } +} + +static int connect_to_wifi(void) +{ + if (!sta_iface) { + LOG_INF("STA: interface no initialized"); + return -EIO; + } + + sta_config.ssid = (const uint8_t *)WIFI_SSID; + sta_config.ssid_length = strlen(WIFI_SSID); + sta_config.psk = (const uint8_t *)WIFI_PSK; + sta_config.psk_length = strlen(WIFI_PSK); + sta_config.security = WIFI_SECURITY_TYPE_PSK; + sta_config.channel = WIFI_CHANNEL_ANY; + sta_config.band = WIFI_FREQ_BAND_2_4_GHZ; + + LOG_INF("Connecting to SSID: %s\n", sta_config.ssid); + + int ret = net_mgmt(NET_REQUEST_WIFI_CONNECT, sta_iface, &sta_config, + sizeof(struct wifi_connect_req_params)); + if (ret) { + LOG_ERR("Unable to Connect to (%s)", WIFI_SSID); + } + + return ret; +} + +static int setup_socket(sa_family_t family, const char *server, int port, + int *sock, struct sockaddr *addr, socklen_t addr_len) +{ + const char *family_str = family == AF_INET; + int ret = 0; + + memset(addr, 0, addr_len); + + net_sin(addr)->sin_family = AF_INET; + net_sin(addr)->sin_port = htons(port); + inet_pton(family, server, &net_sin(addr)->sin_addr); + + *sock = socket(family, SOCK_STREAM, IPPROTO_TCP); + + if (*sock < 0) { + LOG_ERR("Failed to create %s HTTP socket (%d)", family_str, + -errno); + } + + LOG_INF("Socket created successfully. Socket FD: %d", *sock); + LOG_INF("Socket ret is %d", ret); + return ret; +} + +static int response_cb(struct http_response *rsp, + enum http_final_call final_data, + void *user_data) +{ + if (final_data == HTTP_DATA_MORE) { + LOG_INF("Partial data received (%zd bytes)", rsp->data_len); + } else if (final_data == HTTP_DATA_FINAL) { + LOG_INF("All the data received (%zd bytes)", rsp->data_len); + } + + LOG_INF("Response to %s", (const char *)user_data); + LOG_INF("Response status %s", rsp->http_status); + + return 0; +} + +static int connect_socket(sa_family_t family, const char *server, int port, + int *sock, struct sockaddr *addr, socklen_t addr_len) +{ + int ret; + + ret = setup_socket(family, server, port, sock, addr, addr_len); + LOG_INF("setup_socket() ret is %d", ret); + + if (ret < 0 || *sock < 0) { + return -1; + } + LOG_INF("Socket created successfully"); + LOG_INF("Connecting to %s:%d", server, port); + + ret = connect(*sock, addr, addr_len); + LOG_INF("connect() ret is %d", ret); + + if (ret < 0) { + LOG_ERR("Cannot connect to %s remote (%d)", + family == AF_INET ? "IPv4" : "IPv6", + -errno); + close(*sock); + *sock = -1; + ret = -errno; + } + + return ret; +} + +static int run_queries(void) +{ + LOG_INF("Running HTTP POST queries..."); + struct sockaddr_in addr4; //ipv4 socket address + int sock4 = -1; + int32_t timeout = 3 * MSEC_PER_SEC; + int ret = 0; + int port = HTTP_PORT; + + if (IS_ENABLED(CONFIG_NET_IPV4)) { + (void)connect_socket(AF_INET, MAGISTRALA_IP, port, + &sock4, (struct sockaddr *)&addr4, + sizeof(addr4)); + } + + //IPV4 POST + if (sock4 >= 0 && IS_ENABLED(CONFIG_NET_IPV4)) { + struct http_request req; + + memset(&req, 0, sizeof(req)); + + req.method = HTTP_POST; + // Construct URI path: + // m/{domain_id}/c/{channel_id}?authorization={client_secret} + LOG_INF("Constructing URI path for POST request"); + char uri_path[512]; // Increased buffer size + ret = snprintf(uri_path, sizeof(uri_path), "m/%s/c/%s?authorization=%s", + DOMAIN_ID, CHANNEL_ID, CLIENT_SECRET); + if (ret >= sizeof(uri_path)) { + LOG_ERR("URI path too long"); + close(sock4); + return -1; + } + LOG_INF("URI path: %s", uri_path); + req.url = uri_path; + req.host = MAGISTRALA_IP; + req.protocol = "HTTP/1.1"; + req.payload = payload; // Content of payload + req.payload_len = strlen(req.payload); + req.response = response_cb; + req.recv_buf = recv_buf_ipv4; + req.recv_buf_len = sizeof(recv_buf_ipv4); + + ret = http_client_req(sock4, &req, timeout, "IPv4 POST"); + LOG_INF("HTTP client request returned %d", ret); + + close(sock4); + } + + return ret; +} + +int main(void) +{ + k_sleep(K_SECONDS(5)); + + net_mgmt_init_event_callback(&cb, wifi_event_handler, NET_EVENT_WIFI_MASK); + net_mgmt_add_event_callback(&cb); + + /* Get STA interface in AP-STA mode. */ + sta_iface = net_if_get_wifi_sta(); + + connect_to_wifi(); + set_static_ip(); + + k_sleep(K_SECONDS(5)); + + int iterations = CONFIG_NET_SAMPLE_SEND_ITERATIONS; + int i = 0; + int ret = 0; + + while (iterations == 0 || i < iterations) { + ret = run_queries(); + if (ret < 0) { + ret = 1; + break; + } + + if (iterations > 0) { + i++; + if (i >= iterations) { + ret = 0; + break; + } + } else { + ret = 0; + break; + } + } + + if (iterations == 0) { + k_sleep(K_FOREVER); + } + + exit(ret); + return ret; +}