Skip to content

Combine event related function under setEventListener #25

Description

@Mqxx

Currently we have serialSetErrorCallback and serialSetEventCallback. Both take in a callback function that either gets called on an error or on an event (currently only attached/detached).

The API would be more appealing and useful if we would combine and extend those two functions into one function. For example like this:

MODULE_API auto serialSetEventCallback(
    void (*callback_fn)(
        cpp_core::SerialEventType event_type,
        cpp_core::SerialEventContext *event_context
    )
) -> int;

The cpp_core::SerialEventType could be an enum like this:

/**
 * Possible event types serialSetEventCallback().
 */
enum class SerialEventType : int
{
    kError = 0, ///< An error happened.
    kDeviceDisconnected = 1, ///< An existing device was disconnected from the host.
    kDeviceConnected = 2, ///< A new device was connected to the host.
    kRead = 3, ///< Something was read from the device.
    kWrite = 4, ///< Something was written to the device.
    // ... can be extended if needed in the future
};

The cpp_core::SerialEventContext could be a union struct that represents all possible structs for every possible cpp_core::SerialEventType.

This whole concept could also eliminate the need for the error callback passed into every function, because we have the cpp_core::SerialEventContext that we could pass any data needed for the specific event fired.

For example the struct for the cpp_core::SerialEventType::kError could look like this:

struct Error
{
    int error_code,
    const char *message,
}

With this concept the API would be easy to extend with new features aka. events and such.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area | c apiPublic C interface and ABIbreaking changeChange requires users to adaptenhancementNew feature or improvement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions