ublox-cellular-driver-gen Fork
Fork of ublox-cellular-driver-gen by
TESTS/unit_tests/file-system/main.cpp@0:bb5fabac67ab, 2017-06-05 (annotated)
- Committer:
- RobMeades
- Date:
- Mon Jun 05 13:00:31 2017 +0000
- Revision:
- 0:bb5fabac67ab
- Child:
- 1:458e1b3d460c
Initial revision.
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" |
RobMeades | 0:bb5fabac67ab | 7 | #include "FEATURE_COMMON_PAL/nanostack-libservice/mbed-client-libservice/common_functions.h" |
RobMeades | 0:bb5fabac67ab | 8 | #include "mbed_trace.h" |
RobMeades | 0:bb5fabac67ab | 9 | #define TRACE_GROUP "TEST" |
RobMeades | 0:bb5fabac67ab | 10 | |
RobMeades | 0:bb5fabac67ab | 11 | using namespace utest::v1; |
RobMeades | 0:bb5fabac67ab | 12 | |
RobMeades | 0:bb5fabac67ab | 13 | // IMPORTANT: these tests require |
RobMeades | 0:bb5fabac67ab | 14 | |
RobMeades | 0:bb5fabac67ab | 15 | // ---------------------------------------------------------------- |
RobMeades | 0:bb5fabac67ab | 16 | // COMPILE-TIME MACROS |
RobMeades | 0:bb5fabac67ab | 17 | // ---------------------------------------------------------------- |
RobMeades | 0:bb5fabac67ab | 18 | |
RobMeades | 0:bb5fabac67ab | 19 | // These macros can be overridden with an mbed_app.json file and |
RobMeades | 0:bb5fabac67ab | 20 | // contents of the following form: |
RobMeades | 0:bb5fabac67ab | 21 | // |
RobMeades | 0:bb5fabac67ab | 22 | //{ |
RobMeades | 0:bb5fabac67ab | 23 | // "config": { |
RobMeades | 0:bb5fabac67ab | 24 | // "default-pin": { |
RobMeades | 0:bb5fabac67ab | 25 | // "value": "\"my_pin\"" |
RobMeades | 0:bb5fabac67ab | 26 | // } |
RobMeades | 0:bb5fabac67ab | 27 | //} |
RobMeades | 0:bb5fabac67ab | 28 | |
RobMeades | 0:bb5fabac67ab | 29 | // The credentials of the SIM in the board. |
RobMeades | 0:bb5fabac67ab | 30 | #ifndef MBED_CONF_APP_DEFAULT_PIN |
RobMeades | 0:bb5fabac67ab | 31 | // Note: this is the PIN for the SIM with ICCID |
RobMeades | 0:bb5fabac67ab | 32 | // 8944501104169548380. |
RobMeades | 0:bb5fabac67ab | 33 | # define MBED_CONF_APP_DEFAULT_PIN "5134" |
RobMeades | 0:bb5fabac67ab | 34 | #endif |
RobMeades | 0:bb5fabac67ab | 35 | |
RobMeades | 0:bb5fabac67ab | 36 | // The size of file to use. |
RobMeades | 0:bb5fabac67ab | 37 | #ifndef MBED_CONF_APP_FILE_SIZE |
RobMeades | 0:bb5fabac67ab | 38 | # define MBED_CONF_APP_FILE_SIZE 42000 |
RobMeades | 0:bb5fabac67ab | 39 | #endif |
RobMeades | 0:bb5fabac67ab | 40 | |
RobMeades | 0:bb5fabac67ab | 41 | // The name of the file to use. |
RobMeades | 0:bb5fabac67ab | 42 | #ifndef MBED_CONF_APP_FILE_NAME |
RobMeades | 0:bb5fabac67ab | 43 | # define MBED_CONF_APP_FILE_NAME "test_file" |
RobMeades | 0:bb5fabac67ab | 44 | #endif |
RobMeades | 0:bb5fabac67ab | 45 | |
RobMeades | 0:bb5fabac67ab | 46 | // ---------------------------------------------------------------- |
RobMeades | 0:bb5fabac67ab | 47 | // PRIVATE VARIABLES |
RobMeades | 0:bb5fabac67ab | 48 | // ---------------------------------------------------------------- |
RobMeades | 0:bb5fabac67ab | 49 | |
RobMeades | 0:bb5fabac67ab | 50 | // Lock for debug prints |
RobMeades | 0:bb5fabac67ab | 51 | static Mutex mtx; |
RobMeades | 0:bb5fabac67ab | 52 | |
RobMeades | 0:bb5fabac67ab | 53 | // An instance of the generic cellular class |
RobMeades | 0:bb5fabac67ab | 54 | static UbloxCellularDriverGen *pDriver = |
RobMeades | 0:bb5fabac67ab | 55 | new UbloxCellularDriverGen(MDMTXD, MDMRXD, |
RobMeades | 0:bb5fabac67ab | 56 | MBED_CONF_UBLOX_CELL_BAUD_RATE, |
RobMeades | 0:bb5fabac67ab | 57 | true); |
RobMeades | 0:bb5fabac67ab | 58 | |
RobMeades | 0:bb5fabac67ab | 59 | // A general purpose buffer |
RobMeades | 0:bb5fabac67ab | 60 | char buf[MBED_CONF_APP_FILE_SIZE]; |
RobMeades | 0:bb5fabac67ab | 61 | |
RobMeades | 0:bb5fabac67ab | 62 | // ---------------------------------------------------------------- |
RobMeades | 0:bb5fabac67ab | 63 | // PRIVATE FUNCTIONS |
RobMeades | 0:bb5fabac67ab | 64 | // ---------------------------------------------------------------- |
RobMeades | 0:bb5fabac67ab | 65 | |
RobMeades | 0:bb5fabac67ab | 66 | // Locks for debug prints |
RobMeades | 0:bb5fabac67ab | 67 | static void lock() |
RobMeades | 0:bb5fabac67ab | 68 | { |
RobMeades | 0:bb5fabac67ab | 69 | mtx.lock(); |
RobMeades | 0:bb5fabac67ab | 70 | } |
RobMeades | 0:bb5fabac67ab | 71 | |
RobMeades | 0:bb5fabac67ab | 72 | static void unlock() |
RobMeades | 0:bb5fabac67ab | 73 | { |
RobMeades | 0:bb5fabac67ab | 74 | mtx.unlock(); |
RobMeades | 0:bb5fabac67ab | 75 | } |
RobMeades | 0:bb5fabac67ab | 76 | |
RobMeades | 0:bb5fabac67ab | 77 | // ---------------------------------------------------------------- |
RobMeades | 0:bb5fabac67ab | 78 | // TESTS |
RobMeades | 0:bb5fabac67ab | 79 | // ---------------------------------------------------------------- |
RobMeades | 0:bb5fabac67ab | 80 | |
RobMeades | 0:bb5fabac67ab | 81 | // Initialise the module |
RobMeades | 0:bb5fabac67ab | 82 | void test_start() { |
RobMeades | 0:bb5fabac67ab | 83 | TEST_ASSERT(pDriver->init(MBED_CONF_APP_DEFAULT_PIN)); |
RobMeades | 0:bb5fabac67ab | 84 | } |
RobMeades | 0:bb5fabac67ab | 85 | |
RobMeades | 0:bb5fabac67ab | 86 | // Write a file to the module's file system with known contents |
RobMeades | 0:bb5fabac67ab | 87 | void test_write() { |
RobMeades | 0:bb5fabac67ab | 88 | |
RobMeades | 0:bb5fabac67ab | 89 | for (int x = 0; x < sizeof (buf); x++) { |
RobMeades | 0:bb5fabac67ab | 90 | buf[x] = (char) x; |
RobMeades | 0:bb5fabac67ab | 91 | } |
RobMeades | 0:bb5fabac67ab | 92 | |
RobMeades | 0:bb5fabac67ab | 93 | TEST_ASSERT(pDriver->writeFile(MBED_CONF_APP_FILE_NAME, buf, sizeof (buf)) == sizeof (buf)); |
RobMeades | 0:bb5fabac67ab | 94 | TEST_ASSERT(pDriver->fileSize(MBED_CONF_APP_FILE_NAME) >= sizeof (buf)); |
RobMeades | 0:bb5fabac67ab | 95 | tr_debug("%d bytes written to file \"%s\"", sizeof (buf), MBED_CONF_APP_FILE_NAME); |
RobMeades | 0:bb5fabac67ab | 96 | } |
RobMeades | 0:bb5fabac67ab | 97 | |
RobMeades | 0:bb5fabac67ab | 98 | // Read a file back from the module's file system and check the contents |
RobMeades | 0:bb5fabac67ab | 99 | void test_read() { |
RobMeades | 0:bb5fabac67ab | 100 | memset(buf, 0, sizeof (buf)); |
RobMeades | 0:bb5fabac67ab | 101 | |
RobMeades | 0:bb5fabac67ab | 102 | TEST_ASSERT(pDriver->readFile(MBED_CONF_APP_FILE_NAME, buf, sizeof (buf)) == sizeof (buf)); |
RobMeades | 0:bb5fabac67ab | 103 | |
RobMeades | 0:bb5fabac67ab | 104 | tr_debug("%d bytes read from file \"%s\"", sizeof (buf), MBED_CONF_APP_FILE_NAME); |
RobMeades | 0:bb5fabac67ab | 105 | |
RobMeades | 0:bb5fabac67ab | 106 | for (int x = 0; x < sizeof (buf); x++) { |
RobMeades | 0:bb5fabac67ab | 107 | TEST_ASSERT(buf[x] == (char) x); |
RobMeades | 0:bb5fabac67ab | 108 | } |
RobMeades | 0:bb5fabac67ab | 109 | } |
RobMeades | 0:bb5fabac67ab | 110 | |
RobMeades | 0:bb5fabac67ab | 111 | // Delete a file from the module's file system |
RobMeades | 0:bb5fabac67ab | 112 | void test_delete() { |
RobMeades | 0:bb5fabac67ab | 113 | TEST_ASSERT(pDriver->delFile(MBED_CONF_APP_FILE_NAME)); |
RobMeades | 0:bb5fabac67ab | 114 | tr_debug("File \"%s\" deleted", MBED_CONF_APP_FILE_NAME); |
RobMeades | 0:bb5fabac67ab | 115 | } |
RobMeades | 0:bb5fabac67ab | 116 | |
RobMeades | 0:bb5fabac67ab | 117 | // ---------------------------------------------------------------- |
RobMeades | 0:bb5fabac67ab | 118 | // TEST ENVIRONMENT |
RobMeades | 0:bb5fabac67ab | 119 | // ---------------------------------------------------------------- |
RobMeades | 0:bb5fabac67ab | 120 | |
RobMeades | 0:bb5fabac67ab | 121 | // Setup the test environment |
RobMeades | 0:bb5fabac67ab | 122 | utest::v1::status_t test_setup(const size_t number_of_cases) { |
RobMeades | 0:bb5fabac67ab | 123 | // Setup Greentea with a timeout |
RobMeades | 0:bb5fabac67ab | 124 | GREENTEA_SETUP(180, "default_auto"); |
RobMeades | 0:bb5fabac67ab | 125 | return verbose_test_setup_handler(number_of_cases); |
RobMeades | 0:bb5fabac67ab | 126 | } |
RobMeades | 0:bb5fabac67ab | 127 | |
RobMeades | 0:bb5fabac67ab | 128 | // Test cases |
RobMeades | 0:bb5fabac67ab | 129 | Case cases[] = { |
RobMeades | 0:bb5fabac67ab | 130 | Case("Start", test_start), |
RobMeades | 0:bb5fabac67ab | 131 | Case("Write file", test_write), |
RobMeades | 0:bb5fabac67ab | 132 | Case("Read file", test_read), |
RobMeades | 0:bb5fabac67ab | 133 | Case("Delete file", test_delete) |
RobMeades | 0:bb5fabac67ab | 134 | }; |
RobMeades | 0:bb5fabac67ab | 135 | |
RobMeades | 0:bb5fabac67ab | 136 | Specification specification(test_setup, cases); |
RobMeades | 0:bb5fabac67ab | 137 | |
RobMeades | 0:bb5fabac67ab | 138 | // ---------------------------------------------------------------- |
RobMeades | 0:bb5fabac67ab | 139 | // MAIN |
RobMeades | 0:bb5fabac67ab | 140 | // ---------------------------------------------------------------- |
RobMeades | 0:bb5fabac67ab | 141 | |
RobMeades | 0:bb5fabac67ab | 142 | int main() { |
RobMeades | 0:bb5fabac67ab | 143 | mbed_trace_init(); |
RobMeades | 0:bb5fabac67ab | 144 | |
RobMeades | 0:bb5fabac67ab | 145 | mbed_trace_mutex_wait_function_set(lock); |
RobMeades | 0:bb5fabac67ab | 146 | mbed_trace_mutex_release_function_set(unlock); |
RobMeades | 0:bb5fabac67ab | 147 | |
RobMeades | 0:bb5fabac67ab | 148 | // Run tests |
RobMeades | 0:bb5fabac67ab | 149 | return !Harness::run(specification); |
RobMeades | 0:bb5fabac67ab | 150 | } |
RobMeades | 0:bb5fabac67ab | 151 | |
RobMeades | 0:bb5fabac67ab | 152 | // End Of File |
RobMeades | 0:bb5fabac67ab | 153 |