void *read_serial_packet(serial_source src, int *len) /* Effects: Read the serial source src. If a packet is available, return it. If in blocking mode and no packet is available, wait for one. Returns: the packet read (in newly allocated memory), with *len is set to the packet length, or NULL if no packet is yet available and the serial source is in non-blocking mode */ { read_and_process(src, TRUE); for (;;) { struct packet_list *entry; entry = pop_protocol_packet(src, P_PACKET_NO_ACK); if (entry) { uint8_t *packet = entry->packet; *len = entry->len; free(entry); return packet; } if (src->non_blocking && serial_source_empty(src)) return NULL; source_wait(src, NULL); read_and_process(src, src->non_blocking); } }
int main(int argc, char **argv) { int serfd; if (argc != 4) { fprintf(stderr, "Usage: %s <port> <device> <rate> - act as a serial forwarder on <port>\n" "(listens to serial port <device> at baud rate <rate>)\n" , argv[0]); exit(2); } if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) fprintf(stderr, "Warning: failed to ignore SIGPIPE.\n"); open_serial(argv[2], platform_baud_rate(argv[3])); serfd = serial_source_fd(src); open_server_socket(atoi(argv[1])); for (;;) { fd_set rfds; int maxfd = -1; struct timeval zero; int serial_empty; int ret; zero.tv_sec = zero.tv_usec = 0; FD_ZERO(&rfds); fd_wait(&rfds, &maxfd, serfd); fd_wait(&rfds, &maxfd, server_socket); wait_clients(&rfds, &maxfd); serial_empty = serial_source_empty(src); if (serial_empty) ret = select(maxfd + 1, &rfds, NULL, NULL, NULL); else { ret = select(maxfd + 1, &rfds, NULL, NULL, &zero); check_serial(); } if (ret >= 0) { if (FD_ISSET(serfd, &rfds)) check_serial(); if (FD_ISSET(server_socket, &rfds)) check_new_client(); check_clients(&rfds); } } }
int main(int argc, char **argv) { int serfd; if (argc != 4) { fprintf(stderr, "Usage: %s <device> <rate> <file> - act as a serial forwarder on <port>\n" "(listens to serial port <device> at baud rate <rate>)\n", argv[0]); exit(2); } if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) fprintf(stderr, "Warning: failed to ignore SIGPIPE.\n"); if (signal(SIGINT, sigproc) == SIG_ERR) fprintf(stderr, "Warning: failed to set the SIGINT interruption.\n"); srcId = -1; open_serial(argv[1], platform_baud_rate(argv[2])); serfd = serial_source_fd(src); open_file_fd(argv[3]); dup2(filefd, 1); for (;;) { fd_set rfds; int maxfd = -1; struct timeval zero; int serial_empty; int ret; zero.tv_sec = zero.tv_usec = 0; FD_ZERO(&rfds); fd_wait(&rfds, &maxfd, serfd); serial_empty = serial_source_empty(src); check_serial(); if (ret >= 0) { if (FD_ISSET(serfd, &rfds)) check_serial(); } } }