//! Auto-generated unsafe bindings to [libcoap](https://github.com/obgm/libcoap), generated using
//! This crate allows direct (but unsafe) usage of the libcoap C library from Rust. The declarations
//! made in this library are generated automatically using bindgen, for further documentation on how
//! It is strongly recommended that you read the remainder of this page in order to fully understand
//! However, if you lack the time to do so, the following instructions should work in most cases:
//! 2. If you require DTLS support and run into `Required feature "dtls-(psk|pki|rpk|...)" is not
//! 3. If you're building a binary crate (or tests, examples, ...) and are getting non-DTLS-related
//! 4. Inspect your dependency tree to determine whether you already have a DTLS library's sys-crate
//! This may resolve issues related to linking multiple versions of the same library at once, and
//! Most features specified in this crate's Cargo.toml directly correspond to a feature that can be
//! The `default` feature should match the default features enabled in the configure script of the
//! Depending on the build system and linked version of libcoap, the features actually provided may
//! If you want to ensure that all features that are enabled for this crate are actually supported
//! Aside from the features relating to libcoap functionality, the following features may also be
//! - `dtls-<LIBRARY NAME>-sys`: Allows the [vendored](#vendored-build-system) libcoap version to link against the
//! - `dtls-<LIBRARY NAME>-sys-vendored` instructs the sys-crate of the DTLS library corresponding
//! In general, `libcoap-sys` supports four different build systems, which will be explained in more
//! - `vendored`: Build libcoap from source using a bundled version of the library (requires the
//! If you have explicitly specified a build system and building using that system fails, no other
//! 1. If the `vendored` crate feature is enabled, or we are building for the ESP-IDF, act as if the
//! build system is set to `vendored`. If a vendored build is attempted and fails, return with an
//! 4. If `manual` doesn't work, return an error indicating the issues with all previously attempted
//! The following information applies to all build systems (although some specifics may be detailed
//! However, some targets (especially embedded ones such as `espidf`) will use a different library
//! For your convenience, this crate re-exports the used standard library crate as the `c_stdlib`
//! While the default mechanism for determining a DTLS library differs between build systems, you
//! `wolfssl`). Refer to the build-system-specific documentation for information about supported
//! Note that some DTLS-related features (such as `dtls-(cid|psk|pki|pkcs11|rpk)`) are dependent on
//! the used DTLS backend, refer to [the `coap_encryption(3)` man page](https://libcoap.net/doc/reference/4.3.5/man_coap_encryption.html)
//! During compilation, each build system will attempt to ensure that the used version of `libcoap`
//! `coap3/coap_defines.h` header file in order to determine missing features (with `espidf` being a
//! If a build system detects that a requested feature is missing, an appropriate error message will
//! be returned. In most cases, these errors must be resolved by linking to a different version of
//! is not available for all features (especially ones dependent on the DTLS library) and may not
//! Therefore, library users should assume that the compile-time checks may not provide accurate
//! results, and should call [`coap_startup_with_feature_checks()`] during initialization to perform
//! Lastly, if you encounter a false positive error (i.e., a compile time error that indicates that
//! some feature is missing, even though you are 100% certain that it is available), you may bypass
//! the compile-time checks by setting `LIBCOAP_RS_BYPASS_COMPILE_FEATURE_CHECKS` to any non-zero
//! against, which could also cause difficult-to-debug issues and indicates a more severe problem
//! [^3]: For this reason, using this method while cross-compiling [is noted to be unsafe in `libcoap`'s documentation](https://libcoap.net/doc/reference/4.3.5/man_coap_supported.html).
//! The vendored build system uses a bundled version of libcoap (usually the latest stable version
//! Under the hood, it uses the [`autotools`](https://docs.rs/autotools/latest/autotools/) crate to
//! configure and run the build process, and you may therefore customize the build's compiler and
//! If a DTLS library is explicitly selected by the user, it will instruct libcoap to link against
//! If you enable the `dtls-<LIBRARY NAME>-sys` features and do not set the `<LIBRARY NAME>_CFLAGS`
//! or `<LIBRARY NAME>_LIBS` environment variables, this build system will set these environment
//! This is especially relevant if those crates also provide a `vendored` version in order to avoid
//! If you do not specify a DTLS library, this build system will follow the same default order that
//! libcoap does (gnutls > openssl > wolfssl > mbedtls > tinydtls), unless you enabled one of the
//! If multiple of these features are enabled, they are prioritized in the same order as used by
//! To do so, it uses the [`pkg_config`](https://docs.rs/pkg-config/latest/pkg_config/) crate, and
//! you may therefore customize the build process by setting the environment variables described in
//! that library's documentation (which may be of special relevance if you try to cross compile).
//! If you have explicitly requested use of a specific DTLS library, this build system will attempt
//! However, library selection does not take into account any other requested features (i.e., it
//! will not check for feature support before generating the bindings), but will use header-based
//! The `dtls-<LIBRARY NAME>-sys` features have no effect on this build system, but note that static
//! linking against a system-provided version of `libcoap` may cause issues if it causes multiple
//! This build system is intended as a fallback solution if all other options fail. It will attempt
//! - `LIBCOAP_RS_INCLUDE_DIRS`: Paths that should be added to `clang`'s include path to search for
//! - `LIBCOAP_RS_ADDITIONAL_LIBRARIES`: Additional libraries (such as DTLS libraries) that should
//! This build system will be used instead of the regular `vendored` build if you are building for
//! If you encounter errors that indicate that the `espressif/coap` component may not be enabled in
//! - You may have to run `cargo clean`, as the `esp-idf-sys` build script does not always detect
//! [only considers metadata from the root crate and its direct dependencies](https://docs.esp-rs.org/esp-idf-sys/esp_idf_sys/#extra-esp-idf-components)
//! It will then parse the generated bindings file and re-export all symbols in `esp-idf-sys` that
//! Note that `esp-idf-sys` may use a different version of `bindgen` than the other build systems
// Bindgen translates the C headers, clippy's and rustfmt's recommendations are not applicable here.
use c_stdlib::{epoll_event, fd_set, memcmp, sa_family_t, sockaddr, sockaddr_in, sockaddr_in6, socklen_t, time_t};
/// <https://libcoap.net/doc/reference/develop/group__string.html#ga7f43c10b486dc6d45c37fcaf987d711b>.
/// Execute feature check function and panic with an error message if the feature is not supported.
"compile-time detected DTLS backend \"{}\" does not match run-time detected DTLS backend \"{}\".\n",
/// features (as enabled in `Cargo.toml`) are available and that the used DTLS library matches the
/// You *should* prefer using this function over [`coap_startup()`], as without calling this function
/// Either this function or [`coap_startup()`] must be run once before any libcoap function is called.
/// If you are absolutely 100% certain that all features you require are always available (or are
/// prepared to deal with error return values/different behavior on your own if they aren't), you
// This way, these function calls will not cause missing function or struct definition errors that
// SAFETY: We asserted that context != null, listen_addr is a reference and can therefore not be null.
let uri = unsafe { coap_new_str_const(COAP_TEST_RESOURCE_URI.as_ptr(), COAP_TEST_RESOURCE_URI.len()) };
// SAFETY: We just asserted that uri is valid, COAP_RESOURCE_FLAGS_RELEASE_URI is valid because we will not free the uri ourselves.
let test_resource = unsafe { coap_resource_init(uri, COAP_RESOURCE_FLAGS_RELEASE_URI as c_int) };
// SAFETY: We asserted that test_resource and context are valid, other pointers are always valid.
/// Test case that creates a basic coap server and makes a request to it from a separate context
std::thread::spawn(move || run_coap_test_server(&server_address_clone, preparation_barrier_clone));
unsafe { coap_new_client_session(context, std::ptr::null(), &server_address, coap_proto_t_COAP_PROTO_UDP) };
// SAFETY: context is asserted to be valid, no known side effects that would violate any guarantees