Fork of my MQTTGateway

Dependencies:   mbed-http

Committer:
vpcola
Date:
Sat Apr 08 14:45:51 2017 +0000
Revision:
0:f1d3878b8dd9
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vpcola 0:f1d3878b8dd9 1 #include "mbed.h"
vpcola 0:f1d3878b8dd9 2 #include "FATFileSystem.h"
vpcola 0:f1d3878b8dd9 3 #include "SDBlockDevice.h"
vpcola 0:f1d3878b8dd9 4 #include <stdio.h>
vpcola 0:f1d3878b8dd9 5 #include <errno.h>
vpcola 0:f1d3878b8dd9 6 /* mbed_retarget.h is included after errno.h so symbols are mapped to
vpcola 0:f1d3878b8dd9 7 * consistent values for all toolchains */
vpcola 0:f1d3878b8dd9 8 #include "platform/mbed_retarget.h"
vpcola 0:f1d3878b8dd9 9
vpcola 0:f1d3878b8dd9 10
vpcola 0:f1d3878b8dd9 11 SDBlockDevice sd(MBED_CONF_APP_SPI_MOSI, MBED_CONF_APP_SPI_MISO, MBED_CONF_APP_SPI_CLK, MBED_CONF_APP_SPI_CS);
vpcola 0:f1d3878b8dd9 12 FATFileSystem fs("sd", &sd);
vpcola 0:f1d3878b8dd9 13
vpcola 0:f1d3878b8dd9 14 void return_error(int ret_val){
vpcola 0:f1d3878b8dd9 15 if (ret_val)
vpcola 0:f1d3878b8dd9 16 printf("Failure. %d\n", ret_val);
vpcola 0:f1d3878b8dd9 17 else
vpcola 0:f1d3878b8dd9 18 printf("done.\n");
vpcola 0:f1d3878b8dd9 19 }
vpcola 0:f1d3878b8dd9 20
vpcola 0:f1d3878b8dd9 21 void errno_error(void* ret_val){
vpcola 0:f1d3878b8dd9 22 if (ret_val == NULL)
vpcola 0:f1d3878b8dd9 23 printf(" Failure. %d \n", errno);
vpcola 0:f1d3878b8dd9 24 else
vpcola 0:f1d3878b8dd9 25 printf(" done.\n");
vpcola 0:f1d3878b8dd9 26 }
vpcola 0:f1d3878b8dd9 27
vpcola 0:f1d3878b8dd9 28 int main()
vpcola 0:f1d3878b8dd9 29 {
vpcola 0:f1d3878b8dd9 30 int error = 0;
vpcola 0:f1d3878b8dd9 31 printf("Welcome to the filesystem example.\n");
vpcola 0:f1d3878b8dd9 32
vpcola 0:f1d3878b8dd9 33 printf("Opening a new file, numbers.txt.");
vpcola 0:f1d3878b8dd9 34 FILE* fd = fopen("/sd/numbers.txt", "w+");
vpcola 0:f1d3878b8dd9 35 errno_error(fd);
vpcola 0:f1d3878b8dd9 36
vpcola 0:f1d3878b8dd9 37 for (int i = 0; i < 20; i++){
vpcola 0:f1d3878b8dd9 38 printf("Writing decimal numbers to a file (%d/20)\r", i);
vpcola 0:f1d3878b8dd9 39 fprintf(fd, "%d\n", i);
vpcola 0:f1d3878b8dd9 40 }
vpcola 0:f1d3878b8dd9 41 printf("Writing decimal numbers to a file (20/20) done.\n");
vpcola 0:f1d3878b8dd9 42
vpcola 0:f1d3878b8dd9 43 printf("Closing file.");
vpcola 0:f1d3878b8dd9 44 fclose(fd);
vpcola 0:f1d3878b8dd9 45 printf(" done.\n");
vpcola 0:f1d3878b8dd9 46
vpcola 0:f1d3878b8dd9 47 printf("Re-opening file read-only.");
vpcola 0:f1d3878b8dd9 48 fd = fopen("/sd/numbers.txt", "r");
vpcola 0:f1d3878b8dd9 49 errno_error(fd);
vpcola 0:f1d3878b8dd9 50
vpcola 0:f1d3878b8dd9 51 printf("Dumping file to screen.\n");
vpcola 0:f1d3878b8dd9 52 char buff[16] = {0};
vpcola 0:f1d3878b8dd9 53 while (!feof(fd)){
vpcola 0:f1d3878b8dd9 54 int size = fread(&buff[0], 1, 15, fd);
vpcola 0:f1d3878b8dd9 55 fwrite(&buff[0], 1, size, stdout);
vpcola 0:f1d3878b8dd9 56 }
vpcola 0:f1d3878b8dd9 57 printf("EOF.\n");
vpcola 0:f1d3878b8dd9 58
vpcola 0:f1d3878b8dd9 59 printf("Closing file.");
vpcola 0:f1d3878b8dd9 60 fclose(fd);
vpcola 0:f1d3878b8dd9 61 printf(" done.\n");
vpcola 0:f1d3878b8dd9 62
vpcola 0:f1d3878b8dd9 63 printf("Opening root directory.");
vpcola 0:f1d3878b8dd9 64 DIR* dir = opendir("/sd/");
vpcola 0:f1d3878b8dd9 65 errno_error(fd);
vpcola 0:f1d3878b8dd9 66
vpcola 0:f1d3878b8dd9 67 struct dirent* de;
vpcola 0:f1d3878b8dd9 68 printf("Printing all filenames:\n");
vpcola 0:f1d3878b8dd9 69 while((de = readdir(dir)) != NULL){
vpcola 0:f1d3878b8dd9 70 printf(" %s\n", &(de->d_name)[0]);
vpcola 0:f1d3878b8dd9 71 }
vpcola 0:f1d3878b8dd9 72
vpcola 0:f1d3878b8dd9 73 printf("Closeing root directory. ");
vpcola 0:f1d3878b8dd9 74 error = closedir(dir);
vpcola 0:f1d3878b8dd9 75 return_error(error);
vpcola 0:f1d3878b8dd9 76 printf("Filesystem Demo complete.\n");
vpcola 0:f1d3878b8dd9 77
vpcola 0:f1d3878b8dd9 78 while (true) {}
vpcola 0:f1d3878b8dd9 79 }
vpcola 0:f1d3878b8dd9 80