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

Embedded libiio + libtinyiiod #1128

Merged
merged 20 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
7554856
utilities: Avoid inclusion of network headers
pcercuei Jan 25, 2024
4a9fed4
iiod: Start enqueue/dequeue tasks when creating buffer
pcercuei Jan 23, 2024
cc53e33
CMake: Allow to compile IIOD without local backend
pcercuei Jan 23, 2024
0ee73a4
CMake: Add support for external backend
pcercuei Jan 16, 2024
c5708f4
CMake: Add support for building a thread-less library
pcercuei Jan 16, 2024
79a4cf4
task: Support running thread-less
pcercuei Jan 16, 2024
24b489d
iiod-responder: Support running without threads
pcercuei Jan 16, 2024
45b0174
iiod-responder: Remove unused field
pcercuei Jan 16, 2024
2dc6e4b
iiod-client: Drop unused function iiod_client_set_kernel_buffers_count()
pcercuei Jan 16, 2024
3f2a696
iiod-client: Support new protocol without ZSTD
pcercuei Jan 24, 2024
1ffd8fd
iiod: Support new protocol without ZSTD
pcercuei Jan 24, 2024
8745a22
iiod: Start thread-less enqueue/dequeue tasks before enabling buffer
pcercuei Jan 23, 2024
5118b8e
iiod: Use iio_mutex locks instead of using pthread directly
pcercuei Jan 24, 2024
872acee
iiod: Move read_all/write_all to new file
pcercuei Jan 25, 2024
f3c5730
iiod: Use <iio/iio-backend.h> instead of duplicating code
pcercuei Jan 24, 2024
f3ddc3e
iiod: Move poll_nointr() to iiod.c
pcercuei Jan 25, 2024
d61fbe7
iiod: Remove non-standard includes from ops.h
pcercuei Jan 25, 2024
31188d2
iiod: Add support for building libtinyiiod
pcercuei Jan 23, 2024
d476d1b
iiod: Handle event reads in tinyiiod
pcercuei Jan 24, 2024
50e1ae6
xml: Shrink header size used to detect XML string embedded in URI
pcercuei Jan 30, 2024
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
36 changes: 29 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,8 @@ if (ENABLE_IPV6)
endif()
endif()

option(WITH_EXTERNAL_BACKEND "Support external backend provided by the application" OFF)

option(WITH_USB_BACKEND "Enable the libusb backend" ON)
if (WITH_USB_BACKEND)
find_package(PkgConfig)
Expand Down Expand Up @@ -325,13 +327,11 @@ else()
message(STATUS "GCC ${CMAKE_COMPILER_IS_GNUCC}")
endif()

option(WITH_LIBTINYIIOD "Build libtinyiiod" OFF)

IF(${CMAKE_SYSTEM_NAME} MATCHES "Linux")
option(WITH_IIOD "Build the IIO Daemon" ON)
option(WITH_LOCAL_BACKEND "Enable the local backend" ON)

if (WITH_IIOD AND NOT WITH_LOCAL_BACKEND)
message(SEND_ERROR "IIOD can only be enabled if the local backend is enabled")
endif()
endif()

set(DOXYGEN_INPUT "${CMAKE_SOURCE_DIR}")
Expand Down Expand Up @@ -374,6 +374,10 @@ set(LIBIIO_VERSION ${VERSION}.g${LIBIIO_VERSION_GIT})
file(WRITE ${CMAKE_BINARY_DIR}/.version ${LIBIIO_VERSION})

if(WITH_LOCAL_BACKEND)
if (NO_THREADS)
message(SEND_ERROR "Local backend require threads.")
endif()

target_sources(iio PRIVATE local.c)

# Link with librt if present
Expand Down Expand Up @@ -578,7 +582,10 @@ elseif(NEED_LIBXML2)
"If you want to enable the XML backend, set WITH_XML_BACKEND=ON.")
endif()

if (WIN32)
option(NO_THREADS "Build a thread-less Libiio library" OFF)
if (NO_THREADS)
target_sources(iio PRIVATE lock-dummy.c)
elseif (WIN32)
target_sources(iio PRIVATE lock-windows.c)
else ()
if (NOT ANDROID)
Expand All @@ -594,9 +601,9 @@ else ()
target_sources(iio PRIVATE lock.c)
endif()

if (IIOD_CLIENT OR WITH_IIOD)
if (IIOD_CLIENT OR WITH_IIOD OR WITH_LIBTINYIIOD)
add_library(iiod-responder STATIC iiod-responder.c)
target_include_directories(iiod-responder PRIVATE include)
target_include_directories(iiod-responder PRIVATE include ${CMAKE_BINARY_DIR})
set_target_properties(iiod-responder PROPERTIES POSITION_INDEPENDENT_CODE ON)

# Link against exported symbols of Libiio and not the
Expand All @@ -608,6 +615,10 @@ if (IIOD_CLIENT OR WITH_IIOD)
endif()

if (IIOD_CLIENT)
if (NO_THREADS)
message(SEND_ERROR "Backends using iiod-client require threads.")
endif()

target_sources(iio PRIVATE iiod-client.c)
target_link_libraries(iio PRIVATE iiod-responder)
endif()
Expand All @@ -627,6 +638,10 @@ if(WITH_EXAMPLES)
add_subdirectory(examples)
endif()
if (WITH_IIOD)
if (NO_THREADS)
message(SEND_ERROR "IIOD require threads.")
endif()

if (NOT PTHREAD_LIBRARIES)
message(SEND_ERROR "IIOD requires pthread support.\n"
"If you want to disable IIOD, set WITH_IIOD=OFF.")
Expand All @@ -635,6 +650,10 @@ if (WITH_IIOD)
add_subdirectory(iiod)
endif()

if (WITH_LIBTINYIIOD)
add_subdirectory(tinyiiod)
endif()

if ((WITH_IIOD OR IIOD_CLIENT) AND NOT WITH_ZSTD)
message(WARNING "IIOD async. API requires ZSTD support, consider enabling it.\n"
"If you want to enable libzstd support, set WITH_ZSTD=ON.")
Expand Down Expand Up @@ -674,10 +693,12 @@ string(REPLACE ";" "," LIBIIO_SCAN_BACKENDS "${LIBIIO_SCAN_BACKENDS}")

configure_file(iio-config.h.cmakein ${CMAKE_CURRENT_BINARY_DIR}/iio-config.h @ONLY)

toggle_iio_feature("${NO_THREADS}" threadless)
toggle_iio_feature("${LIBIIO_COMPAT}" compat)
toggle_iio_feature("${WITH_XML_BACKEND}" xml)
toggle_iio_feature("${WITH_ZSTD}" zstd)
toggle_iio_feature("${WITH_NETWORK_BACKEND}" network)
toggle_iio_feature("${WITH_EXTERNAL_BACKEND}" external)
toggle_iio_feature("${HAVE_DNS_SD}" dns-sd)
toggle_iio_feature("${HAVE_AVAHI}" avahi)
toggle_iio_feature("${HAVE_BONJOUR}" bonjour)
Expand All @@ -690,6 +711,7 @@ toggle_iio_feature("${WITH_HWMON}" hwmon)
toggle_iio_feature("${WITH_USB_BACKEND}" usb)
toggle_iio_feature("${WITH_UTILS}" utils)
toggle_iio_feature("${WITH_EXAMPLES}" examples)
toggle_iio_feature("${WITH_LIBTINYIIOD}" libtinyiiod)
toggle_iio_feature("${WITH_IIOD}" iiod)
toggle_iio_feature("${WITH_MODULES}" modules)
toggle_iio_feature("${WITH_USB_BACKEND_DYNAMIC}" usb-dynamic)
Expand Down
2 changes: 2 additions & 0 deletions README_BUILD.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ Cmake Options | Default | Depends on | Description
`WITH_SERIAL_BACKEND_DYNAMIC` | ON | Modules + serial backend | Compile the serial backend as a module |
`WITH_NETWORK_BACKEND` | ON | | Supports TCP/IP |
`WITH_NETWORK_BACKEND_DYNAMIC` | ON | Modules + network backend | Compile the network backend as a module |
`WITH_EXTERNAL_BACKEND` | OFF | | Support external backend provided by the application |
`HAVE_DNS_SD` | ON | Networking | Enable DNS-SD (ZeroConf) support |
`ENABLE_IPV6` | ON | Networking | Define if you want to enable IPv6 support |
`WITH_LOCAL_BACKEND` | ON | Linux | Enables local support with iiod |
Expand Down Expand Up @@ -109,6 +110,7 @@ Cmake Options | Default | Description |
`WITH_IIOD_SERIAL` | ON | Add serial (UART) support |
`WITH_IIOD_USBD` | ON | Add support for USB through FunctionFS within IIOD |
`WITH_IIOD_V0_COMPAT` | ON | Add support for Libiio v0.x protocol and clients |
`WITH_LIBTINYIIOD` | OFF | Build libtinyiiod |
`WITH_AIO` | ON | Build IIOD with async. I/O support |
`WITH_SYSTEMD` | OFF | Enable installation of systemd service file for iiod |
`SYSTEMD_UNIT_INSTALL_DIR` | /lib/systemd/system | default install path for systemd unit files |
Expand Down
1 change: 1 addition & 0 deletions context.c
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@ const struct iio_backend * const iio_backends[] = {
IF_ENABLED(WITH_USB_BACKEND && !WITH_USB_BACKEND_DYNAMIC,
&iio_usb_backend),
IF_ENABLED(WITH_XML_BACKEND, &iio_xml_backend),
IF_ENABLED(WITH_EXTERNAL_BACKEND, &iio_external_backend),
};
const unsigned int iio_backends_size = ARRAY_SIZE(iio_backends);

Expand Down
1 change: 0 additions & 1 deletion dns_sd.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

/* IPv6 Max = 4*8 + 7 + 1 for '%' + interface length */
#define DNS_SD_ADDRESS_STR_MAX (40 + IF_NAMESIZE)
#define FQDN_LEN (255) /* RFC 1035 */

/* MacOS doesn't include ENOMEDIUM (No medium found) like Linux does */
#ifndef ENOMEDIUM
Expand Down
1 change: 1 addition & 0 deletions dns_sd_bonjour.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/

#include "dns_sd.h"
#include "network.h" // for FQDN_LEN

#include <arpa/inet.h>
#include <netinet/in.h>
Expand Down
3 changes: 2 additions & 1 deletion iio-config.h.cmakein
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#cmakedefine01 WITH_NETWORK_BACKEND
#cmakedefine01 WITH_USB_BACKEND
#cmakedefine01 WITH_SERIAL_BACKEND
#cmakedefine01 WITH_EXTERNAL_BACKEND

#cmakedefine01 WITH_MODULES
#cmakedefine01 WITH_NETWORK_BACKEND_DYNAMIC
Expand All @@ -40,6 +41,7 @@
#cmakedefine01 HAVE_DNS_SD
#cmakedefine01 HAVE_AVAHI
#cmakedefine01 WITH_ZSTD
#cmakedefine01 NO_THREADS

#cmakedefine HAS_PIPE2
#cmakedefine HAS_STRDUP
Expand All @@ -49,7 +51,6 @@
#cmakedefine HAS_NEWLOCALE
#cmakedefine HAS_PTHREAD_SETNAME_NP
#cmakedefine HAVE_IPV6
#cmakedefine NO_THREADS

#define IF_ENABLED(cfg, ptr) ((cfg) ? (ptr) : NULL)

Expand Down
83 changes: 36 additions & 47 deletions iiod-client.c
Original file line number Diff line number Diff line change
Expand Up @@ -440,25 +440,6 @@ int iiod_client_set_trigger(struct iiod_client *client,
return ret;
}

int iiod_client_set_kernel_buffers_count(struct iiod_client *client,
const struct iio_device *dev,
unsigned int nb_blocks)
{
int ret;
char buf[1024];

if (iiod_client_uses_binary_interface(client))
return -ENOSYS;

iio_snprintf(buf, sizeof(buf), "SET %s BUFFERS_COUNT %u\r\n",
iio_device_get_id(dev), nb_blocks);

iio_mutex_lock(client->lock);
ret = iiod_client_exec_command(client, buf);
iio_mutex_unlock(client->lock);
return ret;
}

static unsigned int calculate_remote_timeout(unsigned int timeout_ms)
{
/* XXX(pcercuei): We currently hardcode timeout / 2 for the backend used
Expand Down Expand Up @@ -963,13 +944,16 @@ iiod_client_create_context_private_new(struct iiod_client *client,
struct iio_context *ctx = NULL;
unsigned long long len;
char *xml_zstd, *xml;
bool is_zstd;
int ret;

xml = malloc(xml_len);
xml = malloc(xml_len + uri_len);
if (!xml)
return iio_ptr(-ENOMEM);

ret = iiod_client_send_print(client, xml, xml_len);
memcpy(xml, "xml:", uri_len);

ret = iiod_client_send_print(client, &xml[uri_len], xml_len);
if (ret < 0) {
prm_perror(client->params, -ret,
"Unable to send PRINT command");
Expand All @@ -978,37 +962,42 @@ iiod_client_create_context_private_new(struct iiod_client *client,

xml_len = ret;

is_zstd = strncmp(xml, "xml:<?xml", sizeof("xml:<?xml") - 1) != 0;
if (is_zstd)
prm_dbg(client->params, "Received ZSTD-compressed XML string.\n");
else
prm_dbg(client->params, "Received uncompressed XML string.\n");

prm_dbg(client->params, "Received ZSTD-compressed XML string.\n");

len = ZSTD_getFrameContentSize(xml, xml_len);
if (len == ZSTD_CONTENTSIZE_UNKNOWN ||
len == ZSTD_CONTENTSIZE_ERROR) {
ret = -EIO;
goto out_free_xml;
}
if (is_zstd) {
len = ZSTD_getFrameContentSize(&xml[uri_len], xml_len);
if (len == ZSTD_CONTENTSIZE_UNKNOWN ||
len == ZSTD_CONTENTSIZE_ERROR) {
ret = -EIO;
goto out_free_xml;
}

xml_zstd = malloc(uri_len + len + 1);
if (!xml_zstd) {
ret = -ENOMEM;
goto out_free_xml;
}
xml_zstd = malloc(uri_len + len + 1);
if (!xml_zstd) {
ret = -ENOMEM;
goto out_free_xml;
}

xml_len = ZSTD_decompress(xml_zstd + uri_len, len, xml, xml_len);
if (ZSTD_isError(xml_len)) {
prm_err(client->params, "Unable to decompress ZSTD data: %s\n",
ZSTD_getErrorName(xml_len));
ret = -EIO;
free(xml_zstd);
goto out_free_xml;
}
xml_len = ZSTD_decompress(&xml_zstd[uri_len], len, &xml[uri_len], xml_len);
if (ZSTD_isError(xml_len)) {
prm_err(client->params, "Unable to decompress ZSTD data: %s\n",
ZSTD_getErrorName(xml_len));
ret = -EIO;
free(xml_zstd);
goto out_free_xml;
}

memcpy(xml_zstd, "xml:", uri_len);
xml_zstd[uri_len + xml_len] = '\0';
memcpy(xml_zstd, "xml:", uri_len);
xml_zstd[uri_len + xml_len] = '\0';

/* Free compressed data, make "xml" point to uncompressed data */
free(xml);
xml = xml_zstd;
/* Free compressed data, make "xml" point to uncompressed data */
free(xml);
xml = xml_zstd;
}

prm_dbg(client->params, "Creating context\n");

Expand Down
36 changes: 25 additions & 11 deletions iiod-responder.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

#include "iiod-responder.h"
#include "iio-config.h"

#include <errno.h>
#include <iio/iio.h>
Expand Down Expand Up @@ -74,7 +75,6 @@ struct iiod_responder {
struct iio_mutex *lock;
struct iio_thrd *read_thrd;
struct iio_task *write_task;
struct iio_thrd *write_thrd;

bool thrd_stop;
int thrd_err_code;
Expand Down Expand Up @@ -237,9 +237,8 @@ static void iiod_responder_cancel_responses(struct iiod_responder *priv)
}
}

static int iiod_responder_reader_thrd(void *d)
static int iiod_responder_reader_worker(struct iiod_responder *priv)
{
struct iiod_responder *priv = d;
struct iiod_command cmd;
struct iiod_buf cmd_buf, ok_buf;
struct iiod_io *io;
Expand Down Expand Up @@ -342,6 +341,11 @@ static int iiod_responder_reader_thrd(void *d)
return (int) ret;
}

static int iiod_responder_reader_thrd(void *d)
{
return iiod_responder_reader_worker(d);
}

static int iiod_responder_write(void *p, void *elm)
{
struct iiod_responder *priv = p;
Expand Down Expand Up @@ -668,11 +672,13 @@ iiod_responder_create(const struct iiod_responder_ops *ops, void *d)
if (err)
goto err_free_io;

priv->read_thrd = iio_thrd_create(iiod_responder_reader_thrd, priv,
"iiod-responder-reader-thd");
err = iio_err(priv->read_thrd);
if (err)
goto err_free_write_task;
if (!NO_THREADS) {
priv->read_thrd = iio_thrd_create(iiod_responder_reader_thrd, priv,
"iiod-responder-reader-thd");
err = iio_err(priv->read_thrd);
if (err)
goto err_free_write_task;
}

iio_task_start(priv->write_task);

Expand All @@ -689,9 +695,14 @@ iiod_responder_create(const struct iiod_responder_ops *ops, void *d)
return iio_ptr(err);
}

void iiod_responder_destroy(struct iiod_responder *priv)
void iiod_responder_stop(struct iiod_responder *priv)
{
priv->thrd_stop = true;
}

void iiod_responder_destroy(struct iiod_responder *priv)
{
iiod_responder_stop(priv);
iiod_responder_wait_done(priv);

iio_task_destroy(priv->write_task);
Expand All @@ -703,9 +714,12 @@ void iiod_responder_destroy(struct iiod_responder *priv)

void iiod_responder_wait_done(struct iiod_responder *priv)
{
if (priv->read_thrd) {
iio_thrd_join_and_destroy(priv->read_thrd);
if (!NO_THREADS) {
if (priv->read_thrd)
iio_thrd_join_and_destroy(priv->read_thrd);
priv->read_thrd = NULL;
} else if (!priv->thrd_stop) {
iiod_responder_reader_worker(priv);
}
}

Expand Down
3 changes: 3 additions & 0 deletions iiod-responder.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ void iiod_io_set_timeout(struct iiod_io *io, unsigned int timeout_ms);
/* Read the current value of the micro-second counter */
uint64_t iiod_responder_read_counter_us(void);

/* Stop the iiod_responder. */
void iiod_responder_stop(struct iiod_responder *responder);

/* Wait until the iiod_responder stops. */
void iiod_responder_wait_done(struct iiod_responder *responder);

Expand Down
2 changes: 1 addition & 1 deletion iiod/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ set(CMAKE_REQUIRED_LIBRARIES)
set(CMAKE_REQUIRED_DEFINITIONS)

add_executable(iiod
iiod.c interpreter.c responder.c thread-pool.c
iiod.c interpreter.c responder.c rw.c thread-pool.c
)
set_target_properties(iiod PROPERTIES
C_STANDARD 99
Expand Down
Loading
Loading