ublox-cellular-driver-gen Fork
Fork of ublox-cellular-driver-gen by
TESTS/unit_tests/file-system/main.cpp@6:804d544f8d8c, 2017-06-15 (annotated)
- Committer:
- rob.meades@u-blox.com
- Date:
- Thu Jun 15 07:19:40 2017 +0100
- Revision:
- 6:804d544f8d8c
- Parent:
- 5:b935404dcf7c
- Child:
- 7:a9eea2dbdd68
Make smaller test file size apply to C027 only.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
RobMeades | 0:bb5fabac67ab | 1 | #include "mbed.h" |
RobMeades | 0:bb5fabac67ab | 2 | #include "greentea-client/test_env.h" |
RobMeades | 0:bb5fabac67ab | 3 | #include "unity.h" |
RobMeades | 0:bb5fabac67ab | 4 | #include "utest.h" |
RobMeades | 0:bb5fabac67ab | 5 | #include "UbloxCellularDriverGen.h" |
RobMeades | 0:bb5fabac67ab | 6 | #include "UDPSocket.h" |
rob.meades@u-blox.com | 3:027c9eaec52c | 7 | #ifdef FEATURE_COMMON_PAL |
RobMeades | 0:bb5fabac67ab | 8 | #include "mbed_trace.h" |
RobMeades | 0:bb5fabac67ab | 9 | #define TRACE_GROUP "TEST" |
rob.meades@u-blox.com | 3:027c9eaec52c | 10 | #else |
rob.meades@u-blox.com | 3:027c9eaec52c | 11 | #define tr_debug(format, ...) debug(format "\n", ## __VA_ARGS__) |
rob.meades@u-blox.com | 3:027c9eaec52c | 12 | #define tr_info(format, ...) debug(format "\n", ## __VA_ARGS__) |
rob.meades@u-blox.com | 3:027c9eaec52c | 13 | #define tr_warn(format, ...) debug(format "\n", ## __VA_ARGS__) |
rob.meades@u-blox.com | 3:027c9eaec52c | 14 | #define tr_error(format, ...) debug(format "\n", ## __VA_ARGS__) |
rob.meades@u-blox.com | 3:027c9eaec52c | 15 | #endif |
RobMeades | 0:bb5fabac67ab | 16 | |
RobMeades | 0:bb5fabac67ab | 17 | using namespace utest::v1; |
RobMeades | 0:bb5fabac67ab | 18 | |
RobMeades | 0:bb5fabac67ab | 19 | // ---------------------------------------------------------------- |
RobMeades | 0:bb5fabac67ab | 20 | // COMPILE-TIME MACROS |
RobMeades | 0:bb5fabac67ab | 21 | // ---------------------------------------------------------------- |
RobMeades | 0:bb5fabac67ab | 22 | |
RobMeades | 0:bb5fabac67ab | 23 | // These macros can be overridden with an mbed_app.json file and |
RobMeades | 0:bb5fabac67ab | 24 | // contents of the following form: |
RobMeades | 0:bb5fabac67ab | 25 | // |
RobMeades | 0:bb5fabac67ab | 26 | //{ |
RobMeades | 0:bb5fabac67ab | 27 | // "config": { |
RobMeades | 0:bb5fabac67ab | 28 | // "default-pin": { |
RobMeades | 0:bb5fabac67ab | 29 | // "value": "\"my_pin\"" |
RobMeades | 0:bb5fabac67ab | 30 | // } |
RobMeades | 0:bb5fabac67ab | 31 | //} |
RobMeades | 0:bb5fabac67ab | 32 | |
rob.meades@u-blox.com | 5:b935404dcf7c | 33 | // Whether debug trace is on |
rob.meades@u-blox.com | 5:b935404dcf7c | 34 | #ifndef MBED_CONF_APP_DEBUG_ON |
rob.meades@u-blox.com | 5:b935404dcf7c | 35 | # define MBED_CONF_APP_DEBUG_ON false |
rob.meades@u-blox.com | 5:b935404dcf7c | 36 | #endif |
rob.meades@u-blox.com | 5:b935404dcf7c | 37 | |
RobMeades | 0:bb5fabac67ab | 38 | // The credentials of the SIM in the board. |
RobMeades | 0:bb5fabac67ab | 39 | #ifndef MBED_CONF_APP_DEFAULT_PIN |
RobMeades | 0:bb5fabac67ab | 40 | // Note: this is the PIN for the SIM with ICCID |
RobMeades | 0:bb5fabac67ab | 41 | // 8944501104169548380. |
RobMeades | 0:bb5fabac67ab | 42 | # define MBED_CONF_APP_DEFAULT_PIN "5134" |
RobMeades | 0:bb5fabac67ab | 43 | #endif |
RobMeades | 0:bb5fabac67ab | 44 | |
RobMeades | 0:bb5fabac67ab | 45 | // The size of file to use. |
RobMeades | 0:bb5fabac67ab | 46 | #ifndef MBED_CONF_APP_FILE_SIZE |
rob.meades@u-blox.com | 6:804d544f8d8c | 47 | # ifdef TARGET_UBLOX_C027 |
rob.meades@u-blox.com | 6:804d544f8d8c | 48 | // C027 doesn't have so much RAM. |
rob.meades@u-blox.com | 6:804d544f8d8c | 49 | // TODO: it should be possible to use, say 1200 |
rob.meades@u-blox.com | 6:804d544f8d8c | 50 | // here but it seems that the Sara-G350 module |
rob.meades@u-blox.com | 6:804d544f8d8c | 51 | // needs flow control so that it can slow |
rob.meades@u-blox.com | 6:804d544f8d8c | 52 | // things down while flash writees complete |
rob.meades@u-blox.com | 6:804d544f8d8c | 53 | // and UARTSerial doesn't yet support HW |
rob.meades@u-blox.com | 6:804d544f8d8c | 54 | // flow control. Come back to this once |
rob.meades@u-blox.com | 6:804d544f8d8c | 55 | // UARTSerial has HW flow control. |
rob.meades@u-blox.com | 6:804d544f8d8c | 56 | # define MBED_CONF_APP_FILE_SIZE 1500 |
rob.meades@u-blox.com | 6:804d544f8d8c | 57 | # else |
rob.meades@u-blox.com | 6:804d544f8d8c | 58 | # define MBED_CONF_APP_FILE_SIZE 42000 |
rob.meades@u-blox.com | 6:804d544f8d8c | 59 | # endif |
RobMeades | 0:bb5fabac67ab | 60 | #endif |
RobMeades | 0:bb5fabac67ab | 61 | |
RobMeades | 0:bb5fabac67ab | 62 | // The name of the file to use. |
RobMeades | 0:bb5fabac67ab | 63 | #ifndef MBED_CONF_APP_FILE_NAME |
RobMeades | 0:bb5fabac67ab | 64 | # define MBED_CONF_APP_FILE_NAME "test_file" |
RobMeades | 0:bb5fabac67ab | 65 | #endif |
RobMeades | 0:bb5fabac67ab | 66 | |
RobMeades | 0:bb5fabac67ab | 67 | // ---------------------------------------------------------------- |
RobMeades | 0:bb5fabac67ab | 68 | // PRIVATE VARIABLES |
RobMeades | 0:bb5fabac67ab | 69 | // ---------------------------------------------------------------- |
RobMeades | 0:bb5fabac67ab | 70 | |
rob.meades@u-blox.com | 5:b935404dcf7c | 71 | #ifdef FEATURE_COMMON_PAL |
RobMeades | 0:bb5fabac67ab | 72 | // Lock for debug prints |
RobMeades | 0:bb5fabac67ab | 73 | static Mutex mtx; |
rob.meades@u-blox.com | 5:b935404dcf7c | 74 | #endif |
RobMeades | 0:bb5fabac67ab | 75 | |
RobMeades | 0:bb5fabac67ab | 76 | // An instance of the generic cellular class |
RobMeades | 0:bb5fabac67ab | 77 | static UbloxCellularDriverGen *pDriver = |
RobMeades | 0:bb5fabac67ab | 78 | new UbloxCellularDriverGen(MDMTXD, MDMRXD, |
RobMeades | 0:bb5fabac67ab | 79 | MBED_CONF_UBLOX_CELL_BAUD_RATE, |
rob.meades@u-blox.com | 5:b935404dcf7c | 80 | MBED_CONF_APP_DEBUG_ON); |
RobMeades | 0:bb5fabac67ab | 81 | |
RobMeades | 0:bb5fabac67ab | 82 | // A general purpose buffer |
RobMeades | 0:bb5fabac67ab | 83 | char buf[MBED_CONF_APP_FILE_SIZE]; |
RobMeades | 0:bb5fabac67ab | 84 | |
RobMeades | 0:bb5fabac67ab | 85 | // ---------------------------------------------------------------- |
RobMeades | 0:bb5fabac67ab | 86 | // PRIVATE FUNCTIONS |
RobMeades | 0:bb5fabac67ab | 87 | // ---------------------------------------------------------------- |
RobMeades | 0:bb5fabac67ab | 88 | |
rob.meades@u-blox.com | 3:027c9eaec52c | 89 | #ifdef FEATURE_COMMON_PAL |
RobMeades | 0:bb5fabac67ab | 90 | // Locks for debug prints |
RobMeades | 0:bb5fabac67ab | 91 | static void lock() |
RobMeades | 0:bb5fabac67ab | 92 | { |
RobMeades | 0:bb5fabac67ab | 93 | mtx.lock(); |
RobMeades | 0:bb5fabac67ab | 94 | } |
RobMeades | 0:bb5fabac67ab | 95 | |
RobMeades | 0:bb5fabac67ab | 96 | static void unlock() |
RobMeades | 0:bb5fabac67ab | 97 | { |
RobMeades | 0:bb5fabac67ab | 98 | mtx.unlock(); |
RobMeades | 0:bb5fabac67ab | 99 | } |
rob.meades@u-blox.com | 3:027c9eaec52c | 100 | #endif |
RobMeades | 0:bb5fabac67ab | 101 | |
RobMeades | 0:bb5fabac67ab | 102 | // ---------------------------------------------------------------- |
RobMeades | 0:bb5fabac67ab | 103 | // TESTS |
RobMeades | 0:bb5fabac67ab | 104 | // ---------------------------------------------------------------- |
RobMeades | 0:bb5fabac67ab | 105 | |
RobMeades | 0:bb5fabac67ab | 106 | // Initialise the module |
RobMeades | 0:bb5fabac67ab | 107 | void test_start() { |
RobMeades | 0:bb5fabac67ab | 108 | TEST_ASSERT(pDriver->init(MBED_CONF_APP_DEFAULT_PIN)); |
RobMeades | 0:bb5fabac67ab | 109 | } |
RobMeades | 0:bb5fabac67ab | 110 | |
RobMeades | 0:bb5fabac67ab | 111 | // Write a file to the module's file system with known contents |
RobMeades | 0:bb5fabac67ab | 112 | void test_write() { |
RobMeades | 0:bb5fabac67ab | 113 | |
rob.meades@u-blox.com | 5:b935404dcf7c | 114 | for (unsigned int x = 0; x < sizeof (buf); x++) { |
RobMeades | 0:bb5fabac67ab | 115 | buf[x] = (char) x; |
RobMeades | 0:bb5fabac67ab | 116 | } |
RobMeades | 0:bb5fabac67ab | 117 | |
RobMeades | 0:bb5fabac67ab | 118 | TEST_ASSERT(pDriver->writeFile(MBED_CONF_APP_FILE_NAME, buf, sizeof (buf)) == sizeof (buf)); |
rob.meades@u-blox.com | 5:b935404dcf7c | 119 | TEST_ASSERT(pDriver->fileSize(MBED_CONF_APP_FILE_NAME) >= (int) sizeof (buf)); |
RobMeades | 0:bb5fabac67ab | 120 | tr_debug("%d bytes written to file \"%s\"", sizeof (buf), MBED_CONF_APP_FILE_NAME); |
RobMeades | 0:bb5fabac67ab | 121 | } |
RobMeades | 0:bb5fabac67ab | 122 | |
RobMeades | 0:bb5fabac67ab | 123 | // Read a file back from the module's file system and check the contents |
RobMeades | 0:bb5fabac67ab | 124 | void test_read() { |
RobMeades | 0:bb5fabac67ab | 125 | memset(buf, 0, sizeof (buf)); |
RobMeades | 0:bb5fabac67ab | 126 | |
RobMeades | 0:bb5fabac67ab | 127 | TEST_ASSERT(pDriver->readFile(MBED_CONF_APP_FILE_NAME, buf, sizeof (buf)) == sizeof (buf)); |
RobMeades | 0:bb5fabac67ab | 128 | |
RobMeades | 0:bb5fabac67ab | 129 | tr_debug("%d bytes read from file \"%s\"", sizeof (buf), MBED_CONF_APP_FILE_NAME); |
RobMeades | 0:bb5fabac67ab | 130 | |
rob.meades@u-blox.com | 5:b935404dcf7c | 131 | for (unsigned int x = 0; x < sizeof (buf); x++) { |
RobMeades | 0:bb5fabac67ab | 132 | TEST_ASSERT(buf[x] == (char) x); |
RobMeades | 0:bb5fabac67ab | 133 | } |
RobMeades | 0:bb5fabac67ab | 134 | } |
RobMeades | 0:bb5fabac67ab | 135 | |
RobMeades | 0:bb5fabac67ab | 136 | // Delete a file from the module's file system |
RobMeades | 0:bb5fabac67ab | 137 | void test_delete() { |
RobMeades | 0:bb5fabac67ab | 138 | TEST_ASSERT(pDriver->delFile(MBED_CONF_APP_FILE_NAME)); |
RobMeades | 0:bb5fabac67ab | 139 | tr_debug("File \"%s\" deleted", MBED_CONF_APP_FILE_NAME); |
RobMeades | 0:bb5fabac67ab | 140 | } |
RobMeades | 0:bb5fabac67ab | 141 | |
RobMeades | 0:bb5fabac67ab | 142 | // ---------------------------------------------------------------- |
RobMeades | 0:bb5fabac67ab | 143 | // TEST ENVIRONMENT |
RobMeades | 0:bb5fabac67ab | 144 | // ---------------------------------------------------------------- |
RobMeades | 0:bb5fabac67ab | 145 | |
RobMeades | 0:bb5fabac67ab | 146 | // Setup the test environment |
RobMeades | 0:bb5fabac67ab | 147 | utest::v1::status_t test_setup(const size_t number_of_cases) { |
RobMeades | 0:bb5fabac67ab | 148 | // Setup Greentea with a timeout |
RobMeades | 0:bb5fabac67ab | 149 | GREENTEA_SETUP(180, "default_auto"); |
RobMeades | 0:bb5fabac67ab | 150 | return verbose_test_setup_handler(number_of_cases); |
RobMeades | 0:bb5fabac67ab | 151 | } |
RobMeades | 0:bb5fabac67ab | 152 | |
RobMeades | 0:bb5fabac67ab | 153 | // Test cases |
RobMeades | 0:bb5fabac67ab | 154 | Case cases[] = { |
RobMeades | 0:bb5fabac67ab | 155 | Case("Start", test_start), |
RobMeades | 0:bb5fabac67ab | 156 | Case("Write file", test_write), |
RobMeades | 0:bb5fabac67ab | 157 | Case("Read file", test_read), |
RobMeades | 0:bb5fabac67ab | 158 | Case("Delete file", test_delete) |
RobMeades | 0:bb5fabac67ab | 159 | }; |
RobMeades | 0:bb5fabac67ab | 160 | |
RobMeades | 0:bb5fabac67ab | 161 | Specification specification(test_setup, cases); |
RobMeades | 0:bb5fabac67ab | 162 | |
RobMeades | 0:bb5fabac67ab | 163 | // ---------------------------------------------------------------- |
RobMeades | 0:bb5fabac67ab | 164 | // MAIN |
RobMeades | 0:bb5fabac67ab | 165 | // ---------------------------------------------------------------- |
RobMeades | 0:bb5fabac67ab | 166 | |
RobMeades | 0:bb5fabac67ab | 167 | int main() { |
rob.meades@u-blox.com | 3:027c9eaec52c | 168 | |
rob.meades@u-blox.com | 3:027c9eaec52c | 169 | #ifdef FEATURE_COMMON_PAL |
RobMeades | 0:bb5fabac67ab | 170 | mbed_trace_init(); |
RobMeades | 0:bb5fabac67ab | 171 | |
RobMeades | 0:bb5fabac67ab | 172 | mbed_trace_mutex_wait_function_set(lock); |
RobMeades | 0:bb5fabac67ab | 173 | mbed_trace_mutex_release_function_set(unlock); |
rob.meades@u-blox.com | 3:027c9eaec52c | 174 | #endif |
RobMeades | 0:bb5fabac67ab | 175 | |
RobMeades | 0:bb5fabac67ab | 176 | // Run tests |
RobMeades | 0:bb5fabac67ab | 177 | return !Harness::run(specification); |
RobMeades | 0:bb5fabac67ab | 178 | } |
RobMeades | 0:bb5fabac67ab | 179 | |
RobMeades | 0:bb5fabac67ab | 180 | // End Of File |
RobMeades | 0:bb5fabac67ab | 181 |