diff options
Diffstat (limited to 'fw/app/src')
| -rw-r--r-- | fw/app/src/mqtt.c | 97 | ||||
| -rw-r--r-- | fw/app/src/mqtt.h | 10 | 
2 files changed, 107 insertions, 0 deletions
diff --git a/fw/app/src/mqtt.c b/fw/app/src/mqtt.c new file mode 100644 index 0000000..605cfd1 --- /dev/null +++ b/fw/app/src/mqtt.c @@ -0,0 +1,97 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public License, + * v. 2.0. If a copy of the MPL was not distributed with this file, You can + * obtain one at https://mozilla.org/MPL/2.0/. + */ + + +#include <stdbool.h> +#include <stdint.h> +#include <poll.h> + +#include <zephyr/kernel.h> +#include <zephyr/logging/log.h> +#include <zephyr/net/socket.h> +#include <zephyr/net/mqtt.h> +#include <zephyr/net/net_ip.h> + + +LOG_MODULE_REGISTER(mqtt); + + +#define MQTT_STACK_SIZE               500 +#define MQTT_PRIO       K_PRIO_PREEMPT(8) + + +static uint8_t rx_buffer[256]; +static uint8_t tx_buffer[256]; +static struct mqtt_client client_ctx; +static struct sockaddr_storage broker; +static struct zsock_pollfd fds[1]; +static bool connected = false; + + +void mqtt_evt_handler(struct mqtt_client *client, const struct mqtt_evt *evt) { +	switch (evt->type) { +		case MQTT_EVT_CONNACK: +			LOG_INF("Connected to MQTT broker"); +			connected = true; +			break; +		default: +			LOG_ERR("Unhandled MQTT event %d", evt->type); +	} +} + + +static void mqtt_thread_function(void *ptr1, void *ptr2, void *ptr3) { +	LOG_INF("Starting MQTT thread"); + +	struct sockaddr_in6 *broker6 = (struct sockaddr_in6 *)&broker; +	broker6->sin6_family = AF_INET6; +	broker6->sin6_port = htons(1883); +	int rc = zsock_inet_pton(AF_INET6, "2001:db8::36", &broker6->sin6_addr); +	if (rc != 1) { +		LOG_ERR("Failed to convert IPv6 address (%d)", rc); +	} else { +		LOG_INF("Converted IPv6 address successfully"); +	} + +	mqtt_client_init(&client_ctx); + +	/* MQTT client configuration */ +	client_ctx.broker = &broker; +	client_ctx.evt_cb = mqtt_evt_handler; +	client_ctx.client_id.utf8 = (uint8_t *)"zephyr_mqtt_client"; +	client_ctx.client_id.size = sizeof("zephyr_mqtt_client") - 1; +	client_ctx.password = NULL; +	client_ctx.user_name = NULL; +	client_ctx.protocol_version = MQTT_VERSION_3_1_1; +	client_ctx.transport.type = MQTT_TRANSPORT_NON_SECURE; + +	/* MQTT buffers configuration */ +	client_ctx.rx_buf = rx_buffer; +	client_ctx.rx_buf_size = sizeof(rx_buffer); +	client_ctx.tx_buf = tx_buffer; +	client_ctx.tx_buf_size = sizeof(tx_buffer); + +	rc = mqtt_connect(&client_ctx); +	if (rc != 0) { +		LOG_ERR("Failed to connect (%d)", rc); +		return; +	} + +	fds[0].fd = client_ctx.transport.tcp.sock; +	fds[0].events = ZSOCK_POLLIN; +	zsock_poll(fds, 1, 5000); + +	mqtt_input(&client_ctx); + +	if (!connected) { +	   mqtt_abort(&client_ctx); +	} +} + + +K_THREAD_DEFINE(mqtt_thread, MQTT_STACK_SIZE, +                mqtt_thread_function, NULL, NULL, NULL, +                MQTT_PRIO, 0, 0); diff --git a/fw/app/src/mqtt.h b/fw/app/src/mqtt.h new file mode 100644 index 0000000..241620f --- /dev/null +++ b/fw/app/src/mqtt.h @@ -0,0 +1,10 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public License, + * v. 2.0. If a copy of the MPL was not distributed with this file, You can + * obtain one at https://mozilla.org/MPL/2.0/. + */ + +#ifndef SRC_MQTT_H +#define SRC_MQTT_H + +#endif // !SRC_MQTT_H  | 
