mbed based IoT Gateway More details http://blog.thiseldo.co.uk/wp-filez/IoTGateway.pdf

Dependencies:   NetServices FatFileSystem csv_parser mbed MQTTClient RF12B DNSResolver SDFileSystem

Committer:
SomeRandomBloke
Date:
Mon Apr 02 22:05:20 2012 +0000
Revision:
0:a29a0225f203
Initial version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
SomeRandomBloke 0:a29a0225f203 1 /* mbed Microcontroller Library - FATFileSystem
SomeRandomBloke 0:a29a0225f203 2 * Copyright (c) 2008, sford
SomeRandomBloke 0:a29a0225f203 3 */
SomeRandomBloke 0:a29a0225f203 4
SomeRandomBloke 0:a29a0225f203 5 #include "FATFileSystem.h"
SomeRandomBloke 0:a29a0225f203 6
SomeRandomBloke 0:a29a0225f203 7 #include "mbed.h"
SomeRandomBloke 0:a29a0225f203 8
SomeRandomBloke 0:a29a0225f203 9 #include "FileSystemLike.h"
SomeRandomBloke 0:a29a0225f203 10 #include "FATFileHandle.h"
SomeRandomBloke 0:a29a0225f203 11 #include "FATDirHandle.h"
SomeRandomBloke 0:a29a0225f203 12 #include "ff.h"
SomeRandomBloke 0:a29a0225f203 13 //#include "Debug.h"
SomeRandomBloke 0:a29a0225f203 14 #include <stdio.h>
SomeRandomBloke 0:a29a0225f203 15 #include <stdlib.h>
SomeRandomBloke 0:a29a0225f203 16 #include <time.h>
SomeRandomBloke 0:a29a0225f203 17
SomeRandomBloke 0:a29a0225f203 18 /*
SomeRandomBloke 0:a29a0225f203 19 Currnet time is returned with packed into a DWORD value. The bit field is as follows:
SomeRandomBloke 0:a29a0225f203 20 bit31:25
SomeRandomBloke 0:a29a0225f203 21 Year from 1980 (0..127)
SomeRandomBloke 0:a29a0225f203 22 bit24:21
SomeRandomBloke 0:a29a0225f203 23 Month (1..12)
SomeRandomBloke 0:a29a0225f203 24 bit20:16
SomeRandomBloke 0:a29a0225f203 25 Day in month(1..31)
SomeRandomBloke 0:a29a0225f203 26 bit15:11
SomeRandomBloke 0:a29a0225f203 27 Hour (0..23)
SomeRandomBloke 0:a29a0225f203 28 bit10:5
SomeRandomBloke 0:a29a0225f203 29 Minute (0..59)
SomeRandomBloke 0:a29a0225f203 30 bit4:0
SomeRandomBloke 0:a29a0225f203 31 Second / 2 (0..29)
SomeRandomBloke 0:a29a0225f203 32
SomeRandomBloke 0:a29a0225f203 33
SomeRandomBloke 0:a29a0225f203 34 int tm_sec;
SomeRandomBloke 0:a29a0225f203 35 int tm_min;
SomeRandomBloke 0:a29a0225f203 36 int tm_hour;
SomeRandomBloke 0:a29a0225f203 37 int tm_mday;
SomeRandomBloke 0:a29a0225f203 38 int tm_mon;
SomeRandomBloke 0:a29a0225f203 39 int tm_year;
SomeRandomBloke 0:a29a0225f203 40 int tm_wday;
SomeRandomBloke 0:a29a0225f203 41 int tm_yday;
SomeRandomBloke 0:a29a0225f203 42 int tm_isdst;
SomeRandomBloke 0:a29a0225f203 43
SomeRandomBloke 0:a29a0225f203 44 */
SomeRandomBloke 0:a29a0225f203 45
SomeRandomBloke 0:a29a0225f203 46 DWORD get_fattime (void) {
SomeRandomBloke 0:a29a0225f203 47 time_t rawtime;
SomeRandomBloke 0:a29a0225f203 48 struct tm *ptm;
SomeRandomBloke 0:a29a0225f203 49 time ( &rawtime );
SomeRandomBloke 0:a29a0225f203 50 ptm = localtime ( &rawtime );
SomeRandomBloke 0:a29a0225f203 51 FFSDEBUG("DTM: %d/%d/%d %d:%d:%d\n",ptm->tm_year,ptm->tm_mon,ptm->tm_mday,ptm->tm_hour,ptm->tm_min,ptm->tm_sec);
SomeRandomBloke 0:a29a0225f203 52 DWORD fattime = (DWORD)(ptm->tm_year - 80) << 25
SomeRandomBloke 0:a29a0225f203 53 | (DWORD)(ptm->tm_mon + 1) << 21
SomeRandomBloke 0:a29a0225f203 54 | (DWORD)(ptm->tm_mday) << 16
SomeRandomBloke 0:a29a0225f203 55 | (DWORD)(ptm->tm_hour) << 11
SomeRandomBloke 0:a29a0225f203 56 | (DWORD)(ptm->tm_min) << 5
SomeRandomBloke 0:a29a0225f203 57 | (DWORD)(ptm->tm_sec/2);
SomeRandomBloke 0:a29a0225f203 58
SomeRandomBloke 0:a29a0225f203 59 FFSDEBUG("Converted: %x\n",fattime);
SomeRandomBloke 0:a29a0225f203 60 return fattime;
SomeRandomBloke 0:a29a0225f203 61 }
SomeRandomBloke 0:a29a0225f203 62
SomeRandomBloke 0:a29a0225f203 63 namespace mbed {
SomeRandomBloke 0:a29a0225f203 64
SomeRandomBloke 0:a29a0225f203 65 #if FFSDEBUG_ENABLED
SomeRandomBloke 0:a29a0225f203 66 static const char *FR_ERRORS[] = {
SomeRandomBloke 0:a29a0225f203 67 "FR_OK = 0",
SomeRandomBloke 0:a29a0225f203 68 "FR_NOT_READY",
SomeRandomBloke 0:a29a0225f203 69 "FR_NO_FILE",
SomeRandomBloke 0:a29a0225f203 70 "FR_NO_PATH",
SomeRandomBloke 0:a29a0225f203 71 "FR_INVALID_NAME",
SomeRandomBloke 0:a29a0225f203 72 "FR_INVALID_DRIVE",
SomeRandomBloke 0:a29a0225f203 73 "FR_DENIED",
SomeRandomBloke 0:a29a0225f203 74 "FR_EXIST",
SomeRandomBloke 0:a29a0225f203 75 "FR_RW_ERROR",
SomeRandomBloke 0:a29a0225f203 76 "FR_WRITE_PROTECTED",
SomeRandomBloke 0:a29a0225f203 77 "FR_NOT_ENABLED",
SomeRandomBloke 0:a29a0225f203 78 "FR_NO_FILESYSTEM",
SomeRandomBloke 0:a29a0225f203 79 "FR_INVALID_OBJECT",
SomeRandomBloke 0:a29a0225f203 80 "FR_MKFS_ABORTED"
SomeRandomBloke 0:a29a0225f203 81 };
SomeRandomBloke 0:a29a0225f203 82 #endif
SomeRandomBloke 0:a29a0225f203 83
SomeRandomBloke 0:a29a0225f203 84 FATFileSystem *FATFileSystem::_ffs[_VOLUMES] = {0};
SomeRandomBloke 0:a29a0225f203 85
SomeRandomBloke 0:a29a0225f203 86 FATFileSystem::FATFileSystem(const char* n) : FileSystemLike(n) {
SomeRandomBloke 0:a29a0225f203 87 FFSDEBUG("FATFileSystem(%s)\n", n);
SomeRandomBloke 0:a29a0225f203 88 for(int i=0; i<_VOLUMES; i++) {
SomeRandomBloke 0:a29a0225f203 89 if(_ffs[i] == 0) {
SomeRandomBloke 0:a29a0225f203 90 _ffs[i] = this;
SomeRandomBloke 0:a29a0225f203 91 _fsid = i;
SomeRandomBloke 0:a29a0225f203 92 FFSDEBUG("Mounting [%s] on ffs drive [%d]\n", _name, _fsid);
SomeRandomBloke 0:a29a0225f203 93 f_mount(i, &_fs);
SomeRandomBloke 0:a29a0225f203 94 return;
SomeRandomBloke 0:a29a0225f203 95 }
SomeRandomBloke 0:a29a0225f203 96 }
SomeRandomBloke 0:a29a0225f203 97 error("Couldn't create %s in FATFileSystem::FATFileSystem\n",n);
SomeRandomBloke 0:a29a0225f203 98 }
SomeRandomBloke 0:a29a0225f203 99
SomeRandomBloke 0:a29a0225f203 100 FATFileSystem::~FATFileSystem() {
SomeRandomBloke 0:a29a0225f203 101 for(int i=0; i<_VOLUMES; i++) {
SomeRandomBloke 0:a29a0225f203 102 if(_ffs[i] == this) {
SomeRandomBloke 0:a29a0225f203 103 _ffs[i] = 0;
SomeRandomBloke 0:a29a0225f203 104 f_mount(i, NULL);
SomeRandomBloke 0:a29a0225f203 105 }
SomeRandomBloke 0:a29a0225f203 106 }
SomeRandomBloke 0:a29a0225f203 107 }
SomeRandomBloke 0:a29a0225f203 108
SomeRandomBloke 0:a29a0225f203 109 FileHandle *FATFileSystem::open(const char* name, int flags) {
SomeRandomBloke 0:a29a0225f203 110 FFSDEBUG("open(%s) on filesystem [%s], drv [%d]\n", name, _name, _fsid);
SomeRandomBloke 0:a29a0225f203 111 char n[64];
SomeRandomBloke 0:a29a0225f203 112 sprintf(n, "%d:/%s", _fsid, name);
SomeRandomBloke 0:a29a0225f203 113
SomeRandomBloke 0:a29a0225f203 114 /* POSIX flags -> FatFS open mode */
SomeRandomBloke 0:a29a0225f203 115 BYTE openmode;
SomeRandomBloke 0:a29a0225f203 116 if(flags & O_RDWR) {
SomeRandomBloke 0:a29a0225f203 117 openmode = FA_READ|FA_WRITE;
SomeRandomBloke 0:a29a0225f203 118 } else if(flags & O_WRONLY) {
SomeRandomBloke 0:a29a0225f203 119 openmode = FA_WRITE;
SomeRandomBloke 0:a29a0225f203 120 } else {
SomeRandomBloke 0:a29a0225f203 121 openmode = FA_READ;
SomeRandomBloke 0:a29a0225f203 122 }
SomeRandomBloke 0:a29a0225f203 123 if(flags & O_CREAT) {
SomeRandomBloke 0:a29a0225f203 124 if(flags & O_TRUNC) {
SomeRandomBloke 0:a29a0225f203 125 openmode |= FA_CREATE_ALWAYS;
SomeRandomBloke 0:a29a0225f203 126 } else {
SomeRandomBloke 0:a29a0225f203 127 openmode |= FA_OPEN_ALWAYS;
SomeRandomBloke 0:a29a0225f203 128 }
SomeRandomBloke 0:a29a0225f203 129 }
SomeRandomBloke 0:a29a0225f203 130
SomeRandomBloke 0:a29a0225f203 131 FIL fh;
SomeRandomBloke 0:a29a0225f203 132 FRESULT res = f_open(&fh, n, openmode);
SomeRandomBloke 0:a29a0225f203 133 if(res) {
SomeRandomBloke 0:a29a0225f203 134 FFSDEBUG("f_open('w') failed (%d, %s)\n", res, FR_ERRORS[res]);
SomeRandomBloke 0:a29a0225f203 135 return NULL;
SomeRandomBloke 0:a29a0225f203 136 }
SomeRandomBloke 0:a29a0225f203 137 if(flags & O_APPEND) {
SomeRandomBloke 0:a29a0225f203 138 f_lseek(&fh, fh.fsize);
SomeRandomBloke 0:a29a0225f203 139 }
SomeRandomBloke 0:a29a0225f203 140 return new FATFileHandle(fh);
SomeRandomBloke 0:a29a0225f203 141 }
SomeRandomBloke 0:a29a0225f203 142
SomeRandomBloke 0:a29a0225f203 143 int FATFileSystem::remove(const char *filename) {
SomeRandomBloke 0:a29a0225f203 144 FRESULT res = f_unlink(filename);
SomeRandomBloke 0:a29a0225f203 145 if(res) {
SomeRandomBloke 0:a29a0225f203 146 FFSDEBUG("f_unlink() failed (%d, %s)\n", res, FR_ERRORS[res]);
SomeRandomBloke 0:a29a0225f203 147 return -1;
SomeRandomBloke 0:a29a0225f203 148 }
SomeRandomBloke 0:a29a0225f203 149 return 0;
SomeRandomBloke 0:a29a0225f203 150 }
SomeRandomBloke 0:a29a0225f203 151
SomeRandomBloke 0:a29a0225f203 152 int FATFileSystem::format() {
SomeRandomBloke 0:a29a0225f203 153 FFSDEBUG("format()\n");
SomeRandomBloke 0:a29a0225f203 154 FRESULT res = f_mkfs(_fsid, 0, 512); // Logical drive number, Partitioning rule, Allocation unit size (bytes per cluster)
SomeRandomBloke 0:a29a0225f203 155 if(res) {
SomeRandomBloke 0:a29a0225f203 156 FFSDEBUG("f_mkfs() failed (%d, %s)\n", res, FR_ERRORS[res]);
SomeRandomBloke 0:a29a0225f203 157 return -1;
SomeRandomBloke 0:a29a0225f203 158 }
SomeRandomBloke 0:a29a0225f203 159 return 0;
SomeRandomBloke 0:a29a0225f203 160 }
SomeRandomBloke 0:a29a0225f203 161
SomeRandomBloke 0:a29a0225f203 162 DirHandle *FATFileSystem::opendir(const char *name) {
SomeRandomBloke 0:a29a0225f203 163 FATFS_DIR dir;
SomeRandomBloke 0:a29a0225f203 164 FRESULT res = f_opendir(&dir, name);
SomeRandomBloke 0:a29a0225f203 165 if(res != 0) {
SomeRandomBloke 0:a29a0225f203 166 return NULL;
SomeRandomBloke 0:a29a0225f203 167 }
SomeRandomBloke 0:a29a0225f203 168 return new FATDirHandle(dir);
SomeRandomBloke 0:a29a0225f203 169 }
SomeRandomBloke 0:a29a0225f203 170
SomeRandomBloke 0:a29a0225f203 171 int FATFileSystem::mkdir(const char *name, mode_t mode) {
SomeRandomBloke 0:a29a0225f203 172 FRESULT res = f_mkdir(name);
SomeRandomBloke 0:a29a0225f203 173 return res == 0 ? 0 : -1;
SomeRandomBloke 0:a29a0225f203 174 }
SomeRandomBloke 0:a29a0225f203 175
SomeRandomBloke 0:a29a0225f203 176 } // namespace mbed