]> git.droids-corp.org - protos/imu.git/commitdiff
add missing sd_log.[ch] files
authorOlivier Matz <zer0@droids-corp.org>
Thu, 17 Jul 2014 17:55:38 +0000 (19:55 +0200)
committerOlivier Matz <zer0@droids-corp.org>
Thu, 17 Jul 2014 17:55:38 +0000 (19:55 +0200)
sd_log.c [new file with mode: 0644]
sd_log.h [new file with mode: 0644]

diff --git a/sd_log.c b/sd_log.c
new file mode 100644 (file)
index 0000000..4659936
--- /dev/null
+++ b/sd_log.c
@@ -0,0 +1,139 @@
+#include <string.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <aversive/pgmspace.h>
+
+#include "fat.h"
+#include "fat_config.h"
+#include "partition.h"
+#include "sd_raw.h"
+#include "sd_raw_config.h"
+#include "sd_log.h"
+
+static struct fat_file_struct *log_fd = NULL;
+
+uint8_t find_file_in_dir(struct fat_fs_struct* fs,
+       struct fat_dir_struct* dd, const char* name,
+       struct fat_dir_entry_struct* dir_entry)
+{
+       (void)fs;
+
+       while(fat_read_dir(dd, dir_entry)) {
+               if(strcmp(dir_entry->long_name, name) == 0) {
+                       fat_reset_dir(dd);
+                       return 1;
+               }
+       }
+
+       return 0;
+}
+
+struct fat_file_struct *open_file_in_dir(struct fat_fs_struct* fs,
+       struct fat_dir_struct* dd, const char* name)
+{
+       struct fat_dir_entry_struct file_entry;
+
+       if(!find_file_in_dir(fs, dd, name, &file_entry))
+               return 0;
+
+       return fat_open_file(fs, &file_entry);
+}
+
+struct fat_file_struct *open_log_file(void)
+{
+       struct fat_file_struct *fd;
+       struct fat_fs_struct *fs;
+       struct partition_struct *partition ;
+       struct fat_dir_struct *dd;
+       struct fat_dir_entry_struct directory;
+       struct fat_dir_entry_struct file_entry;
+       int16_t i = 0;
+       char name[16];
+
+       /* setup sd card slot */
+       if (!sd_raw_init()) {
+#if SD_DEBUG
+               printf_P(PSTR("MMC/SD initialization failed\n"));
+#endif
+               return NULL;
+       }
+
+       /* open first partition */
+       partition = partition_open(sd_raw_read,
+               sd_raw_read_interval,
+#if SD_RAW_WRITE_SUPPORT
+               sd_raw_write, sd_raw_write_interval,
+#else
+               0, 0,
+#endif
+               0);
+
+       if (!partition) {
+               /* If the partition did not open, assume the storage device
+                * is a "superfloppy", i.e. has no MBR.
+                */
+               partition = partition_open(sd_raw_read,
+                       sd_raw_read_interval,
+#if SD_RAW_WRITE_SUPPORT
+                       sd_raw_write,
+                       sd_raw_write_interval,
+#else
+                       0,
+                       0,
+#endif
+                       -1);
+               if (!partition) {
+#if SD_DEBUG
+                       printf_P(PSTR("opening partition failed\n"));
+#endif
+                       return NULL;
+               }
+       }
+
+       /* open file system */
+       fs = fat_open(partition);
+       if (!fs) {
+#if SD_DEBUG
+               printf_P(PSTR("opening filesystem failed\n"));
+#endif
+               return NULL;
+       }
+
+       /* open root directory */
+       fat_get_dir_entry_of_path(fs, "/", &directory);
+       dd = fat_open_dir(fs, &directory);
+       if (!dd) {
+#if SD_DEBUG
+               printf_P(PSTR("opening root directory failed\n"));
+#endif
+               return NULL;
+       }
+
+       /* print some card information as a boot message */
+       //print_disk_info(fs);
+
+       printf("choose log file name\n");
+       while (1) {
+               snprintf(name, sizeof(name), "log%.4d", i++);
+               if (!find_file_in_dir(fs, dd, name, &file_entry))
+                       break;
+       }
+
+       printf("create log file %s\n", name);
+       if (!fat_create_file(dd, name, &file_entry)) {
+               printf_P(PSTR("error creating file: "));
+       }
+
+       fd = open_file_in_dir(fs, dd, name);
+       if (!fd) {
+               printf_P(PSTR("error opening "));
+               return NULL;
+       }
+
+       return fd;
+}
+
+struct fat_file_struct *get_log_file(void)
+{
+       return log_fd;
+}
diff --git a/sd_log.h b/sd_log.h
new file mode 100644 (file)
index 0000000..d2333e2
--- /dev/null
+++ b/sd_log.h
@@ -0,0 +1,7 @@
+#ifndef SD_LOG_H_
+#define SD_LOG_H_
+
+struct fat_file_struct *open_log_file(void);
+struct fat_file_struct *get_log_file(void);
+
+#endif