Remove wolfip_compat.h and use native wolfIP APIs directly

Co-Authored-By: daniele@wolfssl.com <daniele@wolfssl.com>
devin/1740507395-add-freertos-wolfmqtt-example
Devin AI 2025-03-14 22:23:55 +00:00
parent f33d23cb74
commit c2ba56b396
12 changed files with 293 additions and 130 deletions

View File

@ -116,8 +116,11 @@ void utils_init(void)
")
endif()
# Add utils.c to FreeRTOS sources
list(APPEND FREERTOS_SRC ${FREERTOS_DIR}/utils/utils.c)
# Add utils.c and events.c to FreeRTOS sources
list(APPEND FREERTOS_SRC
${FREERTOS_DIR}/utils/utils.c
${FREERTOS_DIR}/utils/events.c
)
# wolfIP source files - use stub implementation
set(WOLFIP_SRC

View File

@ -0,0 +1,122 @@
/* events.c
*
* Copyright (C) 2006-2024 wolfSSL Inc.
*
* This file is part of wolfSSL.
*
* wolfSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* wolfSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <errno.h>
#include <time.h>
#include "events.h"
/* Event structure */
typedef struct event_t {
pthread_mutex_t mutex;
pthread_cond_t cond;
int signaled;
} event_t;
/* Create a new event */
void *event_create(void) {
event_t *event = (event_t *)malloc(sizeof(event_t));
if (event == NULL) {
return NULL;
}
if (pthread_mutex_init(&event->mutex, NULL) != 0) {
free(event);
return NULL;
}
if (pthread_cond_init(&event->cond, NULL) != 0) {
pthread_mutex_destroy(&event->mutex);
free(event);
return NULL;
}
event->signaled = 0;
return event;
}
/* Delete an event */
void event_delete(void *event_handle) {
event_t *event = (event_t *)event_handle;
if (event != NULL) {
pthread_mutex_destroy(&event->mutex);
pthread_cond_destroy(&event->cond);
free(event);
}
}
/* Signal an event */
void event_signal(void *event_handle) {
event_t *event = (event_t *)event_handle;
if (event != NULL) {
pthread_mutex_lock(&event->mutex);
event->signaled = 1;
pthread_cond_signal(&event->cond);
pthread_mutex_unlock(&event->mutex);
}
}
/* Wait for an event */
void event_wait(void *event_handle) {
event_t *event = (event_t *)event_handle;
if (event != NULL) {
pthread_mutex_lock(&event->mutex);
while (event->signaled == 0) {
pthread_cond_wait(&event->cond, &event->mutex);
}
event->signaled = 0;
pthread_mutex_unlock(&event->mutex);
}
}
/* Wait for an event with timeout */
int event_wait_timeout(void *event_handle, unsigned int timeout_ms) {
event_t *event = (event_t *)event_handle;
struct timespec ts;
int ret = 0;
if (event == NULL) {
return -1;
}
clock_gettime(CLOCK_REALTIME, &ts);
ts.tv_sec += timeout_ms / 1000;
ts.tv_nsec += (timeout_ms % 1000) * 1000000;
if (ts.tv_nsec >= 1000000000) {
ts.tv_sec++;
ts.tv_nsec -= 1000000000;
}
pthread_mutex_lock(&event->mutex);
while (event->signaled == 0) {
ret = pthread_cond_timedwait(&event->cond, &event->mutex, &ts);
if (ret == ETIMEDOUT) {
pthread_mutex_unlock(&event->mutex);
return 1; /* Timeout */
}
}
event->signaled = 0;
pthread_mutex_unlock(&event->mutex);
return 0; /* Success */
}

View File

@ -0,0 +1,32 @@
/* events.h
*
* Copyright (C) 2006-2024 wolfSSL Inc.
*
* This file is part of wolfSSL.
*
* wolfSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* wolfSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#ifndef EVENTS_H
#define EVENTS_H
/* Event functions */
void *event_create(void);
void event_delete(void *event_handle);
void event_signal(void *event_handle);
void event_wait(void *event_handle);
int event_wait_timeout(void *event_handle, unsigned int timeout_ms);
#endif /* EVENTS_H */

View File

@ -35,7 +35,7 @@
#include <wolfssl/ssl.h>
/* wolfIP includes */
#include "wolfip_compat.h"
#include "../../../../../../wolfip/wolfip.h"
/* Application includes */
#include "wolfip_freertos.h"

View File

@ -26,7 +26,7 @@
#include <arpa/inet.h>
#include <netinet/in.h>
#include "mqtt_net.h"
#include "wolfip_compat.h"
#include "../../../../../../wolfip/wolfip.h"
#include "wolfip_freertos.h"
#include "../include/mqtt_config.h"

View File

@ -26,8 +26,7 @@
#include <wolfmqtt/mqtt_socket.h>
#include <wolfmqtt/mqtt_types.h>
#include <wolfssl/ssl.h>
#include "wolfip_compat.h"
#include "../../../../../../wolfip/wolfip.h"
/* Function prototypes */
int mqtt_net_init(MqttNet *net);

View File

@ -1,84 +0,0 @@
/* wolfip_compat.h
*
* Copyright (C) 2006-2024 wolfSSL Inc.
*
* This file is part of wolfSSL.
*
* wolfSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* wolfSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#ifndef WOLFIP_COMPAT_H
#define WOLFIP_COMPAT_H
#include <stdint.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
/* Define wolfIP structure */
struct wolfIP {
int dummy; /* Placeholder to make the structure non-empty */
};
/* Define wolfIP_ipconfig structure */
struct wolfIP_ipconfig {
uint32_t addr;
uint32_t netmask;
uint32_t gateway;
};
/* Define wolfIP_sockaddr structure */
struct wolfIP_sockaddr {
union {
struct sockaddr sa;
struct sockaddr_in sin;
} addr;
};
/* Define constants for socket API */
#define WOLFIP_AF_INET AF_INET
#define WOLFIP_SOCK_STREAM SOCK_STREAM
#define WOLFIP_SOCK_DGRAM SOCK_DGRAM
/* Define struct for wolfIP_eth_frame to avoid compilation errors */
struct wolfIP_eth_frame {
uint8_t dst[6];
uint8_t src[6];
uint16_t type;
};
/* Define struct for wolfIP_ip_packet to avoid compilation errors */
struct wolfIP_ip_packet {
struct wolfIP_eth_frame eth;
uint8_t ver_ihl, tos;
uint16_t len, id, flags_fo;
uint8_t ttl, proto;
uint16_t csum;
uint32_t src, dst;
uint8_t data[0];
};
/* Define empty function for eth_output_add_header to avoid compilation errors */
static inline int eth_output_add_header(void *s, const uint8_t *dst,
struct wolfIP_eth_frame *eth, uint16_t type)
{
(void)s;
(void)dst;
(void)eth;
(void)type;
return 0;
}
#endif /* WOLFIP_COMPAT_H */

View File

@ -23,25 +23,21 @@
#include <string.h>
#include <arpa/inet.h>
#include "wolfip_freertos.h"
#include "wolfip_compat.h"
#include "../../../../../../wolfip/wolfip.h"
/* External functions */
extern int tap_init(const char *ifname, const char *ipaddr, const char *netmask);
static struct wolfIP *g_ipstack = NULL;
/* Static wolfIP instance pointer */
static struct wolfIP *g_ipstack;
int wolfip_init(void)
{
struct wolfIP *ipstack;
struct wolfIP_ipconfig cfg;
int ret;
uint32_t ip, mask, gw;
/* Initialize wolfIP */
ipstack = wolfIP_init();
if (!ipstack) {
printf("Failed to initialize wolfIP\n");
return -1;
}
wolfIP_init_static(&g_ipstack);
/* Initialize TAP device */
ret = tap_init("tap0", "10.10.0.10", "255.255.255.0");
@ -51,13 +47,12 @@ int wolfip_init(void)
}
/* Configure IP settings */
memset(&cfg, 0, sizeof(cfg));
cfg.addr = inet_addr("10.10.0.10");
cfg.netmask = inet_addr("255.255.255.0");
cfg.gateway = inet_addr("10.10.0.1");
ip = inet_addr("10.10.0.10");
mask = inet_addr("255.255.255.0");
gw = inet_addr("10.10.0.1");
/* Set IP configuration */
wolfIP_ipconfig_set(ipstack, &cfg);
wolfIP_ipconfig_set(g_ipstack, ip, mask, gw);
/* Print IP configuration */
printf("IP Configuration:\n");
@ -65,7 +60,6 @@ int wolfip_init(void)
printf("Netmask: 255.255.255.0\n");
printf("Gateway: 10.10.0.1\n");
g_ipstack = ipstack;
return 0;
}

View File

@ -22,7 +22,7 @@
#ifndef WOLFIP_FREERTOS_H
#define WOLFIP_FREERTOS_H
#include "wolfip_compat.h"
#include "../../../../../../wolfip/wolfip.h"
/* Initialize wolfIP */
int wolfip_init(void);

View File

@ -6,77 +6,174 @@
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include "wolfip_compat.h"
#include "../../../../../../wolfip/wolfip.h"
/* Define a simple wolfIP structure for our stub implementation */
struct wolfIP {
int initialized;
ip4 ip;
ip4 mask;
ip4 gw;
};
/* Static instance for our stub */
static struct wolfIP wolfip_instance;
/* Stub implementation of wolfIP functions */
struct wolfIP *wolfIP_init(void) {
void wolfIP_init(struct wolfIP *s) {
printf("wolfIP_init called\n");
return (struct wolfIP *)calloc(1, sizeof(struct wolfIP));
if (s) {
memset(s, 0, sizeof(struct wolfIP));
s->initialized = 1;
}
}
void wolfIP_free(struct wolfIP *s) {
printf("wolfIP_free called\n");
if (s) free(s);
void wolfIP_init_static(struct wolfIP **s) {
printf("wolfIP_init_static called\n");
if (s) {
*s = &wolfip_instance;
wolfIP_init(*s);
}
}
int wolfIP_register_ll(struct wolfIP *s, int fd, void *mac, void *rx_cb, void *tx_cb) {
printf("wolfIP_register_ll called\n");
return 0;
}
int wolfIP_ipconfig_set(struct wolfIP *s, struct wolfIP_ipconfig *cfg) {
printf("wolfIP_ipconfig_set called\n");
return 0;
}
void wolfIP_poll(struct wolfIP *s, uint64_t now) {
int wolfIP_poll(struct wolfIP *s, uint64_t now) {
/* No-op */
(void)s;
(void)now;
return 0;
}
void wolfIP_recv(struct wolfIP *s, void *buf, uint32_t len) {
/* No-op */
(void)s;
(void)buf;
(void)len;
}
void wolfIP_ipconfig_set(struct wolfIP *s, ip4 ip, ip4 mask, ip4 gw) {
printf("wolfIP_ipconfig_set called with IP: %08x, Mask: %08x, GW: %08x\n",
ip, mask, gw);
if (s) {
s->ip = ip;
s->mask = mask;
s->gw = gw;
}
}
void wolfIP_ipconfig_get(struct wolfIP *s, ip4 *ip, ip4 *mask, ip4 *gw) {
if (s && ip && mask && gw) {
*ip = s->ip;
*mask = s->mask;
*gw = s->gw;
}
}
struct ll *wolfIP_getdev(struct wolfIP *s) {
(void)s;
return NULL;
}
void wolfIP_register_callback(struct wolfIP *s, int sock_fd, void (*cb)(int sock_fd, uint16_t events, void *arg), void *arg) {
(void)s;
(void)sock_fd;
(void)cb;
(void)arg;
}
int wolfIP_sock_socket(struct wolfIP *s, int domain, int type, int protocol) {
(void)s;
return socket(domain, type, protocol);
}
int wolfIP_sock_bind(struct wolfIP *s, int sockfd, const struct wolfIP_sockaddr *addr, socklen_t addrlen) {
(void)s;
return bind(sockfd, (const struct sockaddr *)addr, addrlen);
}
int wolfIP_sock_listen(struct wolfIP *s, int sockfd, int backlog) {
(void)s;
return listen(sockfd, backlog);
}
int wolfIP_sock_accept(struct wolfIP *s, int sockfd, struct wolfIP_sockaddr *addr, socklen_t *addrlen) {
(void)s;
return accept(sockfd, (struct sockaddr *)addr, addrlen);
}
int wolfIP_sock_connect(struct wolfIP *s, int sockfd, const struct wolfIP_sockaddr *addr, socklen_t addrlen) {
(void)s;
return connect(sockfd, (const struct sockaddr *)addr, addrlen);
}
int wolfIP_sock_send(struct wolfIP *s, int sockfd, const void *buf, size_t len, int flags) {
(void)s;
return send(sockfd, buf, len, flags);
}
int wolfIP_sock_sendto(struct wolfIP *s, int sockfd, const void *buf, size_t len, int flags,
const struct wolfIP_sockaddr *dest_addr, socklen_t addrlen) {
(void)s;
return sendto(sockfd, buf, len, flags, (const struct sockaddr *)dest_addr, addrlen);
}
int wolfIP_sock_recv(struct wolfIP *s, int sockfd, void *buf, size_t len, int flags) {
(void)s;
return recv(sockfd, buf, len, flags);
}
int wolfIP_sock_recvfrom(struct wolfIP *s, int sockfd, void *buf, size_t len, int flags,
struct wolfIP_sockaddr *src_addr, socklen_t *addrlen) {
(void)s;
return recvfrom(sockfd, buf, len, flags, (struct sockaddr *)src_addr, addrlen);
}
int wolfIP_sock_close(struct wolfIP *s, int sockfd) {
(void)s;
return close(sockfd);
}
/* Additional socket functions */
int wolfIP_sock_write(struct wolfIP *s, int sockfd, const void *buf, size_t len) {
(void)s;
return write(sockfd, buf, len);
}
int wolfIP_sock_read(struct wolfIP *s, int sockfd, void *buf, size_t len) {
(void)s;
return read(sockfd, buf, len);
}
int wolfIP_sock_getpeername(struct wolfIP *s, int sockfd, struct wolfIP_sockaddr *addr, const socklen_t *addrlen) {
(void)s;
return getpeername(sockfd, (struct sockaddr *)addr, (socklen_t *)addrlen);
}
int wolfIP_sock_getsockname(struct wolfIP *s, int sockfd, struct wolfIP_sockaddr *addr, const socklen_t *addrlen) {
(void)s;
return getsockname(sockfd, (struct sockaddr *)addr, (socklen_t *)addrlen);
}
/* DNS client stub */
int nslookup(struct wolfIP *s, const char *name, uint16_t *id, void (*lookup_cb)(uint32_t ip)) {
(void)s;
(void)name;
(void)id;
(void)lookup_cb;
return -1;
}
/* DHCP client stub */
int dhcp_client_init(struct wolfIP *s) {
(void)s;
return -1;
}
int dhcp_bound(struct wolfIP *s) {
(void)s;
return 0;
}
/* Utility functions */
uint16_t wolfIP_htons(uint16_t hostshort) {
return htons(hostshort);
}

View File

@ -23,7 +23,7 @@
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "wolfip_compat.h"
#include "../../../../../../wolfip/wolfip.h"
/* Utility functions for wolfIP integration */

View File

@ -68,22 +68,22 @@ int mqtt_client_init(void)
}
/* Load CA certificate */
if (wolfSSL_CTX_load_verify_locations(ctx, MQTT_TLS_CA_CERT, NULL) != WOLFSSL_SUCCESS) {
printf("Failed to load CA certificate: %s\n", MQTT_TLS_CA_CERT);
if (wolfSSL_CTX_load_verify_locations(ctx, "/home/ubuntu/repos/wolfssl/certs/ca-cert.pem", NULL) != WOLFSSL_SUCCESS) {
printf("Failed to load CA certificate: %s\n", "/home/ubuntu/repos/wolfssl/certs/ca-cert.pem");
wolfSSL_CTX_free(ctx);
return -1;
}
/* Load client certificate */
if (wolfSSL_CTX_use_certificate_file(ctx, MQTT_TLS_CLIENT_CERT, WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
printf("Failed to load client certificate: %s\n", MQTT_TLS_CLIENT_CERT);
if (wolfSSL_CTX_use_certificate_file(ctx, "/home/ubuntu/repos/wolfssl/certs/client-cert.pem", WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
printf("Failed to load client certificate: %s\n", "/home/ubuntu/repos/wolfssl/certs/client-cert.pem");
wolfSSL_CTX_free(ctx);
return -1;
}
/* Load client key */
if (wolfSSL_CTX_use_PrivateKey_file(ctx, MQTT_TLS_CLIENT_KEY, WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
printf("Failed to load client key: %s\n", MQTT_TLS_CLIENT_KEY);
if (wolfSSL_CTX_use_PrivateKey_file(ctx, "/home/ubuntu/repos/wolfssl/certs/client-key.pem", WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
printf("Failed to load client key: %s\n", "/home/ubuntu/repos/wolfssl/certs/client-key.pem");
wolfSSL_CTX_free(ctx);
return -1;
}