X-Git-Url: https://codewiz.org/gitweb?a=blobdiff_plain;f=kern%2Fkfile.c;h=9996c1aa0f6cfd74388b8b78c3a361654691227b;hb=1b38e2c9925d6ec7fc50b327fc46d490f20d7ee0;hp=38f438027d3c738a32a42fab054776e5b86a6ef4;hpb=9440bf5d1e06bbbecd17da05a2be5705e3503a47;p=bertos.git diff --git a/kern/kfile.c b/kern/kfile.c index 38f43802..9996c1aa 100644 --- a/kern/kfile.c +++ b/kern/kfile.c @@ -40,10 +40,12 @@ * */ -#include +#include +#include -#inclede +#include +#include /** * Move \a fd file seek position of \a offset bytes @@ -51,7 +53,7 @@ * This is a generic implementation of seek function, you should redefine * it in your local module. */ -int32_t kfile_seek(struct _KFile *fd, kfile_off_t offset, KSeekMode whence) +kfile_off_t kfile_seek(struct _KFile *fd, kfile_off_t offset, KSeekMode whence) { uint32_t seek_pos; @@ -127,10 +129,13 @@ static bool kfile_rwTest(KFile *f, uint8_t *buf, size_t _size) /** * Test for program memory read/write. + * This function write and read \p test_buf long \p _size + * on \p fd handler. If you want not overwrite exist data + * you should pass an \p save_buf where test store exist data, + * otherwise su must pass NULL. */ -bool kfile_test(uint8_t *buf, size_t _size , uint8_t *save_buf, size_t * save_buf_size) +bool kfile_test(KFile *fd, uint8_t *test_buf, size_t _size , uint8_t *save_buf, size_t save_buf_size) { - KFile fd; int32_t size = _size; /* @@ -142,18 +147,15 @@ bool kfile_test(uint8_t *buf, size_t _size , uint8_t *save_buf, size_t * save_bu */ int32_t len = size/2; - /* - * Fill in test buffer - */ - for (int i = 0; i < size; i++) - test_buf[i] = (i & 0xff); - kprintf("Generated test string..\n"); + /* Fill test buffer */ + for (size_t i = 0; i < size; i++) + test_buf[i] = (i & 0xff); /* * Open fd handler */ - fd.open(&fd, NULL, 0); + fd->open(fd, NULL, 0); kprintf("Opened fd handler..\n"); /* @@ -162,25 +164,23 @@ bool kfile_test(uint8_t *buf, size_t _size , uint8_t *save_buf, size_t * save_bu */ if (save_buf != NULL) { - fd.read(&fd, save_buf, save_buf_size); - kprintf("Saved content..form [%lu] to [%lu]\n", fd.seek_pos, fd.seek_pos + save_buf_size); + fd->read(fd, save_buf, save_buf_size); + kprintf("Saved content..form [%lu] to [%lu]\n", fd->seek_pos, fd->seek_pos + save_buf_size); } /* TEST 1 BEGIN. */ - kprintf("Test 1: write from pos 0 to [%lu]\n", fd.size); + kprintf("Test 1: write from pos 0 to [%lu]\n", size); /* * Seek to addr 0 */ - if (fd.seek(&fd, 0, KSM_SEEK_SET) != 0) + if (fd->seek(fd, 0, KSM_SEEK_SET) != 0) goto kfile_test_end; - kprintf("Seek to [%lu], expected[0]\n", fd.seek_pos); - /* * Test flash read/write to address 0..size */ - if (!Kfile_rwTest(&fd, test_buf, size)) + if (!kfile_rwTest(fd, test_buf, size)) goto kfile_test_end; kprintf("Test 1: ok!\n"); @@ -190,24 +190,22 @@ bool kfile_test(uint8_t *buf, size_t _size , uint8_t *save_buf, size_t * save_bu */ if (save_buf != NULL) { - fd.seek(&fd, 0, KSM_SEEK_SET); + fd->seek(fd, 0, KSM_SEEK_SET); - if (fd.write(&fd, save_buf, save_buf_size) != size) + if (fd->write(fd, save_buf, save_buf_size) != size) goto kfile_test_end; - kprintf("Restore content..form [%lu] to [%lu]\n", fd.seek_pos, fd.seek_pos + save_buf_size); + kprintf("Restore content..form [%lu] to [%lu]\n", fd->seek_pos, fd->seek_pos + save_buf_size); } /* TEST 1 END. */ /* TEST 2 BEGIN. */ - kprintf("Test 2: write from pos [%lu] to [%lu]\n", fd.size/2 , size); + kprintf("Test 2: write from pos [%lu] to [%lu]\n", fd->size/2 , size); /* * Go to half test size. */ - fd.seek(&fd, (fd.size/ 2), KSM_SEEK_SET); - - kprintf("Seek to [%lu], expected[%lu]\n", fd.seek_pos, fd.size/2); + fd->seek(fd, (fd->size/ 2), KSM_SEEK_SET); /* * If necessary, user could save content, @@ -215,15 +213,15 @@ bool kfile_test(uint8_t *buf, size_t _size , uint8_t *save_buf, size_t * save_bu */ if (save_buf != NULL) { - fd.read(&fd, save_buf, save_buf_size); - fd.seek(&fd, -size, KSM_SEEK_CUR); - kprintf("Saved content..form [%lu] to [%lu]\n", fd.seek_pos, fd.seek_pos + save_buf_size); + fd->read(fd, save_buf, save_buf_size); + fd->seek(fd, -size, KSM_SEEK_CUR); + kprintf("Saved content..form [%lu] to [%lu]\n", fd->seek_pos, fd->seek_pos + save_buf_size); } /* * Test flash read/write to address FLASHEND/2 ... FLASHEND/2 + size */ - if (!Kfile_rwTest(&fd, test_buf, size)) + if (!kfile_rwTest(fd, test_buf, size)) goto kfile_test_end; kprintf("Test 2: ok!\n"); @@ -233,24 +231,23 @@ bool kfile_test(uint8_t *buf, size_t _size , uint8_t *save_buf, size_t * save_bu */ if (save_buf != NULL) { - fd.seek(&fd, -size, KSM_SEEK_CUR); + fd->seek(fd, -size, KSM_SEEK_CUR); - if (fd.write(&fd, save_buf, save_buf_size) != size) + if (fd->write(fd, save_buf, save_buf_size) != size) goto kfile_test_end; - kprintf("Restore content..form [%lu] to [%lu]\n", fd.seek_pos, fd.seek_pos + save_buf_size); + kprintf("Restore content..form [%lu] to [%lu]\n", fd->seek_pos, fd->seek_pos + save_buf_size); } /* TEST 2 END. */ /* TEST 3 BEGIN. */ - kprintf("Test 3: write outside of fd.size limit [%lu]\n", fd.size); + kprintf("Test 3: write outside of fd->size limit [%lu]\n", fd->size); /* * Go to the Flash end */ - fd.seek(&fd, -len, KSM_SEEK_END); - kprintf("Seek to [%lu], expected[%lu]\n", fd.seek_pos, fd.size - len); + fd->seek(fd, -len, KSM_SEEK_END); /* * If necessary, user could save content, @@ -260,15 +257,15 @@ bool kfile_test(uint8_t *buf, size_t _size , uint8_t *save_buf, size_t * save_bu { ASSERT(len > save_buf_size); - fd.read(&fd, save_buf, len); - fd.seek(&fd, -len, KSM_SEEK_CUR); - kprintf("Saved content..form [%lu] to [%lu]\n", fd.seek_pos, fd.seek_pos + len); + fd->read(fd, save_buf, len); + fd->seek(fd, -len, KSM_SEEK_CUR); + kprintf("Saved content..form [%lu] to [%lu]\n", fd->seek_pos, fd->seek_pos + len); } /* * Test flash read/write to address (FLASHEND - size) ... FLASHEND */ - if (!Kfile_rwTest(&fd, test_buf, size)) + if (kfile_rwTest(fd, test_buf, size)) goto kfile_test_end; kprintf("Test 3: ok !\n"); @@ -278,22 +275,22 @@ bool kfile_test(uint8_t *buf, size_t _size , uint8_t *save_buf, size_t * save_bu */ if (save_buf != NULL) { - fd.seek(&fd, -len, KSM_SEEK_END); + fd->seek(fd, -len, KSM_SEEK_END); - if (fd.write(&fd, save_buf, len) != len) + if (fd->write(fd, save_buf, len) != len) goto kfile_test_end; - kprintf("Restore content..form [%lu] to [%lu]\n", fd.seek_pos, fd.seek_pos + len); + kprintf("Restore content..form [%lu] to [%lu]\n", fd->seek_pos, fd->seek_pos + len); } /* TEST 3 END. */ - fd.close(&fd); + fd->close(fd); return true; kfile_test_end: - fd.close(&fd); + fd->close(fd); return false; } -#endif /* CONFIG_TEST */ \ No newline at end of file +#endif /* CONFIG_TEST */