Skip to content

Commit

Permalink
events: Move implementation of events::send() to lib/events
Browse files Browse the repository at this point in the history
Events have a global, system-wide sequence number, which must be handled
atomically, (fetching and incrementing the sequence AND sending the event
to uORB must be atomic). Currently in FLAT mode, only one instance of this
sequence number exists, so it is OK to have it in px4_platform.

However, in PROTECTED mode px4_platform is instantiated both in kernel-
and user spaces, which makes two instances of this sequence number, which
causes problems in the mavlink event handling logic.

When mavlink receives and handles events, it expects that:
- The sequence numbers arrive in order (seq n is followed by n+1 etc)
- It increments by 1
- There is only one instance of the sequence number

In PROTECTED mode this is violated, as the kernel and user sequence
numbers run freely on their own. This patch fixes the issue by moving
the event backend to the kernel and by providing user access to it via
ioctl.
  • Loading branch information
pussuw committed Sep 21, 2023
1 parent 7c552eb commit a74ae30
Show file tree
Hide file tree
Showing 12 changed files with 232 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,8 @@ endif()
#
add_library(parameters_interface INTERFACE)
add_library(kernel_parameters_interface INTERFACE)
add_library(events_interface INTERFACE)
add_library(kernel_events_interface INTERFACE)

include(px4_add_library)
add_subdirectory(src/lib EXCLUDE_FROM_ALL)
Expand All @@ -423,8 +425,11 @@ add_subdirectory(src/lib/parameters EXCLUDE_FROM_ALL)
if(${PX4_PLATFORM} STREQUAL "nuttx" AND NOT CONFIG_BUILD_FLAT)
target_link_libraries(parameters_interface INTERFACE usr_parameters)
target_link_libraries(kernel_parameters_interface INTERFACE parameters)
target_link_libraries(events_interface INTERFACE usr_events)
target_link_libraries(kernel_events_interface INTERFACE events)
else()
target_link_libraries(parameters_interface INTERFACE parameters)
target_link_libraries(events_interface INTERFACE events)
endif()

# firmware added last to generate the builtin for included modules
Expand Down
5 changes: 3 additions & 2 deletions cmake/px4_add_module.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,14 @@ function(px4_add_module)
if(NOT DYNAMIC)
target_link_libraries(${MODULE} PRIVATE prebuild_targets px4_platform systemlib perf uORB)
if (${PX4_PLATFORM} STREQUAL "nuttx" AND NOT CONFIG_BUILD_FLAT AND KERNEL)
target_link_libraries(${MODULE} PRIVATE kernel_parameters_interface px4_kernel_layer)
target_link_libraries(${MODULE} PRIVATE
kernel_events_interface kernel_parameters_interface px4_kernel_layer)
set_property(GLOBAL APPEND PROPERTY PX4_KERNEL_MODULE_LIBRARIES ${MODULE})
elseif(${PX4_PLATFORM} STREQUAL "qurt")
target_link_libraries(${MODULE} PRIVATE px4_layer uORB)
set_property(GLOBAL APPEND PROPERTY PX4_MODULE_LIBRARIES ${MODULE})
else()
target_link_libraries(${MODULE} PRIVATE parameters_interface px4_layer)
target_link_libraries(${MODULE} PRIVATE events_interface parameters_interface px4_layer)
set_property(GLOBAL APPEND PROPERTY PX4_MODULE_LIBRARIES ${MODULE})
endif()
set_property(GLOBAL APPEND PROPERTY PX4_MODULE_PATHS ${CMAKE_CURRENT_SOURCE_DIR})
Expand Down
1 change: 0 additions & 1 deletion platforms/common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ endif()
add_library(px4_platform STATIC
board_common.c
board_identity.c
events.cpp
external_reset_lockout.cpp
i2c.cpp
i2c_spi_buses.cpp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@
#define _CRYPTOIOCBASE IOCTL_IDX_TO_BASE(2)
#define _PARAMIOCBASE IOCTL_IDX_TO_BASE(3)
#define _PLATFORMIOCBASE IOCTL_IDX_TO_BASE(4)
#define MAX_IOCTL_PTRS 5
#define _EVENTSIOCBASE IOCTL_IDX_TO_BASE(5)
#define MAX_IOCTL_PTRS 6

/* The PLATFORMIOCLAUNCH IOCTL is used to launch kernel side modules
* from the user side code
Expand Down
2 changes: 2 additions & 0 deletions platforms/nuttx/src/px4/common/px4_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include <px4_platform_common/console_buffer.h>
#include <px4_platform_common/defines.h>
#include <drivers/drv_hrt.h>
#include <lib/events/events.h>
#include <lib/parameters/param.h>
#include <px4_platform_common/px4_work_queue/WorkQueueManager.hpp>
#include <px4_platform/cpuload.h>
Expand Down Expand Up @@ -132,6 +133,7 @@ int px4_platform_init()

#if !defined(CONFIG_BUILD_FLAT)
hrt_ioctl_init();
events_ioctl_init();
#endif

#ifdef CONFIG_SYSTEMCMDS_PARAM
Expand Down
2 changes: 2 additions & 0 deletions platforms/nuttx/src/px4/common/px4_protected_layers.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ target_link_libraries(px4_kernel_layer
nuttx_kxx
nuttx_karch
nuttx_kmm
PRIVATE
kernel_events_interface # events_ioctl_init
)

if (DEFINED PX4_CRYPTO)
Expand Down
10 changes: 10 additions & 0 deletions src/lib/events/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,13 @@ add_custom_command(OUTPUT ${generated_events_header}
)
add_custom_target(events_header DEPENDS ${generated_events_header})
add_dependencies(prebuild_targets events_header)

# Build the interface(s)
if (${PX4_PLATFORM} STREQUAL "nuttx" AND NOT CONFIG_BUILD_FLAT)
list(APPEND EXTRA_SRCS events_ioctl.cpp)
add_library(usr_events usr_events.cpp)
add_dependencies(usr_events prebuild_targets)
endif()

add_library(events events.cpp ${EXTRA_SRCS})
add_dependencies(events prebuild_targets)
File renamed without changes.
36 changes: 36 additions & 0 deletions src/lib/events/events.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/****************************************************************************
*
* Copyright (c) 2023 Technology Innovation Institute. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name PX4 nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/

#pragma once

void events_ioctl_init(void);
68 changes: 68 additions & 0 deletions src/lib/events/events_ioctl.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/****************************************************************************
*
* Copyright (c) 2023 Technology Innovation Institute. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name PX4 nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/

/**
* @file events_ioctl.cpp
*
* Interface to send events from user space.
*/

#include <px4_platform/board_ctrl.h>

#include <errno.h>

#include "events_ioctl.h"

static int events_ioctl(unsigned int cmd, unsigned long arg)
{
int ret = OK;

switch (cmd) {
case EVENTSIOCSEND: {
eventiocsend_t *data = (eventiocsend_t *)arg;
events::send(data->event);
}
break;

default:
ret = -ENOTTY;
break;
}

return ret;
}

void events_ioctl_init(void)
{
px4_register_boardct_ioctl(_EVENTSIOCBASE, events_ioctl);
}
51 changes: 51 additions & 0 deletions src/lib/events/events_ioctl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/****************************************************************************
*
* Copyright (c) 2023 Technology Innovation Institute. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name PX4 nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/

/**
* @file events_ioctl.h
*
* User space - kernel space interface for dispatching events.
*/

#pragma once

#include <px4_platform/board_ctrl.h>
#include <px4_platform_common/defines.h>
#include <px4_platform_common/events.h>

#define _EVENTSIOC(_n) (_PX4_IOC(_EVENTSIOCBASE, _n))

#define EVENTSIOCSEND _EVENTSIOC(1)
typedef struct eventiocsend {
events::EventType &event;
} eventiocsend_t;
53 changes: 53 additions & 0 deletions src/lib/events/usr_events.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/****************************************************************************
*
* Copyright (c) 2023 Technology Innovation Institute. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name PX4 nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/

/**
* @file usr_events.cpp
*
* User space interface for dispatching events.
*/

#include <sys/boardctl.h>

#include "events_ioctl.h"

namespace events
{

void send(EventType &event)
{
eventiocsend_t data = {event};
boardctl(EVENTSIOCSEND, reinterpret_cast<unsigned long>(&data));
}

} /* namespace events */

0 comments on commit a74ae30

Please sign in to comment.