void main_loop() { uart_default_config(¤t_config); if (uart_init(&uart, handle_write_completed, handle_read_completed, handle_notify_read) < 0) errx(EXIT_FAILURE, "uart_init failed"); struct erlcmd *handler = malloc(sizeof(struct erlcmd)); erlcmd_init(handler, handle_elixir_request, NULL); for (;;) { struct pollfd fdset[3]; fdset[0].fd = STDIN_FILENO; fdset[0].events = POLLIN; fdset[0].revents = 0; int timeout = -1; // Wait forever unless told by otherwise int count = uart_add_poll_events(uart, &fdset[1], &timeout); int rc = poll(fdset, count + 1, timeout); if (rc < 0) { // Retry if EINTR if (errno == EINTR) continue; err(EXIT_FAILURE, "poll"); } if (fdset[0].revents & (POLLIN | POLLHUP)) { if (erlcmd_process(handler)) break; } // Call uart_process if it added any events if (count) uart_process(uart, &fdset[1]); } // Exit due to Erlang trying to end the process. // if (uart_is_open(uart)) uart_flush_all(uart); }
/** * @brief The main function. * It waits for data in the buffer and calls the driver. */ int i2c_main(int argc, char *argv[]) { if (argc != 4) errx(EXIT_FAILURE, "Must pass device path and device address as arguments"); struct i2c_info i2c; i2c_init(&i2c, argv[2], strtoul(argv[3], 0, 0)); struct erlcmd handler; erlcmd_init(&handler, i2c_handle_request, &i2c); for (;;) { // Loop forever and process requests from Erlang. erlcmd_process(&handler); } return 1; }
void main_loop() { uart_default_config(¤t_config); if (uart_init(&uart, handle_write_completed, handle_read_completed, handle_notify_read) < 0) errx(EXIT_FAILURE, "uart_init failed"); struct erlcmd *handler = malloc(sizeof(struct erlcmd)); erlcmd_init(handler, handle_elixir_request, NULL); bool running = true; while (running) { HANDLE handles[3]; handles[0] = erlcmd_wfmo_event(handler); DWORD timeout = INFINITE; DWORD count = 1 + uart_add_wfmo_handles(uart, &handles[1], &timeout); debug("Calling WFMO count=%d", (int) count); DWORD result = WaitForMultipleObjects(count, handles, FALSE, timeout); debug("WFMO returned %d", (int) result); switch(result) { case WAIT_OBJECT_0 + 0: if (erlcmd_process(handler)) running = false; break; case WAIT_OBJECT_0 + 1: uart_process_handle(uart, handles[1]); break; case WAIT_OBJECT_0 + 2: uart_process_handle(uart, handles[2]); break; case WAIT_TIMEOUT: uart_process_timeout(uart); break; case WAIT_FAILED: debug("WFMO wait failed! %d", (int) GetLastError()); // TODO: Is this ever a transient occurrence that we // should ignore and retry? running = false; break; default: break; } } // Exit due to Erlang trying to end the process. if (uart_is_open(uart)) uart_flush_all(uart); }