Fork of my original MQTTGateway

Dependencies:   mbed-http

Committer:
vpcola
Date:
Sat Apr 08 14:43:14 2017 +0000
Revision:
0:a1734fe1ec4b
Initial commit

Who changed what in which revision?

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