Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Zephyr port #333

Merged
merged 9 commits into from
May 17, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,9 @@ examples/sn-client/sn-client_qos-1
examples/sn-client/sn-multithread
examples/multithread/multithread
examples/wiot/wiot

# eclipse
.cproject
.project
.settings

29 changes: 28 additions & 1 deletion examples/mqttclient/mqttclient.c
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ int mqttclient_test(MQTTCtx *mqttCtx)
}
#endif

int main(int argc, char** argv)
static int run_client(int argc, char** argv)
{
int rc;
MQTTCtx mqttCtx;
Expand All @@ -709,6 +709,21 @@ int main(int argc, char** argv)
mqttCtx.app_name = "mqttclient";
mqttCtx.message = DEFAULT_MESSAGE;

#ifdef WOLFMQTT_ZEPHYR
#ifdef CONFIG_NET_CONFIG_PEER_IPV4_ADDR
mqttCtx.host = CONFIG_NET_CONFIG_PEER_IPV4_ADDR;
#endif
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can specify host with -h option
mqttclient -h <host_name>

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead, define DEFAULT_MQTT_HOST in user_settings.h. This already has an #ifndef check in examples/mqttexample.h

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using DEFAULT_MQTT_HOST.

#ifdef ENABLE_MQTT_TLS
mqttCtx.use_tls = 1;
#endif
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can specify TLS with -t option
mqttclient -t

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using WOLFMQTT_DEFAULT_TLS in user settings.

#ifdef CONFIG_NET_CONFIG_PEER_PORT
mqttCtx.port = CONFIG_NET_CONFIG_PEER_PORT;
#endif
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can specify port with -p option
mqttclient -p <port_number>

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using the default ports that depend on TLS.

#ifdef WOLFMQTT_TOPIC
mqttCtx.topic_name = WOLFMQTT_TOPIC;
#endif
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can specify topic name with -n option
mqttclient -n <topic_name>

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This example is for the Zephyr RTOS. I don't know how to pass in command line parameters there. I was also following the way that the Zephyr MQTT example defines the host, port, and topic.

Copy link
Member

@embhorn embhorn May 1, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about defining WOLFMQTT_TOPIC_NAME in user_settings.h ?

You'll need to add a #ifndef check in examples/mqttexample.h around the default definition.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using WOLFMQTT_TOPIC_NAME.

#endif

/* parse arguments */
rc = mqtt_parse_args(&mqttCtx, argc, argv);
if (rc != 0) {
Expand Down Expand Up @@ -738,4 +753,16 @@ int main(int argc, char** argv)
return (rc == 0) ? 0 : EXIT_FAILURE;
}

#ifdef WOLFMQTT_ZEPHYR
void main(void)
{
(void)run_client(0, NULL);
}
#else
int main(int argc, char** argv)
{
return run_client(argc, argv);
}
#endif

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be irrelevant now:
zephyrproject-rtos/zephyr#54628

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested on the latest release. I'll check with master when I get back.

#endif /* NO_MAIN_DRIVER */
59 changes: 34 additions & 25 deletions examples/mqttexample.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "wolfmqtt/mqtt_client.h"
#include "examples/mqttexample.h"
#include "examples/mqttnet.h"
#include "examples/mqttport.h"


/* locals */
Expand Down Expand Up @@ -464,7 +465,7 @@ void mqtt_free_ctx(MQTTCtx* mqttCtx)
}
}

#if defined(__GNUC__) && !defined(NO_EXIT)
#if defined(__GNUC__) && !defined(NO_EXIT) && !defined(WOLFMQTT_ZEPHYR)
__attribute__ ((noreturn))
#endif
int err_sys(const char* msg)
Expand All @@ -473,6 +474,14 @@ int err_sys(const char* msg)
PRINTF("wolfMQTT error: %s", msg);
}
exit(EXIT_FAILURE);
#ifdef WOLFMQTT_ZEPHYR
/* Zephyr compiler produces below warning. Let's silence it.
* warning: 'noreturn' function does return
* 477 | }
* | ^
*/
return 0;
#endif
}


Expand Down Expand Up @@ -632,31 +641,31 @@ int mqtt_tls_cb(MqttClient* client)
return rc;
}
}
#else
#if 0
#elif defined(WOLFMQTT_ZEPHYR)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is for #if !defined(NO_FILESYSTEM). Would it make sense to have it be #else instead of #elif defined(WOLFMQTT_ZEPHYR) ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I gated it on WOLFMQTT_ZEPHYR due to the #if 0 that was in there. It achieves the same and new configurations that want this section to be active can add the configs here.

#ifdef WOLFSSL_ENCRYPTED_KEYS
/* Setup password callback for pkcs8 key */
wolfSSL_CTX_set_default_passwd_cb(client->tls.ctx,
mqtt_password_cb);
#endif
/* Examples for loading buffer directly */
/* Load CA certificate buffer */
rc = wolfSSL_CTX_load_verify_buffer(client->tls.ctx,
(const byte*)root_ca, (long)XSTRLEN(root_ca), WOLFSSL_FILETYPE_PEM);
rc = wolfSSL_CTX_load_verify_buffer_ex(client->tls.ctx,
(const byte*)root_ca, (long)sizeof(root_ca),
WOLFSSL_FILETYPE_ASN1, 0, WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY);

/* Load Client Cert */
if (rc == WOLFSSL_SUCCESS)
if (rc == WOLFSSL_SUCCESS) {
rc = wolfSSL_CTX_use_certificate_buffer(client->tls.ctx,
(const byte*)device_cert, (long)XSTRLEN(device_cert),
WOLFSSL_FILETYPE_PEM);

#ifdef WOLFSSL_ENCRYPTED_KEYS
/* Setup password callback for pkcs8 key */
wolfSSL_CTX_set_default_passwd_cb(client->tls.ctx,
mqtt_password_cb);
#endif
(const byte*)device_cert, (long)sizeof(device_cert),
WOLFSSL_FILETYPE_ASN1);
}

/* Load Private Key */
if (rc == WOLFSSL_SUCCESS)
if (rc == WOLFSSL_SUCCESS) {
rc = wolfSSL_CTX_use_PrivateKey_buffer(client->tls.ctx,
(const byte*)device_priv_key, (long)XSTRLEN(device_priv_key),
WOLFSSL_FILETYPE_PEM);
#endif
(const byte*)device_priv_key, (long)sizeof(device_priv_key),
WOLFSSL_FILETYPE_ASN1);
}
#endif /* !NO_FILESYSTEM */
#endif /* !NO_CERT */
#ifdef HAVE_SNI
Expand Down Expand Up @@ -712,7 +721,7 @@ int mqtt_file_load(const char* filePath, byte** fileBuf, int *fileLen)
{
#if !defined(NO_FILESYSTEM)
int rc = 0;
FILE* file = NULL;
XFILE file = NULL;
long int pos = -1L;

/* Check arguments */
Expand All @@ -722,29 +731,29 @@ int mqtt_file_load(const char* filePath, byte** fileBuf, int *fileLen)
}

/* Open file */
file = fopen(filePath, "rb");
file = XFOPEN(filePath, "rb");
if (file == NULL) {
PRINTF("File '%s' does not exist!", filePath);
rc = EXIT_FAILURE;
goto exit;
}

/* Determine length of file */
if (fseek(file, 0, SEEK_END) != 0) {
if (XFSEEK(file, 0, XSEEK_END) != 0) {
PRINTF("fseek() failed");
rc = EXIT_FAILURE;
goto exit;
}

pos = (int) ftell(file);
pos = (int)XFTELL(file);
if (pos == -1L) {
PRINTF("ftell() failed");
rc = EXIT_FAILURE;
goto exit;
}

*fileLen = (int)pos;
if (fseek(file, 0, SEEK_SET) != 0) {
if (XFSEEK(file, 0, XSEEK_SET) != 0) {
PRINTF("fseek() failed");
rc = EXIT_FAILURE;
goto exit;
Expand All @@ -762,7 +771,7 @@ int mqtt_file_load(const char* filePath, byte** fileBuf, int *fileLen)
}

/* Load file into buffer */
rc = (int)fread(*fileBuf, 1, *fileLen, file);
rc = (int)XFREAD(*fileBuf, 1, *fileLen, file);
if (rc != *fileLen) {
PRINTF("Error reading file! %d", rc);
rc = EXIT_FAILURE;
Expand All @@ -772,7 +781,7 @@ int mqtt_file_load(const char* filePath, byte** fileBuf, int *fileLen)

exit:
if (file) {
fclose(file);
XFCLOSE(file);
}
if (rc != 0) {
if (*fileBuf) {
Expand Down
2 changes: 1 addition & 1 deletion examples/mqttexample.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
/* For Linux/Mac */
#if !defined(FREERTOS) && !defined(USE_WINDOWS_API) && \
!defined(FREESCALE_MQX) && !defined(FREESCALE_KSDK_MQX) && \
!defined(MICROCHIP_MPLAB_HARMONY)
!defined(MICROCHIP_MPLAB_HARMONY) && !defined(WOLFMQTT_ZEPHYR)
/* Make sure its not explicitly disabled and not already defined */
#if !defined(WOLFMQTT_NO_STDIN_CAP) && \
!defined(WOLFMQTT_ENABLE_STDIN_CAP)
Expand Down
145 changes: 1 addition & 144 deletions examples/mqttnet.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,157 +27,14 @@
#include "wolfmqtt/mqtt_client.h"
#include "examples/mqttnet.h"
#include "examples/mqttexample.h"
#include "examples/mqttport.h"

/* FreeRTOS TCP */
#ifdef FREERTOS_TCP
#include "FreeRTOS.h"
#include "task.h"
#include "FreeRTOS_IP.h"
#include "FreeRTOS_DNS.h"
#include "FreeRTOS_Sockets.h"

#define SOCKET_T Socket_t
#define SOCK_ADDR_IN struct freertos_sockaddr

/* ToppersOS and LWIP */
#elif defined(TOPPERS) && defined(WOLFSSL_LWIP)
/* lwIP includes. */
#include "lwip/api.h"
#include "lwip/tcpip.h"
#include "lwip/memp.h"
#include "lwip/stats.h"
#include "lwip/sockets.h"
#include "lwip/netdb.h"

/* FreeRTOS and LWIP */
#elif defined(FREERTOS) && defined(WOLFSSL_LWIP)
/* Scheduler includes. */
#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"

/* lwIP includes. */
#include "lwip/api.h"
#include "lwip/tcpip.h"
#include "lwip/memp.h"
#include "lwip/stats.h"
#include "lwip/sockets.h"
#include "lwip/netdb.h"

/* Windows */
#elif defined(USE_WINDOWS_API)
#include <winsock2.h>
#include <ws2tcpip.h>
#include <stdio.h>
#define SOCKET_T SOCKET
#ifdef _WIN32
#define SOERROR_T int
#else
#define SOERROR_T char
#endif
#define SELECT_FD(fd) (fd)
#ifndef SOCKET_INVALID /* Do not redefine from wolfssl */
#define SOCKET_INVALID ((SOCKET_T)INVALID_SOCKET)
#endif
#define SOCK_CLOSE closesocket
#define SOCK_SEND(s,b,l,f) send((s), (const char*)(b), (size_t)(l), (f))
#define SOCK_RECV(s,b,l,f) recv((s), (char*)(b), (size_t)(l), (f))
#define GET_SOCK_ERROR(f,s,o,e) (e) = WSAGetLastError()
#define SOCK_EQ_ERROR(e) (((e) == WSAEWOULDBLOCK) || ((e) == WSAEINPROGRESS))

/* Freescale MQX / RTCS */
#elif defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
#if defined(FREESCALE_MQX)
#include <posix.h>
#endif
#include <rtcs.h>
/* Note: Use "RTCS_geterror(sock->fd);" to get error number */
#define SOCKET_INVALID RTCS_SOCKET_ERROR
#define SOCKET_T uint32_t
#define SOCK_CLOSE closesocket
#define SOCK_OPEN RTCS_socket

/* Microchip MPLABX Harmony, TCP/IP */
#elif defined(MICROCHIP_MPLAB_HARMONY)
#include "app.h"
#include "system_config.h"
#include "tcpip/tcpip.h"
#include <sys/errno.h>
#include <errno.h>

#define SOCKET_INVALID (-1)
#define SOCK_CLOSE closesocket

#ifndef WOLFMQTT_NONBLOCK
#error wolfMQTT must be built with WOLFMQTT_NONBLOCK defined for Harmony
#endif

/* Linux */
#else
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/param.h>
#include <sys/time.h>
#include <sys/select.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#endif

/* Setup defaults */
#ifndef SOCK_OPEN
#define SOCK_OPEN socket
#endif
#ifndef SOCKET_T
#define SOCKET_T int
#endif
#ifndef SOERROR_T
#define SOERROR_T int
#endif
#ifndef SELECT_FD
#define SELECT_FD(fd) ((fd) + 1)
#endif
#ifndef SOCKET_INVALID
#define SOCKET_INVALID ((SOCKET_T)0)
#endif
#ifndef SOCK_CONNECT
#define SOCK_CONNECT connect
#endif
#ifndef SOCK_SEND
#define SOCK_SEND(s,b,l,f) send((s), (b), (size_t)(l), (f))
#endif
#ifndef SOCK_RECV
#define SOCK_RECV(s,b,l,f) recv((s), (b), (size_t)(l), (f))
#endif
#ifndef SOCK_CLOSE
#define SOCK_CLOSE close
#endif
#ifndef SOCK_ADDR_IN
#define SOCK_ADDR_IN struct sockaddr_in
#endif
#ifdef SOCK_ADDRINFO
#define SOCK_ADDRINFO struct addrinfo
#endif
#ifndef GET_SOCK_ERROR
#define GET_SOCK_ERROR(f,s,o,e) \
socklen_t len = sizeof(so_error); \
getsockopt((f), (s), (o), &(e), &len)
#endif
#ifndef SOCK_EQ_ERROR
#define SOCK_EQ_ERROR(e) (((e) == EWOULDBLOCK) || ((e) == EAGAIN))
#endif
/* Local context for Net callbacks */
typedef enum {
SOCK_BEGIN = 0,
SOCK_CONN
} NB_Stat;


#if 0 /* TODO: add multicast support */
typedef struct MulticastCtx {

Expand Down
Loading