Fork of ublox-at-cellular-interface to add LARA-R2 support
Fork of ublox-at-cellular-interface by
TESTS/unit_tests/default/main.cpp@19:2e961ce171e6, 2018-09-27 (annotated)
- Committer:
- cblack
- Date:
- Thu Sep 27 15:45:38 2018 -0500
- Revision:
- 19:2e961ce171e6
- Parent:
- 12:ff6fac481487
Fix to hex mode workaround - read 2x the size in hex mode
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rob.meades@u-blox.com | 1:bc228becc45d | 1 | #include "UbloxATCellularInterface.h" |
RobMeades | 0:7ccf0e7e8a83 | 2 | #include "greentea-client/test_env.h" |
RobMeades | 0:7ccf0e7e8a83 | 3 | #include "unity.h" |
RobMeades | 0:7ccf0e7e8a83 | 4 | #include "utest.h" |
RobMeades | 0:7ccf0e7e8a83 | 5 | #include "UDPSocket.h" |
RobMeades | 0:7ccf0e7e8a83 | 6 | #ifdef FEATURE_COMMON_PAL |
RobMeades | 0:7ccf0e7e8a83 | 7 | #include "mbed_trace.h" |
RobMeades | 0:7ccf0e7e8a83 | 8 | #define TRACE_GROUP "TEST" |
RobMeades | 0:7ccf0e7e8a83 | 9 | #else |
rob.meades@u-blox.com | 1:bc228becc45d | 10 | #define tr_debug(format, ...) debug(format "\n", ## __VA_ARGS__) |
rob.meades@u-blox.com | 1:bc228becc45d | 11 | #define tr_info(format, ...) debug(format "\n", ## __VA_ARGS__) |
rob.meades@u-blox.com | 1:bc228becc45d | 12 | #define tr_warn(format, ...) debug(format "\n", ## __VA_ARGS__) |
rob.meades@u-blox.com | 1:bc228becc45d | 13 | #define tr_error(format, ...) debug(format "\n", ## __VA_ARGS__) |
RobMeades | 0:7ccf0e7e8a83 | 14 | #endif |
RobMeades | 0:7ccf0e7e8a83 | 15 | |
RobMeades | 0:7ccf0e7e8a83 | 16 | using namespace utest::v1; |
RobMeades | 0:7ccf0e7e8a83 | 17 | |
RobMeades | 0:7ccf0e7e8a83 | 18 | // IMPORTANT!!! if you make a change to the tests here you should |
RobMeades | 0:7ccf0e7e8a83 | 19 | // check whether the same change should be made to the tests under |
rob.meades@u-blox.com | 6:63dad754c267 | 20 | // the PPP interface. |
rob.meades@u-blox.com | 6:63dad754c267 | 21 | |
rob.meades@u-blox.com | 6:63dad754c267 | 22 | // NOTE: these test are only as reliable as UDP across the internet |
rob.meades@u-blox.com | 6:63dad754c267 | 23 | // over a radio link. The tests expect an NTP server to respond |
rob.meades@u-blox.com | 6:63dad754c267 | 24 | // to UDP packets and, if configured, an echo server to respond |
rob.meades@u-blox.com | 6:63dad754c267 | 25 | // to UDP packets. This simply may not happen. Please be patient. |
RobMeades | 0:7ccf0e7e8a83 | 26 | |
RobMeades | 0:7ccf0e7e8a83 | 27 | // ---------------------------------------------------------------- |
RobMeades | 0:7ccf0e7e8a83 | 28 | // COMPILE-TIME MACROS |
RobMeades | 0:7ccf0e7e8a83 | 29 | // ---------------------------------------------------------------- |
RobMeades | 0:7ccf0e7e8a83 | 30 | |
RobMeades | 0:7ccf0e7e8a83 | 31 | // These macros can be overridden with an mbed_app.json file and |
RobMeades | 0:7ccf0e7e8a83 | 32 | // contents of the following form: |
RobMeades | 0:7ccf0e7e8a83 | 33 | // |
RobMeades | 0:7ccf0e7e8a83 | 34 | //{ |
RobMeades | 0:7ccf0e7e8a83 | 35 | // "config": { |
RobMeades | 0:7ccf0e7e8a83 | 36 | // "default-pin": { |
RobMeades | 0:7ccf0e7e8a83 | 37 | // "value": "\"1234\"" |
RobMeades | 0:7ccf0e7e8a83 | 38 | // } |
RobMeades | 0:7ccf0e7e8a83 | 39 | //} |
RobMeades | 0:7ccf0e7e8a83 | 40 | // |
RobMeades | 0:7ccf0e7e8a83 | 41 | // See the template_mbed_app.txt in this directory for a fuller example. |
RobMeades | 0:7ccf0e7e8a83 | 42 | |
RobMeades | 0:7ccf0e7e8a83 | 43 | // Whether debug trace is on |
RobMeades | 0:7ccf0e7e8a83 | 44 | #ifndef MBED_CONF_APP_DEBUG_ON |
RobMeades | 0:7ccf0e7e8a83 | 45 | # define MBED_CONF_APP_DEBUG_ON false |
RobMeades | 0:7ccf0e7e8a83 | 46 | #endif |
RobMeades | 0:7ccf0e7e8a83 | 47 | |
RobMeades | 0:7ccf0e7e8a83 | 48 | // Run the SIM change tests, which require the DEFAULT_PIN |
RobMeades | 0:7ccf0e7e8a83 | 49 | // above to be correct for the board on which the test |
RobMeades | 0:7ccf0e7e8a83 | 50 | // is being run (and the SIM PIN to be disabled before tests run). |
RobMeades | 0:7ccf0e7e8a83 | 51 | #ifndef MBED_CONF_APP_RUN_SIM_PIN_CHANGE_TESTS |
RobMeades | 0:7ccf0e7e8a83 | 52 | # define MBED_CONF_APP_RUN_SIM_PIN_CHANGE_TESTS 0 |
RobMeades | 0:7ccf0e7e8a83 | 53 | #endif |
RobMeades | 0:7ccf0e7e8a83 | 54 | |
RobMeades | 0:7ccf0e7e8a83 | 55 | #if MBED_CONF_APP_RUN_SIM_PIN_CHANGE_TESTS |
RobMeades | 0:7ccf0e7e8a83 | 56 | # ifndef MBED_CONF_APP_DEFAULT_PIN |
RobMeades | 0:7ccf0e7e8a83 | 57 | # error "MBED_CONF_APP_DEFAULT_PIN must be defined to run the SIM tests" |
RobMeades | 0:7ccf0e7e8a83 | 58 | # endif |
RobMeades | 0:7ccf0e7e8a83 | 59 | # ifndef MBED_CONF_APP_ALT_PIN |
RobMeades | 0:7ccf0e7e8a83 | 60 | # error "MBED_CONF_APP_ALT_PIN must be defined to run the SIM tests" |
RobMeades | 0:7ccf0e7e8a83 | 61 | # endif |
RobMeades | 0:7ccf0e7e8a83 | 62 | # ifndef MBED_CONF_APP_INCORRECT_PIN |
RobMeades | 0:7ccf0e7e8a83 | 63 | # error "MBED_CONF_APP_INCORRECT_PIN must be defined to run the SIM tests" |
RobMeades | 0:7ccf0e7e8a83 | 64 | # endif |
RobMeades | 0:7ccf0e7e8a83 | 65 | #endif |
RobMeades | 0:7ccf0e7e8a83 | 66 | |
RobMeades | 0:7ccf0e7e8a83 | 67 | // The credentials of the SIM in the board. |
RobMeades | 0:7ccf0e7e8a83 | 68 | #ifndef MBED_CONF_APP_DEFAULT_PIN |
RobMeades | 0:7ccf0e7e8a83 | 69 | // Note: if PIN is enabled on your SIM, or you wish to run the SIM PIN change |
RobMeades | 0:7ccf0e7e8a83 | 70 | // tests, you must define the PIN for your SIM (see note above on using |
RobMeades | 0:7ccf0e7e8a83 | 71 | // mbed_app.json to do so). |
RobMeades | 0:7ccf0e7e8a83 | 72 | # define MBED_CONF_APP_DEFAULT_PIN "0000" |
RobMeades | 0:7ccf0e7e8a83 | 73 | #endif |
RobMeades | 0:7ccf0e7e8a83 | 74 | #ifndef MBED_CONF_APP_APN |
RobMeades | 0:7ccf0e7e8a83 | 75 | # define MBED_CONF_APP_APN NULL |
RobMeades | 0:7ccf0e7e8a83 | 76 | #endif |
RobMeades | 0:7ccf0e7e8a83 | 77 | #ifndef MBED_CONF_APP_USERNAME |
RobMeades | 0:7ccf0e7e8a83 | 78 | # define MBED_CONF_APP_USERNAME NULL |
RobMeades | 0:7ccf0e7e8a83 | 79 | #endif |
RobMeades | 0:7ccf0e7e8a83 | 80 | #ifndef MBED_CONF_APP_PASSWORD |
RobMeades | 0:7ccf0e7e8a83 | 81 | # define MBED_CONF_APP_PASSWORD NULL |
RobMeades | 0:7ccf0e7e8a83 | 82 | #endif |
RobMeades | 0:7ccf0e7e8a83 | 83 | |
RobMeades | 0:7ccf0e7e8a83 | 84 | // Alternate PIN to use during pin change testing |
RobMeades | 0:7ccf0e7e8a83 | 85 | #ifndef MBED_CONF_APP_ALT_PIN |
RobMeades | 0:7ccf0e7e8a83 | 86 | # define MBED_CONF_APP_ALT_PIN "9876" |
RobMeades | 0:7ccf0e7e8a83 | 87 | #endif |
RobMeades | 0:7ccf0e7e8a83 | 88 | |
RobMeades | 0:7ccf0e7e8a83 | 89 | // A PIN that is definitely incorrect |
RobMeades | 0:7ccf0e7e8a83 | 90 | #ifndef MBED_CONF_APP_INCORRECT_PIN |
RobMeades | 0:7ccf0e7e8a83 | 91 | # define MBED_CONF_APP_INCORRECT_PIN "1530" |
RobMeades | 0:7ccf0e7e8a83 | 92 | #endif |
RobMeades | 0:7ccf0e7e8a83 | 93 | |
RobMeades | 0:7ccf0e7e8a83 | 94 | // Servers and ports |
RobMeades | 0:7ccf0e7e8a83 | 95 | #ifdef MBED_CONF_APP_ECHO_SERVER |
RobMeades | 0:7ccf0e7e8a83 | 96 | # ifndef MBED_CONF_APP_ECHO_UDP_PORT |
RobMeades | 0:7ccf0e7e8a83 | 97 | # error "MBED_CONF_APP_ECHO_UDP_PORT (the port on which your echo server echoes UDP packets) must be defined" |
RobMeades | 0:7ccf0e7e8a83 | 98 | # endif |
RobMeades | 0:7ccf0e7e8a83 | 99 | # ifndef MBED_CONF_APP_ECHO_TCP_PORT |
RobMeades | 0:7ccf0e7e8a83 | 100 | # error "MBED_CONF_APP_ECHO_TCP_PORT (the port on which your echo server echoes TCP packets) must be defined" |
RobMeades | 0:7ccf0e7e8a83 | 101 | # endif |
RobMeades | 0:7ccf0e7e8a83 | 102 | #endif |
RobMeades | 0:7ccf0e7e8a83 | 103 | |
RobMeades | 0:7ccf0e7e8a83 | 104 | #ifndef MBED_CONF_APP_NTP_SERVER |
RobMeades | 0:7ccf0e7e8a83 | 105 | # define MBED_CONF_APP_NTP_SERVER "2.pool.ntp.org" |
RobMeades | 0:7ccf0e7e8a83 | 106 | #else |
RobMeades | 0:7ccf0e7e8a83 | 107 | # ifndef MBED_CONF_APP_NTP_PORT |
RobMeades | 0:7ccf0e7e8a83 | 108 | # error "MBED_CONF_APP_NTP_PORT must be defined if MBED_CONF_APP_NTP_SERVER is defined" |
RobMeades | 0:7ccf0e7e8a83 | 109 | # endif |
RobMeades | 0:7ccf0e7e8a83 | 110 | #endif |
RobMeades | 0:7ccf0e7e8a83 | 111 | #ifndef MBED_CONF_APP_NTP_PORT |
RobMeades | 0:7ccf0e7e8a83 | 112 | # define MBED_CONF_APP_NTP_PORT 123 |
RobMeades | 0:7ccf0e7e8a83 | 113 | #endif |
RobMeades | 0:7ccf0e7e8a83 | 114 | |
RobMeades | 0:7ccf0e7e8a83 | 115 | #ifndef MBED_CONF_APP_LOCAL_PORT |
RobMeades | 0:7ccf0e7e8a83 | 116 | # define MBED_CONF_APP_LOCAL_PORT 15 |
RobMeades | 0:7ccf0e7e8a83 | 117 | #endif |
RobMeades | 0:7ccf0e7e8a83 | 118 | |
RobMeades | 0:7ccf0e7e8a83 | 119 | // UDP packet size limit for testing |
RobMeades | 0:7ccf0e7e8a83 | 120 | #ifndef MBED_CONF_APP_UDP_MAX_PACKET_SIZE |
RobMeades | 0:7ccf0e7e8a83 | 121 | # define MBED_CONF_APP_UDP_MAX_PACKET_SIZE 1024 |
RobMeades | 0:7ccf0e7e8a83 | 122 | #endif |
RobMeades | 0:7ccf0e7e8a83 | 123 | |
RobMeades | 0:7ccf0e7e8a83 | 124 | // The maximum size of UDP data fragmented across |
RobMeades | 0:7ccf0e7e8a83 | 125 | // multiple packets |
RobMeades | 0:7ccf0e7e8a83 | 126 | #ifndef MBED_CONF_APP_UDP_MAX_FRAG_PACKET_SIZE |
RobMeades | 0:7ccf0e7e8a83 | 127 | # define MBED_CONF_APP_UDP_MAX_FRAG_PACKET_SIZE 1500 |
RobMeades | 0:7ccf0e7e8a83 | 128 | #endif |
RobMeades | 0:7ccf0e7e8a83 | 129 | |
RobMeades | 0:7ccf0e7e8a83 | 130 | // TCP packet size limit for testing |
RobMeades | 0:7ccf0e7e8a83 | 131 | #ifndef MBED_CONF_APP_MBED_CONF_APP_TCP_MAX_PACKET_SIZE |
RobMeades | 0:7ccf0e7e8a83 | 132 | # define MBED_CONF_APP_TCP_MAX_PACKET_SIZE 1500 |
RobMeades | 0:7ccf0e7e8a83 | 133 | #endif |
RobMeades | 0:7ccf0e7e8a83 | 134 | |
RobMeades | 0:7ccf0e7e8a83 | 135 | // The number of retries for UDP exchanges |
RobMeades | 0:7ccf0e7e8a83 | 136 | #define NUM_UDP_RETRIES 5 |
RobMeades | 0:7ccf0e7e8a83 | 137 | |
RobMeades | 0:7ccf0e7e8a83 | 138 | // How long to wait for stuff to travel in the async echo tests |
RobMeades | 0:7ccf0e7e8a83 | 139 | #define ASYNC_TEST_WAIT_TIME 10000 |
RobMeades | 0:7ccf0e7e8a83 | 140 | |
RobMeades | 0:7ccf0e7e8a83 | 141 | // The maximum number of sockets that can be open at one time |
RobMeades | 0:7ccf0e7e8a83 | 142 | #define MAX_NUM_SOCKETS 7 |
RobMeades | 0:7ccf0e7e8a83 | 143 | |
RobMeades | 0:7ccf0e7e8a83 | 144 | // ---------------------------------------------------------------- |
RobMeades | 0:7ccf0e7e8a83 | 145 | // PRIVATE VARIABLES |
RobMeades | 0:7ccf0e7e8a83 | 146 | // ---------------------------------------------------------------- |
RobMeades | 0:7ccf0e7e8a83 | 147 | |
RobMeades | 0:7ccf0e7e8a83 | 148 | #ifdef FEATURE_COMMON_PAL |
RobMeades | 0:7ccf0e7e8a83 | 149 | // Lock for debug prints |
RobMeades | 0:7ccf0e7e8a83 | 150 | static Mutex mtx; |
RobMeades | 0:7ccf0e7e8a83 | 151 | #endif |
RobMeades | 0:7ccf0e7e8a83 | 152 | |
rob.meades@u-blox.com | 6:63dad754c267 | 153 | // An instance of the cellular interface |
rob.meades@u-blox.com | 6:63dad754c267 | 154 | static UbloxATCellularInterface *interface = |
RobMeades | 0:7ccf0e7e8a83 | 155 | new UbloxATCellularInterface(MDMTXD, MDMRXD, |
RobMeades | 0:7ccf0e7e8a83 | 156 | MBED_CONF_UBLOX_CELL_BAUD_RATE, |
RobMeades | 0:7ccf0e7e8a83 | 157 | MBED_CONF_APP_DEBUG_ON); |
RobMeades | 0:7ccf0e7e8a83 | 158 | |
RobMeades | 0:7ccf0e7e8a83 | 159 | // Connection flag |
RobMeades | 0:7ccf0e7e8a83 | 160 | static bool connection_has_gone_down = false; |
RobMeades | 0:7ccf0e7e8a83 | 161 | |
RobMeades | 0:7ccf0e7e8a83 | 162 | // Data to exchange |
RobMeades | 0:7ccf0e7e8a83 | 163 | static const char send_data[] = "_____0000:0123456789012345678901234567890123456789" |
RobMeades | 0:7ccf0e7e8a83 | 164 | "01234567890123456789012345678901234567890123456789" |
RobMeades | 0:7ccf0e7e8a83 | 165 | "_____0100:0123456789012345678901234567890123456789" |
RobMeades | 0:7ccf0e7e8a83 | 166 | "01234567890123456789012345678901234567890123456789" |
RobMeades | 0:7ccf0e7e8a83 | 167 | "_____0200:0123456789012345678901234567890123456789" |
RobMeades | 0:7ccf0e7e8a83 | 168 | "01234567890123456789012345678901234567890123456789" |
RobMeades | 0:7ccf0e7e8a83 | 169 | "_____0300:0123456789012345678901234567890123456789" |
RobMeades | 0:7ccf0e7e8a83 | 170 | "01234567890123456789012345678901234567890123456789" |
RobMeades | 0:7ccf0e7e8a83 | 171 | "_____0400:0123456789012345678901234567890123456789" |
RobMeades | 0:7ccf0e7e8a83 | 172 | "01234567890123456789012345678901234567890123456789" |
RobMeades | 0:7ccf0e7e8a83 | 173 | "_____0500:0123456789012345678901234567890123456789" |
RobMeades | 0:7ccf0e7e8a83 | 174 | "01234567890123456789012345678901234567890123456789" |
RobMeades | 0:7ccf0e7e8a83 | 175 | "_____0600:0123456789012345678901234567890123456789" |
RobMeades | 0:7ccf0e7e8a83 | 176 | "01234567890123456789012345678901234567890123456789" |
RobMeades | 0:7ccf0e7e8a83 | 177 | "_____0700:0123456789012345678901234567890123456789" |
RobMeades | 0:7ccf0e7e8a83 | 178 | "01234567890123456789012345678901234567890123456789" |
RobMeades | 0:7ccf0e7e8a83 | 179 | "_____0800:0123456789012345678901234567890123456789" |
RobMeades | 0:7ccf0e7e8a83 | 180 | "01234567890123456789012345678901234567890123456789" |
RobMeades | 0:7ccf0e7e8a83 | 181 | "_____0900:0123456789012345678901234567890123456789" |
RobMeades | 0:7ccf0e7e8a83 | 182 | "01234567890123456789012345678901234567890123456789" |
RobMeades | 0:7ccf0e7e8a83 | 183 | "_____1000:0123456789012345678901234567890123456789" |
RobMeades | 0:7ccf0e7e8a83 | 184 | "01234567890123456789012345678901234567890123456789" |
RobMeades | 0:7ccf0e7e8a83 | 185 | "_____1100:0123456789012345678901234567890123456789" |
RobMeades | 0:7ccf0e7e8a83 | 186 | "01234567890123456789012345678901234567890123456789" |
RobMeades | 0:7ccf0e7e8a83 | 187 | "_____1200:0123456789012345678901234567890123456789" |
RobMeades | 0:7ccf0e7e8a83 | 188 | "01234567890123456789012345678901234567890123456789" |
RobMeades | 0:7ccf0e7e8a83 | 189 | "_____1300:0123456789012345678901234567890123456789" |
RobMeades | 0:7ccf0e7e8a83 | 190 | "01234567890123456789012345678901234567890123456789" |
RobMeades | 0:7ccf0e7e8a83 | 191 | "_____1400:0123456789012345678901234567890123456789" |
RobMeades | 0:7ccf0e7e8a83 | 192 | "01234567890123456789012345678901234567890123456789" |
RobMeades | 0:7ccf0e7e8a83 | 193 | "_____1500:0123456789012345678901234567890123456789" |
RobMeades | 0:7ccf0e7e8a83 | 194 | "01234567890123456789012345678901234567890123456789" |
RobMeades | 0:7ccf0e7e8a83 | 195 | "_____1600:0123456789012345678901234567890123456789" |
RobMeades | 0:7ccf0e7e8a83 | 196 | "01234567890123456789012345678901234567890123456789" |
RobMeades | 0:7ccf0e7e8a83 | 197 | "_____1700:0123456789012345678901234567890123456789" |
RobMeades | 0:7ccf0e7e8a83 | 198 | "01234567890123456789012345678901234567890123456789" |
RobMeades | 0:7ccf0e7e8a83 | 199 | "_____1800:0123456789012345678901234567890123456789" |
RobMeades | 0:7ccf0e7e8a83 | 200 | "01234567890123456789012345678901234567890123456789" |
RobMeades | 0:7ccf0e7e8a83 | 201 | "_____1900:0123456789012345678901234567890123456789" |
RobMeades | 0:7ccf0e7e8a83 | 202 | "01234567890123456789012345678901234567890123456789" |
RobMeades | 0:7ccf0e7e8a83 | 203 | "_____2000:0123456789012345678901234567890123456789" |
RobMeades | 0:7ccf0e7e8a83 | 204 | "01234567890123456789012345678901234567890123456789"; |
RobMeades | 0:7ccf0e7e8a83 | 205 | |
RobMeades | 0:7ccf0e7e8a83 | 206 | // ---------------------------------------------------------------- |
RobMeades | 0:7ccf0e7e8a83 | 207 | // PRIVATE FUNCTIONS |
RobMeades | 0:7ccf0e7e8a83 | 208 | // ---------------------------------------------------------------- |
RobMeades | 0:7ccf0e7e8a83 | 209 | |
RobMeades | 0:7ccf0e7e8a83 | 210 | #ifdef FEATURE_COMMON_PAL |
RobMeades | 0:7ccf0e7e8a83 | 211 | // Locks for debug prints |
RobMeades | 0:7ccf0e7e8a83 | 212 | static void lock() |
RobMeades | 0:7ccf0e7e8a83 | 213 | { |
RobMeades | 0:7ccf0e7e8a83 | 214 | mtx.lock(); |
RobMeades | 0:7ccf0e7e8a83 | 215 | } |
RobMeades | 0:7ccf0e7e8a83 | 216 | |
RobMeades | 0:7ccf0e7e8a83 | 217 | static void unlock() |
RobMeades | 0:7ccf0e7e8a83 | 218 | { |
RobMeades | 0:7ccf0e7e8a83 | 219 | mtx.unlock(); |
RobMeades | 0:7ccf0e7e8a83 | 220 | } |
RobMeades | 0:7ccf0e7e8a83 | 221 | #endif |
RobMeades | 0:7ccf0e7e8a83 | 222 | |
RobMeades | 0:7ccf0e7e8a83 | 223 | // Callback in case the connection goes down |
RobMeades | 0:7ccf0e7e8a83 | 224 | static void connection_down_cb(nsapi_error_t err) |
RobMeades | 0:7ccf0e7e8a83 | 225 | { |
RobMeades | 0:7ccf0e7e8a83 | 226 | connection_has_gone_down = true; |
RobMeades | 0:7ccf0e7e8a83 | 227 | } |
RobMeades | 0:7ccf0e7e8a83 | 228 | |
RobMeades | 0:7ccf0e7e8a83 | 229 | #ifdef MBED_CONF_APP_ECHO_SERVER |
RobMeades | 0:7ccf0e7e8a83 | 230 | // Make sure that size is greater than 0 and no more than limit, |
RobMeades | 0:7ccf0e7e8a83 | 231 | // useful since, when moduloing a very large number number, |
RobMeades | 0:7ccf0e7e8a83 | 232 | // compilers sometimes screw up and produce a small *negative* |
RobMeades | 0:7ccf0e7e8a83 | 233 | // number. Who knew? For example, GCC decided that |
RobMeades | 0:7ccf0e7e8a83 | 234 | // 492318453 (0x1d582ef5) modulo 508 was -47 (0xffffffd1). |
RobMeades | 0:7ccf0e7e8a83 | 235 | static int fix (int size, int limit) |
RobMeades | 0:7ccf0e7e8a83 | 236 | { |
RobMeades | 0:7ccf0e7e8a83 | 237 | if (size <= 0) { |
RobMeades | 0:7ccf0e7e8a83 | 238 | size = limit / 2; // better than 1 |
RobMeades | 0:7ccf0e7e8a83 | 239 | } else if (size > limit) { |
RobMeades | 0:7ccf0e7e8a83 | 240 | size = limit; |
RobMeades | 0:7ccf0e7e8a83 | 241 | } |
RobMeades | 0:7ccf0e7e8a83 | 242 | |
RobMeades | 0:7ccf0e7e8a83 | 243 | return size; |
RobMeades | 0:7ccf0e7e8a83 | 244 | } |
RobMeades | 0:7ccf0e7e8a83 | 245 | |
RobMeades | 0:7ccf0e7e8a83 | 246 | // Do a UDP socket echo test to a given host of a given packet size |
RobMeades | 0:7ccf0e7e8a83 | 247 | static void do_udp_echo(UDPSocket *sock, SocketAddress *host_address, int size) |
RobMeades | 0:7ccf0e7e8a83 | 248 | { |
RobMeades | 0:7ccf0e7e8a83 | 249 | bool success = false; |
RobMeades | 0:7ccf0e7e8a83 | 250 | void * recv_data = malloc (size); |
RobMeades | 0:7ccf0e7e8a83 | 251 | SocketAddress sender_address; |
RobMeades | 0:7ccf0e7e8a83 | 252 | TEST_ASSERT(recv_data != NULL); |
RobMeades | 0:7ccf0e7e8a83 | 253 | |
RobMeades | 0:7ccf0e7e8a83 | 254 | // Retry this a few times, don't want to fail due to a flaky link |
RobMeades | 0:7ccf0e7e8a83 | 255 | for (int x = 0; !success && (x < NUM_UDP_RETRIES); x++) { |
RobMeades | 0:7ccf0e7e8a83 | 256 | tr_debug("Echo testing UDP packet size %d byte(s), try %d.", size, x + 1); |
RobMeades | 0:7ccf0e7e8a83 | 257 | if ((sock->sendto(*host_address, (void*) send_data, size) == size) && |
RobMeades | 0:7ccf0e7e8a83 | 258 | (sock->recvfrom(&sender_address, recv_data, size) == size)) { |
RobMeades | 0:7ccf0e7e8a83 | 259 | TEST_ASSERT (memcmp(send_data, recv_data, size) == 0); |
RobMeades | 0:7ccf0e7e8a83 | 260 | TEST_ASSERT (strcmp(sender_address.get_ip_address(), host_address->get_ip_address()) == 0); |
RobMeades | 0:7ccf0e7e8a83 | 261 | TEST_ASSERT (sender_address.get_port() == host_address->get_port()); |
RobMeades | 0:7ccf0e7e8a83 | 262 | success = true; |
RobMeades | 0:7ccf0e7e8a83 | 263 | } |
RobMeades | 0:7ccf0e7e8a83 | 264 | } |
RobMeades | 0:7ccf0e7e8a83 | 265 | TEST_ASSERT (success); |
RobMeades | 0:7ccf0e7e8a83 | 266 | TEST_ASSERT(!connection_has_gone_down); |
RobMeades | 0:7ccf0e7e8a83 | 267 | |
RobMeades | 0:7ccf0e7e8a83 | 268 | free (recv_data); |
RobMeades | 0:7ccf0e7e8a83 | 269 | } |
RobMeades | 0:7ccf0e7e8a83 | 270 | |
RobMeades | 0:7ccf0e7e8a83 | 271 | // The asynchronous callback |
RobMeades | 0:7ccf0e7e8a83 | 272 | static void async_cb(bool *callback_triggered) |
RobMeades | 0:7ccf0e7e8a83 | 273 | { |
RobMeades | 0:7ccf0e7e8a83 | 274 | |
RobMeades | 0:7ccf0e7e8a83 | 275 | TEST_ASSERT (callback_triggered != NULL); |
RobMeades | 0:7ccf0e7e8a83 | 276 | *callback_triggered = true; |
RobMeades | 0:7ccf0e7e8a83 | 277 | } |
RobMeades | 0:7ccf0e7e8a83 | 278 | |
rob.meades@u-blox.com | 6:63dad754c267 | 279 | // Do a UDP echo but using the asynchronous interface; we can exchange |
RobMeades | 0:7ccf0e7e8a83 | 280 | // packets longer in size than one UDP packet this way |
RobMeades | 0:7ccf0e7e8a83 | 281 | static void do_udp_echo_async(UDPSocket *sock, SocketAddress *host_address, |
RobMeades | 0:7ccf0e7e8a83 | 282 | int size, bool *callback_triggered) |
RobMeades | 0:7ccf0e7e8a83 | 283 | { |
RobMeades | 0:7ccf0e7e8a83 | 284 | void * recv_data = malloc (size); |
RobMeades | 0:7ccf0e7e8a83 | 285 | int recv_size = 0; |
RobMeades | 0:7ccf0e7e8a83 | 286 | SocketAddress sender_address; |
RobMeades | 0:7ccf0e7e8a83 | 287 | Timer timer; |
RobMeades | 0:7ccf0e7e8a83 | 288 | int x, y, z; |
RobMeades | 0:7ccf0e7e8a83 | 289 | TEST_ASSERT(recv_data != NULL); |
RobMeades | 0:7ccf0e7e8a83 | 290 | |
RobMeades | 0:7ccf0e7e8a83 | 291 | *callback_triggered = false; |
RobMeades | 0:7ccf0e7e8a83 | 292 | for (y = 0; (recv_size < size) && (y < NUM_UDP_RETRIES); y++) { |
RobMeades | 0:7ccf0e7e8a83 | 293 | tr_debug("Echo testing UDP packet size %d byte(s) async, try %d.", size, y + 1); |
RobMeades | 0:7ccf0e7e8a83 | 294 | recv_size = 0; |
RobMeades | 0:7ccf0e7e8a83 | 295 | // Retry this a few times, don't want to fail due to a flaky link |
RobMeades | 0:7ccf0e7e8a83 | 296 | if (sock->sendto(*host_address, (void *) send_data, size) == size) { |
RobMeades | 0:7ccf0e7e8a83 | 297 | // Wait for all the echoed data to arrive |
RobMeades | 0:7ccf0e7e8a83 | 298 | timer.start(); |
RobMeades | 0:7ccf0e7e8a83 | 299 | while ((recv_size < size) && (timer.read_ms() < ASYNC_TEST_WAIT_TIME)) { |
RobMeades | 0:7ccf0e7e8a83 | 300 | if (*callback_triggered) { |
RobMeades | 0:7ccf0e7e8a83 | 301 | *callback_triggered = false; |
RobMeades | 0:7ccf0e7e8a83 | 302 | x = sock->recvfrom(&sender_address, (char *) recv_data + recv_size, size); |
RobMeades | 0:7ccf0e7e8a83 | 303 | if (x > 0) { |
RobMeades | 0:7ccf0e7e8a83 | 304 | recv_size += x; |
RobMeades | 0:7ccf0e7e8a83 | 305 | } |
RobMeades | 0:7ccf0e7e8a83 | 306 | tr_debug("%d byte(s) echoed back so far, %d to go.", recv_size, size - recv_size); |
RobMeades | 0:7ccf0e7e8a83 | 307 | TEST_ASSERT(strcmp(sender_address.get_ip_address(), host_address->get_ip_address()) == 0); |
RobMeades | 0:7ccf0e7e8a83 | 308 | TEST_ASSERT(sender_address.get_port() == host_address->get_port()); |
RobMeades | 0:7ccf0e7e8a83 | 309 | } |
RobMeades | 0:7ccf0e7e8a83 | 310 | wait_ms(10); |
RobMeades | 0:7ccf0e7e8a83 | 311 | } |
RobMeades | 0:7ccf0e7e8a83 | 312 | timer.stop(); |
RobMeades | 0:7ccf0e7e8a83 | 313 | timer.reset(); |
RobMeades | 0:7ccf0e7e8a83 | 314 | |
RobMeades | 0:7ccf0e7e8a83 | 315 | // If everything arrived back, check it's the same as we sent |
RobMeades | 0:7ccf0e7e8a83 | 316 | if (recv_size == size) { |
RobMeades | 0:7ccf0e7e8a83 | 317 | z = memcmp(send_data, recv_data, size); |
RobMeades | 0:7ccf0e7e8a83 | 318 | if (z != 0) { |
RobMeades | 0:7ccf0e7e8a83 | 319 | tr_debug("WARNING: mismatch, retrying"); |
RobMeades | 0:7ccf0e7e8a83 | 320 | tr_debug("Sent %d, |%*.*s|", size, size, size, send_data); |
RobMeades | 0:7ccf0e7e8a83 | 321 | tr_debug("Rcvd %d, |%*.*s|", size, size, size, (char *) recv_data); |
RobMeades | 0:7ccf0e7e8a83 | 322 | // If things don't match, it could be due to data loss (this is UDP |
RobMeades | 0:7ccf0e7e8a83 | 323 | // you know...), so set recv_size to 0 to cause another try |
RobMeades | 0:7ccf0e7e8a83 | 324 | recv_size = 0; |
RobMeades | 0:7ccf0e7e8a83 | 325 | } |
RobMeades | 0:7ccf0e7e8a83 | 326 | } |
RobMeades | 0:7ccf0e7e8a83 | 327 | } |
RobMeades | 0:7ccf0e7e8a83 | 328 | } |
RobMeades | 0:7ccf0e7e8a83 | 329 | |
RobMeades | 0:7ccf0e7e8a83 | 330 | TEST_ASSERT(recv_size == size); |
RobMeades | 0:7ccf0e7e8a83 | 331 | TEST_ASSERT(!connection_has_gone_down); |
RobMeades | 0:7ccf0e7e8a83 | 332 | |
RobMeades | 0:7ccf0e7e8a83 | 333 | free (recv_data); |
RobMeades | 0:7ccf0e7e8a83 | 334 | } |
RobMeades | 0:7ccf0e7e8a83 | 335 | |
RobMeades | 0:7ccf0e7e8a83 | 336 | // Send an entire TCP data buffer until done |
RobMeades | 0:7ccf0e7e8a83 | 337 | static int sendAll(TCPSocket *sock, const char *data, int size) |
RobMeades | 0:7ccf0e7e8a83 | 338 | { |
RobMeades | 0:7ccf0e7e8a83 | 339 | int x; |
RobMeades | 0:7ccf0e7e8a83 | 340 | int count = 0; |
RobMeades | 0:7ccf0e7e8a83 | 341 | Timer timer; |
RobMeades | 0:7ccf0e7e8a83 | 342 | |
RobMeades | 0:7ccf0e7e8a83 | 343 | timer.start(); |
RobMeades | 0:7ccf0e7e8a83 | 344 | while ((count < size) && (timer.read_ms() < 10000)) { |
RobMeades | 0:7ccf0e7e8a83 | 345 | x = sock->send(data + count, size - count); |
RobMeades | 0:7ccf0e7e8a83 | 346 | if (x > 0) { |
RobMeades | 0:7ccf0e7e8a83 | 347 | count += x; |
RobMeades | 0:7ccf0e7e8a83 | 348 | tr_debug("%d byte(s) sent, %d left to send.", count, size - count); |
RobMeades | 0:7ccf0e7e8a83 | 349 | } |
RobMeades | 0:7ccf0e7e8a83 | 350 | wait_ms(10); |
RobMeades | 0:7ccf0e7e8a83 | 351 | } |
RobMeades | 0:7ccf0e7e8a83 | 352 | timer.stop(); |
RobMeades | 0:7ccf0e7e8a83 | 353 | |
RobMeades | 0:7ccf0e7e8a83 | 354 | return count; |
RobMeades | 0:7ccf0e7e8a83 | 355 | } |
RobMeades | 0:7ccf0e7e8a83 | 356 | |
rob.meades@u-blox.com | 6:63dad754c267 | 357 | // Do a TCP echo but using the asynchronous interface |
RobMeades | 0:7ccf0e7e8a83 | 358 | static void do_tcp_echo_async(TCPSocket *sock, int size, bool *callback_triggered) |
RobMeades | 0:7ccf0e7e8a83 | 359 | { |
RobMeades | 0:7ccf0e7e8a83 | 360 | void * recv_data = malloc (size); |
RobMeades | 0:7ccf0e7e8a83 | 361 | int recv_size = 0; |
RobMeades | 0:7ccf0e7e8a83 | 362 | int x, y; |
RobMeades | 0:7ccf0e7e8a83 | 363 | Timer timer; |
RobMeades | 0:7ccf0e7e8a83 | 364 | TEST_ASSERT(recv_data != NULL); |
RobMeades | 0:7ccf0e7e8a83 | 365 | |
RobMeades | 0:7ccf0e7e8a83 | 366 | *callback_triggered = false; |
RobMeades | 0:7ccf0e7e8a83 | 367 | tr_debug("Echo testing TCP packet size %d byte(s) async.", size); |
RobMeades | 0:7ccf0e7e8a83 | 368 | TEST_ASSERT (sendAll(sock, send_data, size) == size); |
RobMeades | 0:7ccf0e7e8a83 | 369 | |
RobMeades | 0:7ccf0e7e8a83 | 370 | // Wait for all the echoed data to arrive |
RobMeades | 0:7ccf0e7e8a83 | 371 | timer.start(); |
RobMeades | 0:7ccf0e7e8a83 | 372 | while ((recv_size < size) && (timer.read_ms() < ASYNC_TEST_WAIT_TIME)) { |
RobMeades | 0:7ccf0e7e8a83 | 373 | if (*callback_triggered) { |
RobMeades | 0:7ccf0e7e8a83 | 374 | *callback_triggered = false; |
RobMeades | 0:7ccf0e7e8a83 | 375 | x = sock->recv((char *) recv_data + recv_size, size); |
RobMeades | 0:7ccf0e7e8a83 | 376 | TEST_ASSERT(x > 0); |
RobMeades | 0:7ccf0e7e8a83 | 377 | recv_size += x; |
RobMeades | 0:7ccf0e7e8a83 | 378 | tr_debug("%d byte(s) echoed back so far, %d to go.", recv_size, size - recv_size); |
RobMeades | 0:7ccf0e7e8a83 | 379 | } |
RobMeades | 0:7ccf0e7e8a83 | 380 | wait_ms(10); |
RobMeades | 0:7ccf0e7e8a83 | 381 | } |
RobMeades | 0:7ccf0e7e8a83 | 382 | TEST_ASSERT(recv_size == size); |
RobMeades | 0:7ccf0e7e8a83 | 383 | y = memcmp(send_data, recv_data, size); |
RobMeades | 0:7ccf0e7e8a83 | 384 | if (y != 0) { |
RobMeades | 0:7ccf0e7e8a83 | 385 | tr_debug("Sent %d, |%*.*s|", size, size, size, send_data); |
RobMeades | 0:7ccf0e7e8a83 | 386 | tr_debug("Rcvd %d, |%*.*s|", size, size, size, (char *) recv_data); |
RobMeades | 0:7ccf0e7e8a83 | 387 | TEST_ASSERT(false); |
RobMeades | 0:7ccf0e7e8a83 | 388 | } |
RobMeades | 0:7ccf0e7e8a83 | 389 | timer.stop(); |
RobMeades | 0:7ccf0e7e8a83 | 390 | |
RobMeades | 0:7ccf0e7e8a83 | 391 | TEST_ASSERT(!connection_has_gone_down); |
RobMeades | 0:7ccf0e7e8a83 | 392 | |
RobMeades | 0:7ccf0e7e8a83 | 393 | free (recv_data); |
RobMeades | 0:7ccf0e7e8a83 | 394 | } |
RobMeades | 0:7ccf0e7e8a83 | 395 | #endif |
RobMeades | 0:7ccf0e7e8a83 | 396 | |
RobMeades | 0:7ccf0e7e8a83 | 397 | // Get NTP time from a socket |
RobMeades | 0:7ccf0e7e8a83 | 398 | static void do_ntp_sock (UDPSocket *sock, SocketAddress ntp_address) |
RobMeades | 0:7ccf0e7e8a83 | 399 | { |
RobMeades | 0:7ccf0e7e8a83 | 400 | char ntp_values[48] = { 0 }; |
RobMeades | 0:7ccf0e7e8a83 | 401 | time_t timestamp = 0; |
RobMeades | 0:7ccf0e7e8a83 | 402 | struct tm *localTime; |
RobMeades | 0:7ccf0e7e8a83 | 403 | char timeString[25]; |
RobMeades | 0:7ccf0e7e8a83 | 404 | time_t TIME1970 = 2208988800U; |
RobMeades | 0:7ccf0e7e8a83 | 405 | int len; |
RobMeades | 0:7ccf0e7e8a83 | 406 | bool comms_done = false; |
RobMeades | 0:7ccf0e7e8a83 | 407 | |
RobMeades | 0:7ccf0e7e8a83 | 408 | ntp_values[0] = '\x1b'; |
RobMeades | 0:7ccf0e7e8a83 | 409 | |
RobMeades | 0:7ccf0e7e8a83 | 410 | // Retry this a few times, don't want to fail due to a flaky link |
RobMeades | 0:7ccf0e7e8a83 | 411 | for (unsigned int x = 0; !comms_done && (x < NUM_UDP_RETRIES); x++) { |
RobMeades | 0:7ccf0e7e8a83 | 412 | sock->sendto(ntp_address, (void*) ntp_values, sizeof(ntp_values)); |
RobMeades | 0:7ccf0e7e8a83 | 413 | len = sock->recvfrom(&ntp_address, (void*) ntp_values, sizeof(ntp_values)); |
RobMeades | 0:7ccf0e7e8a83 | 414 | if (len > 0) { |
RobMeades | 0:7ccf0e7e8a83 | 415 | comms_done = true; |
RobMeades | 0:7ccf0e7e8a83 | 416 | } |
RobMeades | 0:7ccf0e7e8a83 | 417 | } |
RobMeades | 0:7ccf0e7e8a83 | 418 | TEST_ASSERT (comms_done); |
RobMeades | 0:7ccf0e7e8a83 | 419 | |
RobMeades | 0:7ccf0e7e8a83 | 420 | tr_debug("UDP: %d byte(s) returned by NTP server.", len); |
RobMeades | 0:7ccf0e7e8a83 | 421 | if (len >= 43) { |
RobMeades | 0:7ccf0e7e8a83 | 422 | timestamp |= ((int) *(ntp_values + 40)) << 24; |
RobMeades | 0:7ccf0e7e8a83 | 423 | timestamp |= ((int) *(ntp_values + 41)) << 16; |
RobMeades | 0:7ccf0e7e8a83 | 424 | timestamp |= ((int) *(ntp_values + 42)) << 8; |
RobMeades | 0:7ccf0e7e8a83 | 425 | timestamp |= ((int) *(ntp_values + 43)); |
RobMeades | 0:7ccf0e7e8a83 | 426 | timestamp -= TIME1970; |
RobMeades | 0:7ccf0e7e8a83 | 427 | srand (timestamp); |
RobMeades | 0:7ccf0e7e8a83 | 428 | tr_debug("srand() called"); |
RobMeades | 0:7ccf0e7e8a83 | 429 | localTime = localtime(×tamp); |
RobMeades | 0:7ccf0e7e8a83 | 430 | if (localTime) { |
RobMeades | 0:7ccf0e7e8a83 | 431 | if (strftime(timeString, sizeof(timeString), "%a %b %d %H:%M:%S %Y", localTime) > 0) { |
RobMeades | 0:7ccf0e7e8a83 | 432 | printf("NTP timestamp is %s.\n", timeString); |
RobMeades | 0:7ccf0e7e8a83 | 433 | } |
RobMeades | 0:7ccf0e7e8a83 | 434 | } |
RobMeades | 0:7ccf0e7e8a83 | 435 | } |
RobMeades | 0:7ccf0e7e8a83 | 436 | } |
RobMeades | 0:7ccf0e7e8a83 | 437 | |
RobMeades | 0:7ccf0e7e8a83 | 438 | // Get NTP time |
rob.meades@u-blox.com | 6:63dad754c267 | 439 | static void do_ntp(UbloxATCellularInterface *interface) |
RobMeades | 0:7ccf0e7e8a83 | 440 | { |
RobMeades | 0:7ccf0e7e8a83 | 441 | UDPSocket sock; |
RobMeades | 0:7ccf0e7e8a83 | 442 | SocketAddress host_address; |
RobMeades | 0:7ccf0e7e8a83 | 443 | |
rob.meades@u-blox.com | 6:63dad754c267 | 444 | TEST_ASSERT(sock.open(interface) == 0) |
RobMeades | 0:7ccf0e7e8a83 | 445 | |
rob.meades@u-blox.com | 6:63dad754c267 | 446 | TEST_ASSERT(interface->gethostbyname(MBED_CONF_APP_NTP_SERVER, &host_address) == 0); |
RobMeades | 0:7ccf0e7e8a83 | 447 | host_address.set_port(MBED_CONF_APP_NTP_PORT); |
RobMeades | 0:7ccf0e7e8a83 | 448 | |
RobMeades | 0:7ccf0e7e8a83 | 449 | tr_debug("UDP: NIST server %s address: %s on port %d.", MBED_CONF_APP_NTP_SERVER, |
RobMeades | 0:7ccf0e7e8a83 | 450 | host_address.get_ip_address(), host_address.get_port()); |
RobMeades | 0:7ccf0e7e8a83 | 451 | |
RobMeades | 0:7ccf0e7e8a83 | 452 | sock.set_timeout(10000); |
RobMeades | 0:7ccf0e7e8a83 | 453 | |
RobMeades | 0:7ccf0e7e8a83 | 454 | do_ntp_sock(&sock, host_address); |
RobMeades | 0:7ccf0e7e8a83 | 455 | |
RobMeades | 0:7ccf0e7e8a83 | 456 | sock.close(); |
RobMeades | 0:7ccf0e7e8a83 | 457 | } |
RobMeades | 0:7ccf0e7e8a83 | 458 | |
RobMeades | 0:7ccf0e7e8a83 | 459 | // Use a connection, checking that it is good |
rob.meades@u-blox.com | 6:63dad754c267 | 460 | static void use_connection(UbloxATCellularInterface *interface) |
RobMeades | 0:7ccf0e7e8a83 | 461 | { |
rob.meades@u-blox.com | 6:63dad754c267 | 462 | const char * ip_address = interface->get_ip_address(); |
rob.meades@u-blox.com | 6:63dad754c267 | 463 | const char * net_mask = interface->get_netmask(); |
rob.meades@u-blox.com | 6:63dad754c267 | 464 | const char * gateway = interface->get_gateway(); |
RobMeades | 0:7ccf0e7e8a83 | 465 | |
rob.meades@u-blox.com | 6:63dad754c267 | 466 | TEST_ASSERT(interface->is_connected()); |
RobMeades | 0:7ccf0e7e8a83 | 467 | |
RobMeades | 0:7ccf0e7e8a83 | 468 | TEST_ASSERT(ip_address != NULL); |
RobMeades | 0:7ccf0e7e8a83 | 469 | tr_debug ("IP address %s.", ip_address); |
RobMeades | 0:7ccf0e7e8a83 | 470 | TEST_ASSERT(net_mask == NULL); |
RobMeades | 0:7ccf0e7e8a83 | 471 | tr_debug ("Net mask %s.", net_mask); |
RobMeades | 0:7ccf0e7e8a83 | 472 | TEST_ASSERT(gateway != NULL); |
RobMeades | 0:7ccf0e7e8a83 | 473 | tr_debug ("Gateway %s.", gateway); |
RobMeades | 0:7ccf0e7e8a83 | 474 | |
rob.meades@u-blox.com | 6:63dad754c267 | 475 | do_ntp(interface); |
RobMeades | 0:7ccf0e7e8a83 | 476 | TEST_ASSERT(!connection_has_gone_down); |
RobMeades | 0:7ccf0e7e8a83 | 477 | } |
RobMeades | 0:7ccf0e7e8a83 | 478 | |
RobMeades | 0:7ccf0e7e8a83 | 479 | // Drop a connection and check that it has dropped |
rob.meades@u-blox.com | 6:63dad754c267 | 480 | static void drop_connection(UbloxATCellularInterface *interface) |
RobMeades | 0:7ccf0e7e8a83 | 481 | { |
rob.meades@u-blox.com | 6:63dad754c267 | 482 | TEST_ASSERT(interface->disconnect() == 0); |
RobMeades | 0:7ccf0e7e8a83 | 483 | TEST_ASSERT(connection_has_gone_down); |
RobMeades | 0:7ccf0e7e8a83 | 484 | connection_has_gone_down = false; |
rob.meades@u-blox.com | 6:63dad754c267 | 485 | TEST_ASSERT(!interface->is_connected()); |
RobMeades | 0:7ccf0e7e8a83 | 486 | } |
RobMeades | 0:7ccf0e7e8a83 | 487 | |
RobMeades | 0:7ccf0e7e8a83 | 488 | // ---------------------------------------------------------------- |
RobMeades | 0:7ccf0e7e8a83 | 489 | // TESTS |
RobMeades | 0:7ccf0e7e8a83 | 490 | // ---------------------------------------------------------------- |
RobMeades | 0:7ccf0e7e8a83 | 491 | |
RobMeades | 12:ff6fac481487 | 492 | // Tests of stuff in the base class |
RobMeades | 12:ff6fac481487 | 493 | void test_base_class() { |
RobMeades | 12:ff6fac481487 | 494 | const char *imei; |
RobMeades | 12:ff6fac481487 | 495 | const char *meid; |
RobMeades | 12:ff6fac481487 | 496 | const char *imsi; |
RobMeades | 12:ff6fac481487 | 497 | const char *iccid; |
RobMeades | 12:ff6fac481487 | 498 | int rssi; |
RobMeades | 12:ff6fac481487 | 499 | |
RobMeades | 12:ff6fac481487 | 500 | // Power-up the modem |
RobMeades | 12:ff6fac481487 | 501 | interface->init(); |
RobMeades | 12:ff6fac481487 | 502 | |
RobMeades | 12:ff6fac481487 | 503 | // Check all of the IMEI, MEID, IMSI and ICCID calls |
RobMeades | 12:ff6fac481487 | 504 | imei = interface->imei(); |
RobMeades | 12:ff6fac481487 | 505 | if (imei != NULL) { |
RobMeades | 12:ff6fac481487 | 506 | tr_debug("IMEI is %s.", imei); |
RobMeades | 12:ff6fac481487 | 507 | } else { |
RobMeades | 12:ff6fac481487 | 508 | TEST_ASSERT(false); |
RobMeades | 12:ff6fac481487 | 509 | } |
RobMeades | 12:ff6fac481487 | 510 | |
RobMeades | 12:ff6fac481487 | 511 | meid = interface->meid(); |
RobMeades | 12:ff6fac481487 | 512 | if (meid != NULL) { |
RobMeades | 12:ff6fac481487 | 513 | tr_debug("MEID is %s.", meid); |
RobMeades | 12:ff6fac481487 | 514 | } else { |
RobMeades | 12:ff6fac481487 | 515 | TEST_ASSERT(false); |
RobMeades | 12:ff6fac481487 | 516 | } |
RobMeades | 12:ff6fac481487 | 517 | |
RobMeades | 12:ff6fac481487 | 518 | imsi = interface->imsi(); |
RobMeades | 12:ff6fac481487 | 519 | if (imsi != NULL) { |
RobMeades | 12:ff6fac481487 | 520 | tr_debug("IMSI is %s.", imsi); |
RobMeades | 12:ff6fac481487 | 521 | } else { |
RobMeades | 12:ff6fac481487 | 522 | TEST_ASSERT(false); |
RobMeades | 12:ff6fac481487 | 523 | } |
RobMeades | 12:ff6fac481487 | 524 | |
RobMeades | 12:ff6fac481487 | 525 | iccid = interface->iccid(); |
RobMeades | 12:ff6fac481487 | 526 | if (iccid != NULL) { |
RobMeades | 12:ff6fac481487 | 527 | tr_debug("ICCID is %s.", iccid); |
RobMeades | 12:ff6fac481487 | 528 | } else { |
RobMeades | 12:ff6fac481487 | 529 | TEST_ASSERT(false); |
RobMeades | 12:ff6fac481487 | 530 | } |
RobMeades | 12:ff6fac481487 | 531 | |
RobMeades | 12:ff6fac481487 | 532 | // Check the RSSI call at least doesn't assert |
RobMeades | 12:ff6fac481487 | 533 | rssi = interface->rssi(); |
RobMeades | 12:ff6fac481487 | 534 | tr_debug("RSSI is %d dBm.", rssi); |
RobMeades | 12:ff6fac481487 | 535 | |
RobMeades | 12:ff6fac481487 | 536 | // Now connect and check that the answers for the |
RobMeades | 12:ff6fac481487 | 537 | // static fields are the same while connected |
RobMeades | 12:ff6fac481487 | 538 | TEST_ASSERT(interface->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN, |
RobMeades | 12:ff6fac481487 | 539 | MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0); |
RobMeades | 12:ff6fac481487 | 540 | |
RobMeades | 12:ff6fac481487 | 541 | TEST_ASSERT(strcmp(imei, interface->imei()) == 0); |
RobMeades | 12:ff6fac481487 | 542 | TEST_ASSERT(strcmp(meid, interface->meid()) == 0); |
RobMeades | 12:ff6fac481487 | 543 | TEST_ASSERT(strcmp(imsi, interface->imsi()) == 0); |
RobMeades | 12:ff6fac481487 | 544 | TEST_ASSERT(strcmp(iccid, interface->iccid()) == 0); |
RobMeades | 12:ff6fac481487 | 545 | |
RobMeades | 12:ff6fac481487 | 546 | // Check that the RSSI call still doesn't assert |
RobMeades | 12:ff6fac481487 | 547 | rssi = interface->rssi(); |
RobMeades | 12:ff6fac481487 | 548 | tr_debug("RSSI is %d dBm.", rssi); |
RobMeades | 12:ff6fac481487 | 549 | } |
RobMeades | 12:ff6fac481487 | 550 | |
RobMeades | 0:7ccf0e7e8a83 | 551 | // Call srand() using the NTP server |
RobMeades | 0:7ccf0e7e8a83 | 552 | void test_set_randomise() { |
RobMeades | 0:7ccf0e7e8a83 | 553 | UDPSocket sock; |
RobMeades | 0:7ccf0e7e8a83 | 554 | SocketAddress host_address; |
RobMeades | 0:7ccf0e7e8a83 | 555 | |
rob.meades@u-blox.com | 6:63dad754c267 | 556 | TEST_ASSERT(interface->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN, |
rob.meades@u-blox.com | 6:63dad754c267 | 557 | MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0); |
rob.meades@u-blox.com | 6:63dad754c267 | 558 | do_ntp(interface); |
RobMeades | 0:7ccf0e7e8a83 | 559 | TEST_ASSERT(!connection_has_gone_down); |
rob.meades@u-blox.com | 6:63dad754c267 | 560 | drop_connection(interface); |
RobMeades | 0:7ccf0e7e8a83 | 561 | } |
RobMeades | 0:7ccf0e7e8a83 | 562 | |
RobMeades | 0:7ccf0e7e8a83 | 563 | #ifdef MBED_CONF_APP_ECHO_SERVER |
RobMeades | 0:7ccf0e7e8a83 | 564 | |
RobMeades | 0:7ccf0e7e8a83 | 565 | // Test UDP data exchange |
RobMeades | 0:7ccf0e7e8a83 | 566 | void test_udp_echo() { |
RobMeades | 0:7ccf0e7e8a83 | 567 | UDPSocket sock; |
RobMeades | 0:7ccf0e7e8a83 | 568 | SocketAddress host_address; |
RobMeades | 0:7ccf0e7e8a83 | 569 | SocketAddress local_address; |
RobMeades | 0:7ccf0e7e8a83 | 570 | int x; |
RobMeades | 0:7ccf0e7e8a83 | 571 | int size; |
RobMeades | 0:7ccf0e7e8a83 | 572 | |
rob.meades@u-blox.com | 6:63dad754c267 | 573 | TEST_ASSERT(interface->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN, |
rob.meades@u-blox.com | 6:63dad754c267 | 574 | MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0); |
RobMeades | 0:7ccf0e7e8a83 | 575 | |
rob.meades@u-blox.com | 6:63dad754c267 | 576 | TEST_ASSERT(interface->gethostbyname(MBED_CONF_APP_ECHO_SERVER, &host_address) == 0); |
RobMeades | 0:7ccf0e7e8a83 | 577 | host_address.set_port(MBED_CONF_APP_ECHO_UDP_PORT); |
RobMeades | 0:7ccf0e7e8a83 | 578 | |
RobMeades | 0:7ccf0e7e8a83 | 579 | tr_debug("UDP: Server %s address: %s on port %d.", MBED_CONF_APP_ECHO_SERVER, |
RobMeades | 0:7ccf0e7e8a83 | 580 | host_address.get_ip_address(), host_address.get_port()); |
RobMeades | 0:7ccf0e7e8a83 | 581 | |
rob.meades@u-blox.com | 6:63dad754c267 | 582 | TEST_ASSERT(sock.open(interface) == 0) |
RobMeades | 0:7ccf0e7e8a83 | 583 | |
RobMeades | 0:7ccf0e7e8a83 | 584 | // Do a bind, just for the helluvit |
RobMeades | 0:7ccf0e7e8a83 | 585 | local_address.set_port(MBED_CONF_APP_LOCAL_PORT); |
RobMeades | 0:7ccf0e7e8a83 | 586 | TEST_ASSERT(sock.bind(local_address) == 0); |
RobMeades | 0:7ccf0e7e8a83 | 587 | |
RobMeades | 0:7ccf0e7e8a83 | 588 | sock.set_timeout(10000); |
RobMeades | 0:7ccf0e7e8a83 | 589 | |
RobMeades | 0:7ccf0e7e8a83 | 590 | // Test min, max, and some random sizes in-between |
RobMeades | 0:7ccf0e7e8a83 | 591 | do_udp_echo(&sock, &host_address, 1); |
RobMeades | 0:7ccf0e7e8a83 | 592 | do_udp_echo(&sock, &host_address, MBED_CONF_APP_UDP_MAX_PACKET_SIZE); |
RobMeades | 0:7ccf0e7e8a83 | 593 | for (x = 0; x < 10; x++) { |
RobMeades | 0:7ccf0e7e8a83 | 594 | size = (rand() % MBED_CONF_APP_UDP_MAX_PACKET_SIZE) + 1; |
RobMeades | 0:7ccf0e7e8a83 | 595 | size = fix(size, MBED_CONF_APP_UDP_MAX_PACKET_SIZE); |
RobMeades | 0:7ccf0e7e8a83 | 596 | do_udp_echo(&sock, &host_address, size); |
RobMeades | 0:7ccf0e7e8a83 | 597 | } |
RobMeades | 0:7ccf0e7e8a83 | 598 | |
RobMeades | 0:7ccf0e7e8a83 | 599 | sock.close(); |
rob.meades@u-blox.com | 6:63dad754c267 | 600 | drop_connection(interface); |
RobMeades | 0:7ccf0e7e8a83 | 601 | tr_debug("%d UDP packets of size up to %d byte(s) echoed successfully.", |
RobMeades | 0:7ccf0e7e8a83 | 602 | x, MBED_CONF_APP_UDP_MAX_PACKET_SIZE); |
RobMeades | 0:7ccf0e7e8a83 | 603 | } |
RobMeades | 0:7ccf0e7e8a83 | 604 | |
RobMeades | 0:7ccf0e7e8a83 | 605 | // Test many different sizes of UDP data arriving at once |
RobMeades | 0:7ccf0e7e8a83 | 606 | void test_udp_echo_recv_sizes() { |
RobMeades | 0:7ccf0e7e8a83 | 607 | UDPSocket sock; |
RobMeades | 0:7ccf0e7e8a83 | 608 | SocketAddress host_address; |
RobMeades | 0:7ccf0e7e8a83 | 609 | int x, y, z; |
RobMeades | 0:7ccf0e7e8a83 | 610 | int size; |
RobMeades | 0:7ccf0e7e8a83 | 611 | int tries = 0; |
RobMeades | 0:7ccf0e7e8a83 | 612 | unsigned int offset; |
RobMeades | 0:7ccf0e7e8a83 | 613 | char * recv_data; |
RobMeades | 0:7ccf0e7e8a83 | 614 | bool packetLoss; |
RobMeades | 0:7ccf0e7e8a83 | 615 | bool sendSuccess; |
RobMeades | 0:7ccf0e7e8a83 | 616 | Timer timer; |
RobMeades | 0:7ccf0e7e8a83 | 617 | |
rob.meades@u-blox.com | 6:63dad754c267 | 618 | interface->deinit(); |
rob.meades@u-blox.com | 6:63dad754c267 | 619 | TEST_ASSERT(interface->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN, |
rob.meades@u-blox.com | 6:63dad754c267 | 620 | MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0); |
RobMeades | 0:7ccf0e7e8a83 | 621 | |
rob.meades@u-blox.com | 6:63dad754c267 | 622 | TEST_ASSERT(interface->gethostbyname(MBED_CONF_APP_ECHO_SERVER, &host_address) == 0); |
RobMeades | 0:7ccf0e7e8a83 | 623 | host_address.set_port(MBED_CONF_APP_ECHO_UDP_PORT); |
RobMeades | 0:7ccf0e7e8a83 | 624 | |
RobMeades | 0:7ccf0e7e8a83 | 625 | tr_debug("UDP: Server %s address: %s on port %d.", MBED_CONF_APP_ECHO_SERVER, |
RobMeades | 0:7ccf0e7e8a83 | 626 | host_address.get_ip_address(), host_address.get_port()); |
RobMeades | 0:7ccf0e7e8a83 | 627 | |
rob.meades@u-blox.com | 6:63dad754c267 | 628 | TEST_ASSERT(sock.open(interface) == 0) |
RobMeades | 0:7ccf0e7e8a83 | 629 | |
RobMeades | 0:7ccf0e7e8a83 | 630 | do { |
RobMeades | 0:7ccf0e7e8a83 | 631 | tr_debug("--- UDP packet size test, test try %d, flushing input buffers", tries + 1); |
RobMeades | 0:7ccf0e7e8a83 | 632 | // First of all, clear any junk from the socket |
RobMeades | 0:7ccf0e7e8a83 | 633 | sock.set_timeout(1000); |
RobMeades | 0:7ccf0e7e8a83 | 634 | recv_data = (char *) malloc (MBED_CONF_APP_UDP_MAX_PACKET_SIZE); |
RobMeades | 0:7ccf0e7e8a83 | 635 | TEST_ASSERT(recv_data != NULL); |
RobMeades | 0:7ccf0e7e8a83 | 636 | while (sock.recvfrom(&host_address, (void *) recv_data, MBED_CONF_APP_UDP_MAX_PACKET_SIZE) > 0) { |
RobMeades | 0:7ccf0e7e8a83 | 637 | // Throw it away |
RobMeades | 0:7ccf0e7e8a83 | 638 | } |
RobMeades | 0:7ccf0e7e8a83 | 639 | free (recv_data); |
RobMeades | 0:7ccf0e7e8a83 | 640 | |
RobMeades | 0:7ccf0e7e8a83 | 641 | sock.set_timeout(10000); |
RobMeades | 0:7ccf0e7e8a83 | 642 | |
RobMeades | 0:7ccf0e7e8a83 | 643 | // Throw random sized UDP packets up... |
RobMeades | 0:7ccf0e7e8a83 | 644 | x = 0; |
RobMeades | 0:7ccf0e7e8a83 | 645 | offset = 0; |
RobMeades | 0:7ccf0e7e8a83 | 646 | while (offset < sizeof (send_data)) { |
RobMeades | 0:7ccf0e7e8a83 | 647 | size = (rand() % (MBED_CONF_APP_UDP_MAX_PACKET_SIZE / 2)) + 1; |
RobMeades | 0:7ccf0e7e8a83 | 648 | size = fix(size, MBED_CONF_APP_UDP_MAX_PACKET_SIZE / 2); |
RobMeades | 0:7ccf0e7e8a83 | 649 | if (offset + size > sizeof (send_data)) { |
RobMeades | 0:7ccf0e7e8a83 | 650 | size = sizeof (send_data) - offset; |
RobMeades | 0:7ccf0e7e8a83 | 651 | } |
RobMeades | 0:7ccf0e7e8a83 | 652 | sendSuccess = false; |
RobMeades | 0:7ccf0e7e8a83 | 653 | for (y = 0; !sendSuccess && (y < NUM_UDP_RETRIES); y++) { |
RobMeades | 0:7ccf0e7e8a83 | 654 | tr_debug("Sending UDP packet number %d, size %d byte(s), send try %d.", x + 1, size, y + 1); |
RobMeades | 0:7ccf0e7e8a83 | 655 | if (sock.sendto(host_address, (void *) (send_data + offset), size) == size) { |
RobMeades | 0:7ccf0e7e8a83 | 656 | sendSuccess = true; |
RobMeades | 0:7ccf0e7e8a83 | 657 | offset += size; |
RobMeades | 0:7ccf0e7e8a83 | 658 | } |
RobMeades | 0:7ccf0e7e8a83 | 659 | } |
RobMeades | 0:7ccf0e7e8a83 | 660 | TEST_ASSERT(sendSuccess); |
RobMeades | 0:7ccf0e7e8a83 | 661 | x++; |
RobMeades | 0:7ccf0e7e8a83 | 662 | } |
RobMeades | 0:7ccf0e7e8a83 | 663 | tr_debug("--- All UDP packets sent"); |
RobMeades | 0:7ccf0e7e8a83 | 664 | |
RobMeades | 0:7ccf0e7e8a83 | 665 | // ...and capture them all again afterwards |
RobMeades | 0:7ccf0e7e8a83 | 666 | recv_data = (char *) malloc (sizeof (send_data)); |
RobMeades | 0:7ccf0e7e8a83 | 667 | TEST_ASSERT(recv_data != NULL); |
RobMeades | 0:7ccf0e7e8a83 | 668 | memset (recv_data, 0, sizeof (send_data)); |
RobMeades | 0:7ccf0e7e8a83 | 669 | size = 0; |
RobMeades | 0:7ccf0e7e8a83 | 670 | y = 0; |
RobMeades | 0:7ccf0e7e8a83 | 671 | packetLoss = false; |
RobMeades | 0:7ccf0e7e8a83 | 672 | timer.start(); |
RobMeades | 0:7ccf0e7e8a83 | 673 | while ((size < (int) sizeof (send_data)) && (timer.read_ms() < 10000)) { |
RobMeades | 0:7ccf0e7e8a83 | 674 | y = sock.recvfrom(&host_address, (void *) (recv_data + size), sizeof (send_data) - size); |
RobMeades | 0:7ccf0e7e8a83 | 675 | if (y > 0) { |
RobMeades | 0:7ccf0e7e8a83 | 676 | size += y; |
RobMeades | 0:7ccf0e7e8a83 | 677 | } |
RobMeades | 0:7ccf0e7e8a83 | 678 | } |
RobMeades | 0:7ccf0e7e8a83 | 679 | timer.stop(); |
RobMeades | 0:7ccf0e7e8a83 | 680 | timer.reset(); |
RobMeades | 0:7ccf0e7e8a83 | 681 | tr_debug( "--- Either received everything back or timed out waiting"); |
RobMeades | 0:7ccf0e7e8a83 | 682 | |
RobMeades | 0:7ccf0e7e8a83 | 683 | // Check that we reassembled everything correctly |
RobMeades | 0:7ccf0e7e8a83 | 684 | if (size == sizeof (send_data)) { |
RobMeades | 0:7ccf0e7e8a83 | 685 | for (x = 0; ((*(recv_data + x) == *(send_data + x))) && (x < (int) sizeof (send_data)); x++) { |
RobMeades | 0:7ccf0e7e8a83 | 686 | } |
RobMeades | 0:7ccf0e7e8a83 | 687 | if (x != sizeof (send_data)) { |
RobMeades | 0:7ccf0e7e8a83 | 688 | y = x - 5; |
RobMeades | 0:7ccf0e7e8a83 | 689 | if (y < 0) { |
RobMeades | 0:7ccf0e7e8a83 | 690 | y = 0; |
RobMeades | 0:7ccf0e7e8a83 | 691 | } |
RobMeades | 0:7ccf0e7e8a83 | 692 | z = 10; |
RobMeades | 0:7ccf0e7e8a83 | 693 | if (y + z > (int) sizeof (send_data)) { |
RobMeades | 0:7ccf0e7e8a83 | 694 | z = sizeof(send_data) - y; |
RobMeades | 0:7ccf0e7e8a83 | 695 | } |
RobMeades | 0:7ccf0e7e8a83 | 696 | tr_debug(" --- Difference at character %d (send \"%*.*s\", recv \"%*.*s\")", |
RobMeades | 0:7ccf0e7e8a83 | 697 | x + 1, z, z, send_data + y, z, z, recv_data + y); |
RobMeades | 0:7ccf0e7e8a83 | 698 | packetLoss = true; |
RobMeades | 0:7ccf0e7e8a83 | 699 | } |
RobMeades | 0:7ccf0e7e8a83 | 700 | } else { |
RobMeades | 0:7ccf0e7e8a83 | 701 | tr_debug(" --- %d bytes missing (%d bytes received when %d were expected))", |
RobMeades | 0:7ccf0e7e8a83 | 702 | sizeof (send_data) - size, size, sizeof (send_data)); |
RobMeades | 0:7ccf0e7e8a83 | 703 | packetLoss = true; |
RobMeades | 0:7ccf0e7e8a83 | 704 | } |
RobMeades | 0:7ccf0e7e8a83 | 705 | free (recv_data); |
RobMeades | 0:7ccf0e7e8a83 | 706 | tries++; |
RobMeades | 0:7ccf0e7e8a83 | 707 | } while (packetLoss && (tries < NUM_UDP_RETRIES)); |
RobMeades | 0:7ccf0e7e8a83 | 708 | |
RobMeades | 0:7ccf0e7e8a83 | 709 | TEST_ASSERT(!packetLoss); |
RobMeades | 0:7ccf0e7e8a83 | 710 | TEST_ASSERT(!connection_has_gone_down); |
RobMeades | 0:7ccf0e7e8a83 | 711 | sock.close(); |
rob.meades@u-blox.com | 6:63dad754c267 | 712 | drop_connection(interface); |
RobMeades | 0:7ccf0e7e8a83 | 713 | } |
RobMeades | 0:7ccf0e7e8a83 | 714 | |
RobMeades | 0:7ccf0e7e8a83 | 715 | // Test UDP data exchange via the asynchronous sigio() mechanism |
RobMeades | 0:7ccf0e7e8a83 | 716 | void test_udp_echo_async() { |
RobMeades | 0:7ccf0e7e8a83 | 717 | UDPSocket sock; |
RobMeades | 0:7ccf0e7e8a83 | 718 | SocketAddress host_address; |
RobMeades | 0:7ccf0e7e8a83 | 719 | SocketAddress local_address; |
RobMeades | 0:7ccf0e7e8a83 | 720 | bool callback_triggered = false; |
RobMeades | 0:7ccf0e7e8a83 | 721 | int x; |
RobMeades | 0:7ccf0e7e8a83 | 722 | int size; |
RobMeades | 0:7ccf0e7e8a83 | 723 | |
rob.meades@u-blox.com | 6:63dad754c267 | 724 | interface->deinit(); |
rob.meades@u-blox.com | 6:63dad754c267 | 725 | TEST_ASSERT(interface->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN, |
rob.meades@u-blox.com | 6:63dad754c267 | 726 | MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0); |
RobMeades | 0:7ccf0e7e8a83 | 727 | |
rob.meades@u-blox.com | 6:63dad754c267 | 728 | TEST_ASSERT(interface->gethostbyname(MBED_CONF_APP_ECHO_SERVER, &host_address) == 0); |
RobMeades | 0:7ccf0e7e8a83 | 729 | host_address.set_port(MBED_CONF_APP_ECHO_UDP_PORT); |
RobMeades | 0:7ccf0e7e8a83 | 730 | |
RobMeades | 0:7ccf0e7e8a83 | 731 | tr_debug("UDP: Server %s address: %s on port %d.", MBED_CONF_APP_ECHO_SERVER, |
RobMeades | 0:7ccf0e7e8a83 | 732 | host_address.get_ip_address(), host_address.get_port()); |
RobMeades | 0:7ccf0e7e8a83 | 733 | |
rob.meades@u-blox.com | 6:63dad754c267 | 734 | TEST_ASSERT(sock.open(interface) == 0) |
RobMeades | 0:7ccf0e7e8a83 | 735 | |
RobMeades | 0:7ccf0e7e8a83 | 736 | // Set up the async callback and set the timeout to zero |
RobMeades | 0:7ccf0e7e8a83 | 737 | sock.sigio(callback(async_cb, &callback_triggered)); |
RobMeades | 0:7ccf0e7e8a83 | 738 | sock.set_timeout(0); |
RobMeades | 0:7ccf0e7e8a83 | 739 | |
RobMeades | 0:7ccf0e7e8a83 | 740 | // Test min, max, and some random sizes in-between |
RobMeades | 0:7ccf0e7e8a83 | 741 | // and this time allow the UDP packets to be fragmented |
RobMeades | 0:7ccf0e7e8a83 | 742 | do_udp_echo_async(&sock, &host_address, 1, &callback_triggered); |
RobMeades | 0:7ccf0e7e8a83 | 743 | do_udp_echo_async(&sock, &host_address, MBED_CONF_APP_UDP_MAX_FRAG_PACKET_SIZE, |
RobMeades | 0:7ccf0e7e8a83 | 744 | &callback_triggered); |
RobMeades | 0:7ccf0e7e8a83 | 745 | for (x = 0; x < 10; x++) { |
RobMeades | 0:7ccf0e7e8a83 | 746 | size = (rand() % MBED_CONF_APP_UDP_MAX_FRAG_PACKET_SIZE) + 1; |
RobMeades | 0:7ccf0e7e8a83 | 747 | size = fix(size, MBED_CONF_APP_UDP_MAX_FRAG_PACKET_SIZE); |
RobMeades | 0:7ccf0e7e8a83 | 748 | do_udp_echo_async(&sock, &host_address, size, &callback_triggered); |
RobMeades | 0:7ccf0e7e8a83 | 749 | } |
RobMeades | 0:7ccf0e7e8a83 | 750 | |
RobMeades | 0:7ccf0e7e8a83 | 751 | sock.close(); |
RobMeades | 0:7ccf0e7e8a83 | 752 | |
rob.meades@u-blox.com | 6:63dad754c267 | 753 | drop_connection(interface); |
RobMeades | 0:7ccf0e7e8a83 | 754 | |
RobMeades | 0:7ccf0e7e8a83 | 755 | tr_debug("%d UDP packets of size up to %d byte(s) echoed asynchronously and successfully.", |
RobMeades | 0:7ccf0e7e8a83 | 756 | x, MBED_CONF_APP_UDP_MAX_FRAG_PACKET_SIZE); |
RobMeades | 0:7ccf0e7e8a83 | 757 | } |
RobMeades | 0:7ccf0e7e8a83 | 758 | |
RobMeades | 0:7ccf0e7e8a83 | 759 | // Test many different sizes of TCP data arriving at once |
RobMeades | 0:7ccf0e7e8a83 | 760 | void test_tcp_echo_recv_sizes() { |
RobMeades | 0:7ccf0e7e8a83 | 761 | TCPSocket sock; |
RobMeades | 0:7ccf0e7e8a83 | 762 | SocketAddress host_address; |
RobMeades | 0:7ccf0e7e8a83 | 763 | int x, y, z; |
RobMeades | 0:7ccf0e7e8a83 | 764 | int size; |
RobMeades | 0:7ccf0e7e8a83 | 765 | unsigned int offset; |
RobMeades | 0:7ccf0e7e8a83 | 766 | char * recv_data; |
RobMeades | 0:7ccf0e7e8a83 | 767 | Timer timer; |
RobMeades | 0:7ccf0e7e8a83 | 768 | |
rob.meades@u-blox.com | 6:63dad754c267 | 769 | interface->deinit(); |
rob.meades@u-blox.com | 6:63dad754c267 | 770 | TEST_ASSERT(interface->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN, |
rob.meades@u-blox.com | 6:63dad754c267 | 771 | MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0); |
RobMeades | 0:7ccf0e7e8a83 | 772 | |
rob.meades@u-blox.com | 6:63dad754c267 | 773 | TEST_ASSERT(interface->gethostbyname(MBED_CONF_APP_ECHO_SERVER, &host_address) == 0); |
RobMeades | 0:7ccf0e7e8a83 | 774 | host_address.set_port(MBED_CONF_APP_ECHO_TCP_PORT); |
RobMeades | 0:7ccf0e7e8a83 | 775 | |
RobMeades | 0:7ccf0e7e8a83 | 776 | tr_debug("TCP: Server %s address: %s on port %d.", MBED_CONF_APP_ECHO_SERVER, |
RobMeades | 0:7ccf0e7e8a83 | 777 | host_address.get_ip_address(), host_address.get_port()); |
RobMeades | 0:7ccf0e7e8a83 | 778 | |
rob.meades@u-blox.com | 6:63dad754c267 | 779 | TEST_ASSERT(sock.open(interface) == 0) |
RobMeades | 0:7ccf0e7e8a83 | 780 | |
RobMeades | 0:7ccf0e7e8a83 | 781 | TEST_ASSERT(sock.connect(host_address) == 0); |
RobMeades | 0:7ccf0e7e8a83 | 782 | |
RobMeades | 0:7ccf0e7e8a83 | 783 | sock.set_timeout(10000); |
RobMeades | 0:7ccf0e7e8a83 | 784 | |
RobMeades | 0:7ccf0e7e8a83 | 785 | // Throw random sized TCP packets up... |
RobMeades | 0:7ccf0e7e8a83 | 786 | x = 0; |
RobMeades | 0:7ccf0e7e8a83 | 787 | offset = 0; |
RobMeades | 0:7ccf0e7e8a83 | 788 | while (offset < sizeof (send_data)) { |
RobMeades | 0:7ccf0e7e8a83 | 789 | size = (rand() % (MBED_CONF_APP_UDP_MAX_PACKET_SIZE / 2)) + 1; |
RobMeades | 0:7ccf0e7e8a83 | 790 | size = fix(size, MBED_CONF_APP_UDP_MAX_PACKET_SIZE / 2); |
RobMeades | 0:7ccf0e7e8a83 | 791 | if (offset + size > sizeof (send_data)) { |
RobMeades | 0:7ccf0e7e8a83 | 792 | size = sizeof (send_data) - offset; |
RobMeades | 0:7ccf0e7e8a83 | 793 | } |
RobMeades | 0:7ccf0e7e8a83 | 794 | tr_debug("Sending TCP packet number %d, size %d byte(s).", x + 1, size); |
RobMeades | 0:7ccf0e7e8a83 | 795 | TEST_ASSERT(sendAll(&sock, (send_data + offset), size) == size); |
RobMeades | 0:7ccf0e7e8a83 | 796 | offset += size; |
RobMeades | 0:7ccf0e7e8a83 | 797 | x++; |
RobMeades | 0:7ccf0e7e8a83 | 798 | } |
RobMeades | 0:7ccf0e7e8a83 | 799 | |
RobMeades | 0:7ccf0e7e8a83 | 800 | // ...and capture them all again afterwards |
RobMeades | 0:7ccf0e7e8a83 | 801 | recv_data = (char *) malloc (sizeof (send_data)); |
RobMeades | 0:7ccf0e7e8a83 | 802 | TEST_ASSERT(recv_data != NULL); |
RobMeades | 0:7ccf0e7e8a83 | 803 | memset (recv_data, 0, sizeof (send_data)); |
RobMeades | 0:7ccf0e7e8a83 | 804 | size = 0; |
RobMeades | 0:7ccf0e7e8a83 | 805 | x = 0; |
RobMeades | 0:7ccf0e7e8a83 | 806 | timer.start(); |
RobMeades | 0:7ccf0e7e8a83 | 807 | while ((size < (int) sizeof (send_data)) && (timer.read_ms() < 30000)) { |
RobMeades | 0:7ccf0e7e8a83 | 808 | y = sock.recv((void *) (recv_data + size), sizeof (send_data) - size); |
RobMeades | 0:7ccf0e7e8a83 | 809 | tr_debug("Received TCP packet number %d, size %d byte(s).", x, y); |
RobMeades | 0:7ccf0e7e8a83 | 810 | size += y; |
RobMeades | 0:7ccf0e7e8a83 | 811 | x++; |
RobMeades | 0:7ccf0e7e8a83 | 812 | } |
RobMeades | 0:7ccf0e7e8a83 | 813 | timer.stop(); |
RobMeades | 0:7ccf0e7e8a83 | 814 | timer.reset(); |
RobMeades | 0:7ccf0e7e8a83 | 815 | |
RobMeades | 0:7ccf0e7e8a83 | 816 | // Check that we reassembled everything correctly |
RobMeades | 0:7ccf0e7e8a83 | 817 | for (x = 0; ((*(recv_data + x) == *(send_data + x))) && (x < (int) sizeof (send_data)); x++) { |
RobMeades | 0:7ccf0e7e8a83 | 818 | } |
RobMeades | 0:7ccf0e7e8a83 | 819 | if (x != sizeof (send_data)) { |
RobMeades | 0:7ccf0e7e8a83 | 820 | y = x - 5; |
RobMeades | 0:7ccf0e7e8a83 | 821 | if (y < 0) { |
RobMeades | 0:7ccf0e7e8a83 | 822 | y = 0; |
RobMeades | 0:7ccf0e7e8a83 | 823 | } |
RobMeades | 0:7ccf0e7e8a83 | 824 | z = 10; |
RobMeades | 0:7ccf0e7e8a83 | 825 | if (y + z > (int) sizeof (send_data)) { |
RobMeades | 0:7ccf0e7e8a83 | 826 | z = sizeof(send_data) - y; |
RobMeades | 0:7ccf0e7e8a83 | 827 | } |
RobMeades | 0:7ccf0e7e8a83 | 828 | tr_debug("Difference at character %d (send \"%*.*s\", recv \"%*.*s\")", |
RobMeades | 0:7ccf0e7e8a83 | 829 | x + 1, z, z, send_data + y, z, z, recv_data + y); |
RobMeades | 0:7ccf0e7e8a83 | 830 | TEST_ASSERT(false); |
RobMeades | 0:7ccf0e7e8a83 | 831 | } |
RobMeades | 0:7ccf0e7e8a83 | 832 | free (recv_data); |
RobMeades | 0:7ccf0e7e8a83 | 833 | |
RobMeades | 0:7ccf0e7e8a83 | 834 | TEST_ASSERT(!connection_has_gone_down); |
RobMeades | 0:7ccf0e7e8a83 | 835 | sock.close(); |
rob.meades@u-blox.com | 6:63dad754c267 | 836 | drop_connection(interface); |
RobMeades | 0:7ccf0e7e8a83 | 837 | } |
RobMeades | 0:7ccf0e7e8a83 | 838 | |
RobMeades | 0:7ccf0e7e8a83 | 839 | // Test TCP data exchange via the asynchronous sigio() mechanism |
RobMeades | 0:7ccf0e7e8a83 | 840 | void test_tcp_echo_async() { |
RobMeades | 0:7ccf0e7e8a83 | 841 | TCPSocket sock; |
RobMeades | 0:7ccf0e7e8a83 | 842 | SocketAddress host_address; |
RobMeades | 0:7ccf0e7e8a83 | 843 | bool callback_triggered = false; |
RobMeades | 0:7ccf0e7e8a83 | 844 | int x; |
RobMeades | 0:7ccf0e7e8a83 | 845 | int size; |
RobMeades | 0:7ccf0e7e8a83 | 846 | |
rob.meades@u-blox.com | 6:63dad754c267 | 847 | interface->deinit(); |
rob.meades@u-blox.com | 6:63dad754c267 | 848 | TEST_ASSERT(interface->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN, |
rob.meades@u-blox.com | 6:63dad754c267 | 849 | MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0); |
RobMeades | 0:7ccf0e7e8a83 | 850 | |
rob.meades@u-blox.com | 6:63dad754c267 | 851 | TEST_ASSERT(interface->gethostbyname(MBED_CONF_APP_ECHO_SERVER, &host_address) == 0); |
RobMeades | 0:7ccf0e7e8a83 | 852 | host_address.set_port(MBED_CONF_APP_ECHO_TCP_PORT); |
RobMeades | 0:7ccf0e7e8a83 | 853 | |
RobMeades | 0:7ccf0e7e8a83 | 854 | tr_debug("TCP: Server %s address: %s on port %d.", MBED_CONF_APP_ECHO_SERVER, |
RobMeades | 0:7ccf0e7e8a83 | 855 | host_address.get_ip_address(), host_address.get_port()); |
RobMeades | 0:7ccf0e7e8a83 | 856 | |
rob.meades@u-blox.com | 6:63dad754c267 | 857 | TEST_ASSERT(sock.open(interface) == 0) |
RobMeades | 0:7ccf0e7e8a83 | 858 | |
RobMeades | 0:7ccf0e7e8a83 | 859 | // Set up the async callback and set the timeout to zero |
RobMeades | 0:7ccf0e7e8a83 | 860 | sock.sigio(callback(async_cb, &callback_triggered)); |
RobMeades | 0:7ccf0e7e8a83 | 861 | sock.set_timeout(0); |
RobMeades | 0:7ccf0e7e8a83 | 862 | |
RobMeades | 0:7ccf0e7e8a83 | 863 | TEST_ASSERT(sock.connect(host_address) == 0); |
RobMeades | 0:7ccf0e7e8a83 | 864 | // Test min, max, and some random sizes in-between |
RobMeades | 0:7ccf0e7e8a83 | 865 | do_tcp_echo_async(&sock, 1, &callback_triggered); |
RobMeades | 0:7ccf0e7e8a83 | 866 | do_tcp_echo_async(&sock, MBED_CONF_APP_TCP_MAX_PACKET_SIZE, &callback_triggered); |
RobMeades | 0:7ccf0e7e8a83 | 867 | for (x = 0; x < 10; x++) { |
RobMeades | 0:7ccf0e7e8a83 | 868 | size = (rand() % MBED_CONF_APP_TCP_MAX_PACKET_SIZE) + 1; |
RobMeades | 0:7ccf0e7e8a83 | 869 | size = fix(size, MBED_CONF_APP_TCP_MAX_PACKET_SIZE); |
RobMeades | 0:7ccf0e7e8a83 | 870 | do_tcp_echo_async(&sock, size, &callback_triggered); |
RobMeades | 0:7ccf0e7e8a83 | 871 | } |
RobMeades | 0:7ccf0e7e8a83 | 872 | |
RobMeades | 0:7ccf0e7e8a83 | 873 | sock.close(); |
RobMeades | 0:7ccf0e7e8a83 | 874 | |
rob.meades@u-blox.com | 6:63dad754c267 | 875 | drop_connection(interface); |
RobMeades | 0:7ccf0e7e8a83 | 876 | |
RobMeades | 0:7ccf0e7e8a83 | 877 | tr_debug("%d TCP packets of size up to %d byte(s) echoed asynchronously and successfully.", |
RobMeades | 0:7ccf0e7e8a83 | 878 | x, MBED_CONF_APP_TCP_MAX_PACKET_SIZE); |
RobMeades | 0:7ccf0e7e8a83 | 879 | } |
RobMeades | 0:7ccf0e7e8a83 | 880 | #endif |
RobMeades | 0:7ccf0e7e8a83 | 881 | |
RobMeades | 0:7ccf0e7e8a83 | 882 | // Allocate max sockets |
RobMeades | 0:7ccf0e7e8a83 | 883 | void test_max_sockets() { |
RobMeades | 0:7ccf0e7e8a83 | 884 | UDPSocket sock[MAX_NUM_SOCKETS]; |
RobMeades | 0:7ccf0e7e8a83 | 885 | UDPSocket sockNone; |
RobMeades | 0:7ccf0e7e8a83 | 886 | SocketAddress host_address; |
RobMeades | 0:7ccf0e7e8a83 | 887 | |
rob.meades@u-blox.com | 6:63dad754c267 | 888 | TEST_ASSERT(interface->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN, |
rob.meades@u-blox.com | 6:63dad754c267 | 889 | MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0); |
RobMeades | 0:7ccf0e7e8a83 | 890 | |
rob.meades@u-blox.com | 6:63dad754c267 | 891 | TEST_ASSERT(interface->gethostbyname(MBED_CONF_APP_NTP_SERVER, &host_address) == 0); |
RobMeades | 0:7ccf0e7e8a83 | 892 | host_address.set_port(MBED_CONF_APP_NTP_PORT); |
RobMeades | 0:7ccf0e7e8a83 | 893 | |
RobMeades | 0:7ccf0e7e8a83 | 894 | // Open the first socket and use it |
rob.meades@u-blox.com | 6:63dad754c267 | 895 | TEST_ASSERT(sock[0].open(interface) == 0) |
RobMeades | 0:7ccf0e7e8a83 | 896 | sock[0].set_timeout(10000); |
RobMeades | 0:7ccf0e7e8a83 | 897 | do_ntp_sock(&sock[0], host_address); |
RobMeades | 0:7ccf0e7e8a83 | 898 | |
RobMeades | 0:7ccf0e7e8a83 | 899 | // Check that we stop being able to get sockets at the max number |
RobMeades | 0:7ccf0e7e8a83 | 900 | for (int x = 1; x < (int) (sizeof (sock) / sizeof (sock[0])); x++) { |
rob.meades@u-blox.com | 6:63dad754c267 | 901 | TEST_ASSERT(sock[x].open(interface) == 0) |
RobMeades | 0:7ccf0e7e8a83 | 902 | } |
rob.meades@u-blox.com | 6:63dad754c267 | 903 | TEST_ASSERT(sockNone.open(interface) < 0); |
RobMeades | 0:7ccf0e7e8a83 | 904 | |
RobMeades | 0:7ccf0e7e8a83 | 905 | // Now use the last one |
RobMeades | 0:7ccf0e7e8a83 | 906 | sock[sizeof (sock) / sizeof (sock[0]) - 1].set_timeout(10000); |
RobMeades | 0:7ccf0e7e8a83 | 907 | do_ntp_sock(&sock[sizeof (sock) / sizeof (sock[0]) - 1], host_address); |
RobMeades | 0:7ccf0e7e8a83 | 908 | |
RobMeades | 0:7ccf0e7e8a83 | 909 | // Close all of the sockets |
RobMeades | 0:7ccf0e7e8a83 | 910 | for (int x = 0; x < (int) (sizeof (sock) / sizeof (sock[0])); x++) { |
RobMeades | 0:7ccf0e7e8a83 | 911 | TEST_ASSERT(sock[x].close() == 0); |
RobMeades | 0:7ccf0e7e8a83 | 912 | } |
RobMeades | 0:7ccf0e7e8a83 | 913 | |
rob.meades@u-blox.com | 6:63dad754c267 | 914 | drop_connection(interface); |
RobMeades | 0:7ccf0e7e8a83 | 915 | } |
RobMeades | 0:7ccf0e7e8a83 | 916 | |
RobMeades | 0:7ccf0e7e8a83 | 917 | // Connect with credentials included in the connect request |
RobMeades | 0:7ccf0e7e8a83 | 918 | void test_connect_credentials() { |
RobMeades | 0:7ccf0e7e8a83 | 919 | |
rob.meades@u-blox.com | 6:63dad754c267 | 920 | interface->deinit(); |
rob.meades@u-blox.com | 6:63dad754c267 | 921 | TEST_ASSERT(interface->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN, |
rob.meades@u-blox.com | 6:63dad754c267 | 922 | MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0); |
rob.meades@u-blox.com | 6:63dad754c267 | 923 | use_connection(interface); |
rob.meades@u-blox.com | 6:63dad754c267 | 924 | drop_connection(interface); |
RobMeades | 0:7ccf0e7e8a83 | 925 | } |
RobMeades | 0:7ccf0e7e8a83 | 926 | |
RobMeades | 0:7ccf0e7e8a83 | 927 | // Test with credentials preset |
RobMeades | 0:7ccf0e7e8a83 | 928 | void test_connect_preset_credentials() { |
RobMeades | 0:7ccf0e7e8a83 | 929 | |
rob.meades@u-blox.com | 6:63dad754c267 | 930 | interface->deinit(); |
rob.meades@u-blox.com | 6:63dad754c267 | 931 | TEST_ASSERT(interface->init(MBED_CONF_APP_DEFAULT_PIN)); |
rob.meades@u-blox.com | 6:63dad754c267 | 932 | interface->set_credentials(MBED_CONF_APP_APN, MBED_CONF_APP_USERNAME, |
rob.meades@u-blox.com | 6:63dad754c267 | 933 | MBED_CONF_APP_PASSWORD); |
rob.meades@u-blox.com | 6:63dad754c267 | 934 | TEST_ASSERT(interface->connect(MBED_CONF_APP_DEFAULT_PIN) == 0); |
rob.meades@u-blox.com | 6:63dad754c267 | 935 | use_connection(interface); |
rob.meades@u-blox.com | 6:63dad754c267 | 936 | drop_connection(interface); |
RobMeades | 0:7ccf0e7e8a83 | 937 | } |
RobMeades | 0:7ccf0e7e8a83 | 938 | |
RobMeades | 0:7ccf0e7e8a83 | 939 | // Test adding and using a SIM pin, then removing it, using the pending |
RobMeades | 0:7ccf0e7e8a83 | 940 | // mechanism where the change doesn't occur until connect() is called |
RobMeades | 0:7ccf0e7e8a83 | 941 | void test_check_sim_pin_pending() { |
RobMeades | 0:7ccf0e7e8a83 | 942 | |
rob.meades@u-blox.com | 6:63dad754c267 | 943 | interface->deinit(); |
RobMeades | 0:7ccf0e7e8a83 | 944 | |
RobMeades | 0:7ccf0e7e8a83 | 945 | // Enable PIN checking (which will use the current PIN) |
RobMeades | 0:7ccf0e7e8a83 | 946 | // and also flag that the PIN should be changed to MBED_CONF_APP_ALT_PIN, |
RobMeades | 0:7ccf0e7e8a83 | 947 | // then try connecting |
rob.meades@u-blox.com | 6:63dad754c267 | 948 | interface->set_sim_pin_check(true); |
rob.meades@u-blox.com | 6:63dad754c267 | 949 | interface->set_new_sim_pin(MBED_CONF_APP_ALT_PIN); |
rob.meades@u-blox.com | 6:63dad754c267 | 950 | TEST_ASSERT(interface->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN, |
rob.meades@u-blox.com | 6:63dad754c267 | 951 | MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0); |
rob.meades@u-blox.com | 6:63dad754c267 | 952 | use_connection(interface); |
rob.meades@u-blox.com | 6:63dad754c267 | 953 | drop_connection(interface); |
rob.meades@u-blox.com | 6:63dad754c267 | 954 | interface->deinit(); |
RobMeades | 0:7ccf0e7e8a83 | 955 | |
RobMeades | 0:7ccf0e7e8a83 | 956 | // Now change the PIN back to what it was before |
rob.meades@u-blox.com | 6:63dad754c267 | 957 | interface->set_new_sim_pin(MBED_CONF_APP_DEFAULT_PIN); |
rob.meades@u-blox.com | 6:63dad754c267 | 958 | TEST_ASSERT(interface->connect(MBED_CONF_APP_ALT_PIN, MBED_CONF_APP_APN, |
rob.meades@u-blox.com | 6:63dad754c267 | 959 | MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0); |
rob.meades@u-blox.com | 6:63dad754c267 | 960 | use_connection(interface); |
rob.meades@u-blox.com | 6:63dad754c267 | 961 | drop_connection(interface); |
rob.meades@u-blox.com | 6:63dad754c267 | 962 | interface->deinit(); |
RobMeades | 0:7ccf0e7e8a83 | 963 | |
RobMeades | 0:7ccf0e7e8a83 | 964 | // Check that it was changed back, and this time |
RobMeades | 0:7ccf0e7e8a83 | 965 | // use the other way of entering the PIN |
rob.meades@u-blox.com | 6:63dad754c267 | 966 | interface->set_sim_pin(MBED_CONF_APP_DEFAULT_PIN); |
rob.meades@u-blox.com | 6:63dad754c267 | 967 | TEST_ASSERT(interface->connect(NULL, MBED_CONF_APP_APN, MBED_CONF_APP_USERNAME, |
rob.meades@u-blox.com | 6:63dad754c267 | 968 | MBED_CONF_APP_PASSWORD) == 0); |
rob.meades@u-blox.com | 6:63dad754c267 | 969 | use_connection(interface); |
rob.meades@u-blox.com | 6:63dad754c267 | 970 | drop_connection(interface); |
rob.meades@u-blox.com | 6:63dad754c267 | 971 | interface->deinit(); |
RobMeades | 0:7ccf0e7e8a83 | 972 | |
RobMeades | 0:7ccf0e7e8a83 | 973 | // Remove PIN checking again and check that it no |
RobMeades | 0:7ccf0e7e8a83 | 974 | // longer matters what the PIN is |
rob.meades@u-blox.com | 6:63dad754c267 | 975 | interface->set_sim_pin_check(false); |
rob.meades@u-blox.com | 6:63dad754c267 | 976 | TEST_ASSERT(interface->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN, |
rob.meades@u-blox.com | 6:63dad754c267 | 977 | MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0); |
rob.meades@u-blox.com | 6:63dad754c267 | 978 | use_connection(interface); |
rob.meades@u-blox.com | 6:63dad754c267 | 979 | drop_connection(interface); |
rob.meades@u-blox.com | 6:63dad754c267 | 980 | interface->deinit(); |
rob.meades@u-blox.com | 6:63dad754c267 | 981 | TEST_ASSERT(interface->init(NULL)); |
rob.meades@u-blox.com | 6:63dad754c267 | 982 | TEST_ASSERT(interface->connect(MBED_CONF_APP_INCORRECT_PIN, MBED_CONF_APP_APN, |
rob.meades@u-blox.com | 6:63dad754c267 | 983 | MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0); |
rob.meades@u-blox.com | 6:63dad754c267 | 984 | use_connection(interface); |
rob.meades@u-blox.com | 6:63dad754c267 | 985 | drop_connection(interface); |
RobMeades | 0:7ccf0e7e8a83 | 986 | |
RobMeades | 0:7ccf0e7e8a83 | 987 | // Put the SIM pin back to the correct value for any subsequent tests |
rob.meades@u-blox.com | 6:63dad754c267 | 988 | interface->set_sim_pin(MBED_CONF_APP_DEFAULT_PIN); |
RobMeades | 0:7ccf0e7e8a83 | 989 | } |
RobMeades | 0:7ccf0e7e8a83 | 990 | |
RobMeades | 0:7ccf0e7e8a83 | 991 | // Test adding and using a SIM pin, then removing it, using the immediate |
RobMeades | 0:7ccf0e7e8a83 | 992 | // mechanism |
RobMeades | 0:7ccf0e7e8a83 | 993 | void test_check_sim_pin_immediate() { |
RobMeades | 0:7ccf0e7e8a83 | 994 | |
rob.meades@u-blox.com | 6:63dad754c267 | 995 | interface->deinit(); |
rob.meades@u-blox.com | 6:63dad754c267 | 996 | interface->connection_status_cb(connection_down_cb); |
RobMeades | 0:7ccf0e7e8a83 | 997 | |
RobMeades | 0:7ccf0e7e8a83 | 998 | // Enable PIN checking (which will use the current PIN), change |
RobMeades | 0:7ccf0e7e8a83 | 999 | // the PIN to MBED_CONF_APP_ALT_PIN, then try connecting after powering on and |
RobMeades | 0:7ccf0e7e8a83 | 1000 | // off the modem |
rob.meades@u-blox.com | 6:63dad754c267 | 1001 | interface->set_sim_pin_check(true, true, MBED_CONF_APP_DEFAULT_PIN); |
rob.meades@u-blox.com | 6:63dad754c267 | 1002 | interface->set_new_sim_pin(MBED_CONF_APP_ALT_PIN, true); |
rob.meades@u-blox.com | 6:63dad754c267 | 1003 | interface->deinit(); |
rob.meades@u-blox.com | 6:63dad754c267 | 1004 | TEST_ASSERT(interface->init(NULL)); |
rob.meades@u-blox.com | 6:63dad754c267 | 1005 | TEST_ASSERT(interface->connect(MBED_CONF_APP_ALT_PIN, MBED_CONF_APP_APN, |
rob.meades@u-blox.com | 6:63dad754c267 | 1006 | MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0); |
rob.meades@u-blox.com | 6:63dad754c267 | 1007 | use_connection(interface); |
rob.meades@u-blox.com | 6:63dad754c267 | 1008 | drop_connection(interface); |
RobMeades | 0:7ccf0e7e8a83 | 1009 | |
rob.meades@u-blox.com | 6:63dad754c267 | 1010 | interface->connection_status_cb(connection_down_cb); |
RobMeades | 0:7ccf0e7e8a83 | 1011 | |
RobMeades | 0:7ccf0e7e8a83 | 1012 | // Now change the PIN back to what it was before |
rob.meades@u-blox.com | 6:63dad754c267 | 1013 | interface->set_new_sim_pin(MBED_CONF_APP_DEFAULT_PIN, true); |
rob.meades@u-blox.com | 6:63dad754c267 | 1014 | interface->deinit(); |
rob.meades@u-blox.com | 6:63dad754c267 | 1015 | interface->set_sim_pin(MBED_CONF_APP_DEFAULT_PIN); |
rob.meades@u-blox.com | 6:63dad754c267 | 1016 | TEST_ASSERT(interface->init(NULL)); |
rob.meades@u-blox.com | 6:63dad754c267 | 1017 | TEST_ASSERT(interface->connect(NULL, MBED_CONF_APP_APN, MBED_CONF_APP_USERNAME, |
rob.meades@u-blox.com | 6:63dad754c267 | 1018 | MBED_CONF_APP_PASSWORD) == 0); |
rob.meades@u-blox.com | 6:63dad754c267 | 1019 | use_connection(interface); |
rob.meades@u-blox.com | 6:63dad754c267 | 1020 | drop_connection(interface); |
RobMeades | 0:7ccf0e7e8a83 | 1021 | |
rob.meades@u-blox.com | 6:63dad754c267 | 1022 | interface->connection_status_cb(connection_down_cb); |
RobMeades | 0:7ccf0e7e8a83 | 1023 | |
RobMeades | 0:7ccf0e7e8a83 | 1024 | // Remove PIN checking again and check that it no |
RobMeades | 0:7ccf0e7e8a83 | 1025 | // longer matters what the PIN is |
rob.meades@u-blox.com | 6:63dad754c267 | 1026 | interface->set_sim_pin_check(false, true); |
rob.meades@u-blox.com | 6:63dad754c267 | 1027 | interface->deinit(); |
rob.meades@u-blox.com | 6:63dad754c267 | 1028 | TEST_ASSERT(interface->init(MBED_CONF_APP_INCORRECT_PIN)); |
rob.meades@u-blox.com | 6:63dad754c267 | 1029 | TEST_ASSERT(interface->connect(NULL, MBED_CONF_APP_APN, MBED_CONF_APP_USERNAME, |
rob.meades@u-blox.com | 6:63dad754c267 | 1030 | MBED_CONF_APP_PASSWORD) == 0); |
rob.meades@u-blox.com | 6:63dad754c267 | 1031 | use_connection(interface); |
rob.meades@u-blox.com | 6:63dad754c267 | 1032 | drop_connection(interface); |
RobMeades | 0:7ccf0e7e8a83 | 1033 | |
RobMeades | 0:7ccf0e7e8a83 | 1034 | // Put the SIM pin back to the correct value for any subsequent tests |
rob.meades@u-blox.com | 6:63dad754c267 | 1035 | interface->set_sim_pin(MBED_CONF_APP_DEFAULT_PIN); |
RobMeades | 0:7ccf0e7e8a83 | 1036 | } |
RobMeades | 0:7ccf0e7e8a83 | 1037 | |
rob.meades@u-blox.com | 6:63dad754c267 | 1038 | // Test being able to connect with a local instance of the interface |
RobMeades | 0:7ccf0e7e8a83 | 1039 | // NOTE: since this local instance will fiddle with bits of HW that the |
RobMeades | 0:7ccf0e7e8a83 | 1040 | // static instance thought it owned, the static instance will no longer |
RobMeades | 0:7ccf0e7e8a83 | 1041 | // work afterwards, hence this must be run as the last test in the list |
RobMeades | 0:7ccf0e7e8a83 | 1042 | void test_connect_local_instance_last_test() { |
RobMeades | 0:7ccf0e7e8a83 | 1043 | |
RobMeades | 0:7ccf0e7e8a83 | 1044 | UbloxATCellularInterface *pLocalInterface = NULL; |
RobMeades | 0:7ccf0e7e8a83 | 1045 | |
RobMeades | 0:7ccf0e7e8a83 | 1046 | pLocalInterface = new UbloxATCellularInterface(MDMTXD, MDMRXD, |
RobMeades | 0:7ccf0e7e8a83 | 1047 | MBED_CONF_UBLOX_CELL_BAUD_RATE, |
RobMeades | 0:7ccf0e7e8a83 | 1048 | MBED_CONF_APP_DEBUG_ON); |
RobMeades | 0:7ccf0e7e8a83 | 1049 | pLocalInterface->connection_status_cb(connection_down_cb); |
RobMeades | 0:7ccf0e7e8a83 | 1050 | |
RobMeades | 0:7ccf0e7e8a83 | 1051 | TEST_ASSERT(pLocalInterface->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN, |
RobMeades | 0:7ccf0e7e8a83 | 1052 | MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0); |
RobMeades | 0:7ccf0e7e8a83 | 1053 | use_connection(pLocalInterface); |
RobMeades | 0:7ccf0e7e8a83 | 1054 | drop_connection(pLocalInterface); |
RobMeades | 0:7ccf0e7e8a83 | 1055 | delete pLocalInterface; |
RobMeades | 0:7ccf0e7e8a83 | 1056 | |
RobMeades | 0:7ccf0e7e8a83 | 1057 | pLocalInterface = new UbloxATCellularInterface(MDMTXD, MDMRXD, |
RobMeades | 0:7ccf0e7e8a83 | 1058 | MBED_CONF_UBLOX_CELL_BAUD_RATE, |
RobMeades | 0:7ccf0e7e8a83 | 1059 | MBED_CONF_APP_DEBUG_ON); |
RobMeades | 0:7ccf0e7e8a83 | 1060 | pLocalInterface->connection_status_cb(connection_down_cb); |
RobMeades | 0:7ccf0e7e8a83 | 1061 | |
RobMeades | 0:7ccf0e7e8a83 | 1062 | TEST_ASSERT(pLocalInterface->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN, |
RobMeades | 0:7ccf0e7e8a83 | 1063 | MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0); |
RobMeades | 0:7ccf0e7e8a83 | 1064 | use_connection(pLocalInterface); |
RobMeades | 0:7ccf0e7e8a83 | 1065 | drop_connection(pLocalInterface); |
RobMeades | 0:7ccf0e7e8a83 | 1066 | delete pLocalInterface; |
RobMeades | 0:7ccf0e7e8a83 | 1067 | } |
RobMeades | 0:7ccf0e7e8a83 | 1068 | |
RobMeades | 0:7ccf0e7e8a83 | 1069 | // ---------------------------------------------------------------- |
RobMeades | 0:7ccf0e7e8a83 | 1070 | // TEST ENVIRONMENT |
RobMeades | 0:7ccf0e7e8a83 | 1071 | // ---------------------------------------------------------------- |
RobMeades | 0:7ccf0e7e8a83 | 1072 | |
RobMeades | 0:7ccf0e7e8a83 | 1073 | // Setup the test environment |
RobMeades | 0:7ccf0e7e8a83 | 1074 | utest::v1::status_t test_setup(const size_t number_of_cases) { |
RobMeades | 0:7ccf0e7e8a83 | 1075 | // Setup Greentea with a timeout |
RobMeades | 0:7ccf0e7e8a83 | 1076 | GREENTEA_SETUP(960, "default_auto"); |
RobMeades | 0:7ccf0e7e8a83 | 1077 | return verbose_test_setup_handler(number_of_cases); |
RobMeades | 0:7ccf0e7e8a83 | 1078 | } |
RobMeades | 0:7ccf0e7e8a83 | 1079 | |
RobMeades | 0:7ccf0e7e8a83 | 1080 | // IMPORTANT!!! if you make a change to the tests here you should |
RobMeades | 0:7ccf0e7e8a83 | 1081 | // check whether the same change should be made to the tests under |
rob.meades@u-blox.com | 6:63dad754c267 | 1082 | // the PPP interface. |
RobMeades | 0:7ccf0e7e8a83 | 1083 | |
RobMeades | 0:7ccf0e7e8a83 | 1084 | // Test cases |
RobMeades | 0:7ccf0e7e8a83 | 1085 | Case cases[] = { |
RobMeades | 12:ff6fac481487 | 1086 | Case("Base class tests", test_base_class), |
RobMeades | 0:7ccf0e7e8a83 | 1087 | Case("Set randomise", test_set_randomise), |
RobMeades | 0:7ccf0e7e8a83 | 1088 | #ifdef MBED_CONF_APP_ECHO_SERVER |
RobMeades | 0:7ccf0e7e8a83 | 1089 | Case("UDP echo test", test_udp_echo), |
RobMeades | 0:7ccf0e7e8a83 | 1090 | # ifndef TARGET_UBLOX_C027 // Not enough RAM on little 'ole C027 to run this test |
RobMeades | 0:7ccf0e7e8a83 | 1091 | Case("UDP recv sizes", test_udp_echo_recv_sizes), |
RobMeades | 0:7ccf0e7e8a83 | 1092 | # endif |
RobMeades | 0:7ccf0e7e8a83 | 1093 | Case("UDP async echo test", test_udp_echo_async), |
RobMeades | 0:7ccf0e7e8a83 | 1094 | # ifndef TARGET_UBLOX_C027 // Not enough RAM on little 'ole C027 to run this test |
RobMeades | 0:7ccf0e7e8a83 | 1095 | Case("TCP recv sizes", test_tcp_echo_recv_sizes), |
RobMeades | 0:7ccf0e7e8a83 | 1096 | # endif |
RobMeades | 0:7ccf0e7e8a83 | 1097 | Case("TCP async echo test", test_tcp_echo_async), |
RobMeades | 0:7ccf0e7e8a83 | 1098 | #endif |
RobMeades | 0:7ccf0e7e8a83 | 1099 | #ifndef TARGET_UBLOX_C027 // Not enough RAM on little 'ole C027 to run this test |
RobMeades | 0:7ccf0e7e8a83 | 1100 | Case("Alloc max sockets", test_max_sockets), |
RobMeades | 0:7ccf0e7e8a83 | 1101 | #endif |
RobMeades | 0:7ccf0e7e8a83 | 1102 | Case("Connect with credentials", test_connect_credentials), |
RobMeades | 0:7ccf0e7e8a83 | 1103 | Case("Connect with preset credentials", test_connect_preset_credentials), |
RobMeades | 0:7ccf0e7e8a83 | 1104 | #if MBED_CONF_APP_RUN_SIM_PIN_CHANGE_TESTS |
RobMeades | 0:7ccf0e7e8a83 | 1105 | Case("Check SIM pin, pending", test_check_sim_pin_pending), |
RobMeades | 0:7ccf0e7e8a83 | 1106 | Case("Check SIM pin, immediate", test_check_sim_pin_immediate), |
RobMeades | 0:7ccf0e7e8a83 | 1107 | #endif |
RobMeades | 0:7ccf0e7e8a83 | 1108 | #ifndef TARGET_UBLOX_C027 // Not enough RAM on little 'ole C027 for this |
RobMeades | 0:7ccf0e7e8a83 | 1109 | Case("Connect using local instance, must be last test", test_connect_local_instance_last_test) |
RobMeades | 0:7ccf0e7e8a83 | 1110 | #endif |
RobMeades | 0:7ccf0e7e8a83 | 1111 | }; |
RobMeades | 0:7ccf0e7e8a83 | 1112 | |
RobMeades | 0:7ccf0e7e8a83 | 1113 | Specification specification(test_setup, cases); |
RobMeades | 0:7ccf0e7e8a83 | 1114 | |
RobMeades | 0:7ccf0e7e8a83 | 1115 | // ---------------------------------------------------------------- |
RobMeades | 0:7ccf0e7e8a83 | 1116 | // MAIN |
RobMeades | 0:7ccf0e7e8a83 | 1117 | // ---------------------------------------------------------------- |
RobMeades | 0:7ccf0e7e8a83 | 1118 | |
RobMeades | 0:7ccf0e7e8a83 | 1119 | int main() { |
RobMeades | 0:7ccf0e7e8a83 | 1120 | |
RobMeades | 0:7ccf0e7e8a83 | 1121 | #ifdef FEATURE_COMMON_PAL |
RobMeades | 0:7ccf0e7e8a83 | 1122 | mbed_trace_init(); |
RobMeades | 0:7ccf0e7e8a83 | 1123 | |
RobMeades | 0:7ccf0e7e8a83 | 1124 | mbed_trace_mutex_wait_function_set(lock); |
RobMeades | 0:7ccf0e7e8a83 | 1125 | mbed_trace_mutex_release_function_set(unlock); |
RobMeades | 0:7ccf0e7e8a83 | 1126 | #endif |
RobMeades | 0:7ccf0e7e8a83 | 1127 | |
rob.meades@u-blox.com | 6:63dad754c267 | 1128 | interface->connection_status_cb(connection_down_cb); |
RobMeades | 0:7ccf0e7e8a83 | 1129 | |
RobMeades | 0:7ccf0e7e8a83 | 1130 | // Run tests |
RobMeades | 0:7ccf0e7e8a83 | 1131 | return !Harness::run(specification); |
RobMeades | 0:7ccf0e7e8a83 | 1132 | } |
RobMeades | 0:7ccf0e7e8a83 | 1133 | |
RobMeades | 0:7ccf0e7e8a83 | 1134 | // End Of File |