From 5cc24d02cc1ecce3a446f36c7975dd08232f39e3 Mon Sep 17 00:00:00 2001 From: John Hinke Date: Tue, 5 May 2026 15:48:23 -0700 Subject: [PATCH] Initial prototype client/server app - This demonstrates very simple pstop behavior - Fixing bug in getting current time --- pstop_c/CMakeLists.txt | 15 ++ pstop_c/examples/client/client_app.c | 155 ++++++++++++++++++ pstop_c/examples/machine/machine_app.c | 34 +++- pstop_c/pstop/CMakeLists.txt | 1 + pstop_c/pstop/include/pstop/os.h | 22 +++ .../pstop/include/pstop/pstop_application.h | 7 +- pstop_c/pstop/src/pstop/machine.c | 10 +- pstop_c/pstop/src/pstop/os.c | 12 ++ pstop_c/pstop/src/pstop/protocol.c | 16 +- pstop_c/pstop/src/pstop/pstop_application.c | 3 +- pstop_c/pstop/src/pstop/pstop_msg.c | 15 +- pstop_c/pstop/src/pstop/time.c | 4 +- pstop_c/pstop/test/src/pstop/machine_test.c | 2 +- .../test/src/pstop/machine_timeout_test.c | 2 +- pstop_c/pstop/test/src/pstop/pstop_msg_test.c | 14 +- .../include/transport/udp/udp_transport.h | 13 +- .../src/transport/udp/udp_transport.c | 51 ++++-- 17 files changed, 329 insertions(+), 47 deletions(-) create mode 100644 pstop_c/examples/client/client_app.c create mode 100644 pstop_c/pstop/include/pstop/os.h create mode 100644 pstop_c/pstop/src/pstop/os.c diff --git a/pstop_c/CMakeLists.txt b/pstop_c/CMakeLists.txt index 9ac3f99..41234b5 100644 --- a/pstop_c/CMakeLists.txt +++ b/pstop_c/CMakeLists.txt @@ -20,3 +20,18 @@ target_link_libraries(machine_app PUBLIC pstop transport_udp ) + +add_executable(client_app + examples/client/client_app.c +) + +target_include_directories(client_app PUBLIC + ${CMAKE_CURRENT_SOURCE_DIR}/pstop/include + ${CMAKE_CURRENT_SOURCE_DIR}/transport/include + include +) + +target_link_libraries(client_app PUBLIC + pstop + transport_udp +) diff --git a/pstop_c/examples/client/client_app.c b/pstop_c/examples/client/client_app.c new file mode 100644 index 0000000..e84a40f --- /dev/null +++ b/pstop_c/examples/client/client_app.c @@ -0,0 +1,155 @@ + +// SPDX-FileCopyrightText: 2026 Polymath Robotics, Inc. +// SPDX-License-Identifier: Apache-2.0 + + +#include +#include + +#include "pstop/pstop_msg.h" +#include "transport/udp/udp_transport.h" +#include "pstop/device_id.h" +#include "pstop/os.h" + +udp_transport_data_t udp_transport; + +int +read_msg(udp_transport_data_t *transport, pstop_os_env *env, pstop_msg_t *resp, uint64_t timeout) +{ + struct sockaddr_storage client; + uint8_t respbytes[PSTOP_MESSAGE_SIZE]; + + uint64_t start = env->get_time_cb(); + + while(1) { + int result = transport_udp_read(&udp_transport, respbytes, PSTOP_MESSAGE_SIZE, &client); + if(result == PSTOP_MESSAGE_SIZE) { + pstop_message_decode(resp, respbytes); + return 1; + } + uint64_t now = env->get_time_cb(); + if((now - start) >= timeout) { + fprintf(stderr, "Timeout reading response\n"); + break; + } + } + + return 0; +} + +void +send_bond(udp_transport_data_t *transport, pstop_os_env *env, const device_id_t *uuid) +{ + uint8_t reqbytes[PSTOP_MESSAGE_SIZE]; + + pstop_msg_t req_msg; + pstop_msg_t resp_msg; + + req_msg.message = PSTOP_MESSAGE_BOND; + device_id_copy(&req_msg.id, uuid); + + pstop_message_encode(&req_msg, reqbytes); + + transport_udp_write(transport, reqbytes, PSTOP_MESSAGE_SIZE, NULL); + + if(read_msg(transport, env, &resp_msg, 2000)) { + fprintf(stderr, "Received msg: %d\n", resp_msg.message); + } +} + +void +send_ok(udp_transport_data_t *transport, pstop_os_env *env, const device_id_t *uuid, int is_ok) +{ + uint8_t reqbytes[PSTOP_MESSAGE_SIZE]; + + pstop_msg_t req_msg; + pstop_msg_t resp_msg; + + if(is_ok) { + req_msg.message = PSTOP_MESSAGE_OK; + } + else { + req_msg.message = PSTOP_MESSAGE_STOP; + } + + device_id_copy(&req_msg.id, uuid); + + pstop_message_encode(&req_msg, reqbytes); + + transport_udp_write(transport, reqbytes, PSTOP_MESSAGE_SIZE, NULL); + + if(read_msg(transport, env, &resp_msg, 2000)) { + fprintf(stderr, "Received msg: %d\n", resp_msg.message); + } +} + +void +send_unbond(udp_transport_data_t *transport, pstop_os_env *env, const device_id_t *uuid) +{ + uint8_t reqbytes[PSTOP_MESSAGE_SIZE]; + + pstop_msg_t req_msg; + pstop_msg_t resp_msg; + + req_msg.message = PSTOP_MESSAGE_UNBOND; + device_id_copy(&req_msg.id, uuid); + + pstop_message_encode(&req_msg, reqbytes); + + transport_udp_write(transport, reqbytes, PSTOP_MESSAGE_SIZE, NULL); + + if(read_msg(transport, env, &resp_msg, 2000)) { + fprintf(stderr, "Received msg: %d\n", resp_msg.message); + } +} + +int +main(int argc, char *argv[]) +{ + transport_udp_init(&udp_transport); + + if(argc < 3) { + printf("USAGE:\n"); + printf(" %s \n\n", argv[0]); + printf("Example:\n"); + printf(" client_app 8890 15\n"); + return -1; + } + + int port = atoi(argv[1]); + int uuid_byte = atoi(argv[2]); + + int result = transport_udp_connect(&udp_transport, "127.0.0.1", port); + if(result < 0) { + fprintf(stderr, "Unable to open UDP: %d\n", result); + return -1; + } + + uint8_t reqbytes[PSTOP_MESSAGE_SIZE]; + uint8_t respbytes[PSTOP_MESSAGE_SIZE]; + + pstop_os_env env; + pstop_os_env_init(&env); + + pstop_msg_t req_msg; + pstop_msg_t resp_msg; + + device_id_t uuid = { + .data = { + 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, + 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0xFF + } + }; + uuid.data[15] = (uint8_t)(uuid_byte & 0xFFU); + + fprintf(stderr, "uuid=%d\n", uuid.data[15]); + send_bond(&udp_transport, &env, &uuid); + + for(int i = 0; i < 3; ++i) { + send_ok(&udp_transport, &env, &uuid, (i % 2)); + } + + send_unbond(&udp_transport, &env, &uuid); + + return 0; +} diff --git a/pstop_c/examples/machine/machine_app.c b/pstop_c/examples/machine/machine_app.c index 39f2f7f..1e83d69 100644 --- a/pstop_c/examples/machine/machine_app.c +++ b/pstop_c/examples/machine/machine_app.c @@ -3,6 +3,7 @@ // SPDX-License-Identifier: Apache-2.0 #include +#include #include "transport/udp/udp_transport.h" #include "pstop/machine.h" @@ -23,18 +24,37 @@ int is_operator_allowed(const device_id_t *device_id) return 1; } +pstop_status_message_t lastStatus = 0; + +static +int +robot_status(pstop_status_message_t status) +{ + if(lastStatus != status) { + fprintf(stderr, "Status = %d\n", (int)status); + lastStatus = status; + } + + return 0; +} + int main(int argc, char *argv[]) { - transport_init_udp(&udp_transport); + transport_udp_init(&udp_transport); pstop_application_init(&pstop_app); pstop_app.app_config.default_timeout_ms = 1000U; pstop_app.operator_allowed_cb = is_operator_allowed; + pstop_app.status_cb = robot_status; machine_init(&machine, &pstop_app, pstop_clients, MAX_CLIENTS); - int result = transport_udp_open(&udp_transport, "localhost", 8890); + int port = 8890; + if(argc > 1) { + port = atoi(argv[1]); + } + int result = transport_udp_listen(&udp_transport, "127.0.0.1", port); if(result < 0) { fprintf(stderr, "Unable to open UDP: %d\n", result); return -1; @@ -47,21 +67,25 @@ main(int argc, char *argv[]) pstop_msg_t resp_msg; pstop_msg_t *resp_msg_ptr = &resp_msg; - fprintf(stderr, "Connected to localhost:8890\n"); + fprintf(stderr, "Connected to localhost:%d\n", port); while(1) { struct sockaddr_storage client; result = transport_udp_read(&udp_transport, reqbytes, PSTOP_MESSAGE_SIZE, &client); if(result == PSTOP_MESSAGE_SIZE) { - pstop_message_encode(&req_msg, reqbytes); + pstop_message_decode(&req_msg, reqbytes); + fprintf(stderr, "Got message: %d from %d\n", req_msg.message, req_msg.id.data[15]); pstop_error_t error = machine.handle_protocol_message_cb(&machine, &req_msg, &resp_msg_ptr); if(resp_msg_ptr != NULL) { pstop_message_encode(&resp_msg, respbytes); transport_udp_write(&udp_transport, respbytes, PSTOP_MESSAGE_SIZE, (struct sockaddr_in *)&client); } + else { + fprintf(stderr, "Invalid response: %d\n", (int)error); + } } - machine.check_heartbeats_cb(&machine); + //machine.check_heartbeats_cb(&machine); } transport_udp_close(&udp_transport); diff --git a/pstop_c/pstop/CMakeLists.txt b/pstop_c/pstop/CMakeLists.txt index c718d68..5d1032c 100644 --- a/pstop_c/pstop/CMakeLists.txt +++ b/pstop_c/pstop/CMakeLists.txt @@ -9,6 +9,7 @@ add_library(pstop src/pstop/checksum.c src/pstop/device_id.c src/pstop/machine.c + src/pstop/os.c src/pstop/protocol.c src/pstop/pstop_application.c src/pstop/pstop_client.c diff --git a/pstop_c/pstop/include/pstop/os.h b/pstop_c/pstop/include/pstop/os.h new file mode 100644 index 0000000..62be1d7 --- /dev/null +++ b/pstop_c/pstop/include/pstop/os.h @@ -0,0 +1,22 @@ +// SPDX-FileCopyrightText: 2026 Polymath Robotics, Inc. +// SPDX-License-Identifier: Apache-2.0 + +#ifndef PSTOP_OS_H +#define PSTOP_OS_H + +#include + +typedef uint64_t (* get_current_time_t)(void); + +typedef struct pstop_os_env { + + /** + * Callback to return the current time. + */ + get_current_time_t get_time_cb; + +} pstop_os_env; + +void pstop_os_env_init(pstop_os_env *env); + +#endif /* PSTOP_OS_H */ diff --git a/pstop_c/pstop/include/pstop/pstop_application.h b/pstop_c/pstop/include/pstop/pstop_application.h index 81ac1d4..e448a1f 100644 --- a/pstop_c/pstop/include/pstop/pstop_application.h +++ b/pstop_c/pstop/include/pstop/pstop_application.h @@ -8,13 +8,13 @@ #include "pstop/error.h" #include "pstop/device_id.h" +#include "pstop/os.h" typedef enum { PSTOP_STATUS_OK = 0, PSTOP_STATUS_STOP = 1 } pstop_status_message_t; -typedef uint64_t (* get_current_time_t)(void); typedef int (* is_operator_allowed_t)(const device_id_t *device_id); typedef int (* pstop_status_t)(pstop_status_message_t status); @@ -34,10 +34,7 @@ typedef struct { typedef struct pstop_application_t { - /** - * Callback to return the current time. - */ - get_current_time_t get_time_cb; + pstop_os_env env; /** * The device ID for this machine diff --git a/pstop_c/pstop/src/pstop/machine.c b/pstop_c/pstop/src/pstop/machine.c index 28c2632..d781226 100644 --- a/pstop_c/pstop/src/pstop/machine.c +++ b/pstop_c/pstop/src/pstop/machine.c @@ -36,7 +36,7 @@ static void init_new_client(pstop_application_t *application, pstop_client_data_t *client, const pstop_msg_t *msg) { - uint64_t now = application->get_time_cb(); + uint64_t now = application->env.get_time_cb(); device_id_copy(&(client->client_id), &(msg->id)); client->last_timestamp = now; @@ -223,7 +223,7 @@ machine_handle_message(pstop_machine_t *machine, const pstop_msg_t *req, pstop_m init_new_client(machine->application, client, req); } - uint64_t now = machine->application->get_time_cb(); + uint64_t now = machine->application->env.get_time_cb(); client->last_timestamp = now; switch(req->message) { @@ -245,7 +245,7 @@ static pstop_error_t machine_check_heartbeats(pstop_machine_t *machine) { - uint64_t now = machine->application->get_time_cb(); + uint64_t now = machine->application->env.get_time_cb(); int needsStop = 0; @@ -314,5 +314,7 @@ machine_stop_robot(pstop_machine_t *machine) machine->robot_state.restart_state = ROBOT_RESTART_STATE_NEED_STOP; machine->robot_state.client_stop_id = 0U; - machine->application->status_cb(PSTOP_STATUS_STOP); + if(machine->application->status_cb != NULL) { + machine->application->status_cb(PSTOP_STATUS_STOP); + } } diff --git a/pstop_c/pstop/src/pstop/os.c b/pstop_c/pstop/src/pstop/os.c new file mode 100644 index 0000000..8a762cd --- /dev/null +++ b/pstop_c/pstop/src/pstop/os.c @@ -0,0 +1,12 @@ + +// SPDX-FileCopyrightText: 2026 Polymath Robotics, Inc. +// SPDX-License-Identifier: Apache-2.0 + +#include "pstop/os.h" +#include "pstop/time.h" + +void +pstop_os_env_init(pstop_os_env *env) +{ + env->get_time_cb = time_get_now; +} diff --git a/pstop_c/pstop/src/pstop/protocol.c b/pstop_c/pstop/src/pstop/protocol.c index da0f591..a1b4ccd 100644 --- a/pstop_c/pstop/src/pstop/protocol.c +++ b/pstop_c/pstop/src/pstop/protocol.c @@ -31,6 +31,13 @@ check_message_order(pstop_client_data_t *client, const pstop_msg_t *msg) return PSTOP_OK; } +static +int +is_checksum_valid(const pstop_msg_t *req) +{ + return 1; +} + static pstop_error_t validate_message(pstop_machine_t *machine, pstop_client_data_t *client, const pstop_msg_t *req, pstop_msg_t **resp) @@ -65,7 +72,12 @@ validate_message(pstop_machine_t *machine, pstop_client_data_t *client, const ps pstop_error_t protocol_handle_message(pstop_machine_t *machine, const pstop_msg_t *req, pstop_msg_t **resp) { - // validate checksum + // validate black channel bits + + if(!is_checksum_valid(req)) { + *resp = NULL; + return PSTOP_MSG_INVALID_CHECKSUM; + } pstop_client_data_t *client = pstop_client_get(&(machine->pstops), &(req->id)); @@ -74,10 +86,12 @@ protocol_handle_message(pstop_machine_t *machine, const pstop_msg_t *req, pstop_ } else { // validate lost/out of order message + #if 0 pstop_error_t result = validate_message(machine, client, req, resp); if(result != PSTOP_OK) { return result; } + #endif } // now send the message to the machine for pstop handling. diff --git a/pstop_c/pstop/src/pstop/pstop_application.c b/pstop_c/pstop/src/pstop/pstop_application.c index 0813572..f932132 100644 --- a/pstop_c/pstop/src/pstop/pstop_application.c +++ b/pstop_c/pstop/src/pstop/pstop_application.c @@ -5,7 +5,6 @@ #include #include "pstop/pstop_application.h" -#include "pstop/time.h" static void @@ -25,7 +24,7 @@ pstop_application_config_init(pstop_application_config_t *config) void pstop_application_init(pstop_application_t *app) { - app->get_time_cb = time_get_now; + pstop_os_env_init(&app->env); app->operator_allowed_cb = NULL; app->status_cb = NULL; app->log_message_cb = no_log; diff --git a/pstop_c/pstop/src/pstop/pstop_msg.c b/pstop_c/pstop/src/pstop/pstop_msg.c index 683c5ae..4f7ec93 100644 --- a/pstop_c/pstop/src/pstop/pstop_msg.c +++ b/pstop_c/pstop/src/pstop/pstop_msg.c @@ -174,18 +174,13 @@ static void write_uint64(uint64_t value, uint8_t *data, size_t *pos) { - uint8_t *bytes = data + *pos; - *pos = *pos + 4U; #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ - bytes[0] = (uint8_t)(value & 0xFFU); - bytes[1] = (uint8_t)((value >> 8U) & 0xFFU); - bytes[2] = (uint8_t)((value >> 16U) & 0xFFU); - bytes[3] = (uint8_t)((value >> 24U) & 0xFFU); - bytes[4] = (uint8_t)((value >> 32U) & 0xFFU); - bytes[5] = (uint8_t)((value >> 40U) & 0xFFU); - bytes[6] = (uint8_t)((value >> 48U) & 0xFFU); - bytes[7] = (uint8_t)((value >> 56U) & 0xFFU); + uint64_t *bytes = (uint64_t *)(data + *pos); + *pos = *pos + 8U; + *bytes = value; #else + uint8_t *bytes = data + *pos; + *pos = *pos + 8U; bytes[7] = (uint8_t)(value & 0xFFU); bytes[6] = (uint8_t)((value >> 8U) & 0xFFU); bytes[5] = (uint8_t)((value >> 16U) & 0xFFU); diff --git a/pstop_c/pstop/src/pstop/time.c b/pstop_c/pstop/src/pstop/time.c index ff66e41..c49e0b7 100644 --- a/pstop_c/pstop/src/pstop/time.c +++ b/pstop_c/pstop/src/pstop/time.c @@ -11,10 +11,10 @@ time_get_now(void) { #ifdef __linux__ struct timespec ts; - timespec_get(&ts, CLOCK_REALTIME); + timespec_get(&ts, TIME_UTC); // Calculate milliseconds - return ((uint64_t)ts.tv_sec) * 1000ULL + ((uint64_t)ts.tv_nsec) / 1000000ULL; + return (((uint64_t)ts.tv_sec) * 1000ULL) + (((uint64_t)ts.tv_nsec) / 1000000ULL); #else return 0U; #endif diff --git a/pstop_c/pstop/test/src/pstop/machine_test.c b/pstop_c/pstop/test/src/pstop/machine_test.c index 54d6015..e5ed64b 100644 --- a/pstop_c/pstop/test/src/pstop/machine_test.c +++ b/pstop_c/pstop/test/src/pstop/machine_test.c @@ -50,7 +50,7 @@ log_error(pstop_error_t error, const char *message) static pstop_application_t pstop_app = { - .get_time_cb = get_time, + .env.get_time_cb = get_time, .machine_device_id.data = "testing", .operator_allowed_cb = is_operator_allowed, .status_cb = robot_status, diff --git a/pstop_c/pstop/test/src/pstop/machine_timeout_test.c b/pstop_c/pstop/test/src/pstop/machine_timeout_test.c index 1ed0ffb..dae138f 100644 --- a/pstop_c/pstop/test/src/pstop/machine_timeout_test.c +++ b/pstop_c/pstop/test/src/pstop/machine_timeout_test.c @@ -50,7 +50,7 @@ log_error(pstop_error_t error, const char *message) static pstop_application_t pstop_app = { - .get_time_cb = get_time, + .env.get_time_cb = get_time, .machine_device_id.data = "testing", .operator_allowed_cb = is_operator_allowed, .status_cb = robot_status, diff --git a/pstop_c/pstop/test/src/pstop/pstop_msg_test.c b/pstop_c/pstop/test/src/pstop/pstop_msg_test.c index 39940c3..c507777 100644 --- a/pstop_c/pstop/test/src/pstop/pstop_msg_test.c +++ b/pstop_c/pstop/test/src/pstop/pstop_msg_test.c @@ -65,9 +65,20 @@ encode_pstop_msg() { pstop_msg_t msg; msg.version = 0x0U; - msg.message = 0x3U; + msg.message = 0x2U; msg.stamp = 0x0807060504030201U; msg.received_stamp = 0x1817161514131211U; + uint8_t ID_BYTES[] = { + 0x20U, 0x21U, 0x22U, 0x23U, 0x24U, 0x25U, 0x26U, 0x27U, + 0x28U, 0x29U, 0x2AU, 0x2BU, 0x2CU, 0x2DU, 0x2EU, 0x2FU // device UUID + }; + device_id_set_bytes(&msg.id, ID_BYTES); + + uint8_t RECEIVER_ID_BYTES[] = { + 0x30U, 0x31U, 0x32U, 0x33U, 0x34U, 0x35U, 0x36U, 0x37U, + 0x38U, 0x39U, 0x3AU, 0x3BU, 0x3CU, 0x3DU, 0x3EU, 0x3FU // receiver UUID + }; + device_id_set_bytes(&msg.receiver_id, RECEIVER_ID_BYTES); msg.heartbeat_timeout = 0x43424140U; msg.counter = 0x53525150U; @@ -76,6 +87,7 @@ encode_pstop_msg() uint8_t bytes[PSTOP_MESSAGE_SIZE]; pstop_message_encode(&msg, bytes); + TEST_ASSERT_EQUAL_UINT8_ARRAY(PSTOP_MSG_BYTES, bytes, PSTOP_MESSAGE_SIZE); } void diff --git a/pstop_c/transport/include/transport/udp/udp_transport.h b/pstop_c/transport/include/transport/udp/udp_transport.h index 77490fb..d57b9fa 100755 --- a/pstop_c/transport/include/transport/udp/udp_transport.h +++ b/pstop_c/transport/include/transport/udp/udp_transport.h @@ -10,22 +10,27 @@ #include #include +typedef struct pollfd udp_transport_poll_t; + typedef struct { struct sockaddr_in addr; int fd; - struct pollfd poll_fds; + udp_transport_poll_t poll_fds; } udp_transport_data_t; -int transport_udp_open(udp_transport_data_t *transport, const char *host, int port); +void transport_udp_init(udp_transport_data_t *transport); + +int transport_udp_listen(udp_transport_data_t *transport, const char *host, int port); + +int transport_udp_connect(udp_transport_data_t *transport, const char *host, int port); + int transport_udp_close(udp_transport_data_t *transport); int transport_udp_read(udp_transport_data_t *transport, uint8_t *dest, size_t length, struct sockaddr_storage *clientAddr); int transport_udp_write(udp_transport_data_t *transport, const uint8_t *data, size_t length, struct sockaddr_in *target); -void transport_init_udp(udp_transport_data_t *transport); - #endif /* PSTOP_TRANSPORT_UDP_UDPTRANSPORT_H */ diff --git a/pstop_c/transport/src/transport/udp/udp_transport.c b/pstop_c/transport/src/transport/udp/udp_transport.c index 9358636..557f8ff 100755 --- a/pstop_c/transport/src/transport/udp/udp_transport.c +++ b/pstop_c/transport/src/transport/udp/udp_transport.c @@ -2,6 +2,8 @@ // SPDX-FileCopyrightText: 2026 Polymath Robotics, Inc. // SPDX-License-Identifier: Apache-2.0 +#include + #include #include #include @@ -11,8 +13,17 @@ #include "transport/udp/udp_transport.h" +void +transport_udp_init(udp_transport_data_t *transport) +{ + memset(&transport->addr, 0, sizeof(transport->addr)); + transport->fd = -1; + transport->poll_fds.fd = -1; + transport->poll_fds.events = 0; +} + int -transport_udp_open(udp_transport_data_t *transport, const char *host, int port) +transport_udp_listen(udp_transport_data_t *transport, const char *host, int port) { memset(&transport->addr, 0, sizeof(transport->addr)); transport->addr.sin_addr.s_addr = inet_addr(host); @@ -22,15 +33,41 @@ transport_udp_open(udp_transport_data_t *transport, const char *host, int port) // create datagram socket transport->fd = socket(AF_INET, SOCK_DGRAM, 0); if(transport->fd == -1) { + fprintf(stderr, "socket errno=%d\n", errno); return -1; } int result = bind(transport->fd, (struct sockaddr*)&transport->addr, sizeof(transport->addr)); + if(result < 0) { + fprintf(stderr, "errno=%d\n", errno); + return -1; + } + transport->poll_fds.fd = transport->fd; + transport->poll_fds.events = POLLIN; + + return transport->fd; +} + +int +transport_udp_connect(udp_transport_data_t *transport, const char *host, int port) +{ + memset(&transport->addr, 0, sizeof(transport->addr)); + transport->addr.sin_addr.s_addr = inet_addr(host); + transport->addr.sin_port = htons(port); + transport->addr.sin_family = AF_INET; + + // create datagram socket + transport->fd = socket(AF_INET, SOCK_DGRAM, 0); + if(transport->fd == -1) { + fprintf(stderr, "socket errno=%d\n", errno); + return -1; + } + transport->poll_fds.fd = transport->fd; transport->poll_fds.events = POLLIN; - return result; + return transport->fd; } int @@ -76,18 +113,10 @@ transport_udp_write(udp_transport_data_t *transport, const uint8_t *data, size_t } ssize_t numbytes = sendto(transport->fd, data, length, 0, addr, addrSize); - //fprintf(stderr, "Wrote bytes: %d\n", (int)numbytes); if(numbytes == -1) { - // fprintf(stderr, "errono=%d\n", errno); + fprintf(stderr, "errno=%d\n", errno); return -1; } return numbytes; } - -void -transport_init_udp(udp_transport_data_t *transport) -{ - transport->fd = -1; - memset(&transport->addr, 0, sizeof(transport->addr)); -}