Skip to content

Commit 0ba1e19

Browse files
PR #492: Fix wolfIP integration and add MQTT configuration
Co-Authored-By: [email protected] <[email protected]>
1 parent a5d494d commit 0ba1e19

File tree

4 files changed

+99
-251
lines changed

4 files changed

+99
-251
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
/* MQTT Configuration */
26+
#define MQTT_MAX_PACKET_SIZE 1024
27+
#define MQTT_DEFAULT_CMD_TIMEOUT_MS 30000
28+
#define MQTT_KEEP_ALIVE_SEC 60
29+
#define MQTT_CLIENT_ID "FreeRTOS_Client"
30+
#define MQTT_TEST_TOPIC "test/topic"
31+
#define MQTT_HOST "10.10.0.1"
32+
#define MQTT_PORT 8883
33+
34+
/* Debug Configuration */
35+
#ifndef WOLFMQTT_DEBUG
36+
#define WOLFMQTT_DEBUG
37+
#endif
38+
39+
#endif /* MQTT_CONFIG_H */

fullstack/freertos-wolfip-wolfmqtt/src/wolfip_freertos.c

Lines changed: 52 additions & 224 deletions
Original file line numberDiff line numberDiff line change
@@ -19,249 +19,77 @@
1919
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
2020
*/
2121

22-
#include "wolfip_freertos.h"
2322
#include <stdio.h>
2423
#include <string.h>
25-
#include <unistd.h>
26-
#include <fcntl.h>
27-
#include <sys/ioctl.h>
28-
#include <linux/if.h>
29-
#include <linux/if_tun.h>
3024
#include <arpa/inet.h>
31-
#include <sys/time.h>
32-
#include <poll.h>
33-
#include <sys/socket.h>
34-
#include <sys/random.h>
35-
#include <errno.h>
36-
37-
/* Implementation of wolfIP's required random number generator */
38-
uint32_t wolfIP_getrandom(void) {
39-
uint32_t ret;
40-
getrandom(&ret, sizeof(ret), 0);
41-
return ret;
42-
}
43-
44-
struct wolfIP *g_wolfip = NULL;
45-
static TaskHandle_t g_network_task = NULL;
46-
static int tap_fd = -1;
47-
48-
/* TUN/TAP device functions */
49-
static int tap_init(struct ll *dev, const char *ifname) {
50-
struct ifreq ifr;
51-
int sock_fd;
52-
53-
if ((tap_fd = open("/dev/net/tun", O_RDWR)) < 0) {
54-
perror("Error opening /dev/net/tun");
55-
return -1;
56-
}
57-
58-
memset(&ifr, 0, sizeof(ifr));
59-
ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
60-
strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
61-
62-
if (ioctl(tap_fd, TUNSETIFF, (void *)&ifr) < 0) {
63-
perror("ioctl TUNSETIFF");
64-
close(tap_fd);
65-
return -1;
66-
}
67-
68-
/* Get MAC address */
69-
if (ioctl(tap_fd, SIOCGIFHWADDR, &ifr) < 0) {
70-
perror("ioctl SIOCGIFHWADDR");
71-
close(tap_fd);
72-
return -1;
73-
}
74-
75-
strncpy(dev->ifname, ifname, sizeof(dev->ifname) - 1);
76-
memcpy(dev->mac, ifr.ifr_hwaddr.sa_data, 6);
77-
dev->mac[5] ^= 1; /* Make MAC unique */
25+
#include "wolfip_freertos.h"
26+
#include "../../../wolfip/wolfip.h"
7827

79-
/* Configure network interface */
80-
sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
81-
if (sock_fd < 0) {
82-
perror("socket");
83-
close(tap_fd);
84-
return -1;
85-
}
28+
/* External functions from linux_tap.c */
29+
extern int tap_init(struct ll *ll, const char *ifname, uint32_t host_ip);
8630

87-
/* Set interface UP */
88-
if (ioctl(sock_fd, SIOCGIFFLAGS, &ifr) < 0) {
89-
perror("ioctl SIOCGIFFLAGS");
90-
close(sock_fd);
91-
return -1;
92-
}
93-
ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
94-
if (ioctl(sock_fd, SIOCSIFFLAGS, &ifr) < 0) {
95-
perror("ioctl SIOCSIFFLAGS");
96-
close(sock_fd);
97-
return -1;
98-
}
31+
static struct wolfIP *g_ipstack = NULL;
9932

100-
close(sock_fd);
101-
return 0;
102-
}
103-
104-
static int tap_poll(struct ll *ll, void *buf, uint32_t len) {
105-
struct pollfd pfd;
33+
int wolfip_init(void)
34+
{
35+
struct wolfIP *ipstack;
36+
ip4 ip, netmask, gateway;
37+
struct ll *dev;
10638
int ret;
10739

108-
pfd.fd = tap_fd;
109-
pfd.events = POLLIN;
110-
111-
do {
112-
ret = poll(&pfd, 1, 1); /* Short timeout */
113-
} while (ret < 0 && errno == EINTR);
114-
115-
if (ret < 0) {
116-
perror("poll");
117-
return -1;
118-
}
119-
if (ret == 0) {
120-
return 0;
121-
}
122-
123-
do {
124-
ret = read(tap_fd, buf, len);
125-
} while (ret < 0 && errno == EINTR);
126-
127-
return ret;
128-
}
129-
130-
static int tap_send(struct ll *ll, void *buf, uint32_t len) {
131-
return write(tap_fd, buf, len);
132-
}
133-
134-
/* Network task implementation */
135-
static void wolfIP_NetworkTask(void *pvParameters) {
136-
TickType_t last_wake_time;
137-
const TickType_t frequency = pdMS_TO_TICKS(WOLFIP_POLL_INTERVAL_MS);
138-
struct timeval tv;
139-
140-
last_wake_time = xTaskGetTickCount();
141-
142-
while (1) {
143-
gettimeofday(&tv, NULL);
144-
wolfIP_poll(g_wolfip, tv.tv_sec * 1000 + tv.tv_usec / 1000);
145-
vTaskDelayUntil(&last_wake_time, frequency);
146-
}
147-
}
148-
149-
int wolfIP_FreeRTOS_Init(void) {
150-
struct ll *tapdev;
151-
15240
/* Initialize wolfIP */
153-
wolfIP_init_static(&g_wolfip);
154-
if (!g_wolfip) {
41+
wolfIP_init_static(&ipstack);
42+
if (!ipstack) {
15543
printf("Failed to initialize wolfIP\n");
15644
return -1;
15745
}
158-
159-
/* Setup TUN/TAP interface */
160-
tapdev = wolfIP_getdev(g_wolfip);
161-
if (!tapdev) {
162-
printf("Failed to get device from wolfIP\n");
46+
47+
/* Configure IP settings */
48+
ip = htonl(0x0A0A000A); /* 10.10.0.10 */
49+
netmask = htonl(0xFFFFFF00); /* 255.255.255.0 */
50+
gateway = htonl(0x0A0A0001); /* 10.10.0.1 */
51+
52+
/* Get device interface */
53+
dev = wolfIP_getdev(ipstack);
54+
if (!dev) {
55+
printf("Failed to get device interface\n");
16356
return -1;
16457
}
16558

166-
printf("Initializing TAP device wtap0...\n");
167-
if (tap_init(tapdev, "wtap0") < 0) {
168-
printf("Failed to initialize TAP device: %s\n", strerror(errno));
59+
/* Initialize TAP device */
60+
ret = tap_init(dev, "wtap0", ip);
61+
if (ret < 0) {
62+
printf("Failed to initialize TAP device\n");
16963
return -1;
17064
}
171-
printf("TAP device initialized successfully\n");
17265

173-
/* Set device callbacks */
174-
tapdev->poll = tap_poll;
175-
tapdev->send = tap_send;
176-
177-
/* Configure IP settings */
178-
printf("Configuring network settings:\n");
179-
printf(" IP: 10.10.0.10\n");
180-
printf(" Netmask: 255.255.255.0\n");
181-
printf(" Gateway: 10.10.0.1\n");
182-
183-
wolfIP_ipconfig_set(g_wolfip,
184-
atoip4("10.10.0.10"), /* IP */
185-
atoip4("255.255.255.0"), /* Netmask */
186-
atoip4("10.10.0.1")); /* Gateway */
187-
188-
printf("Network configuration complete\n");
66+
/* Set IP configuration */
67+
wolfIP_ipconfig_set(ipstack, ip, netmask, gateway);
68+
69+
/* Print IP configuration */
70+
wolfIP_ipconfig_get(ipstack, &ip, &netmask, &gateway);
71+
printf("IP Configuration:\n");
72+
printf("IP: %d.%d.%d.%d\n",
73+
(ntohl(ip) >> 24) & 0xFF,
74+
(ntohl(ip) >> 16) & 0xFF,
75+
(ntohl(ip) >> 8) & 0xFF,
76+
ntohl(ip) & 0xFF);
77+
printf("Netmask: %d.%d.%d.%d\n",
78+
(ntohl(netmask) >> 24) & 0xFF,
79+
(ntohl(netmask) >> 16) & 0xFF,
80+
(ntohl(netmask) >> 8) & 0xFF,
81+
ntohl(netmask) & 0xFF);
82+
printf("Gateway: %d.%d.%d.%d\n",
83+
(ntohl(gateway) >> 24) & 0xFF,
84+
(ntohl(gateway) >> 16) & 0xFF,
85+
(ntohl(gateway) >> 8) & 0xFF,
86+
ntohl(gateway) & 0xFF);
87+
88+
g_ipstack = ipstack;
18989
return 0;
19090
}
19191

192-
static void UDP_Echo_Task(void* pvParameters) {
193-
int sockfd;
194-
uint8_t buf[1024];
195-
int ret;
196-
struct wolfIP_sockaddr_in addr;
197-
struct wolfIP_sockaddr_in client_addr;
198-
socklen_t client_len;
199-
200-
sockfd = wolfIP_sock_socket(g_wolfip, AF_INET, SOCK_DGRAM, 0);
201-
if (sockfd < 0) {
202-
printf("Failed to create UDP socket\n");
203-
return;
204-
}
205-
206-
memset(&addr, 0, sizeof(addr));
207-
addr.sin_family = AF_INET;
208-
addr.sin_port = htons(UDP_TEST_PORT);
209-
addr.sin_addr.s_addr = htonl(INADDR_ANY);
210-
211-
if (wolfIP_sock_bind(g_wolfip, sockfd, (struct wolfIP_sockaddr*)&addr, sizeof(addr)) < 0) {
212-
printf("Failed to bind UDP socket\n");
213-
wolfIP_sock_close(g_wolfip, sockfd);
214-
return;
215-
}
216-
217-
printf("UDP Echo Server running on port %d\n", UDP_TEST_PORT);
218-
219-
while (1) {
220-
client_len = sizeof(client_addr);
221-
ret = wolfIP_sock_recvfrom(g_wolfip, sockfd, buf, sizeof(buf), 0,
222-
(struct wolfIP_sockaddr*)&client_addr, &client_len);
223-
if (ret > 0) {
224-
uint32_t ip = ntohl(client_addr.sin_addr.s_addr);
225-
printf("Received %d bytes from %d.%d.%d.%d:%d\n", ret,
226-
(ip >> 24) & 0xFF,
227-
(ip >> 16) & 0xFF,
228-
(ip >> 8) & 0xFF,
229-
ip & 0xFF,
230-
ntohs(client_addr.sin_port));
231-
wolfIP_sock_sendto(g_wolfip, sockfd, buf, ret, 0,
232-
(struct wolfIP_sockaddr*)&client_addr, client_len);
233-
}
234-
vTaskDelay(pdMS_TO_TICKS(10));
235-
}
236-
}
237-
238-
int wolfIP_Start_UDP_Echo(void) {
239-
BaseType_t ret;
240-
241-
ret = xTaskCreate(UDP_Echo_Task,
242-
"UDP_Echo",
243-
WOLFIP_TASK_STACK_SIZE,
244-
NULL,
245-
tskIDLE_PRIORITY + 1,
246-
NULL);
247-
248-
return (ret == pdPASS) ? 0 : -1;
249-
}
250-
251-
int wolfIP_FreeRTOS_Start(void) {
252-
BaseType_t ret;
253-
254-
if (!g_wolfip) {
255-
printf("wolfIP not initialized\n");
256-
return -1;
257-
}
258-
259-
ret = xTaskCreate(wolfIP_NetworkTask,
260-
"WolfIP_Net",
261-
WOLFIP_TASK_STACK_SIZE,
262-
NULL,
263-
WOLFIP_TASK_PRIORITY,
264-
&g_network_task);
265-
266-
return (ret == pdPASS) ? 0 : -1;
92+
struct wolfIP* wolfip_get_stack(void)
93+
{
94+
return g_ipstack;
26795
}

fullstack/freertos-wolfip-wolfmqtt/src/wolfip_freertos.h

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,9 @@
2222
#ifndef WOLFIP_FREERTOS_H
2323
#define WOLFIP_FREERTOS_H
2424

25-
#include "FreeRTOS.h"
26-
#include "task.h"
27-
#include "wolfip.h"
25+
#include "../../../wolfip/wolfip.h"
2826

29-
/* Global wolfIP instance */
30-
extern struct wolfIP *g_wolfip;
31-
32-
/* Network task configuration */
33-
#define WOLFIP_TASK_PRIORITY (tskIDLE_PRIORITY + 2)
34-
#define WOLFIP_TASK_STACK_SIZE (8 * 1024)
35-
#define WOLFIP_POLL_INTERVAL_MS 10
36-
#define UDP_TEST_PORT 7777
37-
38-
/* Initialize wolfIP with FreeRTOS */
39-
int wolfIP_FreeRTOS_Init(void);
40-
41-
/* Start wolfIP network task */
42-
int wolfIP_FreeRTOS_Start(void);
43-
44-
/* Start UDP echo server task */
45-
int wolfIP_Start_UDP_Echo(void);
27+
int wolfip_init(void);
28+
struct wolfIP* wolfip_get_stack(void);
4629

4730
#endif /* WOLFIP_FREERTOS_H */

fullstack/freertos-wolfip-wolfmqtt/test_mqtt.sh

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,10 @@ if [ "$EUID" -ne 0 ]; then
1010
fi
1111

1212
# Certificate paths (relative to wolfssl-examples repo)
13-
CA_CERT="../../../wolfssl/certs/ca-cert.pem"
14-
CLIENT_CERT="../../../wolfssl/certs/client-cert.pem"
15-
CLIENT_KEY="../../../wolfssl/certs/client-key.pem"
13+
CERT_PATH=$(realpath ../../../wolfssl/certs)
1614

1715
# Verify certificate files exist
18-
for cert in "$CA_CERT" "$CLIENT_CERT" "$CLIENT_KEY"; do
16+
for cert in "$CERT_PATH/ca-cert.pem" "$CERT_PATH/client-cert.pem" "$CERT_PATH/client-key.pem"; do
1917
if [ ! -f "$cert" ]; then
2018
echo "Error: Certificate file not found: $cert"
2119
exit 1
@@ -25,9 +23,9 @@ done
2523
# Start mosquitto subscriber for testing with TLS
2624
echo "Starting MQTT subscriber on test/topic with TLS..."
2725
mosquitto_sub -h 10.10.0.1 -p 8883 \
28-
--cafile "$CA_CERT" \
29-
--cert "$CLIENT_CERT" \
30-
--key "$CLIENT_KEY" \
26+
--cafile "$CERT_PATH/ca-cert.pem" \
27+
--cert "$CERT_PATH/client-cert.pem" \
28+
--key "$CERT_PATH/client-key.pem" \
3129
--tls-version tlsv1.3 \
3230
-t "test/topic" -v &
3331
SUB_PID=$!

0 commit comments

Comments
 (0)