Add the generic kfile resync.
[bertos.git] / bertos / kern / kfile.c
index cab20aa668803ac929ed65157a813608b33ae8db..81cd9d0a422d941717250c0e84cdb3c71d166ce5 100644 (file)
@@ -42,7 +42,9 @@
 
 #include "cfg/cfg_kfile.h"
 #include <cfg/debug.h>
+#include <cfg/log.h>
 
+#include <drv/timer.h>
 #include <mware/formatwr.h>
 
 #include <string.h>
@@ -194,12 +196,11 @@ kfile_off_t kfile_genericSeek(struct KFile *fd, kfile_off_t offset, KSeekMode wh
                break;
        }
 
+       #if LOG_LEVEL >= LOG_LVL_INFO
        /* Bound check */
        if (seek_pos + offset > fd->size)
-       {
-               ASSERT(0);
-               return EOF;
-       }
+               LOG_INFO("seek outside EOF\n");
+       #endif
 
        fd->seek_pos = seek_pos + offset;
 
@@ -228,3 +229,30 @@ int kfile_genericClose(UNUSED_ARG(struct KFile *, fd))
 };
 
 
+/**
+ * Discard input to resynchronize with remote end.
+ *
+ * Discard incoming data until the kfile_getc stops receiving
+ * characters for at least \a delay milliseconds.
+ *
+ * \note If the timeout occur, we reset the error before to
+ * quit.
+ */
+void kfile_resync(KFile *fd, mtime_t delay)
+{
+       ticks_t start_time = timer_clock();
+       for(;;)
+       {
+               if(kfile_getc(fd) != EOF)
+                       start_time = timer_clock();
+
+               if ((timer_clock() - start_time) > ms_to_ticks(delay))
+               {
+                       kfile_clearerr(fd);
+                       break;
+               }
+
+       }
+}
+
+