Commit graph

56 commits

Author SHA1 Message Date
Felix Walter
553cb23bdd Fix constParameter and constVariable warnings by cppcheck
This improves const correctness, at least for pointers (making the
pointed-to data const where possible).

Some suppressions are added for false positive warnings, such as in
`simple_queue.c` where an arithmethic operation is used on the pointer
to calculate a non-const pointer that is returned (thus, the passed
pointer should also not point to const).

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2024-09-11 10:13:21 +02:00
Felix Walter
b04ecb36c9 hal_task: Only catch SIGINT in release builds
This otherwise prevents efficient debugging with GDB as GDB does not get
the signal anymore.

See also: https://sourceware.org/bugzilla/show_bug.cgi?id=9425

Closes: #197

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2024-06-21 11:16:07 +02:00
Felix Walter
972d62f9d8 hal_task: Add hal_task_wait function to join threads
This enables us to wait for threads to exit cleanly.

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2024-06-21 10:36:30 +02:00
Maximilian Nitsch
16f7214215 simple_queue: Add NULL pointer checks to queueCreate() and queueDelete()
- queueCreate: return NULL if malloc() failed
- queueDelete: no-op if queue is NULL

Signed-off-by: Maximilian Nitsch <maximilian.nitsch@d3tn.com>
2024-06-03 13:41:58 +02:00
Maximilian Nitsch
36b92c5e41 hal_task: Replace sigaction exit handler by sigwait
In order to properly clean up all resources allocated by threads, we
need to join the non-detached threads. This is not possible within the
`sigaction` exit handler function. For this reason, the termination
logic is changed to use `sigwait`.

In addition, SIGALRM is added to the list of handled signals and
SIGUSR1, SIGUSR2 are ignored.

Signed-off-by: Maximilian Nitsch <maximilian.nitsch@d3tn.com>
2024-01-26 14:28:50 +01:00
Maximilian Nitsch
3935a15441 hal_task: Make detached threads optional
Adds a parameter to the `hal_task_create()` function to allow
non-detached threads and a second parameter to pass the thread
identifier.

Signed-off-by: Maximilian Nitsch <maximilian.nitsch@d3tn.com>
2024-01-23 15:25:59 +01:00
Felix Walter
5f5269ec1a
Merge branch 'feature/162-clean-up-hal-task'
See !150
2023-12-20 16:25:50 +01:00
Felix Walter
2601666049
Merge branch 'feature/172-log-errno-log-levels'
See !149
2023-12-20 16:25:38 +01:00
Felix Walter
7fcdeab31d Remove hal_config headers
They are unnecessary now. We should do platform-specific definitions
centrally via config.mk as well.

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-12-20 10:32:48 +01:00
Felix Walter
75edf0cbd7 hal_config: Remove LINUX_SPECIFIC_API switch
We only used it for thread naming - the hal_platform malloc analysis
support was removed a long time ago. As we do not really need thread
naming and removed it when cleaning up hal_task, we do not need this
define anymore.

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-12-20 10:32:48 +01:00
Felix Walter
47194d21ac hal_task: Remove unused parameters
This removes the parameters for task name, priority, and stack size,
which were mostly relevant for FreeRTOS on STM32.

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-12-20 10:32:48 +01:00
Felix Walter
9fc5f753b4 hal_task: Remove custom priorities and stack size
We implemented this to mirror the behavior on STM32 with FreeRTOS. We do
not need it anymore on POSIX systems, so better go with the defaults of
the OS.

Closes: #162

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-12-20 10:32:22 +01:00
Felix Walter
976563b608 hal_io: Format system error messages in logs properly
This simplifies the logging of system error messages by passing
everything to a single `fprintf` call. The error message is obtained
from `strerror` - as we guard everything via a semaphore this should not
lead to any issue(s) we had in the past with it.

Fixes: #171

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-12-20 10:09:07 +01:00
Felix Walter
0742de7ba9 hal_io: Print log level when printing system errors
Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-12-20 10:06:48 +01:00
Felix Walter
a16fe7d69b socket_util: Also check for POLLNVAL
This checks for `POLLNVAL` errors for which we would otherwise return
`0` (as if the timeout was triggered). It also makes the formatting of
checks consistent.

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-12-20 09:04:21 +01:00
Felix Walter
5dd53b37c8 Ensure proper termination on fatal errors in release builds
Previously we were using ASSERT(0|false), which only works in debug
builds. In some cases, this is intended, but we cannot reliably continue
operation in others. We now use abort() where we have to terminate the
program abnormally. In cases where only debug builds should terminate, a
comment is added.

This is done along with the logging updates as the code is typically
related and would cause conflicts otherwise.

Closes: #163
Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-11-24 15:52:00 +01:00
Felix Walter
6525a453ba Adjust all logging calls to include a log level
Rules that have been applied:

- ERROR: Anything that is or may be problematic for the program to
  continue running.
- WARN: Anything abnormal but definitely not critical for program
  continuation, especially issues encountered when interacting with
  clients (e.g. AAP).
- INFO: All state changes not corresponding to the "normal" processing of
  individual data or messages/bundles, as well as further informative and
  helpful messages. Includea errors that occur during normal data
  processing.
- DEBUG: Messages to trace what happens with individual bundles and
  other fine-grained information about internal processes.

At the moment, `LOG_ERRNO` always emits messages with the "ERROR" level.
We might want to change this in the future to better differentiate WARN
and ERROR in this regard also.

Closes: #140
Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-11-24 14:50:25 +01:00
Felix Walter
f02fdc042d hal_io: Protect logging via mutex
This resolves the race condition documented in #139.
The log macros now each call only a single function which locks
a binary semaphore during execution.

Fixes: #139

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-11-16 14:20:04 +01:00
Felix Walter
485a06b381 AAP, AAP2: Move common socket handling parts to dedicated module
There is some common functionality that should not be copied.

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-11-09 14:33:48 +01:00
Felix Walter
7330c79b52 style: Spacing, indentation, long lines, continuations
This harmonizes several style issues found by `checkpatch.pl` when using
strict mode.

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-07-25 13:15:05 +02:00
Felix Walter
5038e9c21d hal_task: Minor style adjustment
Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-06-08 11:08:49 +02:00
Oleksandr Nazymko
33ab422e11 Fix hal_task_delay function for POSIX compatibility
This fixes the implementation of `hal_task_delay` function by replacing `usleep` which has been removed from POSIX with `nanosleep`. Also handles `EINTR` error.

Signed-off-by: Oleksandr Nazymko <oleksandr.nazymko@d3tn.com>
2023-06-07 13:22:16 +02:00
Felix Walter
f81c96834e platform/hal_semaphore: Use defined constant instead of magic number
Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-05-15 11:39:29 +02:00
Felix Walter
1a0eca1587 platform/hal_semaphore: Reduce inline #ifdefs
This provides dedicated `hal_semaphore` functions for Apple and
non-Apple platforms.

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-05-15 11:39:29 +02:00
Felix Walter
6880b22b90 hal_semaphore/hal_queue: Accept 64-bit timestamps as delay
Previously the maximum waiting time was just over 24 days. In a DTN
setup we may want to wait longer, e.g., for contacts to occur. This
allows for waiting about 292 years. If the provided delay exceeds this
threshold, an infinite delay is assumed.

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-05-15 11:39:29 +02:00
Felix Walter
7f322764e9 hal_semaphore: Fix determination of timestamp for sem_trywait
The nanoseconds field in struct timespec must not exceed 1000 million,
i.e., one second. As we add to an absolute time we might exceed this in
some cases. Thus, we need to check for it and reflect the additional
second in the seconds field of struct timespec.

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-05-15 11:39:29 +02:00
Felix Walter
15f2748207 simple_queue: Deallocate pop and push semaphore on queue deletion
Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-05-15 11:39:29 +02:00
Felix Walter
adb80c8938 simple_queue: Remove queueItemsWaiting
We cannot easily support this on MacOS, but we do not use it except in
tests. Thus, remove it and make the tests only run on Linux and use
`sem_getvalue` directly.

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-05-15 11:01:34 +02:00
Felix Walter
1e8d86548c hal: Add a hal_semaphore implementation that works on MacOS
Unnamed semaphores are unsupported there, which is why we were seeing
segfaults.

See also: https://stackoverflow.com/a/27847103

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-05-15 11:01:34 +02:00
Felix Walter
00d5d2d050 external, hal: Remove unused crypto (HMAC, SHA2) and JSON functions
This removes all external functions currently unused in uD3TN.
We may re-introduce esp. JSON at a later point in time, but should
include updated libraries anyway.

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-05-12 09:12:30 +02:00
Felix Walter
22b4d9d7db
Merge branch 'hotfix/clang-tidy-false-positives'
See !98
2023-05-12 08:30:25 +02:00
Felix Walter
424ea0e186 Remove comments to silence clang-tidy false positives
We disable the warning altogether via the proper commandline option.

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-05-11 17:08:39 +02:00
Felix Walter
6695ff0ae4 hal_time: Use integer operations to calculate timestamps
Fixes: #98

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-05-11 16:59:13 +02:00
Felix Walter
379f09d7af hal_time, agents: Remove the function to set the system time
This should be a function offered by the operating system, not by a
userland process like uD3TN.

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-05-11 16:59:13 +02:00
Felix Walter
1237eaa2db Remove heap-allocated Task_t
`hal_task_create` returned a reference to the new task allocated on the
heap via `malloc`. We commonly used the pattern to `free` this reference
from the new thread which, however, could result in race conditions and
other synchronization issues. We added a fix using a semaphore in the
application agent, but this would have been needed in other places (CLA,
...) as well. As we do not make use of the task reference anywhere
besides in the CLA for error handling, we can remove the heap-allocated
value altogether and replace it with an `enum ud3tn_result` to simplify
things and circumvent the named synchronization issues.

In the case of the CLA function `cla_link_init`, logic is introduced to
request the RX task to exit immediately in case the TX task cannot be
launched.

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-05-10 17:23:55 +02:00
Felix Walter
69e4095dcb Remove hal_random
The test was returning indeterministic errors and we did not even use
the function anymore.

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-05-10 17:23:55 +02:00
Felix Walter
334edbd8ac hal_task: Do not cancel threads upon deletion
The delete function should just deallocate the heap memory, termination
of the thread should be controlled by itself, to make it safe to call
`hal_task_delete()` from within the thread.

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-05-10 15:37:02 +02:00
Felix Walter
65d7582a88 Provide LOGERROR() macro for errno logging and remove strerror
`strerror()` is not thread-safe, thus, we have to use an alternative. If
we want to prevent allocating a temporary buffer (e.g., for
`strerror_r`), we need to use `perror()`. To allow writing stored
`errno` values, the function sets `errno` from an argument passed to it,
which should be safe according to the manual.

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-05-10 15:37:02 +02:00
Felix Walter
14007d8c15 hal_task: Ensure hal_task_start_scheduler() actually pauses forever
As on the targeted platforms the scheduler is always running when the
program is launched, the function should simply pause indefinitely. It
did not do this, though, as a signal can interrupt `pause()`.

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-05-10 15:07:16 +02:00
Felix Walter
e27c157107 hal_platform: Remove unnecessary platform-dependent functions
This removes some functions solely useful for the STM32, debugging and
error handling with these boards. When doing a future uC port, we should
move such functions to the platform-specific part.

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-05-10 15:07:16 +02:00
Felix Walter
0a82b87782 CLA: TX: Prevent data race by removing TX thread name
The static variables might be accessed at the same time. The name is not
really needed if we have a debugger available, which we probably always
do...

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-05-10 15:07:16 +02:00
Felix Walter
d46e53b263 hal_semaphore: Assert if semaphore is not blocked
This can lead to hard-to-debug errors as it would just increment the
semaphore further and then a double "locking" is needed to actually lock it.

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-05-10 15:07:16 +02:00
Felix Walter
ea7b8da449 hal_semaphore: Do not calculate timeout if we do not want to block
If the timeout is set to 0, a non-blocking attempt to block the
semaphore is directly executed.

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-05-10 14:37:11 +02:00
Felix Walter
e31a865bf8 hal_semaphore: Supply proper function to check if blocked
The previous polling method was not clean in that it left the semaphore
locked and did not return whether the operation was successful or not.
This adds a more descriptive and correct method to chieve the goal of
checking the semaphore's status.

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-05-10 14:37:11 +02:00
Felix Walter
baf1aac4c2 hal_io: Forward all logs to stderr and make it line-buffered
This switches all log messages to `stderr`, which is intended for
diagnostic purposes (i.e., matches the intent of our log messages). This
makes it easy to distinguish the log output from actual output (which
the uD3TN daemon does not have, but a program based on libud3tn might).

This also flushes stderr after every log message (i.e., makes it
line-buffered) to prevent delays when outputting the logs.

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-05-10 14:32:56 +02:00
Felix Walter
ffbd5de056 hal_platform: Do not use signal-unsafe function in signal handler
LOG() is not safe to be called inside signal handlers as it requires
malloc() and free() to work.

Detected by Clang's thread sanitizer.

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-05-10 14:32:47 +02:00
Felix Walter
dd6d34d707 hal_time: Make time functions thread-safe
Do not return a reference to a non-thread-safe global variable:
ctime() is not thread safe. As we only use the function in logging, we
can directly print the return value, protected by a mutex.

This also adapts the tests and makes the initialization routine always
run. To use the system time again, UINT64_MAX can be passed.

Found by Clang's thread (data race) sanitizer.

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2023-05-10 14:32:47 +02:00
Marius Feldmann
691b7c3172 Remove STM32/FreeRTOS support from uD3TN
Former releases of uD3TN have included support for STM32/FreeRTOS.
Having this support from the first moment on was a good thing in
order to optimize for platforms with limited resources. As this
platform is not used in current uD3TN setups and as it results in quite
some maintenance work, it is removed with this commit. The removed parts
cover the implementation itself, tests, tools, documentation, parts of
the build/make scripts as well as licensing information.

Signed-off-by: Marius Feldmann <marius.feldmann@d3tn.com>
2022-03-22 00:05:13 +01:00
Georg Alexander Murzik
298431c68f Insert SPDX license expression comments into our .c, .h, .py, and .sh code
ud3tn is available under multiple licenses and we want to reflect this in
our source code. But which license information should appear first and how
can we manage this efficiently in the future? The Linux Kernel uses SPDX
expressions instead of boilerplate sections. This seems to be a great
approach, so we do the same here.

Signed-off-by: Georg Alexander Murzik <georg.murzik@d3tn.com>
2022-02-20 16:24:38 +01:00
Felix Walter
de21a0f5ea simple_queue: Adapt license notice
As we are moving to `BSD-3-Clause OR Apache-2.0`, this needs to be
adapted as well. (We got the permission of Robert to publish his code
under the new licensing scheme.)

Signed-off-by: Felix Walter <felix.walter@d3tn.com>
2022-02-19 23:42:22 +01:00