Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NOISSUE - Add MQTTS zephyrs target. #8

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions targets/zephyr_targets/mqtts/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
build
Kconfig
src/creds
felixgateru marked this conversation as resolved.
Show resolved Hide resolved
22 changes: 22 additions & 0 deletions targets/zephyr_targets/mqtts/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
cmake_minimum_required(VERSION 3.20.0)

find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(mqtts)

if(USE_DUMMY_CREDS)
set(creds "src/creds/dummy.c")
else()
if(NOT EXISTS ${APPLICATION_SOURCE_DIR}/src/creds/key.c OR
NOT EXISTS ${APPLICATION_SOURCE_DIR}/src/creds/cert.c OR
NOT EXISTS ${APPLICATION_SOURCE_DIR}/src/creds/ca.c)
message(FATAL_ERROR
"Credentials not found. Please run "
"'python3 src/creds/convert_keys.py' before building"
)
endif()

set(creds "src/creds/ca.c" "src/creds/key.c" "src/creds/cert.c")
endif()

target_sources(app PRIVATE "src/main.c" ${creds})
target_sources_ifdef(CONFIG_NET_DHCPV4 app PRIVATE "src/dhcp.c")
20 changes: 20 additions & 0 deletions targets/zephyr_targets/mqtts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
MQTTS- stm32f4 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](include/config.h) with your broker details.

## Build
The project can be built by utilising the make file within the target directory

```bash
west build -p always -b <your-board-name> mqtt
```
## Flash
Platform io generate a build directory with the fimware.bin within it. Use the make command to flash to board
```bash
west flash
```
54 changes: 54 additions & 0 deletions targets/zephyr_targets/mqtts/prj.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
CONFIG_MAIN_STACK_SIZE=4096
CONFIG_ENTROPY_GENERATOR=y
CONFIG_TEST_RANDOM_GENERATOR=y
CONFIG_INIT_STACKS=y
CONFIG_HW_STACK_PROTECTION=y
CONFIG_NEWLIB_LIBC=y
CONFIG_SNTP=y
CONFIG_JSON_LIBRARY=y
CONFIG_POSIX_CLOCK=y

# DNS
CONFIG_DNS_RESOLVER=y
CONFIG_DNS_RESOLVER_ADDITIONAL_BUF_CTR=2
CONFIG_DNS_RESOLVER_MAX_SERVERS=1
CONFIG_DNS_SERVER_IP_ADDRESSES=y
CONFIG_DNS_SERVER1="8.8.8.8"
CONFIG_NET_SOCKETS_DNS_TIMEOUT=5000
CONFIG_DNS_RESOLVER_LOG_LEVEL_DBG=n

# Generic networking options
CONFIG_NETWORKING=y
CONFIG_NET_UDP=y
CONFIG_NET_TCP=y
CONFIG_NET_IPV6=y
CONFIG_NET_IPV4=y
CONFIG_NET_SOCKETS=y
CONFIG_NET_SOCKETS_SOCKOPT_TLS=y

# Logging
CONFIG_LOG=y

# Network buffers
CONFIG_NET_PKT_RX_COUNT=32
CONFIG_NET_PKT_TX_COUNT=16
CONFIG_NET_BUF_RX_COUNT=64
CONFIG_NET_BUF_TX_COUNT=32

# MQTT
CONFIG_MQTT_LIB=y
CONFIG_MQTT_LIB_TLS=y
CONFIG_MQTT_KEEPALIVE=60

# TLS
CONFIG_MBEDTLS=y
CONFIG_MBEDTLS_BUILTIN=y
CONFIG_MBEDTLS_ENABLE_HEAP=y
CONFIG_MBEDTLS_HEAP_SIZE=65536
CONFIG_MBEDTLS_SSL_MAX_CONTENT_LEN=16384
CONFIG_MBEDTLS_PEM_CERTIFICATE_FORMAT=y
CONFIG_MBEDTLS_SERVER_NAME_INDICATION=y
CONFIG_MBEDTLS_AES_ROM_TABLES=y
CONFIG_MBEDTLS_TLS_VERSION_1_2=y
CONFIG_MBEDTLS_MEMORY_DEBUG=y
CONFIG_MBEDTLS_HAVE_TIME_DATE=y
23 changes: 23 additions & 0 deletions targets/zephyr_targets/mqtts/src/config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef CONFIG_H
#define CONFIG_H

#define MQTT_CLIENTID ""
#define SNTP_SERVER "0.pool.ntp.org
#define BROKER ""
#define BROKER_PORT "8883"
#define MQTT_BUFFER_SIZE 256u
#define APP_BUFFER_SIZE 4096u
#define MAX_RETRIES 10u
#define BACKOFF_EXP_BASE_MS 1000u
#define BACKOFF_EXP_MAX_MS 60000u
#define BACKOFF_CONST_MS 5000u
#define KEEP_ALIVE 60

const char *mfThingId = " ";
const char *mfThingKey = " ";
const char *mfChannelId = " ";
char mfTopic[150];
felixgateru marked this conversation as resolved.
Show resolved Hide resolved

const char *brokername = ""

#endif
52 changes: 52 additions & 0 deletions targets/zephyr_targets/mqtts/src/dhcp.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <zephyr/logging/log.h>

LOG_MODULE_DECLARE(aws, LOG_LEVEL_DBG);

#include <zephyr/kernel.h>

#include <zephyr/net/net_if.h>
#include <zephyr/net/net_core.h>
#include <zephyr/net/net_context.h>
#include <zephyr/net/net_mgmt.h>

static struct net_mgmt_event_callback mgmt_cb;

static K_SEM_DEFINE(got_address, 0, 1);

static void handler(struct net_mgmt_event_callback *cb,
uint32_t mgmt_event,
struct net_if *iface)
{
int i;
bool notified = false;

if (mgmt_event != NET_EVENT_IPV4_ADDR_ADD) {
return;
}

for (i = 0; i < NET_IF_MAX_IPV4_ADDR; i++) {
if (iface->config.ip.ipv4->unicast[i].addr_type !=
NET_ADDR_DHCP) {
continue;
}

if (!notified) {
k_sem_give(&got_address);
notified = true;
}
break;
}
}

void app_dhcpv4_startup(void)
{
LOG_INF("starting DHCPv4");

net_mgmt_init_event_callback(&mgmt_cb, handler,
NET_EVENT_IPV4_ADDR_ADD);
net_mgmt_add_event_callback(&mgmt_cb);

net_dhcpv4_start(net_if_get_default());

k_sem_take(&got_address, K_FOREVER);
}
6 changes: 6 additions & 0 deletions targets/zephyr_targets/mqtts/src/dhcp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#ifndef __DHCP_H__
#define __DHCP_H__

void app_dhcpv4_startup(void);

#endif
Loading