mbed-os
Fork of mbed-os by
features/unsupported/tests/mbed/dir/main.cpp@0:f269e3021894, 2016-10-23 (annotated)
- Committer:
- elessair
- Date:
- Sun Oct 23 15:10:02 2016 +0000
- Revision:
- 0:f269e3021894
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
elessair | 0:f269e3021894 | 1 | #include "mbed.h" |
elessair | 0:f269e3021894 | 2 | |
elessair | 0:f269e3021894 | 3 | #if !DEVICE_LOCALFILESYSTEM |
elessair | 0:f269e3021894 | 4 | #error [NOT_SUPPORTED] LocalFileSystem not supported |
elessair | 0:f269e3021894 | 5 | #endif |
elessair | 0:f269e3021894 | 6 | |
elessair | 0:f269e3021894 | 7 | void led_blink(PinName led) { |
elessair | 0:f269e3021894 | 8 | DigitalOut myled(led); |
elessair | 0:f269e3021894 | 9 | while (1) { |
elessair | 0:f269e3021894 | 10 | myled = !myled; |
elessair | 0:f269e3021894 | 11 | wait(1.0); |
elessair | 0:f269e3021894 | 12 | } |
elessair | 0:f269e3021894 | 13 | } |
elessair | 0:f269e3021894 | 14 | |
elessair | 0:f269e3021894 | 15 | void notify_completion(bool success) { |
elessair | 0:f269e3021894 | 16 | if (success) { |
elessair | 0:f269e3021894 | 17 | printf("{success}\n"); |
elessair | 0:f269e3021894 | 18 | } else { |
elessair | 0:f269e3021894 | 19 | printf("{failure}\n"); |
elessair | 0:f269e3021894 | 20 | } |
elessair | 0:f269e3021894 | 21 | |
elessair | 0:f269e3021894 | 22 | printf("{end}\n"); |
elessair | 0:f269e3021894 | 23 | led_blink(success ? LED1 : LED4); |
elessair | 0:f269e3021894 | 24 | } |
elessair | 0:f269e3021894 | 25 | |
elessair | 0:f269e3021894 | 26 | #define TEST_STRING "Hello World!" |
elessair | 0:f269e3021894 | 27 | |
elessair | 0:f269e3021894 | 28 | FILE* test_open(char* path, const char* mode) { |
elessair | 0:f269e3021894 | 29 | FILE *f; |
elessair | 0:f269e3021894 | 30 | f = fopen(path, mode); |
elessair | 0:f269e3021894 | 31 | if (f == NULL) { |
elessair | 0:f269e3021894 | 32 | printf("Error opening file\n"); |
elessair | 0:f269e3021894 | 33 | notify_completion(false); |
elessair | 0:f269e3021894 | 34 | } |
elessair | 0:f269e3021894 | 35 | |
elessair | 0:f269e3021894 | 36 | return f; |
elessair | 0:f269e3021894 | 37 | } |
elessair | 0:f269e3021894 | 38 | |
elessair | 0:f269e3021894 | 39 | void test_write(FILE* f, const char* str) { |
elessair | 0:f269e3021894 | 40 | int n = fprintf(f, str); |
elessair | 0:f269e3021894 | 41 | if (n != strlen(str)) { |
elessair | 0:f269e3021894 | 42 | printf("Error writing file\n"); |
elessair | 0:f269e3021894 | 43 | notify_completion(false); |
elessair | 0:f269e3021894 | 44 | } |
elessair | 0:f269e3021894 | 45 | } |
elessair | 0:f269e3021894 | 46 | |
elessair | 0:f269e3021894 | 47 | void test_close(FILE* f) { |
elessair | 0:f269e3021894 | 48 | int rc = fclose(f); |
elessair | 0:f269e3021894 | 49 | if (rc != 0) { |
elessair | 0:f269e3021894 | 50 | printf("Error closing file\n"); |
elessair | 0:f269e3021894 | 51 | notify_completion(false); |
elessair | 0:f269e3021894 | 52 | } |
elessair | 0:f269e3021894 | 53 | } |
elessair | 0:f269e3021894 | 54 | |
elessair | 0:f269e3021894 | 55 | int main() { |
elessair | 0:f269e3021894 | 56 | LocalFileSystem local("local"); |
elessair | 0:f269e3021894 | 57 | |
elessair | 0:f269e3021894 | 58 | FILE *f; |
elessair | 0:f269e3021894 | 59 | char* str = TEST_STRING; |
elessair | 0:f269e3021894 | 60 | char* buffer = (char*) malloc(sizeof(unsigned char)*strlen(TEST_STRING)); |
elessair | 0:f269e3021894 | 61 | int str_len = strlen(TEST_STRING); |
elessair | 0:f269e3021894 | 62 | |
elessair | 0:f269e3021894 | 63 | printf("Write files\n"); |
elessair | 0:f269e3021894 | 64 | char filename[32]; |
elessair | 0:f269e3021894 | 65 | for (int i=0; i<10; i++) { |
elessair | 0:f269e3021894 | 66 | sprintf(filename, "/local/test_%d.txt", i); |
elessair | 0:f269e3021894 | 67 | printf("Creating file: %s\n", filename); |
elessair | 0:f269e3021894 | 68 | f = test_open(filename, "w"); |
elessair | 0:f269e3021894 | 69 | test_write(f, str); |
elessair | 0:f269e3021894 | 70 | test_close(f); |
elessair | 0:f269e3021894 | 71 | } |
elessair | 0:f269e3021894 | 72 | |
elessair | 0:f269e3021894 | 73 | printf("List files:\n"); |
elessair | 0:f269e3021894 | 74 | DIR *d = opendir("/local"); |
elessair | 0:f269e3021894 | 75 | struct dirent *p; |
elessair | 0:f269e3021894 | 76 | while((p = readdir(d)) != NULL) { |
elessair | 0:f269e3021894 | 77 | printf("%s\n", p->d_name); |
elessair | 0:f269e3021894 | 78 | } |
elessair | 0:f269e3021894 | 79 | closedir(d); |
elessair | 0:f269e3021894 | 80 | |
elessair | 0:f269e3021894 | 81 | notify_completion(true); |
elessair | 0:f269e3021894 | 82 | } |