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 /*
vpcola 0:f1d3878b8dd9 2 * mbed Microcontroller Library
vpcola 0:f1d3878b8dd9 3 * Copyright (c) 2006-2016 ARM Limited
vpcola 0:f1d3878b8dd9 4 *
vpcola 0:f1d3878b8dd9 5 * Licensed under the Apache License, Version 2.0 (the "License");
vpcola 0:f1d3878b8dd9 6 * you may not use this file except in compliance with the License.
vpcola 0:f1d3878b8dd9 7 * You may obtain a copy of the License at
vpcola 0:f1d3878b8dd9 8 *
vpcola 0:f1d3878b8dd9 9 * http://www.apache.org/licenses/LICENSE-2.0
vpcola 0:f1d3878b8dd9 10 *
vpcola 0:f1d3878b8dd9 11 * Unless required by applicable law or agreed to in writing, software
vpcola 0:f1d3878b8dd9 12 * distributed under the License is distributed on an "AS IS" BASIS,
vpcola 0:f1d3878b8dd9 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
vpcola 0:f1d3878b8dd9 14 * See the License for the specific language governing permissions and
vpcola 0:f1d3878b8dd9 15 * limitations under the License.
vpcola 0:f1d3878b8dd9 16 *
vpcola 0:f1d3878b8dd9 17 */
vpcola 0:f1d3878b8dd9 18
vpcola 0:f1d3878b8dd9 19 /* The following copyright notice is reproduced from the glibc project
vpcola 0:f1d3878b8dd9 20 * REF_LICENCE_GLIBC
vpcola 0:f1d3878b8dd9 21 *
vpcola 0:f1d3878b8dd9 22 * Copyright (C) 1991, 1992 Free Software Foundation, Inc.
vpcola 0:f1d3878b8dd9 23 * This file is part of the GNU C Library.
vpcola 0:f1d3878b8dd9 24 *
vpcola 0:f1d3878b8dd9 25 * The GNU C Library is free software; you can redistribute it and/or
vpcola 0:f1d3878b8dd9 26 * modify it under the terms of the GNU Library General Public License as
vpcola 0:f1d3878b8dd9 27 * published by the Free Software Foundation; either version 2 of the
vpcola 0:f1d3878b8dd9 28 * License, or (at your option) any later version.
vpcola 0:f1d3878b8dd9 29 *
vpcola 0:f1d3878b8dd9 30 * The GNU C Library is distributed in the hope that it will be useful,
vpcola 0:f1d3878b8dd9 31 * but WITHOUT ANY WARRANTY; without even the implied warranty of
vpcola 0:f1d3878b8dd9 32 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
vpcola 0:f1d3878b8dd9 33 * Library General Public License for more details.
vpcola 0:f1d3878b8dd9 34 *
vpcola 0:f1d3878b8dd9 35 * You should have received a copy of the GNU Library General Public
vpcola 0:f1d3878b8dd9 36 * License along with the GNU C Library; see the file COPYING.LIB. If
vpcola 0:f1d3878b8dd9 37 * not, write to the Free Software Foundation, Inc., 675 Mass Ave,
vpcola 0:f1d3878b8dd9 38 * Cambridge, MA 02139, USA.
vpcola 0:f1d3878b8dd9 39 */
vpcola 0:f1d3878b8dd9 40
vpcola 0:f1d3878b8dd9 41
vpcola 0:f1d3878b8dd9 42 /** @file basic.cpp POSIX File API (stdio) test cases
vpcola 0:f1d3878b8dd9 43 *
vpcola 0:f1d3878b8dd9 44 * Consult the documentation under the test-case functions for
vpcola 0:f1d3878b8dd9 45 * a description of the individual test case.
vpcola 0:f1d3878b8dd9 46 *
vpcola 0:f1d3878b8dd9 47 * this file includes ports for the mbed 2 test cases from the following locations:
vpcola 0:f1d3878b8dd9 48 * - https://github.com:/armmbed/mbed-os/features/unsupported/tests/mbed/dir_sd/main.cpp.
vpcola 0:f1d3878b8dd9 49 * - https://github.com:/armmbed/mbed-os/features/unsupported/tests/mbed/file/main.cpp.
vpcola 0:f1d3878b8dd9 50 * - https://github.com:/armmbed/mbed-os/features/unsupported/tests/mbed/sd/main.cpp
vpcola 0:f1d3878b8dd9 51 * - https://github.com:/armmbed/mbed-os/features/unsupported/tests/mbed/sd_perf_handle/main.cpp
vpcola 0:f1d3878b8dd9 52 * - https://github.com:/armmbed/mbed-os/features/unsupported/tests/mbed/sd_perf_stdio/main.cpp.
vpcola 0:f1d3878b8dd9 53 */
vpcola 0:f1d3878b8dd9 54
vpcola 0:f1d3878b8dd9 55 #include "mbed.h"
vpcola 0:f1d3878b8dd9 56 #include "mbed_config.h"
vpcola 0:f1d3878b8dd9 57 #include "FATFileSystem.h"
vpcola 0:f1d3878b8dd9 58 #include "SDBlockDevice.h"
vpcola 0:f1d3878b8dd9 59 #include "test_env.h"
vpcola 0:f1d3878b8dd9 60 #include "fsfat_debug.h"
vpcola 0:f1d3878b8dd9 61 #include "fsfat_test.h"
vpcola 0:f1d3878b8dd9 62 #include "utest/utest.h"
vpcola 0:f1d3878b8dd9 63 #include "unity/unity.h"
vpcola 0:f1d3878b8dd9 64 #include "greentea-client/test_env.h"
vpcola 0:f1d3878b8dd9 65
vpcola 0:f1d3878b8dd9 66 #include <stdio.h>
vpcola 0:f1d3878b8dd9 67 #include <stdlib.h>
vpcola 0:f1d3878b8dd9 68 #include <string.h>
vpcola 0:f1d3878b8dd9 69 #include <errno.h>
vpcola 0:f1d3878b8dd9 70 #include <algorithm>
vpcola 0:f1d3878b8dd9 71 /* retarget.h is included after errno.h so symbols are mapped to
vpcola 0:f1d3878b8dd9 72 * consistent values for all toolchains */
vpcola 0:f1d3878b8dd9 73 #include "platform/mbed_retarget.h"
vpcola 0:f1d3878b8dd9 74
vpcola 0:f1d3878b8dd9 75 using namespace utest::v1;
vpcola 0:f1d3878b8dd9 76
vpcola 0:f1d3878b8dd9 77 /* DEVICE_SPI
vpcola 0:f1d3878b8dd9 78 * This symbol is defined in targets.json if the target has a SPI interface, which is required for SDCard support.
vpcola 0:f1d3878b8dd9 79 *
vpcola 0:f1d3878b8dd9 80 * MBED_CONF_APP_FSFAT_SDCARD_INSTALLED
vpcola 0:f1d3878b8dd9 81 * For testing purposes, an SDCard must be installed on the target for the test cases in this file to succeed.
vpcola 0:f1d3878b8dd9 82 * If the target has an SD card installed then the MBED_CONF_APP_FSFAT_SDCARD_INSTALLED will be generated
vpcola 0:f1d3878b8dd9 83 * from the mbed_app.json, which includes the line
vpcola 0:f1d3878b8dd9 84 * {
vpcola 0:f1d3878b8dd9 85 * "config": {
vpcola 0:f1d3878b8dd9 86 * "UART_RX": "D0",
vpcola 0:f1d3878b8dd9 87 * <<< lines removed >>>
vpcola 0:f1d3878b8dd9 88 * "DEVICE_SPI": 1,
vpcola 0:f1d3878b8dd9 89 * "MBED_CONF_APP_FSFAT_SDCARD_INSTALLED": 1
vpcola 0:f1d3878b8dd9 90 * },
vpcola 0:f1d3878b8dd9 91 * <<< lines removed >>>
vpcola 0:f1d3878b8dd9 92 */
vpcola 0:f1d3878b8dd9 93 #if defined(DEVICE_SPI) && defined(MBED_CONF_APP_FSFAT_SDCARD_INSTALLED)
vpcola 0:f1d3878b8dd9 94
vpcola 0:f1d3878b8dd9 95 #define FSFAT_BASIC_TEST_00 fsfat_basic_test_00
vpcola 0:f1d3878b8dd9 96 #define FSFAT_BASIC_TEST_01 fsfat_basic_test_01
vpcola 0:f1d3878b8dd9 97 #define FSFAT_BASIC_TEST_02 fsfat_basic_test_02
vpcola 0:f1d3878b8dd9 98 #define FSFAT_BASIC_TEST_03 fsfat_basic_test_03
vpcola 0:f1d3878b8dd9 99 #define FSFAT_BASIC_TEST_04 fsfat_basic_test_04
vpcola 0:f1d3878b8dd9 100 #define FSFAT_BASIC_TEST_05 fsfat_basic_test_05
vpcola 0:f1d3878b8dd9 101 #define FSFAT_BASIC_TEST_06 fsfat_basic_test_06
vpcola 0:f1d3878b8dd9 102 #define FSFAT_BASIC_TEST_07 fsfat_basic_test_07
vpcola 0:f1d3878b8dd9 103 #define FSFAT_BASIC_TEST_08 fsfat_basic_test_08
vpcola 0:f1d3878b8dd9 104 #define FSFAT_BASIC_TEST_09 fsfat_basic_test_09
vpcola 0:f1d3878b8dd9 105 #define FSFAT_BASIC_TEST_10 fsfat_basic_test_10
vpcola 0:f1d3878b8dd9 106
vpcola 0:f1d3878b8dd9 107 #define FSFAT_BASIC_MSG_BUF_SIZE 256
vpcola 0:f1d3878b8dd9 108 #define FSFAT_BASIC_TEST_05_TEST_STRING "Hello World!"
vpcola 0:f1d3878b8dd9 109
vpcola 0:f1d3878b8dd9 110 static const char *sd_file_path = "/sd/out.txt";
vpcola 0:f1d3878b8dd9 111 static const char *sd_mount_pt = "sd";
vpcola 0:f1d3878b8dd9 112 static const int FSFAT_BASIC_DATA_SIZE = 256;
vpcola 0:f1d3878b8dd9 113 static char fsfat_basic_msg_g[FSFAT_BASIC_MSG_BUF_SIZE];
vpcola 0:f1d3878b8dd9 114 static char fsfat_basic_buffer[1024];
vpcola 0:f1d3878b8dd9 115 static const int FSFAT_BASIC_KIB_RW = 128;
vpcola 0:f1d3878b8dd9 116 static Timer fsfat_basic_timer;
vpcola 0:f1d3878b8dd9 117 static const char *fsfat_basic_bin_filename = "/sd/testfile.bin";
vpcola 0:f1d3878b8dd9 118 static const char *fsfat_basic_bin_filename_test_08 = "testfile.bin";
vpcola 0:f1d3878b8dd9 119 static const char *fsfat_basic_bin_filename_test_10 = "0:testfile.bin";
vpcola 0:f1d3878b8dd9 120
vpcola 0:f1d3878b8dd9 121
vpcola 0:f1d3878b8dd9 122
vpcola 0:f1d3878b8dd9 123 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 124 FATFileSystem fs(sd_mount_pt, &sd);
vpcola 0:f1d3878b8dd9 125
vpcola 0:f1d3878b8dd9 126 #define FSFAT_BASIC_MSG(_buf, _max_len, _fmt, ...) \
vpcola 0:f1d3878b8dd9 127 do \
vpcola 0:f1d3878b8dd9 128 { \
vpcola 0:f1d3878b8dd9 129 snprintf((_buf), (_max_len), (_fmt), __VA_ARGS__); \
vpcola 0:f1d3878b8dd9 130 }while(0);
vpcola 0:f1d3878b8dd9 131
vpcola 0:f1d3878b8dd9 132 /** @brief fopen test case
vpcola 0:f1d3878b8dd9 133 *
vpcola 0:f1d3878b8dd9 134 * - open a file
vpcola 0:f1d3878b8dd9 135 * - generate random data items, write the item to the file and store a coy in a buffer for later use.
vpcola 0:f1d3878b8dd9 136 * - close the file.
vpcola 0:f1d3878b8dd9 137 * - open the file.
vpcola 0:f1d3878b8dd9 138 * - read the data items from the file and check they are the same as write.
vpcola 0:f1d3878b8dd9 139 * - close the file.
vpcola 0:f1d3878b8dd9 140 *
vpcola 0:f1d3878b8dd9 141 * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors.
vpcola 0:f1d3878b8dd9 142 */
vpcola 0:f1d3878b8dd9 143 static control_t fsfat_basic_test_00()
vpcola 0:f1d3878b8dd9 144 {
vpcola 0:f1d3878b8dd9 145
vpcola 0:f1d3878b8dd9 146 uint8_t data_written[FSFAT_BASIC_DATA_SIZE] = { 0 };
vpcola 0:f1d3878b8dd9 147 bool read_result = false;
vpcola 0:f1d3878b8dd9 148 bool write_result = false;
vpcola 0:f1d3878b8dd9 149
vpcola 0:f1d3878b8dd9 150 // Fill data_written buffer with random data
vpcola 0:f1d3878b8dd9 151 // Write these data into the file
vpcola 0:f1d3878b8dd9 152 FSFAT_FENTRYLOG("%s:entered\n", __func__);
vpcola 0:f1d3878b8dd9 153 {
vpcola 0:f1d3878b8dd9 154 FSFAT_DBGLOG("%s:SD: Writing ... ", __func__);
vpcola 0:f1d3878b8dd9 155 FILE *f = fopen(sd_file_path, "w");
vpcola 0:f1d3878b8dd9 156 if (f) {
vpcola 0:f1d3878b8dd9 157 for (int i = 0; i < FSFAT_BASIC_DATA_SIZE; i++) {
vpcola 0:f1d3878b8dd9 158 data_written[i] = rand() % 0XFF;
vpcola 0:f1d3878b8dd9 159 fprintf(f, "%c", data_written[i]);
vpcola 0:f1d3878b8dd9 160 }
vpcola 0:f1d3878b8dd9 161 write_result = true;
vpcola 0:f1d3878b8dd9 162 fclose(f);
vpcola 0:f1d3878b8dd9 163 }
vpcola 0:f1d3878b8dd9 164 FSFAT_DBGLOG("[%s]\n", write_result ? "OK" : "FAIL");
vpcola 0:f1d3878b8dd9 165 }
vpcola 0:f1d3878b8dd9 166 TEST_ASSERT_MESSAGE(write_result == true, "Error: write_result is set to false.");
vpcola 0:f1d3878b8dd9 167
vpcola 0:f1d3878b8dd9 168 // Read back the data from the file and store them in data_read
vpcola 0:f1d3878b8dd9 169 {
vpcola 0:f1d3878b8dd9 170 FSFAT_DBGLOG("%s:SD: Reading data ... ", __func__);
vpcola 0:f1d3878b8dd9 171 FILE *f = fopen(sd_file_path, "r");
vpcola 0:f1d3878b8dd9 172 if (f) {
vpcola 0:f1d3878b8dd9 173 read_result = true;
vpcola 0:f1d3878b8dd9 174 for (int i = 0; i < FSFAT_BASIC_DATA_SIZE; i++) {
vpcola 0:f1d3878b8dd9 175 uint8_t data = fgetc(f);
vpcola 0:f1d3878b8dd9 176 if (data != data_written[i]) {
vpcola 0:f1d3878b8dd9 177 read_result = false;
vpcola 0:f1d3878b8dd9 178 break;
vpcola 0:f1d3878b8dd9 179 }
vpcola 0:f1d3878b8dd9 180 }
vpcola 0:f1d3878b8dd9 181 fclose(f);
vpcola 0:f1d3878b8dd9 182 }
vpcola 0:f1d3878b8dd9 183 FSFAT_DBGLOG("[%s]\n", read_result ? "OK" : "FAIL");
vpcola 0:f1d3878b8dd9 184 }
vpcola 0:f1d3878b8dd9 185 TEST_ASSERT_MESSAGE(read_result == true, "Error: read_result is set to false.");
vpcola 0:f1d3878b8dd9 186 return CaseNext;
vpcola 0:f1d3878b8dd9 187 }
vpcola 0:f1d3878b8dd9 188
vpcola 0:f1d3878b8dd9 189
vpcola 0:f1d3878b8dd9 190 /** @brief test-fseek.c test ported from glibc project. See the licence at REF_LICENCE_GLIBC.
vpcola 0:f1d3878b8dd9 191 *
vpcola 0:f1d3878b8dd9 192 * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors.
vpcola 0:f1d3878b8dd9 193 */
vpcola 0:f1d3878b8dd9 194 static control_t fsfat_basic_test_01()
vpcola 0:f1d3878b8dd9 195 {
vpcola 0:f1d3878b8dd9 196 FILE *fp, *fp1;
vpcola 0:f1d3878b8dd9 197 int i, j;
vpcola 0:f1d3878b8dd9 198 int ret = 0;
vpcola 0:f1d3878b8dd9 199
vpcola 0:f1d3878b8dd9 200 FSFAT_FENTRYLOG("%s:entered\n", __func__);
vpcola 0:f1d3878b8dd9 201 fp = fopen (sd_file_path, "w+");
vpcola 0:f1d3878b8dd9 202 if (fp == NULL) {
vpcola 0:f1d3878b8dd9 203 FSFAT_DBGLOG("errno=%d\n", errno);
vpcola 0:f1d3878b8dd9 204 TEST_ASSERT_MESSAGE(false, "error");
vpcola 0:f1d3878b8dd9 205 return CaseNext;
vpcola 0:f1d3878b8dd9 206 }
vpcola 0:f1d3878b8dd9 207
vpcola 0:f1d3878b8dd9 208 for (i = 0; i < 256; i++) {
vpcola 0:f1d3878b8dd9 209 putc (i, fp);
vpcola 0:f1d3878b8dd9 210 }
vpcola 0:f1d3878b8dd9 211 /* FIXME: freopen() should open the specified file closing the first stream. As can be seen from the
vpcola 0:f1d3878b8dd9 212 * code below, the old file descriptor fp can still be used, and this should not happen.
vpcola 0:f1d3878b8dd9 213 */
vpcola 0:f1d3878b8dd9 214 fp1 = freopen (sd_file_path, "r", fp);
vpcola 0:f1d3878b8dd9 215 TEST_ASSERT_MESSAGE(fp1 == fp, "Error: cannot open file for reading");
vpcola 0:f1d3878b8dd9 216
vpcola 0:f1d3878b8dd9 217 for (i = 1; i <= 255; i++) {
vpcola 0:f1d3878b8dd9 218 ret = fseek (fp, (long) -i, SEEK_END);
vpcola 0:f1d3878b8dd9 219 FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s:Error: fseek() failed (ret=%d).\n", __func__, (int) ret);
vpcola 0:f1d3878b8dd9 220 TEST_ASSERT_MESSAGE(ret == 0, fsfat_basic_msg_g);
vpcola 0:f1d3878b8dd9 221
vpcola 0:f1d3878b8dd9 222 if ((j = getc (fp)) != 256 - i) {
vpcola 0:f1d3878b8dd9 223 FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: SEEK_END failed (j=%d)\n", __func__, j);
vpcola 0:f1d3878b8dd9 224 TEST_ASSERT_MESSAGE(false, fsfat_basic_msg_g);
vpcola 0:f1d3878b8dd9 225 }
vpcola 0:f1d3878b8dd9 226 ret = fseek (fp, (long) i, SEEK_SET);
vpcola 0:f1d3878b8dd9 227 FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: Cannot SEEK_SET (ret=%d).\n", __func__, (int) ret);
vpcola 0:f1d3878b8dd9 228 TEST_ASSERT_MESSAGE(ret == 0, fsfat_basic_msg_g);
vpcola 0:f1d3878b8dd9 229
vpcola 0:f1d3878b8dd9 230 if ((j = getc (fp)) != i) {
vpcola 0:f1d3878b8dd9 231 FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: Cannot SEEK_SET (j=%d).\n", __func__, j);
vpcola 0:f1d3878b8dd9 232 TEST_ASSERT_MESSAGE(ret == 0, fsfat_basic_msg_g);
vpcola 0:f1d3878b8dd9 233 }
vpcola 0:f1d3878b8dd9 234 if ((ret = fseek (fp, (long) i, SEEK_SET))) {
vpcola 0:f1d3878b8dd9 235 FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: Cannot SEEK_SET (ret=%d).\n", __func__, (int) ret);
vpcola 0:f1d3878b8dd9 236 TEST_ASSERT_MESSAGE(ret == 0, fsfat_basic_msg_g);
vpcola 0:f1d3878b8dd9 237 }
vpcola 0:f1d3878b8dd9 238 if ((ret = fseek (fp, (long) (i >= 128 ? -128 : 128), SEEK_CUR))) {
vpcola 0:f1d3878b8dd9 239 FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: Cannot SEEK_CUR (ret=%d).\n", __func__, (int) ret);
vpcola 0:f1d3878b8dd9 240 TEST_ASSERT_MESSAGE(ret == 0, fsfat_basic_msg_g);
vpcola 0:f1d3878b8dd9 241 }
vpcola 0:f1d3878b8dd9 242 if ((j = getc (fp)) != (i >= 128 ? i - 128 : i + 128)) {
vpcola 0:f1d3878b8dd9 243 FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: Cannot SEEK_CUR (j=%d).\n", __func__, j);
vpcola 0:f1d3878b8dd9 244 TEST_ASSERT_MESSAGE(ret == 0, fsfat_basic_msg_g);
vpcola 0:f1d3878b8dd9 245 }
vpcola 0:f1d3878b8dd9 246 }
vpcola 0:f1d3878b8dd9 247 fclose (fp);
vpcola 0:f1d3878b8dd9 248 remove(sd_file_path);
vpcola 0:f1d3878b8dd9 249 return CaseNext;
vpcola 0:f1d3878b8dd9 250 }
vpcola 0:f1d3878b8dd9 251
vpcola 0:f1d3878b8dd9 252
vpcola 0:f1d3878b8dd9 253 /** @brief test_rdwr.c test ported from glibc project. See the licence at REF_LICENCE_GLIBC.
vpcola 0:f1d3878b8dd9 254 *
vpcola 0:f1d3878b8dd9 255 * WARNING: this test does not currently work. See WARNING comments below.
vpcola 0:f1d3878b8dd9 256 *
vpcola 0:f1d3878b8dd9 257 * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors.
vpcola 0:f1d3878b8dd9 258 */
vpcola 0:f1d3878b8dd9 259 static control_t fsfat_basic_test_02()
vpcola 0:f1d3878b8dd9 260 {
vpcola 0:f1d3878b8dd9 261 static const char hello[] = "Hello, world.\n";
vpcola 0:f1d3878b8dd9 262 static const char replace[] = "Hewwo, world.\n";
vpcola 0:f1d3878b8dd9 263 static const size_t replace_from = 2, replace_to = 4;
vpcola 0:f1d3878b8dd9 264 const char *filename = sd_file_path;
vpcola 0:f1d3878b8dd9 265 char buf[BUFSIZ];
vpcola 0:f1d3878b8dd9 266 FILE *f;
vpcola 0:f1d3878b8dd9 267 int lose = 0;
vpcola 0:f1d3878b8dd9 268 int32_t ret = 0;
vpcola 0:f1d3878b8dd9 269 char *rets = NULL;
vpcola 0:f1d3878b8dd9 270
vpcola 0:f1d3878b8dd9 271 FSFAT_FENTRYLOG("%s:entered\n", __func__);
vpcola 0:f1d3878b8dd9 272 f = fopen(filename, "w+");
vpcola 0:f1d3878b8dd9 273 if (f == NULL) {
vpcola 0:f1d3878b8dd9 274 FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: Cannot open file for writing (filename=%s).\n", __func__, filename);
vpcola 0:f1d3878b8dd9 275 TEST_ASSERT_MESSAGE(false, fsfat_basic_msg_g);
vpcola 0:f1d3878b8dd9 276 }
vpcola 0:f1d3878b8dd9 277
vpcola 0:f1d3878b8dd9 278 ret = fputs(hello, f);
vpcola 0:f1d3878b8dd9 279 if (ret == EOF) {
vpcola 0:f1d3878b8dd9 280 FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: fputs() failed to write string to file (filename=%s, string=%s).\n", __func__, filename, hello);
vpcola 0:f1d3878b8dd9 281 TEST_ASSERT_MESSAGE(false, fsfat_basic_msg_g);
vpcola 0:f1d3878b8dd9 282 }
vpcola 0:f1d3878b8dd9 283
vpcola 0:f1d3878b8dd9 284 rewind(f);
vpcola 0:f1d3878b8dd9 285 rets = fgets(buf, sizeof(buf), f);
vpcola 0:f1d3878b8dd9 286 if (rets == NULL) {
vpcola 0:f1d3878b8dd9 287 FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: fgets() failed to get string from file (filename=%s).\n", __func__, filename);
vpcola 0:f1d3878b8dd9 288 TEST_ASSERT_MESSAGE(false, fsfat_basic_msg_g);
vpcola 0:f1d3878b8dd9 289 }
vpcola 0:f1d3878b8dd9 290 rets = NULL;
vpcola 0:f1d3878b8dd9 291
vpcola 0:f1d3878b8dd9 292 rewind(f);
vpcola 0:f1d3878b8dd9 293 ret = fputs(buf, f);
vpcola 0:f1d3878b8dd9 294 if (ret == EOF) {
vpcola 0:f1d3878b8dd9 295 FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: fputs() failed to write string to file (filename=%s, string=%s).\n", __func__, filename, buf);
vpcola 0:f1d3878b8dd9 296 TEST_ASSERT_MESSAGE(false, fsfat_basic_msg_g);
vpcola 0:f1d3878b8dd9 297 }
vpcola 0:f1d3878b8dd9 298
vpcola 0:f1d3878b8dd9 299 rewind(f);
vpcola 0:f1d3878b8dd9 300 {
vpcola 0:f1d3878b8dd9 301 register size_t i;
vpcola 0:f1d3878b8dd9 302 for (i = 0; i < replace_from; ++i)
vpcola 0:f1d3878b8dd9 303 {
vpcola 0:f1d3878b8dd9 304 int c = getc(f);
vpcola 0:f1d3878b8dd9 305 if (c == EOF)
vpcola 0:f1d3878b8dd9 306 {
vpcola 0:f1d3878b8dd9 307 FSFAT_DBGLOG("EOF at %u.\n", i);
vpcola 0:f1d3878b8dd9 308 lose = 1;
vpcola 0:f1d3878b8dd9 309 break;
vpcola 0:f1d3878b8dd9 310 }
vpcola 0:f1d3878b8dd9 311 else if (c != hello[i])
vpcola 0:f1d3878b8dd9 312 {
vpcola 0:f1d3878b8dd9 313 FSFAT_DBGLOG("Got '%c' instead of '%c' at %u.\n",
vpcola 0:f1d3878b8dd9 314 (unsigned char) c, hello[i], i);
vpcola 0:f1d3878b8dd9 315 lose = 1;
vpcola 0:f1d3878b8dd9 316 break;
vpcola 0:f1d3878b8dd9 317 }
vpcola 0:f1d3878b8dd9 318 }
vpcola 0:f1d3878b8dd9 319 }
vpcola 0:f1d3878b8dd9 320 /* WARNING: printf("%s: here1. (lose = %d)\n", __func__, lose); */
vpcola 0:f1d3878b8dd9 321 {
vpcola 0:f1d3878b8dd9 322 long int where = ftell(f);
vpcola 0:f1d3878b8dd9 323 if (where == replace_from)
vpcola 0:f1d3878b8dd9 324 {
vpcola 0:f1d3878b8dd9 325 register size_t i;
vpcola 0:f1d3878b8dd9 326 for (i = replace_from; i < replace_to; ++i) {
vpcola 0:f1d3878b8dd9 327 if (putc(replace[i], f) == EOF) {
vpcola 0:f1d3878b8dd9 328 FSFAT_DBGLOG("putc('%c') got %s at %u.\n",
vpcola 0:f1d3878b8dd9 329 replace[i], strerror(errno), i);
vpcola 0:f1d3878b8dd9 330 lose = 1;
vpcola 0:f1d3878b8dd9 331 break;
vpcola 0:f1d3878b8dd9 332 }
vpcola 0:f1d3878b8dd9 333 /* WARNING: The problem seems to be that putc() is not writing the 'w' chars into the file
vpcola 0:f1d3878b8dd9 334 * FSFAT_DBGLOG("%s: here1.5. (char = %c, char as int=%d, ret=%d) \n", __func__, replace[i], (int) replace[i], ret);
vpcola 0:f1d3878b8dd9 335 */
vpcola 0:f1d3878b8dd9 336 }
vpcola 0:f1d3878b8dd9 337 }
vpcola 0:f1d3878b8dd9 338 else if (where == -1L)
vpcola 0:f1d3878b8dd9 339 {
vpcola 0:f1d3878b8dd9 340 FSFAT_DBGLOG("ftell got %s (should be at %u).\n",
vpcola 0:f1d3878b8dd9 341 strerror(errno), replace_from);
vpcola 0:f1d3878b8dd9 342 lose = 1;
vpcola 0:f1d3878b8dd9 343 }
vpcola 0:f1d3878b8dd9 344 else
vpcola 0:f1d3878b8dd9 345 {
vpcola 0:f1d3878b8dd9 346 FSFAT_DBGLOG("ftell returns %ld; should be %u.\n", where, replace_from);
vpcola 0:f1d3878b8dd9 347 lose = 1;
vpcola 0:f1d3878b8dd9 348 }
vpcola 0:f1d3878b8dd9 349 }
vpcola 0:f1d3878b8dd9 350
vpcola 0:f1d3878b8dd9 351 if (!lose)
vpcola 0:f1d3878b8dd9 352 {
vpcola 0:f1d3878b8dd9 353 rewind(f);
vpcola 0:f1d3878b8dd9 354 memset(buf, 0, BUFSIZ);
vpcola 0:f1d3878b8dd9 355 if (fgets(buf, sizeof(buf), f) == NULL)
vpcola 0:f1d3878b8dd9 356 {
vpcola 0:f1d3878b8dd9 357 FSFAT_DBGLOG("fgets got %s.\n", strerror(errno));
vpcola 0:f1d3878b8dd9 358 lose = 1;
vpcola 0:f1d3878b8dd9 359 }
vpcola 0:f1d3878b8dd9 360 else if (strcmp(buf, replace))
vpcola 0:f1d3878b8dd9 361 {
vpcola 0:f1d3878b8dd9 362 FSFAT_DBGLOG("Read \"%s\" instead of \"%s\".\n", buf, replace);
vpcola 0:f1d3878b8dd9 363 lose = 1;
vpcola 0:f1d3878b8dd9 364 }
vpcola 0:f1d3878b8dd9 365 }
vpcola 0:f1d3878b8dd9 366
vpcola 0:f1d3878b8dd9 367 if (lose) {
vpcola 0:f1d3878b8dd9 368 FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: Test Failed. Losing file (filename=%s).\n", __func__, filename);
vpcola 0:f1d3878b8dd9 369 TEST_ASSERT_MESSAGE(false, fsfat_basic_msg_g);
vpcola 0:f1d3878b8dd9 370 }
vpcola 0:f1d3878b8dd9 371 remove(filename);
vpcola 0:f1d3878b8dd9 372 return CaseNext;
vpcola 0:f1d3878b8dd9 373 }
vpcola 0:f1d3878b8dd9 374
vpcola 0:f1d3878b8dd9 375 /** @brief temptest.c test ported from glibc project. See the licence at REF_LICENCE_GLIBC.
vpcola 0:f1d3878b8dd9 376 *
vpcola 0:f1d3878b8dd9 377 * tmpnam() is currently not implemented
vpcola 0:f1d3878b8dd9 378 *
vpcola 0:f1d3878b8dd9 379 * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors.
vpcola 0:f1d3878b8dd9 380 */
vpcola 0:f1d3878b8dd9 381 static control_t fsfat_basic_test_03()
vpcola 0:f1d3878b8dd9 382 {
vpcola 0:f1d3878b8dd9 383 char *fn = NULL;
vpcola 0:f1d3878b8dd9 384
vpcola 0:f1d3878b8dd9 385 FSFAT_FENTRYLOG("%s:entered\n", __func__);
vpcola 0:f1d3878b8dd9 386 fn = tmpnam((char *) NULL);
vpcola 0:f1d3878b8dd9 387 FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: appeared to generate a filename when function is not implemented.\n", __func__);
vpcola 0:f1d3878b8dd9 388 TEST_ASSERT_MESSAGE(fn == NULL, fsfat_basic_msg_g);
vpcola 0:f1d3878b8dd9 389 return CaseNext;
vpcola 0:f1d3878b8dd9 390 }
vpcola 0:f1d3878b8dd9 391
vpcola 0:f1d3878b8dd9 392
vpcola 0:f1d3878b8dd9 393 static bool fsfat_basic_fileno_check(const char *name, FILE *stream, int fd)
vpcola 0:f1d3878b8dd9 394 {
vpcola 0:f1d3878b8dd9 395 /* ARMCC stdio.h currently does not define fileno() */
vpcola 0:f1d3878b8dd9 396 #ifndef __ARMCC_VERSION
vpcola 0:f1d3878b8dd9 397 int sfd = fileno (stream);
vpcola 0:f1d3878b8dd9 398 FSFAT_DBGLOG("(fileno (%s) = %d) %c= %d\n", name, sfd, sfd == fd ? '=' : '!', fd);
vpcola 0:f1d3878b8dd9 399
vpcola 0:f1d3878b8dd9 400 if (sfd == fd) {
vpcola 0:f1d3878b8dd9 401 return true;
vpcola 0:f1d3878b8dd9 402 } else {
vpcola 0:f1d3878b8dd9 403 return false;
vpcola 0:f1d3878b8dd9 404 }
vpcola 0:f1d3878b8dd9 405 #else
vpcola 0:f1d3878b8dd9 406 /* For ARMCC behave as though test had passed. */
vpcola 0:f1d3878b8dd9 407 return true;
vpcola 0:f1d3878b8dd9 408 #endif /* __ARMCC_VERSION */
vpcola 0:f1d3878b8dd9 409 }
vpcola 0:f1d3878b8dd9 410
vpcola 0:f1d3878b8dd9 411 /* defines for next test case */
vpcola 0:f1d3878b8dd9 412 #ifndef STDIN_FILENO
vpcola 0:f1d3878b8dd9 413 #define STDIN_FILENO 0
vpcola 0:f1d3878b8dd9 414 #endif
vpcola 0:f1d3878b8dd9 415
vpcola 0:f1d3878b8dd9 416 #ifndef STDOUT_FILENO
vpcola 0:f1d3878b8dd9 417 #define STDOUT_FILENO 1
vpcola 0:f1d3878b8dd9 418 #endif
vpcola 0:f1d3878b8dd9 419
vpcola 0:f1d3878b8dd9 420 #ifndef STDERR_FILENO
vpcola 0:f1d3878b8dd9 421 #define STDERR_FILENO 2
vpcola 0:f1d3878b8dd9 422 #endif
vpcola 0:f1d3878b8dd9 423
vpcola 0:f1d3878b8dd9 424
vpcola 0:f1d3878b8dd9 425 /** @brief tst-fileno.c test ported from glibc project. See the licence at REF_LICENCE_GLIBC.
vpcola 0:f1d3878b8dd9 426 *
vpcola 0:f1d3878b8dd9 427 * WARNING: this test does not currently work. See WARNING comments below.
vpcola 0:f1d3878b8dd9 428 *
vpcola 0:f1d3878b8dd9 429 *
vpcola 0:f1d3878b8dd9 430 * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors.
vpcola 0:f1d3878b8dd9 431 */
vpcola 0:f1d3878b8dd9 432 static control_t fsfat_basic_test_04()
vpcola 0:f1d3878b8dd9 433 {
vpcola 0:f1d3878b8dd9 434 /* ARMCC stdio.h currently does not define fileno() */
vpcola 0:f1d3878b8dd9 435 #ifndef __ARMCC_VERSION
vpcola 0:f1d3878b8dd9 436 int ret = -1;
vpcola 0:f1d3878b8dd9 437 ret = fsfat_basic_fileno_check("stdin", stdin, STDIN_FILENO);
vpcola 0:f1d3878b8dd9 438 FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: stdin does not have expected file number (expected=%d, fileno=%d.\n", __func__, stdin, fileno(stdin));
vpcola 0:f1d3878b8dd9 439 TEST_ASSERT_MESSAGE(ret == true, fsfat_basic_msg_g);
vpcola 0:f1d3878b8dd9 440
vpcola 0:f1d3878b8dd9 441 ret = fsfat_basic_fileno_check("stdout", stdout, STDOUT_FILENO);
vpcola 0:f1d3878b8dd9 442 FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: stdout does not have expected file number (expected=%d, fileno=%d.\n", __func__, stdout, fileno(stdout));
vpcola 0:f1d3878b8dd9 443 TEST_ASSERT_MESSAGE(ret == true, fsfat_basic_msg_g);
vpcola 0:f1d3878b8dd9 444
vpcola 0:f1d3878b8dd9 445 ret = fsfat_basic_fileno_check("stderr", stderr, STDERR_FILENO);
vpcola 0:f1d3878b8dd9 446 FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: stderr does not have expected file number (expected=%d, fileno=%d.\n", __func__, stderr, fileno(stderr));
vpcola 0:f1d3878b8dd9 447 TEST_ASSERT_MESSAGE(ret == true, fsfat_basic_msg_g);
vpcola 0:f1d3878b8dd9 448 #endif /* __ARMCC_VERSION */
vpcola 0:f1d3878b8dd9 449 return CaseNext;
vpcola 0:f1d3878b8dd9 450 }
vpcola 0:f1d3878b8dd9 451
vpcola 0:f1d3878b8dd9 452
vpcola 0:f1d3878b8dd9 453 /** @brief basic test to opendir() on a directory.
vpcola 0:f1d3878b8dd9 454 *
vpcola 0:f1d3878b8dd9 455 * This test has been ported from armmbed/mbed-os/features/unsupported/tests/mbed/dir_sd/main.cpp.
vpcola 0:f1d3878b8dd9 456 *
vpcola 0:f1d3878b8dd9 457 * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors.
vpcola 0:f1d3878b8dd9 458 */
vpcola 0:f1d3878b8dd9 459 static control_t fsfat_basic_test_05()
vpcola 0:f1d3878b8dd9 460 {
vpcola 0:f1d3878b8dd9 461 FILE *f;
vpcola 0:f1d3878b8dd9 462 char *str = FSFAT_BASIC_TEST_05_TEST_STRING;
vpcola 0:f1d3878b8dd9 463 char *buffer = (char *)malloc(sizeof(unsigned char) * strlen(FSFAT_BASIC_TEST_05_TEST_STRING));
vpcola 0:f1d3878b8dd9 464 int str_len = strlen(FSFAT_BASIC_TEST_05_TEST_STRING);
vpcola 0:f1d3878b8dd9 465 int ret = 0;
vpcola 0:f1d3878b8dd9 466
vpcola 0:f1d3878b8dd9 467 FSFAT_DBGLOG("%s:Write files\n", __func__);
vpcola 0:f1d3878b8dd9 468 char filename[32];
vpcola 0:f1d3878b8dd9 469 for (int i = 0; i < 10; i++) {
vpcola 0:f1d3878b8dd9 470 sprintf(filename, "/sd/test_%d.txt", i);
vpcola 0:f1d3878b8dd9 471 FSFAT_DBGLOG("Creating file: %s\n", filename);
vpcola 0:f1d3878b8dd9 472 f = fopen(filename, "w");
vpcola 0:f1d3878b8dd9 473 FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: fopen() failed.\n", __func__);
vpcola 0:f1d3878b8dd9 474 TEST_ASSERT_MESSAGE(f != NULL, fsfat_basic_msg_g);
vpcola 0:f1d3878b8dd9 475
vpcola 0:f1d3878b8dd9 476 ret = fprintf(f, str);
vpcola 0:f1d3878b8dd9 477 FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: writing file.\n", __func__);
vpcola 0:f1d3878b8dd9 478 TEST_ASSERT_MESSAGE(ret == strlen(str), fsfat_basic_msg_g);
vpcola 0:f1d3878b8dd9 479
vpcola 0:f1d3878b8dd9 480 ret = fclose(f);
vpcola 0:f1d3878b8dd9 481 FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: fclose() failed.\n", __func__);
vpcola 0:f1d3878b8dd9 482 TEST_ASSERT_MESSAGE(ret == 0, fsfat_basic_msg_g);
vpcola 0:f1d3878b8dd9 483 }
vpcola 0:f1d3878b8dd9 484
vpcola 0:f1d3878b8dd9 485 FSFAT_DBGLOG("%s:List files:\n", __func__);
vpcola 0:f1d3878b8dd9 486 DIR *d = opendir("/sd");
vpcola 0:f1d3878b8dd9 487 FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: opendir() failed.\n", __func__);
vpcola 0:f1d3878b8dd9 488 TEST_ASSERT_MESSAGE(d != NULL, fsfat_basic_msg_g);
vpcola 0:f1d3878b8dd9 489
vpcola 0:f1d3878b8dd9 490 struct dirent *p;
vpcola 0:f1d3878b8dd9 491 while ((p = readdir(d)) != NULL)
vpcola 0:f1d3878b8dd9 492 FSFAT_DBGLOG("%s\n", p->d_name);
vpcola 0:f1d3878b8dd9 493 closedir(d);
vpcola 0:f1d3878b8dd9 494
vpcola 0:f1d3878b8dd9 495 return CaseNext;
vpcola 0:f1d3878b8dd9 496 }
vpcola 0:f1d3878b8dd9 497
vpcola 0:f1d3878b8dd9 498
vpcola 0:f1d3878b8dd9 499 /** @brief basic test to write a file to sd card, and read it back again
vpcola 0:f1d3878b8dd9 500 *
vpcola 0:f1d3878b8dd9 501 * This test has been ported from armmbed/mbed-os/features/unsupported/tests/mbed/file/main.cpp.
vpcola 0:f1d3878b8dd9 502 *
vpcola 0:f1d3878b8dd9 503 * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors.
vpcola 0:f1d3878b8dd9 504 */
vpcola 0:f1d3878b8dd9 505 static control_t fsfat_basic_test_06()
vpcola 0:f1d3878b8dd9 506 {
vpcola 0:f1d3878b8dd9 507 int ret = -1;
vpcola 0:f1d3878b8dd9 508 char mac[16];
vpcola 0:f1d3878b8dd9 509 mbed_mac_address(mac);
vpcola 0:f1d3878b8dd9 510 FSFAT_DBGLOG("mac address: %02x,%02x,%02x,%02x,%02x,%02x\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
vpcola 0:f1d3878b8dd9 511
vpcola 0:f1d3878b8dd9 512 FILE *f;
vpcola 0:f1d3878b8dd9 513 char *str = FSFAT_BASIC_TEST_05_TEST_STRING;
vpcola 0:f1d3878b8dd9 514 char *buffer = (char *)malloc(sizeof(unsigned char) * strlen(FSFAT_BASIC_TEST_05_TEST_STRING));
vpcola 0:f1d3878b8dd9 515 int str_len = strlen(FSFAT_BASIC_TEST_05_TEST_STRING);
vpcola 0:f1d3878b8dd9 516
vpcola 0:f1d3878b8dd9 517 f = fopen(sd_file_path, "w");
vpcola 0:f1d3878b8dd9 518 FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: fopen() failed.\n", __func__);
vpcola 0:f1d3878b8dd9 519 TEST_ASSERT_MESSAGE(f != NULL, fsfat_basic_msg_g);
vpcola 0:f1d3878b8dd9 520
vpcola 0:f1d3878b8dd9 521 ret = fprintf(f, str);
vpcola 0:f1d3878b8dd9 522 FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: writing file.\n", __func__);
vpcola 0:f1d3878b8dd9 523 TEST_ASSERT_MESSAGE(ret == strlen(str), fsfat_basic_msg_g);
vpcola 0:f1d3878b8dd9 524
vpcola 0:f1d3878b8dd9 525 ret = fclose(f);
vpcola 0:f1d3878b8dd9 526 FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: fclose() failed.\n", __func__);
vpcola 0:f1d3878b8dd9 527 TEST_ASSERT_MESSAGE(ret == 0, fsfat_basic_msg_g);
vpcola 0:f1d3878b8dd9 528
vpcola 0:f1d3878b8dd9 529 // Read
vpcola 0:f1d3878b8dd9 530 f = fopen(sd_file_path, "r");
vpcola 0:f1d3878b8dd9 531 FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: fopen() failed.\n", __func__);
vpcola 0:f1d3878b8dd9 532 TEST_ASSERT_MESSAGE(f != NULL, fsfat_basic_msg_g);
vpcola 0:f1d3878b8dd9 533
vpcola 0:f1d3878b8dd9 534 int n = fread(fsfat_basic_buffer, sizeof(unsigned char), str_len, f);
vpcola 0:f1d3878b8dd9 535 FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: fread() failed.\n", __func__);
vpcola 0:f1d3878b8dd9 536 TEST_ASSERT_MESSAGE(n == str_len, fsfat_basic_msg_g);
vpcola 0:f1d3878b8dd9 537
vpcola 0:f1d3878b8dd9 538 ret = fclose(f);
vpcola 0:f1d3878b8dd9 539 FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: fclose() failed.\n", __func__);
vpcola 0:f1d3878b8dd9 540 TEST_ASSERT_MESSAGE(ret == 0, fsfat_basic_msg_g);
vpcola 0:f1d3878b8dd9 541
vpcola 0:f1d3878b8dd9 542 return CaseNext;
vpcola 0:f1d3878b8dd9 543 }
vpcola 0:f1d3878b8dd9 544
vpcola 0:f1d3878b8dd9 545
vpcola 0:f1d3878b8dd9 546 /** @brief basic test to write a file to sd card.
vpcola 0:f1d3878b8dd9 547 *
vpcola 0:f1d3878b8dd9 548 * This test has been ported from armmbed/mbed-os/features/unsupported/tests/mbed/sd/main.cpp.
vpcola 0:f1d3878b8dd9 549 *
vpcola 0:f1d3878b8dd9 550 * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors.
vpcola 0:f1d3878b8dd9 551 */
vpcola 0:f1d3878b8dd9 552 static control_t fsfat_basic_test_07()
vpcola 0:f1d3878b8dd9 553 {
vpcola 0:f1d3878b8dd9 554 uint8_t data_written[FSFAT_BASIC_DATA_SIZE] = { 0 };
vpcola 0:f1d3878b8dd9 555
vpcola 0:f1d3878b8dd9 556 // Fill data_written buffer with random data
vpcola 0:f1d3878b8dd9 557 // Write these data into the file
vpcola 0:f1d3878b8dd9 558 bool write_result = false;
vpcola 0:f1d3878b8dd9 559 {
vpcola 0:f1d3878b8dd9 560 FSFAT_DBGLOG("%s:SD: Writing ... ", __func__);
vpcola 0:f1d3878b8dd9 561 FILE *f = fopen(sd_file_path, "w");
vpcola 0:f1d3878b8dd9 562 if (f) {
vpcola 0:f1d3878b8dd9 563 for (int i = 0; i < FSFAT_BASIC_DATA_SIZE; i++) {
vpcola 0:f1d3878b8dd9 564 data_written[i] = rand() % 0XFF;
vpcola 0:f1d3878b8dd9 565 fprintf(f, "%c", data_written[i]);
vpcola 0:f1d3878b8dd9 566 }
vpcola 0:f1d3878b8dd9 567 write_result = true;
vpcola 0:f1d3878b8dd9 568 fclose(f);
vpcola 0:f1d3878b8dd9 569 }
vpcola 0:f1d3878b8dd9 570 FSFAT_DBGLOG("[%s]\n", write_result ? "OK" : "FAIL");
vpcola 0:f1d3878b8dd9 571 FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: unexpected write failure.\n", __func__);
vpcola 0:f1d3878b8dd9 572 TEST_ASSERT_MESSAGE(write_result == true, fsfat_basic_msg_g);
vpcola 0:f1d3878b8dd9 573 }
vpcola 0:f1d3878b8dd9 574
vpcola 0:f1d3878b8dd9 575 // Read back the data from the file and store them in data_read
vpcola 0:f1d3878b8dd9 576 bool read_result = false;
vpcola 0:f1d3878b8dd9 577 {
vpcola 0:f1d3878b8dd9 578 FSFAT_DBGLOG("%s:SD: Reading data ... ", __func__);
vpcola 0:f1d3878b8dd9 579 FILE *f = fopen(sd_file_path, "r");
vpcola 0:f1d3878b8dd9 580 if (f) {
vpcola 0:f1d3878b8dd9 581 read_result = true;
vpcola 0:f1d3878b8dd9 582 for (int i = 0; i < FSFAT_BASIC_DATA_SIZE; i++) {
vpcola 0:f1d3878b8dd9 583 uint8_t data = fgetc(f);
vpcola 0:f1d3878b8dd9 584 if (data != data_written[i]) {
vpcola 0:f1d3878b8dd9 585 read_result = false;
vpcola 0:f1d3878b8dd9 586 break;
vpcola 0:f1d3878b8dd9 587 }
vpcola 0:f1d3878b8dd9 588 }
vpcola 0:f1d3878b8dd9 589 fclose(f);
vpcola 0:f1d3878b8dd9 590 }
vpcola 0:f1d3878b8dd9 591 FSFAT_DBGLOG("[%s]\n", read_result ? "OK" : "FAIL");
vpcola 0:f1d3878b8dd9 592 FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: unexpected read failure.\n", __func__);
vpcola 0:f1d3878b8dd9 593 TEST_ASSERT_MESSAGE(read_result == true, fsfat_basic_msg_g);
vpcola 0:f1d3878b8dd9 594 }
vpcola 0:f1d3878b8dd9 595 return CaseNext;
vpcola 0:f1d3878b8dd9 596 }
vpcola 0:f1d3878b8dd9 597
vpcola 0:f1d3878b8dd9 598
vpcola 0:f1d3878b8dd9 599 static bool fsfat_basic_test_file_write_fhandle(const char *filename, const int kib_rw)
vpcola 0:f1d3878b8dd9 600 {
vpcola 0:f1d3878b8dd9 601 int ret = -1;
vpcola 0:f1d3878b8dd9 602 File file;
vpcola 0:f1d3878b8dd9 603
vpcola 0:f1d3878b8dd9 604 ret = file.open(&fs, filename, O_WRONLY | O_CREAT | O_TRUNC);
vpcola 0:f1d3878b8dd9 605 FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: failed to open file.\n", __func__);
vpcola 0:f1d3878b8dd9 606 TEST_ASSERT_MESSAGE(ret == 0, fsfat_basic_msg_g);
vpcola 0:f1d3878b8dd9 607
vpcola 0:f1d3878b8dd9 608 int byte_write = 0;
vpcola 0:f1d3878b8dd9 609 fsfat_basic_timer.start();
vpcola 0:f1d3878b8dd9 610 for (int i = 0; i < kib_rw; i++) {
vpcola 0:f1d3878b8dd9 611 ret = file.write(fsfat_basic_buffer, sizeof(fsfat_basic_buffer));
vpcola 0:f1d3878b8dd9 612 FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: failed to write to file.\n", __func__);
vpcola 0:f1d3878b8dd9 613 TEST_ASSERT_MESSAGE(ret == sizeof(fsfat_basic_buffer), fsfat_basic_msg_g);
vpcola 0:f1d3878b8dd9 614 byte_write++;
vpcola 0:f1d3878b8dd9 615 }
vpcola 0:f1d3878b8dd9 616 fsfat_basic_timer.stop();
vpcola 0:f1d3878b8dd9 617 file.close();
vpcola 0:f1d3878b8dd9 618 double test_time_sec = fsfat_basic_timer.read_us() / 1000000.0;
vpcola 0:f1d3878b8dd9 619 double speed = kib_rw / test_time_sec;
vpcola 0:f1d3878b8dd9 620 FSFAT_DBGLOG("%d KiB write in %.3f sec with speed of %.4f KiB/s\n", byte_write, test_time_sec, speed);
vpcola 0:f1d3878b8dd9 621 fsfat_basic_timer.reset();
vpcola 0:f1d3878b8dd9 622 return true;
vpcola 0:f1d3878b8dd9 623 }
vpcola 0:f1d3878b8dd9 624
vpcola 0:f1d3878b8dd9 625
vpcola 0:f1d3878b8dd9 626 static bool fsfat_basic_test_file_read_fhandle(const char *filename, const int kib_rw)
vpcola 0:f1d3878b8dd9 627 {
vpcola 0:f1d3878b8dd9 628 int ret = -1;
vpcola 0:f1d3878b8dd9 629 File file;
vpcola 0:f1d3878b8dd9 630 ret = file.open(&fs, filename, O_RDONLY);
vpcola 0:f1d3878b8dd9 631
vpcola 0:f1d3878b8dd9 632 FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: failed to open file.\n", __func__);
vpcola 0:f1d3878b8dd9 633 TEST_ASSERT_MESSAGE(ret == 0, fsfat_basic_msg_g);
vpcola 0:f1d3878b8dd9 634
vpcola 0:f1d3878b8dd9 635 fsfat_basic_timer.start();
vpcola 0:f1d3878b8dd9 636 int byte_read = 0;
vpcola 0:f1d3878b8dd9 637 while (file.read(fsfat_basic_buffer, sizeof(fsfat_basic_buffer)) == sizeof(fsfat_basic_buffer)) {
vpcola 0:f1d3878b8dd9 638 byte_read++;
vpcola 0:f1d3878b8dd9 639 }
vpcola 0:f1d3878b8dd9 640 fsfat_basic_timer.stop();
vpcola 0:f1d3878b8dd9 641 file.close();
vpcola 0:f1d3878b8dd9 642 double test_time_sec = fsfat_basic_timer.read_us() / 1000000.0;
vpcola 0:f1d3878b8dd9 643 double speed = kib_rw / test_time_sec;
vpcola 0:f1d3878b8dd9 644 FSFAT_DBGLOG("%d KiB read in %.3f sec with speed of %.4f KiB/s\n", byte_read, test_time_sec, speed);
vpcola 0:f1d3878b8dd9 645 fsfat_basic_timer.reset();
vpcola 0:f1d3878b8dd9 646 return true;
vpcola 0:f1d3878b8dd9 647 }
vpcola 0:f1d3878b8dd9 648
vpcola 0:f1d3878b8dd9 649
vpcola 0:f1d3878b8dd9 650 static char fsfat_basic_test_random_char()
vpcola 0:f1d3878b8dd9 651 {
vpcola 0:f1d3878b8dd9 652 return rand() % 100;
vpcola 0:f1d3878b8dd9 653 }
vpcola 0:f1d3878b8dd9 654
vpcola 0:f1d3878b8dd9 655
vpcola 0:f1d3878b8dd9 656 /** @brief basic sd card performance test
vpcola 0:f1d3878b8dd9 657 *
vpcola 0:f1d3878b8dd9 658 * This test has been ported from armmbed/mbed-os/features/unsupported/tests/mbed/sd_perf_handle/main.cpp.
vpcola 0:f1d3878b8dd9 659 *
vpcola 0:f1d3878b8dd9 660 * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors.
vpcola 0:f1d3878b8dd9 661 */
vpcola 0:f1d3878b8dd9 662 static control_t fsfat_basic_test_08()
vpcola 0:f1d3878b8dd9 663 {
vpcola 0:f1d3878b8dd9 664 // Test header
vpcola 0:f1d3878b8dd9 665 FSFAT_DBGLOG("\n%s:SD Card FileHandle Performance Test\n", __func__);
vpcola 0:f1d3878b8dd9 666 FSFAT_DBGLOG("File name: %s\n", fsfat_basic_bin_filename);
vpcola 0:f1d3878b8dd9 667 FSFAT_DBGLOG("Buffer size: %d KiB\n", (FSFAT_BASIC_KIB_RW * sizeof(fsfat_basic_buffer)) / 1024);
vpcola 0:f1d3878b8dd9 668
vpcola 0:f1d3878b8dd9 669 // Initialize buffer
vpcola 0:f1d3878b8dd9 670 srand(0);
vpcola 0:f1d3878b8dd9 671 char *buffer_end = fsfat_basic_buffer + sizeof(fsfat_basic_buffer);
vpcola 0:f1d3878b8dd9 672 std::generate (fsfat_basic_buffer, buffer_end, fsfat_basic_test_random_char);
vpcola 0:f1d3878b8dd9 673
vpcola 0:f1d3878b8dd9 674 bool result = true;
vpcola 0:f1d3878b8dd9 675 for (;;) {
vpcola 0:f1d3878b8dd9 676 FSFAT_DBGLOG("%s:Write test...\n", __func__);
vpcola 0:f1d3878b8dd9 677 if (fsfat_basic_test_file_write_fhandle(fsfat_basic_bin_filename_test_08, FSFAT_BASIC_KIB_RW) == false) {
vpcola 0:f1d3878b8dd9 678 result = false;
vpcola 0:f1d3878b8dd9 679 break;
vpcola 0:f1d3878b8dd9 680 }
vpcola 0:f1d3878b8dd9 681
vpcola 0:f1d3878b8dd9 682 FSFAT_DBGLOG("%s:Read test...\n", __func__);
vpcola 0:f1d3878b8dd9 683 if (fsfat_basic_test_file_read_fhandle(fsfat_basic_bin_filename_test_08, FSFAT_BASIC_KIB_RW) == false) {
vpcola 0:f1d3878b8dd9 684 result = false;
vpcola 0:f1d3878b8dd9 685 break;
vpcola 0:f1d3878b8dd9 686 }
vpcola 0:f1d3878b8dd9 687 break;
vpcola 0:f1d3878b8dd9 688 }
vpcola 0:f1d3878b8dd9 689 return CaseNext;
vpcola 0:f1d3878b8dd9 690 }
vpcola 0:f1d3878b8dd9 691
vpcola 0:f1d3878b8dd9 692
vpcola 0:f1d3878b8dd9 693 bool fsfat_basic_test_sf_file_write_stdio(const char *filename, const int kib_rw)
vpcola 0:f1d3878b8dd9 694 {
vpcola 0:f1d3878b8dd9 695 int ret = -1;
vpcola 0:f1d3878b8dd9 696 FILE* file = fopen(filename, "w");
vpcola 0:f1d3878b8dd9 697
vpcola 0:f1d3878b8dd9 698 FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: failed to open file.\n", __func__);
vpcola 0:f1d3878b8dd9 699 TEST_ASSERT_MESSAGE(file != NULL, fsfat_basic_msg_g);
vpcola 0:f1d3878b8dd9 700
vpcola 0:f1d3878b8dd9 701 int byte_write = 0;
vpcola 0:f1d3878b8dd9 702 fsfat_basic_timer.start();
vpcola 0:f1d3878b8dd9 703 for (int i = 0; i < kib_rw; i++) {
vpcola 0:f1d3878b8dd9 704 ret = fwrite(fsfat_basic_buffer, sizeof(char), sizeof(fsfat_basic_buffer), file);
vpcola 0:f1d3878b8dd9 705 FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: failed to write to file.\n", __func__);
vpcola 0:f1d3878b8dd9 706 TEST_ASSERT_MESSAGE(ret == sizeof(fsfat_basic_buffer), fsfat_basic_msg_g);
vpcola 0:f1d3878b8dd9 707 byte_write++;
vpcola 0:f1d3878b8dd9 708 }
vpcola 0:f1d3878b8dd9 709 fsfat_basic_timer.stop();
vpcola 0:f1d3878b8dd9 710 fclose(file);
vpcola 0:f1d3878b8dd9 711 double test_time_sec = fsfat_basic_timer.read_us() / 1000000.0;
vpcola 0:f1d3878b8dd9 712 double speed = kib_rw / test_time_sec;
vpcola 0:f1d3878b8dd9 713 FSFAT_DBGLOG("%d KiB write in %.3f sec with speed of %.4f KiB/s\n", byte_write, test_time_sec, speed);
vpcola 0:f1d3878b8dd9 714 fsfat_basic_timer.reset();
vpcola 0:f1d3878b8dd9 715 return true;
vpcola 0:f1d3878b8dd9 716 }
vpcola 0:f1d3878b8dd9 717
vpcola 0:f1d3878b8dd9 718
vpcola 0:f1d3878b8dd9 719 bool fsfat_basic_test_sf_file_read_stdio(const char *filename, const int kib_rw)
vpcola 0:f1d3878b8dd9 720 {
vpcola 0:f1d3878b8dd9 721 bool result = true;
vpcola 0:f1d3878b8dd9 722 FILE* file = fopen(filename, "r");
vpcola 0:f1d3878b8dd9 723
vpcola 0:f1d3878b8dd9 724 FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: failed to open file.\n", __func__);
vpcola 0:f1d3878b8dd9 725 TEST_ASSERT_MESSAGE(file != NULL, fsfat_basic_msg_g);
vpcola 0:f1d3878b8dd9 726 fsfat_basic_timer.start();
vpcola 0:f1d3878b8dd9 727 int byte_read = 0;
vpcola 0:f1d3878b8dd9 728 while (fread(fsfat_basic_buffer, sizeof(char), sizeof(fsfat_basic_buffer), file) == sizeof(fsfat_basic_buffer)) {
vpcola 0:f1d3878b8dd9 729 byte_read++;
vpcola 0:f1d3878b8dd9 730 }
vpcola 0:f1d3878b8dd9 731 fsfat_basic_timer.stop();
vpcola 0:f1d3878b8dd9 732 fclose(file);
vpcola 0:f1d3878b8dd9 733 double test_time_sec = fsfat_basic_timer.read_us() / 1000000.0;
vpcola 0:f1d3878b8dd9 734 double speed = kib_rw / test_time_sec;
vpcola 0:f1d3878b8dd9 735 FSFAT_DBGLOG("%d KiB read in %.3f sec with speed of %.4f KiB/s\n", byte_read, test_time_sec, speed);
vpcola 0:f1d3878b8dd9 736
vpcola 0:f1d3878b8dd9 737 fsfat_basic_timer.reset();
vpcola 0:f1d3878b8dd9 738 return true;
vpcola 0:f1d3878b8dd9 739 }
vpcola 0:f1d3878b8dd9 740
vpcola 0:f1d3878b8dd9 741
vpcola 0:f1d3878b8dd9 742 /** @brief basic test to write a file to sd card.
vpcola 0:f1d3878b8dd9 743 *
vpcola 0:f1d3878b8dd9 744 * This test has been ported from armmbed/mbed-os/features/unsupported/tests/mbed/sd_perf_stdio/main.cpp.
vpcola 0:f1d3878b8dd9 745 *
vpcola 0:f1d3878b8dd9 746 * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors.
vpcola 0:f1d3878b8dd9 747 */
vpcola 0:f1d3878b8dd9 748 static control_t fsfat_basic_test_09()
vpcola 0:f1d3878b8dd9 749 {
vpcola 0:f1d3878b8dd9 750 // Test header
vpcola 0:f1d3878b8dd9 751 FSFAT_DBGLOG("\n%s:SD Card Stdio Performance Test\n", __func__);
vpcola 0:f1d3878b8dd9 752 FSFAT_DBGLOG("File name: %s\n", fsfat_basic_bin_filename);
vpcola 0:f1d3878b8dd9 753 FSFAT_DBGLOG("Buffer size: %d KiB\n", (FSFAT_BASIC_KIB_RW * sizeof(fsfat_basic_buffer)) / 1024);
vpcola 0:f1d3878b8dd9 754
vpcola 0:f1d3878b8dd9 755 // Initialize buffer
vpcola 0:f1d3878b8dd9 756 srand(0);
vpcola 0:f1d3878b8dd9 757 char *buffer_end = fsfat_basic_buffer + sizeof(fsfat_basic_buffer);
vpcola 0:f1d3878b8dd9 758 std::generate (fsfat_basic_buffer, buffer_end, fsfat_basic_test_random_char);
vpcola 0:f1d3878b8dd9 759
vpcola 0:f1d3878b8dd9 760 bool result = true;
vpcola 0:f1d3878b8dd9 761 for (;;) {
vpcola 0:f1d3878b8dd9 762 FSFAT_DBGLOG("%s:Write test...\n", __func__);
vpcola 0:f1d3878b8dd9 763 if (fsfat_basic_test_sf_file_write_stdio(fsfat_basic_bin_filename, FSFAT_BASIC_KIB_RW) == false) {
vpcola 0:f1d3878b8dd9 764 result = false;
vpcola 0:f1d3878b8dd9 765 break;
vpcola 0:f1d3878b8dd9 766 }
vpcola 0:f1d3878b8dd9 767
vpcola 0:f1d3878b8dd9 768 FSFAT_DBGLOG("%s:Read test...\n", __func__);
vpcola 0:f1d3878b8dd9 769 if (fsfat_basic_test_sf_file_read_stdio(fsfat_basic_bin_filename, FSFAT_BASIC_KIB_RW) == false) {
vpcola 0:f1d3878b8dd9 770 result = false;
vpcola 0:f1d3878b8dd9 771 break;
vpcola 0:f1d3878b8dd9 772 }
vpcola 0:f1d3878b8dd9 773 break;
vpcola 0:f1d3878b8dd9 774 }
vpcola 0:f1d3878b8dd9 775 return CaseNext;
vpcola 0:f1d3878b8dd9 776 }
vpcola 0:f1d3878b8dd9 777
vpcola 0:f1d3878b8dd9 778
vpcola 0:f1d3878b8dd9 779 bool fsfat_basic_test_file_write_fatfs(const char *filename, const int kib_rw)
vpcola 0:f1d3878b8dd9 780 {
vpcola 0:f1d3878b8dd9 781 FIL file;
vpcola 0:f1d3878b8dd9 782 FRESULT res = f_open(&file, filename, FA_WRITE | FA_CREATE_ALWAYS);
vpcola 0:f1d3878b8dd9 783
vpcola 0:f1d3878b8dd9 784 FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: failed to open file.\n", __func__);
vpcola 0:f1d3878b8dd9 785 TEST_ASSERT_MESSAGE(res == FR_OK, fsfat_basic_msg_g);
vpcola 0:f1d3878b8dd9 786
vpcola 0:f1d3878b8dd9 787 int byte_write = 0;
vpcola 0:f1d3878b8dd9 788 unsigned int bytes = 0;
vpcola 0:f1d3878b8dd9 789 fsfat_basic_timer.start();
vpcola 0:f1d3878b8dd9 790 for (int i = 0; i < kib_rw; i++) {
vpcola 0:f1d3878b8dd9 791 res = f_write(&file, fsfat_basic_buffer, sizeof(fsfat_basic_buffer), &bytes);
vpcola 0:f1d3878b8dd9 792 FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: failed to write to file.\n", __func__);
vpcola 0:f1d3878b8dd9 793 TEST_ASSERT_MESSAGE(res == FR_OK, fsfat_basic_msg_g);
vpcola 0:f1d3878b8dd9 794 byte_write++;
vpcola 0:f1d3878b8dd9 795 }
vpcola 0:f1d3878b8dd9 796 fsfat_basic_timer.stop();
vpcola 0:f1d3878b8dd9 797 f_close(&file);
vpcola 0:f1d3878b8dd9 798 double test_time_sec = fsfat_basic_timer.read_us() / 1000000.0;
vpcola 0:f1d3878b8dd9 799 double speed = kib_rw / test_time_sec;
vpcola 0:f1d3878b8dd9 800 FSFAT_DBGLOG("%d KiB write in %.3f sec with speed of %.4f KiB/s\n", byte_write, test_time_sec, speed);
vpcola 0:f1d3878b8dd9 801 fsfat_basic_timer.reset();
vpcola 0:f1d3878b8dd9 802 return true;
vpcola 0:f1d3878b8dd9 803 }
vpcola 0:f1d3878b8dd9 804
vpcola 0:f1d3878b8dd9 805 bool fsfat_basic_test_file_read_fatfs(const char *filename, const int kib_rw)
vpcola 0:f1d3878b8dd9 806 {
vpcola 0:f1d3878b8dd9 807 FIL file;
vpcola 0:f1d3878b8dd9 808 FRESULT res = f_open(&file, filename, FA_READ | FA_OPEN_EXISTING);
vpcola 0:f1d3878b8dd9 809
vpcola 0:f1d3878b8dd9 810 FSFAT_BASIC_MSG(fsfat_basic_msg_g, FSFAT_BASIC_MSG_BUF_SIZE, "%s: Error: failed to open file.\n", __func__);
vpcola 0:f1d3878b8dd9 811 TEST_ASSERT_MESSAGE(res == FR_OK, fsfat_basic_msg_g);
vpcola 0:f1d3878b8dd9 812
vpcola 0:f1d3878b8dd9 813 fsfat_basic_timer.start();
vpcola 0:f1d3878b8dd9 814 int byte_read = 0;
vpcola 0:f1d3878b8dd9 815 unsigned int bytes = 0;
vpcola 0:f1d3878b8dd9 816 do {
vpcola 0:f1d3878b8dd9 817 res = f_read(&file, fsfat_basic_buffer, sizeof(fsfat_basic_buffer), &bytes);
vpcola 0:f1d3878b8dd9 818 byte_read++;
vpcola 0:f1d3878b8dd9 819 } while (res == FR_OK && bytes == sizeof(fsfat_basic_buffer));
vpcola 0:f1d3878b8dd9 820 fsfat_basic_timer.stop();
vpcola 0:f1d3878b8dd9 821 f_close(&file);
vpcola 0:f1d3878b8dd9 822 double test_time_sec = fsfat_basic_timer.read_us() / 1000000.0;
vpcola 0:f1d3878b8dd9 823 double speed = kib_rw / test_time_sec;
vpcola 0:f1d3878b8dd9 824 FSFAT_DBGLOG("%d KiB read in %.3f sec with speed of %.4f KiB/s\n", byte_read, test_time_sec, speed);
vpcola 0:f1d3878b8dd9 825 fsfat_basic_timer.reset();
vpcola 0:f1d3878b8dd9 826 return true;
vpcola 0:f1d3878b8dd9 827 }
vpcola 0:f1d3878b8dd9 828
vpcola 0:f1d3878b8dd9 829 /** @brief basic test to write a file to sd card.
vpcola 0:f1d3878b8dd9 830 *
vpcola 0:f1d3878b8dd9 831 * This test has been ported from armmbed/mbed-os/features/unsupported/tests/mbed/sd_perf_stdio/main.cpp.
vpcola 0:f1d3878b8dd9 832 *
vpcola 0:f1d3878b8dd9 833 * @return on success returns CaseNext to continue to next test case, otherwise will assert on errors.
vpcola 0:f1d3878b8dd9 834 */
vpcola 0:f1d3878b8dd9 835 static control_t fsfat_basic_test_10()
vpcola 0:f1d3878b8dd9 836 {
vpcola 0:f1d3878b8dd9 837 // Test header
vpcola 0:f1d3878b8dd9 838 FSFAT_DBGLOG("\n%sSD Card FatFS Performance Test\n", __func__);
vpcola 0:f1d3878b8dd9 839 FSFAT_DBGLOG("File name: %s\n", fsfat_basic_bin_filename_test_10);
vpcola 0:f1d3878b8dd9 840 FSFAT_DBGLOG("Buffer size: %d KiB\n", (FSFAT_BASIC_KIB_RW * sizeof(fsfat_basic_buffer)) / 1024);
vpcola 0:f1d3878b8dd9 841
vpcola 0:f1d3878b8dd9 842 // Initialize buffer
vpcola 0:f1d3878b8dd9 843 srand(1);
vpcola 0:f1d3878b8dd9 844 char *buffer_end = fsfat_basic_buffer + sizeof(fsfat_basic_buffer);
vpcola 0:f1d3878b8dd9 845 std::generate (fsfat_basic_buffer, buffer_end, fsfat_basic_test_random_char);
vpcola 0:f1d3878b8dd9 846
vpcola 0:f1d3878b8dd9 847 bool result = true;
vpcola 0:f1d3878b8dd9 848 for (;;) {
vpcola 0:f1d3878b8dd9 849 FSFAT_DBGLOG("%s:Write test...\n", __func__);
vpcola 0:f1d3878b8dd9 850 if (fsfat_basic_test_file_write_fatfs(fsfat_basic_bin_filename_test_10, FSFAT_BASIC_KIB_RW) == false) {
vpcola 0:f1d3878b8dd9 851 result = false;
vpcola 0:f1d3878b8dd9 852 break;
vpcola 0:f1d3878b8dd9 853 }
vpcola 0:f1d3878b8dd9 854
vpcola 0:f1d3878b8dd9 855 FSFAT_DBGLOG("%s:Read test...\n", __func__);
vpcola 0:f1d3878b8dd9 856 if (fsfat_basic_test_file_read_fatfs(fsfat_basic_bin_filename_test_10, FSFAT_BASIC_KIB_RW) == false) {
vpcola 0:f1d3878b8dd9 857 result = false;
vpcola 0:f1d3878b8dd9 858 break;
vpcola 0:f1d3878b8dd9 859 }
vpcola 0:f1d3878b8dd9 860 break;
vpcola 0:f1d3878b8dd9 861 }
vpcola 0:f1d3878b8dd9 862 return CaseNext;
vpcola 0:f1d3878b8dd9 863 }
vpcola 0:f1d3878b8dd9 864
vpcola 0:f1d3878b8dd9 865 #else
vpcola 0:f1d3878b8dd9 866
vpcola 0:f1d3878b8dd9 867 #define FSFAT_BASIC_TEST_00 fsfat_basic_test_dummy
vpcola 0:f1d3878b8dd9 868 #define FSFAT_BASIC_TEST_01 fsfat_basic_test_dummy
vpcola 0:f1d3878b8dd9 869 #define FSFAT_BASIC_TEST_02 fsfat_basic_test_dummy
vpcola 0:f1d3878b8dd9 870 #define FSFAT_BASIC_TEST_03 fsfat_basic_test_dummy
vpcola 0:f1d3878b8dd9 871 #define FSFAT_BASIC_TEST_04 fsfat_basic_test_dummy
vpcola 0:f1d3878b8dd9 872 #define FSFAT_BASIC_TEST_05 fsfat_basic_test_dummy
vpcola 0:f1d3878b8dd9 873 #define FSFAT_BASIC_TEST_06 fsfat_basic_test_dummy
vpcola 0:f1d3878b8dd9 874 #define FSFAT_BASIC_TEST_07 fsfat_basic_test_dummy
vpcola 0:f1d3878b8dd9 875 #define FSFAT_BASIC_TEST_08 fsfat_basic_test_dummy
vpcola 0:f1d3878b8dd9 876 #define FSFAT_BASIC_TEST_09 fsfat_basic_test_dummy
vpcola 0:f1d3878b8dd9 877 #define FSFAT_BASIC_TEST_10 fsfat_basic_test_dummy
vpcola 0:f1d3878b8dd9 878
vpcola 0:f1d3878b8dd9 879
vpcola 0:f1d3878b8dd9 880 /** @brief fsfat_basic_test_dummy Dummy test case for testing when platform doesnt have an SDCard installed.
vpcola 0:f1d3878b8dd9 881 *
vpcola 0:f1d3878b8dd9 882 * @return success always
vpcola 0:f1d3878b8dd9 883 */
vpcola 0:f1d3878b8dd9 884 static control_t fsfat_basic_test_dummy()
vpcola 0:f1d3878b8dd9 885 {
vpcola 0:f1d3878b8dd9 886 printf("Null test\n");
vpcola 0:f1d3878b8dd9 887 return CaseNext;
vpcola 0:f1d3878b8dd9 888 }
vpcola 0:f1d3878b8dd9 889
vpcola 0:f1d3878b8dd9 890 #endif /* defined(DEVICE_SPI) && defined(MBED_CONF_APP_FSFAT_SDCARD_INSTALLED) */
vpcola 0:f1d3878b8dd9 891
vpcola 0:f1d3878b8dd9 892 utest::v1::status_t greentea_setup(const size_t number_of_cases)
vpcola 0:f1d3878b8dd9 893 {
vpcola 0:f1d3878b8dd9 894 GREENTEA_SETUP(300, "default_auto");
vpcola 0:f1d3878b8dd9 895 return greentea_test_setup_handler(number_of_cases);
vpcola 0:f1d3878b8dd9 896 }
vpcola 0:f1d3878b8dd9 897
vpcola 0:f1d3878b8dd9 898
vpcola 0:f1d3878b8dd9 899 Case cases[] = {
vpcola 0:f1d3878b8dd9 900 /* 1 2 3 4 5 6 7 */
vpcola 0:f1d3878b8dd9 901 /* 1234567890123456789012345678901234567890123456789012345678901234567890 */
vpcola 0:f1d3878b8dd9 902 Case("FSFAT_BASIC_TEST_00: fopen()/fgetc()/fprintf()/fclose() test.", FSFAT_BASIC_TEST_00),
vpcola 0:f1d3878b8dd9 903 Case("FSFAT_BASIC_TEST_01: fopen()/fseek()/fclose() test.", FSFAT_BASIC_TEST_01),
vpcola 0:f1d3878b8dd9 904 /* WARNING: Test case not working but currently not required for PAL support
vpcola 0:f1d3878b8dd9 905 * Case("FSFAT_BASIC_TEST_02: fopen()/fgets()/fputs()/ftell()/rewind()/remove() test.", FSFAT_BASIC_TEST_02) */
vpcola 0:f1d3878b8dd9 906 Case("FSFAT_BASIC_TEST_03: tmpnam() test.", FSFAT_BASIC_TEST_03),
vpcola 0:f1d3878b8dd9 907 Case("FSFAT_BASIC_TEST_04: fileno() test.", FSFAT_BASIC_TEST_04),
vpcola 0:f1d3878b8dd9 908 Case("FSFAT_BASIC_TEST_05: opendir() basic test.", FSFAT_BASIC_TEST_05),
vpcola 0:f1d3878b8dd9 909 Case("FSFAT_BASIC_TEST_06: fread()/fwrite() file to sdcard.", FSFAT_BASIC_TEST_06),
vpcola 0:f1d3878b8dd9 910 Case("FSFAT_BASIC_TEST_07: sdcard fwrite() file test.", FSFAT_BASIC_TEST_07),
vpcola 0:f1d3878b8dd9 911 Case("FSFAT_BASIC_TEST_08: FATFileSystem::read()/write() test.", FSFAT_BASIC_TEST_08),
vpcola 0:f1d3878b8dd9 912 Case("FSFAT_BASIC_TEST_09: POSIX FILE API fread()/fwrite() test.", FSFAT_BASIC_TEST_09),
vpcola 0:f1d3878b8dd9 913 Case("FSFAT_BASIC_TEST_10: ChanFS read()/write()) test.", FSFAT_BASIC_TEST_10),
vpcola 0:f1d3878b8dd9 914 };
vpcola 0:f1d3878b8dd9 915
vpcola 0:f1d3878b8dd9 916
vpcola 0:f1d3878b8dd9 917 /* Declare your test specification with a custom setup handler */
vpcola 0:f1d3878b8dd9 918 Specification specification(greentea_setup, cases);
vpcola 0:f1d3878b8dd9 919
vpcola 0:f1d3878b8dd9 920 int main()
vpcola 0:f1d3878b8dd9 921 {
vpcola 0:f1d3878b8dd9 922 return !Harness::run(specification);
vpcola 0:f1d3878b8dd9 923 }