From 2fcb5852aea267f7b6f532738ffb8edba71d951b Mon Sep 17 00:00:00 2001 From: ArenM Date: Wed, 29 Sep 2021 14:28:59 -0400 Subject: [PATCH] gnss: better error handling and messages when fetching data This will print the error message from curl instead of just the http status code if downloading gpsOneXtra data fails. It also adds checks for other errors that are likely to occur. --- src/gnss.c | 48 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 32 insertions(+), 16 deletions(-) diff --git a/src/gnss.c b/src/gnss.c index 16b79a1..898c0da 100644 --- a/src/gnss.c +++ b/src/gnss.c @@ -187,22 +187,32 @@ static void state_at_gnss(struct EG25Manager *manager) static void fetch_assistance_data(struct EG25Manager *manager) { - CURL *curl; CURLcode response; - gchar *url = NULL; + curl_off_t downloaded; + CURL *curl = NULL; + g_autofree gchar *url = NULL; FILE *tmp_file = NULL; gchar errbuf[CURL_ERROR_SIZE]; errbuf[0] = 0; /* Fetch assistance data with curl */ tmp_file = fdopen(manager->gnss_assistance_fd, "wb+"); + if (tmp_file == NULL) { + g_critical("Unable to open file to save assistance data: %s", + g_strerror(errno)); + goto bail; + } + lseek(manager->gnss_assistance_fd, 0, SEEK_SET); ftruncate(manager->gnss_assistance_fd, 0); url = g_strconcat(manager->gnss_assistance_url, "/", manager->gnss_assistance_file, NULL); + curl = curl_easy_init(); - if (!curl) - g_error ("Unable to initialize curl"); + if (!curl) { + g_critical("Unable to initialize curl"); + goto bail; + } curl_easy_setopt(curl, CURLOPT_URL, url); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, NULL); @@ -212,27 +222,33 @@ static void fetch_assistance_data(struct EG25Manager *manager) curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1L); response = curl_easy_perform(curl); - curl_easy_cleanup(curl); if (response != CURLE_OK) { - g_warning ("Unable to fetch GNSS assistance data from %s: %s", - url, strlen(errbuf) ? errbuf : curl_easy_strerror(response)); + g_warning("Unable to fetch GNSS assistance data from %s: %s", + url, strlen(errbuf) ? errbuf : curl_easy_strerror(response)); + goto bail; + } - /* Restart upload on HTTP error status code */ - g_message ("Rescheduling upload because of failure in %ds", - RESCHEDULE_IN_SECS); - manager->gnss_assistance_step = EG25_GNSS_STEP_LAST; - g_timeout_add_seconds(RESCHEDULE_IN_SECS, - G_SOURCE_FUNC(gnss_upload_assistance_data), - manager); - return; + response = curl_easy_getinfo(curl, CURLINFO_SIZE_DOWNLOAD_T, &downloaded); + if (response) { + g_critical("Unable to get number of downloaded bytes from curl"); + goto bail; + } else if (downloaded <= 0) { + g_warning("Downloaded empty assistance data file"); + goto bail; } g_message("Fetching GNSS assistance data from %s was successfull", url); - g_free(url); + curl_easy_cleanup(curl); /* Go to the next step */ manager->gnss_assistance_step++; gnss_step(manager); + return; + +bail: + if (curl != NULL) + curl_easy_cleanup(curl); + manager->gnss_assistance_step = EG25_GNSS_STEP_LAST; } /******************************************************************************/