-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(mdns): WIP support for rust API
- Loading branch information
1 parent
b4ed1f4
commit fd11203
Showing
15 changed files
with
739 additions
and
148 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
name: "mdns: rust-tests" | ||
|
||
on: | ||
push: | ||
branches: | ||
- master | ||
pull_request: | ||
types: [opened, synchronize, reopened, labeled] | ||
|
||
jobs: | ||
host_test_mdns: | ||
if: contains(github.event.pull_request.labels.*.name, 'mdns') || github.event_name == 'push' | ||
name: Host test build | ||
runs-on: ubuntu-22.04 | ||
container: espressif/idf:release-v5.4 | ||
|
||
steps: | ||
- name: Checkout esp-protocols | ||
uses: actions/checkout@v4 | ||
|
||
- name: Build and Test | ||
shell: bash | ||
run: | | ||
. ${IDF_PATH}/export.sh | ||
cd components/mdns/examples/simple_query/ | ||
idf.py build | ||
cd ../.. | ||
rustup | ||
COMPILE_COMMANDS_DIR=examples/simple_query/build/ cargo run --example usage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[package] | ||
name = "mdns" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
libc = "0.2" | ||
dns-parser = "0.8" | ||
|
||
[build-dependencies] | ||
cc = "1.0" | ||
serde = { version = "1.0", features = ["derive"] } | ||
serde_json = "1.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
use std::env; | ||
use std::fs; | ||
use std::path::{Path, PathBuf}; | ||
use serde::Deserialize; | ||
|
||
#[derive(Debug, Deserialize)] | ||
struct CompileCommand { | ||
directory: String, | ||
command: String, | ||
file: String, | ||
} | ||
|
||
fn main() { | ||
// Get the directory for compile_commands.json from an environment variable | ||
let compile_commands_dir = env::var("COMPILE_COMMANDS_DIR") | ||
.unwrap_or_else(|_| ".".to_string()); // Default to current directory | ||
|
||
// Construct the path to the compile_commands.json file | ||
let compile_commands_path = Path::new(&compile_commands_dir).join("compile_commands.json"); | ||
|
||
// Parse compile_commands.json | ||
let compile_commands: Vec<CompileCommand> = { | ||
let data = fs::read_to_string(&compile_commands_path) | ||
.expect("Failed to read compile_commands.json"); | ||
serde_json::from_str(&data) | ||
.expect("Failed to parse compile_commands.json") | ||
}; | ||
|
||
// Directory of compile_commands.json, used to resolve relative paths | ||
let base_dir = compile_commands_path | ||
.parent() | ||
.expect("Failed to get base directory of compile_commands.json"); | ||
|
||
// List of C files to compile (only base names) | ||
let files_to_compile = vec![ | ||
"mdns_networking_socket.c", | ||
"log_write.c", | ||
"log_timestamp.c", | ||
"esp_netif_linux.c", | ||
"freertos_linux.c", | ||
"tag_log_level.c", | ||
"log_linked_list.c", | ||
"log_lock.c", | ||
"log_level.c", | ||
"log_binary_heap.c", | ||
"esp_system_linux2.c", | ||
"heap_caps_linux.c", | ||
"mdns_stub.c", | ||
"log_buffers.c", | ||
"util.c" | ||
]; | ||
|
||
// Initialize the build | ||
let mut build = cc::Build::new(); | ||
|
||
for file in &files_to_compile { | ||
// Extract the base name from `file` for comparison | ||
let target_base_name = Path::new(file) | ||
.file_name() | ||
.expect("Failed to extract base name from target file") | ||
.to_str() | ||
.expect("Target file name is not valid UTF-8"); | ||
|
||
// Find the entry in compile_commands.json by matching the base name | ||
let cmd = compile_commands.iter() | ||
.find(|entry| { | ||
let full_path = Path::new(&entry.directory).join(&entry.file); // Resolve relative paths | ||
if let Some(base_name) = full_path.file_name().and_then(|name| name.to_str()) { | ||
// println!("Checking file: {} against {}", base_name, target_base_name); // Debug information | ||
base_name == target_base_name | ||
} else { | ||
false | ||
} | ||
}) | ||
.unwrap_or_else(|| panic!("{} not found in compile_commands.json", target_base_name)); | ||
|
||
// Add the file to the build | ||
build.file(&cmd.file); | ||
|
||
// Parse flags and include paths from the command | ||
for part in cmd.command.split_whitespace() { | ||
if part.starts_with("-I") { | ||
// Handle include directories | ||
let include_path = &part[2..]; | ||
let full_include_path = if Path::new(include_path).is_relative() { | ||
base_dir.join(include_path).canonicalize() | ||
.expect("Failed to resolve relative include path") | ||
} else { | ||
PathBuf::from(include_path) | ||
}; | ||
build.include(full_include_path); | ||
} else if part.starts_with("-D") || part.starts_with("-std") { | ||
// Add other compilation flags | ||
build.flag(part); | ||
} | ||
} | ||
} | ||
|
||
|
||
|
||
|
||
// Compile with the gathered information | ||
build.compile("mdns"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
cmake_minimum_required(VERSION 3.5) | ||
|
||
|
||
include($ENV{IDF_PATH}/tools/cmake/project.cmake) | ||
if(${IDF_TARGET} STREQUAL "linux") | ||
set(EXTRA_COMPONENT_DIRS "../../../../common_components/linux_compat") | ||
set(COMPONENTS main) | ||
endif() | ||
|
||
project(mdns_host) | ||
|
||
# Enable sanitizers only without console (we'd see some leaks on argtable when console exits) | ||
if(NOT CONFIG_TEST_CONSOLE AND CONFIG_IDF_TARGET_LINUX) | ||
idf_component_get_property(mdns mdns COMPONENT_LIB) | ||
target_link_options(${mdns} INTERFACE -fsanitize=address -fsanitize=undefined) | ||
endif() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
idf_component_register(SRCS "main.c" "mdns_stub.c" "esp_system_linux2.c" | ||
INCLUDE_DIRS | ||
"." | ||
REQUIRES mdns console nvs_flash) |
21 changes: 21 additions & 0 deletions
21
components/mdns/examples/simple_query/main/Kconfig.projbuild
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
menu "Test Configuration" | ||
|
||
config TEST_HOSTNAME | ||
string "mDNS Hostname" | ||
default "esp32-mdns" | ||
help | ||
mDNS Hostname for example to use | ||
|
||
config TEST_NETIF_NAME | ||
string "Network interface name" | ||
default "eth2" | ||
help | ||
Name/ID if the network interface on which we run the mDNS host test | ||
|
||
config TEST_CONSOLE | ||
bool "Start console" | ||
default n | ||
help | ||
Test uses esp_console for interactive testing. | ||
|
||
endmenu |
46 changes: 46 additions & 0 deletions
46
components/mdns/examples/simple_query/main/esp_system_linux2.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
/* | ||
* All functions presented here are stubs for the POSIX/Linux implementation of FReeRTOS. | ||
* They are meant to allow to compile, but they DO NOT return any meaningful value. | ||
*/ | ||
|
||
#include <stdlib.h> | ||
#include <stdint.h> | ||
#include "esp_private/system_internal.h" | ||
#include "esp_heap_caps.h" | ||
|
||
// dummy, we should never get here on Linux | ||
void esp_restart_noos_dig(void) | ||
{ | ||
abort(); | ||
} | ||
|
||
uint32_t esp_get_free_heap_size(void) | ||
{ | ||
return heap_caps_get_free_size(MALLOC_CAP_DEFAULT); | ||
} | ||
|
||
uint32_t esp_get_free_internal_heap_size(void) | ||
{ | ||
return heap_caps_get_free_size(MALLOC_CAP_8BIT | MALLOC_CAP_DMA | MALLOC_CAP_INTERNAL); | ||
} | ||
|
||
uint32_t esp_get_minimum_free_heap_size(void) | ||
{ | ||
return heap_caps_get_minimum_free_size(MALLOC_CAP_DEFAULT); | ||
} | ||
|
||
const char *esp_get_idf_version(void) | ||
{ | ||
return "IDF_VER"; | ||
} | ||
|
||
void __attribute__((noreturn)) esp_system_abort(const char *details) | ||
{ | ||
exit(1); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
dependencies: | ||
idf: ">=5.0" | ||
espressif/mdns: | ||
version: "^1.0.0" | ||
override_path: "../../.." | ||
protocol_examples_common: | ||
path: ${IDF_PATH}/examples/common_components/protocol_examples_common |
Oops, something went wrong.