An easily portable abstraction layer for Windows, Linux, macOS, Free-RTOS, Android, iOS, and WebAssembly platforms containing OS abstractions and utilities that have been proven useful over my career.
Repository: https://github.com/cfogelklou/sweet_osal_platform
License: MIT License (Copyright 2020 Chris Fogelklou)
Sweet OS Platform (SOP) is a lightweight, cross-platform OS abstraction layer with 20+ years of embedded heritage. Many of these source files originated in embedded projects two decades ago, so they are lightweight and portable. The code balances "modern C++" with lightweight C-like heritage—correctness trumps modernity.
- Portability First: Works on Windows, Linux, macOS, Android, iOS, Free-RTOS, TI-RTOS, and WebAssembly
- Lightweight: Minimal overhead, suitable for embedded systems
- Proven: Battle-tested across decades of commercial projects
- Incremental Modernization: Gradually refactoring while maintaining reliability
# Configure and build
cmake -B build
cmake --build build -j8
# Or using make directly
mkdir -p build && cd build
cmake ..
make -j8
# Run all tests
cd build
ctest -j8
# Run specific test
./sweet_osal_test
./osal_test
./utils_testmkdir build && cd build
cmake ..
msbuild sweet_osal_platform.sln /property:Configuration=Debug -maxcpucount:4# Prerequisites:
# - Emscripten SDK (emsdk) installed and activated
# (see https://emscripten.org/docs/getting_started/downloads.html)
#
# From the repository root:
./emscripten_build.shThis script:
- Creates an
em/build directory - Configures with
emcmake cmake - Builds with
emmake make - Runs tests with
nodeon generated.jsfiles
./
├── sop_src/ # Main SOP source code
│ ├── osal/ # OS Abstraction Layer
│ │ ├── osal.h # Main OSAL interface
│ │ ├── osal_mutex.* # Mutex/lock primitives
│ │ ├── osal_thread.* # Threading support
│ │ ├── osal_timer.* # Timing/sleep functions
│ │ └── osal_memory.* # Memory allocation
│ ├── utils/ # Utility functions
│ │ ├── platform_log.h # Logging framework
│ │ ├── byteq.* # Byte queue implementation
│ │ ├── linked_list.* # Linked list utilities
│ │ ├── crc.* # CRC calculations
│ │ ├── uuid.* # UUID generation
│ │ └── file_*.* # File operations
│ ├── task_sched/ # Task scheduler
│ │ └── task_sched.* # Run-to-completion state machine
│ ├── buf_io/ # Buffered I/O
│ │ └── buf_io.* # FIFO-queued buffered I/O
│ ├── mini_socket/ # Socket abstraction
│ │ └── mini_socket.* # Cross-platform sockets
│ ├── simple_plot/ # Plotting utilities
│ │ └── simple_plot.* # Data visualization
│ ├── mbedtls/ # Crypto integration
│ │ └── myconfig.h # mbedTLS configuration
│ ├── libsodium/ # Sodium crypto wrapper
│ ├── LibraryFiles.cmake # Central source file list
│ └── tests/ # SOP-specific tests
├── test/ # Integration tests
│ └── *.cpp # Main test files (sweet_osal_test)
├── ext/ # External dependencies (git submodules)
│ ├── googletest/ # Google Test/Mock framework
│ ├── mbedtls/ # mbedTLS crypto library
│ ├── libsodium/ # Sodium cryptography
│ └── json/ # nlohmann/json
├── build/ # CMake build output
├── CMakeLists.txt # Main build configuration
├── CLAUDE.md # AI developer instructions
└── README.md # This file
Location: sop_src/osal/
Purpose: Provides cross-platform primitives for:
- Threading:
OSALTaskPtrT, task creation and management - Mutexes:
OSALMutexPtrT, recursive and non-recursive locks - Timing:
OSALSleep(),OSALGetMS()for millisecond timing - Memory:
OSALMALLOC(),OSALFREE()for portable allocation
Key APIs (see sop_src/osal/osal.h for full list and exact signatures):
// Initialization
void OSALInit(void);
// Mutexes
OSALMutexPtrT OSALCreateMutex(void);
void OSALDeleteMutex(OSALMutexPtrT* ppMutex);
bool OSALLockMutex(OSALMutexPtrT pMutex, uint32_t timeoutMs);
bool OSALUnlockMutex(OSALMutexPtrT pMutex);
// Timing
void OSALSleep(uint32_t milliseconds);
uint32_t OSALGetMS(void);
// Tasks
OSALTaskPtrT OSALTaskCreate(OSALTaskFuncPtrT pTaskFunc, void* pParam, OSALPrioT prio, const OSALTaskStructT* pPlatform);Location: sop_src/utils/
Purpose: General-purpose utilities:
- Logging:
LOG_Log()with severity levels (LOG_TRACE, LOG_DEBUG, LOG_INFO, etc.) - Byte Queue:
ByteQ_t- lightweight circular buffer for byte streams - Linked Lists: Single and double-linked list implementations
- CRC: CRC-16 and CRC-32 calculations
- UUID: RFC 4122 UUID generation
- File Operations: Cross-platform file I/O helpers
Key Example:
#include "utils/platform_log.h"
#include "utils/byteq.h"
LOG_Log("Processing %d items", count);
ByteQ_t queue;
ByteQCreate(&queue, buffer, sizeof(buffer));
ByteQCommitWrite(&queue, len);Location: sop_src/task_sched/
Purpose: Run-to-completion state machine scheduler for event-driven systems.
Key Concept: Tasks run in phases; each task completes before the next starts—no preemption within a phase.
Location: sop_src/buf_io/
Purpose: Buffered I/O with FIFO queues for reliable data streaming.
Location: sop_src/mini_socket/
Purpose: Cross-platform socket abstraction for TCP/UDP networking.
Location: sop_src/simple_plot/
Purpose: Lightweight data visualization utilities.
mbedtls (sop_src/mbedtls/): mbedTLS integration for TLS/SSL
libsodium (sop_src/libsodium/): Sodium cryptography wrapper
| Test | Location | Description |
|---|---|---|
sweet_osal_test |
test/ |
Comprehensive OS abstraction tests |
osal_test |
sop_src/osal/test/ |
OSAL-specific tests |
utils_test |
sop_src/utils/tests/ |
Utility function tests |
test_simple_plot |
sop_src/simple_plot/tests/ |
Plotting tests |
transport_test |
test/ |
Transport layer tests |
cd build
# Run all tests
ctest -j8
# Run specific test
./sweet_osal_test
# Run with verbose output
ctest -V -R osal_test
# Coverage report (non-CI)
make ctest_coverageMain CMakeLists.txt:
- Project:
sweet_osal_platform - Minimum CMake: 3.5
- C++ Standard: C++11
- Enables testing and code coverage
Key Files:
sop_src/LibraryFiles.cmake- Central SOP source file listtest/CMakeLists.txt- Test executable configuration
- Google Test - C++ testing framework
- Google Mock - C++ mocking framework
- mbedtls - Crypto/TLS library
- libsodium - Sodium cryptography
- nlohmann/json - JSON parsing
- Uses Clang from Xcode toolchain
- Threads::Threads for threading support
- Debug builds include
-fno-omit-frame-pointer
- POSIX threads (
pthread) - May need
-DMBEDTLS_NO_SOCKETSfor some configs
- MSBuild for compilation
- Requires
ws2_32(Windows Sockets)
- Define
FREERTOS_PORTfor FreeRTOS build - OSAL provides appropriate implementations
- Build as part of parent SAU project
- Memory growth enabled for dynamic allocation
See C_CODING_GUIDELINES.md for comprehensive C/C++ coding conventions:
- Naming conventions (classes, functions, variables, constants)
- Code formatting (clang-format)
- Memory safety best practices
- Include ordering and header guards
- Comment style and documentation
- C++11/14 features
- Constant management
Quick Summary:
- C++ Standard: C++11 (C++14 features acceptable)
- Naming: Use existing patterns (
osal_prefix for C API,task_for scheduler) - Error Handling: Check return values, use
platform_logfor logging - Testing: Add tests for new functionality
- Documentation: Update README.md and CLAUDE.md for significant changes
- Cross-Platform: Use OSAL for platform-specific operations
When changes are made to CLAUDE.md, run the sync script:
./scripts/copy-ai-instructions.shThis copies CLAUDE.md to:
GEMINI.md- For Google Gemini.github/copilot-instructions.md- For GitHub Copilot
# Format all source files (after adding format target to CMake)
make format
# Check format without modifying (CI)
make format-check
# Format single file
clang-format -i path/to/file.cppSOP is used as a dependency by:
- SAU (Sweet Audio Utils): https://github.com/cfogelklou/sweet_audio_utils
- Audio processing library and strobe tuner
- Uses SOP for OS abstraction across platforms
When contributing to SOP:
- Follow the coding guidelines in
C_CODING_GUIDELINES.md - Add tests for new functionality
- Ensure all tests pass (
ctest) - Format code with
clang-format - Update documentation as needed
- Keep the embedded heritage in mind—portability over modernity
MIT License
Copyright 2020 Chris Fogelklou
Last Updated: 2025-03-10