Emilio Monti
/
rtos_file
Simple test for using the local file system within a thread
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 #include "mbed.h" 00002 #include "rtos.h" 00003 00004 void led_blink(PinName led_name) { 00005 DigitalOut led(led_name); 00006 while (1) { 00007 Thread::wait(1000); 00008 led = !led; 00009 } 00010 } 00011 00012 void notify_completion(bool success) { 00013 if (success) { 00014 printf("{success}\n"); 00015 } else { 00016 printf("{failure}\n"); 00017 } 00018 00019 printf("{end}\n"); 00020 led_blink(success?LED2:LED4); 00021 } 00022 00023 #define FILENAME "/local/out.txt" 00024 #define TEST_STRING "Hello World!" 00025 00026 FILE* test_open(const char* mode) { 00027 FILE *f; 00028 f = fopen(FILENAME, mode); 00029 if (f == NULL) { 00030 printf("Error opening file\n"); 00031 notify_completion(false); 00032 } 00033 00034 return f; 00035 } 00036 00037 void test_write(FILE* f, char* str, int str_len) { 00038 int n = fprintf(f, str); 00039 if (n != str_len) { 00040 printf("Error writing file\n"); 00041 notify_completion(false); 00042 } 00043 } 00044 00045 void test_read(FILE* f, char* str, int str_len) { 00046 int n = fread(str, sizeof(unsigned char), str_len, f); 00047 if (n != str_len) { 00048 printf("Error reading file\n"); 00049 notify_completion(false); 00050 } 00051 } 00052 00053 void test_close(FILE* f) { 00054 int rc = fclose(f); 00055 if (rc != 0) { 00056 printf("Error closing file\n"); 00057 notify_completion(false); 00058 } 00059 } 00060 00061 void test_localfilesystem(void const *argument) { 00062 LocalFileSystem local("local"); 00063 00064 FILE *f; 00065 char* str = TEST_STRING; 00066 char* buffer = (char*) malloc(sizeof(unsigned char)*strlen(TEST_STRING)); 00067 int str_len = strlen(TEST_STRING); 00068 00069 // Write 00070 f = test_open("w"); 00071 test_write(f, str, str_len); 00072 test_close(f); 00073 00074 // Read 00075 f = test_open("r"); 00076 test_read(f, buffer, str_len); 00077 test_close(f); 00078 00079 // Check the two strings are equal 00080 notify_completion((strncmp(buffer, str, str_len) == 0)); 00081 } 00082 00083 int main() { 00084 Thread t(test_localfilesystem); 00085 led_blink(LED1); 00086 }
Generated on Tue Jul 12 2022 21:55:09 by 1.7.2