diff --git a/dev_in.c b/dev_in.c index b9ca943..ebf4497 100644 --- a/dev_in.c +++ b/dev_in.c @@ -59,61 +59,16 @@ typedef struct dev_in { static int map_message_from_iio(dev_in_iio_t *const in_iio, in_message_t *const messages, size_t messages_len) { int res = -EIO; - /* - * data layout is: - * - [0][0...2] => [gyro][x...z] - * - [1][0...2] => [acce][x...z] - * - */ - uint16_t data[2][3]; + uint8_t data[32]; - res = read(dev_iio_get_buffer_fd(in_iio->iiodev), &data[0][0], sizeof(uint16_t)); - if (res != sizeof(uint16_t)) { - fprintf(stderr, "Error reading from %s: %d\n", dev_iio_get_name(in_iio->iiodev), res); + res = read(dev_iio_get_buffer_fd(in_iio->iiodev), &data[0], sizeof(data)); + if (res == -1) { + res = errno; + res = res > 0 ? -1 * res : res; goto send_message_from_iio_err; - } else { - printf("OK\n"); } - res = read(dev_iio_get_buffer_fd(in_iio->iiodev), &data[0][1], sizeof(uint16_t)); - if (res != sizeof(uint16_t)) { - fprintf(stderr, "Error reading from %s: %d\n", dev_iio_get_name(in_iio->iiodev), res); - goto send_message_from_iio_err; - } else { - printf("OK\n"); - } - - res = read(dev_iio_get_buffer_fd(in_iio->iiodev), &data[0][2], sizeof(uint16_t)); - if (res != sizeof(uint16_t)) { - fprintf(stderr, "Error reading from %s: %d\n", dev_iio_get_name(in_iio->iiodev), res); - goto send_message_from_iio_err; - } else { - printf("OK\n"); - } - - res = read(dev_iio_get_buffer_fd(in_iio->iiodev), &data[1][0], sizeof(uint16_t)); - if (res != sizeof(uint16_t)) { - fprintf(stderr, "Error reading from %s: %d\n", dev_iio_get_name(in_iio->iiodev), res); - goto send_message_from_iio_err; - } else { - printf("OK\n"); - } - - res = read(dev_iio_get_buffer_fd(in_iio->iiodev), &data[1][1], sizeof(uint16_t)); - if (res != sizeof(uint16_t)) { - fprintf(stderr, "Error reading from %s: %d\n", dev_iio_get_name(in_iio->iiodev), res); - goto send_message_from_iio_err; - } else { - printf("OK\n"); - } - - res = read(dev_iio_get_buffer_fd(in_iio->iiodev), &data[1][2], sizeof(uint16_t)); - if (res != sizeof(uint16_t)) { - fprintf(stderr, "Error reading from %s: %d\n", dev_iio_get_name(in_iio->iiodev), res); - goto send_message_from_iio_err; - } else { - printf("OK\n"); - } + res = 0; send_message_from_iio_err: return res; diff --git a/rogue_enemy.h b/rogue_enemy.h index abd4e83..9a8b918 100644 --- a/rogue_enemy.h +++ b/rogue_enemy.h @@ -14,6 +14,7 @@ #include #include #include +#include #include #include @@ -24,6 +25,7 @@ #include #include #include +#include #include #include diff --git a/stray_ally.c b/stray_ally.c index c7cb70e..51ba18e 100644 --- a/stray_ally.c +++ b/stray_ally.c @@ -59,6 +59,12 @@ int main(int argc, char ** argv) { goto main_err; } + const uint64_t timeout_ms = 1500; + + struct pollfd poll_fds[2]; + poll_fds[0].fd = -1 /*signalfd(-1, )*/; + poll_fds[0].events = POLL_IN; + int sd=-1; struct sockaddr_un serveraddr; do { @@ -87,29 +93,38 @@ int main(int argc, char ** argv) { break; } + poll_fds[1].fd = sd; + poll_fds[1].events = POLL_IN; + while (true) { - const int client_fd = accept(sd, NULL, NULL); - if (client_fd < 0) { - fprintf(stderr, "Error in getting a client connected: %d\n", client_fd); - continue; - } + const int poll_ret = poll(poll_fds, sizeof(poll_fds) / sizeof(poll_fds[0]), timeout_ms); - // here the client_fd is good - if (pthread_mutex_lock(&dev_out_thread_data.communication.endpoint.ssocket.mutex) == 0) { - int i; - for (i = 0; i < MAX_CONNECTED_CLIENTS; ++i) { - if (dev_out_thread_data.communication.endpoint.ssocket.clients[i] < 0) { - dev_out_thread_data.communication.endpoint.ssocket.clients[i] = client_fd; - break; + if (poll_fds[0].revents & POLLIN) { + // TODO: read for signals + } else if (poll_fds[1].revents & POLLIN) { + const int client_fd = accept(sd, NULL, NULL); + if (client_fd < 0) { + fprintf(stderr, "Error in getting a client connected: %d\n", client_fd); + continue; + } + + // here the client_fd is good + if (pthread_mutex_lock(&dev_out_thread_data.communication.endpoint.ssocket.mutex) == 0) { + int i; + for (i = 0; i < MAX_CONNECTED_CLIENTS; ++i) { + if (dev_out_thread_data.communication.endpoint.ssocket.clients[i] < 0) { + dev_out_thread_data.communication.endpoint.ssocket.clients[i] = client_fd; + break; + } } - } - if (i == MAX_CONNECTED_CLIENTS) { - fprintf(stderr, "Could not find a free spot fot the incoming client -- client will be rejected\n"); - close(client_fd); - } + if (i == MAX_CONNECTED_CLIENTS) { + fprintf(stderr, "Could not find a free spot fot the incoming client -- client will be rejected\n"); + close(client_fd); + } - pthread_mutex_unlock(&dev_out_thread_data.communication.endpoint.ssocket.mutex); + pthread_mutex_unlock(&dev_out_thread_data.communication.endpoint.ssocket.mutex); + } } } } while (false);