Skip to content

Commit

Permalink
feat(mosq): Add example with serverless brokers
Browse files Browse the repository at this point in the history
  • Loading branch information
david-cermak committed Dec 12, 2024
1 parent 8855c6d commit de0925a
Show file tree
Hide file tree
Showing 10 changed files with 497 additions and 1 deletion.
6 changes: 5 additions & 1 deletion .github/workflows/mosq__build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ jobs:
. ${IDF_PATH}/export.sh
pip install idf-component-manager idf-build-apps --upgrade
python ci/build_apps.py ${TEST_DIR}
cd ${TEST_DIR}
cd ${TEST_DIR}/../serverless_mqtt
# manual tests for now -- build both peers
idf.py -B build1 -DSDKCONFIG=build1/sdkconfig -DPEER1=1 build
idf.py -B build2 -DSDKCONFIG=build2/sdkconfig -DPEER2=1 build
cd ..
${GITHUB_WORKSPACE}/ci/clean_build_artifacts.sh `pwd`/${TARGET_TEST_DIR}
zip -qur artifacts.zip ${TARGET_TEST_DIR}
- uses: actions/upload-artifact@v4
Expand Down
1 change: 1 addition & 0 deletions ci/check_copyright_ignore.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
components/mosquitto/examples/serverless_mqtt/components/libjuice/port/juice_random.c
6 changes: 6 additions & 0 deletions components/mosquitto/examples/serverless_mqtt/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# The following five lines of boilerplate have to be in your project's
# CMakeLists in this exact order for cmake to work correctly
cmake_minimum_required(VERSION 3.16)

include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(serverless_mqtt)
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
set(LIBJUICE_VERSION "73785387eafe15c02b6a210edb10f722474e8e14")
set(LIBJUICE_URL "https://github.com/paullouisageneau/libjuice/archive/${LIBJUICE_VERSION}.zip")

set(libjuice_dir ${CMAKE_BINARY_DIR}/libjuice/libjuice-${LIBJUICE_VERSION})

# Fetch the library
if(NOT EXISTS ${libjuice_dir})
message(STATUS "Downloading libjuice ${LIBJUICE_VERSION}...")
file(DOWNLOAD ${LIBJUICE_URL} ${CMAKE_BINARY_DIR}/libjuice.zip SHOW_PROGRESS)
execute_process(COMMAND unzip -o ${CMAKE_BINARY_DIR}/libjuice.zip -d ${CMAKE_BINARY_DIR}/libjuice
WORKING_DIRECTORY ${CMAKE_BINARY_DIR})
endif()

set(JUICE_SOURCES ${libjuice_dir}/src/addr.c
${libjuice_dir}/src/agent.c
${libjuice_dir}/src/base64.c
${libjuice_dir}/src/conn.c
${libjuice_dir}/src/conn_mux.c
${libjuice_dir}/src/conn_poll.c
${libjuice_dir}/src/conn_thread.c
${libjuice_dir}/src/const_time.c
${libjuice_dir}/src/crc32.c
${libjuice_dir}/src/hash.c
${libjuice_dir}/src/ice.c
${libjuice_dir}/src/juice.c
${libjuice_dir}/src/log.c
${libjuice_dir}/src/server.c
${libjuice_dir}/src/stun.c
${libjuice_dir}/src/timestamp.c
${libjuice_dir}/src/turn.c
${libjuice_dir}/src/udp.c
# Use hmac from mbedtls and random numbers from esp_random:
# ${libjuice_dir}/src/hmac.c
# ${libjuice_dir}/src/random.c
)

message(INFO ${JUICE_SOURCES})
idf_component_register(SRCS port/juice_random.c
${JUICE_SOURCES}
INCLUDE_DIRS "include" "${libjuice_dir}/include" "${libjuice_dir}/include/juice"
REQUIRES esp_netif
PRIV_REQUIRES sock_utils)

target_compile_options(${COMPONENT_LIB} PRIVATE "-Wno-format")
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/*
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
#pragma once

// Purpose of this header is to replace udp_sendto() to avoid name conflict with lwip
// added here since ifaddrs.h is included from juice_udp sources
#define udp_sendto juice_udp_sendto

// other than that, let's just include the ifaddrs (from sock_utils)
#include_next "ifaddrs.h"
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Copyright (c) 2020 Paul-Louis Ageneau
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#include "esp_random.h"

void juice_random(void *buf, size_t size)
{
esp_fill_random(buf, size);
}

void juice_random_str64(char *buf, size_t size)
{
static const char chars64[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
size_t i = 0;
for (i = 0; i + 1 < size; ++i) {
uint8_t byte = 0;
juice_random(&byte, 1);
buf[i] = chars64[byte & 0x3F];
}
buf[i] = '\0';
}

uint32_t juice_rand32(void)
{
uint32_t r = 0;
juice_random(&r, sizeof(r));
return r;
}

uint64_t juice_rand64(void)
{
uint64_t r = 0;
juice_random(&r, sizeof(r));
return r;
}
12 changes: 12 additions & 0 deletions components/mosquitto/examples/serverless_mqtt/main/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
idf_component_register(SRCS "serverless_mqtt.c"
"wifi_connect.c"
INCLUDE_DIRS "."
REQUIRES libjuice nvs_flash mqtt json esp_wifi)

if(PEER1)
target_compile_definitions(${COMPONENT_LIB} PRIVATE "PEER1")
elseif(PEER2)
target_compile_definitions(${COMPONENT_LIB} PRIVATE "PEER2")
else()
message(FATAL_ERROR "Please define either PEER1 or PEER2")
endif()
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## IDF Component Manager Manifest File
dependencies:
espressif/mosquitto:
override_path: ../../..
espressif/sock_utils: "*"
Loading

0 comments on commit de0925a

Please sign in to comment.