Skip to content

Commit e67ad1f

Browse files
Fix wolfIP API compatibility in MQTT example
Co-Authored-By: [email protected] <[email protected]>
1 parent dc36abd commit e67ad1f

16 files changed

+1507
-0
lines changed
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
cmake_minimum_required(VERSION 3.13)
2+
project(freertos_sim C)
3+
4+
# Set build type if not specified
5+
if(NOT CMAKE_BUILD_TYPE)
6+
set(CMAKE_BUILD_TYPE Debug)
7+
endif()
8+
9+
# FreeRTOS source files
10+
set(FREERTOS_DIR "${CMAKE_CURRENT_SOURCE_DIR}/freertos")
11+
set(FREERTOS_KERNEL_DIR "${FREERTOS_DIR}/FreeRTOS-Kernel")
12+
13+
# wolfSSL directories
14+
set(WOLFSSL_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../wolfssl")
15+
set(WOLFIP_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../wolfip")
16+
set(WOLFMQTT_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../../wolfMQTT")
17+
18+
# Certificate paths
19+
set(WOLFSSL_CERTS_DIR "${WOLFSSL_DIR}/certs")
20+
21+
# Add definitions for certificate paths
22+
add_definitions(
23+
-DMQTT_TLS_CA_CERT="${WOLFSSL_CERTS_DIR}/ca-cert.pem"
24+
-DMQTT_TLS_CLIENT_CERT="${WOLFSSL_CERTS_DIR}/client-cert.pem"
25+
-DMQTT_TLS_CLIENT_KEY="${WOLFSSL_CERTS_DIR}/client-key.pem"
26+
)
27+
28+
# FreeRTOS configuration
29+
add_definitions(
30+
-DPOSIX_PORT
31+
-DPOSIX
32+
-D_POSIX_C_SOURCE=200809L
33+
-D_POSIX_THREAD_SAFE_FUNCTIONS
34+
-DENABLE_MQTT_TLS
35+
-DDEBUG=0
36+
)
37+
38+
# wolfSSL configuration
39+
add_definitions(
40+
-DWOLFSSL_USER_SETTINGS
41+
-DHAVE_TLS_EXTENSIONS
42+
-DHAVE_SUPPORTED_CURVES
43+
-DTFM_TIMING_RESISTANT
44+
-DECC_TIMING_RESISTANT
45+
-DWC_RSA_BLINDING
46+
-DHAVE_AESGCM
47+
-DHAVE_CHACHA
48+
-DHAVE_POLY1305
49+
-DHAVE_ECC
50+
-DHAVE_CURVE25519
51+
-DHAVE_ED25519
52+
-DENABLE_MQTT_TLS
53+
)
54+
55+
# wolfIP definitions
56+
add_definitions(
57+
-DLINK_MTU=1500
58+
-DRXBUF_SIZE=8192
59+
-DTXBUF_SIZE=8192
60+
-DMAX_TCPSOCKETS=8
61+
-DMAX_UDPSOCKETS=8
62+
-DMAX_TIMERS=24
63+
-DWOLFIP_DEBUG
64+
-DWOLFSSL_DEBUG
65+
-DWOLFMQTT_DEBUG
66+
-DETH_TYPE_IP=0x0800
67+
)
68+
69+
# FreeRTOS source files
70+
set(FREERTOS_SRC
71+
${FREERTOS_KERNEL_DIR}/tasks.c
72+
${FREERTOS_KERNEL_DIR}/queue.c
73+
${FREERTOS_KERNEL_DIR}/list.c
74+
${FREERTOS_KERNEL_DIR}/timers.c
75+
${FREERTOS_KERNEL_DIR}/event_groups.c
76+
${FREERTOS_KERNEL_DIR}/stream_buffer.c
77+
${FREERTOS_KERNEL_DIR}/portable/ThirdParty/GCC/Posix/port.c
78+
${FREERTOS_KERNEL_DIR}/portable/MemMang/heap_3.c
79+
${FREERTOS_DIR}/utils/posix_utils.c
80+
)
81+
82+
# Create utils.c if it doesn't exist
83+
if(NOT EXISTS "${FREERTOS_DIR}/utils/utils.c")
84+
file(MAKE_DIRECTORY "${FREERTOS_DIR}/utils")
85+
file(WRITE "${FREERTOS_DIR}/utils/utils.c" "/* utils.c
86+
*
87+
* Copyright (C) 2006-2024 wolfSSL Inc.
88+
*
89+
* This file is part of wolfSSL.
90+
*
91+
* wolfSSL is free software; you can redistribute it and/or modify
92+
* it under the terms of the GNU General Public License as published by
93+
* the Free Software Foundation; either version 2 of the License, or
94+
* (at your option) any later version.
95+
*
96+
* wolfSSL is distributed in the hope that it will be useful,
97+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
98+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
99+
* GNU General Public License for more details.
100+
*
101+
* You should have received a copy of the GNU General Public License
102+
* along with this program; if not, write to the Free Software
103+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
104+
*/
105+
106+
#include <stdio.h>
107+
#include <stdlib.h>
108+
#include <string.h>
109+
#include <time.h>
110+
111+
/* Utility functions for FreeRTOS */
112+
void utils_init(void)
113+
{
114+
/* Initialize random seed */
115+
srand(time(NULL));
116+
}
117+
")
118+
endif()
119+
120+
# Add utils.c to FreeRTOS sources
121+
list(APPEND FREERTOS_SRC ${FREERTOS_DIR}/utils/utils.c)
122+
123+
# wolfIP source files - use stub implementation
124+
set(WOLFIP_SRC
125+
${CMAKE_CURRENT_SOURCE_DIR}/src/wolfip_stub.c
126+
)
127+
128+
# Application source files
129+
set(APP_SRC
130+
src/main.c
131+
src/wolfip_freertos.c
132+
src/mqtt_client.c
133+
src/mqtt_net.c
134+
src/wolfip_utils.c
135+
src/wolfmqtt_stub.c
136+
src/tap_interface.c
137+
)
138+
139+
# Include directories
140+
include_directories(
141+
${CMAKE_CURRENT_SOURCE_DIR}/include
142+
${CMAKE_CURRENT_SOURCE_DIR}/include/FreeRTOSConfig
143+
${CMAKE_CURRENT_SOURCE_DIR}/src
144+
${FREERTOS_KERNEL_DIR}/include
145+
${FREERTOS_KERNEL_DIR}/portable/ThirdParty/GCC/Posix
146+
${WOLFSSL_DIR}
147+
${WOLFIP_DIR}
148+
${WOLFIP_DIR}/src/include
149+
${WOLFMQTT_DIR}
150+
)
151+
152+
# Create static library for wolfIP
153+
add_library(wolfip STATIC ${WOLFIP_SRC})
154+
155+
# Set compiler flags for wolfIP library
156+
target_compile_options(wolfip PRIVATE
157+
-Wall
158+
-Wextra
159+
-g
160+
-fpack-struct
161+
)
162+
163+
# Create executable
164+
add_executable(freertos_sim
165+
${FREERTOS_SRC}
166+
${APP_SRC}
167+
)
168+
169+
# Link libraries
170+
target_link_libraries(freertos_sim
171+
wolfip
172+
wolfssl
173+
pthread
174+
m
175+
rt
176+
)
177+
178+
# Compiler flags
179+
target_compile_options(freertos_sim PRIVATE
180+
-Wall
181+
-Wextra
182+
-g
183+
-fpack-struct
184+
)
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/* posix_utils.c
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+
#include <stdio.h>
23+
#include <stdlib.h>
24+
#include <pthread.h>
25+
#include <semaphore.h>
26+
#include <errno.h>
27+
#include <time.h>
28+
29+
/* Event handling for POSIX port */
30+
typedef struct event_t {
31+
pthread_mutex_t mutex;
32+
pthread_cond_t cond;
33+
int signaled;
34+
} event_t;
35+
36+
/* Create event */
37+
void* event_create(void)
38+
{
39+
event_t* event = (event_t*)malloc(sizeof(event_t));
40+
if (event) {
41+
pthread_mutex_init(&event->mutex, NULL);
42+
pthread_cond_init(&event->cond, NULL);
43+
event->signaled = 0;
44+
}
45+
return event;
46+
}
47+
48+
/* Wait for event */
49+
int event_wait(void* handle)
50+
{
51+
event_t* event = (event_t*)handle;
52+
int ret = 0;
53+
54+
if (!event) {
55+
return -1;
56+
}
57+
58+
pthread_mutex_lock(&event->mutex);
59+
while (!event->signaled) {
60+
ret = pthread_cond_wait(&event->cond, &event->mutex);
61+
if (ret != 0) {
62+
break;
63+
}
64+
}
65+
event->signaled = 0;
66+
pthread_mutex_unlock(&event->mutex);
67+
68+
return ret;
69+
}
70+
71+
/* Signal event */
72+
int event_signal(void* handle)
73+
{
74+
event_t* event = (event_t*)handle;
75+
int ret = 0;
76+
77+
if (!event) {
78+
return -1;
79+
}
80+
81+
pthread_mutex_lock(&event->mutex);
82+
event->signaled = 1;
83+
ret = pthread_cond_signal(&event->cond);
84+
pthread_mutex_unlock(&event->mutex);
85+
86+
return ret;
87+
}
88+
89+
/* Delete event */
90+
int event_delete(void* handle)
91+
{
92+
event_t* event = (event_t*)handle;
93+
int ret = 0;
94+
95+
if (!event) {
96+
return -1;
97+
}
98+
99+
pthread_mutex_destroy(&event->mutex);
100+
pthread_cond_destroy(&event->cond);
101+
free(event);
102+
103+
return ret;
104+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/* mqtt_config.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 MQTT_CONFIG_H
23+
#define MQTT_CONFIG_H
24+
25+
/* Enable TLS for MQTT */
26+
#define ENABLE_MQTT_TLS
27+
28+
/* MQTT TLS certificate paths */
29+
#define MQTT_TLS_CA_CERT "../../../wolfssl/certs/ca-cert.pem"
30+
#define MQTT_TLS_CLIENT_CERT "../../../wolfssl/certs/client-cert.pem"
31+
#define MQTT_TLS_CLIENT_KEY "../../../wolfssl/certs/client-key.pem"
32+
33+
/* MQTT client configuration */
34+
#define MQTT_HOST "10.10.0.1"
35+
#define MQTT_PORT 8883
36+
#define MQTT_TOPIC "test/topic"
37+
#define MQTT_QOS 0
38+
#define MQTT_CLIENT_ID "wolfMQTT-client"
39+
40+
#endif /* MQTT_CONFIG_H */

0 commit comments

Comments
 (0)