创建mbed

Dependencies:   EthernetInterface SDFileSystem mbed-rtos mbed

Committer:
sunyiming
Date:
Tue Mar 06 08:53:46 2018 +0000
Revision:
1:6465a3f5c58a
Parent:
0:610235504baa
??OK

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sunyiming 0:610235504baa 1 #ifndef filelib
sunyiming 0:610235504baa 2 #define filelib
sunyiming 0:610235504baa 3
sunyiming 0:610235504baa 4 #include "mbed.h"
sunyiming 0:610235504baa 5 #include <time.h>
sunyiming 0:610235504baa 6 #include <ff.h> //found in lib SDFileSystem->FATFileSystem/ChaN
sunyiming 0:610235504baa 7 #include "SDFileSystem.h"
sunyiming 0:610235504baa 8
sunyiming 0:610235504baa 9
sunyiming 0:610235504baa 10 /*
sunyiming 0:610235504baa 11 typedef enum {
sunyiming 0:610235504baa 12 FR_OK = 0, // (0) Succeeded
sunyiming 0:610235504baa 13 FR_DISK_ERR, // (1) A hard error occurred in the low level disk I/O layer
sunyiming 0:610235504baa 14 FR_INT_ERR, // (2) Assertion failed
sunyiming 0:610235504baa 15 FR_NOT_READY, // (3) The physical drive cannot work
sunyiming 0:610235504baa 16 FR_NO_FILE, // (4) Could not find the file
sunyiming 0:610235504baa 17 FR_NO_PATH, // (5) Could not find the path
sunyiming 0:610235504baa 18 FR_INVALID_NAME, // (6) The path name format is invalid
sunyiming 0:610235504baa 19 FR_DENIED, // (7) Access denied due to prohibited access or directory full
sunyiming 0:610235504baa 20 FR_EXIST, // (8) Access denied due to prohibited access
sunyiming 0:610235504baa 21 FR_INVALID_OBJECT, // (9) The file/directory object is invalid
sunyiming 0:610235504baa 22 FR_WRITE_PROTECTED, // (10) The physical drive is write protected
sunyiming 0:610235504baa 23 FR_INVALID_DRIVE, // (11) The logical drive number is invalid
sunyiming 0:610235504baa 24 FR_NOT_ENABLED, // (12) The volume has no work area
sunyiming 0:610235504baa 25 FR_NO_FILESYSTEM, // (13) There is no valid FAT volume
sunyiming 0:610235504baa 26 FR_MKFS_ABORTED, // (14) The f_mkfs() aborted due to any parameter error
sunyiming 0:610235504baa 27 FR_TIMEOUT, // (15) Could not get a grant to access the volume within defined period
sunyiming 0:610235504baa 28 FR_LOCKED, // (16) The operation is rejected according to the file sharing policy
sunyiming 0:610235504baa 29 FR_NOT_ENOUGH_CORE, // (17) LFN working buffer could not be allocated
sunyiming 0:610235504baa 30 FR_TOO_MANY_OPEN_FILES, // (18) Number of open files > _FS_SHARE
sunyiming 0:610235504baa 31 FR_INVALID_PARAMETER // (19) Given parameter is invalid
sunyiming 0:610235504baa 32 } FRESULT;
sunyiming 0:610235504baa 33
sunyiming 0:610235504baa 34 */
sunyiming 0:610235504baa 35 /*
sunyiming 0:610235504baa 36 mode_t values in octal
sunyiming 0:610235504baa 37 S_IFREG 0100000
sunyiming 0:610235504baa 38 S_IFDIR 040000
sunyiming 0:610235504baa 39 S_IRUSR 0400 read permission, owner
sunyiming 0:610235504baa 40 S_IWUSR 0200 write permission, owner
sunyiming 0:610235504baa 41 S_IXUSR 0100 execute/search permission, owner
sunyiming 0:610235504baa 42 S_IRGRP 040 read permission, group
sunyiming 0:610235504baa 43 S_IWGRP 020 write permission, group
sunyiming 0:610235504baa 44 S_IXGRP 010 execute/search permission, group
sunyiming 0:610235504baa 45 S_IROTH 04 read permission, others
sunyiming 0:610235504baa 46 S_IWOTH 02 write permission, others
sunyiming 0:610235504baa 47 S_IXOTH 01 execute/search permission, others
sunyiming 0:610235504baa 48 */
sunyiming 0:610235504baa 49 struct sMystat {
sunyiming 0:610235504baa 50 // dev_t st_dev; /* ID of device containing file */
sunyiming 0:610235504baa 51 // ino_t st_ino; /* inode number */
sunyiming 0:610235504baa 52 mode_t st_mode; /* protection */
sunyiming 0:610235504baa 53 // nlink_t st_nlink; /* number of hard links */
sunyiming 0:610235504baa 54 // uid_t st_uid; /* user ID of owner */
sunyiming 0:610235504baa 55 // gid_t st_gid; /* group ID of owner */
sunyiming 0:610235504baa 56 // dev_t st_rdev; /* device ID (if special file) */
sunyiming 0:610235504baa 57 // off_t st_size; /* total size, in bytes */
sunyiming 0:610235504baa 58 DWORD st_size; /* total size, in bytes */
sunyiming 0:610235504baa 59 // blksize_t st_blksize; /* blocksize for file system I/O */
sunyiming 0:610235504baa 60 // blkcnt_t st_blocks; /* number of 512B blocks allocated */
sunyiming 0:610235504baa 61 // time_t st_atime; /* time of last access */
sunyiming 0:610235504baa 62 time_t st_mtime; /* time of last modification */
sunyiming 0:610235504baa 63 // time_t st_ctime; /* time of last status change */
sunyiming 0:610235504baa 64 };
sunyiming 0:610235504baa 65
sunyiming 0:610235504baa 66 sMystat myStatBuf; //store info for file
sunyiming 0:610235504baa 67 FILINFO finfo; //global file info struct see ff.h
sunyiming 0:610235504baa 68 FATFS_DIR dinfo; //global directoty info struct see ff.h
sunyiming 0:610235504baa 69
sunyiming 0:610235504baa 70 FRESULT get_fileInfo(const char *path) //use finfo for get result fields
sunyiming 0:610235504baa 71 {
sunyiming 0:610235504baa 72 FRESULT res = f_stat(path,&finfo); /* Get file status */
sunyiming 0:610235504baa 73 if (EnDebugMSG)
sunyiming 0:610235504baa 74 if (res)
sunyiming 0:610235504baa 75 printf("\n-->get_fileInfo:%s ,res=%d ,Not Found!",path,res);
sunyiming 0:610235504baa 76 else
sunyiming 0:610235504baa 77 printf("\n-->get_fileInfo:%s ,res=%d ,Found!",path,res);
sunyiming 0:610235504baa 78 return res;
sunyiming 0:610235504baa 79 }
sunyiming 0:610235504baa 80
sunyiming 0:610235504baa 81 FRESULT get_dirInfo(const char *path)
sunyiming 0:610235504baa 82 {
sunyiming 0:610235504baa 83 FRESULT res= f_opendir (&dinfo,path); /* FR_OK(0): successful, !=0: error code */
sunyiming 0:610235504baa 84 if (EnDebugMSG)
sunyiming 0:610235504baa 85 if (res)
sunyiming 0:610235504baa 86 printf("\n-->get_dirInfo :%s res=%d ,This is Not Directory!",path,res);
sunyiming 0:610235504baa 87 else
sunyiming 0:610235504baa 88 printf("\n-->get_dirInfo :%s res=%d ,This is Directory!",path,res);
sunyiming 0:610235504baa 89 return res;
sunyiming 0:610235504baa 90 }
sunyiming 0:610235504baa 91
sunyiming 0:610235504baa 92 FRESULT Mystat(const char *path, struct sMystat *buf)
sunyiming 0:610235504baa 93 {
sunyiming 0:610235504baa 94 FRESULT res = f_stat(path,&finfo); /* Get file status */
sunyiming 0:610235504baa 95 if (res == FR_OK) {
sunyiming 0:610235504baa 96 buf->st_size = finfo.fsize;
sunyiming 0:610235504baa 97 buf->st_mtime = finfo.ftime; //fdate;
sunyiming 0:610235504baa 98 buf->st_mode = 4; //?
sunyiming 0:610235504baa 99 }
sunyiming 0:610235504baa 100 if (EnDebugMSG)
sunyiming 0:610235504baa 101 printf("\n--Mystat Path:%s ,filesize:%14lld ,res=%d",path, buf->st_size, res);
sunyiming 0:610235504baa 102 return res;
sunyiming 0:610235504baa 103 }
sunyiming 0:610235504baa 104
sunyiming 0:610235504baa 105
sunyiming 0:610235504baa 106 static char* get_mime_type( char* filename )
sunyiming 0:610235504baa 107 {
sunyiming 0:610235504baa 108 char* extension;
sunyiming 0:610235504baa 109 extension = strrchr( filename, '.' ); //get string after last .
sunyiming 0:610235504baa 110 if (EnDebugMSG)
sunyiming 0:610235504baa 111 printf("\n-->get_mime_tipe filename:%s, extension:%s",filename, extension);
sunyiming 0:610235504baa 112 if (extension !=NULL) {
sunyiming 0:610235504baa 113 if (strcasecmp(extension,".htm")==0 || strcasecmp(extension,".html") ==0)
sunyiming 0:610235504baa 114 return "text/html; charset=iso-8859-1";
sunyiming 0:610235504baa 115 if (strcasecmp(extension,".png")==0)
sunyiming 0:610235504baa 116 return "image/png";
sunyiming 0:610235504baa 117 if (strcasecmp(extension,".css")==0)
sunyiming 0:610235504baa 118 return "text/css";
sunyiming 0:610235504baa 119 if (strcasecmp(extension,".gif")==0)
sunyiming 0:610235504baa 120 return "image/gif";
sunyiming 0:610235504baa 121 if (strcasecmp(extension,".jpg")==0 || strcasecmp(extension,".jpeg" )==0)
sunyiming 0:610235504baa 122 return "image/jpeg";
sunyiming 0:610235504baa 123 }
sunyiming 0:610235504baa 124 return "text/plain; charset=iso-8859-1";
sunyiming 0:610235504baa 125 }
sunyiming 0:610235504baa 126
sunyiming 0:610235504baa 127 #endif