DO NOT USE This is pre-alpha. It will not work!
A C library designed to make industrial network protocol implementations simple, clear, and bulletproof.
The Protocol Toolkit is built around three core principles:
- Global managers for common resources (timers, sockets, memory)
- Minimal APIs with just the functions you need
- Single-protocol focus - optimized for applications implementing one protocol
- No complex abstractions - direct, straightforward interfaces
- Simplified resource handling with allocators that support destructors
- Separation of long-term vs. ephemeral data for optimal memory management
- Type-safe serialization using Protocol Description Language (PDL)
- Structured protocol definitions that are easy to read and maintain
- Consistent error handling throughout the library
- Comprehensive documentation with real-world examples
- Cross-platform support for common tasks with unified APIs
- Built-in serialization/deserialization that handles endianness and validation
- Memory safety with built-in buffer management and automatic cleanup
- Robust error handling with clear error codes and recovery paths
- Timeout management for all network operations
- Extensive testing to ensure reliability in industrial environments
- Thread-safe primitives for low-level concurrent operations
- Platform abstraction that works consistently across operating systems
Industrial network protocols are complex, but implementing them shouldn't be. Traditional approaches often result in:
- Fragile, hard-to-maintain code
- Memory leaks and buffer overflows
- Inconsistent error handling
- Poor performance under load
Protocol Toolkit solves these problems by providing:
- Pre-built, tested components for common protocol needs
- Type-safe message serialization that eliminates parsing errors
- Unified event handling for timers, sockets, and protocol events
- Clear patterns that make protocol code predictable and maintainable
Currently supported protocols:
- Modbus TCP/RTU - Complete implementation with client and server
- EtherNet/IP - Device discovery and basic CIP messaging
- More protocols planned based on community needs
#include "ptk_timer.h"
#include "ptk_modbus.h"
// Initialize the toolkit
ptk_timer_init();
ptk_modbus_init();
// Create a simple Modbus client
ptk_modbus_client_t client = ptk_modbus_client_create("192.168.1.100", 502);
// Read holding registers with automatic timeout
uint16_t values[10];
int result = ptk_modbus_read_holding_registers(client, 0, 10, values);
if (result == 0) {
printf("Successfully read %d registers\n", 10);
} else {
printf("Read failed: %s\n", ptk_modbus_error_string(result));
}
// Cleanup is automatic
ptk_modbus_client_destroy(client);
- Global timer system - Simple, efficient timers for timeouts and periodic tasks
- Smart memory management - Allocators with destructors for automatic cleanup
- Event framework - Unified handling of socket and timer events
- Error handling - Consistent error codes and recovery mechanisms
- Cross-platform threading - Mutexes, condition variables, and thread primitives
- Modbus - Complete client/server with all function codes
- EtherNet/IP - Device discovery, explicit messaging, and I/O
- Type-safe parsing - PDL-generated serialization code
- Protocol Description Language - Generate serialization code from simple definitions
- Code generator - Automatic creation of type-safe protocol handlers
- Testing utilities - Built-in support for protocol testing and validation
- Logging system - Unified logging with configurable levels and outputs
- Buffer management - Efficient, safe buffer operations with bounds checking
- Serialization/deserialization - Automatic handling of endianness and data types
- Threading primitives - Mutexes, condition variables, and thread-safe operations
- Platform abstraction - Windows, Linux, macOS, and embedded systems support
The toolkit is designed with clear separation of concerns:
Application Layer
βββ Protocol APIs (Modbus, EtherNet/IP, etc.)
βββ Protocol-Specific Logic
Protocol Toolkit Core
βββ Timer System (ptk_timer.h)
βββ Memory Management (ptk_alloc.h)
βββ Buffer Management (ptk_buf.h)
βββ Event Framework (ptk_event.h)
βββ Logging System (ptk_log.h)
βββ Threading Support (ptk_thread.h)
βββ Serialization (ptk_serialize.h)
βββ Error Handling (ptk_error.h)
System Layer
βββ Socket Operations
βββ Threading Primitives
βββ Platform Abstraction
// Create a timeout for a protocol request
ptk_timer_t timeout = ptk_timer_create_oneshot(5000, handle_timeout, &request);
// Create a keepalive timer
ptk_timer_t keepalive = ptk_timer_create_repeating(30000, send_keepalive, connection);
// Cancel when no longer needed
ptk_timer_cancel(timeout);
// Define protocol structure in PDL
struct ModbusRequest {
uint16_t transaction_id;
uint16_t protocol_id;
uint16_t length;
uint8_t unit_id;
uint8_t function_code;
uint16_t start_address;
uint16_t quantity;
};
// Generated serialization code handles endianness and validation
ptk_buf_t buffer = ptk_buf_create(256);
modbus_request_serialize(&request, buffer);
// Create allocator with automatic cleanup
ptk_allocator_t alloc = ptk_allocator_create_with_destructor(cleanup_connection);
// Allocate long-term data (persists across requests)
connection_t *conn = ptk_alloc_long_term(alloc, sizeof(connection_t));
// Allocate ephemeral data (freed automatically after request)
request_buffer_t *req = ptk_alloc_ephemeral(alloc, sizeof(request_buffer_t));
// Cleanup happens automatically when allocator is destroyed
ptk_allocator_destroy(alloc); // Calls destructors automatically
// Create mutex for thread-safe operations
ptk_mutex_t mutex = ptk_mutex_create();
// Use condition variables for coordination
ptk_cond_t cond = ptk_cond_create();
// Thread-safe logging
ptk_log_info("Connection established to %s:%d", host, port);
ptk_log_error("Failed to read register %d: %s", reg, ptk_error_string(err));
// Cleanup is automatic
ptk_mutex_destroy(mutex);
ptk_cond_destroy(cond);
// Create buffer with bounds checking
ptk_buf_t buf = ptk_buf_create(1024);
// Safe operations with automatic bounds checking
ptk_buf_write_uint16_be(buf, transaction_id); // Big-endian
ptk_buf_write_uint16_le(buf, register_count); // Little-endian
ptk_buf_write_bytes(buf, data, data_len);
// Automatic cleanup
ptk_buf_destroy(buf);
# Clone the repository
git clone https://github.com/your-org/protocol_toolkit.git
cd protocol_toolkit
# Build with CMake
mkdir build
cd build
cmake ..
make
# Run tests
make test
# Install
make install
Current Status: Alpha Development
The Protocol Toolkit is under active development. Current focus areas:
- β Core timer and memory management APIs
- β Modbus TCP/RTU implementation
- β EtherNet/IP device discovery
- π Smart allocators with destructors
- π Cross-platform threading primitives
- π Type-safe serialization system
- π Unified logging system
- π Enhanced buffer management
- π Comprehensive error handling
- π Complete EtherNet/IP implementation
- π Additional protocol support
We welcome contributions! Please see CONTRIBUTING.md for guidelines.
Key areas where we need help:
- Protocol implementations
- Cross-platform testing and validation
- Documentation and examples
- Performance optimization
- Platform-specific optimizations
Protocol Toolkit is dual-licensed:
- Mozilla Public License 2.0 (MPL-2.0) - See LICENSE-1.MPL
- GNU Lesser General Public License 2.1 (LGPL-2.1) - See LICENSE-2.LGPL
Choose the license that best fits your project's needs.
- libplctag - Popular PLC communication library
- Your project here - let us know if you're using Protocol Toolkit!
Making industrial network protocols simple, clear, and bulletproof.