Skip to content

Commit 6b9ff2e

Browse files
Add FreeRTOS + wolfIP + wolfMQTT TLS example
- Implements MQTT client with TLS 1.3 using wolfMQTT and wolfSSL - Uses wolfIP for network stack integration - Runs on FreeRTOS POSIX simulation - Includes test scripts and documentation - Network configuration: 10.10.0.1 (broker), 10.10.0.10 (client) - Supports publish/subscribe on test/topic Co-Authored-By: [email protected] <[email protected]>
1 parent 6d96701 commit 6b9ff2e

File tree

15 files changed

+1314
-0
lines changed

15 files changed

+1314
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# FreeRTOS directories managed by setup script
2+
freertos/FreeRTOS/
3+
freertos/FreeRTOS-Kernel/
4+
5+
# Certificate files
6+
certs/
7+
8+
9+
# Build directory
10+
build/
11+
12+
# Object files
13+
*.o
14+
*.ko
15+
*.obj
16+
*.elf
17+
18+
# Libraries
19+
*.lib
20+
*.a
21+
*.la
22+
*.lo
23+
24+
# Executables
25+
*.exe
26+
*.out
27+
*.app
28+
*.i*86
29+
*.x86_64
30+
*.hex
31+
32+
# Debug files
33+
*.dSYM/
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
cmake_minimum_required(VERSION 3.13)
2+
project(freertos_wolfssl_demo C)
3+
4+
# Set C standard
5+
set(CMAKE_C_STANDARD 11)
6+
set(CMAKE_C_STANDARD_REQUIRED ON)
7+
8+
# wolfSSL configuration
9+
add_definitions(-DWOLFSSL_USER_SETTINGS)
10+
11+
# FreeRTOS Kernel source files for POSIX port
12+
set(FREERTOS_PORT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/freertos/FreeRTOS-Kernel/portable/ThirdParty/GCC/Posix)
13+
set(FREERTOS_HEAP_DIR ${CMAKE_CURRENT_SOURCE_DIR}/freertos/FreeRTOS-Kernel/portable/MemMang)
14+
15+
# Include directories
16+
include_directories(
17+
${CMAKE_CURRENT_SOURCE_DIR}/include
18+
${CMAKE_CURRENT_SOURCE_DIR}/freertos/FreeRTOS-Kernel/include
19+
${FREERTOS_PORT_DIR}
20+
${CMAKE_CURRENT_SOURCE_DIR}/../../../wolfip/src
21+
${CMAKE_CURRENT_SOURCE_DIR}/../../../wolfip
22+
${CMAKE_CURRENT_SOURCE_DIR}/../../../wolfip/src/http
23+
${CMAKE_CURRENT_SOURCE_DIR}/../../../wolfip/src/port
24+
${CMAKE_CURRENT_SOURCE_DIR}/../../../wolfssl
25+
${CMAKE_CURRENT_SOURCE_DIR}/../../../wolfssl/include
26+
${CMAKE_CURRENT_SOURCE_DIR}/../../../wolfMQTT
27+
)
28+
29+
# FreeRTOS source files
30+
set(FREERTOS_SOURCES
31+
${CMAKE_CURRENT_SOURCE_DIR}/freertos/FreeRTOS-Kernel/tasks.c
32+
${CMAKE_CURRENT_SOURCE_DIR}/freertos/FreeRTOS-Kernel/queue.c
33+
${CMAKE_CURRENT_SOURCE_DIR}/freertos/FreeRTOS-Kernel/list.c
34+
${CMAKE_CURRENT_SOURCE_DIR}/freertos/FreeRTOS-Kernel/timers.c
35+
${CMAKE_CURRENT_SOURCE_DIR}/freertos/FreeRTOS-Kernel/event_groups.c
36+
${CMAKE_CURRENT_SOURCE_DIR}/freertos/FreeRTOS-Kernel/stream_buffer.c
37+
${FREERTOS_PORT_DIR}/port.c
38+
${FREERTOS_HEAP_DIR}/heap_3.c
39+
${CMAKE_CURRENT_SOURCE_DIR}/freertos/utils/utils.c
40+
)
41+
42+
# Add wolfIP library
43+
add_library(wolfip STATIC
44+
${CMAKE_CURRENT_SOURCE_DIR}/../../../wolfip/src/wolfip.c
45+
${CMAKE_CURRENT_SOURCE_DIR}/../../../wolfip/src/http/httpd.c
46+
${CMAKE_CURRENT_SOURCE_DIR}/../../../wolfip/src/port/wolfssl_io.c
47+
)
48+
49+
# Add the main application
50+
add_executable(freertos_sim
51+
${FREERTOS_SOURCES}
52+
src/main.c
53+
src/wolfip_freertos.c
54+
src/mqtt_client.c
55+
src/mqtt_net.c
56+
)
57+
58+
target_link_libraries(freertos_sim
59+
pthread
60+
wolfip
61+
${CMAKE_CURRENT_SOURCE_DIR}/../../../wolfMQTT/src/.libs/libwolfmqtt.a
62+
${CMAKE_CURRENT_SOURCE_DIR}/../../../wolfssl/src/.libs/libwolfssl.a
63+
m
64+
crypto
65+
)
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include <errno.h>
2+
#include <pthread.h>
3+
#include <signal.h>
4+
#include <stdio.h>
5+
#include <stdlib.h>
6+
#include <unistd.h>
7+
8+
typedef struct event_t {
9+
pthread_mutex_t mutex;
10+
pthread_cond_t cond;
11+
int value;
12+
} event_t;
13+
14+
event_t *event_create(void) {
15+
event_t *event = malloc(sizeof(event_t));
16+
if (event != NULL) {
17+
pthread_mutex_init(&event->mutex, NULL);
18+
pthread_cond_init(&event->cond, NULL);
19+
event->value = 0;
20+
}
21+
return event;
22+
}
23+
24+
void event_delete(event_t *event) {
25+
if (event != NULL) {
26+
pthread_mutex_destroy(&event->mutex);
27+
pthread_cond_destroy(&event->cond);
28+
free(event);
29+
}
30+
}
31+
32+
void event_signal(event_t *event) {
33+
if (event != NULL) {
34+
pthread_mutex_lock(&event->mutex);
35+
event->value = 1;
36+
pthread_cond_signal(&event->cond);
37+
pthread_mutex_unlock(&event->mutex);
38+
}
39+
}
40+
41+
void event_wait(event_t *event) {
42+
if (event != NULL) {
43+
pthread_mutex_lock(&event->mutex);
44+
while (event->value == 0) {
45+
pthread_cond_wait(&event->cond, &event->mutex);
46+
}
47+
event->value = 0;
48+
pthread_mutex_unlock(&event->mutex);
49+
}
50+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/* FreeRTOSConfig.h
2+
*
3+
* Copyright (C) 2006-2024 wolfSSL Inc.
4+
*
5+
* This file is part of wolfSSL.
6+
*
7+
* wolfSSL is free software; you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation; either version 2 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* wolfSSL is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
20+
*/
21+
22+
#ifndef FREERTOS_CONFIG_H
23+
#define FREERTOS_CONFIG_H
24+
25+
/* Scheduler Related */
26+
#define configUSE_PREEMPTION 1
27+
#define configUSE_PORT_OPTIMISED_TASK_SELECTION 0
28+
#define configUSE_TICKLESS_IDLE 0
29+
#define configCPU_CLOCK_HZ ( ( unsigned long ) 60000000 )
30+
#define configTICK_RATE_HZ ( ( TickType_t ) 1000 )
31+
#define configMAX_PRIORITIES 5
32+
#define configMINIMAL_STACK_SIZE ( ( unsigned short ) 4096 )
33+
#define configMAX_TASK_NAME_LEN 16
34+
#define configUSE_16_BIT_TICKS 0
35+
#define configIDLE_SHOULD_YIELD 1
36+
#define configUSE_TASK_NOTIFICATIONS 1
37+
#define configTASK_NOTIFICATION_ARRAY_ENTRIES 3
38+
#define configUSE_MUTEXES 1
39+
#define configUSE_RECURSIVE_MUTEXES 1
40+
#define configUSE_COUNTING_SEMAPHORES 1
41+
#define configQUEUE_REGISTRY_SIZE 10
42+
#define configUSE_QUEUE_SETS 0
43+
#define configUSE_TIME_SLICING 1
44+
#define configUSE_NEWLIB_REENTRANT 0
45+
#define configENABLE_BACKWARD_COMPATIBILITY 0
46+
#define configNUM_THREAD_LOCAL_STORAGE_POINTERS 5
47+
#define configUSE_MINI_LIST_ITEM 1
48+
49+
/* Memory allocation related definitions. */
50+
#define configSUPPORT_STATIC_ALLOCATION 0
51+
#define configSUPPORT_DYNAMIC_ALLOCATION 1
52+
#define configTOTAL_HEAP_SIZE ( ( size_t ) ( 60 * 1024 ) )
53+
#define configAPPLICATION_ALLOCATED_HEAP 0
54+
55+
/* Hook function related definitions. */
56+
#define configUSE_IDLE_HOOK 0
57+
#define configUSE_TICK_HOOK 0
58+
#define configCHECK_FOR_STACK_OVERFLOW 0
59+
#define configUSE_MALLOC_FAILED_HOOK 0
60+
#define configUSE_DAEMON_TASK_STARTUP_HOOK 0
61+
62+
/* Run time and task stats gathering related definitions. */
63+
#define configGENERATE_RUN_TIME_STATS 0
64+
#define configUSE_TRACE_FACILITY 0
65+
#define configUSE_STATS_FORMATTING_FUNCTIONS 0
66+
67+
/* Co-routine related definitions. */
68+
#define configUSE_CO_ROUTINES 0
69+
#define configMAX_CO_ROUTINE_PRIORITIES 1
70+
71+
/* Software timer related definitions. */
72+
#define configUSE_TIMERS 1
73+
#define configTIMER_TASK_PRIORITY ( configMAX_PRIORITIES - 1 )
74+
#define configTIMER_QUEUE_LENGTH 10
75+
#define configTIMER_TASK_STACK_DEPTH configMINIMAL_STACK_SIZE
76+
77+
/* Define to trap errors during development. */
78+
#define configASSERT( x )
79+
80+
/* Optional functions - most linkers will remove unused functions anyway. */
81+
#define INCLUDE_vTaskPrioritySet 1
82+
#define INCLUDE_uxTaskPriorityGet 1
83+
#define INCLUDE_vTaskDelete 1
84+
#define INCLUDE_vTaskSuspend 1
85+
#define INCLUDE_xResumeFromISR 1
86+
#define INCLUDE_vTaskDelayUntil 1
87+
#define INCLUDE_vTaskDelay 1
88+
#define INCLUDE_xTaskGetSchedulerState 1
89+
#define INCLUDE_xTaskGetCurrentTaskHandle 1
90+
#define INCLUDE_uxTaskGetStackHighWaterMark 0
91+
#define INCLUDE_xTaskGetIdleTaskHandle 0
92+
#define INCLUDE_eTaskGetState 0
93+
#define INCLUDE_xEventGroupSetBitFromISR 1
94+
#define INCLUDE_xTimerPendFunctionCall 0
95+
#define INCLUDE_xTaskAbortDelay 0
96+
#define INCLUDE_xTaskGetHandle 0
97+
#define INCLUDE_xTaskResumeFromISR 1
98+
99+
/* POSIX Port specific definitions. */
100+
#define configPOSIX_STACK_SIZE ( ( unsigned short ) 8192 )
101+
102+
#endif /* FREERTOS_CONFIG_H */
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/* user_settings.h
2+
*
3+
* Copyright (C) 2006-2024 wolfSSL Inc.
4+
*
5+
* This file is part of wolfSSL.
6+
*
7+
* wolfSSL is free software; you can redistribute it and/or modify
8+
* it under the terms of the GNU General Public License as published by
9+
* the Free Software Foundation; either version 2 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* wolfSSL is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
20+
*/
21+
22+
#ifndef USER_SETTINGS_H
23+
#define USER_SETTINGS_H
24+
25+
#ifndef _GNU_SOURCE
26+
#define _GNU_SOURCE
27+
#endif
28+
29+
/* wolfSSL TLS configuration */
30+
#define WOLFSSL_TLS13
31+
#define WOLFSSL_TLS13_NO_OLD_TLS
32+
#define WOLFSSL_CERT_GEN
33+
#define WOLFSSL_DES_ECB
34+
#define HAVE_HKDF
35+
#define HAVE_AEAD
36+
#define HAVE_SUPPORTED_CURVES
37+
#define WOLFSSL_AES_DIRECT
38+
#define HAVE_TLS_EXTENSIONS
39+
#define HAVE_SNI
40+
#define HAVE_OCSP
41+
#define HAVE_CERTIFICATE_STATUS_REQUEST
42+
#define WOLFSSL_ALWAYS_VERIFY_CB
43+
#define WOLFSSL_VERIFY_CB_ALL_CERTS
44+
45+
/* wolfMQTT configuration */
46+
#define WOLFMQTT_NO_ERROR_STRINGS
47+
#define WOLFMQTT_NONBLOCK
48+
#define WOLFMQTT_ENABLE_NONBLOCK
49+
#define ENABLE_MQTT_TLS
50+
#define MQTT_MAX_PACKET_SIZE 1024
51+
#define MQTT_DEFAULT_CMD_TIMEOUT_MS 5000
52+
53+
/* wolfIP configuration */
54+
#define WOLFIP_DEBUG
55+
#define WOLFIP_CHECKSUM_VERIFY
56+
57+
/* Socket configuration */
58+
#define WOLFIP_AF_INET 2
59+
#define WOLFIP_SOCK_STREAM 1
60+
#define WOLFIP_SOCK_DGRAM 2
61+
62+
#endif /* USER_SETTINGS_H */
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/bin/bash
2+
3+
# Script to setup FreeRTOS environment for wolfSSL examples
4+
set -e
5+
6+
FREERTOS_REPO="https://github.com/FreeRTOS/FreeRTOS.git"
7+
FREERTOS_KERNEL_REPO="https://github.com/FreeRTOS/FreeRTOS-Kernel.git"
8+
echo "Setting up FreeRTOS simulation environment..."
9+
10+
# Create directories if they don't exist
11+
mkdir -p freertos
12+
cd freertos
13+
14+
# Clone FreeRTOS repositories if they don't exist
15+
if [ ! -d "FreeRTOS" ]; then
16+
git clone $FREERTOS_REPO
17+
fi
18+
19+
if [ ! -d "FreeRTOS-Kernel" ]; then
20+
git clone $FREERTOS_KERNEL_REPO
21+
fi
22+
23+
echo "FreeRTOS repositories cloned successfully"
24+
25+
# Create basic directory structure for our project
26+
mkdir -p ../src
27+
mkdir -p ../include
28+
mkdir -p ../build
29+
30+
# Create utils directory and copy utils.c from HTTPS example
31+
mkdir -p utils
32+
cp ../freertos-wolfip-wolfssl-https/freertos/utils/utils.c utils/
33+
34+
echo "Directory structure created"
35+
echo "Setup complete!"
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/bin/bash
2+
# Configure host TAP interface for wolfSSL embedded testing
3+
# Creates a TAP interface for virtual networking between host and FreeRTOS
4+
5+
# Check if running as root
6+
if [ "$EUID" -ne 0 ]; then
7+
echo "Please run as root (sudo)"
8+
exit 1
9+
fi
10+
11+
# Remove existing interface if present
12+
ip link show wtap0 >/dev/null 2>&1 && ip link delete wtap0
13+
14+
# Create new TAP interface and configure it
15+
ip tuntap add dev wtap0 mode tap
16+
ip link set wtap0 down
17+
ip addr flush dev wtap0
18+
ip addr add 10.10.0.1/24 dev wtap0
19+
ip link set wtap0 up
20+
21+
echo "TAP interface wtap0 configured with IP 10.10.0.1/24"

0 commit comments

Comments
 (0)