/* * cmd_gps.c * * Copyright (c) 2025 Umber Networks & Robert McMahon * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * 3. Neither the name of the copyright holder nor the names of its * contributors may be used to endorse or promote products derived from * this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. */ #include #include #include #include #include #include "esp_log.h" #include "esp_console.h" #include "argtable3/argtable3.h" #include "gps_sync.h" #include "app_console.h" // --- Forward Declarations --- static int gps_do_status(int argc, char **argv); // ============================================================================ // COMMAND: gps (Dispatcher) // ============================================================================ static void print_gps_usage(void) { printf("Usage: gps [args]\n"); printf("Subcommands:\n"); printf(" status Show GPS lock status, time, and last NMEA message\n"); printf("\nType 'gps --help' for details.\n"); } static int cmd_gps(int argc, char **argv) { if (argc < 2 || strcmp(argv[1], "help") == 0 || strcmp(argv[1], "--help") == 0 || strcmp(argv[1], "-h") == 0) { print_gps_usage(); return 0; } if (strcmp(argv[1], "status") == 0) return gps_do_status(argc - 1, &argv[1]); if (strcmp(argv[1], "info") == 0) return gps_do_status(argc - 1, &argv[1]); // Alias printf("Unknown subcommand '%s'.\n", argv[1]); print_gps_usage(); return 1; } // ---------------------------------------------------------------------------- // Sub-command: status // ---------------------------------------------------------------------------- static struct { struct arg_lit *help; struct arg_end *end; } status_args; static int gps_do_status(int argc, char **argv) { status_args.help = arg_lit0("h", "help", "Help"); status_args.end = arg_end(1); int nerrors = arg_parse(argc, argv, (void **)&status_args); if (nerrors > 0) { arg_print_errors(stderr, status_args.end, argv[0]); return 1; } if (status_args.help->count > 0) { printf("Usage: gps status\n"); return 0; } // 1. Get GPS Data gps_timestamp_t ts = gps_get_timestamp(); int64_t pps_age = gps_get_pps_age_ms(); char nmea_buf[128] = ""; gps_get_last_nmea(nmea_buf, sizeof(nmea_buf)); // Strip trailing newline size_t len = strlen(nmea_buf); if (len > 0 && (nmea_buf[len-1] == '\n' || nmea_buf[len-1] == '\r')) nmea_buf[len-1] = 0; // 2. Format Time char time_str[64] = "Unknown"; struct timespec ts_now = {0, 0}; if (ts.gps_us > 0) { // Get raw timespec for display clock_gettime(CLOCK_REALTIME, &ts_now); time_t now_sec = ts_now.tv_sec; struct tm tm_info; gmtime_r(&now_sec, &tm_info); strftime(time_str, sizeof(time_str), "%Y-%m-%d %H:%M:%S UTC", &tm_info); } // 3. Print printf("GPS Status:\n"); printf(" Time: %s\n", time_str); // UPDATED: Show raw timespec structure printf(" Timespec: tv_sec=%" PRId64 " tv_nsec=%ld\n", (int64_t)ts_now.tv_sec, ts_now.tv_nsec); printf(" PPS Locked: %s (Age: %" PRId64 " ms)\n", ts.synced ? "YES" : "NO", pps_age); printf(" NMEA Valid: %s\n", ts.valid ? "YES" : "NO"); printf(" Last Message: %s\n", nmea_buf); return 0; } // ---------------------------------------------------------------------------- // Registration // ---------------------------------------------------------------------------- void register_gps_cmd(void) { const esp_console_cmd_t cmd = { .command = "gps", .help = "GPS Tool: status", .hint = "", .func = &cmd_gps, .argtable = NULL }; ESP_ERROR_CHECK(esp_console_cmd_register(&cmd)); }