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