This commit is contained in:
r.koeppe
2024-05-14 02:14:13 +02:00
parent 0052d3984b
commit 2d22ccd2d6
1423 changed files with 354055 additions and 7 deletions

View File

@ -0,0 +1,77 @@
/** \file
* {COMMON_HEADER}
*
* \section DESCRIPTION
* This header file contains structures and functions useful for critical sections.
*/
#ifndef _VN_CRITICALSECTION_H_
#define _VN_CRITICALSECTION_H_
#include "vn/error.h"
#ifdef _WIN32
/* Disable some warnings for Visual Studio with -Wall. */
#if defined(_MSC_VER)
#pragma warning(push)
#pragma warning(disable:4668)
#pragma warning(disable:4820)
#pragma warning(disable:4255)
#endif
#include <Windows.h>
#if defined(_MSC_VER)
#pragma warning(pop)
#endif
#endif
#if (defined __linux__ || defined __APPLE__ || defined __CYGWIN__ || defined __QNXNTO__)
#include <pthread.h>
#endif
#ifdef __cplusplus
extern "C" {
#endif
typedef struct
{
#if _WIN32
CRITICAL_SECTION handle;
#elif __linux__ || __APPLE__ || __CYGWIN__ || __QNXNTO__
pthread_mutex_t handle;
#else
#error "Unknown System"
#endif
} VnCriticalSection;
/** \breif Initializes a VnCriticalSection structure.
*
* \param[in] criticalSection The VnCriticalSection structure to initialize.
* \return Any errors encountered. */
VnError VnCriticalSection_initialize(VnCriticalSection *criticalSection);
/** \brief Disposes of a VnCriticalSection structure and associated resources.
*
* \param[in] criticalSection The associated VnCriticalSection structure.
* \return Any errors encountered. */
VnError VnCriticalSection_deinitialize(VnCriticalSection *criticalSection);
/** \brief Attempt to enter a critical section.
*
* \param[in] criticalSection The associated VnCriticalSection structure.
* \return Any errors encountered. */
VnError VnCriticalSection_enter(VnCriticalSection *criticalSection);
/** \brief Leave a critical section.
*
* \param[in] criticalSection The associated VnCriticalSection structure.
* \return Any errors encountered. */
VnError VnCriticalSection_leave(VnCriticalSection *criticalSection);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -0,0 +1,98 @@
/** \file
* {COMMON_HEADER}
*
* \section DESCRIPTION
* This header file contains structures and functions useful events and signals.
*/
#ifndef _VNEVENT_H_
#define _VNEVENT_H_
#include "vn/int.h"
#include "vn/error.h"
#include "vn/bool.h"
#ifdef _WIN32
/* Disable some warnings for Visual Studio with -Wall. */
#if defined(_MSC_VER)
#pragma warning(push)
#pragma warning(disable:4668)
#pragma warning(disable:4820)
#pragma warning(disable:4255)
#endif
#include <Windows.h>
#if defined(_MSC_VER)
#pragma warning(pop)
#endif
#endif
#if (defined __linux__ || defined __APPLE__ || defined __CYGWIN__ || defined __QNXNTO__)
#include <pthread.h>
#endif
#ifdef __cplusplus
extern "C" {
#endif
/** \brief Structure representing an event. */
typedef struct
{
#ifdef _WIN32
HANDLE handle;
#elif (defined __linux__ || defined __APPLE__ || defined __CYGWIN__ || defined __QNXNTO__)
pthread_mutex_t mutex;
pthread_cond_t condition;
bool isTriggered;
#else
#error "Unknown System"
#endif
} VnEvent;
/** \brief Initializes a VnEvent structure.
*
* \param[in] event The VnEvent structure to initialize.
* \return Any errors encountered. */
VnError VnEvent_initialize(VnEvent *event);
/** \brief Causes the calling thread to wait on an event until the event is signalled.
*
* If the event is signalled, the value E_SIGNALED will be returned.
*
* \param[in] event The associated VnEvent.
* \return Any errors encountered. */
VnError VnEvent_wait(VnEvent *event);
/** \brief Causes the calling thread to wait on an event until the event is signalled.
*
* If the event is signalled, the value E_SIGNALED will be returned.
*
* \param[in] event The associated VnEvent.
* \param[in] timeoutUs The number of microseconds to wait before the thread stops waiting on the event. If a timeout
* does occur, the value E_TIMEOUT will be returned.
* \return Any errors encountered. */
VnError VnEvent_waitUs(VnEvent *event, uint32_t timeoutUs);
/** \brief Causes the calling thread to wait on an event until the event is signalled.
*
* If the event is signalled, the value E_SIGNALED will be returned.
*
* \param[in] event The associated VnEvent.
* \param[in] timeoutMs The number of milliseconds to wait before the thread stops waiting on the event. If a timeout
* does occur, the value E_TIMEOUT will be returned.
* \return Any errors encountered. */
VnError VnEvent_waitMs(VnEvent *event, uint32_t timeoutMs);
/** \brief Signals an event.
*
* \param[in] event The associated event.
* \return Any errors encountered. */
VnError VnEvent_signal(VnEvent *event);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -0,0 +1,149 @@
#ifndef VN_SERIALPORT_H
#define VN_SERIALPORT_H
/** Cross-platform access to serial ports. */
#include "vn/int.h"
#include "vn/error.h"
#include "vn/bool.h"
#include "vn/xplat/thread.h"
#include "vn/xplat/criticalsection.h"
#if defined(_WIN32)
/* Disable some warnings for Visual Studio with -Wall. */
#if defined(_MSC_VER)
#pragma warning(push)
#pragma warning(disable:4668)
#pragma warning(disable:4820)
#pragma warning(disable:4255)
#endif
#include <Windows.h>
#if defined(_MSC_VER)
#pragma warning(pop)
#endif
#endif
#ifdef __linux__
#include <linux/serial.h>
#elif defined __APPLE__
#include <dirent.h>
#endif
#ifdef __cplusplus
extern "C" {
#endif
/** \brief Type for listening to data received events from the VnSerialPort. */
typedef void (*VnSerialPort_DataReceivedHandler)(void *userData);
#ifdef _WIN32
#pragma warning(push)
#pragma warning(disable : 4820)
#endif
/** \brief Provides access to a serial port. */
typedef struct
{
#ifdef _WIN32
HANDLE handle;
/* Windows appears to need single-thread access to read/write API functions. */
VnCriticalSection readWriteCS;
#elif (defined __linux__ || defined __APPLE__ || defined __CYGWIN__ || defined __QNXNTO__)
int handle;
#else
#error "Unknown System"
#endif
bool purgeFirstDataBytesWhenSerialPortIsFirstOpened;
VnSerialPort_DataReceivedHandler dataReceivedHandler;
void *dataReceivedHandlerUserData;
VnThread serialPortNotificationsThread;
bool continueHandlingSerialPortEvents;
size_t numberOfReceiveDataDroppedSections;
VnCriticalSection dataReceivedHandlerCriticalSection;
char portName[50];
} VnSerialPort;
#ifdef _WIN32
#pragma warning(pop)
#endif
/** \brief Initializes a VnSerialPort structure.
*
* \param[in] serialport The VnSerialPort structure to initialize.
* \return Any errors encountered. */
VnError VnSerialPort_initialize(VnSerialPort *serialport);
/** \brief Opens a serial port.
*
* \param[in] serialport The associated VnSerialPort structure.
* \param[in] portName The name of the serial port to open.
* \param[in] baudrate The baudrate to open the serial port at.
* \return Any errors encountered. */
VnError VnSerialPort_open(VnSerialPort *serialport, char const *portName, uint32_t baudrate);
/** \brief Closes the serial port.
*
* \param[in] serialport The associated VnSerialPort structure.
* \return Any errors encountered. */
VnError VnSerialPort_close(VnSerialPort *serialport);
/** \brief Indicates if the serial port is open or not.
*
* \param[in] serialport The associated VnSerialPort structure.
* \return <c>true</c> if the serial port is open; otherwise <c>false</c>. */
bool VnSerialPort_isOpen(VnSerialPort *serialport);
/** \brief Reads data from a serial port.
*
* \param[in] serialport The associated VnSerialPort structure.
* \param[in] buffer Buffer to place the read bytes.
* \param[in] numOfBytesToRead The number of bytes to read from the serial port.
* \param[out] numOfBytesActuallyRead The number of bytes actually read from the serial port.
* \return Any errors encountered. */
VnError VnSerialPort_read(VnSerialPort *serialport, char *buffer, size_t numOfBytesToRead, size_t *numOfBytesActuallyRead);
/** \brief Writes data out of a serial port.
*
* \param[in] serialport The associated VnSerialPort.
* \param[in] data The data to write out.
* \param[in] numOfBytesToWrite The number of bytes to write out of the serial port.
* \return Any errors encountered. */
VnError VnSerialPort_write(VnSerialPort *serialport, char const *data, size_t numOfBytesToWrite);
/** \brief Changes the baudrate the port is connected at.
*
* \param[in] serialport The associated VnSerialPort.
* \param[in] baudrate The new baudrate.
* \return Any errors encountered. */
VnError VnSerialPort_changeBaudrate(VnSerialPort *serialport, uint32_t baudrate);
/** \brief Allows registering for notification of data received events.
*
* \param[in] serialPort The associated VnSerialPort.
* \param[in] handler The callback method to receive notifications.
* \param[in] userData User supplied data that will be sent to the handler on callbacks.
* \return Any errors encountered. */
VnError VnSerialPort_registerDataReceivedHandler(VnSerialPort *serialPort, VnSerialPort_DataReceivedHandler handler, void *userData);
/** \brief Allows unregistering for notification of data received events.
*
* \param[in] serialPort The associated VnSerialPort. */
VnError VnSerialPort_unregisterDataReceivedHandler(VnSerialPort *serialPort);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -0,0 +1,87 @@
/** \file
* {COMMON_HEADER}
*
* \section DESCRIPTION
* This header file contains structures and functions useful working with threads.
*/
#ifndef _VN_THREAD_H_
#define _VN_THREAD_H_
#include "vn/error.h"
#include "vn/int.h"
#ifdef _WIN32
/* Disable some warnings for Visual Studio with -Wall. */
#if defined(_MSC_VER)
#pragma warning(push)
#pragma warning(disable:4668)
#pragma warning(disable:4820)
#pragma warning(disable:4255)
#endif
#include <Windows.h>
#if defined(_MSC_VER)
#pragma warning(pop)
#endif
#endif
#if defined __linux__ || defined __APPLE__ || defined __CYGWIN__ || defined __QNXNTO__
#include <pthread.h>
#endif
#ifdef __cplusplus
extern "C" {
#endif
/** \brief Structure for working with threads. */
typedef struct
{
#ifdef _WIN32
HANDLE handle;
#elif defined __linux__ || defined __APPLE__ || defined __CYGWIN__ || defined __QNXNTO__
pthread_t handle;
#else
#error "Unknown System"
#endif
} VnThread;
/** \brief Function signature for a start routine for a thread. */
typedef void (*VnThread_StartRoutine)(void*);
/** \brief Starts a new thread immediately which calls the provided start routine.
*
* \param[in] thread Associated VnThread structure.
* \param[in] startRoutine The routine to be called when the new thread is started.
* \param[in] routineData Pointer to data that will be passed to the routine on the new thread.
* \return Any errors encountered. */
VnError VnThread_startNew(VnThread *thread, VnThread_StartRoutine startRoutine, void* routineData);
/** \brief Blocks the calling thread until the referenced thread finishes.
*
* \param[in] thread The associated VnThread.
* \return Any errors encountered. */
VnError VnThread_join(VnThread *thread);
/** \brief Causes the calling thread to sleep the specified number of seconds.
*
* \param[in] numOfSecsToSleep The number of seconds to sleep. */
void VnThread_sleepSec(uint32_t numOfSecsToSleep);
/** \brief Causes the calling thread to sleep the specified number of milliseconds
*
* \param[in] numOfMsToSleep The number of milliseconds to sleep. */
void VnThread_sleepMs(uint32_t numOfMsToSleep);
/** \brief Causes the calling thread to sleep the specified number of microseconds
*
* \param[in] numOfUsToSleep The number of microseconds to sleep. */
void VnThread_sleepUs(uint32_t numOfUsToSleep);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -0,0 +1,73 @@
/** \file
* {COMMON_HEADER}
*
* \section DESCRIPTION
* This header file contains structures and functions useful timing.
*/
#ifndef _VNTIME_H_
#define _VNTIME_H_
#include "vn/int.h"
#include "vn/error.h"
#include "vn/enum.h"
#ifdef _WIN32
/* Disable some warnings for Visual Studio with -Wall. */
#if defined(_MSC_VER)
#pragma warning(push)
#pragma warning(disable:4668)
#pragma warning(disable:4820)
#pragma warning(disable:4255)
#endif
#include <Windows.h>
#if defined(_MSC_VER)
#pragma warning(pop)
#endif
#endif
#ifdef __cplusplus
extern "C" {
#endif
/** \brief Provides simple timing capabilities. */
typedef struct
{
#if _WIN32
double pcFrequency;
__int64 counterStart;
#elif __linux__ || __APPLE__ ||__CYGWIN__ || __QNXNTO__
double clockStart;
#else
#error "Unknown System"
#endif
} VnStopwatch;
/** \brief Initializes and starts a stopwatch.
*
* \param[in] stopwatch The VnStopwatch to initialize and start.
* \return Any errors encountered. */
VnError VnStopwatch_initializeAndStart(VnStopwatch *stopwatch);
/** \brief Resets the stopwatch's timing.
*
* \param[in] stopwatch The associated VnStopwatch.
* \return Any errors encountered. */
VnError VnStopwatch_reset(VnStopwatch *stopwatch);
/** \brief Determines the number of milliseconds elapsed since the last reset
* of the stopwatch.
*
* \param[in] stopwatch The associated VnStopwatch.
* \param[out] elapsedMs The elapsed time in milliseconds.
* \return Any errors encountered. */
VnError VnStopwatch_elapsedMs(VnStopwatch *stopwatch, float *elapsedMs);
#ifdef __cplusplus
}
#endif
#endif