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