Library that implements the CellularInterface using PPP and LWIP on the mbed MCU. May be used on the C027 and C030 (non-N2xx flavour) boards from mbed 5.5 onwards.
Dependents: example-ublox-cellular-interface HelloMQTT example-ublox-cellular-interface_r410M example-ublox-mbed-client
TESTS/unit_tests/default/main.cpp@3:9863dcade75d, 2018-02-28 (annotated)
- Committer:
- RobMeades
- Date:
- Wed Feb 28 17:33:50 2018 +0000
- Revision:
- 3:9863dcade75d
- Parent:
- 1:80ec3fccad9e
PR 6032 into mbed-os changed the signature of the PPP connection_status_cb() (it now carries two parameters rather than one). This change fixes the UbloxPPPCellularInterface to work with that PR.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
RobMeades | 0:44dd95724bc2 | 1 | #include "UbloxPPPCellularInterface.h" |
RobMeades | 0:44dd95724bc2 | 2 | #include "mbed.h" |
RobMeades | 0:44dd95724bc2 | 3 | #include "Callback.h" |
RobMeades | 0:44dd95724bc2 | 4 | #include "greentea-client/test_env.h" |
RobMeades | 0:44dd95724bc2 | 5 | #include "unity.h" |
RobMeades | 0:44dd95724bc2 | 6 | #include "utest.h" |
RobMeades | 0:44dd95724bc2 | 7 | #include "UDPSocket.h" |
RobMeades | 0:44dd95724bc2 | 8 | #ifdef FEATURE_COMMON_PAL |
RobMeades | 0:44dd95724bc2 | 9 | #include "mbed_trace.h" |
RobMeades | 0:44dd95724bc2 | 10 | #define TRACE_GROUP "TEST" |
RobMeades | 0:44dd95724bc2 | 11 | #else |
rob.meades@u-blox.com | 1:80ec3fccad9e | 12 | #define tr_debug(format, ...) debug(format "\n", ## __VA_ARGS__) |
rob.meades@u-blox.com | 1:80ec3fccad9e | 13 | #define tr_info(format, ...) debug(format "\n", ## __VA_ARGS__) |
rob.meades@u-blox.com | 1:80ec3fccad9e | 14 | #define tr_warn(format, ...) debug(format "\n", ## __VA_ARGS__) |
rob.meades@u-blox.com | 1:80ec3fccad9e | 15 | #define tr_error(format, ...) debug(format "\n", ## __VA_ARGS__) |
RobMeades | 0:44dd95724bc2 | 16 | #endif |
RobMeades | 0:44dd95724bc2 | 17 | |
RobMeades | 0:44dd95724bc2 | 18 | using namespace utest::v1; |
RobMeades | 0:44dd95724bc2 | 19 | |
RobMeades | 0:44dd95724bc2 | 20 | #if NSAPI_PPP_AVAILABLE |
RobMeades | 0:44dd95724bc2 | 21 | |
RobMeades | 0:44dd95724bc2 | 22 | // IMPORTANT!!! if you make a change to the tests here you should |
RobMeades | 0:44dd95724bc2 | 23 | // check whether the same change should be made to the tests under |
rob.meades@u-blox.com | 1:80ec3fccad9e | 24 | // the AT interface. |
rob.meades@u-blox.com | 1:80ec3fccad9e | 25 | |
rob.meades@u-blox.com | 1:80ec3fccad9e | 26 | // NOTE: these test are only as reliable as UDP across the internet |
rob.meades@u-blox.com | 1:80ec3fccad9e | 27 | // over a radio link. The tests expect an NTP server to respond |
rob.meades@u-blox.com | 1:80ec3fccad9e | 28 | // to UDP packets and, if configured, an echo server to respond |
rob.meades@u-blox.com | 1:80ec3fccad9e | 29 | // to UDP packets. This simply may not happen. Please be patient. |
RobMeades | 0:44dd95724bc2 | 30 | |
RobMeades | 0:44dd95724bc2 | 31 | // ---------------------------------------------------------------- |
RobMeades | 0:44dd95724bc2 | 32 | // COMPILE-TIME MACROS |
RobMeades | 0:44dd95724bc2 | 33 | // ---------------------------------------------------------------- |
RobMeades | 0:44dd95724bc2 | 34 | |
RobMeades | 0:44dd95724bc2 | 35 | // These macros can be overridden with an mbed_app.json file and |
RobMeades | 0:44dd95724bc2 | 36 | // contents of the following form: |
RobMeades | 0:44dd95724bc2 | 37 | // |
RobMeades | 0:44dd95724bc2 | 38 | //{ |
RobMeades | 0:44dd95724bc2 | 39 | // "config": { |
RobMeades | 0:44dd95724bc2 | 40 | // "default-pin": { |
RobMeades | 0:44dd95724bc2 | 41 | // "value": "\"1234\"" |
RobMeades | 0:44dd95724bc2 | 42 | // } |
RobMeades | 0:44dd95724bc2 | 43 | //} |
RobMeades | 0:44dd95724bc2 | 44 | // |
RobMeades | 0:44dd95724bc2 | 45 | // See the template_mbed_app.txt in this directory for a fuller example. |
RobMeades | 0:44dd95724bc2 | 46 | |
RobMeades | 0:44dd95724bc2 | 47 | // Whether debug trace is on |
RobMeades | 0:44dd95724bc2 | 48 | #ifndef MBED_CONF_APP_DEBUG_ON |
RobMeades | 0:44dd95724bc2 | 49 | # define MBED_CONF_APP_DEBUG_ON false |
RobMeades | 0:44dd95724bc2 | 50 | #endif |
RobMeades | 0:44dd95724bc2 | 51 | |
RobMeades | 0:44dd95724bc2 | 52 | // Run the SIM change tests, which require the DEFAULT_PIN |
RobMeades | 0:44dd95724bc2 | 53 | // above to be correct for the board on which the test |
RobMeades | 0:44dd95724bc2 | 54 | // is being run (and the SIM PIN to be disabled before tests run). |
RobMeades | 0:44dd95724bc2 | 55 | #ifndef MBED_CONF_APP_RUN_SIM_PIN_CHANGE_TESTS |
RobMeades | 0:44dd95724bc2 | 56 | # define MBED_CONF_APP_RUN_SIM_PIN_CHANGE_TESTS 0 |
RobMeades | 0:44dd95724bc2 | 57 | #endif |
RobMeades | 0:44dd95724bc2 | 58 | |
RobMeades | 0:44dd95724bc2 | 59 | #if MBED_CONF_APP_RUN_SIM_PIN_CHANGE_TESTS |
RobMeades | 0:44dd95724bc2 | 60 | # ifndef MBED_CONF_APP_DEFAULT_PIN |
RobMeades | 0:44dd95724bc2 | 61 | # error "MBED_CONF_APP_DEFAULT_PIN must be defined to run the SIM tests" |
RobMeades | 0:44dd95724bc2 | 62 | # endif |
RobMeades | 0:44dd95724bc2 | 63 | # ifndef MBED_CONF_APP_ALT_PIN |
RobMeades | 0:44dd95724bc2 | 64 | # error "MBED_CONF_APP_ALT_PIN must be defined to run the SIM tests" |
RobMeades | 0:44dd95724bc2 | 65 | # endif |
RobMeades | 0:44dd95724bc2 | 66 | # ifndef MBED_CONF_APP_INCORRECT_PIN |
RobMeades | 0:44dd95724bc2 | 67 | # error "MBED_CONF_APP_INCORRECT_PIN must be defined to run the SIM tests" |
RobMeades | 0:44dd95724bc2 | 68 | # endif |
RobMeades | 0:44dd95724bc2 | 69 | #endif |
RobMeades | 0:44dd95724bc2 | 70 | |
RobMeades | 0:44dd95724bc2 | 71 | // The credentials of the SIM in the board. |
RobMeades | 0:44dd95724bc2 | 72 | #ifndef MBED_CONF_APP_DEFAULT_PIN |
RobMeades | 0:44dd95724bc2 | 73 | // Note: if PIN is enabled on your SIM, or you wish to run the SIM PIN change |
RobMeades | 0:44dd95724bc2 | 74 | // tests, you must define the PIN for your SIM (see note above on using |
RobMeades | 0:44dd95724bc2 | 75 | // mbed_app.json to do so). |
RobMeades | 0:44dd95724bc2 | 76 | # define MBED_CONF_APP_DEFAULT_PIN "0000" |
RobMeades | 0:44dd95724bc2 | 77 | #endif |
RobMeades | 0:44dd95724bc2 | 78 | #ifndef MBED_CONF_APP_APN |
RobMeades | 0:44dd95724bc2 | 79 | # define MBED_CONF_APP_APN NULL |
RobMeades | 0:44dd95724bc2 | 80 | #endif |
RobMeades | 0:44dd95724bc2 | 81 | #ifndef MBED_CONF_APP_USERNAME |
RobMeades | 0:44dd95724bc2 | 82 | # define MBED_CONF_APP_USERNAME NULL |
RobMeades | 0:44dd95724bc2 | 83 | #endif |
RobMeades | 0:44dd95724bc2 | 84 | #ifndef MBED_CONF_APP_PASSWORD |
RobMeades | 0:44dd95724bc2 | 85 | # define MBED_CONF_APP_PASSWORD NULL |
RobMeades | 0:44dd95724bc2 | 86 | #endif |
RobMeades | 0:44dd95724bc2 | 87 | |
RobMeades | 0:44dd95724bc2 | 88 | // Alternate PIN to use during pin change testing |
RobMeades | 0:44dd95724bc2 | 89 | #ifndef MBED_CONF_APP_ALT_PIN |
RobMeades | 0:44dd95724bc2 | 90 | # define MBED_CONF_APP_ALT_PIN "9876" |
RobMeades | 0:44dd95724bc2 | 91 | #endif |
RobMeades | 0:44dd95724bc2 | 92 | |
RobMeades | 0:44dd95724bc2 | 93 | // A PIN that is definitely incorrect |
RobMeades | 0:44dd95724bc2 | 94 | #ifndef MBED_CONF_APP_INCORRECT_PIN |
RobMeades | 0:44dd95724bc2 | 95 | # define MBED_CONF_APP_INCORRECT_PIN "1530" |
RobMeades | 0:44dd95724bc2 | 96 | #endif |
RobMeades | 0:44dd95724bc2 | 97 | |
RobMeades | 0:44dd95724bc2 | 98 | // Servers and ports |
RobMeades | 0:44dd95724bc2 | 99 | #ifdef MBED_CONF_APP_ECHO_SERVER |
RobMeades | 0:44dd95724bc2 | 100 | # ifndef MBED_CONF_APP_ECHO_UDP_PORT |
RobMeades | 0:44dd95724bc2 | 101 | # error "MBED_CONF_APP_ECHO_UDP_PORT (the port on which your echo server echoes UDP packets) must be defined" |
RobMeades | 0:44dd95724bc2 | 102 | # endif |
RobMeades | 0:44dd95724bc2 | 103 | # ifndef MBED_CONF_APP_ECHO_TCP_PORT |
RobMeades | 0:44dd95724bc2 | 104 | # error "MBED_CONF_APP_ECHO_TCP_PORT (the port on which your echo server echoes TCP packets) must be defined" |
RobMeades | 0:44dd95724bc2 | 105 | # endif |
RobMeades | 0:44dd95724bc2 | 106 | #endif |
RobMeades | 0:44dd95724bc2 | 107 | |
RobMeades | 0:44dd95724bc2 | 108 | #ifndef MBED_CONF_APP_NTP_SERVER |
RobMeades | 0:44dd95724bc2 | 109 | # define MBED_CONF_APP_NTP_SERVER "2.pool.ntp.org" |
RobMeades | 0:44dd95724bc2 | 110 | #else |
RobMeades | 0:44dd95724bc2 | 111 | # ifndef MBED_CONF_APP_NTP_PORT |
RobMeades | 0:44dd95724bc2 | 112 | # error "MBED_CONF_APP_NTP_PORT must be defined if MBED_CONF_APP_NTP_SERVER is defined" |
RobMeades | 0:44dd95724bc2 | 113 | # endif |
RobMeades | 0:44dd95724bc2 | 114 | #endif |
RobMeades | 0:44dd95724bc2 | 115 | #ifndef MBED_CONF_APP_NTP_PORT |
RobMeades | 0:44dd95724bc2 | 116 | # define MBED_CONF_APP_NTP_PORT 123 |
RobMeades | 0:44dd95724bc2 | 117 | #endif |
RobMeades | 0:44dd95724bc2 | 118 | |
RobMeades | 0:44dd95724bc2 | 119 | #ifndef MBED_CONF_APP_LOCAL_PORT |
RobMeades | 0:44dd95724bc2 | 120 | # define MBED_CONF_APP_LOCAL_PORT 15 |
RobMeades | 0:44dd95724bc2 | 121 | #endif |
RobMeades | 0:44dd95724bc2 | 122 | |
RobMeades | 0:44dd95724bc2 | 123 | // UDP packet size limit for testing |
RobMeades | 0:44dd95724bc2 | 124 | #ifndef MBED_CONF_APP_UDP_MAX_PACKET_SIZE |
RobMeades | 0:44dd95724bc2 | 125 | # define MBED_CONF_APP_UDP_MAX_PACKET_SIZE 508 |
RobMeades | 0:44dd95724bc2 | 126 | #endif |
RobMeades | 0:44dd95724bc2 | 127 | |
RobMeades | 0:44dd95724bc2 | 128 | // TCP packet size limit for testing |
RobMeades | 0:44dd95724bc2 | 129 | #ifndef MBED_CONF_APP_MBED_CONF_APP_TCP_MAX_PACKET_SIZE |
RobMeades | 0:44dd95724bc2 | 130 | # define MBED_CONF_APP_TCP_MAX_PACKET_SIZE 1500 |
RobMeades | 0:44dd95724bc2 | 131 | #endif |
RobMeades | 0:44dd95724bc2 | 132 | |
RobMeades | 0:44dd95724bc2 | 133 | // The number of retries for UDP exchanges |
RobMeades | 0:44dd95724bc2 | 134 | #define NUM_UDP_RETRIES 5 |
RobMeades | 0:44dd95724bc2 | 135 | |
RobMeades | 0:44dd95724bc2 | 136 | // How long to wait for stuff to travel in the async tests |
RobMeades | 0:44dd95724bc2 | 137 | #define ASYNC_TEST_WAIT_TIME 10000 |
RobMeades | 0:44dd95724bc2 | 138 | |
RobMeades | 0:44dd95724bc2 | 139 | // ---------------------------------------------------------------- |
RobMeades | 0:44dd95724bc2 | 140 | // PRIVATE VARIABLES |
RobMeades | 0:44dd95724bc2 | 141 | // ---------------------------------------------------------------- |
RobMeades | 0:44dd95724bc2 | 142 | |
RobMeades | 0:44dd95724bc2 | 143 | #ifdef FEATURE_COMMON_PAL |
RobMeades | 0:44dd95724bc2 | 144 | // Lock for debug prints |
RobMeades | 0:44dd95724bc2 | 145 | static Mutex mtx; |
RobMeades | 0:44dd95724bc2 | 146 | #endif |
RobMeades | 0:44dd95724bc2 | 147 | |
RobMeades | 0:44dd95724bc2 | 148 | // An instance of the cellular interface |
RobMeades | 0:44dd95724bc2 | 149 | static UbloxPPPCellularInterface *interface = new UbloxPPPCellularInterface(MDMTXD, |
RobMeades | 0:44dd95724bc2 | 150 | MDMRXD, |
RobMeades | 0:44dd95724bc2 | 151 | MBED_CONF_UBLOX_CELL_BAUD_RATE, |
RobMeades | 0:44dd95724bc2 | 152 | MBED_CONF_APP_DEBUG_ON); |
RobMeades | 0:44dd95724bc2 | 153 | |
RobMeades | 0:44dd95724bc2 | 154 | // Connection flag |
RobMeades | 0:44dd95724bc2 | 155 | static bool connection_has_gone_down = false; |
RobMeades | 0:44dd95724bc2 | 156 | |
RobMeades | 0:44dd95724bc2 | 157 | static const char send_data[] = "_____0000:0123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 158 | "01234567890123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 159 | "_____0100:0123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 160 | "01234567890123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 161 | "_____0200:0123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 162 | "01234567890123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 163 | "_____0300:0123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 164 | "01234567890123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 165 | "_____0400:0123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 166 | "01234567890123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 167 | "_____0500:0123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 168 | "01234567890123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 169 | "_____0600:0123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 170 | "01234567890123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 171 | "_____0700:0123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 172 | "01234567890123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 173 | "_____0800:0123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 174 | "01234567890123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 175 | "_____0900:0123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 176 | "01234567890123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 177 | "_____1000:0123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 178 | "01234567890123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 179 | "_____1100:0123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 180 | "01234567890123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 181 | "_____1200:0123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 182 | "01234567890123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 183 | "_____1300:0123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 184 | "01234567890123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 185 | "_____1400:0123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 186 | "01234567890123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 187 | "_____1500:0123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 188 | "01234567890123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 189 | "_____1600:0123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 190 | "01234567890123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 191 | "_____1700:0123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 192 | "01234567890123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 193 | "_____1800:0123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 194 | "01234567890123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 195 | "_____1900:0123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 196 | "01234567890123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 197 | "_____2000:0123456789012345678901234567890123456789" |
RobMeades | 0:44dd95724bc2 | 198 | "01234567890123456789012345678901234567890123456789"; |
RobMeades | 0:44dd95724bc2 | 199 | |
RobMeades | 0:44dd95724bc2 | 200 | // ---------------------------------------------------------------- |
RobMeades | 0:44dd95724bc2 | 201 | // PRIVATE FUNCTIONS |
RobMeades | 0:44dd95724bc2 | 202 | // ---------------------------------------------------------------- |
RobMeades | 0:44dd95724bc2 | 203 | |
RobMeades | 0:44dd95724bc2 | 204 | #ifdef FEATURE_COMMON_PAL |
RobMeades | 0:44dd95724bc2 | 205 | // Locks for debug prints |
RobMeades | 0:44dd95724bc2 | 206 | static void lock() |
RobMeades | 0:44dd95724bc2 | 207 | { |
RobMeades | 0:44dd95724bc2 | 208 | mtx.lock(); |
RobMeades | 0:44dd95724bc2 | 209 | } |
RobMeades | 0:44dd95724bc2 | 210 | |
RobMeades | 0:44dd95724bc2 | 211 | static void unlock() |
RobMeades | 0:44dd95724bc2 | 212 | { |
RobMeades | 0:44dd95724bc2 | 213 | mtx.unlock(); |
RobMeades | 0:44dd95724bc2 | 214 | } |
RobMeades | 0:44dd95724bc2 | 215 | #endif |
RobMeades | 0:44dd95724bc2 | 216 | |
RobMeades | 0:44dd95724bc2 | 217 | // Callback in case the connection goes down |
RobMeades | 3:9863dcade75d | 218 | void connection_down_cb(nsapi_event_t event, intptr_t x) |
RobMeades | 0:44dd95724bc2 | 219 | { |
RobMeades | 3:9863dcade75d | 220 | if ((nsapi_connection_status_t) x == NSAPI_STATUS_DISCONNECTED) { |
RobMeades | 3:9863dcade75d | 221 | connection_has_gone_down = true; |
RobMeades | 3:9863dcade75d | 222 | } |
RobMeades | 0:44dd95724bc2 | 223 | } |
RobMeades | 0:44dd95724bc2 | 224 | |
RobMeades | 0:44dd95724bc2 | 225 | #ifdef MBED_CONF_APP_ECHO_SERVER |
RobMeades | 0:44dd95724bc2 | 226 | // Make sure that size is greater than 0 and no more than limit, |
RobMeades | 0:44dd95724bc2 | 227 | // useful since, when moduloing a very large number number, |
RobMeades | 0:44dd95724bc2 | 228 | // compilers sometimes screw up and produce a small *negative* |
RobMeades | 0:44dd95724bc2 | 229 | // number. Who knew? For example, GCC decided that |
RobMeades | 0:44dd95724bc2 | 230 | // 492318453 (0x1d582ef5) modulo 508 was -47 (0xffffffd1). |
RobMeades | 0:44dd95724bc2 | 231 | static int fix (int size, int limit) |
RobMeades | 0:44dd95724bc2 | 232 | { |
RobMeades | 0:44dd95724bc2 | 233 | if (size <= 0) { |
RobMeades | 0:44dd95724bc2 | 234 | size = limit / 2; // better than 1 |
RobMeades | 0:44dd95724bc2 | 235 | } else if (size > limit) { |
RobMeades | 0:44dd95724bc2 | 236 | size = limit; |
RobMeades | 0:44dd95724bc2 | 237 | } |
RobMeades | 0:44dd95724bc2 | 238 | return size; |
RobMeades | 0:44dd95724bc2 | 239 | } |
RobMeades | 0:44dd95724bc2 | 240 | |
RobMeades | 0:44dd95724bc2 | 241 | // Do a UDP socket echo test to a given host of a given packet size |
RobMeades | 0:44dd95724bc2 | 242 | static void do_udp_echo(UDPSocket *sock, SocketAddress *host_address, int size) |
RobMeades | 0:44dd95724bc2 | 243 | { |
RobMeades | 0:44dd95724bc2 | 244 | bool success = false; |
RobMeades | 0:44dd95724bc2 | 245 | void * recv_data = malloc (size); |
RobMeades | 0:44dd95724bc2 | 246 | TEST_ASSERT(recv_data != NULL); |
RobMeades | 0:44dd95724bc2 | 247 | |
RobMeades | 0:44dd95724bc2 | 248 | // Retry this a few times, don't want to fail due to a flaky link |
RobMeades | 0:44dd95724bc2 | 249 | for (int x = 0; !success && (x < NUM_UDP_RETRIES); x++) { |
RobMeades | 0:44dd95724bc2 | 250 | tr_debug("Echo testing UDP packet size %d byte(s), try %d.", size, x + 1); |
RobMeades | 0:44dd95724bc2 | 251 | if ((sock->sendto(*host_address, (void*) send_data, size) == size) && |
RobMeades | 0:44dd95724bc2 | 252 | (sock->recvfrom(host_address, recv_data, size) == size)) { |
RobMeades | 0:44dd95724bc2 | 253 | TEST_ASSERT (memcmp(send_data, recv_data, size) == 0); |
RobMeades | 0:44dd95724bc2 | 254 | success = true; |
RobMeades | 0:44dd95724bc2 | 255 | } |
RobMeades | 0:44dd95724bc2 | 256 | } |
RobMeades | 0:44dd95724bc2 | 257 | TEST_ASSERT (success); |
RobMeades | 0:44dd95724bc2 | 258 | TEST_ASSERT(!connection_has_gone_down); |
RobMeades | 0:44dd95724bc2 | 259 | |
RobMeades | 0:44dd95724bc2 | 260 | free (recv_data); |
RobMeades | 0:44dd95724bc2 | 261 | } |
RobMeades | 0:44dd95724bc2 | 262 | |
RobMeades | 0:44dd95724bc2 | 263 | // Send an entire TCP data buffer until done |
RobMeades | 0:44dd95724bc2 | 264 | static int sendAll(TCPSocket *sock, const char *data, int size) |
RobMeades | 0:44dd95724bc2 | 265 | { |
RobMeades | 0:44dd95724bc2 | 266 | int x; |
RobMeades | 0:44dd95724bc2 | 267 | int count = 0; |
RobMeades | 0:44dd95724bc2 | 268 | Timer timer; |
RobMeades | 0:44dd95724bc2 | 269 | |
RobMeades | 0:44dd95724bc2 | 270 | timer.start(); |
RobMeades | 0:44dd95724bc2 | 271 | while ((count < size) && (timer.read_ms() < ASYNC_TEST_WAIT_TIME)) { |
RobMeades | 0:44dd95724bc2 | 272 | x = sock->send(data + count, size - count); |
RobMeades | 0:44dd95724bc2 | 273 | if (x > 0) { |
RobMeades | 0:44dd95724bc2 | 274 | count += x; |
RobMeades | 0:44dd95724bc2 | 275 | tr_debug("%d byte(s) sent, %d left to send.", count, size - count); |
RobMeades | 0:44dd95724bc2 | 276 | } |
RobMeades | 0:44dd95724bc2 | 277 | wait_ms(10); |
RobMeades | 0:44dd95724bc2 | 278 | } |
RobMeades | 0:44dd95724bc2 | 279 | timer.stop(); |
RobMeades | 0:44dd95724bc2 | 280 | |
RobMeades | 0:44dd95724bc2 | 281 | return count; |
RobMeades | 0:44dd95724bc2 | 282 | } |
RobMeades | 0:44dd95724bc2 | 283 | |
RobMeades | 0:44dd95724bc2 | 284 | // The asynchronous callback |
RobMeades | 0:44dd95724bc2 | 285 | static void async_cb(bool *callback_triggered) |
RobMeades | 0:44dd95724bc2 | 286 | { |
RobMeades | 0:44dd95724bc2 | 287 | |
RobMeades | 0:44dd95724bc2 | 288 | TEST_ASSERT (callback_triggered != NULL); |
RobMeades | 0:44dd95724bc2 | 289 | *callback_triggered = true; |
RobMeades | 0:44dd95724bc2 | 290 | } |
RobMeades | 0:44dd95724bc2 | 291 | |
RobMeades | 0:44dd95724bc2 | 292 | // Do a TCP echo using the asynchronous interface |
RobMeades | 0:44dd95724bc2 | 293 | static void do_tcp_echo_async(TCPSocket *sock, int size, bool *callback_triggered) |
RobMeades | 0:44dd95724bc2 | 294 | { |
RobMeades | 0:44dd95724bc2 | 295 | void * recv_data = malloc (size); |
RobMeades | 0:44dd95724bc2 | 296 | int recv_size = 0; |
RobMeades | 0:44dd95724bc2 | 297 | int x, y; |
RobMeades | 0:44dd95724bc2 | 298 | Timer timer; |
RobMeades | 0:44dd95724bc2 | 299 | TEST_ASSERT(recv_data != NULL); |
RobMeades | 0:44dd95724bc2 | 300 | |
RobMeades | 0:44dd95724bc2 | 301 | *callback_triggered = false; |
RobMeades | 0:44dd95724bc2 | 302 | tr_debug("Echo testing TCP packet size %d byte(s) async.", size); |
RobMeades | 0:44dd95724bc2 | 303 | TEST_ASSERT (sendAll(sock, send_data, size) == size); |
RobMeades | 0:44dd95724bc2 | 304 | // Wait for all the echoed data to arrive |
RobMeades | 0:44dd95724bc2 | 305 | timer.start(); |
RobMeades | 0:44dd95724bc2 | 306 | while ((recv_size < size) && (timer.read_ms() < ASYNC_TEST_WAIT_TIME)) { |
RobMeades | 0:44dd95724bc2 | 307 | if (*callback_triggered) { |
RobMeades | 0:44dd95724bc2 | 308 | *callback_triggered = false; |
RobMeades | 0:44dd95724bc2 | 309 | x = sock->recv((char *) recv_data + recv_size, size); |
RobMeades | 0:44dd95724bc2 | 310 | // IMPORTANT: this is different to the version in the AT DATA tests |
RobMeades | 0:44dd95724bc2 | 311 | // In the AT DATA case we know that the only reason the callback |
RobMeades | 0:44dd95724bc2 | 312 | // will be triggered is if there is received data. In the case |
RobMeades | 0:44dd95724bc2 | 313 | // of calling the LWIP implementation other things can also trigger |
RobMeades | 0:44dd95724bc2 | 314 | // it, so don't rely on there being any bytes to receive. |
RobMeades | 0:44dd95724bc2 | 315 | if (x > 0) { |
RobMeades | 0:44dd95724bc2 | 316 | recv_size += x; |
RobMeades | 0:44dd95724bc2 | 317 | tr_debug("%d byte(s) echoed back so far.", recv_size); |
RobMeades | 0:44dd95724bc2 | 318 | } |
RobMeades | 0:44dd95724bc2 | 319 | } |
RobMeades | 0:44dd95724bc2 | 320 | wait_ms(10); |
RobMeades | 0:44dd95724bc2 | 321 | } |
RobMeades | 0:44dd95724bc2 | 322 | TEST_ASSERT(recv_size == size); |
RobMeades | 0:44dd95724bc2 | 323 | y = memcmp(send_data, recv_data, size); |
RobMeades | 0:44dd95724bc2 | 324 | if (y != 0) { |
RobMeades | 0:44dd95724bc2 | 325 | tr_debug("Sent %d, |%*.*s|", size, size, size, send_data); |
RobMeades | 0:44dd95724bc2 | 326 | tr_debug("Rcvd %d, |%*.*s|", size, size, size, (char *) recv_data); |
RobMeades | 0:44dd95724bc2 | 327 | TEST_ASSERT(false); |
RobMeades | 0:44dd95724bc2 | 328 | } |
RobMeades | 0:44dd95724bc2 | 329 | timer.stop(); |
RobMeades | 0:44dd95724bc2 | 330 | |
RobMeades | 0:44dd95724bc2 | 331 | TEST_ASSERT(!connection_has_gone_down); |
RobMeades | 0:44dd95724bc2 | 332 | |
RobMeades | 0:44dd95724bc2 | 333 | free (recv_data); |
RobMeades | 0:44dd95724bc2 | 334 | } |
RobMeades | 0:44dd95724bc2 | 335 | #endif |
RobMeades | 0:44dd95724bc2 | 336 | |
RobMeades | 0:44dd95724bc2 | 337 | // Get NTP time |
RobMeades | 0:44dd95724bc2 | 338 | static void do_ntp(UbloxPPPCellularInterface *interface) |
RobMeades | 0:44dd95724bc2 | 339 | { |
RobMeades | 0:44dd95724bc2 | 340 | char ntp_values[48] = { 0 }; |
RobMeades | 0:44dd95724bc2 | 341 | time_t timestamp = 0; |
RobMeades | 0:44dd95724bc2 | 342 | struct tm *localTime; |
RobMeades | 0:44dd95724bc2 | 343 | char timeString[25]; |
RobMeades | 0:44dd95724bc2 | 344 | time_t TIME1970 = 2208988800U; |
RobMeades | 0:44dd95724bc2 | 345 | int len; |
rob.meades@u-blox.com | 1:80ec3fccad9e | 346 | UDPSocket sock; |
rob.meades@u-blox.com | 1:80ec3fccad9e | 347 | SocketAddress ntp_address; |
RobMeades | 0:44dd95724bc2 | 348 | bool comms_done = false; |
RobMeades | 0:44dd95724bc2 | 349 | |
RobMeades | 0:44dd95724bc2 | 350 | ntp_values[0] = '\x1b'; |
RobMeades | 0:44dd95724bc2 | 351 | |
rob.meades@u-blox.com | 1:80ec3fccad9e | 352 | TEST_ASSERT(sock.open(interface) == 0) |
rob.meades@u-blox.com | 1:80ec3fccad9e | 353 | |
rob.meades@u-blox.com | 1:80ec3fccad9e | 354 | TEST_ASSERT(interface->gethostbyname(MBED_CONF_APP_NTP_SERVER, &ntp_address) == 0); |
rob.meades@u-blox.com | 1:80ec3fccad9e | 355 | ntp_address.set_port(MBED_CONF_APP_NTP_PORT); |
rob.meades@u-blox.com | 1:80ec3fccad9e | 356 | |
rob.meades@u-blox.com | 1:80ec3fccad9e | 357 | tr_debug("UDP: NIST server %s address: %s on port %d.", MBED_CONF_APP_NTP_SERVER, |
rob.meades@u-blox.com | 1:80ec3fccad9e | 358 | ntp_address.get_ip_address(), ntp_address.get_port()); |
rob.meades@u-blox.com | 1:80ec3fccad9e | 359 | |
rob.meades@u-blox.com | 1:80ec3fccad9e | 360 | sock.set_timeout(10000); |
rob.meades@u-blox.com | 1:80ec3fccad9e | 361 | |
RobMeades | 0:44dd95724bc2 | 362 | // Retry this a few times, don't want to fail due to a flaky link |
RobMeades | 0:44dd95724bc2 | 363 | for (unsigned int x = 0; !comms_done && (x < NUM_UDP_RETRIES); x++) { |
rob.meades@u-blox.com | 1:80ec3fccad9e | 364 | sock.sendto(ntp_address, (void*) ntp_values, sizeof(ntp_values)); |
rob.meades@u-blox.com | 1:80ec3fccad9e | 365 | len = sock.recvfrom(&ntp_address, (void*) ntp_values, sizeof(ntp_values)); |
RobMeades | 0:44dd95724bc2 | 366 | if (len > 0) { |
RobMeades | 0:44dd95724bc2 | 367 | comms_done = true; |
RobMeades | 0:44dd95724bc2 | 368 | } |
RobMeades | 0:44dd95724bc2 | 369 | } |
RobMeades | 0:44dd95724bc2 | 370 | TEST_ASSERT (comms_done); |
rob.meades@u-blox.com | 1:80ec3fccad9e | 371 | |
rob.meades@u-blox.com | 1:80ec3fccad9e | 372 | sock.close(); |
rob.meades@u-blox.com | 1:80ec3fccad9e | 373 | |
RobMeades | 0:44dd95724bc2 | 374 | tr_debug("UDP: %d byte(s) returned by NTP server.", len); |
RobMeades | 0:44dd95724bc2 | 375 | if (len >= 43) { |
RobMeades | 0:44dd95724bc2 | 376 | timestamp |= ((int) *(ntp_values + 40)) << 24; |
RobMeades | 0:44dd95724bc2 | 377 | timestamp |= ((int) *(ntp_values + 41)) << 16; |
RobMeades | 0:44dd95724bc2 | 378 | timestamp |= ((int) *(ntp_values + 42)) << 8; |
RobMeades | 0:44dd95724bc2 | 379 | timestamp |= ((int) *(ntp_values + 43)); |
RobMeades | 0:44dd95724bc2 | 380 | timestamp -= TIME1970; |
RobMeades | 0:44dd95724bc2 | 381 | srand (timestamp); |
RobMeades | 0:44dd95724bc2 | 382 | tr_debug("srand() called"); |
RobMeades | 0:44dd95724bc2 | 383 | localTime = localtime(×tamp); |
RobMeades | 0:44dd95724bc2 | 384 | if (localTime) { |
RobMeades | 0:44dd95724bc2 | 385 | if (strftime(timeString, sizeof(timeString), "%a %b %d %H:%M:%S %Y", localTime) > 0) { |
RobMeades | 0:44dd95724bc2 | 386 | printf("NTP timestamp is %s.\n", timeString); |
RobMeades | 0:44dd95724bc2 | 387 | } |
RobMeades | 0:44dd95724bc2 | 388 | } |
RobMeades | 0:44dd95724bc2 | 389 | } |
RobMeades | 0:44dd95724bc2 | 390 | } |
RobMeades | 0:44dd95724bc2 | 391 | |
RobMeades | 0:44dd95724bc2 | 392 | // Use a connection, checking that it is good |
RobMeades | 0:44dd95724bc2 | 393 | static void use_connection(UbloxPPPCellularInterface *interface) |
RobMeades | 0:44dd95724bc2 | 394 | { |
RobMeades | 0:44dd95724bc2 | 395 | const char * ip_address = interface->get_ip_address(); |
RobMeades | 0:44dd95724bc2 | 396 | const char * net_mask = interface->get_netmask(); |
RobMeades | 0:44dd95724bc2 | 397 | const char * gateway = interface->get_gateway(); |
RobMeades | 0:44dd95724bc2 | 398 | |
RobMeades | 0:44dd95724bc2 | 399 | TEST_ASSERT(interface->is_connected()); |
RobMeades | 0:44dd95724bc2 | 400 | |
RobMeades | 0:44dd95724bc2 | 401 | TEST_ASSERT(ip_address != NULL); |
RobMeades | 0:44dd95724bc2 | 402 | tr_debug ("IP address %s.", ip_address); |
RobMeades | 0:44dd95724bc2 | 403 | TEST_ASSERT(net_mask != NULL); |
RobMeades | 0:44dd95724bc2 | 404 | tr_debug ("Net mask %s.", net_mask); |
RobMeades | 0:44dd95724bc2 | 405 | TEST_ASSERT(gateway != NULL); |
RobMeades | 0:44dd95724bc2 | 406 | tr_debug ("Gateway %s.", gateway); |
RobMeades | 0:44dd95724bc2 | 407 | |
RobMeades | 0:44dd95724bc2 | 408 | do_ntp(interface); |
RobMeades | 0:44dd95724bc2 | 409 | TEST_ASSERT(!connection_has_gone_down); |
RobMeades | 0:44dd95724bc2 | 410 | } |
RobMeades | 0:44dd95724bc2 | 411 | |
RobMeades | 0:44dd95724bc2 | 412 | // Drop a connection and check that it has dropped |
RobMeades | 0:44dd95724bc2 | 413 | static void drop_connection(UbloxPPPCellularInterface *interface) |
RobMeades | 0:44dd95724bc2 | 414 | { |
RobMeades | 0:44dd95724bc2 | 415 | TEST_ASSERT(interface->disconnect() == 0); |
RobMeades | 0:44dd95724bc2 | 416 | TEST_ASSERT(connection_has_gone_down); |
RobMeades | 0:44dd95724bc2 | 417 | connection_has_gone_down = false; |
RobMeades | 0:44dd95724bc2 | 418 | TEST_ASSERT(!interface->is_connected()); |
RobMeades | 0:44dd95724bc2 | 419 | } |
RobMeades | 0:44dd95724bc2 | 420 | |
RobMeades | 0:44dd95724bc2 | 421 | // ---------------------------------------------------------------- |
RobMeades | 0:44dd95724bc2 | 422 | // TESTS |
RobMeades | 0:44dd95724bc2 | 423 | // ---------------------------------------------------------------- |
RobMeades | 0:44dd95724bc2 | 424 | |
RobMeades | 0:44dd95724bc2 | 425 | // Call srand() using the NTP server |
RobMeades | 0:44dd95724bc2 | 426 | void test_set_randomise() { |
RobMeades | 0:44dd95724bc2 | 427 | UDPSocket sock; |
RobMeades | 0:44dd95724bc2 | 428 | SocketAddress host_address; |
RobMeades | 0:44dd95724bc2 | 429 | |
RobMeades | 0:44dd95724bc2 | 430 | TEST_ASSERT(interface->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN, |
RobMeades | 0:44dd95724bc2 | 431 | MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0); |
RobMeades | 0:44dd95724bc2 | 432 | do_ntp(interface); |
RobMeades | 0:44dd95724bc2 | 433 | TEST_ASSERT(!connection_has_gone_down); |
RobMeades | 0:44dd95724bc2 | 434 | drop_connection(interface); |
RobMeades | 0:44dd95724bc2 | 435 | } |
RobMeades | 0:44dd95724bc2 | 436 | |
RobMeades | 0:44dd95724bc2 | 437 | #ifdef MBED_CONF_APP_ECHO_SERVER |
RobMeades | 0:44dd95724bc2 | 438 | // Test UDP data exchange |
RobMeades | 0:44dd95724bc2 | 439 | void test_udp_echo() { |
RobMeades | 0:44dd95724bc2 | 440 | UDPSocket sock; |
RobMeades | 0:44dd95724bc2 | 441 | SocketAddress host_address; |
RobMeades | 0:44dd95724bc2 | 442 | int x; |
RobMeades | 0:44dd95724bc2 | 443 | int size; |
RobMeades | 0:44dd95724bc2 | 444 | |
RobMeades | 0:44dd95724bc2 | 445 | interface->deinit(); |
RobMeades | 0:44dd95724bc2 | 446 | TEST_ASSERT(interface->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN, |
RobMeades | 0:44dd95724bc2 | 447 | MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0); |
RobMeades | 0:44dd95724bc2 | 448 | |
RobMeades | 0:44dd95724bc2 | 449 | TEST_ASSERT(interface->gethostbyname(MBED_CONF_APP_ECHO_SERVER, &host_address) == 0); |
RobMeades | 0:44dd95724bc2 | 450 | host_address.set_port(MBED_CONF_APP_ECHO_UDP_PORT); |
RobMeades | 0:44dd95724bc2 | 451 | |
RobMeades | 0:44dd95724bc2 | 452 | tr_debug("UDP: Server %s address: %s on port %d.", MBED_CONF_APP_ECHO_SERVER, |
RobMeades | 0:44dd95724bc2 | 453 | host_address.get_ip_address(), host_address.get_port()); |
RobMeades | 0:44dd95724bc2 | 454 | |
RobMeades | 0:44dd95724bc2 | 455 | TEST_ASSERT(sock.open(interface) == 0) |
RobMeades | 0:44dd95724bc2 | 456 | |
RobMeades | 0:44dd95724bc2 | 457 | sock.set_timeout(10000); |
RobMeades | 0:44dd95724bc2 | 458 | |
RobMeades | 0:44dd95724bc2 | 459 | // Test min, max, and some random sizes in-between |
RobMeades | 0:44dd95724bc2 | 460 | do_udp_echo(&sock, &host_address, 1); |
RobMeades | 0:44dd95724bc2 | 461 | do_udp_echo(&sock, &host_address, MBED_CONF_APP_UDP_MAX_PACKET_SIZE); |
RobMeades | 0:44dd95724bc2 | 462 | for (x = 0; x < 10; x++) { |
RobMeades | 0:44dd95724bc2 | 463 | size = (rand() % MBED_CONF_APP_UDP_MAX_PACKET_SIZE) + 1; |
RobMeades | 0:44dd95724bc2 | 464 | size = fix(size, MBED_CONF_APP_UDP_MAX_PACKET_SIZE); |
RobMeades | 0:44dd95724bc2 | 465 | do_udp_echo(&sock, &host_address, size); |
RobMeades | 0:44dd95724bc2 | 466 | } |
RobMeades | 0:44dd95724bc2 | 467 | |
RobMeades | 0:44dd95724bc2 | 468 | sock.close(); |
RobMeades | 0:44dd95724bc2 | 469 | |
RobMeades | 0:44dd95724bc2 | 470 | drop_connection(interface); |
RobMeades | 0:44dd95724bc2 | 471 | |
RobMeades | 0:44dd95724bc2 | 472 | tr_debug("%d UDP packets of size up to %d byte(s) echoed successfully.", x, |
RobMeades | 0:44dd95724bc2 | 473 | MBED_CONF_APP_UDP_MAX_PACKET_SIZE); |
RobMeades | 0:44dd95724bc2 | 474 | } |
RobMeades | 0:44dd95724bc2 | 475 | |
RobMeades | 0:44dd95724bc2 | 476 | // Test TCP data exchange via the asynchronous sigio() mechanism |
RobMeades | 0:44dd95724bc2 | 477 | void test_tcp_echo_async() { |
RobMeades | 0:44dd95724bc2 | 478 | TCPSocket sock; |
RobMeades | 0:44dd95724bc2 | 479 | SocketAddress host_address; |
RobMeades | 0:44dd95724bc2 | 480 | bool callback_triggered = false; |
RobMeades | 0:44dd95724bc2 | 481 | int x; |
RobMeades | 0:44dd95724bc2 | 482 | int size; |
RobMeades | 0:44dd95724bc2 | 483 | |
RobMeades | 0:44dd95724bc2 | 484 | interface->deinit(); |
RobMeades | 0:44dd95724bc2 | 485 | TEST_ASSERT(interface->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN, MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0); |
RobMeades | 0:44dd95724bc2 | 486 | |
RobMeades | 0:44dd95724bc2 | 487 | TEST_ASSERT(interface->gethostbyname(MBED_CONF_APP_ECHO_SERVER, &host_address) == 0); |
RobMeades | 0:44dd95724bc2 | 488 | host_address.set_port(MBED_CONF_APP_ECHO_TCP_PORT); |
RobMeades | 0:44dd95724bc2 | 489 | |
RobMeades | 0:44dd95724bc2 | 490 | tr_debug("TCP: Server %s address: %s on port %d.", MBED_CONF_APP_ECHO_SERVER, |
RobMeades | 0:44dd95724bc2 | 491 | host_address.get_ip_address(), host_address.get_port()); |
RobMeades | 0:44dd95724bc2 | 492 | |
RobMeades | 0:44dd95724bc2 | 493 | TEST_ASSERT(sock.open(interface) == 0) |
RobMeades | 0:44dd95724bc2 | 494 | |
RobMeades | 0:44dd95724bc2 | 495 | // Set up the async callback and set the timeout to zero |
RobMeades | 0:44dd95724bc2 | 496 | sock.sigio(callback(async_cb, &callback_triggered)); |
RobMeades | 0:44dd95724bc2 | 497 | sock.set_timeout(0); |
RobMeades | 0:44dd95724bc2 | 498 | |
RobMeades | 0:44dd95724bc2 | 499 | TEST_ASSERT(sock.connect(host_address) == 0); |
RobMeades | 0:44dd95724bc2 | 500 | // Test min, max, and some random sizes in-between |
RobMeades | 0:44dd95724bc2 | 501 | do_tcp_echo_async(&sock, 1, &callback_triggered); |
RobMeades | 0:44dd95724bc2 | 502 | do_tcp_echo_async(&sock, MBED_CONF_APP_TCP_MAX_PACKET_SIZE, &callback_triggered); |
RobMeades | 0:44dd95724bc2 | 503 | for (x = 0; x < 10; x++) { |
RobMeades | 0:44dd95724bc2 | 504 | size = (rand() % MBED_CONF_APP_TCP_MAX_PACKET_SIZE) + 1; |
RobMeades | 0:44dd95724bc2 | 505 | size = fix(size, MBED_CONF_APP_TCP_MAX_PACKET_SIZE); |
RobMeades | 0:44dd95724bc2 | 506 | do_tcp_echo_async(&sock, size, &callback_triggered); |
RobMeades | 0:44dd95724bc2 | 507 | } |
RobMeades | 0:44dd95724bc2 | 508 | |
RobMeades | 0:44dd95724bc2 | 509 | sock.close(); |
RobMeades | 0:44dd95724bc2 | 510 | |
RobMeades | 0:44dd95724bc2 | 511 | drop_connection(interface); |
RobMeades | 0:44dd95724bc2 | 512 | |
RobMeades | 0:44dd95724bc2 | 513 | tr_debug("%d TCP packets of size up to %d byte(s) echoed asynchronously and successfully.", |
RobMeades | 0:44dd95724bc2 | 514 | x, MBED_CONF_APP_TCP_MAX_PACKET_SIZE); |
RobMeades | 0:44dd95724bc2 | 515 | } |
RobMeades | 0:44dd95724bc2 | 516 | #endif |
RobMeades | 0:44dd95724bc2 | 517 | |
RobMeades | 0:44dd95724bc2 | 518 | // Connect with credentials included in the connect request |
RobMeades | 0:44dd95724bc2 | 519 | void test_connect_credentials() { |
RobMeades | 0:44dd95724bc2 | 520 | |
RobMeades | 0:44dd95724bc2 | 521 | interface->deinit(); |
RobMeades | 0:44dd95724bc2 | 522 | TEST_ASSERT(interface->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN, |
RobMeades | 0:44dd95724bc2 | 523 | MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0); |
RobMeades | 0:44dd95724bc2 | 524 | use_connection(interface); |
RobMeades | 0:44dd95724bc2 | 525 | drop_connection(interface); |
RobMeades | 0:44dd95724bc2 | 526 | } |
RobMeades | 0:44dd95724bc2 | 527 | |
RobMeades | 0:44dd95724bc2 | 528 | // Test with credentials preset |
RobMeades | 0:44dd95724bc2 | 529 | void test_connect_preset_credentials() { |
RobMeades | 0:44dd95724bc2 | 530 | |
RobMeades | 0:44dd95724bc2 | 531 | interface->deinit(); |
RobMeades | 0:44dd95724bc2 | 532 | TEST_ASSERT(interface->init(MBED_CONF_APP_DEFAULT_PIN)); |
RobMeades | 0:44dd95724bc2 | 533 | interface->set_credentials(MBED_CONF_APP_APN, MBED_CONF_APP_USERNAME, |
RobMeades | 0:44dd95724bc2 | 534 | MBED_CONF_APP_PASSWORD); |
RobMeades | 0:44dd95724bc2 | 535 | TEST_ASSERT(interface->connect(MBED_CONF_APP_DEFAULT_PIN) == 0); |
RobMeades | 0:44dd95724bc2 | 536 | use_connection(interface); |
RobMeades | 0:44dd95724bc2 | 537 | drop_connection(interface); |
RobMeades | 0:44dd95724bc2 | 538 | } |
RobMeades | 0:44dd95724bc2 | 539 | |
RobMeades | 0:44dd95724bc2 | 540 | // Test adding and using a SIM pin, then removing it, using the pending |
RobMeades | 0:44dd95724bc2 | 541 | // mechanism where the change doesn't occur until connect() is called |
RobMeades | 0:44dd95724bc2 | 542 | void test_check_sim_pin_pending() { |
RobMeades | 0:44dd95724bc2 | 543 | |
RobMeades | 0:44dd95724bc2 | 544 | interface->deinit(); |
RobMeades | 0:44dd95724bc2 | 545 | |
RobMeades | 0:44dd95724bc2 | 546 | // Enable PIN checking (which will use the current PIN) |
RobMeades | 0:44dd95724bc2 | 547 | // and also flag that the PIN should be changed to MBED_CONF_APP_ALT_PIN, |
RobMeades | 0:44dd95724bc2 | 548 | // then try connecting |
RobMeades | 0:44dd95724bc2 | 549 | interface->sim_pin_check_enable(true); |
RobMeades | 0:44dd95724bc2 | 550 | interface->change_sim_pin(MBED_CONF_APP_ALT_PIN); |
RobMeades | 0:44dd95724bc2 | 551 | TEST_ASSERT(interface->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN, |
RobMeades | 0:44dd95724bc2 | 552 | MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0); |
RobMeades | 0:44dd95724bc2 | 553 | use_connection(interface); |
RobMeades | 0:44dd95724bc2 | 554 | drop_connection(interface); |
RobMeades | 0:44dd95724bc2 | 555 | interface->deinit(); |
RobMeades | 0:44dd95724bc2 | 556 | |
RobMeades | 0:44dd95724bc2 | 557 | // Now change the PIN back to what it was before |
RobMeades | 0:44dd95724bc2 | 558 | interface->change_sim_pin(MBED_CONF_APP_DEFAULT_PIN); |
RobMeades | 0:44dd95724bc2 | 559 | TEST_ASSERT(interface->connect(MBED_CONF_APP_ALT_PIN, MBED_CONF_APP_APN, |
RobMeades | 0:44dd95724bc2 | 560 | MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0); |
RobMeades | 0:44dd95724bc2 | 561 | use_connection(interface); |
RobMeades | 0:44dd95724bc2 | 562 | drop_connection(interface); |
RobMeades | 0:44dd95724bc2 | 563 | interface->deinit(); |
RobMeades | 0:44dd95724bc2 | 564 | |
RobMeades | 0:44dd95724bc2 | 565 | // Check that it was changed back, and this time |
RobMeades | 0:44dd95724bc2 | 566 | // use the other way of entering the PIN |
RobMeades | 0:44dd95724bc2 | 567 | interface->set_sim_pin(MBED_CONF_APP_DEFAULT_PIN); |
RobMeades | 0:44dd95724bc2 | 568 | TEST_ASSERT(interface->connect(NULL, MBED_CONF_APP_APN, MBED_CONF_APP_USERNAME, |
RobMeades | 0:44dd95724bc2 | 569 | MBED_CONF_APP_PASSWORD) == 0); |
RobMeades | 0:44dd95724bc2 | 570 | use_connection(interface); |
RobMeades | 0:44dd95724bc2 | 571 | drop_connection(interface); |
RobMeades | 0:44dd95724bc2 | 572 | interface->deinit(); |
RobMeades | 0:44dd95724bc2 | 573 | |
RobMeades | 0:44dd95724bc2 | 574 | // Remove PIN checking again and check that it no |
RobMeades | 0:44dd95724bc2 | 575 | // longer matters what the PIN is |
RobMeades | 0:44dd95724bc2 | 576 | interface->sim_pin_check_enable(false); |
RobMeades | 0:44dd95724bc2 | 577 | TEST_ASSERT(interface->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN, |
RobMeades | 0:44dd95724bc2 | 578 | MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0); |
RobMeades | 0:44dd95724bc2 | 579 | use_connection(interface); |
RobMeades | 0:44dd95724bc2 | 580 | drop_connection(interface); |
RobMeades | 0:44dd95724bc2 | 581 | interface->deinit(); |
RobMeades | 0:44dd95724bc2 | 582 | TEST_ASSERT(interface->init(NULL)); |
RobMeades | 0:44dd95724bc2 | 583 | TEST_ASSERT(interface->connect(MBED_CONF_APP_INCORRECT_PIN, MBED_CONF_APP_APN, |
RobMeades | 0:44dd95724bc2 | 584 | MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0); |
RobMeades | 0:44dd95724bc2 | 585 | use_connection(interface); |
RobMeades | 0:44dd95724bc2 | 586 | drop_connection(interface); |
RobMeades | 0:44dd95724bc2 | 587 | |
RobMeades | 0:44dd95724bc2 | 588 | // Put the SIM pin back to the correct value for any subsequent tests |
RobMeades | 0:44dd95724bc2 | 589 | interface->set_sim_pin(MBED_CONF_APP_DEFAULT_PIN); |
RobMeades | 0:44dd95724bc2 | 590 | } |
RobMeades | 0:44dd95724bc2 | 591 | |
RobMeades | 0:44dd95724bc2 | 592 | // Test adding and using a SIM pin, then removing it, using the immediate |
RobMeades | 0:44dd95724bc2 | 593 | // mechanism |
RobMeades | 0:44dd95724bc2 | 594 | void test_check_sim_pin_immediate() { |
RobMeades | 0:44dd95724bc2 | 595 | |
RobMeades | 0:44dd95724bc2 | 596 | interface->deinit(); |
RobMeades | 0:44dd95724bc2 | 597 | interface->connection_status_cb(callback(connection_down_cb)); |
RobMeades | 0:44dd95724bc2 | 598 | |
RobMeades | 0:44dd95724bc2 | 599 | // Enable PIN checking (which will use the current PIN), change |
RobMeades | 0:44dd95724bc2 | 600 | // the PIN to MBED_CONF_APP_ALT_PIN, then try connecting after powering on and |
RobMeades | 0:44dd95724bc2 | 601 | // off the modem |
RobMeades | 0:44dd95724bc2 | 602 | interface->set_sim_pin_check(true, true, MBED_CONF_APP_DEFAULT_PIN); |
RobMeades | 0:44dd95724bc2 | 603 | interface->set_new_sim_pin(MBED_CONF_APP_ALT_PIN, true); |
RobMeades | 0:44dd95724bc2 | 604 | interface->deinit(); |
RobMeades | 0:44dd95724bc2 | 605 | TEST_ASSERT(interface->init(NULL)); |
RobMeades | 0:44dd95724bc2 | 606 | TEST_ASSERT(interface->connect(MBED_CONF_APP_ALT_PIN, MBED_CONF_APP_APN, |
RobMeades | 0:44dd95724bc2 | 607 | MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0); |
RobMeades | 0:44dd95724bc2 | 608 | use_connection(interface); |
RobMeades | 0:44dd95724bc2 | 609 | drop_connection(interface); |
RobMeades | 0:44dd95724bc2 | 610 | |
RobMeades | 0:44dd95724bc2 | 611 | interface->connection_status_cb(callback(connection_down_cb)); |
RobMeades | 0:44dd95724bc2 | 612 | |
RobMeades | 0:44dd95724bc2 | 613 | // Now change the PIN back to what it was before |
RobMeades | 0:44dd95724bc2 | 614 | interface->set_new_sim_pin(MBED_CONF_APP_DEFAULT_PIN, true); |
RobMeades | 0:44dd95724bc2 | 615 | interface->deinit(); |
RobMeades | 0:44dd95724bc2 | 616 | interface->set_sim_pin(MBED_CONF_APP_DEFAULT_PIN); |
RobMeades | 0:44dd95724bc2 | 617 | TEST_ASSERT(interface->init(NULL)); |
RobMeades | 0:44dd95724bc2 | 618 | TEST_ASSERT(interface->connect(NULL, MBED_CONF_APP_APN, MBED_CONF_APP_USERNAME, |
RobMeades | 0:44dd95724bc2 | 619 | MBED_CONF_APP_PASSWORD) == 0); |
RobMeades | 0:44dd95724bc2 | 620 | use_connection(interface); |
RobMeades | 0:44dd95724bc2 | 621 | drop_connection(interface); |
RobMeades | 0:44dd95724bc2 | 622 | |
RobMeades | 0:44dd95724bc2 | 623 | interface->connection_status_cb(callback(connection_down_cb)); |
RobMeades | 0:44dd95724bc2 | 624 | |
RobMeades | 0:44dd95724bc2 | 625 | // Remove PIN checking again and check that it no |
RobMeades | 0:44dd95724bc2 | 626 | // longer matters what the PIN is |
RobMeades | 0:44dd95724bc2 | 627 | interface->set_sim_pin_check(false, true); |
RobMeades | 0:44dd95724bc2 | 628 | interface->deinit(); |
RobMeades | 0:44dd95724bc2 | 629 | TEST_ASSERT(interface->init(MBED_CONF_APP_INCORRECT_PIN)); |
RobMeades | 0:44dd95724bc2 | 630 | TEST_ASSERT(interface->connect(NULL, MBED_CONF_APP_APN, MBED_CONF_APP_USERNAME, |
RobMeades | 0:44dd95724bc2 | 631 | MBED_CONF_APP_PASSWORD) == 0); |
RobMeades | 0:44dd95724bc2 | 632 | use_connection(interface); |
RobMeades | 0:44dd95724bc2 | 633 | drop_connection(interface); |
RobMeades | 0:44dd95724bc2 | 634 | |
RobMeades | 0:44dd95724bc2 | 635 | // Put the SIM pin back to the correct value for any subsequent tests |
RobMeades | 0:44dd95724bc2 | 636 | interface->set_sim_pin(MBED_CONF_APP_DEFAULT_PIN); |
RobMeades | 0:44dd95724bc2 | 637 | } |
RobMeades | 0:44dd95724bc2 | 638 | |
RobMeades | 0:44dd95724bc2 | 639 | // Test being able to connect with a local instance of the driver |
RobMeades | 0:44dd95724bc2 | 640 | // NOTE: since this local instance will fiddle with bits of HW that the |
RobMeades | 0:44dd95724bc2 | 641 | // static instance thought it owned, the static instance will no longer |
RobMeades | 0:44dd95724bc2 | 642 | // work afterwards, hence this must be run as the last test in the list |
RobMeades | 0:44dd95724bc2 | 643 | void test_connect_local_instance_last_test() { |
RobMeades | 0:44dd95724bc2 | 644 | |
RobMeades | 0:44dd95724bc2 | 645 | UbloxPPPCellularInterface *pLocalInterface = NULL; |
RobMeades | 0:44dd95724bc2 | 646 | |
RobMeades | 0:44dd95724bc2 | 647 | pLocalInterface = new UbloxPPPCellularInterface(MDMTXD, MDMRXD, |
RobMeades | 0:44dd95724bc2 | 648 | MBED_CONF_UBLOX_CELL_BAUD_RATE, |
RobMeades | 0:44dd95724bc2 | 649 | MBED_CONF_APP_DEBUG_ON); |
RobMeades | 0:44dd95724bc2 | 650 | pLocalInterface->connection_status_cb(callback(connection_down_cb)); |
RobMeades | 0:44dd95724bc2 | 651 | |
RobMeades | 0:44dd95724bc2 | 652 | TEST_ASSERT(pLocalInterface->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN, |
RobMeades | 0:44dd95724bc2 | 653 | MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0); |
RobMeades | 0:44dd95724bc2 | 654 | use_connection(pLocalInterface); |
RobMeades | 0:44dd95724bc2 | 655 | drop_connection(pLocalInterface); |
RobMeades | 0:44dd95724bc2 | 656 | delete pLocalInterface; |
RobMeades | 0:44dd95724bc2 | 657 | |
RobMeades | 0:44dd95724bc2 | 658 | pLocalInterface = new UbloxPPPCellularInterface(MDMTXD, MDMRXD, |
RobMeades | 0:44dd95724bc2 | 659 | MBED_CONF_UBLOX_CELL_BAUD_RATE, |
RobMeades | 0:44dd95724bc2 | 660 | MBED_CONF_APP_DEBUG_ON); |
RobMeades | 0:44dd95724bc2 | 661 | pLocalInterface->connection_status_cb(callback(connection_down_cb)); |
RobMeades | 0:44dd95724bc2 | 662 | |
RobMeades | 0:44dd95724bc2 | 663 | TEST_ASSERT(pLocalInterface->connect(MBED_CONF_APP_DEFAULT_PIN, MBED_CONF_APP_APN, |
RobMeades | 0:44dd95724bc2 | 664 | MBED_CONF_APP_USERNAME, MBED_CONF_APP_PASSWORD) == 0); |
RobMeades | 0:44dd95724bc2 | 665 | use_connection(pLocalInterface); |
RobMeades | 0:44dd95724bc2 | 666 | drop_connection(pLocalInterface); |
RobMeades | 0:44dd95724bc2 | 667 | delete pLocalInterface; |
RobMeades | 0:44dd95724bc2 | 668 | } |
RobMeades | 0:44dd95724bc2 | 669 | |
RobMeades | 0:44dd95724bc2 | 670 | // ---------------------------------------------------------------- |
RobMeades | 0:44dd95724bc2 | 671 | // TEST ENVIRONMENT |
RobMeades | 0:44dd95724bc2 | 672 | // ---------------------------------------------------------------- |
RobMeades | 0:44dd95724bc2 | 673 | |
RobMeades | 0:44dd95724bc2 | 674 | // Setup the test environment |
RobMeades | 0:44dd95724bc2 | 675 | utest::v1::status_t test_setup(const size_t number_of_cases) { |
RobMeades | 0:44dd95724bc2 | 676 | // Setup Greentea with a timeout |
RobMeades | 0:44dd95724bc2 | 677 | GREENTEA_SETUP(600, "default_auto"); |
RobMeades | 0:44dd95724bc2 | 678 | return verbose_test_setup_handler(number_of_cases); |
RobMeades | 0:44dd95724bc2 | 679 | } |
RobMeades | 0:44dd95724bc2 | 680 | |
RobMeades | 0:44dd95724bc2 | 681 | // IMPORTANT!!! if you make a change to the tests here you should |
RobMeades | 0:44dd95724bc2 | 682 | // check whether the same change should be made to the tests under |
rob.meades@u-blox.com | 1:80ec3fccad9e | 683 | // the AT interface. |
RobMeades | 0:44dd95724bc2 | 684 | |
RobMeades | 0:44dd95724bc2 | 685 | // Test cases |
RobMeades | 0:44dd95724bc2 | 686 | Case cases[] = { |
RobMeades | 0:44dd95724bc2 | 687 | Case("Set randomise", test_set_randomise), |
RobMeades | 0:44dd95724bc2 | 688 | #ifdef MBED_CONF_APP_ECHO_SERVER |
RobMeades | 0:44dd95724bc2 | 689 | Case("UDP echo test", test_udp_echo), |
RobMeades | 0:44dd95724bc2 | 690 | # if MBED_CONF_LWIP_TCP_ENABLED |
RobMeades | 0:44dd95724bc2 | 691 | Case("TCP async echo test", test_tcp_echo_async), |
RobMeades | 0:44dd95724bc2 | 692 | # endif |
RobMeades | 0:44dd95724bc2 | 693 | #endif |
RobMeades | 0:44dd95724bc2 | 694 | Case("Connect with credentials", test_connect_credentials), |
RobMeades | 0:44dd95724bc2 | 695 | Case("Connect with preset credentials", test_connect_preset_credentials), |
RobMeades | 0:44dd95724bc2 | 696 | #if MBED_CONF_APP_RUN_SIM_PIN_CHANGE_TESTS |
RobMeades | 0:44dd95724bc2 | 697 | Case("Check SIM pin, pending", test_check_sim_pin_pending), |
RobMeades | 0:44dd95724bc2 | 698 | Case("Check SIM pin, immediate", test_check_sim_pin_immediate), |
RobMeades | 0:44dd95724bc2 | 699 | #endif |
RobMeades | 0:44dd95724bc2 | 700 | #ifndef TARGET_UBLOX_C027 // Not enough RAM on little 'ole C027 for this |
RobMeades | 0:44dd95724bc2 | 701 | Case("Connect using local instance, must be last test", test_connect_local_instance_last_test) |
RobMeades | 0:44dd95724bc2 | 702 | #endif |
RobMeades | 0:44dd95724bc2 | 703 | }; |
RobMeades | 0:44dd95724bc2 | 704 | |
RobMeades | 0:44dd95724bc2 | 705 | Specification specification(test_setup, cases); |
RobMeades | 0:44dd95724bc2 | 706 | |
RobMeades | 0:44dd95724bc2 | 707 | // ---------------------------------------------------------------- |
RobMeades | 0:44dd95724bc2 | 708 | // MAIN |
RobMeades | 0:44dd95724bc2 | 709 | // ---------------------------------------------------------------- |
RobMeades | 0:44dd95724bc2 | 710 | |
RobMeades | 0:44dd95724bc2 | 711 | int main() { |
RobMeades | 0:44dd95724bc2 | 712 | |
RobMeades | 0:44dd95724bc2 | 713 | #ifdef FEATURE_COMMON_PAL |
RobMeades | 0:44dd95724bc2 | 714 | mbed_trace_init(); |
RobMeades | 0:44dd95724bc2 | 715 | |
RobMeades | 0:44dd95724bc2 | 716 | mbed_trace_mutex_wait_function_set(lock); |
RobMeades | 0:44dd95724bc2 | 717 | mbed_trace_mutex_release_function_set(unlock); |
RobMeades | 0:44dd95724bc2 | 718 | #endif |
RobMeades | 0:44dd95724bc2 | 719 | |
rob.meades@u-blox.com | 1:80ec3fccad9e | 720 | interface->connection_status_cb(callback(connection_down_cb)); |
rob.meades@u-blox.com | 1:80ec3fccad9e | 721 | |
RobMeades | 0:44dd95724bc2 | 722 | // Run tests |
RobMeades | 0:44dd95724bc2 | 723 | return !Harness::run(specification); |
RobMeades | 0:44dd95724bc2 | 724 | } |
RobMeades | 0:44dd95724bc2 | 725 | |
RobMeades | 0:44dd95724bc2 | 726 | #else |
RobMeades | 0:44dd95724bc2 | 727 | |
RobMeades | 0:44dd95724bc2 | 728 | // This looks a bit peculiar. These tests should only be compiled if LWIP is included and PPP support is enabled. |
RobMeades | 0:44dd95724bc2 | 729 | // The way the "mbed test" command, which parses these files, does its filtering is by looking for the pattern: |
RobMeades | 0:44dd95724bc2 | 730 | // |
RobMeades | 0:44dd95724bc2 | 731 | // #ifndef THING |
RobMeades | 0:44dd95724bc2 | 732 | // #error [NOT_SUPPORTED] this test can only be used if THING is defined |
RobMeades | 0:44dd95724bc2 | 733 | // endif |
RobMeades | 0:44dd95724bc2 | 734 | // |
RobMeades | 0:44dd95724bc2 | 735 | // So if THING is not defined, this test will not even be compiled. However, the only compilation switch which |
RobMeades | 0:44dd95724bc2 | 736 | // is visible here for PPP being enabled or not is NSAPI_PPP_AVAILABLE, which is 0 if PPP is not enabled (the default) |
RobMeades | 0:44dd95724bc2 | 737 | // or 1 if PPP is enabled. There is NO visible compilation switch which is or isn't _defined_ when PPP is or |
RobMeades | 0:44dd95724bc2 | 738 | // isn't enabled. |
RobMeades | 0:44dd95724bc2 | 739 | // |
RobMeades | 0:44dd95724bc2 | 740 | // Instead, here is a dummy test body which clearly states that these tests are irrelevant 'cos there's no PPP. |
RobMeades | 0:44dd95724bc2 | 741 | |
RobMeades | 0:44dd95724bc2 | 742 | void dummy() { |
RobMeades | 0:44dd95724bc2 | 743 | } |
RobMeades | 0:44dd95724bc2 | 744 | utest::v1::status_t test_setup(const size_t number_of_cases) { |
RobMeades | 0:44dd95724bc2 | 745 | GREENTEA_SETUP(10, "default_auto"); |
RobMeades | 0:44dd95724bc2 | 746 | return verbose_test_setup_handler(number_of_cases); |
RobMeades | 0:44dd95724bc2 | 747 | } |
RobMeades | 0:44dd95724bc2 | 748 | Case cases[] = { |
RobMeades | 0:44dd95724bc2 | 749 | Case("No PPP, no tests to run", dummy) |
RobMeades | 0:44dd95724bc2 | 750 | }; |
RobMeades | 0:44dd95724bc2 | 751 | Specification specification(test_setup, cases); |
RobMeades | 0:44dd95724bc2 | 752 | |
RobMeades | 0:44dd95724bc2 | 753 | int main() { |
RobMeades | 0:44dd95724bc2 | 754 | return !Harness::run(specification);; |
RobMeades | 0:44dd95724bc2 | 755 | } |
RobMeades | 0:44dd95724bc2 | 756 | |
RobMeades | 0:44dd95724bc2 | 757 | #endif |
RobMeades | 0:44dd95724bc2 | 758 | |
RobMeades | 0:44dd95724bc2 | 759 | // End Of File |
RobMeades | 0:44dd95724bc2 | 760 |