| 16 | static const auto LOGGER = Logger("GpsDevice"); |
| 17 | |
| 18 | int32_t GpsDevice::threadMain() { |
| 19 | uint8_t buffer[GPS_UART_BUFFER_SIZE]; |
| 20 | |
| 21 | auto* uart = device_find_by_name(configuration.uartName); |
| 22 | if (uart == nullptr) { |
| 23 | LOGGER.error("Failed to find UART {}", configuration.uartName); |
| 24 | return -1; |
| 25 | } |
| 26 | |
| 27 | struct UartConfig uartConfig = { |
| 28 | .baud_rate = configuration.baudRate, |
| 29 | .data_bits = UART_CONTROLLER_DATA_8_BITS, |
| 30 | .parity = UART_CONTROLLER_PARITY_DISABLE, |
| 31 | .stop_bits = UART_CONTROLLER_STOP_BITS_1 |
| 32 | }; |
| 33 | |
| 34 | error_t error = uart_controller_set_config(uart, &uartConfig); |
| 35 | if (error != ERROR_NONE) { |
| 36 | LOGGER.error("Failed to configure UART {}: {}", configuration.uartName, error_to_string(error)); |
| 37 | return -1; |
| 38 | } |
| 39 | |
| 40 | error = uart_controller_open(uart); |
| 41 | if (error != ERROR_NONE) { |
| 42 | LOGGER.error("Failed to open UART {}: {}", configuration.uartName, error_to_string(error)); |
| 43 | return -1; |
| 44 | } |
| 45 | |
| 46 | GpsModel model = configuration.model; |
| 47 | if (model == GpsModel::Unknown) { |
| 48 | model = probe(uart); |
| 49 | if (model == GpsModel::Unknown) { |
| 50 | LOGGER.error("Probe failed"); |
| 51 | setState(State::Error); |
| 52 | return -1; |
| 53 | } |
| 54 | } |
| 55 | mutex.lock(); |
| 56 | this->model = model; |
| 57 | mutex.unlock(); |
| 58 | |
| 59 | if (!init(uart, model)) { |
| 60 | LOGGER.error("Init failed"); |
| 61 | setState(State::Error); |
| 62 | return -1; |
| 63 | } |
| 64 | |
| 65 | setState(State::On); |
| 66 | |
| 67 | // Reference: https://gpsd.gitlab.io/gpsd/NMEA.html |
| 68 | while (!isThreadInterrupted()) { |
| 69 | size_t bytes_read = 0; |
| 70 | uart_controller_read_until(uart, buffer, GPS_UART_BUFFER_SIZE, '\n', true, &bytes_read, 100 / portTICK_PERIOD_MS); |
| 71 | |
| 72 | // Thread might've been interrupted in the meanwhile |
| 73 | if (isThreadInterrupted()) { |
| 74 | break; |
| 75 | } |
no test coverage detected