/* * This function read from fd socket information about * connected socket */ static void receive_sock_connect_signal(struct pollfd *pollfd) { sock_connect_signal_t cs; char addr_str[MAX_ADDR_STR_LEN]; if (pollfd->revents & POLLIN) { int ret; poll_unregister_fd(pollfd->fd, receive_sock_connect_signal); ret = read(pollfd->fd, &cs, sizeof(cs)); if (ret != sizeof(cs)) { haltest_info("Read on connect return %d\n", ret); return; } haltest_info("Connection to %s channel %d status=%d\n", bt_bdaddr_t2str(&cs.bd_addr, addr_str), cs.channel, cs.status); if (cs.status == 0) poll_register_fd(pollfd->fd, POLLIN, receive_from_client); } if (pollfd->revents & POLLHUP) { haltest_error("Disconnected fd=%d revents=0x%X\n", pollfd->fd, pollfd->revents); poll_unregister_fd(pollfd->fd, receive_sock_connect_signal); } }
/* called when there is something on stdin */ static void stdin_handler(struct pollfd *pollfd) { char buf[10]; if (pollfd->revents & POLLIN) { int count = read(fd_stack[fd_stack_pointer - 1], buf, 10); if (count > 0) { int i; for (i = 0; i < count; ++i) terminal_process_char(buf[i], process_line); return; } } if (fd_stack_pointer > 1) poll_register_fd(fd_stack[fd_stack_pointer - 2], POLLIN, stdin_handler); if (fd_stack_pointer > 0) { poll_unregister_fd(fd_stack[--fd_stack_pointer], stdin_handler); if (fd_stack[fd_stack_pointer]) close(fd_stack[fd_stack_pointer]); } }
/* handles incoming connections on socket */ static void client_connected(struct pollfd *pollfd) { haltest_info("client connected %x\n", pollfd->revents); if (pollfd->revents & POLLHUP) poll_unregister_fd(pollfd->fd, client_connected); else if (pollfd->revents & POLLIN) read_accepted(pollfd->fd); }
/* * This function reads data from file descriptor and * prints it to the user */ static void receive_from_client(struct pollfd *pollfd) { char buf[16]; /* * Buffer for lines: * 41 42 43 20 20 00 31 32 00 07 04 00 00 00 00 00 ABC .12..... */ char outbuf[sizeof(buf) * 4 + 2]; int i; int ret; if (pollfd->revents & POLLHUP) { haltest_error("Disconnected fd=%d\n", pollfd->fd); poll_unregister_fd(pollfd->fd, receive_from_client); } else if (pollfd->revents & POLLIN) { haltest_info("receiving from client fd=%d\n", pollfd->fd); do { memset(outbuf, ' ', sizeof(outbuf)); outbuf[sizeof(outbuf) - 1] = 0; ret = recv(pollfd->fd, buf, sizeof(buf), MSG_DONTWAIT); for (i = 0; i < ret; ++i) sprintf(outbuf + i * 3, "%02X ", (unsigned) buf[i]); outbuf[i * 3] = ' '; for (i = 0; i < ret; ++i) sprintf(outbuf + 48 + i, "%c", (isprint(buf[i]) ? buf[i] : '.')); if (ret > 0) haltest_info("%s\n", outbuf); } while (ret > 0); } else { /* For now disconnect on all other events */ haltest_error("Poll event %x\n", pollfd->revents); poll_unregister_fd(pollfd->fd, receive_from_client); } }
static void process_file(const char *name) { int fd = open(name, O_RDONLY); if (fd < 0) { haltest_error("Can't open file: %s for reading\n", name); return; } if (fd_stack_pointer >= 10) { haltest_error("To many open files\n"); close(fd); return; } fd_stack[fd_stack_pointer++] = fd; poll_unregister_fd(fd_stack[fd_stack_pointer - 2], stdin_handler); poll_register_fd(fd_stack[fd_stack_pointer - 1], POLLIN, stdin_handler); }