1010extern "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 {
2729typedef 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
10394typedef 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
110101typedef 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
118112typedef 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 );
129127ptk_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
138142ptk_status_t ptk_connection_write (ptk_connection_t * conn , ptk_slice_t * data , uint32_t timeout_ms );
139143ptk_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}
0 commit comments