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 11, 2024
1 parent 8855c6d commit 84143de
Show file tree
Hide file tree
Showing 10 changed files with 512 additions and 2 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/mosq__build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
runs-on: ubuntu-22.04
container: espressif/idf:${{ matrix.idf_ver }}
env:
TEST_DIR: components/mosquitto/examples/broker
TEST_DIR: components/mosquitto/examples
TARGET_TEST_DIR: build_esp32_default
steps:
- name: Checkout esp-protocols
Expand All @@ -30,7 +30,7 @@ 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}/broker
${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,66 @@
set(LIBJUICE_VERSION "73785387eafe15c02b6a210edb10f722474e8e14")
set(LIBJUICE_URL "https://github.com/paullouisageneau/libjuice/archive/${LIBJUICE_VERSION}.zip")

# Directory to store the downloaded library
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(libjuice_dir ${CMAKE_BINARY_DIR}/libjuice/libjuice-1.5.7)
## Path to the downloaded libjuice source
##ExternalProject_Get_Property(libjuice_project libjuice_dir)
#set(libjuice_dir ${CMAKE_BINARY_DIR}/libjuice/libjuice-1.5.7/)

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")

#add_custom_target(target1 DEPENDS file1)
# Define a custom target to download the GitHub repository
#https://github.com/github/codeql/archive/refs/tags/v1.5.7.zip
#add_custom_command(
# OUTPUT ${CMAKE_BINARY_DIR}/libjuice.timestamp
# COMMAND wget https://github.com/paullouisageneau/libjuice/archive/refs/tags/v1.5.7.zip -O ${CMAKE_BINARY_DIR}/libjuice.zip
# COMMAND unzip -o ${CMAKE_BINARY_DIR}/libjuice.zip -d ${CMAKE_BINARY_DIR}/libjuice
# COMMAND ${CMAKE_COMMAND} -E touch ${CMAKE_BINARY_DIR}/libjuice.timestamp
# COMMENT "Downloading and extracting the repository"
# VERBATIM
#)
#
#add_custom_target(download_repo ALL DEPENDS ${CMAKE_BINARY_DIR}/libjuice.timestamp)
#
#add_dependencies(${COMPONENT_LIB} download_repo)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*
* SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
#pragma once
//#error hi
#define udp_sendto juice_udp_sendto
#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 84143de

Please sign in to comment.