X-Git-Url: https://git.gag.com/?a=blobdiff_plain;f=src%2Fdrivers%2Fao_log_fat.c;h=62e97868e1f7e445cd4b63262ec9306343041a53;hb=69bdb309f46a28803e93b08921720805b28b18a2;hp=684148b7c131b0fbb4fb07680d2af5e83efcedfc;hpb=649999863c7228ead0225968752d068dc0d30091;p=fw%2Faltos diff --git a/src/drivers/ao_log_fat.c b/src/drivers/ao_log_fat.c index 684148b7..62e97868 100644 --- a/src/drivers/ao_log_fat.c +++ b/src/drivers/ao_log_fat.c @@ -3,7 +3,8 @@ * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; version 2 of the License. + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of @@ -20,24 +21,39 @@ #include "ao_fat.h" static uint8_t log_year, log_month, log_day; -static uint8_t log_running; +static uint8_t log_open; +static int8_t log_fd; static uint8_t log_mutex; static void ao_log_open(void) { - char name[12]; + static char name[12]; + int8_t status; sprintf(name,"%04d%02d%02dLOG", 2000 + log_year, log_month, log_day); - if (ao_fat_open(name, AO_FAT_OPEN_WRITE) == AO_FAT_SUCCESS) - log_running = 1; + status = ao_fat_open(name, AO_FAT_OPEN_WRITE); + if (status >= 0) { + log_fd = status; + ao_fat_seek(log_fd, 0, AO_FAT_SEEK_END); + log_open = 1; + } else if (status == -AO_FAT_ENOENT) { + status = ao_fat_creat(name); + if (status >= 0) { + log_fd = status; + log_open = 1; + } + } } static void ao_log_close(void) { - log_running = 0; - ao_fat_close(); + if (log_open) { + log_open = 0; + ao_fat_close(log_fd); + log_fd = -1; + } } uint8_t @@ -52,23 +68,29 @@ ao_log_mega(struct ao_log_mega *log) uint8_t wrote = 0; ao_mutex_get(&log_mutex); if (log->type == AO_LOG_GPS_TIME) { - if (log_running && + if (log_open && (log_year != log->u.gps.year || log_month != log->u.gps.month || log_day != log->u.gps.day)) { ao_log_close(); } - if (!log_running) { + if (!log_open) { log_year = log->u.gps.year; log_month = log->u.gps.month; log_day = log->u.gps.day; ao_log_open(); } } - if (log_running) { - wrote = ao_fat_write(log, sizeof (*log)) == AO_FAT_SUCCESS; + if (log_open) { + wrote = ao_fat_write(log_fd, log, sizeof (*log)) == AO_FAT_SUCCESS; ao_fat_sync(); } ao_mutex_put(&log_mutex); return wrote; } + +void +ao_log_flush(void) +{ + ao_fat_sync(); +}