Skip to content

Commit 7617c27

Browse files
Fix wolfIP build integration and add random number generator
Co-Authored-By: daniele@wolfssl.com <daniele@wolfssl.com>
1 parent e1f6c36 commit 7617c27

4 files changed

Lines changed: 220 additions & 3 deletions

File tree

fullstack/freertos-wolfip-wolfssl-https/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ include_directories(
1414
${CMAKE_CURRENT_SOURCE_DIR}/include
1515
${CMAKE_CURRENT_SOURCE_DIR}/freertos/FreeRTOS-Kernel/include
1616
${FREERTOS_PORT_DIR}
17+
/home/ubuntu/repos/wolfip/src
18+
/home/ubuntu/repos/wolfip
1719
)
1820

1921
# FreeRTOS source files
@@ -29,12 +31,19 @@ set(FREERTOS_SOURCES
2931
${CMAKE_CURRENT_SOURCE_DIR}/freertos/utils/utils.c
3032
)
3133

34+
# Add wolfIP library
35+
add_library(wolfip STATIC
36+
/home/ubuntu/repos/wolfip/src/wolfip.c
37+
)
38+
3239
# Add the main application
3340
add_executable(freertos_sim
3441
${FREERTOS_SOURCES}
3542
src/main.c
43+
src/wolfip_freertos.c
3644
)
3745

3846
target_link_libraries(freertos_sim
3947
pthread
48+
wolfip
4049
)

fullstack/freertos-wolfip-wolfssl-https/src/main.c

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,38 @@
11
#include <stdio.h>
22
#include "FreeRTOS.h"
33
#include "task.h"
4+
#include "wolfip_freertos.h"
45

56
static void testTask(void* pvParameters) {
67
const TickType_t xDelay = 1000 / portTICK_PERIOD_MS;
8+
int ret;
79

10+
printf("Initializing wolfIP...\n");
11+
ret = wolfIP_FreeRTOS_Init();
12+
if (ret != 0) {
13+
printf("Failed to initialize wolfIP\n");
14+
return;
15+
}
16+
17+
printf("Starting wolfIP network task...\n");
18+
ret = wolfIP_FreeRTOS_Start();
19+
if (ret != 0) {
20+
printf("Failed to start wolfIP network task\n");
21+
return;
22+
}
23+
24+
printf("Network stack running...\n");
825
for(;;) {
9-
printf("FreeRTOS Test Task Running\n");
1026
vTaskDelay(xDelay);
1127
}
1228
}
1329

1430
int main(void) {
15-
printf("Starting FreeRTOS simulation...\n");
31+
printf("Starting FreeRTOS with wolfIP...\n");
1632

1733
/* Create the test task */
18-
xTaskCreate(testTask, "TestTask", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 1, NULL);
34+
xTaskCreate(testTask, "TestTask", configMINIMAL_STACK_SIZE,
35+
NULL, tskIDLE_PRIORITY + 1, NULL);
1936

2037
/* Start the scheduler */
2138
vTaskStartScheduler();
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
#include "wolfip_freertos.h"
2+
#include <stdio.h>
3+
#include <string.h>
4+
#include <unistd.h>
5+
#include <fcntl.h>
6+
#include <sys/ioctl.h>
7+
#include <linux/if.h>
8+
#include <linux/if_tun.h>
9+
#include <arpa/inet.h>
10+
#include <sys/time.h>
11+
#include <poll.h>
12+
#include <sys/socket.h>
13+
#include <sys/random.h>
14+
15+
/* Implementation of wolfIP's required random number generator */
16+
uint32_t wolfIP_getrandom(void) {
17+
uint32_t ret;
18+
getrandom(&ret, sizeof(ret), 0);
19+
return ret;
20+
}
21+
22+
static struct wolfIP *g_wolfip = NULL;
23+
static TaskHandle_t g_network_task = NULL;
24+
static int tap_fd = -1;
25+
26+
/* TUN/TAP device functions */
27+
static int tap_init(struct ll *dev, const char *ifname) {
28+
struct ifreq ifr;
29+
int sock_fd;
30+
31+
if ((tap_fd = open("/dev/net/tun", O_RDWR)) < 0) {
32+
perror("Error opening /dev/net/tun");
33+
return -1;
34+
}
35+
36+
memset(&ifr, 0, sizeof(ifr));
37+
ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
38+
strncpy(ifr.ifr_name, ifname, IFNAMSIZ);
39+
40+
if (ioctl(tap_fd, TUNSETIFF, (void *)&ifr) < 0) {
41+
perror("ioctl TUNSETIFF");
42+
close(tap_fd);
43+
return -1;
44+
}
45+
46+
/* Get MAC address */
47+
if (ioctl(tap_fd, SIOCGIFHWADDR, &ifr) < 0) {
48+
perror("ioctl SIOCGIFHWADDR");
49+
close(tap_fd);
50+
return -1;
51+
}
52+
53+
strncpy(dev->ifname, ifname, sizeof(dev->ifname) - 1);
54+
memcpy(dev->mac, ifr.ifr_hwaddr.sa_data, 6);
55+
dev->mac[5] ^= 1; /* Make MAC unique */
56+
57+
/* Configure network interface */
58+
sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
59+
if (sock_fd < 0) {
60+
perror("socket");
61+
close(tap_fd);
62+
return -1;
63+
}
64+
65+
/* Set interface UP */
66+
if (ioctl(sock_fd, SIOCGIFFLAGS, &ifr) < 0) {
67+
perror("ioctl SIOCGIFFLAGS");
68+
close(sock_fd);
69+
return -1;
70+
}
71+
ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
72+
if (ioctl(sock_fd, SIOCSIFFLAGS, &ifr) < 0) {
73+
perror("ioctl SIOCSIFFLAGS");
74+
close(sock_fd);
75+
return -1;
76+
}
77+
78+
close(sock_fd);
79+
return 0;
80+
}
81+
82+
static int tap_poll(struct ll *ll, void *buf, uint32_t len) {
83+
struct pollfd pfd;
84+
int ret;
85+
86+
pfd.fd = tap_fd;
87+
pfd.events = POLLIN;
88+
ret = poll(&pfd, 1, 1); /* Short timeout */
89+
90+
if (ret < 0) {
91+
perror("poll");
92+
return -1;
93+
}
94+
if (ret == 0) {
95+
return 0;
96+
}
97+
98+
return read(tap_fd, buf, len);
99+
}
100+
101+
static int tap_send(struct ll *ll, void *buf, uint32_t len) {
102+
return write(tap_fd, buf, len);
103+
}
104+
105+
/* Network task implementation */
106+
static void wolfIP_NetworkTask(void *pvParameters) {
107+
TickType_t last_wake_time;
108+
const TickType_t frequency = pdMS_TO_TICKS(WOLFIP_POLL_INTERVAL_MS);
109+
struct timeval tv;
110+
111+
last_wake_time = xTaskGetTickCount();
112+
113+
while (1) {
114+
gettimeofday(&tv, NULL);
115+
wolfIP_poll(g_wolfip, tv.tv_sec * 1000 + tv.tv_usec / 1000);
116+
vTaskDelayUntil(&last_wake_time, frequency);
117+
}
118+
}
119+
120+
int wolfIP_FreeRTOS_Init(void) {
121+
struct ll *tapdev;
122+
123+
/* Initialize wolfIP */
124+
wolfIP_init_static(&g_wolfip);
125+
if (!g_wolfip) {
126+
printf("Failed to initialize wolfIP\n");
127+
return -1;
128+
}
129+
130+
/* Setup TUN/TAP interface */
131+
tapdev = wolfIP_getdev(g_wolfip);
132+
if (!tapdev) {
133+
printf("Failed to get device from wolfIP\n");
134+
return -1;
135+
}
136+
137+
/* Initialize TAP device */
138+
if (tap_init(tapdev, "wtap0") < 0) {
139+
printf("Failed to initialize TAP device\n");
140+
return -1;
141+
}
142+
143+
/* Set device callbacks */
144+
tapdev->poll = tap_poll;
145+
tapdev->send = tap_send;
146+
147+
/* Configure IP settings */
148+
wolfIP_ipconfig_set(g_wolfip,
149+
atoip4("192.168.1.10"), /* IP */
150+
atoip4("255.255.255.0"), /* Netmask */
151+
atoip4("192.168.1.1")); /* Gateway */
152+
153+
return 0;
154+
}
155+
156+
int wolfIP_FreeRTOS_Start(void) {
157+
BaseType_t ret;
158+
159+
if (!g_wolfip) {
160+
printf("wolfIP not initialized\n");
161+
return -1;
162+
}
163+
164+
ret = xTaskCreate(wolfIP_NetworkTask,
165+
"WolfIP_Net",
166+
WOLFIP_TASK_STACK_SIZE,
167+
NULL,
168+
WOLFIP_TASK_PRIORITY,
169+
&g_network_task);
170+
171+
return (ret == pdPASS) ? 0 : -1;
172+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#ifndef WOLFIP_FREERTOS_H
2+
#define WOLFIP_FREERTOS_H
3+
4+
#include "FreeRTOS.h"
5+
#include "task.h"
6+
#include "wolfip.h"
7+
8+
/* Network task configuration */
9+
#define WOLFIP_TASK_PRIORITY (tskIDLE_PRIORITY + 2)
10+
#define WOLFIP_TASK_STACK_SIZE (8 * 1024)
11+
#define WOLFIP_POLL_INTERVAL_MS 10
12+
13+
/* Initialize wolfIP with FreeRTOS */
14+
int wolfIP_FreeRTOS_Init(void);
15+
16+
/* Start wolfIP network task */
17+
int wolfIP_FreeRTOS_Start(void);
18+
19+
#endif /* WOLFIP_FREERTOS_H */

0 commit comments

Comments
 (0)