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 all commits
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
1 change: 1 addition & 0 deletions .github/workflows/cifuzz.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ on: [pull_request]
jobs:
Fuzzing:
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- name: Build Fuzzers
id: build
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/fsanitize-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ jobs:
build:

runs-on: ubuntu-latest
timeout-minutes: 5

steps:
- uses: actions/checkout@master
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/macos-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ jobs:
build:

runs-on: macos-latest
timeout-minutes: 10
steps:
- uses: actions/checkout@master
with:
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/ubuntu-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ jobs:
build:

runs-on: ubuntu-latest
timeout-minutes: 5

steps:
- uses: actions/checkout@master
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/windows-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ env:
jobs:
build:
runs-on: windows-latest
timeout-minutes: 10

steps:
- uses: actions/checkout@v2
Expand Down
130 changes: 130 additions & 0 deletions .github/workflows/zephyr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
name: Zephyr tests

on:
push:
branches: [ '*' ]
pull_request:
branches: [ '*' ]

jobs:
run_test:
name: Build and run
runs-on: ubuntu-latest
timeout-minutes: 20
steps:
- name: Install dependencies
run: |
# Don't prompt for anything
export DEBIAN_FRONTEND=noninteractive
sudo apt-get update
# most of the ci-base zephyr docker image packages
sudo apt-get install -y mosquitto mosquitto-clients zip bridge-utils uml-utilities \
git cmake ninja-build gperf ccache dfu-util device-tree-compiler wget \
python3-dev python3-pip python3-setuptools python3-tk python3-wheel xz-utils file \
make gcc gcc-multilib g++-multilib libsdl2-dev libmagic1 \
autoconf automake bison build-essential ca-certificates cargo ccache chrpath cmake \
cpio device-tree-compiler dfu-util diffstat dos2unix doxygen file flex g++ gawk gcc \
gcovr git git-core gnupg gperf gtk-sharp2 help2man iproute2 lcov libcairo2-dev \
libglib2.0-dev libgtk2.0-0 liblocale-gettext-perl libncurses5-dev libpcap-dev \
libpopt0 libsdl1.2-dev libsdl2-dev libssl-dev libtool libtool-bin locales make \
net-tools ninja-build openssh-client parallel pkg-config python3-dev python3-pip \
python3-ply python3-setuptools python-is-python3 qemu rsync socat srecord sudo \
texinfo unzip wget ovmf xz-utils

- name: Install west
run: sudo pip install west

- name: Init west workspace
run: west init zephyr

- name: Update west.yml
working-directory: zephyr/zephyr
run: |
REF=$(echo '${{ github.ref }}' | sed -e 's/\//\\\//g')
sed -e 's/remotes:/remotes:\n \- name: wolfssl\n url\-base: https:\/\/github.com\/wolfssl/' -i west.yml
sed -e "s/remotes:/remotes:\n \- name: wolfmqtt\n url\-base: https:\/\/github.com\/${{ github.repository_owner }}/" -i west.yml
sed -e "s/projects:/projects:\n \- name: wolfMQTT\n path: modules\/lib\/wolfmqtt\n remote: wolfmqtt\n revision: $REF/" -i west.yml
sed -e 's/projects:/projects:\n \- name: wolfssl\n path: modules\/crypto\/wolfssl\n remote: wolfssl\n revision: master/' -i west.yml

- name: Update west workspace
working-directory: zephyr
run: west update -n -o=--depth=1

- name: Export zephyr
working-directory: zephyr
run: west zephyr-export

- name: Install pip dependencies
working-directory: zephyr
run: sudo pip install -r zephyr/scripts/requirements.txt

- name: Install zephyr SDK
run: |
wget -q https://github.com/zephyrproject-rtos/sdk-ng/releases/download/v0.16.1/zephyr-sdk-0.16.1_linux-x86_64.tar.xz
tar xf zephyr-sdk-0.16.1_linux-x86_64.tar.xz
cd zephyr-sdk-0.16.1
./setup.sh -h -c

- name: Setup qemu networking
working-directory: zephyr/tools/net-tools
run: |
make
./loop-socat.sh &
sleep 1 # let the networks set everything up
sudo ./loop-slip-tap.sh -v &
sleep 1 # let the networks set everything up

- name: Setup mosquitto broker and sub
working-directory: zephyr/modules/lib/wolfmqtt
run: |
# Disable default broker daemon
sudo service mosquitto stop
mosquitto -c scripts/broker_test/mosquitto.conf &> broker.log &
sleep 1 # let the broker set everything up
mosquitto_sub -t sensors &> sub.log &

# This is some debug info useful if something goes wrong
- name: Show network status
run: |
sudo ifconfig
sudo route
sudo netstat -tulpan

- name: Run client
id: client-test
working-directory: zephyr
run: |
./zephyr/scripts/twister --testsuite-root modules/lib/wolfmqtt --test zephyr/samples/client/sample.lib.wolfmqtt_client -vvv
rm -rf zephyr/twister-out

- name: Check output
run: |
grep test zephyr/modules/lib/wolfmqtt/sub.log
> zephyr/modules/lib/wolfmqtt/sub.log # clear file

- name: Run tls client
id: client-tls-test
working-directory: zephyr
run: |
./zephyr/scripts/twister --testsuite-root modules/lib/wolfmqtt --test zephyr/samples/client_tls/sample.lib.wolfmqtt_client_tls -vvv
rm -rf zephyr/twister-out

- name: Check output
run: |
grep test zephyr/modules/lib/wolfmqtt/sub.log
> zephyr/modules/lib/wolfmqtt/sub.log # clear file

- name: Zip failure logs
if: ${{ failure() && (steps.client-test.outcome == 'failure' || steps.client-tls-test.outcome == 'failure') }}
run: |
zip -9 -r logs.zip zephyr/twister-out
zip -9 logs.zip zephyr/modules/lib/wolfmqtt/broker.log \
zephyr/modules/lib/wolfmqtt/sub.log

- name: Upload failure logs
if: ${{ failure() && (steps.client-test.outcome == 'failure' || steps.client-tls-test.outcome == 'failure') }}
uses: actions/upload-artifact@v3
with:
name: zephyr-client-test-logs
path: logs.zip
retention-days: 5
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

4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ cd ../wolfmqtt
make
```

### Zephyr RTOS

Support for Zephyr is available in the [zephyr](zephyr) directory. For instructions on how to build for Zephyr, see the [README.md](zephyr/README.md).

## Architecture

The library has three components.
Expand Down
4 changes: 0 additions & 4 deletions examples/mqttclient/mqttclient.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
#include <config.h>
#endif

#include "wolfmqtt/mqtt_client.h"

#include "mqttclient.h"
#include "examples/mqttnet.h"

Expand Down Expand Up @@ -706,8 +704,6 @@ int main(int argc, char** argv)

/* init defaults */
mqtt_init_ctx(&mqttCtx);
mqttCtx.app_name = "mqttclient";
mqttCtx.message = DEFAULT_MESSAGE;

/* parse arguments */
rc = mqtt_parse_args(&mqttCtx, argc, argv);
Expand Down
64 changes: 39 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 @@ -263,6 +264,11 @@ void mqtt_init_ctx(MQTTCtx* mqttCtx)
mqttCtx->topic_alias = 1;
mqttCtx->topic_alias_max = 1;
#endif
#ifdef WOLFMQTT_DEFAULT_TLS
mqttCtx->use_tls = WOLFMQTT_DEFAULT_TLS;
#endif
mqttCtx->app_name = "mqttclient";
mqttCtx->message = DEFAULT_MESSAGE;
}

int mqtt_parse_args(MQTTCtx* mqttCtx, int argc, char** argv)
Expand Down Expand Up @@ -464,7 +470,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 +479,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 +646,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 +726,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 +736,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 +776,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 +786,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
12 changes: 9 additions & 3 deletions examples/mqttexample.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#ifndef WOLFMQTT_EXAMPLE_H
#define WOLFMQTT_EXAMPLE_H

#include "wolfmqtt/mqtt_client.h"

#ifdef __cplusplus
extern "C" {
#endif
Expand All @@ -41,7 +43,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 Expand Up @@ -77,8 +79,12 @@
#define DEFAULT_MQTT_QOS MQTT_QOS_0
#define DEFAULT_KEEP_ALIVE_SEC 60
#define DEFAULT_CLIENT_ID "WolfMQTTClient"
#define WOLFMQTT_TOPIC_NAME "wolfMQTT/example/"
#define DEFAULT_TOPIC_NAME WOLFMQTT_TOPIC_NAME"testTopic"
#ifndef WOLFMQTT_TOPIC_NAME
#define WOLFMQTT_TOPIC_NAME "wolfMQTT/example/"
#define DEFAULT_TOPIC_NAME WOLFMQTT_TOPIC_NAME"testTopic"
#else
#define DEFAULT_TOPIC_NAME WOLFMQTT_TOPIC_NAME
#endif
#define DEFAULT_AUTH_METHOD "EXTERNAL"
#define PRINT_BUFFER_SIZE 80
#define DEFAULT_MESSAGE "test"
Expand Down
Loading