Skip to content

Commit 649ac7a

Browse files
committed
More clean up and finishing up implementations.
1 parent 95a3d6b commit 649ac7a

27 files changed

+1356
-1573
lines changed

CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# Only add tests if this is the root project
2+
if(CMAKE_SOURCE_DIR STREQUAL PROJECT_SOURCE_DIR)
3+
add_subdirectory(src/tests)
4+
endif()
15
# Ensure all build products are placed in the build directory, not the source tree
26
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
37
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)

src/include/ptk_event.h

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
extern "C" {
1111
#endif
1212

13+
#define PTK_APP_EVENT_DATA_SIZE 256
14+
1315
/**
1416
* Connection state flags - can be combined
1517
*/
@@ -27,7 +29,6 @@ typedef enum {
2729
typedef enum {
2830
PTK_EVENT_SOURCE_TCP = 1, /* TCP socket */
2931
PTK_EVENT_SOURCE_UDP = 2, /* UDP socket */
30-
PTK_EVENT_SOURCE_SERIAL = 3, /* Serial port */
3132
PTK_EVENT_SOURCE_APP_EVENT = 4, /* Application event */
3233
PTK_EVENT_SOURCE_TIMER = 5 /* Timer event source */
3334
} ptk_connection_type_t;
@@ -60,23 +61,13 @@ typedef struct {
6061
bool repeating; /* Whether timer repeats automatically */
6162
uint64_t next_fire_time; /* Internal: next fire time */
6263
bool active; /* Internal: timer is active */
63-
} ptk_timer_event_source_t;
64-
65-
/**
66-
* Application/user event source
67-
* Thread-safe signaling mechanism for inter-thread communication
68-
*/
69-
typedef struct {
70-
ptk_connection_t base; /* Must be first - enables polymorphism */
71-
ptk_atomic32_t signal_count; /* Atomic signal counter */
72-
uint32_t id; /* User-defined event ID */
73-
} ptk_app_event_source_t;
64+
} ptk_timer_connection_t;
7465

7566

7667
/**
7768
* Timer operations
7869
*/
79-
ptk_status_t ptk_init_timer(ptk_timer_event_source_t* timer,
70+
ptk_status_t ptk_init_timer(ptk_timer_connection_t* timer,
8071
uint32_t interval_ms,
8172
uint32_t id,
8273
bool repeating);
@@ -98,37 +89,50 @@ typedef struct {
9889
int fd;
9990
struct sockaddr_in addr;
10091
uint32_t connect_timeout_ms;
101-
} ptk_tcp_connection_t;
92+
} ptk_tcp_client_connection_t;
10293

10394
typedef struct {
10495
ptk_connection_t base; /* Must be first - enables polymorphism */
10596
int fd;
10697
struct sockaddr_in addr;
107-
uint32_t bind_timeout_ms;
108-
} ptk_udp_connection_t;
98+
uint32_t connect_timeout_ms;
99+
} ptk_tcp_server_connection_t;
109100

110101
typedef struct {
111102
ptk_connection_t base; /* Must be first - enables polymorphism */
112103
int fd;
113-
char device_path[256];
114-
int baud_rate;
115-
uint32_t read_timeout_ms;
116-
} ptk_serial_connection_t;
104+
struct sockaddr_in local_addr;
105+
struct sockaddr_in remote_addr;
106+
uint32_t bind_timeout_ms;
107+
} ptk_udp_connection_t;
108+
109+
110+
117111

118112
typedef struct {
119113
ptk_connection_t base; /* Must be first - enables polymorphism */
120114
int fd; /* might not be used */
121-
uint8_t data[PTK_APP_EVENT_DATA_SIZE]
115+
ptk_slice_bytes_t buffer; /* User-provided buffer slice */
116+
size_t data_len; /* Amount of valid data */
117+
volatile int8_t data_ready; /* 0 = empty, 1 = full */
118+
// For POSIX: pthread_mutex_t mutex; pthread_cond_t cond;
122119
} ptk_app_event_connection_t;
123120

124121
/**
125122
* Stack-based connection initialization
126123
* No memory allocation - all data structures are provided by caller
127124
*/
128-
ptk_status_t ptk_init_tcp_connection(ptk_tcp_connection_t* conn, const char* host, uint16_t port);
125+
ptk_status_t ptk_init_tcp_client_connection(ptk_tcp_client_connection_t* conn, const char* host, uint16_t port);
126+
ptk_status_t ptk_init_tcp_server_connection(ptk_tcp_server_connection_t* conn, const char* host, uint16_t port);
129127
ptk_status_t ptk_init_udp_connection(ptk_udp_connection_t* conn, const char* host, uint16_t port);
130-
ptk_status_t ptk_init_serial_connection(ptk_serial_connection_t* conn, const char* device, int baud);
131-
ptk_status_t ptk_init_app_event_connection(ptk_app_event_connection_t* conn);
128+
/**
129+
* Initialize an app event connection with a user-provided buffer slice.
130+
* The buffer must remain valid for the lifetime of the connection.
131+
* @param conn Pointer to the connection struct
132+
* @param buffer_slice User-provided byte slice
133+
* @return PTK_OK on success, error code otherwise
134+
*/
135+
ptk_status_t ptk_init_app_event_connection(ptk_app_event_connection_t* conn, ptk_slice_bytes_t buffer_slice);
132136

133137
/**
134138
* Connection I/O operations
@@ -138,11 +142,10 @@ ptk_slice_t ptk_connection_read(ptk_connection_t* conn, ptk_slice_t *buffer, uin
138142
ptk_status_t ptk_connection_write(ptk_connection_t* conn, ptk_slice_t *data, uint32_t timeout_ms);
139143
ptk_status_t ptk_connection_close(ptk_connection_t* conn);
140144

141-
142145
/**
143-
* Get current time in milliseconds (platform abstracted)
144-
* Used internally by timer system
146+
* @brief * TCP server accept operation
145147
*/
148+
ptk_status_t ptk_tcp_server_accept(ptk_tcp_server_connection_t* server, ptk_tcp_client_connection_t* client_conn, uint32_t timeout_ms);
146149

147150

148151
/* Universal wait function - polymorphic, works with any event source including timers */
@@ -155,9 +158,8 @@ int ptk_wait_for_multiple(ptk_connection_t** event_sources,
155158
/**
156159
* Declare slice types for event management
157160
*/
158-
PTK_DECLARE_SLICE_TYPE(timers, ptk_timer_event_source_t);
159-
PTK_DECLARE_SLICE_TYPE(app_events, ptk_app_event_source_t);
160-
PTK_DECLARE_SLICE_TYPE(event_conns, ptk_event_connection_t);
161+
PTK_DECLARE_SLICE_TYPE(timers, ptk_timer_connection_t);
162+
PTK_DECLARE_SLICE_TYPE(app_events, ptk_app_event_connection_t);
161163

162164
#ifdef __cplusplus
163165
}

src/include/ptk_log.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#ifndef PTK_LOG_H
2+
#define PTK_LOG_H
3+
4+
#include <stdio.h>
5+
6+
#ifdef NDEBUG
7+
#define PTK_LOG(fmt, ...) ((void)0)
8+
#else
9+
#define PTK_LOG(fmt, ...) \
10+
do {fprintf(stderr, "[%s:%s:%d] " fmt "\n", __FILE__, __func__, __LINE__, ##__VA_ARGS__); fflush(stderr);} while(0)
11+
#endif
12+
13+
// Print a byte slice as hex
14+
#ifdef NDEBUG
15+
#define PTK_LOG_SLICE(slice) ((void)0)
16+
#else
17+
#include "ptk_slice.h"
18+
static inline void ptk_log_slice_impl(const char* file, const char* func, int line, const ptk_slice_t* slice) {
19+
fprintf(stderr, "[%s:%s:%d] slice (len=%zu):\n", file, func, line, (slice)->len);
20+
const uint8_t* data = (const uint8_t*)(slice)->data;
21+
size_t len = (slice)->len;
22+
for (size_t i = 0; i < len; i += 16) {
23+
fprintf(stderr, "%08zx: ", i);
24+
size_t row_end = (i + 16 < len) ? (i + 16) : len;
25+
for (size_t j = i; j < row_end; ++j) {
26+
fprintf(stderr, "%02X ", data[j]);
27+
}
28+
fprintf(stderr, "\n");
29+
}
30+
fflush(stderr);
31+
}
32+
#define PTK_LOG_SLICE(slice) ptk_log_slice_impl(__FILE__, __func__, __LINE__, &(slice))
33+
#endif
34+
35+
#endif // PTK_LOG_H

0 commit comments

Comments
 (0)